开发者

change a code from flipping "8bpp .bmp image" horizontally to flip a "1bpp .bmp image" horizontally in x86

开发者 https://www.devze.com 2023-02-05 22:37 出处:网络
Hello here I have developed the code for Mirror/flipping an 8 bpp .BMP image horizontally. Handling any width properly, not only multiples of 4. Now I have to convert this code to do the same but for

Hello here I have developed the code for Mirror/flipping an 8 bpp .BMP image horizontally. Handling any width properly, not only multiples of 4. Now I have to convert this code to do the same but for a 1 bpp . bmp image(grayscale) using x86. The difficult part is that I dont know how to excess the individual bit maybe someone can edit this code..

        section     .text
    global      mirrorbmp8
mirrorbmp8:
    push        ebp
    mov     ebp, esp
    push        ebx
    push        esi
    push        edi

    mov     ebx, [ebp+12]       ;width - without padding
    and     ebx, 11b
    je      init            ;checking if there is a padding
    mov     edi, 4
    sub     edi, ebx
    add     [ebp+12], edi       ;width - with padding

init:
    mov     ebx, [ebp+16]   
    ;calculating the distance between top&bottom pixel
    dec     ebx
    mov     eax, [ebp+12]
    mul     ebx
    mov     esi, eax

    mov     edi, [ebp+8]    ;the first bottom pixel
    mov     edx, edi            ;the first top pixel
    mov     eax, edi
    开发者_JS百科add     eax, esi
    mov     ecx, [ebp+12]   
            ;register responsible for calc left columns

loop0:
    push        esi
    mov     esi, [ebp+12]

loop1:
    mov     bl, [edi]               ;changing pixels
    xchg        bl, [eax]
    mov     [edi], bl

    add     edi, esi        ;next pixel in this column
    sub     eax, esi
    cmp     edi, eax
    jl      loop1


    inc     edx             ;next bottom pixel
    mov     edi, edx

    mov     eax, edi                ;next top pixel
    pop     esi
    add     eax, esi

    dec     ecx         ;decrement number of columns left
    jnz     loop0           ;was that the last column?



end:
    pop     edi
    pop     esi 
    pop     ebx

    mov     esp, ebp
    pop     ebp
    ret

Any help will be appreciated. Thanks in advance :)

p.s if i will be able to do this version then I have to convert the whole code for x86-64 version also, any hints in this regard will also be helpful..


In case you want to know how to flip the image left to right instead of top to bottom, here's how you'd do that.

First, copy the headers of the bitmap into another file. Next, figure out how many bits past imageWidth % 32 are at the end of each scan line:

orphanBits = imageWidth % 32

In the example below, orphanedBits is 19. Read the last two DWORDS from the end of the scanline into two general purpose registers:

ebx = 10001010 11010101 00101010 10101010
eax = 01010101 01011000 00000000 00000000
  END OF SCAN LINE ^

Use the SHRD operand to move bits from ebx into ecx until the entire register is filled up:

shrd eax, ebx, orphanBits

ebx = 00000000 00000000 00010001 01011010
eax = 10100101 01010101 01001010 10101011
                       END OF SCAN LINE ^

Then use the following code to swap the bits of eax:

mov edx,eax
shr eax,1
and edx,055555555h
and eax,055555555h
lea eax,[2*edx+eax]
mov edx,eax
shr eax,2
and edx,033333333h
and eax,033333333h
lea eax,[4*edx+eax]
mov edx,eax
shr eax,4
and edx,0F0F0F0Fh
and eax,0F0F0F0Fh
shl edx,4
add eax,edx
bswap eax

eax = 11010101 01010010 10101010 10100101
      ^ END OF SCAN LINE

Write the adjusted DWORD (now in reverse order) into the new image. Repeat until the whole scanline is read. Repeat until all scanlines are read.

Edit: Originally had just bswap before I remembered it swapped bytes, not bits.


Bitmaps are stored in scanlines with one line of pixels per scan line. All you really have to do is reverse the order of these lines. The firs step would be to copy the headers of the bitmap into another file. Then you should calculate the length of each scanline. Because each scanline is always 32-bit padded, you'll need some math:

scanlineLength = imageWidth / 8
IF imageWidth % 32 != 0
  scanlineLength += 1
ENDIF

Next, copy scanlineLength bytes from the old file into the new file. Move up one scanline and repeat the process until they are all copied.

Edit: After rereading your question I'm still not sure which way you're flipping the images so I'm not sure if this applies.

0

精彩评论

暂无评论...
验证码 换一张
取 消