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