///*----------------------------------------------------------------------------/
/// TJpgDec - Tiny JPEG Decompressor R0.03 include file         (C)ChaN, 2021
///----------------------------------------------------------------------------*/
//#ifndef DEF_TJPGDEC
//#define DEF_TJPGDEC

//#ifdef __cplusplus
//extern "C" {
//#endif

//#include "tjpgdcnf.h"
//#include <string.h>

//#if defined(_WIN32)	/* VC++ or some compiler without stdint.h */
//typedef unsigned char	uint8_t;
//typedef unsigned short	uint16_t;
//typedef short			int16_t;
//typedef unsigned long	uint32_t;
//typedef long			int32_t;
//#else				/* Embedded platform */
//#include <stdint.h>
//#endif

//#if JD_FASTDECODE >= 1
//typedef int16_t jd_yuv_t;
//#else
//typedef uint8_t jd_yuv_t;
//#endif


///* Error code */
//typedef enum {
//	JDR_OK = 0,	/* 0: Succeeded */
//	JDR_INTR,	/* 1: Interrupted by output function */	
//	JDR_INP,	/* 2: Device error or wrong termination of input stream */
//	JDR_MEM1,	/* 3: Insufficient memory pool for the image */
//	JDR_MEM2,	/* 4: Insufficient stream input buffer */
//	JDR_PAR,	/* 5: Parameter error */
//	JDR_FMT1,	/* 6: Data format error (may be broken data) */
//	JDR_FMT2,	/* 7: Right format but not supported */
//	JDR_FMT3	/* 8: Not supported JPEG standard */
//} JRESULT;



///* Rectangular region in the output image */
//typedef struct {
//	uint16_t left;		/* Left end */
//	uint16_t right;		/* Right end */
//	uint16_t top;		/* Top end */
//	uint16_t bottom;	/* Bottom end */
//} JRECT;



///* Decompressor object structure */
//typedef struct JDEC JDEC;
//struct JDEC {
//	size_t dctr;				/* Number of bytes available in the input buffer */
//	uint8_t* dptr;				/* Current data read ptr */
//	uint8_t* inbuf;				/* Bit stream input buffer */
//	uint8_t dbit;				/* Number of bits availavble in wreg or reading bit mask */
//	uint8_t scale;				/* Output scaling ratio */
//	uint8_t msx, msy;			/* MCU size in unit of block (width, height) */
//	uint8_t qtid[3];			/* Quantization table ID of each component, Y, Cb, Cr */
//	uint8_t ncomp;				/* Number of color components 1:grayscale, 3:color */
//	int16_t dcv[3];				/* Previous DC element of each component */
//	uint16_t nrst;				/* Restart inverval */
//	uint16_t width, height;		/* Size of the input image (pixel) */
//	uint8_t* huffbits[2][2];	/* Huffman bit distribution tables [id][dcac] */
//	uint16_t* huffcode[2][2];	/* Huffman code word tables [id][dcac] */
//	uint8_t* huffdata[2][2];	/* Huffman decoded data tables [id][dcac] */
//	int32_t* qttbl[4];			/* Dequantizer tables [id] */
//#if JD_FASTDECODE >= 1
//	uint32_t wreg;				/* Working shift register */
//	uint8_t marker;				/* Detected marker (0:None) */
//#if JD_FASTDECODE == 2
//	uint8_t longofs[2][2];		/* Table offset of long code [id][dcac] */
//	uint16_t* hufflut_ac[2];	/* Fast huffman decode tables for AC short code [id] */
//	uint8_t* hufflut_dc[2];		/* Fast huffman decode tables for DC short code [id] */
//#endif
//#endif
//	void* workbuf;				/* Working buffer for IDCT and RGB output */
//	jd_yuv_t* mcubuf;			/* Working buffer for the MCU */
//	void* pool;					/* Pointer to available memory pool */
//	size_t sz_pool;				/* Size of momory pool (bytes available) */
//	size_t (*infunc)(JDEC*, uint8_t*, size_t);	/* Pointer to jpeg stream input function */
//	void* device;				/* Pointer to I/O device identifiler for the session */
//};



///* TJpgDec API functions */
//JRESULT jd_prepare (JDEC* jd, size_t (*infunc)(JDEC*,uint8_t*,size_t), void* pool, size_t sz_pool, void* dev);
//JRESULT jd_decomp (JDEC* jd, int (*outfunc)(JDEC*,void*,JRECT*), uint8_t scale);


//#include "type.h"
//#include "ff.h"

//extern void load_jpg (
//	FIL* fp,		
//	u8* dis_addr,
//	u16 sreen_width,
//	u16 x0,
//	u16 y0,
//	u16* pic_width,
//	u16* pic_height
//);

//#ifdef __cplusplus
//}
//#endif

//#endif /* _TJPGDEC */


/*----------------------------------------------------------------------------/
/ TJpgDec - Tiny JPEG Decompressor include file               (C)ChaN, 2012
/----------------------------------------------------------------------------*/
#ifndef _TJPGDEC
#define _TJPGDEC
/*---------------------------------------------------------------------------*/
/* System Configurations */
//////////////////////////////////////////用户配置区//////////////////////////////////


#define	JD_SZBUF			2048	/* Size of stream input buffer */
#define JD_FORMAT			1		/* Output pixel format 0:RGB888 (3 BYTE/pix), 1:RGB565 (1 WORD/pix) */
#define	JD_USE_SCALE		1		/* Use descaling feature for output */
#define JD_TBLCLIP			1		/* Use table for saturation (might be a bit faster but increases 1K bytes of code size) */


#define JPEG_USE_MALLOC		0 		//定义是否使用malloc,这里我们选择使用malloc
#define JPEG_WBUF_SIZE  	1024*5 	//定义工作区数组大小,最少应不小于3092字节.	

//////////////////////////////////////////////END/////////////////////////////////////
/*---------------------------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif

#include "sys.h"
#include "bsp.h"

extern uint16_t showpic_xsize;
extern uint16_t showpic_ysize;
extern u16 jpg_x;
extern u16 jpg_y;
extern u16 offset_x;

/* This type MUST be 8-bit */
typedef unsigned char	BYTE;

/* These types MUST be 16-bit */
typedef short			SHORT;
typedef unsigned short	WORD;
typedef unsigned short	WCHAR;

/* These types MUST be 16-bit or 32-bit */
typedef int				INT;
typedef unsigned int	UINT;

/* These types MUST be 32-bit */
typedef long			LONG;
typedef unsigned long	DWORD;
	
	
/* Error code */
typedef enum {
	JDR_OK = 0,	/* 0: Succeeded */
	JDR_INTR,	/* 1: Interrupted by output function */	
	JDR_INP,	/* 2: Device error or wrong termination of input stream */
	JDR_MEM1,	/* 3: Insufficient memory pool for the image */
	JDR_MEM2,	/* 4: Insufficient stream input buffer */
	JDR_PAR,	/* 5: Parameter error */
	JDR_FMT1,	/* 6: Data format error (may be damaged data) */
	JDR_FMT2,	/* 7: Right format but not supported */
	JDR_FMT3	/* 8: Not supported JPEG standard */
} JRESULT;



/* Rectangular structure */
typedef struct {
	WORD left, right, top, bottom;
} JRECT;



/* Decompressor object structure */
typedef struct JDEC JDEC;
struct JDEC {
	UINT dctr;				/* Number of bytes available in the input buffer */
	BYTE* dptr;				/* Current data read ptr */
	BYTE* inbuf;			/* Bit stream input buffer */
	BYTE dmsk;				/* Current bit in the current read byte */
	BYTE scale;				/* Output scaling ratio */
	BYTE msx, msy;			/* MCU size in unit of block (width, height) */
	BYTE qtid[3];			/* Quantization table ID of each component */
	SHORT dcv[3];			/* Previous DC element of each component */
	WORD nrst;				/* Restart inverval */
	UINT width, height;		/* Size of the input image (pixel) */
	BYTE* huffbits[2][2];	/* Huffman bit distribution tables [id][dcac] */
	WORD* huffcode[2][2];	/* Huffman code word tables [id][dcac] */
	BYTE* huffdata[2][2];	/* Huffman decoded data tables [id][dcac] */
	LONG* qttbl[4];			/* Dequaitizer tables [id] */
	void* workbuf;			/* Working buffer for IDCT and RGB output */
	BYTE* mcubuf;			/* Working buffer for the MCU */
	void* pool;				/* Pointer to available memory pool */
	UINT sz_pool;			/* Size of momory pool (bytes available) */
	UINT (*infunc)(JDEC*, BYTE*, UINT);/* Pointer to jpeg stream input function */
	void* device;			/* Pointer to I/O device identifiler for the session */
};



/* TJpgDec API functions */
JRESULT jd_prepare (JDEC*, UINT(*)(JDEC*,BYTE*,UINT), void*, UINT, void*);
JRESULT jd_decomp (JDEC*, UINT(*)(JDEC*,void*,JRECT*), BYTE);
uint32_t jpg_decode_onFlash(uint32_t addr,uint8_t scale,uint16_t x);

#ifdef __cplusplus
}
#endif

#endif /* _TJPGDEC */

