KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > AbstractLight


1 /*
2
3    Copyright 2001 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;
19
20 import java.awt.Color JavaDoc;
21
22 /**
23  * An abstract implementation of the Light interface.
24  *
25  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
26  * @version $Id: AbstractLight.java,v 1.4 2005/03/27 08:58:32 cam Exp $
27  */

28 public abstract class AbstractLight implements Light {
29     /**
30      * Conversion function for light values.
31      */

32     public static final double sRGBToLsRGB(double value) {
33         if(value <= 0.003928)
34             return value/12.92;
35         return Math.pow((value+0.055)/1.055, 2.4);
36     }
37
38     /**
39      * Light color in linear sRGB
40      */

41     private double[] color;
42
43     /**
44      * @param linear if true the color is returned in the Linear sRGB
45      * colorspace otherwise the color is in the gamma
46      * corrected sRGB color space.
47      * @return the light's color
48      */

49     public double[] getColor(boolean linear){
50         double [] ret = new double[3];
51         if (linear) {
52             ret[0] = sRGBToLsRGB(color[0]);
53             ret[1] = sRGBToLsRGB(color[1]);
54             ret[2] = sRGBToLsRGB(color[2]);
55         } else {
56             ret[0] = color[0];
57             ret[1] = color[1];
58             ret[2] = color[2];
59         }
60         return ret;
61     }
62
63     public AbstractLight(Color JavaDoc color){
64         setColor(color);
65     }
66
67     /**
68      * Sets the new light color, <tt>newColor</tt> should be in sRGB.
69      */

70     public void setColor(Color JavaDoc newColor){
71         color = new double[3];
72         color[0] = newColor.getRed() /255.;
73         color[1] = newColor.getGreen()/255.;
74         color[2] = newColor.getBlue() /255.;
75     }
76
77     /**
78      * @return true if the light is constant over the whole surface
79      */

80     public boolean isConstant(){
81         return true;
82     }
83
84     /**
85      * Returns a light map, starting in (x, y) with dx, dy increments, a given
86      * width and height, and z elevations stored in the fourth component on the
87      * N array.
88      *
89      * @param x x-axis coordinate where the light should be computed
90      * @param y y-axis coordinate where the light should be computed
91      * @param dx delta x for computing light vectors in user space
92      * @param dy delta y for computing light vectors in user space
93      * @param width number of samples to compute on the x axis
94      * @param height number of samples to compute on the y axis
95      * @param z array containing the z elevation for all the points
96      */

97     public double[][][] getLightMap(double x, double y,
98                                     final double dx, final double dy,
99                                     final int width, final int height,
100                                     final double[][][] z)
101     {
102         double[][][] L = new double[height][][];
103
104         for(int i=0; i<height; i++){
105             L[i] = getLightRow(x, y, dx, width, z[i], null);
106             y += dy;
107         }
108
109         return L;
110     }
111
112     /**
113      * Returns a row of the light map, starting at (x, y) with dx
114      * increments, a given width, and z elevations stored in the
115      * fourth component on the N array.
116      *
117      * @param x x-axis coordinate where the light should be computed
118      * @param y y-axis coordinate where the light should be computed
119      * @param dx delta x for computing light vectors in user space
120      * @param width number of samples to compute on the x axis
121      * @param z array containing the z elevation for all the points
122      * @param lightRow array to store the light info to, if null it will
123      * be allocated for you and returned.
124      *
125      * @return an array width columns where each element
126      * is an array of three components representing the x, y and z
127      * components of the light vector. */

128     public double[][] getLightRow(double x, double y,
129                                   final double dx, final int width,
130                                   final double[][] z,
131                                   final double[][] lightRow) {
132         double [][] ret = lightRow;
133         if (ret == null)
134             ret = new double[width][3];
135
136         for(int i=0; i<width; i++){
137             getLight(x, y, z[i][3], ret[i]);
138             x += dx;
139         }
140
141         return ret;
142     }
143 }
144
145
146
Popular Tags