blob: 9f1d1fff0821cfb2f158c7cd40f69a426bc08d08 [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>
11
12#pragma pack(1)
13
14/*#define ENABLE_ASCII_BANNERS */
15
16typedef struct {
17 unsigned char id;
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;
29} tga_header_t;
30
31typedef struct {
32 unsigned char r,g,b ;
33} rgb_t ;
34
35typedef struct {
36 unsigned char b,g,r ;
37} bgr_t ;
38
39typedef struct {
40 unsigned char Cb,y1,Cr,y2;
41} yuyv_t ;
42
43typedef struct {
44 unsigned char *data,
45 *palette ;
46 int width,
47 height,
48 pixels,
49 bpp,
50 pixel_size,
51 size,
52 palette_size,
53 yuyv;
54} image_t ;
55
56void StringUpperCase (char *str)
57{
58 int count = strlen(str);
59 char c ;
60
61 while(count--)
62 {
63 c=*str;
64 if ((c >= 'a')&&(c<='z'))
65 *str = 'A' + (c-'a');
66 str++ ;
67 }
68}
69
70void StringLowerCase (char *str)
71{
72 int count = strlen(str);
73 char c ;
74
75 while(count--)
76 {
77 c=*str;
78 if ((c >= 'A')&&(c<='Z'))
79 *str = 'a' + (c-'A');
80 str++ ;
81 }
82}
83void pixel_rgb_to_yuyv (rgb_t *rgb_pixel, yuyv_t *yuyv_pixel)
84{
85 unsigned int pR, pG, pB ;
86
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 ;
91
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 ;
96
97 return ;
98}
99
100void printlogo_rgb (rgb_t *data, int w, int h)
101{
102 int x,y;
103 for (y=0; y<h; y++)
104 {
105 for (x=0; x<w; x++, data++)
106 if ((data->r < 30)/*&&(data->g == 0)&&(data->b == 0)*/)
107 printf(" ");
108 else
109 printf("X");
wdenk8bde7f72003-06-27 21:31:46 +0000110 printf("\n");
wdenkfe8c2802002-11-03 00:38:21 +0000111 }
112}
113
114void printlogo_yuyv (unsigned short *data, int w, int h)
115{
116 int x,y;
117 for (y=0; y<h; y++)
118 {
119 for (x=0; x<w; x++, data++)
120 if (*data == 0x1080) /* Because of inverted on i386! */
121 printf(" ");
122 else
123 printf("X");
wdenk8bde7f72003-06-27 21:31:46 +0000124 printf("\n");
wdenkfe8c2802002-11-03 00:38:21 +0000125 }
126}
127
128int image_load_tga (image_t *image, char *filename)
129{
130 FILE *file ;
131 tga_header_t header ;
132 int i;
133 unsigned char app ;
134 rgb_t *p ;
135
136 if( ( file = fopen( filename, "rb" ) ) == NULL )
wdenk8bde7f72003-06-27 21:31:46 +0000137 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000138
139 fread(&header, sizeof(header), 1, file);
140
141 image->width = header.ImageWidth ;
142 image->height = header.ImageHeight ;
143
144 switch (header.ImageTypeCode){
145 case 2: /* Uncompressed RGB */
146 image->yuyv = 0 ;
147 image->palette_size = 0 ;
148 image->palette = NULL ;
wdenk8bde7f72003-06-27 21:31:46 +0000149 break;
wdenkfe8c2802002-11-03 00:38:21 +0000150
151 default:
152 printf("Format not supported!\n");
153 return -1 ;
154 }
155
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) ;
161
162 if (image->bpp != 24)
163 {
164 printf("Bpp not supported: %d!\n", image->bpp);
165 return -1 ;
166 }
167
168 fread(image->data, image->size, 1, file);
169
170/* Swapping R and B values */
171
172 p = image->data ;
173 for(i=0; i < image->pixels; i++, p++)
174 {
175 app = p->r ;
176 p->r = p->b ;
177 p->b = app ;
178 }
179
180/* Swapping image */
181
182 if(!(header.ImageDescriptorByte & 0x20))
183 {
wdenk8bde7f72003-06-27 21:31:46 +0000184 unsigned char *temp = malloc(image->size);
185 int linesize = image->pixel_size * image->width ;
wdenkfe8c2802002-11-03 00:38:21 +0000186 void *dest = image->data,
187 *source = temp + image->size - linesize ;
188
wdenk8bde7f72003-06-27 21:31:46 +0000189 printf("S");
wdenkfe8c2802002-11-03 00:38:21 +0000190 if (temp == NULL)
191 {
192 printf("Cannot alloc temp buffer!\n");
193 return -1;
194 }
195
wdenk8bde7f72003-06-27 21:31:46 +0000196 memcpy(temp, image->data, image->size);
wdenkfe8c2802002-11-03 00:38:21 +0000197 for(i = 0; i<image->height; i++, dest+=linesize, source-=linesize)
198 memcpy(dest, source, linesize);
199
200 free( temp );
201 }
202
203#ifdef ENABLE_ASCII_BANNERS
204 printlogo_rgb (image->data,image->width, image->height);
205#endif
206
207 fclose (file);
208 return 0;
209}
210
211int image_free (image_t *image)
212{
213 if(image->data != NULL)
214 free(image->data);
215
216 if(image->palette != NULL)
217 free(image->palette);
218
219 return 0;
220}
221
222int image_rgb_to_yuyv (image_t *rgb_image, image_t *yuyv_image)
223{
224 rgb_t *rgb_ptr = (rgb_t *) rgb_image->data ;
225 yuyv_t yuyv ;
226 unsigned short *dest ;
227 int count = 0 ;
228
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 ;
239
240 while((count++) < rgb_image->pixels)
241 {
242 pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
243
244 if ((count & 1)==0) /* Was == 0 */
wdenk8bde7f72003-06-27 21:31:46 +0000245 memcpy (dest, ((void *)&yuyv) + 2, sizeof(short));
wdenkfe8c2802002-11-03 00:38:21 +0000246 else
247 memcpy (dest, (void *)&yuyv, sizeof(short));
248
249 dest ++ ;
250 }
251
252#ifdef ENABLE_ASCII_BANNERS
253 printlogo_yuyv (yuyv_image->data, yuyv_image->width, yuyv_image->height);
254#endif
255 return 0 ;
256}
257
258int image_save_header (image_t *image, char *filename, char *varname)
259{
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 ;
264 if (file==NULL)
265 return -1 ;
266
wdenk47cd00f2003-03-06 13:39:27 +0000267/* Author information */
Marian Balakowicz4c15ef52006-09-01 19:44:05 +0200268 fprintf(file, "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
wdenkfe8c2802002-11-03 00:38:21 +0000269 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");
274
275/* Headers */
276 fprintf(file, "#include <video_easylogo.h>\n\n");
277/* Macros */
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);
286/* Declaration */
287 fprintf(file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n", def_name, def_name);
288
289/* Data */
290 while(count)
291 switch (col){
292 case 0:
293 sprintf(str, " 0x%02x", *dataptr++);
294 col++;
295 count-- ;
296 break;
297
298 case 16:
299 fprintf(file, "%s", str);
300 if (count > 0)
301 fprintf(file,",");
302 fprintf(file, "\n");
303
304 col = 0 ;
305 break;
306
307 default:
308 strcpy(app, str);
309 sprintf(str, "%s, 0x%02x", app, *dataptr++);
310 col++ ;
311 count-- ;
312 break;
313 }
314
315 if (col)
316 fprintf(file, "%s\n", str);
317
318/* End of declaration */
319 fprintf(file, "};\n\n");
320/* Variable */
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);
328
329 fclose (file);
330
331 return 0 ;
332}
333
334#define DEF_FILELEN 256
335
336int main (int argc, char *argv[])
337{
338 char
339 inputfile[DEF_FILELEN],
340 outputfile[DEF_FILELEN],
341 varname[DEF_FILELEN];
342
343 image_t rgb_logo, yuyv_logo ;
344
345 switch (argc){
346 case 2:
347 case 3:
348 case 4:
wdenk8bde7f72003-06-27 21:31:46 +0000349 strcpy (inputfile, argv[1]);
wdenkfe8c2802002-11-03 00:38:21 +0000350
351 if (argc > 2)
352 strcpy (varname, argv[2]);
353 else
354 {
355 int pos = strchr(inputfile, '.');
356
357 if (pos >= 0)
358 {
359 strncpy (varname, inputfile, pos);
360 varname[pos] = 0 ;
361 }
362 }
363
364 if (argc > 3)
365 strcpy (outputfile, argv[3]);
366 else
367 {
368 int pos = strchr (varname, '.');
369
370 if (pos > 0)
371 {
372 char app[DEF_FILELEN] ;
373
374 strncpy(app, varname, pos);
375 sprintf(outputfile, "%s.h", app);
376 }
377 }
wdenk8bde7f72003-06-27 21:31:46 +0000378 break;
wdenkfe8c2802002-11-03 00:38:21 +0000379
380 default:
wdenk8bde7f72003-06-27 21:31:46 +0000381 printf("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
wdenkfe8c2802002-11-03 00:38:21 +0000382
wdenk8bde7f72003-06-27 21:31:46 +0000383 printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n");
384 printf("\n");
385 printf("Where: 'inputfile' is the TGA image to load\n");
wdenkfe8c2802002-11-03 00:38:21 +0000386 printf(" 'outputvar' is the variable name to create\n");
387 printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n");
388
389 return -1 ;
390 }
391
392 printf("Doing '%s' (%s) from '%s'...",
393 outputfile, varname, inputfile);
394
395/* Import TGA logo */
396
397 printf("L");
398 if (image_load_tga (&rgb_logo, inputfile)<0)
399 {
400 printf("input file not found!\n");
wdenk8bde7f72003-06-27 21:31:46 +0000401 exit(1);
wdenkfe8c2802002-11-03 00:38:21 +0000402 }
403
404/* Convert it to YUYV format */
405
406 printf("C");
407 image_rgb_to_yuyv (&rgb_logo, &yuyv_logo) ;
408
409/* Save it into a header format */
410
411 printf("S");
412 image_save_header (&yuyv_logo, outputfile, varname) ;
413
414/* Free original image and copy */
415
416 image_free (&rgb_logo);
417 image_free (&yuyv_logo);
418
419 printf("\n");
420
421 return 0 ;
422}