#include <stdio.h>
#include <gd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
gdImagePtr image;
FILE *png;
int q=25;
int c,x,y,i,cq;
int *seen;
int mem_size;
int seen_size=0,found;
if (argc<2) {
printf("Syntax: colourCount_quant <image.jpg>\n");
exit(1);
}
png = fopen(argv[1], "rb");
if(png==NULL) {
printf("Unable to open %s\n",argv[1]);
exit(1);
}
image = gdImageCreateFromJpeg(png);
fclose(png);
mem_size=sizeof(int)*image->sx*image->sy;
seen=malloc(mem_size);
for (x=0;x<image->sx;x++) {
for (y=0;y<image->sy;y++) {
c=gdImageGetPixel(image,x,y);
cq=(256/q)*(256/q)*(gdTrueColorGetRed(c)/q)+(256/q)*(gdTrueColorGetGreen(c)/q)+gdTrueColorGetBlue(c)/q;
found=0;
for (i=0;i<seen_size;i++) {
if (seen[i]==cq) {found=1;break;}
}
if (found==0) {
seen[seen_size]=cq;
seen_size++;
}
}
}
free(seen);
printf("%s\t%d\n",argv[1],seen_size);
gdImageDestroy(image);
return 0;
}