# Really tough question

• 02-01-2010, 08:02 PM
tranceslave
Really tough question
I have to complete the ImageManipulation Class that allows the manipulation of an image via two static methods in Java.

The first method will rotate the given area by 90 degrees in clockwise direction

/**
* Rotate the pixels in the given area by 90 degrees in
* clockwise direction.
* In case the centre of the area is to close the border of
* the picture no rotation willing occur.
*
* @param image the image you are performing the rotation on
* @param x x-coordinate of the centre of the rectangle
* @param y y-coordinate of the centre of the rectangle
* @param size half the length/width of the square
* @return
*/
static public void rotate(BufferedImage image,
int x, int y,int size) {

}
The second method will for the given area calculate for each a value according to a 3 X 3 matrix.Depending on this value the pixel stays the same or gets altered. This is a basic method to detect edges in a picture.

/**
* PERFORM a threshold calculation for each pixel of specified square and dependent on outcome
* alters a pixel or not. <p>
*
* Apart from pixel on the edge of the image each pixel is surrounded by 8 other pixels.
* A 3 x 3 matrix is given as a parameter to calculate for each pixel out of its on rgb
* value and that of the its 8 surrounding pixel a value.
* The calculation is done in the following way.
* For each of the nine pixels a value is calculated by adding its red, green and blue
* component together.
* These values are multiplied by the corresponding value in the matrix. The center pixel
* correspond to the cell 1,1 in the matrix, the pixel top right of the center pixel correspond
* to the cell 2,0 and so on.
*
* All these values are added up. If the absolute value of this sum is less than 250 the pixel
* will be unchanged. Otherwise the new value for a pixel (r,g,b) will be (255-r,255-g,255-b).
*
* All the calculation for the threshold should be done on the original values, but some
* might get altered. Hence the first two lines of this method are given. Here a copy of the
* BufferedImage is created. You should alter the image and use temp for the calculation.
*
*
* @param image BufferedImage object representing the
* image you are performing the swap on
* @param matrix a 3x3 matrix with the coefficients for the 9 pixels.
* @param x x-coordinate of the centre of the rectangle
* @param y y-coordinate of the centre of the rectangle
* @param size half the length/width of the square
*
*/
static public void toCanny(BufferedImage image,
double[][] matrix,
int x, int y, int size) {
BufferedImage temp=new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
(temp.getGraphics()).drawImage(image,0,0,image.get Width(), image.getHeight(),null );

}

Any kind of help will be appreciated!
• 02-01-2010, 08:28 PM
Fubarable
Why don't you give it a go with your best attempt here first, and then post your attempt. You will learn much more if we do it this way. Oh, and a hearty welcome to this forum!
• 02-03-2010, 01:46 AM
tranceslave
hello again i attempted some code..

Code:

```static public void rotate(BufferedImage image,                             int x, int y,int size) {                         int xaxes=0;             int yaxes=0;             int halfpic1=0;             int halfpic2=0;             //Checks if it inside the bounds if not then it doesn't rotate the square             if((x-size)>0 && (y-size)>0){                 if(x+size<image.getWidth() && y+size<image.getHeight()){                     //If is inside the bounds it starts the nested loop and checks every pixel                     int maxi = Math.min(image.getWidth(),x+size);                     int maxj = Math.min(image.getHeight(),y+size);                     for (int i = Math.max(0,x-size); i<maxi; i++) {                         //Here is a difference in the loop(j<y) so it doesn't go again through the pixels and rotate half of the image back                         for (int j = Math.max(0,y-size); j<y; j++) {                             //Make the rotation of the square                             xaxes=(2*x-i);                             yaxes=(2*y-j-1);//So it connects exactly the 2 pictures                             //Setting the rotated images in the correct place                             halfpic1= image.getRGB(i,j);                             halfpic2= image.getRGB(xaxes, yaxes);                                                  image.setRGB(xaxes,yaxes,halfpic1);                             image.setRGB(i,j,halfpic2);                         }//end of x loop                     }//end of y loop                                     }//end of first if statement             }//end of second if statement                     }```
the above code rotates a part of the image by 180 degrees by cutting the part into two subimages.. but we are told to do it by 4 subimages
• 02-03-2010, 01:54 AM
tranceslave
http://img121.imageshack.us/img121/586/lalalalarz.jpg

the result should be like that.. that part of the picture is rotated by 90 degrees.The mouse pointer is on that part. If the mouse pointer is at the border of the picture,that part of the picture is not rotated