【发布时间】:2015-08-23 09:37:33
【问题描述】:
任务是:将 24 bpp .BMP 图像的顶部淡化为白色,以便对每个像素的颜色进行线性插值 根据其与顶部边缘的距离,在其原始颜色和白色之间。距离像素 >= dist 不会褪色。
我创建了所有代码并试图淡化一些东西。似乎它仅适用于 2 次方的距离,因此 32,64,128 等... 我正在使用线性插值方程,但我不知道为什么它不适用于其他数字。 有任何想法吗 ? :)。
示例图片:
褪色后,我越来越...
global fadetop
; parm.
%define img [ebp+8]
%define width [ebp+12]
%define height [ebp+16]
%define dist [ebp+20]
; local
%define row_bytes [ebp-4]
%define dist_counter [ebp-8]
fadetop:
; create stack frame
push ebp
mov ebp, esp
sub esp, 8
; push register on stack
push ebx
push esi
push edi
; calculate size of row
mov edx, width
lea edx, [edx+edx*2]
add edx, 3
and edx, 0fffffffch
mov row_bytes, edx
; address of datas -calculations to write pixels from top left corner
mov eax,height
mul edx
sub eax ,row_bytes
add eax,3
mov esi, img
add esi,eax
;line couter
xor edx, edx
mov dist_counter , edx
line:
; pixel counter in line
mov edi, width
; index rexister
xor ebx, ebx
; INTERPOlATION STARTS NOW !!!
convert:
movzx eax, byte [esi+ebx+0] ; take color 1
sub eax, 255 ; sub white value
mov ecx, dist_counter
imul ecx ;color * actual position
cdq
mov ecx, dist ; devide by full fade operation distance
idiv ecx
add eax, 255 ; add 255
mov [esi+ebx+0], al ;put in this place
movzx eax,byte [esi+ebx+1] ; take color 2
sub eax, 255
mov ecx , dist_counter
mul ecx
cdq
mov ecx, dist
div ecx
add eax, 255
mov [esi+ebx+1], al
movzx eax,byte [esi+ebx+2] ; take color 3
sub eax, 255
mov ecx , dist_counter
mul ecx
cdq
mov ecx, dist
div ecx
add eax, 255
mov [esi+ebx+2], al
;________________________________________________________________
;if still in one line
add ebx, 3
dec edi
jnz convert
; if next line
sub esi, row_bytes
mov edx,dist_counter
inc edx
mov dist_counter,edx
mov eax, dist
cmp eax, edx
jnz line
; pop registers
pop edi
pop esi
pop ebx
; return trace
mov esp, ebp
pop ebp
ret
为那些想要运行它并尝试淡化的人编写 C 代码:
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void fadetop(void* img, int width, int height ,int dist );
int main (int argc, char** argv) {
char* buff;
if (argc != 3 ) {
printf("Use file: %s [file]\n",argv[0]);
return 1;
}
struct stat st;
stat(argv[1], &st);
buff = (char *) malloc(st.st_size);
if (buff == NULL) {
printf("Memory error!!\n");
return 1;
}
int fd = open(argv[1], O_RDONLY, 0);
if (fd == -1) {
printf("File access error\n");
free(buff);
return 1;
}
int size = read(fd, buff, st.st_size);
uint32_t offset = *(uint32_t *) (buff + 0x0a);
uint32_t width = *(uint32_t *) (buff + 0x12);
uint32_t height = *(uint32_t *) (buff + 0x16);
uint16_t bpp = *(uint16_t *) (buff + 0x1c);
if (bpp == 24) {
int fd_out;
printf("worked dist: %d , heigh: %d\n", atoi(argv[2]), height);
fadetop(buff + offset, width , height , atoi(argv[2]) );
fd_out = creat("fade.bmp", 0644);
write(fd_out, buff, size);
close(fd_out);
printf("Image faded.\n");
}
else {
printf("Invalid BMP\n");
}
close(fd);
free(buff);
return 0;
}
【问题讨论】:
-
dist参数的预期用途是什么?我觉得可以换成min(255,height)。您可以添加指向之前/之后图像的链接吗?只需将其发布到imgur 等公共图像服务器上,就会有人将其插入到您的问题中。 -
我稍微更改了 asm 代码。我不是在插值计算部分 xoring edx ,而是在做 cdq 。有图像。 [链接1]i.imgur.com/RtGmj0q.png。我正在处理这张图片,褪色后我得到... [link2]i.imgur.com/ooEn606.png .
-
dist 参数用于保持应该褪色的全距离
标签: x86 interpolation intel bmp fading