1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.Rectangle ; 21 import java.awt.image.ColorModel ; 22 import java.awt.image.DataBufferInt ; 23 import java.awt.image.SampleModel ; 24 import java.awt.image.SinglePixelPackedSampleModel ; 25 import java.awt.image.WritableRaster ; 26 27 import org.apache.batik.ext.awt.image.GraphicsUtil; 28 import org.apache.batik.ext.awt.image.Light; 29 30 35 public class DiffuseLightingRed extends AbstractRed{ 36 39 private double kd; 40 41 44 private Light light; 45 46 49 private BumpMap bumpMap; 50 51 55 private double scaleX, scaleY; 56 57 60 private Rectangle litRegion; 61 62 65 private boolean linear; 66 67 68 public DiffuseLightingRed(double kd, 69 Light light, 70 BumpMap bumpMap, 71 Rectangle 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 cm; 83 if (linear) 84 cm = GraphicsUtil.Linear_sRGB_Pre; 85 else 86 cm = GraphicsUtil.sRGB_Pre; 87 88 SampleModel 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 copyData(WritableRaster 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 db = (DataBufferInt )wr.getDataBuffer(); 105 final int[] pixels = db.getBankData()[0]; 106 107 final SinglePixelPackedSampleModel sppsm; 108 sppsm = (SinglePixelPackedSampleModel )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 double x = scaleX*minX; 123 double y = scaleY*minY; 124 double NL = 0; 125 126 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 final double [] N = NR[j]; 137 138 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 ((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 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 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 ((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 |