Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FopImageConsumer.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.image;
  19. // Java
  20. import java.util.Hashtable;
  21. import java.awt.image.ColorModel;
  22. import java.awt.image.ImageConsumer;
  23. import java.awt.image.ImageProducer;
  24. import java.awt.image.PixelGrabber;
  25. /**
  26. * ImageConsumer implementation for FopImage classes.
  27. */
  28. public class FopImageConsumer implements ImageConsumer {
  29. /** Image width in pixels */
  30. protected int width = -1;
  31. /** Image height in pixels */
  32. protected int height = -1;
  33. /** Image status */
  34. protected Integer imageStatus = new Integer(-1);
  35. /** hints */
  36. protected int hints = 0;
  37. /** Image properties */
  38. protected Hashtable properties = null;
  39. /** Color model */
  40. protected ColorModel cm = null;
  41. /** Image producer */
  42. protected ImageProducer ip = null;
  43. /**
  44. * Main constructor
  45. * @param iprod ImageProducer to use
  46. */
  47. public FopImageConsumer(ImageProducer iprod) {
  48. this.ip = iprod;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public void imageComplete(int status) {
  54. /*
  55. * log.error("Status ");
  56. * if (status == ImageConsumer.COMPLETESCANLINES) {
  57. * log.error("CompleteScanLines");
  58. * } else if (status == ImageConsumer.IMAGEABORTED) {
  59. * log.error("ImageAborted");
  60. * } else if (status == ImageConsumer.IMAGEERROR) {
  61. * log.error("ImageError");
  62. * } else if (status == ImageConsumer.RANDOMPIXELORDER) {
  63. * log.error("RandomPixelOrder");
  64. * } else if (status == ImageConsumer.SINGLEFRAME) {
  65. * log.error("SingleFrame");
  66. * } else if (status == ImageConsumer.SINGLEFRAMEDONE) {
  67. * log.error("SingleFrameDone");
  68. * } else if (status == ImageConsumer.SINGLEPASS) {
  69. * log.error("SinglePass");
  70. * } else if (status == ImageConsumer.STATICIMAGEDONE) {
  71. * log.error("StaticImageDone");
  72. * } else if (status == ImageConsumer.TOPDOWNLEFTRIGHT) {
  73. * log.error("TopDownLeftRight");
  74. * }
  75. */
  76. synchronized (this.imageStatus) {
  77. // Need to stop status if image done
  78. if (imageStatus.intValue() != ImageConsumer.STATICIMAGEDONE
  79. && imageStatus.intValue() != ImageConsumer.SINGLEFRAMEDONE) {
  80. this.imageStatus = new Integer(status);
  81. }
  82. }
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public void setColorModel(ColorModel model) {
  88. // log.error("setColorModel: " + model);
  89. this.cm = model;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public void setDimensions(int width, int height) {
  95. // log.error("setDimension: w=" + width + " h=" + height);
  96. this.width = width;
  97. this.height = height;
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public void setHints(int hintflags) {
  103. // log.error("setHints: " + hintflags);
  104. this.hints = hintflags;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public void setProperties(Hashtable props) {
  110. // log.error("setProperties: " + props);
  111. this.properties = props;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public void setPixels(int x, int y, int w, int h, ColorModel model,
  117. byte[] pixels, int off, int scansize) {
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public void setPixels(int x, int y, int w, int h, ColorModel model,
  123. int[] pixels, int off, int scansize) {
  124. }
  125. /**
  126. * Indicates whether the image is ready.
  127. * @return boolean True if the image is ready, false if it's still loading
  128. * @throws Exception If an error happened while loading the image
  129. */
  130. public boolean isImageReady() throws Exception {
  131. /**@todo Use a better exception than Exception */
  132. synchronized (this.imageStatus) {
  133. if (this.imageStatus.intValue() == ImageConsumer.IMAGEABORTED) {
  134. throw new Exception("Image aborted");
  135. }
  136. if (this.imageStatus.intValue() == ImageConsumer.IMAGEERROR) {
  137. throw new Exception("Image error");
  138. }
  139. if (imageStatus.intValue() == ImageConsumer.STATICIMAGEDONE
  140. || imageStatus.intValue() == ImageConsumer.SINGLEFRAMEDONE) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. }
  146. /**
  147. * Returns the image width
  148. * @return the width in pixels
  149. */
  150. public int getWidth() {
  151. return this.width;
  152. }
  153. /**
  154. * Returns the image height
  155. * @return the height in pixels
  156. */
  157. public int getHeight() {
  158. return this.height;
  159. }
  160. /**
  161. * Returns the color model of the image
  162. * @return the color model
  163. */
  164. public ColorModel getColorModel() {
  165. return this.cm;
  166. }
  167. /**
  168. * Returns the bitmap as an array.
  169. * @return the bitmap as an array.
  170. * @throws Exception if an error occured while generating the array
  171. */
  172. public int[] getImage() throws Exception {
  173. int tmpMap[] = new int[this.width * this.height];
  174. PixelGrabber pg = new PixelGrabber(this.ip, 0, 0, this.width,
  175. this.height, tmpMap, 0, this.width);
  176. pg.setDimensions(this.width, this.height);
  177. pg.setColorModel(this.cm);
  178. pg.setHints(this.hints);
  179. pg.setProperties(this.properties);
  180. try {
  181. pg.grabPixels();
  182. } catch (InterruptedException intex) {
  183. /**@todo Use a better exception than Exception */
  184. throw new Exception("Image grabbing interrupted : "
  185. + intex.getMessage());
  186. }
  187. return tmpMap;
  188. }
  189. }