/* downsample.c - Don Yang (uguu.org) 11/30/01 */ #include #include #include #include static /*@null@*/char **LoadImage(FILE *infile, int width, int height); static char DownSample(char a, char b, char c, char d); /******************************************************************** main */ int main(int argc, char **argv) { FILE *infile, *outfile; int width, height, x, y; char **src; if( argc < 5 ) return printf("%s \n", *argv); width = atoi(argv[1]); height = atoi(argv[2]); if( width < 2 || height < 2 || (width & 1) != 0 || (height & 1) != 0 ) return printf("Invalid input size: %d x %d\n", width, height); if( (infile = fopen(argv[3], "rt")) == NULL ) return printf("Can not open %s\n", argv[3]); if( (outfile = fopen(argv[4], "wt+")) == NULL ) { (void)fclose(infile); return printf("Can not create %s\n", argv[4]); } if( (src = LoadImage(infile, width, height)) == NULL ) { (void)fclose(infile); (void)fclose(outfile); return 1; } for(y = 0; y < height; y += 2) { for(x = 0; x < width; x += 2) { (void)fputc(DownSample(src[y ][x], src[y ][x+1], src[y+1][x], src[y+1][x+1]), outfile); } (void)fputc('\n', outfile); } (void)fclose(infile); (void)fclose(outfile); for(y = 0; y < height; free(src[y++])); free(src); return 0; } /* main() */ /************************************************************** DownSample */ static char DownSample(char a, char b, char c, char d) { unsigned int block; block = (((unsigned int)a) << 24) | (((unsigned int)b) << 16) | (((unsigned int)c) << 8) | ((unsigned int)d); switch( block ) { case 0x58585858: return '%'; case 0x58585820: case 0x58582058: case 0x58205858: case 0x20585858: return 'X'; case 0x58582020: return '\"'; case 0x58202058: case 0x20585820: case 0x20205858: return 'x'; case 0x58205820: case 0x20582058: return ':'; case 0x58202020: return '\''; case 0x20582020: return '`'; case 0x20205820: return '.'; case 0x20202058: return ','; default: break; } return ' '; } /* DownSample() */ /*************************************************************** LoadImage */ static char **LoadImage(FILE *infile, int width, int height) { char **image; int i, j; if( (image = (char**)malloc(height * sizeof(char*))) == NULL ) { (void)puts("Not enough memory\n"); return NULL; } for(i = 0; i < height; i++) { if( (image[i] = (char*)malloc((size_t)(width + 2))) == NULL ) { for(j = 0; j <= i; free(image[j++])); free(image); (void)puts("Not enough memory\n"); return NULL; } memset(image[i], 32, (size_t)width); if( fgets(image[i], width + 2, infile) == NULL ) { for(j = 0; j <= i; free(image[j++])); free(image); (void)puts("Read error\n"); return NULL; } } return image; } /* LoadImage() */