png2bdf.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. png2bdf.c
  3. Copyright (c) 2017, olikraus@gmail.com
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice, this list
  8. of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice, this
  10. list of conditions and the following disclaimer in the documentation and/or other
  11. materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  13. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. http://www.libpng.org/pub/png/libpng-manual.txt
  26. icons:
  27. http://ionicons.com/
  28. http://ionicons.com/cheatsheet.html
  29. https://github.com/iconic/open-iconic
  30. https://github.com/iconic/open-iconic/blob/master/font/css/open-iconic.styl
  31. gibts nicht mehr:
  32. https://www.joomlart.com/blog/news-updates/freebie-meals-vector-set-120-icons-png-svg
  33. */
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39. #include <stdint.h>
  40. #define PNG_DEBUG 3
  41. #include <png.h>
  42. /*================================================*/
  43. char *bdf_file_name = NULL;
  44. char *bdf_font_name = NULL;
  45. char *bdf_copyright = NULL;
  46. FILE *bdf_fp = NULL;
  47. long encoding = 48;
  48. int is_invert = 0;
  49. /*================================================*/
  50. int width, height;
  51. png_byte color_type;
  52. png_byte bit_depth;
  53. uint8_t min, max;
  54. png_structp png_ptr;
  55. png_infop info_ptr;
  56. png_bytep * row_pointers;
  57. void print_info(const char* file_name)
  58. {
  59. printf("%s: png_get_image_width(png_ptr, info_ptr)=%d\n", file_name, png_get_image_width(png_ptr, info_ptr));
  60. printf("%s: png_get_image_height(png_ptr, info_ptr)=%d\n", file_name, png_get_image_height(png_ptr, info_ptr));
  61. /*
  62. #define PNG_COLOR_MASK_PALETTE 1
  63. #define PNG_COLOR_MASK_COLOR 2
  64. #define PNG_COLOR_MASK_ALPHA 4
  65. #define PNG_COLOR_TYPE_GRAY 0
  66. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  67. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  68. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  69. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  70. */
  71. printf("%s: png_get_color_type(png_ptr, info_ptr)=%d\n", file_name, png_get_color_type(png_ptr, info_ptr));
  72. printf("%s: png_get_bit_depth(png_ptr, info_ptr)=%d\n", file_name, png_get_bit_depth(png_ptr, info_ptr));
  73. printf("%s: png_get_rowbytes(png_ptr, info_ptr)=%lu\n", file_name, png_get_rowbytes(png_ptr, info_ptr));
  74. printf("%s: range: %d .. %d\n", file_name, min, max);
  75. }
  76. void print_short_info(const char* file_name)
  77. {
  78. printf("%s enc=%ld w=%d h=%d\n", file_name, encoding, width, height);
  79. }
  80. uint8_t get_gray(int x, int y)
  81. {
  82. uint8_t *p;
  83. uint8_t g;
  84. if ( color_type & PNG_COLOR_MASK_ALPHA )
  85. {
  86. p = row_pointers[y] + x*4;
  87. g = ((int)p[0] + (int)p[1] + (int)p[2])/3;
  88. g = ((int)g+(int)p[3])/2;
  89. return g;
  90. }
  91. p = row_pointers[y] + x*3;
  92. return ((int)p[0] + (int)p[1] + (int)p[2])/3;
  93. }
  94. uint8_t get_pixel(int x, int y)
  95. {
  96. if ( x < 0 )
  97. return 0;
  98. if ( x >= width )
  99. return 0;
  100. if ( y < 0 )
  101. return 0;
  102. if ( y >= height )
  103. return 0;
  104. if ( get_gray(x, y) > 128/2 )
  105. return is_invert == 0 ? 1 : 0;
  106. return is_invert == 0 ? 0 : 1;
  107. }
  108. uint8_t get_pixel_byte(int x, int y)
  109. {
  110. uint8_t b = 0;
  111. int i;
  112. for( i = 0; i < 8; i++ )
  113. {
  114. b <<= 1;
  115. b |= get_pixel(x + i,y);
  116. }
  117. return b;
  118. }
  119. void write_4bit(int x)
  120. {
  121. if ( bdf_fp == NULL )
  122. return;
  123. if ( x < 10 )
  124. fprintf(bdf_fp, "%c", x + '0' );
  125. else
  126. fprintf(bdf_fp, "%c", x - 10 + 'a' );
  127. }
  128. void write_byte(int x)
  129. {
  130. write_4bit(x >> 4);
  131. write_4bit(x & 15);
  132. }
  133. void write_bdf_bitmap(const char *filename)
  134. {
  135. int x, y;
  136. if ( bdf_fp == NULL )
  137. return;
  138. fprintf(bdf_fp, "STARTCHAR %s\n", filename);
  139. fprintf(bdf_fp, "ENCODING %ld\n", encoding);
  140. fprintf(bdf_fp, "DWIDTH %d 0\n", width);
  141. fprintf(bdf_fp, "BBX %d %d 0 0\n", width, height);
  142. fprintf(bdf_fp, "BITMAP\n");
  143. for( y = 0; y < height; y++ )
  144. {
  145. x = 0;
  146. while( x < width )
  147. {
  148. write_byte(get_pixel_byte(x,y));
  149. x+=8;
  150. }
  151. fprintf(bdf_fp, "\n" );
  152. }
  153. fprintf(bdf_fp, "ENDCHAR\n");
  154. encoding++;
  155. }
  156. void show_ascii(void)
  157. {
  158. int x, y;
  159. for( y = 0; y < height; y++ )
  160. {
  161. for( x = 0; x < width; x++ )
  162. {
  163. if ( get_gray(x, y) > 128/2 )
  164. printf("#");
  165. else
  166. printf(".");
  167. }
  168. printf("\n");
  169. }
  170. }
  171. void calc_min_max(void)
  172. {
  173. int x, y;
  174. uint8_t val;
  175. min = 255;
  176. max = 0;
  177. for( y = 0; y < height; y++ )
  178. {
  179. for( x = 0; x < width; x++ )
  180. {
  181. val = get_gray(x, y);
  182. if ( max < val )
  183. max = val;
  184. if ( min > val )
  185. min = val;
  186. }
  187. }
  188. }
  189. int read_png_file(const char* file_name)
  190. {
  191. unsigned char header[8]; // 8 is the maximum size that can be checked
  192. /* open file and test for it being a png */
  193. FILE *fp = fopen(file_name, "rb");
  194. if (fp == NULL)
  195. return 0; /* open error */
  196. fread(header, 1, 8, fp);
  197. if (png_sig_cmp(header, 0, 8))
  198. return fclose(fp), 0; /* not a png file error */
  199. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  200. if (png_ptr == NULL)
  201. return fclose(fp), 0; /* alloc read struct */
  202. info_ptr = png_create_info_struct(png_ptr);
  203. if (info_ptr == NULL )
  204. return fclose(fp), 0; /* alloc info struct */
  205. if (setjmp(png_jmpbuf(png_ptr)))
  206. {
  207. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  208. return fclose(fp), 0; /* library error */
  209. }
  210. png_init_io(png_ptr, fp);
  211. png_set_sig_bytes(png_ptr, 8);
  212. png_read_png(png_ptr, info_ptr,
  213. PNG_TRANSFORM_SCALE_16|
  214. //PNG_TRANSFORM_STRIP_ALPHA|
  215. PNG_TRANSFORM_PACKING|
  216. //PNG_TRANSFORM_INVERT_ALPHA |
  217. //PNG_TRANSFORM_INVERT_MONO |
  218. PNG_TRANSFORM_GRAY_TO_RGB
  219. , NULL);
  220. width = png_get_image_width(png_ptr, info_ptr);
  221. height = png_get_image_height(png_ptr, info_ptr);
  222. color_type = png_get_color_type(png_ptr, info_ptr);
  223. bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  224. row_pointers = png_get_rows(png_ptr, info_ptr);
  225. //calc_min_max();
  226. //print_info(file_name);
  227. print_short_info(file_name);
  228. //show_ascii();
  229. write_bdf_bitmap(file_name);
  230. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  231. fclose(fp);
  232. return 1;
  233. }
  234. void help(void)
  235. {
  236. printf("png2bdf [BDF options] { [PNG options] png-file }\n");
  237. printf(" BDF options (use at the beginning):'\n");
  238. printf(" -o output.bdf\n");
  239. printf(" -f 'name of the font'\n");
  240. printf(" -c 'copyright note'\n");
  241. printf(" PNG options (use before png-file):'\n");
  242. printf(" -e <enc> Use the specified encoding number for the next image.\n");
  243. printf(" -i Invert next images\n");
  244. printf(" -n Do not invert next images\n");
  245. }
  246. void write_bdf_header(void)
  247. {
  248. fprintf(bdf_fp, "STARTFONT 2.1\n");
  249. fprintf(bdf_fp, "FONT");
  250. if ( bdf_font_name == NULL )
  251. fprintf(bdf_fp, " \"unknown\"\n");
  252. else
  253. fprintf(bdf_fp, " \"%s\"\n", bdf_font_name);
  254. fprintf(bdf_fp, "SIZE 16 75 75\n");
  255. fprintf(bdf_fp, "FONTBOUNDINGBOX 16 16 0 0\n");
  256. fprintf(bdf_fp, "STARTPROPERTIES 3\n");
  257. fprintf(bdf_fp, "COPYRIGHT");
  258. if ( bdf_copyright == NULL )
  259. fprintf(bdf_fp, " \"unknown\"\n");
  260. else
  261. fprintf(bdf_fp, " \"%s\"\n", bdf_copyright);
  262. fprintf(bdf_fp, "FONT_ASCENT 0\n");
  263. fprintf(bdf_fp, "FONT_DESCENT 0\n");
  264. fprintf(bdf_fp, "ENDPROPERTIES\n");
  265. }
  266. int main(int argc, char **argv)
  267. {
  268. if ( argc < 2 )
  269. {
  270. help();
  271. return 0;
  272. }
  273. argc--; argv++;
  274. do
  275. {
  276. if ( argv[0][0] == '-' )
  277. {
  278. if ( strcmp(argv[0], "-f") == 0 )
  279. {
  280. argc--; argv++;
  281. if ( argc > 0 )
  282. {
  283. bdf_font_name = argv[0];
  284. argc--; argv++;
  285. }
  286. }
  287. if ( strcmp(argv[0], "-o") == 0 )
  288. {
  289. argc--; argv++;
  290. if ( argc > 0 )
  291. {
  292. bdf_file_name = argv[0];
  293. argc--; argv++;
  294. bdf_fp = fopen(bdf_file_name, "wb");
  295. if ( bdf_fp == NULL )
  296. {
  297. perror(bdf_file_name);
  298. exit(1);
  299. }
  300. write_bdf_header();
  301. }
  302. }
  303. else if ( strcmp(argv[0], "-c") == 0 )
  304. {
  305. argc--; argv++;
  306. if ( argc > 0 )
  307. {
  308. bdf_copyright = argv[0];
  309. argc--; argv++;
  310. }
  311. }
  312. else if ( strcmp(argv[0], "-e") == 0 )
  313. {
  314. argc--; argv++;
  315. if ( argc > 0 )
  316. {
  317. encoding = atol(argv[0]);
  318. argc--; argv++;
  319. }
  320. }
  321. else if ( strcmp(argv[0], "-i") == 0 )
  322. {
  323. argc--; argv++;
  324. is_invert = 1;
  325. }
  326. else if ( strcmp(argv[0], "-n") == 0 )
  327. {
  328. argc--; argv++;
  329. is_invert = 0;
  330. }
  331. }
  332. else
  333. {
  334. {
  335. /* parse png image */
  336. read_png_file(argv[0]);
  337. argc--; argv++;
  338. }
  339. }
  340. } while( argc > 0 );
  341. if ( bdf_fp != NULL )
  342. fclose(bdf_fp);
  343. return 0;
  344. }