1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 21 import java.awt.Point ; 22 import java.awt.Transparency ; 23 import java.awt.color.ColorSpace ; 24 import java.awt.image.BandCombineOp ; 25 import java.awt.image.BufferedImage ; 26 import java.awt.image.ColorConvertOp ; 27 import java.awt.image.ColorModel ; 28 import java.awt.image.ComponentColorModel ; 29 import java.awt.image.DataBuffer ; 30 import java.awt.image.PixelInterleavedSampleModel ; 31 import java.awt.image.Raster ; 32 import java.awt.image.SampleModel ; 33 import java.awt.image.WritableRaster ; 34 35 import org.apache.batik.ext.awt.ColorSpaceHintKey; 36 import org.apache.batik.ext.awt.image.GraphicsUtil; 37 38 45 public class Any2LumRed extends AbstractRed { 46 47 52 public Any2LumRed(CachableRed src) { 53 super(src,src.getBounds(), 54 fixColorModel(src), 55 fixSampleModel(src), 56 src.getTileGridXOffset(), 57 src.getTileGridYOffset(), 58 null); 59 60 props.put(ColorSpaceHintKey.PROPERTY_COLORSPACE, 61 ColorSpaceHintKey.VALUE_COLORSPACE_GREY); 62 } 63 64 public WritableRaster copyData(WritableRaster wr) { 65 CachableRed src = (CachableRed)getSources().get(0); 67 68 SampleModel sm = src.getSampleModel(); 69 ColorModel srcCM = src.getColorModel(); 70 Raster srcRas = src.getData(wr.getBounds()); 71 if (srcCM == null) { 72 74 float [][] matrix = null; 75 if (sm.getNumBands() == 2) { 76 matrix = new float[2][2]; 77 matrix[0][0] = 1; 78 matrix[1][1] = 1; 79 } else { 80 matrix = new float[sm.getNumBands()][1]; 81 matrix[0][0] = 1; 82 } 83 84 BandCombineOp op = new BandCombineOp (matrix, null); 85 op.filter(srcRas, wr); 86 } else { 87 WritableRaster srcWr = (WritableRaster )srcRas; 88 89 if (srcCM.hasAlpha()) 93 GraphicsUtil.coerceData(srcWr, srcCM, false); 94 95 BufferedImage srcBI, dstBI; 96 srcBI = new BufferedImage (srcCM, 97 srcWr.createWritableTranslatedChild(0,0), 98 false, 99 null); 100 ColorModel dstCM = getColorModel(); 101 if (!dstCM.hasAlpha()) { 102 dstBI = new BufferedImage 105 (dstCM, wr.createWritableTranslatedChild(0,0), 106 dstCM.isAlphaPremultiplied(), null); 107 } else { 108 PixelInterleavedSampleModel dstSM; 112 dstSM = (PixelInterleavedSampleModel )wr.getSampleModel(); 113 SampleModel smna = new PixelInterleavedSampleModel 114 (dstSM.getDataType(), 115 dstSM.getWidth(), dstSM.getHeight(), 116 dstSM.getPixelStride(), dstSM.getScanlineStride(), 117 new int [] { 0 }); 118 119 WritableRaster dstWr; 120 dstWr = Raster.createWritableRaster(smna, 121 wr.getDataBuffer(), 122 new Point (0,0)); 123 dstWr = dstWr.createWritableChild 124 (wr.getMinX()-wr.getSampleModelTranslateX(), 125 wr.getMinY()-wr.getSampleModelTranslateY(), 126 wr.getWidth(), wr.getHeight(), 127 0, 0, null); 128 129 ColorModel cmna = new ComponentColorModel 130 (ColorSpace.getInstance(ColorSpace.CS_GRAY), 131 new int [] {8}, false, false, 132 Transparency.OPAQUE, 133 DataBuffer.TYPE_BYTE); 134 135 dstBI = new BufferedImage (cmna, dstWr, false, null); 136 } 137 138 ColorConvertOp op = new ColorConvertOp (null); 139 op.filter(srcBI, dstBI); 140 141 if (dstCM.hasAlpha()) { 143 copyBand(srcWr, sm.getNumBands()-1, 144 wr, getSampleModel().getNumBands()-1); 145 if (dstCM.isAlphaPremultiplied()) 146 GraphicsUtil.multiplyAlpha(wr); 147 } 148 } 149 return wr; 150 } 151 152 157 protected static ColorModel fixColorModel(CachableRed src) { 158 ColorModel cm = src.getColorModel(); 159 if (cm != null) { 160 if (cm.hasAlpha()) 161 return new ComponentColorModel 162 (ColorSpace.getInstance(ColorSpace.CS_GRAY), 163 new int [] {8,8}, true, 164 cm.isAlphaPremultiplied(), 165 Transparency.TRANSLUCENT, 166 DataBuffer.TYPE_BYTE); 167 168 return new ComponentColorModel 169 (ColorSpace.getInstance(ColorSpace.CS_GRAY), 170 new int [] {8}, false, false, 171 Transparency.OPAQUE, 172 DataBuffer.TYPE_BYTE); 173 } 174 else { 175 SampleModel sm = src.getSampleModel(); 181 182 if (sm.getNumBands() == 2) 183 return new ComponentColorModel 184 (ColorSpace.getInstance(ColorSpace.CS_GRAY), 185 new int [] {8,8}, true, 186 true, Transparency.TRANSLUCENT, 187 DataBuffer.TYPE_BYTE); 188 189 return new ComponentColorModel 190 (ColorSpace.getInstance(ColorSpace.CS_GRAY), 191 new int [] {8}, false, false, 192 Transparency.OPAQUE, 193 DataBuffer.TYPE_BYTE); 194 } 195 } 196 197 202 protected static SampleModel fixSampleModel(CachableRed src) { 203 SampleModel sm = src.getSampleModel(); 204 205 int width = sm.getWidth(); 206 int height = sm.getHeight(); 207 208 ColorModel cm = src.getColorModel(); 209 if (cm != null) { 210 if (cm.hasAlpha()) 211 return new PixelInterleavedSampleModel 212 (DataBuffer.TYPE_BYTE, width, height, 2, 2*width, 213 new int [] { 0, 1 }); 214 215 return new PixelInterleavedSampleModel 216 (DataBuffer.TYPE_BYTE, width, height, 1, width, 217 new int [] { 0 }); 218 } 219 else { 220 if (sm.getNumBands() == 2) 226 return new PixelInterleavedSampleModel 227 (DataBuffer.TYPE_BYTE, width, height, 2, 2*width, 228 new int [] { 0, 1 }); 229 230 return new PixelInterleavedSampleModel 231 (DataBuffer.TYPE_BYTE, width, height, 1, width, 232 new int [] { 0 }); 233 } 234 } 235 } 236 | Popular Tags |