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