Hey guys, hopefully someone can help me getting starting with loop inside loops.  I struggled with this for at least 5 or 6 hours yesterday and finally got something working.  What I have is a 256 * 256 bitmap image (indexed color).  I want to flip the bitmap across the horizontal axis like most image program can do.  I realized that all I have is a 256 * 256 "matrix" of bytes essentially.  And so I need to swap the 1st row the 256th row, 2nd <-> 255th etc..but copy 256 bytes across at a time.  

Mov Esi, ;0th row start
Mov Edi, Esi
Lea Edi, ;255th row end
Sub Edi, 256 ;dont want to start at end of data but 256 bytes before end of data = 255 th row start
Mov Edx, 128 ;total row ops needed= (rows /2)
Mov Ecx, 64 ;64dwords = 256 bytes
Dec Ecx
Jcxz idone
Jmp iloop
Add Esi, 256
Sub Edi, 256
Dec Edx
Jns oloop

I wish there was a swapsd instruction :) But since there isnt I tried numerous times with movsd and couldn't ever get it right. So I decided to push and pop instead.  Other attempts included allocating 2x the memory and putting the rows in the "2nd" buffer but that was flawed too. Eventually I may have to flip other image sizes than 256 * 256.
Posted on 2009-09-12 11:10:45 by GoldStar611
A row of pixels in a bitmap image is always 32-bit padded so you can transfer a DWORD of data at a time without worrying about overflow regardless of the size of the image. In pseudo code:

for( i = 0 ; i < height / 2 ; i++ )
  for( j = 0 ; j < width / 4 ; j += 4 )
    eax = raster
    raster = raster[(height-i-1)*width+j]
    raster[(height-i-1)*width+j] = eax

Height is the number of scan lines in the image and width is the number of bytes per scan line (not always the number pixels). Don't forget to negate the height of the image in the bitmap header or it will be displayed upside down.

Posted on 2009-09-14 07:33:43 by Sparafusile

A row of pixels in a bitmap image is always 32-bit padded
Don't forget to negate the height of the image in the bitmap header or it will be displayed upside down.

Thanks for that info on the bitmap images.  I do have a question about the 32bit padding -- is that for the OS/2 and Windows format bitmaps?  So far as negating the height of the bitmap, I'm actually converting between Amiga IFF format and windows bitmap headers.  The bitmap data is the same except it is flipped for some reason
Posted on 2009-09-14 20:22:27 by GoldStar611
Hm, iirc windows bitmaps are only WORD padded, not DWORD?

Padding depends on format, http://www.wotsit.org is your friend :)
Posted on 2009-09-15 02:41:55 by f0dder
Windows bitmaps are always DWORD padded. I'm not sure about OS/2 bitmaps though, I've never seen one.

If the bitmap data is the same between IFF and Bitmap except for the scan row order, simply copy the data as it is and set the bitmap height to negative the IFF height. The only problem you'll run into is if the IFF file format doesn't DWORD pad its scan lines.

Posted on 2009-09-15 13:11:39 by Sparafusile
Checked wotsit, seems you're right - looking up BITMAP in MSDN, I get the following though:
bmWidthBytes - Specifies the number of bytes in each scan line. This value must be divisible by 2, because the system assumes that the bit values of a bitmap form an array that is word aligned.

memory vs. disk representation issue?
Posted on 2009-09-15 13:18:10 by f0dder
That is odd. I can't explain why it states that. I can't find anywhere in the documentation where they talk about proper padding either. I suggest using wotsit.org for your file formatting needs, as F0dder stated.
Posted on 2009-09-15 13:37:55 by Sparafusile