Example #1
0
  public void fitSpline(int evaluationPoints) {
    if (xpf == null) {
      xpf = toFloat(xp);
      ypf = toFloat(yp);
      subPixel = true;
    }
    if (xSpline == null || splinePoints != evaluationPoints) {
      splinePoints = evaluationPoints;
      xSpline = new float[splinePoints];
      ySpline = new float[splinePoints];
    }
    int nNodes = nPoints;
    if (type == POLYGON) {
      nNodes++;
      if (nNodes >= xpf.length) enlargeArrays();
      xpf[nNodes - 1] = xpf[0];
      ypf[nNodes - 1] = ypf[0];
    }
    float[] xindex = new float[nNodes];
    for (int i = 0; i < nNodes; i++) xindex[i] = i;
    SplineFitter sfx = new SplineFitter(xindex, xpf, nNodes);
    SplineFitter sfy = new SplineFitter(xindex, ypf, nNodes);

    // Evaluate the splines at all points
    double scale = (double) (nNodes - 1) / (splinePoints - 1);
    float xs = 0f, ys = 0f;
    float xmin = Float.MAX_VALUE, xmax = -xmin, ymin = xmin, ymax = xmax;
    for (int i = 0; i < splinePoints; i++) {
      double xvalue = i * scale;
      xs = (float) sfx.evalSpline(xindex, xpf, nNodes, xvalue);
      if (xs < xmin) xmin = xs;
      if (xs > xmax) xmax = xs;
      xSpline[i] = xs;
      ys = (float) sfy.evalSpline(xindex, ypf, nNodes, xvalue);
      if (ys < ymin) ymin = ys;
      if (ys > ymax) ymax = ys;
      ySpline[i] = ys;
    }
    int ixmin = (int) Math.floor(xmin + 0.5f);
    int ixmax = (int) Math.floor(xmax + 0.5f);
    int iymin = (int) Math.floor(ymin + 0.5f);
    int iymax = (int) Math.floor(ymax + 0.5f);
    if (ixmin != 0) {
      for (int i = 0; i < nPoints; i++) xpf[i] -= ixmin;
      for (int i = 0; i < splinePoints; i++) xSpline[i] -= ixmin;
    }
    if (iymin != 0) {
      for (int i = 0; i < nPoints; i++) ypf[i] -= iymin;
      for (int i = 0; i < splinePoints; i++) ySpline[i] -= iymin;
    }
    x += ixmin;
    y += iymin;
    width = ixmax - ixmin;
    height = iymax - iymin;
    bounds = null;
    cachedMask = null;
    // update protected xp and yp arrays for backward compatibility
    xp = toInt(xpf, xp, nPoints);
    yp = toInt(ypf, yp, nPoints);
  }
  public Color getColorAt(double x, double y) {
    int col = img.getRGB((int) x, (int) y);
    double r = col >> 16 & 0xff;
    double g = col >> 8 & 0xff;
    double b = col & 0xff;

    int col_r = img.getRGB((int) x + 1, (int) y);
    double rr = col_r >> 16 & 0xff;
    double gr = col_r >> 8 & 0xff;
    double br = col_r & 0xff;

    double fact = x - Math.floor(x);

    double rf = r + (rr - r) * fact;
    double gf = g + (gr - g) * fact;
    double bf = b + (br - b) * fact;

    col = img.getRGB((int) x, (int) y + 1);
    r = col >> 16 & 0xff;
    g = col >> 8 & 0xff;
    b = col & 0xff;

    col_r = img.getRGB((int) x + 1, (int) y + 1);
    rr = col_r >> 16 & 0xff;
    gr = col_r >> 8 & 0xff;
    br = col_r & 0xff;

    double rf2 = r + (rr - r) * fact;
    double gf2 = g + (gr - g) * fact;
    double bf2 = b + (br - b) * fact;

    fact = y - Math.floor(y);

    double rff = rf + (rf2 - rf) * fact;
    double gff = gf + (gf2 - gf) * fact;
    double bff = bf + (bf2 - bf) * fact;

    return new Color((int) rff, (int) gff, (int) bff);
  }
Example #3
0
  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);
  }
Example #4
0
  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);
  }
Example #5
0
 private final int YtoTileY(int y) {
   return (int) Math.floor((double) (y - tileGridYOffset) / tileHeight);
 }
Example #6
0
 private final int XtoTileX(int x) {
   return (int) Math.floor((double) (x - tileGridXOffset) / tileWidth);
 }
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
              dstCM.isAlphaPremultiplied(),
              null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);

    if (interpolation == NEAREST_NEIGHBOUR)
      return filterPixelsNN(dst, width, height, inPixels, transformedSpace);

    int srcWidth = width;
    int srcHeight = height;
    int srcWidth1 = width - 1;
    int srcHeight1 = height - 1;
    int outWidth = transformedSpace.width;
    int outHeight = transformedSpace.height;
    int outX, outY;
    int index = 0;
    int[] outPixels = new int[outWidth];

    outX = transformedSpace.x;
    outY = transformedSpace.y;
    float[] out = new float[2];

    for (int y = 0; y < outHeight; y++) {
      for (int x = 0; x < outWidth; x++) {
        transformInverse(outX + x, outY + y, out);
        int srcX = (int) Math.floor(out[0]);
        int srcY = (int) Math.floor(out[1]);
        float xWeight = out[0] - srcX;
        float yWeight = out[1] - srcY;
        int nw, ne, sw, se;

        if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
          // Easy case, all corners are in the image
          int i = srcWidth * srcY + srcX;
          nw = inPixels[i];
          ne = inPixels[i + 1];
          sw = inPixels[i + srcWidth];
          se = inPixels[i + srcWidth + 1];
        } else {
          // Some of the corners are off the image
          nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
          ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
          sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
          se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
        }
        outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
      }
      setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
    }
    return dst;
  }
Example #8
0
  /**
   * Parse a double value
   *
   * @param d double to parse
   * @param n number of significant figures
   * @param p precision of the number
   * @param f format of the number scientific, algebraic etc. return <i>true</i> if the parse was
   *     successful
   */
  public boolean parseDouble(double d, int n, int p, int f) {
    double x = d;
    int left = n - p;
    double right = 0;
    int power;
    int exponent;
    int i;
    StringBuffer s = new StringBuffer(n + 4);

    if (left < 0) {
      System.out.println("TextLine.parseDouble: Precision > significant figures!");
      return false;
    }

    if (d < 0.0) {
      x = -d;
      s.append("-");
    }

    // System.out.println("parseDouble: value = "+x);

    if (d == 0.0) exponent = 0;
    else exponent = (int) (Math.floor(SpecialFunction.log10(x)));

    // System.out.println("parseDouble: exponent = "+exponent);

    power = exponent - (left - 1);

    // System.out.println("parseDouble: power = "+power);

    if (power < 0) {
      for (i = power; i < 0; i++) {
        x *= 10.0;
      }
    } else {
      for (i = 0; i < power; i++) {
        x /= 10.0;
      }
    }

    // System.out.println("parseDouble: adjusted value = "+x);

    left = (int) x;
    s.append(left);

    // System.out.println("parseDouble: left = "+left);

    if (p > 0) {
      s.append('.');
      right = x - left;
      for (i = 0; i < p; i++) {
        right *= 10;
        if (i == p - 1) right += 0.5;
        s.append((int) (right));
        right -= (int) right;
      }
    }

    // System.out.println("parseDouble: right = "+right);

    if (power != 0) {
      if (f == SCIENTIFIC) {
        s.append('E');
        if (power < 0) s.append('-');
        else s.append('+');
        power = Math.abs(power);
        if (power > 9) {
          s.append(power);
        } else {
          s.append('0');
          s.append(power);
        }
      } else {
        s.append("x10{^");
        s.append(power);
        s.append("}");
      }
    }

    setText(s.toString());

    return true;
  }
  @Override
  public void paint(Graphics g) {
    try {
      Graphics2D g2d = (Graphics2D) g;
      if (numPaletteEntries > 50) {
        g2d.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
      }
      if (paletteFile != null
          && (paletteFile.toLowerCase().contains("qual")
              || paletteFile.toLowerCase().contains("categorical"))) {
        g2d.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
      }
      int width = getWidth();
      int height = getHeight();

      Image image = null;
      int numDisplayedPaletteEntries = 0;
      if (!categorical) {
        numDisplayedPaletteEntries = paletteData.length;
      } else {
        numDisplayedPaletteEntries =
            (int)
                (Math.floor((maxValue - minValue + 1) / numPaletteEntries)
                    + (maxValue - minValue + 1) % numPaletteEntries);
      }
      int[] imageData = new int[numDisplayedPaletteEntries];
      int i, j;
      int numPaletteEntriesLessOne = numPaletteEntries - 1;
      if (!isReversed && orientation == VERTICAL_ORIENTATION) {
        if (!categorical) {
          for (i = 0; i < numPaletteEntries; i++) {
            j =
                (int)
                    ((Math.pow((1 - ((double) (i) / numPaletteEntriesLessOne)), gamma))
                        * numPaletteEntriesLessOne);
            imageData[i] = paletteData[j];
          }
        } else { // it is categorical
          for (i = 0; i < numDisplayedPaletteEntries; i++) {
            j = (int) (i % numPaletteEntries);
            imageData[numDisplayedPaletteEntries - 1 - i] = paletteData[j];
          }
        }
        image = createImage(new MemoryImageSource(1, numDisplayedPaletteEntries, imageData, 0, 1));
      } else if (!isReversed && orientation == HORIZONTAL_ORIENTATION) {
        for (i = 0; i < numPaletteEntries; i++) {
          j =
              (int)
                  ((Math.pow(((double) (i) / numPaletteEntriesLessOne), gamma))
                      * numPaletteEntriesLessOne);
          imageData[i] = paletteData[j];
        }
        image = createImage(new MemoryImageSource(numPaletteEntries, 1, imageData, 0, 1));
      } else if (isReversed && orientation == VERTICAL_ORIENTATION) {
        if (!categorical) {
          for (i = 0; i < numPaletteEntries; i++) {
            j =
                (int)
                    ((Math.pow(((double) (i) / numPaletteEntriesLessOne), gamma))
                        * numPaletteEntriesLessOne);
            imageData[i] = paletteData[j];
          }
        } else { // it is categorical
          for (i = 0; i < numDisplayedPaletteEntries; i++) {
            j = (int) (numPaletteEntries - i % numPaletteEntries - 1);
            imageData[numDisplayedPaletteEntries - 1 - i] = paletteData[j];
          }
        }
        image = createImage(new MemoryImageSource(1, numDisplayedPaletteEntries, imageData, 0, 1));
      } else if (isReversed && orientation == HORIZONTAL_ORIENTATION) {
        for (i = 0; i < numPaletteEntries; i++) {
          j =
              (int)
                  ((Math.pow((1 - ((double) (i) / numPaletteEntriesLessOne)), gamma))
                      * numPaletteEntriesLessOne);
          imageData[i] = paletteData[j];
        }
        image = createImage(new MemoryImageSource(imageData.length, 1, imageData, 0, 1));
      }

      g.drawImage(image, 0, 0, width, height, this);
      if (!isSelected) {
        g.setColor(Color.black);
        g.drawRect(0, 0, width - 1, height - 1);
      } else {
        g.setColor(Color.white);
        g.drawRect(1, 1, width - 3, height - 3);
        g.setColor(Color.red);
        g.drawRect(0, 0, width - 1, height - 1);
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
  public Image getPaletteImage() {
    try {
      int width = getWidth();
      int height = getHeight();

      Image image = null;
      int numDisplayedPaletteEntries = 0;
      if (!categorical) {
        numDisplayedPaletteEntries = paletteData.length;
      } else {
        numDisplayedPaletteEntries =
            (int)
                (Math.floor((maxValue - minValue + 1) / numPaletteEntries)
                    + (maxValue - minValue + 1) % numPaletteEntries);
      }
      int[] imageData = new int[numDisplayedPaletteEntries];
      int i, j;
      int numPaletteEntriesLessOne = numPaletteEntries - 1;
      if (!isReversed && orientation == VERTICAL_ORIENTATION) {
        if (!categorical) {
          for (i = 0; i < numPaletteEntries; i++) {
            j =
                (int)
                    ((Math.pow((1 - ((double) (i) / numPaletteEntriesLessOne)), gamma))
                        * numPaletteEntriesLessOne);
            imageData[i] = paletteData[j];
          }
        } else { // it is categorical
          for (i = 0; i < numDisplayedPaletteEntries; i++) {
            j = (int) (i % numPaletteEntries);
            imageData[numDisplayedPaletteEntries - 1 - i] = paletteData[j];
          }
        }
        image = createImage(new MemoryImageSource(1, numDisplayedPaletteEntries, imageData, 0, 1));
      } else if (!isReversed && orientation == HORIZONTAL_ORIENTATION) {
        for (i = 0; i < numPaletteEntries; i++) {
          j =
              (int)
                  ((Math.pow(((double) (i) / numPaletteEntriesLessOne), gamma))
                      * numPaletteEntriesLessOne);
          imageData[i] = paletteData[j];
        }
        image = createImage(new MemoryImageSource(numPaletteEntries, 1, imageData, 0, 1));
      } else if (isReversed && orientation == VERTICAL_ORIENTATION) {
        if (!categorical) {
          for (i = 0; i < numPaletteEntries; i++) {
            j =
                (int)
                    ((Math.pow(((double) (i) / numPaletteEntriesLessOne), gamma))
                        * numPaletteEntriesLessOne);
            imageData[i] = paletteData[j];
          }
        } else { // it is categorical
          for (i = 0; i < numDisplayedPaletteEntries; i++) {
            j = (int) (numPaletteEntries - i % numPaletteEntries - 1);
            imageData[numDisplayedPaletteEntries - 1 - i] = paletteData[j];
          }
        }
        image = createImage(new MemoryImageSource(1, numDisplayedPaletteEntries, imageData, 0, 1));
      } else if (isReversed && orientation == HORIZONTAL_ORIENTATION) {
        for (i = 0; i < numPaletteEntries; i++) {
          j =
              (int)
                  ((Math.pow((1 - ((double) (i) / numPaletteEntriesLessOne)), gamma))
                      * numPaletteEntriesLessOne);
          imageData[i] = paletteData[j];
        }
        image = createImage(new MemoryImageSource(imageData.length, 1, imageData, 0, 1));
      }
      return image;
    } catch (Exception e) {
      return null;
    }
  }
Example #11
0
  public static BufferedImage convolvedown(BufferedImage img, Coord tsz, Convolution filter) {
    Raster in = img.getRaster();
    int w = in.getWidth(), h = in.getHeight(), nb = in.getNumBands();
    double xf = (double) w / (double) tsz.x, ixf = 1.0 / xf;
    double yf = (double) h / (double) tsz.y, iyf = 1.0 / yf;
    double[] ca = new double[nb];
    WritableRaster buf = byteraster(new Coord(tsz.x, h), nb);
    double support = filter.support();

    {
      double[] cf = new double[tsz.x * (int) Math.ceil(2 * support * xf + 2)];
      int[] cl = new int[tsz.x];
      int[] cr = new int[tsz.x];
      for (int x = 0, ci = 0; x < tsz.x; x++) {
        int si = ci;
        double wa = 0.0;
        cl[x] = Math.max((int) Math.floor((x + 0.5 - support) * xf), 0);
        cr[x] = Math.min((int) Math.ceil((x + 0.5 + support) * xf), w - 1);
        for (int sx = cl[x]; sx <= cr[x]; sx++) {
          double tx = ((sx + 0.5) * ixf) - x - 0.5;
          double fw = filter.cval(tx);
          wa += fw;
          cf[ci++] = fw;
        }
        wa = 1.0 / wa;
        for (; si < ci; si++) cf[si] *= wa;
      }
      for (int y = 0; y < h; y++) {
        for (int x = 0, ci = 0; x < tsz.x; x++) {
          for (int b = 0; b < nb; b++) ca[b] = 0.0;
          for (int sx = cl[x]; sx <= cr[x]; sx++) {
            double fw = cf[ci++];
            for (int b = 0; b < nb; b++) ca[b] += in.getSample(sx, y, b) * fw;
          }
          for (int b = 0; b < nb; b++) buf.setSample(x, y, b, Utils.clip((int) ca[b], 0, 255));
        }
      }
    }

    WritableRaster res = byteraster(tsz, nb);
    {
      double[] cf = new double[tsz.y * (int) Math.ceil(2 * support * yf + 2)];
      int[] cu = new int[tsz.y];
      int[] cd = new int[tsz.y];
      for (int y = 0, ci = 0; y < tsz.y; y++) {
        int si = ci;
        double wa = 0.0;
        cu[y] = Math.max((int) Math.floor((y + 0.5 - support) * yf), 0);
        cd[y] = Math.min((int) Math.ceil((y + 0.5 + support) * yf), h - 1);
        for (int sy = cu[y]; sy <= cd[y]; sy++) {
          double ty = ((sy + 0.5) * iyf) - y - 0.5;
          double fw = filter.cval(ty);
          wa += fw;
          cf[ci++] = fw;
        }
        wa = 1.0 / wa;
        for (; si < ci; si++) cf[si] *= wa;
      }
      for (int x = 0; x < tsz.x; x++) {
        for (int y = 0, ci = 0; y < tsz.y; y++) {
          for (int b = 0; b < nb; b++) ca[b] = 0.0;
          for (int sy = cu[y]; sy <= cd[y]; sy++) {
            double fw = cf[ci++];
            for (int b = 0; b < nb; b++) ca[b] += buf.getSample(x, sy, b) * fw;
          }
          for (int b = 0; b < nb; b++) res.setSample(x, y, b, Utils.clip((int) ca[b], 0, 255));
        }
      }
    }
    return (new BufferedImage(img.getColorModel(), res, false, null));
  }
Example #12
0
  /**
   * DOCUMENT ME!
   *
   * @param out DOCUMENT ME!
   */
  public void WriteCompressedData(OutputStream out) throws IOException {
    int offset;
    int i;
    int j;
    int r;
    int c;
    int a;
    int b;
    int temp = 0;
    int comp;
    int xpos;
    int ypos;
    int xblockoffset;
    int yblockoffset;
    float[][] inputArray;
    float[][] dctArray1 = new float[8][8];
    double[][] dctArray2 = new double[8][8];
    int[] dctArray3 = new int[8 * 8];

    /*
     * This method controls the compression of the image.
     * Starting at the upper left of the image, it compresses 8x8 blocks
     * of data until the entire image has been compressed.
     */
    int[] lastDCvalue = new int[JpegObj.NumberOfComponents];
    int[] zeroArray = new int[64]; // initialized to hold all zeros
    int Width = 0;
    int Height = 0;
    int nothing = 0;
    int not;
    int MinBlockWidth;
    int MinBlockHeight;

    // This initial setting of MinBlockWidth and MinBlockHeight is done to
    // ensure they start with values larger than will actually be the case.
    MinBlockWidth =
        (((imageWidth % 8) != 0)
            ? ((int) (Math.floor((double) imageWidth / 8.0) + 1) * 8)
            : imageWidth);
    MinBlockHeight =
        (((imageHeight % 8) != 0)
            ? ((int) (Math.floor((double) imageHeight / 8.0) + 1) * 8)
            : imageHeight);

    for (comp = 0; comp < JpegObj.NumberOfComponents; comp++) {
      MinBlockWidth = Math.min(MinBlockWidth, JpegObj.BlockWidth[comp]);
      MinBlockHeight = Math.min(MinBlockHeight, JpegObj.BlockHeight[comp]);
    }

    xpos = 0;

    for (r = 0; r < MinBlockHeight; r++) {
      for (c = 0; c < MinBlockWidth; c++) {
        xpos = c * 8;
        ypos = r * 8;

        for (comp = 0; comp < JpegObj.NumberOfComponents; comp++) {
          Width = JpegObj.BlockWidth[comp];
          Height = JpegObj.BlockHeight[comp];
          inputArray = (float[][]) JpegObj.Components[comp];

          for (i = 0; i < JpegObj.VsampFactor[comp]; i++) {
            for (j = 0; j < JpegObj.HsampFactor[comp]; j++) {

              xblockoffset = j * 8;
              yblockoffset = i * 8;

              for (a = 0; a < 8; a++) {
                for (b = 0; b < 8; b++) {
                  // I believe this is where the dirty line at the bottom of the image is
                  // coming from.  I need to do a check here to make sure I'm not reading past
                  // image data.
                  // This seems to not be a big issue right now. (04/04/98)
                  dctArray1[a][b] = inputArray[ypos + yblockoffset + a][xpos + xblockoffset + b];
                }
              }

              // The following code commented out because on some images this technique
              // results in poor right and bottom borders.
              //                        if ((!JpegObj.lastColumnIsDummy[comp] || c < Width - 1) &&
              // (!JpegObj.lastRowIsDummy[comp] || r < Height - 1)) {
              dctArray2 = dct.forwardDCT(dctArray1);
              dctArray3 = dct.quantizeBlock(dctArray2, JpegObj.QtableNumber[comp]);

              //                        }
              //                        else {
              //                           zeroArray[0] = dctArray3[0];
              //                           zeroArray[0] = lastDCvalue[comp];
              //                           dctArray3 = zeroArray;
              //                        }
              Huf.HuffmanBlockEncoder(
                  out,
                  dctArray3,
                  lastDCvalue[comp],
                  JpegObj.DCtableNumber[comp],
                  JpegObj.ACtableNumber[comp]);
              lastDCvalue[comp] = dctArray3[0];
            }
          }
        }
      }
    }

    Huf.flushBuffer(out);
  }
Example #13
0
  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);
    */
  }
 /**
  * Gets a list of Point2D objects that lie within pixels in a rectangle and along a line.
  *
  * @param searchRect the rectangle
  * @param x0 the x-component of a point on the line
  * @param y0 the y-component of a point on the line
  * @param slope the slope of the line
  * @return a list of Point2D
  */
 public ArrayList<Point2D> getSearchPoints(
     Rectangle searchRect, double x0, double y0, double theta) {
   double slope = -Math.tan(theta);
   // create line to search along
   Line2D line = new Line2D.Double();
   if (slope > LARGE_NUMBER) {
     line.setLine(x0, y0, x0, y0 + 1);
   } else if (slope < 1 / LARGE_NUMBER) {
     line.setLine(x0, y0, x0 + 1, y0);
   } else {
     line.setLine(x0, y0, x0 + 1, y0 + slope);
   }
   // create intersection points (to set line ends)
   Point2D p1 = new Point2D.Double();
   Point2D p2 = new Point2D.Double(Double.NaN, Double.NaN);
   Point2D p = p1;
   boolean foundBoth = false;
   double d = searchRect.x;
   Object[] data = getDistanceAndPointAtX(line, d);
   if (data != null) {
     p.setLocation((Point2D) data[1]);
     if (p.getY() >= searchRect.y && p.getY() <= searchRect.y + searchRect.height) {
       // line end is left edge
       p = p2;
     }
   }
   d += searchRect.width;
   data = getDistanceAndPointAtX(line, d);
   if (data != null) {
     p.setLocation((Point2D) data[1]);
     if (p.getY() >= searchRect.y && p.getY() <= searchRect.y + searchRect.height) {
       // line end is right edge
       if (p == p1) p = p2;
       else foundBoth = true;
     }
   }
   if (!foundBoth) {
     d = searchRect.y;
     data = getDistanceAndPointAtY(line, d);
     if (data != null) {
       p.setLocation((Point2D) data[1]);
       if (p.getX() >= searchRect.x && p.getX() <= searchRect.x + searchRect.width) {
         // line end is top edge
         if (p == p1) p = p2;
         else if (!p1.equals(p2)) foundBoth = true;
       }
     }
   }
   if (!foundBoth) {
     d += searchRect.height;
     data = getDistanceAndPointAtY(line, d);
     if (data != null) {
       p.setLocation((Point2D) data[1]);
       if (p.getX() >= searchRect.x && p.getX() <= searchRect.x + searchRect.width) {
         // line end is bottom edge
         if (p == p2 && !p1.equals(p2)) foundBoth = true;
       }
     }
   }
   // if both line ends have been found, use line to find pixels to search
   if (foundBoth) {
     // set line ends to intersections
     line.setLine(p1, p2);
     if (p1.getX() > p2.getX()) {
       line.setLine(p2, p1);
     }
     // find pixel intersections that fall along the line
     int xMin = (int) Math.ceil(Math.min(p1.getX(), p2.getX()));
     int xMax = (int) Math.floor(Math.max(p1.getX(), p2.getX()));
     int yMin = (int) Math.ceil(Math.min(p1.getY(), p2.getY()));
     int yMax = (int) Math.floor(Math.max(p1.getY(), p2.getY()));
     // collect intersections in TreeMap sorted by position along line
     TreeMap<Double, Point2D> intersections = new TreeMap<Double, Point2D>();
     for (int x = xMin; x <= xMax; x++) {
       Object[] next = getDistanceAndPointAtX(line, x);
       intersections.put((Double) next[0], (Point2D) next[1]);
     }
     for (int y = yMin; y <= yMax; y++) {
       Object[] next = getDistanceAndPointAtY(line, y);
       intersections.put((Double) next[0], (Point2D) next[1]);
     }
     p = null;
     // create array of search points that are midway between intersections
     ArrayList<Point2D> searchPts = new ArrayList<Point2D>();
     for (Double key : intersections.keySet()) {
       Point2D next = intersections.get(key);
       if (p != null) {
         double x = (p.getX() + next.getX()) / 2 - searchRect.x;
         double y = (p.getY() + next.getY()) / 2 - searchRect.y;
         p.setLocation(x, y);
         searchPts.add(p);
       }
       p = next;
     }
     return searchPts;
   }
   return null;
 }