easylogo.c 11.8 KB
Newer Older
W
wdenk 已提交
1 2 3 4 5
/*
** Easylogo TGA->header converter
** ==============================
** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
** AIRVENT SAM s.p.a - RIMINI(ITALY)
6
** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
W
wdenk 已提交
7 8 9 10
**
** This is still under construction!
*/

11
#include <errno.h>
12 13
#include <getopt.h>
#include <stdbool.h>
W
wdenk 已提交
14
#include <stdio.h>
M
Mike Frysinger 已提交
15 16
#include <stdlib.h>
#include <string.h>
17 18
#include <unistd.h>
#include <sys/stat.h>
W
wdenk 已提交
19 20 21 22 23 24

#pragma pack(1)

/*#define ENABLE_ASCII_BANNERS */

typedef struct {
25 26 27 28 29 30 31 32 33 34 35 36
	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;
W
wdenk 已提交
37 38 39
} tga_header_t;

typedef struct {
40 41
	unsigned char r, g, b;
} rgb_t;
W
wdenk 已提交
42 43

typedef struct {
44 45
	unsigned char b, g, r;
} bgr_t;
W
wdenk 已提交
46 47

typedef struct {
48 49
	unsigned char Cb, y1, Cr, y2;
} yuyv_t;
W
wdenk 已提交
50 51

typedef struct {
52 53 54
	void *data, *palette;
	int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
} image_t;
W
wdenk 已提交
55

56 57 58 59 60 61 62 63 64 65 66
void *xmalloc (size_t size)
{
	void *ret = malloc (size);
	if (!ret) {
		fprintf (stderr, "\nerror: malloc(%zu) failed: %s",
			size, strerror(errno));
		exit (1);
	}
	return ret;
}

W
wdenk 已提交
67 68
void StringUpperCase (char *str)
{
69 70 71 72 73 74 75 76 77
	int count = strlen (str);
	char c;

	while (count--) {
		c = *str;
		if ((c >= 'a') && (c <= 'z'))
			*str = 'A' + (c - 'a');
		str++;
	}
W
wdenk 已提交
78 79 80 81
}

void StringLowerCase (char *str)
{
82 83 84 85 86 87 88 89 90
	int count = strlen (str);
	char c;

	while (count--) {
		c = *str;
		if ((c >= 'A') && (c <= 'Z'))
			*str = 'a' + (c - 'A');
		str++;
	}
W
wdenk 已提交
91
}
92
void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
W
wdenk 已提交
93
{
94
	unsigned int pR, pG, pB;
W
wdenk 已提交
95

96 97 98 99
	/* 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;
W
wdenk 已提交
100

101 102 103 104
	/* 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;
W
wdenk 已提交
105

106
	return;
W
wdenk 已提交
107 108
}

109
void printlogo_rgb (rgb_t * data, int w, int h)
W
wdenk 已提交
110
{
111 112 113 114 115 116 117 118 119 120 121
	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 ("\n");
	}
W
wdenk 已提交
122 123 124 125
}

void printlogo_yuyv (unsigned short *data, int w, int h)
{
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 ("\n");
	}
W
wdenk 已提交
136 137
}

138 139
static inline unsigned short le16_to_cpu (unsigned short val)
{
140 141 142 143 144 145 146
	union {
		unsigned char pval[2];
		unsigned short val;
	} swapped;

	swapped.val = val;
	return (swapped.pval[1] << 8) + swapped.pval[0];
147 148
}

149
int image_load_tga (image_t * image, char *filename)
W
wdenk 已提交
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 177 178
	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;
W
wdenk 已提交
179 180

	default:
181 182 183
		printf ("Format not supported!\n");
		return -1;
	}
W
wdenk 已提交
184

185 186 187 188
	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;
189
	image->data = xmalloc (image->size);
W
wdenk 已提交
190

191 192 193 194
	if (image->bpp != 24) {
		printf ("Bpp not supported: %d!\n", image->bpp);
		return -1;
	}
W
wdenk 已提交
195

196
	fread (image->data, image->size, 1, file);
W
wdenk 已提交
197 198 199

/* Swapping R and B values */

200 201 202 203 204 205
	p = image->data;
	for (i = 0; i < image->pixels; i++, p++) {
		app = p->r;
		p->r = p->b;
		p->b = app;
	}
W
wdenk 已提交
206 207 208

/* Swapping image */

209
	if (!(header.ImageDescriptorByte & 0x20)) {
210
		unsigned char *temp = xmalloc (image->size);
211 212 213
		int linesize = image->pixel_size * image->width;
		void *dest = image->data,
			*source = temp + image->size - linesize;
W
wdenk 已提交
214

215 216 217 218 219
		printf ("S");
		if (temp == NULL) {
			printf ("Cannot alloc temp buffer!\n");
			return -1;
		}
W
wdenk 已提交
220

221 222 223 224
		memcpy (temp, image->data, image->size);
		for (i = 0; i < image->height;
		     i++, dest += linesize, source -= linesize)
			memcpy (dest, source, linesize);
W
wdenk 已提交
225

226 227
		free (temp);
	}
W
wdenk 已提交
228
#ifdef ENABLE_ASCII_BANNERS
229
	printlogo_rgb (image->data, image->width, image->height);
W
wdenk 已提交
230 231
#endif

232 233
	fclose (file);
	return 0;
W
wdenk 已提交
234 235
}

236
void image_free (image_t * image)
W
wdenk 已提交
237
{
238 239
	free (image->data);
	free (image->palette);
W
wdenk 已提交
240 241
}

242
int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
W
wdenk 已提交
243
{
244 245 246 247 248 249 250 251 252 253 254 255 256
	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 =
257
				   xmalloc (yuyv_image->size));
258 259 260 261
	yuyv_image->palette = 0;
	yuyv_image->palette_size = 0;

	while ((count++) < rgb_image->pixels) {
W
wdenk 已提交
262 263
		pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);

264 265
		if ((count & 1) == 0)	/* Was == 0 */
			memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
W
wdenk 已提交
266
		else
267
			memcpy (dest, (void *) &yuyv, sizeof (short));
W
wdenk 已提交
268

269
		dest++;
W
wdenk 已提交
270 271 272
	}

#ifdef ENABLE_ASCII_BANNERS
273 274
	printlogo_yuyv (yuyv_image->data, yuyv_image->width,
			yuyv_image->height);
W
wdenk 已提交
275
#endif
276
	return 0;
W
wdenk 已提交
277 278
}

279 280
int use_gzip = 0;

281
int image_save_header (image_t * image, char *filename, char *varname)
W
wdenk 已提交
282
{
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	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,
		 "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
	fprintf (file,
		 " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
		 varname);
	fprintf (file,
		 " * Where:\t'screen'\tis the pointer to the frame buffer\n");
	fprintf (file, " *\t\t'width'\tis the screen width\n");
	fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
	fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");

303 304 305 306 307 308 309 310 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	/*  gzip compress */
	if (use_gzip & 0x1) {
		const char *errstr = NULL;
		unsigned char *compressed;
		struct stat st;
		FILE *gz;
		char *gzfilename = xmalloc(strlen (filename) + 20);
		char *gzcmd = xmalloc(strlen (filename) + 20);

		sprintf (gzfilename, "%s.gz", filename);
		sprintf (gzcmd, "gzip > %s", gzfilename);
		gz = popen (gzcmd, "w");
		if (!gz) {
			errstr = "\nerror: popen() failed";
			goto done;
		}
		if (fwrite (image->data, image->size, 1, gz) != 1) {
			errstr = "\nerror: writing data to gzip failed";
			goto done;
		}
		if (pclose (gz)) {
			errstr = "\nerror: gzip process failed";
			goto done;
		}

		gz = fopen (gzfilename, "r");
		if (!gz) {
			errstr = "\nerror: open() on gzip data failed";
			goto done;
		}
		if (stat (gzfilename, &st)) {
			errstr = "\nerror: stat() on gzip file failed";
			goto done;
		}
		compressed = xmalloc (st.st_size);
		if (fread (compressed, st.st_size, 1, gz) != 1) {
			errstr = "\nerror: reading gzip data failed";
			goto done;
		}
		fclose (gz);

		unlink (gzfilename);

		dataptr = compressed;
		count = st.st_size;
		fprintf (file, "#define EASYLOGO_ENABLE_GZIP %i\n\n", count);
		if (use_gzip & 0x2)
			fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);

 done:
		free (gzfilename);
		free (gzcmd);

		if (errstr) {
			perror (errstr);
			return -1;
		}
	}

362 363 364 365
	/*	Headers */
	fprintf (file, "#include <video_easylogo.h>\n\n");
	/*	Macros */
	strcpy (def_name, varname);
W
wdenk 已提交
366
	StringUpperCase (def_name);
367 368 369 370 371 372 373 374 375 376 377 378
	fprintf (file, "#define	DEF_%s_WIDTH\t\t%d\n", def_name,
		 image->width);
	fprintf (file, "#define	DEF_%s_HEIGHT\t\t%d\n", def_name,
		 image->height);
	fprintf (file, "#define	DEF_%s_PIXELS\t\t%d\n", def_name,
		 image->pixels);
	fprintf (file, "#define	DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
	fprintf (file, "#define	DEF_%s_PIXEL_SIZE\t%d\n", def_name,
		 image->pixel_size);
	fprintf (file, "#define	DEF_%s_SIZE\t\t%d\n\n", def_name,
		 image->size);
	/*  Declaration */
379 380
	fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
		 def_name);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

	/*	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, "\n");

			col = 0;
			break;

		default:
			strcpy (app, str);
			sprintf (str, "%s, 0x%02x", app, *dataptr++);
			col++;
			count--;
			break;
W
wdenk 已提交
406 407 408
		}

	if (col)
409 410
		fprintf (file, "%s\n", str);

W
Wolfgang Denk 已提交
411
	/*	End of declaration */
412 413 414 415 416 417 418 419 420
	fprintf (file, "};\n\n");
	/*	Variable */
	fprintf (file, "fastimage_t %s = {\n", varname);
	fprintf (file, "		DEF_%s_DATA,\n", def_name);
	fprintf (file, "		DEF_%s_WIDTH,\n", def_name);
	fprintf (file, "		DEF_%s_HEIGHT,\n", def_name);
	fprintf (file, "		DEF_%s_BPP,\n", def_name);
	fprintf (file, "		DEF_%s_PIXEL_SIZE,\n", def_name);
	fprintf (file, "		DEF_%s_SIZE\n};\n", def_name);
W
wdenk 已提交
421 422 423

	fclose (file);

424
	return 0;
W
wdenk 已提交
425 426 427 428
}

#define DEF_FILELEN	256

429 430 431 432 433 434 435 436 437
static void usage (int exit_status)
{
	puts (
		"EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
		"\n"
		"Syntax:	easylogo [options] inputfile [outputvar [outputfile]]\n"
		"\n"
		"Options:\n"
		"  -r     Output RGB instead of YUYV\n"
438 439
		"  -g     Compress with gzip\n"
		"  -b     Preallocate space in bss for decompressing image\n"
440 441 442 443 444 445 446 447 448
		"  -h     Help output\n"
		"\n"
		"Where: 'inputfile'   is the TGA image to load\n"
		"       'outputvar'   is the variable name to create\n"
		"       'outputfile'  is the output header file (default is 'inputfile.h')"
	);
	exit (exit_status);
}

W
wdenk 已提交
449 450
int main (int argc, char *argv[])
{
451 452
	int c;
	bool use_rgb = false;
453 454 455 456 457
	char inputfile[DEF_FILELEN],
		outputfile[DEF_FILELEN], varname[DEF_FILELEN];

	image_t rgb_logo, yuyv_logo;

458
	while ((c = getopt(argc, argv, "hrgb")) > 0) {
459 460 461 462 463 464 465 466
		switch (c) {
		case 'h':
			usage (0);
			break;
		case 'r':
			use_rgb = true;
			puts ("Using 24-bit RGB Output Fromat");
			break;
467 468 469 470 471 472 473 474
		case 'g':
			use_gzip |= 0x1;
			puts ("Compressing with gzip");
			break;
		case 'b':
			use_gzip |= 0x2;
			puts ("Preallocating bss space for decompressing image");
			break;
475 476 477
		default:
			usage (1);
			break;
478
		}
479
	}
W
wdenk 已提交
480

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	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';
	}
W
wdenk 已提交
497

498 499 500 501 502 503
	if (c > 2)
		strcpy (outputfile, argv[optind + 2]);
	else {
		/* just append ".h" to input file name */
		strcpy (outputfile, inputfile);
		strcat (outputfile, ".h");
504
	}
W
wdenk 已提交
505

506 507 508
	/* Make sure the output is sent as soon as we printf() */
	setbuf(stdout, NULL);

509 510
	printf ("Doing '%s' (%s) from '%s'...",
		outputfile, varname, inputfile);
W
wdenk 已提交
511

512
	/* Import TGA logo */
W
wdenk 已提交
513

514 515 516 517 518
	printf ("L");
	if (image_load_tga (&rgb_logo, inputfile) < 0) {
		printf ("input file not found!\n");
		exit (1);
	}
W
wdenk 已提交
519

520
	/* Convert it to YUYV format if wanted */
W
wdenk 已提交
521

522 523 524 525
	if (!use_rgb) {
		printf ("C");
		image_rgb_to_yuyv (&rgb_logo, &yuyv_logo);
	}
W
wdenk 已提交
526

527
	/* Save it into a header format */
W
wdenk 已提交
528

529
	printf ("S");
530
	image_save_header (use_rgb ? &rgb_logo : &yuyv_logo, outputfile, varname);
W
wdenk 已提交
531

532
	/* Free original image and copy */
W
wdenk 已提交
533

534
	image_free (&rgb_logo);
535 536
	if (!use_rgb)
		image_free (&yuyv_logo);
W
wdenk 已提交
537

538
	printf ("\n");
W
wdenk 已提交
539

540
	return 0;
W
wdenk 已提交
541
}