private static BufferedImage disparity( ImageFloat32 src, BufferedImage dst, int minValue, int maxValue, int invalidColor) { float range = maxValue - minValue; for (int y = 0; y < src.height; y++) { for (int x = 0; x < src.width; x++) { float v = src.unsafe_get(x, y); if (v > range) { dst.setRGB(x, y, invalidColor); } else { int r, b; if (v == 0) { r = b = 0; } else { r = (int) (255 * v / maxValue); b = (int) (255 * (maxValue - v) / maxValue); } dst.setRGB(x, y, r << 16 | b); } } } return dst; }
/** * This function draws a line using the theta and R which is the perpendicular from origin to the * line * * @param image buffered image to draw a line from an equation using theta and R * @param color color chosen by the user to draw his new image with it * @param height the height of the image * @param width the width of the image boundaries of the line * @param x1 * @param y1 * @param x2 * @param y2 */ public void Draw( BufferedImage image, int color, int height, int width, int X1, int Y1, int X2, int Y2) { // Find center of the picture float Ycenter = height / 2; float Xcenter = width / 2; // sin & cos used in the equation which will generate the line double sinTheta = Math.sin(theta); double cosTheta = Math.cos(theta); int houghHeight = (int) (Math.sqrt(2) * Math.max(height, width)) / 2; if (theta < Math.PI * 0.25 || theta > Math.PI * 0.75) { // Draw vertical & semi-vertical lines for (int y = Y1; y <= Y2; y++) { int x = (int) ((((this.R - houghHeight) - ((y - Ycenter) * sinTheta)) / cosTheta) + Xcenter); if (x <= Math.max(X1, X2) && x >= Math.min(X1, X2)) ; image.setRGB(x, y, color); } } else { // Draw horizontal & semi-horizontal lines for (int x = X1; x <= X2; x++) { int y = (int) ((((this.R - houghHeight) - ((x - Xcenter) * cosTheta)) / sinTheta) + Ycenter); if (y <= Math.max(Y1, Y2) && y >= Math.min(Y1, Y2)) image.setRGB(x, y, color); } } }
public BufferedImage filter(BufferedImage i) { BufferedImage result = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < i.getHeight(); y++) { for (int x = 0; x < i.getWidth(); x++) { if (y >= getMinY() && y <= getMaxY() && x >= getMinX() && x <= getMaxX()) { int pixel = i.getRGB(x, y); int redAmount = (pixel >> 16) & 0xff; int greenAmount = (pixel >> 8) & 0xff; int blueAmount = (pixel >> 0) & 0xff; int sum = redAmount + greenAmount + blueAmount; sum = sum / 3; redAmount = sum; greenAmount = sum; blueAmount = sum; int newPixel = (redAmount << 16) | (greenAmount << 8) | blueAmount; result.setRGB(x, y, newPixel); } else result.setRGB(x, y, i.getRGB(x, y)); } } return result; }
/** * Draws contours. Internal and external contours are different user specified colors. * * @param contours List of contours * @param colorExternal RGB color * @param colorInternal RGB color * @param width Image width * @param height Image height * @param out (Optional) storage for output image * @return Rendered contours */ public static BufferedImage renderContours( List<Contour> contours, int colorExternal, int colorInternal, int width, int height, BufferedImage out) { if (out == null) { out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); } else { Graphics2D g2 = out.createGraphics(); g2.setColor(Color.BLACK); g2.fillRect(0, 0, width, height); } for (Contour c : contours) { for (Point2D_I32 p : c.external) { out.setRGB(p.x, p.y, colorExternal); } for (List<Point2D_I32> l : c.internal) { for (Point2D_I32 p : l) { out.setRGB(p.x, p.y, colorInternal); } } } return out; }
// Implementation of http://scale2x.sourceforge.net/algorithm.html public static BufferedImage scale2x(BufferedImage image) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage tmp = new BufferedImage(w * 2, h * 2, 2); for (int x = 0; x < h; ++x) { int x2 = x * 2; for (int y = 0; y < w; ++y) { int y2 = y * 2; int E = image.getRGB(y, x); int D = (x == 0 ? E : image.getRGB(y, x - 1)); int B = (y == 0 ? E : image.getRGB(y - 1, x)); int H = (y >= w - 1 ? E : image.getRGB(y + 1, x)); int F = (x >= h - 1 ? E : image.getRGB(y, x + 1)); int e0, e1, e2, e3; if (B != H && D != F) { e0 = D == B ? D : E; e1 = B == F ? F : E; e2 = D == H ? D : E; e3 = H == F ? F : E; } else { e0 = e1 = e2 = e3 = E; } tmp.setRGB(y2, x2, e0); tmp.setRGB(y2 + 1, x2, e1); tmp.setRGB(y2, x2 + 1, e2); tmp.setRGB(y2 + 1, x2 + 1, e3); } } return tmp; }
/** * Pads this image with zeroes. The image is padded on all sides by the amount n. * * @param n The length to pad on each side. */ public void padWithZeros(int n) { BufferedImage paddedImage; this.padding = n; int paddedWidth = image.getWidth() + (2 * n); int paddedHeight = image.getHeight() + (2 * n); paddedImage = new BufferedImage(paddedWidth, paddedHeight, BufferedImage.TYPE_BYTE_INDEXED); // First set everything to 0 for (int r = 0; r < paddedImage.getHeight(); r++) { for (int c = 0; c < paddedImage.getWidth(); c++) { paddedImage.setRGB(c, r, rgbFromGrayscale(0)); } } // Insert original image into padded image for (int r = 0; r < image.getHeight(); r++) { for (int c = 0; c < image.getWidth(); c++) { paddedImage.setRGB(c + n, r + n, image.getRGB(c, r)); } } // Finally, update the image this.image = paddedImage; }
private static void copyRegion( BufferedImage var0, int var1, int var2, BufferedImage var3, int var4, int var5, int var6, int var7, boolean var8, boolean var9) { int[] var10 = new int[var6 * var7]; var0.getRGB(var1, var2, var6, var7, var10, 0, var6); if (!var8 && !var9) { var3.setRGB(var4, var5, var6, var7, var10, 0, var6); } else { int[] var11 = new int[var6 * var7]; for (int var12 = 0; var12 < var6; ++var12) { for (int var13 = 0; var13 < var7; ++var13) { var11[var6 * var13 + var12] = var10[var6 * (var9 ? var7 - 1 - var13 : var13) + (var8 ? var6 - 1 - var12 : var12)]; } } var3.setRGB(var4, var5, var6, var7, var11, 0, var6); } }
public BufferedImage getGridImage() { if (rendered) return gridImage; if (!loaded) throw new Loading(); gridImage = TexI.mkbuf(cmaps); BufferedImage[] texes = new BufferedImage[256]; Coord c = new Coord(); for (c.y = 0; c.y < cmaps.y; c.y++) { for (c.x = 0; c.x < cmaps.x; c.x++) { int t = gettile(c); BufferedImage tex = tileimg(t, texes); if (tex != null) gridImage.setRGB( c.x, c.y, tex.getRGB( Utils.floormod(c.x, tex.getWidth()), Utils.floormod(c.y, tex.getHeight()))); } } for (c.y = 1; c.y < cmaps.y - 1; c.y++) { for (c.x = 1; c.x < cmaps.x - 1; c.x++) { int t = gettile(c); if ((gettile(c.add(-1, 0)) > t) || (gettile(c.add(1, 0)) > t) || (gettile(c.add(0, -1)) > t) || (gettile(c.add(0, 1)) > t)) gridImage.setRGB(c.x, c.y, Color.BLACK.getRGB()); } } rendered = true; return gridImage; }
/** * 伺か用PNGに変換する.<br> * * @param img 変換元の透過画像(TYPE_INT_ARGB専用) * @param transparentColorKey 透過色キー、nullの場合は自動選択 * @return 伺か用PNG画像(TYPE_INT_RGB, 左上に透過色指定あり) */ public BufferedImage createUkagakaPNG(BufferedImage img, Color transparentColorKey) { if (img == null) { throw new IllegalArgumentException("引数にnullは指定できません。"); } if (img.getType() != BufferedImage.TYPE_INT_ARGB) { throw new IllegalArgumentException("TYPE_INT_ARGB専用です."); } // 透過色に設定するカラーキーの取得 if (transparentColorKey == null) { transparentColorKey = detectTransparentColorKey(img); } int transparencyColor; if (transparentColorKey != null) { transparencyColor = transparentColorKey.getRGB() & 0xffffff; } else { // カラーキーの取得がでなければ、黒に限りなく近い非黒を透過色として代替する. logger.log(Level.INFO, "透過色の選択ができなかったため、0x010101で代用します."); transparencyColor = 0x010101; } int w = img.getWidth(); int h = img.getHeight(); Raster raster = img.getData(); // 完全な透過ピクセルに対して算定した透過色を割り当て、 // 画像の左上に透過色を設定する. int argb[] = new int[4]; BufferedImage outimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { argb = raster.getPixel(x, y, argb); int a = argb[3]; // alpha int c; if (a == 0) { // 完全透過の場合のみ透過色を設定 c = transparencyColor; } else { // それ以外はアルファを無視してRGBのみ int r = argb[0]; int g = argb[1]; int b = argb[2]; c = r << 16 | g << 8 | b; } outimg.setRGB(x, y, c); } // 左上(0,0)に透過とする色を設定 outimg.setRGB(0, 0, transparencyColor); } return outimg; }
/** * イメージを取得する。 * * @param cellSize セルのサイズ(pixel) * @param margin 余白(pixel) */ public BufferedImage createImage(int cellSize, int margin, String fileInputPath) throws IOException { int imageSize = getModuleCount() * cellSize + margin * 2; BufferedImage image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < imageSize; y++) { for (int x = 0; x < imageSize; x++) { if (margin <= x && x < imageSize - margin && margin <= y && y < imageSize - margin) { int col = (x - margin) / cellSize; int row = (y - margin) / cellSize; if (isDark(row, col)) { image.setRGB(x, y, 0x000000); } else { image.setRGB(x, y, 0xffffff); } } else { image.setRGB(x, y, 0xffffff); } } } // ===>>> add LOGO image if (fileInputPath != null) { File fileInput = new File(fileInputPath); if (fileInput.exists()) { // TODO BufferedImage bufferedImage = ImageIO.read(fileInput); if (bufferedImage != null) { ScaleImage scaleImage = new ScaleImage(); int w = imageSize / 5 > bufferedImage.getWidth() ? bufferedImage.getWidth() : imageSize / 5; int h = imageSize / 5 > bufferedImage.getWidth() ? bufferedImage.getWidth() : imageSize / 5; int[] array = new int[w * h]; bufferedImage = scaleImage.imageZoomOut(bufferedImage, w, h); array = bufferedImage.getRGB(0, 0, w, h, array, 0, w); int x = imageSize / 2 - w / 2; int y = imageSize / 2 - h / 2; if (x > 0 && y > 0) { image.setRGB(x, y, w, h, array, 0, w); } } } } // <<<=== return image; }
public static BufferedImage scale2x(BufferedImage bufferedimage) { int j2 = bufferedimage.getWidth(); int k2 = bufferedimage.getHeight(); BufferedImage bufferedimage1 = new BufferedImage(j2 * 2, k2 * 2, 2); for (int l2 = 0; l2 < k2; l2++) { for (int i3 = 0; i3 < j2; i3++) { int i = bufferedimage.getRGB(i3, l2); int j1; if (l2 == 0) { j1 = i; } else { j1 = bufferedimage.getRGB(i3, l2 - 1); } int k1; if (i3 == 0) { k1 = i; } else { k1 = bufferedimage.getRGB(i3 - 1, l2); } int l1; if (i3 >= j2 - 1) { l1 = i; } else { l1 = bufferedimage.getRGB(i3 + 1, l2); } int i2; if (l2 >= k2 - 1) { i2 = i; } else { i2 = bufferedimage.getRGB(i3, l2 + 1); } int j; int k; int l; int i1; if (j1 != i2 && k1 != l1) { j = k1 != j1 ? i : k1; k = j1 != l1 ? i : l1; l = k1 != i2 ? i : k1; i1 = i2 != l1 ? i : l1; } else { j = i; k = i; l = i; i1 = i; } bufferedimage1.setRGB(i3 * 2, l2 * 2, j); bufferedimage1.setRGB(i3 * 2 + 1, l2 * 2, k); bufferedimage1.setRGB(i3 * 2, l2 * 2 + 1, l); bufferedimage1.setRGB(i3 * 2 + 1, l2 * 2 + 1, i1); } } return bufferedimage1; }
/** * @param pixels - Byte Array with Pixels * @param w - Image Width (columns) * @param h - Image Heigth (row) */ PanelImage(byte[] pixels, int w, int h, boolean quad) { if (quad) { bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); bi.setRGB(0, 0, w, h, byteArrayToIntArrayQuad(pixels), 0, w); } else { bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); bi.setRGB(0, 0, w, h, byteArrayToIntArray(pixels, w, h), 0, w); } this.setPreferredSize(new Dimension(w, h)); this.setVisible(true); }
static void drawgay(BufferedImage t, BufferedImage img, Coord c) { Coord sz = imgsz(img); 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); } } } }
static { BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); Graphics2D gi = bi.createGraphics(); gi.setBackground(Color.white); gi.clearRect(0, 0, 10, 10); GeneralPath p1 = new GeneralPath(); p1.moveTo(0, 0); p1.lineTo(5, 10); p1.lineTo(10, 0); p1.closePath(); gi.setColor(Color.lightGray); gi.fill(p1); triangles = new TexturePaint(bi, new Rectangle(0, 0, 10, 10)); bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); gi.setColor(Color.black); gi.fillRect(0, 0, 5, 5); gi.setColor(Color.gray); gi.fillRect(1, 1, 4, 4); blacklines = new TexturePaint(bi, new Rectangle(0, 0, 5, 5)); int w = 30; int h = 30; bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); gi = bi.createGraphics(); Color oc = Color.white; Color ic = Color.lightGray; gi.setPaint(new GradientPaint(0, 0, oc, w * .35f, h * .35f, ic)); gi.fillRect(0, 0, w / 2, h / 2); gi.setPaint(new GradientPaint(w, 0, oc, w * .65f, h * .35f, ic)); gi.fillRect(w / 2, 0, w / 2, h / 2); gi.setPaint(new GradientPaint(0, h, oc, w * .35f, h * .65f, ic)); gi.fillRect(0, h / 2, w / 2, h / 2); gi.setPaint(new GradientPaint(w, h, oc, w * .65f, h * .65f, ic)); gi.fillRect(w / 2, h / 2, w / 2, h / 2); gradient = new TexturePaint(bi, new Rectangle(0, 0, w, h)); bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff); bluedots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); bi = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00); greendots = new TexturePaint(bi, new Rectangle(0, 0, 2, 2)); }
private BufferedImage invertirColores(BufferedImage imagen) { for (int x = 0; x < ancho; x++) { for (int y = 0; y < alto; y++) { int rgb = imagen.getRGB(x, y); if (rgb == -16777216) { imagen.setRGB(x, y, -1); } else { imagen.setRGB(x, y, -16777216); } } } return imagen; }
private BufferedImage createIcon() { BufferedImage iconImage = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); for (int x = 1; x < 15; x++) { for (int y = 1; y < 15; y++) { if ((x == 1) || (x == 14) || (y == 1) || (y == 14)) { iconImage.setRGB(x, y, 0xFF000000); } else { iconImage.setRGB(x, y, 0xFF000000 | colour); } } } return iconImage; }
public BufferedImage createCelshading() { BufferedImage returnImage = createImage(camera, 3); BufferedImage edgeImage = EdgeDetection.sobelOperation(createImage(camera, 1)); for (int i = 0; i < returnImage.getWidth(); i++) { for (int j = 0; j < returnImage.getHeight(); j++) { if (edgeImage.getRGB(i, j) == -16777216) { returnImage.setRGB(i, j, edgeImage.getRGB(i, j)); } else { returnImage.setRGB(i, j, returnImage.getRGB(i, j)); } } } return returnImage; }
public BufferedImage reverseImage(BufferedImage source, boolean x) { BufferedImage result = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB); for (int i = 0; i < source.getWidth(); i++) for (int j = 0; j < source.getHeight(); j++) { int rgb = source.getRGB(i, j); Color col = new Color(rgb); int alpha = getAlpha(rgb); col = new Color(col.getRed(), col.getGreen(), col.getBlue(), alpha); if (x == true) result.setRGB(result.getWidth() - i - 1, j, col.getRGB()); if (x == false) result.setRGB(i, result.getHeight() - j - 1, col.getRGB()); } return result; }
private static void addDebugOutline(BufferedImage var0, int var1, int var2, int var3) { if (debugColor != 0) { int var4; for (var4 = 0; var4 < var2; ++var4) { var0.setRGB(var4 + border, var1 + border, debugColor); var0.setRGB(var4 + border, var1 + var3 + border, debugColor); } for (var4 = 0; var4 < var3; ++var4) { var0.setRGB(border, var1 + var4 + border, debugColor); var0.setRGB(var3 + border, var1 + var4 + border, debugColor); } } }
public BufferedImage drawmap(Coord ul, Coord sz) { BufferedImage[] texes = new BufferedImage[256]; MCache m = ui.sess.glob.map; BufferedImage buf = TexI.mkbuf(sz); Coord c = new Coord(); for (c.y = 0; c.y < sz.y; c.y++) { for (c.x = 0; c.x < sz.x; c.x++) { int t = m.gettile(ul.add(c)); BufferedImage tex = tileimg(t, texes); int rgb = 0; if (tex != null) rgb = tex.getRGB( Utils.floormod(c.x + ul.x, tex.getWidth()), Utils.floormod(c.y + ul.y, tex.getHeight())); buf.setRGB(c.x, c.y, rgb); } } for (c.y = 1; c.y < sz.y - 1; c.y++) { for (c.x = 1; c.x < sz.x - 1; c.x++) { int t = m.gettile(ul.add(c)); Tiler tl = m.tiler(t); if (tl instanceof Ridges.RidgeTile) { if (Ridges.brokenp(m, ul.add(c))) { for (int y = c.y - 1; y <= c.y + 1; y++) { for (int x = c.x - 1; x <= c.x + 1; x++) { Color cc = new Color(buf.getRGB(x, y)); buf.setRGB( x, y, Utils.blendcol(cc, Color.BLACK, ((x == c.x) && (y == c.y)) ? 1 : 0.1).getRGB()); } } } } } } for (c.y = 0; c.y < sz.y; c.y++) { for (c.x = 0; c.x < sz.x; c.x++) { int t = m.gettile(ul.add(c)); if ((m.gettile(ul.add(c).add(-1, 0)) > t) || (m.gettile(ul.add(c).add(1, 0)) > t) || (m.gettile(ul.add(c).add(0, -1)) > t) || (m.gettile(ul.add(c).add(0, 1)) > t)) buf.setRGB(c.x, c.y, Color.BLACK.getRGB()); } } return (buf); }
public static void flipHorizontal(BufferedImage bi) { int xSize = bi.getWidth(); int ySize = bi.getHeight(); // Temp image, to store pixels as we reverse everything BufferedImage newBi = new BufferedImage(xSize, ySize, 3); /** * INSERT TWO LOOPS HERE: - FIRST LOOP MOVES DATA INTO A SECOND, TEMPORARY IMAGE WITH PIXELS * FLIPPED HORIZONTALLY - SECOND LOOP MOVES DATA BACK INTO ORIGINAL IMAGE * * <p>Note: Due to a limitation in Greenfoot, you can get the backing BufferedImage from a * GreenfootImage, but you cannot set or create a GreenfootImage based on a BufferedImage. So, * you have to use a temporary BufferedImage (as declared for you, above) and then copy it, * pixel by pixel, back to the original image. Changes to bi in this method will affect the * GreenfootImage automatically. */ for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = bi.getRGB(xSize - x - 1, y); int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; int newColour = packagePixel(red, green, blue, alpha); newBi.setRGB(x, y, newColour); } } for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = newBi.getRGB(x, y); int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; int newColour = packagePixel(red, green, blue, alpha); bi.setRGB(x, y, newColour); } } }
/** * Alter the color of each pixel in the image. Make the image cooler in color. * * @param bi The BufferedImage on which the color variation is done. */ public static void cooler(BufferedImage bi) { int xSize = bi.getWidth(); int ySize = bi.getHeight(); for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = bi.getRGB(x, y); int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; // Apply changes to the rgb value. if (blue <= 190) { blue += 5; } if (red >= 130) { red--; } if (green >= 130) { green--; } int newColour = packagePixel(red, green, blue, alpha); bi.setRGB(x, y, newColour); } } addImage(bi); }
/** * Alter the color of each pixel in the image. Make the image sepia. * * @param bi The BufferedImage on which the color variation is done. */ public static void sepia(BufferedImage bi) { // Get image size int xSize = bi.getWidth(); int ySize = bi.getHeight(); int sepia = 25; for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = bi.getRGB(x, y); int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; // Equalizes all RGB values, and then uses Sepia depth to increase Red and Green amounts by // specified value to achieve sepia effect int equalizer = (red + green + blue) / 3; red = equalizer + (sepia * 2); blue = equalizer; green = equalizer + sepia; // Restriction for values of RGB so to make sure image does not become over manipulated if (red > 255) red = 255; if (green > 255) green = 255; if (blue > 255) blue = 255; int newColour = packagePixel(red, green, blue, alpha); bi.setRGB(x, y, newColour); } } addImage(bi); }
/** * Alter the color of each pixel in the image. Make the image negatively colored. * * @param bi The BufferedImage on which the color variation is done. */ public static void negative(BufferedImage bi) { int xSize = bi.getWidth(); int ySize = bi.getHeight(); for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = bi.getRGB(x, y); // calling unpack pixel to get individual values for RGB // this allows for manipulation of these values easily int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; red = 255 - red; green = 255 - green; blue = 255 - blue; int newColour = packagePixel(red, green, blue, alpha); bi.setRGB(x, y, newColour); } } addImage(bi); }
/** * Alter the color of each pixel in the image. Make the image greyscaled. * * @param bi The BufferedImage on which the color variation is done. */ public static void greyScale(BufferedImage bi) { int xSize = bi.getWidth(); int ySize = bi.getHeight(); for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { // return R G B and alpha values in integer form int rgb = bi.getRGB(x, y); // unpack to get individual integers int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; int average = (red + green + blue) / 3; // get rgb values in integers for manipulation if (red <= 255) { red = average; } if (green <= 255) { green = average; } if (blue <= 255) { blue = average; } int newColour = packagePixel(red, green, blue, alpha); bi.setRGB(x, y, newColour); } } addImage(bi); }
/** * Mirrors the image horizontally by reassigning specific pixels around the image. * * @param bi The BufferedImage on which the translation is done */ public static void mirrorHorizontally(BufferedImage bi) { // Get image size to be used in loops to assign rgba values int xSize = bi.getWidth(); int ySize = bi.getHeight(); // Temp image BufferedImage newBi = new BufferedImage(xSize, ySize, 3); // mirroring by flipping half an image for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { int rgb = bi.getRGB(xSize - x - 1, y); // retrieving the four rgba values using unpack pixel int[] rgbValues = unpackPixel(rgb); int alpha = rgbValues[0]; int red = rgbValues[1]; int green = rgbValues[2]; int blue = rgbValues[3]; int newColour = packagePixel( red, green, blue, alpha); // packs pixels to be placed into the buffered image that was initially // used. bi.setRGB(x, y, newColour); } } addImage(bi); }
public static BufferedImage blur(BufferedImage srcImage) { int w = srcImage.getWidth(); int h = srcImage.getHeight(); int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w); int[] dst = new int[src.length]; System.out.println("Array size is " + src.length); System.out.println("Threshold is " + sThreshold); int processors = Runtime.getRuntime().availableProcessors(); System.out.println( Integer.toString(processors) + " processor" + (processors != 1 ? "s are " : " is ") + "available"); ForkBlur fb = new ForkBlur(src, 0, src.length, dst); ForkJoinPool pool = new ForkJoinPool(); long startTime = System.currentTimeMillis(); pool.invoke(fb); long endTime = System.currentTimeMillis(); System.out.println("Image blur took " + (endTime - startTime) + " milliseconds."); BufferedImage dstImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); dstImage.setRGB(0, 0, w, h, dst, 0, w); return dstImage; }
@Override public BufferedImage processImage(BufferedImage image) { originalImage = image; width = originalImage.getWidth(); height = originalImage.getHeight(); filteredImage = new BufferedImage(width, height, originalImage.getType()); kernel = createKernel(); int white = 255; int black = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int color = new Color(originalImage.getRGB(i, j)).getRed(); if (color == black) { convolve(i, j); } else { int alpha = new Color(originalImage.getRGB(i, j)).getAlpha(); int rgb = ImageUtilities.colorToRGB(alpha, white, white, white); filteredImage.setRGB(i, j, rgb); } } } return filteredImage; }
public BufferedImage filter(BufferedImage i) { BufferedImage result = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_RGB); // For each pixel in the image . . . for (int y = 0; y < i.getHeight(); y++) for (int x = 0; x < i.getWidth(); x++) { int pixel = i.getRGB(x, y); // Decompose the pixel in the amounts of red, green, and blue. int redAmount = (pixel >> 16) & 0xff; int greenAmount = (pixel >> 8) & 0xff; int blueAmount = pixel & 0xff; // Build a new pixel with the red and blue the same as the original, // but all green components set to the max value of 255. greenAmount = 255; // Compose the new pixel. int newPixel = (redAmount << 16) | (greenAmount << 8) | blueAmount; // Set the pixel of the new image. result.setRGB(x, y, newPixel); } return result; }
/** @param args */ public static void main(String[] args) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File file = chooser.getSelectedFile(); String indir = file.getAbsolutePath(); BufferedImage out = null; try (BufferedReader br = new BufferedReader(new FileReader(file))) { out = new BufferedImage(256, 1, BufferedImage.TYPE_INT_RGB); String sCurrentLine; int pos = 0; while ((sCurrentLine = br.readLine()) != null) { String[] values = sCurrentLine.split(" "); Color ocol = new Color( Integer.valueOf(values[0]), Integer.valueOf(values[1]), Integer.valueOf(values[2])); out.setRGB(pos, 0, ocol.getRGB()); pos++; } File outputimage = new File("C:\\ydkj\\palette", "GENPALETTE.bmp"); ImageIO.write(out, "bmp", outputimage); } catch (IOException e) { e.printStackTrace(); } }