image_hdc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "libpng/png.h"
  2. #include <rtthread.h>
  3. #include <rtgui/image_hdc.h>
  4. #include <rtgui/rtgui_system.h>
  5. #define HDC_MAGIC_LEN 4
  6. struct rtgui_image_hdc
  7. {
  8. rt_bool_t is_loaded;
  9. struct rtgui_filerw* filerw;
  10. /* hdc image information */
  11. rt_uint32_t width, height;
  12. rt_uint32_t pitch;
  13. rt_uint8_t *pixels;
  14. };
  15. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file);
  16. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load);
  17. static void rtgui_image_hdc_unload(struct rtgui_image* image);
  18. static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect);
  19. struct rtgui_image_engine rtgui_image_hdc_engine =
  20. {
  21. "hdc",
  22. { RT_NULL },
  23. rtgui_image_hdc_check,
  24. rtgui_image_hdc_load,
  25. rtgui_image_hdc_unload,
  26. rtgui_image_hdc_blit
  27. };
  28. static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file)
  29. {
  30. int start;
  31. rt_bool_t is_HDC;
  32. rt_uint8_t magic[4];
  33. if ( !file ) return 0;
  34. start = rtgui_filerw_tell(file);
  35. /* move to the begining of file */
  36. rtgui_filerw_seek(file, 0, SEEK_SET);
  37. is_HDC = RT_FALSE;
  38. if ( rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic) )
  39. {
  40. if ( magic[0] == 'H' &&
  41. magic[1] == 'D' &&
  42. magic[2] == 'C' &&
  43. magic[3] == '\0' )
  44. {
  45. is_HDC = RT_TRUE;
  46. }
  47. }
  48. rtgui_filerw_seek(file, start, SEEK_SET);
  49. return(is_HDC);
  50. }
  51. static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load)
  52. {
  53. rt_uint32_t header[5];
  54. struct rtgui_image_hdc* hdc;
  55. hdc = (struct rtgui_image_hdc*) rtgui_malloc(sizeof(struct rtgui_image_hdc));
  56. if (hdc == RT_NULL) return RT_FALSE;
  57. rtgui_filerw_read(file, (char*)&header, 1, sizeof(header));
  58. /* set image information */
  59. image->w = header[1]; image->h = header[2];
  60. image->engine = &rtgui_image_hdc_engine;
  61. image->data = png;
  62. if (bit_depth == 16)
  63. png_set_strip_16(png->png_ptr);
  64. if (color_type == PNG_COLOR_TYPE_PALETTE)
  65. png_set_expand(png->png_ptr);
  66. if (bit_depth < 8)
  67. png_set_expand(png->png_ptr);
  68. if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
  69. png_set_expand(png->png_ptr);
  70. if (color_type == PNG_COLOR_TYPE_GRAY ||
  71. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  72. png_set_gray_to_rgb(png->png_ptr);
  73. /* Ignore background color */
  74. /* set gamma conversion */
  75. if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
  76. png_set_gamma(png->png_ptr, (double)2.2, gamma);
  77. png_read_update_info(png->png_ptr, png->info_ptr);
  78. if (load == RT_TRUE)
  79. {
  80. /* load all pixels */
  81. png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
  82. if (png->pixels == RT_NULL)
  83. {
  84. png_read_end(png->png_ptr, RT_NULL);
  85. /* destroy png struct */
  86. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  87. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  88. /* release data */
  89. rtgui_free(png);
  90. return RT_FALSE;
  91. }
  92. rtgui_image_hdc_process(png->png_ptr, png->info_ptr, png);
  93. }
  94. else
  95. {
  96. png->pixels = RT_NULL;
  97. }
  98. return RT_TRUE;
  99. }
  100. static void rtgui_image_hdc_unload(struct rtgui_image* image)
  101. {
  102. struct rtgui_image_hdc* png;
  103. if (image != RT_NULL)
  104. {
  105. png = (struct rtgui_image_hdc*) image->data;
  106. png_read_end(png->png_ptr, RT_NULL);
  107. /* destroy png struct */
  108. png_destroy_info_struct(png->png_ptr, &png->info_ptr);
  109. png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
  110. if (png->pixels != RT_NULL) rtgui_free(png->pixels);
  111. /* release data */
  112. rtgui_free(png);
  113. }
  114. }
  115. static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
  116. {
  117. rt_uint16_t x, y, w, h;
  118. rtgui_color_t* ptr;
  119. rtgui_color_t foreground;
  120. struct rtgui_image_hdc* png;
  121. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  122. RT_ASSERT(image->data != RT_NULL);
  123. png = (struct rtgui_image_hdc*) image->data;
  124. if (image->w < rtgui_rect_width(*rect)) w = image->w;
  125. else w = rtgui_rect_width(*rect);
  126. if (image->h < rtgui_rect_height(*rect)) h = image->h;
  127. else h = rtgui_rect_height(*rect);
  128. /* save foreground color */
  129. foreground = rtgui_dc_get_color(dc);
  130. if (png->pixels != RT_NULL)
  131. {
  132. ptr = (rtgui_color_t*)png->pixels;
  133. /* draw each point within dc */
  134. for (y = 0; y < h; y ++)
  135. {
  136. for (x = 0; x < w; x++)
  137. {
  138. /* not alpha */
  139. if ((*ptr >> 24) != 255)
  140. {
  141. rtgui_dc_set_color(dc, *ptr);
  142. rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1);
  143. }
  144. /* move to next color buffer */
  145. ptr ++;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. png_bytep row;
  152. png_bytep data;
  153. row = (png_bytep) rtgui_malloc (png_get_rowbytes(png->png_ptr, png->info_ptr));
  154. if (row == RT_NULL) return ;
  155. switch (png->info_ptr->color_type)
  156. {
  157. case PNG_COLOR_TYPE_RGBA:
  158. for (y = 0; y < h; y++)
  159. {
  160. png_read_row(png->png_ptr, row, png_bytep_NULL);
  161. for (x = 0; x < w; x++)
  162. {
  163. data = &(row[x * 4]);
  164. if (data[3] != 0)
  165. {
  166. rtgui_dc_set_color(dc, RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2]));
  167. rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1);
  168. }
  169. }
  170. }
  171. break;
  172. case PNG_COLOR_TYPE_PALETTE:
  173. for (y = 0; y < h; y++)
  174. {
  175. png_read_row(png->png_ptr, row, png_bytep_NULL);
  176. for (x = 0; x < w; x++)
  177. {
  178. data = &(row[x]);
  179. rtgui_dc_set_color(dc, RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red,
  180. png->info_ptr->palette[data[0]].green,
  181. png->info_ptr->palette[data[0]].blue));
  182. rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1);
  183. }
  184. }
  185. default:
  186. break;
  187. };
  188. rtgui_free(row);
  189. }
  190. /* restore foreground */
  191. rtgui_dc_set_color(dc, foreground);
  192. }
  193. void rtgui_image_hdc_init()
  194. {
  195. /* register png on image system */
  196. rtgui_image_register_engine(&rtgui_image_hdc_engine);
  197. }