List of usage examples for java.awt.image WritableRaster getWidth
public final int getWidth()
From source file:GraphicsUtil.java
protected static void divide_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;/* w ww . j a v a 2s. 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 == 0) { for (int b = 0; b < bands; b++) pixels[sp + bandOff[b]] = (byte) 0xFF; } else if (a < 255) { // this does NOT include a == 255 (0xff) ! int aFP = (0x00FF0000 / a); for (int b = 0; b < bands; b++) { int 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())); // 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) { pixels[sp] = 0x00FFFFFF; } else if (a < 255) { int aFP = (0x00FF0000 / a); pixels[sp] = ((a << 24) | (((((pixel & 0xFF0000) >> 16) * aFP) & 0xFF0000)) | (((((pixel & 0x00FF00) >> 8) * aFP) & 0xFF0000) >> 8) | (((((pixel & 0x0000FF)) * aFP) & 0xFF0000) >> 16)); }/*from ww w .ja v a2s . co m*/ 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)) { // this does NOT include a == 255 (0xff) ! pixels[sp] = ((a << 24) | ((((pixel & 0xFF0000) * a) >> 8) & 0xFF0000) | ((((pixel & 0x00FF00) * a) >> 8) & 0x00FF00) | ((((pixel & 0x0000FF) * a) >> 8) & 0x0000FF)); }// w w w .j a va 2s .c o m sp++; } } }
From source file:GraphicsUtil.java
public static void divideAlpha(WritableRaster wr) { if (is_BYTE_COMP_Data(wr.getSampleModel())) divide_BYTE_COMP_Data(wr); else if (is_INT_PACK_Data(wr.getSampleModel(), true)) divide_INT_PACK_Data(wr); else {//from w w w . ja v a 2 s .co m int x0, x1, y0, y1, a, b; float ialpha; int bands = wr.getNumBands(); int[] pixel = null; x0 = wr.getMinX(); x1 = x0 + wr.getWidth(); y0 = wr.getMinY(); y1 = y0 + wr.getHeight(); for (int y = y0; y < y1; y++) for (int x = x0; x < x1; x++) { pixel = wr.getPixel(x, y, pixel); a = pixel[bands - 1]; if ((a > 0) && (a < 255)) { ialpha = 255 / (float) a; for (b = 0; b < bands - 1; b++) pixel[b] = (int) (pixel[b] * ialpha + 0.5f); wr.setPixel(x, y, pixel); } } } }
From source file:tilt.image.Blob.java
public static float setToWhite(WritableRaster wr, Rectangle bounds) { int blackPixels = 0; int yEnd = bounds.height + bounds.y; int xEnd = bounds.width + bounds.x; // set all to white int[] iArray = new int[bounds.width]; int[] dirty = new int[bounds.width]; for (int x = 0; x < bounds.width; x++) iArray[x] = 255;//from www . j av a 2s . c o m for (int y = bounds.y; y < yEnd; y++) { wr.getPixels(bounds.x, y, bounds.width, 1, dirty); for (int i = 0; i < dirty.length; i++) if (dirty[i] == 0) blackPixels++; wr.setPixels(bounds.x, y, bounds.width, 1, iArray); } return (float) blackPixels / (float) (wr.getWidth() * wr.getHeight()); }
From source file:GraphicsUtil.java
public static void multiplyAlpha(WritableRaster wr) { if (is_BYTE_COMP_Data(wr.getSampleModel())) mult_BYTE_COMP_Data(wr);/*w w w .ja v a 2s .co m*/ else if (is_INT_PACK_Data(wr.getSampleModel(), true)) mult_INT_PACK_Data(wr); else { int[] pixel = null; int bands = wr.getNumBands(); float norm = 1f / 255f; int x0, x1, y0, y1, a, b; float alpha; x0 = wr.getMinX(); x1 = x0 + wr.getWidth(); y0 = wr.getMinY(); y1 = y0 + wr.getHeight(); for (int y = y0; y < y1; y++) for (int x = x0; x < x1; x++) { pixel = wr.getPixel(x, y, pixel); a = pixel[bands - 1]; if ((a >= 0) && (a < 255)) { alpha = a * norm; for (b = 0; b < bands - 1; b++) pixel[b] = (int) (pixel[b] * alpha + 0.5f); wr.setPixel(x, y, pixel); } } } }
From source file:CheckAlignmentOpImage.java
public Raster computeTile(int x, int y) { Raster r1 = source1.getTile(x, y); Raster r2 = source2.getTile(x, y); int xBounds = r1.getWidth(); if (r2.getWidth() < xBounds) xBounds = r2.getWidth();//from ww w.j a v a2 s .c o m int yBounds = r1.getHeight(); if (r2.getHeight() < yBounds) yBounds = r2.getHeight(); WritableRaster wr; wr = r1.createCompatibleWritableRaster(xBounds, yBounds); int tmpi; int tmpj; for (int i = 0; i < wr.getWidth(); i++) for (int j = 0; j < wr.getHeight(); j++) { tmpi = i / samplingPeriod; tmpj = j / samplingPeriod; if ((tmpi % 2 == 0) && (tmpj % 2 == 0)) wr.setDataElements(i, j, r2.getDataElements(i, j, null)); else if ((tmpi % 2 != 0) && (tmpj % 2 != 0)) wr.setDataElements(i, j, r2.getDataElements(i, j, null)); else wr.setDataElements(i, j, r1.getDataElements(i, j, null)); } return wr; }
From source file:GraphicsUtil.java
public static void copyData_FALLBACK(Raster src, WritableRaster dst) { // System.out.println("Fallback copyData"); int x0 = dst.getMinX(); if (x0 < src.getMinX()) x0 = src.getMinX();/*from ww w . j a va2 s . com*/ int y0 = dst.getMinY(); if (y0 < src.getMinY()) y0 = src.getMinY(); int x1 = dst.getMinX() + dst.getWidth() - 1; if (x1 > src.getMinX() + src.getWidth() - 1) x1 = src.getMinX() + src.getWidth() - 1; int y1 = dst.getMinY() + dst.getHeight() - 1; if (y1 > src.getMinY() + src.getHeight() - 1) y1 = src.getMinY() + src.getHeight() - 1; int width = x1 - x0 + 1; int[] data = null; for (int y = y0; y <= y1; y++) { data = src.getPixels(x0, y, width, 1, data); dst.setPixels(x0, y, width, 1, data); } }
From source file:GraphicsUtil.java
/** * An internal optimized version of copyData designed to work on * Integer packed data with a SinglePixelPackedSampleModel. Only * the region of overlap between src and dst is copied. * * Calls to this should be preflighted with is_INT_PACK_Data * on both src and dest (requireAlpha can be false). * * @param src The source of the data/*from w w w. ja va2 s .c om*/ * @param dst The destination for the data. */ public static void copyData_INT_PACK(Raster src, WritableRaster dst) { // System.out.println("Fast copyData"); int x0 = dst.getMinX(); if (x0 < src.getMinX()) x0 = src.getMinX(); int y0 = dst.getMinY(); if (y0 < src.getMinY()) y0 = src.getMinY(); int x1 = dst.getMinX() + dst.getWidth() - 1; if (x1 > src.getMinX() + src.getWidth() - 1) x1 = src.getMinX() + src.getWidth() - 1; int y1 = dst.getMinY() + dst.getHeight() - 1; if (y1 > src.getMinY() + src.getHeight() - 1) y1 = src.getMinY() + src.getHeight() - 1; int width = x1 - x0 + 1; int height = y1 - y0 + 1; SinglePixelPackedSampleModel srcSPPSM; srcSPPSM = (SinglePixelPackedSampleModel) src.getSampleModel(); final int srcScanStride = srcSPPSM.getScanlineStride(); DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer(); final int[] srcPixels = srcDB.getBankData()[0]; final int srcBase = (srcDB.getOffset() + srcSPPSM.getOffset(x0 - src.getSampleModelTranslateX(), y0 - src.getSampleModelTranslateY())); SinglePixelPackedSampleModel dstSPPSM; dstSPPSM = (SinglePixelPackedSampleModel) dst.getSampleModel(); final int dstScanStride = dstSPPSM.getScanlineStride(); DataBufferInt dstDB = (DataBufferInt) dst.getDataBuffer(); final int[] dstPixels = dstDB.getBankData()[0]; final int dstBase = (dstDB.getOffset() + dstSPPSM.getOffset(x0 - dst.getSampleModelTranslateX(), y0 - dst.getSampleModelTranslateY())); if ((srcScanStride == dstScanStride) && (srcScanStride == width)) { // System.out.println("VERY Fast copyData"); System.arraycopy(srcPixels, srcBase, dstPixels, dstBase, width * height); } else if (width > 128) { int srcSP = srcBase; int dstSP = dstBase; for (int y = 0; y < height; y++) { System.arraycopy(srcPixels, srcSP, dstPixels, dstSP, width); srcSP += srcScanStride; dstSP += dstScanStride; } } else { for (int y = 0; y < height; y++) { int srcSP = srcBase + y * srcScanStride; int dstSP = dstBase + y * dstScanStride; for (int x = 0; x < width; x++) dstPixels[dstSP++] = srcPixels[srcSP++]; } } }
From source file:tilt.image.Picture.java
/** * Convert from greyscale to twotone (black and white) * Adapted from OCRopus/*from w ww. j a v a 2 s .c o m*/ * Copyright 2006-2008 Deutsches Forschungszentrum fuer Kuenstliche * Intelligenz or its licensors, as applicable. * http://ocropus.googlecode.com/svn/trunk/ocr-binarize/ocr-binarize-sauvola.cc * @throws Exception */ void convertToTwoTone() throws ImageException { try { int MAXVAL = 256; double k = 0.34; if (greyscale == null) convertToGreyscale(); BufferedImage grey = ImageIO.read(greyscale); WritableRaster grey_image = grey.getRaster(); WritableRaster bin_image = grey.copyData(null); int square = (int) Math.floor(grey_image.getWidth() * 0.025); if (square == 0) square = Math.min(20, grey_image.getWidth()); if (square > grey_image.getHeight()) square = grey_image.getHeight(); int whalf = square >> 1; if (whalf == 0) throw new Exception("whalf==0!"); int image_width = grey_image.getWidth(); int image_height = grey_image.getHeight(); // Calculate the integral image, and integral of the squared image // original algorithm ate up too much memory, use floats for longs float[][] integral_image = new float[image_width][image_height]; float[][] rowsum_image = new float[image_width][image_height]; float[][] integral_sqimg = new float[image_width][image_height]; float[][] rowsum_sqimg = new float[image_width][image_height]; int xmin, ymin, xmax, ymax; double diagsum, idiagsum, diff, sqdiagsum, sqidiagsum, sqdiff, area; double mean, std, threshold; // for get/setPixel int[] iArray = new int[1]; int[] oArray = new int[1]; for (int j = 0; j < image_height; j++) { grey_image.getPixel(0, j, iArray); rowsum_image[0][j] = iArray[0]; rowsum_sqimg[0][j] = iArray[0] * iArray[0]; } for (int i = 1; i < image_width; i++) { for (int j = 0; j < image_height; j++) { grey_image.getPixel(i, j, iArray); rowsum_image[i][j] = rowsum_image[i - 1][j] + iArray[0]; rowsum_sqimg[i][j] = rowsum_sqimg[i - 1][j] + iArray[0] * iArray[0]; } } for (int i = 0; i < image_width; i++) { integral_image[i][0] = rowsum_image[i][0]; integral_sqimg[i][0] = rowsum_sqimg[i][0]; } for (int i = 0; i < image_width; i++) { for (int j = 1; j < image_height; j++) { integral_image[i][j] = integral_image[i][j - 1] + rowsum_image[i][j]; integral_sqimg[i][j] = integral_sqimg[i][j - 1] + rowsum_sqimg[i][j]; } } // compute mean and std.dev. using the integral image for (int i = 0; i < image_width; i++) { for (int j = 0; j < image_height; j++) { xmin = Math.max(0, i - whalf); ymin = Math.max(0, j - whalf); xmax = Math.min(image_width - 1, i + whalf); ymax = Math.min(image_height - 1, j + whalf); area = (xmax - xmin + 1) * (ymax - ymin + 1); grey_image.getPixel(i, j, iArray); // area can't be 0 here if (area == 0) throw new Exception("area can't be 0 here!"); if (xmin == 0 && ymin == 0) { // Point at origin diff = integral_image[xmax][ymax]; sqdiff = integral_sqimg[xmax][ymax]; } else if (xmin == 0 && ymin != 0) { // first column diff = integral_image[xmax][ymax] - integral_image[xmax][ymin - 1]; sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmax][ymin - 1]; } else if (xmin != 0 && ymin == 0) { // first row diff = integral_image[xmax][ymax] - integral_image[xmin - 1][ymax]; sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmin - 1][ymax]; } else { // rest of the image diagsum = integral_image[xmax][ymax] + integral_image[xmin - 1][ymin - 1]; idiagsum = integral_image[xmax][ymin - 1] + integral_image[xmin - 1][ymax]; diff = diagsum - idiagsum; sqdiagsum = integral_sqimg[xmax][ymax] + integral_sqimg[xmin - 1][ymin - 1]; sqidiagsum = integral_sqimg[xmax][ymin - 1] + integral_sqimg[xmin - 1][ymax]; sqdiff = sqdiagsum - sqidiagsum; } mean = diff / area; std = Math.sqrt((sqdiff - diff * diff / area) / (area - 1)); threshold = mean * (1 + k * ((std / 128) - 1)); if (iArray[0] < threshold) oArray[0] = 0; else oArray[0] = MAXVAL - 1; bin_image.setPixel(i, j, oArray); } } twotone = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX); grey.setData(bin_image); ImageIO.write(grey, "png", twotone); } catch (Exception e) { throw new ImageException(e); } }