KickJava   Java API By Example, From Geeks To Geeks.

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


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: SpecularLightingRed.java,v 1.16 2004/08/18 07:14:08 vhardy Exp $
34  */

35 public class SpecularLightingRed extends AbstractTiledRed{
36     /**
37      * Specular lighting constant
38      */

39     private double ks;
40
41     /**
42      * Specular lighting exponent
43      */

44     private double specularExponent;
45
46     /**
47      * Light used for specular lighting
48      */

49     private Light light;
50
51     /**
52      * BumpMap source
53      */

54     private BumpMap bumpMap;
55
56     /**
57      * Device space to user space scale factors, along
58      * each axis.
59      */

60     private double scaleX, scaleY;
61
62     /**
63      * LitRegion
64      */

65     private Rectangle JavaDoc litRegion;
66
67     /**
68      * true if calculations should be performed in linear sRGB
69      */

70     private boolean linear;
71      
72
73     public SpecularLightingRed(double ks,
74                                double specularExponent,
75                                Light light,
76                                BumpMap bumpMap,
77                                Rectangle JavaDoc litRegion,
78                                double scaleX, double scaleY,
79                                boolean linear) {
80         this.ks = ks;
81         this.specularExponent = specularExponent;
82         this.light = light;
83         this.bumpMap = bumpMap;
84         this.litRegion = litRegion;
85         this.scaleX = scaleX;
86         this.scaleY = scaleY;
87         this.linear = linear;
88
89         ColorModel JavaDoc cm;
90         if (linear)
91             cm = GraphicsUtil.Linear_sRGB_Unpre;
92         else
93             cm = GraphicsUtil.sRGB_Unpre;
94
95         int tw = litRegion.width;
96         int th = litRegion.height;
97         int defSz = AbstractTiledRed.getDefaultTileSize();
98         if (tw > defSz) tw = defSz;
99         if (th > defSz) th = defSz;
100         SampleModel JavaDoc sm = cm.createCompatibleSampleModel(tw, th);
101                                              
102         init((CachableRed)null, litRegion, cm, sm,
103              litRegion.x, litRegion.y, null);
104     }
105
106     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
107         copyToRaster(wr);
108         return wr;
109     }
110
111     public void genRect(WritableRaster JavaDoc wr) {
112         // Copy variable on stack for faster access in tight loop
113
final double scaleX = this.scaleX;
114         final double scaleY = this.scaleY;
115
116         final double[] lightColor = light.getColor(linear);
117
118         final int w = wr.getWidth();
119         final int h = wr.getHeight();
120         final int minX = wr.getMinX();
121         final int minY = wr.getMinY();
122
123         final DataBufferInt JavaDoc db = (DataBufferInt JavaDoc)wr.getDataBuffer();
124         final int[] pixels = db.getBankData()[0];
125
126         final SinglePixelPackedSampleModel JavaDoc sppsm;
127         sppsm = (SinglePixelPackedSampleModel JavaDoc)wr.getSampleModel();
128
129         final int offset =
130             (db.getOffset() +
131              sppsm.getOffset(minX-wr.getSampleModelTranslateX(),
132                              minY-wr.getSampleModelTranslateY()));
133         // int offset = db.getOffset();
134
final int scanStride = sppsm.getScanlineStride();
135         final int adjust = scanStride - w;
136         int p = offset;
137         int a=0, i=0, j=0;
138
139         // x and y are in user space
140
double x = scaleX*minX;
141         double y = scaleY*minY;
142         double norm = 0;
143
144         int pixel = 0, tmp;
145         double mult;
146         mult = (lightColor[0]>lightColor[1])?lightColor[0]:lightColor[1];
147         mult = (mult>lightColor[2])?mult:lightColor[2];
148         
149         double scale = 255/mult;
150         pixel = (int)(lightColor[0]*scale+0.5);
151         tmp = (int)(lightColor[1]*scale+0.5);
152         pixel = pixel<<8 | tmp;
153         tmp = (int)(lightColor[2]*scale+0.5);
154         pixel = pixel<<8 | tmp;
155
156         mult*=255*ks;
157
158         // System.out.println("Pixel: 0x" + Integer.toHexString(pixel));
159

160         final double[][][] NA = bumpMap.getNormalArray(minX, minY, w, h);
161
162         // System.out.println("Entering Specular Lighting");
163
if(!light.isConstant()){
164             final double[][] LA = new double[w][3];
165             for(i=0; i<h; i++){
166                 // System.out.println("Row: " + i);
167
final double [][] NR = NA[i];
168                 light.getLightRow(x, y+i*scaleY, scaleX, w, NR, LA);
169                 for (j=0; j<w; j++){
170                     // Get Normal
171
final double [] N = NR[j];
172                     
173                     // Get Light Vector
174
final double [] L = LA[j];
175
176                     // Compute Half-way vector
177
L[2] += 1;
178                     norm = L[0]*L[0] + L[1]*L[1] + L[2]*L[2];
179                     if(norm == 0)
180                         a = (int)(mult+0.5);
181                     else {
182                         norm = Math.sqrt(norm);
183                         a = (int)(mult*Math.pow((N[0]*L[0] +
184                                                  N[1]*L[1] + N[2]*L[2])/norm,
185                                                 specularExponent) + 0.5);
186                         if ((a & 0xFFFFFF00) != 0)
187                             a = ((a & 0x80000000) != 0)?0:255;
188                     }
189                     pixels[p++] = (a << 24 | pixel);
190                 }
191                 p += adjust;
192             }
193         }
194         else{
195             // Get constant light vector
196
final double[] L = new double[3];
197             light.getLight(0, 0, 0, L);
198
199             // Compute Half-way vector
200
L[2] += 1;
201             norm = Math.sqrt(L[0]*L[0] + L[1]*L[1] + L[2]*L[2]);
202             if(norm > 0){
203                 L[0] /= norm;
204                 L[1] /= norm;
205                 L[2] /= norm;
206             }
207
208             for(i=0; i<h; i++){
209                 final double [][] NR = NA[i];
210                 for(j=0; j<w; j++){
211                     // Get Normal
212
final double [] N = NR[j];
213                     
214                     a = (int)(mult*Math.pow(N[0]*L[0] + N[1]*L[1] + N[2]*L[2],
215                                             specularExponent) + 0.5);
216                     
217                     if ((a & 0xFFFFFF00) != 0)
218                         a = ((a & 0x80000000) != 0)?0:255;
219
220                     pixels[p++] = (a << 24 | pixel);
221                 }
222                 p += adjust;
223             }
224         }
225         // System.out.println("Exiting Specular Lighting");
226
}
227 }
228
Popular Tags