1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*-----------------------------------------------------------------------------/
8 / TJpgDec - Tiny JPEG Decompressor R0.01b                     (C)ChaN, 2012
9 /-----------------------------------------------------------------------------/
10 / The TJpgDec is a generic JPEG decompressor module for tiny embedded systems.
11 / This is a free software that opened for education, research and commercial
12 /  developments under license policy of following terms.
13 /
14 /  Copyright (C) 2012, ChaN, all right reserved.
15 /
16 / * The TJpgDec module is a free software and there is NO WARRANTY.
17 / * No restriction on use. You can use, modify and redistribute it for
18 /   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
19 / * Redistributions of source code must retain the above copyright notice.
20 /
21 /-----------------------------------------------------------------------------*/
22 
23 #pragma once
24 
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* Error code */
32 typedef enum {
33     JDR_OK = 0, /* 0: Succeeded */
34     JDR_INTR,   /* 1: Interrupted by output function */
35     JDR_INP,    /* 2: Device error or wrong termination of input stream */
36     JDR_MEM1,   /* 3: Insufficient memory pool for the image */
37     JDR_MEM2,   /* 4: Insufficient stream input buffer */
38     JDR_PAR,    /* 5: Parameter error */
39     JDR_FMT1,   /* 6: Data format error (may be damaged data) */
40     JDR_FMT2,   /* 7: Right format but not supported */
41     JDR_FMT3    /* 8: Not supported JPEG standard */
42 } esp_rom_tjpgd_result_t;
43 
44 /* Rectangular structure */
45 typedef struct {
46     uint16_t left;  /* Left end */
47     uint16_t right; /* Right end */
48     uint16_t top;   /* Top end */
49     uint16_t bottom;/* Bottom end */
50 } esp_rom_tjpgd_rect_t;
51 
52 typedef struct JDEC_s esp_rom_tjpgd_dec_t;
53 
54 /**
55  * @brief Type of user defined input function to read data from input stream
56  * @param dec Specifies the decompression object of the decompression session
57  * @param buffer Specifies the pointer to the read buffer to store the read data. A NULL specifies to remove the data from input stream
58  * @param ndata Specifies number of bytes to read/remove from the input stream
59  *
60  * @return number of bytes read/removed. When a zero is returned, the esp_rom_tjpgd_prepare and esp_rom_tjpgd_decomp function aborts with JDR_INP
61  */
62 typedef uint32_t (*esp_rom_tjpgd_input_function_t)(esp_rom_tjpgd_dec_t *dec, uint8_t *buffer, uint32_t ndata);
63 
64 /**
65  * @brief User defined output function to write decompressed pixels to the output device
66  *
67  * This function is the data output interface of the TJpgDec module.
68  * The corresponding decompression session can be identified by the pointer to the device identifier jdec->device passed to the 5th argument of jd_prepare function.
69  * The bitmap is sent to the frame buffer or display device in this function.
70  * The first pixel in the bitmap is the left-top of the rectangular, the second one is next right and last pixel is the bottom-right of the rectangular.
71  * The size of rectangular varies from 1x1 to 16x16 depends on clipping, scaling and sampling factor of the image.
72  * If the rectangular is out of the frame buffer, it should be clipped in this function.
73  *
74  * The pixel format is currently configured to RGB888
75  *
76  * @param dec Specifies the decompression object of the decompression session
77  * @param bitmap Specifies the RGB bitmap to be output
78  * @param rect Specifies rectangular region in the image to output the RGB bitmap
79  *
80  * @return Normally returns 1. It lets TJpgDec to continue the decompressing process.
81  *         When a 0 is returned, the esp_rom_tjpgd_decomp function aborts with JDR_INTR.
82  *         This is useful to interrupt the decompression process
83  */
84 typedef uint32_t (*esp_rom_tjpgd_output_function_t)(esp_rom_tjpgd_dec_t *dec, void *bitmap, esp_rom_tjpgd_rect_t *rect);
85 
86 struct JDEC_s {
87     uint32_t dctr;              /* Number of bytes available in the input buffer */
88     uint8_t *dptr;              /* Current data read ptr */
89     uint8_t *inbuf;             /* Bit stream input buffer */
90     uint8_t dmsk;               /* Current bit in the current read byte */
91     uint8_t scale;              /* Output scaling ratio */
92     uint8_t msx, msy;           /* MCU size in unit of block (width, height) */
93     uint8_t qtid[3];            /* Quantization table ID of each component */
94     int16_t dcv[3];             /* Previous DC element of each component */
95     uint16_t nrst;              /* Restart inverval */
96     uint32_t width, height;     /* Size of the input image (pixel) */
97     uint8_t *huffbits[2][2];    /* Huffman bit distribution tables [id][dcac] */
98     uint16_t *huffcode[2][2];   /* Huffman code word tables [id][dcac] */
99     uint8_t *huffdata[2][2];    /* Huffman decoded data tables [id][dcac] */
100     int32_t *qttbl[4];          /* Dequaitizer tables [id] */
101     void *workbuf;              /* Working buffer for IDCT and RGB output */
102     uint8_t *mcubuf;            /* Working buffer for the MCU */
103     void *pool;                 /* Pointer to available memory pool */
104     uint32_t sz_pool;           /* Size of momory pool (bytes available) */
105     esp_rom_tjpgd_input_function_t infunc; /* Pointer to jpeg stream input function */
106     void *device;               /* Pointer to I/O device identifiler for the session */
107 };
108 
109 /* TJpgDec API functions */
110 
111 /**
112  * @brief Analyzes the JPEG data and create a decompression object for subsequent decompression process.
113  * @param dec Specifies the decompression object to be initialized. The decompression object is used for subsequent decompression process.
114  * @param infunc Specifies the user defined data input function.
115  * @param work Specifies pointer to the work area for this session. It should be aligned to word boundary or it can result an exception.
116  * @param sz_work Specifies size of the work area in unit of byte.
117  *                TJpgDec requires upto 3092 bytes of work area depends on the built-in parameter tables of the JPEG image.
118  *                Thus 3092 bytes of work area is sufficient for most case.
119  * @param dev Specifies pointer to the user defined device identifier for this session.
120  *            It is stored to the member device in the decompression object. It can be referred by I/O functions to identify the current session.
121  *            When I/O device is fixed in the project or this feature is not needed, set NULL and do not care about this.
122  *
123  * @return
124  *      - JDR_OK Function succeeded and decompression object is valid.
125  *      - JDR_INP An error occurred in input function due to hard error or wrong stream termination.
126  *      - JDR_MEM1 Insufficient work area for this JPEG image.
127  *      - JDR_MEM2 Insufficient input buffer for this JPEG image. JD_SZBUF may be too small.
128  *      - JDR_PAR Parameter error. Given pointer to the work area is NULL.
129  *      - JDR_FMT1 Data format error. The JPEG data can be collapsed.
130  *      - JDR_FMT2 Right format but not supported. May be a grayscale image.
131  *      - JDR_FMT3 Not supported JPEG standard. May be a progressive JPEG image.
132  */
133 esp_rom_tjpgd_result_t esp_rom_tjpgd_prepare(esp_rom_tjpgd_dec_t *dec, esp_rom_tjpgd_input_function_t infunc, void *work, uint32_t sz_work, void *dev);
134 
135 /**
136  * @brief Decompress the JPEG image and output it as RGB data.
137  * @param dec Specifies the valid decompressor object.
138  * @param outfunc Specifies the user defined data output function. The esp_rom_tjpgd_decomp function calls this function to output the decompressed JPEG image in RGB form.
139  * @param scale Specifies scaling factor N for output. The output image is descaled to 1 / 2 ^ N (N = 0 to 3).
140  *
141  * @return
142  *      - JDR_OK Function succeeded.
143  *      - JDR_INTR The decompression process is interrupted by output function.
144  *      - JDR_INP An error occured in input function due to hard error or wrong stream termination.
145  *      - JDR_PAR Parameter error. Given scale factor is invalid.
146  *      - JDR_FMT1 Data format error. The JPEG data can be collapted.
147  */
148 esp_rom_tjpgd_result_t esp_rom_tjpgd_decomp(esp_rom_tjpgd_dec_t *dec, esp_rom_tjpgd_output_function_t outfunc, uint8_t scale);
149 
150 #ifdef __cplusplus
151 }
152 #endif
153