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;
42 unsigned char Cb,y1,Cr,y2;
58 void StringUpperCase (char *str)
60 int count = strlen(str);
66 if ((c >= 'a')&&(c<='z'))
72 void StringLowerCase (char *str)
74 int count = strlen(str);
80 if ((c >= 'A')&&(c<='Z'))
85 void pixel_rgb_to_yuyv (rgb_t *rgb_pixel, yuyv_t *yuyv_pixel)
87 unsigned int pR, pG, pB ;
89 /* Transform (0-255) components to (0-100) */
90 pR = rgb_pixel->r * 100 / 255 ;
91 pG = rgb_pixel->g * 100 / 255 ;
92 pB = rgb_pixel->b * 100 / 255 ;
94 /* Calculate YUV values (0-255) from RGB beetween 0-100 */
95 yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16 ;
96 yuyv_pixel->Cb = pB - (pR/4) - (pG*3/4) + 128 ;
97 yuyv_pixel->Cr = pR - (pG*3/4) - (pB/4) + 128 ;
102 void printlogo_rgb (rgb_t *data, int w, int h)
107 for (x=0; x<w; x++, data++)
108 if ((data->r < 30)/*&&(data->g == 0)&&(data->b == 0)*/)
116 void printlogo_yuyv (unsigned short *data, int w, int h)
121 for (x=0; x<w; x++, data++)
122 if (*data == 0x1080) /* Because of inverted on i386! */
130 int image_load_tga (image_t *image, char *filename)
133 tga_header_t header ;
138 if( ( file = fopen( filename, "rb" ) ) == NULL )
141 fread(&header, sizeof(header), 1, file);
143 image->width = header.ImageWidth ;
144 image->height = header.ImageHeight ;
146 switch (header.ImageTypeCode){
147 case 2: /* Uncompressed RGB */
149 image->palette_size = 0 ;
150 image->palette = NULL ;
154 printf("Format not supported!\n");
158 image->bpp = header.ImagePixelSize ;
159 image->pixel_size = ((image->bpp-1) / 8) + 1 ;
160 image->pixels = image->width * image->height;
161 image->size = image->pixels * image->pixel_size ;
162 image->data = malloc(image->size) ;
164 if (image->bpp != 24)
166 printf("Bpp not supported: %d!\n", image->bpp);
170 fread(image->data, image->size, 1, file);
172 /* Swapping R and B values */
175 for(i=0; i < image->pixels; i++, p++)
184 if(!(header.ImageDescriptorByte & 0x20))
186 unsigned char *temp = malloc(image->size);
187 int linesize = image->pixel_size * image->width ;
188 void *dest = image->data,
189 *source = temp + image->size - linesize ;
194 printf("Cannot alloc temp buffer!\n");
198 memcpy(temp, image->data, image->size);
199 for(i = 0; i<image->height; i++, dest+=linesize, source-=linesize)
200 memcpy(dest, source, linesize);
205 #ifdef ENABLE_ASCII_BANNERS
206 printlogo_rgb (image->data,image->width, image->height);
213 int image_free (image_t *image)
215 if(image->data != NULL)
218 if(image->palette != NULL)
219 free(image->palette);
224 int image_rgb_to_yuyv (image_t *rgb_image, image_t *yuyv_image)
226 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data ;
228 unsigned short *dest ;
231 yuyv_image->pixel_size = 2 ;
232 yuyv_image->bpp = 16 ;
233 yuyv_image->yuyv = 1 ;
234 yuyv_image->width = rgb_image->width ;
235 yuyv_image->height = rgb_image->height ;
236 yuyv_image->pixels = yuyv_image->width * yuyv_image->height ;
237 yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size ;
238 dest = (unsigned short *) (yuyv_image->data = malloc(yuyv_image->size)) ;
239 yuyv_image->palette = 0 ;
240 yuyv_image->palette_size= 0 ;
242 while((count++) < rgb_image->pixels)
244 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
246 if ((count & 1)==0) /* Was == 0 */
247 memcpy (dest, ((void *)&yuyv) + 2, sizeof(short));
249 memcpy (dest, (void *)&yuyv, sizeof(short));
254 #ifdef ENABLE_ASCII_BANNERS
255 printlogo_yuyv (yuyv_image->data, yuyv_image->width, yuyv_image->height);
260 int image_save_header (image_t *image, char *filename, char *varname)
262 FILE *file = fopen (filename, "w");
263 char app[256], str[256]="", def_name[64] ;
264 int count = image->size, col=0;
265 unsigned char *dataptr = image->data ;
269 /* Author information */
270 fprintf(file, "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
271 fprintf(file, " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n", varname);
272 fprintf(file, " * Where:\t'screen'\tis the pointer to the frame buffer\n");
273 fprintf(file, " *\t\t'width'\tis the screen width\n");
274 fprintf(file, " *\t\t'x'\t\tis the horizontal position\n");
275 fprintf(file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
278 fprintf(file, "#include <video_easylogo.h>\n\n");
280 strcpy(def_name, varname);
281 StringUpperCase (def_name);
282 fprintf(file, "#define DEF_%s_WIDTH\t\t%d\n", def_name, image->width);
283 fprintf(file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name, image->height);
284 fprintf(file, "#define DEF_%s_PIXELS\t\t%d\n", def_name, image->pixels);
285 fprintf(file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
286 fprintf(file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name, image->pixel_size);
287 fprintf(file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name, image->size);
289 fprintf(file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n", def_name, def_name);
295 sprintf(str, " 0x%02x", *dataptr++);
301 fprintf(file, "%s", str);
311 sprintf(str, "%s, 0x%02x", app, *dataptr++);
318 fprintf(file, "%s\n", str);
320 /* End of declaration */
321 fprintf(file, "};\n\n");
323 fprintf(file, "fastimage_t %s = {\n", varname);
324 fprintf(file, " DEF_%s_DATA,\n", def_name);
325 fprintf(file, " DEF_%s_WIDTH,\n", def_name);
326 fprintf(file, " DEF_%s_HEIGHT,\n", def_name);
327 fprintf(file, " DEF_%s_BPP,\n", def_name);
328 fprintf(file, " DEF_%s_PIXEL_SIZE,\n", def_name);
329 fprintf(file, " DEF_%s_SIZE\n};\n", def_name);
336 #define DEF_FILELEN 256
338 int main (int argc, char *argv[])
341 inputfile[DEF_FILELEN],
342 outputfile[DEF_FILELEN],
343 varname[DEF_FILELEN];
345 image_t rgb_logo, yuyv_logo ;
351 strcpy (inputfile, argv[1]);
354 strcpy (varname, argv[2]);
357 char *dot = strchr(inputfile, '.');
358 int pos = dot - inputfile;
362 strncpy (varname, inputfile, pos);
368 strcpy (outputfile, argv[3]);
371 char *dot = strchr (varname, '.');
372 int pos = dot - varname;
376 char app[DEF_FILELEN] ;
378 strncpy(app, varname, pos);
380 sprintf(outputfile, "%s.h", app);
386 printf("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
388 printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n");
390 printf("Where: 'inputfile' is the TGA image to load\n");
391 printf(" 'outputvar' is the variable name to create\n");
392 printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n");
397 printf("Doing '%s' (%s) from '%s'...",
398 outputfile, varname, inputfile);
400 /* Import TGA logo */
403 if (image_load_tga (&rgb_logo, inputfile)<0)
405 printf("input file not found!\n");
409 /* Convert it to YUYV format */
412 image_rgb_to_yuyv (&rgb_logo, &yuyv_logo) ;
414 /* Save it into a header format */
417 image_save_header (&yuyv_logo, outputfile, varname) ;
419 /* Free original image and copy */
421 image_free (&rgb_logo);
422 image_free (&yuyv_logo);