Пример #1
0
  /**
   * Save onscreen image to file - suffix must be png, jpg, or gif.
   *
   * @param filename the name of the file with one of the required suffixes
   */
  public static void save(String filename) {
    File file = new File(filename);
    String suffix = filename.substring(filename.lastIndexOf('.') + 1);

    // png files
    if (suffix.toLowerCase().equals("png")) {
      try {
        ImageIO.write(onscreenImage, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // need to change from ARGB to RGB for jpeg
    // reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&L=java2d-interest&D=0&P=2727
    else if (suffix.toLowerCase().equals("jpg")) {
      WritableRaster raster = onscreenImage.getRaster();
      WritableRaster newRaster;
      newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
      DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
      DirectColorModel newCM =
          new DirectColorModel(
              cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
      BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
      try {
        ImageIO.write(rgbBuffer, suffix, file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      System.out.println("Invalid image file type: " + suffix);
    }
  }
Пример #2
0
  public void sobel() {
    sobel = new BufferedImage(width, height, filtered.getType());
    Graphics2D g = sobel.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(0, 0, width, height);
    g.dispose();

    int[] tmp = new int[1];
    sX = new double[height][width];
    sY = new double[height][width];
    double maxX = 0;
    double maxY = 0;
    for (int y = 1; y < height - 1; ++y)
      for (int x = 1; x < width - 1; ++x) {
        double Xvalue = 0;
        double Yvalue = 0;
        for (int j = -1; j <= 1; ++j)
          for (int i = -1; i <= 1; ++i) {

            Xvalue += GX[1 + j][1 + i] * filtered.getRaster().getPixel(x + i, y + j, tmp)[0];
            Yvalue += GY[1 + j][1 + i] * filtered.getRaster().getPixel(x + i, y + j, tmp)[0];
          }
        if (Xvalue > maxX) maxX = Xvalue;
        if (Yvalue > maxY) maxY = Yvalue;
        sX[y][x] = Xvalue;
        sY[y][x] = Yvalue;
      }

    for (int y = 1; y < height - 1; ++y)
      for (int x = 1; x < width - 1; ++x) {
        double[] a = {(Math.abs((sX[y][x] / maxX * 255)) + Math.abs((sY[y][x] / maxY) * 255))};
        // if (a[0] > 0) binary[y][x] = 1;
        // if (a[0] <= 0) binary[y][x] = 0;

        sobel.getRaster().setPixel(x, y, a);
      }
    ImageIcon icon2 = new ImageIcon(sobel);
    lbl2.setIcon(icon2);
  }
Пример #3
0
 //     private BufferedImage makeBI(String str) {
 //         BufferedImage image;
 //         try {
 //             image = ImageIO.read(getClass().getResource(str));
 //         } catch (IOException ioe) {
 //             ioe.printStackTrace();
 //             return null;
 //         }
 //         return image;
 //     }
 private static int[] getData(ImageIcon imageIcon, int w, int h) {
   Image img = imageIcon.getImage();
   BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
   Graphics g = image.createGraphics();
   g.drawImage(img, 0, 0, null);
   g.dispose();
   return ((DataBufferInt) (image.getRaster().getDataBuffer())).getData();
   //         int[] pixels = new int[w * h];
   //         try {
   //             new PixelGrabber(image, 0, 0, width, height, pixels, 0, width).grabPixels();
   //         } catch (InterruptedException ex) {
   //             ex.printStackTrace();
   //         }
   //         return pixels;
 }
Пример #4
0
  private void buildAccumulator(int r) {
    accImage = new BufferedImage(width, height, greyScale.getType());
    Graphics2D g = accImage.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(0, 0, width, height);
    g.dispose();
    int max = 0;

    double[] a = new double[1];
    for (int y = 0; y < height; ++y)
      for (int x = 0; x < width; ++x) {
        a[0] = acc[y * width + x][r - rmin] & 0xFF;
        accImage.getRaster().setPixel(x, y, a);
      }
    ImageIcon icon2 = new ImageIcon(accImage);
    lbl2.setIcon(icon2);
  }
Пример #5
0
  /** Regenerates the image. */
  private synchronized void regenerateImage() {
    int size =
        Math.min(
            MAX_SIZE,
            Math.min(
                getWidth() - imagePadding.left - imagePadding.right,
                getHeight() - imagePadding.top - imagePadding.bottom));

    if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) {
      float bri2 = this.bri;
      float sat2 = this.sat;
      float radius = ((float) size) / 2f;
      float hue2;
      float k = 1.2f; // the number of pixels to antialias
      for (int y = 0; y < size; y++) {
        float y2 = (y - size / 2f);
        for (int x = 0; x < size; x++) {
          float x2 = (x - size / 2f);
          double theta = Math.atan2(y2, x2) - 3 * Math.PI / 2.0;
          if (theta < 0) theta += 2 * Math.PI;

          double r = Math.sqrt(x2 * x2 + y2 * y2);
          if (r <= radius) {
            if (mode == ColorPicker.BRI) {
              hue2 = (float) (theta / (2 * Math.PI));
              sat2 = (float) (r / radius);
            } else { // SAT
              hue2 = (float) (theta / (2 * Math.PI));
              bri2 = (float) (r / radius);
            }
            row[x] = Color.HSBtoRGB(hue2, sat2, bri2);
            if (r > radius - k) {
              int alpha = (int) (255 - 255 * (r - radius + k) / k);
              if (alpha < 0) alpha = 0;
              if (alpha > 255) alpha = 255;
              row[x] = row[x] & 0xffffff + (alpha << 24);
            }
          } else {
            row[x] = 0x00000000;
          }
        }
        image.getRaster().setDataElements(0, y, size, 1, row);
      }
    } else if (mode == ColorPicker.HUE) {
      float hue2 = this.hue;
      for (int y = 0; y < size; y++) {
        float y2 = ((float) y) / ((float) size);
        for (int x = 0; x < size; x++) {
          float x2 = ((float) x) / ((float) size);
          row[x] = Color.HSBtoRGB(hue2, x2, y2);
        }
        image.getRaster().setDataElements(0, y, image.getWidth(), 1, row);
      }
    } else { // mode is RED, GREEN, or BLUE
      int red2 = red;
      int green2 = green;
      int blue2 = blue;
      for (int y = 0; y < size; y++) {
        float y2 = ((float) y) / ((float) size);
        for (int x = 0; x < size; x++) {
          float x2 = ((float) x) / ((float) size);
          if (mode == ColorPicker.RED) {
            green2 = (int) (x2 * 255 + .49);
            blue2 = (int) (y2 * 255 + .49);
          } else if (mode == ColorPicker.GREEN) {
            red2 = (int) (x2 * 255 + .49);
            blue2 = (int) (y2 * 255 + .49);
          } else {
            red2 = (int) (x2 * 255 + .49);
            green2 = (int) (y2 * 255 + .49);
          }
          row[x] = 0xFF000000 + (red2 << 16) + (green2 << 8) + blue2;
        }
        image.getRaster().setDataElements(0, y, size, 1, row);
      }
    }
    repaint();
  }