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