image_png.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. #include <rtgui/image.h>
  29. #ifdef RTGUI_IMAGE_PNG
  30. #include "png.h"
  31. #define PNG_MAGIC_LEN 8
  32. struct rtgui_image_png
  33. {
  34. rt_bool_t is_loaded;
  35. struct rtgui_filerw *filerw;
  36. /* png image information */
  37. png_structp png_ptr;
  38. png_infop info_ptr;
  39. rt_uint8_t *pixels;
  40. };
  41. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file);
  42. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  43. static void rtgui_image_png_unload(struct rtgui_image *image);
  44. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  45. struct rtgui_image_engine rtgui_image_png_engine =
  46. {
  47. "png",
  48. { RT_NULL },
  49. rtgui_image_png_check,
  50. rtgui_image_png_load,
  51. rtgui_image_png_unload,
  52. rtgui_image_png_blit,
  53. };
  54. static void rtgui_image_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  55. {
  56. struct rtgui_filerw *filerw = (struct rtgui_filerw *)png_ptr->io_ptr;
  57. rtgui_filerw_read(filerw, data, length, 1);
  58. }
  59. static rt_bool_t rtgui_image_png_process(png_structp png_ptr, png_infop info_ptr, struct rtgui_image_png *png)
  60. {
  61. rt_uint32_t x, y;
  62. png_bytep row;
  63. png_bytep data;
  64. rtgui_color_t *ptr;
  65. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png_ptr, info_ptr));
  66. if (row == RT_NULL) return RT_FALSE;
  67. ptr = (rtgui_color_t *)png->pixels;
  68. switch (info_ptr->color_type)
  69. {
  70. case PNG_COLOR_TYPE_RGB:
  71. for (y = 0; y < info_ptr->height; y++)
  72. {
  73. png_read_row(png_ptr, row, png_bytep_NULL);
  74. for (x = 0; x < info_ptr->width; x++)
  75. {
  76. data = &(row[x * 3]);
  77. ptr[x + y * info_ptr->width] = RTGUI_RGB(data[0], data[1], data[2]);
  78. }
  79. }
  80. break;
  81. case PNG_COLOR_TYPE_RGBA:
  82. for (y = 0; y < info_ptr->height; y++)
  83. {
  84. png_read_row(png_ptr, row, png_bytep_NULL);
  85. for (x = 0; x < info_ptr->width; x++)
  86. {
  87. data = &(row[x * 4]);
  88. ptr[x + y * info_ptr->width] = RTGUI_ARGB(data[3], data[0], data[1], data[2]);
  89. }
  90. }
  91. break;
  92. case PNG_COLOR_TYPE_PALETTE:
  93. for (y = 0; y < info_ptr->height; y++)
  94. {
  95. png_read_row(png_ptr, row, png_bytep_NULL);
  96. for (x = 0; x < info_ptr->width; x++)
  97. {
  98. data = &(row[x]);
  99. ptr[x] = RTGUI_ARGB(0, info_ptr->palette[data[0]].red,
  100. info_ptr->palette[data[0]].green,
  101. info_ptr->palette[data[0]].blue);
  102. }
  103. }
  104. default:
  105. break;
  106. };
  107. rtgui_free(row);
  108. return RT_TRUE;
  109. }
  110. static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file)
  111. {
  112. int start;
  113. rt_bool_t is_PNG;
  114. rt_uint8_t magic[4];
  115. if (!file) return 0;
  116. start = rtgui_filerw_tell(file);
  117. /* move to the begining of file */
  118. rtgui_filerw_seek(file, 0, SEEK_SET);
  119. is_PNG = RT_FALSE;
  120. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  121. {
  122. if (magic[0] == 0x89 &&
  123. magic[1] == 'P' &&
  124. magic[2] == 'N' &&
  125. magic[3] == 'G')
  126. {
  127. is_PNG = RT_TRUE;
  128. }
  129. }
  130. rtgui_filerw_seek(file, start, SEEK_SET);
  131. return(is_PNG);
  132. }
  133. static void _image_png_error_fn(png_structp png_ptr, png_const_charp error_message)
  134. {
  135. rt_kprintf(error_message);
  136. }
  137. static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  138. {
  139. png_uint_32 width;
  140. png_uint_32 height;
  141. int bit_depth;
  142. int color_type;
  143. double gamma;
  144. struct rtgui_image_png *png;
  145. png = (struct rtgui_image_png *) rtgui_malloc(sizeof(struct rtgui_image_png));
  146. png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  147. if (png->png_ptr == RT_NULL)
  148. {
  149. rtgui_free(png);
  150. return RT_FALSE;
  151. }
  152. png_set_error_fn(png->png_ptr, RT_NULL, _image_png_error_fn, _image_png_error_fn);
  153. png->info_ptr = png_create_info_struct(png->png_ptr);
  154. if (png->info_ptr == RT_NULL)
  155. {
  156. png_destroy_read_struct(&png->png_ptr, NULL, NULL);
  157. rtgui_free(png);
  158. return RT_FALSE;
  159. }
  160. png->filerw = file;
  161. png->is_loaded = RT_FALSE;
  162. png_set_read_fn(png->png_ptr, png->filerw, rtgui_image_png_read_data);
  163. png_read_info(png->png_ptr, png->info_ptr);
  164. png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth,
  165. &color_type, NULL, NULL, NULL);
  166. /* set image information */
  167. image->w = width;
  168. image->h = height;
  169. image->engine = &rtgui_image_png_engine;
  170. image->data = png;
  171. if (bit_depth == 16)
  172. png_set_strip_16(png->png_ptr);
  173. if (color_type == PNG_COLOR_TYPE_PALETTE)
  174. png_set_expand(png->png_ptr);
  175. if (bit_depth < 8)
  176. png_set_expand(png->png_ptr);
  177. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  178. png_set_expand(png->png_ptr);
  179. if (color_type == PNG_COLOR_TYPE_GRAY ||
  180. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  181. png_set_gray_to_rgb(png->png_ptr);
  182. /* Ignore background color */
  183. /* set gamma conversion */
  184. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  185. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  186. png_read_update_info(png->png_ptr, png->info_ptr);
  187. if (load == RT_TRUE)
  188. {
  189. /* load all pixels */
  190. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  191. if (png->pixels == RT_NULL)
  192. {
  193. png_read_end(png->png_ptr, RT_NULL);
  194. /* destroy png struct */
  195. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  196. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  197. /* release data */
  198. rtgui_free(png);
  199. return RT_FALSE;
  200. }
  201. rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
  202. png_read_end(png->png_ptr, RT_NULL);
  203. png->is_loaded = RT_TRUE;
  204. /* close file handler */
  205. rtgui_filerw_close(png->filerw);
  206. png->filerw = RT_NULL;
  207. }
  208. else
  209. {
  210. png->pixels = RT_NULL;
  211. }
  212. return RT_TRUE;
  213. }
  214. static void rtgui_image_png_unload(struct rtgui_image *image)
  215. {
  216. struct rtgui_image_png *png;
  217. if (image != RT_NULL)
  218. {
  219. png = (struct rtgui_image_png *) image->data;
  220. /* destroy png struct */
  221. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  222. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  223. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  224. /* release data */
  225. rtgui_free(png);
  226. }
  227. }
  228. static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  229. {
  230. rt_uint16_t x, y, w, h;
  231. rtgui_color_t *ptr;
  232. struct rtgui_image_png *png;
  233. int fg_maxsample;
  234. int ialpha;
  235. float alpha;
  236. rtgui_color_t color;
  237. rtgui_color_t c, bgcolor;
  238. int fc[3], bc[3];
  239. struct rtgui_graphic_driver *hwdev = rtgui_graphic_get_device();
  240. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  241. RT_ASSERT(image->data != RT_NULL);
  242. png = (struct rtgui_image_png *) image->data;
  243. w = _UI_MIN(image->w, rtgui_rect_width(*rect));
  244. h = _UI_MIN(image->h, rtgui_rect_height(*rect));
  245. fg_maxsample = (1 << png->info_ptr->bit_depth) - 1;
  246. if (png->pixels != RT_NULL)
  247. {
  248. ptr = (rtgui_color_t *)png->pixels;
  249. bgcolor = RTGUI_DC_BC(dc);
  250. bc[0] = RTGUI_RGB_R(bgcolor);
  251. bc[1] = RTGUI_RGB_G(bgcolor);
  252. bc[2] = RTGUI_RGB_B(bgcolor);
  253. /* draw each point within dc */
  254. for (y = 0; y < h; y ++)
  255. {
  256. for (x = 0; x < w; x++)
  257. {
  258. c = *ptr;
  259. ialpha = RTGUI_RGB_A(c);
  260. if (ialpha == 0)
  261. {
  262. /*
  263. * Foreground image is transparent hear.
  264. * If the background image is already in the frame
  265. * buffer, there is nothing to do.
  266. */
  267. }
  268. else if (ialpha == fg_maxsample)
  269. {
  270. /*
  271. * Copy foreground pixel to frame buffer.
  272. */
  273. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, c);
  274. }
  275. else
  276. {
  277. /* output = alpha * foreground + (1-alpha) * background */
  278. /*
  279. * Compositing is necessary.
  280. * Get floating-point alpha and its complement.
  281. * Note: alpha is always linear: gamma does not
  282. * affect it.
  283. */
  284. fc[0] = RTGUI_RGB_R(c);
  285. fc[1] = RTGUI_RGB_G(c);
  286. fc[2] = RTGUI_RGB_B(c);
  287. alpha = (float) ialpha / fg_maxsample;
  288. color = RTGUI_RGB((rt_uint8_t)(fc[0] * alpha + bc[0] * (1 - alpha)),
  289. (rt_uint8_t)(fc[1] * alpha + bc[1] * (1 - alpha)),
  290. (rt_uint8_t)(fc[2] * alpha + bc[2] * (1 - alpha)));
  291. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, color);
  292. }
  293. /* move to next color buffer */
  294. ptr ++;
  295. }
  296. }
  297. }
  298. else
  299. {
  300. png_bytep row;
  301. png_bytep data;
  302. row = (png_bytep) rtgui_malloc(png_get_rowbytes(png->png_ptr, png->info_ptr));
  303. if (row == RT_NULL) return ;
  304. switch (png->info_ptr->color_type)
  305. {
  306. case PNG_COLOR_TYPE_RGB:
  307. for (y = 0; y < h; y++)
  308. {
  309. png_read_row(png->png_ptr, row, png_bytep_NULL);
  310. for (x = 0; x < w; x++)
  311. {
  312. data = &(row[x * 3]);
  313. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  314. RTGUI_RGB(data[0], data[1], data[2]));
  315. }
  316. }
  317. break;
  318. case PNG_COLOR_TYPE_RGBA:
  319. for (y = 0; y < h; y++)
  320. {
  321. png_read_row(png->png_ptr, row, png_bytep_NULL);
  322. for (x = 0; x < w; x++)
  323. {
  324. data = &(row[x * 4]);
  325. if (data[3] != 0)
  326. {
  327. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  328. RTGUI_ARGB(data[3], data[0], data[1], data[2]));
  329. }
  330. }
  331. }
  332. break;
  333. case PNG_COLOR_TYPE_PALETTE:
  334. for (y = 0; y < h; y++)
  335. {
  336. png_read_row(png->png_ptr, row, png_bytep_NULL);
  337. for (x = 0; x < w; x++)
  338. {
  339. data = &(row[x]);
  340. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1,
  341. RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  342. png->info_ptr->palette[data[0]].green,
  343. png->info_ptr->palette[data[0]].blue));
  344. }
  345. }
  346. default:
  347. break;
  348. };
  349. rtgui_free(row);
  350. }
  351. }
  352. void rtgui_image_png_init()
  353. {
  354. /* register png on image system */
  355. rtgui_image_register_engine(&rtgui_image_png_engine);
  356. }
  357. #elif defined(RTGUI_IMAGE_LODEPNG)
  358. #include "lodepng.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 = {0};
  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 = 255;
  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. if (buffer->pixel_alpha == 0)
  564. info.a = 0;
  565. info.dst = rtgui_dc_buffer_get_pixel(RTGUI_DC(buffer)) + dst_y * buffer->pitch +
  566. dst_x * rtgui_color_get_bpp(buffer->pixel_format);
  567. info.dst_h = h;
  568. info.dst_w = w;
  569. info.dst_fmt = buffer->pixel_format;
  570. info.dst_pitch = buffer->pitch;
  571. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(buffer->pixel_format);
  572. }
  573. else if (dc->type == RTGUI_DC_HW)
  574. {
  575. struct rtgui_widget *owner;
  576. struct rtgui_rect r;
  577. owner = ((struct rtgui_dc_hw*)dc)->owner;
  578. rtgui_graphic_driver_get_rect(RT_NULL, &r);
  579. /* blit destination */
  580. info.dst = (rt_uint8_t*)hw_driver->framebuffer;
  581. info.dst = info.dst + (owner->extent.y1 + dst_y) * hw_driver->pitch +
  582. (owner->extent.x1 + dst_x) * rtgui_color_get_bpp(hw_driver->pixel_format);
  583. info.dst_fmt = hw_driver->pixel_format;
  584. info.dst_h = h;
  585. info.dst_w = w;
  586. info.dst_pitch = hw_driver->pitch;
  587. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(hw_driver->pixel_format);
  588. }
  589. rtgui_blit(&info);
  590. }
  591. }
  592. void rtgui_image_png_init()
  593. {
  594. /* register png on image system */
  595. rtgui_image_register_engine(&rtgui_image_png_engine);
  596. }
  597. #endif