首页 > 编程知识 正文

C语言 位数组在XY方向镜像翻转算法实现

时间:2023-05-05 19:40:07 阅读:286170 作者:1437

一、问题

图片已经取模生成点阵数组,每1位控制像素点是否显示,如何镜像翻转?

比如下面这张图:

使用取模软件生成点阵数组:

生成的数组如下:

unsigned char gImage_upload[128] = { /* 0X00,0X01,0X20,0X00,0X20,0X00, */0X00,0X00,0X00,0X00,0X00,0X03,0X00,0X00,0X00,0X03,0X80,0X00,0X00,0X07,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X1F,0XE0,0X00,0X00,0X1F,0XF0,0X00,0X00,0X3F,0XF8,0X00,0X00,0X7F,0XF8,0X00,0X00,0XFF,0XFC,0X00,0X00,0XFF,0XFE,0X00,0X00,0XFF,0XFC,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X0F,0XC0,0X00,0X00,0X07,0X80,0X00,0X3C,0X00,0X00,0XF0,0X3C,0X00,0X00,0X70,0X38,0X00,0X00,0X78,0X78,0X00,0X00,0X78,0X7F,0XFF,0XFF,0XF8,0XFF,0XFF,0XFF,0XFC,0XFF,0XFF,0XFF,0XFC,0XFF,0XFF,0XFF,0XFC,0X7F,0XFF,0XFF,0XF8,0X3F,0XFF,0XFF,0XF0,}; 二、调试手段 1. hexdump void hex_dump(uint8_t *buffer, uint32_t len){ uint32_t i; for (i = 0; i < len; i++) { printf("0x%02x,", buffer[i]); if ( (i+1) % 16 == 0) { printf("n"); } }} 2. 字符方式显示位图 void show_image_by_ascii(uint8_t *image, uint32_t width, uint32_t height, char ch){ uint32_t i, j; uint32_t total_bytes, bytes_per_line; uint8_t t; total_bytes = width * height / 8; bytes_per_line = width / 8; for (i = 0; i < total_bytes; i++) { t = *(image + i); for (j = 0; j < 8; j++) { if (t & 0x80) { printf("%c", ch); } else { printf(" "); } t <<= 1; } if ((i+1) % bytes_per_line == 0) { printf("n"); } }}

调用将图片数组显示出来:

show_image_by_ascii(gImage_upload, 32, 32, '*');

三、在x方向镜像 static uint8_t reverse8(uint8_t c){ c = ( c & 0x55 ) << 1 | ( c & 0xAA ) >> 1; c = ( c & 0x33 ) << 2 | ( c & 0xCC ) >> 2; c = ( c & 0x0F ) << 4 | ( c & 0xF0 ) >> 4; return c;}int image_mirror_x(uint8_t *image, uint32_t width, uint32_t height){ uint32_t bytes_per_line, lines; uint32_t i, j, k, offset; uint8_t t, v; bytes_per_line = width / 8; lines = height; printf("bytes_per_line is %dn", bytes_per_line); printf("lines is %dn", lines); for (i = 0; i < lines; i++) { // line mirror. offset = bytes_per_line*i; for (j = 0; j < bytes_per_line/2; j++) { t = image[offset+j]; image[offset+j] = image[offset+4-j-1]; image[offset+4-j-1] = t; } for (j = 0; j < bytes_per_line; j++) { // byte mirror image[offset+j] = reverse8(image[offset+j]); } }} 四、在Y方向镜像 int image_mirror_y(uint8_t *image, uint32_t width, uint32_t height){ uint32_t bytes_per_line, lines; uint32_t i, j, k, offset, other_offset, total_bytes; uint8_t t, v; // width: 32, height: 16. // 32 / 8 = 4; bytes_per_line = width / 8; lines = height; total_bytes = bytes_per_line * lines; printf("bytes_per_line is %dn", bytes_per_line); printf("lines is %dn", lines); printf("total_bytes is %dn", total_bytes); for (i = 0; i < lines/2; i++) { // line mirror. offset = bytes_per_line*i; other_offset = total_bytes - offset-4; for (j = 0; j < bytes_per_line; j++) { t = image[offset+j]; image[offset+j] = image[other_offset+j]; image[other_offset + j] = t; } }}

将图片镜像之后:

image_mirror_y(gImage_upload, 32, 32);

结果如下:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。