Example usage for java.awt.image PixelGrabber status

List of usage examples for java.awt.image PixelGrabber status

Introduction

In this page you can find the example usage for java.awt.image PixelGrabber status.

Prototype

public synchronized int status() 

Source Link

Document

Returns the status of the pixels.

Usage

From source file:MainClass.java

public void init() {
    MediaTracker mt = new MediaTracker(this);
    i = getImage(getDocumentBase(), "ora-icon.gif");
    mt.addImage(i, 0);//  www .  j av a 2  s. co m
    try {
        mt.waitForAll();
        int width = i.getWidth(this);
        int height = i.getHeight(this);
        int pixels[] = new int[width * height];
        PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, pixels, 0, width);
        if (pg.grabPixels() && ((pg.status() & ImageObserver.ALLBITS) != 0)) {
            j = createImage(
                    new MemoryImageSource(width, height, rowFlipPixels(pixels, width, height), 0, width));
            k = createImage(
                    new MemoryImageSource(width, height, colFlipPixels(pixels, width, height), 0, width));
            l = createImage(
                    new MemoryImageSource(height, width, rot90Pixels(pixels, width, height), 0, height));
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:GIFEncoder.java

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * /*w w w  .  j av a 2 s . c  om*/
 * @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);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder.// w  w w  . ja va2 s. co  m
 *
 * @param image  The image to encode. The image must be
 *               completely loaded.
 * @throws AWTException  If memory is exhausted or image contains
 *                       more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    imageWidth_ = (short) image.getWidth(null);
    imageHeight_ = (short) image.getHeight(null);

    int values[] = new int[imageWidth_ * imageHeight_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, imageWidth_, imageHeight_, values, 0, imageWidth_);

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

    byte[][] r = new byte[imageWidth_][imageHeight_];
    byte[][] g = new byte[imageWidth_][imageHeight_];
    byte[][] b = new byte[imageWidth_][imageHeight_];

    int index = 0;

    for (int y = 0; y < imageHeight_; y++) {
        for (int x = 0; x < imageWidth_; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index] >> 0) & 0xFF);
        }
    }

    toIndexColor(r, g, b);
}

From source file:com.greenline.guahao.biz.manager.image.codes.gif.GifEncoder.java

/**
 * Convenience constructor for class <CODE>GIFEncoder</CODE>. The argument
 * will be converted to an indexed color array. <B>This may take some
 * time.</B>//from   w  w  w  .j  a  v  a 2s  .  co m
 * 
 * @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 {
    this.imageWidth = (short) image.getWidth(null);
    this.imageHeight = (short) image.getHeight(null);

    int values[] = new int[this.imageWidth * this.imageHeight];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, this.imageWidth, this.imageHeight, values, 0,
            this.imageWidth);

    try {
        if (grabber.grabPixels() != true) {
            log.error("GifEncoder#GifEncoder Grabber returned false: " + grabber.status());
            throw new AWTException("Grabber returned false: " + grabber.status());
        }
    } // ends try
    catch (InterruptedException ie) {
        log.error("GifEncoder#GifEncoder " + ie.getMessage(), ie);
    }

    byte[][] r = new byte[this.imageWidth][this.imageHeight];
    byte[][] g = new byte[this.imageWidth][this.imageHeight];
    byte[][] b = new byte[this.imageWidth][this.imageHeight];
    int index = 0;

    for (int y = 0; y < this.imageHeight; y++) {
        for (int x = 0; x < this.imageWidth; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
        } // ends for

    } // ends for

    this.toIndexColor(r, g, b);
}

From source file:Jpeg.java

private void getYCCArray() {
    int values[] = new int[imageWidth * imageHeight];
    int r, g, b, y, 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);//from w  w  w. ja  v a2s .c  o  m
    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(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(compWidth[y] / 8.0);
        compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math.ceil(imageHeight / 8.0)) * 8 : imageHeight)
                / MaxVsampFactor) * VsampFactor[y];
        if (compHeight[y] != ((imageHeight / MaxVsampFactor) * VsampFactor[y])) {
            lastRowIsDummy[y] = true;
        }
        BlockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0);
    }
    try {
        if (grabber.grabPixels() != true) {
            try {
                throw new AWTException("Grabber returned false: " + grabber.status());
            } catch (Exception 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 * r + 0.587 * g + 0.114 * b));
            Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));
            Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * 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;
}