8 int proc_image(const char *fname);
10 int main(int argc, char **argv)
14 for(i=1; i<argc; i++) {
15 if(proc_image(argv[i]) == -1) {
22 int proc_image(const char *fname)
24 int i, xsz, ysz, npixels, len;
25 unsigned char *pixels24, *sptr;
26 uint16_t *pixels16, *dptr;
27 char *outfname, *suffix;
31 outfname = alloca(len + 4);
32 memcpy(outfname, fname, len + 1);
33 if((suffix = strrchr(outfname, '.')) && suffix > outfname) {
34 strcpy(suffix, ".img");
36 strcpy(outfname + len, ".img");
39 if(!(pixels24 = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24))) {
40 fprintf(stderr, "failed to load image: %s\n", fname);
44 if(!(pixels16 = malloc(npixels * 2))) {
45 perror("failed to allocate output image buffer");
46 img_free_pixels(pixels24);
50 if(!(out = fopen(outfname, "wb"))) {
51 fprintf(stderr, "failed to open %s for writing: %s\n", outfname, strerror(errno));
52 img_free_pixels(pixels24);
59 for(i=0; i<npixels; i++) {
63 *dptr++ = (r << 11) | (g << 5) | b;
65 img_free_pixels(pixels24);
67 fwrite(pixels16, 2, npixels, out);