001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image;
020:
021: import java.awt.Color;
022:
023: /**
024: * An abstract implementation of the Light interface.
025: *
026: * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
027: * @version $Id: AbstractLight.java 475477 2006-11-15 22:44:28Z cam $
028: */
029: public abstract class AbstractLight implements Light {
030: /**
031: * Conversion function for light values.
032: */
033: public static final double sRGBToLsRGB(double value) {
034: if (value <= 0.003928)
035: return value / 12.92;
036: return Math.pow((value + 0.055) / 1.055, 2.4);
037: }
038:
039: /**
040: * Light color in linear sRGB
041: */
042: private double[] color;
043:
044: /**
045: * @param linear if true the color is returned in the Linear sRGB
046: * colorspace otherwise the color is in the gamma
047: * corrected sRGB color space.
048: * @return the light's color
049: */
050: public double[] getColor(boolean linear) {
051: double[] ret = new double[3];
052: if (linear) {
053: ret[0] = sRGBToLsRGB(color[0]);
054: ret[1] = sRGBToLsRGB(color[1]);
055: ret[2] = sRGBToLsRGB(color[2]);
056: } else {
057: ret[0] = color[0];
058: ret[1] = color[1];
059: ret[2] = color[2];
060: }
061: return ret;
062: }
063:
064: public AbstractLight(Color color) {
065: setColor(color);
066: }
067:
068: /**
069: * Sets the new light color, <tt>newColor</tt> should be in sRGB.
070: */
071: public void setColor(Color newColor) {
072: color = new double[3];
073: color[0] = newColor.getRed() / 255.;
074: color[1] = newColor.getGreen() / 255.;
075: color[2] = newColor.getBlue() / 255.;
076: }
077:
078: /**
079: * @return true if the light is constant over the whole surface
080: */
081: public boolean isConstant() {
082: return true;
083: }
084:
085: /**
086: * Returns a light map, starting in (x, y) with dx, dy increments, a given
087: * width and height, and z elevations stored in the fourth component on the
088: * N array.
089: *
090: * @param x x-axis coordinate where the light should be computed
091: * @param y y-axis coordinate where the light should be computed
092: * @param dx delta x for computing light vectors in user space
093: * @param dy delta y for computing light vectors in user space
094: * @param width number of samples to compute on the x axis
095: * @param height number of samples to compute on the y axis
096: * @param z array containing the z elevation for all the points
097: */
098: public double[][][] getLightMap(double x, double y,
099: final double dx, final double dy, final int width,
100: final int height, final double[][][] z) {
101: double[][][] L = new double[height][][];
102:
103: for (int i = 0; i < height; i++) {
104: L[i] = getLightRow(x, y, dx, width, z[i], null);
105: y += dy;
106: }
107:
108: return L;
109: }
110:
111: /**
112: * Returns a row of the light map, starting at (x, y) with dx
113: * increments, a given width, and z elevations stored in the
114: * fourth component on the N array.
115: *
116: * @param x x-axis coordinate where the light should be computed
117: * @param y y-axis coordinate where the light should be computed
118: * @param dx delta x for computing light vectors in user space
119: * @param width number of samples to compute on the x axis
120: * @param z array containing the z elevation for all the points
121: * @param lightRow array to store the light info to, if null it will
122: * be allocated for you and returned.
123: *
124: * @return an array width columns where each element
125: * is an array of three components representing the x, y and z
126: * components of the light vector. */
127: public double[][] getLightRow(double x, double y, final double dx,
128: final int width, final double[][] z,
129: final double[][] lightRow) {
130: double[][] ret = lightRow;
131: if (ret == null)
132: ret = new double[width][3];
133:
134: for (int i = 0; i < width; i++) {
135: getLight(x, y, z[i][3], ret[i]);
136: x += dx;
137: }
138:
139: return ret;
140: }
141: }
|