1 #pragma once
2 
3 #include "Stream.h"
4 #include "BitStream.h"
5 #include "Bitmap.h"
6 #include "idct/Interface.h"
7 
8 namespace Framework
9 {
10 	class CJPEG
11 	{
12 	public:
13 		static CBitmap		ReadBitmap(CStream&);
14 
15 		enum JERROR
16 		{
17 			JERROR_SUCCESS				= 0,
18 			JERROR_QTDEST_INVALID		= 1,
19 			JERROR_QT16BITS				= 2,
20 			JERROR_HTDEST_INVALID		= 3,
21 			JERROR_NOTBASELINE			= 4,
22 			JERROR_NOT3CHANNELS			= 5,
23 			JERROR_UNKNMARKER			= 6,
24 			JERROR_UNSUPPORTED_YSCALING	= 7,
25 		};
26 
27 	private:
28 
29 		struct QUANTIZATIONTABLE
30 		{
31 			uint8					nPqTq;
32 			uint8*					pQ;
33 		};
34 
35 		struct FRAMECOMPONENT
36 		{
37 			uint8					nC;
38 			uint8					nHV;
39 			uint8					nTq;
40 		};
41 
42 		struct FRAME
43 		{
44 			uint16					nType;
45 			uint8					nPrecision;
46 			uint16					nCY;
47 			uint16					nCX;
48 			uint8					nComponents;
49 			FRAMECOMPONENT*			pCompoment;
50 		};
51 
52 		struct SCANCOMPONENT
53 		{
54 			unsigned char			nCs;
55 			unsigned char			nTdTa;
56 		};
57 
58 		struct SCAN
59 		{
60 			unsigned char			nComponents;
61 			SCANCOMPONENT*			pComponent;
62 			unsigned char			nStartSpectral;
63 			unsigned char			nEndSpectral;
64 			unsigned char			nAhAl;
65 		};
66 
67 		struct HUFFMANTABLE
68 		{
69 			uint8					nTcTh;
70 			uint8					nL[16];
71 			uint8*					pV[16];
72 			uint16*					nCodeTable;
73 			int16					nMin[16];
74 			int16					nMax[16];
75 		};
76 									CJPEG(Framework::CStream&);
77 									~CJPEG();
78 
79 		uint8						HuffGetBit();
80 		uint8						HuffDecode(HUFFMANTABLE*);
81 		uint32						HuffReceive(unsigned int);
82 		uint32						HuffExtend(uint32, unsigned int);
83 		void						HuffGenerateSubTables(HUFFMANTABLE*);
84 		uint8*						HuffGenerateSizeTable(HUFFMANTABLE*);
85 		uint16*						HuffGenerateCodeTable(uint8*);
86 		void						HuffGenerateMinMaxTables(HUFFMANTABLE*);
87 
88 		unsigned int				Decode();
89 		unsigned int				DecodeMCU(int, uint8*, int*);
90 		void						Draw8x8Block(unsigned int, unsigned int, uint8*, uint8*, uint8*, uint8*);
91 		uint8						FixRange(double);
92 
93 		Framework::CBitmap			Process();
94 		unsigned int				ProcessSOF0();
95 		unsigned int				ProcessDHT();
96 		unsigned int				ProcessSOS();
97 		unsigned int				ProcessDQT();
98 		void						ProcessAPPx();
99 		void						ProcessCOM();
100 
101 		QUANTIZATIONTABLE			m_Qt[4];
102 		FRAME						m_Frame;
103 		SCAN						m_Scan;
104 		HUFFMANTABLE				m_DCHt[4];
105 		HUFFMANTABLE				m_ACHt[4];
106 
107 		//Static members
108 		static IDCT::CInterface*	m_pIDCT;
109 		static unsigned char		m_nZigZag[64];
110 
111 		bool						m_nHuffSkipNext;
112 
113 		Framework::CBitStream*		m_stream;
114 		Framework::CBitmap			m_bitmap;
115 	};
116 }
117