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 SpecularLightingRed extends AbstractTiledRed{ 36 39 private double ks; 40 41 44 private double specularExponent; 45 46 49 private Light light; 50 51 54 private BumpMap bumpMap; 55 56 60 private double scaleX, scaleY; 61 62 65 private Rectangle litRegion; 66 67 70 private boolean linear; 71 72 73 public SpecularLightingRed(double ks, 74 double specularExponent, 75 Light light, 76 BumpMap bumpMap, 77 Rectangle 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 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 sm = cm.createCompatibleSampleModel(tw, th); 101 102 init((CachableRed)null, litRegion, cm, sm, 103 litRegion.x, litRegion.y, null); 104 } 105 106 public WritableRaster copyData(WritableRaster wr) { 107 copyToRaster(wr); 108 return wr; 109 } 110 111 public void genRect(WritableRaster wr) { 112 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 db = (DataBufferInt )wr.getDataBuffer(); 124 final int[] pixels = db.getBankData()[0]; 125 126 final SinglePixelPackedSampleModel sppsm; 127 sppsm = (SinglePixelPackedSampleModel )wr.getSampleModel(); 128 129 final int offset = 130 (db.getOffset() + 131 sppsm.getOffset(minX-wr.getSampleModelTranslateX(), 132 minY-wr.getSampleModelTranslateY())); 133 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 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 160 final double[][][] NA = bumpMap.getNormalArray(minX, minY, w, h); 161 162 if(!light.isConstant()){ 164 final double[][] LA = new double[w][3]; 165 for(i=0; i<h; i++){ 166 final double [][] NR = NA[i]; 168 light.getLightRow(x, y+i*scaleY, scaleX, w, NR, LA); 169 for (j=0; j<w; j++){ 170 final double [] N = NR[j]; 172 173 final double [] L = LA[j]; 175 176 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 final double[] L = new double[3]; 197 light.getLight(0, 0, 0, L); 198 199 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 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 } 227 } 228 | Popular Tags |