import java.awt.*; import java.awt.image.*; class HFEngine extends Thread { static final float C = (float)Math.tan(85*Math.PI/180); //constant for viewing angle between the eyes static final float C4 = C*C*C*C; static final float C2 = C*C; private int[] dest, pattern; private int patternwidth, destwidth, h; MemoryImageSource yar; HFEngine (int[] dest, int[] pattern, int destwidth, int patternwidth, int h) { this.dest = dest; this.pattern = pattern; this.patternwidth = patternwidth; this.destwidth = destwidth; this.h = h; } public void run() { System.out.println("Running"); float[] lastSegment = new float[patternwidth*2]; float[] newSegment = new float[patternwidth*2]; float[] pointer; int lastwidth = 0; int newwidth = 0; int index = 0; int x = 0; int y = 0; int lastx = 0; float radius = (destwidth-patternwidth)/2; float rx = 0; float xe = 0; float scalefactor = 0; float xval = 0; float lastxe = 0; while (index < dest.length) { if (Math.abs(y-radius) != radius) scalefactor = (float)(Math.sqrt(radius*radius-(y-radius)*(y-radius))); else scalefactor = 1; for (x = 0; x< patternwidth; x++) { dest[index] = pattern[y*patternwidth+x]; lastSegment[x] = x; index++; } lastwidth = patternwidth-1; lastx = x; newwidth = -1; for ( ; x< destwidth; x++) { rx = (x-patternwidth-radius)/scalefactor; if ( (4*C4*rx*rx-4*(1+C2)*(C2*rx*rx-1)) >= 0) xe = (float)(2*C2*rx+Math.sqrt(4*C4*rx*rx-4*(1+C2)*(C2*rx*rx-1)))/(2*(1+C2)); else xe = rx; if (x == patternwidth) lastxe = xe; if ((xe-lastxe)*scalefactor > lastwidth) { pointer = lastSegment; lastSegment = newSegment; newSegment = pointer; lastwidth = newwidth; newwidth = -1; lastx = x; lastxe = xe; x--; } else { xval = interpolate(lastSegment,(xe-lastxe)*scalefactor); newSegment[x-lastx] = xval; dest[index] = pattern[y*patternwidth+Math.round(xval)]; newwidth++; index++; } } y++; } yar = new MemoryImageSource(destwidth, h, dest, 0, destwidth); } float interpolate(float[] array, float index) { int ceil = (int)Math.ceil(index); int floor = (int)Math.floor(index); if (ceil==floor) return array[ceil]; else return (index-floor)*array[ceil]+(ceil-index)*array[floor]; } }