By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Need help rotating a PPM image in c++

P: 8
Hi, i have an assignment to open PPM images and prompt the user for either brightening, flipping or rotating the image. Ive done the brightening and flipping, but i cant get the rotating to work. When i try to rotate i get an error, although i think the concept is right. With some testing i figured out thet I get the error when x = 4 and y = 1 i think. Memory read access violation or something. Im using Visual C++ 2005 express edition and windows xp. I was hoping you could tell me why i get the error?

Thanks,

Expand|Select|Wrap|Line Numbers
  1. int p;
  2.     unsigned char temp;
  3.  
  4.     for(int x = 0; x < height; x++)
  5.         for(int y = 0; y < width ; y++)
  6.         {
  7.             p = x + y * width;
  8.  
  9.             temp = image[p].blue;
  10.             image[p].blue = image[(height * x) + (height - y)].blue;
  11.             image[(height * x) + (height - y)].blue = temp;
  12.  
  13.             temp = image[p].red;
  14.             image[p].red = image[(height * x) + (height - y)].red;
  15.             image[(height * x) + (height - y)].red = temp;
  16.  
  17.             temp = image[p].green;
  18.             image[p].green = image[(height * x) + (height - y)].green;
  19.             image[(height * x) + (height - y)].green = temp;
  20.  
  21.         }
  22.  
  23.         int temp1;
  24.         temp1 = width;
  25.         width = height;
  26.         height = temp;
Apr 2 '07 #1
Share this Question
Share on Google+
9 Replies


RedSon
Expert 5K+
P: 5,000
Interesting code you have here. Unfortunately during my graphics classes I feel asleep during the matrix transformations lecture. OpenGL and DirectX provide easy methods to rotate matrices so you do not have to do it manually pixel by pixel. The fundamental mechanics of matrix rotation I do not understand. But let me break down your code algorithmically.
Expand|Select|Wrap|Line Numbers
  1. loop from x = 0 to width 
  2.     loop from y = 0 to height
  3.          locate a not-previously-rotated pixel 
  4.          copy it into a temp
  5.          swap the temp pixel with the pixel in its rotated position
  6.          do this for each RGB data
  7.  
Does that about explain your algorithm?
Apr 2 '07 #2

RedSon
Expert 5K+
P: 5,000
I'm not to sure about the math in your algorithm but I think you have some boundary issues. It appears that sometimes you are trying to copy pixels into a matrix location that does not exist.

What I would suggest is this: When you first start your application take the image as input but then make your matrix four times larger then your picture. Now you can do two things, recursively break your image into quadrants and copy the pixels in that quadrant rotated to another section of the larger image. Or scan it line by line as you are rotate and copy to another section of the larger image. Then shrink your image back to its normal size.

Have you been able to get this to work on a 2x2 pixel image, what about 3x3 or 4x4?
Apr 2 '07 #3

P: 8
thanks for your help.

I tried enlarging the array of structures that holds the pixels and the error didnt come up. What came up was a seriously messed up picture :). I guess i gotta figure out how to roate it right. However, it would be really helpful if you could you tell me why the code i posted was wrong.

by the way, i was trying to rotate the image by 90 degrees clockwise.

I thought that i should move each pixel to its rotated position by this method: The first pixel has to move the length of the old height to get to its position. The second pixel counting downwards has to move and change position with the length of the old height minus the distance from the beginning of the row y. The pixel in the 2nd column and 2nd row has to move the length of the old height + the length of the old height minus the distance from the begining of the row y.
Sorry if this sounds confusing, but i myself am really confused by what im saying and im not sure how to proceed.

P.S.
sorry im a bit of a newbie and i dont understand what you mean by 2x2 and 3x3.
Apr 2 '07 #4

RedSon
Expert 5K+
P: 5,000
Here is a good way to get started, define mathematically the relationship between a pixels original position and its rotated position.

Assuming a coordinate system with origin at top left and increasing both right and down. How can you define an x and y position for your pixel. I think you are on the right track but you really need to describe it in terms of numbers not words.
Apr 2 '07 #5

Motoma
Expert 2.5K+
P: 3,235
P.S.
sorry im a bit of a newbie and i dont understand what you mean by 2x2 and 3x3.
I think what he is trying to say is that this may be a dimensional error: you are trying to rotate a picture that is not square.
If you had a 2x8 pixel image, you would only be able to write the first two pixels before you went outside the image's boundries.
Apr 2 '07 #6

P: 8
oh, well maybe thats what happened in the first place, the image is 200 x 300 pixels.

Anyway i found out that rotating an image by 90 degrees clockwise is the same as transposing it (I hope this is right). So i tought id try to do that in my array of structures.

So im thinking:

in the normal structure the position of a pixel in the image is : i + j * width
so if i substitute that with : j + i * width

then it should work. However, i dont even get a coherent picture.

the code went like this:

Expand|Select|Wrap|Line Numbers
  1. for(int y = 0; y < height ; y++)
  2.         for(int x = 0; x < width ; x++)
  3.         {
  4.             p = x + y * width;
  5.             p1 = y + x * width;

Then i used p and p1 to switch the array. i also tried looping until only half the height, as i dont wanna substitute all the values twice, still didnt work.

Is my concept totally wrong?
Apr 2 '07 #7

Motoma
Expert 2.5K+
P: 3,235
What I would suggest is, rather than substituting pixels in an image, create a new image structure, and build it pixel by pixel, to avoid writing over pixels that you have not yet read.
Apr 2 '07 #8

RedSon
Expert 5K+
P: 5,000
oh, well maybe thats what happened in the first place, the image is 200 x 300 pixels.

Anyway i found out that rotating an image by 90 degrees clockwise is the same as transposing it (I hope this is right). So i tought id try to do that in my array of structures.

So im thinking:

in the normal structure the position of a pixel in the image is : i + j * width
so if i substitute that with : j + i * width

then it should work. However, i dont even get a coherent picture.

the code went like this:

Expand|Select|Wrap|Line Numbers
  1. for(int y = 0; y < height ; y++)
  2.         for(int x = 0; x < width ; x++)
  3.         {
  4.             p = x + y * width;
  5.             p1 = y + x * width;

Then i used p and p1 to switch the array. i also tried looping until only half the height, as i dont wanna substitute all the values twice, still didnt work.

Is my concept totally wrong?
Your concept may be wrong. Instead of guessing and checking what you *think* the proper algorithm might be why don't you try rotating an image on paper first. Draw a 3 pixel by 3 pixel image, make sure it is not symmetrical, and rotate it 90 degrees.

You are on the right track trying to find the relationship between the position of the pixel before it is rotated and the position after, but I do not think that exchanging width for height is the correct format.

As I said before you must have the math down and proven before you can code the computer to do this for a large image.
Apr 3 '07 #9

P: 8
i fixed my formula and tried putting all the values in a new array and it worked, thanks!
Apr 3 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.