image_png.c 9.5 KB

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