Example usage for java.awt.image PixelGrabber PixelGrabber

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

Introduction

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

Prototype

public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize) 

Source Link

Document

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the image produced by the specified ImageProducer into the given array.

Usage

From source file:com.t3.image.ImageUtil.java

/**
 * Look at the image and determine which Transparency is most appropriate.
 * If it finds any translucent pixels it returns Transparency.TRANSLUCENT, if 
 * it finds at least one purely transparent pixel and no translucent pixels
 * it will return Transparency.BITMASK, in all other cases it returns 
 * Transparency.OPAQUE, including errors
 * /*from  w ww.j  ava 2  s .c  o m*/
 * @param image
 * @return one of Transparency constants
 */
public static int pickBestTransparency(Image image) {

    // Take a shortcut if possible
    if (image instanceof BufferedImage) {
        return pickBestTransparency((BufferedImage) image);
    }

    // Legacy method
    // NOTE: This is a horrible memory hog
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int[] pixelArray = new int[width * height];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return Transparency.OPAQUE;
    }

    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return Transparency.OPAQUE;
    }

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = pixelArray[y * width + x];
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }

            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }

    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:net.rptools.lib.image.ImageUtil.java

/**
 * Look at the image and determine which Transparency is most appropriate. If it finds any translucent pixels it
 * returns Transparency.TRANSLUCENT, if it finds at least one purely transparent pixel and no translucent pixels it
 * will return Transparency.BITMASK, in all other cases it returns Transparency.OPAQUE, including errors
 * /* ww  w  .j a  v a  2 s  . c  om*/
 * @param image
 * @return one of Transparency constants
 */
public static int pickBestTransparency(Image image) {
    // Take a shortcut if possible
    if (image instanceof BufferedImage) {
        return pickBestTransparency((BufferedImage) image);
    }

    // Legacy method
    // NOTE: This is a horrible memory hog
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int[] pixelArray = new int[width * height];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return Transparency.OPAQUE;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return Transparency.OPAQUE;
    }
    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = pixelArray[y * width + x];
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }
            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }
    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

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 www  .  java2  s  . co m*/
    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

static void reqGetImage(Client c) throws IOException {
    int n, foo, format;
    InputOutput io = c.client;/* w w w.j  av a 2s. c o m*/

    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
 *///from w w w  . 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: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 w  ww .j  av  a 2  s  . c o 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:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Instantiate equalizer spline panel.//w  w  w. ja va2s .  c  o  m
 */
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: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);/*  www  . j a  v  a 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;
}

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 w w w.  j a  va2 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;
}