metadata reformed...
[asdcplib.git] / src / JP2K.h
1 /*
2 Copyright (c) 2005, 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 <AS_DCP_system.h>
40 #include <hex_utils.h>
41 #include <assert.h>
42
43 namespace ASDCP
44 {
45 namespace JP2K
46 {
47   const byte_t Magic[] = {0xff, 0x4f, 0xff};
48
49   enum Marker_t
50     {
51       MRK_NIL = 0,
52       MRK_SOC = 0xff4f, // Start of codestream
53       MRK_SOT = 0xff90, // Start of tile-part
54       MRK_SOD = 0xff93, // Start of data
55       MRK_EOC = 0xffd9, // End of codestream
56       MRK_SIZ = 0xff51, // Image and tile size
57       MRK_COD = 0xff52, // Coding style default
58       MRK_COC = 0xff53, // Coding style component
59       MRK_RGN = 0xff5e, // Region of interest
60       MRK_QCD = 0xff5c, // Quantization default
61       MRK_QCC = 0xff5d, // Quantization component
62       MRK_POC = 0xff5f, // Progression order change
63       MRK_TLM = 0xff55, // Tile-part lengths
64       MRK_PLM = 0xff57, // Packet length, main header
65       MRK_PLT = 0xff58, // Packet length, tile-part header
66       MRK_PPM = 0xff60, // Packed packet headers, main header
67       MRK_PPT = 0xff61, // Packed packet headers, tile-part header
68       MRK_SOP = 0xff91, // Start of packet
69       MRK_EPH = 0xff92, // End of packet header
70       MRK_CRG = 0xff63, // Component registration
71       MRK_COM = 0xff64, // Comment
72     };
73
74   const char* GetMarkerString(Marker_t m);
75
76   //
77   class Marker
78     {
79       ASDCP_NO_COPY_CONSTRUCT(Marker);
80
81     public:
82       Marker_t m_Type;
83       bool     m_IsSegment;
84       ui32_t   m_DataSize;
85       const byte_t* m_Data;
86
87       Marker() : m_Type(MRK_NIL), m_IsSegment(false), m_DataSize(0), m_Data(0) {}
88       ~Marker() {}
89
90       void Dump(FILE* stream = 0) const;
91     };
92
93   //
94   ASDCP::Result_t GetNextMarker(const byte_t**, Marker&);
95
96   // accessor objects for marker segments
97   namespace Accessor
98     {
99       // image size
100       class SIZ
101         {
102           const byte_t* m_MarkerData;
103           ASDCP_NO_COPY_CONSTRUCT(SIZ);
104           SIZ();
105
106         public:
107           SIZ(const Marker& M)
108             {
109               assert(M.m_Type == MRK_SIZ);
110               m_MarkerData = M.m_Data;
111             }
112
113           ~SIZ() {}
114
115           inline ui16_t Rsize()   { return ASDCP_i16_BE(*(ui16_t*)m_MarkerData); }
116           inline ui32_t Xsize()   { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 2)); }
117           inline ui32_t Ysize()   { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 6)); }
118           inline ui32_t XOsize()  { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 10)); }
119           inline ui32_t YOsize()  { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 14)); }
120           inline ui32_t XTsize()  { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 18)); }
121           inline ui32_t YTsize()  { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 22)); }
122           inline ui32_t XTOsize() { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 26)); }
123           inline ui32_t YTOsize() { return ASDCP_i32_BE(*(ui32_t*)(m_MarkerData + 30)); }
124           inline ui16_t Csize()   { return ASDCP_i16_BE(*(ui16_t*)(m_MarkerData + 34)); }
125           void ReadComponent(ui32_t index, ImageComponent& IC);
126           void Dump(FILE* stream = 0);
127         };
128
129       // a comment
130       class COM
131         {
132           bool          m_IsText;
133           const byte_t* m_MarkerData;
134           ui32_t        m_DataSize;
135
136           ASDCP_NO_COPY_CONSTRUCT(COM);
137           COM();
138
139         public:
140           COM(const Marker& M)
141             {
142               assert(M.m_Type == MRK_COM);
143               m_IsText = M.m_Data[1] == 1;
144               m_MarkerData = M.m_Data + 2;
145               m_DataSize = M.m_DataSize - 2;
146             }
147
148           ~COM() {}
149           
150           inline bool IsText() { return m_IsText; }
151           inline const byte_t* CommentData() { return m_MarkerData; }
152           inline ui32_t CommentSize() { return m_DataSize; }
153           void Dump(FILE* stream = 0);
154         };
155     } // namespace Accessor
156 } // namespace JP2K
157 } // namespace ASDCP
158
159 #endif // _JP2K_H_
160
161 //
162 // end JP2K.h
163 //