1 51 package org.apache.fop.image; 52 53 import java.net.URL ; 55 import java.awt.image.ImageProducer ; 56 import java.awt.image.ColorModel ; 57 import java.awt.image.IndexColorModel ; 58 59 import com.sun.jimi.core.*; 61 62 import org.apache.fop.datatypes.ColorSpace; 64 import org.apache.fop.pdf.PDFColor; 65 import org.apache.fop.image.analyser.ImageReader; 66 67 74 public class JimiImage extends AbstractFopImage { 75 public JimiImage(URL href) throws FopImageException { 76 super(href); 77 try { 78 Class c = Class.forName("com.sun.jimi.core.Jimi"); 79 } catch (ClassNotFoundException e) { 80 throw new FopImageException("Jimi image library not available"); 81 } 82 } 83 84 public JimiImage(URL href, 85 ImageReader imgReader) throws FopImageException { 86 super(href, imgReader); 87 try { 88 Class c = Class.forName("com.sun.jimi.core.Jimi"); 89 } catch (ClassNotFoundException e) { 90 throw new FopImageException("Jimi image library not available"); 91 } 92 } 93 94 protected void loadImage() throws FopImageException { 95 int[] tmpMap = null; 96 try { 97 ImageProducer ip = Jimi.getImageProducer(this.m_href.openStream(), 98 Jimi.SYNCHRONOUS 99 | Jimi.IN_MEMORY); 100 FopImageConsumer consumer = new FopImageConsumer(ip); 101 ip.startProduction(consumer); 102 103 while (!consumer.isImageReady()) { 104 Thread.sleep(500); 105 } 106 this.m_height = consumer.getHeight(); 107 this.m_width = consumer.getWidth(); 108 109 try { 110 tmpMap = consumer.getImage(); 111 } catch (Exception ex) { 112 throw new FopImageException("Image grabbing interrupted : " 113 + ex.getMessage()); 114 } 115 116 ColorModel cm = consumer.getColorModel(); 117 this.m_bitsPerPixel = 8; 118 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 120 if (cm.hasAlpha()) { 121 int transparencyType = 122 cm.getTransparency(); if (transparencyType == java.awt.Transparency.OPAQUE) { 124 this.m_isTransparent = false; 125 } else if (transparencyType 126 == java.awt.Transparency.BITMASK) { 127 if (cm instanceof IndexColorModel ) { 128 this.m_isTransparent = false; 129 byte[] alphas = 130 new byte[((IndexColorModel )cm).getMapSize()]; 131 byte[] reds = 132 new byte[((IndexColorModel )cm).getMapSize()]; 133 byte[] greens = 134 new byte[((IndexColorModel )cm).getMapSize()]; 135 byte[] blues = 136 new byte[((IndexColorModel )cm).getMapSize()]; 137 ((IndexColorModel )cm).getAlphas(alphas); 138 ((IndexColorModel )cm).getReds(reds); 139 ((IndexColorModel )cm).getGreens(greens); 140 ((IndexColorModel )cm).getBlues(blues); 141 for (int i = 0; 142 i < ((IndexColorModel )cm).getMapSize(); i++) { 143 if ((alphas[i] & 0xFF) == 0) { 144 this.m_isTransparent = true; 145 this.m_transparentColor = 146 new PDFColor((int)(reds[i] & 0xFF), 147 (int)(greens[i] & 0xFF), 148 (int)(blues[i] & 0xFF)); 149 break; 150 } 151 } 152 } else { 153 164 this.m_isTransparent = false; 166 } 167 } else { 168 this.m_isTransparent = false; 169 } 170 } else { 171 this.m_isTransparent = false; 172 } 173 } catch (Exception ex) { 174 throw new FopImageException("Error while loading image " 175 + this.m_href.toString() + " : " 176 + ex.getClass() + " - " 177 + ex.getMessage()); 178 } 179 180 181 this.m_bitmapsSize = this.m_width * this.m_height * 3; 183 this.m_bitmaps = new byte[this.m_bitmapsSize]; 184 for (int i = 0; i < this.m_height; i++) { 185 for (int j = 0; j < this.m_width; j++) { 186 int p = tmpMap[i * this.m_width + j]; 187 int r = (p >> 16) & 0xFF; 188 int g = (p >> 8) & 0xFF; 189 int b = (p) & 0xFF; 190 this.m_bitmaps[3 * (i * this.m_width + j)] = (byte)(r & 0xFF); 191 this.m_bitmaps[3 * (i * this.m_width + j) + 1] = (byte)(g 192 & 0xFF); 193 this.m_bitmaps[3 * (i * this.m_width + j) + 2] = (byte)(b 194 & 0xFF); 195 } 196 } 197 } 198 199 } 200 201 | Popular Tags |