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.BufferedImage ; 25 import java.awt.image.ColorModel ; 26 import java.awt.image.ComponentColorModel ; 27 import java.awt.image.ComponentSampleModel ; 28 import java.awt.image.DataBuffer ; 29 import java.awt.image.DirectColorModel ; 30 import java.awt.image.Raster ; 31 import java.awt.image.SampleModel ; 32 import java.awt.image.SinglePixelPackedSampleModel ; 33 import java.awt.image.WritableRaster ; 34 35 import org.apache.batik.ext.awt.image.GraphicsUtil; 36 37 38 46 public class FormatRed extends AbstractRed { 47 48 public static CachableRed construct(CachableRed src, ColorModel cm) { 49 ColorModel srcCM = src.getColorModel(); 50 if ((cm.hasAlpha() != srcCM.hasAlpha()) || 51 (cm.isAlphaPremultiplied() != srcCM.isAlphaPremultiplied())) 52 return new FormatRed(src, cm); 53 54 if (cm.getNumComponents() != srcCM.getNumComponents()) 55 throw new IllegalArgumentException 56 ("Incompatible ColorModel given"); 57 58 59 if ((srcCM instanceof ComponentColorModel ) && 60 (cm instanceof ComponentColorModel )) 61 return src; 62 63 if ((srcCM instanceof DirectColorModel ) && 64 (cm instanceof DirectColorModel )) 65 return src; 66 67 return new FormatRed(src, cm); 68 } 69 70 73 public FormatRed(CachableRed cr, SampleModel sm) { 74 super(cr, cr.getBounds(), 75 makeColorModel(cr, sm), sm, 76 cr.getTileGridXOffset(), 77 cr.getTileGridYOffset(), 78 null); 79 } 80 81 public FormatRed(CachableRed cr, ColorModel cm) { 82 super(cr, cr.getBounds(), 83 cm, makeSampleModel(cr, cm), 84 cr.getTileGridXOffset(), 85 cr.getTileGridYOffset(), 86 null); 87 } 88 89 92 public CachableRed getSource() { 93 return (CachableRed)getSources().get(0); 94 } 95 96 public Object getProperty(String name) { 97 return getSource().getProperty(name); 98 } 99 100 public String [] getPropertyNames() { 101 return getSource().getPropertyNames(); 102 } 103 104 public WritableRaster copyData(WritableRaster wr) { 105 ColorModel cm = getColorModel(); 106 CachableRed cr = getSource(); 107 ColorModel srcCM = cr.getColorModel(); 108 SampleModel srcSM = cr.getSampleModel(); 109 srcSM = srcSM.createCompatibleSampleModel(wr.getWidth(), 110 wr.getHeight()); 111 WritableRaster srcWR; 112 srcWR = Raster.createWritableRaster(srcSM, new Point (wr.getMinX(), 113 wr.getMinY())); 114 getSource().copyData(srcWR); 115 116 BufferedImage srcBI = new BufferedImage 117 (srcCM, srcWR.createWritableTranslatedChild(0,0), 118 srcCM.isAlphaPremultiplied(), null); 119 BufferedImage dstBI = new BufferedImage 120 (cm, wr.createWritableTranslatedChild(0,0), 121 cm.isAlphaPremultiplied(), null); 122 123 GraphicsUtil.copyData(srcBI, dstBI); 124 125 return wr; 126 } 127 128 public static SampleModel makeSampleModel(CachableRed cr, ColorModel cm) { 129 SampleModel srcSM = cr.getSampleModel(); 130 return cm.createCompatibleSampleModel(srcSM.getWidth(), 131 srcSM.getHeight()); 132 } 133 134 public static ColorModel makeColorModel(CachableRed cr, SampleModel sm) { 135 ColorModel srcCM = cr.getColorModel(); 136 ColorSpace cs = srcCM.getColorSpace(); 137 138 int bands = sm.getNumBands(); 139 140 int bits; 141 int dt = sm.getDataType(); 142 switch (dt) { 143 case DataBuffer.TYPE_BYTE: bits=8; break; 144 case DataBuffer.TYPE_SHORT: bits=16; break; 145 case DataBuffer.TYPE_USHORT: bits=16; break; 146 case DataBuffer.TYPE_INT: bits=32; break; 147 default: 148 throw new IllegalArgumentException 149 ("Unsupported DataBuffer type: " + dt); 150 } 151 152 boolean hasAlpha = srcCM.hasAlpha(); 153 if (hasAlpha){ 154 if (bands == srcCM.getNumComponents()-1) 158 hasAlpha = false; 159 else if (bands != srcCM.getNumComponents()) 160 throw new IllegalArgumentException 161 ("Incompatible number of bands in and out"); 162 } else { 163 if (bands == srcCM.getNumComponents()+1) 164 hasAlpha = true; 165 else if (bands != srcCM.getNumComponents()) 166 throw new IllegalArgumentException 167 ("Incompatible number of bands in and out"); 168 } 169 170 boolean preMult = srcCM.isAlphaPremultiplied(); 171 if (!hasAlpha) 172 preMult = false; 173 174 if (sm instanceof ComponentSampleModel ) { 175 int [] bitsPer = new int[bands]; 176 for (int i=0; i<bands; i++) 177 bitsPer[i] = bits; 178 179 return new ComponentColorModel 180 (cs, bitsPer, hasAlpha, preMult, 181 hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, 182 dt); 183 } else if (sm instanceof SinglePixelPackedSampleModel ) { 184 SinglePixelPackedSampleModel sppsm; 185 sppsm = (SinglePixelPackedSampleModel )sm; 186 int masks [] = sppsm.getBitMasks(); 187 if (bands == 4) 188 return new DirectColorModel 189 (cs, bits, masks[0], masks[1], masks[2], masks[3], 190 preMult, dt); 191 else if (bands == 3) 192 return new DirectColorModel 193 (cs, bits, masks[0], masks[1], masks[2], 0x0, 194 preMult, dt); 195 else 196 throw new IllegalArgumentException 197 ("Incompatible number of bands out for ColorModel"); 198 } 199 throw new IllegalArgumentException 200 ("Unsupported SampleModel Type"); 201 } 202 } 203 | Popular Tags |