gpt4 book ai didi

c - 翻转图像汇编代码

转载 作者:行者123 更新时间:2023-11-30 20:11:14 25 4
gpt4 key购买 nike

我正在开发一个基于 C 的程序,该程序可用于图像扭曲的汇编。应该起作用的伪代码是这个(始终使用 240x320 的图像

 voltearHorizontal(imgO, imgD){

dirOrig = imgO;

dirDest = imgD;

dirOrig = dirOrig + 239*320; //bring the pointer to the first pixel of the last row

for(f=0; f<240; f++){

for(c=0; c<320; c++){

[dirDest]=[dirOrig];

dirOrig++­­;

dirDest++;

}

dirOrig=dirOrig+640;//move the pixel to the first one of the upper row

}

}

但是当应用于汇编时,结果是,第一行没有被读取,留下了黑色的空间。

https://gyazo.com/7a76f147da96ae2bc27e109593ed6df8

这是我编写的代码,应该可以工作,这就是图像真正发生的情况:

https://gyazo.com/2e389248d9959a786e736eecd3bf1531

为什么使用这段代码,没有将原始图像的上行像素写入/读取到第二个图像?我哪部分代码出错了?

我想我已经没有标签可以解决我的问题了,感谢您提供的任何帮助(关于我错的地方)。此外,水平翻转(上面的是垂直)只是意外地完成了程序:

https://gyazo.com/a7a18cf10ac3c06fc73a93d9e55be70c

最佳答案

有什么特殊原因吗,为什么你把它写成慢速汇编程序?

为什么不将其保留在快速 C++ 中? https://godbolt.org/g/2oIpzt

#include <cstring>

void voltearHorizontal(const unsigned char* imgO, unsigned char* imgD) {
imgO += 239*320; //bring the pointer to the first pixel of the last row
for(unsigned f=0; f<240; ++f) {
memcpy(imgD, imgO, 320);
imgD += 320;
imgO -= 320;
}
}

将使用 gcc6.3 -O3 编译为:

voltearHorizontal(unsigned char const*, unsigned char*):
lea rax, [rdi+76480]
lea r8, [rdi-320]
mov rdx, rsi
.L2:
mov rcx, QWORD PTR [rax]
lea rdi, [rdx+8]
mov rsi, rax
sub rax, 320
and rdi, -8
mov QWORD PTR [rdx], rcx
mov rcx, QWORD PTR [rax+632]
mov QWORD PTR [rdx+312], rcx
mov rcx, rdx
add rdx, 320
sub rcx, rdi
sub rsi, rcx
add ecx, 320
shr ecx, 3
cmp rax, r8
rep movsq
jne .L2
rep ret

即。比内联汇编效率高 800%。

<小时/>

无论如何,在你的问题中,问题是:

dirOrig=dirOrig+640;//move the pixel to the first one of the upper row

您需要执行 -= 640 才能返回两行。

关于屏幕中的那些内联汇编...将它们作为文本提出问题,但是从快速查看它们来看,我会简单地用 C++ 重写它并将其保留给编译器,您在汇编中做了许多性能错误的事情,所以我认为这样做没有任何意义,而且内联汇编很丑陋且难以维护,而且很难正确编写。

<小时/>

我什至检查了图片中的 asm。您在 eax 中有行计数器,但您使用 al 复制像素,因此它确实破坏了行计数器值。

下次使用调试器。

顺便说一句,您的图片是 320x240,而不是 240x320。

关于c - 翻转图像汇编代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com