List of usage examples for java.awt.image WritableRaster getWidth
public final int getWidth()
From source file:tilt.image.Blob.java
/** * Test with small image/*from w w w .j a v a 2s.c o m*/ * @param args ignored */ public static void main(String[] args) { try { Options opts = new Options(new JSONObject()); String url = "http://ecdosis.net/test.png"; Double[][] cc = { { 0.0, 0.0 }, { 100.0, 0.0 }, { 100.0, 100.0 }, { 0.0, 100.0 } }; Picture p = new Picture(opts, url, new TextIndex("", ""), cc, InetAddress.getByName("127.0.0.1")); p.convertToTwoTone(); BufferedImage bandw = ImageIO.read(p.twotone); WritableRaster wr = bandw.getRaster(); Blob largest = null; int max = 0; int[] iArray = new int[1]; WritableRaster darkRegions = bandw.copyData(null); Blob.setToWhite(darkRegions); for (int y = 0; y < wr.getHeight(); y++) { for (int x = 0; x < wr.getWidth(); x++) { Blob b = new Blob(darkRegions, opts, null); wr.getPixel(x, y, iArray); if (iArray[0] == 0) { b.expandArea(wr, new Point(x, y)); if (b.size() > max) { System.out.println("Found new blob at " + x + "," + y + " size=" + b.size()); largest = b; max = b.size(); } } } } if (largest != null) { WritableRaster dirt = bandw.copyData(null); Blob.setToWhite(dirt); largest.save(dirt, wr, largest.firstBlackPixel); System.out.println(largest.toString()); } } catch (Exception e) { e.printStackTrace(System.out); } }
From source file:tilt.image.Blob.java
/** * Set a raster to white (now only called by Picture) * @param wr the raster to white out/*from ww w . j a va 2s . c om*/ * @return the average number of black pixels per pixel */ public static float setToWhite(WritableRaster wr) { int width = wr.getWidth(); int height = wr.getHeight(); Rectangle r = new Rectangle(0, 0, width, height); return setToWhite(wr, r); }
From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java
/** * Invert pixel color if the image has a adobe marker ... not used now * @param raster//ww w .j a va2s .c o m */ @SuppressWarnings("unused") private static void convertInvertedColors(WritableRaster raster) { int height = raster.getHeight(); int width = raster.getWidth(); int stride = width * 4; int[] pixelRow = new int[stride]; for (int h = 0; h < height; h++) { raster.getPixels(0, h, width, 1, pixelRow); for (int x = 0; x < stride; x++) pixelRow[x] = 255 - pixelRow[x]; raster.setPixels(0, h, width, 1, pixelRow); } }
From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java
/** * Convert image profile from Ycck to Cmyk * @param raster/*ww w . j a v a 2 s . c om*/ */ private static void convertYcckToCmyk(WritableRaster raster) { int height = raster.getHeight(); int width = raster.getWidth(); int stride = width * 4; int[] pixelRow = new int[stride]; for (int h = 0; h < height; h++) { raster.getPixels(0, h, width, 1, pixelRow); for (int x = 0; x < stride; x += 4) { int y = pixelRow[x]; int cb = pixelRow[x + 1]; int cr = pixelRow[x + 2]; int c = (int) (y + 1.402 * cr - 178.956); int m = (int) (y - 0.34414 * cb - 0.71414 * cr + 135.95984); y = (int) (y + 1.772 * cb - 226.316); if (c < 0) c = 0; else if (c > 255) c = 255; if (m < 0) m = 0; else if (m > 255) m = 255; if (y < 0) y = 0; else if (y > 255) y = 255; pixelRow[x] = 255 - c; pixelRow[x + 1] = 255 - m; pixelRow[x + 2] = 255 - y; } raster.setPixels(0, h, width, 1, pixelRow); } }
From source file:org.shaman.terrain.vegetation.ImpositorCreator.java
public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out) { WritableRaster wr = out.getRaster(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); byte[] cpuArray = db.getData(); // copy native memory to java memory bgraBuf.clear();//from w ww . j a v a 2 s .c om bgraBuf.get(cpuArray); bgraBuf.clear(); int width = wr.getWidth(); int height = wr.getHeight(); // flip the components the way AWT likes them // calcuate half of height such that all rows of the array are written to // e.g. for odd heights, write 1 more scanline int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2; for (int y = 0; y < heightdiv2ceil; y++) { for (int x = 0; x < width; x++) { int inPtr = (y * width + x) * 4; int outPtr = ((height - y - 1) * width + x) * 4; byte b1 = cpuArray[inPtr + 0]; byte g1 = cpuArray[inPtr + 1]; byte r1 = cpuArray[inPtr + 2]; byte a1 = cpuArray[inPtr + 3]; byte b2 = cpuArray[outPtr + 0]; byte g2 = cpuArray[outPtr + 1]; byte r2 = cpuArray[outPtr + 2]; byte a2 = cpuArray[outPtr + 3]; cpuArray[outPtr + 0] = a1; cpuArray[outPtr + 1] = r1;//b1; cpuArray[outPtr + 2] = g1; cpuArray[outPtr + 3] = b1;//r1; cpuArray[inPtr + 0] = a2; cpuArray[inPtr + 1] = r2;//b2; cpuArray[inPtr + 2] = g2; cpuArray[inPtr + 3] = b2;//r2; } } }
From source file:GraphicsUtil.java
protected static void mult_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;//from w ww.j av a2s . co m csm = (ComponentSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = csm.getScanlineStride(); final int pixStride = csm.getPixelStride(); final int[] bandOff = csm.getBandOffsets(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int a = 0; int aOff = bandOff[bandOff.length - 1]; int bands = bandOff.length - 1; int b, i; // Access the pixel data array final byte pixels[] = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width * pixStride; while (sp < end) { a = pixels[sp + aOff] & 0xFF; if (a != 0xFF) for (b = 0; b < bands; b++) { i = sp + bandOff[b]; pixels[i] = (byte) (((pixels[i] & 0xFF) * a) >> 8); } sp += pixStride; } } }
From source file:GraphicsUtil.java
protected static void divide_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;//from w w w . j a va 2 s .c o m csm = (ComponentSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = csm.getScanlineStride(); final int pixStride = csm.getPixelStride(); final int[] bandOff = csm.getBandOffsets(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int a = 0; int aOff = bandOff[bandOff.length - 1]; int bands = bandOff.length - 1; int b, i; // Access the pixel data array final byte pixels[] = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width * pixStride; while (sp < end) { a = pixels[sp + aOff] & 0xFF; if (a == 0) { for (b = 0; b < bands; b++) pixels[sp + bandOff[b]] = (byte) 0xFF; } else if (a < 255) { int aFP = (0x00FF0000 / a); for (b = 0; b < bands; b++) { i = sp + bandOff[b]; pixels[i] = (byte) (((pixels[i] & 0xFF) * aFP) >>> 16); } } sp += pixStride; } } }
From source file:GraphicsUtil.java
protected static void divide_INT_PACK_Data(WritableRaster wr) { // System.out.println("Divide Int"); SinglePixelPackedSampleModel sppsm; sppsm = (SinglePixelPackedSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = sppsm.getScanlineStride(); DataBufferInt db = (DataBufferInt) wr.getDataBuffer(); final int base = (db.getOffset() + sppsm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int pixel, a, aFP; // Access the pixel data array final int pixels[] = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width; while (sp < end) { pixel = pixels[sp];//from w ww . j a v a 2s . c o m a = pixel >>> 24; if (a <= 0) { pixels[sp] = 0x00FFFFFF; } else if (a < 255) { aFP = (0x00FF0000 / a); pixels[sp] = ((a << 24) | (((((pixel & 0xFF0000) >> 16) * aFP) & 0xFF0000)) | (((((pixel & 0x00FF00) >> 8) * aFP) & 0xFF0000) >> 8) | (((((pixel & 0x0000FF)) * aFP) & 0xFF0000) >> 16)); } sp++; } } }
From source file:GraphicsUtil.java
protected static void mult_INT_PACK_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); SinglePixelPackedSampleModel sppsm; sppsm = (SinglePixelPackedSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = sppsm.getScanlineStride(); DataBufferInt db = (DataBufferInt) wr.getDataBuffer(); final int base = (db.getOffset() + sppsm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); // Access the pixel data array final int pixels[] = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width; while (sp < end) { int pixel = pixels[sp]; int a = pixel >>> 24; if ((a >= 0) && (a < 255)) { pixels[sp] = ((a << 24) | ((((pixel & 0xFF0000) * a) >> 8) & 0xFF0000) | ((((pixel & 0x00FF00) * a) >> 8) & 0x00FF00) | ((((pixel & 0x0000FF) * a) >> 8) & 0x0000FF)); }//from ww w. j a v a 2 s . co m sp++; } } }
From source file:GraphicsUtil.java
protected static void mult_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;/* ww w. j a v a 2 s .c o m*/ csm = (ComponentSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = csm.getScanlineStride(); final int pixStride = csm.getPixelStride(); final int[] bandOff = csm.getBandOffsets(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int aOff = bandOff[bandOff.length - 1]; int bands = bandOff.length - 1; // Access the pixel data array final byte[] pixels = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width * pixStride; while (sp < end) { int a = pixels[sp + aOff] & 0xFF; if (a != 0xFF) for (int b = 0; b < bands; b++) { int i = sp + bandOff[b]; pixels[i] = (byte) (((pixels[i] & 0xFF) * a) >> 8); } sp += pixStride; } } }