1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 21 import java.awt.Point ; 22 import java.awt.Rectangle ; 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.DataBuffer ; 29 import java.awt.image.Raster ; 30 import java.awt.image.SampleModel ; 31 import java.awt.image.SinglePixelPackedSampleModel ; 32 import java.awt.image.WritableRaster ; 33 34 import org.apache.batik.ext.awt.image.GraphicsUtil; 35 36 43 public class Any2LsRGBRed extends AbstractRed { 44 45 boolean srcIssRGB = false; 46 47 52 public Any2LsRGBRed(CachableRed src) { 53 super(src,src.getBounds(), 54 fixColorModel(src), 55 fixSampleModel(src), 56 src.getTileGridXOffset(), 57 src.getTileGridYOffset(), 58 null); 59 60 ColorModel srcCM = src.getColorModel(); 61 if (srcCM == null) return; 62 ColorSpace srcCS = srcCM.getColorSpace(); 63 if (srcCS == ColorSpace.getInstance(ColorSpace.CS_sRGB)) 64 srcIssRGB = true; 65 } 66 67 70 private static final double GAMMA = 2.4; 71 private static final double LFACT = 1.0/12.92; 72 73 74 public static final double sRGBToLsRGB(double value) { 75 if(value <= 0.003928) 76 return value*LFACT; 77 return Math.pow((value+0.055)/1.055, GAMMA); 78 } 79 80 86 private static final int sRGBToLsRGBLut[] = new int[256]; 87 static { 88 final double scale = 1.0/255; 89 90 for(int i=0; i<256; i++){ 92 double value = sRGBToLsRGB(i*scale); 93 sRGBToLsRGBLut[i] = (int)Math.round(value*255.0); 94 } 96 } 98 99 public WritableRaster copyData(WritableRaster wr) { 100 CachableRed src = (CachableRed)getSources().get(0); 102 ColorModel srcCM = src.getColorModel(); 103 SampleModel srcSM = src.getSampleModel(); 104 105 if (srcIssRGB && 107 Any2sRGBRed.is_INT_PACK_COMP(wr.getSampleModel())) { 108 src.copyData(wr); 109 if (srcCM.hasAlpha()) 110 GraphicsUtil.coerceData(wr, srcCM, false); 111 Any2sRGBRed.applyLut_INT(wr, sRGBToLsRGBLut); 112 return wr; 113 } 114 115 if (srcCM == null) { 116 119 float [][] matrix = null; 120 switch (srcSM.getNumBands()) { 121 case 1: 122 matrix = new float[1][3]; 123 matrix[0][0] = 1; matrix[0][1] = 1; matrix[0][2] = 1; break; 127 case 2: 128 matrix = new float[2][4]; 129 matrix[0][0] = 1; matrix[0][1] = 1; matrix[0][2] = 1; matrix[1][3] = 1; break; 134 case 3: 135 matrix = new float[3][3]; 136 matrix[0][0] = 1; matrix[1][1] = 1; matrix[2][2] = 1; break; 140 default: 141 matrix = new float[srcSM.getNumBands()][4]; 142 matrix[0][0] = 1; matrix[1][1] = 1; matrix[2][2] = 1; matrix[3][3] = 1; break; 147 } 148 149 Raster srcRas = src.getData(wr.getBounds()); 150 BandCombineOp op = new BandCombineOp (matrix, null); 151 op.filter(srcRas, wr); 152 } else { 153 ColorModel dstCM = getColorModel(); 154 BufferedImage dstBI; 155 156 if (!dstCM.hasAlpha()) { 157 dstBI = new BufferedImage 160 (dstCM, wr.createWritableTranslatedChild(0,0), 161 dstCM.isAlphaPremultiplied(), null); 162 } else { 163 SinglePixelPackedSampleModel dstSM; 167 dstSM = (SinglePixelPackedSampleModel )wr.getSampleModel(); 168 int [] masks = dstSM.getBitMasks(); 169 SampleModel dstSMNoA = new SinglePixelPackedSampleModel 170 (dstSM.getDataType(), dstSM.getWidth(), dstSM.getHeight(), 171 dstSM.getScanlineStride(), 172 new int[] {masks[0], masks[1], masks[2]}); 173 ColorModel dstCMNoA = GraphicsUtil.Linear_sRGB; 174 175 WritableRaster dstWr; 176 dstWr = Raster.createWritableRaster(dstSMNoA, 177 wr.getDataBuffer(), 178 new Point (0,0)); 179 dstWr = dstWr.createWritableChild 180 (wr.getMinX()-wr.getSampleModelTranslateX(), 181 wr.getMinY()-wr.getSampleModelTranslateY(), 182 wr.getWidth(), wr.getHeight(), 183 0, 0, null); 184 185 dstBI = new BufferedImage (dstCMNoA, dstWr, false, null); 186 } 187 188 ColorModel srcBICM = srcCM; 192 WritableRaster srcWr; 193 if ((srcCM.hasAlpha() == true) && 194 (srcCM.isAlphaPremultiplied() != false)) { 195 Rectangle wrR = wr.getBounds(); 196 SampleModel sm = srcCM.createCompatibleSampleModel 197 (wrR.width, wrR.height); 198 199 srcWr = Raster.createWritableRaster 200 (sm, new Point (wrR.x, wrR.y)); 201 src.copyData(srcWr); 202 srcBICM = GraphicsUtil.coerceData(srcWr, srcCM, false); 203 } else { 204 Raster srcRas = src.getData(wr.getBounds()); 205 srcWr = GraphicsUtil.makeRasterWritable(srcRas); 206 } 207 208 BufferedImage srcBI; 209 srcBI = new BufferedImage (srcBICM, 210 srcWr.createWritableTranslatedChild(0,0), 211 false, 212 null); 213 214 220 221 ColorConvertOp op = new ColorConvertOp (null); 222 op.filter(srcBI, dstBI); 223 224 if (dstCM.hasAlpha()) 225 copyBand(srcWr, srcSM.getNumBands()-1, 226 wr, getSampleModel().getNumBands()-1); 227 } 228 return wr; 229 } 230 231 236 protected static ColorModel fixColorModel(CachableRed src) { 237 ColorModel cm = src.getColorModel(); 238 if (cm != null) { 239 if (cm.hasAlpha()) 240 return GraphicsUtil.Linear_sRGB_Unpre; 241 242 return GraphicsUtil.Linear_sRGB; 243 } 244 else { 245 SampleModel sm = src.getSampleModel(); 252 253 switch (sm.getNumBands()) { 254 case 1: 255 return GraphicsUtil.Linear_sRGB; 256 case 2: 257 return GraphicsUtil.Linear_sRGB_Unpre; 258 case 3: 259 return GraphicsUtil.Linear_sRGB; 260 } 261 return GraphicsUtil.Linear_sRGB_Unpre; 262 } 263 } 264 265 270 protected static SampleModel fixSampleModel(CachableRed src) { 271 SampleModel sm = src.getSampleModel(); 272 ColorModel cm = src.getColorModel(); 273 274 boolean alpha = false; 275 276 if (cm != null) 277 alpha = cm.hasAlpha(); 278 else { 279 switch (sm.getNumBands()) { 280 case 1: case 3: 281 alpha = false; 282 break; 283 default: 284 alpha = true; 285 break; 286 } 287 } 288 if (alpha) 289 return new SinglePixelPackedSampleModel 290 (DataBuffer.TYPE_INT, 291 sm.getWidth(), 292 sm.getHeight(), 293 new int [] {0xFF0000, 0xFF00, 0xFF, 0xFF000000}); 294 else 295 return new SinglePixelPackedSampleModel 296 (DataBuffer.TYPE_INT, 297 sm.getWidth(), 298 sm.getHeight(), 299 new int [] {0xFF0000, 0xFF00, 0xFF}); 300 } 301 } 302 | Popular Tags |