image_hdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * File : image_hdc.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/dc.h>
  26. #include <rtgui/image.h>
  27. #include <rtgui/rtgui_system.h>
  28. #include <rtgui/image_hdc.h>
  29. #include <rtgui/blit.h>
  30. #define HDC_MAGIC_LEN 4
  31. extern int fastlz_decompress(const void *input, int length, void *output, int maxout);
  32. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file);
  33. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  34. static void rtgui_image_hdc_unload(struct rtgui_image *image);
  35. static void rtgui_image_hdc_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  36. static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect);
  37. struct rtgui_image_engine rtgui_image_hdc_engine =
  38. {
  39. "hdc",
  40. { RT_NULL },
  41. rtgui_image_hdc_check,
  42. rtgui_image_hdc_load,
  43. rtgui_image_hdc_unload,
  44. rtgui_image_hdc_blit,
  45. };
  46. const struct rtgui_image_engine rtgui_image_hdcmm_engine =
  47. {
  48. "hdcmm",
  49. {RT_NULL},
  50. RT_NULL,
  51. RT_NULL,
  52. RT_NULL,
  53. rtgui_image_hdcmm_blit,
  54. };
  55. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file)
  56. {
  57. int start;
  58. rt_bool_t is_HDC;
  59. rt_uint8_t magic[4];
  60. if (!file) return 0;
  61. start = rtgui_filerw_tell(file);
  62. /* move to the beginning of file */
  63. rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
  64. is_HDC = RT_FALSE;
  65. if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic))
  66. {
  67. if (magic[0] == 'H' &&
  68. magic[1] == 'D' &&
  69. magic[2] == 'C' &&
  70. magic[3] == '\0')
  71. {
  72. is_HDC = RT_TRUE;
  73. }
  74. }
  75. rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
  76. return (is_HDC);
  77. }
  78. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  79. {
  80. rt_uint32_t header[5];
  81. struct rtgui_image_hdc *hdc;
  82. hdc = (struct rtgui_image_hdc *) rtgui_malloc(sizeof(struct rtgui_image_hdc));
  83. if (hdc == RT_NULL) return RT_FALSE;
  84. rtgui_filerw_read(file, (char *)&header, 1, sizeof(header));
  85. /* set image information */
  86. image->w = (rt_uint16_t)header[1];
  87. image->h = (rt_uint16_t)header[2];
  88. image->engine = &rtgui_image_hdc_engine;
  89. image->data = hdc;
  90. hdc->pixel_format = header[3];
  91. hdc->filerw = file;
  92. if (header[3] == 0)
  93. {
  94. /* 0.x version */
  95. hdc->pixel_format = rtgui_graphic_driver_get_default()->pixel_format;
  96. }
  97. else if (header[3] == 1)
  98. {
  99. /* 1.x version */
  100. hdc->pixel_format = header[4];
  101. }
  102. else if (header[3] == 2)
  103. {
  104. /* 2.x version */
  105. hdc->pixel_format = header[4];
  106. }
  107. hdc->byte_per_pixel = rtgui_color_get_bpp(hdc->pixel_format);
  108. hdc->pitch = image->w * hdc->byte_per_pixel;
  109. hdc->pixel_offset = rtgui_filerw_tell(file);
  110. if (load == RT_TRUE)
  111. {
  112. if (header[3] == 2)
  113. {
  114. #ifdef PKG_USING_FASTLZ
  115. /* TODO: add HDC with fastlz compressed */
  116. int data_length, dec_length;
  117. rt_uint8_t *data;
  118. data_length = rtgui_filerw_seek(hdc->filerw, 0, SEEK_END) - sizeof(header);
  119. rtgui_filerw_seek(hdc->filerw, sizeof(header), SEEK_SET);
  120. data = (rt_uint8_t *)rtgui_malloc(data_length);
  121. if (data == RT_NULL)
  122. {
  123. /* release data */
  124. rtgui_free(hdc);
  125. return RT_FALSE;
  126. }
  127. if (rtgui_filerw_read(hdc->filerw, data, 1, data_length) != data_length)
  128. {
  129. rtgui_filerw_close(hdc->filerw);
  130. /* release data */
  131. rtgui_free(hdc);
  132. return RT_FALSE;
  133. }
  134. rtgui_filerw_close(hdc->filerw);
  135. hdc->filerw = RT_NULL;
  136. hdc->pixels = (rt_uint8_t *)rtgui_malloc(image->h * hdc->pitch);
  137. if (hdc->pixels == RT_NULL)
  138. {
  139. /* release data */
  140. rtgui_free(hdc);
  141. rtgui_free(data);
  142. return RT_FALSE;
  143. }
  144. dec_length = fastlz_decompress(data, data_length, hdc->pixels, image->h * hdc->pitch);
  145. if (dec_length != image->h * hdc->pitch)
  146. {
  147. /* release data */
  148. rtgui_free(hdc->pixels);
  149. rtgui_free(hdc);
  150. rtgui_free(data);
  151. return RT_FALSE;
  152. }
  153. hdc->pixel_offset = 0;
  154. rtgui_free(data);
  155. #else
  156. hdc->pixels = RT_NULL;
  157. return RT_FALSE;
  158. #endif
  159. }
  160. else
  161. {
  162. /* load all pixels */
  163. hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
  164. if (hdc->pixels == RT_NULL)
  165. {
  166. /* release data */
  167. rtgui_free(hdc);
  168. return RT_FALSE;
  169. }
  170. rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
  171. rtgui_filerw_close(hdc->filerw);
  172. hdc->filerw = RT_NULL;
  173. hdc->pixel_offset = 0;
  174. }
  175. }
  176. else
  177. {
  178. hdc->pixels = RT_NULL;
  179. }
  180. return RT_TRUE;
  181. }
  182. static void rtgui_image_hdc_unload(struct rtgui_image *image)
  183. {
  184. struct rtgui_image_hdc *hdc;
  185. if (image != RT_NULL)
  186. {
  187. hdc = (struct rtgui_image_hdc *) image->data;
  188. if (hdc->pixels != RT_NULL)
  189. rtgui_free(hdc->pixels);
  190. if (hdc->filerw != RT_NULL)
  191. rtgui_filerw_close(hdc->filerw);
  192. /* release data */
  193. rtgui_free(hdc);
  194. }
  195. }
  196. static void rtgui_image_hdc_blit(struct rtgui_image *image,
  197. struct rtgui_dc *dc,
  198. struct rtgui_rect *dst_rect)
  199. {
  200. rt_int16_t y, w, h, xoff, yoff;
  201. struct rtgui_image_hdc *hdc;
  202. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
  203. /* this dc is not visible */
  204. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  205. return;
  206. if (!image)
  207. return;
  208. hdc = (struct rtgui_image_hdc *) image->data;
  209. RT_ASSERT(hdc != RT_NULL);
  210. xoff = 0;
  211. if (dst_rect->x1 < 0)
  212. {
  213. xoff = -dst_rect->x1;
  214. dst_rect->x1 = 0;
  215. }
  216. yoff = 0;
  217. if (dst_rect->y1 < 0)
  218. {
  219. yoff = -dst_rect->y1;
  220. dst_rect->y1 = 0;
  221. }
  222. if (dst_rect->x2 <= 0 || dst_rect->y2 <= 0)
  223. return;
  224. if (xoff >= image->w || yoff >= image->h)
  225. return;
  226. /* the minimum rect */
  227. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  228. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  229. if (w == 0 || h == 0)
  230. return;
  231. if (hdc->pixels != RT_NULL)
  232. {
  233. struct rtgui_image_info info;
  234. struct rtgui_rect dest = *dst_rect;
  235. info.a = 255;
  236. info.pixels = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  237. info.src_fmt = hdc->pixel_format;
  238. info.src_pitch = hdc->pitch;
  239. dest.x2 = dst_rect->x1 + w;
  240. dest.y2 = dst_rect->y1 + h;
  241. rtgui_image_info_blit(&info, dc, &dest);
  242. }
  243. else
  244. {
  245. rt_uint8_t *ptr;
  246. ptr = rtgui_malloc(hdc->byte_per_pixel * w);
  247. if (ptr == RT_NULL)
  248. return; /* no memory */
  249. /* seek to the begin of pixel data */
  250. rtgui_filerw_seek(hdc->filerw,
  251. hdc->pixel_offset + hdc->pitch * yoff + hdc->byte_per_pixel * xoff,
  252. RTGUI_FILE_SEEK_SET);
  253. for (y = 0; y < h; y ++)
  254. {
  255. /* read pixel data */
  256. if (rtgui_filerw_read(hdc->filerw, ptr, 1,
  257. hdc->byte_per_pixel * w) != hdc->byte_per_pixel * w)
  258. break; /* read data failed */
  259. dc->engine->blit_line(dc,
  260. dst_rect->x1,
  261. dst_rect->x1 + w,
  262. dst_rect->y1 + y,
  263. ptr);
  264. rtgui_filerw_seek(hdc->filerw, hdc->byte_per_pixel * xoff, RTGUI_FILE_SEEK_CUR);
  265. }
  266. rtgui_free(ptr);
  267. }
  268. }
  269. static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  270. {
  271. rt_uint8_t *ptr;
  272. rt_uint16_t y, w, h, xoff, yoff;
  273. struct rtgui_image_hdcmm *hdc;
  274. RT_ASSERT(image != RT_NULL && dc != RT_NULL && dst_rect != RT_NULL);
  275. /* this dc is not visible */
  276. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  277. return;
  278. hdc = (struct rtgui_image_hdcmm *) image;
  279. if (!hdc->pixels)
  280. return;
  281. xoff = 0;
  282. if (dst_rect->x1 < 0)
  283. {
  284. xoff = -dst_rect->x1;
  285. dst_rect->x1 = 0;
  286. }
  287. yoff = 0;
  288. if (dst_rect->y1 < 0)
  289. {
  290. yoff = -dst_rect->y1;
  291. dst_rect->y1 = 0;
  292. }
  293. if (xoff >= image->w || yoff >= image->h)
  294. return;
  295. /* the minimum rect */
  296. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  297. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  298. /* get pixel pointer */
  299. ptr = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  300. for (y = 0; y < h; y++)
  301. {
  302. dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
  303. ptr += hdc->pitch;
  304. }
  305. }
  306. void rtgui_image_hdc_init()
  307. {
  308. /* register hdc on image system */
  309. rtgui_image_register_engine(&rtgui_image_hdc_engine);
  310. }