Author |
Message |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Tue Aug 24, 2004 7:47 pm Post subject: Image flipping |
|
|
Hi. Suppose, I've got an image loaded in, with the pixels ready to be used to make an opengl texture. BUT, the image was read and stored upside down. How could I flip it to be right side up? Anybody have an idea? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
rizzix
|
Posted: Tue Aug 24, 2004 8:10 pm Post subject: (No subject) |
|
|
howz the image stored? is it some sort of array? if it is.. it shouldn't be too hard |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Wed Aug 25, 2004 5:39 am Post subject: (No subject) |
|
|
Yes, it's an array of pixels accessed by a GLubyte*. My problem is I don't know what kind of order the pixels are stored in. Like, would it go in rows, or in columns? Has anybody done something like this before? |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: Wed Aug 25, 2004 10:09 am Post subject: (No subject) |
|
|
well look at the bitmap turing library i wrote hmm u might figure out something eh? well i can't remember how things are stored and stuff.. its been a while. |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Wed Aug 25, 2004 5:43 pm Post subject: (No subject) |
|
|
Thanks, rizzix, I looked at your bitmap loader again, and it seemed that the pixels are stored just as I imagined. So I went back to my code, and fixed some small problems with it and managed to get it working... somewhat. Here's the code I have currently (in C++)
code: |
for (GLuint row = 0; row < (int)(H/2); row++) // each row
{
for (GLuint pixel = 0; pixel < W; pixel++) // each pixel in row
{
for (GLuint byte = 0; byte < bpp; byte++) // each byte in pixel
{
pixels2[row*rowsize+pixel*bpp+byte] ^= pixels2[H*rowsize-row*rowsize+pixel*bpp+byte] ^=
pixels2[row*rowsize+pixel*bpp+byte] ^= pixels2[H*rowsize-row*rowsize+pixel*bpp+byte];
}
}
}
|
Try not to think about the ugnliness of the code.
-pixels2 (while not a very great name) is a pointer to the pixel data.
-H is the height of the image, W is the width, etc.
-rowsize is the size of each row in bytes
Basically, the idea here is to go through each row of the image from the bottom to the middle, and in each row go pixel by pixel (and byte by byte) and swap it with the pixel on the opposite end.
This "successfully" flipped a picture. However, it only worked for one. The image was 512*512, and trying it with 256*256 or 128*128 would cause the program to crash. Any ideas? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Aug 25, 2004 6:31 pm Post subject: (No subject) |
|
|
When you say flipping... do you just need to swap the pixels from top to bottom and vice versa, or do you need to change the colors somehow?
If it's the latter, then you shouldn't have to change the bytes in each pixel, should you?
Completely untested:
code: | int middle_row = H / 2
int total_pixels = W * H;
for (GLuint row = 0; row < middle_row; row++) // each row
{
for (GLuint pixel = 0; pixel < W; pixel++) // each pixel in row
{
GLuint upper_pixel_base = W * row + pixel;
GLuint lower_pixel_base = W * (H - row) + pixel;
GLuint swap[4] = {0, 0, 0, 0};
for (GLuint i = 0; i < 4; i++)
{
swap[i] = pixels2[upper_pixel_base + 1];
pixels2[upper_pixel_base + 1] = pixels2[lower_pixel_base + 1];
pixels2[lower_pixel_base + 1] = swap[i];
}
}
} |
|
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Wed Aug 25, 2004 6:43 pm Post subject: (No subject) |
|
|
Just swapping the pixels. I'm switching each byte in the pixel individually because I'm working with a GLubyte* for the pixel data, so I can't really get at the whole pixel at once. Sure, I believe there's a better way, but my first concern is to get it working. |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Wed Aug 25, 2004 8:14 pm Post subject: (No subject) |
|
|
The code you posted doesn't seem to do anything. Either that or it flips the image twice maybe.
Anyways, I've seen that the program doesn't actually crash at the two lines of code used for swapping the pixels, but it does crash later at the return statement of the function if those lines are executed. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Aug 25, 2004 8:21 pm Post subject: (No subject) |
|
|
Aha... I see what you were doing with the ^=. It's the kind of bitwise stuff I've been avoiding lately, so it's no wonder I missed that.
Also not tested:
code: |
void flip_image(GLuint image[], const int width, const int height, const int bytes_per_pixel)
{
const int middle_row = width / 2;
for (int row = 0; row < middle_row; row += 1)
{
for (int pixel = 0; pixel < width; pixel += 1)
{
int pixels_over = pixel * bytes_per_pixel;
int upper_pixel_offset = width * row + pixels_over;
int lower_pixel_offset = width * (height - row) + pixel_over;
// Scary pointer arithmetic.
// Add the offset to the pointer that is "image".
// You get another pointer back which can be
// accessed like an array.
GLuint * upper_pixel_start = image + upper_pixel_offset;
GLuint * lower_pixel_start = image + lower_pixel_offset;
for (int byte = 0; byte < bytes_per_pixel; byte++)
{
upper_pixel_start[byte] ^= lower_pixel_start[byte] ^=
upper_pixel_start[byte] ^= lower_pixel_start[byte];
}
}
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Wed Aug 25, 2004 8:59 pm Post subject: (No subject) |
|
|
Well... it didn't crash.
I think I can risk just a couple screenshots to show you what happened, though.
whoa.jpg is with your image flip function (I should point out that I switched the GLuint pointers to GLubyte pointers so I could test it right away)
original.jpg is "normal" without my image code, and just flipping the actual images and saving them upside down.
Thanks for the help though, I'd like to take a closer look at your code tomorrow when I get a chance. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Aug 25, 2004 11:08 pm Post subject: (No subject) |
|
|
Mazer wrote: Well... it didn't crash.
Ah.... progress! |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: Thu Aug 26, 2004 1:08 am Post subject: (No subject) |
|
|
i might as well mention this now bitmap pictures are padded with null bytes if it dosent fit evenly, for ex:
code: |
***********
***********
*******0000
|
there u go where 0 = null byte and * is some pixel data
another thing.. i dont know what number the null byte is: most likely Zero heh
and another thing i dont think 24-bit bitmaps are padded.. its probably just the 256-bit and monochrome.. but then again.. not sure!
hell i dont get it.. how does the padding fit in? man i'm soo lost.. and back then i was aware of all this heh argh.. i'll work on that image lib once again.. this time a better version of it! |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Thu Aug 26, 2004 6:18 am Post subject: (No subject) |
|
|
If it makes any difference at all as to how the image is actually being stored, I'm loading TGAs in this situation. |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Thu Aug 26, 2004 7:22 pm Post subject: (No subject) |
|
|
OK. I changed
int upper_pixel_offset = width * row + pixels_over;
to
int upper_pixel_offset = width * row * bytes_per_pixel + pixels_over;
and
int lower_pixel_offset = width * (height - row) + pixel_over;
to
int lower_pixel_offset = width * (height - row) * bytes_per_pixel - pixels_over;
and then, I changed the for loop to start at pixel = 1 (probably some problem with the lower pixel offset... I should probably be adding bpp or 1 somewhere). Now, it successfully flips the image vertically without crashing. It also flips it horizontally.
I'll get back to you guys later. |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: Thu Aug 26, 2004 7:38 pm Post subject: (No subject) |
|
|
Evreika!
Switched:
int lower_pixel_offset = width * (height - row) * bytes_per_pixel - pixels_over;
to
int lower_pixel_offset = width * (height - row) * bytes_per_pixel - width * bytes_per_pixel + pixels_over;
Thanks guys, you'll probably be in the credits. |
|
|
|
|
![](images/spacer.gif) |
|