KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > FormatRed


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.rendered;
19
20
21 import java.awt.Point JavaDoc;
22 import java.awt.Transparency JavaDoc;
23 import java.awt.color.ColorSpace JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.awt.image.ComponentColorModel JavaDoc;
27 import java.awt.image.ComponentSampleModel JavaDoc;
28 import java.awt.image.DataBuffer JavaDoc;
29 import java.awt.image.DirectColorModel JavaDoc;
30 import java.awt.image.Raster JavaDoc;
31 import java.awt.image.SampleModel JavaDoc;
32 import java.awt.image.SinglePixelPackedSampleModel JavaDoc;
33 import java.awt.image.WritableRaster JavaDoc;
34
35 import org.apache.batik.ext.awt.image.GraphicsUtil;
36
37
38 /**
39  * This allows you to specify the ColorModel, Alpha premult and/or
40  * SampleModel to be used for output. If the input image lacks
41  * Alpha and alpha is included in output then it is filled with
42  * alpha=1. In all other cases bands are simply copied.
43  *
44  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
45  * @version $Id: FormatRed.java,v 1.5 2004/08/18 07:14:08 vhardy Exp $ */

46 public class FormatRed extends AbstractRed {
47
48     public static CachableRed construct(CachableRed src, ColorModel JavaDoc cm) {
49         ColorModel JavaDoc 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 JavaDoc
56                 ("Incompatible ColorModel given");
57                 
58
59         if ((srcCM instanceof ComponentColorModel JavaDoc) &&
60             (cm instanceof ComponentColorModel JavaDoc))
61             return src;
62
63         if ((srcCM instanceof DirectColorModel JavaDoc) &&
64             (cm instanceof DirectColorModel JavaDoc))
65             return src;
66
67         return new FormatRed(src, cm);
68     }
69     
70     /**
71      * Construct an instance of CachableRed around a BufferedImage.
72      */

73     public FormatRed(CachableRed cr, SampleModel JavaDoc 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 JavaDoc cm) {
82         super(cr, cr.getBounds(),
83               cm, makeSampleModel(cr, cm),
84               cr.getTileGridXOffset(),
85               cr.getTileGridYOffset(),
86               null);
87     }
88
89     /**
90      * fetch the source image for this node.
91      */

92     public CachableRed getSource() {
93         return (CachableRed)getSources().get(0);
94     }
95
96     public Object JavaDoc getProperty(String JavaDoc name) {
97         return getSource().getProperty(name);
98     }
99
100     public String JavaDoc [] getPropertyNames() {
101         return getSource().getPropertyNames();
102     }
103
104     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
105         ColorModel JavaDoc cm = getColorModel();
106         CachableRed cr = getSource();
107         ColorModel JavaDoc srcCM = cr.getColorModel();
108         SampleModel JavaDoc srcSM = cr.getSampleModel();
109         srcSM = srcSM.createCompatibleSampleModel(wr.getWidth(),
110                                                   wr.getHeight());
111         WritableRaster JavaDoc srcWR;
112         srcWR = Raster.createWritableRaster(srcSM, new Point JavaDoc(wr.getMinX(),
113                                                              wr.getMinY()));
114         getSource().copyData(srcWR);
115         
116         BufferedImage JavaDoc srcBI = new BufferedImage JavaDoc
117             (srcCM, srcWR.createWritableTranslatedChild(0,0),
118              srcCM.isAlphaPremultiplied(), null);
119         BufferedImage JavaDoc dstBI = new BufferedImage JavaDoc
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 JavaDoc makeSampleModel(CachableRed cr, ColorModel JavaDoc cm) {
129         SampleModel JavaDoc srcSM = cr.getSampleModel();
130         return cm.createCompatibleSampleModel(srcSM.getWidth(),
131                                               srcSM.getHeight());
132     }
133
134     public static ColorModel JavaDoc makeColorModel(CachableRed cr, SampleModel JavaDoc sm) {
135         ColorModel JavaDoc srcCM = cr.getColorModel();
136         ColorSpace JavaDoc 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 JavaDoc
149                 ("Unsupported DataBuffer type: " + dt);
150         }
151
152         boolean hasAlpha = srcCM.hasAlpha();
153         if (hasAlpha){
154             // if Src has Alpha then our out bands must
155
// either be one less than the source (no out alpha)
156
// or equal (still has alpha)
157
if (bands == srcCM.getNumComponents()-1)
158                 hasAlpha = false;
159             else if (bands != srcCM.getNumComponents())
160                 throw new IllegalArgumentException JavaDoc
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 JavaDoc
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 JavaDoc) {
175             int [] bitsPer = new int[bands];
176             for (int i=0; i<bands; i++)
177                 bitsPer[i] = bits;
178
179             return new ComponentColorModel JavaDoc
180                 (cs, bitsPer, hasAlpha, preMult,
181                  hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE,
182                  dt);
183         } else if (sm instanceof SinglePixelPackedSampleModel JavaDoc) {
184             SinglePixelPackedSampleModel JavaDoc sppsm;
185             sppsm = (SinglePixelPackedSampleModel JavaDoc)sm;
186             int masks [] = sppsm.getBitMasks();
187             if (bands == 4)
188                 return new DirectColorModel JavaDoc
189                     (cs, bits, masks[0], masks[1], masks[2], masks[3],
190                      preMult, dt);
191             else if (bands == 3)
192                 return new DirectColorModel JavaDoc
193                     (cs, bits, masks[0], masks[1], masks[2], 0x0,
194                      preMult, dt);
195             else
196                 throw new IllegalArgumentException JavaDoc
197                     ("Incompatible number of bands out for ColorModel");
198         }
199         throw new IllegalArgumentException JavaDoc
200             ("Unsupported SampleModel Type");
201     }
202 }
203
Popular Tags