blob: d9b33faef997e5b4182626c66f68b9cd65526e38 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2** Easylogo TGA->header converter
3** ==============================
4** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
5** AIRVENT SAM s.p.a - RIMINI(ITALY)
Mike Frysinger24113a42008-12-30 03:15:38 -05006** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
wdenkfe8c2802002-11-03 00:38:21 +00007**
8** This is still under construction!
9*/
10
Mike Frysinger24113a42008-12-30 03:15:38 -050011#include <errno.h>
Mike Frysingeredfed1d2008-02-16 02:40:18 -050012#include <getopt.h>
13#include <stdbool.h>
wdenkfe8c2802002-11-03 00:38:21 +000014#include <stdio.h>
Mike Frysinger38d299c2007-12-18 03:23:25 -050015#include <stdlib.h>
16#include <string.h>
Mike Frysinger24113a42008-12-30 03:15:38 -050017#include <unistd.h>
18#include <sys/stat.h>
wdenkfe8c2802002-11-03 00:38:21 +000019
20#pragma pack(1)
21
22/*#define ENABLE_ASCII_BANNERS */
23
24typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010025 unsigned char id;
26 unsigned char ColorMapType;
27 unsigned char ImageTypeCode;
28 unsigned short ColorMapOrigin;
29 unsigned short ColorMapLenght;
30 unsigned char ColorMapEntrySize;
31 unsigned short ImageXOrigin;
32 unsigned short ImageYOrigin;
33 unsigned short ImageWidth;
34 unsigned short ImageHeight;
35 unsigned char ImagePixelSize;
36 unsigned char ImageDescriptorByte;
wdenkfe8c2802002-11-03 00:38:21 +000037} tga_header_t;
38
39typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010040 unsigned char r, g, b;
41} rgb_t;
wdenkfe8c2802002-11-03 00:38:21 +000042
43typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010044 unsigned char b, g, r;
45} bgr_t;
wdenkfe8c2802002-11-03 00:38:21 +000046
47typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010048 unsigned char Cb, y1, Cr, y2;
49} yuyv_t;
wdenkfe8c2802002-11-03 00:38:21 +000050
51typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010052 void *data, *palette;
53 int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
54} image_t;
wdenkfe8c2802002-11-03 00:38:21 +000055
Mike Frysinger24113a42008-12-30 03:15:38 -050056void *xmalloc (size_t size)
57{
58 void *ret = malloc (size);
59 if (!ret) {
60 fprintf (stderr, "\nerror: malloc(%zu) failed: %s",
61 size, strerror(errno));
62 exit (1);
63 }
64 return ret;
65}
66
wdenkfe8c2802002-11-03 00:38:21 +000067void StringUpperCase (char *str)
68{
Wolfgang Denk6007f322008-01-09 15:14:46 +010069 int count = strlen (str);
70 char c;
wdenkfe8c2802002-11-03 00:38:21 +000071
Wolfgang Denk6007f322008-01-09 15:14:46 +010072 while (count--) {
73 c = *str;
74 if ((c >= 'a') && (c <= 'z'))
75 *str = 'A' + (c - 'a');
76 str++;
77 }
wdenkfe8c2802002-11-03 00:38:21 +000078}
79
80void StringLowerCase (char *str)
81{
Wolfgang Denk6007f322008-01-09 15:14:46 +010082 int count = strlen (str);
83 char c;
wdenkfe8c2802002-11-03 00:38:21 +000084
Wolfgang Denk6007f322008-01-09 15:14:46 +010085 while (count--) {
86 c = *str;
87 if ((c >= 'A') && (c <= 'Z'))
88 *str = 'a' + (c - 'A');
89 str++;
90 }
wdenkfe8c2802002-11-03 00:38:21 +000091}
Wolfgang Denk6007f322008-01-09 15:14:46 +010092void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
wdenkfe8c2802002-11-03 00:38:21 +000093{
Wolfgang Denk6007f322008-01-09 15:14:46 +010094 unsigned int pR, pG, pB;
wdenkfe8c2802002-11-03 00:38:21 +000095
Wolfgang Denk6007f322008-01-09 15:14:46 +010096 /* Transform (0-255) components to (0-100) */
97 pR = rgb_pixel->r * 100 / 255;
98 pG = rgb_pixel->g * 100 / 255;
99 pB = rgb_pixel->b * 100 / 255;
wdenkfe8c2802002-11-03 00:38:21 +0000100
Wolfgang Denk6007f322008-01-09 15:14:46 +0100101 /* Calculate YUV values (0-255) from RGB beetween 0-100 */
102 yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16;
103 yuyv_pixel->Cb = pB - (pR / 4) - (pG * 3 / 4) + 128;
104 yuyv_pixel->Cr = pR - (pG * 3 / 4) - (pB / 4) + 128;
wdenkfe8c2802002-11-03 00:38:21 +0000105
Wolfgang Denk6007f322008-01-09 15:14:46 +0100106 return;
wdenkfe8c2802002-11-03 00:38:21 +0000107}
108
Wolfgang Denk6007f322008-01-09 15:14:46 +0100109void printlogo_rgb (rgb_t * data, int w, int h)
wdenkfe8c2802002-11-03 00:38:21 +0000110{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100111 int x, y;
112
113 for (y = 0; y < h; y++) {
114 for (x = 0; x < w; x++, data++)
115 if ((data->r <
116 30) /*&&(data->g == 0)&&(data->b == 0) */ )
117 printf (" ");
118 else
119 printf ("X");
120 printf ("\n");
121 }
wdenkfe8c2802002-11-03 00:38:21 +0000122}
123
124void printlogo_yuyv (unsigned short *data, int w, int h)
125{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100126 int x, y;
127
128 for (y = 0; y < h; y++) {
129 for (x = 0; x < w; x++, data++)
130 if (*data == 0x1080) /* Because of inverted on i386! */
131 printf (" ");
132 else
133 printf ("X");
134 printf ("\n");
135 }
wdenkfe8c2802002-11-03 00:38:21 +0000136}
137
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500138static inline unsigned short le16_to_cpu (unsigned short val)
139{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100140 union {
141 unsigned char pval[2];
142 unsigned short val;
143 } swapped;
144
145 swapped.val = val;
146 return (swapped.pval[1] << 8) + swapped.pval[0];
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500147}
148
Wolfgang Denk6007f322008-01-09 15:14:46 +0100149int image_load_tga (image_t * image, char *filename)
wdenkfe8c2802002-11-03 00:38:21 +0000150{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100151 FILE *file;
152 tga_header_t header;
153 int i;
154 unsigned char app;
155 rgb_t *p;
wdenkfe8c2802002-11-03 00:38:21 +0000156
Wolfgang Denk6007f322008-01-09 15:14:46 +0100157 if ((file = fopen (filename, "rb")) == NULL)
158 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000159
Wolfgang Denk6007f322008-01-09 15:14:46 +0100160 fread (&header, sizeof (header), 1, file);
wdenkfe8c2802002-11-03 00:38:21 +0000161
Wolfgang Denk6007f322008-01-09 15:14:46 +0100162 /* byte swap: tga is little endian, host is ??? */
163 header.ColorMapOrigin = le16_to_cpu (header.ColorMapOrigin);
164 header.ColorMapLenght = le16_to_cpu (header.ColorMapLenght);
165 header.ImageXOrigin = le16_to_cpu (header.ImageXOrigin);
166 header.ImageYOrigin = le16_to_cpu (header.ImageYOrigin);
167 header.ImageWidth = le16_to_cpu (header.ImageWidth);
168 header.ImageHeight = le16_to_cpu (header.ImageHeight);
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500169
Wolfgang Denk6007f322008-01-09 15:14:46 +0100170 image->width = header.ImageWidth;
171 image->height = header.ImageHeight;
wdenkfe8c2802002-11-03 00:38:21 +0000172
Wolfgang Denk6007f322008-01-09 15:14:46 +0100173 switch (header.ImageTypeCode) {
174 case 2: /* Uncompressed RGB */
175 image->yuyv = 0;
176 image->palette_size = 0;
177 image->palette = NULL;
178 break;
wdenkfe8c2802002-11-03 00:38:21 +0000179
180 default:
Wolfgang Denk6007f322008-01-09 15:14:46 +0100181 printf ("Format not supported!\n");
182 return -1;
183 }
wdenkfe8c2802002-11-03 00:38:21 +0000184
Wolfgang Denk6007f322008-01-09 15:14:46 +0100185 image->bpp = header.ImagePixelSize;
186 image->pixel_size = ((image->bpp - 1) / 8) + 1;
187 image->pixels = image->width * image->height;
188 image->size = image->pixels * image->pixel_size;
Mike Frysinger24113a42008-12-30 03:15:38 -0500189 image->data = xmalloc (image->size);
wdenkfe8c2802002-11-03 00:38:21 +0000190
Wolfgang Denk6007f322008-01-09 15:14:46 +0100191 if (image->bpp != 24) {
192 printf ("Bpp not supported: %d!\n", image->bpp);
193 return -1;
194 }
wdenkfe8c2802002-11-03 00:38:21 +0000195
Wolfgang Denk6007f322008-01-09 15:14:46 +0100196 fread (image->data, image->size, 1, file);
wdenkfe8c2802002-11-03 00:38:21 +0000197
198/* Swapping R and B values */
199
Wolfgang Denk6007f322008-01-09 15:14:46 +0100200 p = image->data;
201 for (i = 0; i < image->pixels; i++, p++) {
202 app = p->r;
203 p->r = p->b;
204 p->b = app;
205 }
wdenkfe8c2802002-11-03 00:38:21 +0000206
207/* Swapping image */
208
Wolfgang Denk6007f322008-01-09 15:14:46 +0100209 if (!(header.ImageDescriptorByte & 0x20)) {
Mike Frysinger24113a42008-12-30 03:15:38 -0500210 unsigned char *temp = xmalloc (image->size);
Wolfgang Denk6007f322008-01-09 15:14:46 +0100211 int linesize = image->pixel_size * image->width;
212 void *dest = image->data,
213 *source = temp + image->size - linesize;
wdenkfe8c2802002-11-03 00:38:21 +0000214
Wolfgang Denk6007f322008-01-09 15:14:46 +0100215 printf ("S");
216 if (temp == NULL) {
217 printf ("Cannot alloc temp buffer!\n");
218 return -1;
219 }
220
221 memcpy (temp, image->data, image->size);
222 for (i = 0; i < image->height;
223 i++, dest += linesize, source -= linesize)
224 memcpy (dest, source, linesize);
225
226 free (temp);
wdenkfe8c2802002-11-03 00:38:21 +0000227 }
wdenkfe8c2802002-11-03 00:38:21 +0000228#ifdef ENABLE_ASCII_BANNERS
Wolfgang Denk6007f322008-01-09 15:14:46 +0100229 printlogo_rgb (image->data, image->width, image->height);
wdenkfe8c2802002-11-03 00:38:21 +0000230#endif
231
Wolfgang Denk6007f322008-01-09 15:14:46 +0100232 fclose (file);
233 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000234}
235
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500236void image_free (image_t * image)
wdenkfe8c2802002-11-03 00:38:21 +0000237{
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500238 free (image->data);
239 free (image->palette);
wdenkfe8c2802002-11-03 00:38:21 +0000240}
241
Wolfgang Denk6007f322008-01-09 15:14:46 +0100242int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
wdenkfe8c2802002-11-03 00:38:21 +0000243{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100244 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
245 yuyv_t yuyv;
246 unsigned short *dest;
247 int count = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000248
Wolfgang Denk6007f322008-01-09 15:14:46 +0100249 yuyv_image->pixel_size = 2;
250 yuyv_image->bpp = 16;
251 yuyv_image->yuyv = 1;
252 yuyv_image->width = rgb_image->width;
253 yuyv_image->height = rgb_image->height;
254 yuyv_image->pixels = yuyv_image->width * yuyv_image->height;
255 yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size;
256 dest = (unsigned short *) (yuyv_image->data =
Mike Frysinger24113a42008-12-30 03:15:38 -0500257 xmalloc (yuyv_image->size));
Wolfgang Denk6007f322008-01-09 15:14:46 +0100258 yuyv_image->palette = 0;
259 yuyv_image->palette_size = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000260
Wolfgang Denk6007f322008-01-09 15:14:46 +0100261 while ((count++) < rgb_image->pixels) {
wdenkfe8c2802002-11-03 00:38:21 +0000262 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
263
Wolfgang Denk6007f322008-01-09 15:14:46 +0100264 if ((count & 1) == 0) /* Was == 0 */
265 memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
wdenkfe8c2802002-11-03 00:38:21 +0000266 else
Wolfgang Denk6007f322008-01-09 15:14:46 +0100267 memcpy (dest, (void *) &yuyv, sizeof (short));
wdenkfe8c2802002-11-03 00:38:21 +0000268
Wolfgang Denk6007f322008-01-09 15:14:46 +0100269 dest++;
wdenkfe8c2802002-11-03 00:38:21 +0000270 }
271
272#ifdef ENABLE_ASCII_BANNERS
Wolfgang Denk6007f322008-01-09 15:14:46 +0100273 printlogo_yuyv (yuyv_image->data, yuyv_image->width,
274 yuyv_image->height);
wdenkfe8c2802002-11-03 00:38:21 +0000275#endif
Wolfgang Denk6007f322008-01-09 15:14:46 +0100276 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000277}
278
Michael Hennerich7293e052009-12-19 08:19:09 -0500279int image_rgb888_to_rgb565(image_t *rgb888_image, image_t *rgb565_image)
280{
281 rgb_t *rgb_ptr = (rgb_t *) rgb888_image->data;
282 unsigned short *dest;
283 int count = 0;
284
285 rgb565_image->pixel_size = 2;
286 rgb565_image->bpp = 16;
287 rgb565_image->yuyv = 0;
288 rgb565_image->width = rgb888_image->width;
289 rgb565_image->height = rgb888_image->height;
290 rgb565_image->pixels = rgb565_image->width * rgb565_image->height;
291 rgb565_image->size = rgb565_image->pixels * rgb565_image->pixel_size;
292 dest = (unsigned short *) (rgb565_image->data =
293 xmalloc(rgb565_image->size));
294 rgb565_image->palette = 0;
295 rgb565_image->palette_size = 0;
296
297 while ((count++) < rgb888_image->pixels) {
298
299 *dest++ = ((rgb_ptr->b & 0xF8) << 8) |
300 ((rgb_ptr->g & 0xFC) << 3) |
301 (rgb_ptr->r >> 3);
302 rgb_ptr++;
303 }
304
305 return 0;
306}
307
Mike Frysinger24113a42008-12-30 03:15:38 -0500308int use_gzip = 0;
309
Wolfgang Denk6007f322008-01-09 15:14:46 +0100310int image_save_header (image_t * image, char *filename, char *varname)
wdenkfe8c2802002-11-03 00:38:21 +0000311{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100312 FILE *file = fopen (filename, "w");
313 char app[256], str[256] = "", def_name[64];
314 int count = image->size, col = 0;
315 unsigned char *dataptr = image->data;
wdenkfe8c2802002-11-03 00:38:21 +0000316
Wolfgang Denk6007f322008-01-09 15:14:46 +0100317 if (file == NULL)
318 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000319
Wolfgang Denk6007f322008-01-09 15:14:46 +0100320 /* Author information */
321 fprintf (file,
322 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
323 fprintf (file,
324 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
325 varname);
326 fprintf (file,
327 " * Where:\t'screen'\tis the pointer to the frame buffer\n");
328 fprintf (file, " *\t\t'width'\tis the screen width\n");
329 fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
330 fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
331
Mike Frysinger24113a42008-12-30 03:15:38 -0500332 /* gzip compress */
333 if (use_gzip & 0x1) {
334 const char *errstr = NULL;
335 unsigned char *compressed;
336 struct stat st;
337 FILE *gz;
338 char *gzfilename = xmalloc(strlen (filename) + 20);
339 char *gzcmd = xmalloc(strlen (filename) + 20);
340
341 sprintf (gzfilename, "%s.gz", filename);
342 sprintf (gzcmd, "gzip > %s", gzfilename);
343 gz = popen (gzcmd, "w");
344 if (!gz) {
345 errstr = "\nerror: popen() failed";
346 goto done;
347 }
348 if (fwrite (image->data, image->size, 1, gz) != 1) {
349 errstr = "\nerror: writing data to gzip failed";
350 goto done;
351 }
352 if (pclose (gz)) {
353 errstr = "\nerror: gzip process failed";
354 goto done;
355 }
356
357 gz = fopen (gzfilename, "r");
358 if (!gz) {
359 errstr = "\nerror: open() on gzip data failed";
360 goto done;
361 }
362 if (stat (gzfilename, &st)) {
363 errstr = "\nerror: stat() on gzip file failed";
364 goto done;
365 }
366 compressed = xmalloc (st.st_size);
367 if (fread (compressed, st.st_size, 1, gz) != 1) {
368 errstr = "\nerror: reading gzip data failed";
369 goto done;
370 }
371 fclose (gz);
372
373 unlink (gzfilename);
374
375 dataptr = compressed;
376 count = st.st_size;
377 fprintf (file, "#define EASYLOGO_ENABLE_GZIP %i\n\n", count);
378 if (use_gzip & 0x2)
379 fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);
380
381 done:
382 free (gzfilename);
383 free (gzcmd);
384
385 if (errstr) {
386 perror (errstr);
387 return -1;
388 }
389 }
390
Wolfgang Denk6007f322008-01-09 15:14:46 +0100391 /* Headers */
392 fprintf (file, "#include <video_easylogo.h>\n\n");
393 /* Macros */
394 strcpy (def_name, varname);
wdenkfe8c2802002-11-03 00:38:21 +0000395 StringUpperCase (def_name);
Wolfgang Denk6007f322008-01-09 15:14:46 +0100396 fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
397 image->width);
398 fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
399 image->height);
400 fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
401 image->pixels);
402 fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
403 fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name,
404 image->pixel_size);
405 fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
406 image->size);
407 /* Declaration */
Mike Frysinger24113a42008-12-30 03:15:38 -0500408 fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
409 def_name);
wdenkfe8c2802002-11-03 00:38:21 +0000410
Wolfgang Denk6007f322008-01-09 15:14:46 +0100411 /* Data */
412 while (count)
413 switch (col) {
414 case 0:
415 sprintf (str, " 0x%02x", *dataptr++);
416 col++;
417 count--;
418 break;
wdenkfe8c2802002-11-03 00:38:21 +0000419
Wolfgang Denk6007f322008-01-09 15:14:46 +0100420 case 16:
421 fprintf (file, "%s", str);
422 if (count > 0)
423 fprintf (file, ",");
424 fprintf (file, "\n");
wdenkfe8c2802002-11-03 00:38:21 +0000425
Wolfgang Denk6007f322008-01-09 15:14:46 +0100426 col = 0;
427 break;
wdenkfe8c2802002-11-03 00:38:21 +0000428
Wolfgang Denk6007f322008-01-09 15:14:46 +0100429 default:
430 strcpy (app, str);
431 sprintf (str, "%s, 0x%02x", app, *dataptr++);
432 col++;
433 count--;
434 break;
wdenkfe8c2802002-11-03 00:38:21 +0000435 }
436
437 if (col)
Wolfgang Denk6007f322008-01-09 15:14:46 +0100438 fprintf (file, "%s\n", str);
wdenkfe8c2802002-11-03 00:38:21 +0000439
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200440 /* End of declaration */
Wolfgang Denk6007f322008-01-09 15:14:46 +0100441 fprintf (file, "};\n\n");
442 /* Variable */
443 fprintf (file, "fastimage_t %s = {\n", varname);
444 fprintf (file, " DEF_%s_DATA,\n", def_name);
445 fprintf (file, " DEF_%s_WIDTH,\n", def_name);
446 fprintf (file, " DEF_%s_HEIGHT,\n", def_name);
447 fprintf (file, " DEF_%s_BPP,\n", def_name);
448 fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name);
449 fprintf (file, " DEF_%s_SIZE\n};\n", def_name);
wdenkfe8c2802002-11-03 00:38:21 +0000450
451 fclose (file);
452
Wolfgang Denk6007f322008-01-09 15:14:46 +0100453 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000454}
455
456#define DEF_FILELEN 256
457
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500458static void usage (int exit_status)
459{
460 puts (
461 "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
462 "\n"
463 "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n"
464 "\n"
465 "Options:\n"
Michael Hennerich7293e052009-12-19 08:19:09 -0500466 " -r Output RGB888 instead of YUYV\n"
467 " -s Output RGB565 instead of YUYV\n"
Mike Frysinger24113a42008-12-30 03:15:38 -0500468 " -g Compress with gzip\n"
469 " -b Preallocate space in bss for decompressing image\n"
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500470 " -h Help output\n"
471 "\n"
472 "Where: 'inputfile' is the TGA image to load\n"
473 " 'outputvar' is the variable name to create\n"
474 " 'outputfile' is the output header file (default is 'inputfile.h')"
475 );
476 exit (exit_status);
477}
478
wdenkfe8c2802002-11-03 00:38:21 +0000479int main (int argc, char *argv[])
480{
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500481 int c;
Michael Hennerich7293e052009-12-19 08:19:09 -0500482 bool use_rgb888 = false;
483 bool use_rgb565 = false;
Wolfgang Denk6007f322008-01-09 15:14:46 +0100484 char inputfile[DEF_FILELEN],
485 outputfile[DEF_FILELEN], varname[DEF_FILELEN];
wdenkfe8c2802002-11-03 00:38:21 +0000486
Michael Hennerich7293e052009-12-19 08:19:09 -0500487 image_t rgb888_logo, rgb565_logo, yuyv_logo;
wdenkfe8c2802002-11-03 00:38:21 +0000488
Michael Hennerich7293e052009-12-19 08:19:09 -0500489 while ((c = getopt(argc, argv, "hrsgb")) > 0) {
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500490 switch (c) {
491 case 'h':
492 usage (0);
493 break;
494 case 'r':
Michael Hennerich7293e052009-12-19 08:19:09 -0500495 use_rgb888 = true;
496 puts("Using 24-bit RGB888 Output Fromat");
497 break;
498 case 's':
499 use_rgb565 = true;
500 puts("Using 16-bit RGB565 Output Fromat");
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500501 break;
Mike Frysinger24113a42008-12-30 03:15:38 -0500502 case 'g':
503 use_gzip |= 0x1;
504 puts ("Compressing with gzip");
505 break;
506 case 'b':
507 use_gzip |= 0x2;
508 puts ("Preallocating bss space for decompressing image");
509 break;
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500510 default:
511 usage (1);
512 break;
Wolfgang Denk6007f322008-01-09 15:14:46 +0100513 }
wdenkfe8c2802002-11-03 00:38:21 +0000514 }
515
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500516 c = argc - optind;
517 if (c > 4 || c < 1)
518 usage (1);
519
520 strcpy (inputfile, argv[optind]);
521
522 if (c > 1)
523 strcpy (varname, argv[optind + 1]);
524 else {
525 /* transform "input.tga" to just "input" */
526 char *dot;
527 strcpy (varname, inputfile);
528 dot = strchr (varname, '.');
529 if (dot)
530 *dot = '\0';
531 }
532
533 if (c > 2)
534 strcpy (outputfile, argv[optind + 2]);
535 else {
536 /* just append ".h" to input file name */
537 strcpy (outputfile, inputfile);
538 strcat (outputfile, ".h");
539 }
540
541 /* Make sure the output is sent as soon as we printf() */
542 setbuf(stdout, NULL);
543
Wolfgang Denk6007f322008-01-09 15:14:46 +0100544 printf ("Doing '%s' (%s) from '%s'...",
545 outputfile, varname, inputfile);
wdenkfe8c2802002-11-03 00:38:21 +0000546
Wolfgang Denk6007f322008-01-09 15:14:46 +0100547 /* Import TGA logo */
wdenkfe8c2802002-11-03 00:38:21 +0000548
Wolfgang Denk6007f322008-01-09 15:14:46 +0100549 printf ("L");
Michael Hennerich7293e052009-12-19 08:19:09 -0500550 if (image_load_tga(&rgb888_logo, inputfile) < 0) {
Wolfgang Denk6007f322008-01-09 15:14:46 +0100551 printf ("input file not found!\n");
552 exit (1);
wdenkfe8c2802002-11-03 00:38:21 +0000553 }
wdenkfe8c2802002-11-03 00:38:21 +0000554
Michael Hennerich7293e052009-12-19 08:19:09 -0500555 /* Convert, save, and free the image */
wdenkfe8c2802002-11-03 00:38:21 +0000556
Michael Hennerich7293e052009-12-19 08:19:09 -0500557 if (!use_rgb888 && !use_rgb565) {
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500558 printf ("C");
Michael Hennerich7293e052009-12-19 08:19:09 -0500559 image_rgb_to_yuyv(&rgb888_logo, &yuyv_logo);
560
561 printf("S");
562 image_save_header(&yuyv_logo, outputfile, varname);
563 image_free(&yuyv_logo);
564 } else if (use_rgb565) {
565 printf("C");
566 image_rgb888_to_rgb565(&rgb888_logo, &rgb565_logo);
567
568 printf("S");
569 image_save_header(&rgb565_logo, outputfile, varname);
570 image_free(&rgb565_logo);
571 } else {
572 printf("S");
573 image_save_header(&rgb888_logo, outputfile, varname);
Mike Frysingeredfed1d2008-02-16 02:40:18 -0500574 }
wdenkfe8c2802002-11-03 00:38:21 +0000575
Wolfgang Denk6007f322008-01-09 15:14:46 +0100576 /* Free original image and copy */
wdenkfe8c2802002-11-03 00:38:21 +0000577
Michael Hennerich7293e052009-12-19 08:19:09 -0500578 image_free(&rgb888_logo);
wdenkfe8c2802002-11-03 00:38:21 +0000579
Wolfgang Denk6007f322008-01-09 15:14:46 +0100580 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +0000581
Wolfgang Denk6007f322008-01-09 15:14:46 +0100582 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000583}