Example usage for java.awt.image PixelGrabber grabPixels

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

Introduction

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

Prototype

public boolean grabPixels() throws InterruptedException 

Source Link

Document

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered.

Usage

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>//  w w  w  .j a  va2 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 {
    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:org.pentaho.reporting.libraries.base.util.PngEncoder.java

/**
 * Write the image data into the pngBytes array. This will write one or more PNG "IDAT" chunks. In order to conserve
 * memory, this method grabs as many rows as will fit into 32K bytes, or the whole image; whichever is less.
 *
 * @return true if no errors; false if error grabbing pixels
 *///from  ww w .  j a v  a2s .co m
protected boolean writeImageData() {

    this.bytesPerPixel = (this.encodeAlpha) ? 4 : 3;

    final Deflater scrunch = new Deflater(this.compressionLevel);
    final ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);
    final DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, scrunch);
    try {
        int startRow = 0; // starting row to process this time through
        //noinspection SuspiciousNameCombination
        int rowsLeft = this.height; // number of rows remaining to write
        while (rowsLeft > 0) {
            final int nRows = Math.max(Math.min(32767 / (this.width * (this.bytesPerPixel + 1)), rowsLeft), 1);

            final int[] pixels = new int[this.width * nRows];

            final PixelGrabber pg = new PixelGrabber(this.image, 0, startRow, this.width, nRows, pixels, 0,
                    this.width);
            try {
                pg.grabPixels();
            } catch (Exception e) {
                logger.error("interrupted waiting for pixels!", e);
                return false;
            }
            if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                logger.error("image fetch aborted or errored");
                return false;
            }

            /*
            * Create a data chunk. scanLines adds "nRows" for
            * the filter bytes.
            */
            final byte[] scanLines = new byte[this.width * nRows * this.bytesPerPixel + nRows];

            if (this.filter == PngEncoder.FILTER_SUB) {
                this.leftBytes = new byte[16];
            }
            if (this.filter == PngEncoder.FILTER_UP) {
                this.priorRow = new byte[this.width * this.bytesPerPixel];
            }

            int scanPos = 0;
            int startPos = 1;
            for (int i = 0; i < this.width * nRows; i++) {
                if (i % this.width == 0) {
                    scanLines[scanPos++] = (byte) this.filter;
                    startPos = scanPos;
                }
                scanLines[scanPos++] = (byte) ((pixels[i] >> 16) & 0xff);
                scanLines[scanPos++] = (byte) ((pixels[i] >> 8) & 0xff);
                scanLines[scanPos++] = (byte) ((pixels[i]) & 0xff);
                if (this.encodeAlpha) {
                    scanLines[scanPos++] = (byte) ((pixels[i] >> 24) & 0xff);
                }
                if ((i % this.width == this.width - 1) && (this.filter != PngEncoder.FILTER_NONE)) {
                    if (this.filter == PngEncoder.FILTER_SUB) {
                        filterSub(scanLines, startPos, this.width);
                    }
                    if (this.filter == PngEncoder.FILTER_UP) {
                        filterUp(scanLines, startPos, this.width);
                    }
                }
            }

            /*
            * Write these lines to the output area
            */
            compBytes.write(scanLines, 0, scanPos);

            startRow += nRows;
            rowsLeft -= nRows;
        }
        compBytes.close();

        /*
        * Write the compressed bytes
        */
        final byte[] compressedLines = outBytes.toByteArray();
        final int nCompressed = compressedLines.length;

        this.crc.reset();
        this.bytePos = writeInt4(nCompressed, this.bytePos);
        this.bytePos = writeBytes(PngEncoder.IDAT, this.bytePos);
        this.crc.update(PngEncoder.IDAT);
        this.bytePos = writeBytes(compressedLines, nCompressed, this.bytePos);
        this.crc.update(compressedLines, 0, nCompressed);

        this.crcValue = this.crc.getValue();
        this.bytePos = writeInt4((int) this.crcValue, this.bytePos);
        return true;
    } catch (IOException e) {
        logger.error("Failed to write PNG Data", e);
        return false;
    } finally {
        scrunch.finish();
        scrunch.end();
    }
}

From source file:lucee.runtime.img.Image.java

/**
 * This method returns true if the specified image has transparent pixels
 * @param image// ww w .  j a  v  a2s.c  o  m
 * @return
 */
public static boolean hasAlpha(java.awt.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();
}

From source file:com.jcraft.weirdx.XPixmap.java

static void reqGetImage(Client c) throws IOException {
    int n, foo, format;
    InputOutput io = c.client;/*from   ww w. java2  s.c om*/

    format = c.data;
    foo = io.readInt();
    XDrawable d = c.lookupDrawable(foo);
    c.length -= 2;
    if (d == null) {
        c.errorValue = foo;
        c.errorReason = 9; // BadDrawable;
        return;
    }

    int x, y, width, height;
    x = (short) io.readShort();
    y = (short) io.readShort();
    width = (short) io.readShort();
    height = (short) io.readShort();
    foo = io.readInt();
    c.length = 0;
    Image img = null;
    XColormap colormap = d.getColormap();
    img = d.getImage(null, x, y, width, height);

    //
    //if(d instanceof Window &&
    //   ((Window)d)==((Window)d).screen.root){
    //  Window tmp=(Window)d;
    //  img=RepaintManager.currentManager(tmp.ddxwindow).
    //   getOffscreenBuffer(tmp.ddxwindow, tmp.width, tmp.height);
    //}
    //else{ img=d.getImage(null, x, y, width, height); }

    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(img, x, y, width, height, pixels, 0, width);

    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        LOG.error("interrupted waiting for pixels!");
        for (int i = 0; i < pixels.length; i++)
            pixels[i] = 0;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        LOG.error("image fetch aborted or errored");
        for (int i = 0; i < pixels.length; i++)
            pixels[i] = 0;
    }

    if (d instanceof XWindow) {
        if (((XWindow) d) != ((XWindow) d).screen.root && img != ((XWindow) d).getImage()) {
            img.flush();
        }
    } else {
        if (img != ((XPixmap) d).getImage()) {
            img.flush();
        }
    }

    int i;
    int ww;
    if (d.depth == 1) {
        int www = (width % 32) / 8;
        int wwww = (width % 32) % 8;

        synchronized (io) {
            io.writeByte((byte) 1);
            io.writeByte((byte) 1);
            io.writeShort(c.seq);
            io.writeInt(((width + 31) / 32) * height);
            io.writeInt(0);
            io.writePad(20);
            i = 0;
            if (format == 1) {
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (32 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                foo = 0;
                                i += 8;
                                for (int iii = 0; iii < 8; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += 8;
                                io.writeByte((byte) (foo & 0xff));
                            }
                            ww -= 32;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < www; ii++) {
                                foo = 0;
                                i += 8;
                                for (int iii = 0; iii < 8; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += 8;
                                io.writeByte((byte) (foo & 0xff));
                            }
                            if (wwww != 0) {
                                foo = 0;
                                i += wwww;
                                for (int iii = 0; iii < wwww; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += wwww;
                                io.writeByte((byte) (foo));
                                for (int ii = www + 1; ii < 4; ii++) {
                                    io.writeByte((byte) 0);
                                }
                            } else {
                                for (int ii = www; ii < 4; ii++) {
                                    io.writeByte((byte) 0);
                                }
                            }
                        }
                        break;
                    }
                }
            } else {
                // LSB
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (32 < ww) {
                            for (int ii = 0; ii < 32; ii++) {
                                foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                i++;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                            ww -= 32;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                foo = foo << 1 | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                i++;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                            for (int ii = ww; ii < 32; ii++) {
                                foo = (foo << 1) | 0;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            io.flush();
            return;
        }
    } else if (d.depth == 8) {
        if (format == 1) {

            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width + 3) / 4;
                io.writeInt(n * height);
                io.writeInt(0);
                io.writePad(20);

                i = 0;
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (4 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww -= 4;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww = 4 - ww;
                            while (ww != 0) {
                                io.writeByte(0);
                                ww--;
                            }
                        }
                        break;
                    }
                }
                io.flush();
                return;
            }
        } else { // format==2

            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width + 3) / 4;
                io.writeInt(n * height);
                io.writeInt(0);
                io.writePad(20);

                i = 0;
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (4 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww -= 4;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww = 4 - ww;
                            while (ww != 0) {
                                io.writeByte(0);
                                ww--;
                            }
                        }
                        break;
                    }
                }
                io.flush();
                return;
            }
        }
    } else if (d.depth == 16) {
        if (format == 2) {
            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width / 2 + (width % 2)) * 4;
                io.writeInt(n * height / 4);
                io.writeInt(0);
                io.writePad(20);
                i = 0;
                int iii;
                for (int hh = 0; hh < height; hh++) {
                    for (int ii = 0; ii < width; ii++) {
                        iii = pixels[i];
                        iii = ((iii >> 16) & 0xff) / 8 << 11 | ((iii >> 8) & 0xff) / 4 << 5
                                | ((iii) & 0xff) / 8;
                        io.writeByte((iii) & 0xff);
                        io.writeByte((iii >> 8) & 0xff);
                        i++;
                    }
                    if (width % 2 != 0)
                        io.writePad(2);
                }
                io.flush();
                return;
            }
        }
    }

    synchronized (io) {
        io.writeByte((byte) 0); // error!! Implementation
        io.writeByte((byte) 17);
        io.writeShort(c.seq);
        io.writePad(4);
        io.writeShort(0);
        io.writeByte((byte) 73);
        io.writePad(21);
        io.flush();
    }
}

From source file:PngEncoder.java

/**
 * Write the image data into the pngBytes array.
 * This will write one or more PNG "IDAT" chunks. In order
 * to conserve memory, this method grabs as many rows as will
 * fit into 32K bytes, or the whole image; whichever is less.
 *
 *
 * @return true if no errors; false if error grabbing pixels
 *///w ww  .  j  a  v  a 2s  .c o m
protected boolean writeImageData() {
    int rowsLeft = this.height; // number of rows remaining to write
    int startRow = 0; // starting row to process this time through
    int nRows; // how many rows to grab at a time

    byte[] scanLines; // the scan lines to be compressed
    int scanPos; // where we are in the scan lines
    int startPos; // where this line's actual pixels start (used
                  // for filtering)

    byte[] compressedLines; // the resultant compressed lines
    int nCompressed; // how big is the compressed area?

    //int depth;              // color depth ( handle only 8 or 32 )

    PixelGrabber pg;

    this.bytesPerPixel = (this.encodeAlpha) ? 4 : 3;

    Deflater scrunch = new Deflater(this.compressionLevel);
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);

    DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, scrunch);
    try {
        while (rowsLeft > 0) {
            nRows = Math.min(32767 / (this.width * (this.bytesPerPixel + 1)), rowsLeft);
            nRows = Math.max(nRows, 1);

            int[] pixels = new int[this.width * nRows];

            pg = new PixelGrabber(this.image, 0, startRow, this.width, nRows, pixels, 0, this.width);
            try {
                pg.grabPixels();
            } catch (Exception e) {
                System.err.println("interrupted waiting for pixels!");
                return false;
            }
            if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                System.err.println("image fetch aborted or errored");
                return false;
            }

            /*
             * Create a data chunk. scanLines adds "nRows" for
             * the filter bytes.
             */
            scanLines = new byte[this.width * nRows * this.bytesPerPixel + nRows];

            if (this.filter == FILTER_SUB) {
                this.leftBytes = new byte[16];
            }
            if (this.filter == FILTER_UP) {
                this.priorRow = new byte[this.width * this.bytesPerPixel];
            }

            scanPos = 0;
            startPos = 1;
            for (int i = 0; i < this.width * nRows; i++) {
                if (i % this.width == 0) {
                    scanLines[scanPos++] = (byte) this.filter;
                    startPos = scanPos;
                }
                scanLines[scanPos++] = (byte) ((pixels[i] >> 16) & 0xff);
                scanLines[scanPos++] = (byte) ((pixels[i] >> 8) & 0xff);
                scanLines[scanPos++] = (byte) ((pixels[i]) & 0xff);
                if (this.encodeAlpha) {
                    scanLines[scanPos++] = (byte) ((pixels[i] >> 24) & 0xff);
                }
                if ((i % this.width == this.width - 1) && (this.filter != FILTER_NONE)) {
                    if (this.filter == FILTER_SUB) {
                        filterSub(scanLines, startPos, this.width);
                    }
                    if (this.filter == FILTER_UP) {
                        filterUp(scanLines, startPos, this.width);
                    }
                }
            }

            /*
             * Write these lines to the output area
             */
            compBytes.write(scanLines, 0, scanPos);

            startRow += nRows;
            rowsLeft -= nRows;
        }
        compBytes.close();

        /*
         * Write the compressed bytes
         */
        compressedLines = outBytes.toByteArray();
        nCompressed = compressedLines.length;

        this.crc.reset();
        this.bytePos = writeInt4(nCompressed, this.bytePos);
        this.bytePos = writeBytes(IDAT, this.bytePos);
        this.crc.update(IDAT);
        this.bytePos = writeBytes(compressedLines, nCompressed, this.bytePos);
        this.crc.update(compressedLines, 0, nCompressed);

        this.crcValue = this.crc.getValue();
        this.bytePos = writeInt4((int) this.crcValue, this.bytePos);
        scrunch.finish();
        scrunch.end();
        return true;
    } catch (IOException e) {
        System.err.println(e.toString());
        return false;
    }
}

From source file:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Instantiate equalizer spline panel.//ww w .  j a va2s .  c om
 */
public void setSplinePanel() {
    int w = panelSplineLocation[2];
    int h = panelSplineLocation[3];
    splineImage = null;
    splineBarImage = null;
    spline = null;
    if (imFullEqualizer.getHeight(null) > 294) {
        splineImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        splineBarImage = new BufferedImage(w, 1, BufferedImage.TYPE_INT_RGB);
        splineImage.getGraphics().drawImage(imFullEqualizer, 0, 0, w, h, 0, 294, 0 + w, 294 + h, null);
        splineBarImage.getGraphics().drawImage(imFullEqualizer, 0, 0, w, 1, 0, 294 + h + 1, 0 + w,
                294 + h + 1 + 1, null);
        spline = new SplinePanel();
        spline.setBackgroundImage(splineImage);
        spline.setBarImage(splineBarImage);
        int[] pixels = new int[1 * h];
        PixelGrabber pg = new PixelGrabber(imFullEqualizer, 115, 294, 1, h, pixels, 0, 1);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            log.debug(e);
        }
        Color[] colors = new Color[h];
        for (int i = 0; i < h; i++) {
            int c = pixels[i];
            int red = (c & 0x00ff0000) >> 16;
            int green = (c & 0x0000ff00) >> 8;
            int blue = c & 0x000000ff;
            colors[i] = new Color(red, green, blue);
        }
        spline.setGradient(colors);
        spline.setConstraints(new AbsoluteConstraints(panelSplineLocation[0], panelSplineLocation[1],
                panelSplineLocation[2], panelSplineLocation[3]));
    }
}

From source file:org.yccheok.jstock.gui.Utils.java

private 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();
    }/* ww  w  .j a  va 2  s .c om*/

    // 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();
}

From source file:com.jcraft.weirdx.XPixmap.java

void image2data(int x, int y, int w, int h) {
    if (pixels.length < w * h) {
        pixels = new int[w * h];
    }/* www . jav  a  2 s .com*/
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        LOG.error("interrupted waiting for pixels!");
        return;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        LOG.error("image fetch aborted or errored");
        return;
    }
    byte[] dt = getData();
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dt[(y + i) * width + x + j] = (byte) colormap.rgb2pixel(pixels[i * w + j]);
        }
    }
    time = 0;
}

From source file:com.jcraft.weirdx.XPixmap.java

void image2data(int x, int y, int w, int h) {
    if (pixels.length < w * h) {
        pixels = new int[w * h];
    }//from  ww w .  j  av a2  s .  c  om
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        LOG.error("interrupted waiting for pixels!");
        return;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        LOG.error("image fetch aborted or errored");
        return;
    }

    int scanWidth = getScanWidth();

    byte[] dt = getData();
    int foo;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            foo = pixels[i * w + j];
            foo = (((foo >> 16) & 0xff) / 8) << 11 | (((foo >> 8) & 0xff) / 4) << 5 | ((foo & 0xff) / 8);
            dt[(y + i) * scanWidth + x + j * 2] = (byte) ((foo >> 8) & 0xff);
            dt[(y + i) * scanWidth + x + j * 2 + 1] = (byte) (foo & 0xff);
        }
    }

    int iii = scanWidth * (y - 1);
    int iiii = (scanWidth / 2) * (y - 1);
    int jjj;
    for (int ii = y; ii < y + h; ii++) {
        iii += scanWidth;
        iiii += (scanWidth / 2);
        jjj = (x - 1) * 2;
        for (int jj = x; jj < x + w; jj++) {
            jjj += 2;
            foo = ((dt[iii + jjj] << 8) & 0xff00) | (dt[iii + jjj + 1] & 0xff);
            idata[iiii + jj] = 0xff000000 | ((foo >> 11) & 0x1f) * 8 << 16 | ((foo >> 5) & 0x3f) * 4 << 8
                    | (foo & 0x1f) * 8;
        }
    }
    time = 0;
}

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 . j ava  2 s. 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;
}