tjpgd_example.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int 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. int 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)
  62. {
  63. rt_kprintf("Jpeg_Dec illegal arguments ...\n");
  64. return -1;
  65. }
  66. devid.fp = fopen(argv[1], "rb");
  67. if (!devid.fp)
  68. {
  69. rt_kprintf("Jpeg_Dec open the file failed...\n");
  70. return -1;
  71. }
  72. /* Allocate a work area for TJpgDec */
  73. work = rt_malloc(3100);
  74. if(work == RT_NULL)
  75. {
  76. rt_kprintf("Jpeg_Dec work malloc failed...\n");
  77. res = -1;
  78. goto __exit;
  79. }
  80. /* Prepare to decompress */
  81. res = jd_prepare(&jdec, in_func, work, 3100, &devid);
  82. if (res == JDR_OK)
  83. {
  84. /* Ready to dcompress. Image info is available here. */
  85. rt_kprintf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
  86. devid.fbuf = rt_malloc(3 * jdec.width * jdec.height); /* Frame buffer for output image (assuming RGB888 cfg) */
  87. if(devid.fbuf == RT_NULL)
  88. {
  89. rt_kprintf("Jpeg_Dec devid.fbuf malloc failed, need to use %d Bytes ...\n", 3 * jdec.width * jdec.height);
  90. res = -1;
  91. goto __exit;
  92. }
  93. devid.wfbuf = jdec.width;
  94. res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */
  95. if (res == JDR_OK) {
  96. /* Decompression succeeded. You have the decompressed image in the frame buffer here. */
  97. rt_kprintf("\rOK \n");
  98. }
  99. else
  100. {
  101. rt_kprintf("Failed to decompress: rc=%d\n", res);
  102. }
  103. if(devid.fbuf != RT_NULL)
  104. {
  105. rt_free(devid.fbuf); /* Discard frame buffer */
  106. }
  107. }
  108. else
  109. {
  110. rt_kprintf("Failed to prepare: rc=%d\n", res);
  111. }
  112. __exit:
  113. if(work != RT_NULL)
  114. {
  115. rt_free(work); /* Discard work area */
  116. }
  117. fclose(devid.fp); /* Close the JPEG file */
  118. return res;
  119. }
  120. MSH_CMD_EXPORT(Jpeg_Dec, Jpeg Decode Test);