Exemplo n.º 1
0
  /* Resizing sampling modes */
  private BufferedImage nearestNeighbourSampling(
      int originalWidth,
      int originalHeight,
      int newWidth,
      int newHeight,
      int slice,
      int direction) {
    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_3BYTE_BGR);
    int i, j, c, k;

    byte[] newData = Functions.getImageData(newImage);
    double col;
    short datum;
    int I, J;

    for (j = 0; j < newHeight; j++) {
      for (i = 0; i < newWidth; i++) {
        J = (int) (j * (double) originalHeight / (double) newHeight);
        I = (int) (i * (double) originalWidth / (double) newWidth);

        switch (direction) {
          case 1:
            datum = volume[slice][J][I];
            break;

          case 2:
            datum = volume[J][I][slice];
            break;

          case 3:
            datum = volume[J][slice][I];
            break;

          default:
            datum = 0;
        }

        // calculate the colour by performing a mapping from [min,max] -> [0,255]
        col = Functions.equalizeColourTo255(this, datum);
        for (c = 0; c < 3; c++) {
          // and now we are looping through the bgr components of the pixel
          // set the colour component c of pixel (i,j)
          newData[c + 3 * i + 3 * j * newWidth] = (byte) col;
        } // colour loop
      } // column loop
    } // row loop

    return newImage;
  }
Exemplo n.º 2
0
  private BufferedImage bilinearInterpolationSampling(
      int originalWidth,
      int originalHeight,
      int newWidth,
      int newHeight,
      int slice,
      int direction) {
    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_3BYTE_BGR);
    int i, j, c, k;

    byte[] newData = Functions.getImageData(newImage);
    double col, interpo1, interpo2, interpo3;
    short v_1, v_2, v_3, v_4;
    int I, J;

    // exactly how man times the new image is bigger/smaller than the original
    double stepW = (double) newWidth / (double) originalWidth;
    double stepH = (double) newHeight / (double) originalHeight;

    // System.out.println(newHeight+", "+newWidth+", "+originalHeight+", "+originalWidth+",
    // "+stepH+", "+stepW); /*

    for (j = 0; j < newHeight; j++) {
      J =
          (j < newHeight - stepH)
              ? (int) ((double) (j - (j % (int) stepH)) / stepH)
              : originalHeight - 2;

      for (i = 0; i < newWidth; i++) {
        /*J_1 = (int)(j * (double)originalHeight / (double)newHeight);
        I_1 = (int)(i * (double)originalWidth / (double)newWidth);*/
        I =
            (i < newWidth - stepW)
                ? (int) ((double) (i - (i % (int) stepW)) / stepW)
                : originalWidth - 2;

        // System.out.println(i+" "+I+"; "+j+" "+J);

        switch (direction) {
          case 1:
            v_1 = volume[slice][J][I];
            v_2 = volume[slice][J][I + 1];
            v_3 = volume[slice][J + 1][I];
            v_4 = volume[slice][J + 1][I + 1];
            break;

          case 2:
            v_1 = volume[J][I][slice];
            v_2 = volume[J][I + 1][slice];
            v_3 = volume[J + 1][I][slice];
            v_4 = volume[J + 1][I + 1][slice];
            break;

          case 3:
            v_1 = volume[J][slice][I];
            v_2 = volume[J][slice][I + 1];
            v_3 = volume[J + 1][slice][I];
            v_4 = volume[J + 1][slice][I + 1];
            break;

          default:
            v_1 = 0;
            v_2 = 0;
            v_3 = 0;
            v_4 = 0;
        }

        interpo1 =
            Functions.linearInterpolation(
                (double) v_1,
                (double) v_2,
                I,
                (double) i * (double) originalWidth / (double) newWidth,
                I + 1);
        interpo2 =
            Functions.linearInterpolation(
                (double) v_3,
                (double) v_4,
                I,
                (double) i * (double) originalWidth / (double) newWidth,
                I + 1);
        interpo3 =
            Functions.linearInterpolation(
                interpo1,
                interpo2,
                J,
                (double) j * (double) originalHeight / (double) newHeight,
                J + 1);

        // calculate the colour by performing a mapping from [min,max] -> [0,255]
        col = Functions.equalizeColourTo255(this, interpo3);
        if (col > 250)
          System.out.println(
              interpo3
                  + " -- "
                  + i
                  + ": "
                  + I
                  + " / "
                  + j
                  + ": "
                  + J
                  + " | "
                  + newData[3 * i + 3 * j * newWidth]
                  + ": "
                  + col
                  + " $ "
                  + v_1
                  + " $ "
                  + v_2
                  + " $ "
                  + v_3
                  + " $ "
                  + v_4);

        for (c = 0; c < 3; c++) {
          // and now we are looping through the bgr components of the pixel
          // set the colour component c of pixel (i,j)
          newData[c + 3 * i + 3 * j * newWidth] = (byte) col;
        } // colour loop
      } // column loop
    } // row loop

    return newImage;
  }