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 org.apache.fop.datatypes.ColorSpace; 61 import org.apache.fop.pdf.PDFColor; 62 import org.apache.fop.image.analyser.ImageReader; 63 64 71 public class GifImage extends AbstractFopImage { 72 public GifImage(URL href) throws FopImageException { 73 super(href); 74 } 75 76 public GifImage(URL href, 77 ImageReader imgReader) throws FopImageException { 78 super(href, imgReader); 79 } 80 81 protected void loadImage() throws FopImageException { 82 int[] tmpMap = null; 84 try { 85 ImageProducer ip = (ImageProducer )this.m_href.getContent(); 86 FopImageConsumer consumer = new FopImageConsumer(ip); 87 ip.startProduction(consumer); 88 89 90 while (!consumer.isImageReady()) { 92 Thread.sleep(500); 93 } 94 95 this.m_height = consumer.getHeight(); 96 this.m_width = consumer.getWidth(); 97 98 try { 99 tmpMap = consumer.getImage(); 100 } catch (Exception ex) { 101 throw new FopImageException("Image grabbing interrupted : " 102 + ex.getMessage()); 103 } 104 105 ColorModel cm = consumer.getColorModel(); 106 this.m_bitsPerPixel = 8; 107 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 109 if (cm.hasAlpha()) { 110 int transparencyType = 111 cm.getTransparency(); if (transparencyType == java.awt.Transparency.OPAQUE) { 113 this.m_isTransparent = false; 114 } else if (transparencyType 115 == java.awt.Transparency.BITMASK) { 116 if (cm instanceof IndexColorModel ) { 117 this.m_isTransparent = false; 118 byte[] alphas = 119 new byte[((IndexColorModel )cm).getMapSize()]; 120 byte[] reds = 121 new byte[((IndexColorModel )cm).getMapSize()]; 122 byte[] greens = 123 new byte[((IndexColorModel )cm).getMapSize()]; 124 byte[] blues = 125 new byte[((IndexColorModel )cm).getMapSize()]; 126 ((IndexColorModel )cm).getAlphas(alphas); 127 ((IndexColorModel )cm).getReds(reds); 128 ((IndexColorModel )cm).getGreens(greens); 129 ((IndexColorModel )cm).getBlues(blues); 130 for (int i = 0; 131 i < ((IndexColorModel )cm).getMapSize(); i++) { 132 if ((alphas[i] & 0xFF) == 0) { 133 this.m_isTransparent = true; 134 this.m_transparentColor = 135 new PDFColor((int)(reds[i] & 0xFF), 136 (int)(greens[i] & 0xFF), 137 (int)(blues[i] & 0xFF)); 138 break; 139 } 140 } 141 } else { 142 153 this.m_isTransparent = false; 155 } 156 } else { 157 this.m_isTransparent = false; 158 } 159 } else { 160 this.m_isTransparent = false; 161 } 162 } catch (Exception ex) { 163 throw new FopImageException("Error while loading image " 164 + this.m_href.toString() + " : " 165 + ex.getClass() + " - " 166 + ex.getMessage()); 167 } 168 169 this.m_bitmapsSize = this.m_width * this.m_height * 3; 171 this.m_bitmaps = new byte[this.m_bitmapsSize]; 172 for (int i = 0; i < this.m_height; i++) { 173 for (int j = 0; j < this.m_width; j++) { 174 int p = tmpMap[i * this.m_width + j]; 175 int r = (p >> 16) & 0xFF; 176 int g = (p >> 8) & 0xFF; 177 int b = (p) & 0xFF; 178 this.m_bitmaps[3 * (i * this.m_width + j)] = (byte)(r & 0xFF); 179 this.m_bitmaps[3 * (i * this.m_width + j) + 1] = (byte)(g 180 & 0xFF); 181 this.m_bitmaps[3 * (i * this.m_width + j) + 2] = (byte)(b 182 & 0xFF); 183 } 184 } 185 } 186 187 } 188 189 | Popular Tags |