lbm2bin

view src/main.c @ 0:bfd8aa2ca393

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 15 Jul 2017 13:26:32 +0300
parents
children 0b96363742d3
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "image.h"
7 static int proc_image(const char *fname);
8 static const char *modestr(int cmode);
9 static void output_planar(FILE *fp, struct image *img);
10 static void output_chunky(FILE *fp, struct image *img);
12 static int planar = 0;
14 int main(int argc, char **argv)
15 {
16 int i;
18 for(i=1; i<argc; i++) {
19 if(argv[i][0] == '-') {
20 if(argv[i][2] == 0) {
21 switch(argv[i][1]) {
22 case 'p':
23 planar = 1;
24 break;
26 case 'c':
27 planar = 0;
28 break;
30 case 'h':
31 printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]);
32 printf("Options:\n");
33 printf(" -p: output image in planar format (default)\n");
34 printf(" -c: output image in chunky format\n");
35 printf(" -h: print usage and exit\n");
36 return 0;
38 default:
39 fprintf(stderr, "invalid option: %s\n", argv[i]);
40 return 1;
41 }
42 } else {
43 fprintf(stderr, "invalid option: %s\n", argv[i]);
44 }
45 } else {
46 if(proc_image(argv[i]) == -1) {
47 return 1;
48 }
49 }
50 }
51 return 0;
52 }
54 static int proc_image(const char *fname)
55 {
56 int i;
57 struct image img;
58 char *outfname, *suffix;
59 FILE *fp;
61 if(load_image(&img, fname) == -1) {
62 fprintf(stderr, "failed to load image: %s\n", fname);
63 return -1;
64 }
66 if(!(outfname = malloc(strlen(fname) + 4))) {
67 perror("failed to allocate output filename");
68 return -1;
69 }
70 strcpy(outfname, fname);
71 if(!(suffix = strrchr(outfname, '.'))) {
72 suffix = outfname + strlen(outfname);
73 }
74 strcpy(suffix, ".bin");
76 printf("processing %s -> %s\n", fname, outfname);
78 if(!(fp = fopen(outfname, "wb"))) {
79 fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
80 destroy_image(&img);
81 free(outfname);
82 return -1;
83 }
85 printf(" bits per pixel: %d\n", img.bpp);
86 if(img.num_ranges) {
87 printf(" color ranges:\n");
88 for(i=0; i<img.num_ranges; i++) {
89 printf(" [%d]: %d - %d, rate: %u, mode: %s\n", i, img.range[i].low, img.range[i].high,
90 img.range[i].rate, modestr(img.range[i].cmode));
91 }
92 }
94 if(planar) {
95 output_planar(fp, &img);
96 } else {
97 output_chunky(fp, &img);
98 }
100 free(outfname);
101 fclose(fp);
102 destroy_image(&img);
103 return 0;
104 }
106 static void output_planar(FILE *fp, struct image *img)
107 {
108 int i, x, y;
109 unsigned char acc = 0;
110 int acc_bit = 7;
111 unsigned char *pptr = img->pixels;
113 for(i=0; i<img->bpp; i++) {
114 for(y=0; y<img->height; y++) {
115 for(x=0; x<img->width; x++) {
116 acc |= (((*pptr++ >> i) & 1) << acc_bit);
117 if(--acc_bit == 0) {
118 fputc(acc, fp);
119 acc_bit = 7;
120 acc = 0;
121 }
122 }
123 }
124 }
125 }
127 static void output_chunky(FILE *fp, struct image *img)
128 {
129 int i, j;
130 unsigned char *pptr = img->pixels;
132 for(i=0; i<img->height; i++) {
133 for(j=0; j<img->width; j++) {
134 fputc(*pptr++, fp);
135 }
136 }
137 }
139 static const char *modestr(int cmode)
140 {
141 switch(cmode) {
142 case CYCLE_NORMAL:
143 return "forward";
144 case CYCLE_REVERSE:
145 return "reverse";
146 case CYCLE_PINGPONG:
147 return "ping-pong";
148 case CYCLE_SINE_HALF:
149 return "half-sine";
150 case CYCLE_SINE:
151 return "sine";
152 default:
153 break;
154 }
155 return "unknown";
156 }