lbm2bin

view src/main.c @ 8:aea67378e657

crop
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Jul 2017 08:22:59 +0300
parents f5422c7609c1
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include "image.h"
8 static int proc_image(const char *fname);
9 static const char *modestr(int cmode);
10 static void output_planar(FILE *fp, struct image *img);
11 static void output_linear(FILE *fp, struct image *img);
12 static void output_interleaved(FILE *fp, struct image *img);
13 static void output_palette(FILE *fp, struct image *img, int pcol_bits);
15 enum img_mode {
16 IMG_LINEAR,
17 IMG_PLANAR,
18 IMG_INTERLEAVED
19 };
21 static enum img_mode mode = IMG_LINEAR;
22 static int pcol_bits = 8;
23 static int verbose = 0;
25 static int crop_x, crop_y;
26 static int crop_width, crop_height;
28 int main(int argc, char **argv)
29 {
30 int i;
32 for(i=1; i<argc; i++) {
33 if(argv[i][0] == '-') {
34 if(argv[i][2] == 0) {
35 switch(argv[i][1]) {
36 case '4':
37 pcol_bits = 4;
38 break;
40 case '8':
41 pcol_bits = 8;
42 break;
44 case 'p':
45 mode = IMG_PLANAR;
46 break;
48 case 'l':
49 mode = IMG_LINEAR;
50 break;
52 case 'i':
53 mode = IMG_INTERLEAVED;
54 break;
56 case 'v':
57 verbose = 1;
58 break;
60 case 'c':
61 sscanf(argv[++i], "%dx%d+%d+%d", &crop_width, &crop_height, &crop_x, &crop_y);
62 break;
64 case 'h':
65 printf("usage: %s [options] <file 1> <file 2> ... <file n>\n", argv[0]);
66 printf("Options:\n");
67 printf(" -p: output image in planar mode\n");
68 printf(" -l: output image in linear mode\n");
69 printf(" -i: output image in interleaved mode\n");
70 printf(" -8: output palette with 8bit per color channel\n");
71 printf(" -4: output palette with 4bit per color channel\n");
72 printf(" -c WxH+X+Y: crop image\n");
73 printf(" -v: verbose output\n");
74 printf(" -h: print usage and exit\n");
75 return 0;
77 default:
78 fprintf(stderr, "invalid option: %s\n", argv[i]);
79 return 1;
80 }
81 } else {
82 fprintf(stderr, "invalid option: %s\n", argv[i]);
83 }
84 } else {
85 if(proc_image(argv[i]) == -1) {
86 return 1;
87 }
88 }
89 }
90 return 0;
91 }
93 static int proc_image(const char *fname)
94 {
95 int i, cropping = 0;
96 struct image img;
97 char *outfname, *suffix;
98 FILE *fp;
100 if(load_image(&img, fname) == -1) {
101 fprintf(stderr, "failed to load image: %s\n", fname);
102 return -1;
103 }
105 if(crop_width <= 0) {
106 crop_width = img.width - crop_x;
107 crop_height = img.height - crop_y;
108 } else {
109 cropping = 1;
110 }
112 if(!(outfname = malloc(strlen(fname) + 4))) {
113 perror("failed to allocate output filename");
114 return -1;
115 }
116 strcpy(outfname, fname);
117 if(!(suffix = strrchr(outfname, '.'))) {
118 suffix = outfname + strlen(outfname);
119 }
120 strcpy(suffix, ".img");
122 if(verbose) {
123 printf("processing %s -> %s\n", fname, outfname);
124 }
126 if(!(fp = fopen(outfname, "wb"))) {
127 fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
128 destroy_image(&img);
129 free(outfname);
130 return -1;
131 }
133 if(verbose) {
134 printf(" size: %dx%d\n", img.width, img.height);
135 printf(" bits per pixel: %d (%d colors)\n", img.bpp, 1 << img.bpp);
136 if(img.num_ranges) {
137 printf(" color ranges:\n");
138 for(i=0; i<img.num_ranges; i++) {
139 printf(" [%d]: %d - %d, rate: %u, mode: %s\n", i, img.range[i].low, img.range[i].high,
140 img.range[i].rate, modestr(img.range[i].cmode));
141 }
142 }
143 if(cropping) {
144 printf(" crop: %dx%d+%d+%d\n", crop_width, crop_height, crop_x, crop_y);
145 }
146 }
148 switch(mode) {
149 case IMG_PLANAR:
150 if(verbose) printf(" writing planar pixels\n");
151 output_planar(fp, &img);
152 break;
154 case IMG_LINEAR:
155 if(verbose) printf(" writing linear pixels\n");
156 output_linear(fp, &img);
157 break;
159 case IMG_INTERLEAVED:
160 if(verbose) printf(" writing interleaved pixels\n");
161 output_interleaved(fp, &img);
162 break;
164 default:
165 break;
166 }
167 fclose(fp);
169 /* open fname.pal */
170 strcpy(outfname, fname);
171 if(!(suffix = strrchr(outfname, '.'))) {
172 suffix = outfname + strlen(outfname);
173 }
174 strcpy(suffix, ".pal");
176 if(!(fp = fopen(outfname, "wb"))) {
177 fprintf(stderr, "failed to open palette file %s for writing: %s\n",
178 outfname, strerror(errno));
179 free(outfname);
180 destroy_image(&img);
181 return -1;
182 }
184 if(verbose) {
185 printf(" writing %d bit palette colors\n", pcol_bits);
186 output_palette(fp, &img, pcol_bits);
187 }
189 fclose(fp);
190 free(outfname);
191 destroy_image(&img);
192 return 0;
193 }
195 static void output_planar(FILE *fp, struct image *img)
196 {
197 int i, x, y;
198 unsigned char acc = 0;
199 int acc_bit = 7;
200 unsigned char *pptr = img->pixels + crop_y * img->width + crop_x;
202 for(i=0; i<img->bpp; i++) {
203 for(y=0; y<crop_height; y++) {
204 for(x=0; x<crop_width; x++) {
205 int bit = img->bpp - i - 1;
206 acc |= ((pptr[i] >> bit) & 1) << acc_bit;
207 if(acc_bit-- == 0) {
208 fputc(acc, fp);
209 acc_bit = 7;
210 acc = 0;
211 pptr += img->bpp;
212 }
213 }
214 pptr += img->width - crop_width;
215 }
216 }
217 }
219 static void output_interleaved(FILE *fp, struct image *img)
220 {
221 int i, x, y;
222 unsigned char acc = 0;
223 int acc_bit = 7;
224 unsigned char *pptr = img->pixels + crop_y * img->width + crop_x;
226 for(y=0; y<crop_height; y++) {
227 for(i=0; i<img->bpp; i++) {
228 for(x=0; x<crop_width; x++) {
229 int bit = img->bpp - i - 1;
230 acc |= ((pptr[i] >> bit) & 1) << acc_bit;
231 if(acc_bit-- == 0) {
232 fputc(acc, fp);
233 acc_bit = 7;
234 acc = 0;
235 pptr += img->bpp;
236 }
237 }
238 pptr += img->width - crop_width;
239 }
240 }
241 }
243 static void output_linear(FILE *fp, struct image *img)
244 {
245 int i, j;
246 unsigned char *pptr = img->pixels;
248 for(i=0; i<crop_height; i++) {
249 for(j=0; j<crop_width; j++) {
250 fputc(*pptr++, fp);
251 }
252 pptr += img->width - crop_width;
253 }
254 }
256 static void output_palette(FILE *fp, struct image *img, int pcol_bits)
257 {
258 int i, num_colors;
260 num_colors = 1 << img->bpp;
262 assert(pcol_bits == 8 || pcol_bits == 4);
264 if(pcol_bits == 8) {
265 for(i=0; i<num_colors; i++) {
266 fputc(img->palette[i].r, fp);
267 fputc(img->palette[i].g, fp);
268 fputc(img->palette[i].b, fp);
269 }
270 } else {
271 /* nibble colors */
272 for(i=0; i<num_colors; i++) {
273 fputc(img->palette[i].b | (img->palette[i].g << 4), fp);
274 fputc(img->palette[i].r, fp);
275 }
276 }
277 }
279 static const char *modestr(int cmode)
280 {
281 switch(cmode) {
282 case CYCLE_NORMAL:
283 return "forward";
284 case CYCLE_REVERSE:
285 return "reverse";
286 case CYCLE_PINGPONG:
287 return "ping-pong";
288 case CYCLE_SINE_HALF:
289 return "half-sine";
290 case CYCLE_SINE:
291 return "sine";
292 default:
293 break;
294 }
295 return "unknown";
296 }