decoder.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Copyright 2020 BitBank Software, Inc. All Rights Reserved.
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License.
  12. //===========================================================================
  13. //
  14. #ifndef __JPEGDEC__
  15. #define __JPEGDEC__
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #ifndef PROGMEM
  21. #define memcpy_P memcpy
  22. #define PROGMEM
  23. #endif
  24. //
  25. // JPEG Decoder
  26. // Written by Larry Bank
  27. // Copyright (c) 2020 BitBank Software, Inc.
  28. //
  29. // Designed to decode baseline JPEG images (8 or 24-bpp)
  30. // using less than 22K of RAM
  31. //
  32. /* Defines and variables */
  33. #define FILE_HIGHWATER 1536
  34. #define JPEG_FILE_BUF_SIZE 2048
  35. #define HUFF_TABLEN 273
  36. #define HUFF11SIZE (1<<11)
  37. #define DC_TABLE_SIZE 1024
  38. #define DCTSIZE 64
  39. #define MAX_MCU_COUNT 6
  40. #define MAX_COMPS_IN_SCAN 4
  41. #define MAX_BUFFERED_PIXELS 2048
  42. // Decoder options
  43. #define JPEG_AUTO_ROTATE 1
  44. #define JPEG_SCALE_HALF 2
  45. #define JPEG_SCALE_QUARTER 4
  46. #define JPEG_SCALE_EIGHTH 8
  47. #define JPEG_LE_PIXELS 16
  48. #define JPEG_EXIF_THUMBNAIL 32
  49. #define JPEG_LUMA_ONLY 64
  50. #define MCU0 (DCTSIZE * 0)
  51. #define MCU1 (DCTSIZE * 1)
  52. #define MCU2 (DCTSIZE * 2)
  53. #define MCU3 (DCTSIZE * 3)
  54. #define MCU4 (DCTSIZE * 4)
  55. #define MCU5 (DCTSIZE * 5)
  56. #define DEC_ENON ( 0) // Executing right
  57. #define DEC_EINI (-1) // Error during init.
  58. #define DEC_EDEC (-2) // Error during image decoding
  59. extern uint8_t (*dst_image)[120][160][3];
  60. extern float scale;
  61. extern int32_t zero_point;
  62. // Pixel types (defaults to little endian RGB565)
  63. enum {
  64. RGB565_LITTLE_ENDIAN = 0,
  65. RGB565_BIG_ENDIAN,
  66. EIGHT_BIT_GRAYSCALE,
  67. FOUR_BIT_DITHERED,
  68. TWO_BIT_DITHERED,
  69. ONE_BIT_DITHERED,
  70. INVALID_PIXEL_TYPE
  71. };
  72. enum {
  73. JPEG_MEM_RAM=0,
  74. JPEG_MEM_FLASH
  75. };
  76. // Error codes returned by getLastError()
  77. enum {
  78. JPEG_SUCCESS = 0,
  79. JPEG_INVALID_PARAMETER,
  80. JPEG_DECODE_ERROR,
  81. JPEG_UNSUPPORTED_FEATURE,
  82. JPEG_INVALID_FILE
  83. };
  84. typedef struct buffered_bits
  85. {
  86. unsigned char *pBuf; // buffer pointer
  87. uint32_t ulBits; // buffered bits
  88. uint32_t ulBitOff; // current bit offset
  89. } BUFFERED_BITS;
  90. typedef struct jpeg_file_tag
  91. {
  92. int32_t iPos; // current file position
  93. int32_t iSize; // file size
  94. uint8_t *pData; // memory file pointer
  95. void * fHandle; // class pointer to File/SdFat or whatever you want
  96. } JPEGFILE;
  97. typedef struct jpeg_draw_tag
  98. {
  99. int x, y; // upper left corner of current MCU
  100. int iWidth, iHeight; // size of this MCU
  101. int iBpp; // bit depth of the pixels (8 or 16)
  102. uint16_t *pPixels; // 16-bit pixels
  103. void *pUser;
  104. } JPEGDRAW;
  105. // Callback function prototypes
  106. typedef int32_t (JPEG_READ_CALLBACK)(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen);
  107. typedef int32_t (JPEG_SEEK_CALLBACK)(JPEGFILE *pFile, int32_t iPosition);
  108. typedef int (JPEG_DRAW_CALLBACK)(JPEGDRAW *pDraw);
  109. typedef void * (JPEG_OPEN_CALLBACK)(const char *szFilename, int32_t *pFileSize);
  110. typedef void (JPEG_CLOSE_CALLBACK)(void *pHandle);
  111. /* JPEG color component info */
  112. typedef struct _jpegcompinfo
  113. {
  114. // These values are fixed over the whole image
  115. // For compression, they must be supplied by the user interface
  116. // for decompression, they are read from the SOF marker.
  117. unsigned char component_needed; /* do we need the value of this component? */
  118. unsigned char component_id; /* identifier for this component (0..255) */
  119. unsigned char component_index; /* its index in SOF or cinfo->comp_info[] */
  120. //unsigned char h_samp_factor; /* horizontal sampling factor (1..4) */
  121. //unsigned char v_samp_factor; /* vertical sampling factor (1..4) */
  122. unsigned char quant_tbl_no; /* quantization table selector (0..3) */
  123. // These values may vary between scans
  124. // For compression, they must be supplied by the user interface
  125. // for decompression, they are read from the SOS marker.
  126. unsigned char dc_tbl_no; /* DC entropy table selector (0..3) */
  127. unsigned char ac_tbl_no; /* AC entropy table selector (0..3) */
  128. // These values are computed during compression or decompression startup
  129. //int true_comp_width; /* component's image width in samples */
  130. //int true_comp_height; /* component's image height in samples */
  131. // the above are the logical dimensions of the downsampled image
  132. // These values are computed before starting a scan of the component
  133. //int MCU_width; /* number of blocks per MCU, horizontally */
  134. //int MCU_height; /* number of blocks per MCU, vertically */
  135. //int MCU_blocks; /* MCU_width * MCU_height */
  136. //int downsampled_width; /* image width in samples, after expansion */
  137. //int downsampled_height; /* image height in samples, after expansion */
  138. // the above are the true_comp_xxx values rounded up to multiples of
  139. // the MCU dimensions; these are the working dimensions of the array
  140. // as it is passed through the DCT or IDCT step. NOTE: these values
  141. // differ depending on whether the component is interleaved or not!!
  142. // This flag is used only for decompression. In cases where some of the
  143. // components will be ignored (eg grayscale output from YCbCr image),
  144. // we can skip IDCT etc. computations for the unused components.
  145. } JPEGCOMPINFO;
  146. //
  147. // our private structure to hold a JPEG image decode state
  148. //
  149. typedef struct jpeg_image_tag
  150. {
  151. int iWidth, iHeight; // image size
  152. int iThumbWidth, iThumbHeight; // thumbnail size (if present)
  153. int iThumbData; // offset to image data
  154. int iXOffset, iYOffset; // placement on the display
  155. uint8_t ucBpp, ucSubSample, ucHuffTableUsed;
  156. uint8_t ucMode, ucOrientation, ucHasThumb, b11Bit;
  157. uint8_t ucComponentsInScan, cApproxBitsLow, cApproxBitsHigh;
  158. uint8_t iScanStart, iScanEnd, ucFF, ucNumComponents;
  159. uint8_t ucACTable, ucDCTable, ucMaxACCol, ucMaxACRow;
  160. uint8_t ucMemType, ucPixelType;
  161. int iEXIF; // Offset to EXIF 'TIFF' file
  162. int iError;
  163. int iOptions;
  164. int iVLCOff; // current VLC data offset
  165. int iVLCSize; // current quantity of data in the VLC buffer
  166. int iResInterval, iResCount; // restart interval
  167. int iMaxMCUs; // max MCUs of pixels per JPEGDraw call
  168. JPEG_READ_CALLBACK *pfnRead;
  169. JPEG_SEEK_CALLBACK *pfnSeek;
  170. JPEG_DRAW_CALLBACK *pfnDraw;
  171. JPEG_OPEN_CALLBACK *pfnOpen;
  172. JPEG_CLOSE_CALLBACK *pfnClose;
  173. JPEGCOMPINFO JPCI[MAX_COMPS_IN_SCAN]; /* Max color components */
  174. JPEGFILE JPEGFile;
  175. BUFFERED_BITS bb;
  176. void *pUser;
  177. uint8_t *pDitherBuffer; // provided externally to do Floyd-Steinberg dithering
  178. uint16_t usPixels[MAX_BUFFERED_PIXELS];
  179. int16_t sMCUs[DCTSIZE * MAX_MCU_COUNT]; // 4:2:0 needs 6 DCT blocks per MCU
  180. int16_t sQuantTable[DCTSIZE*4]; // quantization tables
  181. uint8_t ucFileBuf[JPEG_FILE_BUF_SIZE]; // holds temp data and pixel stack
  182. uint8_t ucHuffDC[DC_TABLE_SIZE * 2]; // up to 2 'short' tables
  183. uint16_t usHuffAC[HUFF11SIZE * 2];
  184. } JPEGIMAGE;
  185. #define JPEG_STATIC
  186. int JPEG_openRAM(JPEGIMAGE *pJPEG, uint8_t *pData, int iDataSize, JPEG_DRAW_CALLBACK *pfnDraw);
  187. int JPEG_openFile(JPEGIMAGE *pJPEG, const char *szFilename, JPEG_DRAW_CALLBACK *pfnDraw);
  188. int JPEG_getWidth(JPEGIMAGE *pJPEG);
  189. int JPEG_getHeight(JPEGIMAGE *pJPEG);
  190. int JPEG_decode(JPEGIMAGE *pJPEG, int x, int y, int iOptions);
  191. int JPEG_decodeDither(JPEGIMAGE *pJPEG, uint8_t *pDither, int iOptions);
  192. void JPEG_close(JPEGIMAGE *pJPEG);
  193. int JPEG_getLastError(JPEGIMAGE *pJPEG);
  194. int JPEG_getOrientation(JPEGIMAGE *pJPEG);
  195. int JPEG_getBpp(JPEGIMAGE *pJPEG);
  196. int JPEG_getSubSample(JPEGIMAGE *pJPEG);
  197. int JPEG_hasThumb(JPEGIMAGE *pJPEG);
  198. int JPEG_getThumbWidth(JPEGIMAGE *pJPEG);
  199. int JPEG_getThumbHeight(JPEGIMAGE *pJPEG);
  200. int JPEG_getLastError(JPEGIMAGE *pJPEG);
  201. void JPEG_setPixelType(JPEGIMAGE *pJPEG, int iType); // defaults to little endian
  202. void JPEG_setMaxOutputSize(JPEGIMAGE *pJPEG, int iMaxMCUs);
  203. // Due to unaligned memory causing an exception, we have to do these macros the slow way
  204. #define INTELSHORT(p) ((*p) + (*(p+1)<<8))
  205. #define INTELLONG(p) ((*p) + (*(p+1)<<8) + (*(p+2)<<16) + (*(p+3)<<24))
  206. #define MOTOSHORT(p) (((*(p))<<8) + (*(p+1)))
  207. #define MOTOLONG(p) (((*p)<<24) + ((*(p+1))<<16) + ((*(p+2))<<8) + (*(p+3)))
  208. // Must be a 32-bit target processor
  209. #define REGISTER_WIDTH 32
  210. //void decodeMyImage(uint8_t* pdata, int iDataSize, int x, int y, int iOptions, JPEG_DRAW_CALLBACK *pfnDraw, int);
  211. int32_t decodeMyImage(uint8_t* pdata, int iDataSize, int x, int y, int iOptions, JPEG_DRAW_CALLBACK *pfnDraw, int);
  212. extern int image_available;
  213. #endif // __JPEGDEC__