KickJava   Java API By Example, From Geeks To Geeks.

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


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.BandCombineOp JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.awt.image.ColorConvertOp JavaDoc;
27 import java.awt.image.ColorModel JavaDoc;
28 import java.awt.image.ComponentColorModel JavaDoc;
29 import java.awt.image.DataBuffer JavaDoc;
30 import java.awt.image.PixelInterleavedSampleModel JavaDoc;
31 import java.awt.image.Raster JavaDoc;
32 import java.awt.image.SampleModel JavaDoc;
33 import java.awt.image.WritableRaster JavaDoc;
34
35 import org.apache.batik.ext.awt.ColorSpaceHintKey;
36 import org.apache.batik.ext.awt.image.GraphicsUtil;
37
38 /**
39  * This function will tranform an image from any colorspace into a
40  * luminance image. The alpha channel if any will be copied to the
41  * new image.
42  *
43  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
44  * @version $Id: Any2LumRed.java,v 1.8 2005/02/13 11:32:26 deweese Exp $ */

45 public class Any2LumRed extends AbstractRed {
46
47     /**
48      * Construct a luminace image from src.
49      *
50      * @param src The image to convert to a luminance image
51      */

52     public Any2LumRed(CachableRed src) {
53         super(src,src.getBounds(),
54               fixColorModel(src),
55               fixSampleModel(src),
56               src.getTileGridXOffset(),
57               src.getTileGridYOffset(),
58               null);
59
60         props.put(ColorSpaceHintKey.PROPERTY_COLORSPACE,
61                   ColorSpaceHintKey.VALUE_COLORSPACE_GREY);
62     }
63
64     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
65         // Get my source.
66
CachableRed src = (CachableRed)getSources().get(0);
67
68         SampleModel JavaDoc sm = src.getSampleModel();
69         ColorModel JavaDoc srcCM = src.getColorModel();
70         Raster JavaDoc srcRas = src.getData(wr.getBounds());
71         if (srcCM == null) {
72             // We don't really know much about this source.
73

74             float [][] matrix = null;
75             if (sm.getNumBands() == 2) {
76                 matrix = new float[2][2];
77                 matrix[0][0] = 1;
78                 matrix[1][1] = 1;
79             } else {
80                 matrix = new float[sm.getNumBands()][1];
81                 matrix[0][0] = 1;
82             }
83
84             BandCombineOp JavaDoc op = new BandCombineOp JavaDoc(matrix, null);
85             op.filter(srcRas, wr);
86         } else {
87             WritableRaster JavaDoc srcWr = (WritableRaster JavaDoc)srcRas;
88
89             // Divide out alpha if we have it. We need to do this since
90
// the color convert may not be a linear operation which may
91
// lead to out of range values.
92
if (srcCM.hasAlpha())
93                 GraphicsUtil.coerceData(srcWr, srcCM, false);
94
95             BufferedImage JavaDoc srcBI, dstBI;
96             srcBI = new BufferedImage JavaDoc(srcCM,
97                                       srcWr.createWritableTranslatedChild(0,0),
98                                       false,
99                                       null);
100             ColorModel JavaDoc dstCM = getColorModel();
101             if (!dstCM.hasAlpha()) {
102                 // No alpha ao we don't have to work around the bug
103
// in the color convert op.
104
dstBI = new BufferedImage JavaDoc
105                     (dstCM, wr.createWritableTranslatedChild(0,0),
106                      dstCM.isAlphaPremultiplied(), null);
107             } else {
108                 // All this nonsense is to work around the fact that the
109
// Color convert op doesn't properly copy the Alpha from
110
// src to dst.
111
PixelInterleavedSampleModel JavaDoc dstSM;
112                 dstSM = (PixelInterleavedSampleModel JavaDoc)wr.getSampleModel();
113                 SampleModel JavaDoc smna = new PixelInterleavedSampleModel JavaDoc
114                     (dstSM.getDataType(),
115                      dstSM.getWidth(), dstSM.getHeight(),
116                      dstSM.getPixelStride(), dstSM.getScanlineStride(),
117                      new int [] { 0 });
118
119                 WritableRaster JavaDoc dstWr;
120                 dstWr = Raster.createWritableRaster(smna,
121                                                     wr.getDataBuffer(),
122                                                     new Point JavaDoc(0,0));
123                 dstWr = dstWr.createWritableChild
124                     (wr.getMinX()-wr.getSampleModelTranslateX(),
125                      wr.getMinY()-wr.getSampleModelTranslateY(),
126                      wr.getWidth(), wr.getHeight(),
127                      0, 0, null);
128                 
129                 ColorModel JavaDoc cmna = new ComponentColorModel JavaDoc
130                     (ColorSpace.getInstance(ColorSpace.CS_GRAY),
131                      new int [] {8}, false, false,
132                      Transparency.OPAQUE,
133                      DataBuffer.TYPE_BYTE);
134
135                 dstBI = new BufferedImage JavaDoc(cmna, dstWr, false, null);
136             }
137
138             ColorConvertOp JavaDoc op = new ColorConvertOp JavaDoc(null);
139             op.filter(srcBI, dstBI);
140
141             // Have to 'fix' alpha premult
142
if (dstCM.hasAlpha()) {
143                 copyBand(srcWr, sm.getNumBands()-1,
144                          wr, getSampleModel().getNumBands()-1);
145                 if (dstCM.isAlphaPremultiplied())
146                     GraphicsUtil.multiplyAlpha(wr);
147             }
148         }
149         return wr;
150     }
151
152     /**
153      * This function 'fixes' the source's color model. Right now
154      * it just selects if it should have one or two bands based on
155      * if the source had an alpha channel.
156      */

157     protected static ColorModel JavaDoc fixColorModel(CachableRed src) {
158         ColorModel JavaDoc cm = src.getColorModel();
159         if (cm != null) {
160             if (cm.hasAlpha())
161                 return new ComponentColorModel JavaDoc
162                     (ColorSpace.getInstance(ColorSpace.CS_GRAY),
163                      new int [] {8,8}, true,
164                      cm.isAlphaPremultiplied(),
165                      Transparency.TRANSLUCENT,
166                      DataBuffer.TYPE_BYTE);
167
168             return new ComponentColorModel JavaDoc
169                 (ColorSpace.getInstance(ColorSpace.CS_GRAY),
170                  new int [] {8}, false, false,
171                  Transparency.OPAQUE,
172                  DataBuffer.TYPE_BYTE);
173         }
174         else {
175             // No ColorModel so try to make some intelligent
176
// decisions based just on the number of bands...
177
// 1 bands -> lum
178
// 2 bands -> lum (Band 0) & alpha (Band 1)
179
// >2 bands -> lum (Band 0) - No color conversion...
180
SampleModel JavaDoc sm = src.getSampleModel();
181
182             if (sm.getNumBands() == 2)
183                 return new ComponentColorModel JavaDoc
184                     (ColorSpace.getInstance(ColorSpace.CS_GRAY),
185                      new int [] {8,8}, true,
186                      true, Transparency.TRANSLUCENT,
187                      DataBuffer.TYPE_BYTE);
188
189             return new ComponentColorModel JavaDoc
190                 (ColorSpace.getInstance(ColorSpace.CS_GRAY),
191                  new int [] {8}, false, false,
192                  Transparency.OPAQUE,
193                  DataBuffer.TYPE_BYTE);
194         }
195     }
196
197     /**
198      * This function 'fixes' the source's sample model.
199      * Right now it just selects if it should have one or two bands
200      * based on if the source had an alpha channel.
201      */

202     protected static SampleModel JavaDoc fixSampleModel(CachableRed src) {
203         SampleModel JavaDoc sm = src.getSampleModel();
204
205         int width = sm.getWidth();
206         int height = sm.getHeight();
207
208         ColorModel JavaDoc cm = src.getColorModel();
209         if (cm != null) {
210             if (cm.hasAlpha())
211                 return new PixelInterleavedSampleModel JavaDoc
212                     (DataBuffer.TYPE_BYTE, width, height, 2, 2*width,
213                      new int [] { 0, 1 });
214
215             return new PixelInterleavedSampleModel JavaDoc
216                 (DataBuffer.TYPE_BYTE, width, height, 1, width,
217                  new int [] { 0 });
218         }
219         else {
220             // No ColorModel so try to make some intelligent
221
// decisions based just on the number of bands...
222
// 1 bands -> lum
223
// 2 bands -> lum (Band 0) & alpha (Band 1)
224
// >2 bands -> lum (Band 0) - No color conversion...
225
if (sm.getNumBands() == 2)
226                 return new PixelInterleavedSampleModel JavaDoc
227                     (DataBuffer.TYPE_BYTE, width, height, 2, 2*width,
228                      new int [] { 0, 1 });
229
230             return new PixelInterleavedSampleModel JavaDoc
231                 (DataBuffer.TYPE_BYTE, width, height, 1, width,
232                  new int [] { 0 });
233         }
234     }
235 }
236
Popular Tags