tjpgd_example.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*------------------------------------------------*/
  2. /* TJpgDec Quick Evaluation Program for PCs */
  3. /*------------------------------------------------*/
  4. #include <rtthread.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "tjpgd.h"
  8. /* User defined device identifier */
  9. typedef struct {
  10. FILE *fp; /* File pointer for input function */
  11. uint8_t *fbuf; /* Pointer to the frame buffer for output function */
  12. uint16_t wfbuf; /* Width of the frame buffer [pix] */
  13. } IODEV;
  14. /*------------------------------*/
  15. /* User defined input funciton */
  16. /*------------------------------*/
  17. uint16_t in_func (JDEC* jd, uint8_t* buff, uint16_t nbyte)
  18. {
  19. IODEV *dev = (IODEV*)jd->device; /* Device identifier for the session (5th argument of jd_prepare function) */
  20. if (buff) {
  21. /* Read bytes from input stream */
  22. return (uint16_t)fread(buff, 1, nbyte, dev->fp);
  23. } else {
  24. /* Remove bytes from input stream */
  25. return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte;
  26. }
  27. }
  28. /*------------------------------*/
  29. /* User defined output funciton */
  30. /*------------------------------*/
  31. uint16_t out_func (JDEC* jd, void* bitmap, JRECT* rect)
  32. {
  33. IODEV *dev = (IODEV*)jd->device;
  34. uint8_t *src, *dst;
  35. uint16_t y, bws, bwd;
  36. /* Put progress indicator */
  37. if (rect->left == 0) {
  38. rt_kprintf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height);
  39. }
  40. /* Copy the decompressed RGB rectanglar to the frame buffer (assuming RGB888 cfg) */
  41. src = (uint8_t*)bitmap;
  42. dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); /* Left-top of destination rectangular */
  43. bws = 3 * (rect->right - rect->left + 1); /* Width of source rectangular [byte] */
  44. bwd = 3 * dev->wfbuf; /* Width of frame buffer [byte] */
  45. for (y = rect->top; y <= rect->bottom; y++) {
  46. memcpy(dst, src, bws); /* Copy a line */
  47. src += bws; dst += bwd; /* Next line */
  48. }
  49. return 1; /* Continue to decompress */
  50. }
  51. /*------------------------------*/
  52. /* Program Jpeg_Dec */
  53. /*------------------------------*/
  54. int Jpeg_Dec (int argc, char* argv[])
  55. {
  56. void *work; /* Pointer to the decompressor work area */
  57. JDEC jdec; /* Decompression object */
  58. JRESULT res; /* Result code of TJpgDec API */
  59. IODEV devid; /* User defined device identifier */
  60. /* Open a JPEG file */
  61. if (argc < 2) return -1;
  62. devid.fp = fopen(argv[1], "rb");
  63. if (!devid.fp) return -1;
  64. /* Allocate a work area for TJpgDec */
  65. work = rt_malloc(3100);
  66. /* Prepare to decompress */
  67. res = jd_prepare(&jdec, in_func, work, 3100, &devid);
  68. if (res == JDR_OK) {
  69. /* Ready to dcompress. Image info is available here. */
  70. rt_kprintf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
  71. devid.fbuf = rt_malloc(3 * jdec.width * jdec.height); /* Frame buffer for output image (assuming RGB888 cfg) */
  72. devid.wfbuf = jdec.width;
  73. res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */
  74. if (res == JDR_OK) {
  75. /* Decompression succeeded. You have the decompressed image in the frame buffer here. */
  76. rt_kprintf("\rOK \n");
  77. } else {
  78. rt_kprintf("Failed to decompress: rc=%d\n", res);
  79. }
  80. rt_free(devid.fbuf); /* Discard frame buffer */
  81. } else {
  82. rt_kprintf("Failed to prepare: rc=%d\n", res);
  83. }
  84. rt_free(work); /* Discard work area */
  85. fclose(devid.fp); /* Close the JPEG file */
  86. return res;
  87. }
  88. MSH_CMD_EXPORT(Jpeg_Dec, Jpeg Decode Test);