Handmade Hero » Forums » Code » Mirroring Bitmap about the Y axis
jdduval
Jordan Duval
21 posts
<br>
#9005 Mirroring Bitmap about the Y axis
1 year, 3 months ago

Hi all,

I am trying to see if I can mirror a bitmap about the Y axis before I blt the bitmap, and I gave a shot at writing the code but I am getting an exeption thrown on line 15. I figured if I just swapped each pixel with the pixel equally as far on the other side of the center of the bitmap it would do the trick.

Say we have a 5 pixel bitmap:

p1 p2 p3 p4 p5

and after my method it should look like:

p5 p4 p3 p2 p1

Am I thinking about this wrong? Also are there any faster ways to do this?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    uint32 Temp;
    uint8* Row = (uint8 *) Bitmap->Pixels;
    
    for(int Y = 0;
        Y < Bitmap->Height;
        ++Y)
    {
        uint32* Pixel = (uint32 *) Row;

        for(int X = 0;
            X < (Bitmap->Width / 2);
            ++X)
        {
            Temp = *(Pixel + X);
            *(Pixel + X) = *(Pixel + Bitmap->Width - X);
            *(Pixel + Bitmap->Width - X) = Temp;
        }
        
        Row += Pitch;
    }
RJTracer
Lachlan Easton
15 posts

Technically a Programmer

#9006 Mirroring Bitmap about the Y axis
1 year, 3 months ago Edited by Lachlan Easton on Oct. 18, 2016, 8:17 a.m.

1
Pixel + Bitmap->Width - X

Assuming Width is the number of pixels in a row then this will go one off the end.
e.g. Say a row has 5 pixels. Pixel(0) + Width(5) - X(0) = 5, but highest index of 5 pixels is 4.

Also you can probably get rid of the Pixel var as it's just an alias for Row.
graeme
33 posts
#9007 Mirroring Bitmap about the Y axis
1 year, 3 months ago Edited by graeme on Oct. 18, 2016, 8:33 a.m.

RJTracer
Also you can probably get rid of the Pixel var as it's just an alias for Row.

Then the pointer arithmetic would be working on the wrong size type

w/r/t faster methods I think if you were to reuse the flipped bitmap you wouldn't do any better without going into micro-optimisation territory, but for a one shot, or to save memory if you expect to flipping a lot of them, you could add a flipped render path and just read out the X values backwards when doing the actual blit
jdduval
Jordan Duval
21 posts
&lt;br&gt;
#9015 Mirroring Bitmap about the Y axis
1 year, 3 months ago

graeme
add a flipped render path and just read out the X values backwards when doing the actual blit


This is a lot better, thanks!