1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.Graphics2D ; 21 import java.awt.Point ; 22 import java.awt.RenderingHints ; 23 import java.awt.Transparency ; 24 import java.awt.color.ColorSpace ; 25 import java.awt.image.BandedSampleModel ; 26 import java.awt.image.BufferedImage ; 27 import java.awt.image.ColorConvertOp ; 28 import java.awt.image.ColorModel ; 29 import java.awt.image.ComponentColorModel ; 30 import java.awt.image.DataBuffer ; 31 import java.awt.image.DataBufferByte ; 32 import java.awt.image.DirectColorModel ; 33 import java.awt.image.Raster ; 34 import java.awt.image.RenderedImage ; 35 import java.awt.image.WritableRaster ; 36 37 import org.apache.batik.ext.awt.color.ICCColorSpaceExt; 38 39 40 47 public class ProfileRed extends AbstractRed { 48 private static final ColorSpace sRGBCS 49 = ColorSpace.getInstance(ColorSpace.CS_sRGB); 50 private static final ColorModel sRGBCM 51 = new DirectColorModel (sRGBCS, 52 32, 53 0x00ff0000, 54 0x0000ff00, 55 0x000000ff, 56 0xff000000, 57 false, 58 DataBuffer.TYPE_INT); 59 60 private ICCColorSpaceExt colorSpace; 61 62 68 public ProfileRed(CachableRed src, 69 ICCColorSpaceExt colorSpace){ 70 this.colorSpace = colorSpace; 71 72 init(src, src.getBounds(), 73 sRGBCM, sRGBCM.createCompatibleSampleModel(src.getWidth(), 74 src.getHeight()), 75 src.getTileGridXOffset(), src.getTileGridYOffset(), null); 76 77 } 78 79 public CachableRed getSource() { 80 return (CachableRed)getSources().get(0); 81 } 82 83 111 public WritableRaster copyData(WritableRaster argbWR){ 112 try{ 113 RenderedImage img = getSource(); 114 115 119 ColorModel imgCM = img.getColorModel(); 120 ColorSpace imgCS = imgCM.getColorSpace(); 121 int nImageComponents = imgCS.getNumComponents(); 122 int nProfileComponents = colorSpace.getNumComponents(); 123 if(nImageComponents != nProfileComponents){ 124 System.err.println("Input image and associated color profile have" + 127 " mismatching number of color components: conversion is not possible"); 128 return argbWR; 129 } 130 131 134 int w = argbWR.getWidth(); 135 int h = argbWR.getHeight(); 136 int minX = argbWR.getMinX(); 137 int minY = argbWR.getMinY(); 138 WritableRaster srcWR = 139 imgCM.createCompatibleWritableRaster(w, h); 140 srcWR = srcWR.createWritableTranslatedChild(minX, minY); 141 img.copyData(srcWR); 142 143 147 if(!(imgCM instanceof ComponentColorModel ) || 148 !(img.getSampleModel() instanceof BandedSampleModel ) || 149 (imgCM.hasAlpha() && 150 imgCM.isAlphaPremultiplied() == true)) { 151 ComponentColorModel imgCompCM 152 = new ComponentColorModel 153 (imgCS, imgCM.getComponentSize(), imgCM.hasAlpha(), false, imgCM.getTransparency(), DataBuffer.TYPE_BYTE); 160 WritableRaster wr = Raster.createBandedRaster 161 (DataBuffer.TYPE_BYTE, 162 argbWR.getWidth(), argbWR.getHeight(), 163 imgCompCM.getNumComponents(), 164 new Point (0, 0)); 165 166 BufferedImage imgComp = new BufferedImage 167 (imgCompCM, wr, imgCompCM.isAlphaPremultiplied(), null); 168 169 BufferedImage srcImg = new BufferedImage 170 (imgCM, srcWR.createWritableTranslatedChild(0, 0), 171 imgCM.isAlphaPremultiplied(), null); 172 173 Graphics2D g = imgComp.createGraphics(); 174 g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, 175 RenderingHints.VALUE_COLOR_RENDER_QUALITY); 176 g.drawImage(srcImg, 0, 0, null); 177 img = imgComp; 178 imgCM = imgCompCM; 179 srcWR = wr.createWritableTranslatedChild(minX, minY); 180 } 181 182 188 ComponentColorModel newCM = new ComponentColorModel 189 (colorSpace, imgCM.getComponentSize(), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); 196 DataBufferByte data = (DataBufferByte )srcWR.getDataBuffer(); 198 srcWR = Raster.createBandedRaster 199 (data, argbWR.getWidth(), argbWR.getHeight(), 200 argbWR.getWidth(), new int[]{0, 1, 2}, 201 new int[]{0, 0, 0}, new Point (0, 0)); 202 BufferedImage newImg = new BufferedImage 203 (newCM, srcWR, newCM.isAlphaPremultiplied(), null); 204 205 208 ComponentColorModel sRGBCompCM = new ComponentColorModel 209 (ColorSpace.getInstance(ColorSpace.CS_sRGB), 210 new int[]{8, 8, 8}, 211 false, 212 false, 213 Transparency.OPAQUE, 214 DataBuffer.TYPE_BYTE); 215 216 WritableRaster wr = Raster.createBandedRaster 217 (DataBuffer.TYPE_BYTE, argbWR.getWidth(), argbWR.getHeight(), 218 sRGBCompCM.getNumComponents(), new Point (0, 0)); 219 220 BufferedImage sRGBImage = new BufferedImage 221 (sRGBCompCM, wr, false, null); 222 ColorConvertOp colorConvertOp = new ColorConvertOp (null); 223 colorConvertOp.filter(newImg, sRGBImage); 224 225 228 if (imgCM.hasAlpha()){ 229 DataBufferByte rgbData = (DataBufferByte )wr.getDataBuffer(); 230 byte[][] imgBanks = data.getBankData(); 231 byte[][] rgbBanks = rgbData.getBankData(); 232 233 byte[][] argbBanks = {rgbBanks[0], rgbBanks[1], 234 rgbBanks[2], imgBanks[3]}; 235 DataBufferByte argbData = new DataBufferByte (argbBanks, imgBanks[0].length); 236 srcWR = Raster.createBandedRaster 237 (argbData, argbWR.getWidth(), argbWR.getHeight(), 238 argbWR.getWidth(), new int[]{0, 1, 2, 3}, 239 new int[]{0, 0, 0, 0}, new Point (0, 0)); 240 sRGBCompCM = new ComponentColorModel 241 (ColorSpace.getInstance(ColorSpace.CS_sRGB), 242 new int[]{8, 8, 8, 8}, 243 true, 244 false, 245 Transparency.TRANSLUCENT, 246 DataBuffer.TYPE_BYTE); 247 sRGBImage = new BufferedImage (sRGBCompCM, 248 srcWR, 249 false, 250 null); 251 252 } 253 254 257 BufferedImage result = new BufferedImage (sRGBCM, 258 argbWR.createWritableTranslatedChild(0, 0), 259 false, 260 null); 261 262 263 Graphics2D g = result.createGraphics(); 270 g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, 271 RenderingHints.VALUE_COLOR_RENDER_QUALITY); 272 g.drawImage(sRGBImage, 0, 0, null); 273 g.dispose(); 274 275 return argbWR; 276 }catch(Exception e){ 277 e.printStackTrace(); 278 throw new Error (); 279 } 280 } 281 282 } 283 | Popular Tags |