#include #include #include #include #include int convert(unsigned char *infile, unsigned char *outfile) { int inf, outf; unsigned char inpix[3]; unsigned char outpix[2]; unsigned int pix; unsigned char red, green, blue; printf("Opening input file %s: ", infile); inf = open(infile, O_RDONLY); if(inf < 1) { printf("ERROR: can't open input file\r\n"); return -1; } printf("OK\r\n"); printf("Opening output file %s: ", outfile); outf = open(outfile, O_WRONLY|O_CREAT); if(outf < 1) { printf("ERROR: can't open output file\r\n"); return -1; } printf("OK\r\n"); while(read(inf, inpix, 3) == 3) { red = inpix[0] >> 3; green = inpix[1] >> 2; blue = inpix[2] >> 3; pix = red; pix <<= 6; pix |= green; pix <<= 5; pix |= blue; outpix[0] = (pix & 0x000000FF); outpix[1] = ((pix & 0x0000FF00) >> 8); write(outf, outpix, 2); } close(inf); close(outf); return 0; } int main(int argc, char **argv) { if (argc<3) { printf("Usage: %s \r\n",argv[0]); printf(" is a raw RGB file, 8 bit per value, 24 bits per pixel.\r\n"); printf(" is the resulting packed RGB file with 16 bits per pixel\r\n"); exit(1); } return convert(argv[1], argv[2]); }