image_png.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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; image->h = height;
  139. image->engine = &rtgui_image_png_engine;
  140. image->data = png;
  141. if (bit_depth == 16)
  142. png_set_strip_16(png->png_ptr);
  143. if (color_type == PNG_COLOR_TYPE_PALETTE)
  144. png_set_expand(png->png_ptr);
  145. if (bit_depth < 8)
  146. png_set_expand(png->png_ptr);
  147. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  148. png_set_expand(png->png_ptr);
  149. if (color_type == PNG_COLOR_TYPE_GRAY ||
  150. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  151. png_set_gray_to_rgb(png->png_ptr);
  152. /* Ignore background color */
  153. /* set gamma conversion */
  154. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  155. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  156. png_read_update_info(png->png_ptr, png->info_ptr);
  157. if (load == RT_TRUE)
  158. {
  159. /* load all pixels */
  160. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  161. if (png->pixels == RT_NULL)
  162. {
  163. png_read_end(png->png_ptr, RT_NULL);
  164. /* destroy png struct */
  165. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  166. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  167. /* release data */
  168. rtgui_free(png);
  169. return RT_FALSE;
  170. }
  171. rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
  172. }
  173. else
  174. {
  175. png->pixels = RT_NULL;
  176. }
  177. return RT_TRUE;
  178. }
  179. static void rtgui_image_png_unload(struct rtgui_image* image)
  180. {
  181. struct rtgui_image_png* png;
  182. if (image != RT_NULL)
  183. {
  184. png = (struct rtgui_image_png*) image->data;
  185. png_read_end(png->png_ptr, RT_NULL);
  186. /* destroy png struct */
  187. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  188. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  189. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  190. /* release data */
  191. rtgui_free(png);
  192. }
  193. }
  194. static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  195. {
  196. struct rtgui_graphic_driver* hwdev = rtgui_graphic_get_device();
  197. rt_uint16_t x, y, w, h;
  198. rtgui_color_t* ptr;
  199. struct rtgui_image_png* png;
  200. int fg_maxsample;
  201. int ialpha;
  202. float alpha;
  203. rtgui_color_t color;
  204. rtgui_color_t c, bgcolor;
  205. int fc[3], bc[3];
  206. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  207. RT_ASSERT(image->data != RT_NULL);
  208. png = (struct rtgui_image_png*) image->data;
  209. if (image->w < rtgui_rect_width(*rect)) w = image->w;
  210. else w = rtgui_rect_width(*rect);
  211. if (image->h < rtgui_rect_height(*rect)) h = image->h;
  212. else h = rtgui_rect_height(*rect);
  213. fg_maxsample = (1 << png->info_ptr->bit_depth) - 1;
  214. if (png->pixels != RT_NULL)
  215. {
  216. ptr = (rtgui_color_t*)png->pixels;
  217. bgcolor = rtgui_color_from_565(RTGUI_DC_BC(dc));
  218. bc[0] = RTGUI_RGB_R(bgcolor);
  219. bc[1] = RTGUI_RGB_G(bgcolor);
  220. bc[2] = RTGUI_RGB_B(bgcolor);
  221. /* draw each point within dc */
  222. for (y = 0; y < h; y ++)
  223. {
  224. for (x = 0; x < w; x++)
  225. {
  226. c = *ptr;
  227. ialpha = RTGUI_RGB_A(c);
  228. if(ialpha == 0)
  229. {
  230. /*
  231. * Foreground image is transparent hear.
  232. * If the background image is already in the frame
  233. * buffer, there is nothing to do.
  234. */
  235. }
  236. else if (ialpha == fg_maxsample)
  237. {
  238. /*
  239. * Copy foreground pixel to frame buffer.
  240. */
  241. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, c);
  242. }
  243. else
  244. { /* output = alpha * foreground + (1-alpha) * background */
  245. /*
  246. * Compositing is necessary.
  247. * Get floating-point alpha and its complement.
  248. * Note: alpha is always linear: gamma does not
  249. * affect it.
  250. */
  251. fc[0] = RTGUI_RGB_R(c);
  252. fc[1] = RTGUI_RGB_G(c);
  253. fc[2] = RTGUI_RGB_B(c);
  254. alpha = (float) ialpha / fg_maxsample;
  255. color = RTGUI_RGB( (rt_uint8_t)(fc[0]*alpha + bc[0]*(1-alpha)),
  256. (rt_uint8_t)(fc[1]*alpha + bc[1]*(1-alpha)),
  257. (rt_uint8_t)(fc[2]*alpha + bc[2]*(1-alpha)));
  258. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, color);
  259. }
  260. /* move to next color buffer */
  261. ptr ++;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. png_bytep row;
  268. png_bytep data;
  269. row = (png_bytep) rtgui_malloc (png_get_rowbytes(png->png_ptr, png->info_ptr));
  270. if (row == RT_NULL) return ;
  271. switch (png->info_ptr->color_type)
  272. {
  273. case PNG_COLOR_TYPE_RGBA:
  274. for (y = 0; y < h; y++)
  275. {
  276. png_read_row(png->png_ptr, row, png_bytep_NULL);
  277. for (x = 0; x < w; x++)
  278. {
  279. data = &(row[x * 4]);
  280. if (data[3] != 0)
  281. {
  282. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  283. RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]));
  284. }
  285. }
  286. }
  287. break;
  288. case PNG_COLOR_TYPE_PALETTE:
  289. for (y = 0; y < h; y++)
  290. {
  291. png_read_row(png->png_ptr, row, png_bytep_NULL);
  292. for (x = 0; x < w; x++)
  293. {
  294. data = &(row[x]);
  295. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  296. RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  297. png->info_ptr->palette[data[0]].green,
  298. png->info_ptr->palette[data[0]].blue));
  299. }
  300. }
  301. default:
  302. break;
  303. };
  304. rtgui_free(row);
  305. }
  306. }
  307. void rtgui_image_png_init()
  308. {
  309. /* register png on image system */
  310. rtgui_image_register_engine(&rtgui_image_png_engine);
  311. }
  312. #endif