tjpgd.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*----------------------------------------------------------------------------/
  2. / TJpgDec - Tiny JPEG Decompressor R0.03 include file (C)ChaN, 2021
  3. /----------------------------------------------------------------------------*/
  4. #ifndef DEF_TJPGDEC
  5. #define DEF_TJPGDEC
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "tjpgdcnf.h"
  10. #include <string.h>
  11. // #if defined(_WIN32) /* VC++ or some compiler without stdint.h */
  12. // typedef unsigned char uint8_t;
  13. // typedef unsigned short uint16_t;
  14. // typedef short int16_t;
  15. // typedef unsigned long uint32_t;
  16. // typedef long int32_t;
  17. // #else /* Embedded platform */
  18. #include <stdint.h>
  19. // #endif
  20. #if JD_FASTDECODE >= 1
  21. typedef int16_t jd_yuv_t;
  22. #else
  23. typedef uint8_t jd_yuv_t;
  24. #endif
  25. /* Error code */
  26. typedef enum {
  27. JDR_OK = 0, /* 0: Succeeded */
  28. JDR_INTR, /* 1: Interrupted by output function */
  29. JDR_INP, /* 2: Device error or wrong termination of input stream */
  30. JDR_MEM1, /* 3: Insufficient memory pool for the image */
  31. JDR_MEM2, /* 4: Insufficient stream input buffer */
  32. JDR_PAR, /* 5: Parameter error */
  33. JDR_FMT1, /* 6: Data format error (may be broken data) */
  34. JDR_FMT2, /* 7: Right format but not supported */
  35. JDR_FMT3 /* 8: Not supported JPEG standard */
  36. } JRESULT;
  37. /* Rectangular region in the output image */
  38. typedef struct {
  39. uint16_t left; /* Left end */
  40. uint16_t right; /* Right end */
  41. uint16_t top; /* Top end */
  42. uint16_t bottom; /* Bottom end */
  43. } JRECT;
  44. /* Decompressor object structure */
  45. typedef struct JDEC JDEC;
  46. struct JDEC {
  47. size_t dctr; /* Number of bytes available in the input buffer */
  48. uint8_t* dptr; /* Current data read ptr */
  49. uint8_t* inbuf; /* Bit stream input buffer */
  50. uint8_t dbit; /* Number of bits availavble in wreg or reading bit mask */
  51. uint8_t scale; /* Output scaling ratio */
  52. uint8_t msx, msy; /* MCU size in unit of block (width, height) */
  53. uint8_t qtid[3]; /* Quantization table ID of each component, Y, Cb, Cr */
  54. uint8_t ncomp; /* Number of color components 1:grayscale, 3:color */
  55. int16_t dcv[3]; /* Previous DC element of each component */
  56. uint16_t nrst; /* Restart inverval */
  57. uint16_t width, height; /* Size of the input image (pixel) */
  58. uint8_t* huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */
  59. uint16_t* huffcode[2][2]; /* Huffman code word tables [id][dcac] */
  60. uint8_t* huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */
  61. int32_t* qttbl[4]; /* Dequantizer tables [id] */
  62. #if JD_FASTDECODE >= 1
  63. uint32_t wreg; /* Working shift register */
  64. uint8_t marker; /* Detected marker (0:None) */
  65. #if JD_FASTDECODE == 2
  66. uint8_t longofs[2][2]; /* Table offset of long code [id][dcac] */
  67. uint16_t* hufflut_ac[2]; /* Fast huffman decode tables for AC short code [id] */
  68. uint8_t* hufflut_dc[2]; /* Fast huffman decode tables for DC short code [id] */
  69. #endif
  70. #endif
  71. void* workbuf; /* Working buffer for IDCT and RGB output */
  72. jd_yuv_t* mcubuf; /* Working buffer for the MCU */
  73. void* pool; /* Pointer to available memory pool */
  74. size_t sz_pool; /* Size of momory pool (bytes available) */
  75. size_t (*infunc)(JDEC*, uint8_t*, size_t); /* Pointer to jpeg stream input function */
  76. void* device; /* Pointer to I/O device identifiler for the session */
  77. };
  78. /* TJpgDec API functions */
  79. JRESULT luat_jd_prepare (JDEC* jd, size_t (*infunc)(JDEC*,uint8_t*,size_t), void* pool, size_t sz_pool, void* dev);
  80. JRESULT luat_jd_decomp (JDEC* jd, int (*outfunc)(JDEC*,void*,JRECT*), uint8_t scale);
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif /* _TJPGDEC */