I am currently doing a project where I have to reduce a 24bpp picture to a 3bpp image using a 4x4 ordered dither matrix. However, after doing my dithering processing, my image is only showing up about 1/3 of the way. Does anyone know what I'm doing wrong?
Source:
#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;
}
My before image is a 512x512 image, however my output image which is dithered is only a portion of the image (512x170)