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 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
36 * Read the information contains in VBAS [JPP/JPT stream message header]
37 * Store information (7 bits) in value
38 * @param p_cio the stream to read from.
39 * @param p_value the data to update
40 * @return the nb of bytes read or -1 if an io error occurred.
42 bool jpt_read_VBAS_info(opj_stream_private_t * p_cio, OPJ_UINT32 * p_nb_bytes_read, OPJ_UINT32 * p_value, opj_event_mgr_t * p_manager)
45 OPJ_UINT32 l_nb_bytes_read = 0;
47 // read data till the MSB of the current byte is 1.
48 // concatenate 7 bits of data, last bit is finish flag
50 // read data from the stream
53 (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1)
55 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n");
60 // is the MSB equal to 1 ?
64 // concatenate 7 bits of data, last bit is finish flag
65 *p_value = (*p_value << 7) | (l_elmt & 0x7f);
67 (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1)
69 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n");
74 // concatenate 7 bits of data, last bit is finish flag
75 *p_value = (*p_value << 7) | (l_elmt & 0x7f);
76 * p_nb_bytes_read = l_nb_bytes_read;
81 * Initialize the value of the message header structure
84 void jpt_init_msg_header(opj_jpt_msg_header_t * header)
86 header->Id = 0; /* In-class Identifier */
87 header->last_byte = 0; /* Last byte information */
88 header->Class_Id = 0; /* Class Identifier */
89 header->CSn_Id = 0; /* CSn : index identifier */
90 header->Msg_offset = 0; /* Message offset */
91 header->Msg_length = 0; /* Message length */
92 header->Layer_nb = 0; /* Auxiliary for JPP case */
96 * Re-initialize the value of the message header structure
98 * Only parameters always present in message header
101 void jpt_reinit_msg_header(opj_jpt_msg_header_t * header)
103 header->Id = 0; /* In-class Identifier */
104 header->last_byte = 0; /* Last byte information */
105 header->Msg_offset = 0; /* Message offset */
106 header->Msg_length = 0; /* Message length */
110 * Read the message header for a JPP/JPT - stream
113 bool jpt_read_msg_header(opj_stream_private_t *cio, opj_jpt_msg_header_t *header, OPJ_UINT32 * p_nb_bytes_read, opj_event_mgr_t * p_manager)
115 OPJ_BYTE elmt, Class = 0, CSn = 0;
116 OPJ_UINT32 l_nb_bytes_read = 0;
117 OPJ_UINT32 l_last_nb_bytes_read;
120 jpt_reinit_msg_header(header);
126 (opj_stream_read_data(cio,&elmt,1,p_manager) != 1)
128 opj_event_msg(p_manager, EVT_ERROR, "Forbidden value encounter in message header !!\n");
133 /* See for Class and CSn */
134 switch ((elmt >> 5) & 0x03)
137 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
155 /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */
157 (((elmt >> 4) & 0x01) == 1)
159 header->last_byte = 1;
162 /* In-class identifier */
163 header->Id |= (elmt & 0x0f);
167 l_last_nb_bytes_read = 0;
169 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Id), p_manager))
171 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
174 l_nb_bytes_read += l_last_nb_bytes_read;
182 header->Class_Id = 0;
183 l_last_nb_bytes_read = 0;
185 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Class_Id), p_manager))
187 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
190 l_nb_bytes_read += l_last_nb_bytes_read;
199 l_last_nb_bytes_read = 0;
201 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->CSn_Id), p_manager))
203 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
206 l_nb_bytes_read += l_last_nb_bytes_read;
209 /* ----------------- */
210 /* VBAS : Msg_offset */
211 /* ----------------- */
212 l_last_nb_bytes_read = 0;
214 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_offset), p_manager))
216 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
219 l_nb_bytes_read += l_last_nb_bytes_read;
221 /* ----------------- */
222 /* VBAS : Msg_length */
223 /* ----------------- */
224 l_last_nb_bytes_read = 0;
226 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_length), p_manager))
228 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
231 l_nb_bytes_read += l_last_nb_bytes_read;
236 if ((header->Class_Id & 0x01) == 1)
238 header->Layer_nb = 0;
240 (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Layer_nb), p_manager))
242 opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n");
245 l_nb_bytes_read += l_last_nb_bytes_read;
247 * p_nb_bytes_read = l_nb_bytes_read;