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