Esempio n. 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);
    }
  }
  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);
  }
Esempio n. 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;
 }
  /**
   * Makes the Mandelbrot image.
   *
   * @param width the width
   * @parah height the height
   * @return the image
   */
  public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
      for (int j = 0; j < height; j++) {
        double a = XMIN + i * (XMAX - XMIN) / width;
        double b = YMIN + j * (YMAX - YMIN) / height;
        if (!escapesToInfinity(a, b)) raster.setDataElements(i, j, colorData);
      }
    return image;
  }
  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);
  }
Esempio n. 6
0
  public static BufferedImage toGrayScale(BufferedImage image) {
    BufferedImage result =
        new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Raster raster = image.getData();
    WritableRaster grayRaster = result.getRaster();
    int height = raster.getHeight();
    int sample = 0;

    for (int x = 0; x < raster.getWidth(); ++x) {
      for (int y = 0; y < height; ++y) {

        sample = raster.getSample(x, y, 0) + raster.getSample(x, y, 1) + raster.getSample(x, y, 2);

        grayRaster.setSample(x, y, 0, (int) ((float) sample / 3.0));
      }
    }
    return result;
  }
Esempio n. 7
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();
  }
Esempio n. 8
0
  /**
   * Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those
   * with up to 256 grays (including simple black and white) or indexed colors from an up to
   * 256-sized color table. Each integer value represents the gray level or the color table index
   * value of the pixel. The Y dimension is not flipped.
   */
  public static int[][] loadPNGFile(InputStream str) throws IOException {
    // read the bytes into a byte array
    BufferedInputStream stream = new BufferedInputStream(str);
    ArrayList list = new ArrayList();
    int count = 0;
    while (true) {
      byte[] buffer = new byte[16384 * 16];
      int len = stream.read(buffer);
      if (len <= 0) // all done
      break;
      else if (len < buffer.length) {
        byte[] buf2 = new byte[len];
        System.arraycopy(buffer, 0, buf2, 0, len);
        buffer = buf2;
      }
      count += len;
      list.add(buffer);
    }
    byte[] data = new byte[count];
    int cur = 0;
    for (int i = 0; i < list.size(); i++) {
      byte[] b = (byte[]) (list.get(i));
      System.arraycopy(b, 0, data, cur, b.length);
      cur += b.length;
    }

    // Next convert the byte array to a buffered image
    BufferedImage image = ((ToolkitImage) (new ImageIcon(data).getImage())).getBufferedImage();

    // Is the color model something we can use?
    int type = image.getType();
    if (type == BufferedImage.TYPE_BYTE_BINARY || type == BufferedImage.TYPE_BYTE_GRAY) {
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) result[i][j] = (image.getRGB(i, j) & 0xFF);
      return result;
    } else if (type == BufferedImage.TYPE_BYTE_INDEXED) {
      Raster raster = image.getRaster();
      if (raster.getTransferType() != DataBuffer.TYPE_BYTE) // uh oh
      throw new IOException("Input Stream must contain an image with byte data if indexed.");
      byte[] pixel = new byte[1];
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) {
          result[i][j] = ((byte[]) (raster.getDataElements(i, j, pixel)))[0];
          if (result[i][j] < 0) result[i][j] += 256;
        }
      return result;
    }
    // else if (type == TYPE_USHORT_GRAY)   // at present we don't handle shorts
    //    {
    //    }
    else
      throw new IOException(
          "Input Stream must contain a binary, byte-sized grayscale, or byte-sized indexed color scheme: "
              + image);
  }
Esempio n. 9
0
  private void createView() {

    int scale = getScaleModeScale(scaleMode);

    if (!useHWScaling(scaleMode)) {

      // Create new BufferedImage with scaled width & height:
      img = new BufferedImage(width * scale, height * scale, BufferedImage.TYPE_INT_RGB);

    } else {

      // Create new BufferedImage with normal width & height:
      img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

      // Create graphics object to use for FPS display:
      gfx = img.createGraphics();
      gfx.setFont(fpsFont);

      // Set rendering hints:
      Graphics2D g2d = (Graphics2D) gfx;
      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

      try {

        // Create hardware accellerated image:
        vimg = createVolatileImage(width, height, new ImageCapabilities(true));

      } catch (Exception e) {

        // Unable to create image. Fall back to software scaling:
        // System.out.println("Unable to create HW accellerated image.");
        scaleMode = SCALE_NORMAL;
        img = new BufferedImage(width * scale, height * scale, BufferedImage.TYPE_INT_RGB);
      }
    }

    // Create graphics object to use for FPS display:
    gfx = img.createGraphics();
    gfx.setFont(fpsFont);

    // Set rendering hints:
    Graphics2D g2d = (Graphics2D) gfx;
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    // Retrieve raster from image:
    DataBufferInt dbi = (DataBufferInt) img.getRaster().getDataBuffer();
    int[] raster = dbi.getData();

    // Replace current rasters with the one used by the image:
    if (scaleMode == SCALE_NONE || scaleMode == SCALE_HW2X || scaleMode == SCALE_HW3X) {

      pix = raster;
      nes.ppu.buffer = raster;

    } else {

      pix_scaled = raster;
    }

    // Set background color:
    for (int i = 0; i < raster.length; i++) {
      raster[i] = bgColor;
    }

    // Set component size & bounds:
    setSize(width * scale, height * scale);
    setBounds(getX(), getY(), width * scale, height * scale);

    // Repaint component:
    this.invalidate();
    repaint();
  }