Modify to test email notice.
[asdcplib.git] / src / JP2K.h
1 /*
2 Copyright (c) 2005-2014, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    JP2K.h
28     \version $Id$
29     \brief   JPEG 2000 constants and data structures
30
31     This is not a complete enumeration of all things JP2K.  There is just enough here to
32     support parsing picture metadata from a codestream header.
33 */
34
35 #ifndef _JP2K_H_
36 #define _JP2K_H_
37
38 // AS_DCP.h is included only for it's base type definitions.
39 #include <KM_platform.h>
40 #include <KM_util.h>
41 #include <AS_DCP.h>
42 #include <assert.h>
43
44 namespace ASDCP
45 {
46 namespace JP2K
47 {
48   const byte_t Magic[] = {0xff, 0x4f, 0xff};
49
50   enum Marker_t
51     {
52       MRK_NIL = 0,
53       MRK_SOC = 0xff4f, // Start of codestream
54       MRK_SOT = 0xff90, // Start of tile-part
55       MRK_SOD = 0xff93, // Start of data
56       MRK_EOC = 0xffd9, // End of codestream
57       MRK_SIZ = 0xff51, // Image and tile size
58       MRK_COD = 0xff52, // Coding style default
59       MRK_COC = 0xff53, // Coding style component
60       MRK_RGN = 0xff5e, // Region of interest
61       MRK_QCD = 0xff5c, // Quantization default
62       MRK_QCC = 0xff5d, // Quantization component
63       MRK_POC = 0xff5f, // Progression order change
64       MRK_TLM = 0xff55, // Tile-part lengths
65       MRK_PLM = 0xff57, // Packet length, main header
66       MRK_PLT = 0xff58, // Packet length, tile-part header
67       MRK_PPM = 0xff60, // Packed packet headers, main header
68       MRK_PPT = 0xff61, // Packed packet headers, tile-part header
69       MRK_SOP = 0xff91, // Start of packet
70       MRK_EPH = 0xff92, // End of packet header
71       MRK_CRG = 0xff63, // Component registration
72       MRK_COM = 0xff64, // Comment
73     };
74
75   const char* GetMarkerString(Marker_t m);
76
77   //
78   class Marker
79     {
80       KM_NO_COPY_CONSTRUCT(Marker);
81
82     public:
83       Marker_t m_Type;
84       bool     m_IsSegment;
85       ui32_t   m_DataSize;
86       const byte_t* m_Data;
87
88       Marker() : m_Type(MRK_NIL), m_IsSegment(false), m_DataSize(0), m_Data(0) {}
89       ~Marker() {}
90
91       void Dump(FILE* stream = 0) const;
92     };
93
94   //
95   ASDCP::Result_t GetNextMarker(const byte_t**, Marker&);
96
97   // accessor objects for marker segments
98   namespace Accessor
99     {
100       // image size
101       class SIZ
102         {
103           const byte_t* m_MarkerData;
104           KM_NO_COPY_CONSTRUCT(SIZ);
105           SIZ();
106
107         public:
108           SIZ(const Marker& M)
109             {
110               assert(M.m_Type == MRK_SIZ);
111               m_MarkerData = M.m_Data;
112             }
113
114           ~SIZ() {}
115
116           inline ui16_t Rsize()   const { return KM_i16_BE(*(ui16_t*)m_MarkerData); }
117           inline ui32_t Xsize()   const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 2)); }
118           inline ui32_t Ysize()   const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 6)); }
119           inline ui32_t XOsize()  const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 10)); }
120           inline ui32_t YOsize()  const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 14)); }
121           inline ui32_t XTsize()  const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 18)); }
122           inline ui32_t YTsize()  const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 22)); }
123           inline ui32_t XTOsize() const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 26)); }
124           inline ui32_t YTOsize() const { return KM_i32_BE(*(ui32_t*)(m_MarkerData + 30)); }
125           inline ui16_t Csize()   const { return KM_i16_BE(*(ui16_t*)(m_MarkerData + 34)); }
126           void ReadComponent(const ui32_t index, ImageComponent_t& IC) const;
127           void Dump(FILE* stream = 0) const;
128         };
129
130       const int SGcodOFST = 1;
131       const int SPcodOFST = 5;
132
133       // coding style
134       class COD
135         {
136           const byte_t* m_MarkerData;
137
138           KM_NO_COPY_CONSTRUCT(COD);
139           COD();
140
141         public:
142           COD(const Marker& M)
143             {
144               assert(M.m_Type == MRK_COD);
145               m_MarkerData = M.m_Data;
146             }
147
148           ~COD() {}
149           
150           inline ui8_t  ProgOrder()        const { return *(m_MarkerData + SGcodOFST ); }
151           inline ui16_t Layers()           const { return KM_i16_BE(*(ui16_t*)(m_MarkerData + SGcodOFST + 1));}
152           inline ui8_t  DecompLevels()     const { return *(m_MarkerData + SPcodOFST); }
153           inline ui8_t  CodeBlockWidth()   const { return *(m_MarkerData + SPcodOFST + 1) + 2; }
154           inline ui8_t  CodeBlockHeight()  const { return *(m_MarkerData + SPcodOFST + 2) + 2; }
155           inline ui8_t  CodeBlockStyle()   const { return *(m_MarkerData + SPcodOFST + 3); }
156           inline ui8_t  Transformation()   const { return *(m_MarkerData + SPcodOFST + 4); }
157
158           void Dump(FILE* stream = 0) const;
159         };
160
161       const int SqcdOFST = 1;
162       const int SPqcdOFST = 2;
163
164       enum QuantizationType_t
165       {
166         QT_NONE,
167         QT_DERIVED,
168         QT_EXP
169       };
170
171       const char* GetQuantizationTypeString(const QuantizationType_t m);
172
173       // Quantization default
174       class QCD
175         {
176           const byte_t* m_MarkerData;
177           ui32_t m_DataSize;
178
179           KM_NO_COPY_CONSTRUCT(QCD);
180           QCD();
181
182         public:
183           QCD(const Marker& M)
184             {
185               assert(M.m_Type == MRK_QCD);
186               m_MarkerData = M.m_Data + 2;
187               m_DataSize = M.m_DataSize - 2;
188             }
189
190           ~QCD() {}
191           inline QuantizationType_t QuantizationType() const { return static_cast<QuantizationType_t>(*(m_MarkerData + SqcdOFST) & 0x03); }
192           inline ui8_t  GuardBits() const { return (*(m_MarkerData + SqcdOFST) & 0xe0) >> 5; }
193           void Dump(FILE* stream = 0) const;
194         };
195
196       // a comment
197       class COM
198         {
199           bool          m_IsText;
200           const byte_t* m_MarkerData;
201           ui32_t        m_DataSize;
202
203           KM_NO_COPY_CONSTRUCT(COM);
204           COM();
205
206         public:
207           COM(const Marker& M)
208             {
209               assert(M.m_Type == MRK_COM);
210               m_IsText = M.m_Data[1] == 1;
211               m_MarkerData = M.m_Data + 2;
212               m_DataSize = M.m_DataSize - 2;
213             }
214
215           ~COM() {}
216           
217           inline bool IsText() const { return m_IsText; }
218           inline const byte_t* CommentData() const { return m_MarkerData; }
219           inline ui32_t CommentSize() const { return m_DataSize; }
220           void Dump(FILE* stream = 0) const;
221         };
222     } // namespace Accessor
223 } // namespace JP2K
224 } // namespace ASDCP
225
226 #endif // _JP2K_H_
227
228 //
229 // end JP2K.h
230 //