1 18 package org.apache.batik.transcoder.image; 19 20 import java.awt.image.BufferedImage ; 21 import java.awt.image.DataBufferInt ; 22 import java.awt.image.SinglePixelPackedSampleModel ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 import org.apache.batik.ext.awt.image.codec.PNGEncodeParam; 27 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder; 28 import org.apache.batik.ext.awt.image.rendered.IndexImage; 29 import org.apache.batik.transcoder.TranscoderException; 30 import org.apache.batik.transcoder.TranscoderOutput; 31 import org.apache.batik.transcoder.TranscodingHints; 32 import org.apache.batik.transcoder.image.resources.Messages; 33 import org.apache.batik.transcoder.keys.FloatKey; 34 import org.apache.batik.transcoder.keys.IntegerKey; 35 36 42 public class PNGTranscoder extends ImageTranscoder { 43 44 47 public PNGTranscoder() { 48 hints.put(KEY_FORCE_TRANSPARENT_WHITE, Boolean.FALSE); 49 } 50 51 56 public BufferedImage createImage(int width, int height) { 57 return new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); 58 } 59 60 66 public void writeImage(BufferedImage img, TranscoderOutput output) 67 throws TranscoderException { 68 69 OutputStream ostream = output.getOutputStream(); 70 if (ostream == null) { 71 throw new TranscoderException( 72 Messages.formatMessage("png.badoutput", null)); 73 } 74 75 boolean forceTransparentWhite = false; 80 81 if (hints.containsKey(KEY_FORCE_TRANSPARENT_WHITE)) { 82 forceTransparentWhite = 83 ((Boolean )hints.get 84 (KEY_FORCE_TRANSPARENT_WHITE)).booleanValue(); 85 } 86 87 if (forceTransparentWhite) { 88 int w = img.getWidth(), h = img.getHeight(); 89 DataBufferInt biDB = (DataBufferInt )img.getRaster().getDataBuffer(); 90 int scanStride = ((SinglePixelPackedSampleModel ) 91 img.getSampleModel()).getScanlineStride(); 92 int dbOffset = biDB.getOffset(); 93 int pixels[] = biDB.getBankData()[0]; 94 int p = dbOffset; 95 int adjust = scanStride - w; 96 int a=0, r=0, g=0, b=0, pel=0; 97 for(int i=0; i<h; i++){ 98 for(int j=0; j<w; j++){ 99 pel = pixels[p]; 100 a = (pel >> 24) & 0xff; 101 r = (pel >> 16) & 0xff; 102 g = (pel >> 8 ) & 0xff; 103 b = pel & 0xff; 104 r = (255*(255 -a) + a*r)/255; 105 g = (255*(255 -a) + a*g)/255; 106 b = (255*(255 -a) + a*b)/255; 107 pixels[p++] = 108 (a<<24 & 0xff000000) | 109 (r<<16 & 0xff0000) | 110 (g<<8 & 0xff00) | 111 (b & 0xff); 112 } 113 p += adjust; 114 } 115 } 116 117 int n=-1; 118 if (hints.containsKey(KEY_INDEXED)) { 119 n=((Integer )hints.get(KEY_INDEXED)).intValue(); 120 if (n==1||n==2||n==4||n==8) 121 img = IndexImage.getIndexedImage(img,1<<n); 123 } 124 125 PNGEncodeParam params = PNGEncodeParam.getDefaultEncodeParam(img); 126 if (params instanceof PNGEncodeParam.RGB) { 127 ((PNGEncodeParam.RGB)params).setBackgroundRGB 128 (new int [] { 255, 255, 255 }); 129 } 130 131 if (hints.containsKey(KEY_GAMMA)) { 136 float gamma = ((Float )hints.get(KEY_GAMMA)).floatValue(); 137 if (gamma > 0) { 138 params.setGamma(gamma); 139 } 140 params.setChromaticity(DEFAULT_CHROMA); 141 } else { 142 params.setSRGBIntent(PNGEncodeParam.INTENT_PERCEPTUAL); 145 } 146 147 148 float PixSzMM = userAgent.getPixelUnitToMillimeter(); 149 int numPix = (int)((1000/PixSzMM)+0.5); 151 params.setPhysicalDimension(numPix, numPix, 1); 153 try { 154 PNGImageEncoder pngEncoder = new PNGImageEncoder(ostream, params); 155 pngEncoder.encode(img); 156 ostream.flush(); 157 } catch (IOException ex) { 158 throw new TranscoderException(ex); 159 } 160 } 161 162 166 191 public static final TranscodingHints.Key KEY_GAMMA 192 = new FloatKey(); 193 194 197 public static final float[] DEFAULT_CHROMA = { 198 0.31270F, 0.329F, 0.64F, 0.33F, 0.3F, 0.6F, 0.15F, 0.06F 199 }; 200 201 202 227 public static final TranscodingHints.Key KEY_INDEXED 228 = new IntegerKey(); 229 } 230 | Popular Tags |