easylogo.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. **
  7. ** This is still under construction!
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #pragma pack(1)
  13. /*#define ENABLE_ASCII_BANNERS */
  14. typedef struct {
  15. unsigned char id;
  16. unsigned char ColorMapType;
  17. unsigned char ImageTypeCode;
  18. unsigned short ColorMapOrigin;
  19. unsigned short ColorMapLenght;
  20. unsigned char ColorMapEntrySize;
  21. unsigned short ImageXOrigin;
  22. unsigned short ImageYOrigin;
  23. unsigned short ImageWidth;
  24. unsigned short ImageHeight;
  25. unsigned char ImagePixelSize;
  26. unsigned char ImageDescriptorByte;
  27. } tga_header_t;
  28. typedef struct {
  29. unsigned char r,g,b ;
  30. } rgb_t ;
  31. typedef struct {
  32. unsigned char b,g,r ;
  33. } bgr_t ;
  34. typedef struct {
  35. unsigned char Cb,y1,Cr,y2;
  36. } yuyv_t ;
  37. typedef struct {
  38. void *data,
  39. *palette ;
  40. int width,
  41. height,
  42. pixels,
  43. bpp,
  44. pixel_size,
  45. size,
  46. palette_size,
  47. yuyv;
  48. } image_t ;
  49. void StringUpperCase (char *str)
  50. {
  51. int count = strlen(str);
  52. char c ;
  53. while(count--)
  54. {
  55. c=*str;
  56. if ((c >= 'a')&&(c<='z'))
  57. *str = 'A' + (c-'a');
  58. str++ ;
  59. }
  60. }
  61. void StringLowerCase (char *str)
  62. {
  63. int count = strlen(str);
  64. char c ;
  65. while(count--)
  66. {
  67. c=*str;
  68. if ((c >= 'A')&&(c<='Z'))
  69. *str = 'a' + (c-'A');
  70. str++ ;
  71. }
  72. }
  73. void pixel_rgb_to_yuyv (rgb_t *rgb_pixel, yuyv_t *yuyv_pixel)
  74. {
  75. unsigned int pR, pG, pB ;
  76. /* Transform (0-255) components to (0-100) */
  77. pR = rgb_pixel->r * 100 / 255 ;
  78. pG = rgb_pixel->g * 100 / 255 ;
  79. pB = rgb_pixel->b * 100 / 255 ;
  80. /* Calculate YUV values (0-255) from RGB beetween 0-100 */
  81. yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16 ;
  82. yuyv_pixel->Cb = pB - (pR/4) - (pG*3/4) + 128 ;
  83. yuyv_pixel->Cr = pR - (pG*3/4) - (pB/4) + 128 ;
  84. return ;
  85. }
  86. void printlogo_rgb (rgb_t *data, int w, int h)
  87. {
  88. int x,y;
  89. for (y=0; y<h; y++)
  90. {
  91. for (x=0; x<w; x++, data++)
  92. if ((data->r < 30)/*&&(data->g == 0)&&(data->b == 0)*/)
  93. printf(" ");
  94. else
  95. printf("X");
  96. printf("\n");
  97. }
  98. }
  99. void printlogo_yuyv (unsigned short *data, int w, int h)
  100. {
  101. int x,y;
  102. for (y=0; y<h; y++)
  103. {
  104. for (x=0; x<w; x++, data++)
  105. if (*data == 0x1080) /* Because of inverted on i386! */
  106. printf(" ");
  107. else
  108. printf("X");
  109. printf("\n");
  110. }
  111. }
  112. int image_load_tga (image_t *image, char *filename)
  113. {
  114. FILE *file ;
  115. tga_header_t header ;
  116. int i;
  117. unsigned char app ;
  118. rgb_t *p ;
  119. if( ( file = fopen( filename, "rb" ) ) == NULL )
  120. return -1;
  121. fread(&header, sizeof(header), 1, file);
  122. image->width = header.ImageWidth ;
  123. image->height = header.ImageHeight ;
  124. switch (header.ImageTypeCode){
  125. case 2: /* Uncompressed RGB */
  126. image->yuyv = 0 ;
  127. image->palette_size = 0 ;
  128. image->palette = NULL ;
  129. break;
  130. default:
  131. printf("Format not supported!\n");
  132. return -1 ;
  133. }
  134. image->bpp = header.ImagePixelSize ;
  135. image->pixel_size = ((image->bpp-1) / 8) + 1 ;
  136. image->pixels = image->width * image->height;
  137. image->size = image->pixels * image->pixel_size ;
  138. image->data = malloc(image->size) ;
  139. if (image->bpp != 24)
  140. {
  141. printf("Bpp not supported: %d!\n", image->bpp);
  142. return -1 ;
  143. }
  144. fread(image->data, image->size, 1, file);
  145. /* Swapping R and B values */
  146. p = image->data ;
  147. for(i=0; i < image->pixels; i++, p++)
  148. {
  149. app = p->r ;
  150. p->r = p->b ;
  151. p->b = app ;
  152. }
  153. /* Swapping image */
  154. if(!(header.ImageDescriptorByte & 0x20))
  155. {
  156. unsigned char *temp = malloc(image->size);
  157. int linesize = image->pixel_size * image->width ;
  158. void *dest = image->data,
  159. *source = temp + image->size - linesize ;
  160. printf("S");
  161. if (temp == NULL)
  162. {
  163. printf("Cannot alloc temp buffer!\n");
  164. return -1;
  165. }
  166. memcpy(temp, image->data, image->size);
  167. for(i = 0; i<image->height; i++, dest+=linesize, source-=linesize)
  168. memcpy(dest, source, linesize);
  169. free( temp );
  170. }
  171. #ifdef ENABLE_ASCII_BANNERS
  172. printlogo_rgb (image->data,image->width, image->height);
  173. #endif
  174. fclose (file);
  175. return 0;
  176. }
  177. int image_free (image_t *image)
  178. {
  179. if(image->data != NULL)
  180. free(image->data);
  181. if(image->palette != NULL)
  182. free(image->palette);
  183. return 0;
  184. }
  185. int image_rgb_to_yuyv (image_t *rgb_image, image_t *yuyv_image)
  186. {
  187. rgb_t *rgb_ptr = (rgb_t *) rgb_image->data ;
  188. yuyv_t yuyv ;
  189. unsigned short *dest ;
  190. int count = 0 ;
  191. yuyv_image->pixel_size = 2 ;
  192. yuyv_image->bpp = 16 ;
  193. yuyv_image->yuyv = 1 ;
  194. yuyv_image->width = rgb_image->width ;
  195. yuyv_image->height = rgb_image->height ;
  196. yuyv_image->pixels = yuyv_image->width * yuyv_image->height ;
  197. yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size ;
  198. dest = (unsigned short *) (yuyv_image->data = malloc(yuyv_image->size)) ;
  199. yuyv_image->palette = 0 ;
  200. yuyv_image->palette_size= 0 ;
  201. while((count++) < rgb_image->pixels)
  202. {
  203. pixel_rgb_to_yuyv (rgb_ptr++, &yuyv);
  204. if ((count & 1)==0) /* Was == 0 */
  205. memcpy (dest, ((void *)&yuyv) + 2, sizeof(short));
  206. else
  207. memcpy (dest, (void *)&yuyv, sizeof(short));
  208. dest ++ ;
  209. }
  210. #ifdef ENABLE_ASCII_BANNERS
  211. printlogo_yuyv (yuyv_image->data, yuyv_image->width, yuyv_image->height);
  212. #endif
  213. return 0 ;
  214. }
  215. int image_save_header (image_t *image, char *filename, char *varname)
  216. {
  217. FILE *file = fopen (filename, "w");
  218. char app[256], str[256]="", def_name[64] ;
  219. int count = image->size, col=0;
  220. unsigned char *dataptr = image->data ;
  221. if (file==NULL)
  222. return -1 ;
  223. /* Author information */
  224. fprintf(file, "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n");
  225. fprintf(file, " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n", varname);
  226. fprintf(file, " * Where:\t'screen'\tis the pointer to the frame buffer\n");
  227. fprintf(file, " *\t\t'width'\tis the screen width\n");
  228. fprintf(file, " *\t\t'x'\t\tis the horizontal position\n");
  229. fprintf(file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
  230. /* Headers */
  231. fprintf(file, "#include <video_easylogo.h>\n\n");
  232. /* Macros */
  233. strcpy(def_name, varname);
  234. StringUpperCase (def_name);
  235. fprintf(file, "#define DEF_%s_WIDTH\t\t%d\n", def_name, image->width);
  236. fprintf(file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name, image->height);
  237. fprintf(file, "#define DEF_%s_PIXELS\t\t%d\n", def_name, image->pixels);
  238. fprintf(file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp);
  239. fprintf(file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name, image->pixel_size);
  240. fprintf(file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name, image->size);
  241. /* Declaration */
  242. fprintf(file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n", def_name, def_name);
  243. /* Data */
  244. while(count)
  245. switch (col){
  246. case 0:
  247. sprintf(str, " 0x%02x", *dataptr++);
  248. col++;
  249. count-- ;
  250. break;
  251. case 16:
  252. fprintf(file, "%s", str);
  253. if (count > 0)
  254. fprintf(file,",");
  255. fprintf(file, "\n");
  256. col = 0 ;
  257. break;
  258. default:
  259. strcpy(app, str);
  260. sprintf(str, "%s, 0x%02x", app, *dataptr++);
  261. col++ ;
  262. count-- ;
  263. break;
  264. }
  265. if (col)
  266. fprintf(file, "%s\n", str);
  267. /* End of declaration */
  268. fprintf(file, "};\n\n");
  269. /* Variable */
  270. fprintf(file, "fastimage_t %s = {\n", varname);
  271. fprintf(file, " DEF_%s_DATA,\n", def_name);
  272. fprintf(file, " DEF_%s_WIDTH,\n", def_name);
  273. fprintf(file, " DEF_%s_HEIGHT,\n", def_name);
  274. fprintf(file, " DEF_%s_BPP,\n", def_name);
  275. fprintf(file, " DEF_%s_PIXEL_SIZE,\n", def_name);
  276. fprintf(file, " DEF_%s_SIZE\n};\n", def_name);
  277. fclose (file);
  278. return 0 ;
  279. }
  280. #define DEF_FILELEN 256
  281. int main (int argc, char *argv[])
  282. {
  283. char
  284. inputfile[DEF_FILELEN],
  285. outputfile[DEF_FILELEN],
  286. varname[DEF_FILELEN];
  287. image_t rgb_logo, yuyv_logo ;
  288. switch (argc){
  289. case 2:
  290. case 3:
  291. case 4:
  292. strcpy (inputfile, argv[1]);
  293. if (argc > 2)
  294. strcpy (varname, argv[2]);
  295. else
  296. {
  297. char *dot = strchr(inputfile, '.');
  298. int pos = dot - inputfile;
  299. if (dot)
  300. {
  301. strncpy (varname, inputfile, pos);
  302. varname[pos] = 0 ;
  303. }
  304. }
  305. if (argc > 3)
  306. strcpy (outputfile, argv[3]);
  307. else
  308. {
  309. char *dot = strchr (varname, '.');
  310. int pos = dot - varname;
  311. if (dot)
  312. {
  313. char app[DEF_FILELEN] ;
  314. strncpy(app, varname, pos);
  315. app[pos] = 0;
  316. sprintf(outputfile, "%s.h", app);
  317. }
  318. }
  319. break;
  320. default:
  321. printf("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n");
  322. printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n");
  323. printf("\n");
  324. printf("Where: 'inputfile' is the TGA image to load\n");
  325. printf(" 'outputvar' is the variable name to create\n");
  326. printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n");
  327. return -1 ;
  328. }
  329. printf("Doing '%s' (%s) from '%s'...",
  330. outputfile, varname, inputfile);
  331. /* Import TGA logo */
  332. printf("L");
  333. if (image_load_tga (&rgb_logo, inputfile)<0)
  334. {
  335. printf("input file not found!\n");
  336. exit(1);
  337. }
  338. /* Convert it to YUYV format */
  339. printf("C");
  340. image_rgb_to_yuyv (&rgb_logo, &yuyv_logo) ;
  341. /* Save it into a header format */
  342. printf("S");
  343. image_save_header (&yuyv_logo, outputfile, varname) ;
  344. /* Free original image and copy */
  345. image_free (&rgb_logo);
  346. image_free (&yuyv_logo);
  347. printf("\n");
  348. return 0 ;
  349. }