ImageOp.java
import java.awt.image.*; import java.awt.color.*; import java.io.*; import javax.imageio.*; public class ImageOp { public static BufferedImage readImage(String filename) { BufferedImage img = null; try { img = ImageIO.read(new File(filename)); } catch (IOException e) { //System.out.println(e); // e.getMessage()); System.out.println(filename + " not found"); System.exit(-1); } return img; } public static BufferedImage rgb2gray(BufferedImage img) { int w1 = img.getWidth(); int h1 = img.getHeight(); // int value[][] = new int[w1][h1]; int type = BufferedImage.TYPE_INT_RGB; BufferedImage gray = new BufferedImage(w1, h1, type);// new image int v, r, g, b; int value[] = new int[w1]; for (int j = 0; j < h1; j++) { img.getRGB(0, j, w1, 1, value, 0, w1); // read and store pixel value for (int i = 0; i < w1; i++) { v = value[i]; r = (v >> 16) & 0xFF; g = (v >> 8) & 0xFF; b = v & 0xFF; v = (r + g + b) / 3; // grey by averaging the pixels value[i] = 0xFF000000 | (v << 16) | (v << 8) | v; } gray.setRGB(0, j, w1, 1, value, 0, w1); } return gray; } public static BufferedImage replicate(BufferedImage img, int mag) { int w1 = img.getWidth(); int h1 = img.getHeight(); int w2 = w1*mag; int h2 = h1*mag; int type = BufferedImage.TYPE_INT_RGB; BufferedImage out = new BufferedImage(w2, h2, type); int [] value = new int[w1]; int [] vout = new int[w2]; int r = 0; for (int j=0; j<h1; j++) { img.getRGB(0, j, w1, 1, value, 0, w1); // read and store pixel value int c = 0; for (int i=0; i<w1; i++) { int v = value[i]; for (int k=0; k<mag; k++) vout[c++] = v; } for (int k=0; k<mag; k++) out.setRGB(0,r++,w2,1,vout,0,w2); } return out; } }
Maintained by John Loomis, updated Sat Mar 10 15:24:38 2018