removed clang-format and clang_complete files from the repo
[dosdemo] / tools / img2bin / img2bin.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include "imago2.h"
7
8 int proc_image(const char *fname);
9
10 int main(int argc, char **argv)
11 {
12         int i;
13
14         for(i=1; i<argc; i++) {
15                 if(proc_image(argv[i]) == -1) {
16                         return 1;
17                 }
18         }
19         return 0;
20 }
21
22 int proc_image(const char *fname)
23 {
24         int i, xsz, ysz, npixels, len;
25         unsigned char *pixels24, *sptr;
26         uint16_t *pixels16, *dptr;
27         char *outfname, *suffix;
28         FILE *out;
29
30         len = strlen(fname);
31         outfname = alloca(len + 4);
32         memcpy(outfname, fname, len + 1);
33         if((suffix = strrchr(outfname, '.')) && suffix > outfname) {
34                 strcpy(suffix, ".img");
35         } else {
36                 strcpy(outfname + len, ".img");
37         }
38
39         if(!(pixels24 = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24))) {
40                 fprintf(stderr, "failed to load image: %s\n", fname);
41                 return -1;
42         }
43         npixels = xsz * ysz;
44         if(!(pixels16 = malloc(npixels * 2))) {
45                 perror("failed to allocate output image buffer");
46                 img_free_pixels(pixels24);
47                 return -1;
48         }
49
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);
53                 free(pixels16);
54                 return -1;
55         }
56
57         sptr = pixels24;
58         dptr = pixels16;
59         for(i=0; i<npixels; i++) {
60                 int r = *sptr++ >> 3;
61                 int g = *sptr++ >> 2;
62                 int b = *sptr++ >> 3;
63                 *dptr++ = (r << 11) | (g << 5) | b;
64         }
65         img_free_pixels(pixels24);
66
67         fwrite(pixels16, 2, npixels, out);
68         fclose(out);
69         free(pixels16);
70         return 0;
71 }