KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Rectangle JavaDoc;
21 import java.awt.image.ColorModel JavaDoc;
22 import java.awt.image.DataBufferInt JavaDoc;
23 import java.awt.image.SampleModel JavaDoc;
24 import java.awt.image.SinglePixelPackedSampleModel JavaDoc;
25 import java.awt.image.WritableRaster JavaDoc;
26
27 import org.apache.batik.ext.awt.image.GraphicsUtil;
28 import org.apache.batik.ext.awt.image.Light;
29
30 /**
31  *
32  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: DiffuseLightingRed.java,v 1.10 2004/08/18 07:14:08 vhardy Exp $
34  */

35 public class DiffuseLightingRed extends AbstractRed{
36     /**
37      * Diffuse lighting constant
38      */

39     private double kd;
40
41     /**
42      * Light used for diffuse lighting
43      */

44     private Light light;
45
46     /**
47      * BumpMap source
48      */

49     private BumpMap bumpMap;
50
51     /**
52      * Device space to user space scale factors, along
53      * each axis
54      */

55     private double scaleX, scaleY;
56
57     /**
58      * LitRegion
59      */

60     private Rectangle JavaDoc litRegion;
61
62     /**
63      * true if calculations should be performed in linear sRGB
64      */

65     private boolean linear;
66
67
68     public DiffuseLightingRed(double kd,
69                               Light light,
70                               BumpMap bumpMap,
71                               Rectangle JavaDoc litRegion,
72                               double scaleX, double scaleY,
73                               boolean linear){
74         this.kd = kd;
75         this.light = light;
76         this.bumpMap = bumpMap;
77         this.litRegion = litRegion;
78         this.scaleX = scaleX;
79         this.scaleY = scaleY;
80         this.linear = linear;
81
82         ColorModel JavaDoc cm;
83         if (linear)
84             cm = GraphicsUtil.Linear_sRGB_Pre;
85         else
86             cm = GraphicsUtil.sRGB_Pre;
87
88         SampleModel JavaDoc sm =
89             cm.createCompatibleSampleModel(litRegion.width,
90                                            litRegion.height);
91                                              
92         init((CachableRed)null, litRegion, cm, sm,
93              litRegion.x, litRegion.y, null);
94     }
95
96     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr){
97         final double[] lightColor = light.getColor(linear);
98         
99         final int w = wr.getWidth();
100         final int h = wr.getHeight();
101         final int minX = wr.getMinX();
102         final int minY = wr.getMinY();
103
104         final DataBufferInt JavaDoc db = (DataBufferInt JavaDoc)wr.getDataBuffer();
105         final int[] pixels = db.getBankData()[0];
106
107         final SinglePixelPackedSampleModel JavaDoc sppsm;
108         sppsm = (SinglePixelPackedSampleModel JavaDoc)wr.getSampleModel();
109         
110         final int offset =
111             (db.getOffset() +
112              sppsm.getOffset(minX-wr.getSampleModelTranslateX(),
113                              minY-wr.getSampleModelTranslateY()));
114
115         final int scanStride = sppsm.getScanlineStride();
116         final int adjust = scanStride - w;
117         int p = offset;
118         int r=0, g=0, b=0;
119         int i=0, j=0;
120
121         // System.out.println("Getting diffuse red : " + minX + "/" + minY + "/" + w + "/" + h);
122
double x = scaleX*minX;
123         double y = scaleY*minY;
124         double NL = 0;
125
126         // final double[] L = new double[3];
127
final double[][][] NA = bumpMap.getNormalArray(minX, minY, w, h);
128         if(!light.isConstant()){
129             final double[][] LA = new double[w][3];
130
131             for(i=0; i<h; i++){
132                 final double [][] NR = NA[i];
133                 light.getLightRow(x, y+i*scaleY, scaleX, w, NR, LA);
134                 for(j=0; j<w; j++){
135                     // Get Normal
136
final double [] N = NR[j];
137                     
138                     // Get Light Vector
139
final double [] L = LA[j];
140                     
141                     NL = 255.*kd*(N[0]*L[0] + N[1]*L[1] + N[2]*L[2]);
142                     
143                     r = (int)(NL*lightColor[0]);
144                     g = (int)(NL*lightColor[1]);
145                     b = (int)(NL*lightColor[2]);
146                     
147                     // If any high bits are set we are not in range.
148
// If the highest bit is set then we are negative so
149
// clamp to zero else we are > 255 so clamp to 255.
150
if ((r & 0xFFFFFF00) != 0)
151                         r = ((r & 0x80000000) != 0)?0:255;
152                     if ((g & 0xFFFFFF00) != 0)
153                         g = ((g & 0x80000000) != 0)?0:255;
154                     if ((b & 0xFFFFFF00) != 0)
155                         b = ((b & 0x80000000) != 0)?0:255;
156                     
157                     pixels[p++] = (0xff000000
158                                    |
159                                    r << 16
160                                    |
161                                    g << 8
162                                    |
163                                    b);
164                     
165                 }
166                 p += adjust;
167             }
168         }
169         else{
170             // System.out.println(">>>>>>>> Processing constant light ...");
171
// Constant light
172
final double[] L = new double[3];
173             light.getLight(0, 0, 0, L);
174
175             for(i=0; i<h; i++){
176                 final double [][] NR = NA[i];
177                 for(j=0; j<w; j++){
178                     // Get Normal
179
final double[] N = NR[j];
180                     
181                     NL = 255.*kd*(N[0]*L[0] + N[1]*L[1] + N[2]*L[2]);
182                     
183                     r = (int)(NL*lightColor[0]);
184                     g = (int)(NL*lightColor[1]);
185                     b = (int)(NL*lightColor[2]);
186                     
187                     // If any high bits are set we are not in range.
188
// If the highest bit is set then we are negative so
189
// clamp to zero else we are > 255 so clamp to 255.
190
if ((r & 0xFFFFFF00) != 0)
191                         r = ((r & 0x80000000) != 0)?0:255;
192                     if ((g & 0xFFFFFF00) != 0)
193                         g = ((g & 0x80000000) != 0)?0:255;
194                     if ((b & 0xFFFFFF00) != 0)
195                         b = ((b & 0x80000000) != 0)?0:255;
196                     
197                     pixels[p++] = (0xff000000
198                                    |
199                                    r << 16
200                                    |
201                                    g << 8
202                                    |
203                                    b);
204                 }
205                 p += adjust;
206             }
207         }
208         
209         return wr;
210     }
211
212 }
213
Popular Tags