Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
	fstream flower("flower.bmp", ios::in | ios::out | ios::binary);
	int width=686;
	int height=800;
	flower.seekg(18,ios::beg);
	flower.put(height);
	flower.seekg(4);
	flower.put(width);
	
	flower.close();
}
Posted
Updated 21-Feb-15 11:15am
v3
Comments
Alexis i 21-Feb-15 17:16pm    
i want to rotate a bmp file named flower 90 degrees, so according to the BMP bitmap file format i have changed the 18th byte to 800 and the 22nd byte to 686 which swaps the height and width, but the image is corrupted! can anyone help me?
Sergey Alexandrovich Kryukov 21-Feb-15 22:12pm    
How about thinking just a bit, at least a tiny bit?
—SA

Just exchanging width and height will not rotate the image. You also need to rotate the bitmap data pixel by pixel. That requires quite a bit of buffer handling and data shoveling. For an example how to do it, look here:

Add fast user-extensible image processing support to CBitmap[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-15 22:12pm    
Sure, a 5.
—SA
Sergey Alexandrovich Kryukov 21-Feb-15 22:31pm    
I found inquirer's confusion so amazing that I added a simple example to illustrate this "corruption". Please see Solution 3.
—SA
I was afraid that my suggestion to "think a bit" could possibly offend you a bit, so I probably have to illustrate what this thinking could mean.

Suppose you have some bit data, which tells you (forget the "format", it is irrelevant):
you have
width = 3, height = 2, and pixels data 123456
It means that first line is 123 and the second line is 456:
123
456

If you swap width and height, you would have
width = 2, height = 3:
12
34
56

But turn original picture 90° clockwise (you can just turn your computer screen 90° clockwise and look at the same numbers), what will you see? apparently,
41
52
63

and counterclickwise:
36
25
14

The last two images are correct, and the previous one is "corrupted". What could be not obvious here?
This is what nv3 meant by telling you "to rotate the bitmap data pixel by pixel".

I hope now the problem should be clear to you.

—SA
 
Share this answer
 
v5
Comments
nv3 22-Feb-15 3:46am    
Yes, Sergey, this is a nice explanation. There is also another one, just suitable to a mathematician: If swapping height and width would suffice to rotate an image, how would one specify to rotate it clockwise or counter-clockwise? The lack of this possibility of expression already shows that just swapping height and width can't work.
Sergey Alexandrovich Kryukov 22-Feb-15 8:54am    
Indeed, nice way of proving, through the symmetry...
Thank you.
—SA
enhzflep 22-Feb-15 22:17pm    
"I was afraid that my suggestion to "think a bit" could possibly offend you a bit, so I probably have to illustrate what this thinking could mean." :thumbs-up: +5

... rest of answer ... :thumbs-up: +5
A good, clear, concise description of the problem and the needed re-alignment of the OPs view.
Sergey Alexandrovich Kryukov 22-Feb-15 23:21pm    
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900