image_png.c 19 KB

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