1 17 18 19 20 package org.apache.fop.image; 21 22 import java.awt.Color ; 24 import java.awt.image.ColorModel ; 25 import java.awt.image.IndexColorModel ; 26 import java.awt.image.BufferedImage ; 27 28 import javax.imageio.ImageIO ; 30 31 import org.apache.commons.io.IOUtils; 32 33 41 public class JpegImageIOImage extends JpegImage { 42 43 47 public JpegImageIOImage(FopImage.ImageInfo info) { 48 super(info); 49 } 50 51 54 protected boolean loadDimensions() { 55 if (this.bitmaps == null) { 56 return loadBitmap(); 57 } 58 return true; 59 } 60 61 62 protected boolean loadBitmap() { 63 try { 64 inputStream.reset(); 65 BufferedImage imageData = ImageIO.read(inputStream); 66 67 this.height = imageData.getHeight(); 68 this.width = imageData.getWidth(); 69 70 ColorModel cm = imageData.getColorModel(); 71 this.bitsPerPixel = cm.getComponentSize(0); this.colorSpace = cm.getColorSpace(); 73 74 int[] tmpMap = imageData.getRGB(0, 0, this.width, 75 this.height, null, 0, this.width); 76 77 if (cm.hasAlpha()) { 78 int transparencyType = cm.getTransparency(); 80 81 if (transparencyType == java.awt.Transparency.OPAQUE) { 82 this.isTransparent = false; 83 } else if (transparencyType == java.awt.Transparency.BITMASK) { 84 if (cm instanceof IndexColorModel ) { 85 this.isTransparent = false; 86 byte[] alphas = new byte[ 87 ((IndexColorModel ) cm).getMapSize()]; 88 byte[] reds = new byte[ 89 ((IndexColorModel ) cm).getMapSize()]; 90 byte[] greens = new byte[ 91 ((IndexColorModel ) cm).getMapSize()]; 92 byte[] blues = new byte[ 93 ((IndexColorModel ) cm).getMapSize()]; 94 ((IndexColorModel ) cm).getAlphas(alphas); 95 ((IndexColorModel ) cm).getReds(reds); 96 ((IndexColorModel ) cm).getGreens(greens); 97 ((IndexColorModel ) cm).getBlues(blues); 98 for (int i = 0; 99 i < ((IndexColorModel ) cm).getMapSize(); 100 i++) { 101 if ((alphas[i] & 0xFF) == 0) { 102 this.isTransparent = true; 103 this.transparentColor = new Color ( 104 (int)(reds[i] & 0xFF), 105 (int)(greens[i] & 0xFF), 106 (int)(blues[i] & 0xFF)); 107 break; 108 } 109 } 110 } else { 111 124 this.isTransparent = false; 125 } 126 } else { 127 this.isTransparent = false; 128 } 129 } else { 130 this.isTransparent = false; 131 } 132 133 this.bitmaps = new byte[this.width * this.height * 3]; 135 for (int i = 0; i < this.height; i++) { 136 for (int j = 0; j < this.width; j++) { 137 int p = tmpMap[i * this.width + j]; 138 int r = (p >> 16) & 0xFF; 139 int g = (p >> 8) & 0xFF; 140 int b = (p) & 0xFF; 141 this.bitmaps[3 * (i * this.width + j)] 142 = (byte)(r & 0xFF); 143 this.bitmaps[3 * (i * this.width + j) + 1] 144 = (byte)(g & 0xFF); 145 this.bitmaps[3 * (i * this.width + j) + 2] 146 = (byte)(b & 0xFF); 147 } 148 } 149 150 } catch (Exception ex) { 151 log.error("Error while loading image: " + ex.getMessage(), ex); 152 return false; 153 } finally { 154 IOUtils.closeQuietly(inputStream); 155 inputStream = null; 156 } 157 return true; 158 } 159 160 } 161 162 | Popular Tags |