image_png.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * File : image_png.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-09-15 Bernard first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtgui/rtgui_system.h>
  26. #include <rtgui/blit.h>
  27. #include <rtgui/driver.h>
  28. #ifdef RTGUI_IMAGE_PNG
  29. #include "png.h"
  30. #define PNG_MAGIC_LEN 8
  31. struct rtgui_image_png
  32. {
  33. rt_bool_t is_loaded;
  34. struct rtgui_filerw *filerw;
  35. /* png image information */
  36. png_structp png_ptr;
  37. png_infop info_ptr;
  38. rt_uint8_t *pixels;
  39. };
  40. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file);
  41. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  42. static void rtgui_image_png_unload(struct rtgui_image *image);
  43. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  44. struct rtgui_image_engine rtgui_image_png_engine =
  45. {
  46. "png",
  47. { RT_NULL },
  48. rtgui_image_png_check,
  49. rtgui_image_png_load,
  50. rtgui_image_png_unload,
  51. rtgui_image_png_blit,
  52. };
  53. static void rtgui_image_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  54. {
  55. struct rtgui_filerw *filerw = (struct rtgui_filerw *)png_ptr->io_ptr;
  56. rtgui_filerw_read(filerw, data, length, 1);
  57. }
  58. static rt_bool_t rtgui_image_png_process(png_structp png_ptr, png_infop info_ptr, struct rtgui_image_png *png)
  59. {
  60. rt_uint32_t x, y;
  61. png_bytep row;
  62. png_bytep data;
  63. rtgui_color_t *ptr;
  64. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png_ptr, info_ptr));
  65. if (row == RT_NULL) return RT_FALSE;
  66. ptr = (rtgui_color_t *)png->pixels;
  67. switch (info_ptr->color_type)
  68. {
  69. case PNG_COLOR_TYPE_RGB:
  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 * 3]);
  76. ptr[x + y * info_ptr->width] = RTGUI_RGB(data[0], data[1], data[2]);
  77. }
  78. }
  79. break;
  80. case PNG_COLOR_TYPE_RGBA:
  81. for (y = 0; y < info_ptr->height; y++)
  82. {
  83. png_read_row(png_ptr, row, png_bytep_NULL);
  84. for (x = 0; x < info_ptr->width; x++)
  85. {
  86. data = &(row[x * 4]);
  87. ptr[x + y * info_ptr->width] = RTGUI_ARGB(data[3], data[0], data[1], data[2]);
  88. }
  89. }
  90. break;
  91. case PNG_COLOR_TYPE_PALETTE:
  92. for (y = 0; y < info_ptr->height; y++)
  93. {
  94. png_read_row(png_ptr, row, png_bytep_NULL);
  95. for (x = 0; x < info_ptr->width; x++)
  96. {
  97. data = &(row[x]);
  98. ptr[x] = RTGUI_ARGB(0, info_ptr->palette[data[0]].red,
  99. info_ptr->palette[data[0]].green,
  100. info_ptr->palette[data[0]].blue);
  101. }
  102. }
  103. default:
  104. break;
  105. };
  106. rtgui_free(row);
  107. return RT_TRUE;
  108. }
  109. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file)
  110. {
  111. int start;
  112. rt_bool_t is_PNG;
  113. rt_uint8_t magic[4];
  114. if (!file) return 0;
  115. start = rtgui_filerw_tell(file);
  116. /* move to the begining of file */
  117. rtgui_filerw_seek(file, 0, SEEK_SET);
  118. is_PNG = RT_FALSE;
  119. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  120. {
  121. if (magic[0] == 0x89 &&
  122. magic[1] == 'P' &&
  123. magic[2] == 'N' &&
  124. magic[3] == 'G')
  125. {
  126. is_PNG = RT_TRUE;
  127. }
  128. }
  129. rtgui_filerw_seek(file, start, SEEK_SET);
  130. return(is_PNG);
  131. }
  132. static void _image_png_error_fn(png_structp png_ptr, png_const_charp error_message)
  133. {
  134. rt_kprintf(error_message);
  135. }
  136. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  137. {
  138. png_uint_32 width;
  139. png_uint_32 height;
  140. int bit_depth;
  141. int color_type;
  142. double gamma;
  143. struct rtgui_image_png *png;
  144. png = (struct rtgui_image_png *) rtgui_malloc(sizeof(struct rtgui_image_png));
  145. png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  146. if (png->png_ptr == RT_NULL)
  147. {
  148. rtgui_free(png);
  149. return RT_FALSE;
  150. }
  151. png_set_error_fn(png->png_ptr, RT_NULL, _image_png_error_fn, _image_png_error_fn);
  152. png->info_ptr = png_create_info_struct(png->png_ptr);
  153. if (png->info_ptr == RT_NULL)
  154. {
  155. png_destroy_read_struct(&png->png_ptr, NULL, NULL);
  156. rtgui_free(png);
  157. return RT_FALSE;
  158. }
  159. png->filerw = file;
  160. png->is_loaded = RT_FALSE;
  161. png_set_read_fn(png->png_ptr, png->filerw, rtgui_image_png_read_data);
  162. png_read_info(png->png_ptr, png->info_ptr);
  163. png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth,
  164. &color_type, NULL, NULL, NULL);
  165. /* set image information */
  166. image->w = width;
  167. image->h = height;
  168. image->engine = &rtgui_image_png_engine;
  169. image->data = png;
  170. if (bit_depth == 16)
  171. png_set_strip_16(png->png_ptr);
  172. if (color_type == PNG_COLOR_TYPE_PALETTE)
  173. png_set_expand(png->png_ptr);
  174. if (bit_depth < 8)
  175. png_set_expand(png->png_ptr);
  176. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  177. png_set_expand(png->png_ptr);
  178. if (color_type == PNG_COLOR_TYPE_GRAY ||
  179. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  180. png_set_gray_to_rgb(png->png_ptr);
  181. /* Ignore background color */
  182. /* set gamma conversion */
  183. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  184. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  185. png_read_update_info(png->png_ptr, png->info_ptr);
  186. if (load == RT_TRUE)
  187. {
  188. /* load all pixels */
  189. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  190. if (png->pixels == RT_NULL)
  191. {
  192. png_read_end(png->png_ptr, RT_NULL);
  193. /* destroy png struct */
  194. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  195. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  196. /* release data */
  197. rtgui_free(png);
  198. return RT_FALSE;
  199. }
  200. rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
  201. png_read_end(png->png_ptr, RT_NULL);
  202. png->is_loaded = RT_TRUE;
  203. /* close file handler */
  204. rtgui_filerw_close(png->filerw);
  205. png->filerw = RT_NULL;
  206. }
  207. else
  208. {
  209. png->pixels = RT_NULL;
  210. }
  211. return RT_TRUE;
  212. }
  213. static void rtgui_image_png_unload(struct rtgui_image *image)
  214. {
  215. struct rtgui_image_png *png;
  216. if (image != RT_NULL)
  217. {
  218. png = (struct rtgui_image_png *) image->data;
  219. /* destroy png struct */
  220. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  221. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  222. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  223. /* release data */
  224. rtgui_free(png);
  225. }
  226. }
  227. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  228. {
  229. rt_uint16_t x, y, w, h;
  230. rtgui_color_t *ptr;
  231. struct rtgui_image_png *png;
  232. int fg_maxsample;
  233. int ialpha;
  234. float alpha;
  235. rtgui_color_t color;
  236. rtgui_color_t c, bgcolor;
  237. int fc[3], bc[3];
  238. struct rtgui_graphic_driver *hwdev = rtgui_graphic_get_device();
  239. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  240. RT_ASSERT(image->data != RT_NULL);
  241. png = (struct rtgui_image_png *) image->data;
  242. w = _UI_MIN(image->w, rtgui_rect_width(*rect));
  243. h = _UI_MIN(image->h, rtgui_rect_height(*rect));
  244. fg_maxsample = (1 << png->info_ptr->bit_depth) - 1;
  245. if (png->pixels != RT_NULL)
  246. {
  247. ptr = (rtgui_color_t *)png->pixels;
  248. bgcolor = RTGUI_DC_BC(dc);
  249. bc[0] = RTGUI_RGB_R(bgcolor);
  250. bc[1] = RTGUI_RGB_G(bgcolor);
  251. bc[2] = RTGUI_RGB_B(bgcolor);
  252. /* draw each point within dc */
  253. for (y = 0; y < h; y ++)
  254. {
  255. for (x = 0; x < w; x++)
  256. {
  257. c = *ptr;
  258. ialpha = RTGUI_RGB_A(c);
  259. if (ialpha == 0)
  260. {
  261. /*
  262. * Foreground image is transparent hear.
  263. * If the background image is already in the frame
  264. * buffer, there is nothing to do.
  265. */
  266. }
  267. else if (ialpha == fg_maxsample)
  268. {
  269. /*
  270. * Copy foreground pixel to frame buffer.
  271. */
  272. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, c);
  273. }
  274. else
  275. {
  276. /* output = alpha * foreground + (1-alpha) * background */
  277. /*
  278. * Compositing is necessary.
  279. * Get floating-point alpha and its complement.
  280. * Note: alpha is always linear: gamma does not
  281. * affect it.
  282. */
  283. fc[0] = RTGUI_RGB_R(c);
  284. fc[1] = RTGUI_RGB_G(c);
  285. fc[2] = RTGUI_RGB_B(c);
  286. alpha = (float) ialpha / fg_maxsample;
  287. color = RTGUI_RGB((rt_uint8_t)(fc[0] * alpha + bc[0] * (1 - alpha)),
  288. (rt_uint8_t)(fc[1] * alpha + bc[1] * (1 - alpha)),
  289. (rt_uint8_t)(fc[2] * alpha + bc[2] * (1 - alpha)));
  290. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, color);
  291. }
  292. /* move to next color buffer */
  293. ptr ++;
  294. }
  295. }
  296. }
  297. else
  298. {
  299. png_bytep row;
  300. png_bytep data;
  301. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png->png_ptr, png->info_ptr));
  302. if (row == RT_NULL) return ;
  303. switch (png->info_ptr->color_type)
  304. {
  305. case PNG_COLOR_TYPE_RGB:
  306. for (y = 0; y < h; y++)
  307. {
  308. png_read_row(png->png_ptr, row, png_bytep_NULL);
  309. for (x = 0; x < w; x++)
  310. {
  311. data = &(row[x * 3]);
  312. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  313. RTGUI_RGB(data[0], data[1], data[2]));
  314. }
  315. }
  316. break;
  317. case PNG_COLOR_TYPE_RGBA:
  318. for (y = 0; y < h; y++)
  319. {
  320. png_read_row(png->png_ptr, row, png_bytep_NULL);
  321. for (x = 0; x < w; x++)
  322. {
  323. data = &(row[x * 4]);
  324. if (data[3] != 0)
  325. {
  326. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  327. RTGUI_ARGB(data[3], data[0], data[1], data[2]));
  328. }
  329. }
  330. }
  331. break;
  332. case PNG_COLOR_TYPE_PALETTE:
  333. for (y = 0; y < h; y++)
  334. {
  335. png_read_row(png->png_ptr, row, png_bytep_NULL);
  336. for (x = 0; x < w; x++)
  337. {
  338. data = &(row[x]);
  339. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  340. RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  341. png->info_ptr->palette[data[0]].green,
  342. png->info_ptr->palette[data[0]].blue));
  343. }
  344. }
  345. default:
  346. break;
  347. };
  348. rtgui_free(row);
  349. }
  350. }
  351. void rtgui_image_png_init()
  352. {
  353. /* register png on image system */
  354. rtgui_image_register_engine(&rtgui_image_png_engine);
  355. }
  356. #elif defined(RTGUI_IMAGE_LODEPNG)
  357. #include "lodepng.h"
  358. #include <rtgui/image_png.h>
  359. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file);
  360. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  361. static void rtgui_image_png_unload(struct rtgui_image *image);
  362. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  363. struct rtgui_image_engine rtgui_image_png_engine =
  364. {
  365. "png",
  366. { RT_NULL },
  367. rtgui_image_png_check,
  368. rtgui_image_png_load,
  369. rtgui_image_png_unload,
  370. rtgui_image_png_blit,
  371. };
  372. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file)
  373. {
  374. int start;
  375. rt_bool_t is_PNG;
  376. rt_uint8_t magic[4];
  377. if (!file) return 0;
  378. start = rtgui_filerw_tell(file);
  379. /* move to the begining of file */
  380. rtgui_filerw_seek(file, 0, SEEK_SET);
  381. is_PNG = RT_FALSE;
  382. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  383. {
  384. if (magic[0] == 0x89 &&
  385. magic[1] == 'P' &&
  386. magic[2] == 'N' &&
  387. magic[3] == 'G')
  388. {
  389. is_PNG = RT_TRUE;
  390. }
  391. }
  392. rtgui_filerw_seek(file, start, SEEK_SET);
  393. return(is_PNG);
  394. }
  395. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  396. {
  397. unsigned int width;
  398. unsigned int height;
  399. unsigned int error;
  400. rt_uint8_t* pixel;
  401. rt_uint8_t* in;
  402. rt_uint32_t in_size;
  403. RT_ASSERT(image != RT_NULL);
  404. RT_ASSERT(file != RT_NULL);
  405. rtgui_filerw_seek(file, 0, SEEK_END);
  406. in_size = rtgui_filerw_tell(file);
  407. in = rtgui_malloc(in_size);
  408. if (in == RT_NULL) return RT_FALSE; /* out of memory */
  409. rtgui_filerw_seek(file, 0, SEEK_SET);
  410. rtgui_filerw_read(file, in, in_size, 1);
  411. error = lodepng_decode32(&pixel, &width, &height, in, in_size);
  412. if(error)
  413. {
  414. rt_kprintf("error %u: %s\n", error, lodepng_error_text(error));
  415. rtgui_free(in);
  416. return RT_FALSE;
  417. }
  418. rtgui_free(in);
  419. /* set image information */
  420. image->w = width;
  421. image->h = height;
  422. image->engine = &rtgui_image_png_engine;
  423. image->data = pixel;
  424. /* NOTE: the pixel format of PNG is ABGR888, bit0 R,G,B,A bit31 */
  425. /* convert pixel to ARGB888, swap B/G */
  426. {
  427. rt_uint8_t* pixel_ptr;
  428. rt_uint8_t* pixel_end;
  429. pixel_ptr = (rt_uint8_t*) pixel;
  430. pixel_end = pixel_ptr + width * height * 4;
  431. while (pixel_ptr < pixel_end)
  432. {
  433. pixel_ptr[0] = pixel_ptr[0] ^ pixel_ptr[2];
  434. pixel_ptr[2] = pixel_ptr[0] ^ pixel_ptr[2];
  435. pixel_ptr[0] = pixel_ptr[0] ^ pixel_ptr[2];
  436. pixel_ptr += 4;
  437. }
  438. }
  439. /* close file handler */
  440. rtgui_filerw_close(file);
  441. return RT_TRUE;
  442. }
  443. static void rtgui_image_png_unload(struct rtgui_image *image)
  444. {
  445. rt_uint8_t *pixels;
  446. if (image != RT_NULL)
  447. {
  448. pixels = (rt_uint8_t*) image->data;
  449. /* release data */
  450. //rtgui_free(pixels);
  451. free(pixels);
  452. }
  453. }
  454. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  455. {
  456. int x, y;
  457. int w, h;
  458. struct rtgui_blit_info info;
  459. struct rtgui_graphic_driver *hw_driver = rtgui_graphic_driver_get_default();
  460. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  461. RT_ASSERT(image->data != RT_NULL);
  462. #define blending(s, d, a) (((unsigned)(((s) - (d)) * (a)) >> 8) + (d))
  463. /* this dc is not visible */
  464. if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
  465. w = _UI_MIN(image->w, rtgui_rect_width(*rect));
  466. h = _UI_MIN(image->h, rtgui_rect_height(*rect));
  467. /* border checking */
  468. if (rect->x1 < 0)
  469. {
  470. x = -rect->x1;
  471. w += rect->x1;
  472. }
  473. else x = 0;
  474. if (rect->y1 < 0)
  475. {
  476. y = -rect->y1;
  477. h += rect->y1;
  478. }
  479. else y = 0;
  480. if (w < 0 || h < 0) return; /* no drawing */
  481. if ((dc->type == RTGUI_DC_CLIENT) || (dc->type == RTGUI_DC_HW && hw_driver->framebuffer == RT_NULL))
  482. {
  483. int dx, dy, start_x;
  484. rtgui_rect_t r;
  485. rtgui_color_t *pixel;
  486. rt_uint8_t alpha;
  487. rtgui_widget_t *owner = RT_NULL;
  488. if (dc->type == RTGUI_DC_CLIENT)
  489. {
  490. /* get owner and calculate dx,dy */
  491. owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
  492. dx = owner->extent.x1;
  493. dy = owner->extent.y1;
  494. }
  495. else
  496. {
  497. /* hardware DC */
  498. struct rtgui_dc_hw *hw = (struct rtgui_dc_hw *) dc;
  499. dx = hw->owner->extent.x1;
  500. dy = hw->owner->extent.y1;
  501. }
  502. start_x = x;
  503. for (; y < rect->y1 + h; ++y)
  504. {
  505. for (x = start_x; x < rect->x1 + w; ++x)
  506. {
  507. pixel = (rtgui_color_t*)((rt_uint8_t*)image->data + (y - rect->y1) * image->w * 4 +
  508. (x - rect->x1) * 4);
  509. alpha = RTGUI_RGB_A(*pixel);
  510. if (alpha == 0) continue;
  511. if (alpha == 0xff)
  512. {
  513. rtgui_dc_draw_color_point(dc, x, y, *pixel);
  514. }
  515. else
  516. {
  517. rtgui_color_t bc, fc;
  518. /* draw an alpha blending point */
  519. if (hw_driver->framebuffer != RT_NULL)
  520. rtgui_dc_blend_point(dc, x, y, RTGUI_BLENDMODE_BLEND,
  521. RTGUI_RGB_R(*pixel), RTGUI_RGB_G(*pixel), RTGUI_RGB_B(*pixel), RTGUI_RGB_A(*pixel));
  522. else
  523. {
  524. x = x + dx;
  525. y = y + dy;
  526. if (dc->type == RTGUI_DC_CLIENT)
  527. {
  528. if (rtgui_region_contains_point(&(owner->clip), x, y, &r) != RT_EOK)
  529. continue ;
  530. }
  531. /* get background pixel */
  532. hw_driver->ops->get_pixel(&bc, x, y);
  533. /* alpha blending */
  534. fc = RTGUI_RGB(blending(RTGUI_RGB_R(bc), RTGUI_RGB_R(*pixel), alpha),
  535. blending(RTGUI_RGB_G(bc), RTGUI_RGB_G(*pixel), alpha),
  536. blending(RTGUI_RGB_B(bc), RTGUI_RGB_B(*pixel), alpha));
  537. hw_driver->ops->set_pixel(&fc, x, y);
  538. }
  539. }
  540. }
  541. }
  542. }
  543. else
  544. {
  545. int dst_x, dst_y;
  546. info.a = 0;
  547. /* initialize source blit information */
  548. info.src_fmt = RTGRAPHIC_PIXEL_FORMAT_ARGB888;;
  549. info.src_h = h;
  550. info.src_w = w;
  551. info.src_pitch = image->w * rtgui_color_get_bpp(RTGRAPHIC_PIXEL_FORMAT_ARGB888);
  552. info.src_skip = info.src_pitch - w * rtgui_color_get_bpp(RTGRAPHIC_PIXEL_FORMAT_ARGB888);
  553. info.src = (rt_uint8_t *)image->data + y * info.src_pitch + x * rtgui_color_get_bpp(RTGRAPHIC_PIXEL_FORMAT_ARGB888);
  554. if (rect->x1 < 0) dst_x = 0;
  555. else dst_x = rect->x1;
  556. if (rect->y1 < 0) dst_y = 0;
  557. else dst_y = rect->y1;
  558. /* initialize destination blit information */
  559. if (dc->type == RTGUI_DC_BUFFER)
  560. {
  561. struct rtgui_dc_buffer *buffer;
  562. buffer = (struct rtgui_dc_buffer*)dc;
  563. info.dst = rtgui_dc_buffer_get_pixel(RTGUI_DC(buffer)) + dst_y * buffer->pitch +
  564. dst_x * rtgui_color_get_bpp(buffer->pixel_format);
  565. info.dst_h = h;
  566. info.dst_w = w;
  567. info.dst_fmt = buffer->pixel_format;
  568. info.dst_pitch = buffer->pitch;
  569. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(buffer->pixel_format);
  570. }
  571. else if (dc->type == RTGUI_DC_HW)
  572. {
  573. struct rtgui_widget *owner;
  574. struct rtgui_rect r;
  575. owner = ((struct rtgui_dc_hw*)dc)->owner;
  576. rtgui_graphic_driver_get_rect(RT_NULL, &r);
  577. /* blit destination */
  578. info.dst = (rt_uint8_t*)hw_driver->framebuffer;
  579. info.dst = info.dst + (owner->extent.y1 + dst_y) * hw_driver->pitch +
  580. (owner->extent.x1 + dst_x) * rtgui_color_get_bpp(hw_driver->pixel_format);
  581. info.dst_fmt = hw_driver->pixel_format;
  582. info.dst_h = h;
  583. info.dst_w = w;
  584. info.dst_pitch = hw_driver->pitch;
  585. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(hw_driver->pixel_format);
  586. }
  587. rtgui_blit(&info);
  588. }
  589. }
  590. void rtgui_image_png_init()
  591. {
  592. /* register png on image system */
  593. rtgui_image_register_engine(&rtgui_image_png_engine);
  594. }
  595. #endif