2 ** Easylogo TGA->header converter
3 ** ==============================
4 ** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
5 ** AIRVENT SAM s.p.a - RIMINI(ITALY)
6 ** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
8 ** This is still under construction!
22 /*#define ENABLE_ASCII_BANNERS */
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;
40 unsigned char r, g, b;
44 unsigned char b, g, r;
48 unsigned char Cb, y1, Cr, y2;
53 int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
56 void *xmalloc (size_t size)
58 void *ret = malloc (size);
60 fprintf (stderr, "\nerror: malloc(%zu) failed: %s",
61 size, strerror(errno));
67 void StringUpperCase (char *str)
69 int count = strlen (str);
74 if ((c >= 'a') && (c <= 'z'))
75 *str = 'A' + (c - 'a');
80 void StringLowerCase (char *str)
82 int count = strlen (str);
87 if ((c >= 'A') && (c <= 'Z'))
88 *str = 'a' + (c - 'A');
92 void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
94 unsigned int pR, pG, pB;
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;
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;
109 void printlogo_rgb (rgb_t * data, int w, int h)
113 for (y = 0; y < h; y++) {
114 for (x = 0; x < w; x++, data++)
116 30) /*&&(data->g == 0)&&(data->b == 0) */ )
124 void printlogo_yuyv (unsigned short *data, int w, int h)
128 for (y = 0; y < h; y++) {
129 for (x = 0; x < w; x++, data++)
130 if (*data == 0x1080) /* Because of inverted on i386! */
138 static inline unsigned short le16_to_cpu (unsigned short val)
141 unsigned char pval[2];
146 return (swapped.pval[1] << 8) + swapped.pval[0];
149 int image_load_tga (image_t * image, char *filename)
157 if ((file = fopen (filename, "rb")) == NULL)
160 fread (&header, sizeof (header), 1, file);
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);
170 image->width = header.ImageWidth;
171 image->height = header.ImageHeight;
173 switch (header.ImageTypeCode) {
174 case 2: /* Uncompressed RGB */
176 image->palette_size = 0;
177 image->palette = NULL;
181 printf ("Format not supported!\n");
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;
189 image->data = xmalloc (image->size);
191 if (image->bpp != 24) {
192 printf ("Bpp not supported: %d!\n", image->bpp);
196 fread (image->data, image->size, 1, file);
198 /* Swapping R and B values */
201 for (i = 0; i < image->pixels; i++, p++) {
209 if (!(header.ImageDescriptorByte & 0x20)) {
210 unsigned char *temp = xmalloc (image->size);
211 int linesize = image->pixel_size * image->width;
212 void *dest = image->data,
213 *source = temp + image->size - linesize;
217 printf ("Cannot alloc temp buffer!\n");
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);
228 #ifdef ENABLE_ASCII_BANNERS
229 printlogo_rgb (image->data, image->width, image->height);
236 void image_free (image_t * image)
239 free (image->palette);
242 int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
244 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
246 unsigned short *dest;
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 =
257 xmalloc (yuyv_image->size));
258 yuyv_image->palette = 0;
259 yuyv_image->palette_size = 0;
261 while ((count++) < rgb_image->pixels) {
262 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
264 if ((count & 1) == 0) /* Was == 0 */
265 memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
267 memcpy (dest, (void *) &yuyv, sizeof (short));
272 #ifdef ENABLE_ASCII_BANNERS
273 printlogo_yuyv (yuyv_image->data, yuyv_image->width,
279 int image_rgb888_to_rgb565(image_t *rgb888_image, image_t *rgb565_image)
281 rgb_t *rgb_ptr = (rgb_t *) rgb888_image->data;
282 unsigned short *dest;
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;
297 while ((count++) < rgb888_image->pixels) {
299 *dest++ = ((rgb_ptr->b & 0xF8) << 8) |
300 ((rgb_ptr->g & 0xFC) << 3) |
313 static enum comp_t compression = COMP_NONE;
314 static bool bss_storage = false;
316 int image_save_header (image_t * image, char *filename, char *varname)
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;
326 /* Author information */
328 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
330 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
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");
339 if (compression != COMP_NONE) {
340 const char *errstr = NULL;
341 unsigned char *compressed;
342 const char *comp_name;
345 size_t filename_len = strlen(filename);
346 char *compfilename = xmalloc(filename_len + 20);
347 char *compcmd = xmalloc(filename_len + 50);
349 sprintf(compfilename, "%s.bin", filename);
350 switch (compression) {
352 strcpy(compcmd, "gzip");
356 strcpy(compcmd, "lzma");
360 errstr = "\nerror: unknown compression method";
363 strcat(compcmd, " > ");
364 strcat(compcmd, compfilename);
365 compfp = popen(compcmd, "w");
367 errstr = "\nerror: popen() failed";
370 if (fwrite(image->data, image->size, 1, compfp) != 1) {
371 errstr = "\nerror: writing data to gzip failed";
374 if (pclose(compfp)) {
375 errstr = "\nerror: gzip process failed";
379 compfp = fopen(compfilename, "r");
381 errstr = "\nerror: open() on gzip data failed";
384 if (stat(compfilename, &st)) {
385 errstr = "\nerror: stat() on gzip file failed";
388 compressed = xmalloc(st.st_size);
389 if (fread(compressed, st.st_size, 1, compfp) != 1) {
390 errstr = "\nerror: reading gzip data failed";
395 unlink(compfilename);
397 dataptr = compressed;
399 fprintf(file, "#define EASYLOGO_ENABLE_%s %i\n\n", comp_name, count);
401 fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);
414 fprintf (file, "#include <video_easylogo.h>\n\n");
416 strcpy (def_name, varname);
417 StringUpperCase (def_name);
418 fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
420 fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
422 fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
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,
427 fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
430 fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
437 sprintf (str, " 0x%02x", *dataptr++);
443 fprintf (file, "%s", str);
446 fprintf (file, "\n");
453 sprintf (str, "%s, 0x%02x", app, *dataptr++);
460 fprintf (file, "%s\n", str);
462 /* End of declaration */
463 fprintf (file, "};\n\n");
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);
478 #define DEF_FILELEN 256
480 static void usage (int exit_status)
483 "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
485 "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n"
488 " -r Output RGB888 instead of YUYV\n"
489 " -s Output RGB565 instead of YUYV\n"
490 " -g Compress with gzip\n"
491 " -l Compress with lzma\n"
492 " -b Preallocate space in bss for decompressing image\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')"
502 int main (int argc, char *argv[])
505 bool use_rgb888 = false;
506 bool use_rgb565 = false;
507 char inputfile[DEF_FILELEN],
508 outputfile[DEF_FILELEN], varname[DEF_FILELEN];
510 image_t rgb888_logo, rgb565_logo, yuyv_logo;
512 while ((c = getopt(argc, argv, "hrsglb")) > 0) {
519 puts("Using 24-bit RGB888 Output Fromat");
523 puts("Using 16-bit RGB565 Output Fromat");
526 compression = COMP_GZIP;
527 puts("Compressing with gzip");
530 compression = COMP_LZMA;
531 puts("Compressing with lzma");
535 puts("Preallocating bss space for decompressing image");
547 strcpy (inputfile, argv[optind]);
550 strcpy (varname, argv[optind + 1]);
552 /* transform "input.tga" to just "input" */
554 strcpy (varname, inputfile);
555 dot = strchr (varname, '.');
561 strcpy (outputfile, argv[optind + 2]);
563 /* just append ".h" to input file name */
564 strcpy (outputfile, inputfile);
565 strcat (outputfile, ".h");
568 /* Make sure the output is sent as soon as we printf() */
569 setbuf(stdout, NULL);
571 printf ("Doing '%s' (%s) from '%s'...",
572 outputfile, varname, inputfile);
574 /* Import TGA logo */
577 if (image_load_tga(&rgb888_logo, inputfile) < 0) {
578 printf ("input file not found!\n");
582 /* Convert, save, and free the image */
584 if (!use_rgb888 && !use_rgb565) {
586 image_rgb_to_yuyv(&rgb888_logo, &yuyv_logo);
589 image_save_header(&yuyv_logo, outputfile, varname);
590 image_free(&yuyv_logo);
591 } else if (use_rgb565) {
593 image_rgb888_to_rgb565(&rgb888_logo, &rgb565_logo);
596 image_save_header(&rgb565_logo, outputfile, varname);
597 image_free(&rgb565_logo);
600 image_save_header(&rgb888_logo, outputfile, varname);
603 /* Free original image and copy */
605 image_free(&rgb888_logo);