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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.image;
  18. // Java
  19. import java.awt.color.ColorSpace;
  20. import java.awt.color.ICC_Profile;
  21. import java.io.InputStream;
  22. import java.awt.Color;
  23. // FOP
  24. import org.apache.avalon.framework.logger.Logger;
  25. /**
  26. * Base class to implement the FopImage interface.
  27. * @author Eric SCHAEFFER
  28. * @author Modified by Eric Dalquist - 9/14/2001 - ebdalqui@mtu.edu
  29. * @see FopImage
  30. */
  31. public abstract class AbstractFopImage implements FopImage {
  32. /**
  33. * Keeps track of what has been loaded.
  34. */
  35. protected int loaded = 0;
  36. /**
  37. * Image width (in pixel).
  38. */
  39. protected int width = 0;
  40. /**
  41. * Image height (in pixel).
  42. */
  43. protected int height = 0;
  44. /**
  45. * Image input stream.
  46. */
  47. protected InputStream inputStream = null;
  48. /**
  49. * ImageReader object (to obtain image header informations).
  50. */
  51. protected FopImage.ImageInfo imageInfo = null;
  52. /**
  53. * Image color space (java.awt.color.ColorSpace).
  54. */
  55. protected ColorSpace colorSpace = null;
  56. /**
  57. * Bits per pixel.
  58. */
  59. protected int bitsPerPixel = 0;
  60. /**
  61. * Image data (uncompressed).
  62. */
  63. protected byte[] bitmaps = null;
  64. /**
  65. * Image data size.
  66. */
  67. protected int bitmapsSize = 0;
  68. /**
  69. * Image transparency.
  70. */
  71. protected boolean isTransparent = false;
  72. /**
  73. * Transparent color (java.awt.Color).
  74. */
  75. protected Color transparentColor = null;
  76. /**
  77. * Constructor.
  78. * Construct a new FopImage object and initialize its default properties:
  79. * <UL>
  80. * <LI>image width
  81. * <LI>image height
  82. * </UL>
  83. * The image data isn't kept in memory.
  84. * @param info image information
  85. */
  86. public AbstractFopImage(FopImage.ImageInfo info) {
  87. this.inputStream = info.inputStream;
  88. this.imageInfo = info;
  89. if (this.imageInfo.width != -1) {
  90. width = imageInfo.width;
  91. height = imageInfo.height;
  92. loaded = loaded | DIMENSIONS;
  93. }
  94. }
  95. /**
  96. * Get the mime type for this image.
  97. *
  98. * @return the mime type for the image
  99. */
  100. public String getMimeType() {
  101. return imageInfo.mimeType;
  102. }
  103. /**
  104. * Load image data and initialize its properties.
  105. *
  106. * @param type the type of loading to do
  107. * @param ua the user agent for handling logging etc.
  108. * @return true if the loading was successful
  109. */
  110. public synchronized boolean load(int type, Logger logger) {
  111. if ((loaded & type) != 0) {
  112. return true;
  113. }
  114. boolean success = true;
  115. if (((type & DIMENSIONS) != 0) && ((loaded & DIMENSIONS) == 0)) {
  116. success = success && loadDimensions(logger);
  117. if (!success) {
  118. return false;
  119. }
  120. loaded = loaded | DIMENSIONS;
  121. }
  122. if (((type & BITMAP) != 0) && ((loaded & BITMAP) == 0)) {
  123. success = success && loadBitmap(logger);
  124. if (success) {
  125. loaded = loaded | BITMAP;
  126. }
  127. }
  128. if (((type & ORIGINAL_DATA) != 0) && ((loaded & ORIGINAL_DATA) == 0)) {
  129. success = success && loadOriginalData(logger);
  130. if (success) {
  131. loaded = loaded | ORIGINAL_DATA;
  132. }
  133. }
  134. return success;
  135. }
  136. /**
  137. * Load the dimensions of the image.
  138. * All implementations should override this to get and
  139. * return the dimensions.
  140. *
  141. * @param ua the user agent
  142. * @return true if the loading was successful
  143. */
  144. protected boolean loadDimensions(Logger logger) {
  145. return false;
  146. }
  147. /**
  148. * Load a bitmap array of the image.
  149. * If the renderer requires a bitmap image then the
  150. * implementations should override this to load the bitmap.
  151. *
  152. * @param ua the user agent
  153. * @return true if the loading was successful
  154. */
  155. protected boolean loadBitmap(Logger logger) {
  156. return false;
  157. }
  158. /**
  159. * Load the original image data.
  160. * In some cases the original data can be used by the renderer.
  161. * This should load the data and any other associated information.
  162. *
  163. * @param ua the user agent
  164. * @return true if the loading was successful
  165. */
  166. protected boolean loadOriginalData(Logger logger) {
  167. return false;
  168. }
  169. /**
  170. * Return the image width.
  171. * @return the image width
  172. */
  173. public int getWidth() {
  174. return this.width;
  175. }
  176. /**
  177. * Return the image height.
  178. * @return the image height
  179. */
  180. public int getHeight() {
  181. return this.height;
  182. }
  183. /**
  184. * Return the image color space.
  185. * @return the image color space (java.awt.color.ColorSpace)
  186. */
  187. public ColorSpace getColorSpace() {
  188. return this.colorSpace;
  189. }
  190. /**
  191. * Get ICC profile for this image.
  192. * @return the icc profile or null if not applicable
  193. */
  194. public ICC_Profile getICCProfile() {
  195. return null;
  196. }
  197. /**
  198. * Return the number of bits per pixel.
  199. * @return number of bits per pixel
  200. */
  201. public int getBitsPerPixel() {
  202. return this.bitsPerPixel;
  203. }
  204. /**
  205. * Return the image transparency.
  206. * @return true if the image is transparent
  207. */
  208. public boolean isTransparent() {
  209. return this.isTransparent;
  210. }
  211. /**
  212. * Check if this image has a soft mask.
  213. *
  214. * @return true if the image also has a soft transparency mask
  215. */
  216. public boolean hasSoftMask() {
  217. return false;
  218. }
  219. /**
  220. * Get the soft mask.
  221. * The soft mask should have the same bitdepth as the image data.
  222. *
  223. * @return the data array of soft mask values
  224. */
  225. public byte[] getSoftMask() {
  226. return null;
  227. }
  228. /**
  229. * Return the transparent color.
  230. * @return the transparent color (java.awt.Color)
  231. */
  232. public Color getTransparentColor() {
  233. return this.transparentColor;
  234. }
  235. /**
  236. * Return the image data (uncompressed).
  237. * @return the image data
  238. */
  239. public byte[] getBitmaps() {
  240. return this.bitmaps;
  241. }
  242. /**
  243. * Return the image data size (uncompressed).
  244. * @return the image data size
  245. */
  246. public int getBitmapsSize() {
  247. return this.bitmapsSize;
  248. }
  249. /**
  250. * Return the original image data (compressed).
  251. * @return the original image data
  252. */
  253. public byte[] getRessourceBytes() {
  254. return null;
  255. }
  256. /**
  257. * Return the original image data size (compressed).
  258. * @return the original image data size
  259. */
  260. public int getRessourceBytesSize() {
  261. return 0;
  262. }
  263. }