1 17 18 19 20 package org.apache.fop.image; 21 22 import java.awt.Color ; 23 import java.awt.Transparency ; 24 import java.awt.image.ColorModel ; 25 import java.awt.image.IndexColorModel ; 26 import java.awt.image.RenderedImage ; 27 import java.awt.image.WritableRaster ; 28 import java.awt.image.BufferedImage ; 29 import java.io.IOException ; 30 31 import org.apache.xmlgraphics.image.GraphicsUtil; 32 import org.apache.xmlgraphics.image.codec.util.SeekableStream; 33 import org.apache.xmlgraphics.image.codec.util.MemoryCacheSeekableStream; 34 import org.apache.xmlgraphics.image.codec.util.FileCacheSeekableStream; 35 import org.apache.xmlgraphics.image.rendered.CachableRed; 36 37 import org.apache.commons.io.IOUtils; 38 39 44 public abstract class XmlGraphicsCommonsImage extends AbstractFopImage { 45 46 private byte[] softMask = null; 47 48 51 protected SeekableStream seekableInput = null; 52 53 56 protected CachableRed cr = null; 57 58 62 public XmlGraphicsCommonsImage(FopImage.ImageInfo imgReader) { 63 super(imgReader); 64 } 65 66 69 protected boolean loadDimensions() { 70 if (seekableInput == null && inputStream != null) { 71 try { 72 seekableInput = new FileCacheSeekableStream(inputStream); 73 } catch (IOException ioe) { 74 seekableInput = new MemoryCacheSeekableStream(inputStream); 75 } 76 try { 77 this.bitsPerPixel = 8; 78 cr = decodeImage(seekableInput); 79 this.height = cr.getHeight(); 80 this.width = cr.getWidth(); 81 this.isTransparent = false; 82 this.softMask = null; 83 ColorModel cm = cr.getColorModel(); 84 85 this.height = cr.getHeight(); 86 this.width = cr.getWidth(); 87 this.isTransparent = false; 88 this.softMask = null; 89 90 int transparencyType = cm.getTransparency(); 91 if (cm instanceof IndexColorModel ) { 92 if (transparencyType == Transparency.BITMASK) { 93 IndexColorModel icm = (IndexColorModel )cm; 95 int numColor = icm.getMapSize(); 96 byte [] alpha = new byte[numColor]; 97 icm.getAlphas(alpha); 98 for (int i = 0; i < numColor; i++) { 99 if ((alpha[i] & 0xFF) == 0) { 100 this.isTransparent = true; 101 int red = (icm.getRed (i)) & 0xFF; 102 int grn = (icm.getGreen(i)) & 0xFF; 103 int blu = (icm.getBlue (i)) & 0xFF; 104 this.transparentColor = new Color (red, grn, blu); 105 break; 106 } 107 } 108 } 109 } else { 110 cr = GraphicsUtil.convertTosRGB(cr); 111 } 112 113 cm = cr.getColorModel(); 115 if (this.colorSpace == null) { 116 this.colorSpace = cm.getColorSpace(); 117 } 118 } catch (IOException ioe) { 119 log.error("Error while loading image (Batik): " + ioe.getMessage(), ioe); 120 IOUtils.closeQuietly(seekableInput); 121 IOUtils.closeQuietly(inputStream); 122 seekableInput = null; 123 inputStream = null; 124 return false; 125 } 126 } 127 return this.height != -1; 128 } 129 130 133 protected boolean loadBitmap() { 134 if (this.bitmaps == null) { 135 loadImage(); 136 } 137 138 return this.bitmaps != null; 139 } 140 141 144 public boolean hasSoftMask() { 145 if (this.bitmaps == null && this.raw == null) { 146 loadImage(); 147 } 148 149 return (this.softMask != null); 150 } 151 152 155 public byte[] getSoftMask() { 156 if (this.bitmaps == null) { 157 loadImage(); 158 } 159 160 return this.softMask; 161 } 162 163 169 protected abstract CachableRed decodeImage(SeekableStream stream) throws IOException ; 170 171 174 protected void loadImage() { 175 if (loadDimensions()) { 176 try { 177 if (cr == null) { 178 throw new IllegalStateException ( 179 "Can't load the bitmaps data without the CachableRed instance"); 180 } 181 182 ColorModel cm = cr.getColorModel(); 184 185 if (!this.isTransparent && cm.hasAlpha()) { 187 this.softMask = new byte[this.width * this.height]; 188 } 189 190 this.bitmaps = new byte[this.width * this.height * 3]; 191 192 constructBitmaps(cr, this.bitmaps, this.softMask); 193 } catch (Exception ex) { 194 log.error("Error while loading image (Batik): " + ex.getMessage(), ex); 195 } finally { 196 IOUtils.closeQuietly(seekableInput); 198 IOUtils.closeQuietly(inputStream); 199 seekableInput = null; 200 inputStream = null; 201 cr = null; 202 } 203 } 204 } 205 206 private static void constructBitmaps(RenderedImage red, byte[] bitmaps, byte[] softMask) { 207 WritableRaster wr = (WritableRaster )red.getData(); 208 ColorModel cm = red.getColorModel(); 209 BufferedImage bi = new BufferedImage 210 (cm, wr.createWritableTranslatedChild(0, 0), 211 cm.isAlphaPremultiplied(), null); 212 int width = red.getWidth(); 213 int height = red.getHeight(); 214 int [] tmpMap = new int[width]; 215 int idx = 0; 216 int sfIdx = 0; 217 for (int y = 0; y < height; y++) { 218 tmpMap = bi.getRGB(0, y, width, 1, tmpMap, 0, width); 219 if (softMask != null) { 220 for (int x = 0; x < width; x++) { 221 int pix = tmpMap[x]; 222 softMask[sfIdx++] = (byte)(pix >>> 24); 223 bitmaps[idx++] = (byte)((pix >>> 16) & 0xFF); 224 bitmaps[idx++] = (byte)((pix >>> 8) & 0xFF); 225 bitmaps[idx++] = (byte)((pix) & 0xFF); 226 } 227 } else { 228 for (int x = 0; x < width; x++) { 229 int pix = tmpMap[x]; 230 bitmaps[idx++] = (byte)((pix >> 16) & 0xFF); 231 bitmaps[idx++] = (byte)((pix >> 8) & 0xFF); 232 bitmaps[idx++] = (byte)((pix) & 0xFF); 233 } 234 } 235 } 236 } 237 238 } | Popular Tags |