Here you can find the source of setTranformImagePixels(BufferedImage origImg, Color paramColor, int colorRangePerc, Vector
public static void setTranformImagePixels(BufferedImage origImg, Color paramColor, int colorRangePerc, Vector<Integer> transPixel, Shape cutoutShape)
//package com.java2s; //License from project: Apache License import java.awt.Color; import java.awt.Rectangle; import java.awt.Shape; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; import java.util.Vector; public class Main { static int ALPHA = 3; static int RED = 0; static int GREEN = 1; static int BLUE = 2; public static void setTranformImagePixels(BufferedImage origImg, Color paramColor, int colorRangePerc, Vector<Integer> transPixel, Shape cutoutShape) { if (transPixel == null) throw new NullPointerException("Transformation Pizel is Null"); int maxCols = origImg.getWidth(), maxRows = origImg.getHeight(); GeneralPath cutoutPath = null; int startx = 0, starty = 0; if (cutoutShape != null) { cutoutPath = new GeneralPath(cutoutShape); Rectangle pathBounds = cutoutPath.getBounds(); Rectangle imgRect = new Rectangle(0, 0, maxCols, maxRows); if (!imgRect.contains(pathBounds)) return; // startx = pathBounds.x; starty = pathBounds.y; // maxCols = pathBounds.width; maxRows = pathBounds.height; }/*from w w w .ja v a2 s .c o m*/ // RGPTLogger.logToFile("stx: "+startx+" sty: "+starty+" maxRow: "+maxRows+" macCol:"+maxCols); double paramColorRange = (double) 255 * ((double) colorRangePerc / (double) 100); int[] argbParamColor = null; if (paramColor != null) argbParamColor = getARGBColor(paramColor.getRGB()); int[][][] pixel_rgb = BufferedImagetoPixel3DRGB(origImg); for (int row = starty; row < maxRows; row++) { for (int col = startx; col < maxCols; col++) { boolean processPt = false; if (cutoutPath == null) processPt = true; else if (cutoutPath.contains(col, row)) processPt = true; if (processPt) { // RGPTLogger.logToFile("ProcessPtx: "+col+" pty "+row); if (argbParamColor != null) { double colorRange = findColorRange(pixel_rgb[row][col], argbParamColor); if (colorRange <= paramColorRange) transPixel.addElement(row * origImg.getWidth() + col); } else if (cutoutPath != null) transPixel.addElement(row * origImg.getWidth() + col); } } // for (int col=startx ; col<maxCols ; col++) } // for (int row=starty ; row< maxRows ; row++) } public static int[] getARGBColor(Color color) { return getARGBColor(color.getRGB()); } public static int[] getARGBColor(int pixel) { int[] argbColor = new int[4]; argbColor[ALPHA] = pixel >> 24 & 0xFF; // Alpha argbColor[RED] = pixel >> 16 & 0xFF; // Red argbColor[GREEN] = pixel >> 8 & 0xFF; // Green argbColor[BLUE] = pixel & 0xFF; // blue return argbColor; } public static int[][][] BufferedImagetoPixel3DRGB(BufferedImage bimg) { // System.out.println("Loaded Image : Image Type " + bimg.getType()); int imgCols = bimg.getWidth(); int imgRows = bimg.getHeight(); // System.out.println("Image Rows " + imgRows + " Cols " + imgCols); int[] pixel = new int[imgCols * imgRows]; pixel = bimg.getRGB(0, 0, imgCols, imgRows, pixel, 0, imgCols); // Create the One Dimensional array of type int to be populated with // pixel data, one int value // per pixel, with four color and alpha bytes per int value. int[][][] pixel_rgb = new int[imgRows][imgCols][4]; for (int row = 0; row < imgRows; row++) { for (int col = 0; col < imgCols; col++) { int element = row * imgCols + col; // Alpha data pixel_rgb[row][col][ALPHA] = (pixel[element] >> 24) & 0xFF; // Red data pixel_rgb[row][col][RED] = (pixel[element] >> 16) & 0xFF; // Green data pixel_rgb[row][col][GREEN] = (pixel[element] >> 8) & 0xFF; // Blue data pixel_rgb[row][col][BLUE] = (pixel[element]) & 0xFF; } } return pixel_rgb; } public static double findColorRange(Color paramColor1, Color paramColor2) { return findColorRange(getARGBColor(paramColor1.getRGB()), getARGBColor(paramColor2.getRGB())); } public static double findColorRange(int paramPixel1, int paramPixel2) { return findColorRange(getARGBColor(paramPixel1), getARGBColor(paramPixel2)); } public static double findColorRange(int[] paramColor1, int[] paramColor2) { int r1 = paramColor1[RED], r2 = paramColor2[RED]; int g1 = paramColor1[GREEN], g2 = paramColor2[GREEN]; int b1 = paramColor1[BLUE], b2 = paramColor2[BLUE]; double distance = Math.sqrt((Math.pow((r1 - r2), 2)) + (Math.pow((g1 - g2), 2)) + (Math.pow((b1 - b2), 2))); return distance; } }