1
0

image_png.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include <rtthread.h>
  2. #include <rtgui/rtgui_system.h>
  3. #ifdef RTGUI_IMAGE_PNG
  4. #include "png.h"
  5. #include <rtgui/image_png.h>
  6. #define PNG_MAGIC_LEN 8
  7. struct rtgui_image_png
  8. {
  9. rt_bool_t is_loaded;
  10. struct rtgui_filerw *filerw;
  11. /* png image information */
  12. png_structp png_ptr;
  13. png_infop info_ptr;
  14. rt_uint8_t *pixels;
  15. };
  16. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file);
  17. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  18. static void rtgui_image_png_unload(struct rtgui_image *image);
  19. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  20. struct rtgui_image_engine rtgui_image_png_engine =
  21. {
  22. "png",
  23. { RT_NULL },
  24. rtgui_image_png_check,
  25. rtgui_image_png_load,
  26. rtgui_image_png_unload,
  27. rtgui_image_png_blit,
  28. RT_NULL,
  29. RT_NULL
  30. };
  31. static void rtgui_image_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  32. {
  33. struct rtgui_filerw *filerw = (struct rtgui_filerw *)png_ptr->io_ptr;
  34. rtgui_filerw_read(filerw, data, length, 1);
  35. }
  36. static rt_bool_t rtgui_image_png_process(png_structp png_ptr, png_infop info_ptr, struct rtgui_image_png *png)
  37. {
  38. rt_uint32_t x, y;
  39. png_bytep row;
  40. png_bytep data;
  41. rtgui_color_t *ptr;
  42. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png_ptr, info_ptr));
  43. if (row == RT_NULL) return RT_FALSE;
  44. ptr = (rtgui_color_t *)png->pixels;
  45. switch (info_ptr->color_type)
  46. {
  47. case PNG_COLOR_TYPE_RGB:
  48. for (y = 0; y < info_ptr->height; y++)
  49. {
  50. png_read_row(png_ptr, row, png_bytep_NULL);
  51. for (x = 0; x < info_ptr->width; x++)
  52. {
  53. data = &(row[x * 3]);
  54. ptr[x + y * info_ptr->width] = RTGUI_RGB(data[0], data[1], data[2]);
  55. }
  56. }
  57. break;
  58. case PNG_COLOR_TYPE_RGBA:
  59. for (y = 0; y < info_ptr->height; y++)
  60. {
  61. png_read_row(png_ptr, row, png_bytep_NULL);
  62. for (x = 0; x < info_ptr->width; x++)
  63. {
  64. data = &(row[x * 4]);
  65. ptr[x + y * info_ptr->width] = RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]);
  66. }
  67. }
  68. break;
  69. case PNG_COLOR_TYPE_PALETTE:
  70. for (y = 0; y < info_ptr->height; y++)
  71. {
  72. png_read_row(png_ptr, row, png_bytep_NULL);
  73. for (x = 0; x < info_ptr->width; x++)
  74. {
  75. data = &(row[x]);
  76. ptr[x] = RTGUI_ARGB(0, info_ptr->palette[data[0]].red,
  77. info_ptr->palette[data[0]].green,
  78. info_ptr->palette[data[0]].blue);
  79. }
  80. }
  81. default:
  82. break;
  83. };
  84. rtgui_free(row);
  85. return RT_TRUE;
  86. }
  87. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file)
  88. {
  89. int start;
  90. rt_bool_t is_PNG;
  91. rt_uint8_t magic[4];
  92. if (!file) return 0;
  93. start = rtgui_filerw_tell(file);
  94. /* move to the begining of file */
  95. rtgui_filerw_seek(file, 0, SEEK_SET);
  96. is_PNG = RT_FALSE;
  97. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  98. {
  99. if (magic[0] == 0x89 &&
  100. magic[1] == 'P' &&
  101. magic[2] == 'N' &&
  102. magic[3] == 'G')
  103. {
  104. is_PNG = RT_TRUE;
  105. }
  106. }
  107. rtgui_filerw_seek(file, start, SEEK_SET);
  108. return(is_PNG);
  109. }
  110. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  111. {
  112. png_uint_32 width;
  113. png_uint_32 height;
  114. int bit_depth;
  115. int color_type;
  116. double gamma;
  117. struct rtgui_image_png *png;
  118. png = (struct rtgui_image_png *) rtgui_malloc(sizeof(struct rtgui_image_png));
  119. png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  120. if (png->png_ptr == RT_NULL)
  121. {
  122. rtgui_free(png);
  123. return RT_FALSE;
  124. }
  125. png->info_ptr = png_create_info_struct(png->png_ptr);
  126. if (png->info_ptr == RT_NULL)
  127. {
  128. png_destroy_read_struct(&png->png_ptr, NULL, NULL);
  129. rtgui_free(png);
  130. return RT_FALSE;
  131. }
  132. png->filerw = file;
  133. png_set_read_fn(png->png_ptr, png->filerw, rtgui_image_png_read_data);
  134. png_read_info(png->png_ptr, png->info_ptr);
  135. png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth,
  136. &color_type, NULL, NULL, NULL);
  137. /* set image information */
  138. image->w = width;
  139. image->h = height;
  140. image->engine = &rtgui_image_png_engine;
  141. image->data = png;
  142. if (bit_depth == 16)
  143. png_set_strip_16(png->png_ptr);
  144. if (color_type == PNG_COLOR_TYPE_PALETTE)
  145. png_set_expand(png->png_ptr);
  146. if (bit_depth < 8)
  147. png_set_expand(png->png_ptr);
  148. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  149. png_set_expand(png->png_ptr);
  150. if (color_type == PNG_COLOR_TYPE_GRAY ||
  151. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  152. png_set_gray_to_rgb(png->png_ptr);
  153. /* Ignore background color */
  154. /* set gamma conversion */
  155. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  156. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  157. png_read_update_info(png->png_ptr, png->info_ptr);
  158. if (load == RT_TRUE)
  159. {
  160. /* load all pixels */
  161. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  162. if (png->pixels == RT_NULL)
  163. {
  164. png_read_end(png->png_ptr, RT_NULL);
  165. /* destroy png struct */
  166. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  167. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  168. /* release data */
  169. rtgui_free(png);
  170. return RT_FALSE;
  171. }
  172. rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
  173. }
  174. else
  175. {
  176. png->pixels = RT_NULL;
  177. }
  178. return RT_TRUE;
  179. }
  180. static void rtgui_image_png_unload(struct rtgui_image *image)
  181. {
  182. struct rtgui_image_png *png;
  183. if (image != RT_NULL)
  184. {
  185. png = (struct rtgui_image_png *) image->data;
  186. png_read_end(png->png_ptr, RT_NULL);
  187. /* destroy png struct */
  188. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  189. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  190. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  191. /* release data */
  192. rtgui_free(png);
  193. }
  194. }
  195. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  196. {
  197. struct rtgui_graphic_driver *hwdev = rtgui_graphic_get_device();
  198. rt_uint16_t x, y, w, h;
  199. rtgui_color_t *ptr;
  200. struct rtgui_image_png *png;
  201. int fg_maxsample;
  202. int ialpha;
  203. float alpha;
  204. rtgui_color_t color;
  205. rtgui_color_t c, bgcolor;
  206. int fc[3], bc[3];
  207. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  208. RT_ASSERT(image->data != RT_NULL);
  209. png = (struct rtgui_image_png *) image->data;
  210. if (image->w < rtgui_rect_width(*rect)) w = image->w;
  211. else w = rtgui_rect_width(*rect);
  212. if (image->h < rtgui_rect_height(*rect)) h = image->h;
  213. else h = rtgui_rect_height(*rect);
  214. fg_maxsample = (1 << png->info_ptr->bit_depth) - 1;
  215. if (png->pixels != RT_NULL)
  216. {
  217. ptr = (rtgui_color_t *)png->pixels;
  218. bgcolor = rtgui_color_from_565(RTGUI_DC_BC(dc));
  219. bc[0] = RTGUI_RGB_R(bgcolor);
  220. bc[1] = RTGUI_RGB_G(bgcolor);
  221. bc[2] = RTGUI_RGB_B(bgcolor);
  222. /* draw each point within dc */
  223. for (y = 0; y < h; y ++)
  224. {
  225. for (x = 0; x < w; x++)
  226. {
  227. c = *ptr;
  228. ialpha = RTGUI_RGB_A(c);
  229. if (ialpha == 0)
  230. {
  231. /*
  232. * Foreground image is transparent hear.
  233. * If the background image is already in the frame
  234. * buffer, there is nothing to do.
  235. */
  236. }
  237. else if (ialpha == fg_maxsample)
  238. {
  239. /*
  240. * Copy foreground pixel to frame buffer.
  241. */
  242. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, c);
  243. }
  244. else
  245. {
  246. /* output = alpha * foreground + (1-alpha) * background */
  247. /*
  248. * Compositing is necessary.
  249. * Get floating-point alpha and its complement.
  250. * Note: alpha is always linear: gamma does not
  251. * affect it.
  252. */
  253. fc[0] = RTGUI_RGB_R(c);
  254. fc[1] = RTGUI_RGB_G(c);
  255. fc[2] = RTGUI_RGB_B(c);
  256. alpha = (float) ialpha / fg_maxsample;
  257. color = RTGUI_RGB((rt_uint8_t)(fc[0] * alpha + bc[0] * (1 - alpha)),
  258. (rt_uint8_t)(fc[1] * alpha + bc[1] * (1 - alpha)),
  259. (rt_uint8_t)(fc[2] * alpha + bc[2] * (1 - alpha)));
  260. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, color);
  261. }
  262. /* move to next color buffer */
  263. ptr ++;
  264. }
  265. }
  266. }
  267. else
  268. {
  269. png_bytep row;
  270. png_bytep data;
  271. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png->png_ptr, png->info_ptr));
  272. if (row == RT_NULL) return ;
  273. switch (png->info_ptr->color_type)
  274. {
  275. case PNG_COLOR_TYPE_RGBA:
  276. for (y = 0; y < h; y++)
  277. {
  278. png_read_row(png->png_ptr, row, png_bytep_NULL);
  279. for (x = 0; x < w; x++)
  280. {
  281. data = &(row[x * 4]);
  282. if (data[3] != 0)
  283. {
  284. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  285. RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]));
  286. }
  287. }
  288. }
  289. break;
  290. case PNG_COLOR_TYPE_PALETTE:
  291. for (y = 0; y < h; y++)
  292. {
  293. png_read_row(png->png_ptr, row, png_bytep_NULL);
  294. for (x = 0; x < w; x++)
  295. {
  296. data = &(row[x]);
  297. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  298. RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  299. png->info_ptr->palette[data[0]].green,
  300. png->info_ptr->palette[data[0]].blue));
  301. }
  302. }
  303. default:
  304. break;
  305. };
  306. rtgui_free(row);
  307. }
  308. }
  309. void rtgui_image_png_init()
  310. {
  311. /* register png on image system */
  312. rtgui_image_register_engine(&rtgui_image_png_engine);
  313. }
  314. #endif