image_jpg.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. #ifdef RTGUI_DEBUG_TJPGD
  536. /* Put progress indicator */
  537. if (rect->left == 0)
  538. {
  539. rt_kprintf("\r%lu%%", (rect->top << jpeg->scale) * 100UL / jdec->height);
  540. }
  541. #endif
  542. /* Copy the decompressed RGB rectanglar to the frame buffer */
  543. rectWidth = (rect->right - rect->left + 1) * jpeg->byte_per_pixel;
  544. src = (rt_uint8_t *)bitmap;
  545. if (jpeg->to_buffer)
  546. {
  547. rt_uint16_t imageWidth; /* Width of image (bytes) */
  548. imageWidth = (jdec->width >> jdec->scale) * jpeg->byte_per_pixel;
  549. dst = jpeg->pixels + rect->top * imageWidth + rect->left * jpeg->byte_per_pixel;
  550. /* Left-top of destination rectangular */
  551. for (h = rect->top; h <= rect->bottom; h++)
  552. {
  553. rt_memcpy(dst, src, rectWidth);
  554. src += rectWidth;
  555. dst += imageWidth; /* Next line */
  556. }
  557. }
  558. else
  559. {
  560. rtgui_blit_line_func blit_line = RT_NULL;
  561. /* we decompress from top to bottom if the block is beyond the right
  562. * boundary, just continue to next block. However, if the block is
  563. * beyond the bottom boundary, we don't need to decompress the rest. */
  564. if (rect->left > jpeg->dst_w)
  565. return 1;
  566. if (rect->top > jpeg->dst_h)
  567. return 0;
  568. w = rect->right < jpeg->dst_w ? rect->right : jpeg->dst_w;
  569. w = w - rect->left + 1;
  570. h = rect->bottom < jpeg->dst_h ? rect->bottom : jpeg->dst_h;
  571. h = h - rect->top + 1;
  572. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  573. {
  574. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  575. {
  576. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  577. }
  578. }
  579. else
  580. {
  581. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  582. }
  583. if (blit_line)
  584. {
  585. rt_uint8_t line_buf[TJPGD_MAX_MCU_WIDTH_ON_DISP];
  586. for (y = 0; y < h; y++)
  587. {
  588. blit_line(line_buf, src, w * jpeg->byte_per_pixel);
  589. jpeg->dc->engine->blit_line(jpeg->dc,
  590. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  591. jpeg->dst_y + rect->top + y,
  592. line_buf);
  593. src += rectWidth;
  594. }
  595. }
  596. else
  597. {
  598. for (y = 0; y < h; y++)
  599. {
  600. jpeg->dc->engine->blit_line(jpeg->dc,
  601. jpeg->dst_x + rect->left, jpeg->dst_x + rect->left + w,
  602. jpeg->dst_y + rect->top + y,
  603. src);
  604. src += rectWidth;
  605. }
  606. }
  607. }
  608. return 1; /* Continue to decompress */
  609. }
  610. static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw *file)
  611. {
  612. rt_bool_t is_JPG;
  613. JDEC tjpgd;
  614. void *pool;
  615. if (!file)
  616. {
  617. return RT_FALSE;
  618. }
  619. is_JPG = RT_FALSE;
  620. do
  621. {
  622. pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  623. if (pool == RT_NULL)
  624. {
  625. rt_kprintf("TJPGD err: no mem\n");
  626. break;
  627. }
  628. if (rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET) == -1)
  629. {
  630. break;
  631. }
  632. if (jd_prepare(&tjpgd, tjpgd_in_func, pool,
  633. TJPGD_WORKING_BUFFER_SIZE, (void *)&file) == JDR_OK)
  634. {
  635. is_JPG = RT_TRUE;
  636. }
  637. #ifdef RTGUI_DEBUG_TJPGD
  638. rt_kprintf("TJPGD: check OK\n");
  639. #endif
  640. }
  641. while (0);
  642. rt_free(pool);
  643. return is_JPG;
  644. }
  645. static rt_bool_t rtgui_image_jpeg_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  646. {
  647. rt_uint8_t scale = 0;
  648. rt_bool_t res = RT_FALSE;
  649. struct rtgui_image_jpeg *jpeg;
  650. JRESULT ret;
  651. if (scale > TJPGD_MAX_SCALING_FACTOR)
  652. {
  653. return RT_FALSE;
  654. }
  655. do
  656. {
  657. jpeg = (struct rtgui_image_jpeg *)rt_malloc(sizeof(struct rtgui_image_jpeg));
  658. if (jpeg == RT_NULL)
  659. {
  660. break;
  661. }
  662. jpeg->filerw = file;
  663. jpeg->is_loaded = RT_FALSE;
  664. jpeg->to_buffer = load;
  665. jpeg->scale = scale;
  666. #if (JD_FORMAT == 0)
  667. jpeg->byte_per_pixel = 3;
  668. #elif (JD_FORMAT == 1)
  669. jpeg->byte_per_pixel = 2;
  670. #endif
  671. jpeg->pool = RT_NULL;
  672. jpeg->pixels = RT_NULL;
  673. jpeg->pool = rt_malloc(TJPGD_WORKING_BUFFER_SIZE);
  674. if (jpeg->pool == RT_NULL)
  675. {
  676. rt_kprintf("TJPGD err: no mem (%d)\n", TJPGD_WORKING_BUFFER_SIZE);
  677. break;
  678. }
  679. if (rtgui_filerw_seek(jpeg->filerw, 0, RTGUI_FILE_SEEK_SET) == -1)
  680. {
  681. break;
  682. }
  683. ret = jd_prepare(&jpeg->tjpgd, tjpgd_in_func, jpeg->pool,
  684. TJPGD_WORKING_BUFFER_SIZE, (void *)jpeg);
  685. if (ret != JDR_OK)
  686. {
  687. if (ret == JDR_FMT3)
  688. {
  689. rt_kprintf("TJPGD: not supported format\n");
  690. }
  691. break;
  692. }
  693. #ifdef RTGUI_DEBUG_TJPGD
  694. rt_kprintf("TJPGD: prepare OK\n");
  695. #endif
  696. image->w = (rt_uint16_t)jpeg->tjpgd.width >> jpeg->scale;
  697. image->h = (rt_uint16_t)jpeg->tjpgd.height >> jpeg->scale;
  698. /* set image private data and engine */
  699. image->data = jpeg;
  700. image->engine = &rtgui_image_jpeg_engine;
  701. if (jpeg->to_buffer == RT_TRUE)
  702. {
  703. jpeg->pixels = (rt_uint8_t *)rtgui_malloc(
  704. jpeg->byte_per_pixel * image->w * image->h);
  705. if (jpeg->pixels == RT_NULL)
  706. {
  707. rt_kprintf("TJPGD err: no mem to load (%d)\n",
  708. jpeg->byte_per_pixel * image->w * image->h);
  709. break;
  710. }
  711. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  712. if (ret != JDR_OK)
  713. {
  714. break;
  715. }
  716. rtgui_filerw_close(jpeg->filerw);
  717. jpeg->is_loaded = RT_TRUE;
  718. #ifdef RTGUI_DEBUG_TJPGD
  719. rt_kprintf("TJPGD: load to RAM\n");
  720. #endif
  721. }
  722. res = RT_TRUE;
  723. }
  724. while (0);
  725. if (!res || jpeg->is_loaded)
  726. {
  727. rt_free(jpeg->pool);
  728. }
  729. if (!res)
  730. {
  731. rtgui_free(jpeg->pixels);
  732. rt_free(jpeg);
  733. }
  734. /* create jpeg image successful */
  735. return res;
  736. }
  737. static void rtgui_image_jpeg_unload(struct rtgui_image *image)
  738. {
  739. if (image != RT_NULL)
  740. {
  741. struct rtgui_image_jpeg *jpeg;
  742. jpeg = (struct rtgui_image_jpeg *) image->data;
  743. RT_ASSERT(jpeg != RT_NULL);
  744. if (jpeg->to_buffer == RT_TRUE)
  745. {
  746. if (jpeg->is_loaded == RT_TRUE)
  747. {
  748. rtgui_free(jpeg->pixels);
  749. }
  750. if (jpeg->is_loaded != RT_TRUE)
  751. {
  752. rtgui_filerw_close(jpeg->filerw);
  753. }
  754. }
  755. else
  756. {
  757. rt_free(jpeg->pool);
  758. rtgui_filerw_close(jpeg->filerw);
  759. }
  760. rt_free(jpeg);
  761. }
  762. #ifdef RTGUI_DEBUG_TJPGD
  763. rt_kprintf("TJPGD: unload\n");
  764. #endif
  765. }
  766. static void rtgui_image_jpeg_blit(struct rtgui_image *image,
  767. struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  768. {
  769. rt_uint16_t w, h, y;
  770. struct rtgui_image_jpeg *jpeg;
  771. jpeg = (struct rtgui_image_jpeg *) image->data;
  772. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL || jpeg != RT_NULL);
  773. do
  774. {
  775. /* this dc is not visible */
  776. if (rtgui_dc_get_visible(dc) != RT_TRUE)
  777. {
  778. break;
  779. }
  780. jpeg->dc = dc;
  781. /* the minimum rect */
  782. if (image->w < rtgui_rect_width(*dst_rect))
  783. {
  784. w = image->w;
  785. }
  786. else
  787. {
  788. w = rtgui_rect_width(*dst_rect);
  789. }
  790. if (image->h < rtgui_rect_height(*dst_rect))
  791. {
  792. h = image->h;
  793. }
  794. else
  795. {
  796. h = rtgui_rect_height(*dst_rect);
  797. }
  798. if (!jpeg->is_loaded)
  799. {
  800. JRESULT ret;
  801. jpeg->dst_x = dst_rect->x1;
  802. jpeg->dst_y = dst_rect->y1;
  803. jpeg->dst_w = w;
  804. jpeg->dst_h = h;
  805. ret = jd_decomp(&jpeg->tjpgd, tjpgd_out_func, jpeg->scale);
  806. if (ret != JDR_OK)
  807. {
  808. break;
  809. }
  810. #ifdef RTGUI_DEBUG_TJPGD
  811. rt_kprintf("TJPGD: load to display\n");
  812. #endif
  813. }
  814. else
  815. {
  816. rt_uint8_t *src = jpeg->pixels;
  817. rt_uint16_t imageWidth = image->w * jpeg->byte_per_pixel;
  818. rtgui_blit_line_func blit_line = RT_NULL;
  819. if (jpeg->byte_per_pixel == hw_driver->bits_per_pixel / 8)
  820. {
  821. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  822. {
  823. blit_line = rtgui_blit_line_get_inv(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  824. }
  825. }
  826. else
  827. {
  828. blit_line = rtgui_blit_line_get(hw_driver->bits_per_pixel / 8, jpeg->byte_per_pixel);
  829. }
  830. if (blit_line)
  831. {
  832. rt_uint16_t x;
  833. rt_uint8_t temp[4];
  834. for (y = 0; y < h; y++)
  835. {
  836. for (x = 0; x < w; x++)
  837. {
  838. blit_line(temp, src, jpeg->byte_per_pixel);
  839. src += jpeg->byte_per_pixel;
  840. dc->engine->blit_line(dc,
  841. dst_rect->x1 + x, dst_rect->x1 + x,
  842. dst_rect->y1 + y,
  843. temp);
  844. }
  845. }
  846. }
  847. else
  848. {
  849. for (y = 0; y < h; y++)
  850. {
  851. dc->engine->blit_line(dc,
  852. dst_rect->x1, dst_rect->x1 + w,
  853. dst_rect->y1 + y,
  854. src);
  855. src += imageWidth;
  856. }
  857. }
  858. }
  859. }
  860. while (0);
  861. }
  862. #endif /* defined(RTGUI_IMAGE_TJPGD) */
  863. /***************************************************************************//**
  864. * @}
  865. ******************************************************************************/