// PixelImages.java // David Kosbie, 15-111/AB, Spring 2007 /* Standard disclaimer: As usual with sample code designed (often in class!) to demonstrate specific issues, the style may not be perfect (it is especially lacking comments and some top-down design, and as we've not yet reviewed classes, it's lacking those, too!), and there may even be a bug or two lurking in the code! Note: you are not responsible for the code in the bottom half of this file (there is a comment clearly indicating where this occurs). This code allows you to create a 2d array of integers representing pixels and then display these pixels on the screen. Each pixel is a 32-bit int, where the top byte is the "alpha" (transparency), and the next 3 bytes are, in order, the red, green, and blue values for that pixel. Each of alpha, 4red, green, and blue, being one unsigned byte, vary from 0 to 255. You will find that these are usually written in hexadecimal (good thing we all know that!). For example, check out the colors on the Wikipedia page on web colors (just scroll around and find text in various colors -- the text is usually the hexadecimal RGB value for that color!). For example, here are a few interesting colors: color R G B (in hex!) Crimson DC 14 3C Chartreuse 7F FF 00 Sienna A0 52 2D The important methods for you are as follows: LOADING A JPEG FILE INTO A PIXEL ARRAY: public static int[][] loadPixels(String filename) SETTING PIXELS: public static void setPixel(int[][] pixels, int row, int col, int alpha, int red, int green, int blue) GETTING PIXELS: public static int getAlpha(int[][] pixels, int row, int col) public static int getRed (int[][] pixels, int row, int col) public static int getGreen(int[][] pixels, int row, int col) public static int getBlue (int[][] pixels, int row, int col) DISPLAYING PIXELS: public static void displayPixels(int[][] pixels) */ import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.image.*; public class PixelImages { // The types of fades that we support public enum FadeType { HORIZONTAL_FADE, VERTICAL_FADE, RADIAL_FADE }; public static void main(String[] args) { makeSamplePicture1(); makeSamplePicture2(); } public static void makeSamplePicture2() { int[][] pixels = loadPixels("sampleImage.jpg"); displayPixels(pixels); // display the original int width = pixels.length, height = pixels[0].length; for (int x=0; x>> 24; } // return just the red of the given pixel public static int getRed(int[][] pixels, int x, int y) { return (pixels[x][y] >>> 16) & 0xFF; } // return just the green of the given pixel public static int getGreen(int[][] pixels, int x, int y) { return (pixels[x][y] >>> 8) & 0xFF; } // return just the blue of the given pixel public static int getBlue(int[][] pixels, int x, int y) { return pixels[x][y] & 0xFF; } /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /// YOU ARE NOT RESPONSIBLE FOR THE CODE BELOW HERE!!!! /// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// public static int[][] loadPixels(String filename) { Image image = getImageFromFile(filename); return getPixelsFromImage(image); } public static Image getImageFromFile(String filename) { // this is quick'n'dirty -- should use MediaTracker! Image image = new ImageIcon(filename).getImage(); int width = image.getWidth(null); int height = image.getHeight(null); if ((width == -1) || (height == -1)) { System.err.printf("Could not load file: %s\n",filename); // just punt for now (should be more graceful, of course!) System.exit(1); } System.out.printf("%s: %dx%d\n",filename,width,height); return image; } public static int[][] getPixelsFromImage(Image image) { int width = image.getWidth(null); int height = image.getHeight(null); int[] pixarray = new int[width*height]; PixelGrabber pg = new PixelGrabber(image,0,0,width,height,pixarray,0,width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("Interrupted waiting for pixels!"); // just punt for now (should be more graceful, of course!) System.exit(1); } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("Image fetch error!"); // ditto System.exit(1); } int[][] pixels = new int[width][height]; for (int y=0; y