List of usage examples for java.awt.image BufferedImage setRGB
public void setRGB(int x, int y, int rgb)
From source file:org.eclipse.swt.snippets.Snippet156.java
static BufferedImage convertToAWT(ImageData data) { ColorModel colorModel = null; PaletteData palette = data.palette;/*www . ja va 2 s . c o m*/ if (palette.isDirect) { colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); RGB rgb = palette.getRGB(pixel); bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue); } } return bufferedImage; } else { RGB[] rgbs = palette.getRGBs(); byte[] red = new byte[rgbs.length]; byte[] green = new byte[rgbs.length]; byte[] blue = new byte[rgbs.length]; for (int i = 0; i < rgbs.length; i++) { RGB rgb = rgbs[i]; red[i] = (byte) rgb.red; green[i] = (byte) rgb.green; blue[i] = (byte) rgb.blue; } if (data.transparentPixel != -1) { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); } else { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); } BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); pixelArray[0] = pixel; raster.setPixel(x, y, pixelArray); } } return bufferedImage; } }
From source file:imageprocessingproject.ImageHistogram.java
public static BufferedImage autoContrastImage(BufferedImage image) { createContrastLUT(image);/* w w w . jav a2s. com*/ int height = image.getHeight(); int width = image.getWidth(); int r, g, b; Color c; BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { c = new Color(image.getRGB(i, j)); r = (int) (255 * contrast_lut[c.getRed()]); g = (int) (255 * contrast_lut[c.getGreen()]); b = (int) (255 * contrast_lut[c.getBlue()]); tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB()); } } return tempImage; }
From source file:com.simiacryptus.mindseye.test.data.CIFAR10.java
private static LabeledObject<BufferedImage> toImage(final byte[] b) { @Nonnull final BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { final int red = 0xFF & b[1 + 1024 * 0 + x + y * 32]; final int blue = 0xFF & b[1 + 1024 * 1 + x + y * 32]; final int green = 0xFF & b[1 + 1024 * 2 + x + y * 32]; final int c = (red << 16) + (blue << 8) + green; img.setRGB(x, y, c); }// w w w .j a v a2 s. co m } return new LabeledObject<>(img, Arrays.toString(new byte[] { b[0] })); }
From source file:ImageProcessing.ImageProcessing.java
public static void copyImage(BufferedImage source_image, BufferedImage target_image) { //Copies pixel data from source image to target image. //The size will not be copied/adjusted, so keep in mind the size of both images. for (int i = 0; i < target_image.getHeight(); i++) { for (int j = 0; j < target_image.getWidth(); j++) { int rgb = source_image.getRGB(j, i); target_image.setRGB(j, i, rgb); }/* ww w . j a v a 2s .c om*/ } }
From source file:com.smash.revolance.ui.model.helper.ImageHelper.java
/** * diff = (img2.rgb(x,y) != img1.rgb(x,y)) ? img1.rgb() : 0 * * @param img1// ww w.ja v a2s .c om * @param img2 * @return * @throws IOException */ public static BufferedImage diffImg(BufferedImage img1, BufferedImage img2) throws IOException { int w = img1.getWidth(); int h = img1.getHeight(); int t = img1.getType(); BufferedImage diff = new BufferedImage(w, h, t); if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) { for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { int refIntensity = img1.getRGB(x, y); int imgIntensity = img2.getRGB(x, y); if (refIntensity != imgIntensity) { diff.setRGB(x, y, imgIntensity); } else { diff.setRGB(x, y, 0); } } } } return diff; }
From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java
/** * Convert a SWT Image to a {@link BufferedImage} * // w ww . ja v a 2s . co m * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"} * * @param data the SWT {@link ImageData} * @param applyAlphaMask true if the image data's alpha mask should be * applied to the result image (if there is any). This method * calls {@link #applyTransparencyMask(BufferedImage, ImageData)} * for that purpose. * @return the AWT {@link BufferedImage} */ public static BufferedImage convertToAWT(ImageData data, boolean applyAlphaMask) { ColorModel colorModel = null; PaletteData palette = data.palette; BufferedImage result; if (palette.isDirect) { colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); RGB rgb = palette.getRGB(pixel); bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue); } } result = bufferedImage; } else { RGB[] rgbs = palette.getRGBs(); byte[] red = new byte[rgbs.length]; byte[] green = new byte[rgbs.length]; byte[] blue = new byte[rgbs.length]; for (int i = 0; i < rgbs.length; i++) { RGB rgb = rgbs[i]; red[i] = (byte) rgb.red; green[i] = (byte) rgb.green; blue[i] = (byte) rgb.blue; } if (data.transparentPixel != -1) { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); } else { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); } BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); pixelArray[0] = pixel; raster.setPixel(x, y, pixelArray); } } result = bufferedImage; } if (data.getTransparencyType() == SWT.TRANSPARENCY_MASK && applyAlphaMask) { result = applyTransparencyMask(result, data.getTransparencyMask()); } return result; }
From source file:org.deeplearning4j.examples.multigpu.video.VideoGenerator.java
private static int[] generateVideo(String path, int nFrames, int width, int height, int numShapes, Random r, boolean backgroundNoise, int numDistractorsPerFrame) throws Exception { //First: decide where transitions between one shape and another are double[] rns = new double[numShapes]; double sum = 0; for (int i = 0; i < numShapes; i++) { rns[i] = r.nextDouble();/*w ww . j a va2 s.com*/ sum += rns[i]; } for (int i = 0; i < numShapes; i++) rns[i] /= sum; int[] startFrames = new int[numShapes]; startFrames[0] = 0; for (int i = 1; i < numShapes; i++) { startFrames[i] = (int) (startFrames[i - 1] + MIN_FRAMES + rns[i] * (nFrames - numShapes * MIN_FRAMES)); } //Randomly generate shape positions, velocities, colors, and type int[] shapeTypes = new int[numShapes]; int[] initialX = new int[numShapes]; int[] initialY = new int[numShapes]; double[] velocityX = new double[numShapes]; double[] velocityY = new double[numShapes]; Color[] color = new Color[numShapes]; for (int i = 0; i < numShapes; i++) { shapeTypes[i] = r.nextInt(NUM_SHAPES); initialX[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE); initialY[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE); velocityX[i] = -1 + 2 * r.nextDouble(); velocityY[i] = -1 + 2 * r.nextDouble(); color[i] = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()); } //Generate a sequence of BufferedImages with the given shapes, and write them to the video SequenceEncoder enc = new SequenceEncoder(new File(path)); int currShape = 0; int[] labels = new int[nFrames]; for (int i = 0; i < nFrames; i++) { if (currShape < numShapes - 1 && i >= startFrames[currShape + 1]) currShape++; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bi.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setBackground(Color.BLACK); if (backgroundNoise) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bi.setRGB(x, y, new Color(r.nextFloat() * MAX_NOISE_VALUE, r.nextFloat() * MAX_NOISE_VALUE, r.nextFloat() * MAX_NOISE_VALUE).getRGB()); } } } g2d.setColor(color[currShape]); //Position of shape this frame int currX = (int) (initialX[currShape] + (i - startFrames[currShape]) * velocityX[currShape] * MAX_VELOCITY); int currY = (int) (initialY[currShape] + (i - startFrames[currShape]) * velocityY[currShape] * MAX_VELOCITY); //Render the shape switch (shapeTypes[currShape]) { case 0: //Circle g2d.fill(new Ellipse2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE)); break; case 1: //Square g2d.fill(new Rectangle2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE)); break; case 2: //Arc g2d.fill(new Arc2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE, 315, 225, Arc2D.PIE)); break; case 3: //Line g2d.setStroke(lineStroke); g2d.draw(new Line2D.Double(currX, currY, currX + SHAPE_SIZE, currY + SHAPE_SIZE)); break; default: throw new RuntimeException(); } //Add some distractor shapes, which are present for one frame only for (int j = 0; j < numDistractorsPerFrame; j++) { int distractorShapeIdx = r.nextInt(NUM_SHAPES); int distractorX = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE); int distractorY = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE); g2d.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat())); switch (distractorShapeIdx) { case 0: g2d.fill(new Ellipse2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE)); break; case 1: g2d.fill(new Rectangle2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE)); break; case 2: g2d.fill(new Arc2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE, 315, 225, Arc2D.PIE)); break; case 3: g2d.setStroke(lineStroke); g2d.draw(new Line2D.Double(distractorX, distractorY, distractorX + SHAPE_SIZE, distractorY + SHAPE_SIZE)); break; default: throw new RuntimeException(); } } enc.encodeImage(bi); g2d.dispose(); labels[i] = shapeTypes[currShape]; } enc.finish(); //write .mp4 return labels; }
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image; int width;/*from w ww. j av a 2 s. c om*/ int height; try { image = ImageIO.read(new ByteArrayInputStream(rgb)); width = image.getWidth(); height = image.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color c = new Color(image.getRGB(j, i)); int red = (int) (c.getRed() * 0.299); int green = (int) (c.getGreen() * 0.587); int blue = (int) (c.getBlue() * 0.114); Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); image.setRGB(j, i, newColor.getRGB()); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.squidy.designer.util.ImageUtils.java
/** * Converts a given image to a grayscale image. * //from w w w . ja va2 s. co m * @param source * @return */ public static BufferedImage convertToGrayscaleImage(BufferedImage source) { BufferedImage grayImage = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < source.getWidth(); x++) for (int y = 0; y < source.getHeight(); y++) { int argb = source.getRGB(x, y); int a = (argb >> 24) & 0xff; int r = (argb >> 16) & 0xff; int g = (argb >> 8) & 0xff; int b = (argb) & 0xff; int l = (int) (.299 * r + .587 * g + .114 * b); // luminance grayImage.setRGB(x, y, (a << 24) + (l << 16) + (l << 8) + l); } return grayImage; }
From source file:it.smartcommunitylab.parking.management.web.manager.MarkerIconStorage.java
private static BufferedImage changeColor(BufferedImage image, Color mask, Color replacement) { BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = destImage.createGraphics(); g.drawImage(image, null, 0, 0);/*from ww w . ja v a2s . c om*/ g.dispose(); for (int i = 0; i < destImage.getWidth(); i++) { for (int j = 0; j < destImage.getHeight(); j++) { int destRGB = destImage.getRGB(i, j); if (matches(mask.getRGB(), destRGB)) { int rgbnew = getNewPixelRGB(replacement.getRGB(), destRGB); destImage.setRGB(i, j, rgbnew); } } } return destImage; }