easylogo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. ** Easylogo TGA->header converter
  3. ** ==============================
  4. ** (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
  5. ** AIRVENT SAM s.p.a - RIMINI(ITALY)
  6. ** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org>
  7. **
  8. ** This is still under construction!
  9. */
  10. #include <errno.h>
  11. #include <getopt.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #pragma pack(1)
  19. /*#define ENABLE_ASCII_BANNERS */
  20. typedef struct {
  21. unsigned char id;
  22. unsigned char ColorMapType;
  23. unsigned char ImageTypeCode;
  24. unsigned short ColorMapOrigin;
  25. unsigned short ColorMapLenght;
  26. unsigned char ColorMapEntrySize;
  27. unsigned short ImageXOrigin;
  28. unsigned short ImageYOrigin;
  29. unsigned short ImageWidth;
  30. unsigned short ImageHeight;
  31. unsigned char ImagePixelSize;
  32. unsigned char ImageDescriptorByte;
  33. } tga_header_t;
  34. typedef struct {
  35. unsigned char r, g, b;
  36. } rgb_t;
  37. typedef struct {
  38. unsigned char b, g, r;
  39. } bgr_t;
  40. typedef struct {
  41. unsigned char Cb, y1, Cr, y2;
  42. } yuyv_t;
  43. typedef struct {
  44. void *data, *palette;
  45. int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
  46. } image_t;
  47. void *xmalloc (size_t size)
  48. {
  49. void *ret = malloc (size);
  50. if (!ret) {
  51. fprintf (stderr, "\nerror: malloc(%zu) failed: %s",
  52. size, strerror(errno));
  53. exit (1);
  54. }
  55. return ret;
  56. }
  57. void StringUpperCase (char *str)
  58. {
  59. int count = strlen (str);
  60. char c;
  61. while (count--) {
  62. c = *str;
  63. if ((c >= 'a') && (c <= 'z'))
  64. *str = 'A' + (c - 'a');
  65. str++;
  66. }
  67. }
  68. void StringLowerCase (char *str)
  69. {
  70. int count = strlen (str);
  71. char c;
  72. while (count--) {
  73. c = *str;
  74. if ((c >= 'A') && (c <= 'Z'))
  75. *str = 'a' + (c - 'A');
  76. str++;
  77. }
  78. }
  79. void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel)
  80. {
  81. unsigned int pR, pG, pB;
  82. /* Transform (0-255) components to (0-100) */
  83. pR = rgb_pixel->r * 100 / 255;
  84. pG = rgb_pixel->g * 100 / 255;
  85. pB = rgb_pixel->b * 100 / 255;
  86. /* Calculate YUV values (0-255) from RGB beetween 0-100 */
  87. yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16;
  88. yuyv_pixel->Cb = pB - (pR / 4) - (pG * 3 / 4) + 128;
  89. yuyv_pixel->Cr = pR - (pG * 3 / 4) - (pB / 4) + 128;
  90. return;
  91. }
  92. void printlogo_rgb (rgb_t * data, int w, int h)
  93. {
  94. int x, y;
  95. for (y = 0; y < h; y++) {
  96. for (x = 0; x < w; x++, data++)
  97. if ((data->r <
  98. 30) /*&&(data->g == 0)&&(data->b == 0) */ )
  99. printf (" ");
  100. else
  101. printf ("X");
  102. printf ("\n");
  103. }
  104. }
  105. void printlogo_yuyv (unsigned short *data, int w, int h)
  106. {
  107. int x, y;
  108. for (y = 0; y < h; y++) {
  109. for (x = 0; x < w; x++, data++)
  110. if (*data == 0x1080) /* Because of inverted on i386! */
  111. printf (" ");
  112. else
  113. printf ("X");
  114. printf ("\n");
  115. }
  116. }
  117. static inline unsigned short le16_to_cpu (unsigned short val)
  118. {
  119. union {
  120. unsigned char pval[2];
  121. unsigned short val;
  122. } swapped;
  123. swapped.val = val;
  124. return (swapped.pval[1] << 8) + swapped.pval[0];
  125. }
  126. int image_load_tga (image_t * image, char *filename)
  127. {
  128. FILE *file;
  129. tga_header_t header;
  130. int i;
  131. unsigned char app;
  132. rgb_t *p;
  133. if ((file = fopen (filename, "rb")) == NULL)
  134. return -1;
  135. fread (&header, sizeof (header), 1, file);
  136. /* byte swap: tga is little endian, host is ??? */
  137. header.ColorMapOrigin = le16_to_cpu (header.ColorMapOrigin);
  138. header.ColorMapLenght = le16_to_cpu (header.ColorMapLenght);
  139. header.ImageXOrigin = le16_to_cpu (header.ImageXOrigin);
  140. header.ImageYOrigin = le16_to_cpu (header.ImageYOrigin);
  141. header.ImageWidth = le16_to_cpu (header.ImageWidth);
  142. header.ImageHeight = le16_to_cpu (header.ImageHeight);
  143. image->width = header.ImageWidth;
  144. image->height = header.ImageHeight;
  145. switch (header.ImageTypeCode) {
  146. case 2: /* Uncompressed RGB */
  147. image->yuyv = 0;
  148. image->palette_size = 0;
  149. image->palette = NULL;
  150. break;
  151. default:
  152. printf ("Format not supported!\n");
  153. return -1;
  154. }
  155. image->bpp = header.ImagePixelSize;
  156. image->pixel_size = ((image->bpp - 1) / 8) + 1;
  157. image->pixels = image->width * image->height;
  158. image->size = image->pixels * image->pixel_size;
  159. image->data = xmalloc (image->size);
  160. if (image->bpp != 24) {
  161. printf ("Bpp not supported: %d!\n", image->bpp);
  162. return -1;
  163. }
  164. fread (image->data, image->size, 1, file);
  165. /* Swapping R and B values */
  166. p = image->data;
  167. for (i = 0; i < image->pixels; i++, p++) {
  168. app = p->r;
  169. p->r = p->b;
  170. p->b = app;
  171. }
  172. /* Swapping image */
  173. if (!(header.ImageDescriptorByte & 0x20)) {
  174. unsigned char *temp = xmalloc (image->size);
  175. int linesize = image->pixel_size * image->width;
  176. void *dest = image->data,
  177. *source = temp + image->size - linesize;
  178. printf ("S");
  179. if (temp == NULL) {
  180. printf ("Cannot alloc temp buffer!\n");
  181. return -1;
  182. }
  183. memcpy (temp, image->data, image->size);
  184. for (i = 0; i < image->height;
  185. i++, dest += linesize, source -= linesize)
  186. memcpy (dest, source, linesize);
  187. free (temp);
  188. }
  189. #ifdef ENABLE_ASCII_BANNERS
  190. printlogo_rgb (image->data, image->width, image->height);
  191. #endif
  192. fclose (file);
  193. return 0;
  194. }
  195. void image_free (image_t * image)
  196. {
  197. free (image->data);
  198. free (image->palette);
  199. }
  200. int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
  201. {
  202. rgb_t *rgb_ptr = (rgb_t *) rgb_image->data;
  203. yuyv_t yuyv;
  204. unsigned short *dest;
  205. int count = 0;
  206. yuyv_image->pixel_size = 2;
  207. yuyv_image->bpp = 16;
  208. yuyv_image->yuyv = 1;
  209. yuyv_image->width = rgb_image->width;
  210. yuyv_image->height = rgb_image->height;
  211. yuyv_image->pixels = yuyv_image->width * yuyv_image->height;
  212. yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size;
  213. dest = (unsigned short *) (yuyv_image->data =
  214. xmalloc (yuyv_image->size));
  215. yuyv_image->palette = 0;
  216. yuyv_image->palette_size = 0;
  217. while ((count++) < rgb_image->pixels) {
  218. pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
  219. if ((count & 1) == 0) /* Was == 0 */
  220. memcpy (dest, ((void *) &yuyv) + 2, sizeof (short));
  221. else
  222. memcpy (dest, (void *) &yuyv, sizeof (short));
  223. dest++;
  224. }
  225. #ifdef ENABLE_ASCII_BANNERS
  226. printlogo_yuyv (yuyv_image->data, yuyv_image->width,
  227. yuyv_image->height);
  228. #endif
  229. return 0;
  230. }
  231. int use_gzip = 0;
  232. int image_save_header (image_t * image, char *filename, char *varname)
  233. {
  234. FILE *file = fopen (filename, "w");
  235. char app[256], str[256] = "", def_name[64];
  236. int count = image->size, col = 0;
  237. unsigned char *dataptr = image->data;
  238. if (file == NULL)
  239. return -1;
  240. /* Author information */
  241. fprintf (file,
  242. "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
  243. fprintf (file,
  244. " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
  245. varname);
  246. fprintf (file,
  247. " * Where:\t'screen'\tis the pointer to the frame buffer\n");
  248. fprintf (file, " *\t\t'width'\tis the screen width\n");
  249. fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
  250. fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
  251. /* gzip compress */
  252. if (use_gzip & 0x1) {
  253. const char *errstr = NULL;
  254. unsigned char *compressed;
  255. struct stat st;
  256. FILE *gz;
  257. char *gzfilename = xmalloc(strlen (filename) + 20);
  258. char *gzcmd = xmalloc(strlen (filename) + 20);
  259. sprintf (gzfilename, "%s.gz", filename);
  260. sprintf (gzcmd, "gzip > %s", gzfilename);
  261. gz = popen (gzcmd, "w");
  262. if (!gz) {
  263. errstr = "\nerror: popen() failed";
  264. goto done;
  265. }
  266. if (fwrite (image->data, image->size, 1, gz) != 1) {
  267. errstr = "\nerror: writing data to gzip failed";
  268. goto done;
  269. }
  270. if (pclose (gz)) {
  271. errstr = "\nerror: gzip process failed";
  272. goto done;
  273. }
  274. gz = fopen (gzfilename, "r");
  275. if (!gz) {
  276. errstr = "\nerror: open() on gzip data failed";
  277. goto done;
  278. }
  279. if (stat (gzfilename, &st)) {
  280. errstr = "\nerror: stat() on gzip file failed";
  281. goto done;
  282. }
  283. compressed = xmalloc (st.st_size);
  284. if (fread (compressed, st.st_size, 1, gz) != 1) {
  285. errstr = "\nerror: reading gzip data failed";
  286. goto done;
  287. }
  288. fclose (gz);
  289. unlink (gzfilename);
  290. dataptr = compressed;
  291. count = st.st_size;
  292. fprintf (file, "#define EASYLOGO_ENABLE_GZIP %i\n\n", count);
  293. if (use_gzip & 0x2)
  294. fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);
  295. done:
  296. free (gzfilename);
  297. free (gzcmd);
  298. if (errstr) {
  299. perror (errstr);
  300. return -1;
  301. }
  302. }
  303. /* Headers */
  304. fprintf (file, "#include <video_easylogo.h>\n\n");
  305. /* Macros */
  306. strcpy (def_name, varname);
  307. StringUpperCase (def_name);
  308. fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
  309. image->width);
  310. fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
  311. image->height);
  312. fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
  313. image->pixels);
  314. fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
  315. fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name,
  316. image->pixel_size);
  317. fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
  318. image->size);
  319. /* Declaration */
  320. fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
  321. def_name);
  322. /* Data */
  323. while (count)
  324. switch (col) {
  325. case 0:
  326. sprintf (str, " 0x%02x", *dataptr++);
  327. col++;
  328. count--;
  329. break;
  330. case 16:
  331. fprintf (file, "%s", str);
  332. if (count > 0)
  333. fprintf (file, ",");
  334. fprintf (file, "\n");
  335. col = 0;
  336. break;
  337. default:
  338. strcpy (app, str);
  339. sprintf (str, "%s, 0x%02x", app, *dataptr++);
  340. col++;
  341. count--;
  342. break;
  343. }
  344. if (col)
  345. fprintf (file, "%s\n", str);
  346. /* End of declaration */
  347. fprintf (file, "};\n\n");
  348. /* Variable */
  349. fprintf (file, "fastimage_t %s = {\n", varname);
  350. fprintf (file, " DEF_%s_DATA,\n", def_name);
  351. fprintf (file, " DEF_%s_WIDTH,\n", def_name);
  352. fprintf (file, " DEF_%s_HEIGHT,\n", def_name);
  353. fprintf (file, " DEF_%s_BPP,\n", def_name);
  354. fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name);
  355. fprintf (file, " DEF_%s_SIZE\n};\n", def_name);
  356. fclose (file);
  357. return 0;
  358. }
  359. #define DEF_FILELEN 256
  360. static void usage (int exit_status)
  361. {
  362. puts (
  363. "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
  364. "\n"
  365. "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n"
  366. "\n"
  367. "Options:\n"
  368. " -r Output RGB instead of YUYV\n"
  369. " -g Compress with gzip\n"
  370. " -b Preallocate space in bss for decompressing image\n"
  371. " -h Help output\n"
  372. "\n"
  373. "Where: 'inputfile' is the TGA image to load\n"
  374. " 'outputvar' is the variable name to create\n"
  375. " 'outputfile' is the output header file (default is 'inputfile.h')"
  376. );
  377. exit (exit_status);
  378. }
  379. int main (int argc, char *argv[])
  380. {
  381. int c;
  382. bool use_rgb = false;
  383. char inputfile[DEF_FILELEN],
  384. outputfile[DEF_FILELEN], varname[DEF_FILELEN];
  385. image_t rgb_logo, yuyv_logo;
  386. while ((c = getopt(argc, argv, "hrgb")) > 0) {
  387. switch (c) {
  388. case 'h':
  389. usage (0);
  390. break;
  391. case 'r':
  392. use_rgb = true;
  393. puts ("Using 24-bit RGB Output Fromat");
  394. break;
  395. case 'g':
  396. use_gzip |= 0x1;
  397. puts ("Compressing with gzip");
  398. break;
  399. case 'b':
  400. use_gzip |= 0x2;
  401. puts ("Preallocating bss space for decompressing image");
  402. break;
  403. default:
  404. usage (1);
  405. break;
  406. }
  407. }
  408. c = argc - optind;
  409. if (c > 4 || c < 1)
  410. usage (1);
  411. strcpy (inputfile, argv[optind]);
  412. if (c > 1)
  413. strcpy (varname, argv[optind + 1]);
  414. else {
  415. /* transform "input.tga" to just "input" */
  416. char *dot;
  417. strcpy (varname, inputfile);
  418. dot = strchr (varname, '.');
  419. if (dot)
  420. *dot = '\0';
  421. }
  422. if (c > 2)
  423. strcpy (outputfile, argv[optind + 2]);
  424. else {
  425. /* just append ".h" to input file name */
  426. strcpy (outputfile, inputfile);
  427. strcat (outputfile, ".h");
  428. }
  429. /* Make sure the output is sent as soon as we printf() */
  430. setbuf(stdout, NULL);
  431. printf ("Doing '%s' (%s) from '%s'...",
  432. outputfile, varname, inputfile);
  433. /* Import TGA logo */
  434. printf ("L");
  435. if (image_load_tga (&rgb_logo, inputfile) < 0) {
  436. printf ("input file not found!\n");
  437. exit (1);
  438. }
  439. /* Convert it to YUYV format if wanted */
  440. if (!use_rgb) {
  441. printf ("C");
  442. image_rgb_to_yuyv (&rgb_logo, &yuyv_logo);
  443. }
  444. /* Save it into a header format */
  445. printf ("S");
  446. image_save_header (use_rgb ? &rgb_logo : &yuyv_logo, outputfile, varname);
  447. /* Free original image and copy */
  448. image_free (&rgb_logo);
  449. if (!use_rgb)
  450. image_free (&yuyv_logo);
  451. printf ("\n");
  452. return 0;
  453. }