#include #include #include #include unsigned short PixelCreate(int r, int g, int b) { r >>= 3; g >>= 3; b >>= 3; return ((r & 31) << 10) | ((g & 31) << 5) | ((b & 31) << 0) ; } int PPMRead(FILE* file) { char s[256]; fscanf(file, "%s", s); while (s[0] == '#') fscanf(file, "%[^\n]%c%s", s, s, s); return atoi(s); } void main(int argc, char* argv[]) { char full_path[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; char s[256]; int i, x, y, r, g, b, width, height, depth; unsigned short pixel; FILE* outfile; FILE* infile; for (i = 1; i < argc; i++) { _splitpath(argv[i], drive, dir, fname, ext); _makepath(full_path, drive, dir, fname, "hic"); fprintf(stderr, "Converting: '%s' to '%s'\n", argv[i], full_path); infile = fopen(argv[i], "r"); assert(infile); outfile = fopen(full_path, "wb"); assert(outfile); fscanf(infile, "%s", s); assert(!strcmp(s, "P3")); width = PPMRead(infile); height = PPMRead(infile); depth = PPMRead(infile); assert(depth == 255); fwrite(&width , sizeof(int), 1, outfile); fwrite(&height, sizeof(int), 1, outfile); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { fscanf(infile, "%d%d%d", &r, &g, &b); pixel = PixelCreate(r, g, b); fwrite(&pixel, sizeof(short), 1, outfile); } } fclose(infile); fclose(outfile); } }