Example usage for java.awt.image ImageObserver ABORT

List of usage examples for java.awt.image ImageObserver ABORT

Introduction

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

Prototype

int ABORT

To view the source code for java.awt.image ImageObserver ABORT.

Click Source Link

Document

This flag in the infoflags argument to imageUpdate indicates that an image which was being tracked asynchronously was aborted before production was complete.

Usage

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

static void reqGetImage(Client c) throws IOException {
    int n, foo, format;
    InputOutput io = c.client;//from  w  ww  . jav  a  2 s  .  co  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 ww .j  a va2  s .c  om
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: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];
    }//w ww  .j  a va2  s. c  o  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;
    }

    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: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  w  w .  j ava2s  . com
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:org.pentaho.reporting.libraries.base.util.WaitingImageObserver.java

/**
 * Callback function used by AWT to inform that more data is available. The observer waits until either all data is
 * loaded or AWT signals that the image cannot be loaded.
 *
 * @param img       the image being observed.
 * @param infoflags the bitwise inclusive OR of the following flags:  <code>WIDTH</code>, <code>HEIGHT</code>,
 *                  <code>PROPERTIES</code>, <code>SOMEBITS</code>, <code>FRAMEBITS</code>, <code>ALLBITS</code>,
 *                  <code>ERROR</code>, <code>ABORT</code>.
 * @param x         the <i>x</i> coordinate.
 * @param y         the <i>y</i> coordinate.
 * @param width     the width./*from   ww  w  .  j a  v  a2 s . co m*/
 * @param height    the height.
 * @return <code>false</code> if the infoflags indicate that the image is completely loaded; <code>true</code>
 * otherwise.
 */
public synchronized boolean imageUpdate(final Image img, final int infoflags, final int x, final int y,
        final int width, final int height) {
    if (img == null) {
        throw new NullPointerException();
    }

    lastUpdate = System.currentTimeMillis();
    if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) {
        this.lock = false;
        this.error = false;
        notifyAll();
        return false;
    } else if ((infoflags & ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS) {
        this.lock = false;
        this.error = false;
        notifyAll();
        return false;
    } else if ((infoflags & ImageObserver.ABORT) == ImageObserver.ABORT
            || (infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) {
        this.lock = false;
        this.error = true;
        notifyAll();
        return false;
    }

    // maybe it is enough already to draw the image ..
    notifyAll();
    return true;
}