gpt4 book ai didi

c++ - 有序颜色抖动

转载 作者:行者123 更新时间:2023-11-28 05:28:43 25 4
gpt4 key购买 nike

我目前正在做一个项目,我必须使用 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)

最佳答案

您的源图像是 24bpp。这是每个像素三个字节。如果您只读取 512 字节宽和 512 字节高,则这正好表示图像的 512x170 (512/3)。开始时每个像素需要读取 3 个字节。

此外,您正在使用 int newPixel 转换为 32bpp,而不是 3bpp,而是将其存储在 char 中,这将溢出(截断)该值。

此外,为什么您的抖动模式是 4bpp? (2^4 = 16 个值)。

看来你的老师没有很好地解释事情。

关于c++ - 有序颜色抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40007137/

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