public void accumulateBilinear(double x, double y, double s) /* Bilinearly accumulates 's' to the four integer grid points surrounding * the continuous coordinate (x, y). */ { double xpf = Math.floor(x); int xi = (int) xpf; double xf = x - xpf; double ypf = Math.floor(y); int yi = (int) ypf; double yf = y - ypf; double b; b = (1.0 - xf) * (1.0 - yf); accumulate(xi, yi, s * b); b = xf * (1.0 - yf); accumulate(xi + 1, yi, s * b); b = (1.0 - xf) * yf; accumulate(xi, yi + 1, s * b); b = xf * yf; accumulate(xi + 1, yi + 1, s * b); }
public double getBilinear(double x, double y) /* Returns: the bilinearly-interpolated value of the continuous field * at (x, y). * Requires: (x, y) is inside the domain of the field */ { if (!inBounds(x, y)) throw new RuntimeException( "ScalarImage.getBilinear: RuntimeException at (" + x + "," + y + ")"); int xi, yi; double xf, yf; if (x == (double) (width - 1)) { xi = width - 2; xf = 1.0; } else { double xpf = Math.floor(x); xi = (int) xpf; xf = x - xpf; } if (y == (double) (height - 1)) { yi = height - 2; yf = 1.0; } else { double ypf = Math.floor(y); yi = (int) ypf; yf = y - ypf; } double b1 = get(xi, yi); double b2 = get(xi + 1, yi); double b3 = get(xi, yi + 1); double b4 = get(xi + 1, yi + 1); double bb1 = b1 + xf * (b2 - b1); double bb2 = b3 + xf * (b4 - b3); return bb1 + yf * (bb2 - bb1); }
public void vec2FieldMagnitude(Field field, AffineTransform ftoi) { AffineTransform itof = null; try { itof = ftoi.createInverse(); } catch (NoninvertibleTransformException niv) { TDebug.println(0, "NoninvertibleTransformException: " + niv); } Vector3d v = new Vector3d(); Point2D.Double p = new Point2D.Double(); for (int j = 0, k = 0; j < height; ++j) for (int i = 0; i < width; ++i, ++k) { p.x = i; p.y = j; itof.transform(p, p); v = field.get(p.x, p.y, 0.0); f[k] = (float) Math.sqrt(v.x * v.x + v.y * v.y); } }
public BufferedImage getBufferedImage(int type, Color c) { BufferedImage image = null; float[] colComp = new float[3]; c.getRGBColorComponents(colComp); double red = (double) colComp[0]; double green = (double) colComp[1]; double blue = (double) colComp[2]; // System.out.println("blue, green, red = "+ blue +", " + green + ", " + red); double x = 0.0; double x2; switch (type) { case ScalarImage.TYPE_BYTE_RANDOM: { int numCol = 256; byte[] bBuf = new byte[numCol * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; double delta = 1.0 / (double) (numCol + 1); int j = 0; for (int i = 0; i < numCol; i++) { if (i % 5 == 0) x = 0.7 * Math.random() + 0.3 * x; x2 = x * x; bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); // x += delta; } IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false); // image = new // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm); byte[] idxBuffer = new byte[size]; for (int i = 0; i < size; i++) { idxBuffer[i] = (byte) (clamp(f[i] * 255.)); } DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size); int idxOffset[] = {0}; int idxBits[] = {8}; try { ComponentSampleModel idxSampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset); WritableRaster rasterIdx = java.awt.image.Raster.createWritableRaster( idxSampleModel, dataBuffer, new Point(0, 0)); image = new BufferedImage(cm, rasterIdx, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } } break; case BufferedImage.TYPE_BYTE_INDEXED: { int numCol = 256; byte[] bBuf = new byte[numCol * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; double delta = 1.0 / (double) (numCol + 1); int j = 0; for (int i = 0; i < numCol; i++) { x2 = x * x; bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); x += delta; } IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false); // image = new // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm); byte[] idxBuffer = new byte[size]; for (int i = 0; i < size; i++) { idxBuffer[i] = (byte) (clamp(f[i] * 255.)); } DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size); int idxOffset[] = {0}; int idxBits[] = {8}; try { ComponentSampleModel idxSampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset); WritableRaster rasterIdx = java.awt.image.Raster.createWritableRaster( idxSampleModel, dataBuffer, new Point(0, 0)); image = new BufferedImage(cm, rasterIdx, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } } break; case BufferedImage.TYPE_BYTE_GRAY: break; case BufferedImage.TYPE_3BYTE_BGR: default: byte[] byteBuffer = new byte[size * 3]; blue *= 255 * 4.; green *= 255 * 4.; red *= 255 * 4.; int j = 0; for (int i = 0; i < size; i++) { x = f[i]; x2 = x * x; /* byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * red ) * x * x + ( 4 * red - 255 ) * x); byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * green ) * x * x + ( 4 * green - 255 ) * x); byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * blue ) * x * x + ( 4 * blue - 255 ) * x); */ byteBuffer[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x); byteBuffer[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x); byteBuffer[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x); } DataBufferByte dataBuffer = new DataBufferByte(byteBuffer, size * 3); int componentOffset[] = {0, 1, 2}; int componentBits[] = {8, 8, 8}; try { WritableRaster raster = java.awt.image.Raster.createWritableRaster( new PixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, width, height, 3, width * 3, componentOffset), dataBuffer, new Point(0, 0)); image = new BufferedImage( new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), componentBits, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE), raster, false, null); } catch (Exception e) { System.out.println("Exception caught while acquiring image:"); System.out.println(e.getMessage()); } break; } return image; }
public void normalize(double range) { double scale = range; float min = 1000f; float max = -1000f; for (int k = 0; k < size; k++) { // System.out.println(f[k]); /* * if (f[k] == Float.NaN) { System.out.println("NaN at k= "+k); * f[k] = 0f; } */ if (Float.isNaN(f[k])) { System.out.println("NaN at k= " + k); f[k] = 0f; continue; } if (Float.isInfinite(f[k])) { if (f[k] < 0) { System.out.println("-Infinity at k= " + k); f[k] = 0f; // -1000f; } else { System.out.println("+Infinity at k= " + k); f[k] = 0f; // +1000f; } continue; } // System.out.print(k +"="+f[k]+ ", "); min = Math.min(min, f[k]); max = Math.max(max, f[k]); } // System.out.println(); double offset = 0. - min; if ((max - min) != 0f) { scale /= (max - min); offset *= scale; rescale(scale, offset); } System.out.println( "Normalizing using: min= " + min + " max= " + max + ", through scaleAdd(" + scale + ", " + offset + ")."); min = 1000f; max = -1000f; for (int k = 0; k < size; k++) { /* if (f[k] == Float.NaN) { System.out.println("NaN at k= " + k); f[k] = 0f; } */ if (Float.isNaN(f[k])) { System.out.println("NaN at k= " + k + " after normalization."); f[k] = 0f; continue; } // System.out.print(k +"="+f[k]+ ", "); min = Math.min(min, f[k]); max = Math.max(max, f[k]); } System.out.println("After normalization: min= " + min + " max= " + max); }
public Tuple4f getDynamics() { Tuple4f dynamics = new Vector4f(); long count = 0; double total = 0; float min = Float.POSITIVE_INFINITY; float max = Float.NEGATIVE_INFINITY; for (int k = 0; k < size; k++) { // Do not include NAN or isInfinite in dynamics if (!((Float.isNaN(f[k])) || (Float.isInfinite(f[k])))) { // System.out.print(k +"="+f[k]+ ", "); min = Math.min(min, f[k]); max = Math.max(max, f[k]); total += (double) f[k]; count++; } } dynamics.w = min; dynamics.x = max; dynamics.y = (float) (total / (double) count); float range = max - min; int N = 256; System.out.println("min: " + min + " Max: " + max + " Range: " + range); int[] histogram = new int[N]; int level = 0; float quant = N * 0.999f; for (int k = 0; k < size; k++) { if (!((Float.isNaN(f[k])) || (Float.isInfinite(f[k])))) { level = (int) (quant * ((f[k] - min) / range)); try { histogram[level]++; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [A]."); } } } for (int j = 0; j < N; j++) { System.out.println( "Level: " + j + " count: " + histogram[j] + " value: " + (float) ((j / quant * (max - min)) + min)); } int target = (int) (count * 0.9); int cum = 0; for (int i = 0; i < N; i++) { cum += histogram[i]; System.out.println( "target: " + target + " cum: " + cum + " Level: " + i + " value: " + ((i / quant * (max - min)) + min)); if (cum >= target) { System.out.println( "target: " + target + " Level: " + i + " value: " + ((i * (max - min)) + min)); dynamics.z = (float) ((i / quant * (max - min)) + min); break; } } return dynamics; }
public void analyze(boolean doit) { float min = Float.POSITIVE_INFINITY; float max = Float.NEGATIVE_INFINITY; for (int k = 0; k < size; k++) { // System.out.println(f[k]); /* * if (f[k] == Float.NaN) { System.out.println("NaN at k= "+k); * f[k] = 0f; } */ if (Float.isNaN(f[k])) { System.out.println("NaN at k= " + k); f[k] = 0f; continue; } if (Float.isInfinite(f[k])) { if (f[k] < 0) { System.out.println("-Infinity at k= " + k); f[k] = 0f; // -1000f; } else { System.out.println("+Infinity at k= " + k); f[k] = 0f; // +1000f; } continue; } // System.out.print(k +"="+f[k]+ ", "); min = Math.min(min, f[k]); max = Math.max(max, f[k]); } int N = 256; float[] histogram = new float[N]; for (int k = 0; k < size; k++) { int level = (int) (0.999f * (float) N * (f[k] - min) / (max - min)); try { histogram[level]++; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [A]."); } } float[] cumulative = new float[N]; if (histogram[0] > 0) { if (doit) { cumulative[0] = histogram[0]; } else { cumulative[0] = 1; // histogram[0]; } } for (int i = 1; i < N; i++) { if (histogram[i] > 0) { if (doit) { cumulative[i] = cumulative[i - 1] + histogram[i]; } else { cumulative[i] = cumulative[i - 1] + 1; // histogram[i]; } } else { cumulative[i] = cumulative[i - 1]; } } /* for(int k=0; k<size; k++) { int level = (int) ( 0.999f*(float)N*(f[k]-min)/(max-min) ); try { f[k]=(cumulative[level]-cumulative[0])/(cumulative[N-1]-cumulative[0]); } catch( ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [B]."); } } */ for (int k = 0; k < size; k++) { float x, x1, x2, f1, f2; try { x = Math.abs((f[k] - min) / (max - min)); x1 = (float) Math.floor((float) (N - 1) * x * 0.999f) / (float) (N - 1); x2 = (float) Math.ceil((float) (N - 1) * x * 0.999f) / (float) (N - 1); f1 = (cumulative[(int) Math.floor((float) (N - 1) * x * 0.999f)] - cumulative[0]) / (cumulative[N - 1] - cumulative[0]); f2 = (cumulative[(int) Math.ceil((float) (N - 1) * x * 0.999f)] - cumulative[0]) / (cumulative[N - 1] - cumulative[0]); f[k] = (f2 - f1) * (x - x1) / (x2 - x1) + f1; } catch (ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException in ScalarImage.analyze(double) [C]. " + e.getMessage()); } } return; /* double offset = 0. - min; if ((max - min) != 0f) { scale /= (max - min); offset *= scale; rescale(scale, offset); } System.out.println("Normalizing using: min= " + min + " max= " + max + ", through scaleAdd(" + scale + ", " + offset + ")."); min = 1000f; max = -1000f; for (int k = 0; k < size; k++) { if (Float.isNaN(f[k])) { System.out.println("NaN at k= " + k + " after normalization."); f[k] = 0f; continue; } min = Math.min(min, f[k]); max = Math.max(max, f[k]); } System.out.println("After normalization: min= " + min + " max= " + max); */ }
public void power(double exp) /* Transforms all the scalar values in 'this' by the rule: * f' = f^exp */ { for (int k = 0; k < size; ++k) f[k] = (float) Math.pow(f[k], exp); }