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