You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractFopImage.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * $Id: AbstractFopImage.java,v 1.17 2003/03/06 21:25:44 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.image;
  52. // Java
  53. import java.awt.color.ColorSpace;
  54. import java.awt.color.ICC_Profile;
  55. import java.io.InputStream;
  56. // FOP
  57. import org.apache.fop.pdf.PDFColor;
  58. import org.apache.fop.apps.FOUserAgent;
  59. /**
  60. * Base class to implement the FopImage interface.
  61. * @author Eric SCHAEFFER
  62. * @author Modified by Eric Dalquist - 9/14/2001 - ebdalqui@mtu.edu
  63. * @see FopImage
  64. */
  65. public abstract class AbstractFopImage implements FopImage {
  66. /**
  67. * Keeps track of what has been loaded.
  68. */
  69. protected int loaded = 0;
  70. /**
  71. * Image width (in pixel).
  72. */
  73. protected int width = 0;
  74. /**
  75. * Image height (in pixel).
  76. */
  77. protected int height = 0;
  78. /**
  79. * Image input stream.
  80. */
  81. protected InputStream inputStream = null;
  82. /**
  83. * ImageReader object (to obtain image header informations).
  84. */
  85. protected FopImage.ImageInfo imageInfo = null;
  86. /**
  87. * Image color space (java.awt.color.ColorSpace).
  88. */
  89. protected ColorSpace colorSpace = null;
  90. /**
  91. * Bits per pixel.
  92. */
  93. protected int bitsPerPixel = 0;
  94. /**
  95. * Image data (uncompressed).
  96. */
  97. protected byte[] bitmaps = null;
  98. /**
  99. * Image data size.
  100. */
  101. protected int bitmapsSize = 0;
  102. /**
  103. * Image transparency.
  104. */
  105. protected boolean isTransparent = false;
  106. /**
  107. * Transparent color (org.apache.fop.pdf.PDFColor).
  108. */
  109. protected PDFColor transparentColor = null;
  110. /**
  111. * Constructor.
  112. * Construct a new FopImage object and initialize its default properties:
  113. * <UL>
  114. * <LI>image width
  115. * <LI>image height
  116. * </UL>
  117. * The image data isn't kept in memory.
  118. * @param info image information
  119. */
  120. public AbstractFopImage(FopImage.ImageInfo info) {
  121. this.inputStream = info.inputStream;
  122. this.imageInfo = info;
  123. if (this.imageInfo.width != -1) {
  124. width = imageInfo.width;
  125. height = imageInfo.height;
  126. loaded = loaded | DIMENSIONS;
  127. }
  128. }
  129. /**
  130. * Get the mime type for this image.
  131. *
  132. * @return the mime type for the image
  133. */
  134. public String getMimeType() {
  135. return imageInfo.mimeType;
  136. }
  137. /**
  138. * Load image data and initialize its properties.
  139. *
  140. * @param type the type of loading to do
  141. * @param ua the user agent for handling logging etc.
  142. * @return true if the loading was successful
  143. */
  144. public synchronized boolean load(int type, FOUserAgent ua) {
  145. if ((loaded & type) != 0) {
  146. return true;
  147. }
  148. boolean success = true;
  149. if (((type & DIMENSIONS) != 0) && ((loaded & DIMENSIONS) == 0)) {
  150. success = success && loadDimensions(ua);
  151. if (!success) {
  152. return false;
  153. }
  154. loaded = loaded | DIMENSIONS;
  155. }
  156. if (((type & BITMAP) != 0) && ((loaded & BITMAP) == 0)) {
  157. success = success && loadBitmap(ua);
  158. if (success) {
  159. loaded = loaded | BITMAP;
  160. }
  161. }
  162. if (((type & ORIGINAL_DATA) != 0) && ((loaded & ORIGINAL_DATA) == 0)) {
  163. success = success && loadOriginalData(ua);
  164. if (success) {
  165. loaded = loaded | ORIGINAL_DATA;
  166. }
  167. }
  168. return success;
  169. }
  170. /**
  171. * Load the dimensions of the image.
  172. * All implementations should override this to get and
  173. * return the dimensions.
  174. *
  175. * @param ua the user agent
  176. * @return true if the loading was successful
  177. */
  178. protected boolean loadDimensions(FOUserAgent ua) {
  179. return false;
  180. }
  181. /**
  182. * Load a bitmap array of the image.
  183. * If the renderer requires a bitmap image then the
  184. * implementations should override this to load the bitmap.
  185. *
  186. * @param ua the user agent
  187. * @return true if the loading was successful
  188. */
  189. protected boolean loadBitmap(FOUserAgent ua) {
  190. return false;
  191. }
  192. /**
  193. * Load the original image data.
  194. * In some cases the original data can be used by the renderer.
  195. * This should load the data and any other associated information.
  196. *
  197. * @param ua the user agent
  198. * @return true if the loading was successful
  199. */
  200. protected boolean loadOriginalData(FOUserAgent ua) {
  201. return false;
  202. }
  203. /**
  204. * Return the image width.
  205. * @return the image width
  206. */
  207. public int getWidth() {
  208. return this.width;
  209. }
  210. /**
  211. * Return the image height.
  212. * @return the image height
  213. */
  214. public int getHeight() {
  215. return this.height;
  216. }
  217. /**
  218. * Return the image color space.
  219. * @return the image color space (java.awt.color.ColorSpace)
  220. */
  221. public ColorSpace getColorSpace() {
  222. return this.colorSpace;
  223. }
  224. /**
  225. * Get ICC profile for this image.
  226. * @return the icc profile or null if not applicable
  227. */
  228. public ICC_Profile getICCProfile() {
  229. return null;
  230. }
  231. /**
  232. * Return the number of bits per pixel.
  233. * @return number of bits per pixel
  234. */
  235. public int getBitsPerPixel() {
  236. return this.bitsPerPixel;
  237. }
  238. /**
  239. * Return the image transparency.
  240. * @return true if the image is transparent
  241. */
  242. public boolean isTransparent() {
  243. return this.isTransparent;
  244. }
  245. /**
  246. * Check if this image has a soft mask.
  247. *
  248. * @return true if the image also has a soft transparency mask
  249. */
  250. public boolean hasSoftMask() {
  251. return false;
  252. }
  253. /**
  254. * Get the soft mask.
  255. * The soft mask should have the same bitdepth as the image data.
  256. *
  257. * @return the data array of soft mask values
  258. */
  259. public byte[] getSoftMask() {
  260. return null;
  261. }
  262. /**
  263. * Return the transparent color.
  264. * @return the transparent color (org.apache.fop.pdf.PDFColor)
  265. */
  266. public PDFColor getTransparentColor() {
  267. return this.transparentColor;
  268. }
  269. /**
  270. * Return the image data (uncompressed).
  271. * @return the image data
  272. */
  273. public byte[] getBitmaps() {
  274. return this.bitmaps;
  275. }
  276. /**
  277. * Return the image data size (uncompressed).
  278. * @return the image data size
  279. */
  280. public int getBitmapsSize() {
  281. return this.bitmapsSize;
  282. }
  283. /**
  284. * Return the original image data (compressed).
  285. * @return the original image data
  286. */
  287. public byte[] getRessourceBytes() {
  288. return null;
  289. }
  290. /**
  291. * Return the original image data size (compressed).
  292. * @return the original image data size
  293. */
  294. public int getRessourceBytesSize() {
  295. return 0;
  296. }
  297. }