// Written by Albert Lilly // Alabama School of Mathematics and Science // email: lilly@olympus.asms.state.k12.al.us // For presentation at Supercomputing '96 import java.applet.*; import java.awt.*; import java.awt.image.*; public class Problem1 extends Applet{ static Image img; // The image to be analyzed // Set up storage for 256 color values and the corresponding // number of pixels with the same color value static int[][] NumberofColors = new int[256][2]; static int pointer = 0; // Points to colors static boolean once = true; // Show the results only once static boolean west = true; // Use to color the canvas from the west static boolean east = true; // Use to color the canvas from the east static int high; // High threshold static int low; // Low threshold static int cutoff; // Cutoff point between low and high final static int black = 0; final static int red = (255 << 16); final static int green = (255 << 8); final static int blue = 255; final static int white = red + green + blue; final static int yellow = red + green; // Get the image and threshold values public void init() { String value = this.getParameter("GIF"); img = this.getImage(this.getDocumentBase(),value); value = getParameter("CUTOFF"); cutoff = Integer.parseInt(value); low = cutoff; high = cutoff + 1; } // Set up the screen public void paint(Graphics g) { g = this.getGraphics(); g.drawImage(img,0,0,this); } // Return the width of the image public int analyzeWidth() { return img.getWidth(this); } // Return the height of the image public int analyzeHeight() { return img.getHeight(this); } // Show the results public void update(Graphics g) { if (once) { int width; // The width of the image int height; // The height of the image // Draw the snail shell bitmap paint(g); // Get the width and height of the image width = analyzeWidth(); height = analyzeHeight(); // Analyze the pixels handlepixels(img,0,0,width,height,g); for (int i = 0; i < pointer; i++) { g.setColor(Color.black); g.drawString(Integer.toString((NumberofColors[i][0]*100+50)/ (width*height))+"%",width+60,30+50*i); // Translate the 24-bit color into red, green, and blue components Color temp = new Color( ((NumberofColors[i][1] << 8) >>> 24), ((NumberofColors[i][1] << 16) >>> 24), ((NumberofColors[i][1] << 24) >>> 24)); g.setColor(temp); g.fillRect(width + 100 , 5 + 50 * i, NumberofColors[i][0]/10, 40 ); } // Stop processing once = false; // System.exit(0); } } // Get, sort, and count the pixel values public void handlepixels(Image img2, int x, int y, int w, int h, Graphics g) { int[] pixels = new int[w * h]; // Used to get the pixel values int[] sort = new int[w * h]; // Used for sorting the pixel values int red_west; // Red color component on the west side int green_west; // Green color component on the west side int blue_west; // Blue color component on the west side int brightness_west; // A measure of the illuminosity on the west side int red_east; // Red color component on the east side int green_east; // Green color component on the east side int blue_east; // Blue color component on the east side int brightness_east; // A measure of the illuminosity on the east side // Get the pixel values PixelGrabber pg = new PixelGrabber(img2, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return; } if ((pg.status() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return; } //Redraw the screen for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { Color color_value = new Color ( ((pixels[j * w + i] << 8) >>> 24), ((pixels[j * w + i] << 16) >>> 24), ((pixels[j * w + i] << 24) >>> 24) ); g.setColor(color_value); g.fillRect( i , j, 1, 1); } } // Put the values in an array and attempt to distinguish the // canvas from the damaged areas. // Continue coloring the screen past the halfway point for (int j = 0; j < h; j++) { west = true; east = true; for (int i = 0; i < w; i++) { if (i < w/2) { sort[j * w + i] = pixels[j * w + i]; // Translate the 24-bit color into red, green, and blue components red_west = ((sort[j * w + i] << 8) >>> 24); green_west = ((sort[j * w + i] << 16) >>> 24); blue_west = ((sort[j * w + i] << 24) >>> 24); brightness_west = red_west + green_west + blue_west; { Color color_value = new Color(red_west, green_west, blue_west); g.setColor(color_value); } { if (brightness_west >= high) { Color color_value = new Color( 255, 255, 255); g.setColor(color_value); if(west) sort[j * w + i] = yellow; else sort[j * w + i] = blue; } } { if (brightness_west <= low) { west = false; Color color_value = new Color(0, 0, 0); g.setColor(color_value); sort[j * w + i] = black; } } g.fillRect( i , j+h, 1, 1); if (sort[j * w + i] == yellow) g.setColor(Color.yellow); if (sort[j * w + i] == blue) g.setColor(Color.blue); g.fillRect( i , j+2*h, 1, 1); } else { if (sort[j * w + i] == black) west = false; if (west) { g.setColor(Color.yellow); g.fillRect( i , j+2*h, 1, 1); sort[j * w + i] = yellow; }; }; if (i < w/2) { sort[j * w + (w-1-i)] = pixels[j * w + (w-1-i)]; red_east = ((sort[j * w + (w-1-i)] << 8) >>> 24); green_east = ((sort[j * w + (w-1-i)] << 16) >>> 24); blue_east = ((sort[j * w + (w-1-i)] << 24) >>> 24); brightness_east = red_east + green_east + blue_east; { Color color_value = new Color(red_east, green_east, blue_east); g.setColor(color_value); } { if (brightness_east >= high) { Color color_value = new Color(255, 255, 255); g.setColor(color_value); if(east) sort[j * w + (w-1-i)] = yellow; else sort[j * w + (w-1-i)] = blue; } } { if (brightness_east <= low) { east = false; Color color_value = new Color(0, 0, 0); g.setColor(color_value); sort[j * w + (w-1-i)] = black; } } g.fillRect(w-1-i , j+h, 1, 1); if (sort[j * w + w-1-i] == yellow) g.setColor(Color.yellow); if (sort[j * w + w-1-i] == blue) g.setColor(Color.blue); g.fillRect( w-1-i , j+2*h, 1, 1); } else { if (sort[j * w + w-1-i] == black) east = false; if (east) { g.setColor(Color.yellow); g.fillRect( w-1-i , j+2*h, 1, 1); sort[j * w + (w-1-i)] = yellow; }; }; } } int temp; g.setColor(Color.black); g.drawString("Pixels to process : " ,w+100,165); g.drawString("" + (w*h),w+220,165); g.drawString("Processing pixel #: ",w+100,185); // Sort the values for (int j = 0; j < (w * h); j++) { g.setColor(Color.white); g.drawString(Integer.toString(j),w+220,185); g.setColor(Color.black); g.drawString(Integer.toString(j+1),w+220,185); for (int i = (w * h - 1); i > j; i--) { if(sort[i - 1] < sort[i]) { temp = sort[i - 1]; sort[i - 1] = sort[i]; sort[i] = temp; } } } // Count the sorted pixel values int count = 0; // Used to count the pixels with a given color value for (int i = 1; i < (w * h); i++) { if(sort[i - 1] == sort[i]) count = count + 1; else { count = count + 1; NumberofColors[pointer][0] = count; NumberofColors[pointer][1] = sort[i - 1]; pointer = pointer + 1; count = 0; } } count = count + 1; NumberofColors[pointer][0] = count; NumberofColors[pointer][1] = sort[w * h - 1]; pointer = pointer + 1; } }