1 51 package org.apache.fop.image; 52 53 import java.net.URL ; 55 import java.io.InputStream ; 56 57 import com.sun.media.jai.codec.FileCacheSeekableStream; 59 import org.apache.fop.datatypes.ColorSpace; 61 import org.apache.fop.pdf.CCFFilter; 62 import org.apache.fop.pdf.DCTFilter; 63 import org.apache.fop.image.analyser.ImageReader; 64 65 74 public class TiffImage extends JAIImage { 75 public TiffImage(URL href) throws FopImageException { 76 super(href); 77 } 78 79 public TiffImage(URL href, 80 ImageReader imgReader) throws FopImageException { 81 super(href, imgReader); 82 } 83 84 protected void loadImage() throws FopImageException { 85 try { 86 InputStream inputStream = this.m_href.openStream(); 87 91 com.sun.media.jai.codec.FileCacheSeekableStream seekableInput = 92 new FileCacheSeekableStream(inputStream); 93 94 com.sun.media.jai.codec.TIFFDirectory ifd = new com.sun.media.jai.codec.TIFFDirectory(seekableInput, 0); 95 com.sun.media.jai.codec.TIFFField fld = null; 96 97 this.m_height = (int)ifd.getFieldAsLong(0x101); 98 this.m_width = (int)ifd.getFieldAsLong(0x100); 99 100 fld = ifd.getField(0x115); if (fld != null && fld.getAsInt(0) != 1) { 102 throw new FopImageException("Error while loading image " 103 + this.m_href.toString() + " : " 104 + this.getClass() + " - " 105 + "unsupported samples per pixel value " + fld.getAsInt(0)); 106 } 107 108 this.m_bitsPerPixel = 1; 109 fld = ifd.getField(0x102); if (fld != null) { 111 this.m_bitsPerPixel = fld.getAsInt(0); 112 } 113 114 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_GRAY); 115 fld = ifd.getField(0x106); if (fld != null) { 117 if (fld.getAsInt(0) == 0) { 118 } 120 else if (fld.getAsInt(0) == 2) { 121 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 122 } 123 else { 124 throw new FopImageException("Error while loading image " 125 + this.m_href.toString() + " : " 126 + this.getClass() + " - " 127 + "unsupported photometric interpretation value " + fld.getAsInt(0)); 128 } 129 } 130 131 this.m_isTransparent = false; 132 133 int comp = 1; 134 fld = ifd.getField(0x103); if (fld != null) { 136 comp = fld.getAsInt(0); 137 } 138 if (comp == 1) { 139 } 140 else if (comp == 3) { 141 this.m_compressionType = new CCFFilter(); 142 this.m_compressionType.setApplied(true); 143 } 144 else if (comp == 4) { 145 CCFFilter ccf = new CCFFilter(); 146 this.m_compressionType = ccf; 147 this.m_compressionType.setApplied(true); 148 ccf.setDecodeParms("<< /K -1 /Columns " + this.m_width + " >>"); 149 } 150 else if (comp == 6) { 151 this.m_compressionType = new DCTFilter(); 152 this.m_compressionType.setApplied(true); 153 } 154 else { 155 throw new FopImageException("Error while loading image " 156 + this.m_href.toString() + " : " 157 + this.getClass() + " - " 158 + "unsupported compression value " + comp); 159 } 160 161 fld = ifd.getField(0x116); if (fld != null) { 163 if (this.m_height != fld.getAsLong(0)) { 164 throw new FopImageException("Error while loading image " 165 + this.m_href.toString() + " : " 166 + this.getClass() + " - " 167 + "only single strip images supported "); 168 } 169 } 170 long offset = ifd.getFieldAsLong(0x111); 171 long length = ifd.getFieldAsLong(0x117); 172 173 byte[] readBuf = new byte[(int)length]; 174 int bytes_read; 175 176 inputStream.close(); 177 inputStream = this.m_href.openStream(); 178 inputStream.skip(offset); 179 bytes_read = inputStream.read(readBuf); 180 if (bytes_read != length) { 181 throw new FopImageException("Error while loading image " 182 + this.m_href.toString() + " : " 183 + this.getClass() + " - " 184 + "length mismatch on read"); 185 } 186 187 this.m_bitmaps = readBuf; 188 } catch (FopImageException fie) { 189 org.apache.fop.messaging.MessageHandler.logln("Reverting to TIFF image handling through JAI: " 190 + fie.getMessage()); 191 this.m_compressionType = null; 192 super.loadImage(); 193 } catch (Exception ex) { 194 throw new FopImageException("Error while loading image " 195 + this.m_href.toString() + " : " 196 + ex.getClass() + " - " 197 + ex.getMessage()); 198 } 199 } 200 201 } 202 203 | Popular Tags |