1 18 19 package org.apache.batik.transcoder.image; 20 21 import java.awt.image.BufferedImage ; 22 import java.awt.image.DataBuffer ; 23 import java.awt.image.DataBufferInt ; 24 import java.awt.image.PixelInterleavedSampleModel ; 25 import java.awt.image.RenderedImage ; 26 import java.awt.image.SampleModel ; 27 import java.awt.image.SinglePixelPackedSampleModel ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 31 import org.apache.batik.ext.awt.image.GraphicsUtil; 32 import org.apache.batik.ext.awt.image.codec.tiff.TIFFEncodeParam; 33 import org.apache.batik.ext.awt.image.codec.tiff.TIFFField; 34 import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageDecoder; 35 import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageEncoder; 36 import org.apache.batik.ext.awt.image.rendered.FormatRed; 37 import org.apache.batik.transcoder.TranscoderException; 38 import org.apache.batik.transcoder.TranscoderOutput; 39 import org.apache.batik.transcoder.TranscodingHints; 40 import org.apache.batik.transcoder.image.resources.Messages; 41 42 43 49 public class TIFFTranscoder extends ImageTranscoder { 50 51 54 public TIFFTranscoder() { 55 hints.put(KEY_FORCE_TRANSPARENT_WHITE, Boolean.FALSE); 56 } 57 58 63 public BufferedImage createImage(int width, int height) { 64 return new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); 65 } 66 67 73 public void writeImage(BufferedImage img, TranscoderOutput output) 74 throws TranscoderException { 75 76 OutputStream ostream = output.getOutputStream(); 77 if (ostream == null) { 78 throw new TranscoderException( 79 Messages.formatMessage("tiff.badoutput", null)); 80 } 81 82 TIFFEncodeParam params = new TIFFEncodeParam(); 83 84 float PixSzMM = userAgent.getPixelUnitToMillimeter(); 85 int numPix = (int)(((1000*100)/PixSzMM)+0.5); 87 int denom = 100*100; long [] rational = {numPix, denom}; 89 TIFFField [] fields = { 90 new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT, 91 TIFFField.TIFF_SHORT, 1, 92 new char [] { (char)3 }), 93 new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION, 94 TIFFField.TIFF_RATIONAL, 1, 95 new long [][] { rational }), 96 new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION, 97 TIFFField.TIFF_RATIONAL, 1, 98 new long [][] { rational }) 99 }; 100 101 params.setExtraFields(fields); 102 103 boolean forceTransparentWhite = false; 108 109 if (hints.containsKey(KEY_FORCE_TRANSPARENT_WHITE)) { 110 forceTransparentWhite = 111 ((Boolean )hints.get 112 (KEY_FORCE_TRANSPARENT_WHITE)).booleanValue(); 113 } 114 115 int w = img.getWidth(); 116 int h = img.getHeight(); 117 SinglePixelPackedSampleModel sppsm; 118 sppsm = (SinglePixelPackedSampleModel )img.getSampleModel(); 119 120 if (forceTransparentWhite) { 121 DataBufferInt biDB=(DataBufferInt )img.getRaster().getDataBuffer(); 127 int scanStride = sppsm.getScanlineStride(); 128 int dbOffset = biDB.getOffset(); 129 int pixels[] = biDB.getBankData()[0]; 130 int p = dbOffset; 131 int adjust = scanStride - w; 132 int a=0, r=0, g=0, b=0, pel=0; 133 for(int i=0; i<h; i++){ 134 for(int j=0; j<w; j++){ 135 pel = pixels[p]; 136 a = (pel >> 24) & 0xff; 137 r = (pel >> 16) & 0xff; 138 g = (pel >> 8 ) & 0xff; 139 b = pel & 0xff; 140 r = (255*(255 -a) + a*r)/255; 141 g = (255*(255 -a) + a*g)/255; 142 b = (255*(255 -a) + a*b)/255; 143 pixels[p++] = 144 (a<<24 & 0xff000000) | 145 (r<<16 & 0xff0000) | 146 (g<<8 & 0xff00) | 147 (b & 0xff); 148 } 149 p += adjust; 150 } 151 } 152 153 try { 154 TIFFImageEncoder tiffEncoder = 155 new TIFFImageEncoder(ostream, params); 156 int bands = sppsm.getNumBands(); 157 int [] off = new int[bands]; 158 for (int i=0; i<bands; i++) 159 off[i] = i; 160 SampleModel sm = new PixelInterleavedSampleModel 161 (DataBuffer.TYPE_BYTE, w, h, bands, w*bands, off); 162 163 RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm); 164 tiffEncoder.encode(rimg); 165 } catch (IOException ex) { 166 throw new TranscoderException(ex); 167 } 168 } 169 170 171 175 205 public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE 206 = ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE; 207 208 } 209 | Popular Tags |