]> git.ipfire.org Git - people/ms/u-boot.git/blame - tools/easylogo/easylogo.c
mmc: Fix uninitialised priv member
[people/ms/u-boot.git] / tools / easylogo / easylogo.c
CommitLineData
fe8c2806
WD
1/*
2** Easylogo TGA->header converter
3** ==============================
4** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
5** AIRVENT SAM s.p.a - RIMINI(ITALY)
24113a44 6** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
fe8c2806
WD
7**
8** This is still under construction!
9*/
10
24113a44 11#include <errno.h>
edfed1d9
MF
12#include <getopt.h>
13#include <stdbool.h>
fe8c2806 14#include <stdio.h>
38d299c2
MF
15#include <stdlib.h>
16#include <string.h>
24113a44
MF
17#include <unistd.h>
18#include <sys/stat.h>
fe8c2806
WD
19
20#pragma pack(1)
21
22/*#define ENABLE_ASCII_BANNERS */
23
24typedef struct {
6007f325
WD
25 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;
fe8c2806
WD
37} tga_header_t;
38
39typedef struct {
6007f325
WD
40 unsigned char r, g, b;
41} rgb_t;
fe8c2806
WD
42
43typedef struct {
6007f325
WD
44 unsigned char b, g, r;
45} bgr_t;
fe8c2806
WD
46
47typedef struct {
6007f325
WD
48 unsigned char Cb, y1, Cr, y2;
49} yuyv_t;
fe8c2806
WD
50
51typedef struct {
6007f325
WD
52 void *data, *palette;
53 int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
54} image_t;
fe8c2806 55
24113a44
MF
56void *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
fe8c2806
WD
67void StringUpperCase (char *str)
68{
6007f325
WD
69 int count = strlen (str);
70 char c;
71
72 while (count--) {
73 c = *str;
74 if ((c >= 'a') && (c <= 'z'))
75 *str = 'A' + (c - 'a');
76 str++;
77 }
fe8c2806
WD
78}
79
80void StringLowerCase (char *str)
81{
6007f325
WD
82 int count = strlen (str);
83 char c;
84
85 while (count--) {
86 c = *str;
87 if ((c >= 'A') && (c <= 'Z'))
88 *str = 'a' + (c - 'A');
89 str++;
90 }
fe8c2806 91}
6007f325 92void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
fe8c2806 93{
6007f325 94 unsigned int pR, pG, pB;
fe8c2806 95
6007f325
WD
96 /* 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;
fe8c2806 100
6007f325
WD
101 /* 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;
fe8c2806 105
6007f325 106 return;
fe8c2806
WD
107}
108
6007f325 109void printlogo_rgb (rgb_t * data, int w, int h)
fe8c2806 110{
6007f325
WD
111 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 }
fe8c2806
WD
122}
123
124void printlogo_yuyv (unsigned short *data, int w, int h)
125{
6007f325
WD
126 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 }
fe8c2806
WD
136}
137
fc6414ec
MF
138static inline unsigned short le16_to_cpu (unsigned short val)
139{
6007f325
WD
140 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];
fc6414ec
MF
147}
148
6007f325 149int image_load_tga (image_t * image, char *filename)
fe8c2806 150{
6007f325
WD
151 FILE *file;
152 tga_header_t header;
153 int i;
154 unsigned char app;
155 rgb_t *p;
156
157 if ((file = fopen (filename, "rb")) == NULL)
158 return -1;
159
160 fread (&header, sizeof (header), 1, file);
161
162 /* 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);
169
170 image->width = header.ImageWidth;
171 image->height = header.ImageHeight;
172
173 switch (header.ImageTypeCode) {
174 case 2: /* Uncompressed RGB */
175 image->yuyv = 0;
176 image->palette_size = 0;
177 image->palette = NULL;
178 break;
fe8c2806
WD
179
180 default:
6007f325
WD
181 printf ("Format not supported!\n");
182 return -1;
183 }
fe8c2806 184
6007f325
WD
185 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;
24113a44 189 image->data = xmalloc (image->size);
fe8c2806 190
6007f325
WD
191 if (image->bpp != 24) {
192 printf ("Bpp not supported: %d!\n", image->bpp);
193 return -1;
194 }
fe8c2806 195
6007f325 196 fread (image->data, image->size, 1, file);
fe8c2806
WD
197
198/* Swapping R and B values */
199
6007f325
WD
200 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 }
fe8c2806
WD
206
207/* Swapping image */
208
6007f325 209 if (!(header.ImageDescriptorByte & 0x20)) {
24113a44 210 unsigned char *temp = xmalloc (image->size);
6007f325
WD
211 int linesize = image->pixel_size * image->width;
212 void *dest = image->data,
213 *source = temp + image->size - linesize;
fe8c2806 214
6007f325
WD
215 printf ("S");
216 if (temp == NULL) {
217 printf ("Cannot alloc temp buffer!\n");
218 return -1;
219 }
fe8c2806 220
6007f325
WD
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);
fe8c2806 225
6007f325
WD
226 free (temp);
227 }
fe8c2806 228#ifdef ENABLE_ASCII_BANNERS
6007f325 229 printlogo_rgb (image->data, image->width, image->height);
fe8c2806
WD
230#endif
231
6007f325
WD
232 fclose (file);
233 return 0;
fe8c2806
WD
234}
235
edfed1d9 236void image_free (image_t * image)
fe8c2806 237{
edfed1d9
MF
238 free (image->data);
239 free (image->palette);
fe8c2806
WD
240}
241
6007f325 242int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
fe8c2806 243{
6007f325
WD
244 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
245 yuyv_t yuyv;
246 unsigned short *dest;
247 int count = 0;
248
249 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 =
24113a44 257 xmalloc (yuyv_image->size));
6007f325
WD
258 yuyv_image->palette = 0;
259 yuyv_image->palette_size = 0;
260
261 while ((count++) < rgb_image->pixels) {
fe8c2806
WD
262 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
263
6007f325
WD
264 if ((count & 1) == 0) /* Was == 0 */
265 memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
fe8c2806 266 else
6007f325 267 memcpy (dest, (void *) &yuyv, sizeof (short));
fe8c2806 268
6007f325 269 dest++;
fe8c2806
WD
270 }
271
272#ifdef ENABLE_ASCII_BANNERS
6007f325
WD
273 printlogo_yuyv (yuyv_image->data, yuyv_image->width,
274 yuyv_image->height);
fe8c2806 275#endif
6007f325 276 return 0;
fe8c2806
WD
277}
278
7293e057
MH
279int 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
1a3cb4ad
MF
308enum comp_t {
309 COMP_NONE,
310 COMP_GZIP,
311 COMP_LZMA,
312};
313static enum comp_t compression = COMP_NONE;
314static bool bss_storage = false;
24113a44 315
6007f325 316int image_save_header (image_t * image, char *filename, char *varname)
fe8c2806 317{
6007f325
WD
318 FILE *file = fopen (filename, "w");
319 char app[256], str[256] = "", def_name[64];
320 int count = image->size, col = 0;
321 unsigned char *dataptr = image->data;
322
323 if (file == NULL)
324 return -1;
325
326 /* Author information */
327 fprintf (file,
328 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
329 fprintf (file,
330 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
331 varname);
332 fprintf (file,
333 " * Where:\t'screen'\tis the pointer to the frame buffer\n");
334 fprintf (file, " *\t\t'width'\tis the screen width\n");
335 fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
336 fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
337
1a3cb4ad
MF
338 /* image compress */
339 if (compression != COMP_NONE) {
24113a44
MF
340 const char *errstr = NULL;
341 unsigned char *compressed;
1a3cb4ad 342 const char *comp_name;
24113a44 343 struct stat st;
1a3cb4ad
MF
344 FILE *compfp;
345 size_t filename_len = strlen(filename);
346 char *compfilename = xmalloc(filename_len + 20);
347 char *compcmd = xmalloc(filename_len + 50);
348
349 sprintf(compfilename, "%s.bin", filename);
350 switch (compression) {
351 case COMP_GZIP:
352 strcpy(compcmd, "gzip");
353 comp_name = "GZIP";
354 break;
355 case COMP_LZMA:
356 strcpy(compcmd, "lzma");
357 comp_name = "LZMA";
358 break;
359 default:
360 errstr = "\nerror: unknown compression method";
361 goto done;
362 }
363 strcat(compcmd, " > ");
364 strcat(compcmd, compfilename);
365 compfp = popen(compcmd, "w");
366 if (!compfp) {
24113a44
MF
367 errstr = "\nerror: popen() failed";
368 goto done;
369 }
1a3cb4ad 370 if (fwrite(image->data, image->size, 1, compfp) != 1) {
24113a44
MF
371 errstr = "\nerror: writing data to gzip failed";
372 goto done;
373 }
1a3cb4ad 374 if (pclose(compfp)) {
24113a44
MF
375 errstr = "\nerror: gzip process failed";
376 goto done;
377 }
378
1a3cb4ad
MF
379 compfp = fopen(compfilename, "r");
380 if (!compfp) {
24113a44
MF
381 errstr = "\nerror: open() on gzip data failed";
382 goto done;
383 }
1a3cb4ad 384 if (stat(compfilename, &st)) {
24113a44
MF
385 errstr = "\nerror: stat() on gzip file failed";
386 goto done;
387 }
1a3cb4ad
MF
388 compressed = xmalloc(st.st_size);
389 if (fread(compressed, st.st_size, 1, compfp) != 1) {
24113a44
MF
390 errstr = "\nerror: reading gzip data failed";
391 goto done;
392 }
1a3cb4ad 393 fclose(compfp);
24113a44 394
1a3cb4ad 395 unlink(compfilename);
24113a44
MF
396
397 dataptr = compressed;
398 count = st.st_size;
1a3cb4ad
MF
399 fprintf(file, "#define EASYLOGO_ENABLE_%s %i\n\n", comp_name, count);
400 if (bss_storage)
24113a44
MF
401 fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);
402
403 done:
1a3cb4ad
MF
404 free(compfilename);
405 free(compcmd);
24113a44
MF
406
407 if (errstr) {
408 perror (errstr);
409 return -1;
410 }
411 }
412
6007f325
WD
413 /* Headers */
414 fprintf (file, "#include <video_easylogo.h>\n\n");
415 /* Macros */
416 strcpy (def_name, varname);
fe8c2806 417 StringUpperCase (def_name);
6007f325
WD
418 fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
419 image->width);
420 fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
421 image->height);
422 fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
423 image->pixels);
424 fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
425 fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name,
426 image->pixel_size);
427 fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
428 image->size);
429 /* Declaration */
24113a44
MF
430 fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
431 def_name);
6007f325
WD
432
433 /* Data */
434 while (count)
435 switch (col) {
436 case 0:
437 sprintf (str, " 0x%02x", *dataptr++);
438 col++;
439 count--;
440 break;
441
442 case 16:
443 fprintf (file, "%s", str);
444 if (count > 0)
445 fprintf (file, ",");
446 fprintf (file, "\n");
447
448 col = 0;
449 break;
450
451 default:
452 strcpy (app, str);
453 sprintf (str, "%s, 0x%02x", app, *dataptr++);
454 col++;
455 count--;
456 break;
fe8c2806
WD
457 }
458
459 if (col)
6007f325
WD
460 fprintf (file, "%s\n", str);
461
53677ef1 462 /* End of declaration */
6007f325
WD
463 fprintf (file, "};\n\n");
464 /* Variable */
465 fprintf (file, "fastimage_t %s = {\n", varname);
466 fprintf (file, " DEF_%s_DATA,\n", def_name);
467 fprintf (file, " DEF_%s_WIDTH,\n", def_name);
468 fprintf (file, " DEF_%s_HEIGHT,\n", def_name);
469 fprintf (file, " DEF_%s_BPP,\n", def_name);
470 fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name);
471 fprintf (file, " DEF_%s_SIZE\n};\n", def_name);
fe8c2806
WD
472
473 fclose (file);
474
6007f325 475 return 0;
fe8c2806
WD
476}
477
478#define DEF_FILELEN 256
479
edfed1d9
MF
480static void usage (int exit_status)
481{
482 puts (
483 "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
484 "\n"
485 "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n"
486 "\n"
487 "Options:\n"
7293e057
MH
488 " -r Output RGB888 instead of YUYV\n"
489 " -s Output RGB565 instead of YUYV\n"
24113a44 490 " -g Compress with gzip\n"
1a3cb4ad 491 " -l Compress with lzma\n"
24113a44 492 " -b Preallocate space in bss for decompressing image\n"
edfed1d9
MF
493 " -h Help output\n"
494 "\n"
495 "Where: 'inputfile' is the TGA image to load\n"
496 " 'outputvar' is the variable name to create\n"
497 " 'outputfile' is the output header file (default is 'inputfile.h')"
498 );
499 exit (exit_status);
500}
501
fe8c2806
WD
502int main (int argc, char *argv[])
503{
edfed1d9 504 int c;
7293e057
MH
505 bool use_rgb888 = false;
506 bool use_rgb565 = false;
6007f325
WD
507 char inputfile[DEF_FILELEN],
508 outputfile[DEF_FILELEN], varname[DEF_FILELEN];
509
7293e057 510 image_t rgb888_logo, rgb565_logo, yuyv_logo;
6007f325 511
1a3cb4ad 512 while ((c = getopt(argc, argv, "hrsglb")) > 0) {
edfed1d9
MF
513 switch (c) {
514 case 'h':
515 usage (0);
516 break;
517 case 'r':
7293e057
MH
518 use_rgb888 = true;
519 puts("Using 24-bit RGB888 Output Fromat");
520 break;
521 case 's':
522 use_rgb565 = true;
523 puts("Using 16-bit RGB565 Output Fromat");
edfed1d9 524 break;
24113a44 525 case 'g':
1a3cb4ad
MF
526 compression = COMP_GZIP;
527 puts("Compressing with gzip");
528 break;
529 case 'l':
530 compression = COMP_LZMA;
531 puts("Compressing with lzma");
24113a44
MF
532 break;
533 case 'b':
1a3cb4ad
MF
534 bss_storage = true;
535 puts("Preallocating bss space for decompressing image");
24113a44 536 break;
edfed1d9
MF
537 default:
538 usage (1);
539 break;
6007f325 540 }
edfed1d9 541 }
fe8c2806 542
edfed1d9
MF
543 c = argc - optind;
544 if (c > 4 || c < 1)
545 usage (1);
546
547 strcpy (inputfile, argv[optind]);
548
549 if (c > 1)
550 strcpy (varname, argv[optind + 1]);
551 else {
552 /* transform "input.tga" to just "input" */
553 char *dot;
554 strcpy (varname, inputfile);
555 dot = strchr (varname, '.');
556 if (dot)
557 *dot = '\0';
558 }
fe8c2806 559
edfed1d9
MF
560 if (c > 2)
561 strcpy (outputfile, argv[optind + 2]);
562 else {
563 /* just append ".h" to input file name */
564 strcpy (outputfile, inputfile);
565 strcat (outputfile, ".h");
6007f325 566 }
fe8c2806 567
edfed1d9
MF
568 /* Make sure the output is sent as soon as we printf() */
569 setbuf(stdout, NULL);
570
6007f325
WD
571 printf ("Doing '%s' (%s) from '%s'...",
572 outputfile, varname, inputfile);
fe8c2806 573
6007f325 574 /* Import TGA logo */
fe8c2806 575
6007f325 576 printf ("L");
7293e057 577 if (image_load_tga(&rgb888_logo, inputfile) < 0) {
6007f325
WD
578 printf ("input file not found!\n");
579 exit (1);
580 }
fe8c2806 581
7293e057 582 /* Convert, save, and free the image */
fe8c2806 583
7293e057 584 if (!use_rgb888 && !use_rgb565) {
edfed1d9 585 printf ("C");
7293e057
MH
586 image_rgb_to_yuyv(&rgb888_logo, &yuyv_logo);
587
588 printf("S");
589 image_save_header(&yuyv_logo, outputfile, varname);
590 image_free(&yuyv_logo);
591 } else if (use_rgb565) {
592 printf("C");
593 image_rgb888_to_rgb565(&rgb888_logo, &rgb565_logo);
594
595 printf("S");
596 image_save_header(&rgb565_logo, outputfile, varname);
597 image_free(&rgb565_logo);
598 } else {
599 printf("S");
600 image_save_header(&rgb888_logo, outputfile, varname);
edfed1d9 601 }
fe8c2806 602
6007f325 603 /* Free original image and copy */
fe8c2806 604
7293e057 605 image_free(&rgb888_logo);
fe8c2806 606
6007f325 607 printf ("\n");
fe8c2806 608
6007f325 609 return 0;
fe8c2806 610}