Blame view

tools/easylogo/easylogo.c 13.6 KB
fe8c2806c   wdenk   Initial revision
1
2
3
4
5
  /*
  ** Easylogo TGA->header converter
  ** ==============================
  ** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
  ** AIRVENT SAM s.p.a - RIMINI(ITALY)
24113a44e   Mike Frysinger   easylogo: add opt...
6
  ** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
fe8c2806c   wdenk   Initial revision
7
8
9
  **
  ** This is still under construction!
  */
24113a44e   Mike Frysinger   easylogo: add opt...
10
  #include <errno.h>
edfed1d91   Mike Frysinger   easylogo: clean u...
11
12
  #include <getopt.h>
  #include <stdbool.h>
fe8c2806c   wdenk   Initial revision
13
  #include <stdio.h>
38d299c2d   Mike Frysinger   cleanup easylogo
14
15
  #include <stdlib.h>
  #include <string.h>
24113a44e   Mike Frysinger   easylogo: add opt...
16
17
  #include <unistd.h>
  #include <sys/stat.h>
fe8c2806c   wdenk   Initial revision
18
19
20
21
22
23
  
  #pragma pack(1)
  
  /*#define ENABLE_ASCII_BANNERS */
  
  typedef struct {
6007f3251   Wolfgang Denk   Coding Style clea...
24
25
26
27
28
29
30
31
32
33
34
35
  	unsigned char id;
  	unsigned char ColorMapType;
  	unsigned char ImageTypeCode;
  	unsigned short ColorMapOrigin;
  	unsigned short ColorMapLenght;
  	unsigned char ColorMapEntrySize;
  	unsigned short ImageXOrigin;
  	unsigned short ImageYOrigin;
  	unsigned short ImageWidth;
  	unsigned short ImageHeight;
  	unsigned char ImagePixelSize;
  	unsigned char ImageDescriptorByte;
fe8c2806c   wdenk   Initial revision
36
37
38
  } tga_header_t;
  
  typedef struct {
6007f3251   Wolfgang Denk   Coding Style clea...
39
40
  	unsigned char r, g, b;
  } rgb_t;
fe8c2806c   wdenk   Initial revision
41
42
  
  typedef struct {
6007f3251   Wolfgang Denk   Coding Style clea...
43
44
  	unsigned char b, g, r;
  } bgr_t;
fe8c2806c   wdenk   Initial revision
45
46
  
  typedef struct {
6007f3251   Wolfgang Denk   Coding Style clea...
47
48
  	unsigned char Cb, y1, Cr, y2;
  } yuyv_t;
fe8c2806c   wdenk   Initial revision
49
50
  
  typedef struct {
6007f3251   Wolfgang Denk   Coding Style clea...
51
52
53
  	void *data, *palette;
  	int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
  } image_t;
fe8c2806c   wdenk   Initial revision
54

24113a44e   Mike Frysinger   easylogo: add opt...
55
56
57
58
59
60
61
62
63
64
65
  void *xmalloc (size_t size)
  {
  	void *ret = malloc (size);
  	if (!ret) {
  		fprintf (stderr, "
  error: malloc(%zu) failed: %s",
  			size, strerror(errno));
  		exit (1);
  	}
  	return ret;
  }
fe8c2806c   wdenk   Initial revision
66
67
  void StringUpperCase (char *str)
  {
6007f3251   Wolfgang Denk   Coding Style clea...
68
69
70
71
72
73
74
75
76
  	int count = strlen (str);
  	char c;
  
  	while (count--) {
  		c = *str;
  		if ((c >= 'a') && (c <= 'z'))
  			*str = 'A' + (c - 'a');
  		str++;
  	}
fe8c2806c   wdenk   Initial revision
77
78
79
80
  }
  
  void StringLowerCase (char *str)
  {
6007f3251   Wolfgang Denk   Coding Style clea...
81
82
83
84
85
86
87
88
89
  	int count = strlen (str);
  	char c;
  
  	while (count--) {
  		c = *str;
  		if ((c >= 'A') && (c <= 'Z'))
  			*str = 'a' + (c - 'A');
  		str++;
  	}
fe8c2806c   wdenk   Initial revision
90
  }
6007f3251   Wolfgang Denk   Coding Style clea...
91
  void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
fe8c2806c   wdenk   Initial revision
92
  {
6007f3251   Wolfgang Denk   Coding Style clea...
93
  	unsigned int pR, pG, pB;
fe8c2806c   wdenk   Initial revision
94

6007f3251   Wolfgang Denk   Coding Style clea...
95
96
97
98
  	/* Transform (0-255) components to (0-100) */
  	pR = rgb_pixel->r * 100 / 255;
  	pG = rgb_pixel->g * 100 / 255;
  	pB = rgb_pixel->b * 100 / 255;
fe8c2806c   wdenk   Initial revision
99

6007f3251   Wolfgang Denk   Coding Style clea...
100
101
102
103
  	/* Calculate YUV values (0-255) from RGB beetween 0-100 */
  	yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16;
  	yuyv_pixel->Cb = pB - (pR / 4) - (pG * 3 / 4) + 128;
  	yuyv_pixel->Cr = pR - (pG * 3 / 4) - (pB / 4) + 128;
fe8c2806c   wdenk   Initial revision
104

6007f3251   Wolfgang Denk   Coding Style clea...
105
  	return;
fe8c2806c   wdenk   Initial revision
106
  }
6007f3251   Wolfgang Denk   Coding Style clea...
107
  void printlogo_rgb (rgb_t * data, int w, int h)
fe8c2806c   wdenk   Initial revision
108
  {
6007f3251   Wolfgang Denk   Coding Style clea...
109
110
111
112
113
114
115
116
117
118
119
120
  	int x, y;
  
  	for (y = 0; y < h; y++) {
  		for (x = 0; x < w; x++, data++)
  			if ((data->r <
  			     30) /*&&(data->g == 0)&&(data->b == 0) */ )
  				printf (" ");
  			else
  				printf ("X");
  		printf ("
  ");
  	}
fe8c2806c   wdenk   Initial revision
121
122
123
124
  }
  
  void printlogo_yuyv (unsigned short *data, int w, int h)
  {
6007f3251   Wolfgang Denk   Coding Style clea...
125
126
127
128
129
130
131
132
133
134
135
  	int x, y;
  
  	for (y = 0; y < h; y++) {
  		for (x = 0; x < w; x++, data++)
  			if (*data == 0x1080)	/* Because of inverted on i386! */
  				printf (" ");
  			else
  				printf ("X");
  		printf ("
  ");
  	}
fe8c2806c   wdenk   Initial revision
136
  }
fc6414eca   Mike Frysinger   fix easylogo on b...
137
138
  static inline unsigned short le16_to_cpu (unsigned short val)
  {
6007f3251   Wolfgang Denk   Coding Style clea...
139
140
141
142
143
144
145
  	union {
  		unsigned char pval[2];
  		unsigned short val;
  	} swapped;
  
  	swapped.val = val;
  	return (swapped.pval[1] << 8) + swapped.pval[0];
fc6414eca   Mike Frysinger   fix easylogo on b...
146
  }
6007f3251   Wolfgang Denk   Coding Style clea...
147
  int image_load_tga (image_t * image, char *filename)
fe8c2806c   wdenk   Initial revision
148
  {
6007f3251   Wolfgang Denk   Coding Style clea...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  	FILE *file;
  	tga_header_t header;
  	int i;
  	unsigned char app;
  	rgb_t *p;
  
  	if ((file = fopen (filename, "rb")) == NULL)
  		return -1;
  
  	fread (&header, sizeof (header), 1, file);
  
  	/* byte swap: tga is little endian, host is ??? */
  	header.ColorMapOrigin = le16_to_cpu (header.ColorMapOrigin);
  	header.ColorMapLenght = le16_to_cpu (header.ColorMapLenght);
  	header.ImageXOrigin = le16_to_cpu (header.ImageXOrigin);
  	header.ImageYOrigin = le16_to_cpu (header.ImageYOrigin);
  	header.ImageWidth = le16_to_cpu (header.ImageWidth);
  	header.ImageHeight = le16_to_cpu (header.ImageHeight);
  
  	image->width = header.ImageWidth;
  	image->height = header.ImageHeight;
  
  	switch (header.ImageTypeCode) {
  	case 2:		/* Uncompressed RGB */
  		image->yuyv = 0;
  		image->palette_size = 0;
  		image->palette = NULL;
  		break;
fe8c2806c   wdenk   Initial revision
177
178
  
  	default:
6007f3251   Wolfgang Denk   Coding Style clea...
179
180
181
182
  		printf ("Format not supported!
  ");
  		return -1;
  	}
fe8c2806c   wdenk   Initial revision
183

6007f3251   Wolfgang Denk   Coding Style clea...
184
185
186
187
  	image->bpp = header.ImagePixelSize;
  	image->pixel_size = ((image->bpp - 1) / 8) + 1;
  	image->pixels = image->width * image->height;
  	image->size = image->pixels * image->pixel_size;
24113a44e   Mike Frysinger   easylogo: add opt...
188
  	image->data = xmalloc (image->size);
fe8c2806c   wdenk   Initial revision
189

6007f3251   Wolfgang Denk   Coding Style clea...
190
191
192
193
194
  	if (image->bpp != 24) {
  		printf ("Bpp not supported: %d!
  ", image->bpp);
  		return -1;
  	}
fe8c2806c   wdenk   Initial revision
195

6007f3251   Wolfgang Denk   Coding Style clea...
196
  	fread (image->data, image->size, 1, file);
fe8c2806c   wdenk   Initial revision
197
198
  
  /* Swapping R and B values */
6007f3251   Wolfgang Denk   Coding Style clea...
199
200
201
202
203
204
  	p = image->data;
  	for (i = 0; i < image->pixels; i++, p++) {
  		app = p->r;
  		p->r = p->b;
  		p->b = app;
  	}
fe8c2806c   wdenk   Initial revision
205
206
  
  /* Swapping image */
6007f3251   Wolfgang Denk   Coding Style clea...
207
  	if (!(header.ImageDescriptorByte & 0x20)) {
24113a44e   Mike Frysinger   easylogo: add opt...
208
  		unsigned char *temp = xmalloc (image->size);
6007f3251   Wolfgang Denk   Coding Style clea...
209
210
211
  		int linesize = image->pixel_size * image->width;
  		void *dest = image->data,
  			*source = temp + image->size - linesize;
fe8c2806c   wdenk   Initial revision
212

6007f3251   Wolfgang Denk   Coding Style clea...
213
214
215
216
217
218
  		printf ("S");
  		if (temp == NULL) {
  			printf ("Cannot alloc temp buffer!
  ");
  			return -1;
  		}
fe8c2806c   wdenk   Initial revision
219

6007f3251   Wolfgang Denk   Coding Style clea...
220
221
222
223
  		memcpy (temp, image->data, image->size);
  		for (i = 0; i < image->height;
  		     i++, dest += linesize, source -= linesize)
  			memcpy (dest, source, linesize);
fe8c2806c   wdenk   Initial revision
224

6007f3251   Wolfgang Denk   Coding Style clea...
225
226
  		free (temp);
  	}
fe8c2806c   wdenk   Initial revision
227
  #ifdef ENABLE_ASCII_BANNERS
6007f3251   Wolfgang Denk   Coding Style clea...
228
  	printlogo_rgb (image->data, image->width, image->height);
fe8c2806c   wdenk   Initial revision
229
  #endif
6007f3251   Wolfgang Denk   Coding Style clea...
230
231
  	fclose (file);
  	return 0;
fe8c2806c   wdenk   Initial revision
232
  }
edfed1d91   Mike Frysinger   easylogo: clean u...
233
  void image_free (image_t * image)
fe8c2806c   wdenk   Initial revision
234
  {
edfed1d91   Mike Frysinger   easylogo: clean u...
235
236
  	free (image->data);
  	free (image->palette);
fe8c2806c   wdenk   Initial revision
237
  }
6007f3251   Wolfgang Denk   Coding Style clea...
238
  int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
fe8c2806c   wdenk   Initial revision
239
  {
6007f3251   Wolfgang Denk   Coding Style clea...
240
241
242
243
244
245
246
247
248
249
250
251
252
  	rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
  	yuyv_t yuyv;
  	unsigned short *dest;
  	int count = 0;
  
  	yuyv_image->pixel_size = 2;
  	yuyv_image->bpp = 16;
  	yuyv_image->yuyv = 1;
  	yuyv_image->width = rgb_image->width;
  	yuyv_image->height = rgb_image->height;
  	yuyv_image->pixels = yuyv_image->width * yuyv_image->height;
  	yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size;
  	dest = (unsigned short *) (yuyv_image->data =
24113a44e   Mike Frysinger   easylogo: add opt...
253
  				   xmalloc (yuyv_image->size));
6007f3251   Wolfgang Denk   Coding Style clea...
254
255
256
257
  	yuyv_image->palette = 0;
  	yuyv_image->palette_size = 0;
  
  	while ((count++) < rgb_image->pixels) {
fe8c2806c   wdenk   Initial revision
258
  		pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
6007f3251   Wolfgang Denk   Coding Style clea...
259
260
  		if ((count & 1) == 0)	/* Was == 0 */
  			memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
fe8c2806c   wdenk   Initial revision
261
  		else
6007f3251   Wolfgang Denk   Coding Style clea...
262
  			memcpy (dest, (void *) &yuyv, sizeof (short));
fe8c2806c   wdenk   Initial revision
263

6007f3251   Wolfgang Denk   Coding Style clea...
264
  		dest++;
fe8c2806c   wdenk   Initial revision
265
266
267
  	}
  
  #ifdef ENABLE_ASCII_BANNERS
6007f3251   Wolfgang Denk   Coding Style clea...
268
269
  	printlogo_yuyv (yuyv_image->data, yuyv_image->width,
  			yuyv_image->height);
fe8c2806c   wdenk   Initial revision
270
  #endif
6007f3251   Wolfgang Denk   Coding Style clea...
271
  	return 0;
fe8c2806c   wdenk   Initial revision
272
  }
7293e0577   Michael Hennerich   easylogo: add sup...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  int image_rgb888_to_rgb565(image_t *rgb888_image, image_t *rgb565_image)
  {
  	rgb_t *rgb_ptr = (rgb_t *) rgb888_image->data;
  	unsigned short *dest;
  	int count = 0;
  
  	rgb565_image->pixel_size = 2;
  	rgb565_image->bpp = 16;
  	rgb565_image->yuyv = 0;
  	rgb565_image->width = rgb888_image->width;
  	rgb565_image->height = rgb888_image->height;
  	rgb565_image->pixels = rgb565_image->width * rgb565_image->height;
  	rgb565_image->size = rgb565_image->pixels * rgb565_image->pixel_size;
  	dest = (unsigned short *) (rgb565_image->data =
  				   xmalloc(rgb565_image->size));
  	rgb565_image->palette = 0;
  	rgb565_image->palette_size = 0;
  
  	while ((count++) < rgb888_image->pixels) {
  
  		*dest++ = ((rgb_ptr->b & 0xF8) << 8) |
  			((rgb_ptr->g & 0xFC) << 3) |
  			(rgb_ptr->r >> 3);
  		rgb_ptr++;
  	}
  
  	return 0;
  }
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
301
302
303
304
305
306
307
  enum comp_t {
  	COMP_NONE,
  	COMP_GZIP,
  	COMP_LZMA,
  };
  static enum comp_t compression = COMP_NONE;
  static bool bss_storage = false;
24113a44e   Mike Frysinger   easylogo: add opt...
308

6007f3251   Wolfgang Denk   Coding Style clea...
309
  int image_save_header (image_t * image, char *filename, char *varname)
fe8c2806c   wdenk   Initial revision
310
  {
6007f3251   Wolfgang Denk   Coding Style clea...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  	FILE *file = fopen (filename, "w");
  	char app[256], str[256] = "", def_name[64];
  	int count = image->size, col = 0;
  	unsigned char *dataptr = image->data;
  
  	if (file == NULL)
  		return -1;
  
  	/*  Author information */
  	fprintf (file,
  		 "/*
   * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi
   *
  ");
  	fprintf (file,
  		 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)
   *
  ",
  		 varname);
  	fprintf (file,
  		 " * Where:\t'screen'\tis the pointer to the frame buffer
  ");
  	fprintf (file, " *\t\t'width'\tis the screen width
  ");
  	fprintf (file, " *\t\t'x'\t\tis the horizontal position
  ");
  	fprintf (file, " *\t\t'y'\t\tis the vertical position
   */
  
  ");
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
341
342
  	/* image compress */
  	if (compression != COMP_NONE) {
24113a44e   Mike Frysinger   easylogo: add opt...
343
344
  		const char *errstr = NULL;
  		unsigned char *compressed;
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
345
  		const char *comp_name;
24113a44e   Mike Frysinger   easylogo: add opt...
346
  		struct stat st;
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  		FILE *compfp;
  		size_t filename_len = strlen(filename);
  		char *compfilename = xmalloc(filename_len + 20);
  		char *compcmd = xmalloc(filename_len + 50);
  
  		sprintf(compfilename, "%s.bin", filename);
  		switch (compression) {
  		case COMP_GZIP:
  			strcpy(compcmd, "gzip");
  			comp_name = "GZIP";
  			break;
  		case COMP_LZMA:
  			strcpy(compcmd, "lzma");
  			comp_name = "LZMA";
  			break;
  		default:
  			errstr = "
  error: unknown compression method";
  			goto done;
  		}
  		strcat(compcmd, " > ");
  		strcat(compcmd, compfilename);
  		compfp = popen(compcmd, "w");
  		if (!compfp) {
24113a44e   Mike Frysinger   easylogo: add opt...
371
372
373
374
  			errstr = "
  error: popen() failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
375
  		if (fwrite(image->data, image->size, 1, compfp) != 1) {
24113a44e   Mike Frysinger   easylogo: add opt...
376
377
378
379
  			errstr = "
  error: writing data to gzip failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
380
  		if (pclose(compfp)) {
24113a44e   Mike Frysinger   easylogo: add opt...
381
382
383
384
  			errstr = "
  error: gzip process failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
385
386
  		compfp = fopen(compfilename, "r");
  		if (!compfp) {
24113a44e   Mike Frysinger   easylogo: add opt...
387
388
389
390
  			errstr = "
  error: open() on gzip data failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
391
  		if (stat(compfilename, &st)) {
24113a44e   Mike Frysinger   easylogo: add opt...
392
393
394
395
  			errstr = "
  error: stat() on gzip file failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
396
397
  		compressed = xmalloc(st.st_size);
  		if (fread(compressed, st.st_size, 1, compfp) != 1) {
24113a44e   Mike Frysinger   easylogo: add opt...
398
399
400
401
  			errstr = "
  error: reading gzip data failed";
  			goto done;
  		}
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
402
  		fclose(compfp);
24113a44e   Mike Frysinger   easylogo: add opt...
403

1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
404
  		unlink(compfilename);
24113a44e   Mike Frysinger   easylogo: add opt...
405
406
407
  
  		dataptr = compressed;
  		count = st.st_size;
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
408
409
410
411
  		fprintf(file, "#define EASYLOGO_ENABLE_%s %i
  
  ", comp_name, count);
  		if (bss_storage)
24113a44e   Mike Frysinger   easylogo: add opt...
412
413
414
415
416
  			fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];
  
  ", image->size);
  
   done:
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
417
418
  		free(compfilename);
  		free(compcmd);
24113a44e   Mike Frysinger   easylogo: add opt...
419
420
421
422
423
424
  
  		if (errstr) {
  			perror (errstr);
  			return -1;
  		}
  	}
6007f3251   Wolfgang Denk   Coding Style clea...
425
426
427
428
429
430
  	/*	Headers */
  	fprintf (file, "#include <video_easylogo.h>
  
  ");
  	/*	Macros */
  	strcpy (def_name, varname);
fe8c2806c   wdenk   Initial revision
431
  	StringUpperCase (def_name);
6007f3251   Wolfgang Denk   Coding Style clea...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  	fprintf (file, "#define	DEF_%s_WIDTH\t\t%d
  ", def_name,
  		 image->width);
  	fprintf (file, "#define	DEF_%s_HEIGHT\t\t%d
  ", def_name,
  		 image->height);
  	fprintf (file, "#define	DEF_%s_PIXELS\t\t%d
  ", def_name,
  		 image->pixels);
  	fprintf (file, "#define	DEF_%s_BPP\t\t%d
  ", def_name, image->bpp);
  	fprintf (file, "#define	DEF_%s_PIXEL_SIZE\t%d
  ", def_name,
  		 image->pixel_size);
  	fprintf (file, "#define	DEF_%s_SIZE\t\t%d
  
  ", def_name,
  		 image->size);
  	/*  Declaration */
24113a44e   Mike Frysinger   easylogo: add opt...
451
452
453
  	fprintf (file, "unsigned char DEF_%s_DATA[] = {
  ",
  		 def_name);
6007f3251   Wolfgang Denk   Coding Style clea...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
  
  	/*	Data */
  	while (count)
  		switch (col) {
  		case 0:
  			sprintf (str, " 0x%02x", *dataptr++);
  			col++;
  			count--;
  			break;
  
  		case 16:
  			fprintf (file, "%s", str);
  			if (count > 0)
  				fprintf (file, ",");
  			fprintf (file, "
  ");
  
  			col = 0;
  			break;
  
  		default:
  			strcpy (app, str);
  			sprintf (str, "%s, 0x%02x", app, *dataptr++);
  			col++;
  			count--;
  			break;
fe8c2806c   wdenk   Initial revision
480
481
482
  		}
  
  	if (col)
6007f3251   Wolfgang Denk   Coding Style clea...
483
484
  		fprintf (file, "%s
  ", str);
53677ef18   Wolfgang Denk   Big white-space c...
485
  	/*	End of declaration */
6007f3251   Wolfgang Denk   Coding Style clea...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  	fprintf (file, "};
  
  ");
  	/*	Variable */
  	fprintf (file, "fastimage_t %s = {
  ", varname);
  	fprintf (file, "		DEF_%s_DATA,
  ", def_name);
  	fprintf (file, "		DEF_%s_WIDTH,
  ", def_name);
  	fprintf (file, "		DEF_%s_HEIGHT,
  ", def_name);
  	fprintf (file, "		DEF_%s_BPP,
  ", def_name);
  	fprintf (file, "		DEF_%s_PIXEL_SIZE,
  ", def_name);
  	fprintf (file, "		DEF_%s_SIZE
  };
  ", def_name);
fe8c2806c   wdenk   Initial revision
505
506
  
  	fclose (file);
6007f3251   Wolfgang Denk   Coding Style clea...
507
  	return 0;
fe8c2806c   wdenk   Initial revision
508
509
510
  }
  
  #define DEF_FILELEN	256
edfed1d91   Mike Frysinger   easylogo: clean u...
511
512
513
514
515
516
517
518
519
520
521
522
523
  static void usage (int exit_status)
  {
  	puts (
  		"EasyLogo 1.0 (C) 2000 by Paolo Scaffardi
  "
  		"
  "
  		"Syntax:	easylogo [options] inputfile [outputvar [outputfile]]
  "
  		"
  "
  		"Options:
  "
7293e0577   Michael Hennerich   easylogo: add sup...
524
525
526
527
  		"  -r     Output RGB888 instead of YUYV
  "
  		"  -s     Output RGB565 instead of YUYV
  "
24113a44e   Mike Frysinger   easylogo: add opt...
528
529
  		"  -g     Compress with gzip
  "
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
530
531
  		"  -l     Compress with lzma
  "
24113a44e   Mike Frysinger   easylogo: add opt...
532
533
  		"  -b     Preallocate space in bss for decompressing image
  "
edfed1d91   Mike Frysinger   easylogo: clean u...
534
535
536
537
538
539
540
541
542
543
544
545
  		"  -h     Help output
  "
  		"
  "
  		"Where: 'inputfile'   is the TGA image to load
  "
  		"       'outputvar'   is the variable name to create
  "
  		"       'outputfile'  is the output header file (default is 'inputfile.h')"
  	);
  	exit (exit_status);
  }
fe8c2806c   wdenk   Initial revision
546
547
  int main (int argc, char *argv[])
  {
edfed1d91   Mike Frysinger   easylogo: clean u...
548
  	int c;
7293e0577   Michael Hennerich   easylogo: add sup...
549
550
  	bool use_rgb888 = false;
  	bool use_rgb565 = false;
6007f3251   Wolfgang Denk   Coding Style clea...
551
552
  	char inputfile[DEF_FILELEN],
  		outputfile[DEF_FILELEN], varname[DEF_FILELEN];
7293e0577   Michael Hennerich   easylogo: add sup...
553
  	image_t rgb888_logo, rgb565_logo, yuyv_logo;
6007f3251   Wolfgang Denk   Coding Style clea...
554

1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
555
  	while ((c = getopt(argc, argv, "hrsglb")) > 0) {
edfed1d91   Mike Frysinger   easylogo: clean u...
556
557
558
559
560
  		switch (c) {
  		case 'h':
  			usage (0);
  			break;
  		case 'r':
7293e0577   Michael Hennerich   easylogo: add sup...
561
562
563
564
565
566
  			use_rgb888 = true;
  			puts("Using 24-bit RGB888 Output Fromat");
  			break;
  		case 's':
  			use_rgb565 = true;
  			puts("Using 16-bit RGB565 Output Fromat");
edfed1d91   Mike Frysinger   easylogo: clean u...
567
  			break;
24113a44e   Mike Frysinger   easylogo: add opt...
568
  		case 'g':
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
569
570
571
572
573
574
  			compression = COMP_GZIP;
  			puts("Compressing with gzip");
  			break;
  		case 'l':
  			compression = COMP_LZMA;
  			puts("Compressing with lzma");
24113a44e   Mike Frysinger   easylogo: add opt...
575
576
  			break;
  		case 'b':
1a3cb4ad6   Mike Frysinger   easylogo: add lzm...
577
578
  			bss_storage = true;
  			puts("Preallocating bss space for decompressing image");
24113a44e   Mike Frysinger   easylogo: add opt...
579
  			break;
edfed1d91   Mike Frysinger   easylogo: clean u...
580
581
582
  		default:
  			usage (1);
  			break;
6007f3251   Wolfgang Denk   Coding Style clea...
583
  		}
edfed1d91   Mike Frysinger   easylogo: clean u...
584
  	}
fe8c2806c   wdenk   Initial revision
585

edfed1d91   Mike Frysinger   easylogo: clean u...
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
  	c = argc - optind;
  	if (c > 4 || c < 1)
  		usage (1);
  
  	strcpy (inputfile, argv[optind]);
  
  	if (c > 1)
  		strcpy (varname, argv[optind + 1]);
  	else {
  		/* transform "input.tga" to just "input" */
  		char *dot;
  		strcpy (varname, inputfile);
  		dot = strchr (varname, '.');
  		if (dot)
  			*dot = '\0';
  	}
fe8c2806c   wdenk   Initial revision
602

edfed1d91   Mike Frysinger   easylogo: clean u...
603
604
605
606
607
608
  	if (c > 2)
  		strcpy (outputfile, argv[optind + 2]);
  	else {
  		/* just append ".h" to input file name */
  		strcpy (outputfile, inputfile);
  		strcat (outputfile, ".h");
6007f3251   Wolfgang Denk   Coding Style clea...
609
  	}
fe8c2806c   wdenk   Initial revision
610

edfed1d91   Mike Frysinger   easylogo: clean u...
611
612
  	/* Make sure the output is sent as soon as we printf() */
  	setbuf(stdout, NULL);
6007f3251   Wolfgang Denk   Coding Style clea...
613
614
  	printf ("Doing '%s' (%s) from '%s'...",
  		outputfile, varname, inputfile);
fe8c2806c   wdenk   Initial revision
615

6007f3251   Wolfgang Denk   Coding Style clea...
616
  	/* Import TGA logo */
fe8c2806c   wdenk   Initial revision
617

6007f3251   Wolfgang Denk   Coding Style clea...
618
  	printf ("L");
7293e0577   Michael Hennerich   easylogo: add sup...
619
  	if (image_load_tga(&rgb888_logo, inputfile) < 0) {
6007f3251   Wolfgang Denk   Coding Style clea...
620
621
622
623
  		printf ("input file not found!
  ");
  		exit (1);
  	}
fe8c2806c   wdenk   Initial revision
624

7293e0577   Michael Hennerich   easylogo: add sup...
625
  	/* Convert, save, and free the image */
fe8c2806c   wdenk   Initial revision
626

7293e0577   Michael Hennerich   easylogo: add sup...
627
  	if (!use_rgb888 && !use_rgb565) {
edfed1d91   Mike Frysinger   easylogo: clean u...
628
  		printf ("C");
7293e0577   Michael Hennerich   easylogo: add sup...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  		image_rgb_to_yuyv(&rgb888_logo, &yuyv_logo);
  
  		printf("S");
  		image_save_header(&yuyv_logo, outputfile, varname);
  		image_free(&yuyv_logo);
  	} else if (use_rgb565) {
  		printf("C");
  		image_rgb888_to_rgb565(&rgb888_logo, &rgb565_logo);
  
  		printf("S");
  		image_save_header(&rgb565_logo, outputfile, varname);
  		image_free(&rgb565_logo);
  	} else {
  		printf("S");
  		image_save_header(&rgb888_logo, outputfile, varname);
edfed1d91   Mike Frysinger   easylogo: clean u...
644
  	}
fe8c2806c   wdenk   Initial revision
645

6007f3251   Wolfgang Denk   Coding Style clea...
646
  	/* Free original image and copy */
fe8c2806c   wdenk   Initial revision
647

7293e0577   Michael Hennerich   easylogo: add sup...
648
  	image_free(&rgb888_logo);
fe8c2806c   wdenk   Initial revision
649

6007f3251   Wolfgang Denk   Coding Style clea...
650
651
  	printf ("
  ");
fe8c2806c   wdenk   Initial revision
652

6007f3251   Wolfgang Denk   Coding Style clea...
653
  	return 0;
fe8c2806c   wdenk   Initial revision
654
  }