473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Need help rotating a PPM image in c++

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
9 6806
RedSon
5,000 Expert 4TB
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
5,000 Expert 4TB
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
Kraken
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
5,000 Expert 4TB
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
3,237 Expert 2GB
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
Kraken
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
3,237 Expert 2GB
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
5,000 Expert 4TB
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
Kraken
8
i fixed my formula and tried putting all the values in a new array and it worked, thanks!
Apr 3 '07 #10

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

Similar topics

4
by: Ian Hubling | last post by:
I'm trying to complete a rotating banner ad within a page I have. The rotating add has four images that rotate in three-second increments. I've got the images to rotate ok - but now I want to go...
1
by: Charles A. Lackman | last post by:
Hello, I am trying to rotate a picturebox control. I have done some experimenting with rotating the image inside the picturebox, but I am trying to get the affect of grabbing the corner of the...
5
by: John | last post by:
I am rotating images at one location of my web site. My problem is if I set the width and height of the new image before I show the new image, the old image is stretched first to the new image...
4
by: liz | last post by:
Is there a way to set up a rotating image slide show where people can add or take out any number of images (into a database) without changing the script to give an exact number or names? I've seen...
1
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the...
14
by: mikeoley | last post by:
Why would this script below work on an html page: http://www.eg-designdev.com/aircylinders/test.htm But not a java page such as this: "http://www.eg-designdev.com/aircylinders/index3.jsp" Or...
1
by: AR123 | last post by:
Hi I want to set up a rotating banner. Not sure how to incorporate my rotating banner code into the code below. I want the rotating banner to be the main feature image? This is set up in...
6
by: swethak | last post by:
Hi, I displayed the image taken from database.How to raotate that image using javascript.plz tell that how to start the logic.plz tell that some reference websites.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.