List of usage examples for java.awt.image BufferedImage setRGB
public void setRGB(int x, int y, int rgb)
From source file:com.t3.image.ImageUtil.java
public static BufferedImage createOutline(BufferedImage sourceImage, Color color) { if (sourceImage == null) { return null; }/* ww w . j a v a 2 s.c o m*/ BufferedImage image = new BufferedImage(sourceImage.getWidth() + 2, sourceImage.getHeight() + 2, Transparency.BITMASK); for (int row = 0; row < image.getHeight(); row++) { for (int col = 0; col < image.getWidth(); col++) { int sourceX = col - 1; int sourceY = row - 1; // Pixel under current location if (sourceX >= 0 && sourceY >= 0 && sourceX <= sourceImage.getWidth() - 1 && sourceY <= sourceImage.getHeight() - 1) { int sourcePixel = sourceImage.getRGB(sourceX, sourceY); if (sourcePixel >> 24 != 0) { // Not an empty pixel, don't overwrite it continue; } } for (int i = 0; i < outlineNeighborMap.length; i++) { int[] neighbor = outlineNeighborMap[i]; int x = sourceX + neighbor[0]; int y = sourceY + neighbor[1]; if (x >= 0 && y >= 0 && x <= sourceImage.getWidth() - 1 && y <= sourceImage.getHeight() - 1) { if ((sourceImage.getRGB(x, y) >> 24) != 0) { image.setRGB(col, row, color.getRGB()); break; } } } } } return image; }
From source file:ImageProcessing.ImageProcessing.java
public static void processConvolve(BufferedImage image, double[] filter) { //Applies convolution operation using passed filter to the image. //Initialize values int alphaValue, filteredRed, filteredGreen, filteredBlue; int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double[] temp_alpha = extractAlphaValue(image); double[] temp_red = extractRedColor(image); double[] temp_green = extractGreenColor(image); double[] temp_blue = extractBlueColor(image); //For every pixels (except top/bottom row & borderline left/right row, //Apply filter. for (int i = 1; i < imageHeight - 1; i++) { for (int j = 1; j < imageWidth - 1; j++) { alphaValue = (int) temp_alpha[(i * imageWidth) + j]; //Apply filter to every color component (RGB) filteredRed = processFiltering(j, i, imageWidth, imageHeight, temp_red, filter); filteredGreen = processFiltering(j, i, imageWidth, imageHeight, temp_green, filter); filteredBlue = processFiltering(j, i, imageWidth, imageHeight, temp_blue, filter); //Copy the processed color values to a RGB integer using bitwise operator. int filteredRGB = (alphaValue << 24) + (filteredRed << 16) + (filteredGreen << 8) + filteredBlue; //Set the RGB back to the exact same pixel position. image.setRGB(j, i, filteredRGB); }/*from w w w. java2 s . c o m*/ } }
From source file:org.uva.itast.blended.omr.scanners.SolidSquareMarkScanner.java
@Override protected double fillTemplate(BufferedImage templateimg, int width, int height) { double centerX = templateimg.getWidth() / 2; double centerY = templateimg.getHeight() / 2; int minX = (int) (centerX - width / 2); int maxX = (int) (centerX + width / 2); int minY = (int) (centerY - height / 2); int maxY = (int) (centerY + height / 2); int whites = 0; for (int i = 0; i < templateimg.getWidth(); i++) { for (int j = 0; j < templateimg.getHeight(); j++) { if (i > minX && i < maxX && j > minY && j < maxY) { templateimg.setRGB(i, j, Color.BLACK.getRGB()); } else { templateimg.setRGB(i, j, Color.WHITE.getRGB()); whites++;/*w w w . j av a 2s .com*/ } } } // compute autoSimilarity return ((double) whites) / (templateimg.getHeight() * templateimg.getWidth()); }
From source file:org.csml.tommo.sugar.heatmap.MeanQualityMatrix.java
public BufferedImage createDensityBufferedImage(ColorPaintScale paintScale) { BufferedImage bi = new BufferedImage(N, N, BufferedImage.TYPE_INT_RGB); // make sure that the counter is initialized getMeanValues();/*from www .ja va 2 s . c om*/ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Color c = paintScale.getPaint(totalValueCounter[i][j]); bi.setRGB(i, N - 1 - j, c.getRGB()); } } return bi; }
From source file:edu.umn.cs.spatialHadoop.nasa.HDFRasterLayer.java
public BufferedImage asImage() { // Calculate the average float[][] avg = new float[getWidth()][getHeight()]; for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { avg[x][y] = (float) sum[x][y] / count[x][y]; }/*from w w w. j a v a 2 s . c o m*/ } if (min >= max) { // Values not set. Autodetect min = Float.MAX_VALUE; max = -Float.MAX_VALUE; for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { if (avg[x][y] < min) min = avg[x][y]; if (avg[x][y] > max) max = avg[x][y]; } } } BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { if (count[x][y] > 0) { Color color = calculateColor(avg[x][y], min, max); image.setRGB(x, y, color.getRGB()); } else { image.setRGB(x, y, 0); } } } return image; }
From source file:net.cloudkit.relaxation.VerifyImage.java
private BufferedImage getWhiteImage(int width, int height) { BufferedImage bi = new BufferedImage(width, height, 12); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { bi.setRGB(x, y, Color.WHITE.getRGB()); }/*from w ww. j av a2 s .c o m*/ } return bi; }
From source file:IcoCodec.java
static private void loadPalettedImage(BinaryInputStream in, IconEntry entry, IconHeader header, BufferedImage image) throws Exception { // System.out.println("Loading image..."); // System.out.println("Loading palette..."); // /* w w w .ja va 2 s . c o m*/ // First, load the palette // int cols = (int) header.ColorsUsed; if (cols == 0) { if (entry.bColorCount != 0) cols = entry.bColorCount; else cols = 1 << header.BitsPerPixel; } int[] redp = new int[cols]; int[] greenp = new int[cols]; int[] bluep = new int[cols]; for (int i = 0; i < cols; i++) { bluep[i] = in.readUByte(); greenp[i] = in.readUByte(); redp[i] = in.readUByte(); in.readUByte(); } // System.out.println("Palette read!"); // // Set the image int xorbytes = (((int) header.Height / 2) * (int) header.Width); int readbytes = 0; for (int y = (int) (header.Height / 2) - 1; y >= 0; y--) { for (int x = 0; x < header.Width; x++) { switch (header.BitsPerPixel) { case 4: { int pix = in.readUByte(); readbytes++; int col1 = (pix >> 4) & 0x0F; int col2 = pix & 0x0F; image.setRGB(x, y, (0xFF << 24) | (redp[col1] << 16) | (greenp[col1] << 8) | bluep[col1]); image.setRGB(++x, y, (0xFF << 24) | (redp[col2] << 16) | (greenp[col2] << 8) | bluep[col2]); } break; case 8: { int col1 = in.readUByte(); readbytes++; image.setRGB(x, y, (0xFF << 24) | (redp[col1] << 16) | (greenp[col1] << 8) | bluep[col1]); } break; } } } // System.out.println("XOR data read (" + readbytes + " bytes)"); int height = (int) (header.Height / 2); int rowsize = (int) header.Width / 8; if ((rowsize % 4) > 0) { rowsize += 4 - (rowsize % 4); } // System.out.println("rowsize = " + rowsize); int[] andbytes = new int[rowsize * height]; for (int i = 0; i < andbytes.length; i++) andbytes[i] = in.readUByte(); for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < header.Width; x++) { int offset = ((height - (y + 1)) * rowsize) + (x / 8); if ((andbytes[offset] & (1 << (7 - x % 8))) != 0) { image.setRGB(x, y, 0); } } } // for (int i=0; i<andbytes; i++) // { // int pix = in.readUByte(); // readbytes++; // int xb = (i*8) % (int)header.Width; // int yb = ((int)header.Height/2) - (((i*8) / (int)header.Width)+1); // for (int offset=7; offset>=0; offset--) // { // // // // Modify the transparency only if necessary // // // System.out.println("SET AND (" + xb + "," + yb + ")-" + (7-offset)); // if (((1<<offset) & pix)!=0) // { // int argb = image.getRGB(xb+(7-offset), yb); // image.setRGB(xb+(7-offset), yb, argb & 0xFFFFFF); // } // } // } // System.out.println("AND data read (" + readbytes + " bytes total)"); }
From source file:org.csml.tommo.sugar.heatmap.MeanQualityMatrix.java
public BufferedImage createAverageQualityBufferedImage(ColorPaintScale paintScale) { BufferedImage bi = new BufferedImage(N, N, BufferedImage.TYPE_INT_RGB); // make sure that the counter is initialized getAverageQualityMatrix();/*from w ww .ja va 2s. c o m*/ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Color c = paintScale.getPaint(getAverageQualityMatrix()[i][j]); bi.setRGB(i, N - 1 - j, c.getRGB()); } } return bi; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage rotateImage(BufferedImage img, double angle, int type, Color fillBgColor) { if (img == null) { return null; }// w w w. j av a 2 s. c o m if (angle > 360.0 || angle < -360) { angle = angle % 360.0; } if (angle < 0) { angle = 360 + angle; } if (angle == 0.0 || angle == 360.0) { return img; } //System.out.println("angle="+angle); int w = img.getWidth(); int h = img.getHeight(); /* AffineTransform tr = new AffineTransform(); tr.rotate(theta,w/2,h/2); BufferedImageOp op = new AffineTransformOp(tr, type); BufferedImage out = op.filter(img,null); */ /* AffineTransform tr = new AffineTransform(); tr.rotate(theta, w/2.0, h/2.0); AffineTransform translationTransform = findTranslation(tr, img); tr.preConcatenate(translationTransform); BufferedImageOp op = new AffineTransformOp(tr, type); BufferedImage out = op.filter(img,null); */ BufferedImage out = null; if (angle == 90.0 || angle == 180.0 || angle == 270.0) { switch ((int) angle) { case 90: out = new BufferedImage(h, w, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(h - y - 1, x, img.getRGB(x, y)); } } break; case 180: out = new BufferedImage(w, h, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(w - x - 1, h - y - 1, img.getRGB(x, y)); } } break; case 270: out = new BufferedImage(h, w, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(y, w - x - 1, img.getRGB(x, y)); } } break; } } else { double theta = angle * Math.PI / 180.0; int neww = w, newh = h; double dx = 0.0, dy = 0.0; double s = Math.sin(theta); double c = Math.cos(theta); if (angle > 0.0 && angle < 90.0) { neww = (int) Math.round(((double) w) * c + ((double) h) * s); newh = (int) Math.round(((double) w) * s + ((double) h) * c); dx = ((double) h) * s; dy = 0.0; } else if (angle > 90.0 && angle < 180.0) { neww = (int) Math.round(-((double) w) * c + ((double) h) * s); newh = (int) Math.round(((double) w) * s - ((double) h) * c); dx = -((double) w) * c + ((double) h) * s; dy = -((double) h) * c; } else if (angle > 180.0 && angle < 270.0) { neww = (int) Math.round(-((double) w) * c - ((double) h) * s); newh = (int) Math.round(-((double) w) * s - ((double) h) * c); dx = -((double) w) * c; dy = -((double) w) * s - ((double) h) * c; } else if (angle > 270.0 && angle < 360.0) { neww = (int) Math.round(((double) w) * c - ((double) h) * s); newh = (int) Math.round(-((double) w) * s + ((double) h) * c); dx = 0.0; dy = -((double) w) * s; } AffineTransform tr = new AffineTransform(); tr.translate(dx, dy); tr.rotate(theta); BufferedImageOp op = new AffineTransformOp(tr, type); out = new BufferedImage(neww, newh, img.getType()); Graphics2D g2d = (Graphics2D) out.getGraphics(); Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight()); g2d.setPaint(fillBgColor); g2d.fill(clear); op.filter(img, out); } return out; }
From source file:de.ep3.ftpc.view.designer.UIDesigner.java
/** * Loads an icon image from the resources directory provided. * * The icon will be recolored according to the UI default color * (with respect to its alpha values)./*from w ww. j av a 2s.c o m*/ * * @param dirName The directory name under the resource/drawable directory. * @param fileName The file name under the directory name provided before. * @return The icom ready to be displayed within the GUI. */ public Icon getDefaultIcon(String dirName, String fileName) { BufferedImage image = getDefaultImage(dirName, fileName); /* Change icon color according to UI color */ int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); int defaultRGBA = getDefaultBorderColor().getRGB(); int defaultRGB = defaultRGBA & 0x00FFFFFF; for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { int imageRGBA = image.getRGB(x, y); int imageA = imageRGBA & 0xFF000000; int newRGBA = defaultRGB | imageA; // I'm quite proud of this little bitwise color calculation ^-^ image.setRGB(x, y, newRGBA); } } return new ImageIcon(image); }