/src/freeimage-svn/FreeImage/trunk/Source/LibOpenJPEG/j2k.c
Line  | Count  | Source  | 
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) 2001-2003, David Janssens  | 
5  |  |  * Copyright (c) 2002-2003, Yannick Verschueren  | 
6  |  |  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe  | 
7  |  |  * Copyright (c) 2005, Herve Drolon, FreeImage Team  | 
8  |  |  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>  | 
9  |  |  * Copyright (c) 2006-2007, Parvatha Elangovan  | 
10  |  |  * Copyright (c) 2010-2011, Kaori Hagihara  | 
11  |  |  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France   | 
12  |  |  * Copyright (c) 2012, CS Systemes d'Information, France  | 
13  |  |  * All rights reserved.  | 
14  |  |  *  | 
15  |  |  * Redistribution and use in source and binary forms, with or without  | 
16  |  |  * modification, are permitted provided that the following conditions  | 
17  |  |  * are met:  | 
18  |  |  * 1. Redistributions of source code must retain the above copyright  | 
19  |  |  *    notice, this list of conditions and the following disclaimer.  | 
20  |  |  * 2. Redistributions in binary form must reproduce the above copyright  | 
21  |  |  *    notice, this list of conditions and the following disclaimer in the  | 
22  |  |  *    documentation and/or other materials provided with the distribution.  | 
23  |  |  *  | 
24  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'  | 
25  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
26  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
27  |  |  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE  | 
28  |  |  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  | 
29  |  |  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  | 
30  |  |  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  | 
31  |  |  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  | 
32  |  |  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  | 
33  |  |  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  | 
34  |  |  * POSSIBILITY OF SUCH DAMAGE.  | 
35  |  |  */  | 
36  |  |  | 
37  |  | #include "opj_includes.h"  | 
38  |  |  | 
39  | 0  | #define CINEMA_24_CS 1302083  /*Codestream length for 24fps*/  | 
40  | 0  | #define CINEMA_48_CS 651041    /*Codestream length for 48fps*/  | 
41  | 0  | #define COMP_24_CS 1041666    /*Maximum size per color component for 2K & 4K @ 24fps*/  | 
42  | 0  | #define COMP_48_CS 520833    /*Maximum size per color component for 2K @ 48fps*/  | 
43  |  |  | 
44  |  | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */  | 
45  |  | /*@{*/ | 
46  |  |  | 
47  |  | /** @name Local static functions */  | 
48  |  | /*@{*/ | 
49  |  |  | 
50  |  | /**  | 
51  |  |  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.  | 
52  |  |  */  | 
53  |  | static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k);  | 
54  |  |  | 
55  |  | /**  | 
56  |  |  * The read header procedure.  | 
57  |  |  */  | 
58  |  | static OPJ_BOOL opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,  | 
59  |  |                                                 opj_stream_private_t *p_stream,  | 
60  |  |                                                 opj_event_mgr_t * p_manager);  | 
61  |  |  | 
62  |  | /**  | 
63  |  |  * The default encoding validation procedure without any extension.  | 
64  |  |  *  | 
65  |  |  * @param       p_j2k                   the jpeg2000 codec to validate.  | 
66  |  |  * @param       p_stream                the input stream to validate.  | 
67  |  |  * @param       p_manager               the user event manager.  | 
68  |  |  *  | 
69  |  |  * @return true if the parameters are correct.  | 
70  |  |  */  | 
71  |  | static OPJ_BOOL opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,  | 
72  |  |                                                 opj_stream_private_t *p_stream,  | 
73  |  |                                                 opj_event_mgr_t * p_manager );  | 
74  |  |  | 
75  |  | /**  | 
76  |  |  * The default decoding validation procedure without any extension.  | 
77  |  |  *  | 
78  |  |  * @param       p_j2k                   the jpeg2000 codec to validate.  | 
79  |  |  * @param       p_stream                                the input stream to validate.  | 
80  |  |  * @param       p_manager               the user event manager.  | 
81  |  |  *  | 
82  |  |  * @return true if the parameters are correct.  | 
83  |  |  */  | 
84  |  | static OPJ_BOOL opj_j2k_decoding_validation (   opj_j2k_t * p_j2k,  | 
85  |  |                                                 opj_stream_private_t *p_stream,  | 
86  |  |                                                 opj_event_mgr_t * p_manager );  | 
87  |  |  | 
88  |  | /**  | 
89  |  |  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters  | 
90  |  |  * are valid. Developpers wanting to extend the library can add their own validation procedures.  | 
91  |  |  */  | 
92  |  | static void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k);  | 
93  |  |  | 
94  |  | /**  | 
95  |  |  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters  | 
96  |  |  * are valid. Developpers wanting to extend the library can add their own validation procedures.  | 
97  |  |  */  | 
98  |  | static void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k);  | 
99  |  |  | 
100  |  | /**  | 
101  |  |  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters  | 
102  |  |  * are valid. Developpers wanting to extend the library can add their own validation procedures.  | 
103  |  |  */  | 
104  |  | static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k);  | 
105  |  |  | 
106  |  | /**  | 
107  |  |  * The mct encoding validation procedure.  | 
108  |  |  *  | 
109  |  |  * @param       p_j2k                   the jpeg2000 codec to validate.  | 
110  |  |  * @param       p_stream                                the input stream to validate.  | 
111  |  |  * @param       p_manager               the user event manager.  | 
112  |  |  *  | 
113  |  |  * @return true if the parameters are correct.  | 
114  |  |  */  | 
115  |  | static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,  | 
116  |  |                                         opj_stream_private_t *p_stream,  | 
117  |  |                                         opj_event_mgr_t * p_manager );  | 
118  |  |  | 
119  |  | /**  | 
120  |  |  * Builds the tcd decoder to use to decode tile.  | 
121  |  |  */  | 
122  |  | static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,  | 
123  |  |                                         opj_stream_private_t *p_stream,  | 
124  |  |                                         opj_event_mgr_t * p_manager );  | 
125  |  | /**  | 
126  |  |  * Builds the tcd encoder to use to encode tile.  | 
127  |  |  */  | 
128  |  | static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,  | 
129  |  |                                         opj_stream_private_t *p_stream,  | 
130  |  |                                         opj_event_mgr_t * p_manager );  | 
131  |  |  | 
132  |  | /**  | 
133  |  |  * Creates a tile-coder decoder.  | 
134  |  |  *  | 
135  |  |  * @param       p_stream                        the stream to write data to.  | 
136  |  |  * @param       p_j2k                           J2K codec.  | 
137  |  |  * @param       p_manager                   the user event manager.  | 
138  |  | */  | 
139  |  | static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,  | 
140  |  |                                                                     opj_stream_private_t *p_stream,  | 
141  |  |                                                                     opj_event_mgr_t * p_manager );  | 
142  |  |  | 
143  |  | /**  | 
144  |  |  * Excutes the given procedures on the given codec.  | 
145  |  |  *  | 
146  |  |  * @param       p_procedure_list        the list of procedures to execute  | 
147  |  |  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.  | 
148  |  |  * @param       p_stream                        the stream to execute the procedures on.  | 
149  |  |  * @param       p_manager                       the user manager.  | 
150  |  |  *  | 
151  |  |  * @return      true                            if all the procedures were successfully executed.  | 
152  |  |  */  | 
153  |  | static OPJ_BOOL opj_j2k_exec (  opj_j2k_t * p_j2k,  | 
154  |  |                             opj_procedure_list_t * p_procedure_list,  | 
155  |  |                             opj_stream_private_t *p_stream,  | 
156  |  |                             opj_event_mgr_t * p_manager);  | 
157  |  |  | 
158  |  | /**  | 
159  |  |  * Updates the rates of the tcp.  | 
160  |  |  *  | 
161  |  |  * @param       p_stream                                the stream to write data to.  | 
162  |  |  * @param       p_j2k                           J2K codec.  | 
163  |  |  * @param       p_manager               the user event manager.  | 
164  |  | */  | 
165  |  | static OPJ_BOOL opj_j2k_update_rates(   opj_j2k_t *p_j2k,  | 
166  |  |                                                                             opj_stream_private_t *p_stream,  | 
167  |  |                                                                             opj_event_mgr_t * p_manager );  | 
168  |  |  | 
169  |  | /**  | 
170  |  |  * Copies the decoding tile parameters onto all the tile parameters.  | 
171  |  |  * Creates also the tile decoder.  | 
172  |  |  */  | 
173  |  | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,  | 
174  |  |                                                             opj_stream_private_t *p_stream,  | 
175  |  |                                                             opj_event_mgr_t * p_manager );  | 
176  |  |  | 
177  |  | /**  | 
178  |  |  * Destroys the memory associated with the decoding of headers.  | 
179  |  |  */  | 
180  |  | static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,  | 
181  |  |                                                 opj_stream_private_t *p_stream,  | 
182  |  |                                                 opj_event_mgr_t * p_manager );  | 
183  |  |  | 
184  |  | /**  | 
185  |  |  * Reads the lookup table containing all the marker, status and action, and returns the handler associated  | 
186  |  |  * with the marker value.  | 
187  |  |  * @param       p_id            Marker value to look up  | 
188  |  |  *  | 
189  |  |  * @return      the handler associated with the id.  | 
190  |  | */  | 
191  |  | static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);  | 
192  |  |  | 
193  |  | /**  | 
194  |  |  * Destroys a tile coding parameter structure.  | 
195  |  |  *  | 
196  |  |  * @param       p_tcp           the tile coding parameter to destroy.  | 
197  |  |  */  | 
198  |  | static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp);  | 
199  |  |  | 
200  |  | /**  | 
201  |  |  * Destroys the data inside a tile coding parameter structure.  | 
202  |  |  *  | 
203  |  |  * @param       p_tcp           the tile coding parameter which contain data to destroy.  | 
204  |  |  */  | 
205  |  | static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp);  | 
206  |  |  | 
207  |  | /**  | 
208  |  |  * Destroys a coding parameter structure.  | 
209  |  |  *  | 
210  |  |  * @param       p_cp            the coding parameter to destroy.  | 
211  |  |  */  | 
212  |  | static void opj_j2k_cp_destroy (opj_cp_t *p_cp);  | 
213  |  |  | 
214  |  | /**  | 
215  |  |  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.  | 
216  |  |  *  | 
217  |  |  * @param       p_j2k           J2K codec.  | 
218  |  |  * @param       p_tile_no       FIXME DOC  | 
219  |  |  * @param       p_comp_no       the component number to output.  | 
220  |  |  * @param       p_data          FIXME DOC  | 
221  |  |  * @param       p_header_size   FIXME DOC  | 
222  |  |  * @param       p_manager       the user event manager.  | 
223  |  |  *  | 
224  |  |  * @return FIXME DOC  | 
225  |  | */  | 
226  |  | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(      opj_j2k_t *p_j2k,  | 
227  |  |                                                                                     OPJ_UINT32 p_tile_no,  | 
228  |  |                                                                                     OPJ_UINT32 p_comp_no,  | 
229  |  |                                                                                     OPJ_BYTE * p_data,  | 
230  |  |                                                                                     OPJ_UINT32 * p_header_size,  | 
231  |  |                                                                                     opj_event_mgr_t * p_manager );  | 
232  |  |  | 
233  |  | /**  | 
234  |  |  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.  | 
235  |  |  *  | 
236  |  |  * @param       p_j2k                   the J2K codec.  | 
237  |  |  * @param       p_tile_no               the tile index.  | 
238  |  |  * @param       p_comp_no               the component being outputted.  | 
239  |  |  *  | 
240  |  |  * @return      the number of bytes taken by the SPCod element.  | 
241  |  |  */  | 
242  |  | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,  | 
243  |  |                                                                                             OPJ_UINT32 p_tile_no,  | 
244  |  |                                                                                             OPJ_UINT32 p_comp_no );  | 
245  |  |  | 
246  |  | /**  | 
247  |  |  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.  | 
248  |  |  * @param       p_j2k           the jpeg2000 codec.  | 
249  |  |  * @param       compno          FIXME DOC  | 
250  |  |  * @param       p_header_data   the data contained in the COM box.  | 
251  |  |  * @param       p_header_size   the size of the data contained in the COM marker.  | 
252  |  |  * @param       p_manager       the user event manager.  | 
253  |  | */  | 
254  |  | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(   opj_j2k_t *p_j2k,  | 
255  |  |                                             OPJ_UINT32 compno,  | 
256  |  |                                             OPJ_BYTE * p_header_data,  | 
257  |  |                                             OPJ_UINT32 * p_header_size,  | 
258  |  |                                             opj_event_mgr_t * p_manager );  | 
259  |  |  | 
260  |  | /**  | 
261  |  |  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.  | 
262  |  |  *  | 
263  |  |  * @param       p_tile_no               the tile index.  | 
264  |  |  * @param       p_comp_no               the component being outputted.  | 
265  |  |  * @param       p_j2k                   the J2K codec.  | 
266  |  |  *  | 
267  |  |  * @return      the number of bytes taken by the SPCod element.  | 
268  |  |  */  | 
269  |  | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size (  opj_j2k_t *p_j2k,  | 
270  |  |                                                                                     OPJ_UINT32 p_tile_no,  | 
271  |  |                                                                                     OPJ_UINT32 p_comp_no );  | 
272  |  |  | 
273  |  | /**  | 
274  |  |  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.  | 
275  |  |  *  | 
276  |  |  * @param       p_tile_no               the tile to output.  | 
277  |  |  * @param       p_comp_no               the component number to output.  | 
278  |  |  * @param       p_data                  the data buffer.  | 
279  |  |  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.  | 
280  |  |  * @param       p_j2k                   J2K codec.  | 
281  |  |  * @param       p_manager               the user event manager.  | 
282  |  |  *  | 
283  |  | */  | 
284  |  | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,  | 
285  |  |                                                                             OPJ_UINT32 p_tile_no,  | 
286  |  |                                                                             OPJ_UINT32 p_comp_no,  | 
287  |  |                                                                             OPJ_BYTE * p_data,  | 
288  |  |                                                                             OPJ_UINT32 * p_header_size,  | 
289  |  |                                                                             opj_event_mgr_t * p_manager);  | 
290  |  |  | 
291  |  | /**  | 
292  |  |  * Updates the Tile Length Marker.  | 
293  |  |  */  | 
294  |  | static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);  | 
295  |  |  | 
296  |  | /**  | 
297  |  |  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.  | 
298  |  |  *  | 
299  |  |  * @param       p_j2k           J2K codec.  | 
300  |  |  * @param       compno          the component number to output.  | 
301  |  |  * @param       p_header_data   the data buffer.  | 
302  |  |  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.  | 
303  |  |  * @param       p_manager       the user event manager.  | 
304  |  |  *  | 
305  |  | */  | 
306  |  | static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,  | 
307  |  |                                         OPJ_UINT32 compno,  | 
308  |  |                                         OPJ_BYTE * p_header_data,  | 
309  |  |                                         OPJ_UINT32 * p_header_size,  | 
310  |  |                                         opj_event_mgr_t * p_manager );  | 
311  |  |  | 
312  |  | /**  | 
313  |  |  * Copies the tile component parameters of all the component from the first tile component.  | 
314  |  |  *  | 
315  |  |  * @param               p_j2k           the J2k codec.  | 
316  |  |  */  | 
317  |  | static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k );  | 
318  |  |  | 
319  |  | /**  | 
320  |  |  * Copies the tile quantization parameters of all the component from the first tile component.  | 
321  |  |  *  | 
322  |  |  * @param               p_j2k           the J2k codec.  | 
323  |  |  */  | 
324  |  | static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );  | 
325  |  |  | 
326  |  | /**  | 
327  |  |  * Reads the tiles.  | 
328  |  |  */  | 
329  |  | static OPJ_BOOL opj_j2k_decode_tiles (  opj_j2k_t *p_j2k,  | 
330  |  |                                         opj_stream_private_t *p_stream,  | 
331  |  |                                         opj_event_mgr_t * p_manager);  | 
332  |  |  | 
333  |  | static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,  | 
334  |  |                                                                              OPJ_UINT32 p_tile_index,  | 
335  |  |                                                                              opj_stream_private_t *p_stream,  | 
336  |  |                                                                              opj_event_mgr_t * p_manager );  | 
337  |  |  | 
338  |  | static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);  | 
339  |  |  | 
340  |  | static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);  | 
341  |  |  | 
342  |  | static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,  | 
343  |  |                                                                              OPJ_BYTE * p_data,  | 
344  |  |                                                                              OPJ_UINT32 p_data_size,  | 
345  |  |                                                                              opj_stream_private_t *p_stream,  | 
346  |  |                                                                              opj_event_mgr_t * p_manager );  | 
347  |  |  | 
348  |  | /**  | 
349  |  |  * Sets up the procedures to do on writing header.  | 
350  |  |  * Developers wanting to extend the library can add their own writing procedures.  | 
351  |  |  */  | 
352  |  | static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k);  | 
353  |  |  | 
354  |  | static OPJ_BOOL opj_j2k_write_first_tile_part(  opj_j2k_t *p_j2k,  | 
355  |  |                                                                                             OPJ_BYTE * p_data,  | 
356  |  |                                                                                             OPJ_UINT32 * p_data_written,  | 
357  |  |                                                                                             OPJ_UINT32 p_total_data_size,  | 
358  |  |                                                                                             opj_stream_private_t *p_stream,  | 
359  |  |                                                                                             struct opj_event_mgr * p_manager );  | 
360  |  |  | 
361  |  | static OPJ_BOOL opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,  | 
362  |  |                                                                                             OPJ_BYTE * p_data,  | 
363  |  |                                                                                             OPJ_UINT32 * p_data_written,  | 
364  |  |                                                                                             OPJ_UINT32 p_total_data_size,  | 
365  |  |                                                                                             opj_stream_private_t *p_stream,  | 
366  |  |                                                                                             struct opj_event_mgr * p_manager );  | 
367  |  |  | 
368  |  | /**  | 
369  |  |  * Gets the offset of the header.  | 
370  |  |  *  | 
371  |  |  * @param       p_stream                the stream to write data to.  | 
372  |  |  * @param       p_j2k                   J2K codec.  | 
373  |  |  * @param       p_manager               the user event manager.  | 
374  |  | */  | 
375  |  | static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,  | 
376  |  |                                         opj_stream_private_t *p_stream,  | 
377  |  |                                         opj_event_mgr_t * p_manager );  | 
378  |  |  | 
379  |  | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);  | 
380  |  |  | 
381  |  | /*  | 
382  |  |  * -----------------------------------------------------------------------  | 
383  |  |  * -----------------------------------------------------------------------  | 
384  |  |  * -----------------------------------------------------------------------  | 
385  |  |  */  | 
386  |  |  | 
387  |  | /**  | 
388  |  |  * Writes the SOC marker (Start Of Codestream)  | 
389  |  |  *  | 
390  |  |  * @param       p_stream                        the stream to write data to.  | 
391  |  |  * @param       p_j2k                   J2K codec.  | 
392  |  |  * @param       p_manager       the user event manager.  | 
393  |  | */  | 
394  |  | static OPJ_BOOL opj_j2k_write_soc(      opj_j2k_t *p_j2k,  | 
395  |  |                                                         opj_stream_private_t *p_stream,  | 
396  |  |                                                             opj_event_mgr_t * p_manager );  | 
397  |  |  | 
398  |  | /**  | 
399  |  |  * Reads a SOC marker (Start of Codestream)  | 
400  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
401  |  |  * @param       p_stream        XXX needs data  | 
402  |  |  * @param       p_manager       the user event manager.  | 
403  |  | */  | 
404  |  | static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,  | 
405  |  |                                     opj_stream_private_t *p_stream,  | 
406  |  |                                     opj_event_mgr_t * p_manager );  | 
407  |  |  | 
408  |  | /**  | 
409  |  |  * Writes the SIZ marker (image and tile size)  | 
410  |  |  *  | 
411  |  |  * @param       p_j2k           J2K codec.  | 
412  |  |  * @param       p_stream        the stream to write data to.  | 
413  |  |  * @param       p_manager       the user event manager.  | 
414  |  | */  | 
415  |  | static OPJ_BOOL opj_j2k_write_siz(      opj_j2k_t *p_j2k,  | 
416  |  |                                                                 opj_stream_private_t *p_stream,  | 
417  |  |                                                                 opj_event_mgr_t * p_manager );  | 
418  |  |  | 
419  |  | /**  | 
420  |  |  * Reads a SIZ marker (image and tile size)  | 
421  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
422  |  |  * @param       p_header_data   the data contained in the SIZ box.  | 
423  |  |  * @param       p_header_size   the size of the data contained in the SIZ marker.  | 
424  |  |  * @param       p_manager       the user event manager.  | 
425  |  | */  | 
426  |  | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,  | 
427  |  |                                  OPJ_BYTE * p_header_data,  | 
428  |  |                                  OPJ_UINT32 p_header_size,  | 
429  |  |                                  opj_event_mgr_t * p_manager);  | 
430  |  |  | 
431  |  | /**  | 
432  |  |  * Writes the COM marker (comment)  | 
433  |  |  *  | 
434  |  |  * @param       p_stream                        the stream to write data to.  | 
435  |  |  * @param       p_j2k                   J2K codec.  | 
436  |  |  * @param       p_manager       the user event manager.  | 
437  |  | */  | 
438  |  | static OPJ_BOOL opj_j2k_write_com(      opj_j2k_t *p_j2k,  | 
439  |  |                                                                         opj_stream_private_t *p_stream,  | 
440  |  |                                                                         opj_event_mgr_t * p_manager );  | 
441  |  |  | 
442  |  | /**  | 
443  |  |  * Reads a COM marker (comments)  | 
444  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
445  |  |  * @param       p_header_data   the data contained in the COM box.  | 
446  |  |  * @param       p_header_size   the size of the data contained in the COM marker.  | 
447  |  |  * @param       p_manager       the user event manager.  | 
448  |  | */  | 
449  |  | static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,  | 
450  |  |                                     OPJ_BYTE * p_header_data,  | 
451  |  |                                     OPJ_UINT32 p_header_size,  | 
452  |  |                                     opj_event_mgr_t * p_manager );  | 
453  |  | /**  | 
454  |  |  * Writes the COD marker (Coding style default)  | 
455  |  |  *  | 
456  |  |  * @param       p_stream                        the stream to write data to.  | 
457  |  |  * @param       p_j2k                   J2K codec.  | 
458  |  |  * @param       p_manager       the user event manager.  | 
459  |  | */  | 
460  |  | static OPJ_BOOL opj_j2k_write_cod(      opj_j2k_t *p_j2k,  | 
461  |  |                                                                         opj_stream_private_t *p_stream,  | 
462  |  |                                                                         opj_event_mgr_t * p_manager );  | 
463  |  |  | 
464  |  | /**  | 
465  |  |  * Reads a COD marker (Coding Styke defaults)  | 
466  |  |  * @param       p_header_data   the data contained in the COD box.  | 
467  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
468  |  |  * @param       p_header_size   the size of the data contained in the COD marker.  | 
469  |  |  * @param       p_manager               the user event manager.  | 
470  |  | */  | 
471  |  | static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,  | 
472  |  |                                     OPJ_BYTE * p_header_data,  | 
473  |  |                                     OPJ_UINT32 p_header_size,  | 
474  |  |                                     opj_event_mgr_t * p_manager);  | 
475  |  |  | 
476  |  | #if 0  | 
477  |  | /**  | 
478  |  |  * Writes the COC marker (Coding style component)  | 
479  |  |  *  | 
480  |  |  * @param       p_j2k       J2K codec.  | 
481  |  |  * @param       p_comp_no   the index of the component to output.  | 
482  |  |  * @param       p_stream    the stream to write data to.  | 
483  |  |  * @param       p_manager   the user event manager.  | 
484  |  | */  | 
485  |  | static OPJ_BOOL opj_j2k_write_coc(  opj_j2k_t *p_j2k,  | 
486  |  |                                                                 OPJ_UINT32 p_comp_no,  | 
487  |  |                                                                 opj_stream_private_t *p_stream,  | 
488  |  |                                                                 opj_event_mgr_t * p_manager );  | 
489  |  | #endif  | 
490  |  |  | 
491  |  | #if 0  | 
492  |  | /**  | 
493  |  |  * Writes the COC marker (Coding style component)  | 
494  |  |  *  | 
495  |  |  * @param       p_j2k                   J2K codec.  | 
496  |  |  * @param       p_comp_no               the index of the component to output.  | 
497  |  |  * @param       p_data          FIXME DOC  | 
498  |  |  * @param       p_data_written  FIXME DOC  | 
499  |  |  * @param       p_manager               the user event manager.  | 
500  |  | */  | 
501  |  | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,  | 
502  |  |                                                                             OPJ_UINT32 p_comp_no,  | 
503  |  |                                                                             OPJ_BYTE * p_data,  | 
504  |  |                                                                             OPJ_UINT32 * p_data_written,  | 
505  |  |                                                                             opj_event_mgr_t * p_manager );  | 
506  |  | #endif  | 
507  |  |  | 
508  |  | /**  | 
509  |  |  * Gets the maximum size taken by a coc.  | 
510  |  |  *  | 
511  |  |  * @param       p_j2k   the jpeg2000 codec to use.  | 
512  |  |  */  | 
513  |  | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);  | 
514  |  |  | 
515  |  | /**  | 
516  |  |  * Reads a COC marker (Coding Style Component)  | 
517  |  |  * @param       p_header_data   the data contained in the COC box.  | 
518  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
519  |  |  * @param       p_header_size   the size of the data contained in the COC marker.  | 
520  |  |  * @param       p_manager               the user event manager.  | 
521  |  | */  | 
522  |  | static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,  | 
523  |  |                                     OPJ_BYTE * p_header_data,  | 
524  |  |                                     OPJ_UINT32 p_header_size,  | 
525  |  |                                     opj_event_mgr_t * p_manager );  | 
526  |  |  | 
527  |  | /**  | 
528  |  |  * Writes the QCD marker (quantization default)  | 
529  |  |  *  | 
530  |  |  * @param       p_j2k                   J2K codec.  | 
531  |  |  * @param       p_stream                the stream to write data to.  | 
532  |  |  * @param       p_manager               the user event manager.  | 
533  |  | */  | 
534  |  | static OPJ_BOOL opj_j2k_write_qcd(      opj_j2k_t *p_j2k,  | 
535  |  |                                                                         opj_stream_private_t *p_stream,  | 
536  |  |                                                                         opj_event_mgr_t * p_manager );  | 
537  |  |  | 
538  |  | /**  | 
539  |  |  * Reads a QCD marker (Quantization defaults)  | 
540  |  |  * @param       p_header_data   the data contained in the QCD box.  | 
541  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
542  |  |  * @param       p_header_size   the size of the data contained in the QCD marker.  | 
543  |  |  * @param       p_manager               the user event manager.  | 
544  |  | */  | 
545  |  | static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,  | 
546  |  |                                     OPJ_BYTE * p_header_data,  | 
547  |  |                                     OPJ_UINT32 p_header_size,  | 
548  |  |                                     opj_event_mgr_t * p_manager );  | 
549  |  | #if 0  | 
550  |  | /**  | 
551  |  |  * Writes the QCC marker (quantization component)  | 
552  |  |  *  | 
553  |  |  * @param       p_comp_no       the index of the component to output.  | 
554  |  |  * @param       p_stream                the stream to write data to.  | 
555  |  |  * @param       p_j2k                   J2K codec.  | 
556  |  |  * @param       p_manager               the user event manager.  | 
557  |  | */  | 
558  |  | static OPJ_BOOL opj_j2k_write_qcc(      opj_j2k_t *p_j2k,  | 
559  |  |                                                                         OPJ_UINT32 p_comp_no,  | 
560  |  |                                                                         opj_stream_private_t *p_stream,  | 
561  |  |                                                                         opj_event_mgr_t * p_manager );  | 
562  |  | #endif  | 
563  |  |  | 
564  |  | #if 0  | 
565  |  | /**  | 
566  |  |  * Writes the QCC marker (quantization component)  | 
567  |  |  *  | 
568  |  |  * @param       p_j2k           J2K codec.  | 
569  |  |  * @param       p_comp_no       the index of the component to output.  | 
570  |  |  * @param       p_data          FIXME DOC  | 
571  |  |  * @param       p_data_written  the stream to write data to.  | 
572  |  |  * @param       p_manager       the user event manager.  | 
573  |  | */  | 
574  |  | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,  | 
575  |  |                                                                             OPJ_UINT32 p_comp_no,  | 
576  |  |                                                                             OPJ_BYTE * p_data,  | 
577  |  |                                                                             OPJ_UINT32 * p_data_written,  | 
578  |  |                                                                             opj_event_mgr_t * p_manager );  | 
579  |  | #endif  | 
580  |  |  | 
581  |  | /**  | 
582  |  |  * Gets the maximum size taken by a qcc.  | 
583  |  |  */  | 
584  |  | static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);  | 
585  |  |  | 
586  |  | /**  | 
587  |  |  * Reads a QCC marker (Quantization component)  | 
588  |  |  * @param       p_header_data   the data contained in the QCC box.  | 
589  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
590  |  |  * @param       p_header_size   the size of the data contained in the QCC marker.  | 
591  |  |  * @param       p_manager               the user event manager.  | 
592  |  | */  | 
593  |  | static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,  | 
594  |  |                                     OPJ_BYTE * p_header_data,  | 
595  |  |                                     OPJ_UINT32 p_header_size,  | 
596  |  |                                     opj_event_mgr_t * p_manager);  | 
597  |  | /**  | 
598  |  |  * Writes the POC marker (Progression Order Change)  | 
599  |  |  *  | 
600  |  |  * @param       p_stream                                the stream to write data to.  | 
601  |  |  * @param       p_j2k                           J2K codec.  | 
602  |  |  * @param       p_manager               the user event manager.  | 
603  |  | */  | 
604  |  | static OPJ_BOOL opj_j2k_write_poc(      opj_j2k_t *p_j2k,  | 
605  |  |                                                                         opj_stream_private_t *p_stream,  | 
606  |  |                                                                         opj_event_mgr_t * p_manager );  | 
607  |  | /**  | 
608  |  |  * Writes the POC marker (Progression Order Change)  | 
609  |  |  *  | 
610  |  |  * @param       p_j2k          J2K codec.  | 
611  |  |  * @param       p_data         FIXME DOC  | 
612  |  |  * @param       p_data_written the stream to write data to.  | 
613  |  |  * @param       p_manager      the user event manager.  | 
614  |  |  */  | 
615  |  | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,  | 
616  |  |                                                                             OPJ_BYTE * p_data,  | 
617  |  |                                                                             OPJ_UINT32 * p_data_written,  | 
618  |  |                                                                             opj_event_mgr_t * p_manager );  | 
619  |  | /**  | 
620  |  |  * Gets the maximum size taken by the writing of a POC.  | 
621  |  |  */  | 
622  |  | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);  | 
623  |  |  | 
624  |  | /**  | 
625  |  |  * Reads a POC marker (Progression Order Change)  | 
626  |  |  *  | 
627  |  |  * @param       p_header_data   the data contained in the POC box.  | 
628  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
629  |  |  * @param       p_header_size   the size of the data contained in the POC marker.  | 
630  |  |  * @param       p_manager               the user event manager.  | 
631  |  | */  | 
632  |  | static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,  | 
633  |  |                                     OPJ_BYTE * p_header_data,  | 
634  |  |                                     OPJ_UINT32 p_header_size,  | 
635  |  |                                     opj_event_mgr_t * p_manager );  | 
636  |  |  | 
637  |  | /**  | 
638  |  |  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.  | 
639  |  |  */  | 
640  |  | static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k);  | 
641  |  |  | 
642  |  | /**  | 
643  |  |  * Gets the maximum size taken by the headers of the SOT.  | 
644  |  |  *  | 
645  |  |  * @param       p_j2k   the jpeg2000 codec to use.  | 
646  |  |  */  | 
647  |  | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);  | 
648  |  |  | 
649  |  | /**  | 
650  |  |  * Reads a CRG marker (Component registration)  | 
651  |  |  *  | 
652  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
653  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
654  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
655  |  |  * @param       p_manager               the user event manager.  | 
656  |  | */  | 
657  |  | static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,  | 
658  |  |                                     OPJ_BYTE * p_header_data,  | 
659  |  |                                     OPJ_UINT32 p_header_size,  | 
660  |  |                                     opj_event_mgr_t * p_manager );  | 
661  |  | /**  | 
662  |  |  * Reads a TLM marker (Tile Length Marker)  | 
663  |  |  *  | 
664  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
665  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
666  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
667  |  |  * @param       p_manager               the user event manager.  | 
668  |  | */  | 
669  |  | static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,  | 
670  |  |                                     OPJ_BYTE * p_header_data,  | 
671  |  |                                     OPJ_UINT32 p_header_size,  | 
672  |  |                                     opj_event_mgr_t * p_manager);  | 
673  |  |  | 
674  |  | /**  | 
675  |  |  * Writes the updated tlm.  | 
676  |  |  *  | 
677  |  |  * @param       p_stream                the stream to write data to.  | 
678  |  |  * @param       p_j2k                   J2K codec.  | 
679  |  |  * @param       p_manager               the user event manager.  | 
680  |  | */  | 
681  |  | static OPJ_BOOL opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,  | 
682  |  |                                             opj_stream_private_t *p_stream,  | 
683  |  |                                             opj_event_mgr_t * p_manager );  | 
684  |  |  | 
685  |  | /**  | 
686  |  |  * Reads a PLM marker (Packet length, main header marker)  | 
687  |  |  *  | 
688  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
689  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
690  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
691  |  |  * @param       p_manager               the user event manager.  | 
692  |  | */  | 
693  |  | static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,  | 
694  |  |                                     OPJ_BYTE * p_header_data,  | 
695  |  |                                     OPJ_UINT32 p_header_size,  | 
696  |  |                                     opj_event_mgr_t * p_manager);  | 
697  |  | /**  | 
698  |  |  * Reads a PLT marker (Packet length, tile-part header)  | 
699  |  |  *  | 
700  |  |  * @param       p_header_data   the data contained in the PLT box.  | 
701  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
702  |  |  * @param       p_header_size   the size of the data contained in the PLT marker.  | 
703  |  |  * @param       p_manager               the user event manager.  | 
704  |  | */  | 
705  |  | static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,  | 
706  |  |                                     OPJ_BYTE * p_header_data,  | 
707  |  |                                     OPJ_UINT32 p_header_size,  | 
708  |  |                                     opj_event_mgr_t * p_manager );  | 
709  |  |  | 
710  |  | #if 0  | 
711  |  | /**  | 
712  |  |  * Reads a PPM marker (Packed packet headers, main header)  | 
713  |  |  *  | 
714  |  |  * @param       p_header_data   the data contained in the POC box.  | 
715  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
716  |  |  * @param       p_header_size   the size of the data contained in the POC marker.  | 
717  |  |  * @param       p_manager               the user event manager.  | 
718  |  | */  | 
719  |  | static OPJ_BOOL j2k_read_ppm_v2 (  | 
720  |  |                                                 opj_j2k_t *p_j2k,  | 
721  |  |                                                 OPJ_BYTE * p_header_data,  | 
722  |  |                                                 OPJ_UINT32 p_header_size,  | 
723  |  |                                                 struct opj_event_mgr * p_manager  | 
724  |  |                                         );  | 
725  |  | #endif  | 
726  |  |  | 
727  |  | static OPJ_BOOL j2k_read_ppm_v3 (  | 
728  |  |                                                 opj_j2k_t *p_j2k,  | 
729  |  |                                                 OPJ_BYTE * p_header_data,  | 
730  |  |                                                 OPJ_UINT32 p_header_size,  | 
731  |  |                                                 opj_event_mgr_t * p_manager );  | 
732  |  |  | 
733  |  | /**  | 
734  |  |  * Reads a PPT marker (Packed packet headers, tile-part header)  | 
735  |  |  *  | 
736  |  |  * @param       p_header_data   the data contained in the PPT box.  | 
737  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
738  |  |  * @param       p_header_size   the size of the data contained in the PPT marker.  | 
739  |  |  * @param       p_manager               the user event manager.  | 
740  |  | */  | 
741  |  | static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,  | 
742  |  |                                     OPJ_BYTE * p_header_data,  | 
743  |  |                                     OPJ_UINT32 p_header_size,  | 
744  |  |                                     opj_event_mgr_t * p_manager );  | 
745  |  | /**  | 
746  |  |  * Writes the TLM marker (Tile Length Marker)  | 
747  |  |  *  | 
748  |  |  * @param       p_stream                                the stream to write data to.  | 
749  |  |  * @param       p_j2k                           J2K codec.  | 
750  |  |  * @param       p_manager               the user event manager.  | 
751  |  | */  | 
752  |  | static OPJ_BOOL opj_j2k_write_tlm(      opj_j2k_t *p_j2k,  | 
753  |  |                                                                         opj_stream_private_t *p_stream,  | 
754  |  |                                                                         opj_event_mgr_t * p_manager );  | 
755  |  |  | 
756  |  | /**  | 
757  |  |  * Writes the SOT marker (Start of tile-part)  | 
758  |  |  *  | 
759  |  |  * @param       p_j2k            J2K codec.  | 
760  |  |  * @param       p_data           FIXME DOC  | 
761  |  |  * @param       p_data_written   FIXME DOC  | 
762  |  |  * @param       p_stream         the stream to write data to.  | 
763  |  |  * @param       p_manager        the user event manager.  | 
764  |  | */  | 
765  |  | static OPJ_BOOL opj_j2k_write_sot(      opj_j2k_t *p_j2k,  | 
766  |  |                                                                         OPJ_BYTE * p_data,  | 
767  |  |                                                                         OPJ_UINT32 * p_data_written,  | 
768  |  |                                                                         const opj_stream_private_t *p_stream,  | 
769  |  |                                                                         opj_event_mgr_t * p_manager );  | 
770  |  |  | 
771  |  | /**  | 
772  |  |  * Reads a PPT marker (Packed packet headers, tile-part header)  | 
773  |  |  *  | 
774  |  |  * @param       p_header_data   the data contained in the PPT box.  | 
775  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
776  |  |  * @param       p_header_size   the size of the data contained in the PPT marker.  | 
777  |  |  * @param       p_manager               the user event manager.  | 
778  |  | */  | 
779  |  | static OPJ_BOOL opj_j2k_read_sot (  opj_j2k_t *p_j2k,  | 
780  |  |                                     OPJ_BYTE * p_header_data,  | 
781  |  |                                     OPJ_UINT32 p_header_size,  | 
782  |  |                                     opj_event_mgr_t * p_manager );  | 
783  |  | /**  | 
784  |  |  * Writes the SOD marker (Start of data)  | 
785  |  |  *  | 
786  |  |  * @param       p_j2k               J2K codec.  | 
787  |  |  * @param       p_tile_coder        FIXME DOC  | 
788  |  |  * @param       p_data              FIXME DOC  | 
789  |  |  * @param       p_data_written      FIXME DOC  | 
790  |  |  * @param       p_total_data_size   FIXME DOC  | 
791  |  |  * @param       p_stream            the stream to write data to.  | 
792  |  |  * @param       p_manager           the user event manager.  | 
793  |  | */  | 
794  |  | static OPJ_BOOL opj_j2k_write_sod(      opj_j2k_t *p_j2k,  | 
795  |  |                                                                         opj_tcd_t * p_tile_coder,  | 
796  |  |                                                                         OPJ_BYTE * p_data,  | 
797  |  |                                                                         OPJ_UINT32 * p_data_written,  | 
798  |  |                                                                         OPJ_UINT32 p_total_data_size,  | 
799  |  |                                                                         const opj_stream_private_t *p_stream,  | 
800  |  |                                                                         opj_event_mgr_t * p_manager );  | 
801  |  |  | 
802  |  | /**  | 
803  |  |  * Reads a SOD marker (Start Of Data)  | 
804  |  |  *  | 
805  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
806  |  |  * @param       p_stream                FIXME DOC  | 
807  |  |  * @param       p_manager               the user event manager.  | 
808  |  | */  | 
809  |  | static OPJ_BOOL opj_j2k_read_sod(   opj_j2k_t *p_j2k,  | 
810  |  |                                     opj_stream_private_t *p_stream,  | 
811  |  |                                     opj_event_mgr_t * p_manager );  | 
812  |  |  | 
813  |  | void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )  | 
814  | 0  | { | 
815  | 0  |         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);            /* PSOT */  | 
816  | 0  |         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;  | 
817  |  | 
  | 
818  | 0  |         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */  | 
819  | 0  |         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;  | 
820  | 0  | }  | 
821  |  |  | 
822  |  | /**  | 
823  |  |  * Writes the RGN marker (Region Of Interest)  | 
824  |  |  *  | 
825  |  |  * @param       p_tile_no               the tile to output  | 
826  |  |  * @param       p_comp_no               the component to output  | 
827  |  |  * @param       nb_comps                the number of components  | 
828  |  |  * @param       p_stream                the stream to write data to.  | 
829  |  |  * @param       p_j2k                   J2K codec.  | 
830  |  |  * @param       p_manager               the user event manager.  | 
831  |  | */  | 
832  |  | static OPJ_BOOL opj_j2k_write_rgn(  opj_j2k_t *p_j2k,  | 
833  |  |                                     OPJ_UINT32 p_tile_no,  | 
834  |  |                                     OPJ_UINT32 p_comp_no,  | 
835  |  |                                     OPJ_UINT32 nb_comps,  | 
836  |  |                                     opj_stream_private_t *p_stream,  | 
837  |  |                                     opj_event_mgr_t * p_manager );  | 
838  |  |  | 
839  |  | /**  | 
840  |  |  * Reads a RGN marker (Region Of Interest)  | 
841  |  |  *  | 
842  |  |  * @param       p_header_data   the data contained in the POC box.  | 
843  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
844  |  |  * @param       p_header_size   the size of the data contained in the POC marker.  | 
845  |  |  * @param       p_manager               the user event manager.  | 
846  |  | */  | 
847  |  | static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,  | 
848  |  |                                   OPJ_BYTE * p_header_data,  | 
849  |  |                                   OPJ_UINT32 p_header_size,  | 
850  |  |                                   opj_event_mgr_t * p_manager );  | 
851  |  |  | 
852  |  | /**  | 
853  |  |  * Writes the EOC marker (End of Codestream)  | 
854  |  |  *  | 
855  |  |  * @param       p_stream                the stream to write data to.  | 
856  |  |  * @param       p_j2k                   J2K codec.  | 
857  |  |  * @param       p_manager               the user event manager.  | 
858  |  | */  | 
859  |  | static OPJ_BOOL opj_j2k_write_eoc(      opj_j2k_t *p_j2k,  | 
860  |  |                                     opj_stream_private_t *p_stream,  | 
861  |  |                                     opj_event_mgr_t * p_manager );  | 
862  |  |  | 
863  |  | #if 0  | 
864  |  | /**  | 
865  |  |  * Reads a EOC marker (End Of Codestream)  | 
866  |  |  *  | 
867  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
868  |  |  * @param       p_stream                FIXME DOC  | 
869  |  |  * @param       p_manager               the user event manager.  | 
870  |  | */  | 
871  |  | static OPJ_BOOL opj_j2k_read_eoc (      opj_j2k_t *p_j2k,  | 
872  |  |                                                                 opj_stream_private_t *p_stream,  | 
873  |  |                                                                 opj_event_mgr_t * p_manager );  | 
874  |  | #endif  | 
875  |  |  | 
876  |  | /**  | 
877  |  |  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)  | 
878  |  |  *  | 
879  |  |  * @param       p_stream                        the stream to write data to.  | 
880  |  |  * @param       p_j2k                   J2K codec.  | 
881  |  |  * @param       p_manager       the user event manager.  | 
882  |  | */  | 
883  |  | static OPJ_BOOL opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,  | 
884  |  |                                                 opj_stream_private_t *p_stream,  | 
885  |  |                                                 opj_event_mgr_t * p_manager );  | 
886  |  |  | 
887  |  | /**  | 
888  |  |  * Inits the Info  | 
889  |  |  *  | 
890  |  |  * @param       p_stream                the stream to write data to.  | 
891  |  |  * @param       p_j2k                   J2K codec.  | 
892  |  |  * @param       p_manager               the user event manager.  | 
893  |  | */  | 
894  |  | static OPJ_BOOL opj_j2k_init_info(      opj_j2k_t *p_j2k,  | 
895  |  |                                     opj_stream_private_t *p_stream,  | 
896  |  |                                     opj_event_mgr_t * p_manager );  | 
897  |  |  | 
898  |  | /**  | 
899  |  | Add main header marker information  | 
900  |  | @param cstr_index    Codestream information structure  | 
901  |  | @param type         marker type  | 
902  |  | @param pos          byte offset of marker segment  | 
903  |  | @param len          length of marker segment  | 
904  |  |  */  | 
905  |  | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;  | 
906  |  | /**  | 
907  |  | Add tile header marker information  | 
908  |  | @param tileno       tile index number  | 
909  |  | @param cstr_index   Codestream information structure  | 
910  |  | @param type         marker type  | 
911  |  | @param pos          byte offset of marker segment  | 
912  |  | @param len          length of marker segment  | 
913  |  |  */  | 
914  |  | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);  | 
915  |  |  | 
916  |  | /**  | 
917  |  |  * Reads an unknown marker  | 
918  |  |  *  | 
919  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
920  |  |  * @param       p_stream                the stream object to read from.  | 
921  |  |  * @param       output_marker           FIXME DOC  | 
922  |  |  * @param       p_manager               the user event manager.  | 
923  |  |  *  | 
924  |  |  * @return      true                    if the marker could be deduced.  | 
925  |  | */  | 
926  |  | static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,  | 
927  |  |                                   opj_stream_private_t *p_stream,  | 
928  |  |                                   OPJ_UINT32 *output_marker,  | 
929  |  |                                   opj_event_mgr_t * p_manager );  | 
930  |  |  | 
931  |  | /**  | 
932  |  |  * Writes the MCT marker (Multiple Component Transform)  | 
933  |  |  *  | 
934  |  |  * @param       p_j2k           J2K codec.  | 
935  |  |  * @param       p_mct_record    FIXME DOC  | 
936  |  |  * @param       p_stream        the stream to write data to.  | 
937  |  |  * @param       p_manager       the user event manager.  | 
938  |  | */  | 
939  |  | static OPJ_BOOL opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,  | 
940  |  |                                                                                     opj_mct_data_t * p_mct_record,  | 
941  |  |                                             opj_stream_private_t *p_stream,  | 
942  |  |                                             opj_event_mgr_t * p_manager );  | 
943  |  |  | 
944  |  | /**  | 
945  |  |  * Reads a MCT marker (Multiple Component Transform)  | 
946  |  |  *  | 
947  |  |  * @param       p_header_data   the data contained in the MCT box.  | 
948  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
949  |  |  * @param       p_header_size   the size of the data contained in the MCT marker.  | 
950  |  |  * @param       p_manager               the user event manager.  | 
951  |  | */  | 
952  |  | static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,  | 
953  |  |                                                                     OPJ_BYTE * p_header_data,  | 
954  |  |                                                                     OPJ_UINT32 p_header_size,  | 
955  |  |                                                                     opj_event_mgr_t * p_manager );  | 
956  |  |  | 
957  |  | /**  | 
958  |  |  * Writes the MCC marker (Multiple Component Collection)  | 
959  |  |  *  | 
960  |  |  * @param       p_j2k                   J2K codec.  | 
961  |  |  * @param       p_mcc_record            FIXME DOC  | 
962  |  |  * @param       p_stream                the stream to write data to.  | 
963  |  |  * @param       p_manager               the user event manager.  | 
964  |  | */  | 
965  |  | static OPJ_BOOL opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,  | 
966  |  |                                             opj_simple_mcc_decorrelation_data_t * p_mcc_record,  | 
967  |  |                                             opj_stream_private_t *p_stream,  | 
968  |  |                                             opj_event_mgr_t * p_manager );  | 
969  |  |  | 
970  |  | /**  | 
971  |  |  * Reads a MCC marker (Multiple Component Collection)  | 
972  |  |  *  | 
973  |  |  * @param       p_header_data   the data contained in the MCC box.  | 
974  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
975  |  |  * @param       p_header_size   the size of the data contained in the MCC marker.  | 
976  |  |  * @param       p_manager               the user event manager.  | 
977  |  | */  | 
978  |  | static OPJ_BOOL opj_j2k_read_mcc (      opj_j2k_t *p_j2k,  | 
979  |  |                                                                     OPJ_BYTE * p_header_data,  | 
980  |  |                                                                     OPJ_UINT32 p_header_size,  | 
981  |  |                                                                     opj_event_mgr_t * p_manager );  | 
982  |  |  | 
983  |  | /**  | 
984  |  |  * Writes the MCO marker (Multiple component transformation ordering)  | 
985  |  |  *  | 
986  |  |  * @param       p_stream                                the stream to write data to.  | 
987  |  |  * @param       p_j2k                           J2K codec.  | 
988  |  |  * @param       p_manager               the user event manager.  | 
989  |  | */  | 
990  |  | static OPJ_BOOL opj_j2k_write_mco(      opj_j2k_t *p_j2k,  | 
991  |  |                                     opj_stream_private_t *p_stream,  | 
992  |  |                                     opj_event_mgr_t * p_manager );  | 
993  |  |  | 
994  |  | /**  | 
995  |  |  * Reads a MCO marker (Multiple Component Transform Ordering)  | 
996  |  |  *  | 
997  |  |  * @param       p_header_data   the data contained in the MCO box.  | 
998  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
999  |  |  * @param       p_header_size   the size of the data contained in the MCO marker.  | 
1000  |  |  * @param       p_manager               the user event manager.  | 
1001  |  | */  | 
1002  |  | static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,  | 
1003  |  |                                                                     OPJ_BYTE * p_header_data,  | 
1004  |  |                                                                     OPJ_UINT32 p_header_size,  | 
1005  |  |                                                                     opj_event_mgr_t * p_manager );  | 
1006  |  |  | 
1007  |  | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);  | 
1008  |  |  | 
1009  |  | static void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1010  |  | static void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1011  |  | static void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1012  |  | static void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1013  |  |  | 
1014  |  | static void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1015  |  | static void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1016  |  | static void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1017  |  | static void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1018  |  |  | 
1019  |  | static void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1020  |  | static void  opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1021  |  | static void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1022  |  | static void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1023  |  |  | 
1024  |  | /**  | 
1025  |  |  * Ends the encoding, i.e. frees memory.  | 
1026  |  |  *  | 
1027  |  |  * @param       p_stream                the stream to write data to.  | 
1028  |  |  * @param       p_j2k                   J2K codec.  | 
1029  |  |  * @param       p_manager               the user event manager.  | 
1030  |  | */  | 
1031  |  | static OPJ_BOOL opj_j2k_end_encoding(   opj_j2k_t *p_j2k,  | 
1032  |  |                                                                             opj_stream_private_t *p_stream,  | 
1033  |  |                                                                             opj_event_mgr_t * p_manager );  | 
1034  |  |  | 
1035  |  | /**  | 
1036  |  |  * Writes the CBD marker (Component bit depth definition)  | 
1037  |  |  *  | 
1038  |  |  * @param       p_stream                                the stream to write data to.  | 
1039  |  |  * @param       p_j2k                           J2K codec.  | 
1040  |  |  * @param       p_manager               the user event manager.  | 
1041  |  | */  | 
1042  |  | static OPJ_BOOL opj_j2k_write_cbd(      opj_j2k_t *p_j2k,  | 
1043  |  |                                                                     opj_stream_private_t *p_stream,  | 
1044  |  |                                                                         opj_event_mgr_t * p_manager );  | 
1045  |  |  | 
1046  |  | /**  | 
1047  |  |  * Reads a CBD marker (Component bit depth definition)  | 
1048  |  |  * @param       p_header_data   the data contained in the CBD box.  | 
1049  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
1050  |  |  * @param       p_header_size   the size of the data contained in the CBD marker.  | 
1051  |  |  * @param       p_manager               the user event manager.  | 
1052  |  | */  | 
1053  |  | static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,  | 
1054  |  |                                                                 OPJ_BYTE * p_header_data,  | 
1055  |  |                                                                 OPJ_UINT32 p_header_size,  | 
1056  |  |                                                                 opj_event_mgr_t * p_manager);  | 
1057  |  |  | 
1058  |  | #if 0  | 
1059  |  | /**  | 
1060  |  |  * Writes COC marker for each component.  | 
1061  |  |  *  | 
1062  |  |  * @param       p_stream                the stream to write data to.  | 
1063  |  |  * @param       p_j2k                   J2K codec.  | 
1064  |  |  * @param       p_manager               the user event manager.  | 
1065  |  | */  | 
1066  |  | static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,  | 
1067  |  |                                                                         opj_stream_private_t *p_stream,  | 
1068  |  |                                                                         opj_event_mgr_t * p_manager );  | 
1069  |  | #endif  | 
1070  |  |  | 
1071  |  | #if 0  | 
1072  |  | /**  | 
1073  |  |  * Writes QCC marker for each component.  | 
1074  |  |  *  | 
1075  |  |  * @param       p_stream                the stream to write data to.  | 
1076  |  |  * @param       p_j2k                   J2K codec.  | 
1077  |  |  * @param       p_manager               the user event manager.  | 
1078  |  | */  | 
1079  |  | static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,  | 
1080  |  |                                                                         opj_stream_private_t *p_stream,  | 
1081  |  |                                                                         opj_event_mgr_t * p_manager );  | 
1082  |  | #endif  | 
1083  |  |  | 
1084  |  | /**  | 
1085  |  |  * Writes regions of interests.  | 
1086  |  |  *  | 
1087  |  |  * @param       p_stream                the stream to write data to.  | 
1088  |  |  * @param       p_j2k                   J2K codec.  | 
1089  |  |  * @param       p_manager               the user event manager.  | 
1090  |  | */  | 
1091  |  | static OPJ_BOOL opj_j2k_write_regions(  opj_j2k_t *p_j2k,  | 
1092  |  |                                                                         opj_stream_private_t *p_stream,  | 
1093  |  |                                                                         opj_event_mgr_t * p_manager );  | 
1094  |  |  | 
1095  |  | /**  | 
1096  |  |  * Writes EPC ????  | 
1097  |  |  *  | 
1098  |  |  * @param       p_stream                the stream to write data to.  | 
1099  |  |  * @param       p_j2k                   J2K codec.  | 
1100  |  |  * @param       p_manager               the user event manager.  | 
1101  |  | */  | 
1102  |  | static OPJ_BOOL opj_j2k_write_epc(      opj_j2k_t *p_j2k,  | 
1103  |  |                                                                     opj_stream_private_t *p_stream,  | 
1104  |  |                                                                     opj_event_mgr_t * p_manager );  | 
1105  |  |  | 
1106  |  | /**  | 
1107  |  |  * Checks the progression order changes values. Tells of the poc given as input are valid.  | 
1108  |  |  * A nice message is outputted at errors.  | 
1109  |  |  *  | 
1110  |  |  * @param       p_pocs                  the progression order changes.  | 
1111  |  |  * @param       p_nb_pocs               the number of progression order changes.  | 
1112  |  |  * @param       p_nb_resolutions        the number of resolutions.  | 
1113  |  |  * @param       numcomps                the number of components  | 
1114  |  |  * @param       numlayers               the number of layers.  | 
1115  |  |  * @param       p_manager               the user event manager.  | 
1116  |  |  *  | 
1117  |  |  * @return      true if the pocs are valid.  | 
1118  |  |  */  | 
1119  |  | static OPJ_BOOL opj_j2k_check_poc_val(  const opj_poc_t *p_pocs,  | 
1120  |  |                                                                             OPJ_UINT32 p_nb_pocs,  | 
1121  |  |                                                                             OPJ_UINT32 p_nb_resolutions,  | 
1122  |  |                                                                             OPJ_UINT32 numcomps,  | 
1123  |  |                                                                             OPJ_UINT32 numlayers,  | 
1124  |  |                                                                             opj_event_mgr_t * p_manager);  | 
1125  |  |  | 
1126  |  | /**  | 
1127  |  |  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.  | 
1128  |  |  *  | 
1129  |  |  * @param               cp                      the coding parameters.  | 
1130  |  |  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).  | 
1131  |  |  * @param               tileno          the given tile.  | 
1132  |  |  *  | 
1133  |  |  * @return              the number of tile parts.  | 
1134  |  |  */  | 
1135  |  | static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);  | 
1136  |  |  | 
1137  |  | /**  | 
1138  |  |  * Calculates the total number of tile parts needed by the encoder to  | 
1139  |  |  * encode such an image. If not enough memory is available, then the function return false.  | 
1140  |  |  *  | 
1141  |  |  * @param       p_nb_tiles      pointer that will hold the number of tile parts.  | 
1142  |  |  * @param       cp                      the coding parameters for the image.  | 
1143  |  |  * @param       image           the image to encode.  | 
1144  |  |  * @param       p_j2k                   the p_j2k encoder.  | 
1145  |  |  * @param       p_manager       the user event manager.  | 
1146  |  |  *  | 
1147  |  |  * @return true if the function was successful, false else.  | 
1148  |  |  */  | 
1149  |  | static OPJ_BOOL opj_j2k_calculate_tp(   opj_j2k_t *p_j2k,  | 
1150  |  |                                                                             opj_cp_t *cp,  | 
1151  |  |                                                                             OPJ_UINT32 * p_nb_tiles,  | 
1152  |  |                                                                             opj_image_t *image,  | 
1153  |  |                                                                             opj_event_mgr_t * p_manager);  | 
1154  |  |  | 
1155  |  | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);  | 
1156  |  |  | 
1157  |  | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);  | 
1158  |  |  | 
1159  |  | static opj_codestream_index_t* opj_j2k_create_cstr_index(void);  | 
1160  |  |  | 
1161  |  | static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);  | 
1162  |  |  | 
1163  |  | static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);  | 
1164  |  |  | 
1165  |  | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);  | 
1166  |  |  | 
1167  |  | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);  | 
1168  |  |  | 
1169  |  | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager);  | 
1170  |  |  | 
1171  |  | /*@}*/  | 
1172  |  |  | 
1173  |  | /*@}*/  | 
1174  |  |  | 
1175  |  | /* ----------------------------------------------------------------------- */  | 
1176  |  | typedef struct j2k_prog_order{ | 
1177  |  |         OPJ_PROG_ORDER enum_prog;  | 
1178  |  |         char str_prog[5];  | 
1179  |  | }j2k_prog_order_t;  | 
1180  |  |  | 
1181  |  | j2k_prog_order_t j2k_prog_order_list[] = { | 
1182  |  |         {OPJ_CPRL, "CPRL"}, | 
1183  |  |         {OPJ_LRCP, "LRCP"}, | 
1184  |  |         {OPJ_PCRL, "PCRL"}, | 
1185  |  |         {OPJ_RLCP, "RLCP"}, | 
1186  |  |         {OPJ_RPCL, "RPCL"}, | 
1187  |  |         {(OPJ_PROG_ORDER)-1, ""} | 
1188  |  | };  | 
1189  |  |  | 
1190  |  | /**  | 
1191  |  |  * FIXME DOC  | 
1192  |  |  */  | 
1193  |  | static const OPJ_UINT32 MCT_ELEMENT_SIZE [] =  | 
1194  |  | { | 
1195  |  |         2,  | 
1196  |  |         4,  | 
1197  |  |         4,  | 
1198  |  |         8  | 
1199  |  | };  | 
1200  |  |  | 
1201  |  | typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);  | 
1202  |  |  | 
1203  |  | const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =  | 
1204  |  | { | 
1205  |  |         opj_j2k_read_int16_to_float,  | 
1206  |  |         opj_j2k_read_int32_to_float,  | 
1207  |  |         opj_j2k_read_float32_to_float,  | 
1208  |  |         opj_j2k_read_float64_to_float  | 
1209  |  | };  | 
1210  |  |  | 
1211  |  | const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =  | 
1212  |  | { | 
1213  |  |         opj_j2k_read_int16_to_int32,  | 
1214  |  |         opj_j2k_read_int32_to_int32,  | 
1215  |  |         opj_j2k_read_float32_to_int32,  | 
1216  |  |         opj_j2k_read_float64_to_int32  | 
1217  |  | };  | 
1218  |  |  | 
1219  |  | const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =  | 
1220  |  | { | 
1221  |  |         opj_j2k_write_float_to_int16,  | 
1222  |  |         opj_j2k_write_float_to_int32,  | 
1223  |  |         opj_j2k_write_float_to_float,  | 
1224  |  |         opj_j2k_write_float_to_float64  | 
1225  |  | };  | 
1226  |  |  | 
1227  |  | typedef struct opj_dec_memory_marker_handler  | 
1228  |  | { | 
1229  |  |         /** marker value */  | 
1230  |  |         OPJ_UINT32 id;  | 
1231  |  |         /** value of the state when the marker can appear */  | 
1232  |  |         OPJ_UINT32 states;  | 
1233  |  |         /** action linked to the marker */  | 
1234  |  |         OPJ_BOOL (*handler) (   opj_j2k_t *p_j2k,  | 
1235  |  |                             OPJ_BYTE * p_header_data,  | 
1236  |  |                             OPJ_UINT32 p_header_size,  | 
1237  |  |                             opj_event_mgr_t * p_manager );  | 
1238  |  | }  | 
1239  |  | opj_dec_memory_marker_handler_t;  | 
1240  |  |  | 
1241  |  | const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =  | 
1242  |  | { | 
1243  |  |   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, | 
1244  |  |   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, | 
1245  |  |   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, | 
1246  |  |   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, | 
1247  |  |   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, | 
1248  |  |   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, | 
1249  |  |   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, | 
1250  |  |   {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, | 
1251  |  |   {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, | 
1252  |  |   {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, | 
1253  |  |   {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, | 
1254  |  |   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3}, | 
1255  |  |   {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, | 
1256  |  |   {J2K_MS_SOP, 0, 0}, | 
1257  |  |   {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, | 
1258  |  |   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, | 
1259  |  |   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, | 
1260  |  |   {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd}, | 
1261  |  |   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, | 
1262  |  |   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, | 
1263  |  | #ifdef USE_JPWL  | 
1264  |  | #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */  | 
1265  |  |   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, | 
1266  |  |   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, | 
1267  |  |   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, | 
1268  |  |   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, | 
1269  |  | #endif  | 
1270  |  | #endif /* USE_JPWL */  | 
1271  |  | #ifdef USE_JPSEC  | 
1272  |  |   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, | 
1273  |  |   {J2K_MS_INSEC, 0, j2k_read_insec} | 
1274  |  | #endif /* USE_JPSEC */  | 
1275  |  |   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ | 
1276  |  | };  | 
1277  |  |  | 
1278  |  | void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1279  | 0  | { | 
1280  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1281  | 0  |         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;  | 
1282  | 0  |         OPJ_UINT32 i;  | 
1283  | 0  |         OPJ_UINT32 l_temp;  | 
1284  |  | 
  | 
1285  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1286  | 0  |                 opj_read_bytes(l_src_data,&l_temp,2);  | 
1287  |  | 
  | 
1288  | 0  |                 l_src_data+=sizeof(OPJ_INT16);  | 
1289  |  | 
  | 
1290  | 0  |                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;  | 
1291  | 0  |         }  | 
1292  | 0  | }  | 
1293  |  |  | 
1294  |  | void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1295  | 0  | { | 
1296  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1297  | 0  |         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;  | 
1298  | 0  |         OPJ_UINT32 i;  | 
1299  | 0  |         OPJ_UINT32 l_temp;  | 
1300  |  | 
  | 
1301  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1302  | 0  |                 opj_read_bytes(l_src_data,&l_temp,4);  | 
1303  |  | 
  | 
1304  | 0  |                 l_src_data+=sizeof(OPJ_INT32);  | 
1305  |  | 
  | 
1306  | 0  |                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;  | 
1307  | 0  |         }  | 
1308  | 0  | }  | 
1309  |  |  | 
1310  |  | void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1311  | 0  | { | 
1312  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1313  | 0  |         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;  | 
1314  | 0  |         OPJ_UINT32 i;  | 
1315  | 0  |         OPJ_FLOAT32 l_temp;  | 
1316  |  | 
  | 
1317  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1318  | 0  |                 opj_read_float(l_src_data,&l_temp);  | 
1319  |  | 
  | 
1320  | 0  |                 l_src_data+=sizeof(OPJ_FLOAT32);  | 
1321  |  | 
  | 
1322  | 0  |                 *(l_dest_data++) = l_temp;  | 
1323  | 0  |         }  | 
1324  | 0  | }  | 
1325  |  |  | 
1326  |  | void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1327  | 0  | { | 
1328  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1329  | 0  |         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;  | 
1330  | 0  |         OPJ_UINT32 i;  | 
1331  | 0  |         OPJ_FLOAT64 l_temp;  | 
1332  |  | 
  | 
1333  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1334  | 0  |                 opj_read_double(l_src_data,&l_temp);  | 
1335  |  | 
  | 
1336  | 0  |                 l_src_data+=sizeof(OPJ_FLOAT64);  | 
1337  |  | 
  | 
1338  | 0  |                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;  | 
1339  | 0  |         }  | 
1340  | 0  | }  | 
1341  |  |  | 
1342  |  | void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1343  | 0  | { | 
1344  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1345  | 0  |         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;  | 
1346  | 0  |         OPJ_UINT32 i;  | 
1347  | 0  |         OPJ_UINT32 l_temp;  | 
1348  |  | 
  | 
1349  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1350  | 0  |                 opj_read_bytes(l_src_data,&l_temp,2);  | 
1351  |  | 
  | 
1352  | 0  |                 l_src_data+=sizeof(OPJ_INT16);  | 
1353  |  | 
  | 
1354  | 0  |                 *(l_dest_data++) = (OPJ_INT32) l_temp;  | 
1355  | 0  |         }  | 
1356  | 0  | }  | 
1357  |  |  | 
1358  |  | void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1359  | 0  | { | 
1360  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1361  | 0  |         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;  | 
1362  | 0  |         OPJ_UINT32 i;  | 
1363  | 0  |         OPJ_UINT32 l_temp;  | 
1364  |  | 
  | 
1365  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1366  | 0  |                 opj_read_bytes(l_src_data,&l_temp,4);  | 
1367  |  | 
  | 
1368  | 0  |                 l_src_data+=sizeof(OPJ_INT32);  | 
1369  |  | 
  | 
1370  | 0  |                 *(l_dest_data++) = (OPJ_INT32) l_temp;  | 
1371  | 0  |         }  | 
1372  | 0  | }  | 
1373  |  |  | 
1374  |  | void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1375  | 0  | { | 
1376  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1377  | 0  |         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;  | 
1378  | 0  |         OPJ_UINT32 i;  | 
1379  | 0  |         OPJ_FLOAT32 l_temp;  | 
1380  |  | 
  | 
1381  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1382  | 0  |                 opj_read_float(l_src_data,&l_temp);  | 
1383  |  | 
  | 
1384  | 0  |                 l_src_data+=sizeof(OPJ_FLOAT32);  | 
1385  |  | 
  | 
1386  | 0  |                 *(l_dest_data++) = (OPJ_INT32) l_temp;  | 
1387  | 0  |         }  | 
1388  | 0  | }  | 
1389  |  |  | 
1390  |  | void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1391  | 0  | { | 
1392  | 0  |         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;  | 
1393  | 0  |         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;  | 
1394  | 0  |         OPJ_UINT32 i;  | 
1395  | 0  |         OPJ_FLOAT64 l_temp;  | 
1396  |  | 
  | 
1397  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1398  | 0  |                 opj_read_double(l_src_data,&l_temp);  | 
1399  |  | 
  | 
1400  | 0  |                 l_src_data+=sizeof(OPJ_FLOAT64);  | 
1401  |  | 
  | 
1402  | 0  |                 *(l_dest_data++) = (OPJ_INT32) l_temp;  | 
1403  | 0  |         }  | 
1404  | 0  | }  | 
1405  |  |  | 
1406  |  | void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1407  | 0  | { | 
1408  | 0  |         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;  | 
1409  | 0  |         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;  | 
1410  | 0  |         OPJ_UINT32 i;  | 
1411  | 0  |         OPJ_UINT32 l_temp;  | 
1412  |  | 
  | 
1413  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1414  | 0  |                 l_temp = (OPJ_UINT32) *(l_src_data++);  | 
1415  |  | 
  | 
1416  | 0  |                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));  | 
1417  |  | 
  | 
1418  | 0  |                 l_dest_data+=sizeof(OPJ_INT16);  | 
1419  | 0  |         }  | 
1420  | 0  | }  | 
1421  |  |  | 
1422  |  | void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1423  | 0  | { | 
1424  | 0  |         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;  | 
1425  | 0  |         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;  | 
1426  | 0  |         OPJ_UINT32 i;  | 
1427  | 0  |         OPJ_UINT32 l_temp;  | 
1428  |  | 
  | 
1429  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1430  | 0  |                 l_temp = (OPJ_UINT32) *(l_src_data++);  | 
1431  |  | 
  | 
1432  | 0  |                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));  | 
1433  |  | 
  | 
1434  | 0  |                 l_dest_data+=sizeof(OPJ_INT32);  | 
1435  | 0  |         }  | 
1436  | 0  | }  | 
1437  |  |  | 
1438  |  | void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1439  | 0  | { | 
1440  | 0  |         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;  | 
1441  | 0  |         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;  | 
1442  | 0  |         OPJ_UINT32 i;  | 
1443  | 0  |         OPJ_FLOAT32 l_temp;  | 
1444  |  | 
  | 
1445  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1446  | 0  |                 l_temp = (OPJ_FLOAT32) *(l_src_data++);  | 
1447  |  | 
  | 
1448  | 0  |                 opj_write_float(l_dest_data,l_temp);  | 
1449  |  | 
  | 
1450  | 0  |                 l_dest_data+=sizeof(OPJ_FLOAT32);  | 
1451  | 0  |         }  | 
1452  | 0  | }  | 
1453  |  |  | 
1454  |  | void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)  | 
1455  | 0  | { | 
1456  | 0  |         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;  | 
1457  | 0  |         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;  | 
1458  | 0  |         OPJ_UINT32 i;  | 
1459  | 0  |         OPJ_FLOAT64 l_temp;  | 
1460  |  | 
  | 
1461  | 0  |         for (i=0;i<p_nb_elem;++i) { | 
1462  | 0  |                 l_temp = (OPJ_FLOAT64) *(l_src_data++);  | 
1463  |  | 
  | 
1464  | 0  |                 opj_write_double(l_dest_data,l_temp);  | 
1465  |  | 
  | 
1466  | 0  |                 l_dest_data+=sizeof(OPJ_FLOAT64);  | 
1467  | 0  |         }  | 
1468  | 0  | }  | 
1469  |  |  | 
1470  | 0  | char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){ | 
1471  | 0  |         j2k_prog_order_t *po;  | 
1472  | 0  |         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ | 
1473  | 0  |                 if(po->enum_prog == prg_order){ | 
1474  | 0  |                         return po->str_prog;  | 
1475  | 0  |                 }  | 
1476  | 0  |         }  | 
1477  | 0  |         return po->str_prog;  | 
1478  | 0  | }  | 
1479  |  |  | 
1480  |  | OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,  | 
1481  |  |                                                         OPJ_UINT32 p_nb_pocs,  | 
1482  |  |                                                         OPJ_UINT32 p_nb_resolutions,  | 
1483  |  |                                                         OPJ_UINT32 p_num_comps,  | 
1484  |  |                                                         OPJ_UINT32 p_num_layers,  | 
1485  |  |                                                         opj_event_mgr_t * p_manager)  | 
1486  | 0  | { | 
1487  | 0  |         OPJ_UINT32* packet_array;  | 
1488  | 0  |         OPJ_UINT32 index , resno, compno, layno;  | 
1489  | 0  |         OPJ_UINT32 i;  | 
1490  | 0  |         OPJ_UINT32 step_c = 1;  | 
1491  | 0  |         OPJ_UINT32 step_r = p_num_comps * step_c;  | 
1492  | 0  |         OPJ_UINT32 step_l = p_nb_resolutions * step_r;  | 
1493  | 0  |         OPJ_BOOL loss = OPJ_FALSE;  | 
1494  | 0  |         OPJ_UINT32 layno0 = 0;  | 
1495  |  | 
  | 
1496  | 0  |         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));  | 
1497  | 0  |         if (packet_array == 00) { | 
1498  | 0  |                 opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");  | 
1499  | 0  |                 return OPJ_FALSE;  | 
1500  | 0  |         }  | 
1501  | 0  |         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));  | 
1502  |  | 
  | 
1503  | 0  |         if (p_nb_pocs == 0) { | 
1504  | 0  |         opj_free(packet_array);  | 
1505  | 0  |                 return OPJ_TRUE;  | 
1506  | 0  |         }  | 
1507  |  |  | 
1508  | 0  |         index = step_r * p_pocs->resno0;  | 
1509  |  |         /* take each resolution for each poc */  | 
1510  | 0  |         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)  | 
1511  | 0  |         { | 
1512  | 0  |                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;  | 
1513  |  |  | 
1514  |  |                 /* take each comp of each resolution for each poc */  | 
1515  | 0  |                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { | 
1516  | 0  |                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;  | 
1517  |  |  | 
1518  |  |                         /* and finally take each layer of each res of ... */  | 
1519  | 0  |                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { | 
1520  |  |                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/  | 
1521  | 0  |                                 packet_array[comp_index] = 1;  | 
1522  | 0  |                                 comp_index += step_l;  | 
1523  | 0  |                         }  | 
1524  |  | 
  | 
1525  | 0  |                         res_index += step_c;  | 
1526  | 0  |                 }  | 
1527  |  | 
  | 
1528  | 0  |                 index += step_r;  | 
1529  | 0  |         }  | 
1530  | 0  |         ++p_pocs;  | 
1531  |  |  | 
1532  |  |         /* iterate through all the pocs */  | 
1533  | 0  |         for (i = 1; i < p_nb_pocs ; ++i) { | 
1534  | 0  |                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;  | 
1535  |  | 
  | 
1536  | 0  |                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;  | 
1537  | 0  |                 index = step_r * p_pocs->resno0;  | 
1538  |  |  | 
1539  |  |                 /* take each resolution for each poc */  | 
1540  | 0  |                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { | 
1541  | 0  |                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;  | 
1542  |  |  | 
1543  |  |                         /* take each comp of each resolution for each poc */  | 
1544  | 0  |                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { | 
1545  | 0  |                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;  | 
1546  |  |  | 
1547  |  |                                 /* and finally take each layer of each res of ... */  | 
1548  | 0  |                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { | 
1549  |  |                                         /*index = step_r * resno + step_c * compno + step_l * layno;*/  | 
1550  | 0  |                                         packet_array[comp_index] = 1;  | 
1551  | 0  |                                         comp_index += step_l;  | 
1552  | 0  |                                 }  | 
1553  |  | 
  | 
1554  | 0  |                                 res_index += step_c;  | 
1555  | 0  |                         }  | 
1556  |  | 
  | 
1557  | 0  |                         index += step_r;  | 
1558  | 0  |                 }  | 
1559  |  | 
  | 
1560  | 0  |                 ++p_pocs;  | 
1561  | 0  |         }  | 
1562  |  | 
  | 
1563  | 0  |         index = 0;  | 
1564  | 0  |         for (layno = 0; layno < p_num_layers ; ++layno) { | 
1565  | 0  |                 for (resno = 0; resno < p_nb_resolutions; ++resno) { | 
1566  | 0  |                         for (compno = 0; compno < p_num_comps; ++compno) { | 
1567  | 0  |                                 loss |= (packet_array[index]!=1);  | 
1568  |  |                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/  | 
1569  | 0  |                                 index += step_c;  | 
1570  | 0  |                         }  | 
1571  | 0  |                 }  | 
1572  | 0  |         }  | 
1573  |  | 
  | 
1574  | 0  |         if (loss) { | 
1575  | 0  |                 opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");  | 
1576  | 0  |         }  | 
1577  |  | 
  | 
1578  | 0  |         opj_free(packet_array);  | 
1579  |  | 
  | 
1580  | 0  |         return !loss;  | 
1581  | 0  | }  | 
1582  |  |  | 
1583  |  | /* ----------------------------------------------------------------------- */  | 
1584  |  |  | 
1585  |  | OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)  | 
1586  | 0  | { | 
1587  | 0  |         const OPJ_CHAR *prog = 00;  | 
1588  | 0  |         OPJ_INT32 i;  | 
1589  | 0  |         OPJ_UINT32 tpnum = 1;  | 
1590  | 0  |         opj_tcp_t *tcp = 00;  | 
1591  | 0  |         opj_poc_t * l_current_poc = 00;  | 
1592  |  |  | 
1593  |  |         /*  preconditions */  | 
1594  | 0  |         assert(tileno < (cp->tw * cp->th));  | 
1595  | 0  |         assert(pino < (cp->tcps[tileno].numpocs + 1));  | 
1596  |  |  | 
1597  |  |         /* get the given tile coding parameter */  | 
1598  | 0  |         tcp = &cp->tcps[tileno];  | 
1599  | 0  |         assert(tcp != 00);  | 
1600  |  | 
  | 
1601  | 0  |         l_current_poc = &(tcp->pocs[pino]);  | 
1602  | 0  |         assert(l_current_poc != 0);  | 
1603  |  |  | 
1604  |  |         /* get the progression order as a character string */  | 
1605  | 0  |         prog = opj_j2k_convert_progression_order(tcp->prg);  | 
1606  | 0  |         assert(strlen(prog) > 0);  | 
1607  |  | 
  | 
1608  | 0  |         if (cp->m_specific_param.m_enc.m_tp_on == 1) { | 
1609  | 0  |                 for (i=0;i<4;++i) { | 
1610  | 0  |                         switch (prog[i])  | 
1611  | 0  |                         { | 
1612  |  |                                 /* component wise */  | 
1613  | 0  |                                 case 'C':  | 
1614  | 0  |                                         tpnum *= l_current_poc->compE;  | 
1615  | 0  |                                         break;  | 
1616  |  |                                 /* resolution wise */  | 
1617  | 0  |                                 case 'R':  | 
1618  | 0  |                                         tpnum *= l_current_poc->resE;  | 
1619  | 0  |                                         break;  | 
1620  |  |                                 /* precinct wise */  | 
1621  | 0  |                                 case 'P':  | 
1622  | 0  |                                         tpnum *= l_current_poc->prcE;  | 
1623  | 0  |                                         break;  | 
1624  |  |                                 /* layer wise */  | 
1625  | 0  |                                 case 'L':  | 
1626  | 0  |                                         tpnum *= l_current_poc->layE;  | 
1627  | 0  |                                         break;  | 
1628  | 0  |                         }  | 
1629  |  |                         /* whould we split here ? */  | 
1630  | 0  |                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) { | 
1631  | 0  |                                 cp->m_specific_param.m_enc.m_tp_pos=i;  | 
1632  | 0  |                                 break;  | 
1633  | 0  |                         }  | 
1634  | 0  |                 }  | 
1635  | 0  |         }  | 
1636  | 0  |         else { | 
1637  | 0  |                 tpnum=1;  | 
1638  | 0  |         }  | 
1639  |  |  | 
1640  | 0  |         return tpnum;  | 
1641  | 0  | }  | 
1642  |  |  | 
1643  |  | OPJ_BOOL opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,  | 
1644  |  |                                                         opj_cp_t *cp,  | 
1645  |  |                                                         OPJ_UINT32 * p_nb_tiles,  | 
1646  |  |                                                         opj_image_t *image,  | 
1647  |  |                                                         opj_event_mgr_t * p_manager  | 
1648  |  |                                 )  | 
1649  | 0  | { | 
1650  | 0  |         OPJ_UINT32 pino,tileno;  | 
1651  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
1652  | 0  |         opj_tcp_t *tcp;  | 
1653  |  |  | 
1654  |  |         /* preconditions */  | 
1655  | 0  |         assert(p_nb_tiles != 00);  | 
1656  | 0  |         assert(cp != 00);  | 
1657  | 0  |         assert(image != 00);  | 
1658  | 0  |         assert(p_j2k != 00);  | 
1659  | 0  |         assert(p_manager != 00);  | 
1660  |  | 
  | 
1661  | 0  |         l_nb_tiles = cp->tw * cp->th;  | 
1662  | 0  |         * p_nb_tiles = 0;  | 
1663  | 0  |         tcp = cp->tcps;  | 
1664  |  |  | 
1665  |  |         /* INDEX >> */  | 
1666  |  |         /* TODO mergeV2: check this part which use cstr_info */  | 
1667  |  |         /*if (p_j2k->cstr_info) { | 
1668  |  |                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;  | 
1669  |  |  | 
1670  |  |                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) { | 
1671  |  |                         OPJ_UINT32 cur_totnum_tp = 0;  | 
1672  |  |  | 
1673  |  |                         opj_pi_update_encoding_parameters(image,cp,tileno);  | 
1674  |  |  | 
1675  |  |                         for (pino = 0; pino <= tcp->numpocs; ++pino)  | 
1676  |  |                         { | 
1677  |  |                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);  | 
1678  |  |  | 
1679  |  |                                 *p_nb_tiles = *p_nb_tiles + tp_num;  | 
1680  |  |  | 
1681  |  |                                 cur_totnum_tp += tp_num;  | 
1682  |  |                         }  | 
1683  |  |  | 
1684  |  |                         tcp->m_nb_tile_parts = cur_totnum_tp;  | 
1685  |  |  | 
1686  |  |                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));  | 
1687  |  |                         if (l_info_tile_ptr->tp == 00) { | 
1688  |  |                                 return OPJ_FALSE;  | 
1689  |  |                         }  | 
1690  |  |  | 
1691  |  |                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));  | 
1692  |  |  | 
1693  |  |                         l_info_tile_ptr->num_tps = cur_totnum_tp;  | 
1694  |  |  | 
1695  |  |                         ++l_info_tile_ptr;  | 
1696  |  |                         ++tcp;  | 
1697  |  |                 }  | 
1698  |  |         }  | 
1699  |  |         else */{ | 
1700  | 0  |                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) { | 
1701  | 0  |                         OPJ_UINT32 cur_totnum_tp = 0;  | 
1702  |  | 
  | 
1703  | 0  |                         opj_pi_update_encoding_parameters(image,cp,tileno);  | 
1704  |  | 
  | 
1705  | 0  |                         for (pino = 0; pino <= tcp->numpocs; ++pino) { | 
1706  | 0  |                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);  | 
1707  |  | 
  | 
1708  | 0  |                                 *p_nb_tiles = *p_nb_tiles + tp_num;  | 
1709  |  | 
  | 
1710  | 0  |                                 cur_totnum_tp += tp_num;  | 
1711  | 0  |                         }  | 
1712  | 0  |                         tcp->m_nb_tile_parts = cur_totnum_tp;  | 
1713  |  | 
  | 
1714  | 0  |                         ++tcp;  | 
1715  | 0  |                 }  | 
1716  | 0  |         }  | 
1717  |  | 
  | 
1718  | 0  |         return OPJ_TRUE;  | 
1719  | 0  | }  | 
1720  |  |  | 
1721  |  | OPJ_BOOL opj_j2k_write_soc(     opj_j2k_t *p_j2k,  | 
1722  |  |                                                 opj_stream_private_t *p_stream,  | 
1723  |  |                                                     opj_event_mgr_t * p_manager )  | 
1724  | 0  | { | 
1725  |  |         /* 2 bytes will be written */  | 
1726  | 0  |         OPJ_BYTE * l_start_stream = 00;  | 
1727  |  |  | 
1728  |  |         /* preconditions */  | 
1729  | 0  |         assert(p_stream != 00);  | 
1730  | 0  |         assert(p_j2k != 00);  | 
1731  | 0  |         assert(p_manager != 00);  | 
1732  |  | 
  | 
1733  | 0  |         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
1734  |  |  | 
1735  |  |         /* write SOC identifier */  | 
1736  | 0  |         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);  | 
1737  |  | 
  | 
1738  | 0  |         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) { | 
1739  | 0  |                 return OPJ_FALSE;  | 
1740  | 0  |         }  | 
1741  |  |  | 
1742  |  | /* UniPG>> */  | 
1743  |  | #ifdef USE_JPWL  | 
1744  |  |         /* update markers struct */  | 
1745  |  | /*  | 
1746  |  |         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);  | 
1747  |  | */  | 
1748  |  |   assert( 0 && "TODO" );  | 
1749  |  | #endif /* USE_JPWL */  | 
1750  |  | /* <<UniPG */  | 
1751  |  |  | 
1752  | 0  |         return OPJ_TRUE;  | 
1753  | 0  | }  | 
1754  |  |  | 
1755  |  | /**  | 
1756  |  |  * Reads a SOC marker (Start of Codestream)  | 
1757  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
1758  |  |  * @param       p_stream        FIXME DOC  | 
1759  |  |  * @param       p_manager       the user event manager.  | 
1760  |  | */  | 
1761  |  | static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,  | 
1762  |  |                                     opj_stream_private_t *p_stream,  | 
1763  |  |                                     opj_event_mgr_t * p_manager  | 
1764  |  |                                     )  | 
1765  | 0  | { | 
1766  | 0  |         OPJ_BYTE l_data [2];  | 
1767  | 0  |         OPJ_UINT32 l_marker;  | 
1768  |  |  | 
1769  |  |         /* preconditions */  | 
1770  | 0  |         assert(p_j2k != 00);  | 
1771  | 0  |         assert(p_manager != 00);  | 
1772  | 0  |         assert(p_stream != 00);  | 
1773  |  | 
  | 
1774  | 0  |         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { | 
1775  | 0  |                 return OPJ_FALSE;  | 
1776  | 0  |         }  | 
1777  |  |  | 
1778  | 0  |         opj_read_bytes(l_data,&l_marker,2);  | 
1779  | 0  |         if (l_marker != J2K_MS_SOC) { | 
1780  | 0  |                 return OPJ_FALSE;  | 
1781  | 0  |         }  | 
1782  |  |  | 
1783  |  |         /* Next marker should be a SIZ marker in the main header */  | 
1784  | 0  |         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;  | 
1785  |  |  | 
1786  |  |         /* FIXME move it in a index structure included in p_j2k*/  | 
1787  | 0  |         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;  | 
1788  |  | 
  | 
1789  | 0  |         opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);  | 
1790  |  |  | 
1791  |  |         /* Add the marker to the codestream index*/  | 
1792  | 0  |         if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) { | 
1793  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");  | 
1794  | 0  |                 return OPJ_FALSE;  | 
1795  | 0  |         }  | 
1796  | 0  |         return OPJ_TRUE;  | 
1797  | 0  | }  | 
1798  |  |  | 
1799  |  | OPJ_BOOL opj_j2k_write_siz(     opj_j2k_t *p_j2k,  | 
1800  |  |                                                         opj_stream_private_t *p_stream,  | 
1801  |  |                                                         opj_event_mgr_t * p_manager )  | 
1802  | 0  | { | 
1803  | 0  |         OPJ_UINT32 i;  | 
1804  | 0  |         OPJ_UINT32 l_size_len;  | 
1805  | 0  |         OPJ_BYTE * l_current_ptr;  | 
1806  | 0  |         opj_image_t * l_image = 00;  | 
1807  | 0  |         opj_cp_t *cp = 00;  | 
1808  | 0  |         opj_image_comp_t * l_img_comp = 00;  | 
1809  |  |  | 
1810  |  |         /* preconditions */  | 
1811  | 0  |         assert(p_stream != 00);  | 
1812  | 0  |         assert(p_j2k != 00);  | 
1813  | 0  |         assert(p_manager != 00);  | 
1814  |  | 
  | 
1815  | 0  |         l_image = p_j2k->m_private_image;  | 
1816  | 0  |         cp = &(p_j2k->m_cp);  | 
1817  | 0  |         l_size_len = 40 + 3 * l_image->numcomps;  | 
1818  | 0  |         l_img_comp = l_image->comps;  | 
1819  |  | 
  | 
1820  | 0  |         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
1821  |  | 
  | 
1822  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);  | 
1823  | 0  |                 if (! new_header_tile_data) { | 
1824  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
1825  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
1826  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
1827  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");  | 
1828  | 0  |                         return OPJ_FALSE;  | 
1829  | 0  |                 }  | 
1830  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
1831  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;  | 
1832  | 0  |         }  | 
1833  |  |  | 
1834  | 0  |         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
1835  |  |  | 
1836  |  |         /* write SOC identifier */  | 
1837  | 0  |         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */  | 
1838  | 0  |         l_current_ptr+=2;  | 
1839  |  | 
  | 
1840  | 0  |         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */  | 
1841  | 0  |         l_current_ptr+=2;  | 
1842  |  | 
  | 
1843  | 0  |         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */  | 
1844  | 0  |         l_current_ptr+=2;  | 
1845  |  | 
  | 
1846  | 0  |         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */  | 
1847  | 0  |         l_current_ptr+=4;  | 
1848  |  | 
  | 
1849  | 0  |         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */  | 
1850  | 0  |         l_current_ptr+=4;  | 
1851  |  | 
  | 
1852  | 0  |         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */  | 
1853  | 0  |         l_current_ptr+=4;  | 
1854  |  | 
  | 
1855  | 0  |         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */  | 
1856  | 0  |         l_current_ptr+=4;  | 
1857  |  | 
  | 
1858  | 0  |         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */  | 
1859  | 0  |         l_current_ptr+=4;  | 
1860  |  | 
  | 
1861  | 0  |         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */  | 
1862  | 0  |         l_current_ptr+=4;  | 
1863  |  | 
  | 
1864  | 0  |         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */  | 
1865  | 0  |         l_current_ptr+=4;  | 
1866  |  | 
  | 
1867  | 0  |         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */  | 
1868  | 0  |         l_current_ptr+=4;  | 
1869  |  | 
  | 
1870  | 0  |         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */  | 
1871  | 0  |         l_current_ptr+=2;  | 
1872  |  | 
  | 
1873  | 0  |         for (i = 0; i < l_image->numcomps; ++i) { | 
1874  |  |                 /* TODO here with MCT ? */  | 
1875  | 0  |                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */  | 
1876  | 0  |                 ++l_current_ptr;  | 
1877  |  | 
  | 
1878  | 0  |                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */  | 
1879  | 0  |                 ++l_current_ptr;  | 
1880  |  | 
  | 
1881  | 0  |                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */  | 
1882  | 0  |                 ++l_current_ptr;  | 
1883  |  | 
  | 
1884  | 0  |                 ++l_img_comp;  | 
1885  | 0  |         }  | 
1886  |  | 
  | 
1887  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) { | 
1888  | 0  |                 return OPJ_FALSE;  | 
1889  | 0  |         }  | 
1890  |  |  | 
1891  | 0  |         return OPJ_TRUE;  | 
1892  | 0  | }  | 
1893  |  |  | 
1894  |  | /**  | 
1895  |  |  * Reads a SIZ marker (image and tile size)  | 
1896  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
1897  |  |  * @param       p_header_data   the data contained in the SIZ box.  | 
1898  |  |  * @param       p_header_size   the size of the data contained in the SIZ marker.  | 
1899  |  |  * @param       p_manager       the user event manager.  | 
1900  |  | */  | 
1901  |  | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,  | 
1902  |  |                                  OPJ_BYTE * p_header_data,  | 
1903  |  |                                  OPJ_UINT32 p_header_size,  | 
1904  |  |                                  opj_event_mgr_t * p_manager  | 
1905  |  |                                  )  | 
1906  | 0  | { | 
1907  | 0  |         OPJ_UINT32 i;  | 
1908  | 0  |         OPJ_UINT32 l_nb_comp;  | 
1909  | 0  |         OPJ_UINT32 l_nb_comp_remain;  | 
1910  | 0  |         OPJ_UINT32 l_remaining_size;  | 
1911  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
1912  | 0  |         OPJ_UINT32 l_tmp;  | 
1913  | 0  |         opj_image_t *l_image = 00;  | 
1914  | 0  |         opj_cp_t *l_cp = 00;  | 
1915  | 0  |         opj_image_comp_t * l_img_comp = 00;  | 
1916  | 0  |         opj_tcp_t * l_current_tile_param = 00;  | 
1917  |  |  | 
1918  |  |         /* preconditions */  | 
1919  | 0  |         assert(p_j2k != 00);  | 
1920  | 0  |         assert(p_manager != 00);  | 
1921  | 0  |         assert(p_header_data != 00);  | 
1922  |  | 
  | 
1923  | 0  |         l_image = p_j2k->m_private_image;  | 
1924  | 0  |         l_cp = &(p_j2k->m_cp);  | 
1925  |  |  | 
1926  |  |         /* minimum size == 39 - 3 (= minimum component parameter) */  | 
1927  | 0  |         if (p_header_size < 36) { | 
1928  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");  | 
1929  | 0  |                 return OPJ_FALSE;  | 
1930  | 0  |         }  | 
1931  |  |  | 
1932  | 0  |         l_remaining_size = p_header_size - 36;  | 
1933  | 0  |         l_nb_comp = l_remaining_size / 3;  | 
1934  | 0  |         l_nb_comp_remain = l_remaining_size % 3;  | 
1935  | 0  |         if (l_nb_comp_remain != 0){ | 
1936  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");  | 
1937  | 0  |                 return OPJ_FALSE;  | 
1938  | 0  |         }  | 
1939  |  |  | 
1940  | 0  |         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */  | 
1941  | 0  |         p_header_data+=2;  | 
1942  | 0  |         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;  | 
1943  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */  | 
1944  | 0  |         p_header_data+=4;  | 
1945  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */  | 
1946  | 0  |         p_header_data+=4;  | 
1947  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */  | 
1948  | 0  |         p_header_data+=4;  | 
1949  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */  | 
1950  | 0  |         p_header_data+=4;  | 
1951  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */  | 
1952  | 0  |         p_header_data+=4;  | 
1953  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */  | 
1954  | 0  |         p_header_data+=4;  | 
1955  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */  | 
1956  | 0  |         p_header_data+=4;  | 
1957  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */  | 
1958  | 0  |         p_header_data+=4;  | 
1959  | 0  |         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */  | 
1960  | 0  |         p_header_data+=2;  | 
1961  | 0  |         if (l_tmp < 16385)  | 
1962  | 0  |                 l_image->numcomps = (OPJ_UINT16) l_tmp;  | 
1963  | 0  |         else { | 
1964  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);  | 
1965  | 0  |                 return OPJ_FALSE;  | 
1966  | 0  |         }  | 
1967  |  |  | 
1968  | 0  |         if (l_image->numcomps != l_nb_comp) { | 
1969  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp);  | 
1970  | 0  |                 return OPJ_FALSE;  | 
1971  | 0  |         }  | 
1972  |  |  | 
1973  |  |         /* testcase 4035.pdf.SIGSEGV.d8b.3375 */  | 
1974  | 0  |         if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) { | 
1975  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);  | 
1976  | 0  |                 return OPJ_FALSE;  | 
1977  | 0  |         }  | 
1978  |  |         /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */  | 
1979  | 0  |         if (!(l_cp->tdx * l_cp->tdy)) { | 
1980  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy);  | 
1981  | 0  |                 return OPJ_FALSE;  | 
1982  | 0  |         }  | 
1983  |  |  | 
1984  |  |         /* testcase 1610.pdf.SIGSEGV.59c.681 */  | 
1985  | 0  |         if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) { | 
1986  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)", l_image->x1, l_image->y1);  | 
1987  | 0  |                 return OPJ_FALSE;  | 
1988  | 0  |         }  | 
1989  |  |  | 
1990  |  | #ifdef USE_JPWL  | 
1991  |  |         if (l_cp->correct) { | 
1992  |  |                 /* if JPWL is on, we check whether TX errors have damaged  | 
1993  |  |                   too much the SIZ parameters */  | 
1994  |  |                 if (!(l_image->x1 * l_image->y1)) { | 
1995  |  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
1996  |  |                                 "JPWL: bad image size (%d x %d)\n",  | 
1997  |  |                                 l_image->x1, l_image->y1);  | 
1998  |  |                         if (!JPWL_ASSUME || JPWL_ASSUME) { | 
1999  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2000  |  |                                 return OPJ_FALSE;  | 
2001  |  |                         }  | 
2002  |  |                 }  | 
2003  |  |  | 
2004  |  |         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?  | 
2005  |  |                 if (l_image->numcomps != ((len - 38) / 3)) { | 
2006  |  |                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,  | 
2007  |  |                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",  | 
2008  |  |                                 l_image->numcomps, ((len - 38) / 3));  | 
2009  |  |                         if (!JPWL_ASSUME) { | 
2010  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2011  |  |                                 return OPJ_FALSE;  | 
2012  |  |                         }  | 
2013  |  |         */              /* we try to correct */  | 
2014  |  |         /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");  | 
2015  |  |                         if (l_image->numcomps < ((len - 38) / 3)) { | 
2016  |  |                                 len = 38 + 3 * l_image->numcomps;  | 
2017  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",  | 
2018  |  |                                         len);  | 
2019  |  |                         } else { | 
2020  |  |                                 l_image->numcomps = ((len - 38) / 3);  | 
2021  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",  | 
2022  |  |                                         l_image->numcomps);  | 
2023  |  |                         }  | 
2024  |  |                 }  | 
2025  |  |         */  | 
2026  |  |  | 
2027  |  |                 /* update components number in the jpwl_exp_comps filed */  | 
2028  |  |                 l_cp->exp_comps = l_image->numcomps;  | 
2029  |  |         }  | 
2030  |  | #endif /* USE_JPWL */  | 
2031  |  |  | 
2032  |  |         /* Allocate the resulting image components */  | 
2033  | 0  |         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));  | 
2034  | 0  |         if (l_image->comps == 00){ | 
2035  | 0  |                 l_image->numcomps = 0;  | 
2036  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2037  | 0  |                 return OPJ_FALSE;  | 
2038  | 0  |         }  | 
2039  |  |  | 
2040  | 0  |         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));  | 
2041  | 0  |         l_img_comp = l_image->comps;  | 
2042  |  |  | 
2043  |  |         /* Read the component information */  | 
2044  | 0  |         for (i = 0; i < l_image->numcomps; ++i){ | 
2045  | 0  |                 OPJ_UINT32 tmp;  | 
2046  | 0  |                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */  | 
2047  | 0  |                 ++p_header_data;  | 
2048  | 0  |                 l_img_comp->prec = (tmp & 0x7f) + 1;  | 
2049  | 0  |                 l_img_comp->sgnd = tmp >> 7;  | 
2050  | 0  |                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */  | 
2051  | 0  |                 ++p_header_data;  | 
2052  | 0  |                 l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */  | 
2053  | 0  |                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */  | 
2054  | 0  |                 ++p_header_data;  | 
2055  | 0  |                 l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */  | 
2056  | 0  |                 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||  | 
2057  | 0  |                     l_img_comp->dy < 1 || l_img_comp->dy > 255 ) { | 
2058  | 0  |                     opj_event_msg(p_manager, EVT_ERROR,  | 
2059  | 0  |                                   "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",  | 
2060  | 0  |                                   i, l_img_comp->dx, l_img_comp->dy);  | 
2061  | 0  |                     return OPJ_FALSE;  | 
2062  | 0  |                 }  | 
2063  |  |  | 
2064  |  | #ifdef USE_JPWL  | 
2065  |  |                 if (l_cp->correct) { | 
2066  |  |                 /* if JPWL is on, we check whether TX errors have damaged  | 
2067  |  |                         too much the SIZ parameters, again */  | 
2068  |  |                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { | 
2069  |  |                                 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,  | 
2070  |  |                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",  | 
2071  |  |                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);  | 
2072  |  |                                 if (!JPWL_ASSUME) { | 
2073  |  |                                         opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2074  |  |                                         return OPJ_FALSE;  | 
2075  |  |                                 }  | 
2076  |  |                                 /* we try to correct */  | 
2077  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");  | 
2078  |  |                                 if (!l_image->comps[i].dx) { | 
2079  |  |                                         l_image->comps[i].dx = 1;  | 
2080  |  |                                         opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",  | 
2081  |  |                                                 i, l_image->comps[i].dx);  | 
2082  |  |                                 }  | 
2083  |  |                                 if (!l_image->comps[i].dy) { | 
2084  |  |                                         l_image->comps[i].dy = 1;  | 
2085  |  |                                         opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",  | 
2086  |  |                                                 i, l_image->comps[i].dy);  | 
2087  |  |                                 }  | 
2088  |  |                         }  | 
2089  |  |                 }  | 
2090  |  | #endif /* USE_JPWL */  | 
2091  | 0  |                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */  | 
2092  | 0  |                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */  | 
2093  | 0  |                 ++l_img_comp;  | 
2094  | 0  |         }  | 
2095  |  |  | 
2096  |  |         /* Compute the number of tiles */  | 
2097  | 0  |         l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);  | 
2098  | 0  |         l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);  | 
2099  |  |  | 
2100  |  |         /* Check that the number of tiles is valid */  | 
2101  | 0  |         if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { | 
2102  | 0  |             opj_event_msg(  p_manager, EVT_ERROR,   | 
2103  | 0  |                             "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",  | 
2104  | 0  |                             l_cp->tw, l_cp->th);  | 
2105  | 0  |             return OPJ_FALSE;  | 
2106  | 0  |         }  | 
2107  | 0  |         l_nb_tiles = l_cp->tw * l_cp->th;  | 
2108  |  |  | 
2109  |  |         /* Define the tiles which will be decoded */  | 
2110  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { | 
2111  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;  | 
2112  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;  | 
2113  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx);  | 
2114  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy);  | 
2115  | 0  |         }  | 
2116  | 0  |         else { | 
2117  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;  | 
2118  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;  | 
2119  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;  | 
2120  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;  | 
2121  | 0  |         }  | 
2122  |  | 
  | 
2123  |  | #ifdef USE_JPWL  | 
2124  |  |         if (l_cp->correct) { | 
2125  |  |                 /* if JPWL is on, we check whether TX errors have damaged  | 
2126  |  |                   too much the SIZ parameters */  | 
2127  |  |                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) { | 
2128  |  |                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,  | 
2129  |  |                                 "JPWL: bad number of tiles (%d x %d)\n",  | 
2130  |  |                                 l_cp->tw, l_cp->th);  | 
2131  |  |                         if (!JPWL_ASSUME) { | 
2132  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2133  |  |                                 return OPJ_FALSE;  | 
2134  |  |                         }  | 
2135  |  |                         /* we try to correct */  | 
2136  |  |                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");  | 
2137  |  |                         if (l_cp->tw < 1) { | 
2138  |  |                                 l_cp->tw= 1;  | 
2139  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",  | 
2140  |  |                                                 l_cp->tw);  | 
2141  |  |                         }  | 
2142  |  |                         if (l_cp->tw > l_cp->max_tiles) { | 
2143  |  |                                 l_cp->tw= 1;  | 
2144  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"  | 
2145  |  |                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",  | 
2146  |  |                                         l_cp->max_tiles, l_cp->tw);  | 
2147  |  |                         }  | 
2148  |  |                         if (l_cp->th < 1) { | 
2149  |  |                                 l_cp->th= 1;  | 
2150  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",  | 
2151  |  |                                                 l_cp->th);  | 
2152  |  |                         }  | 
2153  |  |                         if (l_cp->th > l_cp->max_tiles) { | 
2154  |  |                                 l_cp->th= 1;  | 
2155  |  |                                 opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",  | 
2156  |  |                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",  | 
2157  |  |                                         l_cp->max_tiles, l_cp->th);  | 
2158  |  |                         }  | 
2159  |  |                 }  | 
2160  |  |         }  | 
2161  |  | #endif /* USE_JPWL */  | 
2162  |  |  | 
2163  |  |         /* memory allocations */  | 
2164  | 0  |         l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));  | 
2165  | 0  |         if (l_cp->tcps == 00) { | 
2166  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2167  | 0  |                 return OPJ_FALSE;  | 
2168  | 0  |         }  | 
2169  | 0  |         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));  | 
2170  |  | 
  | 
2171  |  | #ifdef USE_JPWL  | 
2172  |  |         if (l_cp->correct) { | 
2173  |  |                 if (!l_cp->tcps) { | 
2174  |  |                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,  | 
2175  |  |                                 "JPWL: could not alloc tcps field of cp\n");  | 
2176  |  |                         if (!JPWL_ASSUME || JPWL_ASSUME) { | 
2177  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2178  |  |                                 return OPJ_FALSE;  | 
2179  |  |                         }  | 
2180  |  |                 }  | 
2181  |  |         }  | 
2182  |  | #endif /* USE_JPWL */  | 
2183  |  | 
  | 
2184  | 0  |         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =  | 
2185  | 0  |                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));  | 
2186  | 0  |         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) { | 
2187  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2188  | 0  |                 return OPJ_FALSE;  | 
2189  | 0  |         }  | 
2190  | 0  |         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));  | 
2191  |  | 
  | 
2192  | 0  |         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =  | 
2193  | 0  |                         (opj_mct_data_t*)opj_malloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));  | 
2194  |  | 
  | 
2195  | 0  |         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { | 
2196  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2197  | 0  |                 return OPJ_FALSE;  | 
2198  | 0  |         }  | 
2199  | 0  |         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));  | 
2200  | 0  |         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;  | 
2201  |  | 
  | 
2202  | 0  |         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =  | 
2203  | 0  |                         (opj_simple_mcc_decorrelation_data_t*)  | 
2204  | 0  |                         opj_malloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
2205  |  | 
  | 
2206  | 0  |         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { | 
2207  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2208  | 0  |                 return OPJ_FALSE;  | 
2209  | 0  |         }  | 
2210  | 0  |         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
2211  | 0  |         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;  | 
2212  |  |  | 
2213  |  |         /* set up default dc level shift */  | 
2214  | 0  |         for (i=0;i<l_image->numcomps;++i) { | 
2215  | 0  |                 if (! l_image->comps[i].sgnd) { | 
2216  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);  | 
2217  | 0  |                 }  | 
2218  | 0  |         }  | 
2219  |  | 
  | 
2220  | 0  |         l_current_tile_param = l_cp->tcps;  | 
2221  | 0  |         for     (i = 0; i < l_nb_tiles; ++i) { | 
2222  | 0  |                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));  | 
2223  | 0  |                 if (l_current_tile_param->tccps == 00) { | 
2224  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");  | 
2225  | 0  |                         return OPJ_FALSE;  | 
2226  | 0  |                 }  | 
2227  | 0  |                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));  | 
2228  |  | 
  | 
2229  | 0  |                 ++l_current_tile_param;  | 
2230  | 0  |         }  | 
2231  |  |  | 
2232  | 0  |         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */  | 
2233  | 0  |         opj_image_comp_header_update(l_image,l_cp);  | 
2234  |  | 
  | 
2235  | 0  |         return OPJ_TRUE;  | 
2236  | 0  | }  | 
2237  |  |  | 
2238  |  | OPJ_BOOL opj_j2k_write_com(     opj_j2k_t *p_j2k,  | 
2239  |  |                                                         opj_stream_private_t *p_stream,  | 
2240  |  |                                                         opj_event_mgr_t * p_manager  | 
2241  |  |                             )  | 
2242  | 0  | { | 
2243  | 0  |         OPJ_UINT32 l_comment_size;  | 
2244  | 0  |         OPJ_UINT32 l_total_com_size;  | 
2245  | 0  |         const OPJ_CHAR *l_comment;  | 
2246  | 0  |         OPJ_BYTE * l_current_ptr = 00;  | 
2247  |  |  | 
2248  |  |         /* preconditions */  | 
2249  | 0  |         assert(p_j2k != 00);  | 
2250  | 0  |         assert(p_stream != 00);  | 
2251  | 0  |         assert(p_manager != 00);  | 
2252  |  | 
  | 
2253  | 0  |         l_comment = p_j2k->m_cp.comment;  | 
2254  | 0  |         l_comment_size = (OPJ_UINT32)strlen(l_comment);  | 
2255  | 0  |         l_total_com_size = l_comment_size + 6;  | 
2256  |  | 
  | 
2257  | 0  |         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2258  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);  | 
2259  | 0  |                 if (! new_header_tile_data) { | 
2260  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2261  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2262  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2263  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");  | 
2264  | 0  |                         return OPJ_FALSE;  | 
2265  | 0  |                 }  | 
2266  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2267  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;  | 
2268  | 0  |         }  | 
2269  |  |  | 
2270  | 0  |         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
2271  |  | 
  | 
2272  | 0  |         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */  | 
2273  | 0  |         l_current_ptr+=2;  | 
2274  |  | 
  | 
2275  | 0  |         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */  | 
2276  | 0  |         l_current_ptr+=2;  | 
2277  |  | 
  | 
2278  | 0  |         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */  | 
2279  | 0  |         l_current_ptr+=2;  | 
2280  |  | 
  | 
2281  | 0  |         memcpy( l_current_ptr,l_comment,l_comment_size);  | 
2282  |  | 
  | 
2283  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) { | 
2284  | 0  |                 return OPJ_FALSE;  | 
2285  | 0  |         }  | 
2286  |  |  | 
2287  | 0  |         return OPJ_TRUE;  | 
2288  | 0  | }  | 
2289  |  |  | 
2290  |  | /**  | 
2291  |  |  * Reads a COM marker (comments)  | 
2292  |  |  * @param       p_j2k           the jpeg2000 file codec.  | 
2293  |  |  * @param       p_header_data   the data contained in the COM box.  | 
2294  |  |  * @param       p_header_size   the size of the data contained in the COM marker.  | 
2295  |  |  * @param       p_manager               the user event manager.  | 
2296  |  | */  | 
2297  |  | static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,  | 
2298  |  |                                     OPJ_BYTE * p_header_data,  | 
2299  |  |                                     OPJ_UINT32 p_header_size,  | 
2300  |  |                                     opj_event_mgr_t * p_manager  | 
2301  |  |                                     )  | 
2302  | 0  | { | 
2303  |  |         /* preconditions */  | 
2304  | 0  |         assert(p_j2k != 00);  | 
2305  | 0  |         assert(p_manager != 00);  | 
2306  | 0  |         assert(p_header_data != 00);  | 
2307  | 0  |   (void)p_header_size;  | 
2308  |  | 
  | 
2309  | 0  |         return OPJ_TRUE;  | 
2310  | 0  | }  | 
2311  |  |  | 
2312  |  | OPJ_BOOL opj_j2k_write_cod(     opj_j2k_t *p_j2k,  | 
2313  |  |                                                         opj_stream_private_t *p_stream,  | 
2314  |  |                                                         opj_event_mgr_t * p_manager )  | 
2315  | 0  | { | 
2316  | 0  |         opj_cp_t *l_cp = 00;  | 
2317  | 0  |         opj_tcp_t *l_tcp = 00;  | 
2318  | 0  |         OPJ_UINT32 l_code_size,l_remaining_size;  | 
2319  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
2320  |  |  | 
2321  |  |         /* preconditions */  | 
2322  | 0  |         assert(p_j2k != 00);  | 
2323  | 0  |         assert(p_manager != 00);  | 
2324  | 0  |         assert(p_stream != 00);  | 
2325  |  | 
  | 
2326  | 0  |         l_cp = &(p_j2k->m_cp);  | 
2327  | 0  |         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];  | 
2328  | 0  |         l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);  | 
2329  | 0  |         l_remaining_size = l_code_size;  | 
2330  |  | 
  | 
2331  | 0  |         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2332  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);  | 
2333  | 0  |                 if (! new_header_tile_data) { | 
2334  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2335  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2336  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2337  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");  | 
2338  | 0  |                         return OPJ_FALSE;  | 
2339  | 0  |                 }  | 
2340  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2341  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;  | 
2342  | 0  |         }  | 
2343  |  |  | 
2344  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
2345  |  | 
  | 
2346  | 0  |         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */  | 
2347  | 0  |         l_current_data += 2;  | 
2348  |  | 
  | 
2349  | 0  |         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */  | 
2350  | 0  |         l_current_data += 2;  | 
2351  |  | 
  | 
2352  | 0  |         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */  | 
2353  | 0  |         ++l_current_data;  | 
2354  |  | 
  | 
2355  | 0  |         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */  | 
2356  | 0  |         ++l_current_data;  | 
2357  |  | 
  | 
2358  | 0  |         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */  | 
2359  | 0  |         l_current_data+=2;  | 
2360  |  | 
  | 
2361  | 0  |         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */  | 
2362  | 0  |         ++l_current_data;  | 
2363  |  | 
  | 
2364  | 0  |         l_remaining_size -= 9;  | 
2365  |  | 
  | 
2366  | 0  |         if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { | 
2367  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");  | 
2368  | 0  |                 return OPJ_FALSE;  | 
2369  | 0  |         }  | 
2370  |  |  | 
2371  | 0  |         if (l_remaining_size != 0) { | 
2372  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");  | 
2373  | 0  |                 return OPJ_FALSE;  | 
2374  | 0  |         }  | 
2375  |  |  | 
2376  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) { | 
2377  | 0  |                 return OPJ_FALSE;  | 
2378  | 0  |         }  | 
2379  |  |  | 
2380  | 0  |         return OPJ_TRUE;  | 
2381  | 0  | }  | 
2382  |  |  | 
2383  |  | /**  | 
2384  |  |  * Reads a COD marker (Coding Styke defaults)  | 
2385  |  |  * @param       p_header_data   the data contained in the COD box.  | 
2386  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
2387  |  |  * @param       p_header_size   the size of the data contained in the COD marker.  | 
2388  |  |  * @param       p_manager               the user event manager.  | 
2389  |  | */  | 
2390  |  | static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,  | 
2391  |  |                                     OPJ_BYTE * p_header_data,  | 
2392  |  |                                     OPJ_UINT32 p_header_size,  | 
2393  |  |                                     opj_event_mgr_t * p_manager  | 
2394  |  |                                     )  | 
2395  | 0  | { | 
2396  |  |         /* loop */  | 
2397  | 0  |         OPJ_UINT32 i;  | 
2398  | 0  |         OPJ_UINT32 l_tmp;  | 
2399  | 0  |         opj_cp_t *l_cp = 00;  | 
2400  | 0  |         opj_tcp_t *l_tcp = 00;  | 
2401  | 0  |         opj_image_t *l_image = 00;  | 
2402  |  |  | 
2403  |  |         /* preconditions */  | 
2404  | 0  |         assert(p_header_data != 00);  | 
2405  | 0  |         assert(p_j2k != 00);  | 
2406  | 0  |         assert(p_manager != 00);  | 
2407  |  | 
  | 
2408  | 0  |         l_image = p_j2k->m_private_image;  | 
2409  | 0  |         l_cp = &(p_j2k->m_cp);  | 
2410  |  |  | 
2411  |  |         /* If we are in the first tile-part header of the current tile */  | 
2412  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?  | 
2413  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
2414  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
2415  |  |  | 
2416  |  |         /* Make sure room is sufficient */  | 
2417  | 0  |         if (p_header_size < 5) { | 
2418  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");  | 
2419  | 0  |                 return OPJ_FALSE;  | 
2420  | 0  |         }  | 
2421  |  |  | 
2422  | 0  |         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */  | 
2423  | 0  |         ++p_header_data;  | 
2424  | 0  |         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */  | 
2425  | 0  |         ++p_header_data;  | 
2426  | 0  |         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;  | 
2427  | 0  |         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */  | 
2428  | 0  |         p_header_data+=2;  | 
2429  |  |  | 
2430  |  |         /* If user didn't set a number layer to decode take the max specify in the codestream. */  | 
2431  | 0  |         if      (l_cp->m_specific_param.m_dec.m_layer) { | 
2432  | 0  |                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;  | 
2433  | 0  |         }  | 
2434  | 0  |         else { | 
2435  | 0  |                 l_tcp->num_layers_to_decode = l_tcp->numlayers;  | 
2436  | 0  |         }  | 
2437  |  | 
  | 
2438  | 0  |         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */  | 
2439  | 0  |         ++p_header_data;  | 
2440  |  | 
  | 
2441  | 0  |         p_header_size -= 5;  | 
2442  | 0  |         for     (i = 0; i < l_image->numcomps; ++i) { | 
2443  | 0  |                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;  | 
2444  | 0  |         }  | 
2445  |  | 
  | 
2446  | 0  |         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { | 
2447  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");  | 
2448  | 0  |                 return OPJ_FALSE;  | 
2449  | 0  |         }  | 
2450  |  |  | 
2451  | 0  |         if (p_header_size != 0) { | 
2452  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");  | 
2453  | 0  |                 return OPJ_FALSE;  | 
2454  | 0  |         }  | 
2455  |  |  | 
2456  |  |         /* Apply the coding style to other components of the current tile or the m_default_tcp*/  | 
2457  | 0  |         opj_j2k_copy_tile_component_parameters(p_j2k);  | 
2458  |  |  | 
2459  |  |         /* Index */  | 
2460  |  | #ifdef WIP_REMOVE_MSD  | 
2461  |  |         if (p_j2k->cstr_info) { | 
2462  |  |                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/  | 
2463  |  |                 p_j2k->cstr_info->prog = l_tcp->prg;  | 
2464  |  |                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;  | 
2465  |  |                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));  | 
2466  |  |                 for     (i = 0; i < l_image->numcomps; ++i) { | 
2467  |  |                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;  | 
2468  |  |                 }  | 
2469  |  |         }  | 
2470  |  | #endif  | 
2471  |  | 
  | 
2472  | 0  |         return OPJ_TRUE;  | 
2473  | 0  | }  | 
2474  |  |  | 
2475  |  | #if 0  | 
2476  |  | OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,  | 
2477  |  |                                                 OPJ_UINT32 p_comp_no,  | 
2478  |  |                                                 opj_stream_private_t *p_stream,  | 
2479  |  |                                                 opj_event_mgr_t * p_manager )  | 
2480  |  | { | 
2481  |  |         OPJ_UINT32 l_coc_size,l_remaining_size;  | 
2482  |  |         OPJ_UINT32 l_comp_room;  | 
2483  |  |  | 
2484  |  |         /* preconditions */  | 
2485  |  |         assert(p_j2k != 00);  | 
2486  |  |         assert(p_manager != 00);  | 
2487  |  |         assert(p_stream != 00);  | 
2488  |  |  | 
2489  |  |         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;  | 
2490  |  |  | 
2491  |  |         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);  | 
2492  |  |  | 
2493  |  |         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2494  |  |                 OPJ_BYTE *new_header_tile_data;  | 
2495  |  |                 /*p_j2k->m_specific_param.m_encoder.m_header_tile_data  | 
2496  |  |                         = (OPJ_BYTE*)opj_realloc(  | 
2497  |  |                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,  | 
2498  |  |                                 l_coc_size);*/  | 
2499  |  |  | 
2500  |  |                 new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);  | 
2501  |  |                 if (! new_header_tile_data) { | 
2502  |  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2503  |  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2504  |  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2505  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");  | 
2506  |  |                         return OPJ_FALSE;  | 
2507  |  |                 }  | 
2508  |  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2509  |  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;  | 
2510  |  |         }  | 
2511  |  |  | 
2512  |  |         opj_j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);  | 
2513  |  |  | 
2514  |  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) { | 
2515  |  |                 return OPJ_FALSE;  | 
2516  |  |         }  | 
2517  |  |  | 
2518  |  |         return OPJ_TRUE;  | 
2519  |  | }  | 
2520  |  | #endif  | 
2521  |  |  | 
2522  |  | #if 0  | 
2523  |  | void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,  | 
2524  |  |                                                 OPJ_UINT32 p_comp_no,  | 
2525  |  |                                                 OPJ_BYTE * p_data,  | 
2526  |  |                                                 OPJ_UINT32 * p_data_written,  | 
2527  |  |                                                 opj_event_mgr_t * p_manager  | 
2528  |  |                                     )  | 
2529  |  | { | 
2530  |  |         opj_cp_t *l_cp = 00;  | 
2531  |  |         opj_tcp_t *l_tcp = 00;  | 
2532  |  |         OPJ_UINT32 l_coc_size,l_remaining_size;  | 
2533  |  |         OPJ_BYTE * l_current_data = 00;  | 
2534  |  |         opj_image_t *l_image = 00;  | 
2535  |  |         OPJ_UINT32 l_comp_room;  | 
2536  |  |  | 
2537  |  |         /* preconditions */  | 
2538  |  |         assert(p_j2k != 00);  | 
2539  |  |         assert(p_manager != 00);  | 
2540  |  |  | 
2541  |  |         l_cp = &(p_j2k->m_cp);  | 
2542  |  |         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];  | 
2543  |  |         l_image = p_j2k->m_private_image;  | 
2544  |  |         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;  | 
2545  |  |  | 
2546  |  |         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);  | 
2547  |  |         l_remaining_size = l_coc_size;  | 
2548  |  |  | 
2549  |  |         l_current_data = p_data;  | 
2550  |  |  | 
2551  |  |         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */  | 
2552  |  |         l_current_data += 2;  | 
2553  |  |  | 
2554  |  |         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */  | 
2555  |  |         l_current_data += 2;  | 
2556  |  |  | 
2557  |  |         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */  | 
2558  |  |         l_current_data+=l_comp_room;  | 
2559  |  |  | 
2560  |  |         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */  | 
2561  |  |         ++l_current_data;  | 
2562  |  |  | 
2563  |  |         l_remaining_size -= (5 + l_comp_room);  | 
2564  |  |         opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);  | 
2565  |  |         * p_data_written = l_coc_size;  | 
2566  |  | }  | 
2567  |  | #endif  | 
2568  |  |  | 
2569  |  | OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)  | 
2570  | 0  | { | 
2571  | 0  |         OPJ_UINT32 i,j;  | 
2572  | 0  |         OPJ_UINT32 l_nb_comp;  | 
2573  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
2574  | 0  |         OPJ_UINT32 l_max = 0;  | 
2575  |  |  | 
2576  |  |         /* preconditions */  | 
2577  |  | 
  | 
2578  | 0  |         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;  | 
2579  | 0  |         l_nb_comp = p_j2k->m_private_image->numcomps;  | 
2580  |  | 
  | 
2581  | 0  |         for (i=0;i<l_nb_tiles;++i) { | 
2582  | 0  |                 for (j=0;j<l_nb_comp;++j) { | 
2583  | 0  |                         l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));  | 
2584  | 0  |                 }  | 
2585  | 0  |         }  | 
2586  |  | 
  | 
2587  | 0  |         return 6 + l_max;  | 
2588  | 0  | }  | 
2589  |  |  | 
2590  |  | /**  | 
2591  |  |  * Reads a COC marker (Coding Style Component)  | 
2592  |  |  * @param       p_header_data   the data contained in the COC box.  | 
2593  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
2594  |  |  * @param       p_header_size   the size of the data contained in the COC marker.  | 
2595  |  |  * @param       p_manager               the user event manager.  | 
2596  |  | */  | 
2597  |  | static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,  | 
2598  |  |                                     OPJ_BYTE * p_header_data,  | 
2599  |  |                                     OPJ_UINT32 p_header_size,  | 
2600  |  |                                     opj_event_mgr_t * p_manager  | 
2601  |  |                                     )  | 
2602  | 0  | { | 
2603  | 0  |         opj_cp_t *l_cp = NULL;  | 
2604  | 0  |         opj_tcp_t *l_tcp = NULL;  | 
2605  | 0  |         opj_image_t *l_image = NULL;  | 
2606  | 0  |         OPJ_UINT32 l_comp_room;  | 
2607  | 0  |         OPJ_UINT32 l_comp_no;  | 
2608  |  |  | 
2609  |  |         /* preconditions */  | 
2610  | 0  |         assert(p_header_data != 00);  | 
2611  | 0  |         assert(p_j2k != 00);  | 
2612  | 0  |         assert(p_manager != 00);  | 
2613  |  | 
  | 
2614  | 0  |         l_cp = &(p_j2k->m_cp);  | 
2615  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/  | 
2616  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
2617  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
2618  | 0  |         l_image = p_j2k->m_private_image;  | 
2619  |  | 
  | 
2620  | 0  |         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;  | 
2621  |  |  | 
2622  |  |         /* make sure room is sufficient*/  | 
2623  | 0  |         if (p_header_size < l_comp_room + 1) { | 
2624  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");  | 
2625  | 0  |                 return OPJ_FALSE;  | 
2626  | 0  |         }  | 
2627  | 0  |         p_header_size -= l_comp_room + 1;  | 
2628  |  | 
  | 
2629  | 0  |         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */  | 
2630  | 0  |         p_header_data += l_comp_room;  | 
2631  | 0  |         if (l_comp_no >= l_image->numcomps) { | 
2632  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");  | 
2633  | 0  |                 return OPJ_FALSE;  | 
2634  | 0  |         }  | 
2635  |  |  | 
2636  | 0  |         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */  | 
2637  | 0  |         ++p_header_data ;  | 
2638  |  | 
  | 
2639  | 0  |         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { | 
2640  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");  | 
2641  | 0  |                 return OPJ_FALSE;  | 
2642  | 0  |         }  | 
2643  |  |  | 
2644  | 0  |         if (p_header_size != 0) { | 
2645  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");  | 
2646  | 0  |                 return OPJ_FALSE;  | 
2647  | 0  |         }  | 
2648  | 0  |         return OPJ_TRUE;  | 
2649  | 0  | }  | 
2650  |  |  | 
2651  |  | OPJ_BOOL opj_j2k_write_qcd(     opj_j2k_t *p_j2k,  | 
2652  |  |                                                         opj_stream_private_t *p_stream,  | 
2653  |  |                                                         opj_event_mgr_t * p_manager  | 
2654  |  |                             )  | 
2655  | 0  | { | 
2656  | 0  |         OPJ_UINT32 l_qcd_size,l_remaining_size;  | 
2657  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
2658  |  |  | 
2659  |  |         /* preconditions */  | 
2660  | 0  |         assert(p_j2k != 00);  | 
2661  | 0  |         assert(p_manager != 00);  | 
2662  | 0  |         assert(p_stream != 00);  | 
2663  |  | 
  | 
2664  | 0  |         l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);  | 
2665  | 0  |         l_remaining_size = l_qcd_size;  | 
2666  |  | 
  | 
2667  | 0  |         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2668  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);  | 
2669  | 0  |                 if (! new_header_tile_data) { | 
2670  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2671  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2672  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2673  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");  | 
2674  | 0  |                         return OPJ_FALSE;  | 
2675  | 0  |                 }  | 
2676  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2677  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;  | 
2678  | 0  |         }  | 
2679  |  |  | 
2680  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
2681  |  | 
  | 
2682  | 0  |         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */  | 
2683  | 0  |         l_current_data += 2;  | 
2684  |  | 
  | 
2685  | 0  |         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */  | 
2686  | 0  |         l_current_data += 2;  | 
2687  |  | 
  | 
2688  | 0  |         l_remaining_size -= 4;  | 
2689  |  | 
  | 
2690  | 0  |         if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { | 
2691  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");  | 
2692  | 0  |                 return OPJ_FALSE;  | 
2693  | 0  |         }  | 
2694  |  |  | 
2695  | 0  |         if (l_remaining_size != 0) { | 
2696  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");  | 
2697  | 0  |                 return OPJ_FALSE;  | 
2698  | 0  |         }  | 
2699  |  |  | 
2700  | 0  |         if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) { | 
2701  | 0  |                 return OPJ_FALSE;  | 
2702  | 0  |         }  | 
2703  |  |  | 
2704  | 0  |         return OPJ_TRUE;  | 
2705  | 0  | }  | 
2706  |  |  | 
2707  |  | /**  | 
2708  |  |  * Reads a QCD marker (Quantization defaults)  | 
2709  |  |  * @param       p_header_data   the data contained in the QCD box.  | 
2710  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
2711  |  |  * @param       p_header_size   the size of the data contained in the QCD marker.  | 
2712  |  |  * @param       p_manager               the user event manager.  | 
2713  |  | */  | 
2714  |  | static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,  | 
2715  |  |                                     OPJ_BYTE * p_header_data,  | 
2716  |  |                                     OPJ_UINT32 p_header_size,  | 
2717  |  |                                     opj_event_mgr_t * p_manager  | 
2718  |  |                                     )  | 
2719  | 0  | { | 
2720  |  |         /* preconditions */  | 
2721  | 0  |         assert(p_header_data != 00);  | 
2722  | 0  |         assert(p_j2k != 00);  | 
2723  | 0  |         assert(p_manager != 00);  | 
2724  |  | 
  | 
2725  | 0  |         if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { | 
2726  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");  | 
2727  | 0  |                 return OPJ_FALSE;  | 
2728  | 0  |         }  | 
2729  |  |  | 
2730  | 0  |         if (p_header_size != 0) { | 
2731  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");  | 
2732  | 0  |                 return OPJ_FALSE;  | 
2733  | 0  |         }  | 
2734  |  |  | 
2735  |  |         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */  | 
2736  | 0  |         opj_j2k_copy_tile_quantization_parameters(p_j2k);  | 
2737  |  | 
  | 
2738  | 0  |         return OPJ_TRUE;  | 
2739  | 0  | }  | 
2740  |  |  | 
2741  |  | #if 0  | 
2742  |  | OPJ_BOOL opj_j2k_write_qcc(     opj_j2k_t *p_j2k,  | 
2743  |  |                                                 OPJ_UINT32 p_comp_no,  | 
2744  |  |                                                 opj_stream_private_t *p_stream,  | 
2745  |  |                                                 opj_event_mgr_t * p_manager  | 
2746  |  |                             )  | 
2747  |  | { | 
2748  |  |         OPJ_UINT32 l_qcc_size,l_remaining_size;  | 
2749  |  |  | 
2750  |  |         /* preconditions */  | 
2751  |  |         assert(p_j2k != 00);  | 
2752  |  |         assert(p_manager != 00);  | 
2753  |  |         assert(p_stream != 00);  | 
2754  |  |  | 
2755  |  |         l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);  | 
2756  |  |         l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;  | 
2757  |  |         l_remaining_size = l_qcc_size;  | 
2758  |  |  | 
2759  |  |         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2760  |  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);  | 
2761  |  |                 if (! new_header_tile_data) { | 
2762  |  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2763  |  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2764  |  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2765  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");  | 
2766  |  |                         return OPJ_FALSE;  | 
2767  |  |                 }  | 
2768  |  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2769  |  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;  | 
2770  |  |         }  | 
2771  |  |  | 
2772  |  |         opj_j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);  | 
2773  |  |  | 
2774  |  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) { | 
2775  |  |                 return OPJ_FALSE;  | 
2776  |  |         }  | 
2777  |  |  | 
2778  |  |         return OPJ_TRUE;  | 
2779  |  | }  | 
2780  |  | #endif  | 
2781  |  |  | 
2782  |  | #if 0  | 
2783  |  | void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,  | 
2784  |  |                                                                 OPJ_UINT32 p_comp_no,  | 
2785  |  |                                                                 OPJ_BYTE * p_data,  | 
2786  |  |                                                                 OPJ_UINT32 * p_data_written,  | 
2787  |  |                                                                 opj_event_mgr_t * p_manager  | 
2788  |  |                                     )  | 
2789  |  | { | 
2790  |  |         OPJ_UINT32 l_qcc_size,l_remaining_size;  | 
2791  |  |         OPJ_BYTE * l_current_data = 00;  | 
2792  |  |  | 
2793  |  |         /* preconditions */  | 
2794  |  |         assert(p_j2k != 00);  | 
2795  |  |         assert(p_manager != 00);  | 
2796  |  |  | 
2797  |  |         l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);  | 
2798  |  |         l_remaining_size = l_qcc_size;  | 
2799  |  |  | 
2800  |  |         l_current_data = p_data;  | 
2801  |  |  | 
2802  |  |         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */  | 
2803  |  |         l_current_data += 2;  | 
2804  |  |  | 
2805  |  |         if (p_j2k->m_private_image->numcomps <= 256) { | 
2806  |  |                 --l_qcc_size;  | 
2807  |  |  | 
2808  |  |                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */  | 
2809  |  |                 l_current_data += 2;  | 
2810  |  |  | 
2811  |  |                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */  | 
2812  |  |                 ++l_current_data;  | 
2813  |  |  | 
2814  |  |                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */  | 
2815  |  |                 l_remaining_size -= 6;  | 
2816  |  |         }  | 
2817  |  |         else { | 
2818  |  |                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */  | 
2819  |  |                 l_current_data += 2;  | 
2820  |  |  | 
2821  |  |                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */  | 
2822  |  |                 l_current_data+=2;  | 
2823  |  |  | 
2824  |  |                 l_remaining_size -= 6;  | 
2825  |  |         }  | 
2826  |  |  | 
2827  |  |         opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);  | 
2828  |  |  | 
2829  |  |         *p_data_written = l_qcc_size;  | 
2830  |  | }  | 
2831  |  | #endif  | 
2832  |  |  | 
2833  |  | OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)  | 
2834  | 0  | { | 
2835  | 0  |         return opj_j2k_get_max_coc_size(p_j2k);  | 
2836  | 0  | }  | 
2837  |  |  | 
2838  |  | /**  | 
2839  |  |  * Reads a QCC marker (Quantization component)  | 
2840  |  |  * @param       p_header_data   the data contained in the QCC box.  | 
2841  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
2842  |  |  * @param       p_header_size   the size of the data contained in the QCC marker.  | 
2843  |  |  * @param       p_manager               the user event manager.  | 
2844  |  | */  | 
2845  |  | static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,  | 
2846  |  |                                     OPJ_BYTE * p_header_data,  | 
2847  |  |                                     OPJ_UINT32 p_header_size,  | 
2848  |  |                                     opj_event_mgr_t * p_manager  | 
2849  |  |                                     )  | 
2850  | 0  | { | 
2851  | 0  |         OPJ_UINT32 l_num_comp,l_comp_no;  | 
2852  |  |  | 
2853  |  |         /* preconditions */  | 
2854  | 0  |         assert(p_header_data != 00);  | 
2855  | 0  |         assert(p_j2k != 00);  | 
2856  | 0  |         assert(p_manager != 00);  | 
2857  |  | 
  | 
2858  | 0  |         l_num_comp = p_j2k->m_private_image->numcomps;  | 
2859  |  | 
  | 
2860  | 0  |         if (l_num_comp <= 256) { | 
2861  | 0  |                 if (p_header_size < 1) { | 
2862  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");  | 
2863  | 0  |                         return OPJ_FALSE;  | 
2864  | 0  |                 }  | 
2865  | 0  |                 opj_read_bytes(p_header_data,&l_comp_no,1);  | 
2866  | 0  |                 ++p_header_data;  | 
2867  | 0  |                 --p_header_size;  | 
2868  | 0  |         }  | 
2869  | 0  |         else { | 
2870  | 0  |                 if (p_header_size < 2) { | 
2871  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");  | 
2872  | 0  |                         return OPJ_FALSE;  | 
2873  | 0  |                 }  | 
2874  | 0  |                 opj_read_bytes(p_header_data,&l_comp_no,2);  | 
2875  | 0  |                 p_header_data+=2;  | 
2876  | 0  |                 p_header_size-=2;  | 
2877  | 0  |         }  | 
2878  |  |  | 
2879  |  | #ifdef USE_JPWL  | 
2880  |  |         if (p_j2k->m_cp.correct) { | 
2881  |  |  | 
2882  |  |                 static OPJ_UINT32 backup_compno = 0;  | 
2883  |  |  | 
2884  |  |                 /* compno is negative or larger than the number of components!!! */  | 
2885  |  |                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { | 
2886  |  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
2887  |  |                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",  | 
2888  |  |                                 l_comp_no, l_num_comp);  | 
2889  |  |                         if (!JPWL_ASSUME) { | 
2890  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
2891  |  |                                 return OPJ_FALSE;  | 
2892  |  |                         }  | 
2893  |  |                         /* we try to correct */  | 
2894  |  |                         l_comp_no = backup_compno % l_num_comp;  | 
2895  |  |                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"  | 
2896  |  |                                 "- setting component number to %d\n",  | 
2897  |  |                                 l_comp_no);  | 
2898  |  |                 }  | 
2899  |  |  | 
2900  |  |                 /* keep your private count of tiles */  | 
2901  |  |                 backup_compno++;  | 
2902  |  |         };  | 
2903  |  | #endif /* USE_JPWL */  | 
2904  |  |  | 
2905  | 0  |         if (l_comp_no >= p_j2k->m_private_image->numcomps) { | 
2906  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
2907  | 0  |                               "Invalid component number: %d, regarding the number of components %d\n",  | 
2908  | 0  |                               l_comp_no, p_j2k->m_private_image->numcomps);  | 
2909  | 0  |                 return OPJ_FALSE;  | 
2910  | 0  |         }  | 
2911  |  |  | 
2912  | 0  |         if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { | 
2913  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");  | 
2914  | 0  |                 return OPJ_FALSE;  | 
2915  | 0  |         }  | 
2916  |  |  | 
2917  | 0  |         if (p_header_size != 0) { | 
2918  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");  | 
2919  | 0  |                 return OPJ_FALSE;  | 
2920  | 0  |         }  | 
2921  |  |  | 
2922  | 0  |         return OPJ_TRUE;  | 
2923  | 0  | }  | 
2924  |  |  | 
2925  |  | OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,  | 
2926  |  |                                                         opj_stream_private_t *p_stream,  | 
2927  |  |                                                         opj_event_mgr_t * p_manager  | 
2928  |  |                             )  | 
2929  | 0  | { | 
2930  | 0  |         OPJ_UINT32 l_nb_comp;  | 
2931  | 0  |         OPJ_UINT32 l_nb_poc;  | 
2932  | 0  |         OPJ_UINT32 l_poc_size;  | 
2933  | 0  |         OPJ_UINT32 l_written_size = 0;  | 
2934  | 0  |         opj_tcp_t *l_tcp = 00;  | 
2935  | 0  |         OPJ_UINT32 l_poc_room;  | 
2936  |  |  | 
2937  |  |         /* preconditions */  | 
2938  | 0  |         assert(p_j2k != 00);  | 
2939  | 0  |         assert(p_manager != 00);  | 
2940  | 0  |         assert(p_stream != 00);  | 
2941  |  | 
  | 
2942  | 0  |         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];  | 
2943  | 0  |         l_nb_comp = p_j2k->m_private_image->numcomps;  | 
2944  | 0  |         l_nb_poc = 1 + l_tcp->numpocs;  | 
2945  |  | 
  | 
2946  | 0  |         if (l_nb_comp <= 256) { | 
2947  | 0  |                 l_poc_room = 1;  | 
2948  | 0  |         }  | 
2949  | 0  |         else { | 
2950  | 0  |                 l_poc_room = 2;  | 
2951  | 0  |         }  | 
2952  | 0  |         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;  | 
2953  |  | 
  | 
2954  | 0  |         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
2955  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);  | 
2956  | 0  |                 if (! new_header_tile_data) { | 
2957  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
2958  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
2959  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
2960  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");  | 
2961  | 0  |                         return OPJ_FALSE;  | 
2962  | 0  |                 }  | 
2963  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
2964  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;  | 
2965  | 0  |         }  | 
2966  |  |  | 
2967  | 0  |         opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);  | 
2968  |  | 
  | 
2969  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) { | 
2970  | 0  |                 return OPJ_FALSE;  | 
2971  | 0  |         }  | 
2972  |  |  | 
2973  | 0  |         return OPJ_TRUE;  | 
2974  | 0  | }  | 
2975  |  |  | 
2976  |  | void opj_j2k_write_poc_in_memory(   opj_j2k_t *p_j2k,  | 
2977  |  |                                                                 OPJ_BYTE * p_data,  | 
2978  |  |                                                                 OPJ_UINT32 * p_data_written,  | 
2979  |  |                                                                 opj_event_mgr_t * p_manager  | 
2980  |  |                                     )  | 
2981  | 0  | { | 
2982  | 0  |         OPJ_UINT32 i;  | 
2983  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
2984  | 0  |         OPJ_UINT32 l_nb_comp;  | 
2985  | 0  |         OPJ_UINT32 l_nb_poc;  | 
2986  | 0  |         OPJ_UINT32 l_poc_size;  | 
2987  | 0  |         opj_image_t *l_image = 00;  | 
2988  | 0  |         opj_tcp_t *l_tcp = 00;  | 
2989  | 0  |         opj_tccp_t *l_tccp = 00;  | 
2990  | 0  |         opj_poc_t *l_current_poc = 00;  | 
2991  | 0  |         OPJ_UINT32 l_poc_room;  | 
2992  |  |  | 
2993  |  |         /* preconditions */  | 
2994  | 0  |         assert(p_j2k != 00);  | 
2995  | 0  |         assert(p_manager != 00);  | 
2996  |  | 
  | 
2997  | 0  |         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];  | 
2998  | 0  |         l_tccp = &l_tcp->tccps[0];  | 
2999  | 0  |         l_image = p_j2k->m_private_image;  | 
3000  | 0  |         l_nb_comp = l_image->numcomps;  | 
3001  | 0  |         l_nb_poc = 1 + l_tcp->numpocs;  | 
3002  |  | 
  | 
3003  | 0  |         if (l_nb_comp <= 256) { | 
3004  | 0  |                 l_poc_room = 1;  | 
3005  | 0  |         }  | 
3006  | 0  |         else { | 
3007  | 0  |                 l_poc_room = 2;  | 
3008  | 0  |         }  | 
3009  |  | 
  | 
3010  | 0  |         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;  | 
3011  |  | 
  | 
3012  | 0  |         l_current_data = p_data;  | 
3013  |  | 
  | 
3014  | 0  |         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */  | 
3015  | 0  |         l_current_data += 2;  | 
3016  |  | 
  | 
3017  | 0  |         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */  | 
3018  | 0  |         l_current_data += 2;  | 
3019  |  | 
  | 
3020  | 0  |         l_current_poc =  l_tcp->pocs;  | 
3021  | 0  |         for (i = 0; i < l_nb_poc; ++i) { | 
3022  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */  | 
3023  | 0  |                 ++l_current_data;  | 
3024  |  | 
  | 
3025  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */  | 
3026  | 0  |                 l_current_data+=l_poc_room;  | 
3027  |  | 
  | 
3028  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */  | 
3029  | 0  |                 l_current_data+=2;  | 
3030  |  | 
  | 
3031  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */  | 
3032  | 0  |                 ++l_current_data;  | 
3033  |  | 
  | 
3034  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */  | 
3035  | 0  |                 l_current_data+=l_poc_room;  | 
3036  |  | 
  | 
3037  | 0  |                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */  | 
3038  | 0  |                 ++l_current_data;  | 
3039  |  |  | 
3040  |  |                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/  | 
3041  | 0  |                 l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);  | 
3042  | 0  |                 l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);  | 
3043  | 0  |                 l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);  | 
3044  |  | 
  | 
3045  | 0  |                 ++l_current_poc;  | 
3046  | 0  |         }  | 
3047  |  | 
  | 
3048  | 0  |         *p_data_written = l_poc_size;  | 
3049  | 0  | }  | 
3050  |  |  | 
3051  |  | OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)  | 
3052  | 0  | { | 
3053  | 0  |         opj_tcp_t * l_tcp = 00;  | 
3054  | 0  |         OPJ_UINT32 l_nb_tiles = 0;  | 
3055  | 0  |         OPJ_UINT32 l_max_poc = 0;  | 
3056  | 0  |         OPJ_UINT32 i;  | 
3057  |  | 
  | 
3058  | 0  |         l_tcp = p_j2k->m_cp.tcps;  | 
3059  | 0  |         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
3060  |  | 
  | 
3061  | 0  |         for (i=0;i<l_nb_tiles;++i) { | 
3062  | 0  |                 l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);  | 
3063  | 0  |                 ++l_tcp;  | 
3064  | 0  |         }  | 
3065  |  | 
  | 
3066  | 0  |         ++l_max_poc;  | 
3067  |  | 
  | 
3068  | 0  |         return 4 + 9 * l_max_poc;  | 
3069  | 0  | }  | 
3070  |  |  | 
3071  |  | OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)  | 
3072  | 0  | { | 
3073  | 0  |         OPJ_UINT32 i;  | 
3074  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
3075  | 0  |         OPJ_UINT32 l_max = 0;  | 
3076  | 0  |         opj_tcp_t * l_tcp = 00;  | 
3077  |  | 
  | 
3078  | 0  |         l_tcp = p_j2k->m_cp.tcps;  | 
3079  | 0  |         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;  | 
3080  |  | 
  | 
3081  | 0  |         for (i=0;i<l_nb_tiles;++i) { | 
3082  | 0  |                 l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);  | 
3083  |  | 
  | 
3084  | 0  |                 ++l_tcp;  | 
3085  | 0  |         }  | 
3086  |  | 
  | 
3087  | 0  |         return 12 * l_max;  | 
3088  | 0  | }  | 
3089  |  |  | 
3090  |  | OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)  | 
3091  | 0  | { | 
3092  | 0  |         OPJ_UINT32 l_nb_bytes = 0;  | 
3093  | 0  |         OPJ_UINT32 l_nb_comps;  | 
3094  | 0  |         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;  | 
3095  |  | 
  | 
3096  | 0  |         l_nb_comps = p_j2k->m_private_image->numcomps - 1;  | 
3097  | 0  |         l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);  | 
3098  |  | 
  | 
3099  | 0  |         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) { | 
3100  | 0  |                 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);  | 
3101  | 0  |                 l_nb_bytes += l_nb_comps * l_coc_bytes;  | 
3102  |  | 
  | 
3103  | 0  |                 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);  | 
3104  | 0  |                 l_nb_bytes += l_nb_comps * l_qcc_bytes;  | 
3105  | 0  |         }  | 
3106  |  | 
  | 
3107  | 0  |         l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);  | 
3108  |  |  | 
3109  |  |         /*** DEVELOPER CORNER, Add room for your headers ***/  | 
3110  |  | 
  | 
3111  | 0  |         return l_nb_bytes;  | 
3112  | 0  | }  | 
3113  |  |  | 
3114  |  | /**  | 
3115  |  |  * Reads a POC marker (Progression Order Change)  | 
3116  |  |  *  | 
3117  |  |  * @param       p_header_data   the data contained in the POC box.  | 
3118  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3119  |  |  * @param       p_header_size   the size of the data contained in the POC marker.  | 
3120  |  |  * @param       p_manager               the user event manager.  | 
3121  |  | */  | 
3122  |  | static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,  | 
3123  |  |                                     OPJ_BYTE * p_header_data,  | 
3124  |  |                                     OPJ_UINT32 p_header_size,  | 
3125  |  |                                     opj_event_mgr_t * p_manager  | 
3126  |  |                                     )  | 
3127  | 0  | { | 
3128  | 0  |         OPJ_UINT32 i, l_nb_comp, l_tmp;  | 
3129  | 0  |         opj_image_t * l_image = 00;  | 
3130  | 0  |         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;  | 
3131  | 0  |         OPJ_UINT32 l_chunk_size, l_comp_room;  | 
3132  |  | 
  | 
3133  | 0  |         opj_cp_t *l_cp = 00;  | 
3134  | 0  |         opj_tcp_t *l_tcp = 00;  | 
3135  | 0  |         opj_poc_t *l_current_poc = 00;  | 
3136  |  |  | 
3137  |  |         /* preconditions */  | 
3138  | 0  |         assert(p_header_data != 00);  | 
3139  | 0  |         assert(p_j2k != 00);  | 
3140  | 0  |         assert(p_manager != 00);  | 
3141  |  | 
  | 
3142  | 0  |         l_image = p_j2k->m_private_image;  | 
3143  | 0  |         l_nb_comp = l_image->numcomps;  | 
3144  | 0  |         if (l_nb_comp <= 256) { | 
3145  | 0  |                 l_comp_room = 1;  | 
3146  | 0  |         }  | 
3147  | 0  |         else { | 
3148  | 0  |                 l_comp_room = 2;  | 
3149  | 0  |         }  | 
3150  | 0  |         l_chunk_size = 5 + 2 * l_comp_room;  | 
3151  | 0  |         l_current_poc_nb = p_header_size / l_chunk_size;  | 
3152  | 0  |         l_current_poc_remaining = p_header_size % l_chunk_size;  | 
3153  |  | 
  | 
3154  | 0  |         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { | 
3155  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");  | 
3156  | 0  |                 return OPJ_FALSE;  | 
3157  | 0  |         }  | 
3158  |  |  | 
3159  | 0  |         l_cp = &(p_j2k->m_cp);  | 
3160  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?  | 
3161  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
3162  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
3163  | 0  |         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;  | 
3164  | 0  |         l_current_poc_nb += l_old_poc_nb;  | 
3165  |  | 
  | 
3166  | 0  |         if(l_current_poc_nb >= 32)  | 
3167  | 0  |           { | 
3168  | 0  |           opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);  | 
3169  | 0  |           return OPJ_FALSE;  | 
3170  | 0  |           }  | 
3171  | 0  |         assert(l_current_poc_nb < 32);  | 
3172  |  |  | 
3173  |  |         /* now poc is in use.*/  | 
3174  | 0  |         l_tcp->POC = 1;  | 
3175  |  | 
  | 
3176  | 0  |         l_current_poc = &l_tcp->pocs[l_old_poc_nb];  | 
3177  | 0  |         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { | 
3178  | 0  |                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */  | 
3179  | 0  |                 ++p_header_data;  | 
3180  | 0  |                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */  | 
3181  | 0  |                 p_header_data+=l_comp_room;  | 
3182  | 0  |                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */  | 
3183  |  |                 /* make sure layer end is in acceptable bounds */  | 
3184  | 0  |                 l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);  | 
3185  | 0  |                 p_header_data+=2;  | 
3186  | 0  |                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */  | 
3187  | 0  |                 ++p_header_data;  | 
3188  | 0  |                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */  | 
3189  | 0  |                 p_header_data+=l_comp_room;  | 
3190  | 0  |                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */  | 
3191  | 0  |                 ++p_header_data;  | 
3192  | 0  |                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;  | 
3193  |  |                 /* make sure comp is in acceptable bounds */  | 
3194  | 0  |                 l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);  | 
3195  | 0  |                 ++l_current_poc;  | 
3196  | 0  |         }  | 
3197  |  | 
  | 
3198  | 0  |         l_tcp->numpocs = l_current_poc_nb - 1;  | 
3199  | 0  |         return OPJ_TRUE;  | 
3200  | 0  | }  | 
3201  |  |  | 
3202  |  | /**  | 
3203  |  |  * Reads a CRG marker (Component registration)  | 
3204  |  |  *  | 
3205  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
3206  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3207  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
3208  |  |  * @param       p_manager               the user event manager.  | 
3209  |  | */  | 
3210  |  | static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,  | 
3211  |  |                                     OPJ_BYTE * p_header_data,  | 
3212  |  |                                     OPJ_UINT32 p_header_size,  | 
3213  |  |                                     opj_event_mgr_t * p_manager  | 
3214  |  |                                     )  | 
3215  | 0  | { | 
3216  | 0  |         OPJ_UINT32 l_nb_comp;  | 
3217  |  |         /* preconditions */  | 
3218  | 0  |         assert(p_header_data != 00);  | 
3219  | 0  |         assert(p_j2k != 00);  | 
3220  | 0  |         assert(p_manager != 00);  | 
3221  |  | 
  | 
3222  | 0  |         l_nb_comp = p_j2k->m_private_image->numcomps;  | 
3223  |  | 
  | 
3224  | 0  |         if (p_header_size != l_nb_comp *4) { | 
3225  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");  | 
3226  | 0  |                 return OPJ_FALSE;  | 
3227  | 0  |         }  | 
3228  |  |         /* Do not care of this at the moment since only local variables are set here */  | 
3229  |  |         /*  | 
3230  |  |         for  | 
3231  |  |                 (i = 0; i < l_nb_comp; ++i)  | 
3232  |  |         { | 
3233  |  |                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i  | 
3234  |  |                 p_header_data+=2;  | 
3235  |  |                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i  | 
3236  |  |                 p_header_data+=2;  | 
3237  |  |         }  | 
3238  |  |         */  | 
3239  | 0  |         return OPJ_TRUE;  | 
3240  | 0  | }  | 
3241  |  |  | 
3242  |  | /**  | 
3243  |  |  * Reads a TLM marker (Tile Length Marker)  | 
3244  |  |  *  | 
3245  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
3246  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3247  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
3248  |  |  * @param       p_manager               the user event manager.  | 
3249  |  | */  | 
3250  |  | static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,  | 
3251  |  |                                     OPJ_BYTE * p_header_data,  | 
3252  |  |                                     OPJ_UINT32 p_header_size,  | 
3253  |  |                                     opj_event_mgr_t * p_manager  | 
3254  |  |                                     )  | 
3255  | 0  | { | 
3256  | 0  |         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;  | 
3257  |  |         /* preconditions */  | 
3258  | 0  |         assert(p_header_data != 00);  | 
3259  | 0  |         assert(p_j2k != 00);  | 
3260  | 0  |         assert(p_manager != 00);  | 
3261  |  | 
  | 
3262  | 0  |         if (p_header_size < 2) { | 
3263  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");  | 
3264  | 0  |                 return OPJ_FALSE;  | 
3265  | 0  |         }  | 
3266  | 0  |         p_header_size -= 2;  | 
3267  |  | 
  | 
3268  | 0  |         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */  | 
3269  | 0  |         ++p_header_data;  | 
3270  | 0  |         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */  | 
3271  | 0  |         ++p_header_data;  | 
3272  |  | 
  | 
3273  | 0  |         l_ST = ((l_Stlm >> 4) & 0x3);  | 
3274  | 0  |         l_SP = (l_Stlm >> 6) & 0x1;  | 
3275  |  | 
  | 
3276  | 0  |         l_Ptlm_size = (l_SP + 1) * 2;  | 
3277  | 0  |         l_quotient = l_Ptlm_size + l_ST;  | 
3278  |  | 
  | 
3279  | 0  |         l_tot_num_tp_remaining = p_header_size % l_quotient;  | 
3280  |  | 
  | 
3281  | 0  |         if (l_tot_num_tp_remaining != 0) { | 
3282  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");  | 
3283  | 0  |                 return OPJ_FALSE;  | 
3284  | 0  |         }  | 
3285  |  |         /* FIXME Do not care of this at the moment since only local variables are set here */  | 
3286  |  |         /*  | 
3287  |  |         for  | 
3288  |  |                 (i = 0; i < l_tot_num_tp; ++i)  | 
3289  |  |         { | 
3290  |  |                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i  | 
3291  |  |                 p_header_data += l_ST;  | 
3292  |  |                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i  | 
3293  |  |                 p_header_data += l_Ptlm_size;  | 
3294  |  |         }*/  | 
3295  | 0  |         return OPJ_TRUE;  | 
3296  | 0  | }  | 
3297  |  |  | 
3298  |  | /**  | 
3299  |  |  * Reads a PLM marker (Packet length, main header marker)  | 
3300  |  |  *  | 
3301  |  |  * @param       p_header_data   the data contained in the TLM box.  | 
3302  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3303  |  |  * @param       p_header_size   the size of the data contained in the TLM marker.  | 
3304  |  |  * @param       p_manager               the user event manager.  | 
3305  |  | */  | 
3306  |  | static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,  | 
3307  |  |                                     OPJ_BYTE * p_header_data,  | 
3308  |  |                                     OPJ_UINT32 p_header_size,  | 
3309  |  |                                     opj_event_mgr_t * p_manager  | 
3310  |  |                                     )  | 
3311  | 0  | { | 
3312  |  |         /* preconditions */  | 
3313  | 0  |         assert(p_header_data != 00);  | 
3314  | 0  |         assert(p_j2k != 00);  | 
3315  | 0  |         assert(p_manager != 00);  | 
3316  |  | 
  | 
3317  | 0  |         if (p_header_size < 1) { | 
3318  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");  | 
3319  | 0  |                 return OPJ_FALSE;  | 
3320  | 0  |         }  | 
3321  |  |         /* Do not care of this at the moment since only local variables are set here */  | 
3322  |  |         /*  | 
3323  |  |         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm  | 
3324  |  |         ++p_header_data;  | 
3325  |  |         --p_header_size;  | 
3326  |  |  | 
3327  |  |         while  | 
3328  |  |                 (p_header_size > 0)  | 
3329  |  |         { | 
3330  |  |                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm  | 
3331  |  |                 ++p_header_data;  | 
3332  |  |                 p_header_size -= (1+l_Nplm);  | 
3333  |  |                 if  | 
3334  |  |                         (p_header_size < 0)  | 
3335  |  |                 { | 
3336  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");  | 
3337  |  |                         return false;  | 
3338  |  |                 }  | 
3339  |  |                 for  | 
3340  |  |                         (i = 0; i < l_Nplm; ++i)  | 
3341  |  |                 { | 
3342  |  |                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij  | 
3343  |  |                         ++p_header_data;  | 
3344  |  |                         // take only the last seven bytes  | 
3345  |  |                         l_packet_len |= (l_tmp & 0x7f);  | 
3346  |  |                         if  | 
3347  |  |                                 (l_tmp & 0x80)  | 
3348  |  |                         { | 
3349  |  |                                 l_packet_len <<= 7;  | 
3350  |  |                         }  | 
3351  |  |                         else  | 
3352  |  |                         { | 
3353  |  |                 // store packet length and proceed to next packet  | 
3354  |  |                                 l_packet_len = 0;  | 
3355  |  |                         }  | 
3356  |  |                 }  | 
3357  |  |                 if  | 
3358  |  |                         (l_packet_len != 0)  | 
3359  |  |                 { | 
3360  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");  | 
3361  |  |                         return false;  | 
3362  |  |                 }  | 
3363  |  |         }  | 
3364  |  |         */  | 
3365  | 0  |         return OPJ_TRUE;  | 
3366  | 0  | }  | 
3367  |  |  | 
3368  |  | /**  | 
3369  |  |  * Reads a PLT marker (Packet length, tile-part header)  | 
3370  |  |  *  | 
3371  |  |  * @param       p_header_data   the data contained in the PLT box.  | 
3372  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3373  |  |  * @param       p_header_size   the size of the data contained in the PLT marker.  | 
3374  |  |  * @param       p_manager               the user event manager.  | 
3375  |  | */  | 
3376  |  | static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,  | 
3377  |  |                                     OPJ_BYTE * p_header_data,  | 
3378  |  |                                     OPJ_UINT32 p_header_size,  | 
3379  |  |                                     opj_event_mgr_t * p_manager  | 
3380  |  |                                     )  | 
3381  | 0  | { | 
3382  | 0  |         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;  | 
3383  |  |  | 
3384  |  |         /* preconditions */  | 
3385  | 0  |         assert(p_header_data != 00);  | 
3386  | 0  |         assert(p_j2k != 00);  | 
3387  | 0  |         assert(p_manager != 00);  | 
3388  |  | 
  | 
3389  | 0  |         if (p_header_size < 1) { | 
3390  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");  | 
3391  | 0  |                 return OPJ_FALSE;  | 
3392  | 0  |         }  | 
3393  |  |  | 
3394  | 0  |         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */  | 
3395  | 0  |         ++p_header_data;  | 
3396  | 0  |         --p_header_size;  | 
3397  |  | 
  | 
3398  | 0  |         for (i = 0; i < p_header_size; ++i) { | 
3399  | 0  |                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */  | 
3400  | 0  |                 ++p_header_data;  | 
3401  |  |                 /* take only the last seven bytes */  | 
3402  | 0  |                 l_packet_len |= (l_tmp & 0x7f);  | 
3403  | 0  |                 if (l_tmp & 0x80) { | 
3404  | 0  |                         l_packet_len <<= 7;  | 
3405  | 0  |                 }  | 
3406  | 0  |                 else { | 
3407  |  |             /* store packet length and proceed to next packet */  | 
3408  | 0  |                         l_packet_len = 0;  | 
3409  | 0  |                 }  | 
3410  | 0  |         }  | 
3411  |  | 
  | 
3412  | 0  |         if (l_packet_len != 0) { | 
3413  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");  | 
3414  | 0  |                 return OPJ_FALSE;  | 
3415  | 0  |         }  | 
3416  |  |  | 
3417  | 0  |         return OPJ_TRUE;  | 
3418  | 0  | }  | 
3419  |  |  | 
3420  |  | #if 0  | 
3421  |  | OPJ_BOOL j2k_read_ppm_v2 (  | 
3422  |  |                                                 opj_j2k_t *p_j2k,  | 
3423  |  |                                                 OPJ_BYTE * p_header_data,  | 
3424  |  |                                                 OPJ_UINT32 p_header_size,  | 
3425  |  |                                                 struct opj_event_mgr * p_manager  | 
3426  |  |                                         )  | 
3427  |  | { | 
3428  |  |  | 
3429  |  |         opj_cp_t *l_cp = 00;  | 
3430  |  |         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;  | 
3431  |  |  | 
3432  |  |         /* preconditions */  | 
3433  |  |         assert(p_header_data != 00);  | 
3434  |  |         assert(p_j2k != 00);  | 
3435  |  |         assert(p_manager != 00);  | 
3436  |  |  | 
3437  |  |         if (p_header_size < 1) { | 
3438  |  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");  | 
3439  |  |                 return OPJ_FALSE;  | 
3440  |  |         }  | 
3441  |  |  | 
3442  |  |         l_cp = &(p_j2k->m_cp);  | 
3443  |  |         l_cp->ppm = 1;  | 
3444  |  |  | 
3445  |  |         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */  | 
3446  |  |         ++p_header_data;  | 
3447  |  |         --p_header_size;  | 
3448  |  |  | 
3449  |  |         /* First PPM marker */  | 
3450  |  |         if (l_Z_ppm == 0) { | 
3451  |  |                 if (p_header_size < 4) { | 
3452  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");  | 
3453  |  |                         return OPJ_FALSE;  | 
3454  |  |                 }  | 
3455  |  |  | 
3456  |  |                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */  | 
3457  |  |                 p_header_data+=4;  | 
3458  |  |                 p_header_size-=4;  | 
3459  |  |  | 
3460  |  |                 /* First PPM marker: Initialization */  | 
3461  |  |                 l_cp->ppm_len = l_N_ppm;  | 
3462  |  |                 l_cp->ppm_data_size = 0;  | 
3463  |  |  | 
3464  |  |                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);  | 
3465  |  |                 if (l_cp->ppm_buffer == 00) { | 
3466  |  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");  | 
3467  |  |                         return OPJ_FALSE;  | 
3468  |  |                 }  | 
3469  |  |                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);  | 
3470  |  |  | 
3471  |  |                 l_cp->ppm_data = l_cp->ppm_buffer;  | 
3472  |  |         }  | 
3473  |  |  | 
3474  |  |         while (1) { | 
3475  |  |                 if (l_cp->ppm_data_size == l_cp->ppm_len) { | 
3476  |  |                         if (p_header_size >= 4) { | 
3477  |  |                                 /* read a N_ppm */  | 
3478  |  |                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */  | 
3479  |  |                                 p_header_data+=4;  | 
3480  |  |                                 p_header_size-=4;  | 
3481  |  |                                 l_cp->ppm_len += l_N_ppm ;  | 
3482  |  |  | 
3483  |  |                                 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);  | 
3484  |  |                                 if (! new_ppm_buffer) { | 
3485  |  |                                         opj_free(l_cp->ppm_buffer);  | 
3486  |  |                                         l_cp->ppm_buffer = NULL;  | 
3487  |  |                                         l_cp->ppm_len = 0;  | 
3488  |  |                                         l_cp->ppm_data = NULL;  | 
3489  |  |                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");  | 
3490  |  |                                         return OPJ_FALSE;  | 
3491  |  |                                 }  | 
3492  |  |                                 l_cp->ppm_buffer = new_ppm_buffer;  | 
3493  |  |                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);  | 
3494  |  |                                 l_cp->ppm_data = l_cp->ppm_buffer;  | 
3495  |  |                         }  | 
3496  |  |                         else { | 
3497  |  |                                 return OPJ_FALSE;  | 
3498  |  |                         }  | 
3499  |  |                 }  | 
3500  |  |  | 
3501  |  |                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;  | 
3502  |  |  | 
3503  |  |                 if (l_remaining_data <= p_header_size) { | 
3504  |  |                         /* we must store less information than available in the packet */  | 
3505  |  |                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);  | 
3506  |  |                         l_cp->ppm_data_size = l_cp->ppm_len;  | 
3507  |  |                         p_header_size -= l_remaining_data;  | 
3508  |  |                         p_header_data += l_remaining_data;  | 
3509  |  |                 }  | 
3510  |  |                 else { | 
3511  |  |                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);  | 
3512  |  |                         l_cp->ppm_data_size += p_header_size;  | 
3513  |  |                         p_header_data += p_header_size;  | 
3514  |  |                         p_header_size = 0;  | 
3515  |  |                         break;  | 
3516  |  |                 }  | 
3517  |  |         }  | 
3518  |  |  | 
3519  |  |         return OPJ_TRUE;  | 
3520  |  | }  | 
3521  |  | #endif  | 
3522  |  |  | 
3523  |  | OPJ_BOOL j2k_read_ppm_v3 (  | 
3524  |  |                                                 opj_j2k_t *p_j2k,  | 
3525  |  |                                                 OPJ_BYTE * p_header_data,  | 
3526  |  |                                                 OPJ_UINT32 p_header_size,  | 
3527  |  |                                                 struct opj_event_mgr * p_manager  | 
3528  |  |                                         )  | 
3529  | 0  | { | 
3530  | 0  |         opj_cp_t *l_cp = 00;  | 
3531  | 0  |         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;  | 
3532  |  |  | 
3533  |  |         /* preconditions */  | 
3534  | 0  |         assert(p_header_data != 00);  | 
3535  | 0  |         assert(p_j2k != 00);  | 
3536  | 0  |         assert(p_manager != 00);  | 
3537  |  |  | 
3538  |  |         /* Minimum size of PPM marker is equal to the size of Zppm element */  | 
3539  | 0  |         if (p_header_size < 1) { | 
3540  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");  | 
3541  | 0  |                 return OPJ_FALSE;  | 
3542  | 0  |         }  | 
3543  |  |  | 
3544  | 0  |         l_cp = &(p_j2k->m_cp);  | 
3545  | 0  |         l_cp->ppm = 1;  | 
3546  |  | 
  | 
3547  | 0  |         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */  | 
3548  | 0  |         ++p_header_data;  | 
3549  | 0  |         --p_header_size;  | 
3550  |  |  | 
3551  |  |         /* First PPM marker */  | 
3552  | 0  |         if (l_Z_ppm == 0) { | 
3553  |  |                 /* We need now at least the Nppm^0 element */  | 
3554  | 0  |                 if (p_header_size < 4) { | 
3555  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");  | 
3556  | 0  |                         return OPJ_FALSE;  | 
3557  | 0  |                 }  | 
3558  |  |  | 
3559  | 0  |                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */  | 
3560  | 0  |                 p_header_data+=4;  | 
3561  | 0  |                 p_header_size-=4;  | 
3562  |  |  | 
3563  |  |                 /* sanity check: how much bytes is left for Ippm */  | 
3564  | 0  |                 if( p_header_size < l_N_ppm )  | 
3565  | 0  |                   { | 
3566  | 0  |                   opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );  | 
3567  | 0  |                   opj_free(l_cp->ppm_data);  | 
3568  | 0  |                   l_cp->ppm_data = NULL;  | 
3569  | 0  |                   l_cp->ppm_buffer = NULL;  | 
3570  | 0  |                   l_cp->ppm = 0; /* do not use PPM */  | 
3571  | 0  |                   return OPJ_TRUE;  | 
3572  | 0  |                   }  | 
3573  |  |  | 
3574  |  |                 /* First PPM marker: Initialization */  | 
3575  | 0  |                 l_cp->ppm_len = l_N_ppm;  | 
3576  | 0  |                 l_cp->ppm_data_read = 0;  | 
3577  |  | 
  | 
3578  | 0  |                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);  | 
3579  | 0  |                 l_cp->ppm_buffer = l_cp->ppm_data;  | 
3580  | 0  |                 if (l_cp->ppm_data == 00) { | 
3581  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");  | 
3582  | 0  |                         return OPJ_FALSE;  | 
3583  | 0  |                 }  | 
3584  | 0  |                 memset(l_cp->ppm_data,0,l_cp->ppm_len);  | 
3585  |  | 
  | 
3586  | 0  |                 l_cp->ppm_data_current = l_cp->ppm_data;  | 
3587  |  |  | 
3588  |  |                 /*l_cp->ppm_data = l_cp->ppm_buffer;*/  | 
3589  | 0  |         }  | 
3590  | 0  |         else { | 
3591  | 0  |                 if (p_header_size < 4) { | 
3592  | 0  |                         opj_event_msg(p_manager, EVT_WARNING, "Empty PPM marker\n");  | 
3593  | 0  |                         return OPJ_TRUE;  | 
3594  | 0  |                 }  | 
3595  | 0  |                 else { | 
3596  |  |                         /* Uncompleted Ippm series in the previous PPM marker?*/  | 
3597  | 0  |                         if (l_cp->ppm_data_read < l_cp->ppm_len) { | 
3598  |  |                                 /* Get the place where add the remaining Ippm series*/  | 
3599  | 0  |                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);  | 
3600  | 0  |                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;  | 
3601  | 0  |                         }  | 
3602  | 0  |                         else { | 
3603  | 0  |                                 OPJ_BYTE *new_ppm_data;  | 
3604  | 0  |                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */  | 
3605  | 0  |                                 p_header_data+=4;  | 
3606  | 0  |                                 p_header_size-=4;  | 
3607  |  |  | 
3608  |  |                                 /* sanity check: how much bytes is left for Ippm */  | 
3609  | 0  |                                 if( p_header_size < l_N_ppm )  | 
3610  | 0  |                                   { | 
3611  | 0  |                                   opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );  | 
3612  | 0  |                                   opj_free(l_cp->ppm_data);  | 
3613  | 0  |                                   l_cp->ppm_data = NULL;  | 
3614  | 0  |                                   l_cp->ppm_buffer = NULL;  | 
3615  | 0  |                                   l_cp->ppm = 0; /* do not use PPM */  | 
3616  | 0  |                                   return OPJ_TRUE;  | 
3617  | 0  |                                   }  | 
3618  |  |                                 /* Increase the size of ppm_data to add the new Ippm series*/  | 
3619  | 0  |                                 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");  | 
3620  | 0  |                                 new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);  | 
3621  | 0  |                                 if (! new_ppm_data) { | 
3622  | 0  |                                         opj_free(l_cp->ppm_data);  | 
3623  | 0  |                                         l_cp->ppm_data = NULL;  | 
3624  | 0  |                                         l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */  | 
3625  | 0  |                                         l_cp->ppm_len = 0;  | 
3626  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n");  | 
3627  | 0  |                                         return OPJ_FALSE;  | 
3628  | 0  |                                 }  | 
3629  | 0  |                                 l_cp->ppm_data = new_ppm_data;  | 
3630  | 0  |                                 l_cp->ppm_buffer = l_cp->ppm_data;  | 
3631  |  |  | 
3632  |  |                                 /* Keep the position of the place where concatenate the new series*/  | 
3633  | 0  |                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);  | 
3634  | 0  |                                 l_cp->ppm_len += l_N_ppm;  | 
3635  | 0  |                         }  | 
3636  | 0  |                 }  | 
3637  | 0  |         }  | 
3638  |  |  | 
3639  | 0  |         l_remaining_data = p_header_size;  | 
3640  |  | 
  | 
3641  | 0  |         while (l_remaining_data >= l_N_ppm) { | 
3642  |  |                 /* read a complete Ippm series*/  | 
3643  | 0  |                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);  | 
3644  | 0  |                 p_header_size -= l_N_ppm;  | 
3645  | 0  |                 p_header_data += l_N_ppm;  | 
3646  |  | 
  | 
3647  | 0  |                 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/  | 
3648  |  | 
  | 
3649  | 0  |                 if (p_header_size)  | 
3650  | 0  |                 { | 
3651  | 0  |                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */  | 
3652  | 0  |                         p_header_data+=4;  | 
3653  | 0  |                         p_header_size-=4;  | 
3654  | 0  |                 }  | 
3655  | 0  |                 else { | 
3656  | 0  |                         l_remaining_data = p_header_size;  | 
3657  | 0  |                         break;  | 
3658  | 0  |                 }  | 
3659  |  |  | 
3660  | 0  |                 l_remaining_data = p_header_size;  | 
3661  |  |  | 
3662  |  |                 /* Next Ippm series is a complete series ?*/  | 
3663  | 0  |                 if (l_remaining_data >= l_N_ppm) { | 
3664  | 0  |                         OPJ_BYTE *new_ppm_data;  | 
3665  |  |                         /* Increase the size of ppm_data to add the new Ippm series*/  | 
3666  | 0  |                         assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");  | 
3667  | 0  |                         new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);  | 
3668  | 0  |                         if (! new_ppm_data) { | 
3669  | 0  |                                 opj_free(l_cp->ppm_data);  | 
3670  | 0  |                                 l_cp->ppm_data = NULL;  | 
3671  | 0  |                                 l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */  | 
3672  | 0  |                                 l_cp->ppm_len = 0;  | 
3673  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");  | 
3674  | 0  |                                 return OPJ_FALSE;  | 
3675  | 0  |                         }  | 
3676  | 0  |                         l_cp->ppm_data = new_ppm_data;  | 
3677  | 0  |                         l_cp->ppm_buffer = l_cp->ppm_data;  | 
3678  |  |  | 
3679  |  |                         /* Keep the position of the place where concatenate the new series */  | 
3680  | 0  |                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);  | 
3681  | 0  |                         l_cp->ppm_len += l_N_ppm;  | 
3682  | 0  |                 }  | 
3683  |  | 
  | 
3684  | 0  |         }  | 
3685  |  |  | 
3686  |  |         /* Need to read an incomplete Ippm series*/  | 
3687  | 0  |         if (l_remaining_data) { | 
3688  | 0  |                 OPJ_BYTE *new_ppm_data;  | 
3689  | 0  |                 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");  | 
3690  | 0  |                 new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);  | 
3691  | 0  |                 if (! new_ppm_data) { | 
3692  | 0  |                         opj_free(l_cp->ppm_data);  | 
3693  | 0  |                         l_cp->ppm_data = NULL;  | 
3694  | 0  |                         l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */  | 
3695  | 0  |                         l_cp->ppm_len = 0;  | 
3696  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n");  | 
3697  | 0  |                         return OPJ_FALSE;  | 
3698  | 0  |                 }  | 
3699  | 0  |                 l_cp->ppm_data = new_ppm_data;  | 
3700  | 0  |                 l_cp->ppm_buffer = l_cp->ppm_data;  | 
3701  |  |  | 
3702  |  |                 /* Keep the position of the place where concatenate the new series*/  | 
3703  | 0  |                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);  | 
3704  | 0  |                 l_cp->ppm_len += l_N_ppm;  | 
3705  |  |  | 
3706  |  |                 /* Read incomplete Ippm series*/  | 
3707  | 0  |                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);  | 
3708  | 0  |                 p_header_size -= l_remaining_data;  | 
3709  | 0  |                 p_header_data += l_remaining_data;  | 
3710  |  | 
  | 
3711  | 0  |                 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/  | 
3712  | 0  |         }  | 
3713  |  |  | 
3714  |  | #ifdef CLEAN_MSD  | 
3715  |  |  | 
3716  |  |                 if (l_cp->ppm_data_size == l_cp->ppm_len) { | 
3717  |  |                         if (p_header_size >= 4) { | 
3718  |  |                                 /* read a N_ppm*/  | 
3719  |  |                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */  | 
3720  |  |                                 p_header_data+=4;  | 
3721  |  |                                 p_header_size-=4;  | 
3722  |  |                                 l_cp->ppm_len += l_N_ppm ;  | 
3723  |  |  | 
3724  |  |                                 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);  | 
3725  |  |                                 if (! new_ppm_buffer) { | 
3726  |  |                                         opj_free(l_cp->ppm_buffer);  | 
3727  |  |                                         l_cp->ppm_buffer = NULL;  | 
3728  |  |                                         l_cp->ppm_len = 0;  | 
3729  |  |                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");  | 
3730  |  |                                         return OPJ_FALSE;  | 
3731  |  |                                 }  | 
3732  |  |                                 l_cp->ppm_buffer = new_ppm_buffer;  | 
3733  |  |                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);  | 
3734  |  |  | 
3735  |  |                                 l_cp->ppm_data = l_cp->ppm_buffer;  | 
3736  |  |                         }  | 
3737  |  |                         else { | 
3738  |  |                                 return OPJ_FALSE;  | 
3739  |  |                         }  | 
3740  |  |                 }  | 
3741  |  |  | 
3742  |  |                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;  | 
3743  |  |  | 
3744  |  |                 if (l_remaining_data <= p_header_size) { | 
3745  |  |                         /* we must store less information than available in the packet */  | 
3746  |  |                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);  | 
3747  |  |                         l_cp->ppm_data_size = l_cp->ppm_len;  | 
3748  |  |                         p_header_size -= l_remaining_data;  | 
3749  |  |                         p_header_data += l_remaining_data;  | 
3750  |  |                 }  | 
3751  |  |                 else { | 
3752  |  |                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);  | 
3753  |  |                         l_cp->ppm_data_size += p_header_size;  | 
3754  |  |                         p_header_data += p_header_size;  | 
3755  |  |                         p_header_size = 0;  | 
3756  |  |                         break;  | 
3757  |  |                 }  | 
3758  |  |         }  | 
3759  |  | #endif  | 
3760  | 0  |         return OPJ_TRUE;  | 
3761  | 0  | }  | 
3762  |  |  | 
3763  |  | /**  | 
3764  |  |  * Reads a PPT marker (Packed packet headers, tile-part header)  | 
3765  |  |  *  | 
3766  |  |  * @param       p_header_data   the data contained in the PPT box.  | 
3767  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
3768  |  |  * @param       p_header_size   the size of the data contained in the PPT marker.  | 
3769  |  |  * @param       p_manager               the user event manager.  | 
3770  |  | */  | 
3771  |  | static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,  | 
3772  |  |                                     OPJ_BYTE * p_header_data,  | 
3773  |  |                                     OPJ_UINT32 p_header_size,  | 
3774  |  |                                     opj_event_mgr_t * p_manager  | 
3775  |  |                                     )  | 
3776  | 0  | { | 
3777  | 0  |         opj_cp_t *l_cp = 00;  | 
3778  | 0  |         opj_tcp_t *l_tcp = 00;  | 
3779  | 0  |         OPJ_UINT32 l_Z_ppt;  | 
3780  |  |  | 
3781  |  |         /* preconditions */  | 
3782  | 0  |         assert(p_header_data != 00);  | 
3783  | 0  |         assert(p_j2k != 00);  | 
3784  | 0  |         assert(p_manager != 00);  | 
3785  |  |  | 
3786  |  |         /* We need to have the Z_ppt element at minimum */  | 
3787  | 0  |         if (p_header_size < 1) { | 
3788  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");  | 
3789  | 0  |                 return OPJ_FALSE;  | 
3790  | 0  |         }  | 
3791  |  |  | 
3792  | 0  |         l_cp = &(p_j2k->m_cp);  | 
3793  | 0  |         if (l_cp->ppm){ | 
3794  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");  | 
3795  | 0  |                 return OPJ_FALSE;  | 
3796  | 0  |         }  | 
3797  |  |  | 
3798  | 0  |         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);  | 
3799  | 0  |         l_tcp->ppt = 1;  | 
3800  |  | 
  | 
3801  | 0  |         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */  | 
3802  | 0  |         ++p_header_data;  | 
3803  | 0  |         --p_header_size;  | 
3804  |  |  | 
3805  |  |         /* Allocate buffer to read the packet header */  | 
3806  | 0  |         if (l_Z_ppt == 0) { | 
3807  |  |                 /* First PPT marker */  | 
3808  | 0  |                 l_tcp->ppt_data_size = 0;  | 
3809  | 0  |                 l_tcp->ppt_len = p_header_size;  | 
3810  |  | 
  | 
3811  | 0  |                 opj_free(l_tcp->ppt_buffer);  | 
3812  | 0  |                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );  | 
3813  | 0  |                 if (l_tcp->ppt_buffer == 00) { | 
3814  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");  | 
3815  | 0  |                         return OPJ_FALSE;  | 
3816  | 0  |                 }  | 
3817  | 0  |                 l_tcp->ppt_data = l_tcp->ppt_buffer;  | 
3818  |  |  | 
3819  |  |                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */  | 
3820  | 0  |         }  | 
3821  | 0  |         else { | 
3822  | 0  |                 OPJ_BYTE *new_ppt_buffer;  | 
3823  | 0  |                 l_tcp->ppt_len += p_header_size;  | 
3824  |  | 
  | 
3825  | 0  |                 new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len);  | 
3826  | 0  |                 if (! new_ppt_buffer) { | 
3827  | 0  |                         opj_free(l_tcp->ppt_buffer);  | 
3828  | 0  |                         l_tcp->ppt_buffer = NULL;  | 
3829  | 0  |                         l_tcp->ppt_len = 0;  | 
3830  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");  | 
3831  | 0  |                         return OPJ_FALSE;  | 
3832  | 0  |                 }  | 
3833  | 0  |                 l_tcp->ppt_buffer = new_ppt_buffer;  | 
3834  | 0  |                 l_tcp->ppt_data = l_tcp->ppt_buffer;  | 
3835  |  | 
  | 
3836  | 0  |                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);  | 
3837  | 0  |         }  | 
3838  |  |  | 
3839  |  |         /* Read packet header from buffer */  | 
3840  | 0  |         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);  | 
3841  |  | 
  | 
3842  | 0  |         l_tcp->ppt_data_size += p_header_size;  | 
3843  |  | 
  | 
3844  | 0  |         return OPJ_TRUE;  | 
3845  | 0  | }  | 
3846  |  |  | 
3847  |  | OPJ_BOOL opj_j2k_write_tlm(     opj_j2k_t *p_j2k,  | 
3848  |  |                                                         opj_stream_private_t *p_stream,  | 
3849  |  |                                                         opj_event_mgr_t * p_manager  | 
3850  |  |                             )  | 
3851  | 0  | { | 
3852  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
3853  | 0  |         OPJ_UINT32 l_tlm_size;  | 
3854  |  |  | 
3855  |  |         /* preconditions */  | 
3856  | 0  |         assert(p_j2k != 00);  | 
3857  | 0  |         assert(p_manager != 00);  | 
3858  | 0  |         assert(p_stream != 00);  | 
3859  |  | 
  | 
3860  | 0  |         l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);  | 
3861  |  | 
  | 
3862  | 0  |         if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
3863  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);  | 
3864  | 0  |                 if (! new_header_tile_data) { | 
3865  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
3866  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
3867  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
3868  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");  | 
3869  | 0  |                         return OPJ_FALSE;  | 
3870  | 0  |                 }  | 
3871  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
3872  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;  | 
3873  | 0  |         }  | 
3874  |  |  | 
3875  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
3876  |  |  | 
3877  |  |         /* change the way data is written to avoid seeking if possible */  | 
3878  |  |         /* TODO */  | 
3879  | 0  |         p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);  | 
3880  |  | 
  | 
3881  | 0  |         opj_write_bytes(l_current_data,J2K_MS_TLM,2);                                   /* TLM */  | 
3882  | 0  |         l_current_data += 2;  | 
3883  |  | 
  | 
3884  | 0  |         opj_write_bytes(l_current_data,l_tlm_size-2,2);                                 /* Lpoc */  | 
3885  | 0  |         l_current_data += 2;  | 
3886  |  | 
  | 
3887  | 0  |         opj_write_bytes(l_current_data,0,1);                                                    /* Ztlm=0*/  | 
3888  | 0  |         ++l_current_data;  | 
3889  |  | 
  | 
3890  | 0  |         opj_write_bytes(l_current_data,0x50,1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */  | 
3891  | 0  |         ++l_current_data;  | 
3892  |  |  | 
3893  |  |         /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */  | 
3894  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) { | 
3895  | 0  |                 return OPJ_FALSE;  | 
3896  | 0  |         }  | 
3897  |  |  | 
3898  | 0  |         return OPJ_TRUE;  | 
3899  | 0  | }  | 
3900  |  |  | 
3901  |  | OPJ_BOOL opj_j2k_write_sot(     opj_j2k_t *p_j2k,  | 
3902  |  |                                                         OPJ_BYTE * p_data,  | 
3903  |  |                                                         OPJ_UINT32 * p_data_written,  | 
3904  |  |                                                         const opj_stream_private_t *p_stream,  | 
3905  |  |                                                         opj_event_mgr_t * p_manager  | 
3906  |  |                             )  | 
3907  | 0  | { | 
3908  |  |         /* preconditions */  | 
3909  | 0  |         assert(p_j2k != 00);  | 
3910  | 0  |         assert(p_manager != 00);  | 
3911  | 0  |         assert(p_stream != 00);  | 
3912  |  | 
  | 
3913  | 0  |         opj_write_bytes(p_data,J2K_MS_SOT,2);                                   /* SOT */  | 
3914  | 0  |         p_data += 2;  | 
3915  |  | 
  | 
3916  | 0  |         opj_write_bytes(p_data,10,2);                                                   /* Lsot */  | 
3917  | 0  |         p_data += 2;  | 
3918  |  | 
  | 
3919  | 0  |         opj_write_bytes(p_data, p_j2k->m_current_tile_number,2);                        /* Isot */  | 
3920  | 0  |         p_data += 2;  | 
3921  |  |  | 
3922  |  |         /* Psot  */  | 
3923  | 0  |         p_data += 4;  | 
3924  |  | 
  | 
3925  | 0  |         opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */  | 
3926  | 0  |         ++p_data;  | 
3927  |  | 
  | 
3928  | 0  |         opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */  | 
3929  | 0  |         ++p_data;  | 
3930  |  |  | 
3931  |  |         /* UniPG>> */  | 
3932  |  | #ifdef USE_JPWL  | 
3933  |  |         /* update markers struct */  | 
3934  |  | /*  | 
3935  |  |         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);  | 
3936  |  | */  | 
3937  |  |   assert( 0 && "TODO" );  | 
3938  |  | #endif /* USE_JPWL */  | 
3939  |  | 
  | 
3940  | 0  |         * p_data_written = 12;  | 
3941  |  | 
  | 
3942  | 0  |         return OPJ_TRUE;  | 
3943  | 0  | }  | 
3944  |  |  | 
3945  |  | OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,  | 
3946  |  |                             OPJ_BYTE * p_header_data,  | 
3947  |  |                             OPJ_UINT32 p_header_size,  | 
3948  |  |                             opj_event_mgr_t * p_manager )  | 
3949  | 0  | { | 
3950  | 0  |         opj_cp_t *l_cp = 00;  | 
3951  | 0  |         opj_tcp_t *l_tcp = 00;  | 
3952  | 0  |         OPJ_UINT32 l_tot_len, l_num_parts = 0;  | 
3953  | 0  |         OPJ_UINT32 l_current_part;  | 
3954  | 0  |         OPJ_UINT32 l_tile_x,l_tile_y;  | 
3955  |  |  | 
3956  |  |         /* preconditions */  | 
3957  | 0  |         assert(p_header_data != 00);  | 
3958  | 0  |         assert(p_j2k != 00);  | 
3959  | 0  |         assert(p_manager != 00);  | 
3960  |  |  | 
3961  |  |         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/  | 
3962  | 0  |         if (p_header_size != 8) { | 
3963  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");  | 
3964  | 0  |                 return OPJ_FALSE;  | 
3965  | 0  |         }  | 
3966  |  |  | 
3967  | 0  |         l_cp = &(p_j2k->m_cp);  | 
3968  | 0  |         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */  | 
3969  | 0  |         p_header_data+=2;  | 
3970  |  |  | 
3971  |  |         /* testcase 2.pdf.SIGFPE.706.1112 */  | 
3972  | 0  |         if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { | 
3973  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);  | 
3974  | 0  |                 return OPJ_FALSE;  | 
3975  | 0  |         }  | 
3976  |  |  | 
3977  | 0  |         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];  | 
3978  | 0  |         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;  | 
3979  | 0  |         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;  | 
3980  |  | 
  | 
3981  |  | #ifdef USE_JPWL  | 
3982  |  |         if (l_cp->correct) { | 
3983  |  |  | 
3984  |  |                 OPJ_UINT32 tileno = p_j2k->m_current_tile_number;  | 
3985  |  |                 static OPJ_UINT32 backup_tileno = 0;  | 
3986  |  |  | 
3987  |  |                 /* tileno is negative or larger than the number of tiles!!! */  | 
3988  |  |                 if (tileno > (l_cp->tw * l_cp->th)) { | 
3989  |  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
3990  |  |                                         "JPWL: bad tile number (%d out of a maximum of %d)\n",  | 
3991  |  |                                         tileno, (l_cp->tw * l_cp->th));  | 
3992  |  |                         if (!JPWL_ASSUME) { | 
3993  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
3994  |  |                                 return OPJ_FALSE;  | 
3995  |  |                         }  | 
3996  |  |                         /* we try to correct */  | 
3997  |  |                         tileno = backup_tileno;  | 
3998  |  |                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"  | 
3999  |  |                                         "- setting tile number to %d\n",  | 
4000  |  |                                         tileno);  | 
4001  |  |                 }  | 
4002  |  |  | 
4003  |  |                 /* keep your private count of tiles */  | 
4004  |  |                 backup_tileno++;  | 
4005  |  |         };  | 
4006  |  | #endif /* USE_JPWL */  | 
4007  |  |  | 
4008  |  |         /* look for the tile in the list of already processed tile (in parts). */  | 
4009  |  |         /* Optimization possible here with a more complex data structure and with the removing of tiles */  | 
4010  |  |         /* since the time taken by this function can only grow at the time */  | 
4011  |  | 
  | 
4012  | 0  |         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */  | 
4013  | 0  |         p_header_data+=4;  | 
4014  |  |  | 
4015  |  |         /* PSot should be equal to zero or >=14 or <= 2^32-1 */  | 
4016  | 0  |         if ((l_tot_len !=0 ) && (l_tot_len < 14) )  | 
4017  | 0  |         { | 
4018  | 0  |             if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/  | 
4019  | 0  |             { | 
4020  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len);  | 
4021  | 0  |             }  | 
4022  | 0  |             else  | 
4023  | 0  |             { | 
4024  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);  | 
4025  | 0  |                 return OPJ_FALSE;  | 
4026  | 0  |             }  | 
4027  | 0  |         }  | 
4028  |  |  | 
4029  |  | #ifdef USE_JPWL  | 
4030  |  |         if (l_cp->correct) { | 
4031  |  |  | 
4032  |  |                 /* totlen is negative or larger than the bytes left!!! */  | 
4033  |  |                 if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ | 
4034  |  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
4035  |  |                                         "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",  | 
4036  |  |                                         l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */  | 
4037  |  |                         if (!JPWL_ASSUME) { | 
4038  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
4039  |  |                                 return OPJ_FALSE;  | 
4040  |  |                         }  | 
4041  |  |                         /* we try to correct */  | 
4042  |  |                         l_tot_len = 0;  | 
4043  |  |                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"  | 
4044  |  |                                         "- setting Psot to %d => assuming it is the last tile\n",  | 
4045  |  |                                         l_tot_len);  | 
4046  |  |                 }  | 
4047  |  |                 };  | 
4048  |  | #endif /* USE_JPWL */  | 
4049  |  |  | 
4050  |  |                 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/  | 
4051  | 0  |                 if (!l_tot_len) { | 
4052  | 0  |                         opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "  | 
4053  | 0  |                                         "we assuming it is the last tile-part of the codestream.\n");  | 
4054  | 0  |                         p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;  | 
4055  | 0  |                 }  | 
4056  |  | 
  | 
4057  | 0  |                 opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */  | 
4058  | 0  |                 ++p_header_data;  | 
4059  |  | 
  | 
4060  | 0  |                 opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */  | 
4061  | 0  |                 ++p_header_data;  | 
4062  |  | 
  | 
4063  | 0  |                 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ | 
4064  |  |                         /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of  | 
4065  |  |                          * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */  | 
4066  | 0  |                         if (l_tcp->m_nb_tile_parts) { | 
4067  | 0  |                                 if (l_current_part >= l_tcp->m_nb_tile_parts){ | 
4068  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "  | 
4069  | 0  |                                                         "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );  | 
4070  | 0  |                                         p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;  | 
4071  | 0  |                                         return OPJ_FALSE;  | 
4072  | 0  |                                 }  | 
4073  | 0  |                         }  | 
4074  | 0  |                         if( l_current_part >= l_num_parts ) { | 
4075  |  |                           /* testcase 451.pdf.SIGSEGV.ce9.3723 */  | 
4076  | 0  |                           opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "  | 
4077  | 0  |                             "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts );  | 
4078  | 0  |                           p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;  | 
4079  | 0  |                           return OPJ_FALSE;  | 
4080  | 0  |                         }  | 
4081  | 0  |                         l_tcp->m_nb_tile_parts = l_num_parts;  | 
4082  | 0  |                 }  | 
4083  |  |  | 
4084  |  |                 /* If know the number of tile part header we will check if we didn't read the last*/  | 
4085  | 0  |                 if (l_tcp->m_nb_tile_parts) { | 
4086  | 0  |                         if (l_tcp->m_nb_tile_parts == (l_current_part+1)) { | 
4087  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/  | 
4088  | 0  |                         }  | 
4089  | 0  |                 }  | 
4090  |  | 
  | 
4091  | 0  |                 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){ | 
4092  |  |                         /* Keep the size of data to skip after this marker */  | 
4093  | 0  |                         p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */  | 
4094  | 0  |                 }  | 
4095  | 0  |                 else { | 
4096  |  |                         /* FIXME: need to be computed from the number of bytes remaining in the codestream */  | 
4097  | 0  |                         p_j2k->m_specific_param.m_decoder.m_sot_length = 0;  | 
4098  | 0  |                 }  | 
4099  |  | 
  | 
4100  | 0  |                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;  | 
4101  |  |  | 
4102  |  |                 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/  | 
4103  | 0  |                 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { | 
4104  | 0  |                         p_j2k->m_specific_param.m_decoder.m_skip_data =  | 
4105  | 0  |                                 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)  | 
4106  | 0  |                                 ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)  | 
4107  | 0  |                                 ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)  | 
4108  | 0  |                                 ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);  | 
4109  | 0  |                 }  | 
4110  | 0  |                 else { | 
4111  | 0  |                         assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );  | 
4112  | 0  |                         p_j2k->m_specific_param.m_decoder.m_skip_data =  | 
4113  | 0  |                                 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);  | 
4114  | 0  |                 }  | 
4115  |  |  | 
4116  |  |                 /* Index */  | 
4117  | 0  |                 if (p_j2k->cstr_index)  | 
4118  | 0  |                 { | 
4119  | 0  |                         assert(p_j2k->cstr_index->tile_index != 00);  | 
4120  | 0  |                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;  | 
4121  | 0  |                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;  | 
4122  |  | 
  | 
4123  | 0  |                         if (l_num_parts != 0){ | 
4124  | 0  |                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;  | 
4125  | 0  |                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;  | 
4126  |  | 
  | 
4127  | 0  |                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { | 
4128  | 0  |                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =  | 
4129  | 0  |                                                 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));  | 
4130  | 0  |                                 }  | 
4131  | 0  |                                 else { | 
4132  | 0  |                                         opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(  | 
4133  | 0  |                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t));  | 
4134  | 0  |                                         if (! new_tp_index) { | 
4135  | 0  |                                                 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);  | 
4136  | 0  |                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;  | 
4137  | 0  |                                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");  | 
4138  | 0  |                                                 return OPJ_FALSE;  | 
4139  | 0  |                                         }  | 
4140  | 0  |                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;  | 
4141  | 0  |                                 }  | 
4142  | 0  |                         }  | 
4143  | 0  |                         else{ | 
4144  | 0  |                                 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { | 
4145  |  | 
  | 
4146  | 0  |                                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { | 
4147  | 0  |                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;  | 
4148  | 0  |                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =  | 
4149  | 0  |                                                         (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,  | 
4150  | 0  |                                                                         sizeof(opj_tp_index_t));  | 
4151  | 0  |                                         }  | 
4152  |  | 
  | 
4153  | 0  |                                         if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ | 
4154  | 0  |                                                 opj_tp_index_t *new_tp_index;  | 
4155  | 0  |                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1;  | 
4156  | 0  |                                                 new_tp_index = (opj_tp_index_t *) opj_realloc(  | 
4157  | 0  |                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,  | 
4158  | 0  |                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));  | 
4159  | 0  |                                                 if (! new_tp_index) { | 
4160  | 0  |                                                         opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);  | 
4161  | 0  |                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;  | 
4162  | 0  |                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;  | 
4163  | 0  |                                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");  | 
4164  | 0  |                                                         return OPJ_FALSE;  | 
4165  | 0  |                                                 }  | 
4166  | 0  |                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;  | 
4167  | 0  |                                         }  | 
4168  | 0  |                                 }  | 
4169  |  | 
  | 
4170  | 0  |                         }  | 
4171  |  | 
  | 
4172  | 0  |                 }  | 
4173  |  |  | 
4174  |  |                 /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */  | 
4175  |  |                 /* if (p_j2k->cstr_info) { | 
4176  |  |                    if (l_tcp->first) { | 
4177  |  |                    if (tileno == 0) { | 
4178  |  |                    p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;  | 
4179  |  |                    }  | 
4180  |  |  | 
4181  |  |                    p_j2k->cstr_info->tile[tileno].tileno = tileno;  | 
4182  |  |                    p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;  | 
4183  |  |                    p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;  | 
4184  |  |                    p_j2k->cstr_info->tile[tileno].num_tps = numparts;  | 
4185  |  |  | 
4186  |  |                    if (numparts) { | 
4187  |  |                    p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));  | 
4188  |  |                    }  | 
4189  |  |                    else { | 
4190  |  |                    p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)  | 
4191  |  |                    }  | 
4192  |  |                    }  | 
4193  |  |                    else { | 
4194  |  |                    p_j2k->cstr_info->tile[tileno].end_pos += totlen;  | 
4195  |  |                    }  | 
4196  |  |  | 
4197  |  |                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;  | 
4198  |  |                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =  | 
4199  |  |                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;  | 
4200  |  |                    }*/  | 
4201  | 0  |                 return OPJ_TRUE;  | 
4202  | 0  |         }  | 
4203  |  |  | 
4204  |  | OPJ_BOOL opj_j2k_write_sod(     opj_j2k_t *p_j2k,  | 
4205  |  |                                                         opj_tcd_t * p_tile_coder,  | 
4206  |  |                                                         OPJ_BYTE * p_data,  | 
4207  |  |                                                         OPJ_UINT32 * p_data_written,  | 
4208  |  |                                                         OPJ_UINT32 p_total_data_size,  | 
4209  |  |                                                         const opj_stream_private_t *p_stream,  | 
4210  |  |                                                         opj_event_mgr_t * p_manager  | 
4211  |  |                             )  | 
4212  | 0  | { | 
4213  | 0  |         opj_codestream_info_t *l_cstr_info = 00;  | 
4214  | 0  |         OPJ_UINT32 l_remaining_data;  | 
4215  |  |  | 
4216  |  |         /* preconditions */  | 
4217  | 0  |         assert(p_j2k != 00);  | 
4218  | 0  |         assert(p_manager != 00);  | 
4219  | 0  |         assert(p_stream != 00);  | 
4220  |  | 
  | 
4221  | 0  |         opj_write_bytes(p_data,J2K_MS_SOD,2);                                   /* SOD */  | 
4222  | 0  |         p_data += 2;  | 
4223  |  |  | 
4224  |  |         /* make room for the EOF marker */  | 
4225  | 0  |         l_remaining_data =  p_total_data_size - 4;  | 
4226  |  |  | 
4227  |  |         /* update tile coder */  | 
4228  | 0  |         p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;  | 
4229  | 0  |         p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;  | 
4230  |  |  | 
4231  |  |          /* INDEX >> */  | 
4232  |  |         /* TODO mergeV2: check this part which use cstr_info */  | 
4233  |  |         /*l_cstr_info = p_j2k->cstr_info;  | 
4234  |  |         if (l_cstr_info) { | 
4235  |  |                 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { | 
4236  |  |                         //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;  | 
4237  |  |                         l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;  | 
4238  |  |                 }  | 
4239  |  |                 else {*/ | 
4240  |  |                         /*  | 
4241  |  |                         TODO  | 
4242  |  |                         if  | 
4243  |  |                                 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))  | 
4244  |  |                         { | 
4245  |  |                                 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);  | 
4246  |  |                         }*/  | 
4247  |  |                 /*}*/  | 
4248  |  |                 /* UniPG>> */  | 
4249  |  | #ifdef USE_JPWL  | 
4250  |  |                 /* update markers struct */  | 
4251  |  |                 /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);  | 
4252  |  | */  | 
4253  |  |   assert( 0 && "TODO" );  | 
4254  |  | #endif /* USE_JPWL */  | 
4255  |  |                 /* <<UniPG */  | 
4256  |  |         /*}*/  | 
4257  |  |         /* << INDEX */  | 
4258  |  | 
  | 
4259  | 0  |         if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) { | 
4260  | 0  |                 p_tile_coder->tcd_image->tiles->packno = 0;  | 
4261  | 0  |                 if (l_cstr_info) { | 
4262  | 0  |                         l_cstr_info->packno = 0;  | 
4263  | 0  |                 }  | 
4264  | 0  |         }  | 
4265  |  | 
  | 
4266  | 0  |         *p_data_written = 0;  | 
4267  |  | 
  | 
4268  | 0  |         if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) { | 
4269  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");  | 
4270  | 0  |                 return OPJ_FALSE;  | 
4271  | 0  |         }  | 
4272  |  |  | 
4273  | 0  |         *p_data_written += 2;  | 
4274  |  | 
  | 
4275  | 0  |         return OPJ_TRUE;  | 
4276  | 0  | }  | 
4277  |  |  | 
4278  |  | OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k,  | 
4279  |  |                            opj_stream_private_t *p_stream,  | 
4280  |  |                                                    opj_event_mgr_t * p_manager  | 
4281  |  |                            )  | 
4282  | 0  | { | 
4283  | 0  |         OPJ_SIZE_T l_current_read_size;  | 
4284  | 0  |         opj_codestream_index_t * l_cstr_index = 00;  | 
4285  | 0  |         OPJ_BYTE ** l_current_data = 00;  | 
4286  | 0  |         opj_tcp_t * l_tcp = 00;  | 
4287  | 0  |         OPJ_UINT32 * l_tile_len = 00;  | 
4288  | 0  |         OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;  | 
4289  |  |  | 
4290  |  |         /* preconditions */  | 
4291  | 0  |         assert(p_j2k != 00);  | 
4292  | 0  |         assert(p_manager != 00);  | 
4293  | 0  |         assert(p_stream != 00);  | 
4294  |  | 
  | 
4295  | 0  |         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);  | 
4296  |  | 
  | 
4297  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { | 
4298  |  |                 /* opj_stream_get_number_byte_left returns OPJ_OFF_T  | 
4299  |  |                 // but we are in the last tile part,  | 
4300  |  |                 // so its result will fit on OPJ_UINT32 unless we find  | 
4301  |  |                 // a file with a single tile part of more than 4 GB...*/  | 
4302  | 0  |                 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);  | 
4303  | 0  |         }  | 
4304  | 0  |         else { | 
4305  |  |             /* Check to avoid pass the limit of OPJ_UINT32 */  | 
4306  | 0  |             if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 )  | 
4307  | 0  |                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;  | 
4308  | 0  |             else { | 
4309  |  |                 /* MSD: case commented to support empty SOT marker (PHR data) */  | 
4310  | 0  |             }  | 
4311  | 0  |         }  | 
4312  |  | 
  | 
4313  | 0  |         l_current_data = &(l_tcp->m_data);  | 
4314  | 0  |         l_tile_len = &l_tcp->m_data_size;  | 
4315  |  |  | 
4316  |  |         /* Patch to support new PHR data */  | 
4317  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_sot_length) { | 
4318  | 0  |             if (! *l_current_data) { | 
4319  |  |                 /* LH: oddly enough, in this path, l_tile_len!=0.  | 
4320  |  |                  * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).  | 
4321  |  |                  */  | 
4322  | 0  |                 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);  | 
4323  | 0  |             }  | 
4324  | 0  |             else { | 
4325  | 0  |                 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);  | 
4326  | 0  |                 if (! l_new_current_data) { | 
4327  | 0  |                         opj_free(*l_current_data);  | 
4328  |  |                         /*nothing more is done as l_current_data will be set to null, and just  | 
4329  |  |                           afterward we enter in the error path  | 
4330  |  |                           and the actual tile_len is updated (committed) at the end of the  | 
4331  |  |                           function. */  | 
4332  | 0  |                 }  | 
4333  | 0  |                 *l_current_data = l_new_current_data;  | 
4334  | 0  |             }  | 
4335  |  |               | 
4336  | 0  |             if (*l_current_data == 00) { | 
4337  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");  | 
4338  | 0  |                 return OPJ_FALSE;  | 
4339  | 0  |             }  | 
4340  | 0  |         }  | 
4341  | 0  |         else { | 
4342  | 0  |             l_sot_length_pb_detected = OPJ_TRUE;  | 
4343  | 0  |         }  | 
4344  |  |  | 
4345  |  |         /* Index */  | 
4346  | 0  |         l_cstr_index = p_j2k->cstr_index;  | 
4347  | 0  |         if (l_cstr_index) { | 
4348  | 0  |                 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;  | 
4349  |  | 
  | 
4350  | 0  |                 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;  | 
4351  | 0  |                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =  | 
4352  | 0  |                                 l_current_pos;  | 
4353  | 0  |                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =  | 
4354  | 0  |                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;  | 
4355  |  | 
  | 
4356  | 0  |                 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,  | 
4357  | 0  |                                         l_cstr_index,  | 
4358  | 0  |                                         J2K_MS_SOD,  | 
4359  | 0  |                                         l_current_pos,  | 
4360  | 0  |                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { | 
4361  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");  | 
4362  | 0  |                         return OPJ_FALSE;  | 
4363  | 0  |                 }  | 
4364  |  |  | 
4365  |  |                 /*l_cstr_index->packno = 0;*/  | 
4366  | 0  |         }  | 
4367  |  |  | 
4368  |  |         /* Patch to support new PHR data */  | 
4369  | 0  |         if (!l_sot_length_pb_detected) { | 
4370  | 0  |             l_current_read_size = opj_stream_read_data(  | 
4371  | 0  |                         p_stream,  | 
4372  | 0  |                         *l_current_data + *l_tile_len,  | 
4373  | 0  |                         p_j2k->m_specific_param.m_decoder.m_sot_length,  | 
4374  | 0  |                         p_manager);  | 
4375  | 0  |         }  | 
4376  | 0  |         else  | 
4377  | 0  |         { | 
4378  | 0  |             l_current_read_size = 0;  | 
4379  | 0  |         }  | 
4380  |  | 
  | 
4381  | 0  |         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { | 
4382  | 0  |                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;  | 
4383  | 0  |         }  | 
4384  | 0  |         else { | 
4385  | 0  |                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;  | 
4386  | 0  |         }  | 
4387  |  | 
  | 
4388  | 0  |         *l_tile_len += (OPJ_UINT32)l_current_read_size;  | 
4389  |  | 
  | 
4390  | 0  |         return OPJ_TRUE;  | 
4391  | 0  | }  | 
4392  |  |  | 
4393  |  |  OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,  | 
4394  |  |                             OPJ_UINT32 p_tile_no,  | 
4395  |  |                             OPJ_UINT32 p_comp_no,  | 
4396  |  |                             OPJ_UINT32 nb_comps,  | 
4397  |  |                             opj_stream_private_t *p_stream,  | 
4398  |  |                             opj_event_mgr_t * p_manager  | 
4399  |  |                             )  | 
4400  | 0  | { | 
4401  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
4402  | 0  |         OPJ_UINT32 l_rgn_size;  | 
4403  | 0  |         opj_cp_t *l_cp = 00;  | 
4404  | 0  |         opj_tcp_t *l_tcp = 00;  | 
4405  | 0  |         opj_tccp_t *l_tccp = 00;  | 
4406  | 0  |         OPJ_UINT32 l_comp_room;  | 
4407  |  |  | 
4408  |  |         /* preconditions */  | 
4409  | 0  |         assert(p_j2k != 00);  | 
4410  | 0  |         assert(p_manager != 00);  | 
4411  | 0  |         assert(p_stream != 00);  | 
4412  |  | 
  | 
4413  | 0  |         l_cp = &(p_j2k->m_cp);  | 
4414  | 0  |         l_tcp = &l_cp->tcps[p_tile_no];  | 
4415  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
4416  |  | 
  | 
4417  | 0  |         if (nb_comps <= 256) { | 
4418  | 0  |                 l_comp_room = 1;  | 
4419  | 0  |         }  | 
4420  | 0  |         else { | 
4421  | 0  |                 l_comp_room = 2;  | 
4422  | 0  |         }  | 
4423  |  | 
  | 
4424  | 0  |         l_rgn_size = 6 + l_comp_room;  | 
4425  |  | 
  | 
4426  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
4427  |  | 
  | 
4428  | 0  |         opj_write_bytes(l_current_data,J2K_MS_RGN,2);                                   /* RGN  */  | 
4429  | 0  |         l_current_data += 2;  | 
4430  |  | 
  | 
4431  | 0  |         opj_write_bytes(l_current_data,l_rgn_size-2,2);                                 /* Lrgn */  | 
4432  | 0  |         l_current_data += 2;  | 
4433  |  | 
  | 
4434  | 0  |         opj_write_bytes(l_current_data,p_comp_no,l_comp_room);                          /* Crgn */  | 
4435  | 0  |         l_current_data+=l_comp_room;  | 
4436  |  | 
  | 
4437  | 0  |         opj_write_bytes(l_current_data, 0,1);                                           /* Srgn */  | 
4438  | 0  |         ++l_current_data;  | 
4439  |  | 
  | 
4440  | 0  |         opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1);                            /* SPrgn */  | 
4441  | 0  |         ++l_current_data;  | 
4442  |  | 
  | 
4443  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) { | 
4444  | 0  |                 return OPJ_FALSE;  | 
4445  | 0  |         }  | 
4446  |  |  | 
4447  | 0  |         return OPJ_TRUE;  | 
4448  | 0  | }  | 
4449  |  |  | 
4450  |  | OPJ_BOOL opj_j2k_write_eoc(     opj_j2k_t *p_j2k,  | 
4451  |  |                             opj_stream_private_t *p_stream,  | 
4452  |  |                             opj_event_mgr_t * p_manager  | 
4453  |  |                             )  | 
4454  | 0  | { | 
4455  |  |         /* preconditions */  | 
4456  | 0  |         assert(p_j2k != 00);  | 
4457  | 0  |         assert(p_manager != 00);  | 
4458  | 0  |         assert(p_stream != 00);  | 
4459  |  | 
  | 
4460  | 0  |         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2);                                     /* EOC */  | 
4461  |  |  | 
4462  |  | /* UniPG>> */  | 
4463  |  | #ifdef USE_JPWL  | 
4464  |  |         /* update markers struct */  | 
4465  |  |         /*  | 
4466  |  |         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);  | 
4467  |  | */  | 
4468  |  | #endif /* USE_JPWL */  | 
4469  |  | 
  | 
4470  | 0  |         if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) { | 
4471  | 0  |                 return OPJ_FALSE;  | 
4472  | 0  |         }  | 
4473  |  |  | 
4474  | 0  |         if ( ! opj_stream_flush(p_stream,p_manager) ) { | 
4475  | 0  |                 return OPJ_FALSE;  | 
4476  | 0  |         }  | 
4477  |  |  | 
4478  | 0  |         return OPJ_TRUE;  | 
4479  | 0  | }  | 
4480  |  |  | 
4481  |  | /**  | 
4482  |  |  * Reads a RGN marker (Region Of Interest)  | 
4483  |  |  *  | 
4484  |  |  * @param       p_header_data   the data contained in the POC box.  | 
4485  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
4486  |  |  * @param       p_header_size   the size of the data contained in the POC marker.  | 
4487  |  |  * @param       p_manager               the user event manager.  | 
4488  |  | */  | 
4489  |  | static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,  | 
4490  |  |                                   OPJ_BYTE * p_header_data,  | 
4491  |  |                                   OPJ_UINT32 p_header_size,  | 
4492  |  |                                   opj_event_mgr_t * p_manager  | 
4493  |  |                                   )  | 
4494  | 0  | { | 
4495  | 0  |         OPJ_UINT32 l_nb_comp;  | 
4496  | 0  |         opj_image_t * l_image = 00;  | 
4497  |  | 
  | 
4498  | 0  |         opj_cp_t *l_cp = 00;  | 
4499  | 0  |         opj_tcp_t *l_tcp = 00;  | 
4500  | 0  |         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;  | 
4501  |  |  | 
4502  |  |         /* preconditions*/  | 
4503  | 0  |         assert(p_header_data != 00);  | 
4504  | 0  |         assert(p_j2k != 00);  | 
4505  | 0  |         assert(p_manager != 00);  | 
4506  |  | 
  | 
4507  | 0  |         l_image = p_j2k->m_private_image;  | 
4508  | 0  |         l_nb_comp = l_image->numcomps;  | 
4509  |  | 
  | 
4510  | 0  |         if (l_nb_comp <= 256) { | 
4511  | 0  |                 l_comp_room = 1; }  | 
4512  | 0  |         else { | 
4513  | 0  |                 l_comp_room = 2; }  | 
4514  |  | 
  | 
4515  | 0  |         if (p_header_size != 2 + l_comp_room) { | 
4516  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");  | 
4517  | 0  |                 return OPJ_FALSE;  | 
4518  | 0  |         }  | 
4519  |  |  | 
4520  | 0  |         l_cp = &(p_j2k->m_cp);  | 
4521  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?  | 
4522  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
4523  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
4524  |  | 
  | 
4525  | 0  |         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */  | 
4526  | 0  |         p_header_data+=l_comp_room;  | 
4527  | 0  |         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */  | 
4528  | 0  |         ++p_header_data;  | 
4529  |  | 
  | 
4530  |  | #ifdef USE_JPWL  | 
4531  |  |         if (l_cp->correct) { | 
4532  |  |                 /* totlen is negative or larger than the bytes left!!! */  | 
4533  |  |                 if (l_comp_room >= l_nb_comp) { | 
4534  |  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
4535  |  |                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",  | 
4536  |  |                                 l_comp_room, l_nb_comp);  | 
4537  |  |                         if (!JPWL_ASSUME || JPWL_ASSUME) { | 
4538  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
4539  |  |                                 return OPJ_FALSE;  | 
4540  |  |                         }  | 
4541  |  |                 }  | 
4542  |  |         };  | 
4543  |  | #endif /* USE_JPWL */  | 
4544  |  |  | 
4545  |  |         /* testcase 3635.pdf.asan.77.2930 */  | 
4546  | 0  |         if (l_comp_no >= l_nb_comp) { | 
4547  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
4548  | 0  |                         "bad component number in RGN (%d when there are only %d)\n",  | 
4549  | 0  |                         l_comp_no, l_nb_comp);  | 
4550  | 0  |                 return OPJ_FALSE;  | 
4551  | 0  |         }  | 
4552  |  |  | 
4553  | 0  |         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */  | 
4554  | 0  |         ++p_header_data;  | 
4555  |  | 
  | 
4556  | 0  |         return OPJ_TRUE;  | 
4557  |  | 
  | 
4558  | 0  | }  | 
4559  |  |  | 
4560  |  | OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp)  | 
4561  | 0  | { | 
4562  | 0  |         return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);  | 
4563  | 0  | }  | 
4564  |  |  | 
4565  |  | OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp)  | 
4566  | 0  | { | 
4567  | 0  |     (void)p_tcp;  | 
4568  | 0  |     return 0;  | 
4569  | 0  | }  | 
4570  |  |  | 
4571  |  | OPJ_BOOL opj_j2k_update_rates(  opj_j2k_t *p_j2k,  | 
4572  |  |                                                             opj_stream_private_t *p_stream,  | 
4573  |  |                                                             opj_event_mgr_t * p_manager )  | 
4574  | 0  | { | 
4575  | 0  |         opj_cp_t * l_cp = 00;  | 
4576  | 0  |         opj_image_t * l_image = 00;  | 
4577  | 0  |         opj_tcp_t * l_tcp = 00;  | 
4578  | 0  |         opj_image_comp_t * l_img_comp = 00;  | 
4579  |  | 
  | 
4580  | 0  |         OPJ_UINT32 i,j,k;  | 
4581  | 0  |         OPJ_INT32 l_x0,l_y0,l_x1,l_y1;  | 
4582  | 0  |         OPJ_FLOAT32 * l_rates = 0;  | 
4583  | 0  |         OPJ_FLOAT32 l_sot_remove;  | 
4584  | 0  |         OPJ_UINT32 l_bits_empty, l_size_pixel;  | 
4585  | 0  |         OPJ_UINT32 l_tile_size = 0;  | 
4586  | 0  |         OPJ_UINT32 l_last_res;  | 
4587  | 0  |         OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00;  | 
4588  |  |  | 
4589  |  |         /* preconditions */  | 
4590  | 0  |         assert(p_j2k != 00);  | 
4591  | 0  |         assert(p_manager != 00);  | 
4592  | 0  |         assert(p_stream != 00);  | 
4593  |  | 
  | 
4594  | 0  |         l_cp = &(p_j2k->m_cp);  | 
4595  | 0  |         l_image = p_j2k->m_private_image;  | 
4596  | 0  |         l_tcp = l_cp->tcps;  | 
4597  |  | 
  | 
4598  | 0  |         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;  | 
4599  | 0  |         l_size_pixel = l_image->numcomps * l_image->comps->prec;  | 
4600  | 0  |         l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(l_cp->th * l_cp->tw);  | 
4601  |  | 
  | 
4602  | 0  |         if (l_cp->m_specific_param.m_enc.m_tp_on) { | 
4603  | 0  |                 l_tp_stride_func = opj_j2k_get_tp_stride;  | 
4604  | 0  |         }  | 
4605  | 0  |         else { | 
4606  | 0  |                 l_tp_stride_func = opj_j2k_get_default_stride;  | 
4607  | 0  |         }  | 
4608  |  | 
  | 
4609  | 0  |         for (i=0;i<l_cp->th;++i) { | 
4610  | 0  |                 for (j=0;j<l_cp->tw;++j) { | 
4611  | 0  |                         OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers;  | 
4612  |  |  | 
4613  |  |                         /* 4 borders of the tile rescale on the image if necessary */  | 
4614  | 0  |                         l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0);  | 
4615  | 0  |                         l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0);  | 
4616  | 0  |                         l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);  | 
4617  | 0  |                         l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);  | 
4618  |  | 
  | 
4619  | 0  |                         l_rates = l_tcp->rates;  | 
4620  |  |  | 
4621  |  |                         /* Modification of the RATE >> */  | 
4622  | 0  |                         if (*l_rates) { | 
4623  | 0  |                                 *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))  | 
4624  | 0  |                                                                 /  | 
4625  | 0  |                                                                 ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)  | 
4626  | 0  |                                                                 )  | 
4627  | 0  |                                                                 -  | 
4628  | 0  |                                                                 l_offset;  | 
4629  | 0  |                         }  | 
4630  |  | 
  | 
4631  | 0  |                         ++l_rates;  | 
4632  |  | 
  | 
4633  | 0  |                         for (k = 1; k < l_tcp->numlayers; ++k) { | 
4634  | 0  |                                 if (*l_rates) { | 
4635  | 0  |                                         *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))  | 
4636  | 0  |                                                                         /  | 
4637  | 0  |                                                                                 ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)  | 
4638  | 0  |                                                                         )  | 
4639  | 0  |                                                                         -  | 
4640  | 0  |                                                                         l_offset;  | 
4641  | 0  |                                 }  | 
4642  |  | 
  | 
4643  | 0  |                                 ++l_rates;  | 
4644  | 0  |                         }  | 
4645  |  | 
  | 
4646  | 0  |                         ++l_tcp;  | 
4647  |  | 
  | 
4648  | 0  |                 }  | 
4649  | 0  |         }  | 
4650  |  | 
  | 
4651  | 0  |         l_tcp = l_cp->tcps;  | 
4652  |  | 
  | 
4653  | 0  |         for (i=0;i<l_cp->th;++i) { | 
4654  | 0  |                 for     (j=0;j<l_cp->tw;++j) { | 
4655  | 0  |                         l_rates = l_tcp->rates;  | 
4656  |  | 
  | 
4657  | 0  |                         if (*l_rates) { | 
4658  | 0  |                                 *l_rates -= l_sot_remove;  | 
4659  |  | 
  | 
4660  | 0  |                                 if (*l_rates < 30) { | 
4661  | 0  |                                         *l_rates = 30;  | 
4662  | 0  |                                 }  | 
4663  | 0  |                         }  | 
4664  |  | 
  | 
4665  | 0  |                         ++l_rates;  | 
4666  |  | 
  | 
4667  | 0  |                         l_last_res = l_tcp->numlayers - 1;  | 
4668  |  | 
  | 
4669  | 0  |                         for (k = 1; k < l_last_res; ++k) { | 
4670  |  | 
  | 
4671  | 0  |                                 if (*l_rates) { | 
4672  | 0  |                                         *l_rates -= l_sot_remove;  | 
4673  |  | 
  | 
4674  | 0  |                                         if (*l_rates < *(l_rates - 1) + 10) { | 
4675  | 0  |                                                 *l_rates  = (*(l_rates - 1)) + 20;  | 
4676  | 0  |                                         }  | 
4677  | 0  |                                 }  | 
4678  |  | 
  | 
4679  | 0  |                                 ++l_rates;  | 
4680  | 0  |                         }  | 
4681  |  | 
  | 
4682  | 0  |                         if (*l_rates) { | 
4683  | 0  |                                 *l_rates -= (l_sot_remove + 2.f);  | 
4684  |  | 
  | 
4685  | 0  |                                 if (*l_rates < *(l_rates - 1) + 10) { | 
4686  | 0  |                                         *l_rates  = (*(l_rates - 1)) + 20;  | 
4687  | 0  |                                 }  | 
4688  | 0  |                         }  | 
4689  |  | 
  | 
4690  | 0  |                         ++l_tcp;  | 
4691  | 0  |                 }  | 
4692  | 0  |         }  | 
4693  |  | 
  | 
4694  | 0  |         l_img_comp = l_image->comps;  | 
4695  | 0  |         l_tile_size = 0;  | 
4696  |  | 
  | 
4697  | 0  |         for (i=0;i<l_image->numcomps;++i) { | 
4698  | 0  |                 l_tile_size += (        opj_uint_ceildiv(l_cp->tdx,l_img_comp->dx)  | 
4699  | 0  |                                                         *  | 
4700  | 0  |                                                         opj_uint_ceildiv(l_cp->tdy,l_img_comp->dy)  | 
4701  | 0  |                                                         *  | 
4702  | 0  |                                                         l_img_comp->prec  | 
4703  | 0  |                                                 );  | 
4704  |  | 
  | 
4705  | 0  |                 ++l_img_comp;  | 
4706  | 0  |         }  | 
4707  |  | 
  | 
4708  | 0  |         l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */  | 
4709  |  | 
  | 
4710  | 0  |         l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);  | 
4711  |  | 
  | 
4712  | 0  |         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;  | 
4713  | 0  |         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =  | 
4714  | 0  |                         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);  | 
4715  | 0  |         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { | 
4716  | 0  |                 return OPJ_FALSE;  | 
4717  | 0  |         }  | 
4718  |  |  | 
4719  | 0  |         if (l_cp->m_specific_param.m_enc.m_cinema) { | 
4720  | 0  |                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =  | 
4721  | 0  |                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);  | 
4722  | 0  |                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { | 
4723  | 0  |                         return OPJ_FALSE;  | 
4724  | 0  |                 }  | 
4725  |  |  | 
4726  | 0  |                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =  | 
4727  | 0  |                                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;  | 
4728  | 0  |         }  | 
4729  |  |  | 
4730  | 0  |         return OPJ_TRUE;  | 
4731  | 0  | }  | 
4732  |  |  | 
4733  |  | #if 0  | 
4734  |  | OPJ_BOOL opj_j2k_read_eoc (     opj_j2k_t *p_j2k,  | 
4735  |  |                                                         opj_stream_private_t *p_stream,  | 
4736  |  |                                                         opj_event_mgr_t * p_manager )  | 
4737  |  | { | 
4738  |  |         OPJ_UINT32 i;  | 
4739  |  |         opj_tcd_t * l_tcd = 00;  | 
4740  |  |         OPJ_UINT32 l_nb_tiles;  | 
4741  |  |         opj_tcp_t * l_tcp = 00;  | 
4742  |  |         OPJ_BOOL l_success;  | 
4743  |  |  | 
4744  |  |         /* preconditions */  | 
4745  |  |         assert(p_j2k != 00);  | 
4746  |  |         assert(p_manager != 00);  | 
4747  |  |         assert(p_stream != 00);  | 
4748  |  |  | 
4749  |  |         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
4750  |  |         l_tcp = p_j2k->m_cp.tcps;  | 
4751  |  |  | 
4752  |  |         l_tcd = opj_tcd_create(OPJ_TRUE);  | 
4753  |  |         if (l_tcd == 00) { | 
4754  |  |                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");  | 
4755  |  |                 return OPJ_FALSE;  | 
4756  |  |         }  | 
4757  |  |  | 
4758  |  |         for (i = 0; i < l_nb_tiles; ++i) { | 
4759  |  |                 if (l_tcp->m_data) { | 
4760  |  |                         if (! opj_tcd_init_decode_tile(l_tcd, i)) { | 
4761  |  |                                 opj_tcd_destroy(l_tcd);  | 
4762  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");  | 
4763  |  |                                 return OPJ_FALSE;  | 
4764  |  |                         }  | 
4765  |  |  | 
4766  |  |                         l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);  | 
4767  |  |                         /* cleanup */  | 
4768  |  |  | 
4769  |  |                         if (! l_success) { | 
4770  |  |                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;  | 
4771  |  |                                 break;  | 
4772  |  |                         }  | 
4773  |  |                 }  | 
4774  |  |  | 
4775  |  |                 opj_j2k_tcp_destroy(l_tcp);  | 
4776  |  |                 ++l_tcp;  | 
4777  |  |         }  | 
4778  |  |  | 
4779  |  |         opj_tcd_destroy(l_tcd);  | 
4780  |  |         return OPJ_TRUE;  | 
4781  |  | }  | 
4782  |  | #endif  | 
4783  |  |  | 
4784  |  | OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,  | 
4785  |  |                                                         struct opj_stream_private *p_stream,  | 
4786  |  |                                                         struct opj_event_mgr * p_manager )  | 
4787  | 0  | { | 
4788  |  |         /* preconditions */  | 
4789  | 0  |         assert(p_j2k != 00);  | 
4790  | 0  |         assert(p_manager != 00);  | 
4791  | 0  |         assert(p_stream != 00);  | 
4792  |  | 
  | 
4793  | 0  |         p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);  | 
4794  |  | 
  | 
4795  | 0  |         return OPJ_TRUE;  | 
4796  | 0  | }  | 
4797  |  |  | 
4798  |  | OPJ_BOOL opj_j2k_write_mct_data_group(  opj_j2k_t *p_j2k,  | 
4799  |  |                                                                         struct opj_stream_private *p_stream,  | 
4800  |  |                                                                         struct opj_event_mgr * p_manager )  | 
4801  | 0  | { | 
4802  | 0  |         OPJ_UINT32 i;  | 
4803  | 0  |         opj_simple_mcc_decorrelation_data_t * l_mcc_record;  | 
4804  | 0  |         opj_mct_data_t * l_mct_record;  | 
4805  | 0  |         opj_tcp_t * l_tcp;  | 
4806  |  |  | 
4807  |  |         /* preconditions */  | 
4808  | 0  |         assert(p_j2k != 00);  | 
4809  | 0  |         assert(p_stream != 00);  | 
4810  | 0  |         assert(p_manager != 00);  | 
4811  |  | 
  | 
4812  | 0  |         if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) { | 
4813  | 0  |                 return OPJ_FALSE;  | 
4814  | 0  |         }  | 
4815  |  |  | 
4816  | 0  |         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);  | 
4817  | 0  |         l_mct_record = l_tcp->m_mct_records;  | 
4818  |  | 
  | 
4819  | 0  |         for (i=0;i<l_tcp->m_nb_mct_records;++i) { | 
4820  |  | 
  | 
4821  | 0  |                 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) { | 
4822  | 0  |                         return OPJ_FALSE;  | 
4823  | 0  |                 }  | 
4824  |  |  | 
4825  | 0  |                 ++l_mct_record;  | 
4826  | 0  |         }  | 
4827  |  |  | 
4828  | 0  |         l_mcc_record = l_tcp->m_mcc_records;  | 
4829  |  | 
  | 
4830  | 0  |         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) { | 
4831  |  | 
  | 
4832  | 0  |                 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) { | 
4833  | 0  |                         return OPJ_FALSE;  | 
4834  | 0  |                 }  | 
4835  |  |  | 
4836  | 0  |                 ++l_mcc_record;  | 
4837  | 0  |         }  | 
4838  |  |  | 
4839  | 0  |         if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) { | 
4840  | 0  |                 return OPJ_FALSE;  | 
4841  | 0  |         }  | 
4842  |  |  | 
4843  | 0  |         return OPJ_TRUE;  | 
4844  | 0  | }  | 
4845  |  |  | 
4846  |  | #if 0  | 
4847  |  | OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,  | 
4848  |  |                                                                         struct opj_stream_private *p_stream,  | 
4849  |  |                                                                         struct opj_event_mgr * p_manager )  | 
4850  |  | { | 
4851  |  |         OPJ_UINT32 compno;  | 
4852  |  |  | 
4853  |  |         /* preconditions */  | 
4854  |  |         assert(p_j2k != 00);  | 
4855  |  |         assert(p_manager != 00);  | 
4856  |  |         assert(p_stream != 00);  | 
4857  |  |  | 
4858  |  |         for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  | 
4859  |  |         { | 
4860  |  |                 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) { | 
4861  |  |                         return OPJ_FALSE;  | 
4862  |  |                 }  | 
4863  |  |         }  | 
4864  |  |  | 
4865  |  |         return OPJ_TRUE;  | 
4866  |  | }  | 
4867  |  | #endif  | 
4868  |  |  | 
4869  |  | #if 0  | 
4870  |  | OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,  | 
4871  |  |                                                                         struct opj_stream_private *p_stream,  | 
4872  |  |                                                                         struct opj_event_mgr * p_manager )  | 
4873  |  | { | 
4874  |  |         OPJ_UINT32 compno;  | 
4875  |  |  | 
4876  |  |         /* preconditions */  | 
4877  |  |         assert(p_j2k != 00);  | 
4878  |  |         assert(p_manager != 00);  | 
4879  |  |         assert(p_stream != 00);  | 
4880  |  |  | 
4881  |  |         for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  | 
4882  |  |         { | 
4883  |  |                 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) { | 
4884  |  |                         return OPJ_FALSE;  | 
4885  |  |                 }  | 
4886  |  |         }  | 
4887  |  |  | 
4888  |  |         return OPJ_TRUE;  | 
4889  |  | }  | 
4890  |  | #endif  | 
4891  |  |  | 
4892  |  |  | 
4893  |  | OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,  | 
4894  |  |                                                         struct opj_stream_private *p_stream,  | 
4895  |  |                                                         struct opj_event_mgr * p_manager )  | 
4896  | 0  | { | 
4897  | 0  |         OPJ_UINT32 compno;  | 
4898  | 0  |         const opj_tccp_t *l_tccp = 00;  | 
4899  |  |  | 
4900  |  |         /* preconditions */  | 
4901  | 0  |         assert(p_j2k != 00);  | 
4902  | 0  |         assert(p_manager != 00);  | 
4903  | 0  |         assert(p_stream != 00);  | 
4904  |  | 
  | 
4905  | 0  |         l_tccp = p_j2k->m_cp.tcps->tccps;  | 
4906  |  | 
  | 
4907  | 0  |         for     (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  { | 
4908  | 0  |                 if (l_tccp->roishift) { | 
4909  |  | 
  | 
4910  | 0  |                         if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream,p_manager)) { | 
4911  | 0  |                                 return OPJ_FALSE;  | 
4912  | 0  |                         }  | 
4913  | 0  |                 }  | 
4914  |  |  | 
4915  | 0  |                 ++l_tccp;  | 
4916  | 0  |         }  | 
4917  |  |  | 
4918  | 0  |         return OPJ_TRUE;  | 
4919  | 0  | }  | 
4920  |  |  | 
4921  |  | OPJ_BOOL opj_j2k_write_epc(     opj_j2k_t *p_j2k,  | 
4922  |  |                                                 struct opj_stream_private *p_stream,  | 
4923  |  |                                                 struct opj_event_mgr * p_manager )  | 
4924  | 0  | { | 
4925  | 0  |         opj_codestream_index_t * l_cstr_index = 00;  | 
4926  |  |  | 
4927  |  |         /* preconditions */  | 
4928  | 0  |         assert(p_j2k != 00);  | 
4929  | 0  |         assert(p_manager != 00);  | 
4930  | 0  |         assert(p_stream != 00);  | 
4931  |  | 
  | 
4932  | 0  |         l_cstr_index = p_j2k->cstr_index;  | 
4933  | 0  |         if (l_cstr_index) { | 
4934  | 0  |                 l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);  | 
4935  |  |                 /* UniPG>> */  | 
4936  |  |                 /* The following adjustment is done to adjust the codestream size */  | 
4937  |  |                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */  | 
4938  |  |                 /* the first bunch of bytes is not in the codestream              */  | 
4939  | 0  |                 l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;  | 
4940  |  |                 /* <<UniPG */  | 
4941  | 0  |         }  | 
4942  |  | 
  | 
4943  |  | #ifdef USE_JPWL  | 
4944  |  |         /* preparation of JPWL marker segments */  | 
4945  |  | #if 0  | 
4946  |  |         if(cp->epc_on) { | 
4947  |  |  | 
4948  |  |                 /* encode according to JPWL */  | 
4949  |  |                 jpwl_encode(p_j2k, p_stream, image);  | 
4950  |  |  | 
4951  |  |         }  | 
4952  |  | #endif  | 
4953  |  |   assert( 0 && "TODO" );  | 
4954  |  | #endif /* USE_JPWL */  | 
4955  |  | 
  | 
4956  | 0  |         return OPJ_TRUE;  | 
4957  | 0  | }  | 
4958  |  |  | 
4959  |  | OPJ_BOOL opj_j2k_read_unk (     opj_j2k_t *p_j2k,  | 
4960  |  |                                                         opj_stream_private_t *p_stream,  | 
4961  |  |                                                         OPJ_UINT32 *output_marker,  | 
4962  |  |                                                         opj_event_mgr_t * p_manager  | 
4963  |  |                                                         )  | 
4964  | 0  | { | 
4965  | 0  |         OPJ_UINT32 l_unknown_marker;  | 
4966  | 0  |         const opj_dec_memory_marker_handler_t * l_marker_handler;  | 
4967  | 0  |         OPJ_UINT32 l_size_unk = 2;  | 
4968  |  |  | 
4969  |  |         /* preconditions*/  | 
4970  | 0  |         assert(p_j2k != 00);  | 
4971  | 0  |         assert(p_manager != 00);  | 
4972  | 0  |         assert(p_stream != 00);  | 
4973  |  | 
  | 
4974  | 0  |         opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");  | 
4975  |  | 
  | 
4976  | 0  |         while(1) { | 
4977  |  |                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/  | 
4978  | 0  |                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
4979  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
4980  | 0  |                         return OPJ_FALSE;  | 
4981  | 0  |                 }  | 
4982  |  |  | 
4983  |  |                 /* read 2 bytes as the new marker ID*/  | 
4984  | 0  |                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);  | 
4985  |  | 
  | 
4986  | 0  |                 if (!(l_unknown_marker < 0xff00)) { | 
4987  |  |  | 
4988  |  |                         /* Get the marker handler from the marker ID*/  | 
4989  | 0  |                         l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);  | 
4990  |  | 
  | 
4991  | 0  |                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { | 
4992  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");  | 
4993  | 0  |                                 return OPJ_FALSE;  | 
4994  | 0  |                         }  | 
4995  | 0  |                         else { | 
4996  | 0  |                                 if (l_marker_handler->id != J2K_MS_UNK) { | 
4997  |  |                                         /* Add the marker to the codestream index*/  | 
4998  | 0  |                                         if (l_marker_handler->id != J2K_MS_SOT)  | 
4999  | 0  |                                         { | 
5000  | 0  |                                                 OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,  | 
5001  | 0  |                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,  | 
5002  | 0  |                                                                 l_size_unk);  | 
5003  | 0  |                                                 if (res == OPJ_FALSE) { | 
5004  | 0  |                                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");  | 
5005  | 0  |                                                         return OPJ_FALSE;  | 
5006  | 0  |                                                 }  | 
5007  | 0  |                                         }  | 
5008  | 0  |                                         break; /* next marker is known and well located */  | 
5009  | 0  |                                 }  | 
5010  | 0  |                                 else  | 
5011  | 0  |                                         l_size_unk += 2;  | 
5012  | 0  |                         }  | 
5013  | 0  |                 }  | 
5014  | 0  |         }  | 
5015  |  |  | 
5016  | 0  |         *output_marker = l_marker_handler->id ;  | 
5017  |  | 
  | 
5018  | 0  |         return OPJ_TRUE;  | 
5019  | 0  | }  | 
5020  |  |  | 
5021  |  | OPJ_BOOL opj_j2k_write_mct_record(      opj_j2k_t *p_j2k,  | 
5022  |  |                                                                 opj_mct_data_t * p_mct_record,  | 
5023  |  |                                                                 struct opj_stream_private *p_stream,  | 
5024  |  |                                                                 struct opj_event_mgr * p_manager )  | 
5025  | 0  | { | 
5026  | 0  |         OPJ_UINT32 l_mct_size;  | 
5027  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
5028  | 0  |         OPJ_UINT32 l_tmp;  | 
5029  |  |  | 
5030  |  |         /* preconditions */  | 
5031  | 0  |         assert(p_j2k != 00);  | 
5032  | 0  |         assert(p_manager != 00);  | 
5033  | 0  |         assert(p_stream != 00);  | 
5034  |  | 
  | 
5035  | 0  |         l_mct_size = 10 + p_mct_record->m_data_size;  | 
5036  |  | 
  | 
5037  | 0  |         if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
5038  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);  | 
5039  | 0  |                 if (! new_header_tile_data) { | 
5040  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
5041  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
5042  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
5043  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");  | 
5044  | 0  |                         return OPJ_FALSE;  | 
5045  | 0  |                 }  | 
5046  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
5047  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;  | 
5048  | 0  |         }  | 
5049  |  |  | 
5050  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
5051  |  | 
  | 
5052  | 0  |         opj_write_bytes(l_current_data,J2K_MS_MCT,2);                                   /* MCT */  | 
5053  | 0  |         l_current_data += 2;  | 
5054  |  | 
  | 
5055  | 0  |         opj_write_bytes(l_current_data,l_mct_size-2,2);                                 /* Lmct */  | 
5056  | 0  |         l_current_data += 2;  | 
5057  |  | 
  | 
5058  | 0  |         opj_write_bytes(l_current_data,0,2);                                                    /* Zmct */  | 
5059  | 0  |         l_current_data += 2;  | 
5060  |  |  | 
5061  |  |         /* only one marker atm */  | 
5062  | 0  |         l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);  | 
5063  |  | 
  | 
5064  | 0  |         opj_write_bytes(l_current_data,l_tmp,2);  | 
5065  | 0  |         l_current_data += 2;  | 
5066  |  | 
  | 
5067  | 0  |         opj_write_bytes(l_current_data,0,2);                                                    /* Ymct */  | 
5068  | 0  |         l_current_data+=2;  | 
5069  |  | 
  | 
5070  | 0  |         memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);  | 
5071  |  | 
  | 
5072  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) { | 
5073  | 0  |                 return OPJ_FALSE;  | 
5074  | 0  |         }  | 
5075  |  |  | 
5076  | 0  |         return OPJ_TRUE;  | 
5077  | 0  | }  | 
5078  |  |  | 
5079  |  | /**  | 
5080  |  |  * Reads a MCT marker (Multiple Component Transform)  | 
5081  |  |  *  | 
5082  |  |  * @param       p_header_data   the data contained in the MCT box.  | 
5083  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
5084  |  |  * @param       p_header_size   the size of the data contained in the MCT marker.  | 
5085  |  |  * @param       p_manager               the user event manager.  | 
5086  |  | */  | 
5087  |  | static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,  | 
5088  |  |                                                                     OPJ_BYTE * p_header_data,  | 
5089  |  |                                                                     OPJ_UINT32 p_header_size,  | 
5090  |  |                                                                     opj_event_mgr_t * p_manager  | 
5091  |  |                                     )  | 
5092  | 0  | { | 
5093  | 0  |         OPJ_UINT32 i;  | 
5094  | 0  |         opj_tcp_t *l_tcp = 00;  | 
5095  | 0  |         OPJ_UINT32 l_tmp;  | 
5096  | 0  |         OPJ_UINT32 l_indix;  | 
5097  | 0  |         opj_mct_data_t * l_mct_data;  | 
5098  |  |  | 
5099  |  |         /* preconditions */  | 
5100  | 0  |         assert(p_header_data != 00);  | 
5101  | 0  |         assert(p_j2k != 00);  | 
5102  |  | 
  | 
5103  | 0  |         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?  | 
5104  | 0  |                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :  | 
5105  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
5106  |  | 
  | 
5107  | 0  |         if (p_header_size < 2) { | 
5108  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");  | 
5109  | 0  |                 return OPJ_FALSE;  | 
5110  | 0  |         }  | 
5111  |  |  | 
5112  |  |         /* first marker */  | 
5113  | 0  |         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */  | 
5114  | 0  |         p_header_data += 2;  | 
5115  | 0  |         if (l_tmp != 0) { | 
5116  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");  | 
5117  | 0  |                 return OPJ_TRUE;  | 
5118  | 0  |         }  | 
5119  |  |  | 
5120  | 0  |         if(p_header_size <= 6) { | 
5121  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");  | 
5122  | 0  |                 return OPJ_FALSE;  | 
5123  | 0  |         }  | 
5124  |  |  | 
5125  |  |         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/  | 
5126  | 0  |         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */  | 
5127  | 0  |         p_header_data += 2;  | 
5128  |  | 
  | 
5129  | 0  |         l_indix = l_tmp & 0xff;  | 
5130  | 0  |         l_mct_data = l_tcp->m_mct_records;  | 
5131  |  | 
  | 
5132  | 0  |         for (i=0;i<l_tcp->m_nb_mct_records;++i) { | 
5133  | 0  |                 if (l_mct_data->m_index == l_indix) { | 
5134  | 0  |                         break;  | 
5135  | 0  |                 }  | 
5136  | 0  |                 ++l_mct_data;  | 
5137  | 0  |         }  | 
5138  |  |  | 
5139  |  |         /* NOT FOUND */  | 
5140  | 0  |         if (i == l_tcp->m_nb_mct_records) { | 
5141  | 0  |                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { | 
5142  | 0  |                         opj_mct_data_t *new_mct_records;  | 
5143  | 0  |                         l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;  | 
5144  |  | 
  | 
5145  | 0  |                         new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));  | 
5146  | 0  |                         if (! new_mct_records) { | 
5147  | 0  |                                 opj_free(l_tcp->m_mct_records);  | 
5148  | 0  |                                 l_tcp->m_mct_records = NULL;  | 
5149  | 0  |                                 l_tcp->m_nb_max_mct_records = 0;  | 
5150  | 0  |                                 l_tcp->m_nb_mct_records = 0;  | 
5151  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");  | 
5152  | 0  |                                 return OPJ_FALSE;  | 
5153  | 0  |                         }  | 
5154  | 0  |                         l_tcp->m_mct_records = new_mct_records;  | 
5155  | 0  |                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;  | 
5156  | 0  |                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));  | 
5157  | 0  |                 }  | 
5158  |  |  | 
5159  | 0  |                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;  | 
5160  | 0  |         }  | 
5161  |  |  | 
5162  | 0  |         if (l_mct_data->m_data) { | 
5163  | 0  |                 opj_free(l_mct_data->m_data);  | 
5164  | 0  |                 l_mct_data->m_data = 00;  | 
5165  | 0  |         }  | 
5166  |  | 
  | 
5167  | 0  |         l_mct_data->m_index = l_indix;  | 
5168  | 0  |         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);  | 
5169  | 0  |         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);  | 
5170  |  | 
  | 
5171  | 0  |         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */  | 
5172  | 0  |         p_header_data+=2;  | 
5173  | 0  |         if (l_tmp != 0) { | 
5174  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");  | 
5175  | 0  |                 return OPJ_TRUE;  | 
5176  | 0  |         }  | 
5177  |  |  | 
5178  | 0  |         p_header_size -= 6;  | 
5179  |  | 
  | 
5180  | 0  |         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);  | 
5181  | 0  |         if (! l_mct_data->m_data) { | 
5182  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");  | 
5183  | 0  |                 return OPJ_FALSE;  | 
5184  | 0  |         }  | 
5185  | 0  |         memcpy(l_mct_data->m_data,p_header_data,p_header_size);  | 
5186  |  | 
  | 
5187  | 0  |         l_mct_data->m_data_size = p_header_size;  | 
5188  | 0  |         ++l_tcp->m_nb_mct_records;  | 
5189  |  | 
  | 
5190  | 0  |         return OPJ_TRUE;  | 
5191  | 0  | }  | 
5192  |  |  | 
5193  |  | OPJ_BOOL opj_j2k_write_mcc_record(      opj_j2k_t *p_j2k,  | 
5194  |  |                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,  | 
5195  |  |                                                                 struct opj_stream_private *p_stream,  | 
5196  |  |                                                                 struct opj_event_mgr * p_manager )  | 
5197  | 0  | { | 
5198  | 0  |         OPJ_UINT32 i;  | 
5199  | 0  |         OPJ_UINT32 l_mcc_size;  | 
5200  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
5201  | 0  |         OPJ_UINT32 l_nb_bytes_for_comp;  | 
5202  | 0  |         OPJ_UINT32 l_mask;  | 
5203  | 0  |         OPJ_UINT32 l_tmcc;  | 
5204  |  |  | 
5205  |  |         /* preconditions */  | 
5206  | 0  |         assert(p_j2k != 00);  | 
5207  | 0  |         assert(p_manager != 00);  | 
5208  | 0  |         assert(p_stream != 00);  | 
5209  |  | 
  | 
5210  | 0  |         if (p_mcc_record->m_nb_comps > 255 ) { | 
5211  | 0  |         l_nb_bytes_for_comp = 2;  | 
5212  | 0  |                 l_mask = 0x8000;  | 
5213  | 0  |         }  | 
5214  | 0  |         else { | 
5215  | 0  |                 l_nb_bytes_for_comp = 1;  | 
5216  | 0  |                 l_mask = 0;  | 
5217  | 0  |         }  | 
5218  |  | 
  | 
5219  | 0  |         l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;  | 
5220  | 0  |         if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)  | 
5221  | 0  |         { | 
5222  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);  | 
5223  | 0  |                 if (! new_header_tile_data) { | 
5224  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
5225  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
5226  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
5227  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");  | 
5228  | 0  |                         return OPJ_FALSE;  | 
5229  | 0  |                 }  | 
5230  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
5231  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;  | 
5232  | 0  |         }  | 
5233  |  |  | 
5234  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
5235  |  | 
  | 
5236  | 0  |         opj_write_bytes(l_current_data,J2K_MS_MCC,2);                                   /* MCC */  | 
5237  | 0  |         l_current_data += 2;  | 
5238  |  | 
  | 
5239  | 0  |         opj_write_bytes(l_current_data,l_mcc_size-2,2);                                 /* Lmcc */  | 
5240  | 0  |         l_current_data += 2;  | 
5241  |  |  | 
5242  |  |         /* first marker */  | 
5243  | 0  |         opj_write_bytes(l_current_data,0,2);                                    /* Zmcc */  | 
5244  | 0  |         l_current_data += 2;  | 
5245  |  | 
  | 
5246  | 0  |         opj_write_bytes(l_current_data,p_mcc_record->m_index,1);                                        /* Imcc -> no need for other values, take the first */  | 
5247  | 0  |         ++l_current_data;  | 
5248  |  |  | 
5249  |  |         /* only one marker atm */  | 
5250  | 0  |         opj_write_bytes(l_current_data,0,2);                                    /* Ymcc */  | 
5251  | 0  |         l_current_data+=2;  | 
5252  |  | 
  | 
5253  | 0  |         opj_write_bytes(l_current_data,1,2);                                    /* Qmcc -> number of collections -> 1 */  | 
5254  | 0  |         l_current_data+=2;  | 
5255  |  | 
  | 
5256  | 0  |         opj_write_bytes(l_current_data,0x1,1);                                  /* Xmcci type of component transformation -> array based decorrelation */  | 
5257  | 0  |         ++l_current_data;  | 
5258  |  | 
  | 
5259  | 0  |         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2);    /* Nmcci number of input components involved and size for each component offset = 8 bits */  | 
5260  | 0  |         l_current_data+=2;  | 
5261  |  | 
  | 
5262  | 0  |         for (i=0;i<p_mcc_record->m_nb_comps;++i) { | 
5263  | 0  |                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Cmccij Component offset*/  | 
5264  | 0  |                 l_current_data+=l_nb_bytes_for_comp;  | 
5265  | 0  |         }  | 
5266  |  | 
  | 
5267  | 0  |         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2);      /* Mmcci number of output components involved and size for each component offset = 8 bits */  | 
5268  | 0  |         l_current_data+=2;  | 
5269  |  | 
  | 
5270  | 0  |         for (i=0;i<p_mcc_record->m_nb_comps;++i)  | 
5271  | 0  |         { | 
5272  | 0  |                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Wmccij Component offset*/  | 
5273  | 0  |                 l_current_data+=l_nb_bytes_for_comp;  | 
5274  | 0  |         }  | 
5275  |  | 
  | 
5276  | 0  |         l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;  | 
5277  |  | 
  | 
5278  | 0  |         if (p_mcc_record->m_decorrelation_array) { | 
5279  | 0  |                 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;  | 
5280  | 0  |         }  | 
5281  |  | 
  | 
5282  | 0  |         if (p_mcc_record->m_offset_array) { | 
5283  | 0  |                 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);  | 
5284  | 0  |         }  | 
5285  |  | 
  | 
5286  | 0  |         opj_write_bytes(l_current_data,l_tmcc,3);       /* Tmcci : use MCT defined as number 1 and irreversible array based. */  | 
5287  | 0  |         l_current_data+=3;  | 
5288  |  | 
  | 
5289  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) { | 
5290  | 0  |                 return OPJ_FALSE;  | 
5291  | 0  |         }  | 
5292  |  |  | 
5293  | 0  |         return OPJ_TRUE;  | 
5294  | 0  | }  | 
5295  |  |  | 
5296  |  | OPJ_BOOL opj_j2k_read_mcc (     opj_j2k_t *p_j2k,  | 
5297  |  |                                                 OPJ_BYTE * p_header_data,  | 
5298  |  |                                                 OPJ_UINT32 p_header_size,  | 
5299  |  |                                                 opj_event_mgr_t * p_manager )  | 
5300  | 0  | { | 
5301  | 0  |         OPJ_UINT32 i,j;  | 
5302  | 0  |         OPJ_UINT32 l_tmp;  | 
5303  | 0  |         OPJ_UINT32 l_indix;  | 
5304  | 0  |         opj_tcp_t * l_tcp;  | 
5305  | 0  |         opj_simple_mcc_decorrelation_data_t * l_mcc_record;  | 
5306  | 0  |         opj_mct_data_t * l_mct_data;  | 
5307  | 0  |         OPJ_UINT32 l_nb_collections;  | 
5308  | 0  |         OPJ_UINT32 l_nb_comps;  | 
5309  | 0  |         OPJ_UINT32 l_nb_bytes_by_comp;  | 
5310  |  |  | 
5311  |  |         /* preconditions */  | 
5312  | 0  |         assert(p_header_data != 00);  | 
5313  | 0  |         assert(p_j2k != 00);  | 
5314  | 0  |         assert(p_manager != 00);  | 
5315  |  | 
  | 
5316  | 0  |         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?  | 
5317  | 0  |                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :  | 
5318  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
5319  |  | 
  | 
5320  | 0  |         if (p_header_size < 2) { | 
5321  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5322  | 0  |                 return OPJ_FALSE;  | 
5323  | 0  |         }  | 
5324  |  |  | 
5325  |  |         /* first marker */  | 
5326  | 0  |         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */  | 
5327  | 0  |         p_header_data += 2;  | 
5328  | 0  |         if (l_tmp != 0) { | 
5329  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");  | 
5330  | 0  |                 return OPJ_TRUE;  | 
5331  | 0  |         }  | 
5332  |  |  | 
5333  | 0  |         if (p_header_size < 7) { | 
5334  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5335  | 0  |                 return OPJ_FALSE;  | 
5336  | 0  |         }  | 
5337  |  |  | 
5338  | 0  |         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */  | 
5339  | 0  |         ++p_header_data;  | 
5340  |  | 
  | 
5341  | 0  |         l_mcc_record = l_tcp->m_mcc_records;  | 
5342  |  | 
  | 
5343  | 0  |         for(i=0;i<l_tcp->m_nb_mcc_records;++i) { | 
5344  | 0  |                 if (l_mcc_record->m_index == l_indix) { | 
5345  | 0  |                         break;  | 
5346  | 0  |                 }  | 
5347  | 0  |                 ++l_mcc_record;  | 
5348  | 0  |         }  | 
5349  |  |  | 
5350  |  |         /** NOT FOUND */  | 
5351  | 0  |         if (i == l_tcp->m_nb_mcc_records) { | 
5352  | 0  |                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { | 
5353  | 0  |                         opj_simple_mcc_decorrelation_data_t *new_mcc_records;  | 
5354  | 0  |                         l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;  | 
5355  |  | 
  | 
5356  | 0  |                         new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(  | 
5357  | 0  |                                         l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
5358  | 0  |                         if (! new_mcc_records) { | 
5359  | 0  |                                 opj_free(l_tcp->m_mcc_records);  | 
5360  | 0  |                                 l_tcp->m_mcc_records = NULL;  | 
5361  | 0  |                                 l_tcp->m_nb_max_mcc_records = 0;  | 
5362  | 0  |                                 l_tcp->m_nb_mcc_records = 0;  | 
5363  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");  | 
5364  | 0  |                                 return OPJ_FALSE;  | 
5365  | 0  |                         }  | 
5366  | 0  |                         l_tcp->m_mcc_records = new_mcc_records;  | 
5367  | 0  |                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;  | 
5368  | 0  |                         memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
5369  | 0  |                 }  | 
5370  | 0  |                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;  | 
5371  | 0  |         }  | 
5372  | 0  |         l_mcc_record->m_index = l_indix;  | 
5373  |  |  | 
5374  |  |         /* only one marker atm */  | 
5375  | 0  |         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */  | 
5376  | 0  |         p_header_data+=2;  | 
5377  | 0  |         if (l_tmp != 0) { | 
5378  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");  | 
5379  | 0  |                 return OPJ_TRUE;  | 
5380  | 0  |         }  | 
5381  |  |  | 
5382  | 0  |         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */  | 
5383  | 0  |         p_header_data+=2;  | 
5384  |  | 
  | 
5385  | 0  |         if (l_nb_collections > 1) { | 
5386  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");  | 
5387  | 0  |                 return OPJ_TRUE;  | 
5388  | 0  |         }  | 
5389  |  |  | 
5390  | 0  |         p_header_size -= 7;  | 
5391  |  | 
  | 
5392  | 0  |         for (i=0;i<l_nb_collections;++i) { | 
5393  | 0  |                 if (p_header_size < 3) { | 
5394  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5395  | 0  |                         return OPJ_FALSE;  | 
5396  | 0  |                 }  | 
5397  |  |  | 
5398  | 0  |                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */  | 
5399  | 0  |                 ++p_header_data;  | 
5400  |  | 
  | 
5401  | 0  |                 if (l_tmp != 1) { | 
5402  | 0  |                         opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");  | 
5403  | 0  |                         return OPJ_TRUE;  | 
5404  | 0  |                 }  | 
5405  |  |  | 
5406  | 0  |                 opj_read_bytes(p_header_data,&l_nb_comps,2);  | 
5407  |  | 
  | 
5408  | 0  |                 p_header_data+=2;  | 
5409  | 0  |                 p_header_size-=3;  | 
5410  |  | 
  | 
5411  | 0  |                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);  | 
5412  | 0  |                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;  | 
5413  |  | 
  | 
5414  | 0  |                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { | 
5415  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5416  | 0  |                         return OPJ_FALSE;  | 
5417  | 0  |                 }  | 
5418  |  |  | 
5419  | 0  |                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);  | 
5420  |  | 
  | 
5421  | 0  |                 for (j=0;j<l_mcc_record->m_nb_comps;++j) { | 
5422  | 0  |                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/  | 
5423  | 0  |                         p_header_data+=l_nb_bytes_by_comp;  | 
5424  |  | 
  | 
5425  | 0  |                         if (l_tmp != j) { | 
5426  | 0  |                                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");  | 
5427  | 0  |                                 return OPJ_TRUE;  | 
5428  | 0  |                         }  | 
5429  | 0  |                 }  | 
5430  |  |  | 
5431  | 0  |                 opj_read_bytes(p_header_data,&l_nb_comps,2);  | 
5432  | 0  |                 p_header_data+=2;  | 
5433  |  | 
  | 
5434  | 0  |                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);  | 
5435  | 0  |                 l_nb_comps &= 0x7fff;  | 
5436  |  | 
  | 
5437  | 0  |                 if (l_nb_comps != l_mcc_record->m_nb_comps) { | 
5438  | 0  |                         opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");  | 
5439  | 0  |                         return OPJ_TRUE;  | 
5440  | 0  |                 }  | 
5441  |  |  | 
5442  | 0  |                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { | 
5443  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5444  | 0  |                         return OPJ_FALSE;  | 
5445  | 0  |                 }  | 
5446  |  |  | 
5447  | 0  |                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);  | 
5448  |  | 
  | 
5449  | 0  |                 for (j=0;j<l_mcc_record->m_nb_comps;++j) { | 
5450  | 0  |                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/  | 
5451  | 0  |                         p_header_data+=l_nb_bytes_by_comp;  | 
5452  |  | 
  | 
5453  | 0  |                         if (l_tmp != j) { | 
5454  | 0  |                                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");  | 
5455  | 0  |                                 return OPJ_TRUE;  | 
5456  | 0  |                         }  | 
5457  | 0  |                 }  | 
5458  |  |  | 
5459  | 0  |                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/  | 
5460  | 0  |                 p_header_data += 3;  | 
5461  |  | 
  | 
5462  | 0  |                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);  | 
5463  | 0  |                 l_mcc_record->m_decorrelation_array = 00;  | 
5464  | 0  |                 l_mcc_record->m_offset_array = 00;  | 
5465  |  | 
  | 
5466  | 0  |                 l_indix = l_tmp & 0xff;  | 
5467  | 0  |                 if (l_indix != 0) { | 
5468  | 0  |                         l_mct_data = l_tcp->m_mct_records;  | 
5469  | 0  |                         for (j=0;j<l_tcp->m_nb_mct_records;++j) { | 
5470  | 0  |                                 if (l_mct_data->m_index == l_indix) { | 
5471  | 0  |                                         l_mcc_record->m_decorrelation_array = l_mct_data;  | 
5472  | 0  |                                         break;  | 
5473  | 0  |                                 }  | 
5474  | 0  |                                 ++l_mct_data;  | 
5475  | 0  |                         }  | 
5476  |  | 
  | 
5477  | 0  |                         if (l_mcc_record->m_decorrelation_array == 00) { | 
5478  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5479  | 0  |                                 return OPJ_FALSE;  | 
5480  | 0  |                         }  | 
5481  | 0  |                 }  | 
5482  |  |  | 
5483  | 0  |                 l_indix = (l_tmp >> 8) & 0xff;  | 
5484  | 0  |                 if (l_indix != 0) { | 
5485  | 0  |                         l_mct_data = l_tcp->m_mct_records;  | 
5486  | 0  |                         for (j=0;j<l_tcp->m_nb_mct_records;++j) { | 
5487  | 0  |                                 if (l_mct_data->m_index == l_indix) { | 
5488  | 0  |                                         l_mcc_record->m_offset_array = l_mct_data;  | 
5489  | 0  |                                         break;  | 
5490  | 0  |                                 }  | 
5491  | 0  |                                 ++l_mct_data;  | 
5492  | 0  |                         }  | 
5493  |  | 
  | 
5494  | 0  |                         if (l_mcc_record->m_offset_array == 00) { | 
5495  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5496  | 0  |                                 return OPJ_FALSE;  | 
5497  | 0  |                         }  | 
5498  | 0  |                 }  | 
5499  | 0  |         }  | 
5500  |  |  | 
5501  | 0  |         if (p_header_size != 0) { | 
5502  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");  | 
5503  | 0  |                 return OPJ_FALSE;  | 
5504  | 0  |         }  | 
5505  |  |  | 
5506  | 0  |         ++l_tcp->m_nb_mcc_records;  | 
5507  |  | 
  | 
5508  | 0  |         return OPJ_TRUE;  | 
5509  | 0  | }  | 
5510  |  |  | 
5511  |  | OPJ_BOOL opj_j2k_write_mco(     opj_j2k_t *p_j2k,  | 
5512  |  |                                                 struct opj_stream_private *p_stream,  | 
5513  |  |                                                 struct opj_event_mgr * p_manager  | 
5514  |  |                                   )  | 
5515  | 0  | { | 
5516  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
5517  | 0  |         OPJ_UINT32 l_mco_size;  | 
5518  | 0  |         opj_tcp_t * l_tcp = 00;  | 
5519  | 0  |         opj_simple_mcc_decorrelation_data_t * l_mcc_record;  | 
5520  | 0  |         OPJ_UINT32 i;  | 
5521  |  |  | 
5522  |  |         /* preconditions */  | 
5523  | 0  |         assert(p_j2k != 00);  | 
5524  | 0  |         assert(p_manager != 00);  | 
5525  | 0  |         assert(p_stream != 00);  | 
5526  |  | 
  | 
5527  | 0  |         l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);  | 
5528  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
5529  |  | 
  | 
5530  | 0  |         l_mco_size = 5 + l_tcp->m_nb_mcc_records;  | 
5531  | 0  |         if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
5532  |  | 
  | 
5533  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);  | 
5534  | 0  |                 if (! new_header_tile_data) { | 
5535  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
5536  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
5537  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
5538  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");  | 
5539  | 0  |                         return OPJ_FALSE;  | 
5540  | 0  |                 }  | 
5541  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
5542  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;  | 
5543  | 0  |         }  | 
5544  |  |  | 
5545  | 0  |         opj_write_bytes(l_current_data,J2K_MS_MCO,2);                   /* MCO */  | 
5546  | 0  |         l_current_data += 2;  | 
5547  |  | 
  | 
5548  | 0  |         opj_write_bytes(l_current_data,l_mco_size-2,2);                 /* Lmco */  | 
5549  | 0  |         l_current_data += 2;  | 
5550  |  | 
  | 
5551  | 0  |         opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1);      /* Nmco : only one tranform stage*/  | 
5552  | 0  |         ++l_current_data;  | 
5553  |  | 
  | 
5554  | 0  |         l_mcc_record = l_tcp->m_mcc_records;  | 
5555  | 0  |         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) { | 
5556  | 0  |                 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/  | 
5557  | 0  |                 ++l_current_data;  | 
5558  |  | 
  | 
5559  | 0  |                 ++l_mcc_record;  | 
5560  | 0  |         }  | 
5561  |  | 
  | 
5562  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) { | 
5563  | 0  |                 return OPJ_FALSE;  | 
5564  | 0  |         }  | 
5565  |  |  | 
5566  | 0  |         return OPJ_TRUE;  | 
5567  | 0  | }  | 
5568  |  |  | 
5569  |  | /**  | 
5570  |  |  * Reads a MCO marker (Multiple Component Transform Ordering)  | 
5571  |  |  *  | 
5572  |  |  * @param       p_header_data   the data contained in the MCO box.  | 
5573  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
5574  |  |  * @param       p_header_size   the size of the data contained in the MCO marker.  | 
5575  |  |  * @param       p_manager               the user event manager.  | 
5576  |  | */  | 
5577  |  | static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,  | 
5578  |  |                                                                     OPJ_BYTE * p_header_data,  | 
5579  |  |                                                                     OPJ_UINT32 p_header_size,  | 
5580  |  |                                                                     opj_event_mgr_t * p_manager  | 
5581  |  |                                     )  | 
5582  | 0  | { | 
5583  | 0  |         OPJ_UINT32 l_tmp, i;  | 
5584  | 0  |         OPJ_UINT32 l_nb_stages;  | 
5585  | 0  |         opj_tcp_t * l_tcp;  | 
5586  | 0  |         opj_tccp_t * l_tccp;  | 
5587  | 0  |         opj_image_t * l_image;  | 
5588  |  |  | 
5589  |  |         /* preconditions */  | 
5590  | 0  |         assert(p_header_data != 00);  | 
5591  | 0  |         assert(p_j2k != 00);  | 
5592  | 0  |         assert(p_manager != 00);  | 
5593  |  | 
  | 
5594  | 0  |         l_image = p_j2k->m_private_image;  | 
5595  | 0  |         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?  | 
5596  | 0  |                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :  | 
5597  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
5598  |  | 
  | 
5599  | 0  |         if (p_header_size < 1) { | 
5600  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");  | 
5601  | 0  |                 return OPJ_FALSE;  | 
5602  | 0  |         }  | 
5603  |  |  | 
5604  | 0  |         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/  | 
5605  | 0  |         ++p_header_data;  | 
5606  |  | 
  | 
5607  | 0  |         if (l_nb_stages > 1) { | 
5608  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");  | 
5609  | 0  |                 return OPJ_TRUE;  | 
5610  | 0  |         }  | 
5611  |  |  | 
5612  | 0  |         if (p_header_size != l_nb_stages + 1) { | 
5613  | 0  |                 opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");  | 
5614  | 0  |                 return OPJ_FALSE;  | 
5615  | 0  |         }  | 
5616  |  |  | 
5617  | 0  |         l_tccp = l_tcp->tccps;  | 
5618  |  | 
  | 
5619  | 0  |         for (i=0;i<l_image->numcomps;++i) { | 
5620  | 0  |                 l_tccp->m_dc_level_shift = 0;  | 
5621  | 0  |                 ++l_tccp;  | 
5622  | 0  |         }  | 
5623  |  | 
  | 
5624  | 0  |         if (l_tcp->m_mct_decoding_matrix) { | 
5625  | 0  |                 opj_free(l_tcp->m_mct_decoding_matrix);  | 
5626  | 0  |                 l_tcp->m_mct_decoding_matrix = 00;  | 
5627  | 0  |         }  | 
5628  |  | 
  | 
5629  | 0  |         for (i=0;i<l_nb_stages;++i) { | 
5630  | 0  |                 opj_read_bytes(p_header_data,&l_tmp,1);  | 
5631  | 0  |                 ++p_header_data;  | 
5632  |  | 
  | 
5633  | 0  |                 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) { | 
5634  | 0  |                         return OPJ_FALSE;  | 
5635  | 0  |                 }  | 
5636  | 0  |         }  | 
5637  |  |  | 
5638  | 0  |         return OPJ_TRUE;  | 
5639  | 0  | }  | 
5640  |  |  | 
5641  |  | OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)  | 
5642  | 0  | { | 
5643  | 0  |         OPJ_UINT32 i;  | 
5644  | 0  |         opj_simple_mcc_decorrelation_data_t * l_mcc_record;  | 
5645  | 0  |         opj_mct_data_t * l_deco_array, * l_offset_array;  | 
5646  | 0  |         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;  | 
5647  | 0  |         OPJ_UINT32 l_nb_elem;  | 
5648  | 0  |         OPJ_UINT32 * l_offset_data, * l_current_offset_data;  | 
5649  | 0  |         opj_tccp_t * l_tccp;  | 
5650  |  |  | 
5651  |  |         /* preconditions */  | 
5652  | 0  |         assert(p_tcp != 00);  | 
5653  |  | 
  | 
5654  | 0  |         l_mcc_record = p_tcp->m_mcc_records;  | 
5655  |  | 
  | 
5656  | 0  |         for (i=0;i<p_tcp->m_nb_mcc_records;++i) { | 
5657  | 0  |                 if (l_mcc_record->m_index == p_index) { | 
5658  | 0  |                         break;  | 
5659  | 0  |                 }  | 
5660  | 0  |         }  | 
5661  |  | 
  | 
5662  | 0  |         if (i==p_tcp->m_nb_mcc_records) { | 
5663  |  |                 /** element discarded **/  | 
5664  | 0  |                 return OPJ_TRUE;  | 
5665  | 0  |         }  | 
5666  |  |  | 
5667  | 0  |         if (l_mcc_record->m_nb_comps != p_image->numcomps) { | 
5668  |  |                 /** do not support number of comps != image */  | 
5669  | 0  |                 return OPJ_TRUE;  | 
5670  | 0  |         }  | 
5671  |  |  | 
5672  | 0  |         l_deco_array = l_mcc_record->m_decorrelation_array;  | 
5673  |  | 
  | 
5674  | 0  |         if (l_deco_array) { | 
5675  | 0  |                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;  | 
5676  | 0  |                 if (l_deco_array->m_data_size != l_data_size) { | 
5677  | 0  |                         return OPJ_FALSE;  | 
5678  | 0  |                 }  | 
5679  |  |  | 
5680  | 0  |                 l_nb_elem = p_image->numcomps * p_image->numcomps;  | 
5681  | 0  |                 l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);  | 
5682  | 0  |                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);  | 
5683  |  | 
  | 
5684  | 0  |                 if (! p_tcp->m_mct_decoding_matrix ) { | 
5685  | 0  |                         return OPJ_FALSE;  | 
5686  | 0  |                 }  | 
5687  |  |  | 
5688  | 0  |                 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);  | 
5689  | 0  |         }  | 
5690  |  |  | 
5691  | 0  |         l_offset_array = l_mcc_record->m_offset_array;  | 
5692  |  | 
  | 
5693  | 0  |         if (l_offset_array) { | 
5694  | 0  |                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;  | 
5695  | 0  |                 if (l_offset_array->m_data_size != l_data_size) { | 
5696  | 0  |                         return OPJ_FALSE;  | 
5697  | 0  |                 }  | 
5698  |  |  | 
5699  | 0  |                 l_nb_elem = p_image->numcomps;  | 
5700  | 0  |                 l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);  | 
5701  | 0  |                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);  | 
5702  |  | 
  | 
5703  | 0  |                 if (! l_offset_data ) { | 
5704  | 0  |                         return OPJ_FALSE;  | 
5705  | 0  |                 }  | 
5706  |  |  | 
5707  | 0  |                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);  | 
5708  |  | 
  | 
5709  | 0  |                 l_tccp = p_tcp->tccps;  | 
5710  | 0  |                 l_current_offset_data = l_offset_data;  | 
5711  |  | 
  | 
5712  | 0  |                 for (i=0;i<p_image->numcomps;++i) { | 
5713  | 0  |                         l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++);  | 
5714  | 0  |                         ++l_tccp;  | 
5715  | 0  |                 }  | 
5716  |  | 
  | 
5717  | 0  |                 opj_free(l_offset_data);  | 
5718  | 0  |         }  | 
5719  |  |  | 
5720  | 0  |         return OPJ_TRUE;  | 
5721  | 0  | }  | 
5722  |  |  | 
5723  |  | OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,  | 
5724  |  |                                                 struct opj_stream_private *p_stream,  | 
5725  |  |                                                 struct opj_event_mgr * p_manager )  | 
5726  | 0  | { | 
5727  | 0  |         OPJ_UINT32 i;  | 
5728  | 0  |         OPJ_UINT32 l_cbd_size;  | 
5729  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
5730  | 0  |         opj_image_t *l_image = 00;  | 
5731  | 0  |         opj_image_comp_t * l_comp = 00;  | 
5732  |  |  | 
5733  |  |         /* preconditions */  | 
5734  | 0  |         assert(p_j2k != 00);  | 
5735  | 0  |         assert(p_manager != 00);  | 
5736  | 0  |         assert(p_stream != 00);  | 
5737  |  | 
  | 
5738  | 0  |         l_image = p_j2k->m_private_image;  | 
5739  | 0  |         l_cbd_size = 6 + p_j2k->m_private_image->numcomps;  | 
5740  |  | 
  | 
5741  | 0  |         if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { | 
5742  | 0  |                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);  | 
5743  | 0  |                 if (! new_header_tile_data) { | 
5744  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
5745  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;  | 
5746  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
5747  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");  | 
5748  | 0  |                         return OPJ_FALSE;  | 
5749  | 0  |                 }  | 
5750  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;  | 
5751  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;  | 
5752  | 0  |         }  | 
5753  |  |  | 
5754  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;  | 
5755  |  | 
  | 
5756  | 0  |         opj_write_bytes(l_current_data,J2K_MS_CBD,2);                   /* CBD */  | 
5757  | 0  |         l_current_data += 2;  | 
5758  |  | 
  | 
5759  | 0  |         opj_write_bytes(l_current_data,l_cbd_size-2,2);                 /* L_CBD */  | 
5760  | 0  |         l_current_data += 2;  | 
5761  |  | 
  | 
5762  | 0  |         opj_write_bytes(l_current_data,l_image->numcomps, 2);           /* Ncbd */  | 
5763  | 0  |         l_current_data+=2;  | 
5764  |  | 
  | 
5765  | 0  |         l_comp = l_image->comps;  | 
5766  |  | 
  | 
5767  | 0  |         for (i=0;i<l_image->numcomps;++i) { | 
5768  | 0  |                 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1);           /* Component bit depth */  | 
5769  | 0  |                 ++l_current_data;  | 
5770  |  | 
  | 
5771  | 0  |                 ++l_comp;  | 
5772  | 0  |         }  | 
5773  |  | 
  | 
5774  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) { | 
5775  | 0  |                 return OPJ_FALSE;  | 
5776  | 0  |         }  | 
5777  |  |  | 
5778  | 0  |         return OPJ_TRUE;  | 
5779  | 0  | }  | 
5780  |  |  | 
5781  |  | /**  | 
5782  |  |  * Reads a CBD marker (Component bit depth definition)  | 
5783  |  |  * @param       p_header_data   the data contained in the CBD box.  | 
5784  |  |  * @param       p_j2k                   the jpeg2000 codec.  | 
5785  |  |  * @param       p_header_size   the size of the data contained in the CBD marker.  | 
5786  |  |  * @param       p_manager               the user event manager.  | 
5787  |  | */  | 
5788  |  | static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,  | 
5789  |  |                                                                 OPJ_BYTE * p_header_data,  | 
5790  |  |                                                                 OPJ_UINT32 p_header_size,  | 
5791  |  |                                                                 opj_event_mgr_t * p_manager  | 
5792  |  |                                     )  | 
5793  | 0  | { | 
5794  | 0  |         OPJ_UINT32 l_nb_comp,l_num_comp;  | 
5795  | 0  |         OPJ_UINT32 l_comp_def;  | 
5796  | 0  |         OPJ_UINT32 i;  | 
5797  | 0  |         opj_image_comp_t * l_comp = 00;  | 
5798  |  |  | 
5799  |  |         /* preconditions */  | 
5800  | 0  |         assert(p_header_data != 00);  | 
5801  | 0  |         assert(p_j2k != 00);  | 
5802  | 0  |         assert(p_manager != 00);  | 
5803  |  | 
  | 
5804  | 0  |         l_num_comp = p_j2k->m_private_image->numcomps;  | 
5805  |  | 
  | 
5806  | 0  |         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { | 
5807  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");  | 
5808  | 0  |                 return OPJ_FALSE;  | 
5809  | 0  |         }  | 
5810  |  |  | 
5811  | 0  |         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */  | 
5812  | 0  |         p_header_data+=2;  | 
5813  |  | 
  | 
5814  | 0  |         if (l_nb_comp != l_num_comp) { | 
5815  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");  | 
5816  | 0  |                 return OPJ_FALSE;  | 
5817  | 0  |         }  | 
5818  |  |  | 
5819  | 0  |         l_comp = p_j2k->m_private_image->comps;  | 
5820  | 0  |         for (i=0;i<l_num_comp;++i) { | 
5821  | 0  |                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */  | 
5822  | 0  |                 ++p_header_data;  | 
5823  | 0  |         l_comp->sgnd = (l_comp_def>>7) & 1;  | 
5824  | 0  |                 l_comp->prec = (l_comp_def&0x7f) + 1;  | 
5825  | 0  |                 ++l_comp;  | 
5826  | 0  |         }  | 
5827  |  | 
  | 
5828  | 0  |         return OPJ_TRUE;  | 
5829  | 0  | }  | 
5830  |  |  | 
5831  |  | /* ----------------------------------------------------------------------- */  | 
5832  |  | /* J2K / JPT decoder interface                                             */  | 
5833  |  | /* ----------------------------------------------------------------------- */  | 
5834  |  |  | 
5835  |  | void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)  | 
5836  | 0  | { | 
5837  | 0  |         if(j2k && parameters) { | 
5838  | 0  |                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;  | 
5839  | 0  |                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;  | 
5840  |  | 
  | 
5841  |  | #ifdef USE_JPWL  | 
5842  |  |                 j2k->m_cp.correct = parameters->jpwl_correct;  | 
5843  |  |                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;  | 
5844  |  |                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;  | 
5845  |  | #endif /* USE_JPWL */  | 
5846  | 0  |         }  | 
5847  | 0  | }  | 
5848  |  |  | 
5849  |  | /* ----------------------------------------------------------------------- */  | 
5850  |  | /* J2K encoder interface                                                       */  | 
5851  |  | /* ----------------------------------------------------------------------- */  | 
5852  |  |  | 
5853  |  | opj_j2k_t* opj_j2k_create_compress(void)  | 
5854  | 0  | { | 
5855  | 0  |         opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));  | 
5856  | 0  |         if (!l_j2k) { | 
5857  | 0  |                 return NULL;  | 
5858  | 0  |         }  | 
5859  |  |  | 
5860  | 0  |         memset(l_j2k,0,sizeof(opj_j2k_t));  | 
5861  |  | 
  | 
5862  | 0  |         l_j2k->m_is_decoder = 0;  | 
5863  | 0  |         l_j2k->m_cp.m_is_decoder = 0;  | 
5864  |  | 
  | 
5865  | 0  |         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);  | 
5866  | 0  |         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { | 
5867  | 0  |                 opj_j2k_destroy(l_j2k);  | 
5868  | 0  |                 return NULL;  | 
5869  | 0  |         }  | 
5870  |  |  | 
5871  | 0  |         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;  | 
5872  |  |  | 
5873  |  |         /* validation list creation*/  | 
5874  | 0  |         l_j2k->m_validation_list = opj_procedure_list_create();  | 
5875  | 0  |         if (! l_j2k->m_validation_list) { | 
5876  | 0  |                 opj_j2k_destroy(l_j2k);  | 
5877  | 0  |                 return NULL;  | 
5878  | 0  |         }  | 
5879  |  |  | 
5880  |  |         /* execution list creation*/  | 
5881  | 0  |         l_j2k->m_procedure_list = opj_procedure_list_create();  | 
5882  | 0  |         if (! l_j2k->m_procedure_list) { | 
5883  | 0  |                 opj_j2k_destroy(l_j2k);  | 
5884  | 0  |                 return NULL;  | 
5885  | 0  |         }  | 
5886  |  |  | 
5887  | 0  |         return l_j2k;  | 
5888  | 0  | }  | 
5889  |  |  | 
5890  | 0  | int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){ | 
5891  | 0  |     POC[0].tile  = 1;  | 
5892  | 0  |     POC[0].resno0  = 0;  | 
5893  | 0  |     POC[0].compno0 = 0;  | 
5894  | 0  |     POC[0].layno1  = 1;  | 
5895  | 0  |     POC[0].resno1  = (OPJ_UINT32)(numres-1);  | 
5896  | 0  |     POC[0].compno1 = 3;  | 
5897  | 0  |     POC[0].prg1 = OPJ_CPRL;  | 
5898  | 0  |     POC[1].tile  = 1;  | 
5899  | 0  |     POC[1].resno0  = (OPJ_UINT32)(numres-1);  | 
5900  | 0  |     POC[1].compno0 = 0;  | 
5901  | 0  |     POC[1].layno1  = 1;  | 
5902  | 0  |     POC[1].resno1  = (OPJ_UINT32)numres;  | 
5903  | 0  |     POC[1].compno1 = 3;  | 
5904  | 0  |     POC[1].prg1 = OPJ_CPRL;  | 
5905  | 0  |     return 2;  | 
5906  | 0  | }  | 
5907  |  |  | 
5908  |  | void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)  | 
5909  | 0  | { | 
5910  |  |     /* Configure cinema parameters */  | 
5911  | 0  |     OPJ_FLOAT32 max_rate = 0;  | 
5912  | 0  |     OPJ_FLOAT32 temp_rate = 0;  | 
5913  | 0  |     int i;  | 
5914  |  |  | 
5915  |  |     /* profile (Rsiz) */  | 
5916  | 0  |     switch (parameters->cp_cinema){ | 
5917  | 0  |     case OPJ_CINEMA2K_24:  | 
5918  | 0  |     case OPJ_CINEMA2K_48:  | 
5919  | 0  |         parameters->cp_rsiz = OPJ_CINEMA2K;  | 
5920  | 0  |         break;  | 
5921  | 0  |     case OPJ_CINEMA4K_24:  | 
5922  | 0  |         parameters->cp_rsiz = OPJ_CINEMA4K;  | 
5923  | 0  |         break;  | 
5924  | 0  |     case OPJ_OFF:  | 
5925  | 0  |         assert(0);  | 
5926  | 0  |         break;  | 
5927  | 0  |     }  | 
5928  |  |  | 
5929  |  |     /* No tiling */  | 
5930  | 0  |     parameters->tile_size_on = OPJ_FALSE;  | 
5931  | 0  |     parameters->cp_tdx=1;  | 
5932  | 0  |     parameters->cp_tdy=1;  | 
5933  |  |  | 
5934  |  |     /* One tile part for each component */  | 
5935  | 0  |     parameters->tp_flag = 'C';  | 
5936  | 0  |     parameters->tp_on = 1;  | 
5937  |  |  | 
5938  |  |     /* Tile and Image shall be at (0,0) */  | 
5939  | 0  |     parameters->cp_tx0 = 0;  | 
5940  | 0  |     parameters->cp_ty0 = 0;  | 
5941  | 0  |     parameters->image_offset_x0 = 0;  | 
5942  | 0  |     parameters->image_offset_y0 = 0;  | 
5943  |  |  | 
5944  |  |     /* Codeblock size= 32*32 */  | 
5945  | 0  |     parameters->cblockw_init = 32;  | 
5946  | 0  |     parameters->cblockh_init = 32;  | 
5947  |  |  | 
5948  |  |     /* Codeblock style: no mode switch enabled */  | 
5949  | 0  |     parameters->mode = 0;  | 
5950  |  |  | 
5951  |  |     /* No ROI */  | 
5952  | 0  |     parameters->roi_compno = -1;  | 
5953  |  |  | 
5954  |  |     /* No subsampling */  | 
5955  | 0  |     parameters->subsampling_dx = 1;  | 
5956  | 0  |     parameters->subsampling_dy = 1;  | 
5957  |  |  | 
5958  |  |     /* 9-7 transform */  | 
5959  | 0  |     parameters->irreversible = 1;  | 
5960  |  |  | 
5961  |  |     /* Number of layers */  | 
5962  | 0  |     if (parameters->tcp_numlayers > 1){ | 
5963  | 0  |         opj_event_msg(p_manager, EVT_WARNING,  | 
5964  | 0  |                 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"  | 
5965  | 0  |                 "1 single quality layer"  | 
5966  | 0  |                 "-> Number of layers forced to 1 (rather than %d)\n",  | 
5967  | 0  |                 parameters->tcp_numlayers);  | 
5968  | 0  |         parameters->tcp_numlayers = 1;  | 
5969  | 0  |     }  | 
5970  |  |  | 
5971  |  |     /* Resolution levels */  | 
5972  | 0  |     switch (parameters->cp_cinema){ | 
5973  | 0  |     case OPJ_CINEMA2K_24:  | 
5974  | 0  |     case OPJ_CINEMA2K_48:  | 
5975  | 0  |         if(parameters->numresolution > 6){ | 
5976  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
5977  | 0  |                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"  | 
5978  | 0  |                     "Number of decomposition levels <= 5\n"  | 
5979  | 0  |                     "-> Number of decomposition levels forced to 5 (rather than %d)\n",  | 
5980  | 0  |                     parameters->numresolution+1);  | 
5981  | 0  |             parameters->numresolution = 6;  | 
5982  | 0  |         }  | 
5983  | 0  |         break;  | 
5984  | 0  |     case OPJ_CINEMA4K_24:  | 
5985  | 0  |         if(parameters->numresolution < 2){ | 
5986  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
5987  | 0  |                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"  | 
5988  | 0  |                     "Number of decomposition levels >= 1 && <= 6\n"  | 
5989  | 0  |                     "-> Number of decomposition levels forced to 1 (rather than %d)\n",  | 
5990  | 0  |                     parameters->numresolution+1);  | 
5991  | 0  |             parameters->numresolution = 1;  | 
5992  | 0  |         }else if(parameters->numresolution > 7){ | 
5993  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
5994  | 0  |                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"  | 
5995  | 0  |                     "Number of decomposition levels >= 1 && <= 6\n"  | 
5996  | 0  |                     "-> Number of decomposition levels forced to 6 (rather than %d)\n",  | 
5997  | 0  |                     parameters->numresolution+1);  | 
5998  | 0  |             parameters->numresolution = 7;  | 
5999  | 0  |         }  | 
6000  | 0  |         break;  | 
6001  | 0  |     default :  | 
6002  | 0  |         break;  | 
6003  | 0  |     }  | 
6004  |  |  | 
6005  |  |     /* Precincts */  | 
6006  | 0  |     parameters->csty |= 0x01;  | 
6007  | 0  |     parameters->res_spec = parameters->numresolution-1;  | 
6008  | 0  |     for (i = 0; i<parameters->res_spec; i++) { | 
6009  | 0  |         parameters->prcw_init[i] = 256;  | 
6010  | 0  |         parameters->prch_init[i] = 256;  | 
6011  | 0  |     }  | 
6012  |  |  | 
6013  |  |     /* The progression order shall be CPRL */  | 
6014  | 0  |     parameters->prog_order = OPJ_CPRL;  | 
6015  |  |  | 
6016  |  |     /* Progression order changes for 4K, disallowed for 2K */  | 
6017  | 0  |     if (parameters->cp_cinema == OPJ_CINEMA4K_24) { | 
6018  | 0  |         parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);  | 
6019  | 0  |     } else { | 
6020  | 0  |         parameters->numpocs = 0;  | 
6021  | 0  |     }  | 
6022  |  |  | 
6023  |  |     /* Limited bit-rate */  | 
6024  | 0  |     parameters->cp_disto_alloc = 1;  | 
6025  | 0  |     switch (parameters->cp_cinema){ | 
6026  | 0  |     case OPJ_CINEMA2K_24:  | 
6027  | 0  |     case OPJ_CINEMA4K_24:  | 
6028  | 0  |         max_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/  | 
6029  | 0  |                 (OPJ_FLOAT32)(CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);  | 
6030  | 0  |         if (parameters->tcp_rates[0] == 0){ | 
6031  | 0  |             parameters->tcp_rates[0] = max_rate;  | 
6032  | 0  |         }else{ | 
6033  | 0  |             temp_rate =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/  | 
6034  | 0  |                     (parameters->tcp_rates[0] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy);  | 
6035  | 0  |             if (temp_rate > CINEMA_24_CS ){ | 
6036  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
6037  | 0  |                         "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"  | 
6038  | 0  |                         "Maximum 1302083 compressed bytes @ 24fps\n"  | 
6039  | 0  |                         "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",  | 
6040  | 0  |                         parameters->tcp_rates[0], max_rate);  | 
6041  | 0  |                 parameters->tcp_rates[0]= max_rate;  | 
6042  | 0  |             }else{ | 
6043  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
6044  | 0  |                         "JPEG 2000 Profile-3 and 4 (2k/4k dc profile):\n"  | 
6045  | 0  |                         "INFO : Specified rate (%3.1f) is below the 2k/4k limit @ 24fps.\n",  | 
6046  | 0  |                         parameters->tcp_rates[0]);  | 
6047  | 0  |             }  | 
6048  | 0  |         }  | 
6049  | 0  |         parameters->max_comp_size = COMP_24_CS;  | 
6050  | 0  |         break;  | 
6051  | 0  |     case OPJ_CINEMA2K_48:  | 
6052  | 0  |         max_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/  | 
6053  | 0  |                 (float)(CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);  | 
6054  | 0  |         if (parameters->tcp_rates[0] == 0){ | 
6055  | 0  |             parameters->tcp_rates[0] = max_rate;  | 
6056  | 0  |         }else{ | 
6057  | 0  |             temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/  | 
6058  | 0  |                     (parameters->tcp_rates[0] * 8 * (float)image->comps[0].dx * (float)image->comps[0].dy);  | 
6059  | 0  |             if (temp_rate > CINEMA_48_CS ){ | 
6060  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
6061  | 0  |                         "JPEG 2000 Profile-3 (2k dc profile) requires:\n"  | 
6062  | 0  |                         "Maximum 651041 compressed bytes @ 48fps\n"  | 
6063  | 0  |                         "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",  | 
6064  | 0  |                         parameters->tcp_rates[0], max_rate);  | 
6065  | 0  |                 parameters->tcp_rates[0]= max_rate;  | 
6066  | 0  |             }else{ | 
6067  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
6068  | 0  |                         "JPEG 2000 Profile-3 (2k dc profile):\n"  | 
6069  | 0  |                         "INFO : Specified rate (%3.1f) is below the 2k limit @ 48 fps.\n",  | 
6070  | 0  |                         parameters->tcp_rates[0]);  | 
6071  | 0  |             }  | 
6072  | 0  |         }  | 
6073  | 0  |         parameters->max_comp_size = COMP_48_CS;  | 
6074  | 0  |         break;  | 
6075  | 0  |     default:  | 
6076  | 0  |         break;  | 
6077  | 0  |     }  | 
6078  | 0  | }  | 
6079  |  |  | 
6080  |  | OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager)  | 
6081  | 0  | { | 
6082  | 0  |     OPJ_UINT32 i;  | 
6083  |  |  | 
6084  |  |     /* Number of components */  | 
6085  | 0  |     if (image->numcomps != 3){ | 
6086  | 0  |         opj_event_msg(p_manager, EVT_WARNING,  | 
6087  | 0  |                 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"  | 
6088  | 0  |                 "3 components"  | 
6089  | 0  |                 "-> Number of components of input image (%d) is not compliant\n"  | 
6090  | 0  |                 "-> Non-profile-3 codestream will be generated\n",  | 
6091  | 0  |                 image->numcomps);  | 
6092  | 0  |         return OPJ_FALSE;  | 
6093  | 0  |     }  | 
6094  |  |  | 
6095  |  |     /* Bitdepth */  | 
6096  | 0  |     for (i = 0; i < image->numcomps; i++) { | 
6097  | 0  |         if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){ | 
6098  | 0  |             char signed_str[] = "signed";  | 
6099  | 0  |             char unsigned_str[] = "unsigned";  | 
6100  | 0  |             char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str;  | 
6101  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
6102  | 0  |                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"  | 
6103  | 0  |                     "Precision of each component shall be 12 bits unsigned"  | 
6104  | 0  |                     "-> At least component %d of input image (%d bits, %s) is not compliant\n"  | 
6105  | 0  |                     "-> Non-profile-3 codestream will be generated\n",  | 
6106  | 0  |                     i,image->comps[i].bpp, tmp_str);  | 
6107  | 0  |             return OPJ_FALSE;  | 
6108  | 0  |         }  | 
6109  | 0  |     }  | 
6110  |  |  | 
6111  |  |     /* Image size */  | 
6112  | 0  |     switch (cinema_mode){ | 
6113  | 0  |     case OPJ_CINEMA2K_24:  | 
6114  | 0  |     case OPJ_CINEMA2K_48:  | 
6115  | 0  |         if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){ | 
6116  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
6117  | 0  |                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"  | 
6118  | 0  |                     "width <= 2048 and height <= 1080\n"  | 
6119  | 0  |                     "-> Input image size %d x %d is not compliant\n"  | 
6120  | 0  |                     "-> Non-profile-3 codestream will be generated\n",  | 
6121  | 0  |                     image->comps[0].w,image->comps[0].h);  | 
6122  | 0  |             return OPJ_FALSE;  | 
6123  | 0  |         }  | 
6124  | 0  |         break;  | 
6125  | 0  |     case OPJ_CINEMA4K_24:  | 
6126  | 0  |         if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){ | 
6127  | 0  |             opj_event_msg(p_manager, EVT_WARNING,  | 
6128  | 0  |                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"  | 
6129  | 0  |                     "width <= 4096 and height <= 2160\n"  | 
6130  | 0  |                     "-> Image size %d x %d is not compliant\n"  | 
6131  | 0  |                     "-> Non-profile-4 codestream will be generated\n",  | 
6132  | 0  |                     image->comps[0].w,image->comps[0].h);  | 
6133  | 0  |             return OPJ_FALSE;  | 
6134  | 0  |         }  | 
6135  | 0  |         break;  | 
6136  | 0  |     default :  | 
6137  | 0  |         break;  | 
6138  | 0  |     }  | 
6139  |  |  | 
6140  | 0  |     return OPJ_TRUE;  | 
6141  | 0  | }  | 
6142  |  |  | 
6143  |  | void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,  | 
6144  |  |                                                     opj_cparameters_t *parameters,  | 
6145  |  |                                                     opj_image_t *image,  | 
6146  |  |                                                     opj_event_mgr_t * p_manager)  | 
6147  | 0  | { | 
6148  | 0  |         OPJ_UINT32 i, j, tileno, numpocs_tile;  | 
6149  | 0  |         opj_cp_t *cp = 00;  | 
6150  |  | 
  | 
6151  | 0  |         if(!p_j2k || !parameters || ! image) { | 
6152  | 0  |                 return;  | 
6153  | 0  |         }  | 
6154  |  |  | 
6155  |  |         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */  | 
6156  | 0  |         cp = &(p_j2k->m_cp);  | 
6157  |  |  | 
6158  |  |         /* set default values for cp */  | 
6159  | 0  |         cp->tw = 1;  | 
6160  | 0  |         cp->th = 1;  | 
6161  |  |  | 
6162  |  |         /* set cinema parameters if required */  | 
6163  | 0  |         if (parameters->cp_cinema){ | 
6164  | 0  |             opj_j2k_set_cinema_parameters(parameters,image,p_manager);  | 
6165  | 0  |             if (!opj_j2k_is_cinema_compliant(image,parameters->cp_cinema,p_manager)) { | 
6166  | 0  |                 parameters->cp_rsiz = OPJ_STD_RSIZ;  | 
6167  | 0  |             }  | 
6168  | 0  |         }  | 
6169  |  |  | 
6170  |  |         /*  | 
6171  |  |         copy user encoding parameters  | 
6172  |  |         */  | 
6173  | 0  |         cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;  | 
6174  | 0  |         cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size;  | 
6175  | 0  |         cp->rsiz   = parameters->cp_rsiz;  | 
6176  | 0  |         cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u;  | 
6177  | 0  |         cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u;  | 
6178  | 0  |         cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u;  | 
6179  |  |  | 
6180  |  |         /* mod fixed_quality */  | 
6181  | 0  |         if (parameters->cp_fixed_alloc && parameters->cp_matrice) { | 
6182  | 0  |                 size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);  | 
6183  | 0  |                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);  | 
6184  | 0  |                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);  | 
6185  | 0  |         }  | 
6186  |  |  | 
6187  |  |         /* tiles */  | 
6188  | 0  |         cp->tdx = (OPJ_UINT32)parameters->cp_tdx;  | 
6189  | 0  |         cp->tdy = (OPJ_UINT32)parameters->cp_tdy;  | 
6190  |  |  | 
6191  |  |         /* tile offset */  | 
6192  | 0  |         cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;  | 
6193  | 0  |         cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;  | 
6194  |  |  | 
6195  |  |         /* comment string */  | 
6196  | 0  |         if(parameters->cp_comment) { | 
6197  | 0  |                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);  | 
6198  | 0  |                 if(cp->comment) { | 
6199  | 0  |                         strcpy(cp->comment, parameters->cp_comment);  | 
6200  | 0  |                 }  | 
6201  | 0  |         }  | 
6202  |  |  | 
6203  |  |         /*  | 
6204  |  |         calculate other encoding parameters  | 
6205  |  |         */  | 
6206  |  | 
  | 
6207  | 0  |         if (parameters->tile_size_on) { | 
6208  | 0  |                 cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), (OPJ_INT32)cp->tdx);  | 
6209  | 0  |                 cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), (OPJ_INT32)cp->tdy);  | 
6210  | 0  |         } else { | 
6211  | 0  |                 cp->tdx = image->x1 - cp->tx0;  | 
6212  | 0  |                 cp->tdy = image->y1 - cp->ty0;  | 
6213  | 0  |         }  | 
6214  |  | 
  | 
6215  | 0  |         if (parameters->tp_on) { | 
6216  | 0  |                 cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;  | 
6217  | 0  |                 cp->m_specific_param.m_enc.m_tp_on = 1;  | 
6218  | 0  |         }  | 
6219  |  | 
  | 
6220  |  | #ifdef USE_JPWL  | 
6221  |  |         /*  | 
6222  |  |         calculate JPWL encoding parameters  | 
6223  |  |         */  | 
6224  |  |  | 
6225  |  |         if (parameters->jpwl_epc_on) { | 
6226  |  |                 OPJ_INT32 i;  | 
6227  |  |  | 
6228  |  |                 /* set JPWL on */  | 
6229  |  |                 cp->epc_on = OPJ_TRUE;  | 
6230  |  |                 cp->info_on = OPJ_FALSE; /* no informative technique */  | 
6231  |  |  | 
6232  |  |                 /* set EPB on */  | 
6233  |  |                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { | 
6234  |  |                         cp->epb_on = OPJ_TRUE;  | 
6235  |  |  | 
6236  |  |                         cp->hprot_MH = parameters->jpwl_hprot_MH;  | 
6237  |  |                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | 
6238  |  |                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];  | 
6239  |  |                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];  | 
6240  |  |                         }  | 
6241  |  |                         /* if tile specs are not specified, copy MH specs */  | 
6242  |  |                         if (cp->hprot_TPH[0] == -1) { | 
6243  |  |                                 cp->hprot_TPH_tileno[0] = 0;  | 
6244  |  |                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;  | 
6245  |  |                         }  | 
6246  |  |                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { | 
6247  |  |                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];  | 
6248  |  |                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];  | 
6249  |  |                                 cp->pprot[i] = parameters->jpwl_pprot[i];  | 
6250  |  |                         }  | 
6251  |  |                 }  | 
6252  |  |  | 
6253  |  |                 /* set ESD writing */  | 
6254  |  |                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { | 
6255  |  |                         cp->esd_on = OPJ_TRUE;  | 
6256  |  |  | 
6257  |  |                         cp->sens_size = parameters->jpwl_sens_size;  | 
6258  |  |                         cp->sens_addr = parameters->jpwl_sens_addr;  | 
6259  |  |                         cp->sens_range = parameters->jpwl_sens_range;  | 
6260  |  |  | 
6261  |  |                         cp->sens_MH = parameters->jpwl_sens_MH;  | 
6262  |  |                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | 
6263  |  |                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];  | 
6264  |  |                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];  | 
6265  |  |                         }  | 
6266  |  |                 }  | 
6267  |  |  | 
6268  |  |                 /* always set RED writing to false: we are at the encoder */  | 
6269  |  |                 cp->red_on = OPJ_FALSE;  | 
6270  |  |  | 
6271  |  |         } else { | 
6272  |  |                 cp->epc_on = OPJ_FALSE;  | 
6273  |  |         }  | 
6274  |  | #endif /* USE_JPWL */  | 
6275  |  |  | 
6276  |  |         /* initialize the mutiple tiles */  | 
6277  |  |         /* ---------------------------- */  | 
6278  | 0  |         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));  | 
6279  | 0  |         if (parameters->numpocs) { | 
6280  |  |                 /* initialisation of POC */  | 
6281  | 0  |                 opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager);  | 
6282  |  |                 /* TODO MSD use the return value*/  | 
6283  | 0  |         }  | 
6284  |  | 
  | 
6285  | 0  |         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | 
6286  | 0  |                 opj_tcp_t *tcp = &cp->tcps[tileno];  | 
6287  | 0  |                 tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;  | 
6288  |  | 
  | 
6289  | 0  |                 for (j = 0; j < tcp->numlayers; j++) { | 
6290  | 0  |                         if(cp->m_specific_param.m_enc.m_cinema){ | 
6291  | 0  |                                 if (cp->m_specific_param.m_enc.m_fixed_quality) { | 
6292  | 0  |                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];  | 
6293  | 0  |                                 }  | 
6294  | 0  |                                 tcp->rates[j] = parameters->tcp_rates[j];  | 
6295  | 0  |                         }else{ | 
6296  | 0  |                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */ | 
6297  | 0  |                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];  | 
6298  | 0  |                                 } else { | 
6299  | 0  |                                         tcp->rates[j] = parameters->tcp_rates[j];  | 
6300  | 0  |                                 }  | 
6301  | 0  |                         }  | 
6302  | 0  |                 }  | 
6303  |  | 
  | 
6304  | 0  |                 tcp->csty = (OPJ_UINT32)parameters->csty;  | 
6305  | 0  |                 tcp->prg = parameters->prog_order;  | 
6306  | 0  |                 tcp->mct = (OPJ_UINT32)parameters->tcp_mct;  | 
6307  |  | 
  | 
6308  | 0  |                 numpocs_tile = 0;  | 
6309  | 0  |                 tcp->POC = 0;  | 
6310  |  | 
  | 
6311  | 0  |                 if (parameters->numpocs) { | 
6312  |  |                         /* initialisation of POC */  | 
6313  | 0  |                         tcp->POC = 1;  | 
6314  | 0  |                         for (i = 0; i < parameters->numpocs; i++) { | 
6315  | 0  |                                 if (tileno + 1 == parameters->POC[i].tile )  { | 
6316  | 0  |                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];  | 
6317  |  | 
  | 
6318  | 0  |                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;  | 
6319  | 0  |                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;  | 
6320  | 0  |                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;  | 
6321  | 0  |                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;  | 
6322  | 0  |                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;  | 
6323  | 0  |                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;  | 
6324  | 0  |                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;  | 
6325  |  | 
  | 
6326  | 0  |                                         numpocs_tile++;  | 
6327  | 0  |                                 }  | 
6328  | 0  |                         }  | 
6329  |  | 
  | 
6330  | 0  |                         tcp->numpocs = numpocs_tile -1 ;  | 
6331  | 0  |                 }else{ | 
6332  | 0  |                         tcp->numpocs = 0;  | 
6333  | 0  |                 }  | 
6334  |  | 
  | 
6335  | 0  |                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));  | 
6336  |  | 
  | 
6337  | 0  |                 if (parameters->mct_data) { | 
6338  |  |                         | 
6339  | 0  |                     OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);  | 
6340  | 0  |                     OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);  | 
6341  | 0  |                     OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);  | 
6342  |  | 
  | 
6343  | 0  |                     tcp->mct = 2;  | 
6344  | 0  |                     tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);  | 
6345  | 0  |                     memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);  | 
6346  | 0  |                     memcpy(lTmpBuf,parameters->mct_data,lMctSize);  | 
6347  |  | 
  | 
6348  | 0  |                     tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);  | 
6349  | 0  |                     assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));  | 
6350  |  | 
  | 
6351  | 0  |                     tcp->mct_norms = (OPJ_FLOAT64*)  | 
6352  | 0  |                                     opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));  | 
6353  |  | 
  | 
6354  | 0  |                     opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);  | 
6355  | 0  |                     opj_free(lTmpBuf);  | 
6356  |  | 
  | 
6357  | 0  |                     for (i = 0; i < image->numcomps; i++) { | 
6358  | 0  |                             opj_tccp_t *tccp = &tcp->tccps[i];  | 
6359  | 0  |                             tccp->m_dc_level_shift = l_dc_shift[i];  | 
6360  | 0  |                     }  | 
6361  |  | 
  | 
6362  | 0  |                     opj_j2k_setup_mct_encoding(tcp,image);                          | 
6363  | 0  |                 }  | 
6364  | 0  |                 else { | 
6365  | 0  |                         for (i = 0; i < image->numcomps; i++) { | 
6366  | 0  |                                 opj_tccp_t *tccp = &tcp->tccps[i];  | 
6367  | 0  |                                 opj_image_comp_t * l_comp = &(image->comps[i]);  | 
6368  |  | 
  | 
6369  | 0  |                                 if (! l_comp->sgnd) { | 
6370  | 0  |                                         tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);  | 
6371  | 0  |                                 }  | 
6372  | 0  |                         }  | 
6373  | 0  |                 }  | 
6374  |  | 
  | 
6375  | 0  |                 for (i = 0; i < image->numcomps; i++) { | 
6376  | 0  |                         opj_tccp_t *tccp = &tcp->tccps[i];  | 
6377  |  | 
  | 
6378  | 0  |                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */  | 
6379  | 0  |                         tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;  | 
6380  | 0  |                         tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);  | 
6381  | 0  |                         tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);  | 
6382  | 0  |                         tccp->cblksty = (OPJ_UINT32)parameters->mode;  | 
6383  | 0  |                         tccp->qmfbid = parameters->irreversible ? 0 : 1;  | 
6384  | 0  |                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;  | 
6385  | 0  |                         tccp->numgbits = 2;  | 
6386  |  | 
  | 
6387  | 0  |                         if ((OPJ_INT32)i == parameters->roi_compno) { | 
6388  | 0  |                                 tccp->roishift = parameters->roi_shift;  | 
6389  | 0  |                         } else { | 
6390  | 0  |                                 tccp->roishift = 0;  | 
6391  | 0  |                         }  | 
6392  |  | 
  | 
6393  | 0  |                                 if (parameters->csty & J2K_CCP_CSTY_PRT) { | 
6394  | 0  |                                         OPJ_INT32 p = 0, it_res;  | 
6395  | 0  |                                         assert( tccp->numresolutions > 0 );  | 
6396  | 0  |                                         for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { | 
6397  | 0  |                                                 if (p < parameters->res_spec) { | 
6398  |  | 
  | 
6399  | 0  |                                                         if (parameters->prcw_init[p] < 1) { | 
6400  | 0  |                                                                 tccp->prcw[it_res] = 1;  | 
6401  | 0  |                                                         } else { | 
6402  | 0  |                                                                 tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);  | 
6403  | 0  |                                                         }  | 
6404  |  | 
  | 
6405  | 0  |                                                         if (parameters->prch_init[p] < 1) { | 
6406  | 0  |                                                                 tccp->prch[it_res] = 1;  | 
6407  | 0  |                                                         }else { | 
6408  | 0  |                                                                 tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);  | 
6409  | 0  |                                                         }  | 
6410  |  | 
  | 
6411  | 0  |                                                 } else { | 
6412  | 0  |                                                         OPJ_INT32 res_spec = parameters->res_spec;  | 
6413  | 0  |                                                         OPJ_INT32 size_prcw = 0;  | 
6414  | 0  |                                                         OPJ_INT32 size_prch = 0;  | 
6415  |  | 
  | 
6416  | 0  |                                                         assert(res_spec>0); /* issue 189 */  | 
6417  | 0  |                                                         size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));  | 
6418  | 0  |                                                         size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));  | 
6419  |  |  | 
6420  |  | 
  | 
6421  | 0  |                                                         if (size_prcw < 1) { | 
6422  | 0  |                                                                 tccp->prcw[it_res] = 1;  | 
6423  | 0  |                                                         } else { | 
6424  | 0  |                                                                 tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);  | 
6425  | 0  |                                                         }  | 
6426  |  | 
  | 
6427  | 0  |                                                         if (size_prch < 1) { | 
6428  | 0  |                                                                 tccp->prch[it_res] = 1;  | 
6429  | 0  |                                                         } else { | 
6430  | 0  |                                                                 tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);  | 
6431  | 0  |                                                         }  | 
6432  | 0  |                                                 }  | 
6433  | 0  |                                                 p++;  | 
6434  |  |                                                 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ | 
6435  | 0  |                                         }       /*end for*/  | 
6436  | 0  |                                 } else { | 
6437  | 0  |                                         for (j = 0; j < tccp->numresolutions; j++) { | 
6438  | 0  |                                                 tccp->prcw[j] = 15;  | 
6439  | 0  |                                                 tccp->prch[j] = 15;  | 
6440  | 0  |                                         }  | 
6441  | 0  |                                 }  | 
6442  |  | 
  | 
6443  | 0  |                         opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);  | 
6444  | 0  |                 }  | 
6445  | 0  |         }  | 
6446  |  | 
  | 
6447  | 0  |         if (parameters->mct_data) { | 
6448  | 0  |                 opj_free(parameters->mct_data);  | 
6449  | 0  |                 parameters->mct_data = 00;  | 
6450  | 0  |         }  | 
6451  | 0  | }  | 
6452  |  |  | 
6453  |  | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)  | 
6454  | 0  | { | 
6455  | 0  |         assert(cstr_index != 00);  | 
6456  |  |  | 
6457  |  |         /* expand the list? */  | 
6458  | 0  |         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { | 
6459  | 0  |                 opj_marker_info_t *new_marker;  | 
6460  | 0  |                 cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum);  | 
6461  | 0  |                 new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));  | 
6462  | 0  |                 if (! new_marker) { | 
6463  | 0  |                         opj_free(cstr_index->marker);  | 
6464  | 0  |                         cstr_index->marker = NULL;  | 
6465  | 0  |                         cstr_index->maxmarknum = 0;  | 
6466  | 0  |                         cstr_index->marknum = 0;  | 
6467  |  |                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */  | 
6468  | 0  |                         return OPJ_FALSE;  | 
6469  | 0  |                 }  | 
6470  | 0  |                 cstr_index->marker = new_marker;  | 
6471  | 0  |         }  | 
6472  |  |  | 
6473  |  |         /* add the marker */  | 
6474  | 0  |         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;  | 
6475  | 0  |         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;  | 
6476  | 0  |         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;  | 
6477  | 0  |         cstr_index->marknum++;  | 
6478  | 0  |         return OPJ_TRUE;  | 
6479  | 0  | }  | 
6480  |  |  | 
6481  |  | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)  | 
6482  | 0  | { | 
6483  | 0  |         assert(cstr_index != 00);  | 
6484  | 0  |         assert(cstr_index->tile_index != 00);  | 
6485  |  |  | 
6486  |  |         /* expand the list? */  | 
6487  | 0  |         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) { | 
6488  | 0  |                 opj_marker_info_t *new_marker;  | 
6489  | 0  |                 cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);  | 
6490  | 0  |                 new_marker = (opj_marker_info_t *) opj_realloc(  | 
6491  | 0  |                                 cstr_index->tile_index[tileno].marker,  | 
6492  | 0  |                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));  | 
6493  | 0  |                 if (! new_marker) { | 
6494  | 0  |                         opj_free(cstr_index->tile_index[tileno].marker);  | 
6495  | 0  |                         cstr_index->tile_index[tileno].marker = NULL;  | 
6496  | 0  |                         cstr_index->tile_index[tileno].maxmarknum = 0;  | 
6497  | 0  |                         cstr_index->tile_index[tileno].marknum = 0;  | 
6498  |  |                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */  | 
6499  | 0  |                         return OPJ_FALSE;  | 
6500  | 0  |                 }  | 
6501  | 0  |                 cstr_index->tile_index[tileno].marker = new_marker;  | 
6502  | 0  |         }  | 
6503  |  |  | 
6504  |  |         /* add the marker */  | 
6505  | 0  |         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;  | 
6506  | 0  |         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;  | 
6507  | 0  |         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;  | 
6508  | 0  |         cstr_index->tile_index[tileno].marknum++;  | 
6509  |  | 
  | 
6510  | 0  |         if (type == J2K_MS_SOT) { | 
6511  | 0  |                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;  | 
6512  |  | 
  | 
6513  | 0  |                 if (cstr_index->tile_index[tileno].tp_index)  | 
6514  | 0  |                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;  | 
6515  |  | 
  | 
6516  | 0  |         }  | 
6517  | 0  |         return OPJ_TRUE;  | 
6518  | 0  | }  | 
6519  |  |  | 
6520  |  | /*  | 
6521  |  |  * -----------------------------------------------------------------------  | 
6522  |  |  * -----------------------------------------------------------------------  | 
6523  |  |  * -----------------------------------------------------------------------  | 
6524  |  |  */  | 
6525  |  |  | 
6526  |  | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,  | 
6527  |  |                                 opj_stream_private_t *p_stream,  | 
6528  |  |                                 opj_event_mgr_t * p_manager  | 
6529  |  |                                 )  | 
6530  | 0  | { | 
6531  | 0  |     (void)p_j2k;  | 
6532  | 0  |     (void)p_stream;  | 
6533  | 0  |     (void)p_manager;  | 
6534  | 0  |     return OPJ_TRUE;  | 
6535  | 0  | }  | 
6536  |  |  | 
6537  |  | OPJ_BOOL opj_j2k_read_header(   opj_stream_private_t *p_stream,  | 
6538  |  |                                                             opj_j2k_t* p_j2k,  | 
6539  |  |                                                             opj_image_t** p_image,  | 
6540  |  |                                                             opj_event_mgr_t* p_manager )  | 
6541  | 0  | { | 
6542  |  |         /* preconditions */  | 
6543  | 0  |         assert(p_j2k != 00);  | 
6544  | 0  |         assert(p_stream != 00);  | 
6545  | 0  |         assert(p_manager != 00);  | 
6546  |  |  | 
6547  |  |         /* create an empty image header */  | 
6548  | 0  |         p_j2k->m_private_image = opj_image_create0();  | 
6549  | 0  |         if (! p_j2k->m_private_image) { | 
6550  | 0  |                 return OPJ_FALSE;  | 
6551  | 0  |         }  | 
6552  |  |  | 
6553  |  |         /* customization of the validation */  | 
6554  | 0  |         opj_j2k_setup_decoding_validation(p_j2k);  | 
6555  |  |  | 
6556  |  |         /* validation of the parameters codec */  | 
6557  | 0  |         if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) { | 
6558  | 0  |                 opj_image_destroy(p_j2k->m_private_image);  | 
6559  | 0  |                 p_j2k->m_private_image = NULL;  | 
6560  | 0  |                 return OPJ_FALSE;  | 
6561  | 0  |         }  | 
6562  |  |  | 
6563  |  |         /* customization of the encoding */  | 
6564  | 0  |         opj_j2k_setup_header_reading(p_j2k);  | 
6565  |  |  | 
6566  |  |         /* read header */  | 
6567  | 0  |         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { | 
6568  | 0  |                 opj_image_destroy(p_j2k->m_private_image);  | 
6569  | 0  |                 p_j2k->m_private_image = NULL;  | 
6570  | 0  |                 return OPJ_FALSE;  | 
6571  | 0  |         }  | 
6572  |  |  | 
6573  | 0  |         *p_image = opj_image_create0();  | 
6574  | 0  |         if (! (*p_image)) { | 
6575  | 0  |                 return OPJ_FALSE;  | 
6576  | 0  |         }  | 
6577  |  |  | 
6578  |  |         /* Copy codestream image information to the output image */  | 
6579  | 0  |         opj_copy_image_header(p_j2k->m_private_image, *p_image);  | 
6580  |  |  | 
6581  |  |     /*Allocate and initialize some elements of codestrem index*/  | 
6582  | 0  |         if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ | 
6583  | 0  |                 return OPJ_FALSE;  | 
6584  | 0  |         }  | 
6585  |  |  | 
6586  | 0  |         return OPJ_TRUE;  | 
6587  | 0  | }  | 
6588  |  |  | 
6589  |  | void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k)  | 
6590  | 0  | { | 
6591  |  |         /* preconditions*/  | 
6592  | 0  |         assert(p_j2k != 00);  | 
6593  |  | 
  | 
6594  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);  | 
6595  |  |  | 
6596  |  |         /* DEVELOPER CORNER, add your custom procedures */  | 
6597  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);  | 
6598  |  | 
  | 
6599  | 0  | }  | 
6600  |  |  | 
6601  |  | void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k)  | 
6602  | 0  | { | 
6603  |  |         /* preconditions*/  | 
6604  | 0  |         assert(p_j2k != 00);  | 
6605  |  | 
  | 
6606  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);  | 
6607  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);  | 
6608  |  |         /* DEVELOPER CORNER, add your custom validation procedure */  | 
6609  |  | 
  | 
6610  | 0  | }  | 
6611  |  |  | 
6612  |  | OPJ_BOOL opj_j2k_mct_validation (       opj_j2k_t * p_j2k,  | 
6613  |  |                                                                 opj_stream_private_t *p_stream,  | 
6614  |  |                                                                 opj_event_mgr_t * p_manager )  | 
6615  | 0  | { | 
6616  | 0  |         OPJ_BOOL l_is_valid = OPJ_TRUE;  | 
6617  | 0  |         OPJ_UINT32 i,j;  | 
6618  |  |  | 
6619  |  |         /* preconditions */  | 
6620  | 0  |         assert(p_j2k != 00);  | 
6621  | 0  |         assert(p_stream != 00);  | 
6622  | 0  |         assert(p_manager != 00);  | 
6623  |  | 
  | 
6624  | 0  |         if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { | 
6625  | 0  |                 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
6626  | 0  |                 opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;  | 
6627  |  | 
  | 
6628  | 0  |                 for (i=0;i<l_nb_tiles;++i) { | 
6629  | 0  |                         if (l_tcp->mct == 2) { | 
6630  | 0  |                                 opj_tccp_t * l_tccp = l_tcp->tccps;  | 
6631  | 0  |                                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);  | 
6632  |  | 
  | 
6633  | 0  |                                 for (j=0;j<p_j2k->m_private_image->numcomps;++j) { | 
6634  | 0  |                                         l_is_valid &= ! (l_tccp->qmfbid & 1);  | 
6635  | 0  |                                         ++l_tccp;  | 
6636  | 0  |                                 }  | 
6637  | 0  |                         }  | 
6638  | 0  |                         ++l_tcp;  | 
6639  | 0  |                 }  | 
6640  | 0  |         }  | 
6641  |  | 
  | 
6642  | 0  |         return l_is_valid;  | 
6643  | 0  | }  | 
6644  |  |  | 
6645  |  | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)  | 
6646  | 0  | { | 
6647  | 0  |         OPJ_UINT32 i;  | 
6648  | 0  |         OPJ_UINT32 l_indix = 1;  | 
6649  | 0  |         opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;  | 
6650  | 0  |         opj_simple_mcc_decorrelation_data_t * l_mcc_data;  | 
6651  | 0  |         OPJ_UINT32 l_mct_size,l_nb_elem;  | 
6652  | 0  |         OPJ_FLOAT32 * l_data, * l_current_data;  | 
6653  | 0  |         opj_tccp_t * l_tccp;  | 
6654  |  |  | 
6655  |  |         /* preconditions */  | 
6656  | 0  |         assert(p_tcp != 00);  | 
6657  |  | 
  | 
6658  | 0  |         if (p_tcp->mct != 2) { | 
6659  | 0  |                 return OPJ_TRUE;  | 
6660  | 0  |         }  | 
6661  |  |  | 
6662  | 0  |         if (p_tcp->m_mct_decoding_matrix) { | 
6663  | 0  |                 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { | 
6664  | 0  |                         opj_mct_data_t *new_mct_records;  | 
6665  | 0  |                         p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;  | 
6666  |  | 
  | 
6667  | 0  |                         new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));  | 
6668  | 0  |                         if (! new_mct_records) { | 
6669  | 0  |                                 opj_free(p_tcp->m_mct_records);  | 
6670  | 0  |                                 p_tcp->m_mct_records = NULL;  | 
6671  | 0  |                                 p_tcp->m_nb_max_mct_records = 0;  | 
6672  | 0  |                                 p_tcp->m_nb_mct_records = 0;  | 
6673  |  |                                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */  | 
6674  | 0  |                                 return OPJ_FALSE;  | 
6675  | 0  |                         }  | 
6676  | 0  |                         p_tcp->m_mct_records = new_mct_records;  | 
6677  | 0  |                         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;  | 
6678  |  | 
  | 
6679  | 0  |                         memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));  | 
6680  | 0  |                 }  | 
6681  | 0  |                 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;  | 
6682  |  | 
  | 
6683  | 0  |                 if (l_mct_deco_data->m_data) { | 
6684  | 0  |                         opj_free(l_mct_deco_data->m_data);  | 
6685  | 0  |                         l_mct_deco_data->m_data = 00;  | 
6686  | 0  |                 }  | 
6687  |  | 
  | 
6688  | 0  |                 l_mct_deco_data->m_index = l_indix++;  | 
6689  | 0  |                 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;  | 
6690  | 0  |                 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;  | 
6691  | 0  |                 l_nb_elem = p_image->numcomps * p_image->numcomps;  | 
6692  | 0  |                 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];  | 
6693  | 0  |                 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );  | 
6694  |  | 
  | 
6695  | 0  |                 if (! l_mct_deco_data->m_data) { | 
6696  | 0  |                         return OPJ_FALSE;  | 
6697  | 0  |                 }  | 
6698  |  |  | 
6699  | 0  |                 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);  | 
6700  |  | 
  | 
6701  | 0  |                 l_mct_deco_data->m_data_size = l_mct_size;  | 
6702  | 0  |                 ++p_tcp->m_nb_mct_records;  | 
6703  | 0  |         }  | 
6704  |  |  | 
6705  | 0  |         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { | 
6706  | 0  |                 opj_mct_data_t *new_mct_records;  | 
6707  | 0  |                 p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;  | 
6708  | 0  |                 new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));  | 
6709  | 0  |                 if (! new_mct_records) { | 
6710  | 0  |                         opj_free(p_tcp->m_mct_records);  | 
6711  | 0  |                         p_tcp->m_mct_records = NULL;  | 
6712  | 0  |                         p_tcp->m_nb_max_mct_records = 0;  | 
6713  | 0  |                         p_tcp->m_nb_mct_records = 0;  | 
6714  |  |                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */  | 
6715  | 0  |                         return OPJ_FALSE;  | 
6716  | 0  |                 }  | 
6717  | 0  |                 p_tcp->m_mct_records = new_mct_records;  | 
6718  | 0  |                 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;  | 
6719  |  | 
  | 
6720  | 0  |                 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));  | 
6721  |  | 
  | 
6722  | 0  |                 if (l_mct_deco_data) { | 
6723  | 0  |                         l_mct_deco_data = l_mct_offset_data - 1;  | 
6724  | 0  |                 }  | 
6725  | 0  |         }  | 
6726  |  |  | 
6727  | 0  |         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;  | 
6728  |  | 
  | 
6729  | 0  |         if (l_mct_offset_data->m_data) { | 
6730  | 0  |                 opj_free(l_mct_offset_data->m_data);  | 
6731  | 0  |                 l_mct_offset_data->m_data = 00;  | 
6732  | 0  |         }  | 
6733  |  | 
  | 
6734  | 0  |         l_mct_offset_data->m_index = l_indix++;  | 
6735  | 0  |         l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;  | 
6736  | 0  |         l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;  | 
6737  | 0  |         l_nb_elem = p_image->numcomps;  | 
6738  | 0  |         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];  | 
6739  | 0  |         l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );  | 
6740  |  | 
  | 
6741  | 0  |         if (! l_mct_offset_data->m_data) { | 
6742  | 0  |                 return OPJ_FALSE;  | 
6743  | 0  |         }  | 
6744  |  |  | 
6745  | 0  |         l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));  | 
6746  | 0  |         if (! l_data) { | 
6747  | 0  |                 opj_free(l_mct_offset_data->m_data);  | 
6748  | 0  |                 l_mct_offset_data->m_data = 00;  | 
6749  | 0  |                 return OPJ_FALSE;  | 
6750  | 0  |         }  | 
6751  |  |  | 
6752  | 0  |         l_tccp = p_tcp->tccps;  | 
6753  | 0  |         l_current_data = l_data;  | 
6754  |  | 
  | 
6755  | 0  |         for (i=0;i<l_nb_elem;++i) { | 
6756  | 0  |                 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);  | 
6757  | 0  |                 ++l_tccp;  | 
6758  | 0  |         }  | 
6759  |  | 
  | 
6760  | 0  |         j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);  | 
6761  |  | 
  | 
6762  | 0  |         opj_free(l_data);  | 
6763  |  | 
  | 
6764  | 0  |         l_mct_offset_data->m_data_size = l_mct_size;  | 
6765  |  | 
  | 
6766  | 0  |         ++p_tcp->m_nb_mct_records;  | 
6767  |  | 
  | 
6768  | 0  |         if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { | 
6769  | 0  |                 opj_simple_mcc_decorrelation_data_t *new_mcc_records;  | 
6770  | 0  |                 p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;  | 
6771  | 0  |                 new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(  | 
6772  | 0  |                                 p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
6773  | 0  |                 if (! new_mcc_records) { | 
6774  | 0  |                         opj_free(p_tcp->m_mcc_records);  | 
6775  | 0  |                         p_tcp->m_mcc_records = NULL;  | 
6776  | 0  |                         p_tcp->m_nb_max_mcc_records = 0;  | 
6777  | 0  |                         p_tcp->m_nb_mcc_records = 0;  | 
6778  |  |                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */  | 
6779  | 0  |                         return OPJ_FALSE;  | 
6780  | 0  |                 }  | 
6781  | 0  |                 p_tcp->m_mcc_records = new_mcc_records;  | 
6782  | 0  |                 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;  | 
6783  | 0  |                 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));  | 
6784  |  | 
  | 
6785  | 0  |         }  | 
6786  |  |  | 
6787  | 0  |         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;  | 
6788  | 0  |         l_mcc_data->m_decorrelation_array = l_mct_deco_data;  | 
6789  | 0  |         l_mcc_data->m_is_irreversible = 1;  | 
6790  | 0  |         l_mcc_data->m_nb_comps = p_image->numcomps;  | 
6791  | 0  |         l_mcc_data->m_index = l_indix++;  | 
6792  | 0  |         l_mcc_data->m_offset_array = l_mct_offset_data;  | 
6793  | 0  |         ++p_tcp->m_nb_mcc_records;  | 
6794  |  | 
  | 
6795  | 0  |         return OPJ_TRUE;  | 
6796  | 0  | }  | 
6797  |  |  | 
6798  |  | OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k,  | 
6799  |  |                                                             opj_stream_private_t *p_stream,  | 
6800  |  |                                                             opj_event_mgr_t * p_manager )  | 
6801  | 0  | { | 
6802  |  |         /* add here initialization of cp  | 
6803  |  |            copy paste of setup_decoder */  | 
6804  | 0  |   (void)p_j2k;  | 
6805  | 0  |   (void)p_stream;  | 
6806  | 0  |   (void)p_manager;  | 
6807  | 0  |         return OPJ_TRUE;  | 
6808  | 0  | }  | 
6809  |  |  | 
6810  |  | OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k,  | 
6811  |  |                                                         opj_stream_private_t *p_stream,  | 
6812  |  |                                                         opj_event_mgr_t * p_manager )  | 
6813  | 0  | { | 
6814  |  |         /* add here initialization of cp  | 
6815  |  |            copy paste of setup_encoder */  | 
6816  | 0  |   (void)p_j2k;  | 
6817  | 0  |   (void)p_stream;  | 
6818  | 0  |   (void)p_manager;  | 
6819  | 0  |         return OPJ_TRUE;  | 
6820  | 0  | }  | 
6821  |  |  | 
6822  |  | OPJ_BOOL opj_j2k_encoding_validation (  opj_j2k_t * p_j2k,  | 
6823  |  |                                                                             opj_stream_private_t *p_stream,  | 
6824  |  |                                                                             opj_event_mgr_t * p_manager )  | 
6825  | 0  | { | 
6826  | 0  |         OPJ_BOOL l_is_valid = OPJ_TRUE;  | 
6827  |  |  | 
6828  |  |         /* preconditions */  | 
6829  | 0  |         assert(p_j2k != 00);  | 
6830  | 0  |         assert(p_stream != 00);  | 
6831  | 0  |         assert(p_manager != 00);  | 
6832  |  |  | 
6833  |  |         /* STATE checking */  | 
6834  |  |         /* make sure the state is at 0 */  | 
6835  | 0  |         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);  | 
6836  |  |  | 
6837  |  |         /* POINTER validation */  | 
6838  |  |         /* make sure a p_j2k codec is present */  | 
6839  | 0  |         l_is_valid &= (p_j2k->m_procedure_list != 00);  | 
6840  |  |         /* make sure a validation list is present */  | 
6841  | 0  |         l_is_valid &= (p_j2k->m_validation_list != 00);  | 
6842  |  | 
  | 
6843  | 0  |         if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { | 
6844  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");  | 
6845  | 0  |                 return OPJ_FALSE;  | 
6846  | 0  |         }  | 
6847  |  |  | 
6848  | 0  |         if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { | 
6849  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");  | 
6850  | 0  |                 return OPJ_FALSE;  | 
6851  | 0  |         }  | 
6852  |  |  | 
6853  |  |         /* PARAMETER VALIDATION */  | 
6854  | 0  |         return l_is_valid;  | 
6855  | 0  | }  | 
6856  |  |  | 
6857  |  | OPJ_BOOL opj_j2k_decoding_validation (  opj_j2k_t *p_j2k,  | 
6858  |  |                                         opj_stream_private_t *p_stream,  | 
6859  |  |                                         opj_event_mgr_t * p_manager  | 
6860  |  |                                         )  | 
6861  | 0  | { | 
6862  | 0  |         OPJ_BOOL l_is_valid = OPJ_TRUE;  | 
6863  |  |  | 
6864  |  |         /* preconditions*/  | 
6865  | 0  |         assert(p_j2k != 00);  | 
6866  | 0  |         assert(p_stream != 00);  | 
6867  | 0  |         assert(p_manager != 00);  | 
6868  |  |  | 
6869  |  |         /* STATE checking */  | 
6870  |  |         /* make sure the state is at 0 */  | 
6871  |  | #ifdef TODO_MSD  | 
6872  |  |         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);  | 
6873  |  | #endif  | 
6874  | 0  |         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);  | 
6875  |  |  | 
6876  |  |         /* POINTER validation */  | 
6877  |  |         /* make sure a p_j2k codec is present */  | 
6878  |  |         /* make sure a procedure list is present */  | 
6879  | 0  |         l_is_valid &= (p_j2k->m_procedure_list != 00);  | 
6880  |  |         /* make sure a validation list is present */  | 
6881  | 0  |         l_is_valid &= (p_j2k->m_validation_list != 00);  | 
6882  |  |  | 
6883  |  |         /* PARAMETER VALIDATION */  | 
6884  | 0  |         return l_is_valid;  | 
6885  | 0  | }  | 
6886  |  |  | 
6887  |  | OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,  | 
6888  |  |                                                                             opj_stream_private_t *p_stream,  | 
6889  |  |                                                                             opj_event_mgr_t * p_manager)  | 
6890  | 0  | { | 
6891  | 0  |         OPJ_UINT32 l_current_marker;  | 
6892  | 0  |         OPJ_UINT32 l_marker_size;  | 
6893  | 0  |         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;  | 
6894  |  |  | 
6895  |  |         /* preconditions */  | 
6896  | 0  |         assert(p_stream != 00);  | 
6897  | 0  |         assert(p_j2k != 00);  | 
6898  | 0  |         assert(p_manager != 00);  | 
6899  |  |  | 
6900  |  |         /*  We enter in the main header */  | 
6901  | 0  |         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;  | 
6902  |  |  | 
6903  |  |         /* Try to read the SOC marker, the codestream must begin with SOC marker */  | 
6904  | 0  |         if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) { | 
6905  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");  | 
6906  | 0  |                 return OPJ_FALSE;  | 
6907  | 0  |         }  | 
6908  |  |  | 
6909  |  |         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */  | 
6910  | 0  |         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
6911  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
6912  | 0  |                 return OPJ_FALSE;  | 
6913  | 0  |         }  | 
6914  |  |  | 
6915  |  |         /* Read 2 bytes as the new marker ID */  | 
6916  | 0  |         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);  | 
6917  |  |  | 
6918  |  |         /* Try to read until the SOT is detected */  | 
6919  | 0  |         while (l_current_marker != J2K_MS_SOT) { | 
6920  |  |  | 
6921  |  |                 /* Check if the current marker ID is valid */  | 
6922  | 0  |                 if (l_current_marker < 0xff00) { | 
6923  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);  | 
6924  | 0  |                         return OPJ_FALSE;  | 
6925  | 0  |                 }  | 
6926  |  |  | 
6927  |  |                 /* Get the marker handler from the marker ID */  | 
6928  | 0  |                 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);  | 
6929  |  |  | 
6930  |  |                 /* Manage case where marker is unknown */  | 
6931  | 0  |                 if (l_marker_handler->id == J2K_MS_UNK) { | 
6932  | 0  |                         if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){ | 
6933  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");  | 
6934  | 0  |                                 return OPJ_FALSE;  | 
6935  | 0  |                         }  | 
6936  |  |  | 
6937  | 0  |                         if (l_current_marker == J2K_MS_SOT)  | 
6938  | 0  |                                 break; /* SOT marker is detected main header is completely read */  | 
6939  | 0  |                         else    /* Get the marker handler from the marker ID */  | 
6940  | 0  |                                 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);  | 
6941  | 0  |                 }  | 
6942  |  |  | 
6943  |  |                 /* Check if the marker is known and if it is the right place to find it */  | 
6944  | 0  |                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { | 
6945  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");  | 
6946  | 0  |                         return OPJ_FALSE;  | 
6947  | 0  |                 }  | 
6948  |  |  | 
6949  |  |                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */  | 
6950  | 0  |                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
6951  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
6952  | 0  |                         return OPJ_FALSE;  | 
6953  | 0  |                 }  | 
6954  |  |  | 
6955  |  |                 /* read 2 bytes as the marker size */  | 
6956  | 0  |                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);  | 
6957  | 0  |                 l_marker_size -= 2; /* Subtract the size of the marker ID already read */  | 
6958  |  |  | 
6959  |  |                 /* Check if the marker size is compatible with the header data size */  | 
6960  | 0  |                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { | 
6961  | 0  |                         OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);  | 
6962  | 0  |                         if (! new_header_data) { | 
6963  | 0  |                                 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);  | 
6964  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;  | 
6965  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;  | 
6966  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");  | 
6967  | 0  |                                 return OPJ_FALSE;  | 
6968  | 0  |                         }  | 
6969  | 0  |                         p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;  | 
6970  | 0  |                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;  | 
6971  | 0  |                 }  | 
6972  |  |  | 
6973  |  |                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */  | 
6974  | 0  |                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { | 
6975  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
6976  | 0  |                         return OPJ_FALSE;  | 
6977  | 0  |                 }  | 
6978  |  |  | 
6979  |  |                 /* Read the marker segment with the correct marker handler */  | 
6980  | 0  |                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { | 
6981  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");  | 
6982  | 0  |                         return OPJ_FALSE;  | 
6983  | 0  |                 }  | 
6984  |  |  | 
6985  |  |                 /* Add the marker to the codestream index*/  | 
6986  | 0  |                 if (OPJ_FALSE == opj_j2k_add_mhmarker(  | 
6987  | 0  |                                         p_j2k->cstr_index,  | 
6988  | 0  |                                         l_marker_handler->id,  | 
6989  | 0  |                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,  | 
6990  | 0  |                                         l_marker_size + 4 )) { | 
6991  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");  | 
6992  | 0  |                         return OPJ_FALSE;  | 
6993  | 0  |                 }  | 
6994  |  |  | 
6995  |  |                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */  | 
6996  | 0  |                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
6997  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
6998  | 0  |                         return OPJ_FALSE;  | 
6999  | 0  |                 }  | 
7000  |  |  | 
7001  |  |                 /* read 2 bytes as the new marker ID */  | 
7002  | 0  |                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);  | 
7003  | 0  |         }  | 
7004  |  |  | 
7005  | 0  |         opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");  | 
7006  |  |  | 
7007  |  |         /* Position of the last element if the main header */  | 
7008  | 0  |         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;  | 
7009  |  |  | 
7010  |  |         /* Next step: read a tile-part header */  | 
7011  | 0  |         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;  | 
7012  |  | 
  | 
7013  | 0  |         return OPJ_TRUE;  | 
7014  | 0  | }  | 
7015  |  |  | 
7016  |  | OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,  | 
7017  |  |                                         opj_procedure_list_t * p_procedure_list,  | 
7018  |  |                                         opj_stream_private_t *p_stream,  | 
7019  |  |                                         opj_event_mgr_t * p_manager )  | 
7020  | 0  | { | 
7021  | 0  |         OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;  | 
7022  | 0  |         OPJ_BOOL l_result = OPJ_TRUE;  | 
7023  | 0  |         OPJ_UINT32 l_nb_proc, i;  | 
7024  |  |  | 
7025  |  |         /* preconditions*/  | 
7026  | 0  |         assert(p_procedure_list != 00);  | 
7027  | 0  |         assert(p_j2k != 00);  | 
7028  | 0  |         assert(p_stream != 00);  | 
7029  | 0  |         assert(p_manager != 00);  | 
7030  |  | 
  | 
7031  | 0  |         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);  | 
7032  | 0  |         l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);  | 
7033  |  | 
  | 
7034  | 0  |         for     (i=0;i<l_nb_proc;++i) { | 
7035  | 0  |                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));  | 
7036  | 0  |                 ++l_procedure;  | 
7037  | 0  |         }  | 
7038  |  |  | 
7039  |  |         /* and clear the procedure list at the end.*/  | 
7040  | 0  |         opj_procedure_list_clear(p_procedure_list);  | 
7041  | 0  |         return l_result;  | 
7042  | 0  | }  | 
7043  |  |  | 
7044  |  | /* FIXME DOC*/  | 
7045  |  | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,  | 
7046  |  |                                                             opj_stream_private_t *p_stream,  | 
7047  |  |                                                             opj_event_mgr_t * p_manager  | 
7048  |  |                                                             )  | 
7049  | 0  | { | 
7050  | 0  |         opj_tcp_t * l_tcp = 00;  | 
7051  | 0  |         opj_tcp_t * l_default_tcp = 00;  | 
7052  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
7053  | 0  |         OPJ_UINT32 i,j;  | 
7054  | 0  |         opj_tccp_t *l_current_tccp = 00;  | 
7055  | 0  |         OPJ_UINT32 l_tccp_size;  | 
7056  | 0  |         OPJ_UINT32 l_mct_size;  | 
7057  | 0  |         opj_image_t * l_image;  | 
7058  | 0  |         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;  | 
7059  | 0  |         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;  | 
7060  | 0  |         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;  | 
7061  | 0  |         OPJ_UINT32 l_offset;  | 
7062  |  |  | 
7063  |  |         /* preconditions */  | 
7064  | 0  |         assert(p_j2k != 00);  | 
7065  | 0  |         assert(p_stream != 00);  | 
7066  | 0  |         assert(p_manager != 00);  | 
7067  |  | 
  | 
7068  | 0  |         l_image = p_j2k->m_private_image;  | 
7069  | 0  |         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
7070  | 0  |         l_tcp = p_j2k->m_cp.tcps;  | 
7071  | 0  |         l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);  | 
7072  | 0  |         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
7073  | 0  |         l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);  | 
7074  |  |  | 
7075  |  |         /* For each tile */  | 
7076  | 0  |         for (i=0; i<l_nb_tiles; ++i) { | 
7077  |  |                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/  | 
7078  | 0  |                 l_current_tccp = l_tcp->tccps;  | 
7079  |  |                 /*Copy default coding parameters into the current tile coding parameters*/  | 
7080  | 0  |                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));  | 
7081  |  |                 /* Initialize some values of the current tile coding parameters*/  | 
7082  | 0  |                 l_tcp->ppt = 0;  | 
7083  | 0  |                 l_tcp->ppt_data = 00;  | 
7084  |  |                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/  | 
7085  | 0  |                 l_tcp->tccps = l_current_tccp;  | 
7086  |  |  | 
7087  |  |                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/  | 
7088  | 0  |                 if (l_default_tcp->m_mct_decoding_matrix) { | 
7089  | 0  |                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);  | 
7090  | 0  |                         if (! l_tcp->m_mct_decoding_matrix ) { | 
7091  | 0  |                                 return OPJ_FALSE;  | 
7092  | 0  |                         }  | 
7093  | 0  |                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);  | 
7094  | 0  |                 }  | 
7095  |  |  | 
7096  |  |                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/  | 
7097  | 0  |                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(opj_mct_data_t);  | 
7098  | 0  |                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);  | 
7099  | 0  |                 if (! l_tcp->m_mct_records) { | 
7100  | 0  |                         return OPJ_FALSE;  | 
7101  | 0  |                 }  | 
7102  | 0  |                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);  | 
7103  |  |  | 
7104  |  |                 /* Copy the mct record data from dflt_tile_cp to the current tile*/  | 
7105  | 0  |                 l_src_mct_rec = l_default_tcp->m_mct_records;  | 
7106  | 0  |                 l_dest_mct_rec = l_tcp->m_mct_records;  | 
7107  |  | 
  | 
7108  | 0  |                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) { | 
7109  |  | 
  | 
7110  | 0  |                         if (l_src_mct_rec->m_data) { | 
7111  |  | 
  | 
7112  | 0  |                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);  | 
7113  | 0  |                                 if(! l_dest_mct_rec->m_data) { | 
7114  | 0  |                                         return OPJ_FALSE;  | 
7115  | 0  |                                 }  | 
7116  | 0  |                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);  | 
7117  | 0  |                         }  | 
7118  |  |  | 
7119  | 0  |                         ++l_src_mct_rec;  | 
7120  | 0  |                         ++l_dest_mct_rec;  | 
7121  | 0  |                 }  | 
7122  |  |  | 
7123  |  |                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/  | 
7124  | 0  |                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t);  | 
7125  | 0  |                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);  | 
7126  | 0  |                 if (! l_tcp->m_mcc_records) { | 
7127  | 0  |                         return OPJ_FALSE;  | 
7128  | 0  |                 }  | 
7129  | 0  |                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);  | 
7130  |  |  | 
7131  |  |                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/  | 
7132  | 0  |                 l_src_mcc_rec = l_default_tcp->m_mcc_records;  | 
7133  | 0  |                 l_dest_mcc_rec = l_tcp->m_mcc_records;  | 
7134  |  | 
  | 
7135  | 0  |                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) { | 
7136  |  | 
  | 
7137  | 0  |                         if (l_src_mcc_rec->m_decorrelation_array) { | 
7138  | 0  |                                 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records);  | 
7139  | 0  |                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;  | 
7140  | 0  |                         }  | 
7141  |  | 
  | 
7142  | 0  |                         if (l_src_mcc_rec->m_offset_array) { | 
7143  | 0  |                                 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records);  | 
7144  | 0  |                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;  | 
7145  | 0  |                         }  | 
7146  |  | 
  | 
7147  | 0  |                         ++l_src_mcc_rec;  | 
7148  | 0  |                         ++l_dest_mcc_rec;  | 
7149  | 0  |                 }  | 
7150  |  |  | 
7151  |  |                 /* Copy all the dflt_tile_compo_cp to the current tile cp */  | 
7152  | 0  |                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);  | 
7153  |  |  | 
7154  |  |                 /* Move to next tile cp*/  | 
7155  | 0  |                 ++l_tcp;  | 
7156  | 0  |         }  | 
7157  |  |  | 
7158  |  |         /* Create the current tile decoder*/  | 
7159  | 0  |         p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */  | 
7160  | 0  |         if (! p_j2k->m_tcd ) { | 
7161  | 0  |                 return OPJ_FALSE;  | 
7162  | 0  |         }  | 
7163  |  |  | 
7164  | 0  |         if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) { | 
7165  | 0  |                 opj_tcd_destroy(p_j2k->m_tcd);  | 
7166  | 0  |                 p_j2k->m_tcd = 00;  | 
7167  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");  | 
7168  | 0  |                 return OPJ_FALSE;  | 
7169  | 0  |         }  | 
7170  |  |  | 
7171  | 0  |         return OPJ_TRUE;  | 
7172  | 0  | }  | 
7173  |  |  | 
7174  |  | const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)  | 
7175  | 0  | { | 
7176  | 0  |         const opj_dec_memory_marker_handler_t *e;  | 
7177  | 0  |         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { | 
7178  | 0  |                 if (e->id == p_id) { | 
7179  | 0  |                         break; /* we find a handler corresponding to the marker ID*/  | 
7180  | 0  |                 }  | 
7181  | 0  |         }  | 
7182  | 0  |         return e;  | 
7183  | 0  | }  | 
7184  |  |  | 
7185  |  | void opj_j2k_destroy (opj_j2k_t *p_j2k)  | 
7186  | 0  | { | 
7187  | 0  |         if (p_j2k == 00) { | 
7188  | 0  |                 return;  | 
7189  | 0  |         }  | 
7190  |  |  | 
7191  | 0  |         if (p_j2k->m_is_decoder) { | 
7192  |  | 
  | 
7193  | 0  |                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { | 
7194  | 0  |                         opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);  | 
7195  | 0  |                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);  | 
7196  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;  | 
7197  | 0  |                 }  | 
7198  |  | 
  | 
7199  | 0  |                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { | 
7200  | 0  |                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);  | 
7201  | 0  |                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;  | 
7202  | 0  |                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;  | 
7203  | 0  |                 }  | 
7204  | 0  |         }  | 
7205  | 0  |         else { | 
7206  |  | 
  | 
7207  | 0  |                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { | 
7208  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);  | 
7209  | 0  |                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;  | 
7210  | 0  |                 }  | 
7211  |  | 
  | 
7212  | 0  |                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { | 
7213  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);  | 
7214  | 0  |                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;  | 
7215  | 0  |                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;  | 
7216  | 0  |                 }  | 
7217  |  | 
  | 
7218  | 0  |                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { | 
7219  | 0  |                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
7220  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;  | 
7221  | 0  |                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
7222  | 0  |                 }  | 
7223  | 0  |         }  | 
7224  |  | 
  | 
7225  | 0  |         opj_tcd_destroy(p_j2k->m_tcd);  | 
7226  |  | 
  | 
7227  | 0  |         opj_j2k_cp_destroy(&(p_j2k->m_cp));  | 
7228  | 0  |         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));  | 
7229  |  | 
  | 
7230  | 0  |         opj_procedure_list_destroy(p_j2k->m_procedure_list);  | 
7231  | 0  |         p_j2k->m_procedure_list = 00;  | 
7232  |  | 
  | 
7233  | 0  |         opj_procedure_list_destroy(p_j2k->m_validation_list);  | 
7234  | 0  |         p_j2k->m_procedure_list = 00;  | 
7235  |  | 
  | 
7236  | 0  |         j2k_destroy_cstr_index(p_j2k->cstr_index);  | 
7237  | 0  |         p_j2k->cstr_index = NULL;  | 
7238  |  | 
  | 
7239  | 0  |         opj_image_destroy(p_j2k->m_private_image);  | 
7240  | 0  |         p_j2k->m_private_image = NULL;  | 
7241  |  | 
  | 
7242  | 0  |         opj_image_destroy(p_j2k->m_output_image);  | 
7243  | 0  |         p_j2k->m_output_image = NULL;  | 
7244  |  | 
  | 
7245  | 0  |         opj_free(p_j2k);  | 
7246  | 0  | }  | 
7247  |  |  | 
7248  |  | void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)  | 
7249  | 0  | { | 
7250  | 0  |         if (p_cstr_ind) { | 
7251  |  | 
  | 
7252  | 0  |                 if (p_cstr_ind->marker) { | 
7253  | 0  |                         opj_free(p_cstr_ind->marker);  | 
7254  | 0  |                         p_cstr_ind->marker = NULL;  | 
7255  | 0  |                 }  | 
7256  |  | 
  | 
7257  | 0  |                 if (p_cstr_ind->tile_index) { | 
7258  | 0  |                         OPJ_UINT32 it_tile = 0;  | 
7259  |  | 
  | 
7260  | 0  |                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { | 
7261  |  | 
  | 
7262  | 0  |                                 if(p_cstr_ind->tile_index[it_tile].packet_index) { | 
7263  | 0  |                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);  | 
7264  | 0  |                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;  | 
7265  | 0  |                                 }  | 
7266  |  | 
  | 
7267  | 0  |                                 if(p_cstr_ind->tile_index[it_tile].tp_index){ | 
7268  | 0  |                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);  | 
7269  | 0  |                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;  | 
7270  | 0  |                                 }  | 
7271  |  | 
  | 
7272  | 0  |                                 if(p_cstr_ind->tile_index[it_tile].marker){ | 
7273  | 0  |                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);  | 
7274  | 0  |                                         p_cstr_ind->tile_index[it_tile].marker = NULL;  | 
7275  |  | 
  | 
7276  | 0  |                                 }  | 
7277  | 0  |                         }  | 
7278  |  | 
  | 
7279  | 0  |                         opj_free( p_cstr_ind->tile_index);  | 
7280  | 0  |                         p_cstr_ind->tile_index = NULL;  | 
7281  | 0  |                 }  | 
7282  |  | 
  | 
7283  | 0  |                 opj_free(p_cstr_ind);  | 
7284  | 0  |         }  | 
7285  | 0  | }  | 
7286  |  |  | 
7287  |  | void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp)  | 
7288  | 0  | { | 
7289  | 0  |         if (p_tcp == 00) { | 
7290  | 0  |                 return;  | 
7291  | 0  |         }  | 
7292  |  |  | 
7293  | 0  |         if (p_tcp->ppt_buffer != 00) { | 
7294  | 0  |                 opj_free(p_tcp->ppt_buffer);  | 
7295  | 0  |                 p_tcp->ppt_buffer = 00;  | 
7296  | 0  |         }  | 
7297  |  | 
  | 
7298  | 0  |         if (p_tcp->tccps != 00) { | 
7299  | 0  |                 opj_free(p_tcp->tccps);  | 
7300  | 0  |                 p_tcp->tccps = 00;  | 
7301  | 0  |         }  | 
7302  |  | 
  | 
7303  | 0  |         if (p_tcp->m_mct_coding_matrix != 00) { | 
7304  | 0  |                 opj_free(p_tcp->m_mct_coding_matrix);  | 
7305  | 0  |                 p_tcp->m_mct_coding_matrix = 00;  | 
7306  | 0  |         }  | 
7307  |  | 
  | 
7308  | 0  |         if (p_tcp->m_mct_decoding_matrix != 00) { | 
7309  | 0  |                 opj_free(p_tcp->m_mct_decoding_matrix);  | 
7310  | 0  |                 p_tcp->m_mct_decoding_matrix = 00;  | 
7311  | 0  |         }  | 
7312  |  | 
  | 
7313  | 0  |         if (p_tcp->m_mcc_records) { | 
7314  | 0  |                 opj_free(p_tcp->m_mcc_records);  | 
7315  | 0  |                 p_tcp->m_mcc_records = 00;  | 
7316  | 0  |                 p_tcp->m_nb_max_mcc_records = 0;  | 
7317  | 0  |                 p_tcp->m_nb_mcc_records = 0;  | 
7318  | 0  |         }  | 
7319  |  | 
  | 
7320  | 0  |         if (p_tcp->m_mct_records) { | 
7321  | 0  |                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;  | 
7322  | 0  |                 OPJ_UINT32 i;  | 
7323  |  | 
  | 
7324  | 0  |                 for (i=0;i<p_tcp->m_nb_mct_records;++i) { | 
7325  | 0  |                         if (l_mct_data->m_data) { | 
7326  | 0  |                                 opj_free(l_mct_data->m_data);  | 
7327  | 0  |                                 l_mct_data->m_data = 00;  | 
7328  | 0  |                         }  | 
7329  |  | 
  | 
7330  | 0  |                         ++l_mct_data;  | 
7331  | 0  |                 }  | 
7332  |  | 
  | 
7333  | 0  |                 opj_free(p_tcp->m_mct_records);  | 
7334  | 0  |                 p_tcp->m_mct_records = 00;  | 
7335  | 0  |         }  | 
7336  |  | 
  | 
7337  | 0  |         if (p_tcp->mct_norms != 00) { | 
7338  | 0  |                 opj_free(p_tcp->mct_norms);  | 
7339  | 0  |                 p_tcp->mct_norms = 00;  | 
7340  | 0  |         }  | 
7341  |  | 
  | 
7342  | 0  |         opj_j2k_tcp_data_destroy(p_tcp);  | 
7343  |  | 
  | 
7344  | 0  | }  | 
7345  |  |  | 
7346  |  | void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp)  | 
7347  | 0  | { | 
7348  | 0  |         if (p_tcp->m_data) { | 
7349  | 0  |                 opj_free(p_tcp->m_data);  | 
7350  | 0  |                 p_tcp->m_data = NULL;  | 
7351  | 0  |                 p_tcp->m_data_size = 0;  | 
7352  | 0  |         }  | 
7353  | 0  | }  | 
7354  |  |  | 
7355  |  | void opj_j2k_cp_destroy (opj_cp_t *p_cp)  | 
7356  | 0  | { | 
7357  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
7358  | 0  |         opj_tcp_t * l_current_tile = 00;  | 
7359  | 0  |         OPJ_UINT32 i;  | 
7360  |  | 
  | 
7361  | 0  |         if (p_cp == 00)  | 
7362  | 0  |         { | 
7363  | 0  |                 return;  | 
7364  | 0  |         }  | 
7365  | 0  |         if (p_cp->tcps != 00)  | 
7366  | 0  |         { | 
7367  | 0  |                 l_current_tile = p_cp->tcps;  | 
7368  | 0  |                 l_nb_tiles = p_cp->th * p_cp->tw;  | 
7369  |  | 
  | 
7370  | 0  |                 for (i = 0; i < l_nb_tiles; ++i)  | 
7371  | 0  |                 { | 
7372  | 0  |                         opj_j2k_tcp_destroy(l_current_tile);  | 
7373  | 0  |                         ++l_current_tile;  | 
7374  | 0  |                 }  | 
7375  | 0  |                 opj_free(p_cp->tcps);  | 
7376  | 0  |                 p_cp->tcps = 00;  | 
7377  | 0  |         }  | 
7378  | 0  |         opj_free(p_cp->ppm_buffer);  | 
7379  | 0  |         p_cp->ppm_buffer = 00;  | 
7380  | 0  |         p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */  | 
7381  | 0  |         opj_free(p_cp->comment);  | 
7382  | 0  |         p_cp->comment = 00;  | 
7383  | 0  |         if (! p_cp->m_is_decoder)  | 
7384  | 0  |         { | 
7385  | 0  |                 opj_free(p_cp->m_specific_param.m_enc.m_matrice);  | 
7386  | 0  |                 p_cp->m_specific_param.m_enc.m_matrice = 00;  | 
7387  | 0  |         }  | 
7388  | 0  | }  | 
7389  |  |  | 
7390  |  | OPJ_BOOL opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,  | 
7391  |  |                                                                     OPJ_UINT32 * p_tile_index,  | 
7392  |  |                                                                     OPJ_UINT32 * p_data_size,  | 
7393  |  |                                                                     OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,  | 
7394  |  |                                                                     OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,  | 
7395  |  |                                                                     OPJ_UINT32 * p_nb_comps,  | 
7396  |  |                                                                     OPJ_BOOL * p_go_on,  | 
7397  |  |                                                                     opj_stream_private_t *p_stream,  | 
7398  |  |                                                                     opj_event_mgr_t * p_manager )  | 
7399  | 0  | { | 
7400  | 0  |         OPJ_UINT32 l_current_marker = J2K_MS_SOT;  | 
7401  | 0  |         OPJ_UINT32 l_marker_size;  | 
7402  | 0  |         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;  | 
7403  | 0  |         opj_tcp_t * l_tcp = NULL;  | 
7404  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
7405  |  |  | 
7406  |  |         /* preconditions */  | 
7407  | 0  |         assert(p_stream != 00);  | 
7408  | 0  |         assert(p_j2k != 00);  | 
7409  | 0  |         assert(p_manager != 00);  | 
7410  |  |  | 
7411  |  |         /* Reach the End Of Codestream ?*/  | 
7412  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){ | 
7413  | 0  |                 l_current_marker = J2K_MS_EOC;  | 
7414  | 0  |         }  | 
7415  |  |         /* We need to encounter a SOT marker (a new tile-part header) */  | 
7416  | 0  |         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){ | 
7417  | 0  |                 return OPJ_FALSE;  | 
7418  | 0  |         }  | 
7419  |  |  | 
7420  |  |         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */  | 
7421  | 0  |         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) { | 
7422  |  |  | 
7423  |  |                 /* Try to read until the Start Of Data is detected */  | 
7424  | 0  |                 while (l_current_marker != J2K_MS_SOD) { | 
7425  |  |                       | 
7426  | 0  |                     if(opj_stream_get_number_byte_left(p_stream) == 0)  | 
7427  | 0  |                     { | 
7428  | 0  |                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;  | 
7429  | 0  |                         break;  | 
7430  | 0  |                     }  | 
7431  |  |  | 
7432  |  |                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */  | 
7433  | 0  |                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
7434  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7435  | 0  |                                 return OPJ_FALSE;  | 
7436  | 0  |                         }  | 
7437  |  |  | 
7438  |  |                         /* Read 2 bytes from the buffer as the marker size */  | 
7439  | 0  |                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);  | 
7440  |  |  | 
7441  |  |                         /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */  | 
7442  | 0  |                         if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) { | 
7443  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;  | 
7444  | 0  |                                 break;  | 
7445  | 0  |                         }  | 
7446  |  |  | 
7447  |  |                         /* Why this condition? FIXME */  | 
7448  | 0  |                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){ | 
7449  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);  | 
7450  | 0  |                         }  | 
7451  | 0  |                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */  | 
7452  |  |  | 
7453  |  |                         /* Get the marker handler from the marker ID */  | 
7454  | 0  |                         l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);  | 
7455  |  |  | 
7456  |  |                         /* Check if the marker is known and if it is the right place to find it */  | 
7457  | 0  |                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { | 
7458  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");  | 
7459  | 0  |                                 return OPJ_FALSE;  | 
7460  | 0  |                         }  | 
7461  |  | /* FIXME manage case of unknown marker as in the main header ? */  | 
7462  |  |  | 
7463  |  |                         /* Check if the marker size is compatible with the header data size */  | 
7464  | 0  |                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { | 
7465  | 0  |                                 OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);  | 
7466  | 0  |                                 if (! new_header_data) { | 
7467  | 0  |                                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);  | 
7468  | 0  |                                         p_j2k->m_specific_param.m_decoder.m_header_data = NULL;  | 
7469  | 0  |                                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;  | 
7470  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");  | 
7471  | 0  |                                         return OPJ_FALSE;  | 
7472  | 0  |                                 }  | 
7473  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;  | 
7474  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;  | 
7475  | 0  |                         }  | 
7476  |  |  | 
7477  |  |                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */  | 
7478  | 0  |                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { | 
7479  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7480  | 0  |                                 return OPJ_FALSE;  | 
7481  | 0  |                         }  | 
7482  |  |  | 
7483  | 0  |                         if (!l_marker_handler->handler) { | 
7484  |  |                                 /* See issue #175 */  | 
7485  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");  | 
7486  | 0  |                                 return OPJ_FALSE;  | 
7487  | 0  |                         }  | 
7488  |  |                         /* Read the marker segment with the correct marker handler */  | 
7489  | 0  |                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { | 
7490  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);  | 
7491  | 0  |                                 return OPJ_FALSE;  | 
7492  | 0  |                         }  | 
7493  |  |  | 
7494  |  |                         /* Add the marker to the codestream index*/  | 
7495  | 0  |                         if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,  | 
7496  | 0  |                                                 p_j2k->cstr_index,  | 
7497  | 0  |                                                 l_marker_handler->id,  | 
7498  | 0  |                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,  | 
7499  | 0  |                                                 l_marker_size + 4 )) { | 
7500  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");  | 
7501  | 0  |                                 return OPJ_FALSE;  | 
7502  | 0  |                         }  | 
7503  |  |  | 
7504  |  |                         /* Keep the position of the last SOT marker read */  | 
7505  | 0  |                         if ( l_marker_handler->id == J2K_MS_SOT ) { | 
7506  | 0  |                                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;  | 
7507  | 0  |                                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)  | 
7508  | 0  |                                 { | 
7509  | 0  |                                         p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;  | 
7510  | 0  |                                 }  | 
7511  | 0  |                         }  | 
7512  |  | 
  | 
7513  | 0  |                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) { | 
7514  |  |                                 /* Skip the rest of the tile part header*/  | 
7515  | 0  |                                 if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { | 
7516  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7517  | 0  |                                         return OPJ_FALSE;  | 
7518  | 0  |                                 }  | 
7519  | 0  |                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */  | 
7520  | 0  |                         }  | 
7521  | 0  |                         else { | 
7522  |  |                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/  | 
7523  | 0  |                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
7524  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7525  | 0  |                                         return OPJ_FALSE;  | 
7526  | 0  |                                 }  | 
7527  |  |                                 /* Read 2 bytes from the buffer as the new marker ID */  | 
7528  | 0  |                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);  | 
7529  | 0  |                         }  | 
7530  | 0  |                 }  | 
7531  | 0  |                 if(opj_stream_get_number_byte_left(p_stream) == 0  | 
7532  | 0  |                     && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)  | 
7533  | 0  |                     break;  | 
7534  |  |  | 
7535  |  |                 /* If we didn't skip data before, we need to read the SOD marker*/  | 
7536  | 0  |                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { | 
7537  |  |                         /* Try to read the SOD marker and skip data ? FIXME */  | 
7538  | 0  |                         if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { | 
7539  | 0  |                                 return OPJ_FALSE;  | 
7540  | 0  |                         }  | 
7541  |  |  | 
7542  | 0  |                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){ | 
7543  |  |                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */  | 
7544  | 0  |                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
7545  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7546  | 0  |                                         return OPJ_FALSE;  | 
7547  | 0  |                                 }  | 
7548  |  |  | 
7549  |  |                                 /* Read 2 bytes from buffer as the new marker ID */  | 
7550  | 0  |                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);  | 
7551  | 0  |                         }  | 
7552  | 0  |                 }  | 
7553  | 0  |                 else { | 
7554  |  |                         /* Indicate we will try to read a new tile-part header*/  | 
7555  | 0  |                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;  | 
7556  | 0  |                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;  | 
7557  | 0  |                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;  | 
7558  |  |  | 
7559  |  |                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */  | 
7560  | 0  |                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { | 
7561  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7562  | 0  |                                 return OPJ_FALSE;  | 
7563  | 0  |                         }  | 
7564  |  |  | 
7565  |  |                         /* Read 2 bytes from buffer as the new marker ID */  | 
7566  | 0  |                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);  | 
7567  | 0  |                 }  | 
7568  | 0  |         }  | 
7569  |  |  | 
7570  |  |         /* Current marker is the EOC marker ?*/  | 
7571  | 0  |         if (l_current_marker == J2K_MS_EOC) { | 
7572  | 0  |                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){ | 
7573  | 0  |                         p_j2k->m_current_tile_number = 0;  | 
7574  | 0  |                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;  | 
7575  | 0  |                 }  | 
7576  | 0  |         }  | 
7577  |  |  | 
7578  |  |         /* FIXME DOC ???*/  | 
7579  | 0  |         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) { | 
7580  | 0  |                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;  | 
7581  | 0  |                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
7582  |  | 
  | 
7583  | 0  |                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) { | 
7584  | 0  |                         ++p_j2k->m_current_tile_number;  | 
7585  | 0  |                         ++l_tcp;  | 
7586  | 0  |                 }  | 
7587  |  | 
  | 
7588  | 0  |                 if (p_j2k->m_current_tile_number == l_nb_tiles) { | 
7589  | 0  |                         *p_go_on = OPJ_FALSE;  | 
7590  | 0  |                         return OPJ_TRUE;  | 
7591  | 0  |                 }  | 
7592  | 0  |         }  | 
7593  |  |  | 
7594  |  |         /*FIXME ???*/  | 
7595  | 0  |         if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { | 
7596  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");  | 
7597  | 0  |                 return OPJ_FALSE;  | 
7598  | 0  |         }  | 
7599  |  |  | 
7600  | 0  |         opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",  | 
7601  | 0  |                         p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);  | 
7602  |  | 
  | 
7603  | 0  |         *p_tile_index = p_j2k->m_current_tile_number;  | 
7604  | 0  |         *p_go_on = OPJ_TRUE;  | 
7605  | 0  |         *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd);  | 
7606  | 0  |         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;  | 
7607  | 0  |         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;  | 
7608  | 0  |         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;  | 
7609  | 0  |         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;  | 
7610  | 0  |         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;  | 
7611  |  | 
  | 
7612  | 0  |          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/  | 
7613  |  | 
  | 
7614  | 0  |         return OPJ_TRUE;  | 
7615  | 0  | }  | 
7616  |  |  | 
7617  |  | OPJ_BOOL opj_j2k_decode_tile (  opj_j2k_t * p_j2k,  | 
7618  |  |                                                         OPJ_UINT32 p_tile_index,  | 
7619  |  |                                                         OPJ_BYTE * p_data,  | 
7620  |  |                                                         OPJ_UINT32 p_data_size,  | 
7621  |  |                                                         opj_stream_private_t *p_stream,  | 
7622  |  |                                                         opj_event_mgr_t * p_manager )  | 
7623  | 0  | { | 
7624  | 0  |         OPJ_UINT32 l_current_marker;  | 
7625  | 0  |         OPJ_BYTE l_data [2];  | 
7626  | 0  |         opj_tcp_t * l_tcp;  | 
7627  |  |  | 
7628  |  |         /* preconditions */  | 
7629  | 0  |         assert(p_stream != 00);  | 
7630  | 0  |         assert(p_j2k != 00);  | 
7631  | 0  |         assert(p_manager != 00);  | 
7632  |  | 
  | 
7633  | 0  |         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)  | 
7634  | 0  |                 || (p_tile_index != p_j2k->m_current_tile_number) ) { | 
7635  | 0  |                 return OPJ_FALSE;  | 
7636  | 0  |         }  | 
7637  |  |  | 
7638  | 0  |         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);  | 
7639  | 0  |         if (! l_tcp->m_data) { | 
7640  | 0  |                 opj_j2k_tcp_destroy(l_tcp);  | 
7641  | 0  |                 return OPJ_FALSE;  | 
7642  | 0  |         }  | 
7643  |  |  | 
7644  | 0  |         if (! opj_tcd_decode_tile(      p_j2k->m_tcd,  | 
7645  | 0  |                                                                 l_tcp->m_data,  | 
7646  | 0  |                                                                 l_tcp->m_data_size,  | 
7647  | 0  |                                                                 p_tile_index,  | 
7648  | 0  |                                                                 p_j2k->cstr_index) ) { | 
7649  | 0  |                 opj_j2k_tcp_destroy(l_tcp);  | 
7650  | 0  |                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/  | 
7651  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");  | 
7652  | 0  |                 return OPJ_FALSE;  | 
7653  | 0  |         }  | 
7654  |  |  | 
7655  | 0  |         if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { | 
7656  | 0  |                 return OPJ_FALSE;  | 
7657  | 0  |         }  | 
7658  |  |  | 
7659  |  |         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)  | 
7660  |  |          * we destroy just the data which will be re-read in read_tile_header*/  | 
7661  |  |         /*opj_j2k_tcp_destroy(l_tcp);  | 
7662  |  |         p_j2k->m_tcd->tcp = 0;*/  | 
7663  | 0  |         opj_j2k_tcp_data_destroy(l_tcp);  | 
7664  |  | 
  | 
7665  | 0  |         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;  | 
7666  | 0  |         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/  | 
7667  |  | 
  | 
7668  | 0  |         if(opj_stream_get_number_byte_left(p_stream) == 0   | 
7669  | 0  |             && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){ | 
7670  | 0  |             return OPJ_TRUE;  | 
7671  | 0  |         }  | 
7672  |  |  | 
7673  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/ | 
7674  | 0  |                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { | 
7675  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");  | 
7676  | 0  |                         return OPJ_FALSE;  | 
7677  | 0  |                 }  | 
7678  |  |  | 
7679  | 0  |                 opj_read_bytes(l_data,&l_current_marker,2);  | 
7680  |  | 
  | 
7681  | 0  |                 if (l_current_marker == J2K_MS_EOC) { | 
7682  | 0  |                         p_j2k->m_current_tile_number = 0;  | 
7683  | 0  |                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;/*FIXME J2K_DEC_STATE_EOC;*/  | 
7684  | 0  |                 }  | 
7685  | 0  |                 else if (l_current_marker != J2K_MS_SOT)  | 
7686  | 0  |                 { | 
7687  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");  | 
7688  |  |                           | 
7689  | 0  |                         if(opj_stream_get_number_byte_left(p_stream) == 0) { | 
7690  | 0  |                             p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;  | 
7691  | 0  |                             return OPJ_TRUE;  | 
7692  | 0  |                         }  | 
7693  | 0  |                         return OPJ_FALSE;  | 
7694  | 0  |                 }  | 
7695  | 0  |         }  | 
7696  |  |  | 
7697  | 0  |         return OPJ_TRUE;  | 
7698  | 0  | }  | 
7699  |  |  | 
7700  |  | OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)  | 
7701  | 0  | { | 
7702  | 0  |         OPJ_UINT32 i,j,k = 0;  | 
7703  | 0  |         OPJ_UINT32 l_width_src,l_height_src;  | 
7704  | 0  |         OPJ_UINT32 l_width_dest,l_height_dest;  | 
7705  | 0  |         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;  | 
7706  | 0  |         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;  | 
7707  | 0  |         OPJ_UINT32 l_start_x_dest , l_start_y_dest;  | 
7708  | 0  |         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;  | 
7709  | 0  |         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;  | 
7710  |  | 
  | 
7711  | 0  |         opj_image_comp_t * l_img_comp_src = 00;  | 
7712  | 0  |         opj_image_comp_t * l_img_comp_dest = 00;  | 
7713  |  | 
  | 
7714  | 0  |         opj_tcd_tilecomp_t * l_tilec = 00;  | 
7715  | 0  |         opj_image_t * l_image_src = 00;  | 
7716  | 0  |         OPJ_UINT32 l_size_comp, l_remaining;  | 
7717  | 0  |         OPJ_INT32 * l_dest_ptr;  | 
7718  | 0  |         opj_tcd_resolution_t* l_res= 00;  | 
7719  |  | 
  | 
7720  | 0  |         l_tilec = p_tcd->tcd_image->tiles->comps;  | 
7721  | 0  |         l_image_src = p_tcd->image;  | 
7722  | 0  |         l_img_comp_src = l_image_src->comps;  | 
7723  |  | 
  | 
7724  | 0  |         l_img_comp_dest = p_output_image->comps;  | 
7725  |  | 
  | 
7726  | 0  |         for (i=0; i<l_image_src->numcomps; i++) { | 
7727  |  |  | 
7728  |  |                 /* Allocate output component buffer if necessary */  | 
7729  | 0  |                 if (!l_img_comp_dest->data) { | 
7730  |  | 
  | 
7731  | 0  |                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));  | 
7732  | 0  |                         if (! l_img_comp_dest->data) { | 
7733  | 0  |                                 return OPJ_FALSE;  | 
7734  | 0  |                         }  | 
7735  | 0  |                 }  | 
7736  |  |  | 
7737  |  |                 /* Copy info from decoded comp image to output image */  | 
7738  | 0  |                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;  | 
7739  |  |  | 
7740  |  |                 /*-----*/  | 
7741  |  |                 /* Compute the precision of the output buffer */  | 
7742  | 0  |                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/  | 
7743  | 0  |                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */  | 
7744  | 0  |                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;  | 
7745  |  | 
  | 
7746  | 0  |                 if (l_remaining) { | 
7747  | 0  |                         ++l_size_comp;  | 
7748  | 0  |                 }  | 
7749  |  | 
  | 
7750  | 0  |                 if (l_size_comp == 3) { | 
7751  | 0  |                         l_size_comp = 4;  | 
7752  | 0  |                 }  | 
7753  |  |                 /*-----*/  | 
7754  |  |  | 
7755  |  |                 /* Current tile component size*/  | 
7756  |  |                 /*if (i == 0) { | 
7757  |  |                 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",  | 
7758  |  |                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);  | 
7759  |  |                 }*/  | 
7760  |  | 
  | 
7761  | 0  |                 l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0);  | 
7762  | 0  |                 l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0);  | 
7763  |  |  | 
7764  |  |                 /* Border of the current output component*/  | 
7765  | 0  |                 l_x0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->x0, (OPJ_INT32)l_img_comp_dest->factor);  | 
7766  | 0  |                 l_y0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->y0, (OPJ_INT32)l_img_comp_dest->factor);  | 
7767  | 0  |                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;  | 
7768  | 0  |                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;  | 
7769  |  |  | 
7770  |  |                 /*if (i == 0) { | 
7771  |  |                 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",  | 
7772  |  |                                 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );  | 
7773  |  |                 }*/  | 
7774  |  |  | 
7775  |  |                 /*-----*/  | 
7776  |  |                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)  | 
7777  |  |                  * of the input buffer (decoded tile component) which will be move  | 
7778  |  |                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,  | 
7779  |  |                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified  | 
7780  |  |                  * by this input area.  | 
7781  |  |                  * */  | 
7782  | 0  |                 assert( l_res->x0 >= 0);  | 
7783  | 0  |                 assert( l_res->x1 >= 0);  | 
7784  | 0  |                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) { | 
7785  | 0  |                         l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest;  | 
7786  | 0  |                         l_offset_x0_src = 0;  | 
7787  |  | 
  | 
7788  | 0  |                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { | 
7789  | 0  |                                 l_width_dest = l_width_src;  | 
7790  | 0  |                                 l_offset_x1_src = 0;  | 
7791  | 0  |                         }  | 
7792  | 0  |                         else { | 
7793  | 0  |                                 l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ;  | 
7794  | 0  |                                 l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);  | 
7795  | 0  |                         }  | 
7796  | 0  |                 }  | 
7797  | 0  |                 else { | 
7798  | 0  |                         l_start_x_dest = 0 ;  | 
7799  | 0  |                         l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0;  | 
7800  |  | 
  | 
7801  | 0  |                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { | 
7802  | 0  |                                 l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;  | 
7803  | 0  |                                 l_offset_x1_src = 0;  | 
7804  | 0  |                         }  | 
7805  | 0  |                         else { | 
7806  | 0  |                                 l_width_dest = l_img_comp_dest->w ;  | 
7807  | 0  |                                 l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest;  | 
7808  | 0  |                         }  | 
7809  | 0  |                 }  | 
7810  |  | 
  | 
7811  | 0  |                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) { | 
7812  | 0  |                         l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest;  | 
7813  | 0  |                         l_offset_y0_src = 0;  | 
7814  |  | 
  | 
7815  | 0  |                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { | 
7816  | 0  |                                 l_height_dest = l_height_src;  | 
7817  | 0  |                                 l_offset_y1_src = 0;  | 
7818  | 0  |                         }  | 
7819  | 0  |                         else { | 
7820  | 0  |                                 l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ;  | 
7821  | 0  |                                 l_offset_y1_src =  (OPJ_INT32)(l_height_src - l_height_dest);  | 
7822  | 0  |                         }  | 
7823  | 0  |                 }  | 
7824  | 0  |                 else { | 
7825  | 0  |                         l_start_y_dest = 0 ;  | 
7826  | 0  |                         l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0;  | 
7827  |  | 
  | 
7828  | 0  |                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { | 
7829  | 0  |                                 l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;  | 
7830  | 0  |                                 l_offset_y1_src = 0;  | 
7831  | 0  |                         }  | 
7832  | 0  |                         else { | 
7833  | 0  |                                 l_height_dest = l_img_comp_dest->h ;  | 
7834  | 0  |                                 l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest;  | 
7835  | 0  |                         }  | 
7836  | 0  |                 }  | 
7837  |  | 
  | 
7838  | 0  |                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){ | 
7839  | 0  |                         return OPJ_FALSE;  | 
7840  | 0  |                 }  | 
7841  |  |                 /* testcase 2977.pdf.asan.67.2198 */  | 
7842  | 0  |                 if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { | 
7843  | 0  |                         return OPJ_FALSE;  | 
7844  | 0  |                 }  | 
7845  |  |                 /*-----*/  | 
7846  |  |  | 
7847  |  |                 /* Compute the input buffer offset */  | 
7848  | 0  |                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * (OPJ_INT32)l_width_src;  | 
7849  | 0  |                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;  | 
7850  | 0  |                 l_end_offset_src = l_offset_y1_src * (OPJ_INT32)l_width_src - l_offset_x0_src;  | 
7851  |  |  | 
7852  |  |                 /* Compute the output buffer offset */  | 
7853  | 0  |                 l_start_offset_dest = (OPJ_INT32)(l_start_x_dest + l_start_y_dest * l_img_comp_dest->w);  | 
7854  | 0  |                 l_line_offset_dest = (OPJ_INT32)(l_img_comp_dest->w - l_width_dest);  | 
7855  |  |  | 
7856  |  |                 /* Move the output buffer to the first place where we will write*/  | 
7857  | 0  |                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;  | 
7858  |  |  | 
7859  |  |                 /*if (i == 0) { | 
7860  |  |                         fprintf(stdout, "COMPO[%d]:\n",i);  | 
7861  |  |                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"  | 
7862  |  |                                         "\t tile offset:%d, %d, %d, %d\n"  | 
7863  |  |                                         "\t buffer offset: %d; %d, %d\n",  | 
7864  |  |                                         l_res->x0, l_res->y0, l_width_src, l_height_src,  | 
7865  |  |                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,  | 
7866  |  |                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);  | 
7867  |  |  | 
7868  |  |                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"  | 
7869  |  |                                         "\t start offset: %d, line offset= %d\n",  | 
7870  |  |                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);  | 
7871  |  |                 }*/  | 
7872  |  | 
  | 
7873  | 0  |                 switch (l_size_comp) { | 
7874  | 0  |                         case 1:  | 
7875  | 0  |                                 { | 
7876  | 0  |                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;  | 
7877  | 0  |                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/  | 
7878  |  | 
  | 
7879  | 0  |                                         if (l_img_comp_src->sgnd) { | 
7880  | 0  |                                                 for (j = 0 ; j < l_height_dest ; ++j) { | 
7881  | 0  |                                                         for ( k = 0 ; k < l_width_dest ; ++k) { | 
7882  | 0  |                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */  | 
7883  | 0  |                                                         }  | 
7884  |  | 
  | 
7885  | 0  |                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */  | 
7886  | 0  |                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */  | 
7887  | 0  |                                                 }  | 
7888  | 0  |                                         }  | 
7889  | 0  |                                         else { | 
7890  | 0  |                                                 for ( j = 0 ; j < l_height_dest ; ++j ) { | 
7891  | 0  |                                                         for ( k = 0 ; k < l_width_dest ; ++k) { | 
7892  | 0  |                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);  | 
7893  | 0  |                                                         }  | 
7894  |  | 
  | 
7895  | 0  |                                                         l_dest_ptr+= l_line_offset_dest;  | 
7896  | 0  |                                                         l_src_ptr += l_line_offset_src;  | 
7897  | 0  |                                                 }  | 
7898  | 0  |                                         }  | 
7899  |  | 
  | 
7900  | 0  |                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */  | 
7901  | 0  |                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */  | 
7902  | 0  |                                 }  | 
7903  | 0  |                                 break;  | 
7904  | 0  |                         case 2:  | 
7905  | 0  |                                 { | 
7906  | 0  |                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;  | 
7907  | 0  |                                         l_src_ptr += l_start_offset_src;  | 
7908  |  | 
  | 
7909  | 0  |                                         if (l_img_comp_src->sgnd) { | 
7910  | 0  |                                                 for (j=0;j<l_height_dest;++j) { | 
7911  | 0  |                                                         for (k=0;k<l_width_dest;++k) { | 
7912  | 0  |                                                                 *(l_dest_ptr++) = *(l_src_ptr++);  | 
7913  | 0  |                                                         }  | 
7914  |  | 
  | 
7915  | 0  |                                                         l_dest_ptr+= l_line_offset_dest;  | 
7916  | 0  |                                                         l_src_ptr += l_line_offset_src ;  | 
7917  | 0  |                                                 }  | 
7918  | 0  |                                         }  | 
7919  | 0  |                                         else { | 
7920  | 0  |                                                 for (j=0;j<l_height_dest;++j) { | 
7921  | 0  |                                                         for (k=0;k<l_width_dest;++k) { | 
7922  | 0  |                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;  | 
7923  | 0  |                                                         }  | 
7924  |  | 
  | 
7925  | 0  |                                                         l_dest_ptr+= l_line_offset_dest;  | 
7926  | 0  |                                                         l_src_ptr += l_line_offset_src ;  | 
7927  | 0  |                                                 }  | 
7928  | 0  |                                         }  | 
7929  |  | 
  | 
7930  | 0  |                                         l_src_ptr += l_end_offset_src;  | 
7931  | 0  |                                         p_data = (OPJ_BYTE*) l_src_ptr;  | 
7932  | 0  |                                 }  | 
7933  | 0  |                                 break;  | 
7934  | 0  |                         case 4:  | 
7935  | 0  |                                 { | 
7936  | 0  |                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;  | 
7937  | 0  |                                         l_src_ptr += l_start_offset_src;  | 
7938  |  | 
  | 
7939  | 0  |                                         for (j=0;j<l_height_dest;++j) { | 
7940  | 0  |                                                 for (k=0;k<l_width_dest;++k) { | 
7941  | 0  |                                                         *(l_dest_ptr++) = (*(l_src_ptr++));  | 
7942  | 0  |                                                 }  | 
7943  |  | 
  | 
7944  | 0  |                                                 l_dest_ptr+= l_line_offset_dest;  | 
7945  | 0  |                                                 l_src_ptr += l_line_offset_src ;  | 
7946  | 0  |                                         }  | 
7947  |  | 
  | 
7948  | 0  |                                         l_src_ptr += l_end_offset_src;  | 
7949  | 0  |                                         p_data = (OPJ_BYTE*) l_src_ptr;  | 
7950  | 0  |                                 }  | 
7951  | 0  |                                 break;  | 
7952  | 0  |                 }  | 
7953  |  |  | 
7954  | 0  |                 ++l_img_comp_dest;  | 
7955  | 0  |                 ++l_img_comp_src;  | 
7956  | 0  |                 ++l_tilec;  | 
7957  | 0  |         }  | 
7958  |  |  | 
7959  | 0  |         return OPJ_TRUE;  | 
7960  | 0  | }  | 
7961  |  |  | 
7962  |  | OPJ_BOOL opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,  | 
7963  |  |                                                                     opj_image_t* p_image,  | 
7964  |  |                                                                     OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,  | 
7965  |  |                                                                     OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,  | 
7966  |  |                                                                     opj_event_mgr_t * p_manager )  | 
7967  | 0  | { | 
7968  | 0  |         opj_cp_t * l_cp = &(p_j2k->m_cp);  | 
7969  | 0  |         opj_image_t * l_image = p_j2k->m_private_image;  | 
7970  |  | 
  | 
7971  | 0  |         OPJ_UINT32 it_comp;  | 
7972  | 0  |         OPJ_INT32 l_comp_x1, l_comp_y1;  | 
7973  | 0  |         opj_image_comp_t* l_img_comp = NULL;  | 
7974  |  |  | 
7975  |  |         /* Check if we are read the main header */  | 
7976  | 0  |         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/ | 
7977  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");  | 
7978  | 0  |                 return OPJ_FALSE;  | 
7979  | 0  |         }  | 
7980  |  |  | 
7981  | 0  |         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){ | 
7982  | 0  |                 opj_event_msg(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");  | 
7983  |  | 
  | 
7984  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;  | 
7985  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;  | 
7986  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;  | 
7987  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;  | 
7988  |  | 
  | 
7989  | 0  |                 return OPJ_TRUE;  | 
7990  | 0  |         }  | 
7991  |  |  | 
7992  |  |         /* ----- */  | 
7993  |  |         /* Check if the positions provided by the user are correct */  | 
7994  |  |  | 
7995  |  |         /* Left */  | 
7996  | 0  |         assert(p_start_x >= 0 );  | 
7997  | 0  |         assert(p_start_y >= 0 );  | 
7998  |  | 
  | 
7999  | 0  |         if ((OPJ_UINT32)p_start_x > l_image->x1 ) { | 
8000  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
8001  | 0  |                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",  | 
8002  | 0  |                         p_start_x, l_image->x1);  | 
8003  | 0  |                 return OPJ_FALSE;  | 
8004  | 0  |         }  | 
8005  | 0  |         else if ((OPJ_UINT32)p_start_x < l_image->x0){ | 
8006  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
8007  | 0  |                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",  | 
8008  | 0  |                                 p_start_x, l_image->x0);  | 
8009  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;  | 
8010  | 0  |                 p_image->x0 = l_image->x0;  | 
8011  | 0  |         }  | 
8012  | 0  |         else { | 
8013  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx;  | 
8014  | 0  |                 p_image->x0 = (OPJ_UINT32)p_start_x;  | 
8015  | 0  |         }  | 
8016  |  |  | 
8017  |  |         /* Up */  | 
8018  | 0  |         if ((OPJ_UINT32)p_start_y > l_image->y1){ | 
8019  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
8020  | 0  |                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",  | 
8021  | 0  |                                 p_start_y, l_image->y1);  | 
8022  | 0  |                 return OPJ_FALSE;  | 
8023  | 0  |         }  | 
8024  | 0  |         else if ((OPJ_UINT32)p_start_y < l_image->y0){ | 
8025  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
8026  | 0  |                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",  | 
8027  | 0  |                                 p_start_y, l_image->y0);  | 
8028  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;  | 
8029  | 0  |                 p_image->y0 = l_image->y0;  | 
8030  | 0  |         }  | 
8031  | 0  |         else { | 
8032  | 0  |                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy;  | 
8033  | 0  |                 p_image->y0 = (OPJ_UINT32)p_start_y;  | 
8034  | 0  |         }  | 
8035  |  |  | 
8036  |  |         /* Right */  | 
8037  | 0  |         assert((OPJ_UINT32)p_end_x > 0);  | 
8038  | 0  |         assert((OPJ_UINT32)p_end_y > 0);  | 
8039  | 0  |         if ((OPJ_UINT32)p_end_x < l_image->x0) { | 
8040  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
8041  | 0  |                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",  | 
8042  | 0  |                         p_end_x, l_image->x0);  | 
8043  | 0  |                 return OPJ_FALSE;  | 
8044  | 0  |         }  | 
8045  | 0  |         else if ((OPJ_UINT32)p_end_x > l_image->x1) { | 
8046  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
8047  | 0  |                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",  | 
8048  | 0  |                         p_end_x, l_image->x1);  | 
8049  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;  | 
8050  | 0  |                 p_image->x1 = l_image->x1;  | 
8051  | 0  |         }  | 
8052  | 0  |         else { | 
8053  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);  | 
8054  | 0  |                 p_image->x1 = (OPJ_UINT32)p_end_x;  | 
8055  | 0  |         }  | 
8056  |  |  | 
8057  |  |         /* Bottom */  | 
8058  | 0  |         if ((OPJ_UINT32)p_end_y < l_image->y0) { | 
8059  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
8060  | 0  |                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",  | 
8061  | 0  |                         p_end_y, l_image->y0);  | 
8062  | 0  |                 return OPJ_FALSE;  | 
8063  | 0  |         }  | 
8064  | 0  |         if ((OPJ_UINT32)p_end_y > l_image->y1){ | 
8065  | 0  |                 opj_event_msg(p_manager, EVT_WARNING,  | 
8066  | 0  |                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",  | 
8067  | 0  |                         p_end_y, l_image->y1);  | 
8068  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;  | 
8069  | 0  |                 p_image->y1 = l_image->y1;  | 
8070  | 0  |         }  | 
8071  | 0  |         else{ | 
8072  | 0  |                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);  | 
8073  | 0  |                 p_image->y1 = (OPJ_UINT32)p_end_y;  | 
8074  | 0  |         }  | 
8075  |  |         /* ----- */  | 
8076  |  | 
  | 
8077  | 0  |         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;  | 
8078  |  | 
  | 
8079  | 0  |         l_img_comp = p_image->comps;  | 
8080  | 0  |         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)  | 
8081  | 0  |         { | 
8082  | 0  |                 OPJ_INT32 l_h,l_w;  | 
8083  |  | 
  | 
8084  | 0  |                 l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);  | 
8085  | 0  |                 l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);  | 
8086  | 0  |                 l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);  | 
8087  | 0  |                 l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);  | 
8088  |  | 
  | 
8089  | 0  |                 l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)  | 
8090  | 0  |                                 - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);  | 
8091  | 0  |                 if (l_w < 0){ | 
8092  | 0  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
8093  | 0  |                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",  | 
8094  | 0  |                                 it_comp, l_w);  | 
8095  | 0  |                         return OPJ_FALSE;  | 
8096  | 0  |                 }  | 
8097  | 0  |                 l_img_comp->w = (OPJ_UINT32)l_w;  | 
8098  |  | 
  | 
8099  | 0  |                 l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)  | 
8100  | 0  |                                 - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);  | 
8101  | 0  |                 if (l_h < 0){ | 
8102  | 0  |                         opj_event_msg(p_manager, EVT_ERROR,  | 
8103  | 0  |                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",  | 
8104  | 0  |                                 it_comp, l_h);  | 
8105  | 0  |                         return OPJ_FALSE;  | 
8106  | 0  |                 }  | 
8107  | 0  |                 l_img_comp->h = (OPJ_UINT32)l_h;  | 
8108  |  | 
  | 
8109  | 0  |                 l_img_comp++;  | 
8110  | 0  |         }  | 
8111  |  |  | 
8112  | 0  |         opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",  | 
8113  | 0  |                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);  | 
8114  |  | 
  | 
8115  | 0  |         return OPJ_TRUE;  | 
8116  | 0  | }  | 
8117  |  |  | 
8118  |  | opj_j2k_t* opj_j2k_create_decompress(void)  | 
8119  | 0  | { | 
8120  | 0  |         opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));  | 
8121  | 0  |         if (!l_j2k) { | 
8122  | 0  |                 return 00;  | 
8123  | 0  |         }  | 
8124  | 0  |         memset(l_j2k,0,sizeof(opj_j2k_t));  | 
8125  |  | 
  | 
8126  | 0  |         l_j2k->m_is_decoder = 1;  | 
8127  | 0  |         l_j2k->m_cp.m_is_decoder = 1;  | 
8128  |  | 
  | 
8129  | 0  |         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_malloc(sizeof(opj_tcp_t));  | 
8130  | 0  |         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { | 
8131  | 0  |                 opj_j2k_destroy(l_j2k);  | 
8132  | 0  |                 return 00;  | 
8133  | 0  |         }  | 
8134  | 0  |         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_t));  | 
8135  |  | 
  | 
8136  | 0  |         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);  | 
8137  | 0  |         if (! l_j2k->m_specific_param.m_decoder.m_header_data) { | 
8138  | 0  |                 opj_j2k_destroy(l_j2k);  | 
8139  | 0  |                 return 00;  | 
8140  | 0  |         }  | 
8141  |  |  | 
8142  | 0  |         l_j2k->m_specific_param.m_decoder.m_header_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;  | 
8143  |  | 
  | 
8144  | 0  |         l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;  | 
8145  |  | 
  | 
8146  | 0  |         l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;  | 
8147  |  |  | 
8148  |  |         /* codestream index creation */  | 
8149  | 0  |         l_j2k->cstr_index = opj_j2k_create_cstr_index();  | 
8150  |  |  | 
8151  |  |                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));  | 
8152  |  |         if (!l_j2k->cstr_index){ | 
8153  |  |                 opj_j2k_destroy(l_j2k);  | 
8154  |  |                 return NULL;  | 
8155  |  |         }  | 
8156  |  |  | 
8157  |  |         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));  | 
8158  |  | */  | 
8159  |  |  | 
8160  |  |         /* validation list creation */  | 
8161  | 0  |         l_j2k->m_validation_list = opj_procedure_list_create();  | 
8162  | 0  |         if (! l_j2k->m_validation_list) { | 
8163  | 0  |                 opj_j2k_destroy(l_j2k);  | 
8164  | 0  |                 return 00;  | 
8165  | 0  |         }  | 
8166  |  |  | 
8167  |  |         /* execution list creation */  | 
8168  | 0  |         l_j2k->m_procedure_list = opj_procedure_list_create();  | 
8169  | 0  |         if (! l_j2k->m_procedure_list) { | 
8170  | 0  |                 opj_j2k_destroy(l_j2k);  | 
8171  | 0  |                 return 00;  | 
8172  | 0  |         }  | 
8173  |  |  | 
8174  | 0  |         return l_j2k;  | 
8175  | 0  | }  | 
8176  |  |  | 
8177  |  | opj_codestream_index_t* opj_j2k_create_cstr_index(void)  | 
8178  | 0  | { | 
8179  | 0  |         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)  | 
8180  | 0  |                         opj_calloc(1,sizeof(opj_codestream_index_t));  | 
8181  | 0  |         if (!cstr_index)  | 
8182  | 0  |                 return NULL;  | 
8183  |  |  | 
8184  | 0  |         cstr_index->maxmarknum = 100;  | 
8185  | 0  |         cstr_index->marknum = 0;  | 
8186  | 0  |         cstr_index->marker = (opj_marker_info_t*)  | 
8187  | 0  |                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));  | 
8188  | 0  |         if (!cstr_index-> marker)  | 
8189  | 0  |                 return NULL;  | 
8190  |  |  | 
8191  | 0  |         cstr_index->tile_index = NULL;  | 
8192  |  | 
  | 
8193  | 0  |         return cstr_index;  | 
8194  | 0  | }  | 
8195  |  |  | 
8196  |  | OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (       opj_j2k_t *p_j2k,  | 
8197  |  |                                                                                 OPJ_UINT32 p_tile_no,  | 
8198  |  |                                                                                 OPJ_UINT32 p_comp_no )  | 
8199  | 0  | { | 
8200  | 0  |         opj_cp_t *l_cp = 00;  | 
8201  | 0  |         opj_tcp_t *l_tcp = 00;  | 
8202  | 0  |         opj_tccp_t *l_tccp = 00;  | 
8203  |  |  | 
8204  |  |         /* preconditions */  | 
8205  | 0  |         assert(p_j2k != 00);  | 
8206  |  | 
  | 
8207  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8208  | 0  |         l_tcp = &l_cp->tcps[p_tile_no];  | 
8209  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
8210  |  |  | 
8211  |  |         /* preconditions again */  | 
8212  | 0  |         assert(p_tile_no < (l_cp->tw * l_cp->th));  | 
8213  | 0  |         assert(p_comp_no < p_j2k->m_private_image->numcomps);  | 
8214  |  | 
  | 
8215  | 0  |         if (l_tccp->csty & J2K_CCP_CSTY_PRT) { | 
8216  | 0  |                 return 5 + l_tccp->numresolutions;  | 
8217  | 0  |         }  | 
8218  | 0  |         else { | 
8219  | 0  |                 return 5;  | 
8220  | 0  |         }  | 
8221  | 0  | }  | 
8222  |  |  | 
8223  |  | OPJ_BOOL opj_j2k_write_SPCod_SPCoc(     opj_j2k_t *p_j2k,  | 
8224  |  |                                                                     OPJ_UINT32 p_tile_no,  | 
8225  |  |                                                                     OPJ_UINT32 p_comp_no,  | 
8226  |  |                                                                     OPJ_BYTE * p_data,  | 
8227  |  |                                                                     OPJ_UINT32 * p_header_size,  | 
8228  |  |                                                                     struct opj_event_mgr * p_manager )  | 
8229  | 0  | { | 
8230  | 0  |         OPJ_UINT32 i;  | 
8231  | 0  |         opj_cp_t *l_cp = 00;  | 
8232  | 0  |         opj_tcp_t *l_tcp = 00;  | 
8233  | 0  |         opj_tccp_t *l_tccp = 00;  | 
8234  |  |  | 
8235  |  |         /* preconditions */  | 
8236  | 0  |         assert(p_j2k != 00);  | 
8237  | 0  |         assert(p_header_size != 00);  | 
8238  | 0  |         assert(p_manager != 00);  | 
8239  | 0  |         assert(p_data != 00);  | 
8240  |  | 
  | 
8241  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8242  | 0  |         l_tcp = &l_cp->tcps[p_tile_no];  | 
8243  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
8244  |  |  | 
8245  |  |         /* preconditions again */  | 
8246  | 0  |         assert(p_tile_no < (l_cp->tw * l_cp->th));  | 
8247  | 0  |         assert(p_comp_no <(p_j2k->m_private_image->numcomps));  | 
8248  |  | 
  | 
8249  | 0  |         if (*p_header_size < 5) { | 
8250  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");  | 
8251  | 0  |                 return OPJ_FALSE;  | 
8252  | 0  |         }  | 
8253  |  |  | 
8254  | 0  |         opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1);  /* SPcoc (D) */  | 
8255  | 0  |         ++p_data;  | 
8256  |  | 
  | 
8257  | 0  |         opj_write_bytes(p_data,l_tccp->cblkw - 2, 1);                   /* SPcoc (E) */  | 
8258  | 0  |         ++p_data;  | 
8259  |  | 
  | 
8260  | 0  |         opj_write_bytes(p_data,l_tccp->cblkh - 2, 1);                   /* SPcoc (F) */  | 
8261  | 0  |         ++p_data;  | 
8262  |  | 
  | 
8263  | 0  |         opj_write_bytes(p_data,l_tccp->cblksty, 1);                             /* SPcoc (G) */  | 
8264  | 0  |         ++p_data;  | 
8265  |  | 
  | 
8266  | 0  |         opj_write_bytes(p_data,l_tccp->qmfbid, 1);                              /* SPcoc (H) */  | 
8267  | 0  |         ++p_data;  | 
8268  |  | 
  | 
8269  | 0  |         *p_header_size = *p_header_size - 5;  | 
8270  |  | 
  | 
8271  | 0  |         if (l_tccp->csty & J2K_CCP_CSTY_PRT) { | 
8272  |  | 
  | 
8273  | 0  |                 if (*p_header_size < l_tccp->numresolutions) { | 
8274  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");  | 
8275  | 0  |                         return OPJ_FALSE;  | 
8276  | 0  |                 }  | 
8277  |  |  | 
8278  | 0  |                 for (i = 0; i < l_tccp->numresolutions; ++i) { | 
8279  | 0  |                         opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1);    /* SPcoc (I_i) */  | 
8280  | 0  |                         ++p_data;  | 
8281  | 0  |                 }  | 
8282  |  | 
  | 
8283  | 0  |                 *p_header_size = *p_header_size - l_tccp->numresolutions;  | 
8284  | 0  |         }  | 
8285  |  |  | 
8286  | 0  |         return OPJ_TRUE;  | 
8287  | 0  | }  | 
8288  |  |  | 
8289  |  | OPJ_BOOL opj_j2k_read_SPCod_SPCoc(  opj_j2k_t *p_j2k,  | 
8290  |  |                                                                 OPJ_UINT32 compno,  | 
8291  |  |                                                                 OPJ_BYTE * p_header_data,  | 
8292  |  |                                                                 OPJ_UINT32 * p_header_size,  | 
8293  |  |                                                                 opj_event_mgr_t * p_manager)  | 
8294  | 0  | { | 
8295  | 0  |         OPJ_UINT32 i, l_tmp;  | 
8296  | 0  |         opj_cp_t *l_cp = NULL;  | 
8297  | 0  |         opj_tcp_t *l_tcp = NULL;  | 
8298  | 0  |         opj_tccp_t *l_tccp = NULL;  | 
8299  | 0  |         OPJ_BYTE * l_current_ptr = NULL;  | 
8300  |  |  | 
8301  |  |         /* preconditions */  | 
8302  | 0  |         assert(p_j2k != 00);  | 
8303  | 0  |         assert(p_manager != 00);  | 
8304  | 0  |         assert(p_header_data != 00);  | 
8305  |  | 
  | 
8306  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8307  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?  | 
8308  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
8309  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
8310  |  |  | 
8311  |  |         /* precondition again */  | 
8312  | 0  |         assert(compno < p_j2k->m_private_image->numcomps);  | 
8313  |  | 
  | 
8314  | 0  |         l_tccp = &l_tcp->tccps[compno];  | 
8315  | 0  |         l_current_ptr = p_header_data;  | 
8316  |  |  | 
8317  |  |         /* make sure room is sufficient */  | 
8318  | 0  |         if (*p_header_size < 5) { | 
8319  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");  | 
8320  | 0  |                 return OPJ_FALSE;  | 
8321  | 0  |         }  | 
8322  |  |  | 
8323  | 0  |         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */  | 
8324  | 0  |         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */  | 
8325  | 0  |         if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { | 
8326  | 0  |                 opj_event_msg(p_manager, EVT_ERROR,  | 
8327  | 0  |                               "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",  | 
8328  | 0  |                               l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);  | 
8329  | 0  |                 return OPJ_FALSE;  | 
8330  | 0  |         }  | 
8331  | 0  |         ++l_current_ptr;  | 
8332  |  |  | 
8333  |  |         /* If user wants to remove more resolutions than the codestream contains, return error */  | 
8334  | 0  |         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { | 
8335  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "  | 
8336  | 0  |                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);  | 
8337  | 0  |                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/  | 
8338  | 0  |                 return OPJ_FALSE;  | 
8339  | 0  |         }  | 
8340  |  |  | 
8341  | 0  |         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */  | 
8342  | 0  |         ++l_current_ptr;  | 
8343  | 0  |         l_tccp->cblkw += 2;  | 
8344  |  | 
  | 
8345  | 0  |         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */  | 
8346  | 0  |         ++l_current_ptr;  | 
8347  | 0  |         l_tccp->cblkh += 2;  | 
8348  |  | 
  | 
8349  | 0  |         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */  | 
8350  | 0  |         ++l_current_ptr;  | 
8351  |  | 
  | 
8352  | 0  |         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */  | 
8353  | 0  |         ++l_current_ptr;  | 
8354  |  | 
  | 
8355  | 0  |         *p_header_size = *p_header_size - 5;  | 
8356  |  |  | 
8357  |  |         /* use custom precinct size ? */  | 
8358  | 0  |         if (l_tccp->csty & J2K_CCP_CSTY_PRT) { | 
8359  | 0  |                 if (*p_header_size < l_tccp->numresolutions) { | 
8360  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");  | 
8361  | 0  |                         return OPJ_FALSE;  | 
8362  | 0  |                 }  | 
8363  |  |  | 
8364  | 0  |                 for     (i = 0; i < l_tccp->numresolutions; ++i) { | 
8365  | 0  |                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */  | 
8366  | 0  |                         ++l_current_ptr;  | 
8367  | 0  |                         l_tccp->prcw[i] = l_tmp & 0xf;  | 
8368  | 0  |                         l_tccp->prch[i] = l_tmp >> 4;  | 
8369  | 0  |                 }  | 
8370  |  | 
  | 
8371  | 0  |                 *p_header_size = *p_header_size - l_tccp->numresolutions;  | 
8372  | 0  |         }  | 
8373  | 0  |         else { | 
8374  |  |                 /* set default size for the precinct width and height */  | 
8375  | 0  |                 for     (i = 0; i < l_tccp->numresolutions; ++i) { | 
8376  | 0  |                         l_tccp->prcw[i] = 15;  | 
8377  | 0  |                         l_tccp->prch[i] = 15;  | 
8378  | 0  |                 }  | 
8379  | 0  |         }  | 
8380  |  |  | 
8381  |  | #ifdef WIP_REMOVE_MSD  | 
8382  |  |         /* INDEX >> */  | 
8383  |  |         if (p_j2k->cstr_info && compno == 0) { | 
8384  |  |                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);  | 
8385  |  |  | 
8386  |  |                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;  | 
8387  |  |                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;  | 
8388  |  |                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;  | 
8389  |  |                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;  | 
8390  |  |                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;  | 
8391  |  |  | 
8392  |  |                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);  | 
8393  |  |                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);  | 
8394  |  |         }  | 
8395  |  |         /* << INDEX */  | 
8396  |  | #endif  | 
8397  |  |  | 
8398  | 0  |         return OPJ_TRUE;  | 
8399  | 0  | }  | 
8400  |  |  | 
8401  |  | void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k )  | 
8402  | 0  | { | 
8403  |  |         /* loop */  | 
8404  | 0  |         OPJ_UINT32 i;  | 
8405  | 0  |         opj_cp_t *l_cp = NULL;  | 
8406  | 0  |         opj_tcp_t *l_tcp = NULL;  | 
8407  | 0  |         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;  | 
8408  | 0  |         OPJ_UINT32 l_prc_size;  | 
8409  |  |  | 
8410  |  |         /* preconditions */  | 
8411  | 0  |         assert(p_j2k != 00);  | 
8412  |  | 
  | 
8413  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8414  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/  | 
8415  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
8416  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
8417  |  | 
  | 
8418  | 0  |         l_ref_tccp = &l_tcp->tccps[0];  | 
8419  | 0  |         l_copied_tccp = l_ref_tccp + 1;  | 
8420  | 0  |         l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);  | 
8421  |  | 
  | 
8422  | 0  |         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) { | 
8423  | 0  |                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;  | 
8424  | 0  |                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;  | 
8425  | 0  |                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;  | 
8426  | 0  |                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;  | 
8427  | 0  |                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;  | 
8428  | 0  |                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);  | 
8429  | 0  |                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);  | 
8430  | 0  |                 ++l_copied_tccp;  | 
8431  | 0  |         }  | 
8432  | 0  | }  | 
8433  |  |  | 
8434  |  | OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,  | 
8435  |  |                                                                         OPJ_UINT32 p_tile_no,  | 
8436  |  |                                                                         OPJ_UINT32 p_comp_no )  | 
8437  | 0  | { | 
8438  | 0  |         OPJ_UINT32 l_num_bands;  | 
8439  |  | 
  | 
8440  | 0  |         opj_cp_t *l_cp = 00;  | 
8441  | 0  |         opj_tcp_t *l_tcp = 00;  | 
8442  | 0  |         opj_tccp_t *l_tccp = 00;  | 
8443  |  |  | 
8444  |  |         /* preconditions */  | 
8445  | 0  |         assert(p_j2k != 00);  | 
8446  |  | 
  | 
8447  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8448  | 0  |         l_tcp = &l_cp->tcps[p_tile_no];  | 
8449  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
8450  |  |  | 
8451  |  |         /* preconditions again */  | 
8452  | 0  |         assert(p_tile_no < l_cp->tw * l_cp->th);  | 
8453  | 0  |         assert(p_comp_no < p_j2k->m_private_image->numcomps);  | 
8454  |  | 
  | 
8455  | 0  |         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);  | 
8456  |  | 
  | 
8457  | 0  |         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  { | 
8458  | 0  |                 return 1 + l_num_bands;  | 
8459  | 0  |         }  | 
8460  | 0  |         else { | 
8461  | 0  |                 return 1 + 2*l_num_bands;  | 
8462  | 0  |         }  | 
8463  | 0  | }  | 
8464  |  |  | 
8465  |  | OPJ_BOOL opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,  | 
8466  |  |                                                                 OPJ_UINT32 p_tile_no,  | 
8467  |  |                                                                 OPJ_UINT32 p_comp_no,  | 
8468  |  |                                                                 OPJ_BYTE * p_data,  | 
8469  |  |                                                                 OPJ_UINT32 * p_header_size,  | 
8470  |  |                                                                 struct opj_event_mgr * p_manager )  | 
8471  | 0  | { | 
8472  | 0  |         OPJ_UINT32 l_header_size;  | 
8473  | 0  |         OPJ_UINT32 l_band_no, l_num_bands;  | 
8474  | 0  |         OPJ_UINT32 l_expn,l_mant;  | 
8475  |  | 
  | 
8476  | 0  |         opj_cp_t *l_cp = 00;  | 
8477  | 0  |         opj_tcp_t *l_tcp = 00;  | 
8478  | 0  |         opj_tccp_t *l_tccp = 00;  | 
8479  |  |  | 
8480  |  |         /* preconditions */  | 
8481  | 0  |         assert(p_j2k != 00);  | 
8482  | 0  |         assert(p_header_size != 00);  | 
8483  | 0  |         assert(p_manager != 00);  | 
8484  | 0  |         assert(p_data != 00);  | 
8485  |  | 
  | 
8486  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8487  | 0  |         l_tcp = &l_cp->tcps[p_tile_no];  | 
8488  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
8489  |  |  | 
8490  |  |         /* preconditions again */  | 
8491  | 0  |         assert(p_tile_no < l_cp->tw * l_cp->th);  | 
8492  | 0  |         assert(p_comp_no <p_j2k->m_private_image->numcomps);  | 
8493  |  | 
  | 
8494  | 0  |         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);  | 
8495  |  | 
  | 
8496  | 0  |         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  { | 
8497  | 0  |                 l_header_size = 1 + l_num_bands;  | 
8498  |  | 
  | 
8499  | 0  |                 if (*p_header_size < l_header_size) { | 
8500  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");  | 
8501  | 0  |                         return OPJ_FALSE;  | 
8502  | 0  |                 }  | 
8503  |  |  | 
8504  | 0  |                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */  | 
8505  | 0  |                 ++p_data;  | 
8506  |  | 
  | 
8507  | 0  |                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { | 
8508  | 0  |                         l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;  | 
8509  | 0  |                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */  | 
8510  | 0  |                         ++p_data;  | 
8511  | 0  |                 }  | 
8512  | 0  |         }  | 
8513  | 0  |         else { | 
8514  | 0  |                 l_header_size = 1 + 2*l_num_bands;  | 
8515  |  | 
  | 
8516  | 0  |                 if (*p_header_size < l_header_size) { | 
8517  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");  | 
8518  | 0  |                         return OPJ_FALSE;  | 
8519  | 0  |                 }  | 
8520  |  |  | 
8521  | 0  |                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */  | 
8522  | 0  |                 ++p_data;  | 
8523  |  | 
  | 
8524  | 0  |                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { | 
8525  | 0  |                         l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;  | 
8526  | 0  |                         l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;  | 
8527  |  | 
  | 
8528  | 0  |                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */  | 
8529  | 0  |                         p_data += 2;  | 
8530  | 0  |                 }  | 
8531  | 0  |         }  | 
8532  |  |  | 
8533  | 0  |         *p_header_size = *p_header_size - l_header_size;  | 
8534  |  | 
  | 
8535  | 0  |         return OPJ_TRUE;  | 
8536  | 0  | }  | 
8537  |  |  | 
8538  |  | OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,  | 
8539  |  |                                                             OPJ_UINT32 p_comp_no,  | 
8540  |  |                                                             OPJ_BYTE* p_header_data,  | 
8541  |  |                                                             OPJ_UINT32 * p_header_size,  | 
8542  |  |                                                             opj_event_mgr_t * p_manager  | 
8543  |  |                                                             )  | 
8544  | 0  | { | 
8545  |  |         /* loop*/  | 
8546  | 0  |         OPJ_UINT32 l_band_no;  | 
8547  | 0  |         opj_cp_t *l_cp = 00;  | 
8548  | 0  |         opj_tcp_t *l_tcp = 00;  | 
8549  | 0  |         opj_tccp_t *l_tccp = 00;  | 
8550  | 0  |         OPJ_BYTE * l_current_ptr = 00;  | 
8551  | 0  |         OPJ_UINT32 l_tmp, l_num_band;  | 
8552  |  |  | 
8553  |  |         /* preconditions*/  | 
8554  | 0  |         assert(p_j2k != 00);  | 
8555  | 0  |         assert(p_manager != 00);  | 
8556  | 0  |         assert(p_header_data != 00);  | 
8557  |  | 
  | 
8558  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8559  |  |         /* come from tile part header or main header ?*/  | 
8560  | 0  |         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/  | 
8561  | 0  |                                 &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
8562  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
8563  |  |  | 
8564  |  |         /* precondition again*/  | 
8565  | 0  |         assert(p_comp_no <  p_j2k->m_private_image->numcomps);  | 
8566  |  | 
  | 
8567  | 0  |         l_tccp = &l_tcp->tccps[p_comp_no];  | 
8568  | 0  |         l_current_ptr = p_header_data;  | 
8569  |  | 
  | 
8570  | 0  |         if (*p_header_size < 1) { | 
8571  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");  | 
8572  | 0  |                 return OPJ_FALSE;  | 
8573  | 0  |         }  | 
8574  | 0  |         *p_header_size -= 1;  | 
8575  |  | 
  | 
8576  | 0  |         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */  | 
8577  | 0  |         ++l_current_ptr;  | 
8578  |  | 
  | 
8579  | 0  |         l_tccp->qntsty = l_tmp & 0x1f;  | 
8580  | 0  |         l_tccp->numgbits = l_tmp >> 5;  | 
8581  | 0  |         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { | 
8582  | 0  |         l_num_band = 1;  | 
8583  | 0  |         }  | 
8584  | 0  |         else { | 
8585  | 0  |                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?  | 
8586  | 0  |                         (*p_header_size) :  | 
8587  | 0  |                         (*p_header_size) / 2;  | 
8588  |  | 
  | 
8589  | 0  |                 if( l_num_band > OPJ_J2K_MAXBANDS ) { | 
8590  | 0  |                         opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "  | 
8591  | 0  |                                 "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "  | 
8592  | 0  |                                 "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS);  | 
8593  |  |                         /*return OPJ_FALSE;*/  | 
8594  | 0  |                 }  | 
8595  | 0  |         }  | 
8596  |  | 
  | 
8597  |  | #ifdef USE_JPWL  | 
8598  |  |         if (l_cp->correct) { | 
8599  |  |  | 
8600  |  |                 /* if JPWL is on, we check whether there are too many subbands */  | 
8601  |  |                 if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { | 
8602  |  |                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,  | 
8603  |  |                                 "JPWL: bad number of subbands in Sqcx (%d)\n",  | 
8604  |  |                                 l_num_band);  | 
8605  |  |                         if (!JPWL_ASSUME) { | 
8606  |  |                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");  | 
8607  |  |                                 return OPJ_FALSE;  | 
8608  |  |                         }  | 
8609  |  |                         /* we try to correct */  | 
8610  |  |                         l_num_band = 1;  | 
8611  |  |                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"  | 
8612  |  |                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",  | 
8613  |  |                                 l_num_band);  | 
8614  |  |                 };  | 
8615  |  |  | 
8616  |  |         };  | 
8617  |  | #endif /* USE_JPWL */  | 
8618  |  | 
  | 
8619  | 0  |         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { | 
8620  | 0  |                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { | 
8621  | 0  |                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */  | 
8622  | 0  |                         ++l_current_ptr;  | 
8623  | 0  |                         if (l_band_no < OPJ_J2K_MAXBANDS){ | 
8624  | 0  |                                 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);  | 
8625  | 0  |                                 l_tccp->stepsizes[l_band_no].mant = 0;  | 
8626  | 0  |                         }  | 
8627  | 0  |                 }  | 
8628  | 0  |                 *p_header_size = *p_header_size - l_num_band;  | 
8629  | 0  |         }  | 
8630  | 0  |         else { | 
8631  | 0  |                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { | 
8632  | 0  |                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */  | 
8633  | 0  |                         l_current_ptr+=2;  | 
8634  | 0  |                         if (l_band_no < OPJ_J2K_MAXBANDS){ | 
8635  | 0  |                                 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);  | 
8636  | 0  |                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;  | 
8637  | 0  |                         }  | 
8638  | 0  |                 }  | 
8639  | 0  |                 *p_header_size = *p_header_size - 2*l_num_band;  | 
8640  | 0  |         }  | 
8641  |  |  | 
8642  |  |         /* Add Antonin : if scalar_derived -> compute other stepsizes */  | 
8643  | 0  |         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { | 
8644  | 0  |                 for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { | 
8645  | 0  |                         l_tccp->stepsizes[l_band_no].expn =  | 
8646  | 0  |                                 ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ?  | 
8647  | 0  |                                         (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;  | 
8648  | 0  |                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;  | 
8649  | 0  |                 }  | 
8650  | 0  |         }  | 
8651  |  | 
  | 
8652  | 0  |         return OPJ_TRUE;  | 
8653  | 0  | }  | 
8654  |  |  | 
8655  |  | void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k )  | 
8656  | 0  | { | 
8657  | 0  |         OPJ_UINT32 i;  | 
8658  | 0  |         opj_cp_t *l_cp = NULL;  | 
8659  | 0  |         opj_tcp_t *l_tcp = NULL;  | 
8660  | 0  |         opj_tccp_t *l_ref_tccp = NULL;  | 
8661  | 0  |         opj_tccp_t *l_copied_tccp = NULL;  | 
8662  | 0  |         OPJ_UINT32 l_size;  | 
8663  |  |  | 
8664  |  |         /* preconditions */  | 
8665  | 0  |         assert(p_j2k != 00);  | 
8666  |  | 
  | 
8667  | 0  |         l_cp = &(p_j2k->m_cp);  | 
8668  | 0  |         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?  | 
8669  | 0  |                         &l_cp->tcps[p_j2k->m_current_tile_number] :  | 
8670  | 0  |                         p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
8671  |  | 
  | 
8672  | 0  |         l_ref_tccp = &l_tcp->tccps[0];  | 
8673  | 0  |         l_copied_tccp = l_ref_tccp + 1;  | 
8674  | 0  |         l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);  | 
8675  |  | 
  | 
8676  | 0  |         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) { | 
8677  | 0  |                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;  | 
8678  | 0  |                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;  | 
8679  | 0  |                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);  | 
8680  | 0  |                 ++l_copied_tccp;  | 
8681  | 0  |         }  | 
8682  | 0  | }  | 
8683  |  |  | 
8684  |  | static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream)  | 
8685  | 0  | { | 
8686  | 0  |         if (l_default_tile)  | 
8687  | 0  |         { | 
8688  | 0  |                 OPJ_INT32 compno;  | 
8689  |  | 
  | 
8690  | 0  |                 fprintf(out_stream, "\t default tile {\n"); | 
8691  | 0  |                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);  | 
8692  | 0  |                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);  | 
8693  | 0  |                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);  | 
8694  | 0  |                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);  | 
8695  |  | 
  | 
8696  | 0  |                 for (compno = 0; compno < numcomps; compno++) { | 
8697  | 0  |                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);  | 
8698  | 0  |                         OPJ_UINT32 resno;  | 
8699  | 0  |       OPJ_INT32 bandno, numbands;  | 
8700  |  |  | 
8701  |  |                         /* coding style*/  | 
8702  | 0  |                         fprintf(out_stream, "\t\t comp %d {\n", compno); | 
8703  | 0  |                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);  | 
8704  | 0  |                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);  | 
8705  | 0  |                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);  | 
8706  | 0  |                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);  | 
8707  | 0  |                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);  | 
8708  | 0  |                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);  | 
8709  |  | 
  | 
8710  | 0  |                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");  | 
8711  | 0  |                         for (resno = 0; resno < l_tccp->numresolutions; resno++) { | 
8712  | 0  |                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);  | 
8713  | 0  |                         }  | 
8714  | 0  |                         fprintf(out_stream, "\n");  | 
8715  |  |  | 
8716  |  |                         /* quantization style*/  | 
8717  | 0  |                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);  | 
8718  | 0  |                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);  | 
8719  | 0  |                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");  | 
8720  | 0  |                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;  | 
8721  | 0  |                         for (bandno = 0; bandno < numbands; bandno++) { | 
8722  | 0  |                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,  | 
8723  | 0  |                                         l_tccp->stepsizes[bandno].expn);  | 
8724  | 0  |                         }  | 
8725  | 0  |                         fprintf(out_stream, "\n");  | 
8726  |  |  | 
8727  |  |                         /* RGN value*/  | 
8728  | 0  |                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);  | 
8729  |  | 
  | 
8730  | 0  |                         fprintf(out_stream, "\t\t }\n");  | 
8731  | 0  |                 } /*end of component of default tile*/  | 
8732  | 0  |                 fprintf(out_stream, "\t }\n"); /*end of default tile*/  | 
8733  | 0  |             }  | 
8734  | 0  | }  | 
8735  |  |  | 
8736  |  | void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)  | 
8737  | 0  | { | 
8738  |  |         /* Check if the flag is compatible with j2k file*/  | 
8739  | 0  |         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){ | 
8740  | 0  |                 fprintf(out_stream, "Wrong flag\n");  | 
8741  | 0  |                 return;  | 
8742  | 0  |         }  | 
8743  |  |  | 
8744  |  |         /* Dump the image_header */  | 
8745  | 0  |         if (flag & OPJ_IMG_INFO){ | 
8746  | 0  |                 if (p_j2k->m_private_image)  | 
8747  | 0  |                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);  | 
8748  | 0  |         }  | 
8749  |  |  | 
8750  |  |         /* Dump the codestream info from main header */  | 
8751  | 0  |         if (flag & OPJ_J2K_MH_INFO){ | 
8752  | 0  |                 opj_j2k_dump_MH_info(p_j2k, out_stream);  | 
8753  | 0  |         }  | 
8754  |  |         /* Dump all tile/codestream info */  | 
8755  | 0  |         if (flag & OPJ_J2K_TCH_INFO){ | 
8756  | 0  |           OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
8757  | 0  |           OPJ_UINT32 i;  | 
8758  | 0  |           opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;  | 
8759  | 0  |           for (i=0;i<l_nb_tiles;++i) { | 
8760  | 0  |             opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);  | 
8761  | 0  |             ++l_tcp;  | 
8762  | 0  |           }  | 
8763  | 0  |         }  | 
8764  |  |  | 
8765  |  |         /* Dump the codestream info of the current tile */  | 
8766  | 0  |         if (flag & OPJ_J2K_TH_INFO){ | 
8767  |  | 
  | 
8768  | 0  |         }  | 
8769  |  |  | 
8770  |  |         /* Dump the codestream index from main header */  | 
8771  | 0  |         if (flag & OPJ_J2K_MH_IND){ | 
8772  | 0  |                 opj_j2k_dump_MH_index(p_j2k, out_stream);  | 
8773  | 0  |         }  | 
8774  |  |  | 
8775  |  |         /* Dump the codestream index of the current tile */  | 
8776  | 0  |         if (flag & OPJ_J2K_TH_IND){ | 
8777  |  | 
  | 
8778  | 0  |         }  | 
8779  |  | 
  | 
8780  | 0  | }  | 
8781  |  |  | 
8782  |  | void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)  | 
8783  | 0  | { | 
8784  | 0  |         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;  | 
8785  | 0  |         OPJ_UINT32 it_marker, it_tile, it_tile_part;  | 
8786  |  | 
  | 
8787  | 0  |         fprintf(out_stream, "Codestream index from main header: {\n"); | 
8788  |  | 
  | 
8789  | 0  |         fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"  | 
8790  | 0  |                                     "\t Main header end position=%" PRIi64 "\n",  | 
8791  | 0  |                         cstr_index->main_head_start, cstr_index->main_head_end);  | 
8792  |  | 
  | 
8793  | 0  |         fprintf(out_stream, "\t Marker list: {\n"); | 
8794  |  | 
  | 
8795  | 0  |         if (cstr_index->marker){ | 
8796  | 0  |                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){ | 
8797  | 0  |                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",  | 
8798  | 0  |                                         cstr_index->marker[it_marker].type,  | 
8799  | 0  |                                         cstr_index->marker[it_marker].pos,  | 
8800  | 0  |                                         cstr_index->marker[it_marker].len );  | 
8801  | 0  |                 }  | 
8802  | 0  |         }  | 
8803  |  | 
  | 
8804  | 0  |         fprintf(out_stream, "\t }\n");  | 
8805  |  | 
  | 
8806  | 0  |         if (cstr_index->tile_index){ | 
8807  |  |  | 
8808  |  |         /* Simple test to avoid to write empty information*/  | 
8809  | 0  |         OPJ_UINT32 l_acc_nb_of_tile_part = 0;  | 
8810  | 0  |         for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ | 
8811  | 0  |                         l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;  | 
8812  | 0  |         }  | 
8813  |  | 
  | 
8814  | 0  |         if (l_acc_nb_of_tile_part)  | 
8815  | 0  |         { | 
8816  | 0  |             fprintf(out_stream, "\t Tile index: {\n"); | 
8817  |  | 
  | 
8818  | 0  |                     for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ | 
8819  | 0  |                             OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;  | 
8820  |  | 
  | 
8821  | 0  |                             fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);  | 
8822  |  | 
  | 
8823  | 0  |                             if (cstr_index->tile_index[it_tile].tp_index){ | 
8824  | 0  |                                     for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){ | 
8825  | 0  |                                             fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",  | 
8826  | 0  |                                                             it_tile_part,  | 
8827  | 0  |                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,  | 
8828  | 0  |                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,  | 
8829  | 0  |                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);  | 
8830  | 0  |                                     }  | 
8831  | 0  |                             }  | 
8832  |  | 
  | 
8833  | 0  |                             if (cstr_index->tile_index[it_tile].marker){ | 
8834  | 0  |                                     for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){ | 
8835  | 0  |                                             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",  | 
8836  | 0  |                                                             cstr_index->tile_index[it_tile].marker[it_marker].type,  | 
8837  | 0  |                                                             cstr_index->tile_index[it_tile].marker[it_marker].pos,  | 
8838  | 0  |                                                             cstr_index->tile_index[it_tile].marker[it_marker].len );  | 
8839  | 0  |                                     }  | 
8840  | 0  |                             }  | 
8841  | 0  |                     }  | 
8842  | 0  |                     fprintf(out_stream,"\t }\n");  | 
8843  | 0  |         }  | 
8844  | 0  |         }  | 
8845  |  | 
  | 
8846  | 0  |         fprintf(out_stream,"}\n");  | 
8847  |  | 
  | 
8848  | 0  | }  | 
8849  |  |  | 
8850  |  |  | 
8851  |  | void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)  | 
8852  | 0  | { | 
8853  |  | 
  | 
8854  | 0  |         fprintf(out_stream, "Codestream info from main header: {\n"); | 
8855  |  | 
  | 
8856  | 0  |         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);  | 
8857  | 0  |         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);  | 
8858  | 0  |         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);  | 
8859  | 0  |         opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);  | 
8860  | 0  |         fprintf(out_stream, "}\n");  | 
8861  | 0  | }  | 
8862  |  |  | 
8863  |  | void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)  | 
8864  | 0  | { | 
8865  | 0  |         char tab[2];  | 
8866  |  | 
  | 
8867  | 0  |         if (dev_dump_flag){ | 
8868  | 0  |                 fprintf(stdout, "[DEV] Dump an image_header struct {\n"); | 
8869  | 0  |                 tab[0] = '\0';  | 
8870  | 0  |         }  | 
8871  | 0  |         else { | 
8872  | 0  |                 fprintf(out_stream, "Image info {\n"); | 
8873  | 0  |                 tab[0] = '\t';tab[1] = '\0';  | 
8874  | 0  |         }  | 
8875  |  | 
  | 
8876  | 0  |         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);  | 
8877  | 0  |         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);  | 
8878  | 0  |         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);  | 
8879  |  | 
  | 
8880  | 0  |         if (img_header->comps){ | 
8881  | 0  |                 OPJ_UINT32 compno;  | 
8882  | 0  |                 for (compno = 0; compno < img_header->numcomps; compno++) { | 
8883  | 0  |                         fprintf(out_stream, "%s\t component %d {\n", tab, compno); | 
8884  | 0  |                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);  | 
8885  | 0  |                         fprintf(out_stream,"%s}\n",tab);  | 
8886  | 0  |                 }  | 
8887  | 0  |         }  | 
8888  |  | 
  | 
8889  | 0  |         fprintf(out_stream, "}\n");  | 
8890  | 0  | }  | 
8891  |  |  | 
8892  |  | void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)  | 
8893  | 0  | { | 
8894  | 0  |         char tab[3];  | 
8895  |  | 
  | 
8896  | 0  |         if (dev_dump_flag){ | 
8897  | 0  |                 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); | 
8898  | 0  |                 tab[0] = '\0';  | 
8899  | 0  |         }       else { | 
8900  | 0  |                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';  | 
8901  | 0  |         }  | 
8902  |  | 
  | 
8903  | 0  |         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);  | 
8904  | 0  |         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);  | 
8905  | 0  |         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);  | 
8906  |  | 
  | 
8907  | 0  |         if (dev_dump_flag)  | 
8908  | 0  |                 fprintf(out_stream, "}\n");  | 
8909  | 0  | }  | 
8910  |  |  | 
8911  |  | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)  | 
8912  | 0  | { | 
8913  | 0  |         OPJ_UINT32 compno;  | 
8914  | 0  |         OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;  | 
8915  | 0  |         opj_tcp_t *l_default_tile;  | 
8916  | 0  |         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));  | 
8917  | 0  |     if (!cstr_info)  | 
8918  | 0  |       return NULL;  | 
8919  |  |  | 
8920  | 0  |         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;  | 
8921  |  | 
  | 
8922  | 0  |         cstr_info->tx0 = p_j2k->m_cp.tx0;  | 
8923  | 0  |         cstr_info->ty0 = p_j2k->m_cp.ty0;  | 
8924  | 0  |         cstr_info->tdx = p_j2k->m_cp.tdx;  | 
8925  | 0  |         cstr_info->tdy = p_j2k->m_cp.tdy;  | 
8926  | 0  |         cstr_info->tw = p_j2k->m_cp.tw;  | 
8927  | 0  |         cstr_info->th = p_j2k->m_cp.th;  | 
8928  |  | 
  | 
8929  | 0  |         cstr_info->tile_info = NULL; /* Not fill from the main header*/  | 
8930  |  | 
  | 
8931  | 0  |         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;  | 
8932  |  | 
  | 
8933  | 0  |         cstr_info->m_default_tile_info.csty = l_default_tile->csty;  | 
8934  | 0  |         cstr_info->m_default_tile_info.prg = l_default_tile->prg;  | 
8935  | 0  |         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;  | 
8936  | 0  |         cstr_info->m_default_tile_info.mct = l_default_tile->mct;  | 
8937  |  | 
  | 
8938  | 0  |         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));  | 
8939  | 0  |     if (!cstr_info->m_default_tile_info.tccp_info)  | 
8940  | 0  |     { | 
8941  | 0  |       opj_destroy_cstr_info(&cstr_info);  | 
8942  | 0  |       return NULL;  | 
8943  | 0  |     }  | 
8944  |  |  | 
8945  | 0  |         for (compno = 0; compno < numcomps; compno++) { | 
8946  | 0  |                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);  | 
8947  | 0  |                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);  | 
8948  | 0  |                 OPJ_INT32 bandno, numbands;  | 
8949  |  |  | 
8950  |  |                 /* coding style*/  | 
8951  | 0  |                 l_tccp_info->csty = l_tccp->csty;  | 
8952  | 0  |                 l_tccp_info->numresolutions = l_tccp->numresolutions;  | 
8953  | 0  |                 l_tccp_info->cblkw = l_tccp->cblkw;  | 
8954  | 0  |                 l_tccp_info->cblkh = l_tccp->cblkh;  | 
8955  | 0  |                 l_tccp_info->cblksty = l_tccp->cblksty;  | 
8956  | 0  |                 l_tccp_info->qmfbid = l_tccp->qmfbid;  | 
8957  | 0  |                 if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS)  | 
8958  | 0  |                 { | 
8959  | 0  |                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);  | 
8960  | 0  |                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);  | 
8961  | 0  |                 }  | 
8962  |  |  | 
8963  |  |                 /* quantization style*/  | 
8964  | 0  |                 l_tccp_info->qntsty = l_tccp->qntsty;  | 
8965  | 0  |                 l_tccp_info->numgbits = l_tccp->numgbits;  | 
8966  |  | 
  | 
8967  | 0  |                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;  | 
8968  | 0  |                 if (numbands < OPJ_J2K_MAXBANDS) { | 
8969  | 0  |                         for (bandno = 0; bandno < numbands; bandno++) { | 
8970  | 0  |                                 l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant;  | 
8971  | 0  |                                 l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn;  | 
8972  | 0  |                         }  | 
8973  | 0  |                 }  | 
8974  |  |  | 
8975  |  |                 /* RGN value*/  | 
8976  | 0  |                 l_tccp_info->roishift = l_tccp->roishift;  | 
8977  | 0  |         }  | 
8978  |  | 
  | 
8979  | 0  |         return cstr_info;  | 
8980  | 0  | }  | 
8981  |  |  | 
8982  |  | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)  | 
8983  | 0  | { | 
8984  | 0  |         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)  | 
8985  | 0  |                         opj_calloc(1,sizeof(opj_codestream_index_t));  | 
8986  | 0  |         if (!l_cstr_index)  | 
8987  | 0  |                 return NULL;  | 
8988  |  |  | 
8989  | 0  |         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;  | 
8990  | 0  |         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;  | 
8991  | 0  |         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;  | 
8992  |  | 
  | 
8993  | 0  |         l_cstr_index->marknum = p_j2k->cstr_index->marknum;  | 
8994  | 0  |         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));  | 
8995  | 0  |         if (!l_cstr_index->marker){ | 
8996  | 0  |                 opj_free( l_cstr_index);  | 
8997  | 0  |                 return NULL;  | 
8998  | 0  |         }  | 
8999  |  |  | 
9000  | 0  |         if (p_j2k->cstr_index->marker)  | 
9001  | 0  |                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );  | 
9002  | 0  |         else{ | 
9003  | 0  |                 opj_free(l_cstr_index->marker);  | 
9004  | 0  |                 l_cstr_index->marker = NULL;  | 
9005  | 0  |         }  | 
9006  |  | 
  | 
9007  | 0  |         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;  | 
9008  | 0  |         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );  | 
9009  | 0  |         if (!l_cstr_index->tile_index){ | 
9010  | 0  |                 opj_free( l_cstr_index->marker);  | 
9011  | 0  |                 opj_free( l_cstr_index);  | 
9012  | 0  |                 return NULL;  | 
9013  | 0  |         }  | 
9014  |  |  | 
9015  | 0  |         if (!p_j2k->cstr_index->tile_index){ | 
9016  | 0  |                 opj_free(l_cstr_index->tile_index);  | 
9017  | 0  |                 l_cstr_index->tile_index = NULL;  | 
9018  | 0  |         }  | 
9019  | 0  |         else { | 
9020  | 0  |                 OPJ_UINT32 it_tile = 0;  | 
9021  | 0  |                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){ | 
9022  |  |  | 
9023  |  |                         /* Tile Marker*/  | 
9024  | 0  |                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;  | 
9025  |  | 
  | 
9026  | 0  |                         l_cstr_index->tile_index[it_tile].marker =  | 
9027  | 0  |                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));  | 
9028  |  | 
  | 
9029  | 0  |                         if (!l_cstr_index->tile_index[it_tile].marker) { | 
9030  | 0  |                                 OPJ_UINT32 it_tile_free;  | 
9031  |  | 
  | 
9032  | 0  |                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ | 
9033  | 0  |                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);  | 
9034  | 0  |                                 }  | 
9035  |  | 
  | 
9036  | 0  |                                 opj_free( l_cstr_index->tile_index);  | 
9037  | 0  |                                 opj_free( l_cstr_index->marker);  | 
9038  | 0  |                                 opj_free( l_cstr_index);  | 
9039  | 0  |                                 return NULL;  | 
9040  | 0  |                         }  | 
9041  |  |  | 
9042  | 0  |                         if (p_j2k->cstr_index->tile_index[it_tile].marker)  | 
9043  | 0  |                                 memcpy( l_cstr_index->tile_index[it_tile].marker,  | 
9044  | 0  |                                                 p_j2k->cstr_index->tile_index[it_tile].marker,  | 
9045  | 0  |                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );  | 
9046  | 0  |                         else{ | 
9047  | 0  |                                 opj_free(l_cstr_index->tile_index[it_tile].marker);  | 
9048  | 0  |                                 l_cstr_index->tile_index[it_tile].marker = NULL;  | 
9049  | 0  |                         }  | 
9050  |  |  | 
9051  |  |                         /* Tile part index*/  | 
9052  | 0  |                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;  | 
9053  |  | 
  | 
9054  | 0  |                         l_cstr_index->tile_index[it_tile].tp_index =  | 
9055  | 0  |                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));  | 
9056  |  | 
  | 
9057  | 0  |                         if(!l_cstr_index->tile_index[it_tile].tp_index){ | 
9058  | 0  |                                 OPJ_UINT32 it_tile_free;  | 
9059  |  | 
  | 
9060  | 0  |                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ | 
9061  | 0  |                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);  | 
9062  | 0  |                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);  | 
9063  | 0  |                                 }  | 
9064  |  | 
  | 
9065  | 0  |                                 opj_free( l_cstr_index->tile_index);  | 
9066  | 0  |                                 opj_free( l_cstr_index->marker);  | 
9067  | 0  |                                 opj_free( l_cstr_index);  | 
9068  | 0  |                                 return NULL;  | 
9069  | 0  |                         }  | 
9070  |  |  | 
9071  | 0  |                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){ | 
9072  | 0  |                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,  | 
9073  | 0  |                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,  | 
9074  | 0  |                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );  | 
9075  | 0  |                         }  | 
9076  | 0  |                         else{ | 
9077  | 0  |                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);  | 
9078  | 0  |                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;  | 
9079  | 0  |                         }  | 
9080  |  |  | 
9081  |  |                         /* Packet index (NOT USED)*/  | 
9082  | 0  |                         l_cstr_index->tile_index[it_tile].nb_packet = 0;  | 
9083  | 0  |                         l_cstr_index->tile_index[it_tile].packet_index = NULL;  | 
9084  |  | 
  | 
9085  | 0  |                 }  | 
9086  | 0  |         }  | 
9087  |  |  | 
9088  | 0  |         return l_cstr_index;  | 
9089  | 0  | }  | 
9090  |  |  | 
9091  |  | OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)  | 
9092  | 0  | { | 
9093  | 0  |         OPJ_UINT32 it_tile=0;  | 
9094  |  | 
  | 
9095  | 0  |         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;  | 
9096  | 0  |         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));  | 
9097  | 0  |         if (!p_j2k->cstr_index->tile_index)  | 
9098  | 0  |                 return OPJ_FALSE;  | 
9099  |  |  | 
9100  | 0  |         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){ | 
9101  | 0  |                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;  | 
9102  | 0  |                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;  | 
9103  | 0  |                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)  | 
9104  | 0  |                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));  | 
9105  | 0  |                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)  | 
9106  | 0  |                         return OPJ_FALSE;  | 
9107  | 0  |         }  | 
9108  |  |  | 
9109  | 0  |         return OPJ_TRUE;  | 
9110  | 0  | }  | 
9111  |  |  | 
9112  |  | OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,  | 
9113  |  |                                                             opj_stream_private_t *p_stream,  | 
9114  |  |                                                             opj_event_mgr_t * p_manager)  | 
9115  | 0  | { | 
9116  | 0  |         OPJ_BOOL l_go_on = OPJ_TRUE;  | 
9117  | 0  |         OPJ_UINT32 l_current_tile_no;  | 
9118  | 0  |         OPJ_UINT32 l_data_size,l_max_data_size;  | 
9119  | 0  |         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;  | 
9120  | 0  |         OPJ_UINT32 l_nb_comps;  | 
9121  | 0  |         OPJ_BYTE * l_current_data;  | 
9122  | 0  |         OPJ_UINT32 nr_tiles = 0;  | 
9123  |  | 
  | 
9124  | 0  |         l_current_data = (OPJ_BYTE*)opj_malloc(1000);  | 
9125  | 0  |         if (! l_current_data) { | 
9126  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n");  | 
9127  | 0  |                 return OPJ_FALSE;  | 
9128  | 0  |         }  | 
9129  | 0  |         l_max_data_size = 1000;  | 
9130  |  | 
  | 
9131  | 0  |         while (OPJ_TRUE) { | 
9132  | 0  |                 if (! opj_j2k_read_tile_header( p_j2k,  | 
9133  | 0  |                                         &l_current_tile_no,  | 
9134  | 0  |                                         &l_data_size,  | 
9135  | 0  |                                         &l_tile_x0, &l_tile_y0,  | 
9136  | 0  |                                         &l_tile_x1, &l_tile_y1,  | 
9137  | 0  |                                         &l_nb_comps,  | 
9138  | 0  |                                         &l_go_on,  | 
9139  | 0  |                                         p_stream,  | 
9140  | 0  |                                         p_manager)) { | 
9141  | 0  |                         opj_free(l_current_data);  | 
9142  | 0  |                         return OPJ_FALSE;  | 
9143  | 0  |                 }  | 
9144  |  |  | 
9145  | 0  |                 if (! l_go_on) { | 
9146  | 0  |                         break;  | 
9147  | 0  |                 }  | 
9148  |  |  | 
9149  | 0  |                 if (l_data_size > l_max_data_size) { | 
9150  | 0  |                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);  | 
9151  | 0  |                         if (! l_new_current_data) { | 
9152  | 0  |                                 opj_free(l_current_data);  | 
9153  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);  | 
9154  | 0  |                                 return OPJ_FALSE;  | 
9155  | 0  |                         }  | 
9156  | 0  |                         l_current_data = l_new_current_data;  | 
9157  | 0  |                         l_max_data_size = l_data_size;  | 
9158  | 0  |                 }  | 
9159  |  |  | 
9160  | 0  |                 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { | 
9161  | 0  |                         opj_free(l_current_data);  | 
9162  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);  | 
9163  | 0  |                         return OPJ_FALSE;  | 
9164  | 0  |                 }  | 
9165  | 0  |                 opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);  | 
9166  |  | 
  | 
9167  | 0  |                 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { | 
9168  | 0  |                         opj_free(l_current_data);  | 
9169  | 0  |                         return OPJ_FALSE;  | 
9170  | 0  |                 }  | 
9171  | 0  |                 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);  | 
9172  |  |                   | 
9173  | 0  |                 if(opj_stream_get_number_byte_left(p_stream) == 0    | 
9174  | 0  |                     && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)  | 
9175  | 0  |                     break;  | 
9176  | 0  |                 if(++nr_tiles ==  p_j2k->m_cp.th * p_j2k->m_cp.tw)   | 
9177  | 0  |                     break;  | 
9178  | 0  |         }  | 
9179  |  |  | 
9180  | 0  |         opj_free(l_current_data);  | 
9181  |  | 
  | 
9182  | 0  |         return OPJ_TRUE;  | 
9183  | 0  | }  | 
9184  |  |  | 
9185  |  | /**  | 
9186  |  |  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.  | 
9187  |  |  */  | 
9188  |  | static void opj_j2k_setup_decoding (opj_j2k_t *p_j2k)  | 
9189  | 0  | { | 
9190  |  |         /* preconditions*/  | 
9191  | 0  |         assert(p_j2k != 00);  | 
9192  |  | 
  | 
9193  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);  | 
9194  |  |         /* DEVELOPER CORNER, add your custom procedures */  | 
9195  |  | 
  | 
9196  | 0  | }  | 
9197  |  |  | 
9198  |  | /*  | 
9199  |  |  * Read and decode one tile.  | 
9200  |  |  */  | 
9201  |  | static OPJ_BOOL opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,  | 
9202  |  |                                                                             opj_stream_private_t *p_stream,  | 
9203  |  |                                                                             opj_event_mgr_t * p_manager)  | 
9204  | 0  | { | 
9205  | 0  |         OPJ_BOOL l_go_on = OPJ_TRUE;  | 
9206  | 0  |         OPJ_UINT32 l_current_tile_no;  | 
9207  | 0  |         OPJ_UINT32 l_tile_no_to_dec;  | 
9208  | 0  |         OPJ_UINT32 l_data_size,l_max_data_size;  | 
9209  | 0  |         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;  | 
9210  | 0  |         OPJ_UINT32 l_nb_comps;  | 
9211  | 0  |         OPJ_BYTE * l_current_data;  | 
9212  |  | 
  | 
9213  | 0  |         l_current_data = (OPJ_BYTE*)opj_malloc(1000);  | 
9214  | 0  |         if (! l_current_data) { | 
9215  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n");  | 
9216  | 0  |                 return OPJ_FALSE;  | 
9217  | 0  |         }  | 
9218  | 0  |         l_max_data_size = 1000;  | 
9219  |  |  | 
9220  |  |         /*Allocate and initialize some elements of codestrem index if not already done*/  | 
9221  | 0  |         if( !p_j2k->cstr_index->tile_index)  | 
9222  | 0  |         { | 
9223  | 0  |                 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ | 
9224  | 0  |                         opj_free(l_current_data);  | 
9225  | 0  |                         return OPJ_FALSE;  | 
9226  | 0  |                 }  | 
9227  | 0  |         }  | 
9228  |  |         /* Move into the codestream to the first SOT used to decode the desired tile */  | 
9229  | 0  |         l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;  | 
9230  | 0  |         if (p_j2k->cstr_index->tile_index)  | 
9231  | 0  |                 if(p_j2k->cstr_index->tile_index->tp_index)  | 
9232  | 0  |                 { | 
9233  | 0  |                         if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { | 
9234  |  |                                 /* the index for this tile has not been built,  | 
9235  |  |                                  *  so move to the last SOT read */  | 
9236  | 0  |                                 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){ | 
9237  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");  | 
9238  | 0  |                         opj_free(l_current_data);  | 
9239  | 0  |                                         return OPJ_FALSE;  | 
9240  | 0  |                                 }  | 
9241  | 0  |                         }  | 
9242  | 0  |                         else{ | 
9243  | 0  |                                 if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) { | 
9244  | 0  |                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");  | 
9245  | 0  |                         opj_free(l_current_data);  | 
9246  | 0  |                                         return OPJ_FALSE;  | 
9247  | 0  |                                 }  | 
9248  | 0  |                         }  | 
9249  |  |                         /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */  | 
9250  | 0  |                         if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)  | 
9251  | 0  |                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;  | 
9252  | 0  |                 }  | 
9253  |  |  | 
9254  | 0  |         while (OPJ_TRUE) { | 
9255  | 0  |                 if (! opj_j2k_read_tile_header( p_j2k,  | 
9256  | 0  |                                         &l_current_tile_no,  | 
9257  | 0  |                                         &l_data_size,  | 
9258  | 0  |                                         &l_tile_x0, &l_tile_y0,  | 
9259  | 0  |                                         &l_tile_x1, &l_tile_y1,  | 
9260  | 0  |                                         &l_nb_comps,  | 
9261  | 0  |                                         &l_go_on,  | 
9262  | 0  |                                         p_stream,  | 
9263  | 0  |                                         p_manager)) { | 
9264  | 0  |                         opj_free(l_current_data);  | 
9265  | 0  |                         return OPJ_FALSE;  | 
9266  | 0  |                 }  | 
9267  |  |  | 
9268  | 0  |                 if (! l_go_on) { | 
9269  | 0  |                         break;  | 
9270  | 0  |                 }  | 
9271  |  |  | 
9272  | 0  |                 if (l_data_size > l_max_data_size) { | 
9273  | 0  |                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);  | 
9274  | 0  |                         if (! l_new_current_data) { | 
9275  | 0  |                                 opj_free(l_current_data);  | 
9276  | 0  |                                 l_current_data = NULL;  | 
9277  |  |                                 /* TODO: LH: why tile numbering policy used in messages differs from  | 
9278  |  |                                    the one used in opj_j2k_decode_tiles() ? */  | 
9279  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);  | 
9280  | 0  |                                 return OPJ_FALSE;  | 
9281  | 0  |                         }  | 
9282  | 0  |                         l_current_data = l_new_current_data;  | 
9283  | 0  |                         l_max_data_size = l_data_size;  | 
9284  | 0  |                 }  | 
9285  |  |  | 
9286  | 0  |                 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { | 
9287  | 0  |                         opj_free(l_current_data);  | 
9288  | 0  |                         return OPJ_FALSE;  | 
9289  | 0  |                 }  | 
9290  | 0  |                 opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);  | 
9291  |  | 
  | 
9292  | 0  |                 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { | 
9293  | 0  |                         opj_free(l_current_data);  | 
9294  | 0  |                         return OPJ_FALSE;  | 
9295  | 0  |                 }  | 
9296  | 0  |                 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);  | 
9297  |  | 
  | 
9298  | 0  |                 if(l_current_tile_no == l_tile_no_to_dec)  | 
9299  | 0  |                 { | 
9300  |  |                         /* move into the codestream to the the first SOT (FIXME or not move?)*/  | 
9301  | 0  |                         if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) { | 
9302  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");  | 
9303  | 0  |                                 return OPJ_FALSE;  | 
9304  | 0  |                         }  | 
9305  | 0  |                         break;  | 
9306  | 0  |                 }  | 
9307  | 0  |                 else { | 
9308  | 0  |                         opj_event_msg(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);  | 
9309  | 0  |                 }  | 
9310  |  | 
  | 
9311  | 0  |         }  | 
9312  |  |  | 
9313  | 0  |         opj_free(l_current_data);  | 
9314  |  | 
  | 
9315  | 0  |         return OPJ_TRUE;  | 
9316  | 0  | }  | 
9317  |  |  | 
9318  |  | /**  | 
9319  |  |  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.  | 
9320  |  |  */  | 
9321  |  | static void opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k)  | 
9322  | 0  | { | 
9323  |  |         /* preconditions*/  | 
9324  | 0  |         assert(p_j2k != 00);  | 
9325  |  | 
  | 
9326  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);  | 
9327  |  |         /* DEVELOPER CORNER, add your custom procedures */  | 
9328  |  | 
  | 
9329  | 0  | }  | 
9330  |  |  | 
9331  |  | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,  | 
9332  |  |                                                 opj_stream_private_t * p_stream,  | 
9333  |  |                                                 opj_image_t * p_image,  | 
9334  |  |                                                 opj_event_mgr_t * p_manager)  | 
9335  | 0  | { | 
9336  | 0  |         OPJ_UINT32 compno;  | 
9337  |  | 
  | 
9338  | 0  |         if (!p_image)  | 
9339  | 0  |                 return OPJ_FALSE;  | 
9340  |  |  | 
9341  | 0  |         p_j2k->m_output_image = opj_image_create0();  | 
9342  | 0  |         if (! (p_j2k->m_output_image)) { | 
9343  | 0  |                 return OPJ_FALSE;  | 
9344  | 0  |         }  | 
9345  | 0  |         opj_copy_image_header(p_image, p_j2k->m_output_image);  | 
9346  |  |  | 
9347  |  |         /* customization of the decoding */  | 
9348  | 0  |         opj_j2k_setup_decoding(p_j2k);  | 
9349  |  |  | 
9350  |  |         /* Decode the codestream */  | 
9351  | 0  |         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { | 
9352  | 0  |                 opj_image_destroy(p_j2k->m_private_image);  | 
9353  | 0  |                 p_j2k->m_private_image = NULL;  | 
9354  | 0  |                 return OPJ_FALSE;  | 
9355  | 0  |         }  | 
9356  |  |  | 
9357  |  |         /* Move data and copy one information from codec to output image*/  | 
9358  | 0  |         for (compno = 0; compno < p_image->numcomps; compno++) { | 
9359  | 0  |                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;  | 
9360  | 0  |                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;  | 
9361  |  | #if 0  | 
9362  |  |                 char fn[256];  | 
9363  |  |                 sprintf( fn, "/tmp/%d.raw", compno );  | 
9364  |  |                 FILE *debug = fopen( fn, "wb" );  | 
9365  |  |                 fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug );  | 
9366  |  |                 fclose( debug );  | 
9367  |  | #endif  | 
9368  | 0  |                 p_j2k->m_output_image->comps[compno].data = NULL;  | 
9369  | 0  |         }  | 
9370  |  | 
  | 
9371  | 0  |         return OPJ_TRUE;  | 
9372  | 0  | }  | 
9373  |  |  | 
9374  |  | OPJ_BOOL opj_j2k_get_tile(      opj_j2k_t *p_j2k,  | 
9375  |  |                                                     opj_stream_private_t *p_stream,  | 
9376  |  |                                                     opj_image_t* p_image,  | 
9377  |  |                                                     opj_event_mgr_t * p_manager,  | 
9378  |  |                                                     OPJ_UINT32 tile_index )  | 
9379  | 0  | { | 
9380  | 0  |         OPJ_UINT32 compno;  | 
9381  | 0  |         OPJ_UINT32 l_tile_x, l_tile_y;  | 
9382  | 0  |         opj_image_comp_t* l_img_comp;  | 
9383  |  | 
  | 
9384  | 0  |         if (!p_image) { | 
9385  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");  | 
9386  | 0  |                 return OPJ_FALSE;  | 
9387  | 0  |         }  | 
9388  |  |  | 
9389  | 0  |         if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){ | 
9390  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);  | 
9391  | 0  |                 return OPJ_FALSE;  | 
9392  | 0  |         }  | 
9393  |  |  | 
9394  |  |         /* Compute the dimension of the desired tile*/  | 
9395  | 0  |         l_tile_x = tile_index % p_j2k->m_cp.tw;  | 
9396  | 0  |         l_tile_y = tile_index / p_j2k->m_cp.tw;  | 
9397  |  | 
  | 
9398  | 0  |         p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;  | 
9399  | 0  |         if (p_image->x0 < p_j2k->m_private_image->x0)  | 
9400  | 0  |                 p_image->x0 = p_j2k->m_private_image->x0;  | 
9401  | 0  |         p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;  | 
9402  | 0  |         if (p_image->x1 > p_j2k->m_private_image->x1)  | 
9403  | 0  |                 p_image->x1 = p_j2k->m_private_image->x1;  | 
9404  |  | 
  | 
9405  | 0  |         p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;  | 
9406  | 0  |         if (p_image->y0 < p_j2k->m_private_image->y0)  | 
9407  | 0  |                 p_image->y0 = p_j2k->m_private_image->y0;  | 
9408  | 0  |         p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;  | 
9409  | 0  |         if (p_image->y1 > p_j2k->m_private_image->y1)  | 
9410  | 0  |                 p_image->y1 = p_j2k->m_private_image->y1;  | 
9411  |  | 
  | 
9412  | 0  |         l_img_comp = p_image->comps;  | 
9413  | 0  |         for (compno=0; compno < p_image->numcomps; ++compno)  | 
9414  | 0  |         { | 
9415  | 0  |                 OPJ_INT32 l_comp_x1, l_comp_y1;  | 
9416  |  | 
  | 
9417  | 0  |                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;  | 
9418  |  | 
  | 
9419  | 0  |                 l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);  | 
9420  | 0  |                 l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);  | 
9421  | 0  |                 l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);  | 
9422  | 0  |                 l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);  | 
9423  |  | 
  | 
9424  | 0  |                 l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor));  | 
9425  | 0  |                 l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor));  | 
9426  |  | 
  | 
9427  | 0  |                 l_img_comp++;  | 
9428  | 0  |         }  | 
9429  |  |  | 
9430  |  |         /* Destroy the previous output image*/  | 
9431  | 0  |         if (p_j2k->m_output_image)  | 
9432  | 0  |                 opj_image_destroy(p_j2k->m_output_image);  | 
9433  |  |  | 
9434  |  |         /* Create the ouput image from the information previously computed*/  | 
9435  | 0  |         p_j2k->m_output_image = opj_image_create0();  | 
9436  | 0  |         if (! (p_j2k->m_output_image)) { | 
9437  | 0  |                 return OPJ_FALSE;  | 
9438  | 0  |         }  | 
9439  | 0  |         opj_copy_image_header(p_image, p_j2k->m_output_image);  | 
9440  |  | 
  | 
9441  | 0  |         p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;  | 
9442  |  |  | 
9443  |  |         /* customization of the decoding */  | 
9444  | 0  |         opj_j2k_setup_decoding_tile(p_j2k);  | 
9445  |  |  | 
9446  |  |         /* Decode the codestream */  | 
9447  | 0  |         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { | 
9448  | 0  |                 opj_image_destroy(p_j2k->m_private_image);  | 
9449  | 0  |                 p_j2k->m_private_image = NULL;  | 
9450  | 0  |                 return OPJ_FALSE;  | 
9451  | 0  |         }  | 
9452  |  |  | 
9453  |  |         /* Move data and copy one information from codec to output image*/  | 
9454  | 0  |         for (compno = 0; compno < p_image->numcomps; compno++) { | 
9455  | 0  |                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;  | 
9456  |  | 
  | 
9457  | 0  |                 if (p_image->comps[compno].data)  | 
9458  | 0  |                         opj_free(p_image->comps[compno].data);  | 
9459  |  | 
  | 
9460  | 0  |                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;  | 
9461  |  | 
  | 
9462  | 0  |                 p_j2k->m_output_image->comps[compno].data = NULL;  | 
9463  | 0  |         }  | 
9464  |  | 
  | 
9465  | 0  |         return OPJ_TRUE;  | 
9466  | 0  | }  | 
9467  |  |  | 
9468  |  | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,  | 
9469  |  |                                                OPJ_UINT32 res_factor,  | 
9470  |  |                                                opj_event_mgr_t * p_manager)  | 
9471  | 0  | { | 
9472  | 0  |         OPJ_UINT32 it_comp;  | 
9473  |  | 
  | 
9474  | 0  |         p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;  | 
9475  |  | 
  | 
9476  | 0  |         if (p_j2k->m_private_image) { | 
9477  | 0  |                 if (p_j2k->m_private_image->comps) { | 
9478  | 0  |                         if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { | 
9479  | 0  |                                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { | 
9480  | 0  |                                         for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { | 
9481  | 0  |                                                 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;  | 
9482  | 0  |                                                 if ( res_factor >= max_res){ | 
9483  | 0  |                                                         opj_event_msg(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");  | 
9484  | 0  |                                                         return OPJ_FALSE;  | 
9485  | 0  |                                                 }  | 
9486  | 0  |                                                 p_j2k->m_private_image->comps[it_comp].factor = res_factor;  | 
9487  | 0  |                                         }  | 
9488  | 0  |                                         return OPJ_TRUE;  | 
9489  | 0  |                                 }  | 
9490  | 0  |                         }  | 
9491  | 0  |                 }  | 
9492  | 0  |         }  | 
9493  |  |  | 
9494  | 0  |         return OPJ_FALSE;  | 
9495  | 0  | }  | 
9496  |  |  | 
9497  |  | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,  | 
9498  |  |                         opj_stream_private_t *p_stream,  | 
9499  |  |                         opj_event_mgr_t * p_manager )  | 
9500  | 0  | { | 
9501  | 0  |         OPJ_UINT32 i;  | 
9502  | 0  |         OPJ_UINT32 l_nb_tiles;  | 
9503  | 0  |         OPJ_UINT32 l_max_tile_size, l_current_tile_size;  | 
9504  | 0  |         OPJ_BYTE * l_current_data;  | 
9505  |  |  | 
9506  |  |         /* preconditions */  | 
9507  | 0  |         assert(p_j2k != 00);  | 
9508  | 0  |         assert(p_stream != 00);  | 
9509  | 0  |         assert(p_manager != 00);  | 
9510  |  | 
  | 
9511  | 0  |         l_current_data = (OPJ_BYTE*)opj_malloc(1000);  | 
9512  | 0  |         if (! l_current_data) { | 
9513  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");  | 
9514  | 0  |                 return OPJ_FALSE;  | 
9515  | 0  |         }  | 
9516  | 0  |         l_max_tile_size = 1000;  | 
9517  |  | 
  | 
9518  | 0  |         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;  | 
9519  | 0  |         for (i=0;i<l_nb_tiles;++i) { | 
9520  | 0  |                 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) { | 
9521  | 0  |                         opj_free(l_current_data);  | 
9522  | 0  |                         return OPJ_FALSE;  | 
9523  | 0  |                 }  | 
9524  |  |  | 
9525  | 0  |                 l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);  | 
9526  | 0  |                 if (l_current_tile_size > l_max_tile_size) { | 
9527  | 0  |                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);  | 
9528  | 0  |                         if (! l_new_current_data) { | 
9529  | 0  |                                 opj_free(l_current_data);  | 
9530  | 0  |                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");  | 
9531  | 0  |                                 return OPJ_FALSE;  | 
9532  | 0  |                         }  | 
9533  | 0  |                         l_current_data = l_new_current_data;  | 
9534  | 0  |                         l_max_tile_size = l_current_tile_size;  | 
9535  | 0  |                 }  | 
9536  |  |  | 
9537  | 0  |                 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);  | 
9538  |  | 
  | 
9539  | 0  |                 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) { | 
9540  | 0  |                         return OPJ_FALSE;  | 
9541  | 0  |                 }  | 
9542  | 0  |         }  | 
9543  |  |  | 
9544  | 0  |         opj_free(l_current_data);  | 
9545  | 0  |         return OPJ_TRUE;  | 
9546  | 0  | }  | 
9547  |  |  | 
9548  |  | OPJ_BOOL opj_j2k_end_compress(  opj_j2k_t *p_j2k,  | 
9549  |  |                                                         opj_stream_private_t *p_stream,  | 
9550  |  |                                                         opj_event_mgr_t * p_manager)  | 
9551  | 0  | { | 
9552  |  |         /* customization of the encoding */  | 
9553  | 0  |         opj_j2k_setup_end_compress(p_j2k);  | 
9554  |  | 
  | 
9555  | 0  |         if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))  | 
9556  | 0  |         { | 
9557  | 0  |                 return OPJ_FALSE;  | 
9558  | 0  |         }  | 
9559  |  |  | 
9560  | 0  |         return OPJ_TRUE;  | 
9561  | 0  | }  | 
9562  |  |  | 
9563  |  | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,  | 
9564  |  |                                                             opj_stream_private_t *p_stream,  | 
9565  |  |                                                             opj_image_t * p_image,  | 
9566  |  |                                                             opj_event_mgr_t * p_manager)  | 
9567  | 0  | { | 
9568  |  |         /* preconditions */  | 
9569  | 0  |         assert(p_j2k != 00);  | 
9570  | 0  |         assert(p_stream != 00);  | 
9571  | 0  |         assert(p_manager != 00);  | 
9572  |  | 
  | 
9573  | 0  |         p_j2k->m_private_image = opj_image_create0();  | 
9574  | 0  |         opj_copy_image_header(p_image, p_j2k->m_private_image);  | 
9575  |  |  | 
9576  |  |         /* TODO_MSD: Find a better way */  | 
9577  | 0  |         if (p_image->comps) { | 
9578  | 0  |                 OPJ_UINT32 it_comp;  | 
9579  | 0  |                 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { | 
9580  | 0  |                         if (p_image->comps[it_comp].data) { | 
9581  | 0  |                                 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;  | 
9582  | 0  |                                 p_image->comps[it_comp].data = NULL;  | 
9583  |  | 
  | 
9584  | 0  |                         }  | 
9585  | 0  |                 }  | 
9586  | 0  |         }  | 
9587  |  |  | 
9588  |  |         /* customization of the validation */  | 
9589  | 0  |         opj_j2k_setup_encoding_validation (p_j2k);  | 
9590  |  |  | 
9591  |  |         /* validation of the parameters codec */  | 
9592  | 0  |         if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) { | 
9593  | 0  |                 return OPJ_FALSE;  | 
9594  | 0  |         }  | 
9595  |  |  | 
9596  |  |         /* customization of the encoding */  | 
9597  | 0  |         opj_j2k_setup_header_writing(p_j2k);  | 
9598  |  |  | 
9599  |  |         /* write header */  | 
9600  | 0  |         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { | 
9601  | 0  |                 return OPJ_FALSE;  | 
9602  | 0  |         }  | 
9603  |  |  | 
9604  | 0  |         return OPJ_TRUE;  | 
9605  | 0  | }  | 
9606  |  |  | 
9607  |  | OPJ_BOOL opj_j2k_pre_write_tile (       opj_j2k_t * p_j2k,  | 
9608  |  |                                                                 OPJ_UINT32 p_tile_index,  | 
9609  |  |                                                                 opj_stream_private_t *p_stream,  | 
9610  |  |                                                                 opj_event_mgr_t * p_manager )  | 
9611  | 0  | { | 
9612  | 0  |   (void)p_stream;  | 
9613  | 0  |         if (p_tile_index != p_j2k->m_current_tile_number) { | 
9614  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." );  | 
9615  | 0  |                 return OPJ_FALSE;  | 
9616  | 0  |         }  | 
9617  |  |  | 
9618  | 0  |         opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);  | 
9619  |  | 
  | 
9620  | 0  |         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;  | 
9621  | 0  |         p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;  | 
9622  | 0  |         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;  | 
9623  |  |  | 
9624  |  |         /* initialisation before tile encoding  */  | 
9625  | 0  |         if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { | 
9626  | 0  |                 return OPJ_FALSE;  | 
9627  | 0  |         }  | 
9628  |  |  | 
9629  | 0  |         return OPJ_TRUE;  | 
9630  | 0  | }  | 
9631  |  |  | 
9632  |  | void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)  | 
9633  | 0  | { | 
9634  | 0  |         OPJ_UINT32 i,j,k = 0;  | 
9635  | 0  |         OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;  | 
9636  | 0  |         opj_image_comp_t * l_img_comp = 00;  | 
9637  | 0  |         opj_tcd_tilecomp_t * l_tilec = 00;  | 
9638  | 0  |         opj_image_t * l_image = 00;  | 
9639  | 0  |         OPJ_UINT32 l_size_comp, l_remaining;  | 
9640  | 0  |         OPJ_INT32 * l_src_ptr;  | 
9641  | 0  |         l_tilec = p_tcd->tcd_image->tiles->comps;  | 
9642  | 0  |         l_image = p_tcd->image;  | 
9643  | 0  |         l_img_comp = l_image->comps;  | 
9644  |  | 
  | 
9645  | 0  |         for (i=0;i<p_tcd->image->numcomps;++i) { | 
9646  | 0  |                 l_size_comp = l_img_comp->prec >> 3; /* (/8) */  | 
9647  | 0  |                 l_remaining = l_img_comp->prec & 7;  /* (%8) */  | 
9648  | 0  |                 if (l_remaining) { | 
9649  | 0  |                         ++l_size_comp;  | 
9650  | 0  |                 }  | 
9651  |  | 
  | 
9652  | 0  |                 if (l_size_comp == 3) { | 
9653  | 0  |                         l_size_comp = 4;  | 
9654  | 0  |                 }  | 
9655  |  | 
  | 
9656  | 0  |                 l_width  = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);  | 
9657  | 0  |                 l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);  | 
9658  | 0  |                 l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);  | 
9659  | 0  |                 l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, (OPJ_INT32)l_img_comp->dy);  | 
9660  | 0  |                 l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);  | 
9661  | 0  |                 l_stride = l_image_width - l_width;  | 
9662  | 0  |                 l_src_ptr = l_img_comp->data + ((OPJ_UINT32)l_tilec->x0 - l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - l_offset_y) * l_image_width;  | 
9663  |  | 
  | 
9664  | 0  |                 switch (l_size_comp) { | 
9665  | 0  |                         case 1:  | 
9666  | 0  |                                 { | 
9667  | 0  |                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;  | 
9668  | 0  |                                         if (l_img_comp->sgnd) { | 
9669  | 0  |                                                 for     (j=0;j<l_height;++j) { | 
9670  | 0  |                                                         for (k=0;k<l_width;++k) { | 
9671  | 0  |                                                                 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);  | 
9672  | 0  |                                                                 ++l_dest_ptr;  | 
9673  | 0  |                                                                 ++l_src_ptr;  | 
9674  | 0  |                                                         }  | 
9675  | 0  |                                                         l_src_ptr += l_stride;  | 
9676  | 0  |                                                 }  | 
9677  | 0  |                                         }  | 
9678  | 0  |                                         else { | 
9679  | 0  |                                                 for (j=0;j<l_height;++j) { | 
9680  | 0  |                                                         for (k=0;k<l_width;++k) { | 
9681  | 0  |                                                                 *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr)&0xff);  | 
9682  | 0  |                                                                 ++l_dest_ptr;  | 
9683  | 0  |                                                                 ++l_src_ptr;  | 
9684  | 0  |                                                         }  | 
9685  | 0  |                                                         l_src_ptr += l_stride;  | 
9686  | 0  |                                                 }  | 
9687  | 0  |                                         }  | 
9688  |  | 
  | 
9689  | 0  |                                         p_data = (OPJ_BYTE*) l_dest_ptr;  | 
9690  | 0  |                                 }  | 
9691  | 0  |                                 break;  | 
9692  | 0  |                         case 2:  | 
9693  | 0  |                                 { | 
9694  | 0  |                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;  | 
9695  | 0  |                                         if (l_img_comp->sgnd) { | 
9696  | 0  |                                                 for (j=0;j<l_height;++j) { | 
9697  | 0  |                                                         for (k=0;k<l_width;++k) { | 
9698  | 0  |                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));  | 
9699  | 0  |                                                         }  | 
9700  | 0  |                                                         l_src_ptr += l_stride;  | 
9701  | 0  |                                                 }  | 
9702  | 0  |                                         }  | 
9703  | 0  |                                         else { | 
9704  | 0  |                                                 for (j=0;j<l_height;++j) { | 
9705  | 0  |                                                         for (k=0;k<l_width;++k) { | 
9706  | 0  |                                                                 *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);  | 
9707  | 0  |                                                         }  | 
9708  | 0  |                                                         l_src_ptr += l_stride;  | 
9709  | 0  |                                                 }  | 
9710  | 0  |                                         }  | 
9711  |  | 
  | 
9712  | 0  |                                         p_data = (OPJ_BYTE*) l_dest_ptr;  | 
9713  | 0  |                                 }  | 
9714  | 0  |                                 break;  | 
9715  | 0  |                         case 4:  | 
9716  | 0  |                                 { | 
9717  | 0  |                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;  | 
9718  | 0  |                                         for (j=0;j<l_height;++j) { | 
9719  | 0  |                                                 for (k=0;k<l_width;++k) { | 
9720  | 0  |                                                         *(l_dest_ptr++) = *(l_src_ptr++);  | 
9721  | 0  |                                                 }  | 
9722  | 0  |                                                 l_src_ptr += l_stride;  | 
9723  | 0  |                                         }  | 
9724  |  | 
  | 
9725  | 0  |                                         p_data = (OPJ_BYTE*) l_dest_ptr;  | 
9726  | 0  |                                 }  | 
9727  | 0  |                                 break;  | 
9728  | 0  |                 }  | 
9729  |  |  | 
9730  | 0  |                 ++l_img_comp;  | 
9731  | 0  |                 ++l_tilec;  | 
9732  | 0  |         }  | 
9733  | 0  | }  | 
9734  |  |  | 
9735  |  | OPJ_BOOL opj_j2k_post_write_tile (      opj_j2k_t * p_j2k,  | 
9736  |  |                                                                 OPJ_BYTE * p_data,  | 
9737  |  |                                                                 OPJ_UINT32 p_data_size,  | 
9738  |  |                                                                 opj_stream_private_t *p_stream,  | 
9739  |  |                                                                 opj_event_mgr_t * p_manager )  | 
9740  | 0  | { | 
9741  | 0  |         opj_tcd_t * l_tcd = 00;  | 
9742  | 0  |         OPJ_UINT32 l_nb_bytes_written;  | 
9743  | 0  |         OPJ_BYTE * l_current_data = 00;  | 
9744  | 0  |         OPJ_UINT32 l_tile_size = 0;  | 
9745  | 0  |         OPJ_UINT32 l_available_data;  | 
9746  |  |  | 
9747  |  |         /* preconditions */  | 
9748  | 0  |         assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);  | 
9749  |  | 
  | 
9750  | 0  |         l_tcd = p_j2k->m_tcd;  | 
9751  |  |           | 
9752  | 0  |         l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;  | 
9753  | 0  |         l_available_data = l_tile_size;  | 
9754  | 0  |         l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;  | 
9755  |  | 
  | 
9756  | 0  |         if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) { | 
9757  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );  | 
9758  | 0  |                 return OPJ_FALSE;  | 
9759  | 0  |         }  | 
9760  |  |  | 
9761  | 0  |         l_nb_bytes_written = 0;  | 
9762  | 0  |         if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { | 
9763  | 0  |                 return OPJ_FALSE;  | 
9764  | 0  |         }  | 
9765  | 0  |         l_current_data += l_nb_bytes_written;  | 
9766  | 0  |         l_available_data -= l_nb_bytes_written;  | 
9767  |  | 
  | 
9768  | 0  |         l_nb_bytes_written = 0;  | 
9769  | 0  |         if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { | 
9770  | 0  |                 return OPJ_FALSE;  | 
9771  | 0  |         }  | 
9772  |  |  | 
9773  | 0  |         l_available_data -= l_nb_bytes_written;  | 
9774  | 0  |         l_nb_bytes_written = l_tile_size - l_available_data;  | 
9775  |  | 
  | 
9776  | 0  |         if ( opj_stream_write_data(     p_stream,  | 
9777  | 0  |                                                                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,  | 
9778  | 0  |                                                                 l_nb_bytes_written,p_manager) != l_nb_bytes_written) { | 
9779  | 0  |                 return OPJ_FALSE;  | 
9780  | 0  |         }  | 
9781  |  |  | 
9782  | 0  |         ++p_j2k->m_current_tile_number;  | 
9783  |  | 
  | 
9784  | 0  |         return OPJ_TRUE;  | 
9785  | 0  | }  | 
9786  |  |  | 
9787  |  | void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k)  | 
9788  | 0  | { | 
9789  |  |         /* preconditions */  | 
9790  | 0  |         assert(p_j2k != 00);  | 
9791  |  |  | 
9792  |  |         /* DEVELOPER CORNER, insert your custom procedures */  | 
9793  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );  | 
9794  |  | 
  | 
9795  | 0  |         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { | 
9796  | 0  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);  | 
9797  | 0  |         }  | 
9798  |  | 
  | 
9799  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );  | 
9800  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );  | 
9801  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);  | 
9802  | 0  | }  | 
9803  |  |  | 
9804  |  | void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k)  | 
9805  | 0  | { | 
9806  |  |         /* preconditions */  | 
9807  | 0  |         assert(p_j2k != 00);  | 
9808  |  | 
  | 
9809  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);  | 
9810  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);  | 
9811  |  |  | 
9812  |  |         /* DEVELOPER CORNER, add your custom validation procedure */  | 
9813  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);  | 
9814  | 0  | }  | 
9815  |  |  | 
9816  |  | void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)  | 
9817  | 0  | { | 
9818  |  |         /* preconditions */  | 
9819  | 0  |         assert(p_j2k != 00);  | 
9820  |  | 
  | 
9821  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );  | 
9822  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );  | 
9823  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );  | 
9824  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );  | 
9825  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );  | 
9826  |  | 
  | 
9827  | 0  |         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { | 
9828  |  |                 /* No need for COC or QCC, QCD and COD are used  | 
9829  |  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc );  | 
9830  |  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc );  | 
9831  |  |                 */  | 
9832  | 0  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );  | 
9833  |  | 
  | 
9834  | 0  |                 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24) { | 
9835  | 0  |                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );  | 
9836  | 0  |                 }  | 
9837  | 0  |         }  | 
9838  |  | 
  | 
9839  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);  | 
9840  |  | 
  | 
9841  | 0  |         if (p_j2k->m_cp.comment != 00)  { | 
9842  | 0  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);  | 
9843  | 0  |         }  | 
9844  |  |  | 
9845  |  |         /* DEVELOPER CORNER, insert your custom procedures */  | 
9846  | 0  |         if (p_j2k->m_cp.rsiz & OPJ_MCT) { | 
9847  | 0  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );  | 
9848  | 0  |         }  | 
9849  |  |         /* End of Developer Corner */  | 
9850  |  | 
  | 
9851  | 0  |         if (p_j2k->cstr_index) { | 
9852  | 0  |                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );  | 
9853  | 0  |         }  | 
9854  |  | 
  | 
9855  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);  | 
9856  | 0  |         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);  | 
9857  | 0  | }  | 
9858  |  |  | 
9859  |  | OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,  | 
9860  |  |                                                                         OPJ_BYTE * p_data,  | 
9861  |  |                                                                         OPJ_UINT32 * p_data_written,  | 
9862  |  |                                                                         OPJ_UINT32 p_total_data_size,  | 
9863  |  |                                                                         opj_stream_private_t *p_stream,  | 
9864  |  |                                                                         struct opj_event_mgr * p_manager )  | 
9865  | 0  | { | 
9866  | 0  |         OPJ_UINT32 l_nb_bytes_written = 0;  | 
9867  | 0  |         OPJ_UINT32 l_current_nb_bytes_written;  | 
9868  | 0  |         OPJ_BYTE * l_begin_data = 00;  | 
9869  |  | 
  | 
9870  | 0  |         opj_tcd_t * l_tcd = 00;  | 
9871  | 0  |         opj_cp_t * l_cp = 00;  | 
9872  |  | 
  | 
9873  | 0  |         l_tcd = p_j2k->m_tcd;  | 
9874  | 0  |         l_cp = &(p_j2k->m_cp);  | 
9875  |  | 
  | 
9876  | 0  |         l_tcd->cur_pino = 0;  | 
9877  |  |  | 
9878  |  |         /*Get number of tile parts*/  | 
9879  | 0  |         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;  | 
9880  |  |  | 
9881  |  |         /* INDEX >> */  | 
9882  |  |         /* << INDEX */  | 
9883  |  | 
  | 
9884  | 0  |         l_current_nb_bytes_written = 0;  | 
9885  | 0  |         l_begin_data = p_data;  | 
9886  | 0  |         if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))  | 
9887  | 0  |         { | 
9888  | 0  |                 return OPJ_FALSE;  | 
9889  | 0  |         }  | 
9890  |  |  | 
9891  | 0  |         l_nb_bytes_written += l_current_nb_bytes_written;  | 
9892  | 0  |         p_data += l_current_nb_bytes_written;  | 
9893  | 0  |         p_total_data_size -= l_current_nb_bytes_written;  | 
9894  |  | 
  | 
9895  | 0  |         if (l_cp->m_specific_param.m_enc.m_cinema == 0) { | 
9896  |  | #if 0  | 
9897  |  |                 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { | 
9898  |  |                         l_current_nb_bytes_written = 0;  | 
9899  |  |                         opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);  | 
9900  |  |                         l_nb_bytes_written += l_current_nb_bytes_written;  | 
9901  |  |                         p_data += l_current_nb_bytes_written;  | 
9902  |  |                         p_total_data_size -= l_current_nb_bytes_written;  | 
9903  |  |  | 
9904  |  |                         l_current_nb_bytes_written = 0;  | 
9905  |  |                         opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);  | 
9906  |  |                         l_nb_bytes_written += l_current_nb_bytes_written;  | 
9907  |  |                         p_data += l_current_nb_bytes_written;  | 
9908  |  |                         p_total_data_size -= l_current_nb_bytes_written;  | 
9909  |  |                 }  | 
9910  |  | #endif  | 
9911  |  | 
  | 
9912  | 0  |                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { | 
9913  | 0  |                         l_current_nb_bytes_written = 0;  | 
9914  | 0  |                         opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);  | 
9915  | 0  |                         l_nb_bytes_written += l_current_nb_bytes_written;  | 
9916  | 0  |                         p_data += l_current_nb_bytes_written;  | 
9917  | 0  |                         p_total_data_size -= l_current_nb_bytes_written;  | 
9918  | 0  |                 }  | 
9919  | 0  |         }  | 
9920  |  | 
  | 
9921  | 0  |         l_current_nb_bytes_written = 0;  | 
9922  | 0  |         if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { | 
9923  | 0  |                 return OPJ_FALSE;  | 
9924  | 0  |         }  | 
9925  |  |  | 
9926  | 0  |         l_nb_bytes_written += l_current_nb_bytes_written;  | 
9927  | 0  |         * p_data_written = l_nb_bytes_written;  | 
9928  |  |  | 
9929  |  |         /* Writing Psot in SOT marker */  | 
9930  | 0  |         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */  | 
9931  |  | 
  | 
9932  | 0  |         if (l_cp->m_specific_param.m_enc.m_cinema){ | 
9933  | 0  |                 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);  | 
9934  | 0  |         }  | 
9935  |  | 
  | 
9936  | 0  |         return OPJ_TRUE;  | 
9937  | 0  | }  | 
9938  |  |  | 
9939  |  | OPJ_BOOL opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,  | 
9940  |  |                                                                         OPJ_BYTE * p_data,  | 
9941  |  |                                                                         OPJ_UINT32 * p_data_written,  | 
9942  |  |                                                                         OPJ_UINT32 p_total_data_size,  | 
9943  |  |                                                                         opj_stream_private_t *p_stream,  | 
9944  |  |                                                                         struct opj_event_mgr * p_manager  | 
9945  |  |                                                                 )  | 
9946  | 0  | { | 
9947  | 0  |         OPJ_UINT32 tilepartno=0;  | 
9948  | 0  |         OPJ_UINT32 l_nb_bytes_written = 0;  | 
9949  | 0  |         OPJ_UINT32 l_current_nb_bytes_written;  | 
9950  | 0  |         OPJ_UINT32 l_part_tile_size;  | 
9951  | 0  |         OPJ_UINT32 tot_num_tp;  | 
9952  | 0  |         OPJ_UINT32 pino;  | 
9953  |  | 
  | 
9954  | 0  |         OPJ_BYTE * l_begin_data;  | 
9955  | 0  |         opj_tcp_t *l_tcp = 00;  | 
9956  | 0  |         opj_tcd_t * l_tcd = 00;  | 
9957  | 0  |         opj_cp_t * l_cp = 00;  | 
9958  |  | 
  | 
9959  | 0  |         l_tcd = p_j2k->m_tcd;  | 
9960  | 0  |         l_cp = &(p_j2k->m_cp);  | 
9961  | 0  |         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;  | 
9962  |  |  | 
9963  |  |         /*Get number of tile parts*/  | 
9964  | 0  |         tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);  | 
9965  |  |  | 
9966  |  |         /* start writing remaining tile parts */  | 
9967  | 0  |         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;  | 
9968  | 0  |         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { | 
9969  | 0  |                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;  | 
9970  | 0  |                 l_current_nb_bytes_written = 0;  | 
9971  | 0  |                 l_part_tile_size = 0;  | 
9972  | 0  |                 l_begin_data = p_data;  | 
9973  |  | 
  | 
9974  | 0  |                 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { | 
9975  | 0  |                         return OPJ_FALSE;  | 
9976  | 0  |                 }  | 
9977  |  |  | 
9978  | 0  |                 l_nb_bytes_written += l_current_nb_bytes_written;  | 
9979  | 0  |                 p_data += l_current_nb_bytes_written;  | 
9980  | 0  |                 p_total_data_size -= l_current_nb_bytes_written;  | 
9981  | 0  |                 l_part_tile_size += l_current_nb_bytes_written;  | 
9982  |  | 
  | 
9983  | 0  |                 l_current_nb_bytes_written = 0;  | 
9984  | 0  |                 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { | 
9985  | 0  |                         return OPJ_FALSE;  | 
9986  | 0  |                 }  | 
9987  |  |  | 
9988  | 0  |                 p_data += l_current_nb_bytes_written;  | 
9989  | 0  |                 l_nb_bytes_written += l_current_nb_bytes_written;  | 
9990  | 0  |                 p_total_data_size -= l_current_nb_bytes_written;  | 
9991  | 0  |                 l_part_tile_size += l_current_nb_bytes_written;  | 
9992  |  |  | 
9993  |  |                 /* Writing Psot in SOT marker */  | 
9994  | 0  |                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */  | 
9995  |  | 
  | 
9996  | 0  |                 if (l_cp->m_specific_param.m_enc.m_cinema) { | 
9997  | 0  |                         opj_j2k_update_tlm(p_j2k,l_part_tile_size);  | 
9998  | 0  |                 }  | 
9999  |  | 
  | 
10000  | 0  |                 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;  | 
10001  | 0  |         }  | 
10002  |  |  | 
10003  | 0  |         for (pino = 1; pino <= l_tcp->numpocs; ++pino) { | 
10004  | 0  |                 l_tcd->cur_pino = pino;  | 
10005  |  |  | 
10006  |  |                 /*Get number of tile parts*/  | 
10007  | 0  |                 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);  | 
10008  | 0  |                 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { | 
10009  | 0  |                         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;  | 
10010  | 0  |                         l_current_nb_bytes_written = 0;  | 
10011  | 0  |                         l_part_tile_size = 0;  | 
10012  | 0  |                         l_begin_data = p_data;  | 
10013  |  | 
  | 
10014  | 0  |                         if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { | 
10015  | 0  |                                 return OPJ_FALSE;  | 
10016  | 0  |                         }  | 
10017  |  |  | 
10018  | 0  |                         l_nb_bytes_written += l_current_nb_bytes_written;  | 
10019  | 0  |                         p_data += l_current_nb_bytes_written;  | 
10020  | 0  |                         p_total_data_size -= l_current_nb_bytes_written;  | 
10021  | 0  |                         l_part_tile_size += l_current_nb_bytes_written;  | 
10022  |  | 
  | 
10023  | 0  |                         l_current_nb_bytes_written = 0;  | 
10024  |  | 
  | 
10025  | 0  |                         if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { | 
10026  | 0  |                                 return OPJ_FALSE;  | 
10027  | 0  |                         }  | 
10028  |  |  | 
10029  | 0  |                         l_nb_bytes_written += l_current_nb_bytes_written;  | 
10030  | 0  |                         p_data += l_current_nb_bytes_written;  | 
10031  | 0  |                         p_total_data_size -= l_current_nb_bytes_written;  | 
10032  | 0  |                         l_part_tile_size += l_current_nb_bytes_written;  | 
10033  |  |  | 
10034  |  |                         /* Writing Psot in SOT marker */  | 
10035  | 0  |                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */  | 
10036  |  | 
  | 
10037  | 0  |                         if (l_cp->m_specific_param.m_enc.m_cinema) { | 
10038  | 0  |                                 opj_j2k_update_tlm(p_j2k,l_part_tile_size);  | 
10039  | 0  |                         }  | 
10040  |  | 
  | 
10041  | 0  |                         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;  | 
10042  | 0  |                 }  | 
10043  | 0  |         }  | 
10044  |  |  | 
10045  | 0  |         *p_data_written = l_nb_bytes_written;  | 
10046  |  | 
  | 
10047  | 0  |         return OPJ_TRUE;  | 
10048  | 0  | }  | 
10049  |  |  | 
10050  |  | OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,  | 
10051  |  |                                                                     struct opj_stream_private *p_stream,  | 
10052  |  |                                                                     struct opj_event_mgr * p_manager )  | 
10053  | 0  | { | 
10054  | 0  |         OPJ_UINT32 l_tlm_size;  | 
10055  | 0  |         OPJ_OFF_T l_tlm_position, l_current_position;  | 
10056  |  |  | 
10057  |  |         /* preconditions */  | 
10058  | 0  |         assert(p_j2k != 00);  | 
10059  | 0  |         assert(p_manager != 00);  | 
10060  | 0  |         assert(p_stream != 00);  | 
10061  |  | 
  | 
10062  | 0  |         l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;  | 
10063  | 0  |         l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;  | 
10064  | 0  |         l_current_position = opj_stream_tell(p_stream);  | 
10065  |  | 
  | 
10066  | 0  |         if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) { | 
10067  | 0  |                 return OPJ_FALSE;  | 
10068  | 0  |         }  | 
10069  |  |  | 
10070  | 0  |         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) { | 
10071  | 0  |                 return OPJ_FALSE;  | 
10072  | 0  |         }  | 
10073  |  |  | 
10074  | 0  |         if (! opj_stream_seek(p_stream,l_current_position,p_manager)) { | 
10075  | 0  |                 return OPJ_FALSE;  | 
10076  | 0  |         }  | 
10077  |  |  | 
10078  | 0  |         return OPJ_TRUE;  | 
10079  | 0  | }  | 
10080  |  |  | 
10081  |  | OPJ_BOOL opj_j2k_end_encoding(  opj_j2k_t *p_j2k,  | 
10082  |  |                                                         struct opj_stream_private *p_stream,  | 
10083  |  |                                                         struct opj_event_mgr * p_manager )  | 
10084  | 0  | { | 
10085  |  |         /* preconditions */  | 
10086  | 0  |         assert(p_j2k != 00);  | 
10087  | 0  |         assert(p_manager != 00);  | 
10088  | 0  |         assert(p_stream != 00);  | 
10089  |  | 
  | 
10090  | 0  |         opj_tcd_destroy(p_j2k->m_tcd);  | 
10091  | 0  |         p_j2k->m_tcd = 00;  | 
10092  |  | 
  | 
10093  | 0  |         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { | 
10094  | 0  |                 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);  | 
10095  | 0  |                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;  | 
10096  | 0  |                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;  | 
10097  | 0  |         }  | 
10098  |  | 
  | 
10099  | 0  |         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { | 
10100  | 0  |                 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);  | 
10101  | 0  |                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;  | 
10102  | 0  |         }  | 
10103  |  | 
  | 
10104  | 0  |         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;  | 
10105  |  | 
  | 
10106  | 0  |         return OPJ_TRUE;  | 
10107  | 0  | }  | 
10108  |  |  | 
10109  |  | /**  | 
10110  |  |  * Destroys the memory associated with the decoding of headers.  | 
10111  |  |  */  | 
10112  |  | static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,  | 
10113  |  |                                                 opj_stream_private_t *p_stream,  | 
10114  |  |                                                 opj_event_mgr_t * p_manager  | 
10115  |  |                                                 )  | 
10116  | 0  | { | 
10117  |  |         /* preconditions */  | 
10118  | 0  |         assert(p_j2k != 00);  | 
10119  | 0  |         assert(p_stream != 00);  | 
10120  | 0  |         assert(p_manager != 00);  | 
10121  |  | 
  | 
10122  | 0  |         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { | 
10123  | 0  |                 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);  | 
10124  | 0  |                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;  | 
10125  | 0  |         }  | 
10126  |  | 
  | 
10127  | 0  |         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;  | 
10128  |  | 
  | 
10129  | 0  |         return OPJ_TRUE;  | 
10130  | 0  | }  | 
10131  |  |  | 
10132  |  | OPJ_BOOL opj_j2k_init_info(     opj_j2k_t *p_j2k,  | 
10133  |  |                                                 struct opj_stream_private *p_stream,  | 
10134  |  |                                                 struct opj_event_mgr * p_manager )  | 
10135  | 0  | { | 
10136  | 0  |         opj_codestream_info_t * l_cstr_info = 00;  | 
10137  |  |  | 
10138  |  |         /* preconditions */  | 
10139  | 0  |         assert(p_j2k != 00);  | 
10140  | 0  |         assert(p_manager != 00);  | 
10141  | 0  |         assert(p_stream != 00);  | 
10142  | 0  |   (void)l_cstr_info;  | 
10143  |  |  | 
10144  |  |         /* TODO mergeV2: check this part which use cstr_info */  | 
10145  |  |         /*l_cstr_info = p_j2k->cstr_info;  | 
10146  |  |  | 
10147  |  |         if (l_cstr_info)  { | 
10148  |  |                 OPJ_UINT32 compno;  | 
10149  |  |                 l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));  | 
10150  |  |  | 
10151  |  |                 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;  | 
10152  |  |                 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;  | 
10153  |  |  | 
10154  |  |                 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;  | 
10155  |  |  | 
10156  |  |                 l_cstr_info->tw = p_j2k->m_cp.tw;  | 
10157  |  |                 l_cstr_info->th = p_j2k->m_cp.th;  | 
10158  |  |  | 
10159  |  |                 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */  | 
10160  |  |                 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */  | 
10161  |  |                 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */  | 
10162  |  |                 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */  | 
10163  |  |  | 
10164  |  |                 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;  | 
10165  |  |  | 
10166  |  |                 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;  | 
10167  |  |  | 
10168  |  |                 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));  | 
10169  |  |  | 
10170  |  |                 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { | 
10171  |  |                         l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;  | 
10172  |  |                 }  | 
10173  |  |  | 
10174  |  |                 l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */  | 
10175  |  |  | 
10176  |  |                 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */  | 
10177  |  |  | 
10178  |  |                 /*l_cstr_info->maxmarknum = 100;  | 
10179  |  |                 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));  | 
10180  |  |                 l_cstr_info->marknum = 0;  | 
10181  |  |         }*/  | 
10182  |  | 
  | 
10183  | 0  |         return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);  | 
10184  | 0  | }  | 
10185  |  |  | 
10186  |  | /**  | 
10187  |  |  * Creates a tile-coder decoder.  | 
10188  |  |  *  | 
10189  |  |  * @param       p_stream                the stream to write data to.  | 
10190  |  |  * @param       p_j2k                   J2K codec.  | 
10191  |  |  * @param       p_manager               the user event manager.  | 
10192  |  | */  | 
10193  |  | static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,  | 
10194  |  |                                                                     opj_stream_private_t *p_stream,  | 
10195  |  |                                                                     opj_event_mgr_t * p_manager  | 
10196  |  |                                     )  | 
10197  | 0  | { | 
10198  |  |         /* preconditions */  | 
10199  | 0  |         assert(p_j2k != 00);  | 
10200  | 0  |         assert(p_manager != 00);  | 
10201  | 0  |         assert(p_stream != 00);  | 
10202  |  | 
  | 
10203  | 0  |         p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);  | 
10204  |  | 
  | 
10205  | 0  |         if (! p_j2k->m_tcd) { | 
10206  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");  | 
10207  | 0  |                 return OPJ_FALSE;  | 
10208  | 0  |         }  | 
10209  |  |  | 
10210  | 0  |         if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) { | 
10211  | 0  |                 opj_tcd_destroy(p_j2k->m_tcd);  | 
10212  | 0  |                 p_j2k->m_tcd = 00;  | 
10213  | 0  |                 return OPJ_FALSE;  | 
10214  | 0  |         }  | 
10215  |  |  | 
10216  | 0  |         return OPJ_TRUE;  | 
10217  | 0  | }  | 
10218  |  |  | 
10219  |  | OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k,  | 
10220  |  |                                                  OPJ_UINT32 p_tile_index,  | 
10221  |  |                                                  OPJ_BYTE * p_data,  | 
10222  |  |                                                  OPJ_UINT32 p_data_size,  | 
10223  |  |                                                  opj_stream_private_t *p_stream,  | 
10224  |  |                                                  opj_event_mgr_t * p_manager )  | 
10225  | 0  | { | 
10226  | 0  |         if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) { | 
10227  | 0  |                 opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);  | 
10228  | 0  |                 return OPJ_FALSE;  | 
10229  | 0  |         }  | 
10230  | 0  |         else { | 
10231  | 0  |                 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) { | 
10232  | 0  |                         opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);  | 
10233  | 0  |                         return OPJ_FALSE;  | 
10234  | 0  |                 }  | 
10235  | 0  |         }  | 
10236  |  |  | 
10237  | 0  |         return OPJ_TRUE;  | 
10238  | 0  | }  |