// imageIO.java // This class provides routines for image input and output. import java.util.*; import java.awt.*; import java.awt.image.*; import java.awt.MediaTracker.*; import java.io.*; public class imageIO { // ****************************** loadImage ********************************** // The loadImage method asks the user for the name of a file that contains // an image. It reads the contents of this file, returning the // data as a three dimensional array (row, column, colour). Colour // is 0 for red, 1 for green, 2 for blue, 3 for offset. // The file name may be for a local file (such as "Camel.jpg") or it may // be a path (such as "/home/turing/alan.bmp"). // // private static int[][][] loadImage() // Parameters: // none // // Precondition: // none // // Postcondition: // Provided that the user gives a legal file name (the file exists and // ends in .gif, .jpg, .jpeg or .bmp), a 3D array containing the image // data is returned. public static int[][][] loadImage() { Image img = (Image)null; // create a null Image try{ while (img == null) { // Ask for the name of a file that contains an image. System.out.print("What image file do you want to open? (gif, jpeg or bmp) "); String openName = null; openName = imageMain.console.readLine(); // Check that the file name has a legal extension if (openName.endsWith(".gif") || openName.endsWith(".jpg") || openName.endsWith(".jpeg")) { img = Toolkit.getDefaultToolkit().getImage(openName); } else if (openName.endsWith(".bmp")) { img = utils.loadbitmap("./", openName); } else { img=(Image)null; // we can't read the file } if (img != null) { // Make sure the entire image is loaded before continuing Button b = new Button(); // Create a button to use as a paraemter // to the constructor for MediaTracker. MediaTracker tracker = new MediaTracker(b); tracker.addImage(img,0); tracker.waitForID(0); // Create "observer", an object that allows us to // use getWidth and getHeight. iObserver observer = new iObserver(); int width = img.getWidth(observer); int height = img.getHeight(observer); if(width==-1 || height==-1){ // the image has not loaded. img = (Image)null; } } // if img != null // If the image did not load, print an explanatory // message to the user and ask him/her to try again. if (img == null) { System.out.println("Could not read an image from file " +openName); System.out.println("Make sure that you supply the name of an image file, \nand that you include the bmp, gif, jpg or jpeg extension."); } // if (img==null) } // while img==null } // end of "try" // Catch InterruptedException for tracker.waitfor(), and catch // IOException for the console operations. catch(InterruptedException e) { System.out.println(e); System.exit(1); } catch(IOException e){ System.out.println(e); System.exit(1); } // Translate from Image img to a 3D array "imagePixels". // Using this 3D array, imagePixels[r][c][w] gives the value // of row r, column c, colour w. int[][][] imagePixels = getImagePixels(img); return imagePixels; } // end of method loadImage // **************************** getImagePixels **************************** // The getImagePixels method converts an image object into a 3D array // representing pixels (rows, columns, pixel value (red, green, blue, offset)) // // private static int[][][] getImagePixels(Image image) // // Parameters: // img - the image which is to be converted // // Precondition: // image img should be fully loaded // // Postcondition: // a 3D array representing the pixels of image is returned private static int[][][] getImagePixels(Image img) { // Get the raw pixel data iObserver observer = new iObserver(); int width1 = img.getWidth(observer); int height1 = img.getHeight(observer); int[] rawPixels = utils.getPixels(img,width1,height1); // Each pixel is represented by 32 bits. Separate the tH32 bits into // four 8-bit values (red, green, blue, offset). int[][] rgbPixels = new int[rawPixels.length][4]; for(int j=0; j>16)&0xff); rgbPixels[j][1] = ((rawPixels[j]>>8)&0xff); rgbPixels[j][2] = (rawPixels[j]&0xff); rgbPixels[j][3] =((rawPixels[j]>>24)&0xff); } // for j // Arrange the data by rows and columns int[][][] imagePixels = new int[height1][width1][4]; int index=0; for(int row=0; row