image_jpg.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Change Logs:
  3. * Date Author Notes
  4. * 2012-01-24 onelife add TJpgDec (Tiny JPEG Decompressor) support
  5. */
  6. #include <rtthread.h>
  7. #include <rtgui/rtgui.h>
  8. #ifdef RTGUI_IMAGE_JPEG
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "jpeglib.h"
  12. #include <rtgui/rtgui_system.h>
  13. #include <rtgui/filerw.h>
  14. #include <rtgui/image_jpeg.h>
  15. #ifdef RTGUI_USING_DFS_FILERW
  16. #include <dfs_posix.h>
  17. #endif
  18. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file);
  19. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  20. static void rtgui_image_jpeg_unload(struct rtgui_image *image);
  21. static void rtgui_image_jpeg_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  22. struct rtgui_jpeg_error_mgr
  23. {
  24. struct jpeg_error_mgr pub; /* "public" fields */
  25. };
  26. struct rtgui_image_jpeg
  27. {
  28. rt_bool_t is_loaded;
  29. struct rtgui_filerw *filerw;
  30. /* jpeg structure */
  31. struct jpeg_decompress_struct cinfo;
  32. struct rtgui_jpeg_error_mgr errmgr;
  33. rt_uint8_t *pixels;
  34. rt_uint8_t *line_pixels;
  35. };
  36. struct rtgui_image_engine rtgui_image_jpeg_engine =
  37. {
  38. "jpeg",
  39. {RT_NULL},
  40. rtgui_image_jpeg_check,
  41. rtgui_image_jpeg_load,
  42. rtgui_image_jpeg_unload,
  43. rtgui_image_jpeg_blit
  44. };
  45. struct rtgui_image_engine rtgui_image_jpg_engine =
  46. {
  47. "jpg",
  48. {RT_NULL},
  49. rtgui_image_jpeg_check,
  50. rtgui_image_jpeg_load,
  51. rtgui_image_jpeg_unload,
  52. rtgui_image_jpeg_blit
  53. };
  54. #define INPUT_BUFFER_SIZE 4096
  55. typedef struct
  56. {
  57. struct jpeg_source_mgr pub;
  58. struct rtgui_filerw *ctx;
  59. rt_uint8_t buffer[INPUT_BUFFER_SIZE];
  60. } rtgui_jpeg_source_mgr;
  61. /*
  62. * Initialize source --- called by jpeg_read_header
  63. * before any data is actually read.
  64. */
  65. static void init_source(j_decompress_ptr cinfo)
  66. {
  67. /* We don't actually need to do anything */
  68. return;
  69. }
  70. /*
  71. * Fill the input buffer --- called whenever buffer is emptied.
  72. */
  73. static boolean fill_input_buffer(j_decompress_ptr cinfo)
  74. {
  75. rtgui_jpeg_source_mgr *src = (rtgui_jpeg_source_mgr *) cinfo->src;
  76. int nbytes;
  77. nbytes = rtgui_filerw_read(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
  78. if (nbytes <= 0)
  79. {
  80. /* Insert a fake EOI marker */
  81. src->buffer[0] = (rt_uint8_t) 0xFF;
  82. src->buffer[1] = (rt_uint8_t) JPEG_EOI;
  83. nbytes = 2;
  84. }
  85. src->pub.next_input_byte = src->buffer;
  86. src->pub.bytes_in_buffer = nbytes;
  87. return TRUE;
  88. }
  89. /*
  90. * Skip data --- used to skip over a potentially large amount of
  91. * uninteresting data (such as an APPn marker).
  92. *
  93. * Writers of suspendable-input applications must note that skip_input_data
  94. * is not granted the right to give a suspension return. If the skip extends
  95. * beyond the data currently in the buffer, the buffer can be marked empty so
  96. * that the next read will cause a fill_input_buffer call that can suspend.
  97. * Arranging for additional bytes to be discarded before reloading the input
  98. * buffer is the application writer's problem.
  99. */
  100. static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
  101. {
  102. rtgui_jpeg_source_mgr *src = (rtgui_jpeg_source_mgr *) cinfo->src;
  103. /* Just a dumb implementation for now. Could use fseek() except
  104. * it doesn't work on pipes. Not clear that being smart is worth
  105. * any trouble anyway --- large skips are infrequent.
  106. */
  107. if (num_bytes > 0)
  108. {
  109. while (num_bytes > (long) src->pub.bytes_in_buffer)
  110. {
  111. num_bytes -= (long) src->pub.bytes_in_buffer;
  112. (void) src->pub.fill_input_buffer(cinfo);
  113. /* note we assume that fill_input_buffer will never
  114. * return FALSE, so suspension need not be handled.
  115. */
  116. }
  117. src->pub.next_input_byte += (size_t) num_bytes;
  118. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  119. }
  120. }
  121. /*
  122. * Terminate source --- called by jpeg_finish_decompress
  123. * after all data has been read.
  124. */
  125. static void term_source(j_decompress_ptr cinfo)
  126. {
  127. /* We don't actually need to do anything */
  128. return;
  129. }
  130. /*
  131. * Prepare for input from a stdio stream.
  132. * The caller must have already opened the stream, and is responsible
  133. * for closing it after finishing decompression.
  134. */
  135. static void rtgui_jpeg_filerw_src_init(j_decompress_ptr cinfo, struct rtgui_filerw *ctx)
  136. {
  137. rtgui_jpeg_source_mgr *src;
  138. /* The source object and input buffer are made permanent so that a series
  139. * of JPEG images can be read from the same file by calling jpeg_stdio_src
  140. * only before the first one. (If we discarded the buffer at the end of
  141. * one image, we'd likely lose the start of the next one.)
  142. * This makes it unsafe to use this manager and a different source
  143. * manager serially with the same JPEG object. Caveat programmer.
  144. */
  145. if (cinfo->src == NULL) /* first time for this JPEG object? */
  146. {
  147. cinfo->src = (struct jpeg_source_mgr *)
  148. (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT,
  149. sizeof(rtgui_jpeg_source_mgr));
  150. src = (rtgui_jpeg_source_mgr *) cinfo->src;
  151. }
  152. src = (rtgui_jpeg_source_mgr *) cinfo->src;
  153. src->pub.init_source = init_source;
  154. src->pub.fill_input_buffer = fill_input_buffer;
  155. src->pub.skip_input_data = skip_input_data;
  156. src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  157. src->pub.term_source = term_source;
  158. src->ctx = ctx;
  159. src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  160. src->pub.next_input_byte = NULL; /* until buffer loaded */
  161. }
  162. /* get line data of a jpeg image */
  163. static rt_uint8_t *rtgui_image_get_line(struct rtgui_image *image, int h)
  164. {
  165. struct rtgui_image_jpeg *jpeg;
  166. rt_uint8_t *result_ptr;
  167. JSAMPARRAY buffer; /* Output row buffer */
  168. int row_stride;
  169. RT_ASSERT(image != RT_NULL);
  170. jpeg = (struct rtgui_image_jpeg *) image->data;
  171. RT_ASSERT(jpeg != RT_NULL);
  172. if (h < 0 || h > image->h) return RT_NULL;
  173. /* if the image is loaded, */
  174. if (jpeg->is_loaded == RT_TRUE)
  175. {
  176. result_ptr = jpeg->pixels + (image->w * sizeof(rtgui_color_t)) * h;
  177. return result_ptr;
  178. }
  179. if (jpeg->line_pixels == RT_NULL)
  180. jpeg->line_pixels = rtgui_malloc(image->w * sizeof(rtgui_color_t));
  181. row_stride = jpeg->cinfo.output_width * jpeg->cinfo.output_components;
  182. buffer = (*jpeg->cinfo.mem->alloc_sarray)
  183. ((j_common_ptr) &jpeg->cinfo, JPOOL_IMAGE, row_stride, 1);
  184. /* decompress line data */
  185. jpeg->cinfo.output_scanline = h;
  186. jpeg_read_scanlines(&jpeg->cinfo, buffer, (JDIMENSION) 1);
  187. /* copy pixels memory */
  188. {
  189. int index;
  190. rtgui_color_t *ptr;
  191. ptr = (rtgui_color_t *)jpeg->line_pixels;
  192. for (index = 0; index < image->w; index ++)
  193. ptr[index] = RTGUI_ARGB(0, buffer[0][index * 3], buffer[0][index * 3 + 1], buffer[0][index * 3 + 2]);
  194. }
  195. return jpeg->line_pixels;
  196. }
  197. static rt_bool_t rtgui_image_jpeg_loadall(struct rtgui_image *image)
  198. {
  199. struct rtgui_image_jpeg *jpeg;
  200. rt_uint8_t *line_ptr;
  201. JSAMPARRAY buffer; /* Output row buffer */
  202. int row_stride;
  203. jpeg = (struct rtgui_image_jpeg *) image->data;
  204. RT_ASSERT(jpeg != RT_NULL);
  205. /* already load */
  206. if (jpeg->pixels != RT_NULL) return RT_TRUE;
  207. /* allocate all pixels */
  208. jpeg->pixels = rtgui_malloc(image->h * image->w * sizeof(rtgui_color_t));
  209. if (jpeg->pixels == RT_NULL) return RT_FALSE;
  210. /* reset scan line to zero */
  211. jpeg->cinfo.output_scanline = 0;
  212. line_ptr = jpeg->pixels;
  213. row_stride = jpeg->cinfo.output_width * jpeg->cinfo.output_components;
  214. buffer = (*jpeg->cinfo.mem->alloc_sarray)
  215. ((j_common_ptr) &jpeg->cinfo, JPOOL_IMAGE, row_stride, 1);
  216. /* decompress all pixels */
  217. while (jpeg->cinfo.output_scanline < jpeg->cinfo.output_height)
  218. {
  219. /* jpeg_read_scanlines expects an array of pointers to scanlines.
  220. * Here the array is only one element long, but you could ask for
  221. * more than one scanline at a time if that's more convenient.
  222. */
  223. (void) jpeg_read_scanlines(&jpeg->cinfo, buffer, 1);
  224. /* copy pixels memory */
  225. {
  226. int index;
  227. rtgui_color_t *ptr;
  228. ptr = (rtgui_color_t *)line_ptr;
  229. for (index = 0; index < image->w; index ++)
  230. ptr[index] = RTGUI_ARGB(0, buffer[0][index * 3], buffer[0][index * 3 + 1], buffer[0][index * 3 + 2]);
  231. }
  232. /* move to next line */
  233. line_ptr += image->w * sizeof(rtgui_color_t);
  234. }
  235. /* decompress done */
  236. rtgui_filerw_close(jpeg->filerw);
  237. jpeg_finish_decompress(&jpeg->cinfo);
  238. jpeg->is_loaded = RT_TRUE;
  239. return RT_TRUE;
  240. }
  241. void rtgui_image_jpeg_init()
  242. {
  243. /* register jpeg on image system */
  244. rtgui_image_register_engine(&rtgui_image_jpeg_engine);
  245. /* register jpg on image system */
  246. rtgui_image_register_engine(&rtgui_image_jpg_engine);
  247. }
  248. static void my_error_exit(j_common_ptr cinfo)
  249. {
  250. }
  251. static void output_no_message(j_common_ptr cinfo)
  252. {
  253. /* do nothing */
  254. }
  255. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  256. {
  257. struct rtgui_image_jpeg *jpeg;
  258. jpeg = (struct rtgui_image_jpeg *) rtgui_malloc(sizeof(struct rtgui_image_jpeg));
  259. if (jpeg == RT_NULL) return RT_FALSE;
  260. jpeg->filerw = file;
  261. /* read file header */
  262. /* Create a decompression structure and load the JPEG header */
  263. jpeg->cinfo.err = jpeg_std_error(&jpeg->errmgr.pub);
  264. jpeg->errmgr.pub.error_exit = my_error_exit;
  265. jpeg->errmgr.pub.output_message = output_no_message;
  266. jpeg_create_decompress(&jpeg->cinfo);
  267. rtgui_jpeg_filerw_src_init(&jpeg->cinfo, jpeg->filerw);
  268. (void)jpeg_read_header(&jpeg->cinfo, TRUE);
  269. image->w = jpeg->cinfo.image_width;
  270. image->h = jpeg->cinfo.image_height;
  271. /* set image private data and engine */
  272. image->data = jpeg;
  273. image->engine = &rtgui_image_jpeg_engine;
  274. /* start decompression */
  275. (void) jpeg_start_decompress(&jpeg->cinfo);
  276. jpeg->cinfo.out_color_space = JCS_RGB;
  277. jpeg->cinfo.quantize_colors = FALSE;
  278. /* use fast jpeg */
  279. jpeg->cinfo.scale_num = 1;
  280. jpeg->cinfo.scale_denom = 1;
  281. jpeg->cinfo.dct_method = JDCT_FASTEST;
  282. jpeg->cinfo.do_fancy_upsampling = FALSE;
  283. jpeg->pixels = RT_NULL;
  284. jpeg->is_loaded = RT_FALSE;
  285. /* allocate line pixels */
  286. jpeg->line_pixels = rtgui_malloc(image->w * sizeof(rtgui_color_t));
  287. if (jpeg->line_pixels == RT_NULL)
  288. {
  289. /* no memory */
  290. jpeg_finish_decompress(&jpeg->cinfo);
  291. jpeg_destroy_decompress(&jpeg->cinfo);
  292. rt_free(jpeg);
  293. return RT_FALSE;
  294. }
  295. if (load == RT_TRUE) rtgui_image_jpeg_loadall(image);
  296. /* create jpeg image successful */
  297. return RT_TRUE;
  298. }
  299. static void rtgui_image_jpeg_unload(struct rtgui_image *image)
  300. {
  301. if (image != RT_NULL)
  302. {
  303. struct rtgui_image_jpeg *jpeg;
  304. jpeg = (struct rtgui_image_jpeg *) image->data;
  305. RT_ASSERT(jpeg != RT_NULL);
  306. if (jpeg->is_loaded == RT_TRUE)
  307. rtgui_free(jpeg->pixels);
  308. if (jpeg->line_pixels != RT_NULL) rtgui_free(jpeg->line_pixels);
  309. if (jpeg->is_loaded != RT_TRUE)
  310. {
  311. rtgui_filerw_close(jpeg->filerw);
  312. jpeg_finish_decompress(&jpeg->cinfo);
  313. }
  314. jpeg_destroy_decompress(&jpeg->cinfo);
  315. rt_free(jpeg);
  316. }
  317. }
  318. static void rtgui_image_jpeg_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  319. {
  320. rt_uint16_t x, y;
  321. rtgui_color_t *ptr;
  322. struct rtgui_image_jpeg *jpeg;
  323. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  324. jpeg = (struct rtgui_image_jpeg *) image->data;
  325. RT_ASSERT(jpeg != RT_NULL);
  326. if (jpeg->pixels != RT_NULL)
  327. {
  328. ptr = (rtgui_color_t *) jpeg->pixels;
  329. /* draw each point within dc */
  330. for (y = 0; y < image->h; y ++)
  331. {
  332. for (x = 0; x < image->w; x++)
  333. {
  334. /* not alpha */
  335. if ((*ptr >> 24) != 255)
  336. {
  337. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr);
  338. }
  339. /* move to next color buffer */
  340. ptr ++;
  341. }
  342. }
  343. }
  344. else
  345. {
  346. /* seek to the begin of file */
  347. rtgui_filerw_seek(jpeg->filerw, 0, RTGUI_FILE_SEEK_SET);
  348. /* decompress line and line */
  349. for (y = 0; y < image->h; y ++)
  350. {
  351. ptr = (rtgui_color_t *)rtgui_image_get_line(image, y);
  352. for (x = 0; x < image->w; x++)
  353. {
  354. /* not alpha */
  355. if ((*ptr >> 24) != 255)
  356. {
  357. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr);
  358. }
  359. /* move to next color buffer */
  360. ptr ++;
  361. }
  362. }
  363. }
  364. }
  365. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file)
  366. {
  367. int start;
  368. rt_bool_t is_JPG;
  369. int in_scan;
  370. rt_uint8_t magic[4];
  371. if (file == RT_NULL) return RT_FALSE; /* open file failed */
  372. start = rtgui_filerw_tell(file);
  373. is_JPG = RT_FALSE;
  374. in_scan = 0;
  375. /* seek to the begining of file */
  376. rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
  377. if (rtgui_filerw_read(file, magic, 2, 1))
  378. {
  379. if ((magic[0] == 0xFF) && (magic[1] == 0xD8))
  380. {
  381. is_JPG = RT_TRUE;
  382. while (is_JPG == RT_TRUE)
  383. {
  384. if (rtgui_filerw_read(file, magic, 1, 2) != 2)
  385. {
  386. is_JPG = RT_FALSE;
  387. }
  388. else if ((magic[0] != 0xFF) && (in_scan == 0))
  389. {
  390. is_JPG = RT_FALSE;
  391. }
  392. else if ((magic[0] != 0xFF) || (magic[1] == 0xFF))
  393. {
  394. /* Extra padding in JPEG (legal) */
  395. /* or this is data and we are scanning */
  396. rtgui_filerw_seek(file, -1, RTGUI_FILE_SEEK_CUR);
  397. }
  398. else if (magic[1] == 0xD9)
  399. {
  400. /* Got to end of good JPEG */
  401. break;
  402. }
  403. else if ((in_scan == 1) && (magic[1] == 0x00))
  404. {
  405. /* This is an encoded 0xFF within the data */
  406. }
  407. else if ((magic[1] >= 0xD0) && (magic[1] < 0xD9))
  408. {
  409. /* These have nothing else */
  410. }
  411. else if (rtgui_filerw_read(file, magic + 2, 1, 2) != 2)
  412. {
  413. is_JPG = RT_FALSE;
  414. }
  415. else
  416. {
  417. /* Yes, it's big-endian */
  418. rt_uint32_t start;
  419. rt_uint32_t size;
  420. rt_uint32_t end;
  421. start = rtgui_filerw_tell(file);
  422. size = (magic[2] << 8) + magic[3];
  423. end = rtgui_filerw_seek(file, size - 2, RTGUI_FILE_SEEK_CUR);
  424. if (end != start + size - 2) is_JPG = RT_FALSE;
  425. if (magic[1] == 0xDA)
  426. {
  427. /* Now comes the actual JPEG meat */
  428. /* It is a JPEG. */
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
  436. return is_JPG;
  437. }
  438. #endif
  439. #if defined(RTGUI_IMAGE_TJPGD)
  440. /***************************************************************************//**
  441. * @file image_jpg.c
  442. * @brief JPEG decoder using TJpgDec module (elm-chan.org)
  443. * COPYRIGHT (C) 2012, RT-Thread Development Team
  444. * @author onelife
  445. * @version 1.0
  446. *******************************************************************************
  447. * @section License
  448. * The license and distribution terms for this file may be found in the file
  449. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  450. *******************************************************************************
  451. * @section Change Logs
  452. * Date Author Notes
  453. * 2012-01-24 onelife Initial creation for limited memory devices
  454. ******************************************************************************/
  455. /***************************************************************************//**
  456. * @addtogroup TJpgDec
  457. * @{
  458. ******************************************************************************/
  459. /* Includes ------------------------------------------------------------------*/
  460. #include "tjpgd.h"
  461. #include <rtgui/rtgui_system.h>
  462. #include <rtgui/filerw.h>
  463. #include <rtgui/blit.h>
  464. #include <rtgui/image_jpeg.h>
  465. #ifdef RTGUI_USING_DFS_FILERW
  466. #include <dfs_posix.h>
  467. #endif
  468. /* Private typedef -----------------------------------------------------------*/
  469. struct rtgui_image_jpeg
  470. {
  471. struct rtgui_filerw *filerw;
  472. struct rtgui_dc *dc;
  473. rt_uint16_t dst_x, dst_y;
  474. rt_uint16_t dst_w, dst_h;
  475. rt_bool_t is_loaded;
  476. rt_bool_t to_buffer;
  477. rt_uint8_t scale;
  478. rt_uint8_t byte_per_pixel;
  479. JDEC tjpgd; /* jpeg structure */
  480. void *pool;
  481. rt_uint8_t *pixels;
  482. };
  483. /* Private define ------------------------------------------------------------*/
  484. #define TJPGD_WORKING_BUFFER_SIZE (3100)
  485. #define TJPGD_MAX_MCU_WIDTH_ON_DISP (2 * 8 * 4) /* Y component: 2x2; Display: 4-byte per pixel */
  486. #define TJPGD_MAX_SCALING_FACTOR (3)
  487. #define hw_driver (rtgui_graphic_driver_get_default())
  488. /* Private macro -------------------------------------------------------------*/
  489. /* Private function prototypes -----------------------------------------------*/
  490. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file);
  491. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  492. static void rtgui_image_jpeg_unload(struct rtgui_image *image);
  493. static void rtgui_image_jpeg_blit(struct rtgui_image *image,
  494. struct rtgui_dc *dc, struct rtgui_rect *dst_rect);
  495. /* Private variables ---------------------------------------------------------*/
  496. struct rtgui_image_engine rtgui_image_jpeg_engine =
  497. {
  498. "jpeg",
  499. {RT_NULL},
  500. rtgui_image_jpeg_check,
  501. rtgui_image_jpeg_load,
  502. rtgui_image_jpeg_unload,
  503. rtgui_image_jpeg_blit
  504. };
  505. struct rtgui_image_engine rtgui_image_jpg_engine =
  506. {
  507. "jpg",
  508. {RT_NULL},
  509. rtgui_image_jpeg_check,
  510. rtgui_image_jpeg_load,
  511. rtgui_image_jpeg_unload,
  512. rtgui_image_jpeg_blit
  513. };
  514. /* Private functions ---------------------------------------------------------*/
  515. void rtgui_image_jpeg_init()
  516. {
  517. /* register jpeg on image system */
  518. rtgui_image_register_engine(&rtgui_image_jpeg_engine);
  519. /* register jpg on image system */
  520. rtgui_image_register_engine(&rtgui_image_jpg_engine);
  521. }
  522. static UINT tjpgd_in_func(JDEC *jdec, BYTE *buff, UINT ndata)
  523. {
  524. struct rtgui_filerw *file = *(struct rtgui_filerw **)jdec->device;
  525. if (buff == RT_NULL)
  526. {
  527. return rtgui_filerw_seek(file, ndata, RTGUI_FILE_SEEK_CUR);
  528. }
  529. return rtgui_filerw_read(file, (void *)buff, 1, ndata);
  530. }
  531. static UINT tjpgd_out_func(JDEC *jdec, void *bitmap, JRECT *rect)
  532. {
  533. struct rtgui_image_jpeg *jpeg = (struct rtgui_image_jpeg *)jdec->device;
  534. rt_uint16_t w, h, y;
  535. rt_uint16_t rectWidth; /* Width of source rectangular (bytes) */
  536. rt_uint8_t *src, *dst;
  537. #ifdef RTGUI_DEBUG_TJPGD
  538. /* Put progress indicator */
  539. if (rect->left == 0)
  540. {
  541. rt_kprintf("\r%lu%%", (rect->top << jpeg->scale) * 100UL / jdec->height);
  542. }
  543. #endif
  544. /* Copy the decompressed RGB rectanglar to the frame buffer */
  545. rectWidth = (rect->right - rect->left + 1) * jpeg->byte_per_pixel;
  546. src = (rt_uint8_t *)bitmap;
  547. if (jpeg->to_buffer)
  548. {
  549. rt_uint16_t imageWidth; /* Width of image (bytes) */
  550. imageWidth = (jdec->width >> jdec->scale) * jpeg->byte_per_pixel;
  551. dst = jpeg->pixels + rect->top * imageWidth + rect->left * jpeg->byte_per_pixel;
  552. /* Left-top of destination rectangular */
  553. for (h = rect->top; h <= rect->bottom; h++)
  554. {
  555. rt_memcpy(dst, src, rectWidth);
  556. src += rectWidth;
  557. dst += imageWidth; /* Next line */
  558. }
  559. }
  560. else
  561. {
  562. rtgui_blit_line_func blit_line = RT_NULL;
  563. /* we decompress from top to bottom if the block is beyond the right
  564. * boundary, just continue to next block. However, if the block is
  565. * beyond the bottom boundary, we don't need to decompress the rest. */
  566. if (rect->left > jpeg->dst_w)
  567. return 1;
  568. if (rect->top > jpeg->dst_h)
  569. return 0;
  570. w = rect->right < jpeg->dst_w ? rect->right : jpeg->dst_w;
  571. w = w - rect->left + 1;
  572. h = rect->bottom < jpeg->dst_h ? rect->bottom : jpeg->dst_h;
  573. h = h - rect->top + 1;
  574. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  575. {
  576. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  577. {
  578. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  579. }
  580. }
  581. else
  582. {
  583. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  584. }
  585. if (blit_line)
  586. {
  587. rt_uint8_t line_buf[TJPGD_MAX_MCU_WIDTH_ON_DISP];
  588. for (y = 0; y < h; y++)
  589. {
  590. blit_line(line_buf, src, w * jpeg->byte_per_pixel);
  591. jpeg->dc->engine->blit_line(jpeg->dc,
  592. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  593. jpeg->dst_y + rect->top + y,
  594. line_buf);
  595. src += rectWidth;
  596. }
  597. }
  598. else
  599. {
  600. for (y = 0; y < h; y++)
  601. {
  602. jpeg->dc->engine->blit_line(jpeg->dc,
  603. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  604. jpeg->dst_y + rect->top + y,
  605. src);
  606. src += rectWidth;
  607. }
  608. }
  609. }
  610. return 1; /* Continue to decompress */
  611. }
  612. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file)
  613. {
  614. rt_bool_t is_JPG;
  615. JDEC tjpgd;
  616. void *pool;
  617. if (!file)
  618. {
  619. return RT_FALSE;
  620. }
  621. is_JPG = RT_FALSE;
  622. do
  623. {
  624. pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  625. if (pool == RT_NULL)
  626. {
  627. rt_kprintf("TJPGD err: no mem\n");
  628. break;
  629. }
  630. if (rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET) == -1)
  631. {
  632. break;
  633. }
  634. if (jd_prepare(&tjpgd, tjpgd_in_func, pool,
  635. TJPGD_WORKING_BUFFER_SIZE, (void *)&file) == JDR_OK)
  636. {
  637. is_JPG = RT_TRUE;
  638. }
  639. #ifdef RTGUI_DEBUG_TJPGD
  640. rt_kprintf("TJPGD: check OK\n");
  641. #endif
  642. }
  643. while (0);
  644. rt_free(pool);
  645. return is_JPG;
  646. }
  647. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  648. {
  649. rt_uint8_t scale = 0;
  650. rt_bool_t res = RT_FALSE;
  651. struct rtgui_image_jpeg *jpeg;
  652. JRESULT ret;
  653. if (scale > TJPGD_MAX_SCALING_FACTOR)
  654. {
  655. return RT_FALSE;
  656. }
  657. do
  658. {
  659. jpeg = (struct rtgui_image_jpeg *)rt_malloc(sizeof(struct rtgui_image_jpeg));
  660. if (jpeg == RT_NULL)
  661. {
  662. break;
  663. }
  664. jpeg->filerw = file;
  665. jpeg->is_loaded = RT_FALSE;
  666. jpeg->to_buffer = load;
  667. jpeg->scale = scale;
  668. #if (JD_FORMAT == 0)
  669. jpeg->byte_per_pixel = 3;
  670. #elif (JD_FORMAT == 1)
  671. jpeg->byte_per_pixel = 2;
  672. #endif
  673. jpeg->pool = RT_NULL;
  674. jpeg->pixels = RT_NULL;
  675. jpeg->pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  676. if (jpeg->pool == RT_NULL)
  677. {
  678. rt_kprintf("TJPGD err: no mem (%d)\n", TJPGD_WORKING_BUFFER_SIZE);
  679. break;
  680. }
  681. if (rtgui_filerw_seek(jpeg->filerw, 0, RTGUI_FILE_SEEK_SET) == -1)
  682. {
  683. break;
  684. }
  685. ret = jd_prepare(&jpeg->tjpgd, tjpgd_in_func, jpeg->pool,
  686. TJPGD_WORKING_BUFFER_SIZE, (void *)jpeg);
  687. if (ret != JDR_OK)
  688. {
  689. if (ret == JDR_FMT3)
  690. {
  691. rt_kprintf("TJPGD: not supported format\n");
  692. }
  693. break;
  694. }
  695. #ifdef RTGUI_DEBUG_TJPGD
  696. rt_kprintf("TJPGD: prepare OK\n");
  697. #endif
  698. image->w = (rt_uint16_t)jpeg->tjpgd.width >> jpeg->scale;
  699. image->h = (rt_uint16_t)jpeg->tjpgd.height >> jpeg->scale;
  700. /* set image private data and engine */
  701. image->data = jpeg;
  702. image->engine = &rtgui_image_jpeg_engine;
  703. if (jpeg->to_buffer == RT_TRUE)
  704. {
  705. jpeg->pixels = (rt_uint8_t *)rtgui_malloc(
  706. jpeg->byte_per_pixel * image->w * image->h);
  707. if (jpeg->pixels == RT_NULL)
  708. {
  709. rt_kprintf("TJPGD err: no mem to load (%d)\n",
  710. jpeg->byte_per_pixel * image->w * image->h);
  711. break;
  712. }
  713. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  714. if (ret != JDR_OK)
  715. {
  716. break;
  717. }
  718. rtgui_filerw_close(jpeg->filerw);
  719. jpeg->is_loaded = RT_TRUE;
  720. #ifdef RTGUI_DEBUG_TJPGD
  721. rt_kprintf("TJPGD: load to RAM\n");
  722. #endif
  723. }
  724. res = RT_TRUE;
  725. }
  726. while (0);
  727. if (!res || jpeg->is_loaded)
  728. {
  729. rt_free(jpeg->pool);
  730. }
  731. if (!res)
  732. {
  733. rtgui_free(jpeg->pixels);
  734. rt_free(jpeg);
  735. }
  736. /* create jpeg image successful */
  737. return res;
  738. }
  739. static void rtgui_image_jpeg_unload(struct rtgui_image *image)
  740. {
  741. if (image != RT_NULL)
  742. {
  743. struct rtgui_image_jpeg *jpeg;
  744. jpeg = (struct rtgui_image_jpeg *) image->data;
  745. RT_ASSERT(jpeg != RT_NULL);
  746. if (jpeg->to_buffer == RT_TRUE)
  747. {
  748. if (jpeg->is_loaded == RT_TRUE)
  749. {
  750. rtgui_free(jpeg->pixels);
  751. }
  752. if (jpeg->is_loaded != RT_TRUE)
  753. {
  754. rtgui_filerw_close(jpeg->filerw);
  755. }
  756. }
  757. else
  758. {
  759. rt_free(jpeg->pool);
  760. rtgui_filerw_close(jpeg->filerw);
  761. }
  762. rt_free(jpeg);
  763. }
  764. #ifdef RTGUI_DEBUG_TJPGD
  765. rt_kprintf("TJPGD: unload\n");
  766. #endif
  767. }
  768. static void rtgui_image_jpeg_blit(struct rtgui_image *image,
  769. struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  770. {
  771. rt_uint16_t w, h, y;
  772. struct rtgui_image_jpeg *jpeg;
  773. jpeg = (struct rtgui_image_jpeg *) image->data;
  774. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL || jpeg != RT_NULL);
  775. do
  776. {
  777. /* this dc is not visible */
  778. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  779. {
  780. break;
  781. }
  782. jpeg->dc = dc;
  783. /* the minimum rect */
  784. if (image->w < rtgui_rect_width(*dst_rect))
  785. {
  786. w = image->w;
  787. }
  788. else
  789. {
  790. w = rtgui_rect_width(*dst_rect);
  791. }
  792. if (image->h < rtgui_rect_height(*dst_rect))
  793. {
  794. h = image->h;
  795. }
  796. else
  797. {
  798. h = rtgui_rect_height(*dst_rect);
  799. }
  800. if (!jpeg->is_loaded)
  801. {
  802. JRESULT ret;
  803. jpeg->dst_x = dst_rect->x1;
  804. jpeg->dst_y = dst_rect->y1;
  805. jpeg->dst_w = w;
  806. jpeg->dst_h = h;
  807. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  808. if (ret != JDR_OK)
  809. {
  810. break;
  811. }
  812. #ifdef RTGUI_DEBUG_TJPGD
  813. rt_kprintf("TJPGD: load to display\n");
  814. #endif
  815. }
  816. else
  817. {
  818. rt_uint8_t *src = jpeg->pixels;
  819. rt_uint16_t imageWidth = image->w * jpeg->byte_per_pixel;
  820. rtgui_blit_line_func blit_line = RT_NULL;
  821. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  822. {
  823. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  824. {
  825. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  826. }
  827. }
  828. else
  829. {
  830. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  831. }
  832. if (blit_line)
  833. {
  834. rt_uint16_t x;
  835. rt_uint8_t temp[4];
  836. for (y = 0; y < h; y++)
  837. {
  838. for (x = 0; x < w; x++)
  839. {
  840. blit_line(temp, src, jpeg->byte_per_pixel);
  841. src += jpeg->byte_per_pixel;
  842. dc->engine->blit_line(dc,
  843. dst_rect->x1 + x, dst_rect->x1 + x,
  844. dst_rect->y1 + y,
  845. temp);
  846. }
  847. }
  848. }
  849. else
  850. {
  851. for (y = 0; y < h; y++)
  852. {
  853. dc->engine->blit_line(dc,
  854. dst_rect->x1, dst_rect->x1 + w,
  855. dst_rect->y1 + y,
  856. src);
  857. src += imageWidth;
  858. }
  859. }
  860. }
  861. }
  862. while (0);
  863. }
  864. #endif /* defined(RTGUI_IMAGE_TJPGD) */
  865. /***************************************************************************//**
  866. * @}
  867. ******************************************************************************/