1 51 package org.apache.fop.image; 52 53 import java.net.URL ; 55 import java.io.InputStream ; 56 57 import java.awt.image.ColorModel ; 59 import java.awt.image.IndexColorModel ; 60 import java.awt.image.BufferedImage ; 61 62 import javax.media.jai.JAI; 64 import javax.media.jai.RenderedOp; 65 import com.sun.media.jai.codec.FileCacheSeekableStream; 67 import org.apache.fop.datatypes.ColorSpace; 69 import org.apache.fop.pdf.PDFColor; 70 import org.apache.fop.image.analyser.ImageReader; 71 72 78 public class JAIImage extends AbstractFopImage { 79 public JAIImage(URL href) throws FopImageException { 80 super(href); 81 } 82 83 public JAIImage(URL href, 84 ImageReader imgReader) throws FopImageException { 85 super(href, imgReader); 86 } 87 88 protected void loadImage() throws FopImageException { 89 try { 90 InputStream inputStream = this.m_href.openStream(); 91 95 com.sun.media.jai.codec.FileCacheSeekableStream seekableInput = 96 new FileCacheSeekableStream(inputStream); 97 RenderedOp imageOp = JAI.create("stream", seekableInput); 98 99 this.m_height = imageOp.getHeight(); 100 this.m_width = imageOp.getWidth(); 101 102 ColorModel cm = imageOp.getColorModel(); 103 this.m_bitsPerPixel = 8; 104 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 106 107 BufferedImage imageData = imageOp.getAsBufferedImage(); 108 int[] tmpMap = imageData.getRGB(0, 0, this.m_width, 109 this.m_height, null, 0, 110 this.m_width); 111 112 if (cm.hasAlpha()) { 113 int transparencyType = 114 cm.getTransparency(); if (transparencyType == java.awt.Transparency.OPAQUE) { 116 this.m_isTransparent = false; 117 } else if (transparencyType 118 == java.awt.Transparency.BITMASK) { 119 if (cm instanceof IndexColorModel ) { 120 this.m_isTransparent = false; 121 byte[] alphas = 122 new byte[((IndexColorModel )cm).getMapSize()]; 123 byte[] reds = 124 new byte[((IndexColorModel )cm).getMapSize()]; 125 byte[] greens = 126 new byte[((IndexColorModel )cm).getMapSize()]; 127 byte[] blues = 128 new byte[((IndexColorModel )cm).getMapSize()]; 129 ((IndexColorModel )cm).getAlphas(alphas); 130 ((IndexColorModel )cm).getReds(reds); 131 ((IndexColorModel )cm).getGreens(greens); 132 ((IndexColorModel )cm).getBlues(blues); 133 for (int i = 0; 134 i < ((IndexColorModel )cm).getMapSize(); i++) { 135 if ((alphas[i] & 0xFF) == 0) { 136 this.m_isTransparent = true; 137 this.m_transparentColor = 138 new PDFColor((int)(reds[i] & 0xFF), 139 (int)(greens[i] & 0xFF), 140 (int)(blues[i] & 0xFF)); 141 break; 142 } 143 } 144 } else { 145 157 this.m_isTransparent = false; 158 } 159 } else { 160 this.m_isTransparent = false; 161 } 162 } else { 163 this.m_isTransparent = false; 164 } 165 166 this.m_bitmapsSize = this.m_width * this.m_height * 3; 168 this.m_bitmaps = new byte[this.m_bitmapsSize]; 169 for (int i = 0; i < this.m_height; i++) { 170 for (int j = 0; j < this.m_width; j++) { 171 int p = tmpMap[i * this.m_width + j]; 172 int r = (p >> 16) & 0xFF; 173 int g = (p >> 8) & 0xFF; 174 int b = (p) & 0xFF; 175 this.m_bitmaps[3 * (i * this.m_width + j)] = (byte)(r 176 & 0xFF); 177 this.m_bitmaps[3 * (i * this.m_width + j) + 1] = (byte)(g 178 & 0xFF); 179 this.m_bitmaps[3 * (i * this.m_width + j) + 2] = (byte)(b 180 & 0xFF); 181 } 182 } 183 184 } catch (Exception ex) { 185 throw new FopImageException("Error while loading image " 186 + this.m_href.toString() + " : " 187 + ex.getClass() + " - " 188 + ex.getMessage()); 189 } 190 } 191 192 } 193 194 | Popular Tags |