更改翻转“8bpp .bmp 图像”的代码水平翻转“1bpp .bmp 图像” x86 水平

发布于 2024-10-13 15:43:42 字数 1718 浏览 5 评论 0原文

你好,我开发了水平镜像/翻转 8 bpp .BMP 图像的代码。正确处理任何宽度,而不仅仅是 4 的倍数。现在我必须转换此代码以执行相同的操作,但对于 1 bpp 。使用 x86 的 bmp 图像(灰度)。困难的部分是我不知道如何超出个别位也许有人可以编辑此代码..

        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
    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

任何帮助将不胜感激。 提前致谢:)

ps 如果我能够执行此版本,那么我还必须将整个代码转换为 x86-64 版本,这方面的任何提示也会有所帮助。

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
    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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-10-20 15:43:42

如果您想知道如何从左到右翻转图像而不是从上到下翻转图像,请按以下方法操作。

首先,将位图的标题复制到另一个文件中。接下来,计算出每条扫描线末尾有多少位超过 imageWidth % 32:

orphanBits = imageWidth % 32

在下面的示例中,orphanedBits 为 19。将扫描线末尾的最后两个 DWORD 读入两个通用位中。目的寄存器:

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

使用 SHRD 操作数将位从 ebx 移至 ecx,直到填满整个寄存器:

shrd eax, ebx, orphanBits

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

然后使用以下代码交换 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

写入调整后的 DWORD(现在按相反顺序) )进入新图像。重复此操作,直到读取整个扫描线。重复此操作直至读取所有扫描线。

编辑:最初只是 bswap,然后我记得它交换了字节,而不是位。

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.

青衫负雪 2024-10-20 15:43:42

位图存储在扫描线中,每条扫描线有一行像素。您真正需要做的就是颠倒这些行的顺序。第一步是将位图的标题复制到另一个文件中。然后你应该计算每条扫描线的长度。因为每个扫描线始终是 32 位填充的,所以您需要一些数学运算:

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

接下来,将 scanlineLength 字节从旧文件复制到新文件中。向上移动一条扫描线并重复该过程,直到全部复制完毕。

编辑:重读您的问题后,我仍然不确定您翻转图像的方式,所以我不确定这是否适用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文