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!
14 /*#define ENABLE_ASCII_BANNERS */
18 unsigned char ColorMapType;
19 unsigned char ImageTypeCode;
20 unsigned short ColorMapOrigin;
21 unsigned short ColorMapLenght;
22 unsigned char ColorMapEntrySize;
23 unsigned short ImageXOrigin;
24 unsigned short ImageYOrigin;
25 unsigned short ImageWidth;
26 unsigned short ImageHeight;
27 unsigned char ImagePixelSize;
28 unsigned char ImageDescriptorByte;
40 unsigned char Cb,y1,Cr,y2;
56 void StringUpperCase (char *str)
58 int count = strlen(str);
64 if ((c >= 'a')&&(c<='z'))
70 void StringLowerCase (char *str)
72 int count = strlen(str);
78 if ((c >= 'A')&&(c<='Z'))
83 void pixel_rgb_to_yuyv (rgb_t *rgb_pixel, yuyv_t *yuyv_pixel)
85 unsigned int pR, pG, pB ;
87 /* Transform (0-255) components to (0-100) */
88 pR = rgb_pixel->r * 100 / 255 ;
89 pG = rgb_pixel->g * 100 / 255 ;
90 pB = rgb_pixel->b * 100 / 255 ;
92 /* Calculate YUV values (0-255) from RGB beetween 0-100 */
93 yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16 ;
94 yuyv_pixel->Cb = pB - (pR/4) - (pG*3/4) + 128 ;
95 yuyv_pixel->Cr = pR - (pG*3/4) - (pB/4) + 128 ;
100 void printlogo_rgb (rgb_t *data, int w, int h)
105 for (x=0; x<w; x++, data++)
106 if ((data->r < 30)/*&&(data->g == 0)&&(data->b == 0)*/)
114 void printlogo_yuyv (unsigned short *data, int w, int h)
119 for (x=0; x<w; x++, data++)
120 if (*data == 0x1080) /* Because of inverted on i386! */
128 int image_load_tga (image_t *image, char *filename)
131 tga_header_t header ;
136 if( ( file = fopen( filename, "rb" ) ) == NULL )
139 fread(&header, sizeof(header), 1, file);
141 image->width = header.ImageWidth ;
142 image->height = header.ImageHeight ;
144 switch (header.ImageTypeCode){
145 case 2: /* Uncompressed RGB */
147 image->palette_size = 0 ;
148 image->palette = NULL ;
152 printf("Format not supported!\n");
156 image->bpp = header.ImagePixelSize ;
157 image->pixel_size = ((image->bpp-1) / 8) + 1 ;
158 image->pixels = image->width * image->height;
159 image->size = image->pixels * image->pixel_size ;
160 image->data = malloc(image->size) ;
162 if (image->bpp != 24)
164 printf("Bpp not supported: %d!\n", image->bpp);
168 fread(image->data, image->size, 1, file);
170 /* Swapping R and B values */
173 for(i=0; i < image->pixels; i++, p++)
182 if(!(header.ImageDescriptorByte & 0x20))
184 unsigned char *temp = malloc(image->size);
185 int linesize = image->pixel_size * image->width ;
186 void *dest = image->data,
187 *source = temp + image->size - linesize ;
192 printf("Cannot alloc temp buffer!\n");
196 memcpy(temp, image->data, image->size);
197 for(i = 0; i<image->height; i++, dest+=linesize, source-=linesize)
198 memcpy(dest, source, linesize);
203 #ifdef ENABLE_ASCII_BANNERS
204 printlogo_rgb (image->data,image->width, image->height);
211 int image_free (image_t *image)
213 if(image->data != NULL)
216 if(image->palette != NULL)
217 free(image->palette);
222 int image_rgb_to_yuyv (image_t *rgb_image, image_t *yuyv_image)
224 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data ;
226 unsigned short *dest ;
229 yuyv_image->pixel_size = 2 ;
230 yuyv_image->bpp = 16 ;
231 yuyv_image->yuyv = 1 ;
232 yuyv_image->width = rgb_image->width ;
233 yuyv_image->height = rgb_image->height ;
234 yuyv_image->pixels = yuyv_image->width * yuyv_image->height ;
235 yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size ;
236 dest = (unsigned short *) (yuyv_image->data = malloc(yuyv_image->size)) ;
237 yuyv_image->palette = 0 ;
238 yuyv_image->palette_size= 0 ;
240 while((count++) < rgb_image->pixels)
242 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
244 if ((count & 1)==0) /* Was == 0 */
245 memcpy (dest, ((void *)&yuyv) + 2, sizeof(short));
247 memcpy (dest, (void *)&yuyv, sizeof(short));
252 #ifdef ENABLE_ASCII_BANNERS
253 printlogo_yuyv (yuyv_image->data, yuyv_image->width, yuyv_image->height);
258 int image_save_header (image_t *image, char *filename, char *varname)
260 FILE *file = fopen (filename, "w");
261 char app[256], str[256]="", def_name[64] ;
262 int count = image->size, col=0;
263 unsigned char *dataptr = image->data ;
267 /* Author informations */
268 fprintf(file, "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n/*\n"); */
269 fprintf(file, " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n", varname);
270 fprintf(file, " * Where:\t'screen'\tis the pointer to the frame buffer\n");
271 fprintf(file, " *\t\t'width'\tis the screen width\n");
272 fprintf(file, " *\t\t'x'\t\tis the horizontal position\n");
273 fprintf(file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
276 fprintf(file, "#include <video_easylogo.h>\n\n");
278 strcpy(def_name, varname);
279 StringUpperCase (def_name);
280 fprintf(file, "#define DEF_%s_WIDTH\t\t%d\n", def_name, image->width);
281 fprintf(file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name, image->height);
282 fprintf(file, "#define DEF_%s_PIXELS\t\t%d\n", def_name, image->pixels);
283 fprintf(file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
284 fprintf(file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name, image->pixel_size);
285 fprintf(file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name, image->size);
287 fprintf(file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n", def_name, def_name);
293 sprintf(str, " 0x%02x", *dataptr++);
299 fprintf(file, "%s", str);
309 sprintf(str, "%s, 0x%02x", app, *dataptr++);
316 fprintf(file, "%s\n", str);
318 /* End of declaration */
319 fprintf(file, "};\n\n");
321 fprintf(file, "fastimage_t %s = {\n", varname);
322 fprintf(file, " DEF_%s_DATA,\n", def_name);
323 fprintf(file, " DEF_%s_WIDTH,\n", def_name);
324 fprintf(file, " DEF_%s_HEIGHT,\n", def_name);
325 fprintf(file, " DEF_%s_BPP,\n", def_name);
326 fprintf(file, " DEF_%s_PIXEL_SIZE,\n", def_name);
327 fprintf(file, " DEF_%s_SIZE\n};\n", def_name);
334 #define DEF_FILELEN 256
336 int main (int argc, char *argv[])
339 inputfile[DEF_FILELEN],
340 outputfile[DEF_FILELEN],
341 varname[DEF_FILELEN];
343 image_t rgb_logo, yuyv_logo ;
349 strcpy (inputfile, argv[1]);
352 strcpy (varname, argv[2]);
355 int pos = strchr(inputfile, '.');
359 strncpy (varname, inputfile, pos);
365 strcpy (outputfile, argv[3]);
368 int pos = strchr (varname, '.');
372 char app[DEF_FILELEN] ;
374 strncpy(app, varname, pos);
375 sprintf(outputfile, "%s.h", app);
381 printf("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
383 printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n");
385 printf("Where: 'inputfile' is the TGA image to load\n");
386 printf(" 'outputvar' is the variable name to create\n");
387 printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n");
392 printf("Doing '%s' (%s) from '%s'...",
393 outputfile, varname, inputfile);
395 /* Import TGA logo */
398 if (image_load_tga (&rgb_logo, inputfile)<0)
400 printf("input file not found!\n");
404 /* Convert it to YUYV format */
407 image_rgb_to_yuyv (&rgb_logo, &yuyv_logo) ;
409 /* Save it into a header format */
412 image_save_header (&yuyv_logo, outputfile, varname) ;
414 /* Free original image and copy */
416 image_free (&rgb_logo);
417 image_free (&yuyv_logo);