2 ** Easylogo TGA->header converter
3 ** ==============================
4 ** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
5 ** AIRVENT SAM s.p.a - RIMINI(ITALY)
7 ** This is still under construction!
16 /*#define ENABLE_ASCII_BANNERS */
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;
34 unsigned char r, g, b;
38 unsigned char b, g, r;
42 unsigned char Cb, y1, Cr, y2;
47 int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
50 void StringUpperCase (char *str)
52 int count = strlen (str);
57 if ((c >= 'a') && (c <= 'z'))
58 *str = 'A' + (c - 'a');
63 void StringLowerCase (char *str)
65 int count = strlen (str);
70 if ((c >= 'A') && (c <= 'Z'))
71 *str = 'a' + (c - 'A');
75 void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
77 unsigned int pR, pG, pB;
79 /* 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;
84 /* 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;
92 void printlogo_rgb (rgb_t * data, int w, int h)
96 for (y = 0; y < h; y++) {
97 for (x = 0; x < w; x++, data++)
99 30) /*&&(data->g == 0)&&(data->b == 0) */ )
107 void printlogo_yuyv (unsigned short *data, int w, int h)
111 for (y = 0; y < h; y++) {
112 for (x = 0; x < w; x++, data++)
113 if (*data == 0x1080) /* Because of inverted on i386! */
121 static inline unsigned short le16_to_cpu (unsigned short val)
124 unsigned char pval[2];
129 return (swapped.pval[1] << 8) + swapped.pval[0];
132 int image_load_tga (image_t * image, char *filename)
140 if ((file = fopen (filename, "rb")) == NULL)
143 fread (&header, sizeof (header), 1, file);
145 /* 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);
153 image->width = header.ImageWidth;
154 image->height = header.ImageHeight;
156 switch (header.ImageTypeCode) {
157 case 2: /* Uncompressed RGB */
159 image->palette_size = 0;
160 image->palette = NULL;
164 printf ("Format not supported!\n");
168 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);
174 if (image->bpp != 24) {
175 printf ("Bpp not supported: %d!\n", image->bpp);
179 fread (image->data, image->size, 1, file);
181 /* Swapping R and B values */
184 for (i = 0; i < image->pixels; i++, p++) {
192 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;
200 printf ("Cannot alloc temp buffer!\n");
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);
211 #ifdef ENABLE_ASCII_BANNERS
212 printlogo_rgb (image->data, image->width, image->height);
219 int image_free (image_t * image)
221 if (image->data != NULL)
224 if (image->palette != NULL)
225 free (image->palette);
230 int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
232 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
234 unsigned short *dest;
237 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;
249 while ((count++) < rgb_image->pixels) {
250 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
252 if ((count & 1) == 0) /* Was == 0 */
253 memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
255 memcpy (dest, (void *) &yuyv, sizeof (short));
260 #ifdef ENABLE_ASCII_BANNERS
261 printlogo_yuyv (yuyv_image->data, yuyv_image->width,
267 int image_save_header (image_t * image, char *filename, char *varname)
269 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;
277 /* Author information */
279 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
281 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
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");
290 fprintf (file, "#include <video_easylogo.h>\n\n");
292 strcpy (def_name, varname);
293 StringUpperCase (def_name);
294 fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
296 fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
298 fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
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,
303 fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
306 fprintf (file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n",
313 sprintf (str, " 0x%02x", *dataptr++);
319 fprintf (file, "%s", str);
322 fprintf (file, "\n");
329 sprintf (str, "%s, 0x%02x", app, *dataptr++);
336 fprintf (file, "%s\n", str);
338 /* End of declaration */
339 fprintf (file, "};\n\n");
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);
354 #define DEF_FILELEN 256
356 int main (int argc, char *argv[])
358 char inputfile[DEF_FILELEN],
359 outputfile[DEF_FILELEN], varname[DEF_FILELEN];
361 image_t rgb_logo, yuyv_logo;
367 strcpy (inputfile, argv[1]);
370 strcpy (varname, argv[2]);
372 char *dot = strchr (inputfile, '.');
373 int pos = dot - inputfile;
376 strncpy (varname, inputfile, pos);
382 strcpy (outputfile, argv[3]);
384 char *dot = strchr (varname, '.');
385 int pos = dot - varname;
388 char app[DEF_FILELEN];
390 strncpy (app, varname, pos);
392 sprintf (outputfile, "%s.h", app);
398 printf ("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
400 printf("Syntax: easylogo inputfile [outputvar {outputfile}] \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");
409 printf ("Doing '%s' (%s) from '%s'...",
410 outputfile, varname, inputfile);
412 /* Import TGA logo */
415 if (image_load_tga (&rgb_logo, inputfile) < 0) {
416 printf ("input file not found!\n");
420 /* Convert it to YUYV format */
423 image_rgb_to_yuyv (&rgb_logo, &yuyv_logo);
425 /* Save it into a header format */
428 image_save_header (&yuyv_logo, outputfile, varname);
430 /* Free original image and copy */
432 image_free (&rgb_logo);
433 image_free (&yuyv_logo);