List of usage examples for java.awt.image BufferedImage getRGB
public int getRGB(int x, int y)
From source file:org.elfinder.servlets.commands.ResizeCommand.java
/** * Rotation function: this could be better!!! Rotation point by point not so * fast but work well./*from w w w . j a v a2s. c o m*/ * * @param BufferedImage */ private BufferedImage rotateCw(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); BufferedImage newImage = new BufferedImage(height, width, img.getType()); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { newImage.setRGB(height - 1 - j, i, img.getRGB(i, j)); } } return newImage; }
From source file:ImageUtils.java
/** * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can * function on a completely headless system. This especially includes Linux and Unix systems * that do not have the X11 libraries installed, which are required for the AWT subsystem to * operate. The resulting image will be smoothly scaled using bilinear filtering. * /*from ww w. j a v a2 s.c o m*/ * @param source The image to convert * @param w The desired image width * @param h The desired image height * @return The converted image * @param type int */ public static BufferedImage createHeadlessSmoothBufferedImage(BufferedImage source, int type, int width, int height) { if (type == ImageUtils.IMAGE_PNG && hasAlpha(source)) { type = BufferedImage.TYPE_INT_ARGB; } else { type = BufferedImage.TYPE_INT_RGB; } BufferedImage dest = new BufferedImage(width, height, type); int sourcex; int sourcey; double scalex = (double) width / source.getWidth(); double scaley = (double) height / source.getHeight(); int x1; int y1; double xdiff; double ydiff; int rgb; int rgb1; int rgb2; for (int y = 0; y < height; y++) { sourcey = y * source.getHeight() / dest.getHeight(); ydiff = scale(y, scaley) - sourcey; for (int x = 0; x < width; x++) { sourcex = x * source.getWidth() / dest.getWidth(); xdiff = scale(x, scalex) - sourcex; x1 = Math.min(source.getWidth() - 1, sourcex + 1); y1 = Math.min(source.getHeight() - 1, sourcey + 1); rgb1 = getRGBInterpolation(source.getRGB(sourcex, sourcey), source.getRGB(x1, sourcey), xdiff); rgb2 = getRGBInterpolation(source.getRGB(sourcex, y1), source.getRGB(x1, y1), xdiff); rgb = getRGBInterpolation(rgb1, rgb2, ydiff); dest.setRGB(x, y, rgb); } } return dest; }
From source file:MainClass.java
public BufferedImage emboss(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); BufferedImage dst;// w w w . j a v a 2 s . com dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { int upperLeft = 0; int lowerRight = 0; if (i > 0 && j > 0) upperLeft = src.getRGB(j - 1, i - 1); if (i < height - 1 && j < width - 1) lowerRight = src.getRGB(j + 1, i + 1); int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255); int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255); int blueDiff = (lowerRight & 255) - (upperLeft & 255); int diff = redDiff; if (Math.abs(greenDiff) > Math.abs(diff)) diff = greenDiff; if (Math.abs(blueDiff) > Math.abs(diff)) diff = blueDiff; int grayColor = 128 + diff; if (grayColor > 255) grayColor = 255; else if (grayColor < 0) grayColor = 0; int newColor = (grayColor << 16) + (grayColor << 8) + grayColor; dst.setRGB(j, i, newColor); } return dst; }
From source file:gr.iti.mklab.reveal.forensics.util.Util.java
public static float[][][] getResizedImageDifference(BufferedImage image1, BufferedImage image2, int newWidth, int newHeight) { // Calculate the subsampled difference between two buffered images float[][][] outputMap = new float[3][newWidth][newHeight]; float widthModifier = (float) image1.getWidth() / newWidth; float heightModifier = (float) image1.getHeight() / newHeight; Color tmpColor1, tmpColor2;/* w w w .jav a 2s . c o m*/ for (int ii = 0; ii < newHeight; ii++) { for (int jj = 0; jj < newWidth; jj++) { try { tmpColor1 = new Color( image1.getRGB(Math.round(jj * widthModifier), Math.round(ii * heightModifier))); tmpColor2 = new Color( image2.getRGB(Math.round(jj * widthModifier), Math.round(ii * heightModifier))); outputMap[0][jj][ii] = (float) (tmpColor1.getRed() - tmpColor2.getRed()) * (tmpColor1.getRed() - tmpColor2.getRed()); outputMap[1][jj][ii] = (float) (tmpColor1.getGreen() - tmpColor2.getGreen()) * (tmpColor1.getGreen() - tmpColor2.getGreen()); outputMap[2][jj][ii] = (float) (tmpColor1.getBlue() - tmpColor2.getBlue()) * (tmpColor1.getBlue() - tmpColor2.getBlue()); } catch (Exception e) { System.out.println(newHeight + " " + newWidth + " " + image1.getHeight() + " " + image1.getWidth() + " " + ii + " " + jj + " " + Math.round(ii * heightModifier) + " " + Math.round(jj * widthModifier) + " " + heightModifier + " " + widthModifier); e.printStackTrace(); return outputMap; } } } return outputMap; }
From source file:de.ailis.wlandsuite.WebExtract.java
/** * Extracts the sprites./* w w w. j a va 2 s . co m*/ * * @param sourceDirectory * The input directory * @param targetDirectory * The output directory * @throws IOException * When file operation fails. */ private void extractSprites(final File sourceDirectory, final File targetDirectory) throws IOException { // Extract tilesets final File imagesDirectory = new File(targetDirectory, "images"); imagesDirectory.mkdirs(); String filename = "ic0_9.wlf"; log.info("Reading " + filename); final Sprites sprites; InputStream stream = new FileInputStream(new File(sourceDirectory, filename)); try { sprites = Sprites.read(stream); } finally { stream.close(); } filename = "masks.wlf"; log.info("Reading " + filename); final Masks masks; stream = new FileInputStream(new File(sourceDirectory, filename)); try { masks = Masks.read(stream, 10); } finally { stream.close(); } final int scale = this.scaleFilter.getScaleFactor(); final BufferedImage out; final int outType = this.scaleFilter.getImageType(); if (outType == -1) out = new TransparentEgaImage(10 * 16 * scale, 16 * scale); else out = new BufferedImage(10 * 16 * scale, 16 * scale, BufferedImage.TYPE_INT_ARGB); log.info("Writing sprites"); for (int i = 0; i < 10; i++) { final BufferedImage sprite = this.scaleFilter.scale(sprites.getSprites().get(i)); final BufferedImage mask = this.scaleFilter.scale(masks.getMasks().get(i)); for (int x = 0; x < 16 * scale; x++) { for (int y = 0; y < 16 * scale; y++) { if (mask.getRGB(x, y) == Color.BLACK.getRGB()) out.setRGB(x + i * 16 * scale, y, sprite.getRGB(x, y)); } } } ImageIO.write(out, "png", new File(imagesDirectory, "sprites.png")); }
From source file:haven.Utils.java
static void drawgay(BufferedImage t, BufferedImage img, Coord c) { Coord sz = imgsz(img);/* w w w. j ava 2 s. co m*/ for (int y = 0; y < sz.y; y++) { for (int x = 0; x < sz.x; x++) { int p = img.getRGB(x, y); if (Utils.rgbm.getAlpha(p) > 128) { if ((p & 0x00ffffff) == 0x00ff0080) t.setRGB(x + c.x, y + c.y, 0); else t.setRGB(x + c.x, y + c.y, p); } } } }
From source file:com.seleniumtests.it.driver.TestBrowserSnapshot.java
/** * Read the capture file to detect dimension * It assumes that picture background is white and counts the number of white pixels in width and height (first column and first row) * @return//w w w. j av a 2 s. co m * @throws IOException */ private Dimension getViewPortDimension(File picture) throws IOException { BufferedImage image = ImageIO.read(picture); int width = 0; int height = 0; for (width = 0; width < image.getWidth(); width++) { Color color = new Color(image.getRGB(width, 0)); if (!(color.equals(Color.WHITE) || color.equals(Color.YELLOW) || color.equals(Color.ORANGE) || color.equals(Color.GREEN) || color.equals(Color.RED))) { break; } } for (height = 0; height < image.getHeight(); height++) { Color color = new Color(image.getRGB(5, height)); if (!(color.equals(Color.WHITE) || color.equals(Color.YELLOW) || color.equals(Color.ORANGE) || color.equals(Color.GREEN) || color.equals(Color.RED))) { break; } } return new Dimension(width, height); }
From source file:de.ailis.wlandsuite.image.PaletteImage.java
/** * Creates a diff between this picture and the specified one and returns it * in form of a transparent ega image.//from www . j a v a 2 s .c om * * @param image * The other image to create a diff for * @return The diff as a transparent EGA image */ public TransparentEgaImage getDiff(final BufferedImage image) { final int aw = getWidth(); final int ah = getHeight(); final int bw = image.getWidth(); final int bh = image.getHeight(); final TransparentEgaImage diff = new TransparentEgaImage(bw, bh); for (int y = 0; y < bh; y++) { for (int x = 0; x < bw; x++) { if (y >= ah || x >= aw || getRGB(x, y) != image.getRGB(x, y)) diff.setRGB(x, y, image.getRGB(x, y)); } } return diff; }
From source file:org.shaman.rpg.editor.textures.impl.TextureIOTest.java
private void assertImageEquals(BufferedImage expected, BufferedImage actual, double delta) { assertEquals(expected.getWidth(), actual.getWidth()); assertEquals(expected.getHeight(), actual.getHeight()); for (int x = 0; x < expected.getWidth(); ++x) { for (int y = 0; y < expected.getHeight(); ++y) { assertColorEquals(expected.getRGB(x, y), actual.getRGB(x, y), delta); }// ww w . ja v a 2 s .c o m } }
From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java
@Test public void testCreateFromImageINT_ARGB() throws IOException { // workaround Open JDK bug // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044758 if (System.getProperty("java.runtime.name").equals("OpenJDK Runtime Environment") && (System.getProperty("java.specification.version").equals("1.6") || System.getProperty("java.specification.version").equals("1.7") || System.getProperty("java.specification.version").equals("1.8"))) { return;// w w w. j a va2 s . c o m } PDDocument document = new PDDocument(); BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); // create an ARGB image int width = image.getWidth(); int height = image.getHeight(); BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics ag = argbImage.getGraphics(); ag.drawImage(image, 0, 0, null); ag.dispose(); for (int x = 0; x < argbImage.getWidth(); ++x) { for (int y = 0; y < argbImage.getHeight(); ++y) { argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); } } PDImageXObject ximage = JPEGFactory.createFromImage(argbImage); validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); assertNotNull(ximage.getSoftMask()); validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName()); assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10); doWritePDF(document, ximage, testResultsDir, "jpeg-intargb.pdf"); }