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.

ImageIOImage.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // AWT
  20. import java.awt.Color;
  21. import java.awt.color.ColorSpace;
  22. import java.awt.image.ColorModel;
  23. import java.awt.image.IndexColorModel;
  24. import java.awt.image.BufferedImage;
  25. import java.util.Iterator;
  26. // ImageIO
  27. import javax.imageio.ImageIO;
  28. import javax.imageio.ImageReadParam;
  29. import javax.imageio.ImageReader;
  30. import javax.imageio.metadata.IIOMetadata;
  31. import javax.imageio.stream.ImageInputStream;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.fop.util.UnitConv;
  34. import org.w3c.dom.Element;
  35. import org.w3c.dom.NodeList;
  36. /**
  37. * FopImage object using ImageIO.
  38. * @see AbstractFopImage
  39. * @see FopImage
  40. */
  41. public class ImageIOImage extends AbstractFopImage {
  42. private byte[] softMask = null;
  43. /**
  44. * Creates a new ImageIOImage.
  45. * @param info the image info from the ImageReader
  46. */
  47. public ImageIOImage(FopImage.ImageInfo info) {
  48. super(info);
  49. if ("image/png".equals(info.mimeType)
  50. || "image/tiff".equals(info.mimeType)) {
  51. this.loaded = 0; //TODO The PNG and TIFF Readers cannot read the resolution, yet.
  52. }
  53. }
  54. /**
  55. * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
  56. */
  57. protected boolean loadDimensions() {
  58. if (this.bitmaps == null) {
  59. return loadBitmap();
  60. }
  61. return true;
  62. }
  63. private Element getChild(Element el, String name) {
  64. NodeList nodes = el.getElementsByTagName(name);
  65. if (nodes.getLength() > 0) {
  66. return (Element)nodes.item(0);
  67. } else {
  68. return null;
  69. }
  70. }
  71. /** @see org.apache.fop.image.AbstractFopImage#loadBitmap() */
  72. protected boolean loadBitmap() {
  73. if (this.bitmaps != null) {
  74. return true;
  75. }
  76. try {
  77. inputStream.reset();
  78. ImageInputStream imgStream = ImageIO.createImageInputStream(inputStream);
  79. Iterator iter = ImageIO.getImageReaders(imgStream);
  80. if (!iter.hasNext()) {
  81. log.error("No ImageReader found.");
  82. return false;
  83. }
  84. ImageReader reader = (ImageReader)iter.next();
  85. ImageReadParam param = reader.getDefaultReadParam();
  86. reader.setInput(imgStream, true, false);
  87. BufferedImage imageData = reader.read(0, param);
  88. //Read image resolution
  89. IIOMetadata iiometa = reader.getImageMetadata(0);
  90. if (iiometa != null && iiometa.isStandardMetadataFormatSupported()) {
  91. Element metanode = (Element)iiometa.getAsTree("javax_imageio_1.0");
  92. Element dim = getChild(metanode, "Dimension");
  93. if (dim != null) {
  94. Element child;
  95. child = getChild(dim, "HorizontalPixelSize");
  96. if (child != null) {
  97. this.dpiHorizontal = UnitConv.IN2MM
  98. / Float.parseFloat(child.getAttribute("value"));
  99. }
  100. child = getChild(dim, "VerticalPixelSize");
  101. if (child != null) {
  102. this.dpiVertical = UnitConv.IN2MM
  103. / Float.parseFloat(child.getAttribute("value"));
  104. }
  105. }
  106. }
  107. imgStream.close();
  108. reader.dispose();
  109. this.height = imageData.getHeight();
  110. this.width = imageData.getWidth();
  111. ColorModel cm = imageData.getColorModel();
  112. this.bitsPerPixel = cm.getComponentSize(0); //only use first, we assume all are equal
  113. //this.colorSpace = cm.getColorSpace();
  114. //We currently force the image to sRGB
  115. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  116. int[] tmpMap = imageData.getRGB(0, 0, this.width,
  117. this.height, null, 0, this.width);
  118. if (cm.hasAlpha()) {
  119. // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
  120. int transparencyType = cm.getTransparency();
  121. if (transparencyType == java.awt.Transparency.OPAQUE) {
  122. this.isTransparent = false;
  123. } else if (transparencyType == java.awt.Transparency.BITMASK) {
  124. if (cm instanceof IndexColorModel) {
  125. this.isTransparent = false;
  126. byte[] alphas = new byte[
  127. ((IndexColorModel) cm).getMapSize()];
  128. byte[] reds = new byte[
  129. ((IndexColorModel) cm).getMapSize()];
  130. byte[] greens = new byte[
  131. ((IndexColorModel) cm).getMapSize()];
  132. byte[] blues = new byte[
  133. ((IndexColorModel) cm).getMapSize()];
  134. ((IndexColorModel) cm).getAlphas(alphas);
  135. ((IndexColorModel) cm).getReds(reds);
  136. ((IndexColorModel) cm).getGreens(greens);
  137. ((IndexColorModel) cm).getBlues(blues);
  138. for (int i = 0;
  139. i < ((IndexColorModel) cm).getMapSize();
  140. i++) {
  141. if ((alphas[i] & 0xFF) == 0) {
  142. this.isTransparent = true;
  143. this.transparentColor = new Color(
  144. (int)(reds[i] & 0xFF),
  145. (int)(greens[i] & 0xFF),
  146. (int)(blues[i] & 0xFF));
  147. break;
  148. }
  149. }
  150. } else {
  151. //TODO Is there another case?
  152. this.isTransparent = false;
  153. }
  154. } else {
  155. // TRANSLUCENT
  156. this.softMask = new byte[width * height];
  157. imageData.getAlphaRaster().getDataElements(
  158. 0, 0, width, height, this.softMask);
  159. this.isTransparent = false;
  160. }
  161. } else {
  162. this.isTransparent = false;
  163. }
  164. // Should take care of the ColorSpace and bitsPerPixel
  165. this.bitmaps = new byte[this.width * this.height * 3];
  166. for (int i = 0; i < this.height; i++) {
  167. for (int j = 0; j < this.width; j++) {
  168. int p = tmpMap[i * this.width + j];
  169. int r = (p >> 16) & 0xFF;
  170. int g = (p >> 8) & 0xFF;
  171. int b = (p) & 0xFF;
  172. this.bitmaps[3 * (i * this.width + j)]
  173. = (byte)(r & 0xFF);
  174. this.bitmaps[3 * (i * this.width + j) + 1]
  175. = (byte)(g & 0xFF);
  176. this.bitmaps[3 * (i * this.width + j) + 2]
  177. = (byte)(b & 0xFF);
  178. }
  179. }
  180. } catch (Exception ex) {
  181. log.error("Error while loading image: " + ex.getMessage(), ex);
  182. return false;
  183. } finally {
  184. IOUtils.closeQuietly(inputStream);
  185. inputStream = null;
  186. }
  187. return true;
  188. }
  189. /** @see org.apache.fop.image.AbstractFopImage#loadOriginalData() */
  190. protected boolean loadOriginalData() {
  191. if (inputStream == null && getBitmaps() != null) {
  192. return false;
  193. } else {
  194. return loadDefaultOriginalData();
  195. }
  196. }
  197. /** @see org.apache.fop.image.FopImage#hasSoftMask() */
  198. public boolean hasSoftMask() {
  199. if (this.bitmaps == null && this.raw == null) {
  200. loadBitmap();
  201. }
  202. return (this.softMask != null);
  203. }
  204. /** @see org.apache.fop.image.FopImage#getSoftMask() */
  205. public byte[] getSoftMask() {
  206. if (this.bitmaps == null) {
  207. loadBitmap();
  208. }
  209. return this.softMask;
  210. }
  211. }