1 /*
2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
15 * Copyright (c) 2006-2007, Parvatha Elangovan
16 * Copyright (c) 2010-2011, Kaori Hagihara
17 * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
18 * Copyright (c) 2012, CS Systemes d'Information, France
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #include "opj_includes.h"
44
45 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
46 /*@{*/
47
48 /** @name Local static functions */
49 /*@{*/
50
51 /**
52 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
53 */
54 static OPJ_BOOL opj_j2k_setup_header_reading (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager);
55
56 /**
57 * The read header procedure.
58 */
59 static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
60 opj_stream_private_t *p_stream,
61 opj_event_mgr_t * p_manager);
62
63 /**
64 * The default encoding validation procedure without any extension.
65 *
66 * @param p_j2k the jpeg2000 codec to validate.
67 * @param p_stream the input stream to validate.
68 * @param p_manager the user event manager.
69 *
70 * @return true if the parameters are correct.
71 */
72 static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k,
73 opj_stream_private_t *p_stream,
74 opj_event_mgr_t * p_manager );
75
76 /**
77 * The default decoding validation procedure without any extension.
78 *
79 * @param p_j2k the jpeg2000 codec to validate.
80 * @param p_stream the input stream to validate.
81 * @param p_manager the user event manager.
82 *
83 * @return true if the parameters are correct.
84 */
85 static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t * p_j2k,
86 opj_stream_private_t *p_stream,
87 opj_event_mgr_t * p_manager );
88
89 /**
90 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
91 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 */
93 static OPJ_BOOL opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager);
94
95 /**
96 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
97 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 */
99 static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager);
100
101 /**
102 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
103 * are valid. Developpers wanting to extend the library can add their own validation procedures.
104 */
105 static OPJ_BOOL opj_j2k_setup_end_compress (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager);
106
107 /**
108 * The mct encoding validation procedure.
109 *
110 * @param p_j2k the jpeg2000 codec to validate.
111 * @param p_stream the input stream to validate.
112 * @param p_manager the user event manager.
113 *
114 * @return true if the parameters are correct.
115 */
116 static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
119
120 /**
121 * Builds the tcd decoder to use to decode tile.
122 */
123 static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
124 opj_stream_private_t *p_stream,
125 opj_event_mgr_t * p_manager );
126 /**
127 * Builds the tcd encoder to use to encode tile.
128 */
129 static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
130 opj_stream_private_t *p_stream,
131 opj_event_mgr_t * p_manager );
132
133 /**
134 * Creates a tile-coder decoder.
135 *
136 * @param p_stream the stream to write data to.
137 * @param p_j2k J2K codec.
138 * @param p_manager the user event manager.
139 */
140 static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k,
141 opj_stream_private_t *p_stream,
142 opj_event_mgr_t * p_manager );
143
144 /**
145 * Excutes the given procedures on the given codec.
146 *
147 * @param p_procedure_list the list of procedures to execute
148 * @param p_j2k the jpeg2000 codec to execute the procedures on.
149 * @param p_stream the stream to execute the procedures on.
150 * @param p_manager the user manager.
151 *
152 * @return true if all the procedures were successfully executed.
153 */
154 static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
155 opj_procedure_list_t * p_procedure_list,
156 opj_stream_private_t *p_stream,
157 opj_event_mgr_t * p_manager);
158
159 /**
160 * Updates the rates of the tcp.
161 *
162 * @param p_stream the stream to write data to.
163 * @param p_j2k J2K codec.
164 * @param p_manager the user event manager.
165 */
166 static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
167 opj_stream_private_t *p_stream,
168 opj_event_mgr_t * p_manager );
169
170 /**
171 * Copies the decoding tile parameters onto all the tile parameters.
172 * Creates also the tile decoder.
173 */
174 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
177
178 /**
179 * Destroys the memory associated with the decoding of headers.
180 */
181 static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
182 opj_stream_private_t *p_stream,
183 opj_event_mgr_t * p_manager );
184
185 /**
186 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
187 * with the marker value.
188 * @param p_id Marker value to look up
189 *
190 * @return the handler associated with the id.
191 */
192 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
193
194 /**
195 * Destroys a tile coding parameter structure.
196 *
197 * @param p_tcp the tile coding parameter to destroy.
198 */
199 static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp);
200
201 /**
202 * Destroys the data inside a tile coding parameter structure.
203 *
204 * @param p_tcp the tile coding parameter which contain data to destroy.
205 */
206 static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp);
207
208 /**
209 * Destroys a coding parameter structure.
210 *
211 * @param p_cp the coding parameter to destroy.
212 */
213 static void opj_j2k_cp_destroy (opj_cp_t *p_cp);
214
215 /**
216 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
217 *
218 * @param p_j2k J2K codec.
219 * @param p_tile_no FIXME DOC
220 * @param p_comp_no the component number to output.
221 * @param p_data FIXME DOC
222 * @param p_header_size FIXME DOC
223 * @param p_manager the user event manager.
224 *
225 * @return FIXME DOC
226 */
227 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k,
228 OPJ_UINT32 p_tile_no,
229 OPJ_UINT32 p_comp_no,
230 OPJ_BYTE * p_data,
231 OPJ_UINT32 * p_header_size,
232 opj_event_mgr_t * p_manager );
233
234 /**
235 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
236 *
237 * @param p_j2k the J2K codec.
238 * @param p_tile_no the tile index.
239 * @param p_comp_no the component being outputted.
240 *
241 * @return the number of bytes taken by the SPCod element.
242 */
243 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,
244 OPJ_UINT32 p_tile_no,
245 OPJ_UINT32 p_comp_no );
246
247 /**
248 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
249 * @param p_j2k the jpeg2000 codec.
250 * @param compno FIXME DOC
251 * @param p_header_data the data contained in the COM box.
252 * @param p_header_size the size of the data contained in the COM marker.
253 * @param p_manager the user event manager.
254 */
255 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k,
256 OPJ_UINT32 compno,
257 OPJ_BYTE * p_header_data,
258 OPJ_UINT32 * p_header_size,
259 opj_event_mgr_t * p_manager );
260
261 /**
262 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
263 *
264 * @param p_tile_no the tile index.
265 * @param p_comp_no the component being outputted.
266 * @param p_j2k the J2K codec.
267 *
268 * @return the number of bytes taken by the SPCod element.
269 */
270 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
271 OPJ_UINT32 p_tile_no,
272 OPJ_UINT32 p_comp_no );
273
274 /**
275 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
276 *
277 * @param p_tile_no the tile to output.
278 * @param p_comp_no the component number to output.
279 * @param p_data the data buffer.
280 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
281 * @param p_j2k J2K codec.
282 * @param p_manager the user event manager.
283 *
284 */
285 static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
286 OPJ_UINT32 p_tile_no,
287 OPJ_UINT32 p_comp_no,
288 OPJ_BYTE * p_data,
289 OPJ_UINT32 * p_header_size,
290 opj_event_mgr_t * p_manager);
291
292 /**
293 * Updates the Tile Length Marker.
294 */
295 static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
296
297 /**
298 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
299 *
300 * @param p_j2k J2K codec.
301 * @param compno the component number to output.
302 * @param p_header_data the data buffer.
303 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
304 * @param p_manager the user event manager.
305 *
306 */
307 static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
308 OPJ_UINT32 compno,
309 OPJ_BYTE * p_header_data,
310 OPJ_UINT32 * p_header_size,
311 opj_event_mgr_t * p_manager );
312
313 /**
314 * Copies the tile component parameters of all the component from the first tile component.
315 *
316 * @param p_j2k the J2k codec.
317 */
318 static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k );
319
320 /**
321 * Copies the tile quantization parameters of all the component from the first tile component.
322 *
323 * @param p_j2k the J2k codec.
324 */
325 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );
326
327 /**
328 * Reads the tiles.
329 */
330 static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
331 opj_stream_private_t *p_stream,
332 opj_event_mgr_t * p_manager);
333
334 static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
335 OPJ_UINT32 p_tile_index,
336 opj_stream_private_t *p_stream,
337 opj_event_mgr_t * p_manager );
338
339 static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
340
341 static void opj_get_tile_dimensions(opj_image_t * l_image,
342 opj_tcd_tilecomp_t * l_tilec,
343 opj_image_comp_t * l_img_comp,
344 OPJ_UINT32* l_size_comp,
345 OPJ_UINT32* l_width,
346 OPJ_UINT32* l_height,
347 OPJ_UINT32* l_offset_x,
348 OPJ_UINT32* l_offset_y,
349 OPJ_UINT32* l_image_width,
350 OPJ_UINT32* l_stride,
351 OPJ_UINT32* l_tile_offset);
352
353 static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
354
355 static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
356 opj_stream_private_t *p_stream,
357 opj_event_mgr_t * p_manager );
358
359 /**
360 * Sets up the procedures to do on writing header.
361 * Developers wanting to extend the library can add their own writing procedures.
362 */
363 static OPJ_BOOL opj_j2k_setup_header_writing (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager);
364
365 static OPJ_BOOL opj_j2k_write_first_tile_part( opj_j2k_t *p_j2k,
366 OPJ_BYTE * p_data,
367 OPJ_UINT32 * p_data_written,
368 OPJ_UINT32 p_total_data_size,
369 opj_stream_private_t *p_stream,
370 struct opj_event_mgr * p_manager );
371
372 static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k,
373 OPJ_BYTE * p_data,
374 OPJ_UINT32 * p_data_written,
375 OPJ_UINT32 p_total_data_size,
376 opj_stream_private_t *p_stream,
377 struct opj_event_mgr * p_manager );
378
379 /**
380 * Gets the offset of the header.
381 *
382 * @param p_stream the stream to write data to.
383 * @param p_j2k J2K codec.
384 * @param p_manager the user event manager.
385 */
386 static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,
387 opj_stream_private_t *p_stream,
388 opj_event_mgr_t * p_manager );
389
390 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
391
392 /*
393 * -----------------------------------------------------------------------
394 * -----------------------------------------------------------------------
395 * -----------------------------------------------------------------------
396 */
397
398 /**
399 * Writes the SOC marker (Start Of Codestream)
400 *
401 * @param p_stream the stream to write data to.
402 * @param p_j2k J2K codec.
403 * @param p_manager the user event manager.
404 */
405 static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k,
406 opj_stream_private_t *p_stream,
407 opj_event_mgr_t * p_manager );
408
409 /**
410 * Reads a SOC marker (Start of Codestream)
411 * @param p_j2k the jpeg2000 file codec.
412 * @param p_stream XXX needs data
413 * @param p_manager the user event manager.
414 */
415 static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k,
416 opj_stream_private_t *p_stream,
417 opj_event_mgr_t * p_manager );
418
419 /**
420 * Writes the SIZ marker (image and tile size)
421 *
422 * @param p_j2k J2K codec.
423 * @param p_stream the stream to write data to.
424 * @param p_manager the user event manager.
425 */
426 static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k,
427 opj_stream_private_t *p_stream,
428 opj_event_mgr_t * p_manager );
429
430 /**
431 * Reads a SIZ marker (image and tile size)
432 * @param p_j2k the jpeg2000 file codec.
433 * @param p_header_data the data contained in the SIZ box.
434 * @param p_header_size the size of the data contained in the SIZ marker.
435 * @param p_manager the user event manager.
436 */
437 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
438 OPJ_BYTE * p_header_data,
439 OPJ_UINT32 p_header_size,
440 opj_event_mgr_t * p_manager);
441
442 /**
443 * Writes the COM marker (comment)
444 *
445 * @param p_stream the stream to write data to.
446 * @param p_j2k J2K codec.
447 * @param p_manager the user event manager.
448 */
449 static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k,
450 opj_stream_private_t *p_stream,
451 opj_event_mgr_t * p_manager );
452
453 /**
454 * Reads a COM marker (comments)
455 * @param p_j2k the jpeg2000 file codec.
456 * @param p_header_data the data contained in the COM box.
457 * @param p_header_size the size of the data contained in the COM marker.
458 * @param p_manager the user event manager.
459 */
460 static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k,
461 OPJ_BYTE * p_header_data,
462 OPJ_UINT32 p_header_size,
463 opj_event_mgr_t * p_manager );
464 /**
465 * Writes the COD marker (Coding style default)
466 *
467 * @param p_stream the stream to write data to.
468 * @param p_j2k J2K codec.
469 * @param p_manager the user event manager.
470 */
471 static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k,
472 opj_stream_private_t *p_stream,
473 opj_event_mgr_t * p_manager );
474
475 /**
476 * Reads a COD marker (Coding Styke defaults)
477 * @param p_header_data the data contained in the COD box.
478 * @param p_j2k the jpeg2000 codec.
479 * @param p_header_size the size of the data contained in the COD marker.
480 * @param p_manager the user event manager.
481 */
482 static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k,
483 OPJ_BYTE * p_header_data,
484 OPJ_UINT32 p_header_size,
485 opj_event_mgr_t * p_manager);
486
487 #if 0
488 /**
489 * Writes the COC marker (Coding style component)
490 *
491 * @param p_j2k J2K codec.
492 * @param p_comp_no the index of the component to output.
493 * @param p_stream the stream to write data to.
494 * @param p_manager the user event manager.
495 */
496 static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
497 OPJ_UINT32 p_comp_no,
498 opj_stream_private_t *p_stream,
499 opj_event_mgr_t * p_manager );
500 #endif
501
502 #if 0
503 /**
504 * Writes the COC marker (Coding style component)
505 *
506 * @param p_j2k J2K codec.
507 * @param p_comp_no the index of the component to output.
508 * @param p_data FIXME DOC
509 * @param p_data_written FIXME DOC
510 * @param p_manager the user event manager.
511 */
512 static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
513 OPJ_UINT32 p_comp_no,
514 OPJ_BYTE * p_data,
515 OPJ_UINT32 * p_data_written,
516 opj_event_mgr_t * p_manager );
517 #endif
518
519 /**
520 * Gets the maximum size taken by a coc.
521 *
522 * @param p_j2k the jpeg2000 codec to use.
523 */
524 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
525
526 /**
527 * Reads a COC marker (Coding Style Component)
528 * @param p_header_data the data contained in the COC box.
529 * @param p_j2k the jpeg2000 codec.
530 * @param p_header_size the size of the data contained in the COC marker.
531 * @param p_manager the user event manager.
532 */
533 static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k,
534 OPJ_BYTE * p_header_data,
535 OPJ_UINT32 p_header_size,
536 opj_event_mgr_t * p_manager );
537
538 /**
539 * Writes the QCD marker (quantization default)
540 *
541 * @param p_j2k J2K codec.
542 * @param p_stream the stream to write data to.
543 * @param p_manager the user event manager.
544 */
545 static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k,
546 opj_stream_private_t *p_stream,
547 opj_event_mgr_t * p_manager );
548
549 /**
550 * Reads a QCD marker (Quantization defaults)
551 * @param p_header_data the data contained in the QCD box.
552 * @param p_j2k the jpeg2000 codec.
553 * @param p_header_size the size of the data contained in the QCD marker.
554 * @param p_manager the user event manager.
555 */
556 static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k,
557 OPJ_BYTE * p_header_data,
558 OPJ_UINT32 p_header_size,
559 opj_event_mgr_t * p_manager );
560 #if 0
561 /**
562 * Writes the QCC marker (quantization component)
563 *
564 * @param p_comp_no the index of the component to output.
565 * @param p_stream the stream to write data to.
566 * @param p_j2k J2K codec.
567 * @param p_manager the user event manager.
568 */
569 static OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k,
570 OPJ_UINT32 p_comp_no,
571 opj_stream_private_t *p_stream,
572 opj_event_mgr_t * p_manager );
573 #endif
574
575 #if 0
576 /**
577 * Writes the QCC marker (quantization component)
578 *
579 * @param p_j2k J2K codec.
580 * @param p_comp_no the index of the component to output.
581 * @param p_data FIXME DOC
582 * @param p_data_written the stream to write data to.
583 * @param p_manager the user event manager.
584 */
585 static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
586 OPJ_UINT32 p_comp_no,
587 OPJ_BYTE * p_data,
588 OPJ_UINT32 * p_data_written,
589 opj_event_mgr_t * p_manager );
590 #endif
591
592 /**
593 * Gets the maximum size taken by a qcc.
594 */
595 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);
596
597 /**
598 * Reads a QCC marker (Quantization component)
599 * @param p_header_data the data contained in the QCC box.
600 * @param p_j2k the jpeg2000 codec.
601 * @param p_header_size the size of the data contained in the QCC marker.
602 * @param p_manager the user event manager.
603 */
604 static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k,
605 OPJ_BYTE * p_header_data,
606 OPJ_UINT32 p_header_size,
607 opj_event_mgr_t * p_manager);
608 /**
609 * Writes the POC marker (Progression Order Change)
610 *
611 * @param p_stream the stream to write data to.
612 * @param p_j2k J2K codec.
613 * @param p_manager the user event manager.
614 */
615 static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k,
616 opj_stream_private_t *p_stream,
617 opj_event_mgr_t * p_manager );
618 /**
619 * Writes the POC marker (Progression Order Change)
620 *
621 * @param p_j2k J2K codec.
622 * @param p_data FIXME DOC
623 * @param p_data_written the stream to write data to.
624 * @param p_manager the user event manager.
625 */
626 static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
627 OPJ_BYTE * p_data,
628 OPJ_UINT32 * p_data_written,
629 opj_event_mgr_t * p_manager );
630 /**
631 * Gets the maximum size taken by the writing of a POC.
632 */
633 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
634
635 /**
636 * Reads a POC marker (Progression Order Change)
637 *
638 * @param p_header_data the data contained in the POC box.
639 * @param p_j2k the jpeg2000 codec.
640 * @param p_header_size the size of the data contained in the POC marker.
641 * @param p_manager the user event manager.
642 */
643 static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k,
644 OPJ_BYTE * p_header_data,
645 OPJ_UINT32 p_header_size,
646 opj_event_mgr_t * p_manager );
647
648 /**
649 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
650 */
651 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k);
652
653 /**
654 * Gets the maximum size taken by the headers of the SOT.
655 *
656 * @param p_j2k the jpeg2000 codec to use.
657 */
658 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
659
660 /**
661 * Reads a CRG marker (Component registration)
662 *
663 * @param p_header_data the data contained in the TLM box.
664 * @param p_j2k the jpeg2000 codec.
665 * @param p_header_size the size of the data contained in the TLM marker.
666 * @param p_manager the user event manager.
667 */
668 static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k,
669 OPJ_BYTE * p_header_data,
670 OPJ_UINT32 p_header_size,
671 opj_event_mgr_t * p_manager );
672 /**
673 * Reads a TLM marker (Tile Length Marker)
674 *
675 * @param p_header_data the data contained in the TLM box.
676 * @param p_j2k the jpeg2000 codec.
677 * @param p_header_size the size of the data contained in the TLM marker.
678 * @param p_manager the user event manager.
679 */
680 static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k,
681 OPJ_BYTE * p_header_data,
682 OPJ_UINT32 p_header_size,
683 opj_event_mgr_t * p_manager);
684
685 /**
686 * Writes the updated tlm.
687 *
688 * @param p_stream the stream to write data to.
689 * @param p_j2k J2K codec.
690 * @param p_manager the user event manager.
691 */
692 static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
693 opj_stream_private_t *p_stream,
694 opj_event_mgr_t * p_manager );
695
696 /**
697 * Reads a PLM marker (Packet length, main header marker)
698 *
699 * @param p_header_data the data contained in the TLM box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the TLM marker.
702 * @param p_manager the user event manager.
703 */
704 static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k,
705 OPJ_BYTE * p_header_data,
706 OPJ_UINT32 p_header_size,
707 opj_event_mgr_t * p_manager);
708 /**
709 * Reads a PLT marker (Packet length, tile-part header)
710 *
711 * @param p_header_data the data contained in the PLT box.
712 * @param p_j2k the jpeg2000 codec.
713 * @param p_header_size the size of the data contained in the PLT marker.
714 * @param p_manager the user event manager.
715 */
716 static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k,
717 OPJ_BYTE * p_header_data,
718 OPJ_UINT32 p_header_size,
719 opj_event_mgr_t * p_manager );
720
721 /**
722 * Reads a PPM marker (Packed headers, main header)
723 *
724 * @param p_header_data the data contained in the POC box.
725 * @param p_j2k the jpeg2000 codec.
726 * @param p_header_size the size of the data contained in the POC marker.
727 * @param p_manager the user event manager.
728 */
729
730 static OPJ_BOOL opj_j2k_read_ppm (
731 opj_j2k_t *p_j2k,
732 OPJ_BYTE * p_header_data,
733 OPJ_UINT32 p_header_size,
734 opj_event_mgr_t * p_manager );
735
736 /**
737 * Merges all PPM markers read (Packed headers, main header)
738 *
739 * @param p_cp main coding parameters.
740 * @param p_manager the user event manager.
741 */
742 static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager );
743
744 /**
745 * Reads a PPT marker (Packed packet headers, tile-part header)
746 *
747 * @param p_header_data the data contained in the PPT box.
748 * @param p_j2k the jpeg2000 codec.
749 * @param p_header_size the size of the data contained in the PPT marker.
750 * @param p_manager the user event manager.
751 */
752 static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k,
753 OPJ_BYTE * p_header_data,
754 OPJ_UINT32 p_header_size,
755 opj_event_mgr_t * p_manager );
756
757 /**
758 * Merges all PPT markers read (Packed headers, tile-part header)
759 *
760 * @param p_tcp the tile.
761 * @param p_manager the user event manager.
762 */
763 static OPJ_BOOL opj_j2k_merge_ppt ( opj_tcp_t *p_tcp,
764 opj_event_mgr_t * p_manager );
765
766
767 /**
768 * Writes the TLM marker (Tile Length Marker)
769 *
770 * @param p_stream the stream to write data to.
771 * @param p_j2k J2K codec.
772 * @param p_manager the user event manager.
773 */
774 static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k,
775 opj_stream_private_t *p_stream,
776 opj_event_mgr_t * p_manager );
777
778 /**
779 * Writes the SOT marker (Start of tile-part)
780 *
781 * @param p_j2k J2K codec.
782 * @param p_data FIXME DOC
783 * @param p_data_written FIXME DOC
784 * @param p_stream the stream to write data to.
785 * @param p_manager the user event manager.
786 */
787 static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k,
788 OPJ_BYTE * p_data,
789 OPJ_UINT32 * p_data_written,
790 const opj_stream_private_t *p_stream,
791 opj_event_mgr_t * p_manager );
792
793 /**
794 * Reads values from a SOT marker (Start of tile-part)
795 *
796 * the j2k decoder state is not affected. No side effects, no checks except for p_header_size.
797 *
798 * @param p_header_data the data contained in the SOT marker.
799 * @param p_header_size the size of the data contained in the SOT marker.
800 * @param p_tile_no Isot.
801 * @param p_tot_len Psot.
802 * @param p_current_part TPsot.
803 * @param p_num_parts TNsot.
804 * @param p_manager the user event manager.
805 */
806 static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,
807 OPJ_UINT32 p_header_size,
808 OPJ_UINT32* p_tile_no,
809 OPJ_UINT32* p_tot_len,
810 OPJ_UINT32* p_current_part,
811 OPJ_UINT32* p_num_parts,
812 opj_event_mgr_t * p_manager );
813 /**
814 * Reads a SOT marker (Start of tile-part)
815 *
816 * @param p_header_data the data contained in the SOT marker.
817 * @param p_j2k the jpeg2000 codec.
818 * @param p_header_size the size of the data contained in the PPT marker.
819 * @param p_manager the user event manager.
820 */
821 static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
822 OPJ_BYTE * p_header_data,
823 OPJ_UINT32 p_header_size,
824 opj_event_mgr_t * p_manager );
825 /**
826 * Writes the SOD marker (Start of data)
827 *
828 * @param p_j2k J2K codec.
829 * @param p_tile_coder FIXME DOC
830 * @param p_data FIXME DOC
831 * @param p_data_written FIXME DOC
832 * @param p_total_data_size FIXME DOC
833 * @param p_stream the stream to write data to.
834 * @param p_manager the user event manager.
835 */
836 static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k,
837 opj_tcd_t * p_tile_coder,
838 OPJ_BYTE * p_data,
839 OPJ_UINT32 * p_data_written,
840 OPJ_UINT32 p_total_data_size,
841 const opj_stream_private_t *p_stream,
842 opj_event_mgr_t * p_manager );
843
844 /**
845 * Reads a SOD marker (Start Of Data)
846 *
847 * @param p_j2k the jpeg2000 codec.
848 * @param p_stream FIXME DOC
849 * @param p_manager the user event manager.
850 */
851 static OPJ_BOOL opj_j2k_read_sod( opj_j2k_t *p_j2k,
852 opj_stream_private_t *p_stream,
853 opj_event_mgr_t * p_manager );
854
opj_j2k_update_tlm(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_part_size)855 static void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )
856 {
857 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
858 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
859
860 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
861 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
862 }
863
864 /**
865 * Writes the RGN marker (Region Of Interest)
866 *
867 * @param p_tile_no the tile to output
868 * @param p_comp_no the component to output
869 * @param nb_comps the number of components
870 * @param p_stream the stream to write data to.
871 * @param p_j2k J2K codec.
872 * @param p_manager the user event manager.
873 */
874 static OPJ_BOOL opj_j2k_write_rgn( opj_j2k_t *p_j2k,
875 OPJ_UINT32 p_tile_no,
876 OPJ_UINT32 p_comp_no,
877 OPJ_UINT32 nb_comps,
878 opj_stream_private_t *p_stream,
879 opj_event_mgr_t * p_manager );
880
881 /**
882 * Reads a RGN marker (Region Of Interest)
883 *
884 * @param p_header_data the data contained in the POC box.
885 * @param p_j2k the jpeg2000 codec.
886 * @param p_header_size the size of the data contained in the POC marker.
887 * @param p_manager the user event manager.
888 */
889 static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
890 OPJ_BYTE * p_header_data,
891 OPJ_UINT32 p_header_size,
892 opj_event_mgr_t * p_manager );
893
894 /**
895 * Writes the EOC marker (End of Codestream)
896 *
897 * @param p_stream the stream to write data to.
898 * @param p_j2k J2K codec.
899 * @param p_manager the user event manager.
900 */
901 static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k,
902 opj_stream_private_t *p_stream,
903 opj_event_mgr_t * p_manager );
904
905 #if 0
906 /**
907 * Reads a EOC marker (End Of Codestream)
908 *
909 * @param p_j2k the jpeg2000 codec.
910 * @param p_stream FIXME DOC
911 * @param p_manager the user event manager.
912 */
913 static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
914 opj_stream_private_t *p_stream,
915 opj_event_mgr_t * p_manager );
916 #endif
917
918 /**
919 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
920 *
921 * @param p_stream the stream to write data to.
922 * @param p_j2k J2K codec.
923 * @param p_manager the user event manager.
924 */
925 static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k,
926 opj_stream_private_t *p_stream,
927 opj_event_mgr_t * p_manager );
928
929 /**
930 * Inits the Info
931 *
932 * @param p_stream the stream to write data to.
933 * @param p_j2k J2K codec.
934 * @param p_manager the user event manager.
935 */
936 static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k,
937 opj_stream_private_t *p_stream,
938 opj_event_mgr_t * p_manager );
939
940 /**
941 Add main header marker information
942 @param cstr_index Codestream information structure
943 @param type marker type
944 @param pos byte offset of marker segment
945 @param len length of marker segment
946 */
947 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
948 /**
949 Add tile header marker information
950 @param tileno tile index number
951 @param cstr_index Codestream information structure
952 @param type marker type
953 @param pos byte offset of marker segment
954 @param len length of marker segment
955 */
956 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);
957
958 /**
959 * Reads an unknown marker
960 *
961 * @param p_j2k the jpeg2000 codec.
962 * @param p_stream the stream object to read from.
963 * @param output_marker FIXME DOC
964 * @param p_manager the user event manager.
965 *
966 * @return true if the marker could be deduced.
967 */
968 static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,
969 opj_stream_private_t *p_stream,
970 OPJ_UINT32 *output_marker,
971 opj_event_mgr_t * p_manager );
972
973 /**
974 * Writes the MCT marker (Multiple Component Transform)
975 *
976 * @param p_j2k J2K codec.
977 * @param p_mct_record FIXME DOC
978 * @param p_stream the stream to write data to.
979 * @param p_manager the user event manager.
980 */
981 static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k,
982 opj_mct_data_t * p_mct_record,
983 opj_stream_private_t *p_stream,
984 opj_event_mgr_t * p_manager );
985
986 /**
987 * Reads a MCT marker (Multiple Component Transform)
988 *
989 * @param p_header_data the data contained in the MCT box.
990 * @param p_j2k the jpeg2000 codec.
991 * @param p_header_size the size of the data contained in the MCT marker.
992 * @param p_manager the user event manager.
993 */
994 static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k,
995 OPJ_BYTE * p_header_data,
996 OPJ_UINT32 p_header_size,
997 opj_event_mgr_t * p_manager );
998
999 /**
1000 * Writes the MCC marker (Multiple Component Collection)
1001 *
1002 * @param p_j2k J2K codec.
1003 * @param p_mcc_record FIXME DOC
1004 * @param p_stream the stream to write data to.
1005 * @param p_manager the user event manager.
1006 */
1007 static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k,
1008 opj_simple_mcc_decorrelation_data_t * p_mcc_record,
1009 opj_stream_private_t *p_stream,
1010 opj_event_mgr_t * p_manager );
1011
1012 /**
1013 * Reads a MCC marker (Multiple Component Collection)
1014 *
1015 * @param p_header_data the data contained in the MCC box.
1016 * @param p_j2k the jpeg2000 codec.
1017 * @param p_header_size the size of the data contained in the MCC marker.
1018 * @param p_manager the user event manager.
1019 */
1020 static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k,
1021 OPJ_BYTE * p_header_data,
1022 OPJ_UINT32 p_header_size,
1023 opj_event_mgr_t * p_manager );
1024
1025 /**
1026 * Writes the MCO marker (Multiple component transformation ordering)
1027 *
1028 * @param p_stream the stream to write data to.
1029 * @param p_j2k J2K codec.
1030 * @param p_manager the user event manager.
1031 */
1032 static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k,
1033 opj_stream_private_t *p_stream,
1034 opj_event_mgr_t * p_manager );
1035
1036 /**
1037 * Reads a MCO marker (Multiple Component Transform Ordering)
1038 *
1039 * @param p_header_data the data contained in the MCO box.
1040 * @param p_j2k the jpeg2000 codec.
1041 * @param p_header_size the size of the data contained in the MCO marker.
1042 * @param p_manager the user event manager.
1043 */
1044 static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k,
1045 OPJ_BYTE * p_header_data,
1046 OPJ_UINT32 p_header_size,
1047 opj_event_mgr_t * p_manager );
1048
1049 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1050
1051 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1052 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1053 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1054 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1055
1056 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1057 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1058 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1059 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1060
1061 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1062 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1063 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1064 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1065
1066 /**
1067 * Ends the encoding, i.e. frees memory.
1068 *
1069 * @param p_stream the stream to write data to.
1070 * @param p_j2k J2K codec.
1071 * @param p_manager the user event manager.
1072 */
1073 static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k,
1074 opj_stream_private_t *p_stream,
1075 opj_event_mgr_t * p_manager );
1076
1077 /**
1078 * Writes the CBD marker (Component bit depth definition)
1079 *
1080 * @param p_stream the stream to write data to.
1081 * @param p_j2k J2K codec.
1082 * @param p_manager the user event manager.
1083 */
1084 static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
1085 opj_stream_private_t *p_stream,
1086 opj_event_mgr_t * p_manager );
1087
1088 /**
1089 * Reads a CBD marker (Component bit depth definition)
1090 * @param p_header_data the data contained in the CBD box.
1091 * @param p_j2k the jpeg2000 codec.
1092 * @param p_header_size the size of the data contained in the CBD marker.
1093 * @param p_manager the user event manager.
1094 */
1095 static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k,
1096 OPJ_BYTE * p_header_data,
1097 OPJ_UINT32 p_header_size,
1098 opj_event_mgr_t * p_manager);
1099
1100 #if 0
1101 /**
1102 * Writes COC marker for each component.
1103 *
1104 * @param p_stream the stream to write data to.
1105 * @param p_j2k J2K codec.
1106 * @param p_manager the user event manager.
1107 */
1108 static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
1109 opj_stream_private_t *p_stream,
1110 opj_event_mgr_t * p_manager );
1111 #endif
1112
1113 #if 0
1114 /**
1115 * Writes QCC marker for each component.
1116 *
1117 * @param p_stream the stream to write data to.
1118 * @param p_j2k J2K codec.
1119 * @param p_manager the user event manager.
1120 */
1121 static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
1122 opj_stream_private_t *p_stream,
1123 opj_event_mgr_t * p_manager );
1124 #endif
1125
1126 /**
1127 * Writes regions of interests.
1128 *
1129 * @param p_stream the stream to write data to.
1130 * @param p_j2k J2K codec.
1131 * @param p_manager the user event manager.
1132 */
1133 static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
1134 opj_stream_private_t *p_stream,
1135 opj_event_mgr_t * p_manager );
1136
1137 /**
1138 * Writes EPC ????
1139 *
1140 * @param p_stream the stream to write data to.
1141 * @param p_j2k J2K codec.
1142 * @param p_manager the user event manager.
1143 */
1144 static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k,
1145 opj_stream_private_t *p_stream,
1146 opj_event_mgr_t * p_manager );
1147
1148 /**
1149 * Checks the progression order changes values. Tells of the poc given as input are valid.
1150 * A nice message is outputted at errors.
1151 *
1152 * @param p_pocs the progression order changes.
1153 * @param p_nb_pocs the number of progression order changes.
1154 * @param p_nb_resolutions the number of resolutions.
1155 * @param numcomps the number of components
1156 * @param numlayers the number of layers.
1157 * @param p_manager the user event manager.
1158 *
1159 * @return true if the pocs are valid.
1160 */
1161 static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1162 OPJ_UINT32 p_nb_pocs,
1163 OPJ_UINT32 p_nb_resolutions,
1164 OPJ_UINT32 numcomps,
1165 OPJ_UINT32 numlayers,
1166 opj_event_mgr_t * p_manager);
1167
1168 /**
1169 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1170 *
1171 * @param cp the coding parameters.
1172 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1173 * @param tileno the given tile.
1174 *
1175 * @return the number of tile parts.
1176 */
1177 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1178
1179 /**
1180 * Calculates the total number of tile parts needed by the encoder to
1181 * encode such an image. If not enough memory is available, then the function return false.
1182 *
1183 * @param p_nb_tiles pointer that will hold the number of tile parts.
1184 * @param cp the coding parameters for the image.
1185 * @param image the image to encode.
1186 * @param p_j2k the p_j2k encoder.
1187 * @param p_manager the user event manager.
1188 *
1189 * @return true if the function was successful, false else.
1190 */
1191 static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k,
1192 opj_cp_t *cp,
1193 OPJ_UINT32 * p_nb_tiles,
1194 opj_image_t *image,
1195 opj_event_mgr_t * p_manager);
1196
1197 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
1198
1199 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
1200
1201 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1202
1203 static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);
1204
1205 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);
1206
1207 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
1208
1209 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
1210
1211 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager);
1212
1213 /**
1214 * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254.
1215 *
1216 * @param p_stream the stream to read data from.
1217 * @param tile_no tile number we're looking for.
1218 * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction.
1219 * @param p_manager the user event manager.
1220 *
1221 * @return true if the function was successful, false else.
1222 */
1223 static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, opj_event_mgr_t * p_manager );
1224
1225 /*@}*/
1226
1227 /*@}*/
1228
1229 /* ----------------------------------------------------------------------- */
1230 typedef struct j2k_prog_order{
1231 OPJ_PROG_ORDER enum_prog;
1232 char str_prog[5];
1233 }j2k_prog_order_t;
1234
1235 static j2k_prog_order_t j2k_prog_order_list[] = {
1236 {OPJ_CPRL, "CPRL"},
1237 {OPJ_LRCP, "LRCP"},
1238 {OPJ_PCRL, "PCRL"},
1239 {OPJ_RLCP, "RLCP"},
1240 {OPJ_RPCL, "RPCL"},
1241 {(OPJ_PROG_ORDER)-1, ""}
1242 };
1243
1244 /**
1245 * FIXME DOC
1246 */
1247 static const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1248 {
1249 2,
1250 4,
1251 4,
1252 8
1253 };
1254
1255 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1256
1257 static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1258 {
1259 opj_j2k_read_int16_to_float,
1260 opj_j2k_read_int32_to_float,
1261 opj_j2k_read_float32_to_float,
1262 opj_j2k_read_float64_to_float
1263 };
1264
1265 static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1266 {
1267 opj_j2k_read_int16_to_int32,
1268 opj_j2k_read_int32_to_int32,
1269 opj_j2k_read_float32_to_int32,
1270 opj_j2k_read_float64_to_int32
1271 };
1272
1273 static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1274 {
1275 opj_j2k_write_float_to_int16,
1276 opj_j2k_write_float_to_int32,
1277 opj_j2k_write_float_to_float,
1278 opj_j2k_write_float_to_float64
1279 };
1280
1281 typedef struct opj_dec_memory_marker_handler
1282 {
1283 /** marker value */
1284 OPJ_UINT32 id;
1285 /** value of the state when the marker can appear */
1286 OPJ_UINT32 states;
1287 /** action linked to the marker */
1288 OPJ_BOOL (*handler) ( opj_j2k_t *p_j2k,
1289 OPJ_BYTE * p_header_data,
1290 OPJ_UINT32 p_header_size,
1291 opj_event_mgr_t * p_manager );
1292 }
1293 opj_dec_memory_marker_handler_t;
1294
1295 static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1296 {
1297 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1298 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1299 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1300 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1301 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1302 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1303 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1304 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1305 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1306 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1307 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1308 {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm},
1309 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1310 {J2K_MS_SOP, 0, 0},
1311 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1312 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1313 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1314 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1315 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1316 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1317 #ifdef USE_JPWL
1318 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1319 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1320 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1321 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1322 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1323 #endif
1324 #endif /* USE_JPWL */
1325 #ifdef USE_JPSEC
1326 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1327 {J2K_MS_INSEC, 0, j2k_read_insec}
1328 #endif /* USE_JPSEC */
1329 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1330 };
1331
opj_j2k_read_int16_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1332 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1333 {
1334 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1335 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1336 OPJ_UINT32 i;
1337 OPJ_UINT32 l_temp;
1338
1339 for (i=0;i<p_nb_elem;++i) {
1340 opj_read_bytes(l_src_data,&l_temp,2);
1341
1342 l_src_data+=sizeof(OPJ_INT16);
1343
1344 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1345 }
1346 }
1347
opj_j2k_read_int32_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1348 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1349 {
1350 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1351 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1352 OPJ_UINT32 i;
1353 OPJ_UINT32 l_temp;
1354
1355 for (i=0;i<p_nb_elem;++i) {
1356 opj_read_bytes(l_src_data,&l_temp,4);
1357
1358 l_src_data+=sizeof(OPJ_INT32);
1359
1360 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1361 }
1362 }
1363
opj_j2k_read_float32_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1364 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1365 {
1366 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1367 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1368 OPJ_UINT32 i;
1369 OPJ_FLOAT32 l_temp;
1370
1371 for (i=0;i<p_nb_elem;++i) {
1372 opj_read_float(l_src_data,&l_temp);
1373
1374 l_src_data+=sizeof(OPJ_FLOAT32);
1375
1376 *(l_dest_data++) = l_temp;
1377 }
1378 }
1379
opj_j2k_read_float64_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1380 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1381 {
1382 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1383 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1384 OPJ_UINT32 i;
1385 OPJ_FLOAT64 l_temp;
1386
1387 for (i=0;i<p_nb_elem;++i) {
1388 opj_read_double(l_src_data,&l_temp);
1389
1390 l_src_data+=sizeof(OPJ_FLOAT64);
1391
1392 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1393 }
1394 }
1395
opj_j2k_read_int16_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1396 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1397 {
1398 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1399 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1400 OPJ_UINT32 i;
1401 OPJ_UINT32 l_temp;
1402
1403 for (i=0;i<p_nb_elem;++i) {
1404 opj_read_bytes(l_src_data,&l_temp,2);
1405
1406 l_src_data+=sizeof(OPJ_INT16);
1407
1408 *(l_dest_data++) = (OPJ_INT32) l_temp;
1409 }
1410 }
1411
opj_j2k_read_int32_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1412 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1413 {
1414 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1415 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1416 OPJ_UINT32 i;
1417 OPJ_UINT32 l_temp;
1418
1419 for (i=0;i<p_nb_elem;++i) {
1420 opj_read_bytes(l_src_data,&l_temp,4);
1421
1422 l_src_data+=sizeof(OPJ_INT32);
1423
1424 *(l_dest_data++) = (OPJ_INT32) l_temp;
1425 }
1426 }
1427
opj_j2k_read_float32_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1428 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1429 {
1430 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1431 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1432 OPJ_UINT32 i;
1433 OPJ_FLOAT32 l_temp;
1434
1435 for (i=0;i<p_nb_elem;++i) {
1436 opj_read_float(l_src_data,&l_temp);
1437
1438 l_src_data+=sizeof(OPJ_FLOAT32);
1439
1440 *(l_dest_data++) = (OPJ_INT32) l_temp;
1441 }
1442 }
1443
opj_j2k_read_float64_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1444 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1445 {
1446 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1447 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1448 OPJ_UINT32 i;
1449 OPJ_FLOAT64 l_temp;
1450
1451 for (i=0;i<p_nb_elem;++i) {
1452 opj_read_double(l_src_data,&l_temp);
1453
1454 l_src_data+=sizeof(OPJ_FLOAT64);
1455
1456 *(l_dest_data++) = (OPJ_INT32) l_temp;
1457 }
1458 }
1459
opj_j2k_write_float_to_int16(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1460 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1461 {
1462 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1463 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1464 OPJ_UINT32 i;
1465 OPJ_UINT32 l_temp;
1466
1467 for (i=0;i<p_nb_elem;++i) {
1468 l_temp = (OPJ_UINT32) *(l_src_data++);
1469
1470 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1471
1472 l_dest_data+=sizeof(OPJ_INT16);
1473 }
1474 }
1475
opj_j2k_write_float_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1476 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1477 {
1478 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1479 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1480 OPJ_UINT32 i;
1481 OPJ_UINT32 l_temp;
1482
1483 for (i=0;i<p_nb_elem;++i) {
1484 l_temp = (OPJ_UINT32) *(l_src_data++);
1485
1486 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1487
1488 l_dest_data+=sizeof(OPJ_INT32);
1489 }
1490 }
1491
opj_j2k_write_float_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1492 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1493 {
1494 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1495 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1496 OPJ_UINT32 i;
1497 OPJ_FLOAT32 l_temp;
1498
1499 for (i=0;i<p_nb_elem;++i) {
1500 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1501
1502 opj_write_float(l_dest_data,l_temp);
1503
1504 l_dest_data+=sizeof(OPJ_FLOAT32);
1505 }
1506 }
1507
opj_j2k_write_float_to_float64(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1508 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1509 {
1510 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1511 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1512 OPJ_UINT32 i;
1513 OPJ_FLOAT64 l_temp;
1514
1515 for (i=0;i<p_nb_elem;++i) {
1516 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1517
1518 opj_write_double(l_dest_data,l_temp);
1519
1520 l_dest_data+=sizeof(OPJ_FLOAT64);
1521 }
1522 }
1523
opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)1524 char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1525 j2k_prog_order_t *po;
1526 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1527 if(po->enum_prog == prg_order){
1528 return po->str_prog;
1529 }
1530 }
1531 return po->str_prog;
1532 }
1533
opj_j2k_check_poc_val(const opj_poc_t * p_pocs,OPJ_UINT32 p_nb_pocs,OPJ_UINT32 p_nb_resolutions,OPJ_UINT32 p_num_comps,OPJ_UINT32 p_num_layers,opj_event_mgr_t * p_manager)1534 static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1535 OPJ_UINT32 p_nb_pocs,
1536 OPJ_UINT32 p_nb_resolutions,
1537 OPJ_UINT32 p_num_comps,
1538 OPJ_UINT32 p_num_layers,
1539 opj_event_mgr_t * p_manager)
1540 {
1541 OPJ_UINT32* packet_array;
1542 OPJ_UINT32 index , resno, compno, layno;
1543 OPJ_UINT32 i;
1544 OPJ_UINT32 step_c = 1;
1545 OPJ_UINT32 step_r = p_num_comps * step_c;
1546 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1547 OPJ_BOOL loss = OPJ_FALSE;
1548 OPJ_UINT32 layno0 = 0;
1549
1550 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1551 if (packet_array == 00) {
1552 opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1553 return OPJ_FALSE;
1554 }
1555
1556 if (p_nb_pocs == 0) {
1557 opj_free(packet_array);
1558 return OPJ_TRUE;
1559 }
1560
1561 index = step_r * p_pocs->resno0;
1562 /* take each resolution for each poc */
1563 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1564 {
1565 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1566
1567 /* take each comp of each resolution for each poc */
1568 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1569 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1570
1571 /* and finally take each layer of each res of ... */
1572 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1573 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1574 packet_array[comp_index] = 1;
1575 comp_index += step_l;
1576 }
1577
1578 res_index += step_c;
1579 }
1580
1581 index += step_r;
1582 }
1583 ++p_pocs;
1584
1585 /* iterate through all the pocs */
1586 for (i = 1; i < p_nb_pocs ; ++i) {
1587 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1588
1589 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1590 index = step_r * p_pocs->resno0;
1591
1592 /* take each resolution for each poc */
1593 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1594 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1595
1596 /* take each comp of each resolution for each poc */
1597 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1598 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1599
1600 /* and finally take each layer of each res of ... */
1601 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1602 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1603 packet_array[comp_index] = 1;
1604 comp_index += step_l;
1605 }
1606
1607 res_index += step_c;
1608 }
1609
1610 index += step_r;
1611 }
1612
1613 ++p_pocs;
1614 }
1615
1616 index = 0;
1617 for (layno = 0; layno < p_num_layers ; ++layno) {
1618 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1619 for (compno = 0; compno < p_num_comps; ++compno) {
1620 loss |= (packet_array[index]!=1);
1621 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1622 index += step_c;
1623 }
1624 }
1625 }
1626
1627 if (loss) {
1628 opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1629 }
1630
1631 opj_free(packet_array);
1632
1633 return !loss;
1634 }
1635
1636 /* ----------------------------------------------------------------------- */
1637
opj_j2k_get_num_tp(opj_cp_t * cp,OPJ_UINT32 pino,OPJ_UINT32 tileno)1638 static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1639 {
1640 const OPJ_CHAR *prog = 00;
1641 OPJ_INT32 i;
1642 OPJ_UINT32 tpnum = 1;
1643 opj_tcp_t *tcp = 00;
1644 opj_poc_t * l_current_poc = 00;
1645
1646 /* preconditions */
1647 assert(tileno < (cp->tw * cp->th));
1648 assert(pino < (cp->tcps[tileno].numpocs + 1));
1649
1650 /* get the given tile coding parameter */
1651 tcp = &cp->tcps[tileno];
1652 assert(tcp != 00);
1653
1654 l_current_poc = &(tcp->pocs[pino]);
1655 assert(l_current_poc != 0);
1656
1657 /* get the progression order as a character string */
1658 prog = opj_j2k_convert_progression_order(tcp->prg);
1659 assert(strlen(prog) > 0);
1660
1661 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1662 for (i=0;i<4;++i) {
1663 switch (prog[i])
1664 {
1665 /* component wise */
1666 case 'C':
1667 tpnum *= l_current_poc->compE;
1668 break;
1669 /* resolution wise */
1670 case 'R':
1671 tpnum *= l_current_poc->resE;
1672 break;
1673 /* precinct wise */
1674 case 'P':
1675 tpnum *= l_current_poc->prcE;
1676 break;
1677 /* layer wise */
1678 case 'L':
1679 tpnum *= l_current_poc->layE;
1680 break;
1681 }
1682 /* whould we split here ? */
1683 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1684 cp->m_specific_param.m_enc.m_tp_pos=i;
1685 break;
1686 }
1687 }
1688 }
1689 else {
1690 tpnum=1;
1691 }
1692
1693 return tpnum;
1694 }
1695
opj_j2k_calculate_tp(opj_j2k_t * p_j2k,opj_cp_t * cp,OPJ_UINT32 * p_nb_tiles,opj_image_t * image,opj_event_mgr_t * p_manager)1696 static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k,
1697 opj_cp_t *cp,
1698 OPJ_UINT32 * p_nb_tiles,
1699 opj_image_t *image,
1700 opj_event_mgr_t * p_manager
1701 )
1702 {
1703 OPJ_UINT32 pino,tileno;
1704 OPJ_UINT32 l_nb_tiles;
1705 opj_tcp_t *tcp;
1706
1707 /* preconditions */
1708 assert(p_nb_tiles != 00);
1709 assert(cp != 00);
1710 assert(image != 00);
1711 assert(p_j2k != 00);
1712 assert(p_manager != 00);
1713
1714 l_nb_tiles = cp->tw * cp->th;
1715 * p_nb_tiles = 0;
1716 tcp = cp->tcps;
1717
1718 /* INDEX >> */
1719 /* TODO mergeV2: check this part which use cstr_info */
1720 /*if (p_j2k->cstr_info) {
1721 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1722
1723 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1724 OPJ_UINT32 cur_totnum_tp = 0;
1725
1726 opj_pi_update_encoding_parameters(image,cp,tileno);
1727
1728 for (pino = 0; pino <= tcp->numpocs; ++pino)
1729 {
1730 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1731
1732 *p_nb_tiles = *p_nb_tiles + tp_num;
1733
1734 cur_totnum_tp += tp_num;
1735 }
1736
1737 tcp->m_nb_tile_parts = cur_totnum_tp;
1738
1739 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1740 if (l_info_tile_ptr->tp == 00) {
1741 return OPJ_FALSE;
1742 }
1743
1744 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1745
1746 l_info_tile_ptr->num_tps = cur_totnum_tp;
1747
1748 ++l_info_tile_ptr;
1749 ++tcp;
1750 }
1751 }
1752 else */{
1753 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1754 OPJ_UINT32 cur_totnum_tp = 0;
1755
1756 opj_pi_update_encoding_parameters(image,cp,tileno);
1757
1758 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1759 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1760
1761 *p_nb_tiles = *p_nb_tiles + tp_num;
1762
1763 cur_totnum_tp += tp_num;
1764 }
1765 tcp->m_nb_tile_parts = cur_totnum_tp;
1766
1767 ++tcp;
1768 }
1769 }
1770
1771 return OPJ_TRUE;
1772 }
1773
opj_j2k_write_soc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1774 static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k,
1775 opj_stream_private_t *p_stream,
1776 opj_event_mgr_t * p_manager )
1777 {
1778 /* 2 bytes will be written */
1779 OPJ_BYTE * l_start_stream = 00;
1780
1781 /* preconditions */
1782 assert(p_stream != 00);
1783 assert(p_j2k != 00);
1784 assert(p_manager != 00);
1785
1786 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1787
1788 /* write SOC identifier */
1789 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1790
1791 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1792 return OPJ_FALSE;
1793 }
1794
1795 /* UniPG>> */
1796 #ifdef USE_JPWL
1797 /* update markers struct */
1798 /*
1799 OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1800 */
1801 assert( 0 && "TODO" );
1802 #endif /* USE_JPWL */
1803 /* <<UniPG */
1804
1805 return OPJ_TRUE;
1806 }
1807
1808 /**
1809 * Reads a SOC marker (Start of Codestream)
1810 * @param p_j2k the jpeg2000 file codec.
1811 * @param p_stream FIXME DOC
1812 * @param p_manager the user event manager.
1813 */
opj_j2k_read_soc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1814 static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k,
1815 opj_stream_private_t *p_stream,
1816 opj_event_mgr_t * p_manager
1817 )
1818 {
1819 OPJ_BYTE l_data [2];
1820 OPJ_UINT32 l_marker;
1821
1822 /* preconditions */
1823 assert(p_j2k != 00);
1824 assert(p_manager != 00);
1825 assert(p_stream != 00);
1826
1827 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1828 return OPJ_FALSE;
1829 }
1830
1831 opj_read_bytes(l_data,&l_marker,2);
1832 if (l_marker != J2K_MS_SOC) {
1833 return OPJ_FALSE;
1834 }
1835
1836 /* Next marker should be a SIZ marker in the main header */
1837 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1838
1839 /* FIXME move it in a index structure included in p_j2k*/
1840 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1841
1842 opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1843
1844 /* Add the marker to the codestream index*/
1845 if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) {
1846 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1847 return OPJ_FALSE;
1848 }
1849 return OPJ_TRUE;
1850 }
1851
opj_j2k_write_siz(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1852 static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k,
1853 opj_stream_private_t *p_stream,
1854 opj_event_mgr_t * p_manager )
1855 {
1856 OPJ_UINT32 i;
1857 OPJ_UINT32 l_size_len;
1858 OPJ_BYTE * l_current_ptr;
1859 opj_image_t * l_image = 00;
1860 opj_cp_t *cp = 00;
1861 opj_image_comp_t * l_img_comp = 00;
1862
1863 /* preconditions */
1864 assert(p_stream != 00);
1865 assert(p_j2k != 00);
1866 assert(p_manager != 00);
1867
1868 l_image = p_j2k->m_private_image;
1869 cp = &(p_j2k->m_cp);
1870 l_size_len = 40 + 3 * l_image->numcomps;
1871 l_img_comp = l_image->comps;
1872
1873 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1874
1875 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1876 if (! new_header_tile_data) {
1877 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1878 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1879 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1880 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1881 return OPJ_FALSE;
1882 }
1883 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
1884 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1885 }
1886
1887 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1888
1889 /* write SOC identifier */
1890 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1891 l_current_ptr+=2;
1892
1893 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1894 l_current_ptr+=2;
1895
1896 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1897 l_current_ptr+=2;
1898
1899 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1900 l_current_ptr+=4;
1901
1902 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1903 l_current_ptr+=4;
1904
1905 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1906 l_current_ptr+=4;
1907
1908 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1909 l_current_ptr+=4;
1910
1911 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1912 l_current_ptr+=4;
1913
1914 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1915 l_current_ptr+=4;
1916
1917 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
1918 l_current_ptr+=4;
1919
1920 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
1921 l_current_ptr+=4;
1922
1923 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
1924 l_current_ptr+=2;
1925
1926 for (i = 0; i < l_image->numcomps; ++i) {
1927 /* TODO here with MCT ? */
1928 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
1929 ++l_current_ptr;
1930
1931 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
1932 ++l_current_ptr;
1933
1934 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
1935 ++l_current_ptr;
1936
1937 ++l_img_comp;
1938 }
1939
1940 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) {
1941 return OPJ_FALSE;
1942 }
1943
1944 return OPJ_TRUE;
1945 }
1946
1947 /**
1948 * Reads a SIZ marker (image and tile size)
1949 * @param p_j2k the jpeg2000 file codec.
1950 * @param p_header_data the data contained in the SIZ box.
1951 * @param p_header_size the size of the data contained in the SIZ marker.
1952 * @param p_manager the user event manager.
1953 */
opj_j2k_read_siz(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)1954 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
1955 OPJ_BYTE * p_header_data,
1956 OPJ_UINT32 p_header_size,
1957 opj_event_mgr_t * p_manager
1958 )
1959 {
1960 OPJ_UINT32 i;
1961 OPJ_UINT32 l_nb_comp;
1962 OPJ_UINT32 l_nb_comp_remain;
1963 OPJ_UINT32 l_remaining_size;
1964 OPJ_UINT32 l_nb_tiles;
1965 OPJ_UINT32 l_tmp, l_tx1, l_ty1;
1966 opj_image_t *l_image = 00;
1967 opj_cp_t *l_cp = 00;
1968 opj_image_comp_t * l_img_comp = 00;
1969 opj_tcp_t * l_current_tile_param = 00;
1970
1971 /* preconditions */
1972 assert(p_j2k != 00);
1973 assert(p_manager != 00);
1974 assert(p_header_data != 00);
1975
1976 l_image = p_j2k->m_private_image;
1977 l_cp = &(p_j2k->m_cp);
1978
1979 /* minimum size == 39 - 3 (= minimum component parameter) */
1980 if (p_header_size < 36) {
1981 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1982 return OPJ_FALSE;
1983 }
1984
1985 l_remaining_size = p_header_size - 36;
1986 l_nb_comp = l_remaining_size / 3;
1987 l_nb_comp_remain = l_remaining_size % 3;
1988 if (l_nb_comp_remain != 0){
1989 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1990 return OPJ_FALSE;
1991 }
1992
1993 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1994 p_header_data+=2;
1995 l_cp->rsiz = (OPJ_UINT16) l_tmp;
1996 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1997 p_header_data+=4;
1998 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1999 p_header_data+=4;
2000 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2001 p_header_data+=4;
2002 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2003 p_header_data+=4;
2004 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2005 p_header_data+=4;
2006 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2007 p_header_data+=4;
2008 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2009 p_header_data+=4;
2010 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2011 p_header_data+=4;
2012 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2013 p_header_data+=2;
2014 if (l_tmp < 16385)
2015 l_image->numcomps = (OPJ_UINT16) l_tmp;
2016 else {
2017 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2018 return OPJ_FALSE;
2019 }
2020
2021 if (l_image->numcomps != l_nb_comp) {
2022 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);
2023 return OPJ_FALSE;
2024 }
2025
2026 /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
2027 /* testcase issue427-null-image-size.jp2 */
2028 if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
2029 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
2030 return OPJ_FALSE;
2031 }
2032 /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
2033 if (!(l_cp->tdx * l_cp->tdy)) {
2034 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);
2035 return OPJ_FALSE;
2036 }
2037
2038 /* testcase 1610.pdf.SIGSEGV.59c.681 */
2039 if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
2040 opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
2041 return OPJ_FALSE;
2042 }
2043
2044 /* testcase issue427-illegal-tile-offset.jp2 */
2045 l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */
2046 l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */
2047 if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0) ) {
2048 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: illegal tile offset\n");
2049 return OPJ_FALSE;
2050 }
2051
2052 #ifdef USE_JPWL
2053 if (l_cp->correct) {
2054 /* if JPWL is on, we check whether TX errors have damaged
2055 too much the SIZ parameters */
2056 if (!(l_image->x1 * l_image->y1)) {
2057 opj_event_msg(p_manager, EVT_ERROR,
2058 "JPWL: bad image size (%d x %d)\n",
2059 l_image->x1, l_image->y1);
2060 if (!JPWL_ASSUME || JPWL_ASSUME) {
2061 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2062 return OPJ_FALSE;
2063 }
2064 }
2065
2066 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2067 if (l_image->numcomps != ((len - 38) / 3)) {
2068 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2069 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2070 l_image->numcomps, ((len - 38) / 3));
2071 if (!JPWL_ASSUME) {
2072 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2073 return OPJ_FALSE;
2074 }
2075 */ /* we try to correct */
2076 /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
2077 if (l_image->numcomps < ((len - 38) / 3)) {
2078 len = 38 + 3 * l_image->numcomps;
2079 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2080 len);
2081 } else {
2082 l_image->numcomps = ((len - 38) / 3);
2083 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2084 l_image->numcomps);
2085 }
2086 }
2087 */
2088
2089 /* update components number in the jpwl_exp_comps filed */
2090 l_cp->exp_comps = l_image->numcomps;
2091 }
2092 #endif /* USE_JPWL */
2093
2094 /* Allocate the resulting image components */
2095 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2096 if (l_image->comps == 00){
2097 l_image->numcomps = 0;
2098 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2099 return OPJ_FALSE;
2100 }
2101
2102 l_img_comp = l_image->comps;
2103
2104 /* Read the component information */
2105 for (i = 0; i < l_image->numcomps; ++i){
2106 OPJ_UINT32 tmp;
2107 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2108 ++p_header_data;
2109 l_img_comp->prec = (tmp & 0x7f) + 1;
2110 l_img_comp->sgnd = tmp >> 7;
2111 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2112 ++p_header_data;
2113 l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2114 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2115 ++p_header_data;
2116 l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2117 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2118 l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
2119 opj_event_msg(p_manager, EVT_ERROR,
2120 "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n",
2121 i, l_img_comp->dx, l_img_comp->dy);
2122 return OPJ_FALSE;
2123 }
2124 if( l_img_comp->prec > 38) { /* TODO openjpeg won't handle more than ? */
2125 opj_event_msg(p_manager, EVT_ERROR,
2126 "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm)\n",
2127 i, l_img_comp->prec);
2128 return OPJ_FALSE;
2129 }
2130
2131 #ifdef USE_JPWL
2132 if (l_cp->correct) {
2133 /* if JPWL is on, we check whether TX errors have damaged
2134 too much the SIZ parameters, again */
2135 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2136 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2137 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2138 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2139 if (!JPWL_ASSUME) {
2140 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2141 return OPJ_FALSE;
2142 }
2143 /* we try to correct */
2144 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2145 if (!l_image->comps[i].dx) {
2146 l_image->comps[i].dx = 1;
2147 opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2148 i, l_image->comps[i].dx);
2149 }
2150 if (!l_image->comps[i].dy) {
2151 l_image->comps[i].dy = 1;
2152 opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2153 i, l_image->comps[i].dy);
2154 }
2155 }
2156 }
2157 #endif /* USE_JPWL */
2158 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2159 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2160 ++l_img_comp;
2161 }
2162
2163 /* Compute the number of tiles */
2164 l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2165 l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2166
2167 /* Check that the number of tiles is valid */
2168 if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2169 opj_event_msg( p_manager, EVT_ERROR,
2170 "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2171 l_cp->tw, l_cp->th);
2172 return OPJ_FALSE;
2173 }
2174 l_nb_tiles = l_cp->tw * l_cp->th;
2175
2176 /* Define the tiles which will be decoded */
2177 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2178 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;
2179 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;
2180 p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0, l_cp->tdx);
2181 p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0, l_cp->tdy);
2182 }
2183 else {
2184 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2185 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2186 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2187 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2188 }
2189
2190 #ifdef USE_JPWL
2191 if (l_cp->correct) {
2192 /* if JPWL is on, we check whether TX errors have damaged
2193 too much the SIZ parameters */
2194 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2195 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2196 "JPWL: bad number of tiles (%d x %d)\n",
2197 l_cp->tw, l_cp->th);
2198 if (!JPWL_ASSUME) {
2199 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2200 return OPJ_FALSE;
2201 }
2202 /* we try to correct */
2203 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2204 if (l_cp->tw < 1) {
2205 l_cp->tw= 1;
2206 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2207 l_cp->tw);
2208 }
2209 if (l_cp->tw > l_cp->max_tiles) {
2210 l_cp->tw= 1;
2211 opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2212 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2213 l_cp->max_tiles, l_cp->tw);
2214 }
2215 if (l_cp->th < 1) {
2216 l_cp->th= 1;
2217 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2218 l_cp->th);
2219 }
2220 if (l_cp->th > l_cp->max_tiles) {
2221 l_cp->th= 1;
2222 opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2223 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2224 l_cp->max_tiles, l_cp->th);
2225 }
2226 }
2227 }
2228 #endif /* USE_JPWL */
2229
2230 /* memory allocations */
2231 l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2232 if (l_cp->tcps == 00) {
2233 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2234 return OPJ_FALSE;
2235 }
2236
2237 #ifdef USE_JPWL
2238 if (l_cp->correct) {
2239 if (!l_cp->tcps) {
2240 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2241 "JPWL: could not alloc tcps field of cp\n");
2242 if (!JPWL_ASSUME || JPWL_ASSUME) {
2243 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2244 return OPJ_FALSE;
2245 }
2246 }
2247 }
2248 #endif /* USE_JPWL */
2249
2250 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2251 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2252 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2253 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2254 return OPJ_FALSE;
2255 }
2256
2257 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2258 (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS ,sizeof(opj_mct_data_t));
2259
2260 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2261 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2262 return OPJ_FALSE;
2263 }
2264 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2265
2266 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2267 (opj_simple_mcc_decorrelation_data_t*)
2268 opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, sizeof(opj_simple_mcc_decorrelation_data_t));
2269
2270 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2271 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2272 return OPJ_FALSE;
2273 }
2274 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2275
2276 /* set up default dc level shift */
2277 for (i=0;i<l_image->numcomps;++i) {
2278 if (! l_image->comps[i].sgnd) {
2279 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2280 }
2281 }
2282
2283 l_current_tile_param = l_cp->tcps;
2284 for (i = 0; i < l_nb_tiles; ++i) {
2285 l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2286 if (l_current_tile_param->tccps == 00) {
2287 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2288 return OPJ_FALSE;
2289 }
2290
2291 ++l_current_tile_param;
2292 }
2293
2294 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2295 opj_image_comp_header_update(l_image,l_cp);
2296
2297 return OPJ_TRUE;
2298 }
2299
opj_j2k_write_com(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2300 static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k,
2301 opj_stream_private_t *p_stream,
2302 opj_event_mgr_t * p_manager
2303 )
2304 {
2305 OPJ_UINT32 l_comment_size;
2306 OPJ_UINT32 l_total_com_size;
2307 const OPJ_CHAR *l_comment;
2308 OPJ_BYTE * l_current_ptr = 00;
2309
2310 /* preconditions */
2311 assert(p_j2k != 00);
2312 assert(p_stream != 00);
2313 assert(p_manager != 00);
2314
2315 l_comment = p_j2k->m_cp.comment;
2316 l_comment_size = (OPJ_UINT32)strlen(l_comment);
2317 l_total_com_size = l_comment_size + 6;
2318
2319 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2320 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);
2321 if (! new_header_tile_data) {
2322 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2323 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2324 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2325 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
2326 return OPJ_FALSE;
2327 }
2328 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2329 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2330 }
2331
2332 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2333
2334 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2335 l_current_ptr+=2;
2336
2337 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2338 l_current_ptr+=2;
2339
2340 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2341 l_current_ptr+=2;
2342
2343 memcpy( l_current_ptr,l_comment,l_comment_size);
2344
2345 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) {
2346 return OPJ_FALSE;
2347 }
2348
2349 return OPJ_TRUE;
2350 }
2351
2352 /**
2353 * Reads a COM marker (comments)
2354 * @param p_j2k the jpeg2000 file codec.
2355 * @param p_header_data the data contained in the COM box.
2356 * @param p_header_size the size of the data contained in the COM marker.
2357 * @param p_manager the user event manager.
2358 */
opj_j2k_read_com(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2359 static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k,
2360 OPJ_BYTE * p_header_data,
2361 OPJ_UINT32 p_header_size,
2362 opj_event_mgr_t * p_manager
2363 )
2364 {
2365 /* preconditions */
2366 assert(p_j2k != 00);
2367 assert(p_manager != 00);
2368 assert(p_header_data != 00);
2369 (void)p_header_size;
2370
2371 return OPJ_TRUE;
2372 }
2373
opj_j2k_write_cod(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2374 static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k,
2375 opj_stream_private_t *p_stream,
2376 opj_event_mgr_t * p_manager )
2377 {
2378 opj_cp_t *l_cp = 00;
2379 opj_tcp_t *l_tcp = 00;
2380 OPJ_UINT32 l_code_size,l_remaining_size;
2381 OPJ_BYTE * l_current_data = 00;
2382
2383 /* preconditions */
2384 assert(p_j2k != 00);
2385 assert(p_manager != 00);
2386 assert(p_stream != 00);
2387
2388 l_cp = &(p_j2k->m_cp);
2389 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2390 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2391 l_remaining_size = l_code_size;
2392
2393 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2394 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2395 if (! new_header_tile_data) {
2396 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2397 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2398 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2399 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2400 return OPJ_FALSE;
2401 }
2402 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2403 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2404 }
2405
2406 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2407
2408 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2409 l_current_data += 2;
2410
2411 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2412 l_current_data += 2;
2413
2414 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2415 ++l_current_data;
2416
2417 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2418 ++l_current_data;
2419
2420 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2421 l_current_data+=2;
2422
2423 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2424 ++l_current_data;
2425
2426 l_remaining_size -= 9;
2427
2428 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2429 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2430 return OPJ_FALSE;
2431 }
2432
2433 if (l_remaining_size != 0) {
2434 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2435 return OPJ_FALSE;
2436 }
2437
2438 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) {
2439 return OPJ_FALSE;
2440 }
2441
2442 return OPJ_TRUE;
2443 }
2444
2445 /**
2446 * Reads a COD marker (Coding Styke defaults)
2447 * @param p_header_data the data contained in the COD box.
2448 * @param p_j2k the jpeg2000 codec.
2449 * @param p_header_size the size of the data contained in the COD marker.
2450 * @param p_manager the user event manager.
2451 */
opj_j2k_read_cod(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2452 static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k,
2453 OPJ_BYTE * p_header_data,
2454 OPJ_UINT32 p_header_size,
2455 opj_event_mgr_t * p_manager
2456 )
2457 {
2458 /* loop */
2459 OPJ_UINT32 i;
2460 OPJ_UINT32 l_tmp;
2461 opj_cp_t *l_cp = 00;
2462 opj_tcp_t *l_tcp = 00;
2463 opj_image_t *l_image = 00;
2464
2465 /* preconditions */
2466 assert(p_header_data != 00);
2467 assert(p_j2k != 00);
2468 assert(p_manager != 00);
2469
2470 l_image = p_j2k->m_private_image;
2471 l_cp = &(p_j2k->m_cp);
2472
2473 /* If we are in the first tile-part header of the current tile */
2474 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2475 &l_cp->tcps[p_j2k->m_current_tile_number] :
2476 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2477
2478 /* Only one COD per tile */
2479 if (l_tcp->cod) {
2480 opj_event_msg(p_manager, EVT_ERROR, "COD marker already read. No more than one COD marker per tile.\n");
2481 return OPJ_FALSE;
2482 }
2483 l_tcp->cod = 1;
2484
2485 /* Make sure room is sufficient */
2486 if (p_header_size < 5) {
2487 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2488 return OPJ_FALSE;
2489 }
2490
2491 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2492 ++p_header_data;
2493 /* Make sure we know how to decode this */
2494 if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | J2K_CP_CSTY_EPH)) != 0U) {
2495 opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
2496 return OPJ_FALSE;
2497 }
2498 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2499 ++p_header_data;
2500 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2501 /* Make sure progression order is valid */
2502 if (l_tcp->prg > OPJ_CPRL ) {
2503 opj_event_msg(p_manager, EVT_ERROR, "Unknown progression order in COD marker\n");
2504 l_tcp->prg = OPJ_PROG_UNKNOWN;
2505 }
2506 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2507 p_header_data+=2;
2508
2509 if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) {
2510 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of layers in COD marker : %d not in range [1-65535]\n", l_tcp->numlayers);
2511 return OPJ_FALSE;
2512 }
2513
2514 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2515 if (l_cp->m_specific_param.m_dec.m_layer) {
2516 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2517 }
2518 else {
2519 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2520 }
2521
2522 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2523 ++p_header_data;
2524
2525 p_header_size -= 5;
2526 for (i = 0; i < l_image->numcomps; ++i) {
2527 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2528 }
2529
2530 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2531 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2532 return OPJ_FALSE;
2533 }
2534
2535 if (p_header_size != 0) {
2536 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2537 return OPJ_FALSE;
2538 }
2539
2540 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2541 opj_j2k_copy_tile_component_parameters(p_j2k);
2542
2543 /* Index */
2544 #ifdef WIP_REMOVE_MSD
2545 if (p_j2k->cstr_info) {
2546 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2547 p_j2k->cstr_info->prog = l_tcp->prg;
2548 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2549 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2550 for (i = 0; i < l_image->numcomps; ++i) {
2551 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2552 }
2553 }
2554 #endif
2555
2556 return OPJ_TRUE;
2557 }
2558
2559 #if 0
2560 static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
2561 OPJ_UINT32 p_comp_no,
2562 opj_stream_private_t *p_stream,
2563 opj_event_mgr_t * p_manager )
2564 {
2565 OPJ_UINT32 l_coc_size,l_remaining_size;
2566 OPJ_UINT32 l_comp_room;
2567
2568 /* preconditions */
2569 assert(p_j2k != 00);
2570 assert(p_manager != 00);
2571 assert(p_stream != 00);
2572
2573 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2574
2575 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2576
2577 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2578 OPJ_BYTE *new_header_tile_data;
2579 /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2580 = (OPJ_BYTE*)opj_realloc(
2581 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2582 l_coc_size);*/
2583
2584 new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2585 if (! new_header_tile_data) {
2586 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2587 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2588 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2589 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2590 return OPJ_FALSE;
2591 }
2592 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2593 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2594 }
2595
2596 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);
2597
2598 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) {
2599 return OPJ_FALSE;
2600 }
2601
2602 return OPJ_TRUE;
2603 }
2604 #endif
2605
2606 #if 0
2607 static void opj_j2k_write_coc_in_memory( opj_j2k_t *p_j2k,
2608 OPJ_UINT32 p_comp_no,
2609 OPJ_BYTE * p_data,
2610 OPJ_UINT32 * p_data_written,
2611 opj_event_mgr_t * p_manager
2612 )
2613 {
2614 opj_cp_t *l_cp = 00;
2615 opj_tcp_t *l_tcp = 00;
2616 OPJ_UINT32 l_coc_size,l_remaining_size;
2617 OPJ_BYTE * l_current_data = 00;
2618 opj_image_t *l_image = 00;
2619 OPJ_UINT32 l_comp_room;
2620
2621 /* preconditions */
2622 assert(p_j2k != 00);
2623 assert(p_manager != 00);
2624
2625 l_cp = &(p_j2k->m_cp);
2626 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2627 l_image = p_j2k->m_private_image;
2628 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2629
2630 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2631 l_remaining_size = l_coc_size;
2632
2633 l_current_data = p_data;
2634
2635 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2636 l_current_data += 2;
2637
2638 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2639 l_current_data += 2;
2640
2641 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2642 l_current_data+=l_comp_room;
2643
2644 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2645 ++l_current_data;
2646
2647 l_remaining_size -= (5 + l_comp_room);
2648 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2649 * p_data_written = l_coc_size;
2650 }
2651 #endif
2652
opj_j2k_get_max_coc_size(opj_j2k_t * p_j2k)2653 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2654 {
2655 OPJ_UINT32 i,j;
2656 OPJ_UINT32 l_nb_comp;
2657 OPJ_UINT32 l_nb_tiles;
2658 OPJ_UINT32 l_max = 0;
2659
2660 /* preconditions */
2661
2662 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2663 l_nb_comp = p_j2k->m_private_image->numcomps;
2664
2665 for (i=0;i<l_nb_tiles;++i) {
2666 for (j=0;j<l_nb_comp;++j) {
2667 l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2668 }
2669 }
2670
2671 return 6 + l_max;
2672 }
2673
2674 /**
2675 * Reads a COC marker (Coding Style Component)
2676 * @param p_header_data the data contained in the COC box.
2677 * @param p_j2k the jpeg2000 codec.
2678 * @param p_header_size the size of the data contained in the COC marker.
2679 * @param p_manager the user event manager.
2680 */
opj_j2k_read_coc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2681 static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k,
2682 OPJ_BYTE * p_header_data,
2683 OPJ_UINT32 p_header_size,
2684 opj_event_mgr_t * p_manager
2685 )
2686 {
2687 opj_cp_t *l_cp = NULL;
2688 opj_tcp_t *l_tcp = NULL;
2689 opj_image_t *l_image = NULL;
2690 OPJ_UINT32 l_comp_room;
2691 OPJ_UINT32 l_comp_no;
2692
2693 /* preconditions */
2694 assert(p_header_data != 00);
2695 assert(p_j2k != 00);
2696 assert(p_manager != 00);
2697
2698 l_cp = &(p_j2k->m_cp);
2699 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2700 &l_cp->tcps[p_j2k->m_current_tile_number] :
2701 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2702 l_image = p_j2k->m_private_image;
2703
2704 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2705
2706 /* make sure room is sufficient*/
2707 if (p_header_size < l_comp_room + 1) {
2708 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2709 return OPJ_FALSE;
2710 }
2711 p_header_size -= l_comp_room + 1;
2712
2713 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2714 p_header_data += l_comp_room;
2715 if (l_comp_no >= l_image->numcomps) {
2716 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2717 return OPJ_FALSE;
2718 }
2719
2720 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2721 ++p_header_data ;
2722
2723 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2724 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2725 return OPJ_FALSE;
2726 }
2727
2728 if (p_header_size != 0) {
2729 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2730 return OPJ_FALSE;
2731 }
2732 return OPJ_TRUE;
2733 }
2734
opj_j2k_write_qcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2735 static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k,
2736 opj_stream_private_t *p_stream,
2737 opj_event_mgr_t * p_manager
2738 )
2739 {
2740 OPJ_UINT32 l_qcd_size,l_remaining_size;
2741 OPJ_BYTE * l_current_data = 00;
2742
2743 /* preconditions */
2744 assert(p_j2k != 00);
2745 assert(p_manager != 00);
2746 assert(p_stream != 00);
2747
2748 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2749 l_remaining_size = l_qcd_size;
2750
2751 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2752 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2753 if (! new_header_tile_data) {
2754 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2755 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2756 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2757 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2758 return OPJ_FALSE;
2759 }
2760 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2761 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2762 }
2763
2764 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2765
2766 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2767 l_current_data += 2;
2768
2769 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2770 l_current_data += 2;
2771
2772 l_remaining_size -= 4;
2773
2774 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2775 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2776 return OPJ_FALSE;
2777 }
2778
2779 if (l_remaining_size != 0) {
2780 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2781 return OPJ_FALSE;
2782 }
2783
2784 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) {
2785 return OPJ_FALSE;
2786 }
2787
2788 return OPJ_TRUE;
2789 }
2790
2791 /**
2792 * Reads a QCD marker (Quantization defaults)
2793 * @param p_header_data the data contained in the QCD box.
2794 * @param p_j2k the jpeg2000 codec.
2795 * @param p_header_size the size of the data contained in the QCD marker.
2796 * @param p_manager the user event manager.
2797 */
opj_j2k_read_qcd(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2798 static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k,
2799 OPJ_BYTE * p_header_data,
2800 OPJ_UINT32 p_header_size,
2801 opj_event_mgr_t * p_manager
2802 )
2803 {
2804 /* preconditions */
2805 assert(p_header_data != 00);
2806 assert(p_j2k != 00);
2807 assert(p_manager != 00);
2808
2809 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2810 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2811 return OPJ_FALSE;
2812 }
2813
2814 if (p_header_size != 0) {
2815 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2816 return OPJ_FALSE;
2817 }
2818
2819 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2820 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2821
2822 return OPJ_TRUE;
2823 }
2824
2825 #if 0
2826 static OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k,
2827 OPJ_UINT32 p_comp_no,
2828 opj_stream_private_t *p_stream,
2829 opj_event_mgr_t * p_manager
2830 )
2831 {
2832 OPJ_UINT32 l_qcc_size,l_remaining_size;
2833
2834 /* preconditions */
2835 assert(p_j2k != 00);
2836 assert(p_manager != 00);
2837 assert(p_stream != 00);
2838
2839 l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2840 l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
2841 l_remaining_size = l_qcc_size;
2842
2843 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2844 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
2845 if (! new_header_tile_data) {
2846 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2847 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2848 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2849 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
2850 return OPJ_FALSE;
2851 }
2852 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2853 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2854 }
2855
2856 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);
2857
2858 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) {
2859 return OPJ_FALSE;
2860 }
2861
2862 return OPJ_TRUE;
2863 }
2864 #endif
2865
2866 #if 0
2867 static void opj_j2k_write_qcc_in_memory( opj_j2k_t *p_j2k,
2868 OPJ_UINT32 p_comp_no,
2869 OPJ_BYTE * p_data,
2870 OPJ_UINT32 * p_data_written,
2871 opj_event_mgr_t * p_manager
2872 )
2873 {
2874 OPJ_UINT32 l_qcc_size,l_remaining_size;
2875 OPJ_BYTE * l_current_data = 00;
2876
2877 /* preconditions */
2878 assert(p_j2k != 00);
2879 assert(p_manager != 00);
2880
2881 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2882 l_remaining_size = l_qcc_size;
2883
2884 l_current_data = p_data;
2885
2886 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2887 l_current_data += 2;
2888
2889 if (p_j2k->m_private_image->numcomps <= 256) {
2890 --l_qcc_size;
2891
2892 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2893 l_current_data += 2;
2894
2895 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2896 ++l_current_data;
2897
2898 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2899 l_remaining_size -= 6;
2900 }
2901 else {
2902 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2903 l_current_data += 2;
2904
2905 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2906 l_current_data+=2;
2907
2908 l_remaining_size -= 6;
2909 }
2910
2911 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2912
2913 *p_data_written = l_qcc_size;
2914 }
2915 #endif
2916
opj_j2k_get_max_qcc_size(opj_j2k_t * p_j2k)2917 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
2918 {
2919 return opj_j2k_get_max_coc_size(p_j2k);
2920 }
2921
2922 /**
2923 * Reads a QCC marker (Quantization component)
2924 * @param p_header_data the data contained in the QCC box.
2925 * @param p_j2k the jpeg2000 codec.
2926 * @param p_header_size the size of the data contained in the QCC marker.
2927 * @param p_manager the user event manager.
2928 */
opj_j2k_read_qcc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2929 static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k,
2930 OPJ_BYTE * p_header_data,
2931 OPJ_UINT32 p_header_size,
2932 opj_event_mgr_t * p_manager
2933 )
2934 {
2935 OPJ_UINT32 l_num_comp,l_comp_no;
2936
2937 /* preconditions */
2938 assert(p_header_data != 00);
2939 assert(p_j2k != 00);
2940 assert(p_manager != 00);
2941
2942 l_num_comp = p_j2k->m_private_image->numcomps;
2943
2944 if (l_num_comp <= 256) {
2945 if (p_header_size < 1) {
2946 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2947 return OPJ_FALSE;
2948 }
2949 opj_read_bytes(p_header_data,&l_comp_no,1);
2950 ++p_header_data;
2951 --p_header_size;
2952 }
2953 else {
2954 if (p_header_size < 2) {
2955 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2956 return OPJ_FALSE;
2957 }
2958 opj_read_bytes(p_header_data,&l_comp_no,2);
2959 p_header_data+=2;
2960 p_header_size-=2;
2961 }
2962
2963 #ifdef USE_JPWL
2964 if (p_j2k->m_cp.correct) {
2965
2966 static OPJ_UINT32 backup_compno = 0;
2967
2968 /* compno is negative or larger than the number of components!!! */
2969 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2970 opj_event_msg(p_manager, EVT_ERROR,
2971 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2972 l_comp_no, l_num_comp);
2973 if (!JPWL_ASSUME) {
2974 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2975 return OPJ_FALSE;
2976 }
2977 /* we try to correct */
2978 l_comp_no = backup_compno % l_num_comp;
2979 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
2980 "- setting component number to %d\n",
2981 l_comp_no);
2982 }
2983
2984 /* keep your private count of tiles */
2985 backup_compno++;
2986 };
2987 #endif /* USE_JPWL */
2988
2989 if (l_comp_no >= p_j2k->m_private_image->numcomps) {
2990 opj_event_msg(p_manager, EVT_ERROR,
2991 "Invalid component number: %d, regarding the number of components %d\n",
2992 l_comp_no, p_j2k->m_private_image->numcomps);
2993 return OPJ_FALSE;
2994 }
2995
2996 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2997 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2998 return OPJ_FALSE;
2999 }
3000
3001 if (p_header_size != 0) {
3002 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3003 return OPJ_FALSE;
3004 }
3005
3006 return OPJ_TRUE;
3007 }
3008
opj_j2k_write_poc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3009 static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k,
3010 opj_stream_private_t *p_stream,
3011 opj_event_mgr_t * p_manager
3012 )
3013 {
3014 OPJ_UINT32 l_nb_comp;
3015 OPJ_UINT32 l_nb_poc;
3016 OPJ_UINT32 l_poc_size;
3017 OPJ_UINT32 l_written_size = 0;
3018 opj_tcp_t *l_tcp = 00;
3019 OPJ_UINT32 l_poc_room;
3020
3021 /* preconditions */
3022 assert(p_j2k != 00);
3023 assert(p_manager != 00);
3024 assert(p_stream != 00);
3025
3026 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3027 l_nb_comp = p_j2k->m_private_image->numcomps;
3028 l_nb_poc = 1 + l_tcp->numpocs;
3029
3030 if (l_nb_comp <= 256) {
3031 l_poc_room = 1;
3032 }
3033 else {
3034 l_poc_room = 2;
3035 }
3036 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3037
3038 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3039 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
3040 if (! new_header_tile_data) {
3041 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3042 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3043 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3044 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
3045 return OPJ_FALSE;
3046 }
3047 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3048 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3049 }
3050
3051 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3052
3053 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) {
3054 return OPJ_FALSE;
3055 }
3056
3057 return OPJ_TRUE;
3058 }
3059
opj_j2k_write_poc_in_memory(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,opj_event_mgr_t * p_manager)3060 static void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k,
3061 OPJ_BYTE * p_data,
3062 OPJ_UINT32 * p_data_written,
3063 opj_event_mgr_t * p_manager
3064 )
3065 {
3066 OPJ_UINT32 i;
3067 OPJ_BYTE * l_current_data = 00;
3068 OPJ_UINT32 l_nb_comp;
3069 OPJ_UINT32 l_nb_poc;
3070 OPJ_UINT32 l_poc_size;
3071 opj_image_t *l_image = 00;
3072 opj_tcp_t *l_tcp = 00;
3073 opj_tccp_t *l_tccp = 00;
3074 opj_poc_t *l_current_poc = 00;
3075 OPJ_UINT32 l_poc_room;
3076
3077 /* preconditions */
3078 assert(p_j2k != 00);
3079 assert(p_manager != 00);
3080
3081 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3082 l_tccp = &l_tcp->tccps[0];
3083 l_image = p_j2k->m_private_image;
3084 l_nb_comp = l_image->numcomps;
3085 l_nb_poc = 1 + l_tcp->numpocs;
3086
3087 if (l_nb_comp <= 256) {
3088 l_poc_room = 1;
3089 }
3090 else {
3091 l_poc_room = 2;
3092 }
3093
3094 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3095
3096 l_current_data = p_data;
3097
3098 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3099 l_current_data += 2;
3100
3101 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3102 l_current_data += 2;
3103
3104 l_current_poc = l_tcp->pocs;
3105 for (i = 0; i < l_nb_poc; ++i) {
3106 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3107 ++l_current_data;
3108
3109 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3110 l_current_data+=l_poc_room;
3111
3112 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3113 l_current_data+=2;
3114
3115 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3116 ++l_current_data;
3117
3118 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3119 l_current_data+=l_poc_room;
3120
3121 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3122 ++l_current_data;
3123
3124 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3125 l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
3126 l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
3127 l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
3128
3129 ++l_current_poc;
3130 }
3131
3132 *p_data_written = l_poc_size;
3133 }
3134
opj_j2k_get_max_poc_size(opj_j2k_t * p_j2k)3135 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3136 {
3137 opj_tcp_t * l_tcp = 00;
3138 OPJ_UINT32 l_nb_tiles = 0;
3139 OPJ_UINT32 l_max_poc = 0;
3140 OPJ_UINT32 i;
3141
3142 l_tcp = p_j2k->m_cp.tcps;
3143 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3144
3145 for (i=0;i<l_nb_tiles;++i) {
3146 l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);
3147 ++l_tcp;
3148 }
3149
3150 ++l_max_poc;
3151
3152 return 4 + 9 * l_max_poc;
3153 }
3154
opj_j2k_get_max_toc_size(opj_j2k_t * p_j2k)3155 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)
3156 {
3157 OPJ_UINT32 i;
3158 OPJ_UINT32 l_nb_tiles;
3159 OPJ_UINT32 l_max = 0;
3160 opj_tcp_t * l_tcp = 00;
3161
3162 l_tcp = p_j2k->m_cp.tcps;
3163 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3164
3165 for (i=0;i<l_nb_tiles;++i) {
3166 l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);
3167
3168 ++l_tcp;
3169 }
3170
3171 return 12 * l_max;
3172 }
3173
opj_j2k_get_specific_header_sizes(opj_j2k_t * p_j2k)3174 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3175 {
3176 OPJ_UINT32 l_nb_bytes = 0;
3177 OPJ_UINT32 l_nb_comps;
3178 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3179
3180 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3181 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3182
3183 if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
3184 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3185 l_nb_bytes += l_nb_comps * l_coc_bytes;
3186
3187 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3188 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3189 }
3190
3191 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3192
3193 /*** DEVELOPER CORNER, Add room for your headers ***/
3194
3195 return l_nb_bytes;
3196 }
3197
3198 /**
3199 * Reads a POC marker (Progression Order Change)
3200 *
3201 * @param p_header_data the data contained in the POC box.
3202 * @param p_j2k the jpeg2000 codec.
3203 * @param p_header_size the size of the data contained in the POC marker.
3204 * @param p_manager the user event manager.
3205 */
opj_j2k_read_poc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3206 static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k,
3207 OPJ_BYTE * p_header_data,
3208 OPJ_UINT32 p_header_size,
3209 opj_event_mgr_t * p_manager
3210 )
3211 {
3212 OPJ_UINT32 i, l_nb_comp, l_tmp;
3213 opj_image_t * l_image = 00;
3214 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3215 OPJ_UINT32 l_chunk_size, l_comp_room;
3216
3217 opj_cp_t *l_cp = 00;
3218 opj_tcp_t *l_tcp = 00;
3219 opj_poc_t *l_current_poc = 00;
3220
3221 /* preconditions */
3222 assert(p_header_data != 00);
3223 assert(p_j2k != 00);
3224 assert(p_manager != 00);
3225
3226 l_image = p_j2k->m_private_image;
3227 l_nb_comp = l_image->numcomps;
3228 if (l_nb_comp <= 256) {
3229 l_comp_room = 1;
3230 }
3231 else {
3232 l_comp_room = 2;
3233 }
3234 l_chunk_size = 5 + 2 * l_comp_room;
3235 l_current_poc_nb = p_header_size / l_chunk_size;
3236 l_current_poc_remaining = p_header_size % l_chunk_size;
3237
3238 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3239 opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3240 return OPJ_FALSE;
3241 }
3242
3243 l_cp = &(p_j2k->m_cp);
3244 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3245 &l_cp->tcps[p_j2k->m_current_tile_number] :
3246 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3247 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3248 l_current_poc_nb += l_old_poc_nb;
3249
3250 if(l_current_poc_nb >= 32)
3251 {
3252 opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
3253 return OPJ_FALSE;
3254 }
3255 assert(l_current_poc_nb < 32);
3256
3257 /* now poc is in use.*/
3258 l_tcp->POC = 1;
3259
3260 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3261 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3262 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3263 ++p_header_data;
3264 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3265 p_header_data+=l_comp_room;
3266 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3267 /* make sure layer end is in acceptable bounds */
3268 l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
3269 p_header_data+=2;
3270 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3271 ++p_header_data;
3272 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3273 p_header_data+=l_comp_room;
3274 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3275 ++p_header_data;
3276 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3277 /* make sure comp is in acceptable bounds */
3278 l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3279 ++l_current_poc;
3280 }
3281
3282 l_tcp->numpocs = l_current_poc_nb - 1;
3283 return OPJ_TRUE;
3284 }
3285
3286 /**
3287 * Reads a CRG marker (Component registration)
3288 *
3289 * @param p_header_data the data contained in the TLM box.
3290 * @param p_j2k the jpeg2000 codec.
3291 * @param p_header_size the size of the data contained in the TLM marker.
3292 * @param p_manager the user event manager.
3293 */
opj_j2k_read_crg(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3294 static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k,
3295 OPJ_BYTE * p_header_data,
3296 OPJ_UINT32 p_header_size,
3297 opj_event_mgr_t * p_manager
3298 )
3299 {
3300 OPJ_UINT32 l_nb_comp;
3301 /* preconditions */
3302 assert(p_header_data != 00);
3303 assert(p_j2k != 00);
3304 assert(p_manager != 00);
3305
3306 l_nb_comp = p_j2k->m_private_image->numcomps;
3307
3308 if (p_header_size != l_nb_comp *4) {
3309 opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3310 return OPJ_FALSE;
3311 }
3312 /* Do not care of this at the moment since only local variables are set here */
3313 /*
3314 for
3315 (i = 0; i < l_nb_comp; ++i)
3316 {
3317 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3318 p_header_data+=2;
3319 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3320 p_header_data+=2;
3321 }
3322 */
3323 return OPJ_TRUE;
3324 }
3325
3326 /**
3327 * Reads a TLM marker (Tile Length Marker)
3328 *
3329 * @param p_header_data the data contained in the TLM box.
3330 * @param p_j2k the jpeg2000 codec.
3331 * @param p_header_size the size of the data contained in the TLM marker.
3332 * @param p_manager the user event manager.
3333 */
opj_j2k_read_tlm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3334 static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k,
3335 OPJ_BYTE * p_header_data,
3336 OPJ_UINT32 p_header_size,
3337 opj_event_mgr_t * p_manager
3338 )
3339 {
3340 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3341 /* preconditions */
3342 assert(p_header_data != 00);
3343 assert(p_j2k != 00);
3344 assert(p_manager != 00);
3345
3346 if (p_header_size < 2) {
3347 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3348 return OPJ_FALSE;
3349 }
3350 p_header_size -= 2;
3351
3352 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3353 ++p_header_data;
3354 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3355 ++p_header_data;
3356
3357 l_ST = ((l_Stlm >> 4) & 0x3);
3358 l_SP = (l_Stlm >> 6) & 0x1;
3359
3360 l_Ptlm_size = (l_SP + 1) * 2;
3361 l_quotient = l_Ptlm_size + l_ST;
3362
3363 l_tot_num_tp_remaining = p_header_size % l_quotient;
3364
3365 if (l_tot_num_tp_remaining != 0) {
3366 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3367 return OPJ_FALSE;
3368 }
3369 /* FIXME Do not care of this at the moment since only local variables are set here */
3370 /*
3371 for
3372 (i = 0; i < l_tot_num_tp; ++i)
3373 {
3374 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3375 p_header_data += l_ST;
3376 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3377 p_header_data += l_Ptlm_size;
3378 }*/
3379 return OPJ_TRUE;
3380 }
3381
3382 /**
3383 * Reads a PLM marker (Packet length, main header marker)
3384 *
3385 * @param p_header_data the data contained in the TLM box.
3386 * @param p_j2k the jpeg2000 codec.
3387 * @param p_header_size the size of the data contained in the TLM marker.
3388 * @param p_manager the user event manager.
3389 */
opj_j2k_read_plm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3390 static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k,
3391 OPJ_BYTE * p_header_data,
3392 OPJ_UINT32 p_header_size,
3393 opj_event_mgr_t * p_manager
3394 )
3395 {
3396 /* preconditions */
3397 assert(p_header_data != 00);
3398 assert(p_j2k != 00);
3399 assert(p_manager != 00);
3400
3401 if (p_header_size < 1) {
3402 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3403 return OPJ_FALSE;
3404 }
3405 /* Do not care of this at the moment since only local variables are set here */
3406 /*
3407 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3408 ++p_header_data;
3409 --p_header_size;
3410
3411 while
3412 (p_header_size > 0)
3413 {
3414 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3415 ++p_header_data;
3416 p_header_size -= (1+l_Nplm);
3417 if
3418 (p_header_size < 0)
3419 {
3420 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3421 return false;
3422 }
3423 for
3424 (i = 0; i < l_Nplm; ++i)
3425 {
3426 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3427 ++p_header_data;
3428 // take only the last seven bytes
3429 l_packet_len |= (l_tmp & 0x7f);
3430 if
3431 (l_tmp & 0x80)
3432 {
3433 l_packet_len <<= 7;
3434 }
3435 else
3436 {
3437 // store packet length and proceed to next packet
3438 l_packet_len = 0;
3439 }
3440 }
3441 if
3442 (l_packet_len != 0)
3443 {
3444 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3445 return false;
3446 }
3447 }
3448 */
3449 return OPJ_TRUE;
3450 }
3451
3452 /**
3453 * Reads a PLT marker (Packet length, tile-part header)
3454 *
3455 * @param p_header_data the data contained in the PLT box.
3456 * @param p_j2k the jpeg2000 codec.
3457 * @param p_header_size the size of the data contained in the PLT marker.
3458 * @param p_manager the user event manager.
3459 */
opj_j2k_read_plt(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3460 static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k,
3461 OPJ_BYTE * p_header_data,
3462 OPJ_UINT32 p_header_size,
3463 opj_event_mgr_t * p_manager
3464 )
3465 {
3466 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3467
3468 /* preconditions */
3469 assert(p_header_data != 00);
3470 assert(p_j2k != 00);
3471 assert(p_manager != 00);
3472
3473 if (p_header_size < 1) {
3474 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3475 return OPJ_FALSE;
3476 }
3477
3478 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3479 ++p_header_data;
3480 --p_header_size;
3481
3482 for (i = 0; i < p_header_size; ++i) {
3483 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3484 ++p_header_data;
3485 /* take only the last seven bytes */
3486 l_packet_len |= (l_tmp & 0x7f);
3487 if (l_tmp & 0x80) {
3488 l_packet_len <<= 7;
3489 }
3490 else {
3491 /* store packet length and proceed to next packet */
3492 l_packet_len = 0;
3493 }
3494 }
3495
3496 if (l_packet_len != 0) {
3497 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3498 return OPJ_FALSE;
3499 }
3500
3501 return OPJ_TRUE;
3502 }
3503
3504 /**
3505 * Reads a PPM marker (Packed packet headers, main header)
3506 *
3507 * @param p_header_data the data contained in the POC box.
3508 * @param p_j2k the jpeg2000 codec.
3509 * @param p_header_size the size of the data contained in the POC marker.
3510 * @param p_manager the user event manager.
3511 */
3512
opj_j2k_read_ppm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3513 static OPJ_BOOL opj_j2k_read_ppm (
3514 opj_j2k_t *p_j2k,
3515 OPJ_BYTE * p_header_data,
3516 OPJ_UINT32 p_header_size,
3517 opj_event_mgr_t * p_manager )
3518 {
3519 opj_cp_t *l_cp = 00;
3520 OPJ_UINT32 l_Z_ppm;
3521
3522 /* preconditions */
3523 assert(p_header_data != 00);
3524 assert(p_j2k != 00);
3525 assert(p_manager != 00);
3526
3527 /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */
3528 if (p_header_size < 2) {
3529 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3530 return OPJ_FALSE;
3531 }
3532
3533 l_cp = &(p_j2k->m_cp);
3534 l_cp->ppm = 1;
3535
3536 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3537 ++p_header_data;
3538 --p_header_size;
3539
3540 /* check allocation needed */
3541 if (l_cp->ppm_markers == NULL) { /* first PPM marker */
3542 OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3543 assert(l_cp->ppm_markers_count == 0U);
3544
3545 l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
3546 if (l_cp->ppm_markers == NULL) {
3547 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3548 return OPJ_FALSE;
3549 }
3550 l_cp->ppm_markers_count = l_newCount;
3551 } else if (l_cp->ppm_markers_count <= l_Z_ppm) {
3552 OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3553 opj_ppx *new_ppm_markers;
3554 new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, l_newCount * sizeof(opj_ppx));
3555 if (new_ppm_markers == NULL) {
3556 /* clean up to be done on l_cp destruction */
3557 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3558 return OPJ_FALSE;
3559 }
3560 l_cp->ppm_markers = new_ppm_markers;
3561 memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx));
3562 l_cp->ppm_markers_count = l_newCount;
3563 }
3564
3565 if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) {
3566 /* clean up to be done on l_cp destruction */
3567 opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm);
3568 return OPJ_FALSE;
3569 }
3570
3571 l_cp->ppm_markers[l_Z_ppm].m_data = opj_malloc(p_header_size);
3572 if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) {
3573 /* clean up to be done on l_cp destruction */
3574 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3575 return OPJ_FALSE;
3576 }
3577 l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size;
3578 memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size);
3579
3580 return OPJ_TRUE;
3581 }
3582
3583 /**
3584 * Merges all PPM markers read (Packed headers, main header)
3585 *
3586 * @param p_cp main coding parameters.
3587 * @param p_manager the user event manager.
3588 */
opj_j2k_merge_ppm(opj_cp_t * p_cp,opj_event_mgr_t * p_manager)3589 static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager )
3590 {
3591 OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining;
3592
3593 /* preconditions */
3594 assert(p_cp != 00);
3595 assert(p_manager != 00);
3596 assert(p_cp->ppm_buffer == NULL);
3597
3598 if (p_cp->ppm == 0U) {
3599 return OPJ_TRUE;
3600 }
3601
3602 l_ppm_data_size = 0U;
3603 l_N_ppm_remaining = 0U;
3604 for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3605 if (p_cp->ppm_markers[i].m_data != NULL) { /* standard doesn't seem to require contiguous Zppm */
3606 OPJ_UINT32 l_N_ppm;
3607 OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3608 const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
3609
3610 if (l_N_ppm_remaining >= l_data_size) {
3611 l_N_ppm_remaining -= l_data_size;
3612 l_data_size = 0U;
3613 } else {
3614 l_data += l_N_ppm_remaining;
3615 l_data_size -= l_N_ppm_remaining;
3616 l_N_ppm_remaining = 0U;
3617 }
3618
3619 if (l_data_size > 0U) {
3620 do
3621 {
3622 /* read Nppm */
3623 if (l_data_size < 4U) {
3624 /* clean up to be done on l_cp destruction */
3625 opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
3626 return OPJ_FALSE;
3627 }
3628 opj_read_bytes(l_data, &l_N_ppm, 4);
3629 l_data+=4;
3630 l_data_size-=4;
3631 l_ppm_data_size += l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */
3632
3633 if (l_data_size >= l_N_ppm) {
3634 l_data_size -= l_N_ppm;
3635 l_data += l_N_ppm;
3636 } else {
3637 l_N_ppm_remaining = l_N_ppm - l_data_size;
3638 l_data_size = 0U;
3639 }
3640 } while (l_data_size > 0U);
3641 }
3642 }
3643 }
3644
3645 if (l_N_ppm_remaining != 0U) {
3646 /* clean up to be done on l_cp destruction */
3647 opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n");
3648 return OPJ_FALSE;
3649 }
3650
3651 p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size);
3652 if (p_cp->ppm_buffer == 00) {
3653 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3654 return OPJ_FALSE;
3655 }
3656 p_cp->ppm_len = l_ppm_data_size;
3657 l_ppm_data_size = 0U;
3658 l_N_ppm_remaining = 0U;
3659 for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3660 if (p_cp->ppm_markers[i].m_data != NULL) { /* standard doesn't seem to require contiguous Zppm */
3661 OPJ_UINT32 l_N_ppm;
3662 OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3663 const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
3664
3665 if (l_N_ppm_remaining >= l_data_size) {
3666 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
3667 l_ppm_data_size += l_data_size;
3668 l_N_ppm_remaining -= l_data_size;
3669 l_data_size = 0U;
3670 } else {
3671 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining);
3672 l_ppm_data_size += l_N_ppm_remaining;
3673 l_data += l_N_ppm_remaining;
3674 l_data_size -= l_N_ppm_remaining;
3675 l_N_ppm_remaining = 0U;
3676 }
3677
3678 if (l_data_size > 0U) {
3679 do
3680 {
3681 /* read Nppm */
3682 if (l_data_size < 4U) {
3683 /* clean up to be done on l_cp destruction */
3684 opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
3685 return OPJ_FALSE;
3686 }
3687 opj_read_bytes(l_data, &l_N_ppm, 4);
3688 l_data+=4;
3689 l_data_size-=4;
3690
3691 if (l_data_size >= l_N_ppm) {
3692 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm);
3693 l_ppm_data_size += l_N_ppm;
3694 l_data_size -= l_N_ppm;
3695 l_data += l_N_ppm;
3696 } else {
3697 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
3698 l_ppm_data_size += l_data_size;
3699 l_N_ppm_remaining = l_N_ppm - l_data_size;
3700 l_data_size = 0U;
3701 }
3702 } while (l_data_size > 0U);
3703 }
3704 opj_free(p_cp->ppm_markers[i].m_data);
3705 p_cp->ppm_markers[i].m_data = NULL;
3706 p_cp->ppm_markers[i].m_data_size = 0U;
3707 }
3708 }
3709
3710 p_cp->ppm_data = p_cp->ppm_buffer;
3711 p_cp->ppm_data_size = p_cp->ppm_len;
3712
3713 p_cp->ppm_markers_count = 0U;
3714 opj_free(p_cp->ppm_markers);
3715 p_cp->ppm_markers = NULL;
3716
3717 return OPJ_TRUE;
3718 }
3719
3720 /**
3721 * Reads a PPT marker (Packed packet headers, tile-part header)
3722 *
3723 * @param p_header_data the data contained in the PPT box.
3724 * @param p_j2k the jpeg2000 codec.
3725 * @param p_header_size the size of the data contained in the PPT marker.
3726 * @param p_manager the user event manager.
3727 */
opj_j2k_read_ppt(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3728 static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k,
3729 OPJ_BYTE * p_header_data,
3730 OPJ_UINT32 p_header_size,
3731 opj_event_mgr_t * p_manager
3732 )
3733 {
3734 opj_cp_t *l_cp = 00;
3735 opj_tcp_t *l_tcp = 00;
3736 OPJ_UINT32 l_Z_ppt;
3737
3738 /* preconditions */
3739 assert(p_header_data != 00);
3740 assert(p_j2k != 00);
3741 assert(p_manager != 00);
3742
3743 /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */
3744 if (p_header_size < 2) {
3745 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3746 return OPJ_FALSE;
3747 }
3748
3749 l_cp = &(p_j2k->m_cp);
3750 if (l_cp->ppm){
3751 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
3752 return OPJ_FALSE;
3753 }
3754
3755 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3756 l_tcp->ppt = 1;
3757
3758 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3759 ++p_header_data;
3760 --p_header_size;
3761
3762 /* check allocation needed */
3763 if (l_tcp->ppt_markers == NULL) { /* first PPT marker */
3764 OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
3765 assert(l_tcp->ppt_markers_count == 0U);
3766
3767 l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
3768 if (l_tcp->ppt_markers == NULL) {
3769 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3770 return OPJ_FALSE;
3771 }
3772 l_tcp->ppt_markers_count = l_newCount;
3773 } else if (l_tcp->ppt_markers_count <= l_Z_ppt) {
3774 OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
3775 opj_ppx *new_ppt_markers;
3776 new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, l_newCount * sizeof(opj_ppx));
3777 if (new_ppt_markers == NULL) {
3778 /* clean up to be done on l_tcp destruction */
3779 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3780 return OPJ_FALSE;
3781 }
3782 l_tcp->ppt_markers = new_ppt_markers;
3783 memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx));
3784 l_tcp->ppt_markers_count = l_newCount;
3785 }
3786
3787 if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) {
3788 /* clean up to be done on l_tcp destruction */
3789 opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt);
3790 return OPJ_FALSE;
3791 }
3792
3793 l_tcp->ppt_markers[l_Z_ppt].m_data = opj_malloc(p_header_size);
3794 if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) {
3795 /* clean up to be done on l_tcp destruction */
3796 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3797 return OPJ_FALSE;
3798 }
3799 l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size;
3800 memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size);
3801 return OPJ_TRUE;
3802 }
3803
3804 /**
3805 * Merges all PPT markers read (Packed packet headers, tile-part header)
3806 *
3807 * @param p_tcp the tile.
3808 * @param p_manager the user event manager.
3809 */
opj_j2k_merge_ppt(opj_tcp_t * p_tcp,opj_event_mgr_t * p_manager)3810 static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager)
3811 {
3812 OPJ_UINT32 i, l_ppt_data_size;
3813 /* preconditions */
3814 assert(p_tcp != 00);
3815 assert(p_manager != 00);
3816 assert(p_tcp->ppt_buffer == NULL);
3817
3818 if (p_tcp->ppt == 0U) {
3819 return OPJ_TRUE;
3820 }
3821
3822 l_ppt_data_size = 0U;
3823 for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
3824 l_ppt_data_size += p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
3825 }
3826
3827 p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size);
3828 if (p_tcp->ppt_buffer == 00) {
3829 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3830 return OPJ_FALSE;
3831 }
3832 p_tcp->ppt_len = l_ppt_data_size;
3833 l_ppt_data_size = 0U;
3834 for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
3835 if (p_tcp->ppt_markers[i].m_data != NULL) { /* standard doesn't seem to require contiguous Zppt */
3836 memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, p_tcp->ppt_markers[i].m_data_size);
3837 l_ppt_data_size += p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
3838
3839 opj_free(p_tcp->ppt_markers[i].m_data);
3840 p_tcp->ppt_markers[i].m_data = NULL;
3841 p_tcp->ppt_markers[i].m_data_size = 0U;
3842 }
3843 }
3844
3845 p_tcp->ppt_markers_count = 0U;
3846 opj_free(p_tcp->ppt_markers);
3847 p_tcp->ppt_markers = NULL;
3848
3849 p_tcp->ppt_data = p_tcp->ppt_buffer;
3850 p_tcp->ppt_data_size = p_tcp->ppt_len;
3851 return OPJ_TRUE;
3852 }
3853
opj_j2k_write_tlm(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3854 static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k,
3855 opj_stream_private_t *p_stream,
3856 opj_event_mgr_t * p_manager
3857 )
3858 {
3859 OPJ_BYTE * l_current_data = 00;
3860 OPJ_UINT32 l_tlm_size;
3861
3862 /* preconditions */
3863 assert(p_j2k != 00);
3864 assert(p_manager != 00);
3865 assert(p_stream != 00);
3866
3867 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3868
3869 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3870 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
3871 if (! new_header_tile_data) {
3872 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3873 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3874 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3875 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
3876 return OPJ_FALSE;
3877 }
3878 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3879 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3880 }
3881
3882 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3883
3884 /* change the way data is written to avoid seeking if possible */
3885 /* TODO */
3886 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
3887
3888 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
3889 l_current_data += 2;
3890
3891 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
3892 l_current_data += 2;
3893
3894 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
3895 ++l_current_data;
3896
3897 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3898 ++l_current_data;
3899
3900 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
3901 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) {
3902 return OPJ_FALSE;
3903 }
3904
3905 return OPJ_TRUE;
3906 }
3907
opj_j2k_write_sot(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,const opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3908 static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k,
3909 OPJ_BYTE * p_data,
3910 OPJ_UINT32 * p_data_written,
3911 const opj_stream_private_t *p_stream,
3912 opj_event_mgr_t * p_manager
3913 )
3914 {
3915 /* preconditions */
3916 assert(p_j2k != 00);
3917 assert(p_manager != 00);
3918 assert(p_stream != 00);
3919
3920 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
3921 p_data += 2;
3922
3923 opj_write_bytes(p_data,10,2); /* Lsot */
3924 p_data += 2;
3925
3926 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
3927 p_data += 2;
3928
3929 /* Psot */
3930 p_data += 4;
3931
3932 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
3933 ++p_data;
3934
3935 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
3936 ++p_data;
3937
3938 /* UniPG>> */
3939 #ifdef USE_JPWL
3940 /* update markers struct */
3941 /*
3942 OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
3943 */
3944 assert( 0 && "TODO" );
3945 #endif /* USE_JPWL */
3946
3947 * p_data_written = 12;
3948
3949 return OPJ_TRUE;
3950 }
3951
opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,OPJ_UINT32 * p_tile_no,OPJ_UINT32 * p_tot_len,OPJ_UINT32 * p_current_part,OPJ_UINT32 * p_num_parts,opj_event_mgr_t * p_manager)3952 static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,
3953 OPJ_UINT32 p_header_size,
3954 OPJ_UINT32* p_tile_no,
3955 OPJ_UINT32* p_tot_len,
3956 OPJ_UINT32* p_current_part,
3957 OPJ_UINT32* p_num_parts,
3958 opj_event_mgr_t * p_manager )
3959 {
3960 /* preconditions */
3961 assert(p_header_data != 00);
3962 assert(p_manager != 00);
3963
3964 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3965 if (p_header_size != 8) {
3966 opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3967 return OPJ_FALSE;
3968 }
3969
3970 opj_read_bytes(p_header_data,p_tile_no,2); /* Isot */
3971 p_header_data+=2;
3972 opj_read_bytes(p_header_data,p_tot_len,4); /* Psot */
3973 p_header_data+=4;
3974 opj_read_bytes(p_header_data,p_current_part,1); /* TPsot */
3975 ++p_header_data;
3976 opj_read_bytes(p_header_data,p_num_parts ,1); /* TNsot */
3977 ++p_header_data;
3978 return OPJ_TRUE;
3979 }
3980
opj_j2k_read_sot(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3981 static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
3982 OPJ_BYTE * p_header_data,
3983 OPJ_UINT32 p_header_size,
3984 opj_event_mgr_t * p_manager )
3985 {
3986 opj_cp_t *l_cp = 00;
3987 opj_tcp_t *l_tcp = 00;
3988 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3989 OPJ_UINT32 l_current_part;
3990 OPJ_UINT32 l_tile_x,l_tile_y;
3991
3992 /* preconditions */
3993
3994 assert(p_j2k != 00);
3995 assert(p_manager != 00);
3996
3997 if (! opj_j2k_get_sot_values(p_header_data, p_header_size, &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, p_manager)) {
3998 opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3999 return OPJ_FALSE;
4000 }
4001
4002 l_cp = &(p_j2k->m_cp);
4003
4004 /* testcase 2.pdf.SIGFPE.706.1112 */
4005 if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
4006 opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);
4007 return OPJ_FALSE;
4008 }
4009
4010 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4011 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4012 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4013
4014 #ifdef USE_JPWL
4015 if (l_cp->correct) {
4016
4017 OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
4018 static OPJ_UINT32 backup_tileno = 0;
4019
4020 /* tileno is negative or larger than the number of tiles!!! */
4021 if (tileno > (l_cp->tw * l_cp->th)) {
4022 opj_event_msg(p_manager, EVT_ERROR,
4023 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4024 tileno, (l_cp->tw * l_cp->th));
4025 if (!JPWL_ASSUME) {
4026 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4027 return OPJ_FALSE;
4028 }
4029 /* we try to correct */
4030 tileno = backup_tileno;
4031 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4032 "- setting tile number to %d\n",
4033 tileno);
4034 }
4035
4036 /* keep your private count of tiles */
4037 backup_tileno++;
4038 };
4039 #endif /* USE_JPWL */
4040
4041 /* look for the tile in the list of already processed tile (in parts). */
4042 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4043 /* since the time taken by this function can only grow at the time */
4044
4045 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4046 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4047 {
4048 if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/
4049 {
4050 opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len);
4051 }
4052 else
4053 {
4054 opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
4055 return OPJ_FALSE;
4056 }
4057 }
4058
4059 #ifdef USE_JPWL
4060 if (l_cp->correct) {
4061
4062 /* totlen is negative or larger than the bytes left!!! */
4063 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))) { */
4064 opj_event_msg(p_manager, EVT_ERROR,
4065 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4066 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4067 if (!JPWL_ASSUME) {
4068 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4069 return OPJ_FALSE;
4070 }
4071 /* we try to correct */
4072 l_tot_len = 0;
4073 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4074 "- setting Psot to %d => assuming it is the last tile\n",
4075 l_tot_len);
4076 }
4077 };
4078 #endif /* USE_JPWL */
4079
4080 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4081 if (!l_tot_len) {
4082 opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4083 "we assuming it is the last tile-part of the codestream.\n");
4084 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4085 }
4086
4087 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4088 l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction;
4089 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4090 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4091 if (l_tcp->m_nb_tile_parts) {
4092 if (l_current_part >= l_tcp->m_nb_tile_parts){
4093 opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4094 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4095 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4096 return OPJ_FALSE;
4097 }
4098 }
4099 if( l_current_part >= l_num_parts ) {
4100 /* testcase 451.pdf.SIGSEGV.ce9.3723 */
4101 opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4102 "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts );
4103 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4104 return OPJ_FALSE;
4105 }
4106 l_tcp->m_nb_tile_parts = l_num_parts;
4107 }
4108
4109 /* If know the number of tile part header we will check if we didn't read the last*/
4110 if (l_tcp->m_nb_tile_parts) {
4111 if (l_tcp->m_nb_tile_parts == (l_current_part+1)) {
4112 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4113 }
4114 }
4115
4116 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4117 /* Keep the size of data to skip after this marker */
4118 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4119 }
4120 else {
4121 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4122 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4123 }
4124
4125 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4126
4127 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4128 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4129 p_j2k->m_specific_param.m_decoder.m_skip_data =
4130 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4131 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4132 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4133 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4134 }
4135 else {
4136 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4137 p_j2k->m_specific_param.m_decoder.m_skip_data =
4138 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4139 }
4140
4141 /* Index */
4142 if (p_j2k->cstr_index)
4143 {
4144 assert(p_j2k->cstr_index->tile_index != 00);
4145 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4146 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4147
4148 if (l_num_parts != 0){
4149 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4150 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4151
4152 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4153 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4154 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4155 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4156 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
4157 return OPJ_FALSE;
4158 }
4159 }
4160 else {
4161 opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4162 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t));
4163 if (! new_tp_index) {
4164 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4165 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4166 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
4167 return OPJ_FALSE;
4168 }
4169 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4170 }
4171 }
4172 else{
4173 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4174
4175 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4176 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4177 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4178 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4179 sizeof(opj_tp_index_t));
4180 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4181 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4182 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
4183 return OPJ_FALSE;
4184 }
4185 }
4186
4187 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4188 opj_tp_index_t *new_tp_index;
4189 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1;
4190 new_tp_index = (opj_tp_index_t *) opj_realloc(
4191 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4192 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));
4193 if (! new_tp_index) {
4194 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4195 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4196 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4197 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
4198 return OPJ_FALSE;
4199 }
4200 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4201 }
4202 }
4203
4204 }
4205
4206 }
4207
4208 /* 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 */
4209 /* if (p_j2k->cstr_info) {
4210 if (l_tcp->first) {
4211 if (tileno == 0) {
4212 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4213 }
4214
4215 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4216 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4217 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4218 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4219
4220 if (numparts) {
4221 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4222 }
4223 else {
4224 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4225 }
4226 }
4227 else {
4228 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4229 }
4230
4231 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4232 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4233 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4234 }*/
4235 return OPJ_TRUE;
4236 }
4237
opj_j2k_write_sod(opj_j2k_t * p_j2k,opj_tcd_t * p_tile_coder,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,const opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4238 static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k,
4239 opj_tcd_t * p_tile_coder,
4240 OPJ_BYTE * p_data,
4241 OPJ_UINT32 * p_data_written,
4242 OPJ_UINT32 p_total_data_size,
4243 const opj_stream_private_t *p_stream,
4244 opj_event_mgr_t * p_manager
4245 )
4246 {
4247 opj_codestream_info_t *l_cstr_info = 00;
4248 OPJ_UINT32 l_remaining_data;
4249
4250 /* preconditions */
4251 assert(p_j2k != 00);
4252 assert(p_manager != 00);
4253 assert(p_stream != 00);
4254
4255 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4256 p_data += 2;
4257
4258 /* make room for the EOF marker */
4259 l_remaining_data = p_total_data_size - 4;
4260
4261 /* update tile coder */
4262 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4263 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4264
4265 /* INDEX >> */
4266 /* TODO mergeV2: check this part which use cstr_info */
4267 /*l_cstr_info = p_j2k->cstr_info;
4268 if (l_cstr_info) {
4269 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4270 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4271 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4272 }
4273 else {*/
4274 /*
4275 TODO
4276 if
4277 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4278 {
4279 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4280 }*/
4281 /*}*/
4282 /* UniPG>> */
4283 #ifdef USE_JPWL
4284 /* update markers struct */
4285 /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4286 */
4287 assert( 0 && "TODO" );
4288 #endif /* USE_JPWL */
4289 /* <<UniPG */
4290 /*}*/
4291 /* << INDEX */
4292
4293 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4294 p_tile_coder->tcd_image->tiles->packno = 0;
4295 if (l_cstr_info) {
4296 l_cstr_info->packno = 0;
4297 }
4298 }
4299
4300 *p_data_written = 0;
4301
4302 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)) {
4303 opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
4304 return OPJ_FALSE;
4305 }
4306
4307 *p_data_written += 2;
4308
4309 return OPJ_TRUE;
4310 }
4311
opj_j2k_read_sod(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4312 static OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k,
4313 opj_stream_private_t *p_stream,
4314 opj_event_mgr_t * p_manager
4315 )
4316 {
4317 OPJ_SIZE_T l_current_read_size;
4318 opj_codestream_index_t * l_cstr_index = 00;
4319 OPJ_BYTE ** l_current_data = 00;
4320 opj_tcp_t * l_tcp = 00;
4321 OPJ_UINT32 * l_tile_len = 00;
4322 OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
4323
4324 /* preconditions */
4325 assert(p_j2k != 00);
4326 assert(p_manager != 00);
4327 assert(p_stream != 00);
4328
4329 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4330
4331 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4332 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4333 // but we are in the last tile part,
4334 // so its result will fit on OPJ_UINT32 unless we find
4335 // a file with a single tile part of more than 4 GB...*/
4336 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4337 }
4338 else {
4339 /* Check to avoid pass the limit of OPJ_UINT32 */
4340 if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 )
4341 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4342 else {
4343 /* MSD: case commented to support empty SOT marker (PHR data) */
4344 }
4345 }
4346
4347 l_current_data = &(l_tcp->m_data);
4348 l_tile_len = &l_tcp->m_data_size;
4349
4350 /* Patch to support new PHR data */
4351 if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
4352 /* If we are here, we'll try to read the data after allocation */
4353 /* Check enough bytes left in stream before allocation */
4354 if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > opj_stream_get_number_byte_left(p_stream)) {
4355 opj_event_msg(p_manager, EVT_ERROR, "Tile part length size inconsistent with stream length\n");
4356 return OPJ_FALSE;
4357 }
4358 if (! *l_current_data) {
4359 /* LH: oddly enough, in this path, l_tile_len!=0.
4360 * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
4361 */
4362 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4363 }
4364 else {
4365 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);
4366 if (! l_new_current_data) {
4367 opj_free(*l_current_data);
4368 /*nothing more is done as l_current_data will be set to null, and just
4369 afterward we enter in the error path
4370 and the actual tile_len is updated (committed) at the end of the
4371 function. */
4372 }
4373 *l_current_data = l_new_current_data;
4374 }
4375
4376 if (*l_current_data == 00) {
4377 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
4378 return OPJ_FALSE;
4379 }
4380 }
4381 else {
4382 l_sot_length_pb_detected = OPJ_TRUE;
4383 }
4384
4385 /* Index */
4386 l_cstr_index = p_j2k->cstr_index;
4387 if (l_cstr_index) {
4388 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4389
4390 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4391 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4392 l_current_pos;
4393 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4394 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4395
4396 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
4397 l_cstr_index,
4398 J2K_MS_SOD,
4399 l_current_pos,
4400 p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
4401 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
4402 return OPJ_FALSE;
4403 }
4404
4405 /*l_cstr_index->packno = 0;*/
4406 }
4407
4408 /* Patch to support new PHR data */
4409 if (!l_sot_length_pb_detected) {
4410 l_current_read_size = opj_stream_read_data(
4411 p_stream,
4412 *l_current_data + *l_tile_len,
4413 p_j2k->m_specific_param.m_decoder.m_sot_length,
4414 p_manager);
4415 }
4416 else
4417 {
4418 l_current_read_size = 0;
4419 }
4420
4421 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4422 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4423 }
4424 else {
4425 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4426 }
4427
4428 *l_tile_len += (OPJ_UINT32)l_current_read_size;
4429
4430 return OPJ_TRUE;
4431 }
4432
opj_j2k_write_rgn(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_UINT32 nb_comps,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4433 static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
4434 OPJ_UINT32 p_tile_no,
4435 OPJ_UINT32 p_comp_no,
4436 OPJ_UINT32 nb_comps,
4437 opj_stream_private_t *p_stream,
4438 opj_event_mgr_t * p_manager
4439 )
4440 {
4441 OPJ_BYTE * l_current_data = 00;
4442 OPJ_UINT32 l_rgn_size;
4443 opj_cp_t *l_cp = 00;
4444 opj_tcp_t *l_tcp = 00;
4445 opj_tccp_t *l_tccp = 00;
4446 OPJ_UINT32 l_comp_room;
4447
4448 /* preconditions */
4449 assert(p_j2k != 00);
4450 assert(p_manager != 00);
4451 assert(p_stream != 00);
4452
4453 l_cp = &(p_j2k->m_cp);
4454 l_tcp = &l_cp->tcps[p_tile_no];
4455 l_tccp = &l_tcp->tccps[p_comp_no];
4456
4457 if (nb_comps <= 256) {
4458 l_comp_room = 1;
4459 }
4460 else {
4461 l_comp_room = 2;
4462 }
4463
4464 l_rgn_size = 6 + l_comp_room;
4465
4466 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4467
4468 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4469 l_current_data += 2;
4470
4471 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4472 l_current_data += 2;
4473
4474 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4475 l_current_data+=l_comp_room;
4476
4477 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4478 ++l_current_data;
4479
4480 opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1); /* SPrgn */
4481 ++l_current_data;
4482
4483 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) {
4484 return OPJ_FALSE;
4485 }
4486
4487 return OPJ_TRUE;
4488 }
4489
opj_j2k_write_eoc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4490 static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k,
4491 opj_stream_private_t *p_stream,
4492 opj_event_mgr_t * p_manager
4493 )
4494 {
4495 /* preconditions */
4496 assert(p_j2k != 00);
4497 assert(p_manager != 00);
4498 assert(p_stream != 00);
4499
4500 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4501
4502 /* UniPG>> */
4503 #ifdef USE_JPWL
4504 /* update markers struct */
4505 /*
4506 OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4507 */
4508 #endif /* USE_JPWL */
4509
4510 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4511 return OPJ_FALSE;
4512 }
4513
4514 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4515 return OPJ_FALSE;
4516 }
4517
4518 return OPJ_TRUE;
4519 }
4520
4521 /**
4522 * Reads a RGN marker (Region Of Interest)
4523 *
4524 * @param p_header_data the data contained in the POC box.
4525 * @param p_j2k the jpeg2000 codec.
4526 * @param p_header_size the size of the data contained in the POC marker.
4527 * @param p_manager the user event manager.
4528 */
opj_j2k_read_rgn(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)4529 static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
4530 OPJ_BYTE * p_header_data,
4531 OPJ_UINT32 p_header_size,
4532 opj_event_mgr_t * p_manager
4533 )
4534 {
4535 OPJ_UINT32 l_nb_comp;
4536 opj_image_t * l_image = 00;
4537
4538 opj_cp_t *l_cp = 00;
4539 opj_tcp_t *l_tcp = 00;
4540 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4541
4542 /* preconditions*/
4543 assert(p_header_data != 00);
4544 assert(p_j2k != 00);
4545 assert(p_manager != 00);
4546
4547 l_image = p_j2k->m_private_image;
4548 l_nb_comp = l_image->numcomps;
4549
4550 if (l_nb_comp <= 256) {
4551 l_comp_room = 1; }
4552 else {
4553 l_comp_room = 2; }
4554
4555 if (p_header_size != 2 + l_comp_room) {
4556 opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4557 return OPJ_FALSE;
4558 }
4559
4560 l_cp = &(p_j2k->m_cp);
4561 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4562 &l_cp->tcps[p_j2k->m_current_tile_number] :
4563 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4564
4565 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4566 p_header_data+=l_comp_room;
4567 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4568 ++p_header_data;
4569
4570 #ifdef USE_JPWL
4571 if (l_cp->correct) {
4572 /* totlen is negative or larger than the bytes left!!! */
4573 if (l_comp_room >= l_nb_comp) {
4574 opj_event_msg(p_manager, EVT_ERROR,
4575 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4576 l_comp_room, l_nb_comp);
4577 if (!JPWL_ASSUME || JPWL_ASSUME) {
4578 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4579 return OPJ_FALSE;
4580 }
4581 }
4582 };
4583 #endif /* USE_JPWL */
4584
4585 /* testcase 3635.pdf.asan.77.2930 */
4586 if (l_comp_no >= l_nb_comp) {
4587 opj_event_msg(p_manager, EVT_ERROR,
4588 "bad component number in RGN (%d when there are only %d)\n",
4589 l_comp_no, l_nb_comp);
4590 return OPJ_FALSE;
4591 }
4592
4593 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4594 ++p_header_data;
4595
4596 return OPJ_TRUE;
4597
4598 }
4599
opj_j2k_get_tp_stride(opj_tcp_t * p_tcp)4600 static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp)
4601 {
4602 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4603 }
4604
opj_j2k_get_default_stride(opj_tcp_t * p_tcp)4605 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp)
4606 {
4607 (void)p_tcp;
4608 return 0;
4609 }
4610
opj_j2k_update_rates(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4611 static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
4612 opj_stream_private_t *p_stream,
4613 opj_event_mgr_t * p_manager )
4614 {
4615 opj_cp_t * l_cp = 00;
4616 opj_image_t * l_image = 00;
4617 opj_tcp_t * l_tcp = 00;
4618 opj_image_comp_t * l_img_comp = 00;
4619
4620 OPJ_UINT32 i,j,k;
4621 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4622 OPJ_FLOAT32 * l_rates = 0;
4623 OPJ_FLOAT32 l_sot_remove;
4624 OPJ_UINT32 l_bits_empty, l_size_pixel;
4625 OPJ_UINT32 l_tile_size = 0;
4626 OPJ_UINT32 l_last_res;
4627 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00;
4628
4629 /* preconditions */
4630 assert(p_j2k != 00);
4631 assert(p_manager != 00);
4632 assert(p_stream != 00);
4633
4634 l_cp = &(p_j2k->m_cp);
4635 l_image = p_j2k->m_private_image;
4636 l_tcp = l_cp->tcps;
4637
4638 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4639 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4640 l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(l_cp->th * l_cp->tw);
4641
4642 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4643 l_tp_stride_func = opj_j2k_get_tp_stride;
4644 }
4645 else {
4646 l_tp_stride_func = opj_j2k_get_default_stride;
4647 }
4648
4649 for (i=0;i<l_cp->th;++i) {
4650 for (j=0;j<l_cp->tw;++j) {
4651 OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers;
4652
4653 /* 4 borders of the tile rescale on the image if necessary */
4654 l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0);
4655 l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0);
4656 l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);
4657 l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);
4658
4659 l_rates = l_tcp->rates;
4660
4661 /* Modification of the RATE >> */
4662 if (*l_rates) {
4663 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
4664 /
4665 ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
4666 )
4667 -
4668 l_offset;
4669 }
4670
4671 ++l_rates;
4672
4673 for (k = 1; k < l_tcp->numlayers; ++k) {
4674 if (*l_rates) {
4675 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
4676 /
4677 ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
4678 )
4679 -
4680 l_offset;
4681 }
4682
4683 ++l_rates;
4684 }
4685
4686 ++l_tcp;
4687
4688 }
4689 }
4690
4691 l_tcp = l_cp->tcps;
4692
4693 for (i=0;i<l_cp->th;++i) {
4694 for (j=0;j<l_cp->tw;++j) {
4695 l_rates = l_tcp->rates;
4696
4697 if (*l_rates) {
4698 *l_rates -= l_sot_remove;
4699
4700 if (*l_rates < 30) {
4701 *l_rates = 30;
4702 }
4703 }
4704
4705 ++l_rates;
4706
4707 l_last_res = l_tcp->numlayers - 1;
4708
4709 for (k = 1; k < l_last_res; ++k) {
4710
4711 if (*l_rates) {
4712 *l_rates -= l_sot_remove;
4713
4714 if (*l_rates < *(l_rates - 1) + 10) {
4715 *l_rates = (*(l_rates - 1)) + 20;
4716 }
4717 }
4718
4719 ++l_rates;
4720 }
4721
4722 if (*l_rates) {
4723 *l_rates -= (l_sot_remove + 2.f);
4724
4725 if (*l_rates < *(l_rates - 1) + 10) {
4726 *l_rates = (*(l_rates - 1)) + 20;
4727 }
4728 }
4729
4730 ++l_tcp;
4731 }
4732 }
4733
4734 l_img_comp = l_image->comps;
4735 l_tile_size = 0;
4736
4737 for (i=0;i<l_image->numcomps;++i) {
4738 l_tile_size += ( opj_uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4739 *
4740 opj_uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4741 *
4742 l_img_comp->prec
4743 );
4744
4745 ++l_img_comp;
4746 }
4747
4748 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4749
4750 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4751
4752 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4753 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4754 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4755 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4756 return OPJ_FALSE;
4757 }
4758
4759 if (OPJ_IS_CINEMA(l_cp->rsiz)) {
4760 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4761 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4762 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4763 return OPJ_FALSE;
4764 }
4765
4766 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4767 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4768 }
4769
4770 return OPJ_TRUE;
4771 }
4772
4773 #if 0
4774 static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
4775 opj_stream_private_t *p_stream,
4776 opj_event_mgr_t * p_manager )
4777 {
4778 OPJ_UINT32 i;
4779 opj_tcd_t * l_tcd = 00;
4780 OPJ_UINT32 l_nb_tiles;
4781 opj_tcp_t * l_tcp = 00;
4782 OPJ_BOOL l_success;
4783
4784 /* preconditions */
4785 assert(p_j2k != 00);
4786 assert(p_manager != 00);
4787 assert(p_stream != 00);
4788
4789 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4790 l_tcp = p_j2k->m_cp.tcps;
4791
4792 l_tcd = opj_tcd_create(OPJ_TRUE);
4793 if (l_tcd == 00) {
4794 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4795 return OPJ_FALSE;
4796 }
4797
4798 for (i = 0; i < l_nb_tiles; ++i) {
4799 if (l_tcp->m_data) {
4800 if (! opj_tcd_init_decode_tile(l_tcd, i)) {
4801 opj_tcd_destroy(l_tcd);
4802 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4803 return OPJ_FALSE;
4804 }
4805
4806 l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4807 /* cleanup */
4808
4809 if (! l_success) {
4810 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4811 break;
4812 }
4813 }
4814
4815 opj_j2k_tcp_destroy(l_tcp);
4816 ++l_tcp;
4817 }
4818
4819 opj_tcd_destroy(l_tcd);
4820 return OPJ_TRUE;
4821 }
4822 #endif
4823
opj_j2k_get_end_header(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)4824 static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
4825 struct opj_stream_private *p_stream,
4826 struct opj_event_mgr * p_manager )
4827 {
4828 /* preconditions */
4829 assert(p_j2k != 00);
4830 assert(p_manager != 00);
4831 assert(p_stream != 00);
4832
4833 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4834
4835 return OPJ_TRUE;
4836 }
4837
opj_j2k_write_mct_data_group(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)4838 static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k,
4839 struct opj_stream_private *p_stream,
4840 struct opj_event_mgr * p_manager )
4841 {
4842 OPJ_UINT32 i;
4843 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4844 opj_mct_data_t * l_mct_record;
4845 opj_tcp_t * l_tcp;
4846
4847 /* preconditions */
4848 assert(p_j2k != 00);
4849 assert(p_stream != 00);
4850 assert(p_manager != 00);
4851
4852 if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
4853 return OPJ_FALSE;
4854 }
4855
4856 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4857 l_mct_record = l_tcp->m_mct_records;
4858
4859 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4860
4861 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
4862 return OPJ_FALSE;
4863 }
4864
4865 ++l_mct_record;
4866 }
4867
4868 l_mcc_record = l_tcp->m_mcc_records;
4869
4870 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
4871
4872 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
4873 return OPJ_FALSE;
4874 }
4875
4876 ++l_mcc_record;
4877 }
4878
4879 if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
4880 return OPJ_FALSE;
4881 }
4882
4883 return OPJ_TRUE;
4884 }
4885
4886 #if 0
4887 static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
4888 struct opj_stream_private *p_stream,
4889 struct opj_event_mgr * p_manager )
4890 {
4891 OPJ_UINT32 compno;
4892
4893 /* preconditions */
4894 assert(p_j2k != 00);
4895 assert(p_manager != 00);
4896 assert(p_stream != 00);
4897
4898 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
4899 {
4900 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
4901 return OPJ_FALSE;
4902 }
4903 }
4904
4905 return OPJ_TRUE;
4906 }
4907 #endif
4908
4909 #if 0
4910 static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
4911 struct opj_stream_private *p_stream,
4912 struct opj_event_mgr * p_manager )
4913 {
4914 OPJ_UINT32 compno;
4915
4916 /* preconditions */
4917 assert(p_j2k != 00);
4918 assert(p_manager != 00);
4919 assert(p_stream != 00);
4920
4921 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
4922 {
4923 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
4924 return OPJ_FALSE;
4925 }
4926 }
4927
4928 return OPJ_TRUE;
4929 }
4930 #endif
4931
4932
opj_j2k_write_regions(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)4933 static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
4934 struct opj_stream_private *p_stream,
4935 struct opj_event_mgr * p_manager )
4936 {
4937 OPJ_UINT32 compno;
4938 const opj_tccp_t *l_tccp = 00;
4939
4940 /* preconditions */
4941 assert(p_j2k != 00);
4942 assert(p_manager != 00);
4943 assert(p_stream != 00);
4944
4945 l_tccp = p_j2k->m_cp.tcps->tccps;
4946
4947 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
4948 if (l_tccp->roishift) {
4949
4950 if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream,p_manager)) {
4951 return OPJ_FALSE;
4952 }
4953 }
4954
4955 ++l_tccp;
4956 }
4957
4958 return OPJ_TRUE;
4959 }
4960
opj_j2k_write_epc(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)4961 static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k,
4962 struct opj_stream_private *p_stream,
4963 struct opj_event_mgr * p_manager )
4964 {
4965 opj_codestream_index_t * l_cstr_index = 00;
4966
4967 /* preconditions */
4968 assert(p_j2k != 00);
4969 assert(p_manager != 00);
4970 assert(p_stream != 00);
4971
4972 l_cstr_index = p_j2k->cstr_index;
4973 if (l_cstr_index) {
4974 l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
4975 /* UniPG>> */
4976 /* The following adjustment is done to adjust the codestream size */
4977 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4978 /* the first bunch of bytes is not in the codestream */
4979 l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
4980 /* <<UniPG */
4981 }
4982
4983 #ifdef USE_JPWL
4984 /* preparation of JPWL marker segments */
4985 #if 0
4986 if(cp->epc_on) {
4987
4988 /* encode according to JPWL */
4989 jpwl_encode(p_j2k, p_stream, image);
4990
4991 }
4992 #endif
4993 assert( 0 && "TODO" );
4994 #endif /* USE_JPWL */
4995
4996 return OPJ_TRUE;
4997 }
4998
opj_j2k_read_unk(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,OPJ_UINT32 * output_marker,opj_event_mgr_t * p_manager)4999 static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k,
5000 opj_stream_private_t *p_stream,
5001 OPJ_UINT32 *output_marker,
5002 opj_event_mgr_t * p_manager
5003 )
5004 {
5005 OPJ_UINT32 l_unknown_marker;
5006 const opj_dec_memory_marker_handler_t * l_marker_handler;
5007 OPJ_UINT32 l_size_unk = 2;
5008
5009 /* preconditions*/
5010 assert(p_j2k != 00);
5011 assert(p_manager != 00);
5012 assert(p_stream != 00);
5013
5014 opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
5015
5016 for (;;) {
5017 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5018 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5019 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
5020 return OPJ_FALSE;
5021 }
5022
5023 /* read 2 bytes as the new marker ID*/
5024 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5025
5026 if (!(l_unknown_marker < 0xff00)) {
5027
5028 /* Get the marker handler from the marker ID*/
5029 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5030
5031 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5032 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5033 return OPJ_FALSE;
5034 }
5035 else {
5036 if (l_marker_handler->id != J2K_MS_UNK) {
5037 /* Add the marker to the codestream index*/
5038 if (l_marker_handler->id != J2K_MS_SOT)
5039 {
5040 OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
5041 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5042 l_size_unk);
5043 if (res == OPJ_FALSE) {
5044 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
5045 return OPJ_FALSE;
5046 }
5047 }
5048 break; /* next marker is known and well located */
5049 }
5050 else
5051 l_size_unk += 2;
5052 }
5053 }
5054 }
5055
5056 *output_marker = l_marker_handler->id ;
5057
5058 return OPJ_TRUE;
5059 }
5060
opj_j2k_write_mct_record(opj_j2k_t * p_j2k,opj_mct_data_t * p_mct_record,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5061 static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k,
5062 opj_mct_data_t * p_mct_record,
5063 struct opj_stream_private *p_stream,
5064 struct opj_event_mgr * p_manager )
5065 {
5066 OPJ_UINT32 l_mct_size;
5067 OPJ_BYTE * l_current_data = 00;
5068 OPJ_UINT32 l_tmp;
5069
5070 /* preconditions */
5071 assert(p_j2k != 00);
5072 assert(p_manager != 00);
5073 assert(p_stream != 00);
5074
5075 l_mct_size = 10 + p_mct_record->m_data_size;
5076
5077 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5078 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
5079 if (! new_header_tile_data) {
5080 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5081 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5082 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5083 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
5084 return OPJ_FALSE;
5085 }
5086 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5087 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5088 }
5089
5090 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5091
5092 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5093 l_current_data += 2;
5094
5095 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5096 l_current_data += 2;
5097
5098 opj_write_bytes(l_current_data,0,2); /* Zmct */
5099 l_current_data += 2;
5100
5101 /* only one marker atm */
5102 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5103
5104 opj_write_bytes(l_current_data,l_tmp,2);
5105 l_current_data += 2;
5106
5107 opj_write_bytes(l_current_data,0,2); /* Ymct */
5108 l_current_data+=2;
5109
5110 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5111
5112 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) {
5113 return OPJ_FALSE;
5114 }
5115
5116 return OPJ_TRUE;
5117 }
5118
5119 /**
5120 * Reads a MCT marker (Multiple Component Transform)
5121 *
5122 * @param p_header_data the data contained in the MCT box.
5123 * @param p_j2k the jpeg2000 codec.
5124 * @param p_header_size the size of the data contained in the MCT marker.
5125 * @param p_manager the user event manager.
5126 */
opj_j2k_read_mct(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5127 static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k,
5128 OPJ_BYTE * p_header_data,
5129 OPJ_UINT32 p_header_size,
5130 opj_event_mgr_t * p_manager
5131 )
5132 {
5133 OPJ_UINT32 i;
5134 opj_tcp_t *l_tcp = 00;
5135 OPJ_UINT32 l_tmp;
5136 OPJ_UINT32 l_indix;
5137 opj_mct_data_t * l_mct_data;
5138 OPJ_BOOL new_mct = OPJ_FALSE;
5139
5140 /* preconditions */
5141 assert(p_header_data != 00);
5142 assert(p_j2k != 00);
5143
5144 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5145 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5146 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5147
5148 if (p_header_size < 2) {
5149 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5150 return OPJ_FALSE;
5151 }
5152
5153 /* first marker */
5154 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5155 p_header_data += 2;
5156 if (l_tmp != 0) {
5157 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5158 return OPJ_TRUE;
5159 }
5160
5161 if(p_header_size <= 6) {
5162 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5163 return OPJ_FALSE;
5164 }
5165
5166 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5167 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5168 p_header_data += 2;
5169
5170 l_indix = l_tmp & 0xff;
5171 l_mct_data = l_tcp->m_mct_records;
5172
5173 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5174 if (l_mct_data->m_index == l_indix) {
5175 break;
5176 }
5177 ++l_mct_data;
5178 }
5179
5180 /* NOT FOUND */
5181 if (i == l_tcp->m_nb_mct_records) {
5182 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5183 opj_mct_data_t *new_mct_records;
5184 l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
5185
5186 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));
5187 if (! new_mct_records) {
5188 opj_free(l_tcp->m_mct_records);
5189 l_tcp->m_mct_records = NULL;
5190 l_tcp->m_nb_max_mct_records = 0;
5191 l_tcp->m_nb_mct_records = 0;
5192 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
5193 return OPJ_FALSE;
5194 }
5195 l_tcp->m_mct_records = new_mct_records;
5196 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5197 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5198 }
5199
5200 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5201 new_mct = OPJ_TRUE;
5202 }
5203
5204 if (l_mct_data->m_data) {
5205 opj_free(l_mct_data->m_data);
5206 l_mct_data->m_data = 00;
5207 }
5208
5209 l_mct_data->m_index = l_indix;
5210 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5211 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5212
5213 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5214 p_header_data+=2;
5215 if (l_tmp != 0) {
5216 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5217 return OPJ_TRUE;
5218 }
5219
5220 p_header_size -= 6;
5221
5222 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5223 if (! l_mct_data->m_data) {
5224 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5225 return OPJ_FALSE;
5226 }
5227 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5228
5229 l_mct_data->m_data_size = p_header_size;
5230
5231 if (new_mct) {
5232 ++l_tcp->m_nb_mct_records;
5233 }
5234 return OPJ_TRUE;
5235 }
5236
opj_j2k_write_mcc_record(opj_j2k_t * p_j2k,struct opj_simple_mcc_decorrelation_data * p_mcc_record,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5237 static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k,
5238 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5239 struct opj_stream_private *p_stream,
5240 struct opj_event_mgr * p_manager )
5241 {
5242 OPJ_UINT32 i;
5243 OPJ_UINT32 l_mcc_size;
5244 OPJ_BYTE * l_current_data = 00;
5245 OPJ_UINT32 l_nb_bytes_for_comp;
5246 OPJ_UINT32 l_mask;
5247 OPJ_UINT32 l_tmcc;
5248
5249 /* preconditions */
5250 assert(p_j2k != 00);
5251 assert(p_manager != 00);
5252 assert(p_stream != 00);
5253
5254 if (p_mcc_record->m_nb_comps > 255 ) {
5255 l_nb_bytes_for_comp = 2;
5256 l_mask = 0x8000;
5257 }
5258 else {
5259 l_nb_bytes_for_comp = 1;
5260 l_mask = 0;
5261 }
5262
5263 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5264 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5265 {
5266 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
5267 if (! new_header_tile_data) {
5268 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5269 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5270 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5271 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
5272 return OPJ_FALSE;
5273 }
5274 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5275 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5276 }
5277
5278 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5279
5280 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5281 l_current_data += 2;
5282
5283 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5284 l_current_data += 2;
5285
5286 /* first marker */
5287 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5288 l_current_data += 2;
5289
5290 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5291 ++l_current_data;
5292
5293 /* only one marker atm */
5294 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5295 l_current_data+=2;
5296
5297 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5298 l_current_data+=2;
5299
5300 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5301 ++l_current_data;
5302
5303 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 */
5304 l_current_data+=2;
5305
5306 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5307 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5308 l_current_data+=l_nb_bytes_for_comp;
5309 }
5310
5311 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 */
5312 l_current_data+=2;
5313
5314 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5315 {
5316 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5317 l_current_data+=l_nb_bytes_for_comp;
5318 }
5319
5320 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5321
5322 if (p_mcc_record->m_decorrelation_array) {
5323 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5324 }
5325
5326 if (p_mcc_record->m_offset_array) {
5327 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5328 }
5329
5330 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5331 l_current_data+=3;
5332
5333 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) {
5334 return OPJ_FALSE;
5335 }
5336
5337 return OPJ_TRUE;
5338 }
5339
opj_j2k_read_mcc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5340 static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k,
5341 OPJ_BYTE * p_header_data,
5342 OPJ_UINT32 p_header_size,
5343 opj_event_mgr_t * p_manager )
5344 {
5345 OPJ_UINT32 i,j;
5346 OPJ_UINT32 l_tmp;
5347 OPJ_UINT32 l_indix;
5348 opj_tcp_t * l_tcp;
5349 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5350 opj_mct_data_t * l_mct_data;
5351 OPJ_UINT32 l_nb_collections;
5352 OPJ_UINT32 l_nb_comps;
5353 OPJ_UINT32 l_nb_bytes_by_comp;
5354 OPJ_BOOL new_mcc = OPJ_FALSE;
5355
5356 /* preconditions */
5357 assert(p_header_data != 00);
5358 assert(p_j2k != 00);
5359 assert(p_manager != 00);
5360
5361 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5362 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5363 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5364
5365 if (p_header_size < 2) {
5366 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5367 return OPJ_FALSE;
5368 }
5369
5370 /* first marker */
5371 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5372 p_header_data += 2;
5373 if (l_tmp != 0) {
5374 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5375 return OPJ_TRUE;
5376 }
5377
5378 if (p_header_size < 7) {
5379 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5380 return OPJ_FALSE;
5381 }
5382
5383 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5384 ++p_header_data;
5385
5386 l_mcc_record = l_tcp->m_mcc_records;
5387
5388 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5389 if (l_mcc_record->m_index == l_indix) {
5390 break;
5391 }
5392 ++l_mcc_record;
5393 }
5394
5395 /** NOT FOUND */
5396 if (i == l_tcp->m_nb_mcc_records) {
5397 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5398 opj_simple_mcc_decorrelation_data_t *new_mcc_records;
5399 l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
5400
5401 new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
5402 l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5403 if (! new_mcc_records) {
5404 opj_free(l_tcp->m_mcc_records);
5405 l_tcp->m_mcc_records = NULL;
5406 l_tcp->m_nb_max_mcc_records = 0;
5407 l_tcp->m_nb_mcc_records = 0;
5408 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
5409 return OPJ_FALSE;
5410 }
5411 l_tcp->m_mcc_records = new_mcc_records;
5412 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5413 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));
5414 }
5415 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5416 new_mcc = OPJ_TRUE;
5417 }
5418 l_mcc_record->m_index = l_indix;
5419
5420 /* only one marker atm */
5421 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5422 p_header_data+=2;
5423 if (l_tmp != 0) {
5424 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5425 return OPJ_TRUE;
5426 }
5427
5428 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5429 p_header_data+=2;
5430
5431 if (l_nb_collections > 1) {
5432 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5433 return OPJ_TRUE;
5434 }
5435
5436 p_header_size -= 7;
5437
5438 for (i=0;i<l_nb_collections;++i) {
5439 if (p_header_size < 3) {
5440 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5441 return OPJ_FALSE;
5442 }
5443
5444 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5445 ++p_header_data;
5446
5447 if (l_tmp != 1) {
5448 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5449 return OPJ_TRUE;
5450 }
5451
5452 opj_read_bytes(p_header_data,&l_nb_comps,2);
5453
5454 p_header_data+=2;
5455 p_header_size-=3;
5456
5457 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5458 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5459
5460 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5461 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5462 return OPJ_FALSE;
5463 }
5464
5465 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5466
5467 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5468 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5469 p_header_data+=l_nb_bytes_by_comp;
5470
5471 if (l_tmp != j) {
5472 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5473 return OPJ_TRUE;
5474 }
5475 }
5476
5477 opj_read_bytes(p_header_data,&l_nb_comps,2);
5478 p_header_data+=2;
5479
5480 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5481 l_nb_comps &= 0x7fff;
5482
5483 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5484 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5485 return OPJ_TRUE;
5486 }
5487
5488 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5489 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5490 return OPJ_FALSE;
5491 }
5492
5493 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5494
5495 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5496 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5497 p_header_data+=l_nb_bytes_by_comp;
5498
5499 if (l_tmp != j) {
5500 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5501 return OPJ_TRUE;
5502 }
5503 }
5504
5505 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5506 p_header_data += 3;
5507
5508 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5509 l_mcc_record->m_decorrelation_array = 00;
5510 l_mcc_record->m_offset_array = 00;
5511
5512 l_indix = l_tmp & 0xff;
5513 if (l_indix != 0) {
5514 l_mct_data = l_tcp->m_mct_records;
5515 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5516 if (l_mct_data->m_index == l_indix) {
5517 l_mcc_record->m_decorrelation_array = l_mct_data;
5518 break;
5519 }
5520 ++l_mct_data;
5521 }
5522
5523 if (l_mcc_record->m_decorrelation_array == 00) {
5524 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5525 return OPJ_FALSE;
5526 }
5527 }
5528
5529 l_indix = (l_tmp >> 8) & 0xff;
5530 if (l_indix != 0) {
5531 l_mct_data = l_tcp->m_mct_records;
5532 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5533 if (l_mct_data->m_index == l_indix) {
5534 l_mcc_record->m_offset_array = l_mct_data;
5535 break;
5536 }
5537 ++l_mct_data;
5538 }
5539
5540 if (l_mcc_record->m_offset_array == 00) {
5541 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5542 return OPJ_FALSE;
5543 }
5544 }
5545 }
5546
5547 if (p_header_size != 0) {
5548 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5549 return OPJ_FALSE;
5550 }
5551
5552 if (new_mcc) {
5553 ++l_tcp->m_nb_mcc_records;
5554 }
5555
5556 return OPJ_TRUE;
5557 }
5558
opj_j2k_write_mco(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5559 static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k,
5560 struct opj_stream_private *p_stream,
5561 struct opj_event_mgr * p_manager
5562 )
5563 {
5564 OPJ_BYTE * l_current_data = 00;
5565 OPJ_UINT32 l_mco_size;
5566 opj_tcp_t * l_tcp = 00;
5567 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5568 OPJ_UINT32 i;
5569
5570 /* preconditions */
5571 assert(p_j2k != 00);
5572 assert(p_manager != 00);
5573 assert(p_stream != 00);
5574
5575 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5576
5577 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5578 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5579
5580 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
5581 if (! new_header_tile_data) {
5582 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5583 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5584 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5585 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
5586 return OPJ_FALSE;
5587 }
5588 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5589 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5590 }
5591 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5592
5593
5594 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5595 l_current_data += 2;
5596
5597 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5598 l_current_data += 2;
5599
5600 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one transform stage*/
5601 ++l_current_data;
5602
5603 l_mcc_record = l_tcp->m_mcc_records;
5604 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5605 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/
5606 ++l_current_data;
5607 ++l_mcc_record;
5608 }
5609
5610 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) {
5611 return OPJ_FALSE;
5612 }
5613
5614 return OPJ_TRUE;
5615 }
5616
5617 /**
5618 * Reads a MCO marker (Multiple Component Transform Ordering)
5619 *
5620 * @param p_header_data the data contained in the MCO box.
5621 * @param p_j2k the jpeg2000 codec.
5622 * @param p_header_size the size of the data contained in the MCO marker.
5623 * @param p_manager the user event manager.
5624 */
opj_j2k_read_mco(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5625 static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k,
5626 OPJ_BYTE * p_header_data,
5627 OPJ_UINT32 p_header_size,
5628 opj_event_mgr_t * p_manager
5629 )
5630 {
5631 OPJ_UINT32 l_tmp, i;
5632 OPJ_UINT32 l_nb_stages;
5633 opj_tcp_t * l_tcp;
5634 opj_tccp_t * l_tccp;
5635 opj_image_t * l_image;
5636
5637 /* preconditions */
5638 assert(p_header_data != 00);
5639 assert(p_j2k != 00);
5640 assert(p_manager != 00);
5641
5642 l_image = p_j2k->m_private_image;
5643 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5644 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5645 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5646
5647 if (p_header_size < 1) {
5648 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5649 return OPJ_FALSE;
5650 }
5651
5652 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one transform stage*/
5653 ++p_header_data;
5654
5655 if (l_nb_stages > 1) {
5656 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5657 return OPJ_TRUE;
5658 }
5659
5660 if (p_header_size != l_nb_stages + 1) {
5661 opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5662 return OPJ_FALSE;
5663 }
5664
5665 l_tccp = l_tcp->tccps;
5666
5667 for (i=0;i<l_image->numcomps;++i) {
5668 l_tccp->m_dc_level_shift = 0;
5669 ++l_tccp;
5670 }
5671
5672 if (l_tcp->m_mct_decoding_matrix) {
5673 opj_free(l_tcp->m_mct_decoding_matrix);
5674 l_tcp->m_mct_decoding_matrix = 00;
5675 }
5676
5677 for (i=0;i<l_nb_stages;++i) {
5678 opj_read_bytes(p_header_data,&l_tmp,1);
5679 ++p_header_data;
5680
5681 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5682 return OPJ_FALSE;
5683 }
5684 }
5685
5686 return OPJ_TRUE;
5687 }
5688
opj_j2k_add_mct(opj_tcp_t * p_tcp,opj_image_t * p_image,OPJ_UINT32 p_index)5689 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5690 {
5691 OPJ_UINT32 i;
5692 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5693 opj_mct_data_t * l_deco_array, * l_offset_array;
5694 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5695 OPJ_UINT32 l_nb_elem;
5696 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5697 opj_tccp_t * l_tccp;
5698
5699 /* preconditions */
5700 assert(p_tcp != 00);
5701
5702 l_mcc_record = p_tcp->m_mcc_records;
5703
5704 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5705 if (l_mcc_record->m_index == p_index) {
5706 break;
5707 }
5708 }
5709
5710 if (i==p_tcp->m_nb_mcc_records) {
5711 /** element discarded **/
5712 return OPJ_TRUE;
5713 }
5714
5715 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5716 /** do not support number of comps != image */
5717 return OPJ_TRUE;
5718 }
5719
5720 l_deco_array = l_mcc_record->m_decorrelation_array;
5721
5722 if (l_deco_array) {
5723 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5724 if (l_deco_array->m_data_size != l_data_size || ! l_deco_array->m_data) {
5725 return OPJ_FALSE;
5726 }
5727
5728 l_nb_elem = p_image->numcomps * p_image->numcomps;
5729 l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
5730 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5731
5732 if (! p_tcp->m_mct_decoding_matrix ) {
5733 return OPJ_FALSE;
5734 }
5735
5736 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);
5737 }
5738
5739 l_offset_array = l_mcc_record->m_offset_array;
5740
5741 if (l_offset_array) {
5742 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5743 if (l_offset_array->m_data_size != l_data_size || ! l_offset_array->m_data) {
5744 return OPJ_FALSE;
5745 }
5746
5747 l_nb_elem = p_image->numcomps;
5748 l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
5749 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5750
5751 if (! l_offset_data ) {
5752 return OPJ_FALSE;
5753 }
5754
5755 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5756
5757 l_tccp = p_tcp->tccps;
5758 l_current_offset_data = l_offset_data;
5759
5760 for (i=0;i<p_image->numcomps;++i) {
5761 l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++);
5762 ++l_tccp;
5763 }
5764
5765 opj_free(l_offset_data);
5766 }
5767
5768 return OPJ_TRUE;
5769 }
5770
opj_j2k_write_cbd(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5771 static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
5772 struct opj_stream_private *p_stream,
5773 struct opj_event_mgr * p_manager )
5774 {
5775 OPJ_UINT32 i;
5776 OPJ_UINT32 l_cbd_size;
5777 OPJ_BYTE * l_current_data = 00;
5778 opj_image_t *l_image = 00;
5779 opj_image_comp_t * l_comp = 00;
5780
5781 /* preconditions */
5782 assert(p_j2k != 00);
5783 assert(p_manager != 00);
5784 assert(p_stream != 00);
5785
5786 l_image = p_j2k->m_private_image;
5787 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
5788
5789 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5790 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
5791 if (! new_header_tile_data) {
5792 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5793 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5794 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5795 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
5796 return OPJ_FALSE;
5797 }
5798 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5799 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
5800 }
5801
5802 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5803
5804 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
5805 l_current_data += 2;
5806
5807 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
5808 l_current_data += 2;
5809
5810 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
5811 l_current_data+=2;
5812
5813 l_comp = l_image->comps;
5814
5815 for (i=0;i<l_image->numcomps;++i) {
5816 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
5817 ++l_current_data;
5818
5819 ++l_comp;
5820 }
5821
5822 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) {
5823 return OPJ_FALSE;
5824 }
5825
5826 return OPJ_TRUE;
5827 }
5828
5829 /**
5830 * Reads a CBD marker (Component bit depth definition)
5831 * @param p_header_data the data contained in the CBD box.
5832 * @param p_j2k the jpeg2000 codec.
5833 * @param p_header_size the size of the data contained in the CBD marker.
5834 * @param p_manager the user event manager.
5835 */
opj_j2k_read_cbd(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5836 static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k,
5837 OPJ_BYTE * p_header_data,
5838 OPJ_UINT32 p_header_size,
5839 opj_event_mgr_t * p_manager
5840 )
5841 {
5842 OPJ_UINT32 l_nb_comp,l_num_comp;
5843 OPJ_UINT32 l_comp_def;
5844 OPJ_UINT32 i;
5845 opj_image_comp_t * l_comp = 00;
5846
5847 /* preconditions */
5848 assert(p_header_data != 00);
5849 assert(p_j2k != 00);
5850 assert(p_manager != 00);
5851
5852 l_num_comp = p_j2k->m_private_image->numcomps;
5853
5854 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
5855 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5856 return OPJ_FALSE;
5857 }
5858
5859 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
5860 p_header_data+=2;
5861
5862 if (l_nb_comp != l_num_comp) {
5863 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5864 return OPJ_FALSE;
5865 }
5866
5867 l_comp = p_j2k->m_private_image->comps;
5868 for (i=0;i<l_num_comp;++i) {
5869 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
5870 ++p_header_data;
5871 l_comp->sgnd = (l_comp_def>>7) & 1;
5872 l_comp->prec = (l_comp_def&0x7f) + 1;
5873 ++l_comp;
5874 }
5875
5876 return OPJ_TRUE;
5877 }
5878
5879 /* ----------------------------------------------------------------------- */
5880 /* J2K / JPT decoder interface */
5881 /* ----------------------------------------------------------------------- */
5882
opj_j2k_setup_decoder(opj_j2k_t * j2k,opj_dparameters_t * parameters)5883 void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
5884 {
5885 if(j2k && parameters) {
5886 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5887 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5888
5889 #ifdef USE_JPWL
5890 j2k->m_cp.correct = parameters->jpwl_correct;
5891 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5892 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5893 #endif /* USE_JPWL */
5894 }
5895 }
5896
5897 /* ----------------------------------------------------------------------- */
5898 /* J2K encoder interface */
5899 /* ----------------------------------------------------------------------- */
5900
opj_j2k_create_compress(void)5901 opj_j2k_t* opj_j2k_create_compress(void)
5902 {
5903 opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t));
5904 if (!l_j2k) {
5905 return NULL;
5906 }
5907
5908
5909 l_j2k->m_is_decoder = 0;
5910 l_j2k->m_cp.m_is_decoder = 0;
5911
5912 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
5913 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5914 opj_j2k_destroy(l_j2k);
5915 return NULL;
5916 }
5917
5918 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
5919
5920 /* validation list creation*/
5921 l_j2k->m_validation_list = opj_procedure_list_create();
5922 if (! l_j2k->m_validation_list) {
5923 opj_j2k_destroy(l_j2k);
5924 return NULL;
5925 }
5926
5927 /* execution list creation*/
5928 l_j2k->m_procedure_list = opj_procedure_list_create();
5929 if (! l_j2k->m_procedure_list) {
5930 opj_j2k_destroy(l_j2k);
5931 return NULL;
5932 }
5933
5934 return l_j2k;
5935 }
5936
opj_j2k_initialise_4K_poc(opj_poc_t * POC,int numres)5937 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){
5938 POC[0].tile = 1;
5939 POC[0].resno0 = 0;
5940 POC[0].compno0 = 0;
5941 POC[0].layno1 = 1;
5942 POC[0].resno1 = (OPJ_UINT32)(numres-1);
5943 POC[0].compno1 = 3;
5944 POC[0].prg1 = OPJ_CPRL;
5945 POC[1].tile = 1;
5946 POC[1].resno0 = (OPJ_UINT32)(numres-1);
5947 POC[1].compno0 = 0;
5948 POC[1].layno1 = 1;
5949 POC[1].resno1 = (OPJ_UINT32)numres;
5950 POC[1].compno1 = 3;
5951 POC[1].prg1 = OPJ_CPRL;
5952 return 2;
5953 }
5954
opj_j2k_set_cinema_parameters(opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)5955 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)
5956 {
5957 /* Configure cinema parameters */
5958 int i;
5959
5960 /* No tiling */
5961 parameters->tile_size_on = OPJ_FALSE;
5962 parameters->cp_tdx=1;
5963 parameters->cp_tdy=1;
5964
5965 /* One tile part for each component */
5966 parameters->tp_flag = 'C';
5967 parameters->tp_on = 1;
5968
5969 /* Tile and Image shall be at (0,0) */
5970 parameters->cp_tx0 = 0;
5971 parameters->cp_ty0 = 0;
5972 parameters->image_offset_x0 = 0;
5973 parameters->image_offset_y0 = 0;
5974
5975 /* Codeblock size= 32*32 */
5976 parameters->cblockw_init = 32;
5977 parameters->cblockh_init = 32;
5978
5979 /* Codeblock style: no mode switch enabled */
5980 parameters->mode = 0;
5981
5982 /* No ROI */
5983 parameters->roi_compno = -1;
5984
5985 /* No subsampling */
5986 parameters->subsampling_dx = 1;
5987 parameters->subsampling_dy = 1;
5988
5989 /* 9-7 transform */
5990 parameters->irreversible = 1;
5991
5992 /* Number of layers */
5993 if (parameters->tcp_numlayers > 1){
5994 opj_event_msg(p_manager, EVT_WARNING,
5995 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
5996 "1 single quality layer"
5997 "-> Number of layers forced to 1 (rather than %d)\n"
5998 "-> Rate of the last layer (%3.1f) will be used",
5999 parameters->tcp_numlayers, parameters->tcp_rates[parameters->tcp_numlayers-1]);
6000 parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers-1];
6001 parameters->tcp_numlayers = 1;
6002 }
6003
6004 /* Resolution levels */
6005 switch (parameters->rsiz){
6006 case OPJ_PROFILE_CINEMA_2K:
6007 if(parameters->numresolution > 6){
6008 opj_event_msg(p_manager, EVT_WARNING,
6009 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6010 "Number of decomposition levels <= 5\n"
6011 "-> Number of decomposition levels forced to 5 (rather than %d)\n",
6012 parameters->numresolution+1);
6013 parameters->numresolution = 6;
6014 }
6015 break;
6016 case OPJ_PROFILE_CINEMA_4K:
6017 if(parameters->numresolution < 2){
6018 opj_event_msg(p_manager, EVT_WARNING,
6019 "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6020 "Number of decomposition levels >= 1 && <= 6\n"
6021 "-> Number of decomposition levels forced to 1 (rather than %d)\n",
6022 parameters->numresolution+1);
6023 parameters->numresolution = 1;
6024 }else if(parameters->numresolution > 7){
6025 opj_event_msg(p_manager, EVT_WARNING,
6026 "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6027 "Number of decomposition levels >= 1 && <= 6\n"
6028 "-> Number of decomposition levels forced to 6 (rather than %d)\n",
6029 parameters->numresolution+1);
6030 parameters->numresolution = 7;
6031 }
6032 break;
6033 default :
6034 break;
6035 }
6036
6037 /* Precincts */
6038 parameters->csty |= 0x01;
6039 parameters->res_spec = parameters->numresolution-1;
6040 for (i = 0; i<parameters->res_spec; i++) {
6041 parameters->prcw_init[i] = 256;
6042 parameters->prch_init[i] = 256;
6043 }
6044
6045 /* The progression order shall be CPRL */
6046 parameters->prog_order = OPJ_CPRL;
6047
6048 /* Progression order changes for 4K, disallowed for 2K */
6049 if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
6050 parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
6051 } else {
6052 parameters->numpocs = 0;
6053 }
6054
6055 /* Limited bit-rate */
6056 parameters->cp_disto_alloc = 1;
6057 if (parameters->max_cs_size <= 0) {
6058 /* No rate has been introduced, 24 fps is assumed */
6059 parameters->max_cs_size = OPJ_CINEMA_24_CS;
6060 opj_event_msg(p_manager, EVT_WARNING,
6061 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6062 "Maximum 1302083 compressed bytes @ 24fps\n"
6063 "As no rate has been given, this limit will be used.\n");
6064 } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
6065 opj_event_msg(p_manager, EVT_WARNING,
6066 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6067 "Maximum 1302083 compressed bytes @ 24fps\n"
6068 "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
6069 parameters->max_cs_size = OPJ_CINEMA_24_CS;
6070 }
6071
6072 if (parameters->max_comp_size <= 0) {
6073 /* No rate has been introduced, 24 fps is assumed */
6074 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6075 opj_event_msg(p_manager, EVT_WARNING,
6076 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6077 "Maximum 1041666 compressed bytes @ 24fps\n"
6078 "As no rate has been given, this limit will be used.\n");
6079 } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
6080 opj_event_msg(p_manager, EVT_WARNING,
6081 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6082 "Maximum 1041666 compressed bytes @ 24fps\n"
6083 "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
6084 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6085 }
6086
6087 parameters->tcp_rates[0] = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
6088 (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy);
6089
6090 }
6091
opj_j2k_is_cinema_compliant(opj_image_t * image,OPJ_UINT16 rsiz,opj_event_mgr_t * p_manager)6092 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager)
6093 {
6094 OPJ_UINT32 i;
6095
6096 /* Number of components */
6097 if (image->numcomps != 3){
6098 opj_event_msg(p_manager, EVT_WARNING,
6099 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6100 "3 components"
6101 "-> Number of components of input image (%d) is not compliant\n"
6102 "-> Non-profile-3 codestream will be generated\n",
6103 image->numcomps);
6104 return OPJ_FALSE;
6105 }
6106
6107 /* Bitdepth */
6108 for (i = 0; i < image->numcomps; i++) {
6109 if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){
6110 char signed_str[] = "signed";
6111 char unsigned_str[] = "unsigned";
6112 char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str;
6113 opj_event_msg(p_manager, EVT_WARNING,
6114 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6115 "Precision of each component shall be 12 bits unsigned"
6116 "-> At least component %d of input image (%d bits, %s) is not compliant\n"
6117 "-> Non-profile-3 codestream will be generated\n",
6118 i,image->comps[i].bpp, tmp_str);
6119 return OPJ_FALSE;
6120 }
6121 }
6122
6123 /* Image size */
6124 switch (rsiz){
6125 case OPJ_PROFILE_CINEMA_2K:
6126 if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){
6127 opj_event_msg(p_manager, EVT_WARNING,
6128 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6129 "width <= 2048 and height <= 1080\n"
6130 "-> Input image size %d x %d is not compliant\n"
6131 "-> Non-profile-3 codestream will be generated\n",
6132 image->comps[0].w,image->comps[0].h);
6133 return OPJ_FALSE;
6134 }
6135 break;
6136 case OPJ_PROFILE_CINEMA_4K:
6137 if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){
6138 opj_event_msg(p_manager, EVT_WARNING,
6139 "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6140 "width <= 4096 and height <= 2160\n"
6141 "-> Image size %d x %d is not compliant\n"
6142 "-> Non-profile-4 codestream will be generated\n",
6143 image->comps[0].w,image->comps[0].h);
6144 return OPJ_FALSE;
6145 }
6146 break;
6147 default :
6148 break;
6149 }
6150
6151 return OPJ_TRUE;
6152 }
6153
opj_j2k_setup_encoder(opj_j2k_t * p_j2k,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)6154 OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
6155 opj_cparameters_t *parameters,
6156 opj_image_t *image,
6157 opj_event_mgr_t * p_manager)
6158 {
6159 OPJ_UINT32 i, j, tileno, numpocs_tile;
6160 opj_cp_t *cp = 00;
6161
6162 if(!p_j2k || !parameters || ! image) {
6163 return OPJ_FALSE;
6164 }
6165
6166 if ((parameters->numresolution <= 0) || (parameters->numresolution > OPJ_J2K_MAXRLVLS)) {
6167 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of resolutions : %d not in range [1,%d]\n", parameters->numresolution, OPJ_J2K_MAXRLVLS);
6168 return OPJ_FALSE;
6169 }
6170
6171 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6172 cp = &(p_j2k->m_cp);
6173
6174 /* set default values for cp */
6175 cp->tw = 1;
6176 cp->th = 1;
6177
6178 /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
6179 if (parameters->rsiz == OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
6180 OPJ_BOOL deprecated_used = OPJ_FALSE;
6181 switch (parameters->cp_cinema){
6182 case OPJ_CINEMA2K_24:
6183 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6184 parameters->max_cs_size = OPJ_CINEMA_24_CS;
6185 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6186 deprecated_used = OPJ_TRUE;
6187 break;
6188 case OPJ_CINEMA2K_48:
6189 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6190 parameters->max_cs_size = OPJ_CINEMA_48_CS;
6191 parameters->max_comp_size = OPJ_CINEMA_48_COMP;
6192 deprecated_used = OPJ_TRUE;
6193 break;
6194 case OPJ_CINEMA4K_24:
6195 parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
6196 parameters->max_cs_size = OPJ_CINEMA_24_CS;
6197 parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6198 deprecated_used = OPJ_TRUE;
6199 break;
6200 case OPJ_OFF:
6201 default:
6202 break;
6203 }
6204 switch (parameters->cp_rsiz){
6205 case OPJ_CINEMA2K:
6206 parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6207 deprecated_used = OPJ_TRUE;
6208 break;
6209 case OPJ_CINEMA4K:
6210 parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
6211 deprecated_used = OPJ_TRUE;
6212 break;
6213 case OPJ_MCT:
6214 parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
6215 deprecated_used = OPJ_TRUE;
6216 case OPJ_STD_RSIZ:
6217 default:
6218 break;
6219 }
6220 if (deprecated_used) {
6221 opj_event_msg(p_manager, EVT_WARNING,
6222 "Deprecated fields cp_cinema or cp_rsiz are used\n"
6223 "Please consider using only the rsiz field\n"
6224 "See openjpeg.h documentation for more details\n");
6225 }
6226 }
6227
6228 /* see if max_codestream_size does limit input rate */
6229 if (parameters->max_cs_size <= 0) {
6230 if (parameters->tcp_rates[parameters->tcp_numlayers-1] > 0) {
6231 OPJ_FLOAT32 temp_size;
6232 temp_size =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
6233 (parameters->tcp_rates[parameters->tcp_numlayers-1] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy);
6234 parameters->max_cs_size = (int) floor(temp_size);
6235 } else {
6236 parameters->max_cs_size = 0;
6237 }
6238 } else {
6239 OPJ_FLOAT32 temp_rate;
6240 OPJ_BOOL cap = OPJ_FALSE;
6241 temp_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
6242 (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy);
6243 for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
6244 if (parameters->tcp_rates[i] < temp_rate) {
6245 parameters->tcp_rates[i] = temp_rate;
6246 cap = OPJ_TRUE;
6247 }
6248 }
6249 if (cap) {
6250 opj_event_msg(p_manager, EVT_WARNING,
6251 "The desired maximum codestream size has limited\n"
6252 "at least one of the desired quality layers\n");
6253 }
6254 }
6255
6256 /* Manage profiles and applications and set RSIZ */
6257 /* set cinema parameters if required */
6258 if (OPJ_IS_CINEMA(parameters->rsiz)){
6259 if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
6260 || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)){
6261 opj_event_msg(p_manager, EVT_WARNING,
6262 "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
6263 parameters->rsiz = OPJ_PROFILE_NONE;
6264 } else {
6265 opj_j2k_set_cinema_parameters(parameters,image,p_manager);
6266 if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,p_manager)) {
6267 parameters->rsiz = OPJ_PROFILE_NONE;
6268 }
6269 }
6270 } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
6271 opj_event_msg(p_manager, EVT_WARNING,
6272 "JPEG 2000 Long Term Storage profile not yet supported\n");
6273 parameters->rsiz = OPJ_PROFILE_NONE;
6274 } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
6275 opj_event_msg(p_manager, EVT_WARNING,
6276 "JPEG 2000 Broadcast profiles not yet supported\n");
6277 parameters->rsiz = OPJ_PROFILE_NONE;
6278 } else if (OPJ_IS_IMF(parameters->rsiz)) {
6279 opj_event_msg(p_manager, EVT_WARNING,
6280 "JPEG 2000 IMF profiles not yet supported\n");
6281 parameters->rsiz = OPJ_PROFILE_NONE;
6282 } else if (OPJ_IS_PART2(parameters->rsiz)) {
6283 if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
6284 opj_event_msg(p_manager, EVT_WARNING,
6285 "JPEG 2000 Part-2 profile defined\n"
6286 "but no Part-2 extension enabled.\n"
6287 "Profile set to NONE.\n");
6288 parameters->rsiz = OPJ_PROFILE_NONE;
6289 } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
6290 opj_event_msg(p_manager, EVT_WARNING,
6291 "Unsupported Part-2 extension enabled\n"
6292 "Profile set to NONE.\n");
6293 parameters->rsiz = OPJ_PROFILE_NONE;
6294 }
6295 }
6296
6297 /*
6298 copy user encoding parameters
6299 */
6300 cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size;
6301 cp->rsiz = parameters->rsiz;
6302 cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u;
6303 cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u;
6304 cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u;
6305
6306 /* mod fixed_quality */
6307 if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
6308 size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
6309 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6310 if (!cp->m_specific_param.m_enc.m_matrice) {
6311 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of user encoding parameters matrix \n");
6312 return OPJ_FALSE;
6313 }
6314 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6315 }
6316
6317 /* tiles */
6318 cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
6319 cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
6320
6321 /* tile offset */
6322 cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
6323 cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
6324
6325 /* comment string */
6326 if(parameters->cp_comment) {
6327 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U);
6328 if(!cp->comment) {
6329 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of comment string\n");
6330 return OPJ_FALSE;
6331 }
6332 strcpy(cp->comment, parameters->cp_comment);
6333 } else {
6334 /* Create default comment for codestream */
6335 const char comment[] = "Created by OpenJPEG version ";
6336 const size_t clen = strlen(comment);
6337 const char *version = opj_version();
6338
6339 /* UniPG>> */
6340 #ifdef USE_JPWL
6341 cp->comment = (char*)opj_malloc(clen+strlen(version)+11);
6342 if(!cp->comment) {
6343 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n");
6344 return OPJ_FALSE;
6345 }
6346 sprintf(cp->comment,"%s%s with JPWL", comment, version);
6347 #else
6348 cp->comment = (char*)opj_malloc(clen+strlen(version)+1);
6349 if(!cp->comment) {
6350 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n");
6351 return OPJ_FALSE;
6352 }
6353 sprintf(cp->comment,"%s%s", comment, version);
6354 #endif
6355 /* <<UniPG */
6356 }
6357
6358 /*
6359 calculate other encoding parameters
6360 */
6361
6362 if (parameters->tile_size_on) {
6363 cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx);
6364 cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy);
6365 } else {
6366 cp->tdx = image->x1 - cp->tx0;
6367 cp->tdy = image->y1 - cp->ty0;
6368 }
6369
6370 if (parameters->tp_on) {
6371 cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
6372 cp->m_specific_param.m_enc.m_tp_on = 1;
6373 }
6374
6375 #ifdef USE_JPWL
6376 /*
6377 calculate JPWL encoding parameters
6378 */
6379
6380 if (parameters->jpwl_epc_on) {
6381 OPJ_INT32 i;
6382
6383 /* set JPWL on */
6384 cp->epc_on = OPJ_TRUE;
6385 cp->info_on = OPJ_FALSE; /* no informative technique */
6386
6387 /* set EPB on */
6388 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6389 cp->epb_on = OPJ_TRUE;
6390
6391 cp->hprot_MH = parameters->jpwl_hprot_MH;
6392 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6393 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6394 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6395 }
6396 /* if tile specs are not specified, copy MH specs */
6397 if (cp->hprot_TPH[0] == -1) {
6398 cp->hprot_TPH_tileno[0] = 0;
6399 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6400 }
6401 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6402 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6403 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6404 cp->pprot[i] = parameters->jpwl_pprot[i];
6405 }
6406 }
6407
6408 /* set ESD writing */
6409 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6410 cp->esd_on = OPJ_TRUE;
6411
6412 cp->sens_size = parameters->jpwl_sens_size;
6413 cp->sens_addr = parameters->jpwl_sens_addr;
6414 cp->sens_range = parameters->jpwl_sens_range;
6415
6416 cp->sens_MH = parameters->jpwl_sens_MH;
6417 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6418 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6419 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6420 }
6421 }
6422
6423 /* always set RED writing to false: we are at the encoder */
6424 cp->red_on = OPJ_FALSE;
6425
6426 } else {
6427 cp->epc_on = OPJ_FALSE;
6428 }
6429 #endif /* USE_JPWL */
6430
6431 /* initialize the mutiple tiles */
6432 /* ---------------------------- */
6433 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6434 if (!cp->tcps) {
6435 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile coding parameters\n");
6436 return OPJ_FALSE;
6437 }
6438 if (parameters->numpocs) {
6439 /* initialisation of POC */
6440 opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
6441 /* TODO MSD use the return value*/
6442 }
6443
6444 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6445 opj_tcp_t *tcp = &cp->tcps[tileno];
6446 tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
6447
6448 for (j = 0; j < tcp->numlayers; j++) {
6449 if(OPJ_IS_CINEMA(cp->rsiz)){
6450 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6451 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6452 }
6453 tcp->rates[j] = parameters->tcp_rates[j];
6454 }else{
6455 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6456 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6457 } else {
6458 tcp->rates[j] = parameters->tcp_rates[j];
6459 }
6460 }
6461 }
6462
6463 tcp->csty = (OPJ_UINT32)parameters->csty;
6464 tcp->prg = parameters->prog_order;
6465 tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
6466
6467 numpocs_tile = 0;
6468 tcp->POC = 0;
6469
6470 if (parameters->numpocs) {
6471 /* initialisation of POC */
6472 tcp->POC = 1;
6473 for (i = 0; i < parameters->numpocs; i++) {
6474 if (tileno + 1 == parameters->POC[i].tile ) {
6475 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6476
6477 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6478 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6479 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6480 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6481 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6482 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6483 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6484
6485 numpocs_tile++;
6486 }
6487 }
6488
6489 tcp->numpocs = numpocs_tile -1 ;
6490 }else{
6491 tcp->numpocs = 0;
6492 }
6493
6494 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6495 if (!tcp->tccps) {
6496 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile component coding parameters\n");
6497 return OPJ_FALSE;
6498 }
6499 if (parameters->mct_data) {
6500
6501 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
6502 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6503 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6504
6505 if (!lTmpBuf) {
6506 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate temp buffer\n");
6507 return OPJ_FALSE;
6508 }
6509
6510 tcp->mct = 2;
6511 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6512 if (! tcp->m_mct_coding_matrix) {
6513 opj_free(lTmpBuf);
6514 lTmpBuf = NULL;
6515 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT coding matrix \n");
6516 return OPJ_FALSE;
6517 }
6518 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6519 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6520
6521 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6522 if (! tcp->m_mct_decoding_matrix) {
6523 opj_free(lTmpBuf);
6524 lTmpBuf = NULL;
6525 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT decoding matrix \n");
6526 return OPJ_FALSE;
6527 }
6528 if(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps) == OPJ_FALSE) {
6529 opj_free(lTmpBuf);
6530 lTmpBuf = NULL;
6531 opj_event_msg(p_manager, EVT_ERROR, "Failed to inverse encoder MCT decoding matrix \n");
6532 return OPJ_FALSE;
6533 }
6534
6535 tcp->mct_norms = (OPJ_FLOAT64*)
6536 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6537 if (! tcp->mct_norms) {
6538 opj_free(lTmpBuf);
6539 lTmpBuf = NULL;
6540 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT norms \n");
6541 return OPJ_FALSE;
6542 }
6543 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6544 opj_free(lTmpBuf);
6545
6546 for (i = 0; i < image->numcomps; i++) {
6547 opj_tccp_t *tccp = &tcp->tccps[i];
6548 tccp->m_dc_level_shift = l_dc_shift[i];
6549 }
6550
6551 if (opj_j2k_setup_mct_encoding(tcp,image) == OPJ_FALSE) {
6552 /* free will be handled by opj_j2k_destroy */
6553 opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n");
6554 return OPJ_FALSE;
6555 }
6556 }
6557 else {
6558 if(tcp->mct==1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */
6559 if ((image->comps[0].dx != image->comps[1].dx) ||
6560 (image->comps[0].dx != image->comps[2].dx) ||
6561 (image->comps[0].dy != image->comps[1].dy) ||
6562 (image->comps[0].dy != image->comps[2].dy)) {
6563 opj_event_msg(p_manager, EVT_WARNING, "Cannot perform MCT on components with different sizes. Disabling MCT.\n");
6564 tcp->mct = 0;
6565 }
6566 }
6567 for (i = 0; i < image->numcomps; i++) {
6568 opj_tccp_t *tccp = &tcp->tccps[i];
6569 opj_image_comp_t * l_comp = &(image->comps[i]);
6570
6571 if (! l_comp->sgnd) {
6572 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6573 }
6574 }
6575 }
6576
6577 for (i = 0; i < image->numcomps; i++) {
6578 opj_tccp_t *tccp = &tcp->tccps[i];
6579
6580 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6581 tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
6582 tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
6583 tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
6584 tccp->cblksty = (OPJ_UINT32)parameters->mode;
6585 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6586 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6587 tccp->numgbits = 2;
6588
6589 if ((OPJ_INT32)i == parameters->roi_compno) {
6590 tccp->roishift = parameters->roi_shift;
6591 } else {
6592 tccp->roishift = 0;
6593 }
6594
6595 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6596 OPJ_INT32 p = 0, it_res;
6597 assert( tccp->numresolutions > 0 );
6598 for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
6599 if (p < parameters->res_spec) {
6600
6601 if (parameters->prcw_init[p] < 1) {
6602 tccp->prcw[it_res] = 1;
6603 } else {
6604 tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
6605 }
6606
6607 if (parameters->prch_init[p] < 1) {
6608 tccp->prch[it_res] = 1;
6609 }else {
6610 tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
6611 }
6612
6613 } else {
6614 OPJ_INT32 res_spec = parameters->res_spec;
6615 OPJ_INT32 size_prcw = 0;
6616 OPJ_INT32 size_prch = 0;
6617
6618 assert(res_spec>0); /* issue 189 */
6619 size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6620 size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6621
6622
6623 if (size_prcw < 1) {
6624 tccp->prcw[it_res] = 1;
6625 } else {
6626 tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
6627 }
6628
6629 if (size_prch < 1) {
6630 tccp->prch[it_res] = 1;
6631 } else {
6632 tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
6633 }
6634 }
6635 p++;
6636 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6637 } /*end for*/
6638 } else {
6639 for (j = 0; j < tccp->numresolutions; j++) {
6640 tccp->prcw[j] = 15;
6641 tccp->prch[j] = 15;
6642 }
6643 }
6644
6645 opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6646 }
6647 }
6648
6649 if (parameters->mct_data) {
6650 opj_free(parameters->mct_data);
6651 parameters->mct_data = 00;
6652 }
6653 return OPJ_TRUE;
6654 }
6655
opj_j2k_add_mhmarker(opj_codestream_index_t * cstr_index,OPJ_UINT32 type,OPJ_OFF_T pos,OPJ_UINT32 len)6656 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6657 {
6658 assert(cstr_index != 00);
6659
6660 /* expand the list? */
6661 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6662 opj_marker_info_t *new_marker;
6663 cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum);
6664 new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6665 if (! new_marker) {
6666 opj_free(cstr_index->marker);
6667 cstr_index->marker = NULL;
6668 cstr_index->maxmarknum = 0;
6669 cstr_index->marknum = 0;
6670 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
6671 return OPJ_FALSE;
6672 }
6673 cstr_index->marker = new_marker;
6674 }
6675
6676 /* add the marker */
6677 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6678 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6679 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6680 cstr_index->marknum++;
6681 return OPJ_TRUE;
6682 }
6683
opj_j2k_add_tlmarker(OPJ_UINT32 tileno,opj_codestream_index_t * cstr_index,OPJ_UINT32 type,OPJ_OFF_T pos,OPJ_UINT32 len)6684 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)
6685 {
6686 assert(cstr_index != 00);
6687 assert(cstr_index->tile_index != 00);
6688
6689 /* expand the list? */
6690 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6691 opj_marker_info_t *new_marker;
6692 cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
6693 new_marker = (opj_marker_info_t *) opj_realloc(
6694 cstr_index->tile_index[tileno].marker,
6695 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6696 if (! new_marker) {
6697 opj_free(cstr_index->tile_index[tileno].marker);
6698 cstr_index->tile_index[tileno].marker = NULL;
6699 cstr_index->tile_index[tileno].maxmarknum = 0;
6700 cstr_index->tile_index[tileno].marknum = 0;
6701 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
6702 return OPJ_FALSE;
6703 }
6704 cstr_index->tile_index[tileno].marker = new_marker;
6705 }
6706
6707 /* add the marker */
6708 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6709 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6710 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6711 cstr_index->tile_index[tileno].marknum++;
6712
6713 if (type == J2K_MS_SOT) {
6714 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6715
6716 if (cstr_index->tile_index[tileno].tp_index)
6717 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6718
6719 }
6720 return OPJ_TRUE;
6721 }
6722
6723 /*
6724 * -----------------------------------------------------------------------
6725 * -----------------------------------------------------------------------
6726 * -----------------------------------------------------------------------
6727 */
6728
opj_j2k_end_decompress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)6729 OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
6730 opj_stream_private_t *p_stream,
6731 opj_event_mgr_t * p_manager
6732 )
6733 {
6734 (void)p_j2k;
6735 (void)p_stream;
6736 (void)p_manager;
6737 return OPJ_TRUE;
6738 }
6739
opj_j2k_read_header(opj_stream_private_t * p_stream,opj_j2k_t * p_j2k,opj_image_t ** p_image,opj_event_mgr_t * p_manager)6740 OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream,
6741 opj_j2k_t* p_j2k,
6742 opj_image_t** p_image,
6743 opj_event_mgr_t* p_manager )
6744 {
6745 /* preconditions */
6746 assert(p_j2k != 00);
6747 assert(p_stream != 00);
6748 assert(p_manager != 00);
6749
6750 /* create an empty image header */
6751 p_j2k->m_private_image = opj_image_create0();
6752 if (! p_j2k->m_private_image) {
6753 return OPJ_FALSE;
6754 }
6755
6756 /* customization of the validation */
6757 if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) {
6758 opj_image_destroy(p_j2k->m_private_image);
6759 p_j2k->m_private_image = NULL;
6760 return OPJ_FALSE;
6761 }
6762
6763 /* validation of the parameters codec */
6764 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6765 opj_image_destroy(p_j2k->m_private_image);
6766 p_j2k->m_private_image = NULL;
6767 return OPJ_FALSE;
6768 }
6769
6770 /* customization of the encoding */
6771 if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) {
6772 opj_image_destroy(p_j2k->m_private_image);
6773 p_j2k->m_private_image = NULL;
6774 return OPJ_FALSE;
6775 }
6776
6777 /* read header */
6778 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6779 opj_image_destroy(p_j2k->m_private_image);
6780 p_j2k->m_private_image = NULL;
6781 return OPJ_FALSE;
6782 }
6783
6784 *p_image = opj_image_create0();
6785 if (! (*p_image)) {
6786 return OPJ_FALSE;
6787 }
6788
6789 /* Copy codestream image information to the output image */
6790 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6791
6792 /*Allocate and initialize some elements of codestrem index*/
6793 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6794 return OPJ_FALSE;
6795 }
6796
6797 return OPJ_TRUE;
6798 }
6799
opj_j2k_setup_header_reading(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)6800 static OPJ_BOOL opj_j2k_setup_header_reading (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
6801 {
6802 /* preconditions*/
6803 assert(p_j2k != 00);
6804 assert(p_manager != 00);
6805
6806 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure, p_manager)) {
6807 return OPJ_FALSE;
6808 }
6809
6810 /* DEVELOPER CORNER, add your custom procedures */
6811 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) {
6812 return OPJ_FALSE;
6813 }
6814
6815 return OPJ_TRUE;
6816 }
6817
opj_j2k_setup_decoding_validation(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)6818 static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
6819 {
6820 /* preconditions*/
6821 assert(p_j2k != 00);
6822 assert(p_manager != 00);
6823
6824 if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,(opj_procedure)opj_j2k_build_decoder, p_manager)) {
6825 return OPJ_FALSE;
6826 }
6827 if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,(opj_procedure)opj_j2k_decoding_validation, p_manager)) {
6828 return OPJ_FALSE;
6829 }
6830
6831 /* DEVELOPER CORNER, add your custom validation procedure */
6832 return OPJ_TRUE;
6833 }
6834
opj_j2k_mct_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)6835 static OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k,
6836 opj_stream_private_t *p_stream,
6837 opj_event_mgr_t * p_manager )
6838 {
6839 OPJ_BOOL l_is_valid = OPJ_TRUE;
6840 OPJ_UINT32 i,j;
6841
6842 /* preconditions */
6843 assert(p_j2k != 00);
6844 assert(p_stream != 00);
6845 assert(p_manager != 00);
6846
6847 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6848 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6849 opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
6850
6851 for (i=0;i<l_nb_tiles;++i) {
6852 if (l_tcp->mct == 2) {
6853 opj_tccp_t * l_tccp = l_tcp->tccps;
6854 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6855
6856 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
6857 l_is_valid &= ! (l_tccp->qmfbid & 1);
6858 ++l_tccp;
6859 }
6860 }
6861 ++l_tcp;
6862 }
6863 }
6864
6865 return l_is_valid;
6866 }
6867
opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp,opj_image_t * p_image)6868 OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
6869 {
6870 OPJ_UINT32 i;
6871 OPJ_UINT32 l_indix = 1;
6872 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
6873 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
6874 OPJ_UINT32 l_mct_size,l_nb_elem;
6875 OPJ_FLOAT32 * l_data, * l_current_data;
6876 opj_tccp_t * l_tccp;
6877
6878 /* preconditions */
6879 assert(p_tcp != 00);
6880
6881 if (p_tcp->mct != 2) {
6882 return OPJ_TRUE;
6883 }
6884
6885 if (p_tcp->m_mct_decoding_matrix) {
6886 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6887 opj_mct_data_t *new_mct_records;
6888 p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6889
6890 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));
6891 if (! new_mct_records) {
6892 opj_free(p_tcp->m_mct_records);
6893 p_tcp->m_mct_records = NULL;
6894 p_tcp->m_nb_max_mct_records = 0;
6895 p_tcp->m_nb_mct_records = 0;
6896 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6897 return OPJ_FALSE;
6898 }
6899 p_tcp->m_mct_records = new_mct_records;
6900 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6901
6902 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6903 }
6904 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6905
6906 if (l_mct_deco_data->m_data) {
6907 opj_free(l_mct_deco_data->m_data);
6908 l_mct_deco_data->m_data = 00;
6909 }
6910
6911 l_mct_deco_data->m_index = l_indix++;
6912 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
6913 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
6914 l_nb_elem = p_image->numcomps * p_image->numcomps;
6915 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
6916 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6917
6918 if (! l_mct_deco_data->m_data) {
6919 return OPJ_FALSE;
6920 }
6921
6922 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);
6923
6924 l_mct_deco_data->m_data_size = l_mct_size;
6925 ++p_tcp->m_nb_mct_records;
6926 }
6927
6928 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6929 opj_mct_data_t *new_mct_records;
6930 p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6931 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));
6932 if (! new_mct_records) {
6933 opj_free(p_tcp->m_mct_records);
6934 p_tcp->m_mct_records = NULL;
6935 p_tcp->m_nb_max_mct_records = 0;
6936 p_tcp->m_nb_mct_records = 0;
6937 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6938 return OPJ_FALSE;
6939 }
6940 p_tcp->m_mct_records = new_mct_records;
6941 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6942
6943 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6944
6945 if (l_mct_deco_data) {
6946 l_mct_deco_data = l_mct_offset_data - 1;
6947 }
6948 }
6949
6950 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6951
6952 if (l_mct_offset_data->m_data) {
6953 opj_free(l_mct_offset_data->m_data);
6954 l_mct_offset_data->m_data = 00;
6955 }
6956
6957 l_mct_offset_data->m_index = l_indix++;
6958 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
6959 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
6960 l_nb_elem = p_image->numcomps;
6961 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
6962 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6963
6964 if (! l_mct_offset_data->m_data) {
6965 return OPJ_FALSE;
6966 }
6967
6968 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
6969 if (! l_data) {
6970 opj_free(l_mct_offset_data->m_data);
6971 l_mct_offset_data->m_data = 00;
6972 return OPJ_FALSE;
6973 }
6974
6975 l_tccp = p_tcp->tccps;
6976 l_current_data = l_data;
6977
6978 for (i=0;i<l_nb_elem;++i) {
6979 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
6980 ++l_tccp;
6981 }
6982
6983 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
6984
6985 opj_free(l_data);
6986
6987 l_mct_offset_data->m_data_size = l_mct_size;
6988
6989 ++p_tcp->m_nb_mct_records;
6990
6991 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
6992 opj_simple_mcc_decorrelation_data_t *new_mcc_records;
6993 p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6994 new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
6995 p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6996 if (! new_mcc_records) {
6997 opj_free(p_tcp->m_mcc_records);
6998 p_tcp->m_mcc_records = NULL;
6999 p_tcp->m_nb_max_mcc_records = 0;
7000 p_tcp->m_nb_mcc_records = 0;
7001 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
7002 return OPJ_FALSE;
7003 }
7004 p_tcp->m_mcc_records = new_mcc_records;
7005 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7006 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));
7007
7008 }
7009
7010 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7011 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7012 l_mcc_data->m_is_irreversible = 1;
7013 l_mcc_data->m_nb_comps = p_image->numcomps;
7014 l_mcc_data->m_index = l_indix++;
7015 l_mcc_data->m_offset_array = l_mct_offset_data;
7016 ++p_tcp->m_nb_mcc_records;
7017
7018 return OPJ_TRUE;
7019 }
7020
opj_j2k_build_decoder(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7021 static OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k,
7022 opj_stream_private_t *p_stream,
7023 opj_event_mgr_t * p_manager )
7024 {
7025 /* add here initialization of cp
7026 copy paste of setup_decoder */
7027 (void)p_j2k;
7028 (void)p_stream;
7029 (void)p_manager;
7030 return OPJ_TRUE;
7031 }
7032
opj_j2k_build_encoder(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7033 static OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k,
7034 opj_stream_private_t *p_stream,
7035 opj_event_mgr_t * p_manager )
7036 {
7037 /* add here initialization of cp
7038 copy paste of setup_encoder */
7039 (void)p_j2k;
7040 (void)p_stream;
7041 (void)p_manager;
7042 return OPJ_TRUE;
7043 }
7044
opj_j2k_encoding_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7045 static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k,
7046 opj_stream_private_t *p_stream,
7047 opj_event_mgr_t * p_manager )
7048 {
7049 OPJ_BOOL l_is_valid = OPJ_TRUE;
7050
7051 /* preconditions */
7052 assert(p_j2k != 00);
7053 assert(p_stream != 00);
7054 assert(p_manager != 00);
7055
7056 /* STATE checking */
7057 /* make sure the state is at 0 */
7058 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7059
7060 /* POINTER validation */
7061 /* make sure a p_j2k codec is present */
7062 l_is_valid &= (p_j2k->m_procedure_list != 00);
7063 /* make sure a validation list is present */
7064 l_is_valid &= (p_j2k->m_validation_list != 00);
7065
7066 /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */
7067 /* 33 (32) would always fail the check below (if a cast to 64bits was done) */
7068 /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */
7069 if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) {
7070 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7071 return OPJ_FALSE;
7072 }
7073
7074 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
7075 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7076 return OPJ_FALSE;
7077 }
7078
7079 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
7080 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7081 return OPJ_FALSE;
7082 }
7083
7084 /* PARAMETER VALIDATION */
7085 return l_is_valid;
7086 }
7087
opj_j2k_decoding_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7088 static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t *p_j2k,
7089 opj_stream_private_t *p_stream,
7090 opj_event_mgr_t * p_manager
7091 )
7092 {
7093 OPJ_BOOL l_is_valid = OPJ_TRUE;
7094
7095 /* preconditions*/
7096 assert(p_j2k != 00);
7097 assert(p_stream != 00);
7098 assert(p_manager != 00);
7099
7100 /* STATE checking */
7101 /* make sure the state is at 0 */
7102 #ifdef TODO_MSD
7103 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7104 #endif
7105 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7106
7107 /* POINTER validation */
7108 /* make sure a p_j2k codec is present */
7109 /* make sure a procedure list is present */
7110 l_is_valid &= (p_j2k->m_procedure_list != 00);
7111 /* make sure a validation list is present */
7112 l_is_valid &= (p_j2k->m_validation_list != 00);
7113
7114 /* PARAMETER VALIDATION */
7115 return l_is_valid;
7116 }
7117
opj_j2k_read_header_procedure(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7118 static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
7119 opj_stream_private_t *p_stream,
7120 opj_event_mgr_t * p_manager)
7121 {
7122 OPJ_UINT32 l_current_marker;
7123 OPJ_UINT32 l_marker_size;
7124 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7125 OPJ_BOOL l_has_siz = 0;
7126 OPJ_BOOL l_has_cod = 0;
7127 OPJ_BOOL l_has_qcd = 0;
7128
7129 /* preconditions */
7130 assert(p_stream != 00);
7131 assert(p_j2k != 00);
7132 assert(p_manager != 00);
7133
7134 /* We enter in the main header */
7135 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7136
7137 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7138 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7139 opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7140 return OPJ_FALSE;
7141 }
7142
7143 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7144 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7145 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7146 return OPJ_FALSE;
7147 }
7148
7149 /* Read 2 bytes as the new marker ID */
7150 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7151
7152 /* Try to read until the SOT is detected */
7153 while (l_current_marker != J2K_MS_SOT) {
7154
7155 /* Check if the current marker ID is valid */
7156 if (l_current_marker < 0xff00) {
7157 opj_event_msg(p_manager, EVT_ERROR, "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker);
7158 return OPJ_FALSE;
7159 }
7160
7161 /* Get the marker handler from the marker ID */
7162 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7163
7164 /* Manage case where marker is unknown */
7165 if (l_marker_handler->id == J2K_MS_UNK) {
7166 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7167 opj_event_msg(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7168 return OPJ_FALSE;
7169 }
7170
7171 if (l_current_marker == J2K_MS_SOT)
7172 break; /* SOT marker is detected main header is completely read */
7173 else /* Get the marker handler from the marker ID */
7174 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7175 }
7176
7177 if (l_marker_handler->id == J2K_MS_SIZ) {
7178 /* Mark required SIZ marker as found */
7179 l_has_siz = 1;
7180 }
7181 if (l_marker_handler->id == J2K_MS_COD) {
7182 /* Mark required COD marker as found */
7183 l_has_cod = 1;
7184 }
7185 if (l_marker_handler->id == J2K_MS_QCD) {
7186 /* Mark required QCD marker as found */
7187 l_has_qcd = 1;
7188 }
7189
7190 /* Check if the marker is known and if it is the right place to find it */
7191 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7192 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7193 return OPJ_FALSE;
7194 }
7195
7196 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7197 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7198 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7199 return OPJ_FALSE;
7200 }
7201
7202 /* read 2 bytes as the marker size */
7203 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7204 if (l_marker_size < 2) {
7205 opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n");
7206 return OPJ_FALSE;
7207 }
7208 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7209
7210 /* Check if the marker size is compatible with the header data size */
7211 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7212 OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
7213 if (! new_header_data) {
7214 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7215 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
7216 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7217 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
7218 return OPJ_FALSE;
7219 }
7220 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
7221 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7222 }
7223
7224 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7225 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) {
7226 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7227 return OPJ_FALSE;
7228 }
7229
7230 /* Read the marker segment with the correct marker handler */
7231 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7232 opj_event_msg(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7233 return OPJ_FALSE;
7234 }
7235
7236 /* Add the marker to the codestream index*/
7237 if (OPJ_FALSE == opj_j2k_add_mhmarker(
7238 p_j2k->cstr_index,
7239 l_marker_handler->id,
7240 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7241 l_marker_size + 4 )) {
7242 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
7243 return OPJ_FALSE;
7244 }
7245
7246 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7247 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7248 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7249 return OPJ_FALSE;
7250 }
7251
7252 /* read 2 bytes as the new marker ID */
7253 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7254 }
7255
7256 if (l_has_siz == 0) {
7257 opj_event_msg(p_manager, EVT_ERROR, "required SIZ marker not found in main header\n");
7258 return OPJ_FALSE;
7259 }
7260 if (l_has_cod == 0) {
7261 opj_event_msg(p_manager, EVT_ERROR, "required COD marker not found in main header\n");
7262 return OPJ_FALSE;
7263 }
7264 if (l_has_qcd == 0) {
7265 opj_event_msg(p_manager, EVT_ERROR, "required QCD marker not found in main header\n");
7266 return OPJ_FALSE;
7267 }
7268
7269 if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) {
7270 opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n");
7271 return OPJ_FALSE;
7272 }
7273
7274 opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7275
7276 /* Position of the last element if the main header */
7277 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7278
7279 /* Next step: read a tile-part header */
7280 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7281
7282 return OPJ_TRUE;
7283 }
7284
opj_j2k_exec(opj_j2k_t * p_j2k,opj_procedure_list_t * p_procedure_list,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7285 static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
7286 opj_procedure_list_t * p_procedure_list,
7287 opj_stream_private_t *p_stream,
7288 opj_event_mgr_t * p_manager )
7289 {
7290 OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7291 OPJ_BOOL l_result = OPJ_TRUE;
7292 OPJ_UINT32 l_nb_proc, i;
7293
7294 /* preconditions*/
7295 assert(p_procedure_list != 00);
7296 assert(p_j2k != 00);
7297 assert(p_stream != 00);
7298 assert(p_manager != 00);
7299
7300 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7301 l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
7302
7303 for (i=0;i<l_nb_proc;++i) {
7304 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7305 ++l_procedure;
7306 }
7307
7308 /* and clear the procedure list at the end.*/
7309 opj_procedure_list_clear(p_procedure_list);
7310 return l_result;
7311 }
7312
7313 /* FIXME DOC*/
opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7314 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k,
7315 opj_stream_private_t *p_stream,
7316 opj_event_mgr_t * p_manager
7317 )
7318 {
7319 opj_tcp_t * l_tcp = 00;
7320 opj_tcp_t * l_default_tcp = 00;
7321 OPJ_UINT32 l_nb_tiles;
7322 OPJ_UINT32 i,j;
7323 opj_tccp_t *l_current_tccp = 00;
7324 OPJ_UINT32 l_tccp_size;
7325 OPJ_UINT32 l_mct_size;
7326 opj_image_t * l_image;
7327 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7328 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7329 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7330 OPJ_UINT32 l_offset;
7331
7332 /* preconditions */
7333 assert(p_j2k != 00);
7334 assert(p_stream != 00);
7335 assert(p_manager != 00);
7336
7337 l_image = p_j2k->m_private_image;
7338 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7339 l_tcp = p_j2k->m_cp.tcps;
7340 l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
7341 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7342 l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
7343
7344 /* For each tile */
7345 for (i=0; i<l_nb_tiles; ++i) {
7346 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7347 l_current_tccp = l_tcp->tccps;
7348 /*Copy default coding parameters into the current tile coding parameters*/
7349 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
7350 /* Initialize some values of the current tile coding parameters*/
7351 l_tcp->cod = 0;
7352 l_tcp->ppt = 0;
7353 l_tcp->ppt_data = 00;
7354 /* Remove memory not owned by this tile in case of early error return. */
7355 l_tcp->m_mct_decoding_matrix = 00;
7356 l_tcp->m_nb_max_mct_records = 0;
7357 l_tcp->m_mct_records = 00;
7358 l_tcp->m_nb_max_mcc_records = 0;
7359 l_tcp->m_mcc_records = 00;
7360 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7361 l_tcp->tccps = l_current_tccp;
7362
7363 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7364 if (l_default_tcp->m_mct_decoding_matrix) {
7365 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7366 if (! l_tcp->m_mct_decoding_matrix ) {
7367 return OPJ_FALSE;
7368 }
7369 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7370 }
7371
7372 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7373 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(opj_mct_data_t);
7374 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7375 if (! l_tcp->m_mct_records) {
7376 return OPJ_FALSE;
7377 }
7378 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7379
7380 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7381 l_src_mct_rec = l_default_tcp->m_mct_records;
7382 l_dest_mct_rec = l_tcp->m_mct_records;
7383
7384 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7385
7386 if (l_src_mct_rec->m_data) {
7387
7388 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7389 if(! l_dest_mct_rec->m_data) {
7390 return OPJ_FALSE;
7391 }
7392 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7393 }
7394
7395 ++l_src_mct_rec;
7396 ++l_dest_mct_rec;
7397 /* Update with each pass to free exactly what has been allocated on early return. */
7398 l_tcp->m_nb_max_mct_records += 1;
7399 }
7400
7401 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7402 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t);
7403 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7404 if (! l_tcp->m_mcc_records) {
7405 return OPJ_FALSE;
7406 }
7407 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7408 l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records;
7409
7410 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7411 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7412 l_dest_mcc_rec = l_tcp->m_mcc_records;
7413
7414 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7415
7416 if (l_src_mcc_rec->m_decorrelation_array) {
7417 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records);
7418 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7419 }
7420
7421 if (l_src_mcc_rec->m_offset_array) {
7422 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records);
7423 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7424 }
7425
7426 ++l_src_mcc_rec;
7427 ++l_dest_mcc_rec;
7428 }
7429
7430 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7431 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7432
7433 /* Move to next tile cp*/
7434 ++l_tcp;
7435 }
7436
7437 /* Create the current tile decoder*/
7438 p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */
7439 if (! p_j2k->m_tcd ) {
7440 return OPJ_FALSE;
7441 }
7442
7443 if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7444 opj_tcd_destroy(p_j2k->m_tcd);
7445 p_j2k->m_tcd = 00;
7446 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7447 return OPJ_FALSE;
7448 }
7449
7450 return OPJ_TRUE;
7451 }
7452
opj_j2k_get_marker_handler(OPJ_UINT32 p_id)7453 static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7454 {
7455 const opj_dec_memory_marker_handler_t *e;
7456 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7457 if (e->id == p_id) {
7458 break; /* we find a handler corresponding to the marker ID*/
7459 }
7460 }
7461 return e;
7462 }
7463
opj_j2k_destroy(opj_j2k_t * p_j2k)7464 void opj_j2k_destroy (opj_j2k_t *p_j2k)
7465 {
7466 if (p_j2k == 00) {
7467 return;
7468 }
7469
7470 if (p_j2k->m_is_decoder) {
7471
7472 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7473 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7474 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7475 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7476 }
7477
7478 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7479 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7480 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7481 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7482 }
7483 }
7484 else {
7485
7486 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7487 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7488 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7489 }
7490
7491 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7492 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7493 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7494 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7495 }
7496
7497 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7498 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7499 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7500 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7501 }
7502 }
7503
7504 opj_tcd_destroy(p_j2k->m_tcd);
7505
7506 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7507 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
7508
7509 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7510 p_j2k->m_procedure_list = 00;
7511
7512 opj_procedure_list_destroy(p_j2k->m_validation_list);
7513 p_j2k->m_procedure_list = 00;
7514
7515 j2k_destroy_cstr_index(p_j2k->cstr_index);
7516 p_j2k->cstr_index = NULL;
7517
7518 opj_image_destroy(p_j2k->m_private_image);
7519 p_j2k->m_private_image = NULL;
7520
7521 opj_image_destroy(p_j2k->m_output_image);
7522 p_j2k->m_output_image = NULL;
7523
7524 opj_free(p_j2k);
7525 }
7526
j2k_destroy_cstr_index(opj_codestream_index_t * p_cstr_ind)7527 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7528 {
7529 if (p_cstr_ind) {
7530
7531 if (p_cstr_ind->marker) {
7532 opj_free(p_cstr_ind->marker);
7533 p_cstr_ind->marker = NULL;
7534 }
7535
7536 if (p_cstr_ind->tile_index) {
7537 OPJ_UINT32 it_tile = 0;
7538
7539 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7540
7541 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7542 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7543 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7544 }
7545
7546 if(p_cstr_ind->tile_index[it_tile].tp_index){
7547 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7548 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7549 }
7550
7551 if(p_cstr_ind->tile_index[it_tile].marker){
7552 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7553 p_cstr_ind->tile_index[it_tile].marker = NULL;
7554
7555 }
7556 }
7557
7558 opj_free( p_cstr_ind->tile_index);
7559 p_cstr_ind->tile_index = NULL;
7560 }
7561
7562 opj_free(p_cstr_ind);
7563 }
7564 }
7565
opj_j2k_tcp_destroy(opj_tcp_t * p_tcp)7566 static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp)
7567 {
7568 if (p_tcp == 00) {
7569 return;
7570 }
7571
7572 if (p_tcp->ppt_markers != 00) {
7573 OPJ_UINT32 i;
7574 for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
7575 if (p_tcp->ppt_markers[i].m_data != NULL) {
7576 opj_free(p_tcp->ppt_markers[i].m_data);
7577 }
7578 }
7579 p_tcp->ppt_markers_count = 0U;
7580 opj_free(p_tcp->ppt_markers);
7581 p_tcp->ppt_markers = NULL;
7582 }
7583
7584 if (p_tcp->ppt_buffer != 00) {
7585 opj_free(p_tcp->ppt_buffer);
7586 p_tcp->ppt_buffer = 00;
7587 }
7588
7589 if (p_tcp->tccps != 00) {
7590 opj_free(p_tcp->tccps);
7591 p_tcp->tccps = 00;
7592 }
7593
7594 if (p_tcp->m_mct_coding_matrix != 00) {
7595 opj_free(p_tcp->m_mct_coding_matrix);
7596 p_tcp->m_mct_coding_matrix = 00;
7597 }
7598
7599 if (p_tcp->m_mct_decoding_matrix != 00) {
7600 opj_free(p_tcp->m_mct_decoding_matrix);
7601 p_tcp->m_mct_decoding_matrix = 00;
7602 }
7603
7604 if (p_tcp->m_mcc_records) {
7605 opj_free(p_tcp->m_mcc_records);
7606 p_tcp->m_mcc_records = 00;
7607 p_tcp->m_nb_max_mcc_records = 0;
7608 p_tcp->m_nb_mcc_records = 0;
7609 }
7610
7611 if (p_tcp->m_mct_records) {
7612 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7613 OPJ_UINT32 i;
7614
7615 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7616 if (l_mct_data->m_data) {
7617 opj_free(l_mct_data->m_data);
7618 l_mct_data->m_data = 00;
7619 }
7620
7621 ++l_mct_data;
7622 }
7623
7624 opj_free(p_tcp->m_mct_records);
7625 p_tcp->m_mct_records = 00;
7626 }
7627
7628 if (p_tcp->mct_norms != 00) {
7629 opj_free(p_tcp->mct_norms);
7630 p_tcp->mct_norms = 00;
7631 }
7632
7633 opj_j2k_tcp_data_destroy(p_tcp);
7634
7635 }
7636
opj_j2k_tcp_data_destroy(opj_tcp_t * p_tcp)7637 static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp)
7638 {
7639 if (p_tcp->m_data) {
7640 opj_free(p_tcp->m_data);
7641 p_tcp->m_data = NULL;
7642 p_tcp->m_data_size = 0;
7643 }
7644 }
7645
opj_j2k_cp_destroy(opj_cp_t * p_cp)7646 static void opj_j2k_cp_destroy (opj_cp_t *p_cp)
7647 {
7648 OPJ_UINT32 l_nb_tiles;
7649 opj_tcp_t * l_current_tile = 00;
7650
7651 if (p_cp == 00)
7652 {
7653 return;
7654 }
7655 if (p_cp->tcps != 00)
7656 {
7657 OPJ_UINT32 i;
7658 l_current_tile = p_cp->tcps;
7659 l_nb_tiles = p_cp->th * p_cp->tw;
7660
7661 for (i = 0U; i < l_nb_tiles; ++i)
7662 {
7663 opj_j2k_tcp_destroy(l_current_tile);
7664 ++l_current_tile;
7665 }
7666 opj_free(p_cp->tcps);
7667 p_cp->tcps = 00;
7668 }
7669 if (p_cp->ppm_markers != 00) {
7670 OPJ_UINT32 i;
7671 for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
7672 if (p_cp->ppm_markers[i].m_data != NULL) {
7673 opj_free(p_cp->ppm_markers[i].m_data);
7674 }
7675 }
7676 p_cp->ppm_markers_count = 0U;
7677 opj_free(p_cp->ppm_markers);
7678 p_cp->ppm_markers = NULL;
7679 }
7680 opj_free(p_cp->ppm_buffer);
7681 p_cp->ppm_buffer = 00;
7682 p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
7683 opj_free(p_cp->comment);
7684 p_cp->comment = 00;
7685 if (! p_cp->m_is_decoder)
7686 {
7687 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7688 p_cp->m_specific_param.m_enc.m_matrice = 00;
7689 }
7690 }
7691
opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t * p_stream,OPJ_UINT32 tile_no,OPJ_BOOL * p_correction_needed,opj_event_mgr_t * p_manager)7692 static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, opj_event_mgr_t * p_manager )
7693 {
7694 OPJ_BYTE l_header_data[10];
7695 OPJ_OFF_T l_stream_pos_backup;
7696 OPJ_UINT32 l_current_marker;
7697 OPJ_UINT32 l_marker_size;
7698 OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts;
7699
7700 /* initialize to no correction needed */
7701 *p_correction_needed = OPJ_FALSE;
7702
7703 l_stream_pos_backup = opj_stream_tell(p_stream);
7704 if (l_stream_pos_backup == -1) {
7705 /* let's do nothing */
7706 return OPJ_TRUE;
7707 }
7708
7709 for (;;) {
7710 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7711 if (opj_stream_read_data(p_stream,l_header_data, 2, p_manager) != 2) {
7712 /* assume all is OK */
7713 if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
7714 return OPJ_FALSE;
7715 }
7716 return OPJ_TRUE;
7717 }
7718
7719 /* Read 2 bytes from buffer as the new marker ID */
7720 opj_read_bytes(l_header_data, &l_current_marker, 2);
7721
7722 if (l_current_marker != J2K_MS_SOT) {
7723 /* assume all is OK */
7724 if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
7725 return OPJ_FALSE;
7726 }
7727 return OPJ_TRUE;
7728 }
7729
7730 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7731 if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
7732 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7733 return OPJ_FALSE;
7734 }
7735
7736 /* Read 2 bytes from the buffer as the marker size */
7737 opj_read_bytes(l_header_data, &l_marker_size, 2);
7738
7739 /* Check marker size for SOT Marker */
7740 if (l_marker_size != 10) {
7741 opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
7742 return OPJ_FALSE;
7743 }
7744 l_marker_size -= 2;
7745
7746 if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, p_manager) != l_marker_size) {
7747 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7748 return OPJ_FALSE;
7749 }
7750
7751 if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, &l_tot_len, &l_current_part, &l_num_parts, p_manager)) {
7752 return OPJ_FALSE;
7753 }
7754
7755 if (l_tile_no == tile_no) {
7756 /* we found what we were looking for */
7757 break;
7758 }
7759
7760 if ((l_tot_len == 0U) || (l_tot_len < 14U)) {
7761 /* last SOT until EOC or invalid Psot value */
7762 /* assume all is OK */
7763 if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
7764 return OPJ_FALSE;
7765 }
7766 return OPJ_TRUE;
7767 }
7768 l_tot_len -= 12U;
7769 /* look for next SOT marker */
7770 if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), p_manager) != (OPJ_OFF_T)(l_tot_len)) {
7771 /* assume all is OK */
7772 if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
7773 return OPJ_FALSE;
7774 }
7775 return OPJ_TRUE;
7776 }
7777 }
7778
7779 /* check for correction */
7780 if (l_current_part == l_num_parts) {
7781 *p_correction_needed = OPJ_TRUE;
7782 }
7783
7784 if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
7785 return OPJ_FALSE;
7786 }
7787 return OPJ_TRUE;
7788 }
7789
opj_j2k_read_tile_header(opj_j2k_t * p_j2k,OPJ_UINT32 * p_tile_index,OPJ_UINT32 * p_data_size,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_INT32 * p_tile_x1,OPJ_INT32 * p_tile_y1,OPJ_UINT32 * p_nb_comps,OPJ_BOOL * p_go_on,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7790 OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k,
7791 OPJ_UINT32 * p_tile_index,
7792 OPJ_UINT32 * p_data_size,
7793 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7794 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7795 OPJ_UINT32 * p_nb_comps,
7796 OPJ_BOOL * p_go_on,
7797 opj_stream_private_t *p_stream,
7798 opj_event_mgr_t * p_manager )
7799 {
7800 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7801 OPJ_UINT32 l_marker_size;
7802 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7803 opj_tcp_t * l_tcp = NULL;
7804
7805 /* preconditions */
7806 assert(p_stream != 00);
7807 assert(p_j2k != 00);
7808 assert(p_manager != 00);
7809
7810 /* Reach the End Of Codestream ?*/
7811 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7812 l_current_marker = J2K_MS_EOC;
7813 }
7814 /* We need to encounter a SOT marker (a new tile-part header) */
7815 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7816 return OPJ_FALSE;
7817 }
7818
7819 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7820 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7821
7822 /* Try to read until the Start Of Data is detected */
7823 while (l_current_marker != J2K_MS_SOD) {
7824
7825 if(opj_stream_get_number_byte_left(p_stream) == 0)
7826 {
7827 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
7828 break;
7829 }
7830
7831 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7832 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7833 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7834 return OPJ_FALSE;
7835 }
7836
7837 /* Read 2 bytes from the buffer as the marker size */
7838 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7839
7840 /* Check marker size (does not include marker ID but includes marker size) */
7841 if (l_marker_size < 2) {
7842 opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
7843 return OPJ_FALSE;
7844 }
7845
7846 /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
7847 if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) {
7848 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
7849 break;
7850 }
7851
7852 /* Why this condition? FIXME */
7853 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7854 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7855 }
7856 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7857
7858 /* Get the marker handler from the marker ID */
7859 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7860
7861 /* Check if the marker is known and if it is the right place to find it */
7862 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7863 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7864 return OPJ_FALSE;
7865 }
7866 /* FIXME manage case of unknown marker as in the main header ? */
7867
7868 /* Check if the marker size is compatible with the header data size */
7869 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7870 OPJ_BYTE *new_header_data = NULL;
7871 /* If we are here, this means we consider this marker as known & we will read it */
7872 /* Check enough bytes left in stream before allocation */
7873 if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) {
7874 opj_event_msg(p_manager, EVT_ERROR, "Marker size inconsistent with stream length\n");
7875 return OPJ_FALSE;
7876 }
7877 new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
7878 if (! new_header_data) {
7879 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7880 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
7881 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7882 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
7883 return OPJ_FALSE;
7884 }
7885 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
7886 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7887 }
7888
7889 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7890 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) {
7891 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7892 return OPJ_FALSE;
7893 }
7894
7895 if (!l_marker_handler->handler) {
7896 /* See issue #175 */
7897 opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
7898 return OPJ_FALSE;
7899 }
7900 /* Read the marker segment with the correct marker handler */
7901 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7902 opj_event_msg(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7903 return OPJ_FALSE;
7904 }
7905
7906 /* Add the marker to the codestream index*/
7907 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
7908 p_j2k->cstr_index,
7909 l_marker_handler->id,
7910 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7911 l_marker_size + 4 )) {
7912 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
7913 return OPJ_FALSE;
7914 }
7915
7916 /* Keep the position of the last SOT marker read */
7917 if ( l_marker_handler->id == J2K_MS_SOT ) {
7918 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7919 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7920 {
7921 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7922 }
7923 }
7924
7925 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7926 /* Skip the rest of the tile part header*/
7927 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) {
7928 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7929 return OPJ_FALSE;
7930 }
7931 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7932 }
7933 else {
7934 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7935 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7936 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7937 return OPJ_FALSE;
7938 }
7939 /* Read 2 bytes from the buffer as the new marker ID */
7940 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7941 }
7942 }
7943 if(opj_stream_get_number_byte_left(p_stream) == 0
7944 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
7945 break;
7946
7947 /* If we didn't skip data before, we need to read the SOD marker*/
7948 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7949 /* Try to read the SOD marker and skip data ? FIXME */
7950 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7951 return OPJ_FALSE;
7952 }
7953 if (p_j2k->m_specific_param.m_decoder.m_can_decode && !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) {
7954 /* Issue 254 */
7955 OPJ_BOOL l_correction_needed;
7956
7957 p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
7958 if(!opj_j2k_need_nb_tile_parts_correction(p_stream, p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) {
7959 opj_event_msg(p_manager, EVT_ERROR, "opj_j2k_apply_nb_tile_parts_correction error\n");
7960 return OPJ_FALSE;
7961 }
7962 if (l_correction_needed) {
7963 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
7964 OPJ_UINT32 l_tile_no;
7965
7966 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7967 p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1;
7968 /* correct tiles */
7969 for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
7970 if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) {
7971 p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts+=1;
7972 }
7973 }
7974 opj_event_msg(p_manager, EVT_WARNING, "Non conformant codestream TPsot==TNsot.\n");
7975 }
7976 }
7977 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7978 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7979 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7980 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7981 return OPJ_FALSE;
7982 }
7983
7984 /* Read 2 bytes from buffer as the new marker ID */
7985 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7986 }
7987 }
7988 else {
7989 /* Indicate we will try to read a new tile-part header*/
7990 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7991 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7992 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7993
7994 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7995 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7996 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7997 return OPJ_FALSE;
7998 }
7999
8000 /* Read 2 bytes from buffer as the new marker ID */
8001 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8002 }
8003 }
8004
8005 /* Current marker is the EOC marker ?*/
8006 if (l_current_marker == J2K_MS_EOC) {
8007 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8008 p_j2k->m_current_tile_number = 0;
8009 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8010 }
8011 }
8012
8013 /* FIXME DOC ???*/
8014 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8015 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8016 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8017
8018 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8019 ++p_j2k->m_current_tile_number;
8020 ++l_tcp;
8021 }
8022
8023 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8024 *p_go_on = OPJ_FALSE;
8025 return OPJ_TRUE;
8026 }
8027 }
8028
8029 if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, p_manager)) {
8030 opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n");
8031 return OPJ_FALSE;
8032 }
8033 /*FIXME ???*/
8034 if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, p_manager)) {
8035 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8036 return OPJ_FALSE;
8037 }
8038
8039 opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8040 p_j2k->m_current_tile_number+1, (p_j2k->m_cp.th * p_j2k->m_cp.tw));
8041
8042 *p_tile_index = p_j2k->m_current_tile_number;
8043 *p_go_on = OPJ_TRUE;
8044 *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd);
8045 if (*p_data_size == (OPJ_UINT32)-1) {
8046 return OPJ_FALSE;
8047 }
8048
8049 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8050 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8051 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8052 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8053 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8054
8055 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8056
8057 return OPJ_TRUE;
8058 }
8059
opj_j2k_decode_tile(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)8060 OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k,
8061 OPJ_UINT32 p_tile_index,
8062 OPJ_BYTE * p_data,
8063 OPJ_UINT32 p_data_size,
8064 opj_stream_private_t *p_stream,
8065 opj_event_mgr_t * p_manager )
8066 {
8067 OPJ_UINT32 l_current_marker;
8068 OPJ_BYTE l_data [2];
8069 opj_tcp_t * l_tcp;
8070
8071 /* preconditions */
8072 assert(p_stream != 00);
8073 assert(p_j2k != 00);
8074 assert(p_manager != 00);
8075
8076 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8077 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8078 return OPJ_FALSE;
8079 }
8080
8081 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8082 if (! l_tcp->m_data) {
8083 opj_j2k_tcp_destroy(l_tcp);
8084 return OPJ_FALSE;
8085 }
8086
8087 if (! opj_tcd_decode_tile( p_j2k->m_tcd,
8088 l_tcp->m_data,
8089 l_tcp->m_data_size,
8090 p_tile_index,
8091 p_j2k->cstr_index, p_manager) ) {
8092 opj_j2k_tcp_destroy(l_tcp);
8093 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8094 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
8095 return OPJ_FALSE;
8096 }
8097
8098 if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8099 return OPJ_FALSE;
8100 }
8101
8102 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8103 * we destroy just the data which will be re-read in read_tile_header*/
8104 /*opj_j2k_tcp_destroy(l_tcp);
8105 p_j2k->m_tcd->tcp = 0;*/
8106 opj_j2k_tcp_data_destroy(l_tcp);
8107
8108 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8109 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/
8110
8111 if(opj_stream_get_number_byte_left(p_stream) == 0
8112 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){
8113 return OPJ_TRUE;
8114 }
8115
8116 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8117 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8118 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8119 return OPJ_FALSE;
8120 }
8121
8122 opj_read_bytes(l_data,&l_current_marker,2);
8123
8124 if (l_current_marker == J2K_MS_EOC) {
8125 p_j2k->m_current_tile_number = 0;
8126 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8127 }
8128 else if (l_current_marker != J2K_MS_SOT)
8129 {
8130 if(opj_stream_get_number_byte_left(p_stream) == 0) {
8131 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
8132 opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n");
8133 return OPJ_TRUE;
8134 }
8135 opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8136 return OPJ_FALSE;
8137 }
8138 }
8139
8140 return OPJ_TRUE;
8141 }
8142
opj_j2k_update_image_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_data,opj_image_t * p_output_image)8143 static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8144 {
8145 OPJ_UINT32 i,j,k = 0;
8146 OPJ_UINT32 l_width_src,l_height_src;
8147 OPJ_UINT32 l_width_dest,l_height_dest;
8148 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8149 OPJ_SIZE_T l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8150 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8151 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8152 OPJ_SIZE_T l_start_offset_dest, l_line_offset_dest;
8153
8154 opj_image_comp_t * l_img_comp_src = 00;
8155 opj_image_comp_t * l_img_comp_dest = 00;
8156
8157 opj_tcd_tilecomp_t * l_tilec = 00;
8158 opj_image_t * l_image_src = 00;
8159 OPJ_UINT32 l_size_comp, l_remaining;
8160 OPJ_INT32 * l_dest_ptr;
8161 opj_tcd_resolution_t* l_res= 00;
8162
8163 l_tilec = p_tcd->tcd_image->tiles->comps;
8164 l_image_src = p_tcd->image;
8165 l_img_comp_src = l_image_src->comps;
8166
8167 l_img_comp_dest = p_output_image->comps;
8168
8169 for (i=0; i<l_image_src->numcomps; i++) {
8170
8171 /* Allocate output component buffer if necessary */
8172 if (!l_img_comp_dest->data) {
8173 OPJ_UINT32 width = l_img_comp_dest->w;
8174 OPJ_UINT32 height = l_img_comp_dest->h;
8175 const OPJ_UINT32 MAX_SIZE = UINT32_MAX / sizeof(OPJ_INT32);
8176 if (height == 0 || width > MAX_SIZE / height) {
8177 return OPJ_FALSE;
8178 }
8179 l_img_comp_dest->data = (OPJ_INT32*)opj_calloc(width * height, sizeof(OPJ_INT32));
8180 if (!l_img_comp_dest->data) {
8181 return OPJ_FALSE;
8182 }
8183 }
8184
8185 /* Copy info from decoded comp image to output image */
8186 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8187
8188 /*-----*/
8189 /* Compute the precision of the output buffer */
8190 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8191 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8192 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8193
8194 if (l_remaining) {
8195 ++l_size_comp;
8196 }
8197
8198 if (l_size_comp == 3) {
8199 l_size_comp = 4;
8200 }
8201 /*-----*/
8202
8203 /* Current tile component size*/
8204 /*if (i == 0) {
8205 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8206 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8207 }*/
8208
8209 l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0);
8210 l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0);
8211
8212 /* Border of the current output component*/
8213 l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8214 l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8215 l_x1_dest = l_x0_dest + l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */
8216 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8217
8218 /*if (i == 0) {
8219 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8220 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8221 }*/
8222
8223 /*-----*/
8224 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8225 * of the input buffer (decoded tile component) which will be move
8226 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8227 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8228 * by this input area.
8229 * */
8230 assert( l_res->x0 >= 0);
8231 assert( l_res->x1 >= 0);
8232
8233 /* Prevent bad casting to unsigned values in the subsequent lines. */
8234 if ( l_res->x0 < 0 || l_res->x1 < 0 || l_res->y0 < 0 || l_res->y1 < 0 ) {
8235 return OPJ_FALSE;
8236 }
8237
8238 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8239 l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest;
8240 l_offset_x0_src = 0;
8241
8242 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8243 l_width_dest = l_width_src;
8244 l_offset_x1_src = 0;
8245 }
8246 else {
8247 l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ;
8248 l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
8249 }
8250 }
8251 else {
8252 l_start_x_dest = 0U;
8253 l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0;
8254
8255 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8256 l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
8257 l_offset_x1_src = 0;
8258 }
8259 else {
8260 l_width_dest = l_img_comp_dest->w ;
8261 l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest;
8262 }
8263 }
8264
8265 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8266 l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest;
8267 l_offset_y0_src = 0;
8268
8269 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8270 l_height_dest = l_height_src;
8271 l_offset_y1_src = 0;
8272 }
8273 else {
8274 l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ;
8275 l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
8276 }
8277 }
8278 else {
8279 l_start_y_dest = 0U;
8280 l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0;
8281
8282 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8283 l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
8284 l_offset_y1_src = 0;
8285 }
8286 else {
8287 l_height_dest = l_img_comp_dest->h ;
8288 l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest;
8289 }
8290 }
8291
8292 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8293 return OPJ_FALSE;
8294 }
8295 /* testcase 2977.pdf.asan.67.2198 */
8296 if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
8297 return OPJ_FALSE;
8298 }
8299 /*-----*/
8300
8301 /* Compute the input buffer offset */
8302 l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src * (OPJ_SIZE_T)l_width_src;
8303 l_line_offset_src = (OPJ_SIZE_T)l_offset_x1_src + (OPJ_SIZE_T)l_offset_x0_src;
8304 l_end_offset_src = (OPJ_SIZE_T)l_offset_y1_src * (OPJ_SIZE_T)l_width_src - (OPJ_SIZE_T)l_offset_x0_src;
8305
8306 /* Compute the output buffer offset */
8307 l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest * (OPJ_SIZE_T)l_img_comp_dest->w;
8308 l_line_offset_dest = (OPJ_SIZE_T)l_img_comp_dest->w - (OPJ_SIZE_T)l_width_dest;
8309
8310 /* Move the output buffer to the first place where we will write*/
8311 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8312
8313 /*if (i == 0) {
8314 fprintf(stdout, "COMPO[%d]:\n",i);
8315 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8316 "\t tile offset:%d, %d, %d, %d\n"
8317 "\t buffer offset: %d; %d, %d\n",
8318 l_res->x0, l_res->y0, l_width_src, l_height_src,
8319 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8320 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8321
8322 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8323 "\t start offset: %d, line offset= %d\n",
8324 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8325 }*/
8326
8327 switch (l_size_comp) {
8328 case 1:
8329 {
8330 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8331 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8332
8333 if (l_img_comp_src->sgnd) {
8334 for (j = 0 ; j < l_height_dest ; ++j) {
8335 for ( k = 0 ; k < l_width_dest ; ++k) {
8336 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8337 }
8338
8339 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8340 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8341 }
8342 }
8343 else {
8344 for ( j = 0 ; j < l_height_dest ; ++j ) {
8345 for ( k = 0 ; k < l_width_dest ; ++k) {
8346 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8347 }
8348
8349 l_dest_ptr+= l_line_offset_dest;
8350 l_src_ptr += l_line_offset_src;
8351 }
8352 }
8353
8354 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8355 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8356 }
8357 break;
8358 case 2:
8359 {
8360 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8361 l_src_ptr += l_start_offset_src;
8362
8363 if (l_img_comp_src->sgnd) {
8364 for (j=0;j<l_height_dest;++j) {
8365 for (k=0;k<l_width_dest;++k) {
8366 *(l_dest_ptr++) = *(l_src_ptr++);
8367 }
8368
8369 l_dest_ptr+= l_line_offset_dest;
8370 l_src_ptr += l_line_offset_src ;
8371 }
8372 }
8373 else {
8374 for (j=0;j<l_height_dest;++j) {
8375 for (k=0;k<l_width_dest;++k) {
8376 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8377 }
8378
8379 l_dest_ptr+= l_line_offset_dest;
8380 l_src_ptr += l_line_offset_src ;
8381 }
8382 }
8383
8384 l_src_ptr += l_end_offset_src;
8385 p_data = (OPJ_BYTE*) l_src_ptr;
8386 }
8387 break;
8388 case 4:
8389 {
8390 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8391 l_src_ptr += l_start_offset_src;
8392
8393 for (j=0;j<l_height_dest;++j) {
8394 for (k=0;k<l_width_dest;++k) {
8395 *(l_dest_ptr++) = (*(l_src_ptr++));
8396 }
8397
8398 l_dest_ptr+= l_line_offset_dest;
8399 l_src_ptr += l_line_offset_src ;
8400 }
8401
8402 l_src_ptr += l_end_offset_src;
8403 p_data = (OPJ_BYTE*) l_src_ptr;
8404 }
8405 break;
8406 }
8407
8408 ++l_img_comp_dest;
8409 ++l_img_comp_src;
8410 ++l_tilec;
8411 }
8412
8413 return OPJ_TRUE;
8414 }
8415
opj_j2k_set_decode_area(opj_j2k_t * p_j2k,opj_image_t * p_image,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y,opj_event_mgr_t * p_manager)8416 OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k,
8417 opj_image_t* p_image,
8418 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8419 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8420 opj_event_mgr_t * p_manager )
8421 {
8422 opj_cp_t * l_cp = &(p_j2k->m_cp);
8423 opj_image_t * l_image = p_j2k->m_private_image;
8424
8425 OPJ_UINT32 it_comp;
8426 OPJ_INT32 l_comp_x1, l_comp_y1;
8427 opj_image_comp_t* l_img_comp = NULL;
8428
8429 /* Check if we are read the main header */
8430 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8431 opj_event_msg(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8432 return OPJ_FALSE;
8433 }
8434
8435 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8436 opj_event_msg(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8437
8438 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8439 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8440 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8441 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8442
8443 return OPJ_TRUE;
8444 }
8445
8446 /* ----- */
8447 /* Check if the positions provided by the user are correct */
8448
8449 /* Left */
8450 assert(p_start_x >= 0 );
8451 assert(p_start_y >= 0 );
8452
8453 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8454 opj_event_msg(p_manager, EVT_ERROR,
8455 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8456 p_start_x, l_image->x1);
8457 return OPJ_FALSE;
8458 }
8459 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8460 opj_event_msg(p_manager, EVT_WARNING,
8461 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8462 p_start_x, l_image->x0);
8463 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8464 p_image->x0 = l_image->x0;
8465 }
8466 else {
8467 p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx;
8468 p_image->x0 = (OPJ_UINT32)p_start_x;
8469 }
8470
8471 /* Up */
8472 if ((OPJ_UINT32)p_start_y > l_image->y1){
8473 opj_event_msg(p_manager, EVT_ERROR,
8474 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8475 p_start_y, l_image->y1);
8476 return OPJ_FALSE;
8477 }
8478 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8479 opj_event_msg(p_manager, EVT_WARNING,
8480 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8481 p_start_y, l_image->y0);
8482 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8483 p_image->y0 = l_image->y0;
8484 }
8485 else {
8486 p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy;
8487 p_image->y0 = (OPJ_UINT32)p_start_y;
8488 }
8489
8490 /* Right */
8491 assert((OPJ_UINT32)p_end_x > 0);
8492 assert((OPJ_UINT32)p_end_y > 0);
8493 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8494 opj_event_msg(p_manager, EVT_ERROR,
8495 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8496 p_end_x, l_image->x0);
8497 return OPJ_FALSE;
8498 }
8499 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8500 opj_event_msg(p_manager, EVT_WARNING,
8501 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8502 p_end_x, l_image->x1);
8503 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8504 p_image->x1 = l_image->x1;
8505 }
8506 else {
8507 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);
8508 p_image->x1 = (OPJ_UINT32)p_end_x;
8509 }
8510
8511 /* Bottom */
8512 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8513 opj_event_msg(p_manager, EVT_ERROR,
8514 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8515 p_end_y, l_image->y0);
8516 return OPJ_FALSE;
8517 }
8518 if ((OPJ_UINT32)p_end_y > l_image->y1){
8519 opj_event_msg(p_manager, EVT_WARNING,
8520 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8521 p_end_y, l_image->y1);
8522 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8523 p_image->y1 = l_image->y1;
8524 }
8525 else{
8526 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);
8527 p_image->y1 = (OPJ_UINT32)p_end_y;
8528 }
8529 /* ----- */
8530
8531 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8532
8533 l_img_comp = p_image->comps;
8534 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8535 {
8536 OPJ_INT32 l_h,l_w;
8537
8538 l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
8539 l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
8540 l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
8541 l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
8542
8543 l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
8544 - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
8545 if (l_w < 0){
8546 opj_event_msg(p_manager, EVT_ERROR,
8547 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8548 it_comp, l_w);
8549 return OPJ_FALSE;
8550 }
8551 l_img_comp->w = (OPJ_UINT32)l_w;
8552
8553 l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
8554 - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
8555 if (l_h < 0){
8556 opj_event_msg(p_manager, EVT_ERROR,
8557 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8558 it_comp, l_h);
8559 return OPJ_FALSE;
8560 }
8561 l_img_comp->h = (OPJ_UINT32)l_h;
8562
8563 l_img_comp++;
8564 }
8565
8566 opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8567 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8568
8569 return OPJ_TRUE;
8570 }
8571
opj_j2k_create_decompress(void)8572 opj_j2k_t* opj_j2k_create_decompress(void)
8573 {
8574 opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t));
8575 if (!l_j2k) {
8576 return 00;
8577 }
8578
8579 l_j2k->m_is_decoder = 1;
8580 l_j2k->m_cp.m_is_decoder = 1;
8581
8582 #ifdef OPJ_DISABLE_TPSOT_FIX
8583 l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
8584 #endif
8585
8586 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,sizeof(opj_tcp_t));
8587 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8588 opj_j2k_destroy(l_j2k);
8589 return 00;
8590 }
8591
8592 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,OPJ_J2K_DEFAULT_HEADER_SIZE);
8593 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8594 opj_j2k_destroy(l_j2k);
8595 return 00;
8596 }
8597
8598 l_j2k->m_specific_param.m_decoder.m_header_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
8599
8600 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8601
8602 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8603
8604 /* codestream index creation */
8605 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8606 if (!l_j2k->cstr_index){
8607 opj_j2k_destroy(l_j2k);
8608 return 00;
8609 }
8610
8611 /* validation list creation */
8612 l_j2k->m_validation_list = opj_procedure_list_create();
8613 if (! l_j2k->m_validation_list) {
8614 opj_j2k_destroy(l_j2k);
8615 return 00;
8616 }
8617
8618 /* execution list creation */
8619 l_j2k->m_procedure_list = opj_procedure_list_create();
8620 if (! l_j2k->m_procedure_list) {
8621 opj_j2k_destroy(l_j2k);
8622 return 00;
8623 }
8624
8625 return l_j2k;
8626 }
8627
opj_j2k_create_cstr_index(void)8628 static opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8629 {
8630 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8631 opj_calloc(1,sizeof(opj_codestream_index_t));
8632 if (!cstr_index)
8633 return NULL;
8634
8635 cstr_index->maxmarknum = 100;
8636 cstr_index->marknum = 0;
8637 cstr_index->marker = (opj_marker_info_t*)
8638 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8639 if (!cstr_index-> marker) {
8640 opj_free(cstr_index);
8641 return NULL;
8642 }
8643
8644 cstr_index->tile_index = NULL;
8645
8646 return cstr_index;
8647 }
8648
opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no)8649 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_t *p_j2k,
8650 OPJ_UINT32 p_tile_no,
8651 OPJ_UINT32 p_comp_no )
8652 {
8653 opj_cp_t *l_cp = 00;
8654 opj_tcp_t *l_tcp = 00;
8655 opj_tccp_t *l_tccp = 00;
8656
8657 /* preconditions */
8658 assert(p_j2k != 00);
8659
8660 l_cp = &(p_j2k->m_cp);
8661 l_tcp = &l_cp->tcps[p_tile_no];
8662 l_tccp = &l_tcp->tccps[p_comp_no];
8663
8664 /* preconditions again */
8665 assert(p_tile_no < (l_cp->tw * l_cp->th));
8666 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8667
8668 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8669 return 5 + l_tccp->numresolutions;
8670 }
8671 else {
8672 return 5;
8673 }
8674 }
8675
opj_j2k_write_SPCod_SPCoc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_header_size,struct opj_event_mgr * p_manager)8676 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k,
8677 OPJ_UINT32 p_tile_no,
8678 OPJ_UINT32 p_comp_no,
8679 OPJ_BYTE * p_data,
8680 OPJ_UINT32 * p_header_size,
8681 struct opj_event_mgr * p_manager )
8682 {
8683 OPJ_UINT32 i;
8684 opj_cp_t *l_cp = 00;
8685 opj_tcp_t *l_tcp = 00;
8686 opj_tccp_t *l_tccp = 00;
8687
8688 /* preconditions */
8689 assert(p_j2k != 00);
8690 assert(p_header_size != 00);
8691 assert(p_manager != 00);
8692 assert(p_data != 00);
8693
8694 l_cp = &(p_j2k->m_cp);
8695 l_tcp = &l_cp->tcps[p_tile_no];
8696 l_tccp = &l_tcp->tccps[p_comp_no];
8697
8698 /* preconditions again */
8699 assert(p_tile_no < (l_cp->tw * l_cp->th));
8700 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8701
8702 if (*p_header_size < 5) {
8703 opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8704 return OPJ_FALSE;
8705 }
8706
8707 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8708 ++p_data;
8709
8710 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8711 ++p_data;
8712
8713 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8714 ++p_data;
8715
8716 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8717 ++p_data;
8718
8719 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8720 ++p_data;
8721
8722 *p_header_size = *p_header_size - 5;
8723
8724 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8725
8726 if (*p_header_size < l_tccp->numresolutions) {
8727 opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8728 return OPJ_FALSE;
8729 }
8730
8731 for (i = 0; i < l_tccp->numresolutions; ++i) {
8732 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8733 ++p_data;
8734 }
8735
8736 *p_header_size = *p_header_size - l_tccp->numresolutions;
8737 }
8738
8739 return OPJ_TRUE;
8740 }
8741
opj_j2k_read_SPCod_SPCoc(opj_j2k_t * p_j2k,OPJ_UINT32 compno,OPJ_BYTE * p_header_data,OPJ_UINT32 * p_header_size,opj_event_mgr_t * p_manager)8742 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k,
8743 OPJ_UINT32 compno,
8744 OPJ_BYTE * p_header_data,
8745 OPJ_UINT32 * p_header_size,
8746 opj_event_mgr_t * p_manager)
8747 {
8748 OPJ_UINT32 i, l_tmp;
8749 opj_cp_t *l_cp = NULL;
8750 opj_tcp_t *l_tcp = NULL;
8751 opj_tccp_t *l_tccp = NULL;
8752 OPJ_BYTE * l_current_ptr = NULL;
8753
8754 /* preconditions */
8755 assert(p_j2k != 00);
8756 assert(p_manager != 00);
8757 assert(p_header_data != 00);
8758
8759 l_cp = &(p_j2k->m_cp);
8760 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8761 &l_cp->tcps[p_j2k->m_current_tile_number] :
8762 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8763
8764 /* precondition again */
8765 if (compno >= p_j2k->m_private_image->numcomps) {
8766 return OPJ_FALSE;
8767 }
8768
8769 l_tccp = &l_tcp->tccps[compno];
8770 l_current_ptr = p_header_data;
8771
8772 /* make sure room is sufficient */
8773 if (*p_header_size < 5) {
8774 opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8775 return OPJ_FALSE;
8776 }
8777
8778 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8779 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8780 if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
8781 opj_event_msg(p_manager, EVT_ERROR,
8782 "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
8783 l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
8784 return OPJ_FALSE;
8785 }
8786 ++l_current_ptr;
8787
8788 /* If user wants to remove more resolutions than the codestream contains, return error */
8789 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8790 opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8791 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8792 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8793 return OPJ_FALSE;
8794 }
8795
8796 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8797 ++l_current_ptr;
8798 l_tccp->cblkw += 2;
8799
8800 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8801 ++l_current_ptr;
8802 l_tccp->cblkh += 2;
8803
8804 if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || ((l_tccp->cblkw + l_tccp->cblkh) > 12)) {
8805 opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n");
8806 return OPJ_FALSE;
8807 }
8808
8809
8810 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8811 ++l_current_ptr;
8812
8813 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8814 ++l_current_ptr;
8815
8816 *p_header_size = *p_header_size - 5;
8817
8818 /* use custom precinct size ? */
8819 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8820 if (*p_header_size < l_tccp->numresolutions) {
8821 opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8822 return OPJ_FALSE;
8823 }
8824
8825 for (i = 0; i < l_tccp->numresolutions; ++i) {
8826 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8827 ++l_current_ptr;
8828 /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */
8829 if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) {
8830 opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n");
8831 return OPJ_FALSE;
8832 }
8833 l_tccp->prcw[i] = l_tmp & 0xf;
8834 l_tccp->prch[i] = l_tmp >> 4;
8835 }
8836
8837 *p_header_size = *p_header_size - l_tccp->numresolutions;
8838 }
8839 else {
8840 /* set default size for the precinct width and height */
8841 for (i = 0; i < l_tccp->numresolutions; ++i) {
8842 l_tccp->prcw[i] = 15;
8843 l_tccp->prch[i] = 15;
8844 }
8845 }
8846
8847 #ifdef WIP_REMOVE_MSD
8848 /* INDEX >> */
8849 if (p_j2k->cstr_info && compno == 0) {
8850 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8851
8852 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8853 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8854 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8855 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8856 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8857
8858 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8859 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8860 }
8861 /* << INDEX */
8862 #endif
8863
8864 return OPJ_TRUE;
8865 }
8866
opj_j2k_copy_tile_component_parameters(opj_j2k_t * p_j2k)8867 static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k )
8868 {
8869 /* loop */
8870 OPJ_UINT32 i;
8871 opj_cp_t *l_cp = NULL;
8872 opj_tcp_t *l_tcp = NULL;
8873 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8874 OPJ_UINT32 l_prc_size;
8875
8876 /* preconditions */
8877 assert(p_j2k != 00);
8878
8879 l_cp = &(p_j2k->m_cp);
8880 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8881 &l_cp->tcps[p_j2k->m_current_tile_number] :
8882 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8883
8884 l_ref_tccp = &l_tcp->tccps[0];
8885 l_copied_tccp = l_ref_tccp + 1;
8886 l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
8887
8888 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8889 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8890 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8891 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8892 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8893 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8894 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8895 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8896 ++l_copied_tccp;
8897 }
8898 }
8899
opj_j2k_get_SQcd_SQcc_size(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no)8900 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
8901 OPJ_UINT32 p_tile_no,
8902 OPJ_UINT32 p_comp_no )
8903 {
8904 OPJ_UINT32 l_num_bands;
8905
8906 opj_cp_t *l_cp = 00;
8907 opj_tcp_t *l_tcp = 00;
8908 opj_tccp_t *l_tccp = 00;
8909
8910 /* preconditions */
8911 assert(p_j2k != 00);
8912
8913 l_cp = &(p_j2k->m_cp);
8914 l_tcp = &l_cp->tcps[p_tile_no];
8915 l_tccp = &l_tcp->tccps[p_comp_no];
8916
8917 /* preconditions again */
8918 assert(p_tile_no < l_cp->tw * l_cp->th);
8919 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8920
8921 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8922
8923 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8924 return 1 + l_num_bands;
8925 }
8926 else {
8927 return 1 + 2*l_num_bands;
8928 }
8929 }
8930
opj_j2k_write_SQcd_SQcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_header_size,struct opj_event_mgr * p_manager)8931 static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k,
8932 OPJ_UINT32 p_tile_no,
8933 OPJ_UINT32 p_comp_no,
8934 OPJ_BYTE * p_data,
8935 OPJ_UINT32 * p_header_size,
8936 struct opj_event_mgr * p_manager )
8937 {
8938 OPJ_UINT32 l_header_size;
8939 OPJ_UINT32 l_band_no, l_num_bands;
8940 OPJ_UINT32 l_expn,l_mant;
8941
8942 opj_cp_t *l_cp = 00;
8943 opj_tcp_t *l_tcp = 00;
8944 opj_tccp_t *l_tccp = 00;
8945
8946 /* preconditions */
8947 assert(p_j2k != 00);
8948 assert(p_header_size != 00);
8949 assert(p_manager != 00);
8950 assert(p_data != 00);
8951
8952 l_cp = &(p_j2k->m_cp);
8953 l_tcp = &l_cp->tcps[p_tile_no];
8954 l_tccp = &l_tcp->tccps[p_comp_no];
8955
8956 /* preconditions again */
8957 assert(p_tile_no < l_cp->tw * l_cp->th);
8958 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8959
8960 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8961
8962 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8963 l_header_size = 1 + l_num_bands;
8964
8965 if (*p_header_size < l_header_size) {
8966 opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8967 return OPJ_FALSE;
8968 }
8969
8970 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8971 ++p_data;
8972
8973 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8974 l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
8975 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8976 ++p_data;
8977 }
8978 }
8979 else {
8980 l_header_size = 1 + 2*l_num_bands;
8981
8982 if (*p_header_size < l_header_size) {
8983 opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8984 return OPJ_FALSE;
8985 }
8986
8987 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8988 ++p_data;
8989
8990 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8991 l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
8992 l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
8993
8994 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
8995 p_data += 2;
8996 }
8997 }
8998
8999 *p_header_size = *p_header_size - l_header_size;
9000
9001 return OPJ_TRUE;
9002 }
9003
opj_j2k_read_SQcd_SQcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_header_data,OPJ_UINT32 * p_header_size,opj_event_mgr_t * p_manager)9004 static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
9005 OPJ_UINT32 p_comp_no,
9006 OPJ_BYTE* p_header_data,
9007 OPJ_UINT32 * p_header_size,
9008 opj_event_mgr_t * p_manager
9009 )
9010 {
9011 /* loop*/
9012 OPJ_UINT32 l_band_no;
9013 opj_cp_t *l_cp = 00;
9014 opj_tcp_t *l_tcp = 00;
9015 opj_tccp_t *l_tccp = 00;
9016 OPJ_BYTE * l_current_ptr = 00;
9017 OPJ_UINT32 l_tmp, l_num_band;
9018
9019 /* preconditions*/
9020 assert(p_j2k != 00);
9021 assert(p_manager != 00);
9022 assert(p_header_data != 00);
9023
9024 l_cp = &(p_j2k->m_cp);
9025 /* come from tile part header or main header ?*/
9026 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9027 &l_cp->tcps[p_j2k->m_current_tile_number] :
9028 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9029
9030 /* precondition again*/
9031 if (p_comp_no >= p_j2k->m_private_image->numcomps) {
9032 return OPJ_FALSE;
9033 }
9034
9035 l_tccp = &l_tcp->tccps[p_comp_no];
9036 l_current_ptr = p_header_data;
9037
9038 if (*p_header_size < 1) {
9039 opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9040 return OPJ_FALSE;
9041 }
9042 *p_header_size -= 1;
9043
9044 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9045 ++l_current_ptr;
9046
9047 l_tccp->qntsty = l_tmp & 0x1f;
9048 l_tccp->numgbits = l_tmp >> 5;
9049 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9050 l_num_band = 1;
9051 }
9052 else {
9053 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9054 (*p_header_size) :
9055 (*p_header_size) / 2;
9056
9057 if( l_num_band > OPJ_J2K_MAXBANDS ) {
9058 opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9059 "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9060 "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS);
9061 /*return OPJ_FALSE;*/
9062 }
9063 }
9064
9065 #ifdef USE_JPWL
9066 if (l_cp->correct) {
9067
9068 /* if JPWL is on, we check whether there are too many subbands */
9069 if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
9070 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9071 "JPWL: bad number of subbands in Sqcx (%d)\n",
9072 l_num_band);
9073 if (!JPWL_ASSUME) {
9074 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
9075 return OPJ_FALSE;
9076 }
9077 /* we try to correct */
9078 l_num_band = 1;
9079 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
9080 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9081 l_num_band);
9082 };
9083
9084 };
9085 #endif /* USE_JPWL */
9086
9087 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9088 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9089 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9090 ++l_current_ptr;
9091 if (l_band_no < OPJ_J2K_MAXBANDS){
9092 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
9093 l_tccp->stepsizes[l_band_no].mant = 0;
9094 }
9095 }
9096 *p_header_size = *p_header_size - l_num_band;
9097 }
9098 else {
9099 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9100 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9101 l_current_ptr+=2;
9102 if (l_band_no < OPJ_J2K_MAXBANDS){
9103 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
9104 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9105 }
9106 }
9107 *p_header_size = *p_header_size - 2*l_num_band;
9108 }
9109
9110 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9111 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9112 for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
9113 l_tccp->stepsizes[l_band_no].expn =
9114 ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ?
9115 (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
9116 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9117 }
9118 }
9119
9120 return OPJ_TRUE;
9121 }
9122
opj_j2k_copy_tile_quantization_parameters(opj_j2k_t * p_j2k)9123 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k )
9124 {
9125 OPJ_UINT32 i;
9126 opj_cp_t *l_cp = NULL;
9127 opj_tcp_t *l_tcp = NULL;
9128 opj_tccp_t *l_ref_tccp = NULL;
9129 opj_tccp_t *l_copied_tccp = NULL;
9130 OPJ_UINT32 l_size;
9131
9132 /* preconditions */
9133 assert(p_j2k != 00);
9134
9135 l_cp = &(p_j2k->m_cp);
9136 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9137 &l_cp->tcps[p_j2k->m_current_tile_number] :
9138 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9139
9140 l_ref_tccp = &l_tcp->tccps[0];
9141 l_copied_tccp = l_ref_tccp + 1;
9142 l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
9143
9144 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9145 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9146 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9147 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9148 ++l_copied_tccp;
9149 }
9150 }
9151
opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE * out_stream)9152 static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream)
9153 {
9154 if (l_default_tile)
9155 {
9156 OPJ_INT32 compno;
9157
9158 fprintf(out_stream, "\t default tile {\n");
9159 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9160 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9161 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9162 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9163
9164 for (compno = 0; compno < numcomps; compno++) {
9165 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9166 OPJ_UINT32 resno;
9167 OPJ_INT32 bandno, numbands;
9168
9169 /* coding style*/
9170 fprintf(out_stream, "\t\t comp %d {\n", compno);
9171 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9172 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9173 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9174 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9175 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9176 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9177
9178 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9179 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9180 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9181 }
9182 fprintf(out_stream, "\n");
9183
9184 /* quantization style*/
9185 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9186 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9187 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9188 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
9189 for (bandno = 0; bandno < numbands; bandno++) {
9190 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9191 l_tccp->stepsizes[bandno].expn);
9192 }
9193 fprintf(out_stream, "\n");
9194
9195 /* RGN value*/
9196 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9197
9198 fprintf(out_stream, "\t\t }\n");
9199 } /*end of component of default tile*/
9200 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9201 }
9202 }
9203
j2k_dump(opj_j2k_t * p_j2k,OPJ_INT32 flag,FILE * out_stream)9204 void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9205 {
9206 /* Check if the flag is compatible with j2k file*/
9207 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9208 fprintf(out_stream, "Wrong flag\n");
9209 return;
9210 }
9211
9212 /* Dump the image_header */
9213 if (flag & OPJ_IMG_INFO){
9214 if (p_j2k->m_private_image)
9215 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9216 }
9217
9218 /* Dump the codestream info from main header */
9219 if (flag & OPJ_J2K_MH_INFO){
9220 opj_j2k_dump_MH_info(p_j2k, out_stream);
9221 }
9222 /* Dump all tile/codestream info */
9223 if (flag & OPJ_J2K_TCH_INFO){
9224 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9225 OPJ_UINT32 i;
9226 opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
9227 for (i=0;i<l_nb_tiles;++i) {
9228 opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
9229 ++l_tcp;
9230 }
9231 }
9232
9233 /* Dump the codestream info of the current tile */
9234 if (flag & OPJ_J2K_TH_INFO){
9235
9236 }
9237
9238 /* Dump the codestream index from main header */
9239 if (flag & OPJ_J2K_MH_IND){
9240 opj_j2k_dump_MH_index(p_j2k, out_stream);
9241 }
9242
9243 /* Dump the codestream index of the current tile */
9244 if (flag & OPJ_J2K_TH_IND){
9245
9246 }
9247
9248 }
9249
opj_j2k_dump_MH_index(opj_j2k_t * p_j2k,FILE * out_stream)9250 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
9251 {
9252 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9253 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9254
9255 fprintf(out_stream, "Codestream index from main header: {\n");
9256
9257 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9258 "\t Main header end position=%" PRIi64 "\n",
9259 cstr_index->main_head_start, cstr_index->main_head_end);
9260
9261 fprintf(out_stream, "\t Marker list: {\n");
9262
9263 if (cstr_index->marker){
9264 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9265 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9266 cstr_index->marker[it_marker].type,
9267 cstr_index->marker[it_marker].pos,
9268 cstr_index->marker[it_marker].len );
9269 }
9270 }
9271
9272 fprintf(out_stream, "\t }\n");
9273
9274 if (cstr_index->tile_index){
9275
9276 /* Simple test to avoid to write empty information*/
9277 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9278 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9279 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9280 }
9281
9282 if (l_acc_nb_of_tile_part)
9283 {
9284 fprintf(out_stream, "\t Tile index: {\n");
9285
9286 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9287 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9288
9289 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9290
9291 if (cstr_index->tile_index[it_tile].tp_index){
9292 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9293 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9294 it_tile_part,
9295 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9296 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9297 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9298 }
9299 }
9300
9301 if (cstr_index->tile_index[it_tile].marker){
9302 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9303 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9304 cstr_index->tile_index[it_tile].marker[it_marker].type,
9305 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9306 cstr_index->tile_index[it_tile].marker[it_marker].len );
9307 }
9308 }
9309 }
9310 fprintf(out_stream,"\t }\n");
9311 }
9312 }
9313
9314 fprintf(out_stream,"}\n");
9315
9316 }
9317
9318
opj_j2k_dump_MH_info(opj_j2k_t * p_j2k,FILE * out_stream)9319 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
9320 {
9321
9322 fprintf(out_stream, "Codestream info from main header: {\n");
9323
9324 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9325 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9326 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9327 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);
9328 fprintf(out_stream, "}\n");
9329 }
9330
j2k_dump_image_header(opj_image_t * img_header,OPJ_BOOL dev_dump_flag,FILE * out_stream)9331 void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
9332 {
9333 char tab[2];
9334
9335 if (dev_dump_flag){
9336 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9337 tab[0] = '\0';
9338 }
9339 else {
9340 fprintf(out_stream, "Image info {\n");
9341 tab[0] = '\t';tab[1] = '\0';
9342 }
9343
9344 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9345 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9346 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9347
9348 if (img_header->comps){
9349 OPJ_UINT32 compno;
9350 for (compno = 0; compno < img_header->numcomps; compno++) {
9351 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9352 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9353 fprintf(out_stream,"%s}\n",tab);
9354 }
9355 }
9356
9357 fprintf(out_stream, "}\n");
9358 }
9359
j2k_dump_image_comp_header(opj_image_comp_t * comp_header,OPJ_BOOL dev_dump_flag,FILE * out_stream)9360 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
9361 {
9362 char tab[3];
9363
9364 if (dev_dump_flag){
9365 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9366 tab[0] = '\0';
9367 } else {
9368 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9369 }
9370
9371 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9372 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9373 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9374
9375 if (dev_dump_flag)
9376 fprintf(out_stream, "}\n");
9377 }
9378
j2k_get_cstr_info(opj_j2k_t * p_j2k)9379 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
9380 {
9381 OPJ_UINT32 compno;
9382 OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
9383 opj_tcp_t *l_default_tile;
9384 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9385 if (!cstr_info)
9386 return NULL;
9387
9388 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9389
9390 cstr_info->tx0 = p_j2k->m_cp.tx0;
9391 cstr_info->ty0 = p_j2k->m_cp.ty0;
9392 cstr_info->tdx = p_j2k->m_cp.tdx;
9393 cstr_info->tdy = p_j2k->m_cp.tdy;
9394 cstr_info->tw = p_j2k->m_cp.tw;
9395 cstr_info->th = p_j2k->m_cp.th;
9396
9397 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9398
9399 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9400
9401 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9402 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9403 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9404 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9405
9406 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9407 if (!cstr_info->m_default_tile_info.tccp_info)
9408 {
9409 opj_destroy_cstr_info(&cstr_info);
9410 return NULL;
9411 }
9412
9413 for (compno = 0; compno < numcomps; compno++) {
9414 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9415 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9416 OPJ_INT32 bandno, numbands;
9417
9418 /* coding style*/
9419 l_tccp_info->csty = l_tccp->csty;
9420 l_tccp_info->numresolutions = l_tccp->numresolutions;
9421 l_tccp_info->cblkw = l_tccp->cblkw;
9422 l_tccp_info->cblkh = l_tccp->cblkh;
9423 l_tccp_info->cblksty = l_tccp->cblksty;
9424 l_tccp_info->qmfbid = l_tccp->qmfbid;
9425 if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS)
9426 {
9427 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9428 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9429 }
9430
9431 /* quantization style*/
9432 l_tccp_info->qntsty = l_tccp->qntsty;
9433 l_tccp_info->numgbits = l_tccp->numgbits;
9434
9435 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
9436 if (numbands < OPJ_J2K_MAXBANDS) {
9437 for (bandno = 0; bandno < numbands; bandno++) {
9438 l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant;
9439 l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn;
9440 }
9441 }
9442
9443 /* RGN value*/
9444 l_tccp_info->roishift = l_tccp->roishift;
9445 }
9446
9447 return cstr_info;
9448 }
9449
j2k_get_cstr_index(opj_j2k_t * p_j2k)9450 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
9451 {
9452 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9453 opj_calloc(1,sizeof(opj_codestream_index_t));
9454 if (!l_cstr_index)
9455 return NULL;
9456
9457 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9458 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9459 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9460
9461 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9462 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9463 if (!l_cstr_index->marker){
9464 opj_free( l_cstr_index);
9465 return NULL;
9466 }
9467
9468 if (p_j2k->cstr_index->marker)
9469 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9470 else{
9471 opj_free(l_cstr_index->marker);
9472 l_cstr_index->marker = NULL;
9473 }
9474
9475 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9476 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9477 if (!l_cstr_index->tile_index){
9478 opj_free( l_cstr_index->marker);
9479 opj_free( l_cstr_index);
9480 return NULL;
9481 }
9482
9483 if (!p_j2k->cstr_index->tile_index){
9484 opj_free(l_cstr_index->tile_index);
9485 l_cstr_index->tile_index = NULL;
9486 }
9487 else {
9488 OPJ_UINT32 it_tile = 0;
9489 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9490
9491 /* Tile Marker*/
9492 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9493
9494 l_cstr_index->tile_index[it_tile].marker =
9495 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9496
9497 if (!l_cstr_index->tile_index[it_tile].marker) {
9498 OPJ_UINT32 it_tile_free;
9499
9500 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9501 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9502 }
9503
9504 opj_free( l_cstr_index->tile_index);
9505 opj_free( l_cstr_index->marker);
9506 opj_free( l_cstr_index);
9507 return NULL;
9508 }
9509
9510 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9511 memcpy( l_cstr_index->tile_index[it_tile].marker,
9512 p_j2k->cstr_index->tile_index[it_tile].marker,
9513 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9514 else{
9515 opj_free(l_cstr_index->tile_index[it_tile].marker);
9516 l_cstr_index->tile_index[it_tile].marker = NULL;
9517 }
9518
9519 /* Tile part index*/
9520 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9521
9522 l_cstr_index->tile_index[it_tile].tp_index =
9523 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9524
9525 if(!l_cstr_index->tile_index[it_tile].tp_index){
9526 OPJ_UINT32 it_tile_free;
9527
9528 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9529 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9530 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9531 }
9532
9533 opj_free( l_cstr_index->tile_index);
9534 opj_free( l_cstr_index->marker);
9535 opj_free( l_cstr_index);
9536 return NULL;
9537 }
9538
9539 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9540 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9541 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9542 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9543 }
9544 else{
9545 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9546 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9547 }
9548
9549 /* Packet index (NOT USED)*/
9550 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9551 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9552
9553 }
9554 }
9555
9556 return l_cstr_index;
9557 }
9558
opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t * p_j2k)9559 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
9560 {
9561 OPJ_UINT32 it_tile=0;
9562
9563 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9564 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9565 if (!p_j2k->cstr_index->tile_index)
9566 return OPJ_FALSE;
9567
9568 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9569 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9570 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9571 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9572 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9573 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9574 return OPJ_FALSE;
9575 }
9576
9577 return OPJ_TRUE;
9578 }
9579
opj_j2k_decode_tiles(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)9580 static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
9581 opj_stream_private_t *p_stream,
9582 opj_event_mgr_t * p_manager)
9583 {
9584 OPJ_BOOL l_go_on = OPJ_TRUE;
9585 OPJ_UINT32 l_current_tile_no;
9586 OPJ_UINT32 l_data_size,l_max_data_size;
9587 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9588 OPJ_UINT32 l_nb_comps;
9589 OPJ_BYTE * l_current_data;
9590 OPJ_UINT32 nr_tiles = 0;
9591
9592 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9593 if (! l_current_data) {
9594 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n");
9595 return OPJ_FALSE;
9596 }
9597 l_max_data_size = 1000;
9598
9599 for (;;) {
9600 if (! opj_j2k_read_tile_header( p_j2k,
9601 &l_current_tile_no,
9602 &l_data_size,
9603 &l_tile_x0, &l_tile_y0,
9604 &l_tile_x1, &l_tile_y1,
9605 &l_nb_comps,
9606 &l_go_on,
9607 p_stream,
9608 p_manager)) {
9609 opj_free(l_current_data);
9610 return OPJ_FALSE;
9611 }
9612
9613 if (! l_go_on) {
9614 break;
9615 }
9616
9617 if (l_data_size > l_max_data_size) {
9618 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9619 if (! l_new_current_data) {
9620 opj_free(l_current_data);
9621 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);
9622 return OPJ_FALSE;
9623 }
9624 l_current_data = l_new_current_data;
9625 l_max_data_size = l_data_size;
9626 }
9627
9628 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9629 opj_free(l_current_data);
9630 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);
9631 return OPJ_FALSE;
9632 }
9633 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);
9634
9635 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9636 opj_free(l_current_data);
9637 return OPJ_FALSE;
9638 }
9639 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9640
9641 if(opj_stream_get_number_byte_left(p_stream) == 0
9642 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
9643 break;
9644 if(++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw)
9645 break;
9646 }
9647
9648 opj_free(l_current_data);
9649
9650 return OPJ_TRUE;
9651 }
9652
9653 /**
9654 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9655 */
opj_j2k_setup_decoding(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)9656 static OPJ_BOOL opj_j2k_setup_decoding (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
9657 {
9658 /* preconditions*/
9659 assert(p_j2k != 00);
9660 assert(p_manager != 00);
9661
9662 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles, p_manager)) {
9663 return OPJ_FALSE;
9664 }
9665 /* DEVELOPER CORNER, add your custom procedures */
9666
9667 return OPJ_TRUE;
9668 }
9669
9670 /*
9671 * Read and decode one tile.
9672 */
opj_j2k_decode_one_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)9673 static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k,
9674 opj_stream_private_t *p_stream,
9675 opj_event_mgr_t * p_manager)
9676 {
9677 OPJ_BOOL l_go_on = OPJ_TRUE;
9678 OPJ_UINT32 l_current_tile_no;
9679 OPJ_UINT32 l_tile_no_to_dec;
9680 OPJ_UINT32 l_data_size,l_max_data_size;
9681 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9682 OPJ_UINT32 l_nb_comps;
9683 OPJ_BYTE * l_current_data;
9684
9685 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9686 if (! l_current_data) {
9687 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n");
9688 return OPJ_FALSE;
9689 }
9690 l_max_data_size = 1000;
9691
9692 /*Allocate and initialize some elements of codestrem index if not already done*/
9693 if( !p_j2k->cstr_index->tile_index)
9694 {
9695 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9696 opj_free(l_current_data);
9697 return OPJ_FALSE;
9698 }
9699 }
9700 /* Move into the codestream to the first SOT used to decode the desired tile */
9701 l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9702 if (p_j2k->cstr_index->tile_index)
9703 if(p_j2k->cstr_index->tile_index->tp_index)
9704 {
9705 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9706 /* the index for this tile has not been built,
9707 * so move to the last SOT read */
9708 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9709 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9710 opj_free(l_current_data);
9711 return OPJ_FALSE;
9712 }
9713 }
9714 else{
9715 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)) ) {
9716 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9717 opj_free(l_current_data);
9718 return OPJ_FALSE;
9719 }
9720 }
9721 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9722 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9723 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9724 }
9725
9726 for (;;) {
9727 if (! opj_j2k_read_tile_header( p_j2k,
9728 &l_current_tile_no,
9729 &l_data_size,
9730 &l_tile_x0, &l_tile_y0,
9731 &l_tile_x1, &l_tile_y1,
9732 &l_nb_comps,
9733 &l_go_on,
9734 p_stream,
9735 p_manager)) {
9736 opj_free(l_current_data);
9737 return OPJ_FALSE;
9738 }
9739
9740 if (! l_go_on) {
9741 break;
9742 }
9743
9744 if (l_data_size > l_max_data_size) {
9745 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9746 if (! l_new_current_data) {
9747 opj_free(l_current_data);
9748 l_current_data = NULL;
9749 /* TODO: LH: why tile numbering policy used in messages differs from
9750 the one used in opj_j2k_decode_tiles() ? */
9751 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);
9752 return OPJ_FALSE;
9753 }
9754 l_current_data = l_new_current_data;
9755 l_max_data_size = l_data_size;
9756 }
9757
9758 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9759 opj_free(l_current_data);
9760 return OPJ_FALSE;
9761 }
9762 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);
9763
9764 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9765 opj_free(l_current_data);
9766 return OPJ_FALSE;
9767 }
9768 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9769
9770 if(l_current_tile_no == l_tile_no_to_dec)
9771 {
9772 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9773 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9774 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9775 opj_free(l_current_data);
9776 return OPJ_FALSE;
9777 }
9778 break;
9779 }
9780 else {
9781 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);
9782 }
9783
9784 }
9785
9786 opj_free(l_current_data);
9787
9788 return OPJ_TRUE;
9789 }
9790
9791 /**
9792 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9793 */
opj_j2k_setup_decoding_tile(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)9794 static OPJ_BOOL opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
9795 {
9796 /* preconditions*/
9797 assert(p_j2k != 00);
9798 assert(p_manager != 00);
9799
9800 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile, p_manager)) {
9801 return OPJ_FALSE;
9802 }
9803 /* DEVELOPER CORNER, add your custom procedures */
9804
9805 return OPJ_TRUE;
9806 }
9807
opj_j2k_decode(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)9808 OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
9809 opj_stream_private_t * p_stream,
9810 opj_image_t * p_image,
9811 opj_event_mgr_t * p_manager)
9812 {
9813 OPJ_UINT32 compno;
9814
9815 if (!p_image)
9816 return OPJ_FALSE;
9817
9818 p_j2k->m_output_image = opj_image_create0();
9819 if (! (p_j2k->m_output_image)) {
9820 return OPJ_FALSE;
9821 }
9822 opj_copy_image_header(p_image, p_j2k->m_output_image);
9823
9824 /* customization of the decoding */
9825 opj_j2k_setup_decoding(p_j2k, p_manager);
9826
9827 /* Decode the codestream */
9828 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9829 opj_image_destroy(p_j2k->m_private_image);
9830 p_j2k->m_private_image = NULL;
9831 return OPJ_FALSE;
9832 }
9833
9834 /* Move data and copy one information from codec to output image*/
9835 for (compno = 0; compno < p_image->numcomps; compno++) {
9836 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9837 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9838 #if 0
9839 char fn[256];
9840 sprintf( fn, "/tmp/%d.raw", compno );
9841 FILE *debug = fopen( fn, "wb" );
9842 fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug );
9843 fclose( debug );
9844 #endif
9845 p_j2k->m_output_image->comps[compno].data = NULL;
9846 }
9847
9848 return OPJ_TRUE;
9849 }
9850
opj_j2k_get_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager,OPJ_UINT32 tile_index)9851 OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k,
9852 opj_stream_private_t *p_stream,
9853 opj_image_t* p_image,
9854 opj_event_mgr_t * p_manager,
9855 OPJ_UINT32 tile_index )
9856 {
9857 OPJ_UINT32 compno;
9858 OPJ_UINT32 l_tile_x, l_tile_y;
9859 opj_image_comp_t* l_img_comp;
9860
9861 if (!p_image) {
9862 opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
9863 return OPJ_FALSE;
9864 }
9865
9866 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9867 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);
9868 return OPJ_FALSE;
9869 }
9870
9871 /* Compute the dimension of the desired tile*/
9872 l_tile_x = tile_index % p_j2k->m_cp.tw;
9873 l_tile_y = tile_index / p_j2k->m_cp.tw;
9874
9875 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9876 if (p_image->x0 < p_j2k->m_private_image->x0)
9877 p_image->x0 = p_j2k->m_private_image->x0;
9878 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9879 if (p_image->x1 > p_j2k->m_private_image->x1)
9880 p_image->x1 = p_j2k->m_private_image->x1;
9881
9882 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9883 if (p_image->y0 < p_j2k->m_private_image->y0)
9884 p_image->y0 = p_j2k->m_private_image->y0;
9885 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9886 if (p_image->y1 > p_j2k->m_private_image->y1)
9887 p_image->y1 = p_j2k->m_private_image->y1;
9888
9889 l_img_comp = p_image->comps;
9890 for (compno=0; compno < p_image->numcomps; ++compno)
9891 {
9892 OPJ_INT32 l_comp_x1, l_comp_y1;
9893
9894 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9895
9896 l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
9897 l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
9898 l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
9899 l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
9900
9901 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));
9902 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));
9903
9904 l_img_comp++;
9905 }
9906
9907 /* Destroy the previous output image*/
9908 if (p_j2k->m_output_image)
9909 opj_image_destroy(p_j2k->m_output_image);
9910
9911 /* Create the ouput image from the information previously computed*/
9912 p_j2k->m_output_image = opj_image_create0();
9913 if (! (p_j2k->m_output_image)) {
9914 return OPJ_FALSE;
9915 }
9916 opj_copy_image_header(p_image, p_j2k->m_output_image);
9917
9918 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
9919
9920 /* customization of the decoding */
9921 opj_j2k_setup_decoding_tile(p_j2k, p_manager);
9922
9923 /* Decode the codestream */
9924 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9925 opj_image_destroy(p_j2k->m_private_image);
9926 p_j2k->m_private_image = NULL;
9927 return OPJ_FALSE;
9928 }
9929
9930 /* Move data and copy one information from codec to output image*/
9931 for (compno = 0; compno < p_image->numcomps; compno++) {
9932 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9933
9934 if (p_image->comps[compno].data)
9935 opj_free(p_image->comps[compno].data);
9936
9937 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9938
9939 p_j2k->m_output_image->comps[compno].data = NULL;
9940 }
9941
9942 return OPJ_TRUE;
9943 }
9944
opj_j2k_set_decoded_resolution_factor(opj_j2k_t * p_j2k,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)9945 OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
9946 OPJ_UINT32 res_factor,
9947 opj_event_mgr_t * p_manager)
9948 {
9949 OPJ_UINT32 it_comp;
9950
9951 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9952
9953 if (p_j2k->m_private_image) {
9954 if (p_j2k->m_private_image->comps) {
9955 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9956 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9957 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9958 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9959 if ( res_factor >= max_res){
9960 opj_event_msg(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
9961 return OPJ_FALSE;
9962 }
9963 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
9964 }
9965 return OPJ_TRUE;
9966 }
9967 }
9968 }
9969 }
9970
9971 return OPJ_FALSE;
9972 }
9973
opj_j2k_encode(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)9974 OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
9975 opj_stream_private_t *p_stream,
9976 opj_event_mgr_t * p_manager )
9977 {
9978 OPJ_UINT32 i, j;
9979 OPJ_UINT32 l_nb_tiles;
9980 OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size;
9981 OPJ_BYTE * l_current_data = 00;
9982 opj_tcd_t* p_tcd = 00;
9983
9984 /* preconditions */
9985 assert(p_j2k != 00);
9986 assert(p_stream != 00);
9987 assert(p_manager != 00);
9988
9989 p_tcd = p_j2k->m_tcd;
9990
9991 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9992 for (i=0;i<l_nb_tiles;++i) {
9993 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
9994 if (l_current_data) {
9995 opj_free(l_current_data);
9996 }
9997 return OPJ_FALSE;
9998 }
9999
10000 /* if we only have one tile, then simply set tile component data equal to image component data */
10001 /* otherwise, allocate the data */
10002 for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) {
10003 opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j;
10004 if (l_nb_tiles == 1) {
10005 opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
10006 l_tilec->data = l_img_comp->data;
10007 l_tilec->ownsData = OPJ_FALSE;
10008 } else {
10009 if(! opj_alloc_tile_component_data(l_tilec)) {
10010 opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." );
10011 if (l_current_data) {
10012 opj_free(l_current_data);
10013 }
10014 return OPJ_FALSE;
10015 }
10016 }
10017 }
10018 l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
10019 if (l_nb_tiles > 1) {
10020 if (l_current_tile_size > l_max_tile_size) {
10021 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
10022 if (! l_new_current_data) {
10023 if (l_current_data) {
10024 opj_free(l_current_data);
10025 }
10026 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
10027 return OPJ_FALSE;
10028 }
10029 l_current_data = l_new_current_data;
10030 l_max_tile_size = l_current_tile_size;
10031 }
10032
10033 /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */
10034 /* 32 bit components @ 8 bit precision get converted to 8 bit */
10035 /* 32 bit components @ 16 bit precision get converted to 16 bit */
10036 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10037
10038 /* now copy this data into the tile component */
10039 if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,l_current_data,l_current_tile_size)) {
10040 opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10041 opj_free(l_current_data);
10042 return OPJ_FALSE;
10043 }
10044 }
10045
10046 if (! opj_j2k_post_write_tile (p_j2k,p_stream,p_manager)) {
10047 if (l_current_data) {
10048 opj_free(l_current_data);
10049 }
10050 return OPJ_FALSE;
10051 }
10052 }
10053
10054 if (l_current_data) {
10055 opj_free(l_current_data);
10056 }
10057 return OPJ_TRUE;
10058 }
10059
opj_j2k_end_compress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10060 OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k,
10061 opj_stream_private_t *p_stream,
10062 opj_event_mgr_t * p_manager)
10063 {
10064 /* customization of the encoding */
10065 if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) {
10066 return OPJ_FALSE;
10067 }
10068
10069 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10070 {
10071 return OPJ_FALSE;
10072 }
10073
10074 return OPJ_TRUE;
10075 }
10076
opj_j2k_start_compress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)10077 OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
10078 opj_stream_private_t *p_stream,
10079 opj_image_t * p_image,
10080 opj_event_mgr_t * p_manager)
10081 {
10082 /* preconditions */
10083 assert(p_j2k != 00);
10084 assert(p_stream != 00);
10085 assert(p_manager != 00);
10086
10087 p_j2k->m_private_image = opj_image_create0();
10088 if (! p_j2k->m_private_image) {
10089 opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." );
10090 return OPJ_FALSE;
10091 }
10092 opj_copy_image_header(p_image, p_j2k->m_private_image);
10093
10094 /* TODO_MSD: Find a better way */
10095 if (p_image->comps) {
10096 OPJ_UINT32 it_comp;
10097 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10098 if (p_image->comps[it_comp].data) {
10099 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10100 p_image->comps[it_comp].data = NULL;
10101
10102 }
10103 }
10104 }
10105
10106 /* customization of the validation */
10107 if (! opj_j2k_setup_encoding_validation (p_j2k, p_manager)) {
10108 return OPJ_FALSE;
10109 }
10110
10111 /* validation of the parameters codec */
10112 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10113 return OPJ_FALSE;
10114 }
10115
10116 /* customization of the encoding */
10117 if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) {
10118 return OPJ_FALSE;
10119 }
10120
10121 /* write header */
10122 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10123 return OPJ_FALSE;
10124 }
10125
10126 return OPJ_TRUE;
10127 }
10128
opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_index,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10129 static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
10130 OPJ_UINT32 p_tile_index,
10131 opj_stream_private_t *p_stream,
10132 opj_event_mgr_t * p_manager )
10133 {
10134 (void)p_stream;
10135 if (p_tile_index != p_j2k->m_current_tile_number) {
10136 opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." );
10137 return OPJ_FALSE;
10138 }
10139
10140 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);
10141
10142 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10143 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10144 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10145
10146 /* initialisation before tile encoding */
10147 if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, p_manager)) {
10148 return OPJ_FALSE;
10149 }
10150
10151 return OPJ_TRUE;
10152 }
10153
opj_get_tile_dimensions(opj_image_t * l_image,opj_tcd_tilecomp_t * l_tilec,opj_image_comp_t * l_img_comp,OPJ_UINT32 * l_size_comp,OPJ_UINT32 * l_width,OPJ_UINT32 * l_height,OPJ_UINT32 * l_offset_x,OPJ_UINT32 * l_offset_y,OPJ_UINT32 * l_image_width,OPJ_UINT32 * l_stride,OPJ_UINT32 * l_tile_offset)10154 static void opj_get_tile_dimensions(opj_image_t * l_image,
10155 opj_tcd_tilecomp_t * l_tilec,
10156 opj_image_comp_t * l_img_comp,
10157 OPJ_UINT32* l_size_comp,
10158 OPJ_UINT32* l_width,
10159 OPJ_UINT32* l_height,
10160 OPJ_UINT32* l_offset_x,
10161 OPJ_UINT32* l_offset_y,
10162 OPJ_UINT32* l_image_width,
10163 OPJ_UINT32* l_stride,
10164 OPJ_UINT32* l_tile_offset) {
10165 OPJ_UINT32 l_remaining;
10166 *l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10167 l_remaining = l_img_comp->prec & 7; /* (%8) */
10168 if (l_remaining) {
10169 *l_size_comp += 1;
10170 }
10171
10172 if (*l_size_comp == 3) {
10173 *l_size_comp = 4;
10174 }
10175
10176 *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
10177 *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
10178 *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx);
10179 *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy);
10180 *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
10181 *l_stride = *l_image_width - *l_width;
10182 *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width;
10183 }
10184
opj_j2k_get_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_data)10185 static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
10186 {
10187 OPJ_UINT32 i,j,k = 0;
10188
10189 for (i=0;i<p_tcd->image->numcomps;++i) {
10190 opj_image_t * l_image = p_tcd->image;
10191 OPJ_INT32 * l_src_ptr;
10192 opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i;
10193 opj_image_comp_t * l_img_comp = l_image->comps + i;
10194 OPJ_UINT32 l_size_comp,l_width,l_height,l_offset_x,l_offset_y, l_image_width,l_stride,l_tile_offset;
10195
10196 opj_get_tile_dimensions(l_image,
10197 l_tilec,
10198 l_img_comp,
10199 &l_size_comp,
10200 &l_width,
10201 &l_height,
10202 &l_offset_x,
10203 &l_offset_y,
10204 &l_image_width,
10205 &l_stride,
10206 &l_tile_offset);
10207
10208 l_src_ptr = l_img_comp->data + l_tile_offset;
10209
10210 switch (l_size_comp) {
10211 case 1:
10212 {
10213 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10214 if (l_img_comp->sgnd) {
10215 for (j=0;j<l_height;++j) {
10216 for (k=0;k<l_width;++k) {
10217 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10218 ++l_dest_ptr;
10219 ++l_src_ptr;
10220 }
10221 l_src_ptr += l_stride;
10222 }
10223 }
10224 else {
10225 for (j=0;j<l_height;++j) {
10226 for (k=0;k<l_width;++k) {
10227 *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr)&0xff);
10228 ++l_dest_ptr;
10229 ++l_src_ptr;
10230 }
10231 l_src_ptr += l_stride;
10232 }
10233 }
10234
10235 p_data = (OPJ_BYTE*) l_dest_ptr;
10236 }
10237 break;
10238 case 2:
10239 {
10240 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10241 if (l_img_comp->sgnd) {
10242 for (j=0;j<l_height;++j) {
10243 for (k=0;k<l_width;++k) {
10244 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10245 }
10246 l_src_ptr += l_stride;
10247 }
10248 }
10249 else {
10250 for (j=0;j<l_height;++j) {
10251 for (k=0;k<l_width;++k) {
10252 *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
10253 }
10254 l_src_ptr += l_stride;
10255 }
10256 }
10257
10258 p_data = (OPJ_BYTE*) l_dest_ptr;
10259 }
10260 break;
10261 case 4:
10262 {
10263 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10264 for (j=0;j<l_height;++j) {
10265 for (k=0;k<l_width;++k) {
10266 *(l_dest_ptr++) = *(l_src_ptr++);
10267 }
10268 l_src_ptr += l_stride;
10269 }
10270
10271 p_data = (OPJ_BYTE*) l_dest_ptr;
10272 }
10273 break;
10274 }
10275 }
10276 }
10277
opj_j2k_post_write_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10278 static OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k,
10279 opj_stream_private_t *p_stream,
10280 opj_event_mgr_t * p_manager )
10281 {
10282 OPJ_UINT32 l_nb_bytes_written;
10283 OPJ_BYTE * l_current_data = 00;
10284 OPJ_UINT32 l_tile_size = 0;
10285 OPJ_UINT32 l_available_data;
10286
10287 /* preconditions */
10288 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10289
10290 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10291 l_available_data = l_tile_size;
10292 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10293
10294 l_nb_bytes_written = 0;
10295 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10296 return OPJ_FALSE;
10297 }
10298 l_current_data += l_nb_bytes_written;
10299 l_available_data -= l_nb_bytes_written;
10300
10301 l_nb_bytes_written = 0;
10302 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10303 return OPJ_FALSE;
10304 }
10305
10306 l_available_data -= l_nb_bytes_written;
10307 l_nb_bytes_written = l_tile_size - l_available_data;
10308
10309 if ( opj_stream_write_data( p_stream,
10310 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10311 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10312 return OPJ_FALSE;
10313 }
10314
10315 ++p_j2k->m_current_tile_number;
10316
10317 return OPJ_TRUE;
10318 }
10319
opj_j2k_setup_end_compress(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)10320 static OPJ_BOOL opj_j2k_setup_end_compress (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
10321 {
10322 /* preconditions */
10323 assert(p_j2k != 00);
10324 assert(p_manager != 00);
10325
10326 /* DEVELOPER CORNER, insert your custom procedures */
10327 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc, p_manager)) {
10328 return OPJ_FALSE;
10329 }
10330
10331 if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
10332 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm, p_manager)) {
10333 return OPJ_FALSE;
10334 }
10335 }
10336
10337 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc, p_manager)) {
10338 return OPJ_FALSE;
10339 }
10340 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding, p_manager)) {
10341 return OPJ_FALSE;
10342 }
10343 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory, p_manager)) {
10344 return OPJ_FALSE;
10345 }
10346 return OPJ_TRUE;
10347 }
10348
opj_j2k_setup_encoding_validation(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)10349 static OPJ_BOOL opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
10350 {
10351 /* preconditions */
10352 assert(p_j2k != 00);
10353 assert(p_manager != 00);
10354
10355 if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder, p_manager)) {
10356 return OPJ_FALSE;
10357 }
10358 if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation, p_manager)) {
10359 return OPJ_FALSE;
10360 }
10361
10362 /* DEVELOPER CORNER, add your custom validation procedure */
10363 if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation, p_manager)) {
10364 return OPJ_FALSE;
10365 }
10366
10367 return OPJ_TRUE;
10368 }
10369
opj_j2k_setup_header_writing(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)10370 static OPJ_BOOL opj_j2k_setup_header_writing (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager)
10371 {
10372 /* preconditions */
10373 assert(p_j2k != 00);
10374 assert(p_manager != 00);
10375
10376 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info, p_manager)) {
10377 return OPJ_FALSE;
10378 }
10379 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc, p_manager)) {
10380 return OPJ_FALSE;
10381 }
10382 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz, p_manager)) {
10383 return OPJ_FALSE;
10384 }
10385 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod, p_manager)) {
10386 return OPJ_FALSE;
10387 }
10388 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd, p_manager)) {
10389 return OPJ_FALSE;
10390 }
10391
10392 if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
10393 /* No need for COC or QCC, QCD and COD are used
10394 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc, p_manager)) {
10395 return OPJ_FALSE;
10396 }
10397 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc, p_manager)) {
10398 return OPJ_FALSE;
10399 }
10400 */
10401 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm, p_manager)) {
10402 return OPJ_FALSE;
10403 }
10404
10405 if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
10406 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc, p_manager)) {
10407 return OPJ_FALSE;
10408 }
10409 }
10410 }
10411
10412 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions, p_manager)) {
10413 return OPJ_FALSE;
10414 }
10415
10416 if (p_j2k->m_cp.comment != 00) {
10417 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com, p_manager)) {
10418 return OPJ_FALSE;
10419 }
10420 }
10421
10422 /* DEVELOPER CORNER, insert your custom procedures */
10423 if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) {
10424 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group, p_manager)) {
10425 return OPJ_FALSE;
10426 }
10427 }
10428 /* End of Developer Corner */
10429
10430 if (p_j2k->cstr_index) {
10431 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header, p_manager)) {
10432 return OPJ_FALSE;
10433 }
10434 }
10435
10436 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd, p_manager)) {
10437 return OPJ_FALSE;
10438 }
10439 if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates, p_manager)) {
10440 return OPJ_FALSE;
10441 }
10442
10443 return OPJ_TRUE;
10444 }
10445
opj_j2k_write_first_tile_part(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,opj_stream_private_t * p_stream,struct opj_event_mgr * p_manager)10446 static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
10447 OPJ_BYTE * p_data,
10448 OPJ_UINT32 * p_data_written,
10449 OPJ_UINT32 p_total_data_size,
10450 opj_stream_private_t *p_stream,
10451 struct opj_event_mgr * p_manager )
10452 {
10453 OPJ_UINT32 l_nb_bytes_written = 0;
10454 OPJ_UINT32 l_current_nb_bytes_written;
10455 OPJ_BYTE * l_begin_data = 00;
10456
10457 opj_tcd_t * l_tcd = 00;
10458 opj_cp_t * l_cp = 00;
10459
10460 l_tcd = p_j2k->m_tcd;
10461 l_cp = &(p_j2k->m_cp);
10462
10463 l_tcd->cur_pino = 0;
10464
10465 /*Get number of tile parts*/
10466 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10467
10468 /* INDEX >> */
10469 /* << INDEX */
10470
10471 l_current_nb_bytes_written = 0;
10472 l_begin_data = p_data;
10473 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10474 {
10475 return OPJ_FALSE;
10476 }
10477
10478 l_nb_bytes_written += l_current_nb_bytes_written;
10479 p_data += l_current_nb_bytes_written;
10480 p_total_data_size -= l_current_nb_bytes_written;
10481
10482 if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
10483 #if 0
10484 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10485 l_current_nb_bytes_written = 0;
10486 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10487 l_nb_bytes_written += l_current_nb_bytes_written;
10488 p_data += l_current_nb_bytes_written;
10489 p_total_data_size -= l_current_nb_bytes_written;
10490
10491 l_current_nb_bytes_written = 0;
10492 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10493 l_nb_bytes_written += l_current_nb_bytes_written;
10494 p_data += l_current_nb_bytes_written;
10495 p_total_data_size -= l_current_nb_bytes_written;
10496 }
10497 #endif
10498
10499 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10500 l_current_nb_bytes_written = 0;
10501 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10502 l_nb_bytes_written += l_current_nb_bytes_written;
10503 p_data += l_current_nb_bytes_written;
10504 p_total_data_size -= l_current_nb_bytes_written;
10505 }
10506 }
10507
10508 l_current_nb_bytes_written = 0;
10509 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10510 return OPJ_FALSE;
10511 }
10512
10513 l_nb_bytes_written += l_current_nb_bytes_written;
10514 * p_data_written = l_nb_bytes_written;
10515
10516 /* Writing Psot in SOT marker */
10517 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10518
10519 if (OPJ_IS_CINEMA(l_cp->rsiz)){
10520 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10521 }
10522
10523 return OPJ_TRUE;
10524 }
10525
opj_j2k_write_all_tile_parts(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,opj_stream_private_t * p_stream,struct opj_event_mgr * p_manager)10526 static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k,
10527 OPJ_BYTE * p_data,
10528 OPJ_UINT32 * p_data_written,
10529 OPJ_UINT32 p_total_data_size,
10530 opj_stream_private_t *p_stream,
10531 struct opj_event_mgr * p_manager
10532 )
10533 {
10534 OPJ_UINT32 tilepartno=0;
10535 OPJ_UINT32 l_nb_bytes_written = 0;
10536 OPJ_UINT32 l_current_nb_bytes_written;
10537 OPJ_UINT32 l_part_tile_size;
10538 OPJ_UINT32 tot_num_tp;
10539 OPJ_UINT32 pino;
10540
10541 OPJ_BYTE * l_begin_data;
10542 opj_tcp_t *l_tcp = 00;
10543 opj_tcd_t * l_tcd = 00;
10544 opj_cp_t * l_cp = 00;
10545
10546 l_tcd = p_j2k->m_tcd;
10547 l_cp = &(p_j2k->m_cp);
10548 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10549
10550 /*Get number of tile parts*/
10551 tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
10552
10553 /* start writing remaining tile parts */
10554 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10555 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10556 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10557 l_current_nb_bytes_written = 0;
10558 l_part_tile_size = 0;
10559 l_begin_data = p_data;
10560
10561 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10562 return OPJ_FALSE;
10563 }
10564
10565 l_nb_bytes_written += l_current_nb_bytes_written;
10566 p_data += l_current_nb_bytes_written;
10567 p_total_data_size -= l_current_nb_bytes_written;
10568 l_part_tile_size += l_current_nb_bytes_written;
10569
10570 l_current_nb_bytes_written = 0;
10571 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10572 return OPJ_FALSE;
10573 }
10574
10575 p_data += l_current_nb_bytes_written;
10576 l_nb_bytes_written += l_current_nb_bytes_written;
10577 p_total_data_size -= l_current_nb_bytes_written;
10578 l_part_tile_size += l_current_nb_bytes_written;
10579
10580 /* Writing Psot in SOT marker */
10581 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10582
10583 if (OPJ_IS_CINEMA(l_cp->rsiz)) {
10584 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10585 }
10586
10587 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10588 }
10589
10590 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10591 l_tcd->cur_pino = pino;
10592
10593 /*Get number of tile parts*/
10594 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
10595 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10596 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10597 l_current_nb_bytes_written = 0;
10598 l_part_tile_size = 0;
10599 l_begin_data = p_data;
10600
10601 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10602 return OPJ_FALSE;
10603 }
10604
10605 l_nb_bytes_written += l_current_nb_bytes_written;
10606 p_data += l_current_nb_bytes_written;
10607 p_total_data_size -= l_current_nb_bytes_written;
10608 l_part_tile_size += l_current_nb_bytes_written;
10609
10610 l_current_nb_bytes_written = 0;
10611
10612 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10613 return OPJ_FALSE;
10614 }
10615
10616 l_nb_bytes_written += l_current_nb_bytes_written;
10617 p_data += l_current_nb_bytes_written;
10618 p_total_data_size -= l_current_nb_bytes_written;
10619 l_part_tile_size += l_current_nb_bytes_written;
10620
10621 /* Writing Psot in SOT marker */
10622 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10623
10624 if (OPJ_IS_CINEMA(l_cp->rsiz)) {
10625 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10626 }
10627
10628 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10629 }
10630 }
10631
10632 *p_data_written = l_nb_bytes_written;
10633
10634 return OPJ_TRUE;
10635 }
10636
opj_j2k_write_updated_tlm(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)10637 static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
10638 struct opj_stream_private *p_stream,
10639 struct opj_event_mgr * p_manager )
10640 {
10641 OPJ_UINT32 l_tlm_size;
10642 OPJ_OFF_T l_tlm_position, l_current_position;
10643
10644 /* preconditions */
10645 assert(p_j2k != 00);
10646 assert(p_manager != 00);
10647 assert(p_stream != 00);
10648
10649 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10650 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10651 l_current_position = opj_stream_tell(p_stream);
10652
10653 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10654 return OPJ_FALSE;
10655 }
10656
10657 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) {
10658 return OPJ_FALSE;
10659 }
10660
10661 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10662 return OPJ_FALSE;
10663 }
10664
10665 return OPJ_TRUE;
10666 }
10667
opj_j2k_end_encoding(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)10668 static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k,
10669 struct opj_stream_private *p_stream,
10670 struct opj_event_mgr * p_manager )
10671 {
10672 /* preconditions */
10673 assert(p_j2k != 00);
10674 assert(p_manager != 00);
10675 assert(p_stream != 00);
10676
10677 opj_tcd_destroy(p_j2k->m_tcd);
10678 p_j2k->m_tcd = 00;
10679
10680 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10681 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10682 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10683 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10684 }
10685
10686 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10687 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10688 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10689 }
10690
10691 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10692
10693 return OPJ_TRUE;
10694 }
10695
10696 /**
10697 * Destroys the memory associated with the decoding of headers.
10698 */
opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10699 static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
10700 opj_stream_private_t *p_stream,
10701 opj_event_mgr_t * p_manager
10702 )
10703 {
10704 /* preconditions */
10705 assert(p_j2k != 00);
10706 assert(p_stream != 00);
10707 assert(p_manager != 00);
10708
10709 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10710 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10711 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10712 }
10713
10714 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10715
10716 return OPJ_TRUE;
10717 }
10718
opj_j2k_init_info(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)10719 static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k,
10720 struct opj_stream_private *p_stream,
10721 struct opj_event_mgr * p_manager )
10722 {
10723 opj_codestream_info_t * l_cstr_info = 00;
10724
10725 /* preconditions */
10726 assert(p_j2k != 00);
10727 assert(p_manager != 00);
10728 assert(p_stream != 00);
10729 (void)l_cstr_info;
10730
10731 /* TODO mergeV2: check this part which use cstr_info */
10732 /*l_cstr_info = p_j2k->cstr_info;
10733
10734 if (l_cstr_info) {
10735 OPJ_UINT32 compno;
10736 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));
10737
10738 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10739 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10740
10741 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10742
10743 l_cstr_info->tw = p_j2k->m_cp.tw;
10744 l_cstr_info->th = p_j2k->m_cp.th;
10745
10746 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10747 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10748 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10749 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10750
10751 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10752
10753 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10754
10755 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10756
10757 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10758 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10759 }
10760
10761 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10762
10763 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10764
10765 /*l_cstr_info->maxmarknum = 100;
10766 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10767 l_cstr_info->marknum = 0;
10768 }*/
10769
10770 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);
10771 }
10772
10773 /**
10774 * Creates a tile-coder decoder.
10775 *
10776 * @param p_stream the stream to write data to.
10777 * @param p_j2k J2K codec.
10778 * @param p_manager the user event manager.
10779 */
opj_j2k_create_tcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10780 static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k,
10781 opj_stream_private_t *p_stream,
10782 opj_event_mgr_t * p_manager
10783 )
10784 {
10785 /* preconditions */
10786 assert(p_j2k != 00);
10787 assert(p_manager != 00);
10788 assert(p_stream != 00);
10789
10790 p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
10791
10792 if (! p_j2k->m_tcd) {
10793 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10794 return OPJ_FALSE;
10795 }
10796
10797 if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10798 opj_tcd_destroy(p_j2k->m_tcd);
10799 p_j2k->m_tcd = 00;
10800 return OPJ_FALSE;
10801 }
10802
10803 return OPJ_TRUE;
10804 }
10805
opj_j2k_write_tile(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10806 OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k,
10807 OPJ_UINT32 p_tile_index,
10808 OPJ_BYTE * p_data,
10809 OPJ_UINT32 p_data_size,
10810 opj_stream_private_t *p_stream,
10811 opj_event_mgr_t * p_manager )
10812 {
10813 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10814 opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10815 return OPJ_FALSE;
10816 }
10817 else {
10818 OPJ_UINT32 j;
10819 /* Allocate data */
10820 for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) {
10821 opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j;
10822
10823 if(! opj_alloc_tile_component_data(l_tilec)) {
10824 opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." );
10825 return OPJ_FALSE;
10826 }
10827 }
10828
10829 /* now copy data into the the tile component */
10830 if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
10831 opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10832 return OPJ_FALSE;
10833 }
10834 if (! opj_j2k_post_write_tile(p_j2k,p_stream,p_manager)) {
10835 opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
10836 return OPJ_FALSE;
10837 }
10838 }
10839
10840 return OPJ_TRUE;
10841 }
10842