List of usage examples for java.awt.image PixelGrabber getStatus
public synchronized int getStatus()
From source file:Util.java
/** * Converts a java.awt.Image into an array of pixels *///from w w w. j ava2 s . c o m public static int[] convertToPixels(Image img) { int width = img.getWidth(null); int height = img.getHeight(null); int[] pixel = new int[width * height]; PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { throw new IllegalStateException("Error: Interrupted Waiting for Pixels"); } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { throw new IllegalStateException("Error: Image Fetch Aborted"); } return pixel; }
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 * //from w w w . j av a 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: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 ww w.j ava 2s. co 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:com.jcraft.weirdx.TransparentFilter.java
TransparentFilter(int cx, int cy, XPixmap pixmap) { this.cx = cx; this.cy = cy; this.width = pixmap.width; this.height = pixmap.height; pixels = new byte[pixmap.width * pixmap.height]; if (pixmap.data != null) { for (int i = 0; i < pixmap.height; i++) { for (int j = 0; j < pixmap.width; j++) { if (pixmap.data[i * pixmap.width + j] == 0) { pixels[i * pixmap.width + j] = 0; } else { pixels[i * pixmap.width + j] = 1; }//from ww w . j a va 2s .c o m } } } else { int[] ipixels = new int[pixmap.width * pixmap.height]; Image img = pixmap.img; PixelGrabber pg = null; pg = new PixelGrabber(img, 0, 0, pixmap.width, pixmap.height, ipixels, 0, pixmap.width); try { pg.grabPixels(); } catch (InterruptedException e) { //System.err.println("interrupted waiting for pixels!"); return; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { LOG.error("image fetch aborted or errored"); return; } for (int i = 0; i < pixmap.height; i++) { for (int j = 0; j < pixmap.width; j++) { if (ipixels[i * pixmap.width + j] == 0xff000000) { pixels[i * pixmap.width + j] = 0; } else { pixels[i * pixmap.width + j] = 1; } } } ipixels = null; } }
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 . j ava2s . com*/ 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 w w.ja v a2 s . 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: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 ww .j a v a2 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; } 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]; }/* w ww .j a v a 2 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: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 *//* w ww . j a v a2 s .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(); } }