png2bin.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. png2bin.c
  3. Convert a png file to the internal file format, suitable for
  4. https://github.com/olikraus/u8g2/tree/master/sys/arduino/u8g2_page_buffer/LoadFromSD
  5. Copyright (c) 2019, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. http://www.libpng.org/pub/png/libpng-manual.txt
  28. */
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <stdint.h>
  35. #define PNG_DEBUG 3
  36. #include <png.h>
  37. /*================================================*/
  38. int is_invert = 0;
  39. int is_16bit = 0;
  40. int is_preview = 0;
  41. /*================================================*/
  42. int width, height;
  43. png_byte color_type;
  44. png_byte bit_depth;
  45. uint8_t min, max;
  46. png_structp png_ptr;
  47. png_infop info_ptr;
  48. png_bytep * row_pointers;
  49. void print_info(const char* file_name)
  50. {
  51. printf("%s: png_get_image_width(png_ptr, info_ptr)=%d\n", file_name, png_get_image_width(png_ptr, info_ptr));
  52. printf("%s: png_get_image_height(png_ptr, info_ptr)=%d\n", file_name, png_get_image_height(png_ptr, info_ptr));
  53. /*
  54. #define PNG_COLOR_MASK_PALETTE 1
  55. #define PNG_COLOR_MASK_COLOR 2
  56. #define PNG_COLOR_MASK_ALPHA 4
  57. #define PNG_COLOR_TYPE_GRAY 0
  58. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  59. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  60. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  61. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  62. */
  63. printf("%s: png_get_color_type(png_ptr, info_ptr)=%d\n", file_name, png_get_color_type(png_ptr, info_ptr));
  64. printf("%s: png_get_bit_depth(png_ptr, info_ptr)=%d\n", file_name, png_get_bit_depth(png_ptr, info_ptr));
  65. printf("%s: png_get_rowbytes(png_ptr, info_ptr)=%lu\n", file_name, png_get_rowbytes(png_ptr, info_ptr));
  66. printf("%s: range: %d .. %d\n", file_name, min, max);
  67. }
  68. void print_short_info(const char* file_name)
  69. {
  70. printf("%s rgb=%s alpha=%s w=%d h=%d\n",
  71. file_name,
  72. color_type&2?"no":"yes",
  73. color_type&4?"no":"yes",
  74. width, height);
  75. }
  76. uint8_t get_gray(int x, int y)
  77. {
  78. uint8_t *p;
  79. uint8_t g;
  80. if ( color_type & PNG_COLOR_MASK_ALPHA )
  81. {
  82. p = row_pointers[y] + x*4;
  83. g = ((int)p[0] + (int)p[1] + (int)p[2])/3;
  84. //g = ((int)g+(int)p[3])/2;
  85. return g;
  86. }
  87. p = row_pointers[y] + x*3;
  88. return ((int)p[0] + (int)p[1] + (int)p[2])/3;
  89. }
  90. uint8_t get_pixel(int x, int y)
  91. {
  92. if ( x < 0 )
  93. return 0;
  94. if ( x >= width )
  95. return 0;
  96. if ( y < 0 )
  97. return 0;
  98. if ( y >= height )
  99. return 0;
  100. if ( get_gray(x, y) > 128/2 )
  101. return is_invert == 0 ? 1 : 0;
  102. return is_invert == 0 ? 0 : 1;
  103. }
  104. uint8_t get_pixel_byte(int x, int y)
  105. {
  106. uint8_t b = 0;
  107. int i;
  108. for( i = 0; i < 8; i++ )
  109. {
  110. b |= get_pixel(x + i,y)<<i; // lsb contains the leftmost pixel
  111. }
  112. return b;
  113. }
  114. void write_bdf_bitmap(const char *filename)
  115. {
  116. int x, y;
  117. FILE *bin_fp;
  118. bin_fp = fopen(filename, "wb");
  119. if ( bin_fp == NULL )
  120. {
  121. perror(filename);
  122. return;
  123. }
  124. if ( is_16bit )
  125. {
  126. fputc(width>>8, bin_fp);
  127. fputc(width&255, bin_fp);
  128. fputc(height>>8, bin_fp);
  129. fputc(height&255, bin_fp);
  130. }
  131. else
  132. {
  133. fputc(width, bin_fp);
  134. fputc(height, bin_fp);
  135. }
  136. for( y = 0; y < height; y++ )
  137. {
  138. x = 0;
  139. while( x < width )
  140. {
  141. fputc(get_pixel_byte(x,y), bin_fp);
  142. x+=8;
  143. }
  144. }
  145. fclose(bin_fp);
  146. }
  147. void show_ascii(void)
  148. {
  149. int x, y;
  150. for( y = 0; y < height; y++ )
  151. {
  152. for( x = 0; x < width; x++ )
  153. {
  154. if ( is_invert )
  155. {
  156. if ( get_gray(x, y) <= 128/2 )
  157. printf("#");
  158. else
  159. printf(".");
  160. }
  161. else
  162. {
  163. if ( get_gray(x, y) > 128/2 )
  164. printf("#");
  165. else
  166. printf(".");
  167. }
  168. }
  169. printf("\n");
  170. }
  171. }
  172. void calc_min_max(void)
  173. {
  174. int x, y;
  175. uint8_t val;
  176. min = 255;
  177. max = 0;
  178. for( y = 0; y < height; y++ )
  179. {
  180. for( x = 0; x < width; x++ )
  181. {
  182. val = get_gray(x, y);
  183. if ( max < val )
  184. max = val;
  185. if ( min > val )
  186. min = val;
  187. }
  188. }
  189. }
  190. int read_png_file(const char* file_name, const char* bin_name)
  191. {
  192. unsigned char header[8]; // 8 is the maximum size that can be checked
  193. /* open file and test for it being a png */
  194. FILE *fp = fopen(file_name, "rb");
  195. if (fp == NULL)
  196. {
  197. perror(file_name);
  198. return 0; /* open error */
  199. }
  200. fread(header, 1, 8, fp);
  201. if (png_sig_cmp(header, 0, 8))
  202. return fclose(fp), 0; /* not a png file error */
  203. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  204. if (png_ptr == NULL)
  205. return fclose(fp), 0; /* alloc read struct */
  206. info_ptr = png_create_info_struct(png_ptr);
  207. if (info_ptr == NULL )
  208. return fclose(fp), 0; /* alloc info struct */
  209. if (setjmp(png_jmpbuf(png_ptr)))
  210. {
  211. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  212. return fclose(fp), 0; /* library error */
  213. }
  214. png_init_io(png_ptr, fp);
  215. png_set_sig_bytes(png_ptr, 8);
  216. png_read_png(png_ptr, info_ptr,
  217. PNG_TRANSFORM_SCALE_16|
  218. //PNG_TRANSFORM_STRIP_ALPHA|
  219. PNG_TRANSFORM_PACKING|
  220. //PNG_TRANSFORM_INVERT_ALPHA |
  221. //PNG_TRANSFORM_INVERT_MONO |
  222. PNG_TRANSFORM_GRAY_TO_RGB
  223. , NULL);
  224. width = png_get_image_width(png_ptr, info_ptr);
  225. height = png_get_image_height(png_ptr, info_ptr);
  226. color_type = png_get_color_type(png_ptr, info_ptr);
  227. bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  228. row_pointers = png_get_rows(png_ptr, info_ptr);
  229. //calc_min_max();
  230. //print_info(file_name);
  231. print_short_info(file_name);
  232. if ( is_preview )
  233. show_ascii();
  234. write_bdf_bitmap(bin_name);
  235. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  236. fclose(fp);
  237. return 1;
  238. }
  239. void help(void)
  240. {
  241. printf("png2bin [options] png-file bin-file\n");
  242. printf("Convert a png image to a binary \"XBM\" image, prefixed by hieght and length value.\n");
  243. printf("png-file format: RGB, gray, 1-bit index are supported, alpha channel is ignored, \n");
  244. printf("options:\n");
  245. printf(" -i Invert image\n");
  246. printf(" -p Preview image as ASCII art\n");
  247. printf(" -2 Two byte heigh/length value (16 bit values instead of 8 bit values, high byte first)\n");
  248. }
  249. int main(int argc, char **argv)
  250. {
  251. if ( argc < 3 )
  252. {
  253. help();
  254. return 0;
  255. }
  256. argc--; argv++;
  257. for(;;)
  258. {
  259. if ( argv[0][0] == '-' )
  260. {
  261. if ( strcmp(argv[0], "-i") == 0 )
  262. {
  263. argc--; argv++;
  264. is_invert = 1;
  265. }
  266. if ( strcmp(argv[0], "-2") == 0 )
  267. {
  268. argc--; argv++;
  269. is_16bit = 1;
  270. }
  271. if ( strcmp(argv[0], "-p") == 0 )
  272. {
  273. argc--; argv++;
  274. is_preview = 1;
  275. }
  276. }
  277. else
  278. {
  279. break;
  280. }
  281. }
  282. if ( argv[0] != NULL && argv[1] != NULL )
  283. {
  284. if ( strcmp(argv[0], argv[1]) == 0 )
  285. {
  286. printf("source and destination file must be different\n");
  287. }
  288. else
  289. {
  290. read_png_file(argv[0], argv[1]);
  291. }
  292. }
  293. else
  294. {
  295. help();
  296. }
  297. return 0;
  298. }