1 17 18 19 20 package org.apache.fop.image; 21 22 import java.awt.Color ; 24 import java.awt.color.ColorSpace ; 25 import java.awt.image.ColorModel ; 26 import java.awt.image.IndexColorModel ; 27 import java.awt.image.BufferedImage ; 28 import java.util.Iterator ; 29 30 import javax.imageio.ImageIO ; 32 import javax.imageio.ImageReadParam ; 33 import javax.imageio.ImageReader ; 34 import javax.imageio.metadata.IIOMetadata ; 35 import javax.imageio.stream.ImageInputStream ; 36 37 import org.apache.commons.io.IOUtils; 38 import org.apache.fop.util.UnitConv; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.NodeList ; 41 42 47 public class ImageIOImage extends AbstractFopImage { 48 49 53 public ImageIOImage(FopImage.ImageInfo info) { 54 super(info); 55 if ("image/png".equals(info.mimeType) 56 || "image/tiff".equals(info.mimeType)) { 57 this.loaded = 0; } 59 } 60 61 64 protected boolean loadDimensions() { 65 if (this.bitmaps == null) { 66 return loadBitmap(); 67 } 68 return true; 69 } 70 71 private Element getChild(Element el, String name) { 72 NodeList nodes = el.getElementsByTagName(name); 73 if (nodes.getLength() > 0) { 74 return (Element )nodes.item(0); 75 } else { 76 return null; 77 } 78 } 79 80 81 protected boolean loadBitmap() { 82 if (this.bitmaps != null) { 83 return true; 84 } 85 try { 86 inputStream.reset(); 87 ImageInputStream imgStream = ImageIO.createImageInputStream(inputStream); 88 Iterator iter = ImageIO.getImageReaders(imgStream); 89 if (!iter.hasNext()) { 90 log.error("No ImageReader found."); 91 return false; 92 } 93 ImageReader reader = (ImageReader )iter.next(); 94 ImageReadParam param = reader.getDefaultReadParam(); 95 reader.setInput(imgStream, true, false); 96 BufferedImage imageData = reader.read(0, param); 97 98 IIOMetadata iiometa = reader.getImageMetadata(0); 100 if (iiometa != null && iiometa.isStandardMetadataFormatSupported()) { 101 Element metanode = (Element )iiometa.getAsTree("javax_imageio_1.0"); 102 Element dim = getChild(metanode, "Dimension"); 103 if (dim != null) { 104 Element child; 105 child = getChild(dim, "HorizontalPixelSize"); 106 if (child != null) { 107 this.dpiHorizontal = UnitConv.IN2MM 108 / Float.parseFloat(child.getAttribute("value")); 109 } 110 child = getChild(dim, "VerticalPixelSize"); 111 if (child != null) { 112 this.dpiVertical = UnitConv.IN2MM 113 / Float.parseFloat(child.getAttribute("value")); 114 } 115 } 116 } 117 imgStream.close(); 118 reader.dispose(); 119 120 this.height = imageData.getHeight(); 121 this.width = imageData.getWidth(); 122 123 ColorModel cm = imageData.getColorModel(); 124 this.bitsPerPixel = cm.getComponentSize(0); this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); 128 129 int[] tmpMap = imageData.getRGB(0, 0, this.width, 130 this.height, null, 0, this.width); 131 132 if (cm.hasAlpha()) { 133 int transparencyType = cm.getTransparency(); 135 136 if (transparencyType == java.awt.Transparency.OPAQUE) { 137 this.isTransparent = false; 138 } else if (transparencyType == java.awt.Transparency.BITMASK) { 139 if (cm instanceof IndexColorModel ) { 140 this.isTransparent = false; 141 byte[] alphas = new byte[ 142 ((IndexColorModel ) cm).getMapSize()]; 143 byte[] reds = new byte[ 144 ((IndexColorModel ) cm).getMapSize()]; 145 byte[] greens = new byte[ 146 ((IndexColorModel ) cm).getMapSize()]; 147 byte[] blues = new byte[ 148 ((IndexColorModel ) cm).getMapSize()]; 149 ((IndexColorModel ) cm).getAlphas(alphas); 150 ((IndexColorModel ) cm).getReds(reds); 151 ((IndexColorModel ) cm).getGreens(greens); 152 ((IndexColorModel ) cm).getBlues(blues); 153 for (int i = 0; 154 i < ((IndexColorModel ) cm).getMapSize(); 155 i++) { 156 if ((alphas[i] & 0xFF) == 0) { 157 this.isTransparent = true; 158 this.transparentColor = new Color ( 159 (int)(reds[i] & 0xFF), 160 (int)(greens[i] & 0xFF), 161 (int)(blues[i] & 0xFF)); 162 break; 163 } 164 } 165 } else { 166 179 this.isTransparent = false; 180 } 181 } else { 182 this.isTransparent = false; 183 } 184 } else { 185 this.isTransparent = false; 186 } 187 188 this.bitmaps = new byte[this.width * this.height * 3]; 190 for (int i = 0; i < this.height; i++) { 191 for (int j = 0; j < this.width; j++) { 192 int p = tmpMap[i * this.width + j]; 193 int r = (p >> 16) & 0xFF; 194 int g = (p >> 8) & 0xFF; 195 int b = (p) & 0xFF; 196 this.bitmaps[3 * (i * this.width + j)] 197 = (byte)(r & 0xFF); 198 this.bitmaps[3 * (i * this.width + j) + 1] 199 = (byte)(g & 0xFF); 200 this.bitmaps[3 * (i * this.width + j) + 2] 201 = (byte)(b & 0xFF); 202 } 203 } 204 205 } catch (Exception ex) { 206 log.error("Error while loading image: " + ex.getMessage(), ex); 207 return false; 208 } finally { 209 IOUtils.closeQuietly(inputStream); 210 inputStream = null; 211 } 212 return true; 213 } 214 215 216 protected boolean loadOriginalData() { 217 return loadDefaultOriginalData(); 218 } 219 220 } 221 222 | Popular Tags |