Пример #1
0
  public Iterator handlePixels(Image img, Rectangle rect) {
    int x = rect.x;
    int y = rect.y;
    int w = rect.width;
    int h = rect.height;

    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      System.err.println("interrupted waiting for pixels!");
      return null;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
      System.err.println("image fetch aborted or errored");
      return null;
    }
    ArrayList tmpList = new ArrayList();
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        tmpList.add(handleSinglePixel(x + i, y + j, pixels[j * w + i]));
      }
    }
    return tmpList.iterator();
  }
Пример #2
0
  /**
   * Construct a GIFEncoder. The constructor will convert the image to an indexed color array.
   * <B>This may take some time.</B>
   *
   * <p>
   *
   * @param image The image to encode. The image <B>must</B> be completely loaded.
   * @exception AWTException Will be thrown if the pixel grab fails. This can happen if Java runs
   *     out of memory. It may also indicate that the image contains more than 256 colors.
   */
  public GIFEncoder(Image image) throws AWTException {
    width_ = (short) image.getWidth(null);
    height_ = (short) image.getHeight(null);

    int values[] = new int[width_ * height_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);

    try {
      if (grabber.grabPixels() != true)
        throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException e) {;
    }

    byte r[][] = new byte[width_][height_];
    byte g[][] = new byte[width_][height_];
    byte b[][] = new byte[width_][height_];
    int index = 0;
    for (int y = 0; y < height_; ++y)
      for (int x = 0; x < width_; ++x) {
        r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
        g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
        b[x][y] = (byte) ((values[index]) & 0xFF);
        ++index;
      }
    ToIndexedColor(r, g, b);
  }
Пример #3
0
 /**
  * Return the Image pixels in default Java int ARGB format.
  *
  * @return
  */
 public static int[] getImagePixels(Image image) {
   int[] pixelsARGB = null;
   if (image != null) {
     int imgw = image.getWidth(null);
     int imgh = image.getHeight(null);
     pixelsARGB = new int[imgw * imgh];
     PixelGrabber pg = new PixelGrabber(image, 0, 0, imgw, imgh, pixelsARGB, 0, imgw);
     try {
       pg.grabPixels();
     } catch (Exception e) {
       GLApp.err("Pixel Grabbing interrupted!");
       return null;
     }
   }
   return pixelsARGB;
 }
Пример #4
0
 private void grabPixels(PixelGrabber grabber) {
   try {
     grabber.grabPixels();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   // if (grabber.getColorModel() != ColorModel.getRGBdefault()) {
   //	System.err.println("Warning: found other colormodel than default.");
   // }
 }
Пример #5
0
  // This method returns true if the specified image has transparent pixels
  // Source: http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html
  public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
      BufferedImage bimage = (BufferedImage) image;
      return bimage.getColorModel().hasAlpha();
    }

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
  }
Пример #6
0
  private void createImage2() {
    java.awt.Image img2 = createImage(height, width);
    Graphics g = img2.getGraphics();

    g.setColor(Color.lightGray);
    g.fillRect(0, 0, height, width);

    g.setColor(Color.black);
    FontMetrics fm = g.getFontMetrics();
    String str = "Work Unit keyrate (kkeys/sec)";
    int length = fm.stringWidth(str);
    g.drawString(str, (height / 2) - length / 2, fm.getHeight());
    g.dispose();
    g.finalize();
    img2.flush();

    int[] pixels = new int[height * width];
    PixelGrabber pg = new PixelGrabber(img2, 0, 0, height, width, pixels, 0, height);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      System.err.println("interrupted waiting for pixels!");
      return;
    }

    /* Rotate the Image */
    int pixels2[] = new int[height * width];
    for (int x = 0; x < width; x++)
      for (int y = 0; y < height; y++) {
        int c = pixels[y + (x) * height];

        // Due a bug in the MS-JavaVM the Background for Images are not the same
        // as for Panel's  so. mark it as non-opaque ..
        if (c != 0xff000000) {
          c = 0;
        }
        pixels2[x + (height - y - 1) * width] = c;
      }

    img = createImage(new MemoryImageSource(width, height, pixels2, 0, width));
    repaint();
  }
Пример #7
0
  public getPicInfo(Frame father) {
    try {
      FileDialog diag = new FileDialog(father);
      diag.setVisible(true);
      m_Img =
          getToolkit()
              .getImage(diag.getDirectory() + diag.getFile())
              .getScaledInstance(W, H, Image.SCALE_SMOOTH);
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(m_Img, 0);
      mt.waitForAll();
      PixelGrabber grab = new PixelGrabber(m_Img, 0, 0, W, H, m_Pix, 0, W);
      grab.grabPixels();
      m_ImgSrc = new MemoryImageSource(W, H, m_Pix, 0, W);
      m_Img = createImage(m_ImgSrc);
      System.out.println("Wait HERE !");
    } catch (InterruptedException e) {

    }
  }
Пример #8
0
  public void init() {
    MediaTracker mt = new MediaTracker(this);

    URL url = getClass().getResource("tiger.gif");

    try {
      image = createImage((ImageProducer) url.getContent());
      mt.addImage(image, 0);
      mt.waitForID(0);
    } catch (Exception e) {
      e.printStackTrace();
    }
    imw = image.getWidth(this);
    imh = image.getWidth(this);
    pixels = new int[imw * imh];

    try {
      PixelGrabber pg = new PixelGrabber(image, 0, 0, imw, imh, pixels, 0, imw);
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    addMouseMotionListener(
        new MouseMotionAdapter() {
          public void mouseMoved(MouseEvent e) {
            int mx = e.getX(), my = e.getY();

            if (mx > 0 && mx < imw && my > 0 && my < imh) {
              int pixel = ((int[]) pixels)[my * imw + mx];

              int red = defaultRGB.getRed(pixel),
                  green = defaultRGB.getGreen(pixel),
                  blue = defaultRGB.getBlue(pixel),
                  alpha = defaultRGB.getAlpha(pixel);

              showStatus("red=" + red + " green=" + green + " blue=" + blue + " alpha=" + alpha);
            }
          }
        });
  }
Пример #9
0
  private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = {0f, .5f, 1f};
    Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);
    g2.dispose();

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
      pg.grabPixels();
    } catch (InterruptedException ex) {
      ex.printStackTrace();
    }
    return pallet;
  }
Пример #10
0
  /** *************** Grab Pixels and Convert to gray scale ************** */
  int[] pixel_grab(Image img, int width, int height) {
    int pixSrc[] = new int[w * h];
    try {
      PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixSrc, 0, w);
      pg.grabPixels();
    } catch (Exception e) {
      System.out.println("Exception at Pixel Grabbing");
    }

    for (int i = 0; i < w * h; i++) //  Convertion of pixle form to normal single Value...
    {
      int a = pixSrc[i];
      int r = (0xff & (a >> 16));
      int g = (0xff & (a >> 8));
      int b = (0xff & a);
      int avg = (int) ((.33 * r + .56 * g + .11 * b)); // Add a constant to each pixel
      pixSrc[i] = avg;
    }

    return (pixSrc);
  }
Пример #11
0
  /**
   * PS see http://astronomy.swin.edu.au/~pbourke/geomformats/postscript/ Java
   * http://show.docjava.com:8086/book/cgij/doc/ip/graphics/SimpleImageFrame.java.html
   */
  public boolean drawImage(
      Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
    try {
      // get data from image
      int[] pixels = new int[width * height];
      PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
      grabber.grabPixels();
      ColorModel model = ColorModel.getRGBdefault();

      // print data to ps
      m_printstream.println("gsave");
      m_printstream.println(
          xTransform(xScale(x)) + " " + (yTransform(yScale(y)) - yScale(height)) + " translate");
      m_printstream.println(xScale(width) + " " + yScale(height) + " scale");
      m_printstream.println(
          width + " " + height + " " + "8" + " [" + width + " 0 0 " + (-height) + " 0 " + height
              + "]");
      m_printstream.println("{<");

      int index;
      for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
          index = i * width + j;
          m_printstream.print(toHex(model.getRed(pixels[index])));
          m_printstream.print(toHex(model.getGreen(pixels[index])));
          m_printstream.print(toHex(model.getBlue(pixels[index])));
        }
        m_printstream.println();
      }

      m_printstream.println(">}");
      m_printstream.println("false 3 colorimage");
      m_printstream.println("grestore");
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
Пример #12
0
  /** Convert standard img to a buffered image. */
  public static BufferedImage convertImage(Image img) {
    int height = img.getHeight(null), width = img.getWidth(null);
    // FloatMatrix fm = new FloatMatrix( height, width ) ;
    PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, false);
    try {
      grabber.grabPixels();
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    Object pixels = grabber.getPixels();
    ColorModel cm = grabber.getColorModel();

    BufferedImage bimg = null;

    // REVISIT  Makes some unwarranted assumptions about the layout of the PixelGrabber data
    // as being either int (ARGB?) or byte (gray scale?)
    if (pixels instanceof int[]) {
      int[] arr = (int[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        int l = arr[j];
        barray[k++] = (byte) (l & 0xFF);
        barray[k++] = (byte) ((l >>> 8) & 0xFF);
        barray[k++] = (byte) ((l >>> 16) & 0xFF);
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else if (pixels instanceof byte[]) { // Assume gray scale model?
      byte[] arr = (byte[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        byte l = arr[j];
        barray[k++] = l;
        barray[k++] = l;
        barray[k++] = l;
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else {
      throw new RuntimeException("Unexpected data.");
    }
    return bimg;
  }
Пример #13
0
  /*
   * This method creates and fills three arrays, Y, Cb, and Cr using the
   * input image.
   */
  private void getYCCArray() {
    int[] values = new int[imageWidth * imageHeight];
    int r;
    int g;
    int b;
    int y;
    int x;

    // In order to minimize the chance that grabPixels will throw an exception
    // it may be necessary to grab some pixels every few scanlines and process
    // those before going for more.  The time expense may be prohibitive.
    // However, for a situation where memory overhead is a concern, this may be
    // the only choice.
    PixelGrabber grabber =
        new PixelGrabber(
            imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0, imageWidth);
    MaxHsampFactor = 1;
    MaxVsampFactor = 1;

    for (y = 0; y < NumberOfComponents; y++) {
      MaxHsampFactor = Math.max(MaxHsampFactor, HsampFactor[y]);
      MaxVsampFactor = Math.max(MaxVsampFactor, VsampFactor[y]);
    }

    for (y = 0; y < NumberOfComponents; y++) {
      compWidth[y] =
          ((((imageWidth % 8) != 0)
                      ? (((int) Math.ceil((double) imageWidth / 8.0)) * 8)
                      : imageWidth)
                  / MaxHsampFactor)
              * HsampFactor[y];

      if (compWidth[y] != ((imageWidth / MaxHsampFactor) * HsampFactor[y])) {
        lastColumnIsDummy[y] = true;
      }

      // results in a multiple of 8 for compWidth
      // this will make the rest of the program fail for the unlikely
      // event that someone tries to compress an 16 x 16 pixel image
      // which would of course be worse than pointless
      BlockWidth[y] = (int) Math.ceil((double) compWidth[y] / 8.0);
      compHeight[y] =
          ((((imageHeight % 8) != 0)
                      ? (((int) Math.ceil((double) imageHeight / 8.0)) * 8)
                      : imageHeight)
                  / MaxVsampFactor)
              * VsampFactor[y];

      if (compHeight[y] != ((imageHeight / MaxVsampFactor) * VsampFactor[y])) {
        lastRowIsDummy[y] = true;
      }

      BlockHeight[y] = (int) Math.ceil((double) compHeight[y] / 8.0);
    }

    try {
      if (grabber.grabPixels() != true) {
        try {
          throw new AWTException("Grabber returned false: " + grabber.status());
        } catch (Exception e) {
          String2.log(MustBe.throwableToString(e));
        }
      }
    } catch (InterruptedException e) {
    }

    ;
    float[][] Y = new float[compHeight[0]][compWidth[0]];
    float[][] Cr1 = new float[compHeight[0]][compWidth[0]];
    float[][] Cb1 = new float[compHeight[0]][compWidth[0]];
    float[][] Cb2 = new float[compHeight[1]][compWidth[1]];
    float[][] Cr2 = new float[compHeight[2]][compWidth[2]];
    int index = 0;

    for (y = 0; y < imageHeight; ++y) {
      for (x = 0; x < imageWidth; ++x) {
        r = ((values[index] >> 16) & 0xff);
        g = ((values[index] >> 8) & 0xff);
        b = (values[index] & 0xff);

        // The following three lines are a more correct color conversion but
        // the current conversion technique is sufficient and results in a higher
        // compression rate.
        //                Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g + 0.114
        // * (float)b ));
        //                Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 * (float)g
        // + 0.5 * (float)b));
        //                Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g -
        // 0.08131 * (float)b));
        Y[y][x] = (float) (((0.299 * (float) r) + (0.587 * (float) g) + (0.114 * (float) b)));
        Cb1[y][x] =
            128 + (float) (((-0.16874 * (float) r) - (0.33126 * (float) g) + (0.5 * (float) b)));
        Cr1[y][x] =
            128 + (float) (((0.5 * (float) r) - (0.41869 * (float) g) - (0.08131 * (float) b)));
        index++;
      }
    }

    // Need a way to set the H and V sample factors before allowing downsampling.
    // For now (04/04/98) downsampling must be hard coded.
    // Until a better downsampler is implemented, this will not be done.
    // Downsampling is currently supported.  The downsampling method here
    // is a simple box filter.
    Components[0] = Y;

    //        Cb2 = DownSample(Cb1, 1);
    Components[1] = Cb1;

    //        Cr2 = DownSample(Cr1, 2);
    Components[2] = Cr1;
  }