image_jpg.c 29 KB

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