/*** FILENAME: ImageFrame.java CLASS: ImageFrame PURPOSE: this class will create a frame and add an ImageCanvas to it. This class has 2 important methods for the purpose of CISC859. getRaster and get2DPelArray. The documentation for these methods is inline with code. AUTHOR: Hanaa Barakat. Email: barakat@cs.queensu.ca ***/ import java.util.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.lang.*; public class ImageFrame extends Frame { ImageCanvas imgCanvas; Image currentImg; int[] pixels; int[][] pixels2d; int w,h; /** * class ImageFrame constructor * **/ public ImageFrame(Image img) { currentImg = img; imgCanvas = new ImageCanvas(img, this, 50, 50); add(imgCanvas); imgCanvas.repaint(); validate(); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); } // end of ImageFrame constructor public Image getCurrentImage() { return currentImg; } /*** Method: getRaster Inputs: an Image object, the width and height, the starting co-ordinates of the topmost-leftmost corner of the image being scanned Returns: a single dimension array of pixel values (32 bit integers) Purpose: to scan an image or a part of an image and return an array of pixel values in the form of 32 bit integers. The integers can be examined for color by looking at the pixel in 8-bit sections. alpha, red, green, blue Here is some code to do thresholding for example. Assume that arraySize is an integer value which is the multiplication of the width of the image, by the height of the image. Assume also that val is the threshold cutoff value. (ie. an integer) for (int i = 0; i < arraySize; i++) { int col = (pixels[i] >> 16) & 0xff; // look at the RGB bits if (col < val) pixels[i] = (Color.black).getRGB(); else pixels[i] = (Color.white).getRGB(); } ***/ public int[] getRaster(Image img, int w, int h, int x, int y) { GifPixelGrabber gpg = new GifPixelGrabber(img,imgCanvas.w, imgCanvas.h,x,y); return pixels; } /*** Method: getPelArray Inputs: none Returns: a one dimensional array of integers Purpose: will return the pixel array garnered from calling the getRaster method above. ***/ public int[] getPelArray() { pixels = getRaster(currentImg, imgCanvas.w, imgCanvas.h, 0,0); return pixels; } /*** Method: get2DPelArray Inputs: none Returns: a 2 dimensional array of integer values Purpose: this method will convert the one-dimensional array obtained from using the getRaster method into a 2 dimensional form. ***/ public int[][] get2DPelArray() { pixels = getRaster(currentImg, imgCanvas.w, imgCanvas.h, 0,0); pixels2d = new int[h][w]; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { pixels2d[y][x] = pixels[y*w+x]; } return pixels2d; } public void redisplay(Image img) { remove(imgCanvas); imgCanvas = new ImageCanvas(img, this, 50, 50); add(imgCanvas); imgCanvas.repaint(); validate(); } }