image_hdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #if 0 /* TODO: add HDC with fastlz compressed */
  115. int data_length, dec_length;
  116. rt_uint8_t *data;
  117. data_length = rtgui_filerw_seek(hdc->filerw, 0, SEEK_END) - sizeof(header);
  118. rtgui_filerw_seek(hdc->filerw, sizeof(header), SEEK_SET);
  119. data = (rt_uint8_t *)rtgui_malloc(data_length);
  120. if (data == RT_NULL)
  121. {
  122. /* release data */
  123. rtgui_free(hdc);
  124. return RT_FALSE;
  125. }
  126. if (rtgui_filerw_read(hdc->filerw, data, 1, data_length) != data_length)
  127. {
  128. rtgui_filerw_close(hdc->filerw);
  129. /* release data */
  130. rtgui_free(hdc);
  131. return RT_FALSE;
  132. }
  133. rtgui_filerw_close(hdc->filerw);
  134. hdc->filerw = RT_NULL;
  135. hdc->pixels = (rt_uint8_t *)rtgui_malloc(image->h * hdc->pitch);
  136. if (hdc->pixels == RT_NULL)
  137. {
  138. /* release data */
  139. rtgui_free(hdc);
  140. rtgui_free(data);
  141. return RT_FALSE;
  142. }
  143. dec_length = fastlz_decompress(data, data_length, hdc->pixels, image->h * hdc->pitch);
  144. if (dec_length != image->h * hdc->pitch)
  145. {
  146. /* release data */
  147. rtgui_free(hdc->pixels);
  148. rtgui_free(hdc);
  149. rtgui_free(data);
  150. return RT_FALSE;
  151. }
  152. hdc->pixel_offset = 0;
  153. rtgui_free(data);
  154. #endif
  155. }
  156. else
  157. {
  158. /* load all pixels */
  159. hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
  160. if (hdc->pixels == RT_NULL)
  161. {
  162. /* release data */
  163. rtgui_free(hdc);
  164. return RT_FALSE;
  165. }
  166. rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
  167. rtgui_filerw_close(hdc->filerw);
  168. hdc->filerw = RT_NULL;
  169. hdc->pixel_offset = 0;
  170. }
  171. }
  172. else
  173. {
  174. hdc->pixels = RT_NULL;
  175. }
  176. return RT_TRUE;
  177. }
  178. static void rtgui_image_hdc_unload(struct rtgui_image *image)
  179. {
  180. struct rtgui_image_hdc *hdc;
  181. if (image != RT_NULL)
  182. {
  183. hdc = (struct rtgui_image_hdc *) image->data;
  184. if (hdc->pixels != RT_NULL)
  185. rtgui_free(hdc->pixels);
  186. if (hdc->filerw != RT_NULL)
  187. rtgui_filerw_close(hdc->filerw);
  188. /* release data */
  189. rtgui_free(hdc);
  190. }
  191. }
  192. static void rtgui_image_hdc_blit(struct rtgui_image *image,
  193. struct rtgui_dc *dc,
  194. struct rtgui_rect *dst_rect)
  195. {
  196. rt_int16_t y, w, h, xoff, yoff;
  197. struct rtgui_image_hdc *hdc;
  198. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
  199. /* this dc is not visible */
  200. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  201. return;
  202. if (!image)
  203. return;
  204. hdc = (struct rtgui_image_hdc *) image->data;
  205. RT_ASSERT(hdc != RT_NULL);
  206. xoff = 0;
  207. if (dst_rect->x1 < 0)
  208. {
  209. xoff = -dst_rect->x1;
  210. dst_rect->x1 = 0;
  211. }
  212. yoff = 0;
  213. if (dst_rect->y1 < 0)
  214. {
  215. yoff = -dst_rect->y1;
  216. dst_rect->y1 = 0;
  217. }
  218. if (dst_rect->x2 <= 0 || dst_rect->y2 <= 0)
  219. return;
  220. if (xoff >= image->w || yoff >= image->h)
  221. return;
  222. /* the minimum rect */
  223. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  224. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  225. if (w == 0 || h == 0)
  226. return;
  227. if (hdc->pixels != RT_NULL)
  228. {
  229. struct rtgui_image_info info;
  230. struct rtgui_rect dest = *dst_rect;
  231. info.a = 255;
  232. info.pixels = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  233. info.src_fmt = hdc->pixel_format;
  234. info.src_pitch = hdc->pitch;
  235. dest.x2 = dst_rect->x1 + w;
  236. dest.y2 = dst_rect->y1 + h;
  237. rtgui_image_info_blit(&info, dc, &dest);
  238. }
  239. else
  240. {
  241. rt_uint8_t *ptr;
  242. ptr = rtgui_malloc(hdc->byte_per_pixel * w);
  243. if (ptr == RT_NULL)
  244. return; /* no memory */
  245. /* seek to the begin of pixel data */
  246. rtgui_filerw_seek(hdc->filerw,
  247. hdc->pixel_offset + hdc->pitch * yoff + hdc->byte_per_pixel * xoff,
  248. RTGUI_FILE_SEEK_SET);
  249. for (y = 0; y < h; y ++)
  250. {
  251. /* read pixel data */
  252. if (rtgui_filerw_read(hdc->filerw, ptr, 1,
  253. hdc->byte_per_pixel * w) != hdc->byte_per_pixel * w)
  254. break; /* read data failed */
  255. dc->engine->blit_line(dc,
  256. dst_rect->x1,
  257. dst_rect->x1 + w,
  258. dst_rect->y1 + y,
  259. ptr);
  260. rtgui_filerw_seek(hdc->filerw, hdc->byte_per_pixel * xoff, RTGUI_FILE_SEEK_CUR);
  261. }
  262. rtgui_free(ptr);
  263. }
  264. }
  265. static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  266. {
  267. rt_uint8_t *ptr;
  268. rt_uint16_t y, w, h, xoff, yoff;
  269. struct rtgui_image_hdcmm *hdc;
  270. RT_ASSERT(image != RT_NULL && dc != RT_NULL && dst_rect != RT_NULL);
  271. /* this dc is not visible */
  272. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  273. return;
  274. hdc = (struct rtgui_image_hdcmm *) image;
  275. if (!hdc->pixels)
  276. return;
  277. xoff = 0;
  278. if (dst_rect->x1 < 0)
  279. {
  280. xoff = -dst_rect->x1;
  281. dst_rect->x1 = 0;
  282. }
  283. yoff = 0;
  284. if (dst_rect->y1 < 0)
  285. {
  286. yoff = -dst_rect->y1;
  287. dst_rect->y1 = 0;
  288. }
  289. if (xoff >= image->w || yoff >= image->h)
  290. return;
  291. /* the minimum rect */
  292. w = _UI_MIN(image->w - xoff, rtgui_rect_width(*dst_rect));
  293. h = _UI_MIN(image->h - yoff, rtgui_rect_height(*dst_rect));
  294. /* get pixel pointer */
  295. ptr = hdc->pixels + hdc->pitch * yoff + hdc->byte_per_pixel * xoff;
  296. for (y = 0; y < h; y++)
  297. {
  298. dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
  299. ptr += hdc->pitch;
  300. }
  301. }
  302. void rtgui_image_hdc_init()
  303. {
  304. /* register hdc on image system */
  305. rtgui_image_register_engine(&rtgui_image_hdc_engine);
  306. }