image_png.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "libpng/png.h"
  2. #include <rtthread.h>
  3. #include <rtgui/image_png.h>
  4. #include <rtgui/rtgui_system.h>
  5. #define PNG_MAGIC_LEN 8
  6. struct rtgui_image_png
  7. {
  8. rt_bool_t is_loaded;
  9. struct rtgui_filerw* filerw;
  10. /* png image information */
  11. png_structp png_ptr;
  12. png_infop info_ptr;
  13. rt_uint8_t *pixels;
  14. };
  15. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw* file);
  16. static rt_bool_t rtgui_image_png_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load);
  17. static void rtgui_image_png_unload(struct rtgui_image* image);
  18. static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect);
  19. struct rtgui_image_engine rtgui_image_png_engine =
  20. {
  21. "png",
  22. { RT_NULL },
  23. rtgui_image_png_check,
  24. rtgui_image_png_load,
  25. rtgui_image_png_unload,
  26. rtgui_image_png_blit
  27. };
  28. static void rtgui_image_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  29. {
  30. struct rtgui_filerw* filerw = (struct rtgui_filerw*)png_ptr->io_ptr;
  31. rtgui_filerw_read(filerw, data, length, 1);
  32. }
  33. static rt_bool_t rtgui_image_png_process(png_structp png_ptr, png_infop info_ptr, struct rtgui_image_png* png)
  34. {
  35. rt_uint32_t x, y;
  36. png_bytep row;
  37. png_bytep data;
  38. rtgui_color_t *ptr;
  39. row = (png_bytep) rtgui_malloc (png_get_rowbytes(png_ptr, info_ptr));
  40. if (row == RT_NULL) return RT_FALSE;
  41. ptr = (rtgui_color_t *)png->pixels;
  42. switch (info_ptr->color_type)
  43. {
  44. case PNG_COLOR_TYPE_RGBA:
  45. for (y = 0; y < info_ptr->height; y++)
  46. {
  47. png_read_row(png_ptr, row, png_bytep_NULL);
  48. for (x = 0; x < info_ptr->width; x++)
  49. {
  50. data = &(row[x * 4]);
  51. ptr[x+y*info_ptr->width] = RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]);
  52. }
  53. }
  54. break;
  55. case PNG_COLOR_TYPE_PALETTE:
  56. for (y = 0; y < info_ptr->height; y++)
  57. {
  58. png_read_row(png_ptr, row, png_bytep_NULL);
  59. for (x = 0; x < info_ptr->width; x++)
  60. {
  61. data = &(row[x]);
  62. ptr[x] = RTGUI_ARGB(0, info_ptr->palette[data[0]].red,
  63. info_ptr->palette[data[0]].green,
  64. info_ptr->palette[data[0]].blue);
  65. }
  66. }
  67. default:
  68. break;
  69. };
  70. rtgui_free(row);
  71. return RT_TRUE;
  72. }
  73. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw* file)
  74. {
  75. int start;
  76. rt_bool_t is_PNG;
  77. rt_uint8_t magic[4];
  78. if ( !file ) return 0;
  79. start = rtgui_filerw_tell(file);
  80. /* move to the begining of file */
  81. rtgui_filerw_seek(file, 0, SEEK_SET);
  82. is_PNG = RT_FALSE;
  83. if ( rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic) )
  84. {
  85. if ( magic[0] == 0x89 &&
  86. magic[1] == 'P' &&
  87. magic[2] == 'N' &&
  88. magic[3] == 'G' )
  89. {
  90. is_PNG = RT_TRUE;
  91. }
  92. }
  93. rtgui_filerw_seek(file, start, SEEK_SET);
  94. return(is_PNG);
  95. }
  96. static rt_bool_t rtgui_image_png_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load)
  97. {
  98. png_uint_32 width;
  99. png_uint_32 height;
  100. int bit_depth;
  101. int color_type;
  102. double gamma;
  103. struct rtgui_image_png* png;
  104. png = (struct rtgui_image_png*) rtgui_malloc(sizeof(struct rtgui_image_png));
  105. png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  106. if (png->png_ptr == RT_NULL)
  107. {
  108. rtgui_free(png);
  109. return RT_FALSE;
  110. }
  111. png->info_ptr = png_create_info_struct(png->png_ptr);
  112. if (png->info_ptr == RT_NULL)
  113. {
  114. png_destroy_read_struct(&png->png_ptr, NULL, NULL);
  115. rtgui_free(png);
  116. return RT_FALSE;
  117. }
  118. png->filerw = file;
  119. png_set_read_fn (png->png_ptr, png->filerw, rtgui_image_png_read_data);
  120. png_read_info(png->png_ptr, png->info_ptr);
  121. png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth,
  122. &color_type, NULL, NULL, NULL);
  123. /* set image information */
  124. image->w = width; image->h = height;
  125. image->engine = &rtgui_image_png_engine;
  126. image->data = png;
  127. if (bit_depth == 16)
  128. png_set_strip_16(png->png_ptr);
  129. if (color_type == PNG_COLOR_TYPE_PALETTE)
  130. png_set_expand(png->png_ptr);
  131. if (bit_depth < 8)
  132. png_set_expand(png->png_ptr);
  133. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  134. png_set_expand(png->png_ptr);
  135. if (color_type == PNG_COLOR_TYPE_GRAY ||
  136. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  137. png_set_gray_to_rgb(png->png_ptr);
  138. /* Ignore background color */
  139. /* set gamma conversion */
  140. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  141. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  142. png_read_update_info(png->png_ptr, png->info_ptr);
  143. if (load == RT_TRUE)
  144. {
  145. /* load all pixels */
  146. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  147. if (png->pixels == RT_NULL)
  148. {
  149. png_read_end(png->png_ptr, RT_NULL);
  150. /* destroy png struct */
  151. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  152. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  153. /* release data */
  154. rtgui_free(png);
  155. return RT_FALSE;
  156. }
  157. rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
  158. }
  159. else
  160. {
  161. png->pixels = RT_NULL;
  162. }
  163. return RT_TRUE;
  164. }
  165. static void rtgui_image_png_unload(struct rtgui_image* image)
  166. {
  167. struct rtgui_image_png* png;
  168. if (image != RT_NULL)
  169. {
  170. png = (struct rtgui_image_png*) image->data;
  171. png_read_end(png->png_ptr, RT_NULL);
  172. /* destroy png struct */
  173. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  174. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  175. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  176. /* release data */
  177. rtgui_free(png);
  178. }
  179. }
  180. static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  181. {
  182. rt_uint16_t x, y, w, h;
  183. rtgui_color_t* ptr;
  184. struct rtgui_image_png* png;
  185. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  186. RT_ASSERT(image->data != RT_NULL);
  187. png = (struct rtgui_image_png*) image->data;
  188. if (image->w < rtgui_rect_width(*rect)) w = image->w;
  189. else w = rtgui_rect_width(*rect);
  190. if (image->h < rtgui_rect_height(*rect)) h = image->h;
  191. else h = rtgui_rect_height(*rect);
  192. if (png->pixels != RT_NULL)
  193. {
  194. ptr = (rtgui_color_t*)png->pixels;
  195. /* draw each point within dc */
  196. for (y = 0; y < h; y ++)
  197. {
  198. for (x = 0; x < w; x++)
  199. {
  200. /* not alpha */
  201. if ((*ptr >> 24) != 255)
  202. {
  203. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr);
  204. }
  205. /* move to next color buffer */
  206. ptr ++;
  207. }
  208. }
  209. }
  210. else
  211. {
  212. png_bytep row;
  213. png_bytep data;
  214. row = (png_bytep) rtgui_malloc (png_get_rowbytes(png->png_ptr, png->info_ptr));
  215. if (row == RT_NULL) return ;
  216. switch (png->info_ptr->color_type)
  217. {
  218. case PNG_COLOR_TYPE_RGBA:
  219. for (y = 0; y < h; y++)
  220. {
  221. png_read_row(png->png_ptr, row, png_bytep_NULL);
  222. for (x = 0; x < w; x++)
  223. {
  224. data = &(row[x * 4]);
  225. if (data[3] != 0)
  226. {
  227. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  228. RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]));
  229. }
  230. }
  231. }
  232. break;
  233. case PNG_COLOR_TYPE_PALETTE:
  234. for (y = 0; y < h; y++)
  235. {
  236. png_read_row(png->png_ptr, row, png_bytep_NULL);
  237. for (x = 0; x < w; x++)
  238. {
  239. data = &(row[x]);
  240. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  241. RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  242. png->info_ptr->palette[data[0]].green,
  243. png->info_ptr->palette[data[0]].blue));
  244. }
  245. }
  246. default:
  247. break;
  248. };
  249. rtgui_free(row);
  250. }
  251. }
  252. void rtgui_image_png_init()
  253. {
  254. /* register png on image system */
  255. rtgui_image_register_engine(&rtgui_image_png_engine);
  256. }