blob: 080bea9bb8cfe5259746874a90d187cd4159b23e [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)
6**
7** This is still under construction!
8*/
9
10#include <stdio.h>
Mike Frysinger38d299c2007-12-18 03:23:25 -050011#include <stdlib.h>
12#include <string.h>
wdenkfe8c2802002-11-03 00:38:21 +000013
14#pragma pack(1)
15
16/*#define ENABLE_ASCII_BANNERS */
17
18typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010019 unsigned char id;
20 unsigned char ColorMapType;
21 unsigned char ImageTypeCode;
22 unsigned short ColorMapOrigin;
23 unsigned short ColorMapLenght;
24 unsigned char ColorMapEntrySize;
25 unsigned short ImageXOrigin;
26 unsigned short ImageYOrigin;
27 unsigned short ImageWidth;
28 unsigned short ImageHeight;
29 unsigned char ImagePixelSize;
30 unsigned char ImageDescriptorByte;
wdenkfe8c2802002-11-03 00:38:21 +000031} tga_header_t;
32
33typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010034 unsigned char r, g, b;
35} rgb_t;
wdenkfe8c2802002-11-03 00:38:21 +000036
37typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010038 unsigned char b, g, r;
39} bgr_t;
wdenkfe8c2802002-11-03 00:38:21 +000040
41typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010042 unsigned char Cb, y1, Cr, y2;
43} yuyv_t;
wdenkfe8c2802002-11-03 00:38:21 +000044
45typedef struct {
Wolfgang Denk6007f322008-01-09 15:14:46 +010046 void *data, *palette;
47 int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
48} image_t;
wdenkfe8c2802002-11-03 00:38:21 +000049
50void StringUpperCase (char *str)
51{
Wolfgang Denk6007f322008-01-09 15:14:46 +010052 int count = strlen (str);
53 char c;
wdenkfe8c2802002-11-03 00:38:21 +000054
Wolfgang Denk6007f322008-01-09 15:14:46 +010055 while (count--) {
56 c = *str;
57 if ((c >= 'a') && (c <= 'z'))
58 *str = 'A' + (c - 'a');
59 str++;
60 }
wdenkfe8c2802002-11-03 00:38:21 +000061}
62
63void StringLowerCase (char *str)
64{
Wolfgang Denk6007f322008-01-09 15:14:46 +010065 int count = strlen (str);
66 char c;
wdenkfe8c2802002-11-03 00:38:21 +000067
Wolfgang Denk6007f322008-01-09 15:14:46 +010068 while (count--) {
69 c = *str;
70 if ((c >= 'A') && (c <= 'Z'))
71 *str = 'a' + (c - 'A');
72 str++;
73 }
wdenkfe8c2802002-11-03 00:38:21 +000074}
Wolfgang Denk6007f322008-01-09 15:14:46 +010075void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
wdenkfe8c2802002-11-03 00:38:21 +000076{
Wolfgang Denk6007f322008-01-09 15:14:46 +010077 unsigned int pR, pG, pB;
wdenkfe8c2802002-11-03 00:38:21 +000078
Wolfgang Denk6007f322008-01-09 15:14:46 +010079 /* Transform (0-255) components to (0-100) */
80 pR = rgb_pixel->r * 100 / 255;
81 pG = rgb_pixel->g * 100 / 255;
82 pB = rgb_pixel->b * 100 / 255;
wdenkfe8c2802002-11-03 00:38:21 +000083
Wolfgang Denk6007f322008-01-09 15:14:46 +010084 /* Calculate YUV values (0-255) from RGB beetween 0-100 */
85 yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16;
86 yuyv_pixel->Cb = pB - (pR / 4) - (pG * 3 / 4) + 128;
87 yuyv_pixel->Cr = pR - (pG * 3 / 4) - (pB / 4) + 128;
wdenkfe8c2802002-11-03 00:38:21 +000088
Wolfgang Denk6007f322008-01-09 15:14:46 +010089 return;
wdenkfe8c2802002-11-03 00:38:21 +000090}
91
Wolfgang Denk6007f322008-01-09 15:14:46 +010092void printlogo_rgb (rgb_t * data, int w, int h)
wdenkfe8c2802002-11-03 00:38:21 +000093{
Wolfgang Denk6007f322008-01-09 15:14:46 +010094 int x, y;
95
96 for (y = 0; y < h; y++) {
97 for (x = 0; x < w; x++, data++)
98 if ((data->r <
99 30) /*&&(data->g == 0)&&(data->b == 0) */ )
100 printf (" ");
101 else
102 printf ("X");
103 printf ("\n");
104 }
wdenkfe8c2802002-11-03 00:38:21 +0000105}
106
107void printlogo_yuyv (unsigned short *data, int w, int h)
108{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100109 int x, y;
110
111 for (y = 0; y < h; y++) {
112 for (x = 0; x < w; x++, data++)
113 if (*data == 0x1080) /* Because of inverted on i386! */
114 printf (" ");
115 else
116 printf ("X");
117 printf ("\n");
118 }
wdenkfe8c2802002-11-03 00:38:21 +0000119}
120
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500121static inline unsigned short le16_to_cpu (unsigned short val)
122{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100123 union {
124 unsigned char pval[2];
125 unsigned short val;
126 } swapped;
127
128 swapped.val = val;
129 return (swapped.pval[1] << 8) + swapped.pval[0];
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500130}
131
Wolfgang Denk6007f322008-01-09 15:14:46 +0100132int image_load_tga (image_t * image, char *filename)
wdenkfe8c2802002-11-03 00:38:21 +0000133{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100134 FILE *file;
135 tga_header_t header;
136 int i;
137 unsigned char app;
138 rgb_t *p;
wdenkfe8c2802002-11-03 00:38:21 +0000139
Wolfgang Denk6007f322008-01-09 15:14:46 +0100140 if ((file = fopen (filename, "rb")) == NULL)
141 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000142
Wolfgang Denk6007f322008-01-09 15:14:46 +0100143 fread (&header, sizeof (header), 1, file);
wdenkfe8c2802002-11-03 00:38:21 +0000144
Wolfgang Denk6007f322008-01-09 15:14:46 +0100145 /* byte swap: tga is little endian, host is ??? */
146 header.ColorMapOrigin = le16_to_cpu (header.ColorMapOrigin);
147 header.ColorMapLenght = le16_to_cpu (header.ColorMapLenght);
148 header.ImageXOrigin = le16_to_cpu (header.ImageXOrigin);
149 header.ImageYOrigin = le16_to_cpu (header.ImageYOrigin);
150 header.ImageWidth = le16_to_cpu (header.ImageWidth);
151 header.ImageHeight = le16_to_cpu (header.ImageHeight);
Mike Frysingerfc6414e2007-12-18 04:29:55 -0500152
Wolfgang Denk6007f322008-01-09 15:14:46 +0100153 image->width = header.ImageWidth;
154 image->height = header.ImageHeight;
wdenkfe8c2802002-11-03 00:38:21 +0000155
Wolfgang Denk6007f322008-01-09 15:14:46 +0100156 switch (header.ImageTypeCode) {
157 case 2: /* Uncompressed RGB */
158 image->yuyv = 0;
159 image->palette_size = 0;
160 image->palette = NULL;
161 break;
wdenkfe8c2802002-11-03 00:38:21 +0000162
163 default:
Wolfgang Denk6007f322008-01-09 15:14:46 +0100164 printf ("Format not supported!\n");
165 return -1;
166 }
wdenkfe8c2802002-11-03 00:38:21 +0000167
Wolfgang Denk6007f322008-01-09 15:14:46 +0100168 image->bpp = header.ImagePixelSize;
169 image->pixel_size = ((image->bpp - 1) / 8) + 1;
170 image->pixels = image->width * image->height;
171 image->size = image->pixels * image->pixel_size;
172 image->data = malloc (image->size);
wdenkfe8c2802002-11-03 00:38:21 +0000173
Wolfgang Denk6007f322008-01-09 15:14:46 +0100174 if (image->bpp != 24) {
175 printf ("Bpp not supported: %d!\n", image->bpp);
176 return -1;
177 }
wdenkfe8c2802002-11-03 00:38:21 +0000178
Wolfgang Denk6007f322008-01-09 15:14:46 +0100179 fread (image->data, image->size, 1, file);
wdenkfe8c2802002-11-03 00:38:21 +0000180
181/* Swapping R and B values */
182
Wolfgang Denk6007f322008-01-09 15:14:46 +0100183 p = image->data;
184 for (i = 0; i < image->pixels; i++, p++) {
185 app = p->r;
186 p->r = p->b;
187 p->b = app;
188 }
wdenkfe8c2802002-11-03 00:38:21 +0000189
190/* Swapping image */
191
Wolfgang Denk6007f322008-01-09 15:14:46 +0100192 if (!(header.ImageDescriptorByte & 0x20)) {
193 unsigned char *temp = malloc (image->size);
194 int linesize = image->pixel_size * image->width;
195 void *dest = image->data,
196 *source = temp + image->size - linesize;
wdenkfe8c2802002-11-03 00:38:21 +0000197
Wolfgang Denk6007f322008-01-09 15:14:46 +0100198 printf ("S");
199 if (temp == NULL) {
200 printf ("Cannot alloc temp buffer!\n");
201 return -1;
202 }
203
204 memcpy (temp, image->data, image->size);
205 for (i = 0; i < image->height;
206 i++, dest += linesize, source -= linesize)
207 memcpy (dest, source, linesize);
208
209 free (temp);
wdenkfe8c2802002-11-03 00:38:21 +0000210 }
wdenkfe8c2802002-11-03 00:38:21 +0000211#ifdef ENABLE_ASCII_BANNERS
Wolfgang Denk6007f322008-01-09 15:14:46 +0100212 printlogo_rgb (image->data, image->width, image->height);
wdenkfe8c2802002-11-03 00:38:21 +0000213#endif
214
Wolfgang Denk6007f322008-01-09 15:14:46 +0100215 fclose (file);
216 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000217}
218
Wolfgang Denk6007f322008-01-09 15:14:46 +0100219int image_free (image_t * image)
wdenkfe8c2802002-11-03 00:38:21 +0000220{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100221 if (image->data != NULL)
222 free (image->data);
wdenkfe8c2802002-11-03 00:38:21 +0000223
Wolfgang Denk6007f322008-01-09 15:14:46 +0100224 if (image->palette != NULL)
225 free (image->palette);
wdenkfe8c2802002-11-03 00:38:21 +0000226
227 return 0;
228}
229
Wolfgang Denk6007f322008-01-09 15:14:46 +0100230int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
wdenkfe8c2802002-11-03 00:38:21 +0000231{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100232 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
233 yuyv_t yuyv;
234 unsigned short *dest;
235 int count = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000236
Wolfgang Denk6007f322008-01-09 15:14:46 +0100237 yuyv_image->pixel_size = 2;
238 yuyv_image->bpp = 16;
239 yuyv_image->yuyv = 1;
240 yuyv_image->width = rgb_image->width;
241 yuyv_image->height = rgb_image->height;
242 yuyv_image->pixels = yuyv_image->width * yuyv_image->height;
243 yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size;
244 dest = (unsigned short *) (yuyv_image->data =
245 malloc (yuyv_image->size));
246 yuyv_image->palette = 0;
247 yuyv_image->palette_size = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000248
Wolfgang Denk6007f322008-01-09 15:14:46 +0100249 while ((count++) < rgb_image->pixels) {
wdenkfe8c2802002-11-03 00:38:21 +0000250 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
251
Wolfgang Denk6007f322008-01-09 15:14:46 +0100252 if ((count & 1) == 0) /* Was == 0 */
253 memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
wdenkfe8c2802002-11-03 00:38:21 +0000254 else
Wolfgang Denk6007f322008-01-09 15:14:46 +0100255 memcpy (dest, (void *) &yuyv, sizeof (short));
wdenkfe8c2802002-11-03 00:38:21 +0000256
Wolfgang Denk6007f322008-01-09 15:14:46 +0100257 dest++;
wdenkfe8c2802002-11-03 00:38:21 +0000258 }
259
260#ifdef ENABLE_ASCII_BANNERS
Wolfgang Denk6007f322008-01-09 15:14:46 +0100261 printlogo_yuyv (yuyv_image->data, yuyv_image->width,
262 yuyv_image->height);
wdenkfe8c2802002-11-03 00:38:21 +0000263#endif
Wolfgang Denk6007f322008-01-09 15:14:46 +0100264 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000265}
266
Wolfgang Denk6007f322008-01-09 15:14:46 +0100267int image_save_header (image_t * image, char *filename, char *varname)
wdenkfe8c2802002-11-03 00:38:21 +0000268{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100269 FILE *file = fopen (filename, "w");
270 char app[256], str[256] = "", def_name[64];
271 int count = image->size, col = 0;
272 unsigned char *dataptr = image->data;
wdenkfe8c2802002-11-03 00:38:21 +0000273
Wolfgang Denk6007f322008-01-09 15:14:46 +0100274 if (file == NULL)
275 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000276
Wolfgang Denk6007f322008-01-09 15:14:46 +0100277 /* Author information */
278 fprintf (file,
279 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
280 fprintf (file,
281 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
282 varname);
283 fprintf (file,
284 " * Where:\t'screen'\tis the pointer to the frame buffer\n");
285 fprintf (file, " *\t\t'width'\tis the screen width\n");
286 fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
287 fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
288
289 /* Headers */
290 fprintf (file, "#include <video_easylogo.h>\n\n");
291 /* Macros */
292 strcpy (def_name, varname);
wdenkfe8c2802002-11-03 00:38:21 +0000293 StringUpperCase (def_name);
Wolfgang Denk6007f322008-01-09 15:14:46 +0100294 fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
295 image->width);
296 fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
297 image->height);
298 fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
299 image->pixels);
300 fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
301 fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name,
302 image->pixel_size);
303 fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
304 image->size);
305 /* Declaration */
306 fprintf (file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n",
307 def_name, def_name);
wdenkfe8c2802002-11-03 00:38:21 +0000308
Wolfgang Denk6007f322008-01-09 15:14:46 +0100309 /* Data */
310 while (count)
311 switch (col) {
312 case 0:
313 sprintf (str, " 0x%02x", *dataptr++);
314 col++;
315 count--;
316 break;
wdenkfe8c2802002-11-03 00:38:21 +0000317
Wolfgang Denk6007f322008-01-09 15:14:46 +0100318 case 16:
319 fprintf (file, "%s", str);
320 if (count > 0)
321 fprintf (file, ",");
322 fprintf (file, "\n");
wdenkfe8c2802002-11-03 00:38:21 +0000323
Wolfgang Denk6007f322008-01-09 15:14:46 +0100324 col = 0;
325 break;
wdenkfe8c2802002-11-03 00:38:21 +0000326
Wolfgang Denk6007f322008-01-09 15:14:46 +0100327 default:
328 strcpy (app, str);
329 sprintf (str, "%s, 0x%02x", app, *dataptr++);
330 col++;
331 count--;
332 break;
wdenkfe8c2802002-11-03 00:38:21 +0000333 }
334
335 if (col)
Wolfgang Denk6007f322008-01-09 15:14:46 +0100336 fprintf (file, "%s\n", str);
wdenkfe8c2802002-11-03 00:38:21 +0000337
Wolfgang Denk6007f322008-01-09 15:14:46 +0100338 /* End of declaration */
339 fprintf (file, "};\n\n");
340 /* Variable */
341 fprintf (file, "fastimage_t %s = {\n", varname);
342 fprintf (file, " DEF_%s_DATA,\n", def_name);
343 fprintf (file, " DEF_%s_WIDTH,\n", def_name);
344 fprintf (file, " DEF_%s_HEIGHT,\n", def_name);
345 fprintf (file, " DEF_%s_BPP,\n", def_name);
346 fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name);
347 fprintf (file, " DEF_%s_SIZE\n};\n", def_name);
wdenkfe8c2802002-11-03 00:38:21 +0000348
349 fclose (file);
350
Wolfgang Denk6007f322008-01-09 15:14:46 +0100351 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000352}
353
354#define DEF_FILELEN 256
355
356int main (int argc, char *argv[])
357{
Wolfgang Denk6007f322008-01-09 15:14:46 +0100358 char inputfile[DEF_FILELEN],
359 outputfile[DEF_FILELEN], varname[DEF_FILELEN];
wdenkfe8c2802002-11-03 00:38:21 +0000360
Wolfgang Denk6007f322008-01-09 15:14:46 +0100361 image_t rgb_logo, yuyv_logo;
wdenkfe8c2802002-11-03 00:38:21 +0000362
Wolfgang Denk6007f322008-01-09 15:14:46 +0100363 switch (argc) {
364 case 2:
365 case 3:
366 case 4:
367 strcpy (inputfile, argv[1]);
wdenkfe8c2802002-11-03 00:38:21 +0000368
Wolfgang Denk6007f322008-01-09 15:14:46 +0100369 if (argc > 2)
370 strcpy (varname, argv[2]);
371 else {
372 char *dot = strchr (inputfile, '.');
373 int pos = dot - inputfile;
wdenkfe8c2802002-11-03 00:38:21 +0000374
Wolfgang Denk6007f322008-01-09 15:14:46 +0100375 if (dot) {
376 strncpy (varname, inputfile, pos);
377 varname[pos] = 0;
378 }
379 }
380
381 if (argc > 3)
382 strcpy (outputfile, argv[3]);
383 else {
384 char *dot = strchr (varname, '.');
385 int pos = dot - varname;
386
387 if (dot) {
388 char app[DEF_FILELEN];
389
390 strncpy (app, varname, pos);
391 app[pos] = 0;
392 sprintf (outputfile, "%s.h", app);
393 }
394 }
395 break;
396
397 default:
398 printf ("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
399
400 printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n");
401 printf("\n");
402 printf("Where: 'inputfile' is the TGA image to load\n");
403 printf(" 'outputvar' is the variable name to create\n");
404 printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n");
405
406 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000407 }
408
Wolfgang Denk6007f322008-01-09 15:14:46 +0100409 printf ("Doing '%s' (%s) from '%s'...",
410 outputfile, varname, inputfile);
wdenkfe8c2802002-11-03 00:38:21 +0000411
Wolfgang Denk6007f322008-01-09 15:14:46 +0100412 /* Import TGA logo */
wdenkfe8c2802002-11-03 00:38:21 +0000413
Wolfgang Denk6007f322008-01-09 15:14:46 +0100414 printf ("L");
415 if (image_load_tga (&rgb_logo, inputfile) < 0) {
416 printf ("input file not found!\n");
417 exit (1);
wdenkfe8c2802002-11-03 00:38:21 +0000418 }
wdenkfe8c2802002-11-03 00:38:21 +0000419
Wolfgang Denk6007f322008-01-09 15:14:46 +0100420 /* Convert it to YUYV format */
wdenkfe8c2802002-11-03 00:38:21 +0000421
Wolfgang Denk6007f322008-01-09 15:14:46 +0100422 printf ("C");
423 image_rgb_to_yuyv (&rgb_logo, &yuyv_logo);
wdenkfe8c2802002-11-03 00:38:21 +0000424
Wolfgang Denk6007f322008-01-09 15:14:46 +0100425 /* Save it into a header format */
wdenkfe8c2802002-11-03 00:38:21 +0000426
Wolfgang Denk6007f322008-01-09 15:14:46 +0100427 printf ("S");
428 image_save_header (&yuyv_logo, outputfile, varname);
wdenkfe8c2802002-11-03 00:38:21 +0000429
Wolfgang Denk6007f322008-01-09 15:14:46 +0100430 /* Free original image and copy */
wdenkfe8c2802002-11-03 00:38:21 +0000431
Wolfgang Denk6007f322008-01-09 15:14:46 +0100432 image_free (&rgb_logo);
433 image_free (&yuyv_logo);
wdenkfe8c2802002-11-03 00:38:21 +0000434
Wolfgang Denk6007f322008-01-09 15:14:46 +0100435 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +0000436
Wolfgang Denk6007f322008-01-09 15:14:46 +0100437 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000438}