package MedianAveraging; /** * This class is a custom class used for storing and manipulating grayscale pictures. * The user is enabled to load, change and get information about the specified Image * using the different methods of the class. The class is the main class for visualising * different image processing algorithms in java. * * @author bka * @version "0.1" */ import java.awt.Graphics; import java.awt.*; import java.awt.image.PixelGrabber; import java.awt.image.MemoryImageSource; public class iRPImage extends Canvas{ protected int sizeX; protected int sizeY; protected int image[]; /** * Constructs an iRPImage with size 0,0 and an initializes the * Image[] Array with 0 elements */ public iRPImage(){ sizeX = 0; sizeY = 0; image = new int[0]; } /** * Constructs an iRPImage given width and height of the Image * * @param width the width of the iRPImage * @param height the height of the iRPImage */ public iRPImage(int width, int height){ sizeX = width; sizeY = height; image = new int[sizeX * sizeY]; } public void Clear(int v) { int i; for (i=0;imax) { max = image[i]; position = i; } } return position; } /** * Returns the arrayvalue of the iRPImage on the place (x,y) * * @param x x position of the pixel * @param y y position of the pixel * @return the value of image at position (x,y) */ public int GetPixel(int x, int y){ return image[x + y * sizeX]; } /** * Returns the value of the image[] array on position i * * @param i the specified position * @return value of image[] at position i */ public int GetPixel(int i){ return image[i]; } /** * Sets the value of image[] at position (x,y) to a desired value * * @param x x-position * @param y y-position * @param value the desired value */ public void SetPixel(int x, int y, int value){ image[x + y * sizeX] = value; } /** * Sets the value of image[] at position i to a desired value * * @param i position i * @param value the desired value */ public void SetPixel(int i,int value){ image[i]=value; } /* * Zieht eine Linie von (a1,b1) nach (a2,b2) und setzt den jeweiligen * Pixelwert auf 'value' */ public void drawLineWithConstantValue(int a1,int b1,int a2,int b2,int value){ double a,b,delta; //System.out.println("("+a1+","+b1+") ("+a2+","+b2); if(Math.abs(b2-b1)a2) delta=-1; else delta=1; for(a = a1; a != a2+delta; a+=delta)//for(a = a1; a < a2; a++) { b = (double)b1 + ((double)(b2-b1)* ((double)a-(double)a1)/((double)a2-(double)a1)); //Die Abfrage ist zufallig wegen ArrayindexofBoundexception!! if(((int)b != sizeY) && ((int)a !=sizeX)) { SetPixel((int)a,(int)b, value); } } } else { if (b1>b2) delta=-1; else delta=1; for(b = b1; b != b2+delta; b+=delta) { a =(double)a1 + ((double)(a2-a1)* ((double)b-(double)b1)/((double)b2-(double)b1)); if((int)b != sizeY && ((int)a != sizeX)) { SetPixel((int)a , (int)b , value); } } } } /** * Draws a line between two given points (a1,b1) and (a2,b2) * * @param a1 x-position of the first point * @param b1 y-position of the first point * @param a2 x-position of the second point * @param b2 y-position of the second point */ public void drawLine(int a1, int b1, int a2, int b2) { double a,b,delta; //System.out.println("("+a1+","+b1+") ("+a2+","+b2); if(Math.abs(b2-b1)a2) delta=-1; else delta=1; for(a = a1; a != a2+delta; a+=delta)//for(a = a1; a < a2; a++) { b = (double)b1 + ((double)(b2-b1)* ((double)a-(double)a1)/((double)a2-(double)a1)); //Die Abfrage ist zufallig wegen ArrayindexofBoundexception!! if(((int)b != sizeY) && ((int)a !=sizeX)) { SetPixel((int)a,(int)b, this.GetPixel((int)a,(int)b)+1 ); } } } else { if (b1>b2) delta=-1; else delta=1; for(b = b1; b != b2+delta; b+=delta) { a =(double)a1 + ((double)(a2-a1)* ((double)b-(double)b1)/((double)b2-(double)b1)); if((int)b != sizeY && ((int)a != sizeX)) { SetPixel((int)a , (int)b , this.GetPixel((int)a, (int)b)+1 ); } } } } /** * Normalizes a given array to values between 0 and 255 */ protected void Normalize(){ int min, max; min = GetPixel(GetMin()); max = GetPixel(GetMax()); if (max!=min) { for(int i = 0 ; i < sizeX*sizeY; i++) image[i]=((image[i]-min)*255)/(max-min); } else { for (int i=0;i> 16) & 0xff) //red + ( (image[i] >> 8) & 0xff) //green + ( image[i] & 0xff) //blue )/3; } } /** * Draws the iRPImage to the screen, given a point (x,y) where * (x,y) specifies the upper left corner position of the picture. * This method normalizes the image[] array and creates an Image from * it using the MemoryImageSource-class. * * @param g the Graohics context of the screen * @param x x-position to draw from (upper left corner) * @param y y-position to draw from (upper left corner) */ public void PaintImage(Graphics g, int x, int y){ Image OutPut; iRPImage col_image= new iRPImage(sizeX,sizeY); for (int i=0; i