我目前正在做一个项目,我必须使用 4x4 有序抖动矩阵将 24bpp 图片减少为 3bpp 图像。但是,在进行了抖动处理之后,我的图像只显示了大约 1/3 的路径。有谁知道我做错了什么?
资源:
#include <stdio.h>
#include <fstream>
/*24 bit per pixel - 3 bit per pixel dither*/
/*Use the 4x4 Ordered Dither Matrix:
[1 9 3 11]
[13 5 15 7]
[4 12 2 10]
[16 8 14 6]
*/
int checkColor(int a, int b);
unsigned char buf[512][512];
unsigned char out[512][512];
float ratio = 1.0 / 17;
int main(){
FILE *fp, *output;
int i, j, k, l;
/*dither matrix*/
unsigned int dith[4][4] = {{1, 9, 3, 11}, {13, 5, 15, 7}, {4, 12, 2, 10}, {16, 8, 14, 6}};
if((fp = fopen("LennaRGB512.data", "rb")) == NULL){
printf("error opening file\n");
}
for (i = 0; i < 512; i++) {
for (j = 0; j < 512; j++) {
buf[i][j] = fgetc(fp); /*Put data in buffer*/
}
}
i = 0;
j = 0;
int x, y;
int bd = 64;
for (k = 0; k < 512; k++){
for (l = 0; l < 512; l++){
int oldPixel = buf[k][l];
int value = (oldPixel + (ratio * dith[k%4][l%4]));
int r = ((oldPixel >> 16) & 0xff) + value;
int g = ((oldPixel >> 8) & 0xff) + value;
int b = (oldPixel & 0xff) + value;
int newPixel = 0x000000 | checkColor(r, bd) << 16 | checkColor(g, bd) << 8 | checkColor(b, bd);
out[k][l] = newPixel;
}
}
output = fopen("converted_img.data", "wb");
for (i = 0; i < 512; i++){
for (j = 0; j < 512; j++){
fputc(out[i][j], output);
}
}
fclose(output);
fclose(fp);
return 0;
}
int checkColor(int a, int b){
return a / b * b;
}
我之前的图像是 512x512 图像,但是我的输出图像抖动只是图像的一部分(512x170)