easylogo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 image_rgb888_to_rgb565(image_t *rgb888_image, image_t *rgb565_image)
  232. {
  233. rgb_t *rgb_ptr = (rgb_t *) rgb888_image->data;
  234. unsigned short *dest;
  235. int count = 0;
  236. rgb565_image->pixel_size = 2;
  237. rgb565_image->bpp = 16;
  238. rgb565_image->yuyv = 0;
  239. rgb565_image->width = rgb888_image->width;
  240. rgb565_image->height = rgb888_image->height;
  241. rgb565_image->pixels = rgb565_image->width * rgb565_image->height;
  242. rgb565_image->size = rgb565_image->pixels * rgb565_image->pixel_size;
  243. dest = (unsigned short *) (rgb565_image->data =
  244. xmalloc(rgb565_image->size));
  245. rgb565_image->palette = 0;
  246. rgb565_image->palette_size = 0;
  247. while ((count++) < rgb888_image->pixels) {
  248. *dest++ = ((rgb_ptr->b & 0xF8) << 8) |
  249. ((rgb_ptr->g & 0xFC) << 3) |
  250. (rgb_ptr->r >> 3);
  251. rgb_ptr++;
  252. }
  253. return 0;
  254. }
  255. enum comp_t {
  256. COMP_NONE,
  257. COMP_GZIP,
  258. COMP_LZMA,
  259. };
  260. static enum comp_t compression = COMP_NONE;
  261. static bool bss_storage = false;
  262. int image_save_header (image_t * image, char *filename, char *varname)
  263. {
  264. FILE *file = fopen (filename, "w");
  265. char app[256], str[256] = "", def_name[64];
  266. int count = image->size, col = 0;
  267. unsigned char *dataptr = image->data;
  268. if (file == NULL)
  269. return -1;
  270. /* Author information */
  271. fprintf (file,
  272. "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
  273. fprintf (file,
  274. " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n",
  275. varname);
  276. fprintf (file,
  277. " * Where:\t'screen'\tis the pointer to the frame buffer\n");
  278. fprintf (file, " *\t\t'width'\tis the screen width\n");
  279. fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
  280. fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
  281. /* image compress */
  282. if (compression != COMP_NONE) {
  283. const char *errstr = NULL;
  284. unsigned char *compressed;
  285. const char *comp_name;
  286. struct stat st;
  287. FILE *compfp;
  288. size_t filename_len = strlen(filename);
  289. char *compfilename = xmalloc(filename_len + 20);
  290. char *compcmd = xmalloc(filename_len + 50);
  291. sprintf(compfilename, "%s.bin", filename);
  292. switch (compression) {
  293. case COMP_GZIP:
  294. strcpy(compcmd, "gzip");
  295. comp_name = "GZIP";
  296. break;
  297. case COMP_LZMA:
  298. strcpy(compcmd, "lzma");
  299. comp_name = "LZMA";
  300. break;
  301. default:
  302. errstr = "\nerror: unknown compression method";
  303. goto done;
  304. }
  305. strcat(compcmd, " > ");
  306. strcat(compcmd, compfilename);
  307. compfp = popen(compcmd, "w");
  308. if (!compfp) {
  309. errstr = "\nerror: popen() failed";
  310. goto done;
  311. }
  312. if (fwrite(image->data, image->size, 1, compfp) != 1) {
  313. errstr = "\nerror: writing data to gzip failed";
  314. goto done;
  315. }
  316. if (pclose(compfp)) {
  317. errstr = "\nerror: gzip process failed";
  318. goto done;
  319. }
  320. compfp = fopen(compfilename, "r");
  321. if (!compfp) {
  322. errstr = "\nerror: open() on gzip data failed";
  323. goto done;
  324. }
  325. if (stat(compfilename, &st)) {
  326. errstr = "\nerror: stat() on gzip file failed";
  327. goto done;
  328. }
  329. compressed = xmalloc(st.st_size);
  330. if (fread(compressed, st.st_size, 1, compfp) != 1) {
  331. errstr = "\nerror: reading gzip data failed";
  332. goto done;
  333. }
  334. fclose(compfp);
  335. unlink(compfilename);
  336. dataptr = compressed;
  337. count = st.st_size;
  338. fprintf(file, "#define EASYLOGO_ENABLE_%s %i\n\n", comp_name, count);
  339. if (bss_storage)
  340. fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size);
  341. done:
  342. free(compfilename);
  343. free(compcmd);
  344. if (errstr) {
  345. perror (errstr);
  346. return -1;
  347. }
  348. }
  349. /* Headers */
  350. fprintf (file, "#include <video_easylogo.h>\n\n");
  351. /* Macros */
  352. strcpy (def_name, varname);
  353. StringUpperCase (def_name);
  354. fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name,
  355. image->width);
  356. fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name,
  357. image->height);
  358. fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name,
  359. image->pixels);
  360. fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
  361. fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name,
  362. image->pixel_size);
  363. fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name,
  364. image->size);
  365. /* Declaration */
  366. fprintf (file, "unsigned char DEF_%s_DATA[] = {\n",
  367. def_name);
  368. /* Data */
  369. while (count)
  370. switch (col) {
  371. case 0:
  372. sprintf (str, " 0x%02x", *dataptr++);
  373. col++;
  374. count--;
  375. break;
  376. case 16:
  377. fprintf (file, "%s", str);
  378. if (count > 0)
  379. fprintf (file, ",");
  380. fprintf (file, "\n");
  381. col = 0;
  382. break;
  383. default:
  384. strcpy (app, str);
  385. sprintf (str, "%s, 0x%02x", app, *dataptr++);
  386. col++;
  387. count--;
  388. break;
  389. }
  390. if (col)
  391. fprintf (file, "%s\n", str);
  392. /* End of declaration */
  393. fprintf (file, "};\n\n");
  394. /* Variable */
  395. fprintf (file, "fastimage_t %s = {\n", varname);
  396. fprintf (file, " DEF_%s_DATA,\n", def_name);
  397. fprintf (file, " DEF_%s_WIDTH,\n", def_name);
  398. fprintf (file, " DEF_%s_HEIGHT,\n", def_name);
  399. fprintf (file, " DEF_%s_BPP,\n", def_name);
  400. fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name);
  401. fprintf (file, " DEF_%s_SIZE\n};\n", def_name);
  402. fclose (file);
  403. return 0;
  404. }
  405. #define DEF_FILELEN 256
  406. static void usage (int exit_status)
  407. {
  408. puts (
  409. "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n"
  410. "\n"
  411. "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n"
  412. "\n"
  413. "Options:\n"
  414. " -r Output RGB888 instead of YUYV\n"
  415. " -s Output RGB565 instead of YUYV\n"
  416. " -g Compress with gzip\n"
  417. " -l Compress with lzma\n"
  418. " -b Preallocate space in bss for decompressing image\n"
  419. " -h Help output\n"
  420. "\n"
  421. "Where: 'inputfile' is the TGA image to load\n"
  422. " 'outputvar' is the variable name to create\n"
  423. " 'outputfile' is the output header file (default is 'inputfile.h')"
  424. );
  425. exit (exit_status);
  426. }
  427. int main (int argc, char *argv[])
  428. {
  429. int c;
  430. bool use_rgb888 = false;
  431. bool use_rgb565 = false;
  432. char inputfile[DEF_FILELEN],
  433. outputfile[DEF_FILELEN], varname[DEF_FILELEN];
  434. image_t rgb888_logo, rgb565_logo, yuyv_logo;
  435. while ((c = getopt(argc, argv, "hrsglb")) > 0) {
  436. switch (c) {
  437. case 'h':
  438. usage (0);
  439. break;
  440. case 'r':
  441. use_rgb888 = true;
  442. puts("Using 24-bit RGB888 Output Fromat");
  443. break;
  444. case 's':
  445. use_rgb565 = true;
  446. puts("Using 16-bit RGB565 Output Fromat");
  447. break;
  448. case 'g':
  449. compression = COMP_GZIP;
  450. puts("Compressing with gzip");
  451. break;
  452. case 'l':
  453. compression = COMP_LZMA;
  454. puts("Compressing with lzma");
  455. break;
  456. case 'b':
  457. bss_storage = true;
  458. puts("Preallocating bss space for decompressing image");
  459. break;
  460. default:
  461. usage (1);
  462. break;
  463. }
  464. }
  465. c = argc - optind;
  466. if (c > 4 || c < 1)
  467. usage (1);
  468. strcpy (inputfile, argv[optind]);
  469. if (c > 1)
  470. strcpy (varname, argv[optind + 1]);
  471. else {
  472. /* transform "input.tga" to just "input" */
  473. char *dot;
  474. strcpy (varname, inputfile);
  475. dot = strchr (varname, '.');
  476. if (dot)
  477. *dot = '\0';
  478. }
  479. if (c > 2)
  480. strcpy (outputfile, argv[optind + 2]);
  481. else {
  482. /* just append ".h" to input file name */
  483. strcpy (outputfile, inputfile);
  484. strcat (outputfile, ".h");
  485. }
  486. /* Make sure the output is sent as soon as we printf() */
  487. setbuf(stdout, NULL);
  488. printf ("Doing '%s' (%s) from '%s'...",
  489. outputfile, varname, inputfile);
  490. /* Import TGA logo */
  491. printf ("L");
  492. if (image_load_tga(&rgb888_logo, inputfile) < 0) {
  493. printf ("input file not found!\n");
  494. exit (1);
  495. }
  496. /* Convert, save, and free the image */
  497. if (!use_rgb888 && !use_rgb565) {
  498. printf ("C");
  499. image_rgb_to_yuyv(&rgb888_logo, &yuyv_logo);
  500. printf("S");
  501. image_save_header(&yuyv_logo, outputfile, varname);
  502. image_free(&yuyv_logo);
  503. } else if (use_rgb565) {
  504. printf("C");
  505. image_rgb888_to_rgb565(&rgb888_logo, &rgb565_logo);
  506. printf("S");
  507. image_save_header(&rgb565_logo, outputfile, varname);
  508. image_free(&rgb565_logo);
  509. } else {
  510. printf("S");
  511. image_save_header(&rgb888_logo, outputfile, varname);
  512. }
  513. /* Free original image and copy */
  514. image_free(&rgb888_logo);
  515. printf ("\n");
  516. return 0;
  517. }