1 #ifndef LJPEG_DECOMPRESSOR_H
2 #define LJPEG_DECOMPRESSOR_H
3 
4 #include "RawDecoder.h"
5 #include "BitPumpMSB.h"
6 /*
7     RawSpeed - RAW file decoder.
8 
9     Copyright (C) 2009 Klaus Post
10 
11     This library is free software; you can redistribute it and/or
12     modify it under the terms of the GNU Lesser General Public
13     License as published by the Free Software Foundation; either
14     version 2 of the License, or (at your option) any later version.
15 
16     This library is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     Lesser General Public License for more details.
20 
21     You should have received a copy of the GNU Lesser General Public
22     License along with this library; if not, write to the Free Software
23     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 
25     http://www.klauspost.com
26 */
27 
28 /*
29  * The following enum and two structs are stolen from the IJG JPEG library
30  * Comments added by tm. See Copyright in LJpegDecompressor.cpp
31  */
32 
33 namespace RawSpeed {
34 
35 typedef enum {		/* JPEG marker codes			*/
36   M_STUFF = 0x00,
37   M_SOF0  = 0xc0,	/* baseline DCT				*/
38   M_SOF1  = 0xc1,	/* extended sequential DCT		*/
39   M_SOF2  = 0xc2,	/* progressive DCT			*/
40   M_SOF3  = 0xc3,	/* lossless (sequential)		*/
41 
42   M_SOF5  = 0xc5,	/* differential sequential DCT		*/
43   M_SOF6  = 0xc6,	/* differential progressive DCT		*/
44   M_SOF7  = 0xc7,	/* differential lossless		*/
45 
46   M_JPG   = 0xc8,	/* JPEG extensions			*/
47   M_SOF9  = 0xc9,	/* extended sequential DCT		*/
48   M_SOF10 = 0xca,	/* progressive DCT			*/
49   M_SOF11 = 0xcb,	/* lossless (sequential)		*/
50 
51   M_SOF13 = 0xcd,	/* differential sequential DCT		*/
52   M_SOF14 = 0xce,	/* differential progressive DCT		*/
53   M_SOF15 = 0xcf,	/* differential lossless		*/
54 
55   M_DHT   = 0xc4,	/* define Huffman tables		*/
56 
57   M_DAC   = 0xcc,	/* define arithmetic conditioning table	*/
58 
59   M_RST0  = 0xd0,	/* restart				*/
60   M_RST1  = 0xd1,	/* restart				*/
61   M_RST2  = 0xd2,	/* restart				*/
62   M_RST3  = 0xd3,	/* restart				*/
63   M_RST4  = 0xd4,	/* restart				*/
64   M_RST5  = 0xd5,	/* restart				*/
65   M_RST6  = 0xd6,	/* restart				*/
66   M_RST7  = 0xd7,	/* restart				*/
67 
68   M_SOI   = 0xd8,	/* start of image			*/
69   M_EOI   = 0xd9,	/* end of image				*/
70   M_SOS   = 0xda,	/* start of scan			*/
71   M_DQT   = 0xdb,	/* define quantization tables		*/
72   M_DNL   = 0xdc,	/* define number of lines		*/
73   M_DRI   = 0xdd,	/* define restart interval		*/
74   M_DHP   = 0xde,	/* define hierarchical progression	*/
75   M_EXP   = 0xdf,	/* expand reference image(s)		*/
76 
77   M_APP0  = 0xe0,	/* application marker, used for JFIF	*/
78   M_APP1  = 0xe1,	/* application marker			*/
79   M_APP2  = 0xe2,	/* application marker			*/
80   M_APP3  = 0xe3,	/* application marker			*/
81   M_APP4  = 0xe4,	/* application marker			*/
82   M_APP5  = 0xe5,	/* application marker			*/
83   M_APP6  = 0xe6,	/* application marker			*/
84   M_APP7  = 0xe7,	/* application marker			*/
85   M_APP8  = 0xe8,	/* application marker			*/
86   M_APP9  = 0xe9,	/* application marker			*/
87   M_APP10 = 0xea,	/* application marker			*/
88   M_APP11 = 0xeb,	/* application marker			*/
89   M_APP12 = 0xec,	/* application marker			*/
90   M_APP13 = 0xed,	/* application marker			*/
91   M_APP14 = 0xee,	/* application marker, used by Adobe	*/
92   M_APP15 = 0xef,	/* application marker			*/
93 
94   M_JPG0  = 0xf0,	/* reserved for JPEG extensions		*/
95   M_JPG13 = 0xfd,	/* reserved for JPEG extensions		*/
96   M_COM   = 0xfe,	/* comment				*/
97 
98   M_TEM   = 0x01,	/* temporary use			*/
99   M_FILL  = 0xFF
100 
101 } JpegMarker;
102 
103 
104 /*
105 * The following structure stores basic information about one component.
106 */
107 typedef struct JpegComponentInfo {
108   /*
109   * These values are fixed over the whole image.
110   * They are read from the SOF marker.
111   */
112   uint32 componentId;		/* identifier for this component (0..255) */
113   uint32 componentIndex;	/* its index in SOF or cPtr->compInfo[]   */
114 
115   /*
116   * Huffman table selector (0..3). The value may vary
117   * between scans. It is read from the SOS marker.
118   */
119   uint32 dcTblNo;
120   uint32 superH; // Horizontal Supersampling
121   uint32 superV; // Vertical Supersampling
122 } JpegComponentInfo;
123 
124 /*
125 * One of the following structures is created for each huffman coding
126 * table.  We use the same structure for encoding and decoding, so there
127 * may be some extra fields for encoding that aren't used in the decoding
128 * and vice-versa.
129 */
130 
131 struct HuffmanTable {
132   /*
133   * These two fields directly represent the contents of a JPEG DHT
134   * marker
135   */
136   uint32 bits[17];
137   uint32 huffval[256];
138 
139   /*
140   * The remaining fields are computed from the above to allow more
141   * efficient coding and decoding.  These fields should be considered
142   * private to the Huffman compression & decompression modules.
143   */
144 
145   ushort16 mincode[17];
146   int maxcode[18];
147   short valptr[17];
148   uint32 numbits[256];
149   int* bigTable;
150   bool initialized;
151 };
152 
153 class SOFInfo {
154 public:
SOFInfo()155   SOFInfo() { w = h = cps = prec = 0; initialized = false;};
~SOFInfo()156   ~SOFInfo() {initialized = false;};
157   uint32 w;    // Width
158   uint32 h;    // Height
159   uint32 cps;  // Components
160   uint32 prec; // Precision
161   JpegComponentInfo compInfo[4];
162   bool initialized;
163 };
164 
165 class LJpegDecompressor
166 {
167 public:
168   LJpegDecompressor(FileMap* file, RawImage img);
169   virtual ~LJpegDecompressor(void);
170   virtual void startDecoder(uint32 offset, uint32 size, uint32 offsetX, uint32 offsetY);
171   virtual void getSOF(SOFInfo* i, uint32 offset, uint32 size);
172   bool mDNGCompatible;  // DNG v1.0.x compatibility
173   bool mUseBigtable;    // Use only for large images
addSlices(vector<int> slices)174   virtual void addSlices(vector<int> slices) {slicesW=slices;};  // CR2 slices.
175 protected:
176   virtual void parseSOF(SOFInfo* i);
177   virtual void parseSOS();
178   virtual void createHuffmanTable(HuffmanTable *htbl);
179   virtual void createBigTable(HuffmanTable *htbl);
decodeScan()180   virtual void decodeScan() {ThrowRDE("LJpegDecompressor: No Scan decoder found");};
181   JpegMarker getNextMarker(bool allowskip);
182   void parseDHT();
183   int HuffDecode(HuffmanTable *htbl);
184   ByteStream* input;
185   BitPumpJPEG* bits;
186   FileMap *mFile;
187   RawImage mRaw;
188 
189   SOFInfo frame;
190   vector<int> slicesW;
191   uint32 pred;
192   uint32 Pt;
193   uint32 offX, offY;  // Offset into image where decoding should start
194   uint32 skipX, skipY;   // Tile is larger than output, skip these border pixels
195   HuffmanTable huff[4];
196 };
197 
198 } // namespace RawSpeed
199 
200 #endif
201