image_jpg.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. rt_free(jpeg);
  292. return RT_FALSE;
  293. }
  294. if (load == RT_TRUE) rtgui_image_jpeg_loadall(image);
  295. /* create jpeg image successful */
  296. return RT_TRUE;
  297. }
  298. static void rtgui_image_jpeg_unload(struct rtgui_image *image)
  299. {
  300. if (image != RT_NULL)
  301. {
  302. struct rtgui_image_jpeg *jpeg;
  303. jpeg = (struct rtgui_image_jpeg *) image->data;
  304. RT_ASSERT(jpeg != RT_NULL);
  305. if (jpeg->is_loaded == RT_TRUE)
  306. rtgui_free(jpeg->pixels);
  307. if (jpeg->line_pixels != RT_NULL) rtgui_free(jpeg->line_pixels);
  308. if (jpeg->is_loaded != RT_TRUE)
  309. {
  310. rtgui_filerw_close(jpeg->filerw);
  311. jpeg_finish_decompress(&jpeg->cinfo);
  312. }
  313. rt_free(jpeg);
  314. }
  315. }
  316. static void rtgui_image_jpeg_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
  317. {
  318. rt_uint16_t x, y;
  319. rtgui_color_t *ptr;
  320. struct rtgui_image_jpeg *jpeg;
  321. RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL);
  322. jpeg = (struct rtgui_image_jpeg *) image->data;
  323. RT_ASSERT(jpeg != RT_NULL);
  324. if (jpeg->pixels != RT_NULL)
  325. {
  326. ptr = (rtgui_color_t *) jpeg->pixels;
  327. /* draw each point within dc */
  328. for (y = 0; y < image->h; y ++)
  329. {
  330. for (x = 0; x < image->w; x++)
  331. {
  332. /* not alpha */
  333. if ((*ptr >> 24) != 255)
  334. {
  335. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr);
  336. }
  337. /* move to next color buffer */
  338. ptr ++;
  339. }
  340. }
  341. }
  342. else
  343. {
  344. /* seek to the begin of file */
  345. rtgui_filerw_seek(jpeg->filerw, 0, RTGUI_FILE_SEEK_SET);
  346. /* decompress line and line */
  347. for (y = 0; y < image->h; y ++)
  348. {
  349. ptr = (rtgui_color_t *)rtgui_image_get_line(image, y);
  350. for (x = 0; x < image->w; x++)
  351. {
  352. /* not alpha */
  353. if ((*ptr >> 24) != 255)
  354. {
  355. rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr);
  356. }
  357. /* move to next color buffer */
  358. ptr ++;
  359. }
  360. }
  361. }
  362. }
  363. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file)
  364. {
  365. int start;
  366. rt_bool_t is_JPG;
  367. int in_scan;
  368. rt_uint8_t magic[4];
  369. if (file == RT_NULL) return RT_FALSE; /* open file failed */
  370. start = rtgui_filerw_tell(file);
  371. is_JPG = RT_FALSE;
  372. in_scan = 0;
  373. /* seek to the begining of file */
  374. rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
  375. if (rtgui_filerw_read(file, magic, 2, 1))
  376. {
  377. if ((magic[0] == 0xFF) && (magic[1] == 0xD8))
  378. {
  379. is_JPG = RT_TRUE;
  380. while (is_JPG == RT_TRUE)
  381. {
  382. if (rtgui_filerw_read(file, magic, 1, 2) != 2)
  383. {
  384. is_JPG = RT_FALSE;
  385. }
  386. else if ((magic[0] != 0xFF) && (in_scan == 0))
  387. {
  388. is_JPG = RT_FALSE;
  389. }
  390. else if ((magic[0] != 0xFF) || (magic[1] == 0xFF))
  391. {
  392. /* Extra padding in JPEG (legal) */
  393. /* or this is data and we are scanning */
  394. rtgui_filerw_seek(file, -1, RTGUI_FILE_SEEK_CUR);
  395. }
  396. else if (magic[1] == 0xD9)
  397. {
  398. /* Got to end of good JPEG */
  399. break;
  400. }
  401. else if ((in_scan == 1) && (magic[1] == 0x00))
  402. {
  403. /* This is an encoded 0xFF within the data */
  404. }
  405. else if ((magic[1] >= 0xD0) && (magic[1] < 0xD9))
  406. {
  407. /* These have nothing else */
  408. }
  409. else if (rtgui_filerw_read(file, magic + 2, 1, 2) != 2)
  410. {
  411. is_JPG = RT_FALSE;
  412. }
  413. else
  414. {
  415. /* Yes, it's big-endian */
  416. rt_uint32_t start;
  417. rt_uint32_t size;
  418. rt_uint32_t end;
  419. start = rtgui_filerw_tell(file);
  420. size = (magic[2] << 8) + magic[3];
  421. end = rtgui_filerw_seek(file, size - 2, RTGUI_FILE_SEEK_CUR);
  422. if (end != start + size - 2) is_JPG = RT_FALSE;
  423. if (magic[1] == 0xDA)
  424. {
  425. /* Now comes the actual JPEG meat */
  426. /* It is a JPEG. */
  427. break;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
  434. return is_JPG;
  435. }
  436. #endif
  437. #if defined(RTGUI_IMAGE_TJPGD)
  438. /***************************************************************************//**
  439. * @file image_jpg.c
  440. * @brief JPEG decoder using TJpgDec module (elm-chan.org)
  441. * COPYRIGHT (C) 2012, RT-Thread Development Team
  442. * @author onelife
  443. * @version 1.0
  444. *******************************************************************************
  445. * @section License
  446. * The license and distribution terms for this file may be found in the file
  447. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  448. *******************************************************************************
  449. * @section Change Logs
  450. * Date Author Notes
  451. * 2012-01-24 onelife Initial creation for limited memory devices
  452. ******************************************************************************/
  453. /***************************************************************************//**
  454. * @addtogroup TJpgDec
  455. * @{
  456. ******************************************************************************/
  457. /* Includes ------------------------------------------------------------------*/
  458. #include "tjpgd.h"
  459. #include <rtgui/rtgui_system.h>
  460. #include <rtgui/filerw.h>
  461. #include <rtgui/blit.h>
  462. #include <rtgui/image_jpeg.h>
  463. #ifdef RTGUI_USING_DFS_FILERW
  464. #include <dfs_posix.h>
  465. #endif
  466. /* Private typedef -----------------------------------------------------------*/
  467. struct rtgui_image_jpeg
  468. {
  469. struct rtgui_filerw *filerw;
  470. struct rtgui_dc *dc;
  471. rt_uint16_t dst_x, dst_y;
  472. rt_uint16_t dst_w, dst_h;
  473. rt_bool_t is_loaded;
  474. rt_bool_t to_buffer;
  475. rt_uint8_t scale;
  476. rt_uint8_t byte_per_pixel;
  477. JDEC tjpgd; /* jpeg structure */
  478. void *pool;
  479. rt_uint8_t *pixels;
  480. };
  481. /* Private define ------------------------------------------------------------*/
  482. #define TJPGD_WORKING_BUFFER_SIZE (3100)
  483. #define TJPGD_MAX_MCU_WIDTH_ON_DISP (2 * 8 * 4) /* Y component: 2x2; Display: 4-byte per pixel */
  484. #define TJPGD_MAX_SCALING_FACTOR (3)
  485. #define hw_driver (rtgui_graphic_driver_get_default())
  486. /* Private macro -------------------------------------------------------------*/
  487. /* Private function prototypes -----------------------------------------------*/
  488. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file);
  489. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  490. static void rtgui_image_jpeg_unload(struct rtgui_image *image);
  491. static void rtgui_image_jpeg_blit(struct rtgui_image *image,
  492. struct rtgui_dc *dc, struct rtgui_rect *dst_rect);
  493. /* Private variables ---------------------------------------------------------*/
  494. struct rtgui_image_engine rtgui_image_jpeg_engine =
  495. {
  496. "jpeg",
  497. {RT_NULL},
  498. rtgui_image_jpeg_check,
  499. rtgui_image_jpeg_load,
  500. rtgui_image_jpeg_unload,
  501. rtgui_image_jpeg_blit
  502. };
  503. struct rtgui_image_engine rtgui_image_jpg_engine =
  504. {
  505. "jpg",
  506. {RT_NULL},
  507. rtgui_image_jpeg_check,
  508. rtgui_image_jpeg_load,
  509. rtgui_image_jpeg_unload,
  510. rtgui_image_jpeg_blit
  511. };
  512. /* Private functions ---------------------------------------------------------*/
  513. void rtgui_image_jpeg_init()
  514. {
  515. /* register jpeg on image system */
  516. rtgui_image_register_engine(&rtgui_image_jpeg_engine);
  517. /* register jpg on image system */
  518. rtgui_image_register_engine(&rtgui_image_jpg_engine);
  519. }
  520. static UINT tjpgd_in_func(JDEC *jdec, BYTE *buff, UINT ndata)
  521. {
  522. struct rtgui_filerw *file = *(struct rtgui_filerw **)jdec->device;
  523. if (buff == RT_NULL)
  524. {
  525. return rtgui_filerw_seek(file, ndata, RTGUI_FILE_SEEK_CUR);
  526. }
  527. return rtgui_filerw_read(file, (void *)buff, 1, ndata);
  528. }
  529. static UINT tjpgd_out_func(JDEC *jdec, void *bitmap, JRECT *rect)
  530. {
  531. struct rtgui_image_jpeg *jpeg = (struct rtgui_image_jpeg *)jdec->device;
  532. rt_uint16_t w, h, y;
  533. rt_uint16_t rectWidth; /* Width of source rectangular (bytes) */
  534. rt_uint8_t *src, *dst;
  535. /* Put progress indicator */
  536. if (rect->left == 0)
  537. {
  538. rt_kprintf("\r%lu%%", (rect->top << jpeg->scale) * 100UL / jdec->height);
  539. }
  540. /* Copy the decompressed RGB rectanglar to the frame buffer */
  541. rectWidth = (rect->right - rect->left + 1) * jpeg->byte_per_pixel;
  542. src = (rt_uint8_t *)bitmap;
  543. if (jpeg->to_buffer)
  544. {
  545. rt_uint16_t imageWidth; /* Width of image (bytes) */
  546. imageWidth = (jdec->width >> jdec->scale) * jpeg->byte_per_pixel;
  547. dst = jpeg->pixels + rect->top * imageWidth + rect->left * jpeg->byte_per_pixel;
  548. /* Left-top of destination rectangular */
  549. for (h = rect->top; h <= rect->bottom; h++)
  550. {
  551. rt_memcpy(dst, src, rectWidth);
  552. src += rectWidth;
  553. dst += imageWidth; /* Next line */
  554. }
  555. }
  556. else
  557. {
  558. rtgui_blit_line_func blit_line = RT_NULL;
  559. /* we decompress from top to bottom if the block is beyond the right
  560. * boundary, just continue to next block. However, if the block is
  561. * beyond the bottom boundary, we don't need to decompress the rest. */
  562. if (rect->left > jpeg->dst_w)
  563. return 1;
  564. if (rect->top > jpeg->dst_h)
  565. return 0;
  566. w = rect->right < jpeg->dst_w ? rect->right : jpeg->dst_w;
  567. w = w - rect->left + 1;
  568. h = rect->bottom < jpeg->dst_h ? rect->bottom : jpeg->dst_h;
  569. h = h - rect->top + 1;
  570. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  571. {
  572. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  573. {
  574. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  575. }
  576. }
  577. else
  578. {
  579. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  580. }
  581. if (blit_line)
  582. {
  583. rt_uint8_t line_buf[TJPGD_MAX_MCU_WIDTH_ON_DISP];
  584. for (y = 0; y < h; y++)
  585. {
  586. blit_line(line_buf, src, w * jpeg->byte_per_pixel);
  587. jpeg->dc->engine->blit_line(jpeg->dc,
  588. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  589. jpeg->dst_y + rect->top + y,
  590. line_buf);
  591. src += rectWidth;
  592. }
  593. }
  594. else
  595. {
  596. for (y = 0; y < h; y++)
  597. {
  598. jpeg->dc->engine->blit_line(jpeg->dc,
  599. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  600. jpeg->dst_y + rect->top + y,
  601. src);
  602. src += rectWidth;
  603. }
  604. }
  605. }
  606. return 1; /* Continue to decompress */
  607. }
  608. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file)
  609. {
  610. rt_bool_t is_JPG;
  611. JDEC tjpgd;
  612. void *pool;
  613. if (!file)
  614. {
  615. return RT_FALSE;
  616. }
  617. is_JPG = RT_FALSE;
  618. do
  619. {
  620. pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  621. if (pool == RT_NULL)
  622. {
  623. rt_kprintf("TJPGD err: no mem\n");
  624. break;
  625. }
  626. if (rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET) == -1)
  627. {
  628. break;
  629. }
  630. if (jd_prepare(&tjpgd, tjpgd_in_func, pool,
  631. TJPGD_WORKING_BUFFER_SIZE, (void *)&file) == JDR_OK)
  632. {
  633. is_JPG = RT_TRUE;
  634. }
  635. rt_kprintf("TJPGD: check OK\n");
  636. }
  637. while (0);
  638. rt_free(pool);
  639. return is_JPG;
  640. }
  641. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  642. {
  643. rt_uint8_t scale = 0;
  644. rt_bool_t res = RT_FALSE;
  645. struct rtgui_image_jpeg *jpeg;
  646. JRESULT ret;
  647. if (scale > TJPGD_MAX_SCALING_FACTOR)
  648. {
  649. return RT_FALSE;
  650. }
  651. do
  652. {
  653. jpeg = (struct rtgui_image_jpeg *)rt_malloc(sizeof(struct rtgui_image_jpeg));
  654. if (jpeg == RT_NULL)
  655. {
  656. break;
  657. }
  658. jpeg->filerw = file;
  659. jpeg->is_loaded = RT_FALSE;
  660. jpeg->to_buffer = load;
  661. jpeg->scale = scale;
  662. #if (JD_FORMAT == 0)
  663. jpeg->byte_per_pixel = 3;
  664. #elif (JD_FORMAT == 1)
  665. jpeg->byte_per_pixel = 2;
  666. #endif
  667. jpeg->pool = RT_NULL;
  668. jpeg->pixels = RT_NULL;
  669. jpeg->pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  670. if (jpeg->pool == RT_NULL)
  671. {
  672. rt_kprintf("TJPGD err: no mem (%d)\n", TJPGD_WORKING_BUFFER_SIZE);
  673. break;
  674. }
  675. if (rtgui_filerw_seek(jpeg->filerw, 0, RTGUI_FILE_SEEK_SET) == -1)
  676. {
  677. break;
  678. }
  679. ret = jd_prepare(&jpeg->tjpgd, tjpgd_in_func, jpeg->pool,
  680. TJPGD_WORKING_BUFFER_SIZE, (void *)jpeg);
  681. if (ret != JDR_OK)
  682. {
  683. if (ret == JDR_FMT3)
  684. {
  685. rt_kprintf("TJPGD: not supported format\n");
  686. }
  687. break;
  688. }
  689. rt_kprintf("TJPGD: prepare OK\n");
  690. image->w = (rt_uint16_t)jpeg->tjpgd.width >> jpeg->scale;
  691. image->h = (rt_uint16_t)jpeg->tjpgd.height >> jpeg->scale;
  692. /* set image private data and engine */
  693. image->data = jpeg;
  694. image->engine = &rtgui_image_jpeg_engine;
  695. if (jpeg->to_buffer == RT_TRUE)
  696. {
  697. jpeg->pixels = (rt_uint8_t *)rtgui_malloc(
  698. jpeg->byte_per_pixel * image->w * image->h);
  699. if (jpeg->pixels == RT_NULL)
  700. {
  701. rt_kprintf("TJPGD err: no mem to load (%d)\n",
  702. jpeg->byte_per_pixel * image->w * image->h);
  703. break;
  704. }
  705. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  706. if (ret != JDR_OK)
  707. {
  708. break;
  709. }
  710. rtgui_filerw_close(jpeg->filerw);
  711. jpeg->is_loaded = RT_TRUE;
  712. rt_kprintf("TJPGD: load to RAM\n");
  713. }
  714. res = RT_TRUE;
  715. }
  716. while (0);
  717. if (!res || jpeg->is_loaded)
  718. {
  719. rt_free(jpeg->pool);
  720. }
  721. if (!res)
  722. {
  723. rtgui_free(jpeg->pixels);
  724. rt_free(jpeg);
  725. }
  726. /* create jpeg image successful */
  727. return res;
  728. }
  729. static void rtgui_image_jpeg_unload(struct rtgui_image *image)
  730. {
  731. if (image != RT_NULL)
  732. {
  733. struct rtgui_image_jpeg *jpeg;
  734. jpeg = (struct rtgui_image_jpeg *) image->data;
  735. RT_ASSERT(jpeg != RT_NULL);
  736. if (jpeg->to_buffer == RT_TRUE)
  737. {
  738. if (jpeg->is_loaded == RT_TRUE)
  739. {
  740. rtgui_free(jpeg->pixels);
  741. }
  742. if (jpeg->is_loaded != RT_TRUE)
  743. {
  744. rtgui_filerw_close(jpeg->filerw);
  745. }
  746. }
  747. else
  748. {
  749. rt_free(jpeg->pool);
  750. rtgui_filerw_close(jpeg->filerw);
  751. }
  752. rt_free(jpeg);
  753. }
  754. rt_kprintf("TJPGD: unload\n");
  755. }
  756. static void rtgui_image_jpeg_blit(struct rtgui_image *image,
  757. struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  758. {
  759. rt_uint16_t w, h, y;
  760. struct rtgui_image_jpeg *jpeg;
  761. jpeg = (struct rtgui_image_jpeg *) image->data;
  762. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL || jpeg != RT_NULL);
  763. do
  764. {
  765. /* this dc is not visible */
  766. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  767. {
  768. break;
  769. }
  770. jpeg->dc = dc;
  771. /* the minimum rect */
  772. if (image->w < rtgui_rect_width(*dst_rect))
  773. {
  774. w = image->w;
  775. }
  776. else
  777. {
  778. w = rtgui_rect_width(*dst_rect);
  779. }
  780. if (image->h < rtgui_rect_height(*dst_rect))
  781. {
  782. h = image->h;
  783. }
  784. else
  785. {
  786. h = rtgui_rect_height(*dst_rect);
  787. }
  788. if (!jpeg->is_loaded)
  789. {
  790. JRESULT ret;
  791. jpeg->dst_x = dst_rect->x1;
  792. jpeg->dst_y = dst_rect->y1;
  793. jpeg->dst_w = w;
  794. jpeg->dst_h = h;
  795. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  796. if (ret != JDR_OK)
  797. {
  798. break;
  799. }
  800. rt_kprintf("TJPGD: load to display\n");
  801. }
  802. else
  803. {
  804. rt_uint8_t *src = jpeg->pixels;
  805. rt_uint16_t imageWidth = image->w * jpeg->byte_per_pixel;
  806. rtgui_blit_line_func blit_line = RT_NULL;
  807. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  808. {
  809. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  810. {
  811. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  812. }
  813. }
  814. else
  815. {
  816. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  817. }
  818. if (blit_line)
  819. {
  820. rt_uint16_t x;
  821. rt_uint8_t temp[4];
  822. for (y = 0; y < h; y++)
  823. {
  824. for (x = 0; x < w; x++)
  825. {
  826. blit_line(temp, src, jpeg->byte_per_pixel);
  827. src += jpeg->byte_per_pixel;
  828. dc->engine->blit_line(dc,
  829. dst_rect->x1 + x, dst_rect->x1 + x,
  830. dst_rect->y1 + y,
  831. temp);
  832. }
  833. }
  834. }
  835. else
  836. {
  837. for (y = 0; y < h; y++)
  838. {
  839. dc->engine->blit_line(dc,
  840. dst_rect->x1, dst_rect->x1 + w,
  841. dst_rect->y1 + y,
  842. src);
  843. src += imageWidth;
  844. }
  845. }
  846. }
  847. }
  848. while (0);
  849. }
  850. #endif /* defined(RTGUI_IMAGE_TJPGD) */
  851. /***************************************************************************//**
  852. * @}
  853. ******************************************************************************/