1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 21 import java.awt.color.ColorSpace ; 22 import java.awt.image.BandCombineOp ; 23 import java.awt.image.BufferedImage ; 24 import java.awt.image.ColorConvertOp ; 25 import java.awt.image.ColorModel ; 26 import java.awt.image.DataBuffer ; 27 import java.awt.image.DataBufferInt ; 28 import java.awt.image.Raster ; 29 import java.awt.image.SampleModel ; 30 import java.awt.image.SinglePixelPackedSampleModel ; 31 import java.awt.image.WritableRaster ; 32 33 import org.apache.batik.ext.awt.image.GraphicsUtil; 34 35 42 public class Any2sRGBRed extends AbstractRed { 43 44 boolean srcIsLsRGB = false; 45 46 51 public Any2sRGBRed(CachableRed src) { 52 super(src,src.getBounds(), 53 fixColorModel(src), 54 fixSampleModel(src), 55 src.getTileGridXOffset(), 56 src.getTileGridYOffset(), 57 null); 58 59 ColorModel srcCM = src.getColorModel(); 60 if (srcCM == null) return; 61 ColorSpace srcCS = srcCM.getColorSpace(); 62 if (srcCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) 63 srcIsLsRGB = true; 64 } 65 66 public static boolean is_INT_PACK_COMP(SampleModel sm) { 67 if(!(sm instanceof SinglePixelPackedSampleModel )) return false; 68 69 if(sm.getDataType() != DataBuffer.TYPE_INT) return false; 71 72 SinglePixelPackedSampleModel sppsm; 73 sppsm = (SinglePixelPackedSampleModel )sm; 74 75 int [] masks = sppsm.getBitMasks(); 76 if ((masks.length != 3) && (masks.length != 4)) return false; 77 if(masks[0] != 0x00ff0000) return false; 78 if(masks[1] != 0x0000ff00) return false; 79 if(masks[2] != 0x000000ff) return false; 80 if ((masks.length == 4) && 81 (masks[3] != 0xff000000)) return false; 82 83 return true; 84 } 85 86 89 private static final double GAMMA = 2.4; 90 91 97 private static final int linearToSRGBLut[] = new int[256]; 98 static { 99 final double scale = 1.0/255; 100 final double exp = 1.0/GAMMA; 101 for(int i=0; i<256; i++){ 103 double value = i*scale; 104 if(value <= 0.0031308) 105 value *= 12.92; 106 else 107 value = 1.055 * Math.pow(value, exp) - 0.055; 108 109 linearToSRGBLut[i] = (int)Math.round(value*255.); 110 } 112 } 114 115 public static WritableRaster applyLut_INT(WritableRaster wr, 116 final int []lut) { 117 SinglePixelPackedSampleModel sm = 118 (SinglePixelPackedSampleModel )wr.getSampleModel(); 119 DataBufferInt db = (DataBufferInt )wr.getDataBuffer(); 120 121 final int srcBase 122 = (db.getOffset() + 123 sm.getOffset(wr.getMinX()-wr.getSampleModelTranslateX(), 124 wr.getMinY()-wr.getSampleModelTranslateY())); 125 final int pixels[] = db.getBankData()[0]; 127 final int width = wr.getWidth(); 128 final int height = wr.getHeight(); 129 final int scanStride = sm.getScanlineStride(); 130 131 int end, pix; 132 133 for (int y=0; y<height; y++) { 135 int sp = srcBase + y*scanStride; 136 end = sp + width; 137 138 while (sp<end) { 139 pix = pixels[sp]; 140 pixels[sp] = 141 (( pix &0xFF000000)| 142 (lut[(pix>>>16)&0xFF]<<16) | 143 (lut[(pix>>> 8)&0xFF]<< 8) | 144 (lut[(pix )&0xFF] )); 145 sp++; 146 } 147 } 148 149 return wr; 150 } 151 152 public WritableRaster copyData(WritableRaster wr) { 153 154 CachableRed src = (CachableRed)getSources().get(0); 156 ColorModel srcCM = src.getColorModel(); 157 SampleModel srcSM = src.getSampleModel(); 158 159 160 if (srcIsLsRGB && 162 is_INT_PACK_COMP(wr.getSampleModel())) { 163 src.copyData(wr); 164 if (srcCM.hasAlpha()) 165 GraphicsUtil.coerceData(wr, srcCM, false); 166 applyLut_INT(wr, linearToSRGBLut); 167 return wr; 168 } 169 170 if (srcCM == null) { 171 174 float [][] matrix = null; 175 switch (srcSM.getNumBands()) { 176 case 1: 177 matrix = new float[1][3]; 178 matrix[0][0] = 1; matrix[0][1] = 1; matrix[0][2] = 1; break; 182 case 2: 183 matrix = new float[2][4]; 184 matrix[0][0] = 1; matrix[0][1] = 1; matrix[0][2] = 1; matrix[1][3] = 1; break; 189 case 3: 190 matrix = new float[3][3]; 191 matrix[0][0] = 1; matrix[1][1] = 1; matrix[2][2] = 1; break; 195 default: 196 matrix = new float[srcSM.getNumBands()][4]; 197 matrix[0][0] = 1; matrix[1][1] = 1; matrix[2][2] = 1; matrix[3][3] = 1; break; 202 } 203 Raster srcRas = src.getData(wr.getBounds()); 204 BandCombineOp op = new BandCombineOp (matrix, null); 205 op.filter(srcRas, wr); 206 } else { 207 ColorModel dstCM = getColorModel(); 208 if (srcCM.getColorSpace() == dstCM.getColorSpace()) { 209 212 if (is_INT_PACK_COMP(srcSM)) 213 src.copyData(wr); 214 else 215 GraphicsUtil.copyData(src.getData(wr.getBounds()), wr); 216 217 return wr; 218 } 219 220 Raster srcRas = src.getData(wr.getBounds()); 221 WritableRaster srcWr = (WritableRaster )srcRas; 222 223 ColorModel srcBICM = srcCM; 227 if (srcCM.hasAlpha()) 228 srcBICM = GraphicsUtil.coerceData(srcWr, srcCM, false); 229 230 BufferedImage srcBI, dstBI; 231 srcBI = new BufferedImage (srcBICM, 232 srcWr.createWritableTranslatedChild(0,0), 233 false, 234 null); 235 236 239 ColorConvertOp op = new ColorConvertOp (dstCM.getColorSpace(), 240 null); 241 dstBI = op.filter(srcBI, null); 242 243 245 WritableRaster wr00 = wr.createWritableTranslatedChild(0,0); 246 for (int i=0; i<dstCM.getColorSpace().getNumComponents(); i++) 247 copyBand(dstBI.getRaster(), i, wr00, i); 248 249 if (dstCM.hasAlpha()) 250 copyBand(srcWr, srcSM.getNumBands()-1, 251 wr, getSampleModel().getNumBands()-1); 252 } 253 return wr; 254 } 255 256 261 protected static ColorModel fixColorModel(CachableRed src) { 262 ColorModel cm = src.getColorModel(); 263 if (cm != null) { 264 if (cm.hasAlpha()) 265 return GraphicsUtil.sRGB_Unpre; 266 267 return GraphicsUtil.sRGB; 268 } 269 else { 270 SampleModel sm = src.getSampleModel(); 277 278 switch (sm.getNumBands()) { 279 case 1: 280 return GraphicsUtil.sRGB; 281 case 2: 282 return GraphicsUtil.sRGB_Unpre; 283 case 3: 284 return GraphicsUtil.sRGB; 285 } 286 return GraphicsUtil.sRGB_Unpre; 287 } 288 } 289 290 295 protected static SampleModel fixSampleModel(CachableRed src) { 296 SampleModel sm = src.getSampleModel(); 297 ColorModel cm = src.getColorModel(); 298 299 boolean alpha = false; 300 301 if (cm != null) 302 alpha = cm.hasAlpha(); 303 else { 304 switch (sm.getNumBands()) { 305 case 1: case 3: 306 alpha = false; 307 break; 308 default: 309 alpha = true; 310 break; 311 } 312 } 313 if (alpha) 314 return new SinglePixelPackedSampleModel 315 (DataBuffer.TYPE_INT, 316 sm.getWidth(), 317 sm.getHeight(), 318 new int [] {0xFF0000, 0xFF00, 0xFF, 0xFF000000}); 319 else 320 return new SinglePixelPackedSampleModel 321 (DataBuffer.TYPE_INT, 322 sm.getWidth(), 323 sm.getHeight(), 324 new int [] {0xFF0000, 0xFF00, 0xFF}); 325 } 326 } 327 | Popular Tags |