Possibility to choose to apply MCT (multiple component transform) enabled, and new...
[openjpeg.git] / mj2 / libopenjpeg_097 / jpt.c
1 /*\r
2  * Copyright (c) 2004, Yannick Verschueren\r
3  * Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
4  * All rights reserved.\r
5  *\r
6  * Redistribution and use in source and binary forms, with or without\r
7  * modification, are permitted provided that the following conditions\r
8  * are met:\r
9  * 1. Redistributions of source code must retain the above copyright\r
10  *    notice, this list of conditions and the following disclaimer.\r
11  * 2. Redistributions in binary form must reproduce the above copyright\r
12  *    notice, this list of conditions and the following disclaimer in the\r
13  *    documentation and/or other materials provided with the distribution.\r
14  *\r
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
25  * POSSIBILITY OF SUCH DAMAGE.\r
26  */\r
27 \r
28 #include <stdio.h>\r
29 #include <stdlib.h>\r
30 \r
31 #include "jpt.h"\r
32 #include "j2k.h"\r
33 #include "cio.h"\r
34 \r
35 \r
36 /*\r
37  * Read the information contains in VBAS [JPP/JPT stream message header]\r
38  * Store information (7 bits) in value\r
39  *\r
40  */\r
41 unsigned int jpt_read_VBAS_info(unsigned int value)\r
42 {\r
43   unsigned char elmt;\r
44 \r
45   elmt = cio_read(1);\r
46   while ((elmt >> 7) == 1) {\r
47     value = (value << 7);\r
48     value |= (elmt & 0x7f);\r
49     elmt = cio_read(1);\r
50   }\r
51   value = (value << 7);\r
52   value |= (elmt & 0x7f);\r
53 \r
54   return value;\r
55 }\r
56 \r
57 /*\r
58  * Initialize the value of the message header structure \r
59  *\r
60  */\r
61 void jpt_init_Msg_Header(jpt_msg_header_struct_t * header)\r
62 {\r
63   header->Id = 0;               /* In-class Identifier    */\r
64   header->last_byte = 0;        /* Last byte information  */\r
65   header->Class_Id = 0;         /* Class Identifier       */\r
66   header->CSn_Id = 0;           /* CSn : index identifier */\r
67   header->Msg_offset = 0;       /* Message offset         */\r
68   header->Msg_length = 0;       /* Message length         */\r
69   header->Layer_nb = 0;         /* Auxiliary for JPP case */\r
70 }\r
71 \r
72 /*\r
73  * Re-initialize the value of the message header structure\r
74  *\r
75  * Only parameters always present in message header\r
76  *\r
77  */\r
78 void jpt_reinit_Msg_Header(jpt_msg_header_struct_t * header)\r
79 {\r
80   header->Id = 0;               /* In-class Identifier    */\r
81   header->last_byte = 0;        /* Last byte information  */\r
82   header->Msg_offset = 0;       /* Message offset         */\r
83   header->Msg_length = 0;       /* Message length         */\r
84 }\r
85 \r
86 /*\r
87  * Read the message header for a JPP/JPT - stream\r
88  *\r
89  */\r
90 void jpt_read_Msg_Header(jpt_msg_header_struct_t * header)\r
91 {\r
92   unsigned char elmt, Class = 0, CSn = 0;\r
93   jpt_reinit_Msg_Header(header);\r
94 \r
95   /* ------------- */\r
96   /* VBAS : Bin-ID */\r
97   /* ------------- */\r
98   elmt = cio_read(1);\r
99 \r
100   /* See for Class and CSn */\r
101   switch ((elmt >> 5) & 0x03) {\r
102   case 0:\r
103     fprintf(stderr, "Forbidden value encounter in message header !!\n");\r
104     break;\r
105   case 1:\r
106     Class = 0;\r
107     CSn = 0;\r
108     break;\r
109   case 2:\r
110     Class = 1;\r
111     CSn = 0;\r
112     break;\r
113   case 3:\r
114     Class = 1;\r
115     CSn = 1;\r
116     break;\r
117   default:\r
118     break;\r
119   }\r
120 \r
121   /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */\r
122   if (((elmt >> 4) & 0x01) == 1)\r
123     header->last_byte = 1;\r
124 \r
125   /* In-class identifier */\r
126   header->Id |= (elmt & 0x0f);\r
127   if ((elmt >> 7) == 1)\r
128     header->Id = jpt_read_VBAS_info(header->Id);\r
129 \r
130   /* ------------ */\r
131   /* VBAS : Class */\r
132   /* ------------ */\r
133   if (Class == 1) {\r
134     header->Class_Id = 0;\r
135     header->Class_Id = jpt_read_VBAS_info(header->Class_Id);\r
136   }\r
137 \r
138   /* ---------- */\r
139   /* VBAS : CSn */\r
140   /* ---------- */\r
141   if (CSn == 1) {\r
142     header->CSn_Id = 0;\r
143     header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id);\r
144   }\r
145 \r
146   /* ----------------- */\r
147   /* VBAS : Msg_offset */\r
148   /* ----------------- */\r
149   header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset);\r
150 \r
151   /* ----------------- */\r
152   /* VBAS : Msg_length */\r
153   /* ----------------- */\r
154   header->Msg_length = jpt_read_VBAS_info(header->Msg_length);\r
155 \r
156   /* ---------- */\r
157   /* VBAS : Aux */\r
158   /* ---------- */\r
159   if ((header->Class_Id & 0x01) == 1) {\r
160     header->Layer_nb = 0;\r
161     header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb);\r
162   }\r
163 }\r