Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The copyright in this software is being made available under the 2-clauses |
| 3 | * BSD License, included below. This software may be subject to other third |
| 4 | * party and contributor rights, including patent rights, and no such rights |
| 5 | * are granted under this license. |
| 6 | * |
| 7 | * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
| 8 | * Copyright (c) 2002-2014, Professor Benoit Macq |
| 9 | * Copyright (c) 2001-2003, David Janssens |
| 10 | * Copyright (c) 2002-2003, Yannick Verschueren |
| 11 | * Copyright (c) 2003-2007, Francois-Olivier Devaux |
| 12 | * Copyright (c) 2003-2014, Antonin Descampe |
| 13 | * Copyright (c) 2005, Herve Drolon, FreeImage Team |
| 14 | * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr> |
| 15 | * Copyright (c) 2006-2007, Parvatha Elangovan |
| 16 | * Copyright (c) 2010-2011, Kaori Hagihara |
| 17 | * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France |
| 18 | * Copyright (c) 2012, CS Systemes d'Information, France |
| 19 | * All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
| 31 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 40 | * POSSIBILITY OF SUCH DAMAGE. |
| 41 | */ |
| 42 | |
| 43 | #include "opj_includes.h" |
| 44 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 45 | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ |
| 46 | /*@{*/ |
| 47 | |
| 48 | /** @name Local static functions */ |
| 49 | /*@{*/ |
| 50 | |
| 51 | /** |
| 52 | * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures. |
| 53 | */ |
| 54 | static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k); |
| 55 | |
| 56 | /** |
| 57 | * The read header procedure. |
| 58 | */ |
| 59 | static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, |
| 60 | opj_stream_private_t *p_stream, |
| 61 | opj_event_mgr_t * p_manager); |
| 62 | |
| 63 | /** |
| 64 | * The default encoding validation procedure without any extension. |
| 65 | * |
| 66 | * @param p_j2k the jpeg2000 codec to validate. |
| 67 | * @param p_stream the input stream to validate. |
| 68 | * @param p_manager the user event manager. |
| 69 | * |
| 70 | * @return true if the parameters are correct. |
| 71 | */ |
| 72 | static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, |
| 73 | opj_stream_private_t *p_stream, |
| 74 | opj_event_mgr_t * p_manager ); |
| 75 | |
| 76 | /** |
| 77 | * The default decoding validation procedure without any extension. |
| 78 | * |
| 79 | * @param p_j2k the jpeg2000 codec to validate. |
| 80 | * @param p_stream the input stream to validate. |
| 81 | * @param p_manager the user event manager. |
| 82 | * |
| 83 | * @return true if the parameters are correct. |
| 84 | */ |
| 85 | static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t * p_j2k, |
| 86 | opj_stream_private_t *p_stream, |
| 87 | opj_event_mgr_t * p_manager ); |
| 88 | |
| 89 | /** |
| 90 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
| 91 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
| 92 | */ |
| 93 | static void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k); |
| 94 | |
| 95 | /** |
| 96 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
| 97 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
| 98 | */ |
| 99 | static void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k); |
| 100 | |
| 101 | /** |
| 102 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
| 103 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
| 104 | */ |
| 105 | static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k); |
| 106 | |
| 107 | /** |
| 108 | * The mct encoding validation procedure. |
| 109 | * |
| 110 | * @param p_j2k the jpeg2000 codec to validate. |
| 111 | * @param p_stream the input stream to validate. |
| 112 | * @param p_manager the user event manager. |
| 113 | * |
| 114 | * @return true if the parameters are correct. |
| 115 | */ |
| 116 | static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k, |
| 117 | opj_stream_private_t *p_stream, |
| 118 | opj_event_mgr_t * p_manager ); |
| 119 | |
| 120 | /** |
| 121 | * Builds the tcd decoder to use to decode tile. |
| 122 | */ |
| 123 | static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k, |
| 124 | opj_stream_private_t *p_stream, |
| 125 | opj_event_mgr_t * p_manager ); |
| 126 | /** |
| 127 | * Builds the tcd encoder to use to encode tile. |
| 128 | */ |
| 129 | static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k, |
| 130 | opj_stream_private_t *p_stream, |
| 131 | opj_event_mgr_t * p_manager ); |
| 132 | |
| 133 | /** |
| 134 | * Creates a tile-coder decoder. |
| 135 | * |
| 136 | * @param p_stream the stream to write data to. |
| 137 | * @param p_j2k J2K codec. |
| 138 | * @param p_manager the user event manager. |
| 139 | */ |
| 140 | static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k, |
| 141 | opj_stream_private_t *p_stream, |
| 142 | opj_event_mgr_t * p_manager ); |
| 143 | |
| 144 | /** |
| 145 | * Excutes the given procedures on the given codec. |
| 146 | * |
| 147 | * @param p_procedure_list the list of procedures to execute |
| 148 | * @param p_j2k the jpeg2000 codec to execute the procedures on. |
| 149 | * @param p_stream the stream to execute the procedures on. |
| 150 | * @param p_manager the user manager. |
| 151 | * |
| 152 | * @return true if all the procedures were successfully executed. |
| 153 | */ |
| 154 | static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k, |
| 155 | opj_procedure_list_t * p_procedure_list, |
| 156 | opj_stream_private_t *p_stream, |
| 157 | opj_event_mgr_t * p_manager); |
| 158 | |
| 159 | /** |
| 160 | * Updates the rates of the tcp. |
| 161 | * |
| 162 | * @param p_stream the stream to write data to. |
| 163 | * @param p_j2k J2K codec. |
| 164 | * @param p_manager the user event manager. |
| 165 | */ |
| 166 | static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k, |
| 167 | opj_stream_private_t *p_stream, |
| 168 | opj_event_mgr_t * p_manager ); |
| 169 | |
| 170 | /** |
| 171 | * Copies the decoding tile parameters onto all the tile parameters. |
| 172 | * Creates also the tile decoder. |
| 173 | */ |
| 174 | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k, |
| 175 | opj_stream_private_t *p_stream, |
| 176 | opj_event_mgr_t * p_manager ); |
| 177 | |
| 178 | /** |
| 179 | * Destroys the memory associated with the decoding of headers. |
| 180 | */ |
| 181 | static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k, |
| 182 | opj_stream_private_t *p_stream, |
| 183 | opj_event_mgr_t * p_manager ); |
| 184 | |
| 185 | /** |
| 186 | * Reads the lookup table containing all the marker, status and action, and returns the handler associated |
| 187 | * with the marker value. |
| 188 | * @param p_id Marker value to look up |
| 189 | * |
| 190 | * @return the handler associated with the id. |
| 191 | */ |
| 192 | static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id); |
| 193 | |
| 194 | /** |
| 195 | * Destroys a tile coding parameter structure. |
| 196 | * |
| 197 | * @param p_tcp the tile coding parameter to destroy. |
| 198 | */ |
| 199 | static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp); |
| 200 | |
| 201 | /** |
| 202 | * Destroys the data inside a tile coding parameter structure. |
| 203 | * |
| 204 | * @param p_tcp the tile coding parameter which contain data to destroy. |
| 205 | */ |
| 206 | static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp); |
| 207 | |
| 208 | /** |
| 209 | * Destroys a coding parameter structure. |
| 210 | * |
| 211 | * @param p_cp the coding parameter to destroy. |
| 212 | */ |
| 213 | static void opj_j2k_cp_destroy (opj_cp_t *p_cp); |
| 214 | |
| 215 | /** |
| 216 | * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
| 217 | * |
| 218 | * @param p_j2k J2K codec. |
| 219 | * @param p_tile_no FIXME DOC |
| 220 | * @param p_comp_no the component number to output. |
| 221 | * @param p_data FIXME DOC |
| 222 | * @param p_header_size FIXME DOC |
| 223 | * @param p_manager the user event manager. |
| 224 | * |
| 225 | * @return FIXME DOC |
| 226 | */ |
| 227 | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, |
| 228 | OPJ_UINT32 p_tile_no, |
| 229 | OPJ_UINT32 p_comp_no, |
| 230 | OPJ_BYTE * p_data, |
| 231 | OPJ_UINT32 * p_header_size, |
| 232 | opj_event_mgr_t * p_manager ); |
| 233 | |
| 234 | /** |
| 235 | * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. |
| 236 | * |
| 237 | * @param p_j2k the J2K codec. |
| 238 | * @param p_tile_no the tile index. |
| 239 | * @param p_comp_no the component being outputted. |
| 240 | * |
| 241 | * @return the number of bytes taken by the SPCod element. |
| 242 | */ |
| 243 | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k, |
| 244 | OPJ_UINT32 p_tile_no, |
| 245 | OPJ_UINT32 p_comp_no ); |
| 246 | |
| 247 | /** |
| 248 | * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
| 249 | * @param p_j2k the jpeg2000 codec. |
| 250 | * @param compno FIXME DOC |
| 251 | * @param p_header_data the data contained in the COM box. |
| 252 | * @param p_header_size the size of the data contained in the COM marker. |
| 253 | * @param p_manager the user event manager. |
| 254 | */ |
| 255 | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, |
| 256 | OPJ_UINT32 compno, |
| 257 | OPJ_BYTE * p_header_data, |
| 258 | OPJ_UINT32 * p_header_size, |
| 259 | opj_event_mgr_t * p_manager ); |
| 260 | |
| 261 | /** |
| 262 | * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
| 263 | * |
| 264 | * @param p_tile_no the tile index. |
| 265 | * @param p_comp_no the component being outputted. |
| 266 | * @param p_j2k the J2K codec. |
| 267 | * |
| 268 | * @return the number of bytes taken by the SPCod element. |
| 269 | */ |
| 270 | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k, |
| 271 | OPJ_UINT32 p_tile_no, |
| 272 | OPJ_UINT32 p_comp_no ); |
| 273 | |
| 274 | /** |
| 275 | * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
| 276 | * |
| 277 | * @param p_tile_no the tile to output. |
| 278 | * @param p_comp_no the component number to output. |
| 279 | * @param p_data the data buffer. |
| 280 | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
| 281 | * @param p_j2k J2K codec. |
| 282 | * @param p_manager the user event manager. |
| 283 | * |
| 284 | */ |
| 285 | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
| 286 | OPJ_UINT32 p_tile_no, |
| 287 | OPJ_UINT32 p_comp_no, |
| 288 | OPJ_BYTE * p_data, |
| 289 | OPJ_UINT32 * p_header_size, |
| 290 | opj_event_mgr_t * p_manager); |
| 291 | |
| 292 | /** |
| 293 | * Updates the Tile Length Marker. |
| 294 | */ |
| 295 | static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size); |
| 296 | |
| 297 | /** |
| 298 | * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
| 299 | * |
| 300 | * @param p_j2k J2K codec. |
| 301 | * @param compno the component number to output. |
| 302 | * @param p_header_data the data buffer. |
| 303 | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
| 304 | * @param p_manager the user event manager. |
| 305 | * |
| 306 | */ |
| 307 | static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k, |
| 308 | OPJ_UINT32 compno, |
| 309 | OPJ_BYTE * p_header_data, |
| 310 | OPJ_UINT32 * p_header_size, |
| 311 | opj_event_mgr_t * p_manager ); |
| 312 | |
| 313 | /** |
| 314 | * Copies the tile component parameters of all the component from the first tile component. |
| 315 | * |
| 316 | * @param p_j2k the J2k codec. |
| 317 | */ |
| 318 | static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k ); |
| 319 | |
| 320 | /** |
| 321 | * Copies the tile quantization parameters of all the component from the first tile component. |
| 322 | * |
| 323 | * @param p_j2k the J2k codec. |
| 324 | */ |
| 325 | static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k ); |
| 326 | |
| 327 | /** |
| 328 | * Reads the tiles. |
| 329 | */ |
| 330 | static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, |
| 331 | opj_stream_private_t *p_stream, |
| 332 | opj_event_mgr_t * p_manager); |
| 333 | |
| 334 | static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k, |
| 335 | OPJ_UINT32 p_tile_index, |
| 336 | opj_stream_private_t *p_stream, |
| 337 | opj_event_mgr_t * p_manager ); |
| 338 | |
| 339 | static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image); |
| 340 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 341 | static void opj_get_tile_dimensions(opj_image_t * l_image, |
| 342 | opj_tcd_tilecomp_t * l_tilec, |
| 343 | opj_image_comp_t * l_img_comp, |
| 344 | OPJ_UINT32* l_size_comp, |
| 345 | OPJ_UINT32* l_width, |
| 346 | OPJ_UINT32* l_height, |
| 347 | OPJ_UINT32* l_offset_x, |
| 348 | OPJ_UINT32* l_offset_y, |
| 349 | OPJ_UINT32* l_image_width, |
| 350 | OPJ_UINT32* l_stride, |
| 351 | OPJ_UINT32* l_tile_offset); |
| 352 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 353 | static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data); |
| 354 | |
| 355 | static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k, |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 356 | opj_stream_private_t *p_stream, |
| 357 | opj_event_mgr_t * p_manager ); |
| 358 | |
| 359 | /** |
| 360 | * Sets up the procedures to do on writing header. |
| 361 | * Developers wanting to extend the library can add their own writing procedures. |
| 362 | */ |
| 363 | static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k); |
| 364 | |
| 365 | static OPJ_BOOL opj_j2k_write_first_tile_part( opj_j2k_t *p_j2k, |
| 366 | OPJ_BYTE * p_data, |
| 367 | OPJ_UINT32 * p_data_written, |
| 368 | OPJ_UINT32 p_total_data_size, |
| 369 | opj_stream_private_t *p_stream, |
| 370 | struct opj_event_mgr * p_manager ); |
| 371 | |
| 372 | static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, |
| 373 | OPJ_BYTE * p_data, |
| 374 | OPJ_UINT32 * p_data_written, |
| 375 | OPJ_UINT32 p_total_data_size, |
| 376 | opj_stream_private_t *p_stream, |
| 377 | struct opj_event_mgr * p_manager ); |
| 378 | |
| 379 | /** |
| 380 | * Gets the offset of the header. |
| 381 | * |
| 382 | * @param p_stream the stream to write data to. |
| 383 | * @param p_j2k J2K codec. |
| 384 | * @param p_manager the user event manager. |
| 385 | */ |
| 386 | static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k, |
| 387 | opj_stream_private_t *p_stream, |
| 388 | opj_event_mgr_t * p_manager ); |
| 389 | |
| 390 | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); |
| 391 | |
| 392 | /* |
| 393 | * ----------------------------------------------------------------------- |
| 394 | * ----------------------------------------------------------------------- |
| 395 | * ----------------------------------------------------------------------- |
| 396 | */ |
| 397 | |
| 398 | /** |
| 399 | * Writes the SOC marker (Start Of Codestream) |
| 400 | * |
| 401 | * @param p_stream the stream to write data to. |
| 402 | * @param p_j2k J2K codec. |
| 403 | * @param p_manager the user event manager. |
| 404 | */ |
| 405 | static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k, |
| 406 | opj_stream_private_t *p_stream, |
| 407 | opj_event_mgr_t * p_manager ); |
| 408 | |
| 409 | /** |
| 410 | * Reads a SOC marker (Start of Codestream) |
| 411 | * @param p_j2k the jpeg2000 file codec. |
| 412 | * @param p_stream XXX needs data |
| 413 | * @param p_manager the user event manager. |
| 414 | */ |
| 415 | static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, |
| 416 | opj_stream_private_t *p_stream, |
| 417 | opj_event_mgr_t * p_manager ); |
| 418 | |
| 419 | /** |
| 420 | * Writes the SIZ marker (image and tile size) |
| 421 | * |
| 422 | * @param p_j2k J2K codec. |
| 423 | * @param p_stream the stream to write data to. |
| 424 | * @param p_manager the user event manager. |
| 425 | */ |
| 426 | static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, |
| 427 | opj_stream_private_t *p_stream, |
| 428 | opj_event_mgr_t * p_manager ); |
| 429 | |
| 430 | /** |
| 431 | * Reads a SIZ marker (image and tile size) |
| 432 | * @param p_j2k the jpeg2000 file codec. |
| 433 | * @param p_header_data the data contained in the SIZ box. |
| 434 | * @param p_header_size the size of the data contained in the SIZ marker. |
| 435 | * @param p_manager the user event manager. |
| 436 | */ |
| 437 | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
| 438 | OPJ_BYTE * p_header_data, |
| 439 | OPJ_UINT32 p_header_size, |
| 440 | opj_event_mgr_t * p_manager); |
| 441 | |
| 442 | /** |
| 443 | * Writes the COM marker (comment) |
| 444 | * |
| 445 | * @param p_stream the stream to write data to. |
| 446 | * @param p_j2k J2K codec. |
| 447 | * @param p_manager the user event manager. |
| 448 | */ |
| 449 | static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k, |
| 450 | opj_stream_private_t *p_stream, |
| 451 | opj_event_mgr_t * p_manager ); |
| 452 | |
| 453 | /** |
| 454 | * Reads a COM marker (comments) |
| 455 | * @param p_j2k the jpeg2000 file codec. |
| 456 | * @param p_header_data the data contained in the COM box. |
| 457 | * @param p_header_size the size of the data contained in the COM marker. |
| 458 | * @param p_manager the user event manager. |
| 459 | */ |
| 460 | static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k, |
| 461 | OPJ_BYTE * p_header_data, |
| 462 | OPJ_UINT32 p_header_size, |
| 463 | opj_event_mgr_t * p_manager ); |
| 464 | /** |
| 465 | * Writes the COD marker (Coding style default) |
| 466 | * |
| 467 | * @param p_stream the stream to write data to. |
| 468 | * @param p_j2k J2K codec. |
| 469 | * @param p_manager the user event manager. |
| 470 | */ |
| 471 | static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k, |
| 472 | opj_stream_private_t *p_stream, |
| 473 | opj_event_mgr_t * p_manager ); |
| 474 | |
| 475 | /** |
| 476 | * Reads a COD marker (Coding Styke defaults) |
| 477 | * @param p_header_data the data contained in the COD box. |
| 478 | * @param p_j2k the jpeg2000 codec. |
| 479 | * @param p_header_size the size of the data contained in the COD marker. |
| 480 | * @param p_manager the user event manager. |
| 481 | */ |
| 482 | static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, |
| 483 | OPJ_BYTE * p_header_data, |
| 484 | OPJ_UINT32 p_header_size, |
| 485 | opj_event_mgr_t * p_manager); |
| 486 | |
| 487 | #if 0 |
| 488 | /** |
| 489 | * Writes the COC marker (Coding style component) |
| 490 | * |
| 491 | * @param p_j2k J2K codec. |
| 492 | * @param p_comp_no the index of the component to output. |
| 493 | * @param p_stream the stream to write data to. |
| 494 | * @param p_manager the user event manager. |
| 495 | */ |
| 496 | static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k, |
| 497 | OPJ_UINT32 p_comp_no, |
| 498 | opj_stream_private_t *p_stream, |
| 499 | opj_event_mgr_t * p_manager ); |
| 500 | #endif |
| 501 | |
| 502 | #if 0 |
| 503 | /** |
| 504 | * Writes the COC marker (Coding style component) |
| 505 | * |
| 506 | * @param p_j2k J2K codec. |
| 507 | * @param p_comp_no the index of the component to output. |
| 508 | * @param p_data FIXME DOC |
| 509 | * @param p_data_written FIXME DOC |
| 510 | * @param p_manager the user event manager. |
| 511 | */ |
| 512 | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
| 513 | OPJ_UINT32 p_comp_no, |
| 514 | OPJ_BYTE * p_data, |
| 515 | OPJ_UINT32 * p_data_written, |
| 516 | opj_event_mgr_t * p_manager ); |
| 517 | #endif |
| 518 | |
| 519 | /** |
| 520 | * Gets the maximum size taken by a coc. |
| 521 | * |
| 522 | * @param p_j2k the jpeg2000 codec to use. |
| 523 | */ |
| 524 | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); |
| 525 | |
| 526 | /** |
| 527 | * Reads a COC marker (Coding Style Component) |
| 528 | * @param p_header_data the data contained in the COC box. |
| 529 | * @param p_j2k the jpeg2000 codec. |
| 530 | * @param p_header_size the size of the data contained in the COC marker. |
| 531 | * @param p_manager the user event manager. |
| 532 | */ |
| 533 | static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k, |
| 534 | OPJ_BYTE * p_header_data, |
| 535 | OPJ_UINT32 p_header_size, |
| 536 | opj_event_mgr_t * p_manager ); |
| 537 | |
| 538 | /** |
| 539 | * Writes the QCD marker (quantization default) |
| 540 | * |
| 541 | * @param p_j2k J2K codec. |
| 542 | * @param p_stream the stream to write data to. |
| 543 | * @param p_manager the user event manager. |
| 544 | */ |
| 545 | static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k, |
| 546 | opj_stream_private_t *p_stream, |
| 547 | opj_event_mgr_t * p_manager ); |
| 548 | |
| 549 | /** |
| 550 | * Reads a QCD marker (Quantization defaults) |
| 551 | * @param p_header_data the data contained in the QCD box. |
| 552 | * @param p_j2k the jpeg2000 codec. |
| 553 | * @param p_header_size the size of the data contained in the QCD marker. |
| 554 | * @param p_manager the user event manager. |
| 555 | */ |
| 556 | static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k, |
| 557 | OPJ_BYTE * p_header_data, |
| 558 | OPJ_UINT32 p_header_size, |
| 559 | opj_event_mgr_t * p_manager ); |
| 560 | #if 0 |
| 561 | /** |
| 562 | * Writes the QCC marker (quantization component) |
| 563 | * |
| 564 | * @param p_comp_no the index of the component to output. |
| 565 | * @param p_stream the stream to write data to. |
| 566 | * @param p_j2k J2K codec. |
| 567 | * @param p_manager the user event manager. |
| 568 | */ |
| 569 | static OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k, |
| 570 | OPJ_UINT32 p_comp_no, |
| 571 | opj_stream_private_t *p_stream, |
| 572 | opj_event_mgr_t * p_manager ); |
| 573 | #endif |
| 574 | |
| 575 | #if 0 |
| 576 | /** |
| 577 | * Writes the QCC marker (quantization component) |
| 578 | * |
| 579 | * @param p_j2k J2K codec. |
| 580 | * @param p_comp_no the index of the component to output. |
| 581 | * @param p_data FIXME DOC |
| 582 | * @param p_data_written the stream to write data to. |
| 583 | * @param p_manager the user event manager. |
| 584 | */ |
| 585 | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
| 586 | OPJ_UINT32 p_comp_no, |
| 587 | OPJ_BYTE * p_data, |
| 588 | OPJ_UINT32 * p_data_written, |
| 589 | opj_event_mgr_t * p_manager ); |
| 590 | #endif |
| 591 | |
| 592 | /** |
| 593 | * Gets the maximum size taken by a qcc. |
| 594 | */ |
| 595 | static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k); |
| 596 | |
| 597 | /** |
| 598 | * Reads a QCC marker (Quantization component) |
| 599 | * @param p_header_data the data contained in the QCC box. |
| 600 | * @param p_j2k the jpeg2000 codec. |
| 601 | * @param p_header_size the size of the data contained in the QCC marker. |
| 602 | * @param p_manager the user event manager. |
| 603 | */ |
| 604 | static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, |
| 605 | OPJ_BYTE * p_header_data, |
| 606 | OPJ_UINT32 p_header_size, |
| 607 | opj_event_mgr_t * p_manager); |
| 608 | /** |
| 609 | * Writes the POC marker (Progression Order Change) |
| 610 | * |
| 611 | * @param p_stream the stream to write data to. |
| 612 | * @param p_j2k J2K codec. |
| 613 | * @param p_manager the user event manager. |
| 614 | */ |
| 615 | static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k, |
| 616 | opj_stream_private_t *p_stream, |
| 617 | opj_event_mgr_t * p_manager ); |
| 618 | /** |
| 619 | * Writes the POC marker (Progression Order Change) |
| 620 | * |
| 621 | * @param p_j2k J2K codec. |
| 622 | * @param p_data FIXME DOC |
| 623 | * @param p_data_written the stream to write data to. |
| 624 | * @param p_manager the user event manager. |
| 625 | */ |
| 626 | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
| 627 | OPJ_BYTE * p_data, |
| 628 | OPJ_UINT32 * p_data_written, |
| 629 | opj_event_mgr_t * p_manager ); |
| 630 | /** |
| 631 | * Gets the maximum size taken by the writing of a POC. |
| 632 | */ |
| 633 | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); |
| 634 | |
| 635 | /** |
| 636 | * Reads a POC marker (Progression Order Change) |
| 637 | * |
| 638 | * @param p_header_data the data contained in the POC box. |
| 639 | * @param p_j2k the jpeg2000 codec. |
| 640 | * @param p_header_size the size of the data contained in the POC marker. |
| 641 | * @param p_manager the user event manager. |
| 642 | */ |
| 643 | static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, |
| 644 | OPJ_BYTE * p_header_data, |
| 645 | OPJ_UINT32 p_header_size, |
| 646 | opj_event_mgr_t * p_manager ); |
| 647 | |
| 648 | /** |
| 649 | * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. |
| 650 | */ |
| 651 | static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k); |
| 652 | |
| 653 | /** |
| 654 | * Gets the maximum size taken by the headers of the SOT. |
| 655 | * |
| 656 | * @param p_j2k the jpeg2000 codec to use. |
| 657 | */ |
| 658 | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); |
| 659 | |
| 660 | /** |
| 661 | * Reads a CRG marker (Component registration) |
| 662 | * |
| 663 | * @param p_header_data the data contained in the TLM box. |
| 664 | * @param p_j2k the jpeg2000 codec. |
| 665 | * @param p_header_size the size of the data contained in the TLM marker. |
| 666 | * @param p_manager the user event manager. |
| 667 | */ |
| 668 | static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k, |
| 669 | OPJ_BYTE * p_header_data, |
| 670 | OPJ_UINT32 p_header_size, |
| 671 | opj_event_mgr_t * p_manager ); |
| 672 | /** |
| 673 | * Reads a TLM marker (Tile Length Marker) |
| 674 | * |
| 675 | * @param p_header_data the data contained in the TLM box. |
| 676 | * @param p_j2k the jpeg2000 codec. |
| 677 | * @param p_header_size the size of the data contained in the TLM marker. |
| 678 | * @param p_manager the user event manager. |
| 679 | */ |
| 680 | static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k, |
| 681 | OPJ_BYTE * p_header_data, |
| 682 | OPJ_UINT32 p_header_size, |
| 683 | opj_event_mgr_t * p_manager); |
| 684 | |
| 685 | /** |
| 686 | * Writes the updated tlm. |
| 687 | * |
| 688 | * @param p_stream the stream to write data to. |
| 689 | * @param p_j2k J2K codec. |
| 690 | * @param p_manager the user event manager. |
| 691 | */ |
| 692 | static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k, |
| 693 | opj_stream_private_t *p_stream, |
| 694 | opj_event_mgr_t * p_manager ); |
| 695 | |
| 696 | /** |
| 697 | * Reads a PLM marker (Packet length, main header marker) |
| 698 | * |
| 699 | * @param p_header_data the data contained in the TLM box. |
| 700 | * @param p_j2k the jpeg2000 codec. |
| 701 | * @param p_header_size the size of the data contained in the TLM marker. |
| 702 | * @param p_manager the user event manager. |
| 703 | */ |
| 704 | static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k, |
| 705 | OPJ_BYTE * p_header_data, |
| 706 | OPJ_UINT32 p_header_size, |
| 707 | opj_event_mgr_t * p_manager); |
| 708 | /** |
| 709 | * Reads a PLT marker (Packet length, tile-part header) |
| 710 | * |
| 711 | * @param p_header_data the data contained in the PLT box. |
| 712 | * @param p_j2k the jpeg2000 codec. |
| 713 | * @param p_header_size the size of the data contained in the PLT marker. |
| 714 | * @param p_manager the user event manager. |
| 715 | */ |
| 716 | static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, |
| 717 | OPJ_BYTE * p_header_data, |
| 718 | OPJ_UINT32 p_header_size, |
| 719 | opj_event_mgr_t * p_manager ); |
| 720 | |
| 721 | #if 0 |
| 722 | /** |
| 723 | * Reads a PPM marker (Packed packet headers, main header) |
| 724 | * |
| 725 | * @param p_header_data the data contained in the POC box. |
| 726 | * @param p_j2k the jpeg2000 codec. |
| 727 | * @param p_header_size the size of the data contained in the POC marker. |
| 728 | * @param p_manager the user event manager. |
| 729 | */ |
| 730 | static OPJ_BOOL j2k_read_ppm_v2 ( |
| 731 | opj_j2k_t *p_j2k, |
| 732 | OPJ_BYTE * p_header_data, |
| 733 | OPJ_UINT32 p_header_size, |
| 734 | struct opj_event_mgr * p_manager |
| 735 | ); |
| 736 | #endif |
| 737 | |
| 738 | static OPJ_BOOL j2k_read_ppm_v3 ( |
| 739 | opj_j2k_t *p_j2k, |
| 740 | OPJ_BYTE * p_header_data, |
| 741 | OPJ_UINT32 p_header_size, |
| 742 | opj_event_mgr_t * p_manager ); |
| 743 | |
| 744 | /** |
| 745 | * Reads a PPT marker (Packed packet headers, tile-part header) |
| 746 | * |
| 747 | * @param p_header_data the data contained in the PPT box. |
| 748 | * @param p_j2k the jpeg2000 codec. |
| 749 | * @param p_header_size the size of the data contained in the PPT marker. |
| 750 | * @param p_manager the user event manager. |
| 751 | */ |
| 752 | static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, |
| 753 | OPJ_BYTE * p_header_data, |
| 754 | OPJ_UINT32 p_header_size, |
| 755 | opj_event_mgr_t * p_manager ); |
| 756 | /** |
| 757 | * Writes the TLM marker (Tile Length Marker) |
| 758 | * |
| 759 | * @param p_stream the stream to write data to. |
| 760 | * @param p_j2k J2K codec. |
| 761 | * @param p_manager the user event manager. |
| 762 | */ |
| 763 | static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k, |
| 764 | opj_stream_private_t *p_stream, |
| 765 | opj_event_mgr_t * p_manager ); |
| 766 | |
| 767 | /** |
| 768 | * Writes the SOT marker (Start of tile-part) |
| 769 | * |
| 770 | * @param p_j2k J2K codec. |
| 771 | * @param p_data FIXME DOC |
| 772 | * @param p_data_written FIXME DOC |
| 773 | * @param p_stream the stream to write data to. |
| 774 | * @param p_manager the user event manager. |
| 775 | */ |
| 776 | static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k, |
| 777 | OPJ_BYTE * p_data, |
| 778 | OPJ_UINT32 * p_data_written, |
| 779 | const opj_stream_private_t *p_stream, |
| 780 | opj_event_mgr_t * p_manager ); |
| 781 | |
| 782 | /** |
| 783 | * Reads a PPT marker (Packed packet headers, tile-part header) |
| 784 | * |
| 785 | * @param p_header_data the data contained in the PPT box. |
| 786 | * @param p_j2k the jpeg2000 codec. |
| 787 | * @param p_header_size the size of the data contained in the PPT marker. |
| 788 | * @param p_manager the user event manager. |
| 789 | */ |
| 790 | static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k, |
| 791 | OPJ_BYTE * p_header_data, |
| 792 | OPJ_UINT32 p_header_size, |
| 793 | opj_event_mgr_t * p_manager ); |
| 794 | /** |
| 795 | * Writes the SOD marker (Start of data) |
| 796 | * |
| 797 | * @param p_j2k J2K codec. |
| 798 | * @param p_tile_coder FIXME DOC |
| 799 | * @param p_data FIXME DOC |
| 800 | * @param p_data_written FIXME DOC |
| 801 | * @param p_total_data_size FIXME DOC |
| 802 | * @param p_stream the stream to write data to. |
| 803 | * @param p_manager the user event manager. |
| 804 | */ |
| 805 | static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k, |
| 806 | opj_tcd_t * p_tile_coder, |
| 807 | OPJ_BYTE * p_data, |
| 808 | OPJ_UINT32 * p_data_written, |
| 809 | OPJ_UINT32 p_total_data_size, |
| 810 | const opj_stream_private_t *p_stream, |
| 811 | opj_event_mgr_t * p_manager ); |
| 812 | |
| 813 | /** |
| 814 | * Reads a SOD marker (Start Of Data) |
| 815 | * |
| 816 | * @param p_j2k the jpeg2000 codec. |
| 817 | * @param p_stream FIXME DOC |
| 818 | * @param p_manager the user event manager. |
| 819 | */ |
| 820 | static OPJ_BOOL opj_j2k_read_sod( opj_j2k_t *p_j2k, |
| 821 | opj_stream_private_t *p_stream, |
| 822 | opj_event_mgr_t * p_manager ); |
| 823 | |
| 824 | void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size ) |
| 825 | { |
| 826 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */ |
| 827 | ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; |
| 828 | |
| 829 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */ |
| 830 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Writes the RGN marker (Region Of Interest) |
| 835 | * |
| 836 | * @param p_tile_no the tile to output |
| 837 | * @param p_comp_no the component to output |
| 838 | * @param nb_comps the number of components |
| 839 | * @param p_stream the stream to write data to. |
| 840 | * @param p_j2k J2K codec. |
| 841 | * @param p_manager the user event manager. |
| 842 | */ |
| 843 | static OPJ_BOOL opj_j2k_write_rgn( opj_j2k_t *p_j2k, |
| 844 | OPJ_UINT32 p_tile_no, |
| 845 | OPJ_UINT32 p_comp_no, |
| 846 | OPJ_UINT32 nb_comps, |
| 847 | opj_stream_private_t *p_stream, |
| 848 | opj_event_mgr_t * p_manager ); |
| 849 | |
| 850 | /** |
| 851 | * Reads a RGN marker (Region Of Interest) |
| 852 | * |
| 853 | * @param p_header_data the data contained in the POC box. |
| 854 | * @param p_j2k the jpeg2000 codec. |
| 855 | * @param p_header_size the size of the data contained in the POC marker. |
| 856 | * @param p_manager the user event manager. |
| 857 | */ |
| 858 | static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k, |
| 859 | OPJ_BYTE * p_header_data, |
| 860 | OPJ_UINT32 p_header_size, |
| 861 | opj_event_mgr_t * p_manager ); |
| 862 | |
| 863 | /** |
| 864 | * Writes the EOC marker (End of Codestream) |
| 865 | * |
| 866 | * @param p_stream the stream to write data to. |
| 867 | * @param p_j2k J2K codec. |
| 868 | * @param p_manager the user event manager. |
| 869 | */ |
| 870 | static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k, |
| 871 | opj_stream_private_t *p_stream, |
| 872 | opj_event_mgr_t * p_manager ); |
| 873 | |
| 874 | #if 0 |
| 875 | /** |
| 876 | * Reads a EOC marker (End Of Codestream) |
| 877 | * |
| 878 | * @param p_j2k the jpeg2000 codec. |
| 879 | * @param p_stream FIXME DOC |
| 880 | * @param p_manager the user event manager. |
| 881 | */ |
| 882 | static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, |
| 883 | opj_stream_private_t *p_stream, |
| 884 | opj_event_mgr_t * p_manager ); |
| 885 | #endif |
| 886 | |
| 887 | /** |
| 888 | * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) |
| 889 | * |
| 890 | * @param p_stream the stream to write data to. |
| 891 | * @param p_j2k J2K codec. |
| 892 | * @param p_manager the user event manager. |
| 893 | */ |
| 894 | static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, |
| 895 | opj_stream_private_t *p_stream, |
| 896 | opj_event_mgr_t * p_manager ); |
| 897 | |
| 898 | /** |
| 899 | * Inits the Info |
| 900 | * |
| 901 | * @param p_stream the stream to write data to. |
| 902 | * @param p_j2k J2K codec. |
| 903 | * @param p_manager the user event manager. |
| 904 | */ |
| 905 | static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, |
| 906 | opj_stream_private_t *p_stream, |
| 907 | opj_event_mgr_t * p_manager ); |
| 908 | |
| 909 | /** |
| 910 | Add main header marker information |
| 911 | @param cstr_index Codestream information structure |
| 912 | @param type marker type |
| 913 | @param pos byte offset of marker segment |
| 914 | @param len length of marker segment |
| 915 | */ |
| 916 | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; |
| 917 | /** |
| 918 | Add tile header marker information |
| 919 | @param tileno tile index number |
| 920 | @param cstr_index Codestream information structure |
| 921 | @param type marker type |
| 922 | @param pos byte offset of marker segment |
| 923 | @param len length of marker segment |
| 924 | */ |
| 925 | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len); |
| 926 | |
| 927 | /** |
| 928 | * Reads an unknown marker |
| 929 | * |
| 930 | * @param p_j2k the jpeg2000 codec. |
| 931 | * @param p_stream the stream object to read from. |
| 932 | * @param output_marker FIXME DOC |
| 933 | * @param p_manager the user event manager. |
| 934 | * |
| 935 | * @return true if the marker could be deduced. |
| 936 | */ |
| 937 | static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k, |
| 938 | opj_stream_private_t *p_stream, |
| 939 | OPJ_UINT32 *output_marker, |
| 940 | opj_event_mgr_t * p_manager ); |
| 941 | |
| 942 | /** |
| 943 | * Writes the MCT marker (Multiple Component Transform) |
| 944 | * |
| 945 | * @param p_j2k J2K codec. |
| 946 | * @param p_mct_record FIXME DOC |
| 947 | * @param p_stream the stream to write data to. |
| 948 | * @param p_manager the user event manager. |
| 949 | */ |
| 950 | static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, |
| 951 | opj_mct_data_t * p_mct_record, |
| 952 | opj_stream_private_t *p_stream, |
| 953 | opj_event_mgr_t * p_manager ); |
| 954 | |
| 955 | /** |
| 956 | * Reads a MCT marker (Multiple Component Transform) |
| 957 | * |
| 958 | * @param p_header_data the data contained in the MCT box. |
| 959 | * @param p_j2k the jpeg2000 codec. |
| 960 | * @param p_header_size the size of the data contained in the MCT marker. |
| 961 | * @param p_manager the user event manager. |
| 962 | */ |
| 963 | static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, |
| 964 | OPJ_BYTE * p_header_data, |
| 965 | OPJ_UINT32 p_header_size, |
| 966 | opj_event_mgr_t * p_manager ); |
| 967 | |
| 968 | /** |
| 969 | * Writes the MCC marker (Multiple Component Collection) |
| 970 | * |
| 971 | * @param p_j2k J2K codec. |
| 972 | * @param p_mcc_record FIXME DOC |
| 973 | * @param p_stream the stream to write data to. |
| 974 | * @param p_manager the user event manager. |
| 975 | */ |
| 976 | static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, |
| 977 | opj_simple_mcc_decorrelation_data_t * p_mcc_record, |
| 978 | opj_stream_private_t *p_stream, |
| 979 | opj_event_mgr_t * p_manager ); |
| 980 | |
| 981 | /** |
| 982 | * Reads a MCC marker (Multiple Component Collection) |
| 983 | * |
| 984 | * @param p_header_data the data contained in the MCC box. |
| 985 | * @param p_j2k the jpeg2000 codec. |
| 986 | * @param p_header_size the size of the data contained in the MCC marker. |
| 987 | * @param p_manager the user event manager. |
| 988 | */ |
| 989 | static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, |
| 990 | OPJ_BYTE * p_header_data, |
| 991 | OPJ_UINT32 p_header_size, |
| 992 | opj_event_mgr_t * p_manager ); |
| 993 | |
| 994 | /** |
| 995 | * Writes the MCO marker (Multiple component transformation ordering) |
| 996 | * |
| 997 | * @param p_stream the stream to write data to. |
| 998 | * @param p_j2k J2K codec. |
| 999 | * @param p_manager the user event manager. |
| 1000 | */ |
| 1001 | static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, |
| 1002 | opj_stream_private_t *p_stream, |
| 1003 | opj_event_mgr_t * p_manager ); |
| 1004 | |
| 1005 | /** |
| 1006 | * Reads a MCO marker (Multiple Component Transform Ordering) |
| 1007 | * |
| 1008 | * @param p_header_data the data contained in the MCO box. |
| 1009 | * @param p_j2k the jpeg2000 codec. |
| 1010 | * @param p_header_size the size of the data contained in the MCO marker. |
| 1011 | * @param p_manager the user event manager. |
| 1012 | */ |
| 1013 | static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, |
| 1014 | OPJ_BYTE * p_header_data, |
| 1015 | OPJ_UINT32 p_header_size, |
| 1016 | opj_event_mgr_t * p_manager ); |
| 1017 | |
| 1018 | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index); |
| 1019 | |
| 1020 | static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1021 | static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1022 | static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1023 | static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1024 | |
| 1025 | static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1026 | static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1027 | static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1028 | static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1029 | |
| 1030 | static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1031 | static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1032 | static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1033 | static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1034 | |
| 1035 | /** |
| 1036 | * Ends the encoding, i.e. frees memory. |
| 1037 | * |
| 1038 | * @param p_stream the stream to write data to. |
| 1039 | * @param p_j2k J2K codec. |
| 1040 | * @param p_manager the user event manager. |
| 1041 | */ |
| 1042 | static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k, |
| 1043 | opj_stream_private_t *p_stream, |
| 1044 | opj_event_mgr_t * p_manager ); |
| 1045 | |
| 1046 | /** |
| 1047 | * Writes the CBD marker (Component bit depth definition) |
| 1048 | * |
| 1049 | * @param p_stream the stream to write data to. |
| 1050 | * @param p_j2k J2K codec. |
| 1051 | * @param p_manager the user event manager. |
| 1052 | */ |
| 1053 | static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, |
| 1054 | opj_stream_private_t *p_stream, |
| 1055 | opj_event_mgr_t * p_manager ); |
| 1056 | |
| 1057 | /** |
| 1058 | * Reads a CBD marker (Component bit depth definition) |
| 1059 | * @param p_header_data the data contained in the CBD box. |
| 1060 | * @param p_j2k the jpeg2000 codec. |
| 1061 | * @param p_header_size the size of the data contained in the CBD marker. |
| 1062 | * @param p_manager the user event manager. |
| 1063 | */ |
| 1064 | static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, |
| 1065 | OPJ_BYTE * p_header_data, |
| 1066 | OPJ_UINT32 p_header_size, |
| 1067 | opj_event_mgr_t * p_manager); |
| 1068 | |
| 1069 | #if 0 |
| 1070 | /** |
| 1071 | * Writes COC marker for each component. |
| 1072 | * |
| 1073 | * @param p_stream the stream to write data to. |
| 1074 | * @param p_j2k J2K codec. |
| 1075 | * @param p_manager the user event manager. |
| 1076 | */ |
| 1077 | static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k, |
| 1078 | opj_stream_private_t *p_stream, |
| 1079 | opj_event_mgr_t * p_manager ); |
| 1080 | #endif |
| 1081 | |
| 1082 | #if 0 |
| 1083 | /** |
| 1084 | * Writes QCC marker for each component. |
| 1085 | * |
| 1086 | * @param p_stream the stream to write data to. |
| 1087 | * @param p_j2k J2K codec. |
| 1088 | * @param p_manager the user event manager. |
| 1089 | */ |
| 1090 | static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k, |
| 1091 | opj_stream_private_t *p_stream, |
| 1092 | opj_event_mgr_t * p_manager ); |
| 1093 | #endif |
| 1094 | |
| 1095 | /** |
| 1096 | * Writes regions of interests. |
| 1097 | * |
| 1098 | * @param p_stream the stream to write data to. |
| 1099 | * @param p_j2k J2K codec. |
| 1100 | * @param p_manager the user event manager. |
| 1101 | */ |
| 1102 | static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, |
| 1103 | opj_stream_private_t *p_stream, |
| 1104 | opj_event_mgr_t * p_manager ); |
| 1105 | |
| 1106 | /** |
| 1107 | * Writes EPC ???? |
| 1108 | * |
| 1109 | * @param p_stream the stream to write data to. |
| 1110 | * @param p_j2k J2K codec. |
| 1111 | * @param p_manager the user event manager. |
| 1112 | */ |
| 1113 | static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, |
| 1114 | opj_stream_private_t *p_stream, |
| 1115 | opj_event_mgr_t * p_manager ); |
| 1116 | |
| 1117 | /** |
| 1118 | * Checks the progression order changes values. Tells of the poc given as input are valid. |
| 1119 | * A nice message is outputted at errors. |
| 1120 | * |
| 1121 | * @param p_pocs the progression order changes. |
| 1122 | * @param p_nb_pocs the number of progression order changes. |
| 1123 | * @param p_nb_resolutions the number of resolutions. |
| 1124 | * @param numcomps the number of components |
| 1125 | * @param numlayers the number of layers. |
| 1126 | * @param p_manager the user event manager. |
| 1127 | * |
| 1128 | * @return true if the pocs are valid. |
| 1129 | */ |
| 1130 | static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, |
| 1131 | OPJ_UINT32 p_nb_pocs, |
| 1132 | OPJ_UINT32 p_nb_resolutions, |
| 1133 | OPJ_UINT32 numcomps, |
| 1134 | OPJ_UINT32 numlayers, |
| 1135 | opj_event_mgr_t * p_manager); |
| 1136 | |
| 1137 | /** |
| 1138 | * Gets the number of tile parts used for the given change of progression (if any) and the given tile. |
| 1139 | * |
| 1140 | * @param cp the coding parameters. |
| 1141 | * @param pino the offset of the given poc (i.e. its position in the coding parameter). |
| 1142 | * @param tileno the given tile. |
| 1143 | * |
| 1144 | * @return the number of tile parts. |
| 1145 | */ |
| 1146 | static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno); |
| 1147 | |
| 1148 | /** |
| 1149 | * Calculates the total number of tile parts needed by the encoder to |
| 1150 | * encode such an image. If not enough memory is available, then the function return false. |
| 1151 | * |
| 1152 | * @param p_nb_tiles pointer that will hold the number of tile parts. |
| 1153 | * @param cp the coding parameters for the image. |
| 1154 | * @param image the image to encode. |
| 1155 | * @param p_j2k the p_j2k encoder. |
| 1156 | * @param p_manager the user event manager. |
| 1157 | * |
| 1158 | * @return true if the function was successful, false else. |
| 1159 | */ |
| 1160 | static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k, |
| 1161 | opj_cp_t *cp, |
| 1162 | OPJ_UINT32 * p_nb_tiles, |
| 1163 | opj_image_t *image, |
| 1164 | opj_event_mgr_t * p_manager); |
| 1165 | |
| 1166 | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); |
| 1167 | |
| 1168 | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); |
| 1169 | |
| 1170 | static opj_codestream_index_t* opj_j2k_create_cstr_index(void); |
| 1171 | |
| 1172 | static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp); |
| 1173 | |
| 1174 | static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp); |
| 1175 | |
| 1176 | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres); |
| 1177 | |
| 1178 | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager); |
| 1179 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 1180 | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1181 | |
| 1182 | /*@}*/ |
| 1183 | |
| 1184 | /*@}*/ |
| 1185 | |
| 1186 | /* ----------------------------------------------------------------------- */ |
| 1187 | typedef struct j2k_prog_order{ |
| 1188 | OPJ_PROG_ORDER enum_prog; |
| 1189 | char str_prog[5]; |
| 1190 | }j2k_prog_order_t; |
| 1191 | |
| 1192 | j2k_prog_order_t j2k_prog_order_list[] = { |
| 1193 | {OPJ_CPRL, "CPRL"}, |
| 1194 | {OPJ_LRCP, "LRCP"}, |
| 1195 | {OPJ_PCRL, "PCRL"}, |
| 1196 | {OPJ_RLCP, "RLCP"}, |
| 1197 | {OPJ_RPCL, "RPCL"}, |
| 1198 | {(OPJ_PROG_ORDER)-1, ""} |
| 1199 | }; |
| 1200 | |
| 1201 | /** |
| 1202 | * FIXME DOC |
| 1203 | */ |
| 1204 | static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = |
| 1205 | { |
| 1206 | 2, |
| 1207 | 4, |
| 1208 | 4, |
| 1209 | 8 |
| 1210 | }; |
| 1211 | |
| 1212 | typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); |
| 1213 | |
| 1214 | const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = |
| 1215 | { |
| 1216 | opj_j2k_read_int16_to_float, |
| 1217 | opj_j2k_read_int32_to_float, |
| 1218 | opj_j2k_read_float32_to_float, |
| 1219 | opj_j2k_read_float64_to_float |
| 1220 | }; |
| 1221 | |
| 1222 | const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = |
| 1223 | { |
| 1224 | opj_j2k_read_int16_to_int32, |
| 1225 | opj_j2k_read_int32_to_int32, |
| 1226 | opj_j2k_read_float32_to_int32, |
| 1227 | opj_j2k_read_float64_to_int32 |
| 1228 | }; |
| 1229 | |
| 1230 | const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = |
| 1231 | { |
| 1232 | opj_j2k_write_float_to_int16, |
| 1233 | opj_j2k_write_float_to_int32, |
| 1234 | opj_j2k_write_float_to_float, |
| 1235 | opj_j2k_write_float_to_float64 |
| 1236 | }; |
| 1237 | |
| 1238 | typedef struct opj_dec_memory_marker_handler |
| 1239 | { |
| 1240 | /** marker value */ |
| 1241 | OPJ_UINT32 id; |
| 1242 | /** value of the state when the marker can appear */ |
| 1243 | OPJ_UINT32 states; |
| 1244 | /** action linked to the marker */ |
| 1245 | OPJ_BOOL (*handler) ( opj_j2k_t *p_j2k, |
| 1246 | OPJ_BYTE * p_header_data, |
| 1247 | OPJ_UINT32 p_header_size, |
| 1248 | opj_event_mgr_t * p_manager ); |
| 1249 | } |
| 1250 | opj_dec_memory_marker_handler_t; |
| 1251 | |
| 1252 | const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = |
| 1253 | { |
| 1254 | {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, |
| 1255 | {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, |
| 1256 | {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, |
| 1257 | {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, |
| 1258 | {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, |
| 1259 | {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, |
| 1260 | {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, |
| 1261 | {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, |
| 1262 | {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, |
| 1263 | {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, |
| 1264 | {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, |
| 1265 | {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3}, |
| 1266 | {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, |
| 1267 | {J2K_MS_SOP, 0, 0}, |
| 1268 | {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, |
| 1269 | {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, |
| 1270 | {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, |
| 1271 | {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd}, |
| 1272 | {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, |
| 1273 | {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, |
| 1274 | #ifdef USE_JPWL |
| 1275 | #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */ |
| 1276 | {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, |
| 1277 | {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, |
| 1278 | {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, |
| 1279 | {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, |
| 1280 | #endif |
| 1281 | #endif /* USE_JPWL */ |
| 1282 | #ifdef USE_JPSEC |
| 1283 | {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, |
| 1284 | {J2K_MS_INSEC, 0, j2k_read_insec} |
| 1285 | #endif /* USE_JPSEC */ |
| 1286 | {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ |
| 1287 | }; |
| 1288 | |
| 1289 | void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1290 | { |
| 1291 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1292 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
| 1293 | OPJ_UINT32 i; |
| 1294 | OPJ_UINT32 l_temp; |
| 1295 | |
| 1296 | for (i=0;i<p_nb_elem;++i) { |
| 1297 | opj_read_bytes(l_src_data,&l_temp,2); |
| 1298 | |
| 1299 | l_src_data+=sizeof(OPJ_INT16); |
| 1300 | |
| 1301 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1306 | { |
| 1307 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1308 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
| 1309 | OPJ_UINT32 i; |
| 1310 | OPJ_UINT32 l_temp; |
| 1311 | |
| 1312 | for (i=0;i<p_nb_elem;++i) { |
| 1313 | opj_read_bytes(l_src_data,&l_temp,4); |
| 1314 | |
| 1315 | l_src_data+=sizeof(OPJ_INT32); |
| 1316 | |
| 1317 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1322 | { |
| 1323 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1324 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
| 1325 | OPJ_UINT32 i; |
| 1326 | OPJ_FLOAT32 l_temp; |
| 1327 | |
| 1328 | for (i=0;i<p_nb_elem;++i) { |
| 1329 | opj_read_float(l_src_data,&l_temp); |
| 1330 | |
| 1331 | l_src_data+=sizeof(OPJ_FLOAT32); |
| 1332 | |
| 1333 | *(l_dest_data++) = l_temp; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1338 | { |
| 1339 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1340 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
| 1341 | OPJ_UINT32 i; |
| 1342 | OPJ_FLOAT64 l_temp; |
| 1343 | |
| 1344 | for (i=0;i<p_nb_elem;++i) { |
| 1345 | opj_read_double(l_src_data,&l_temp); |
| 1346 | |
| 1347 | l_src_data+=sizeof(OPJ_FLOAT64); |
| 1348 | |
| 1349 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1354 | { |
| 1355 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1356 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
| 1357 | OPJ_UINT32 i; |
| 1358 | OPJ_UINT32 l_temp; |
| 1359 | |
| 1360 | for (i=0;i<p_nb_elem;++i) { |
| 1361 | opj_read_bytes(l_src_data,&l_temp,2); |
| 1362 | |
| 1363 | l_src_data+=sizeof(OPJ_INT16); |
| 1364 | |
| 1365 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1370 | { |
| 1371 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1372 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
| 1373 | OPJ_UINT32 i; |
| 1374 | OPJ_UINT32 l_temp; |
| 1375 | |
| 1376 | for (i=0;i<p_nb_elem;++i) { |
| 1377 | opj_read_bytes(l_src_data,&l_temp,4); |
| 1378 | |
| 1379 | l_src_data+=sizeof(OPJ_INT32); |
| 1380 | |
| 1381 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1386 | { |
| 1387 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1388 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
| 1389 | OPJ_UINT32 i; |
| 1390 | OPJ_FLOAT32 l_temp; |
| 1391 | |
| 1392 | for (i=0;i<p_nb_elem;++i) { |
| 1393 | opj_read_float(l_src_data,&l_temp); |
| 1394 | |
| 1395 | l_src_data+=sizeof(OPJ_FLOAT32); |
| 1396 | |
| 1397 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1402 | { |
| 1403 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
| 1404 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
| 1405 | OPJ_UINT32 i; |
| 1406 | OPJ_FLOAT64 l_temp; |
| 1407 | |
| 1408 | for (i=0;i<p_nb_elem;++i) { |
| 1409 | opj_read_double(l_src_data,&l_temp); |
| 1410 | |
| 1411 | l_src_data+=sizeof(OPJ_FLOAT64); |
| 1412 | |
| 1413 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1418 | { |
| 1419 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
| 1420 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
| 1421 | OPJ_UINT32 i; |
| 1422 | OPJ_UINT32 l_temp; |
| 1423 | |
| 1424 | for (i=0;i<p_nb_elem;++i) { |
| 1425 | l_temp = (OPJ_UINT32) *(l_src_data++); |
| 1426 | |
| 1427 | opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16)); |
| 1428 | |
| 1429 | l_dest_data+=sizeof(OPJ_INT16); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1434 | { |
| 1435 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
| 1436 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
| 1437 | OPJ_UINT32 i; |
| 1438 | OPJ_UINT32 l_temp; |
| 1439 | |
| 1440 | for (i=0;i<p_nb_elem;++i) { |
| 1441 | l_temp = (OPJ_UINT32) *(l_src_data++); |
| 1442 | |
| 1443 | opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32)); |
| 1444 | |
| 1445 | l_dest_data+=sizeof(OPJ_INT32); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1450 | { |
| 1451 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
| 1452 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
| 1453 | OPJ_UINT32 i; |
| 1454 | OPJ_FLOAT32 l_temp; |
| 1455 | |
| 1456 | for (i=0;i<p_nb_elem;++i) { |
| 1457 | l_temp = (OPJ_FLOAT32) *(l_src_data++); |
| 1458 | |
| 1459 | opj_write_float(l_dest_data,l_temp); |
| 1460 | |
| 1461 | l_dest_data+=sizeof(OPJ_FLOAT32); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) |
| 1466 | { |
| 1467 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
| 1468 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
| 1469 | OPJ_UINT32 i; |
| 1470 | OPJ_FLOAT64 l_temp; |
| 1471 | |
| 1472 | for (i=0;i<p_nb_elem;++i) { |
| 1473 | l_temp = (OPJ_FLOAT64) *(l_src_data++); |
| 1474 | |
| 1475 | opj_write_double(l_dest_data,l_temp); |
| 1476 | |
| 1477 | l_dest_data+=sizeof(OPJ_FLOAT64); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){ |
| 1482 | j2k_prog_order_t *po; |
| 1483 | for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ |
| 1484 | if(po->enum_prog == prg_order){ |
| 1485 | return po->str_prog; |
| 1486 | } |
| 1487 | } |
| 1488 | return po->str_prog; |
| 1489 | } |
| 1490 | |
| 1491 | OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, |
| 1492 | OPJ_UINT32 p_nb_pocs, |
| 1493 | OPJ_UINT32 p_nb_resolutions, |
| 1494 | OPJ_UINT32 p_num_comps, |
| 1495 | OPJ_UINT32 p_num_layers, |
| 1496 | opj_event_mgr_t * p_manager) |
| 1497 | { |
| 1498 | OPJ_UINT32* packet_array; |
| 1499 | OPJ_UINT32 index , resno, compno, layno; |
| 1500 | OPJ_UINT32 i; |
| 1501 | OPJ_UINT32 step_c = 1; |
| 1502 | OPJ_UINT32 step_r = p_num_comps * step_c; |
| 1503 | OPJ_UINT32 step_l = p_nb_resolutions * step_r; |
| 1504 | OPJ_BOOL loss = OPJ_FALSE; |
| 1505 | OPJ_UINT32 layno0 = 0; |
| 1506 | |
| 1507 | packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); |
| 1508 | if (packet_array == 00) { |
| 1509 | opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n"); |
| 1510 | return OPJ_FALSE; |
| 1511 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1512 | |
| 1513 | if (p_nb_pocs == 0) { |
| 1514 | opj_free(packet_array); |
| 1515 | return OPJ_TRUE; |
| 1516 | } |
| 1517 | |
| 1518 | index = step_r * p_pocs->resno0; |
| 1519 | /* take each resolution for each poc */ |
| 1520 | for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) |
| 1521 | { |
| 1522 | OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; |
| 1523 | |
| 1524 | /* take each comp of each resolution for each poc */ |
| 1525 | for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { |
| 1526 | OPJ_UINT32 comp_index = res_index + layno0 * step_l; |
| 1527 | |
| 1528 | /* and finally take each layer of each res of ... */ |
| 1529 | for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { |
| 1530 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
| 1531 | packet_array[comp_index] = 1; |
| 1532 | comp_index += step_l; |
| 1533 | } |
| 1534 | |
| 1535 | res_index += step_c; |
| 1536 | } |
| 1537 | |
| 1538 | index += step_r; |
| 1539 | } |
| 1540 | ++p_pocs; |
| 1541 | |
| 1542 | /* iterate through all the pocs */ |
| 1543 | for (i = 1; i < p_nb_pocs ; ++i) { |
| 1544 | OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ; |
| 1545 | |
| 1546 | layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0; |
| 1547 | index = step_r * p_pocs->resno0; |
| 1548 | |
| 1549 | /* take each resolution for each poc */ |
| 1550 | for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { |
| 1551 | OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; |
| 1552 | |
| 1553 | /* take each comp of each resolution for each poc */ |
| 1554 | for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { |
| 1555 | OPJ_UINT32 comp_index = res_index + layno0 * step_l; |
| 1556 | |
| 1557 | /* and finally take each layer of each res of ... */ |
| 1558 | for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { |
| 1559 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
| 1560 | packet_array[comp_index] = 1; |
| 1561 | comp_index += step_l; |
| 1562 | } |
| 1563 | |
| 1564 | res_index += step_c; |
| 1565 | } |
| 1566 | |
| 1567 | index += step_r; |
| 1568 | } |
| 1569 | |
| 1570 | ++p_pocs; |
| 1571 | } |
| 1572 | |
| 1573 | index = 0; |
| 1574 | for (layno = 0; layno < p_num_layers ; ++layno) { |
| 1575 | for (resno = 0; resno < p_nb_resolutions; ++resno) { |
| 1576 | for (compno = 0; compno < p_num_comps; ++compno) { |
| 1577 | loss |= (packet_array[index]!=1); |
| 1578 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
| 1579 | index += step_c; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | if (loss) { |
| 1585 | opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n"); |
| 1586 | } |
| 1587 | |
| 1588 | opj_free(packet_array); |
| 1589 | |
| 1590 | return !loss; |
| 1591 | } |
| 1592 | |
| 1593 | /* ----------------------------------------------------------------------- */ |
| 1594 | |
| 1595 | OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno) |
| 1596 | { |
| 1597 | const OPJ_CHAR *prog = 00; |
| 1598 | OPJ_INT32 i; |
| 1599 | OPJ_UINT32 tpnum = 1; |
| 1600 | opj_tcp_t *tcp = 00; |
| 1601 | opj_poc_t * l_current_poc = 00; |
| 1602 | |
| 1603 | /* preconditions */ |
| 1604 | assert(tileno < (cp->tw * cp->th)); |
| 1605 | assert(pino < (cp->tcps[tileno].numpocs + 1)); |
| 1606 | |
| 1607 | /* get the given tile coding parameter */ |
| 1608 | tcp = &cp->tcps[tileno]; |
| 1609 | assert(tcp != 00); |
| 1610 | |
| 1611 | l_current_poc = &(tcp->pocs[pino]); |
| 1612 | assert(l_current_poc != 0); |
| 1613 | |
| 1614 | /* get the progression order as a character string */ |
| 1615 | prog = opj_j2k_convert_progression_order(tcp->prg); |
| 1616 | assert(strlen(prog) > 0); |
| 1617 | |
| 1618 | if (cp->m_specific_param.m_enc.m_tp_on == 1) { |
| 1619 | for (i=0;i<4;++i) { |
| 1620 | switch (prog[i]) |
| 1621 | { |
| 1622 | /* component wise */ |
| 1623 | case 'C': |
| 1624 | tpnum *= l_current_poc->compE; |
| 1625 | break; |
| 1626 | /* resolution wise */ |
| 1627 | case 'R': |
| 1628 | tpnum *= l_current_poc->resE; |
| 1629 | break; |
| 1630 | /* precinct wise */ |
| 1631 | case 'P': |
| 1632 | tpnum *= l_current_poc->prcE; |
| 1633 | break; |
| 1634 | /* layer wise */ |
| 1635 | case 'L': |
| 1636 | tpnum *= l_current_poc->layE; |
| 1637 | break; |
| 1638 | } |
| 1639 | /* whould we split here ? */ |
| 1640 | if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) { |
| 1641 | cp->m_specific_param.m_enc.m_tp_pos=i; |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | else { |
| 1647 | tpnum=1; |
| 1648 | } |
| 1649 | |
| 1650 | return tpnum; |
| 1651 | } |
| 1652 | |
| 1653 | OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k, |
| 1654 | opj_cp_t *cp, |
| 1655 | OPJ_UINT32 * p_nb_tiles, |
| 1656 | opj_image_t *image, |
| 1657 | opj_event_mgr_t * p_manager |
| 1658 | ) |
| 1659 | { |
| 1660 | OPJ_UINT32 pino,tileno; |
| 1661 | OPJ_UINT32 l_nb_tiles; |
| 1662 | opj_tcp_t *tcp; |
| 1663 | |
| 1664 | /* preconditions */ |
| 1665 | assert(p_nb_tiles != 00); |
| 1666 | assert(cp != 00); |
| 1667 | assert(image != 00); |
| 1668 | assert(p_j2k != 00); |
| 1669 | assert(p_manager != 00); |
| 1670 | |
| 1671 | l_nb_tiles = cp->tw * cp->th; |
| 1672 | * p_nb_tiles = 0; |
| 1673 | tcp = cp->tcps; |
| 1674 | |
| 1675 | /* INDEX >> */ |
| 1676 | /* TODO mergeV2: check this part which use cstr_info */ |
| 1677 | /*if (p_j2k->cstr_info) { |
| 1678 | opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; |
| 1679 | |
| 1680 | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
| 1681 | OPJ_UINT32 cur_totnum_tp = 0; |
| 1682 | |
| 1683 | opj_pi_update_encoding_parameters(image,cp,tileno); |
| 1684 | |
| 1685 | for (pino = 0; pino <= tcp->numpocs; ++pino) |
| 1686 | { |
| 1687 | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); |
| 1688 | |
| 1689 | *p_nb_tiles = *p_nb_tiles + tp_num; |
| 1690 | |
| 1691 | cur_totnum_tp += tp_num; |
| 1692 | } |
| 1693 | |
| 1694 | tcp->m_nb_tile_parts = cur_totnum_tp; |
| 1695 | |
| 1696 | l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); |
| 1697 | if (l_info_tile_ptr->tp == 00) { |
| 1698 | return OPJ_FALSE; |
| 1699 | } |
| 1700 | |
| 1701 | memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); |
| 1702 | |
| 1703 | l_info_tile_ptr->num_tps = cur_totnum_tp; |
| 1704 | |
| 1705 | ++l_info_tile_ptr; |
| 1706 | ++tcp; |
| 1707 | } |
| 1708 | } |
| 1709 | else */{ |
| 1710 | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
| 1711 | OPJ_UINT32 cur_totnum_tp = 0; |
| 1712 | |
| 1713 | opj_pi_update_encoding_parameters(image,cp,tileno); |
| 1714 | |
| 1715 | for (pino = 0; pino <= tcp->numpocs; ++pino) { |
| 1716 | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); |
| 1717 | |
| 1718 | *p_nb_tiles = *p_nb_tiles + tp_num; |
| 1719 | |
| 1720 | cur_totnum_tp += tp_num; |
| 1721 | } |
| 1722 | tcp->m_nb_tile_parts = cur_totnum_tp; |
| 1723 | |
| 1724 | ++tcp; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | return OPJ_TRUE; |
| 1729 | } |
| 1730 | |
| 1731 | OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k, |
| 1732 | opj_stream_private_t *p_stream, |
| 1733 | opj_event_mgr_t * p_manager ) |
| 1734 | { |
| 1735 | /* 2 bytes will be written */ |
| 1736 | OPJ_BYTE * l_start_stream = 00; |
| 1737 | |
| 1738 | /* preconditions */ |
| 1739 | assert(p_stream != 00); |
| 1740 | assert(p_j2k != 00); |
| 1741 | assert(p_manager != 00); |
| 1742 | |
| 1743 | l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 1744 | |
| 1745 | /* write SOC identifier */ |
| 1746 | opj_write_bytes(l_start_stream,J2K_MS_SOC,2); |
| 1747 | |
| 1748 | if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) { |
| 1749 | return OPJ_FALSE; |
| 1750 | } |
| 1751 | |
| 1752 | /* UniPG>> */ |
| 1753 | #ifdef USE_JPWL |
| 1754 | /* update markers struct */ |
| 1755 | /* |
| 1756 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); |
| 1757 | */ |
| 1758 | assert( 0 && "TODO" ); |
| 1759 | #endif /* USE_JPWL */ |
| 1760 | /* <<UniPG */ |
| 1761 | |
| 1762 | return OPJ_TRUE; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * Reads a SOC marker (Start of Codestream) |
| 1767 | * @param p_j2k the jpeg2000 file codec. |
| 1768 | * @param p_stream FIXME DOC |
| 1769 | * @param p_manager the user event manager. |
| 1770 | */ |
| 1771 | static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, |
| 1772 | opj_stream_private_t *p_stream, |
| 1773 | opj_event_mgr_t * p_manager |
| 1774 | ) |
| 1775 | { |
| 1776 | OPJ_BYTE l_data [2]; |
| 1777 | OPJ_UINT32 l_marker; |
| 1778 | |
| 1779 | /* preconditions */ |
| 1780 | assert(p_j2k != 00); |
| 1781 | assert(p_manager != 00); |
| 1782 | assert(p_stream != 00); |
| 1783 | |
| 1784 | if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { |
| 1785 | return OPJ_FALSE; |
| 1786 | } |
| 1787 | |
| 1788 | opj_read_bytes(l_data,&l_marker,2); |
| 1789 | if (l_marker != J2K_MS_SOC) { |
| 1790 | return OPJ_FALSE; |
| 1791 | } |
| 1792 | |
| 1793 | /* Next marker should be a SIZ marker in the main header */ |
| 1794 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; |
| 1795 | |
| 1796 | /* FIXME move it in a index structure included in p_j2k*/ |
| 1797 | p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; |
| 1798 | |
| 1799 | opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start); |
| 1800 | |
| 1801 | /* Add the marker to the codestream index*/ |
| 1802 | if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) { |
| 1803 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
| 1804 | return OPJ_FALSE; |
| 1805 | } |
| 1806 | return OPJ_TRUE; |
| 1807 | } |
| 1808 | |
| 1809 | OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, |
| 1810 | opj_stream_private_t *p_stream, |
| 1811 | opj_event_mgr_t * p_manager ) |
| 1812 | { |
| 1813 | OPJ_UINT32 i; |
| 1814 | OPJ_UINT32 l_size_len; |
| 1815 | OPJ_BYTE * l_current_ptr; |
| 1816 | opj_image_t * l_image = 00; |
| 1817 | opj_cp_t *cp = 00; |
| 1818 | opj_image_comp_t * l_img_comp = 00; |
| 1819 | |
| 1820 | /* preconditions */ |
| 1821 | assert(p_stream != 00); |
| 1822 | assert(p_j2k != 00); |
| 1823 | assert(p_manager != 00); |
| 1824 | |
| 1825 | l_image = p_j2k->m_private_image; |
| 1826 | cp = &(p_j2k->m_cp); |
| 1827 | l_size_len = 40 + 3 * l_image->numcomps; |
| 1828 | l_img_comp = l_image->comps; |
| 1829 | |
| 1830 | if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 1831 | |
| 1832 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); |
| 1833 | if (! new_header_tile_data) { |
| 1834 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 1835 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 1836 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 1837 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n"); |
| 1838 | return OPJ_FALSE; |
| 1839 | } |
| 1840 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 1841 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; |
| 1842 | } |
| 1843 | |
| 1844 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 1845 | |
| 1846 | /* write SOC identifier */ |
| 1847 | opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */ |
| 1848 | l_current_ptr+=2; |
| 1849 | |
| 1850 | opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */ |
| 1851 | l_current_ptr+=2; |
| 1852 | |
| 1853 | opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ |
| 1854 | l_current_ptr+=2; |
| 1855 | |
| 1856 | opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ |
| 1857 | l_current_ptr+=4; |
| 1858 | |
| 1859 | opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ |
| 1860 | l_current_ptr+=4; |
| 1861 | |
| 1862 | opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ |
| 1863 | l_current_ptr+=4; |
| 1864 | |
| 1865 | opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ |
| 1866 | l_current_ptr+=4; |
| 1867 | |
| 1868 | opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ |
| 1869 | l_current_ptr+=4; |
| 1870 | |
| 1871 | opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ |
| 1872 | l_current_ptr+=4; |
| 1873 | |
| 1874 | opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ |
| 1875 | l_current_ptr+=4; |
| 1876 | |
| 1877 | opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ |
| 1878 | l_current_ptr+=4; |
| 1879 | |
| 1880 | opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ |
| 1881 | l_current_ptr+=2; |
| 1882 | |
| 1883 | for (i = 0; i < l_image->numcomps; ++i) { |
| 1884 | /* TODO here with MCT ? */ |
| 1885 | opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */ |
| 1886 | ++l_current_ptr; |
| 1887 | |
| 1888 | opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ |
| 1889 | ++l_current_ptr; |
| 1890 | |
| 1891 | opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ |
| 1892 | ++l_current_ptr; |
| 1893 | |
| 1894 | ++l_img_comp; |
| 1895 | } |
| 1896 | |
| 1897 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) { |
| 1898 | return OPJ_FALSE; |
| 1899 | } |
| 1900 | |
| 1901 | return OPJ_TRUE; |
| 1902 | } |
| 1903 | |
| 1904 | /** |
| 1905 | * Reads a SIZ marker (image and tile size) |
| 1906 | * @param p_j2k the jpeg2000 file codec. |
| 1907 | * @param p_header_data the data contained in the SIZ box. |
| 1908 | * @param p_header_size the size of the data contained in the SIZ marker. |
| 1909 | * @param p_manager the user event manager. |
| 1910 | */ |
| 1911 | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
| 1912 | OPJ_BYTE * p_header_data, |
| 1913 | OPJ_UINT32 p_header_size, |
| 1914 | opj_event_mgr_t * p_manager |
| 1915 | ) |
| 1916 | { |
| 1917 | OPJ_UINT32 i; |
| 1918 | OPJ_UINT32 l_nb_comp; |
| 1919 | OPJ_UINT32 l_nb_comp_remain; |
| 1920 | OPJ_UINT32 l_remaining_size; |
| 1921 | OPJ_UINT32 l_nb_tiles; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 1922 | OPJ_UINT32 l_tmp, l_tx1, l_ty1; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1923 | opj_image_t *l_image = 00; |
| 1924 | opj_cp_t *l_cp = 00; |
| 1925 | opj_image_comp_t * l_img_comp = 00; |
| 1926 | opj_tcp_t * l_current_tile_param = 00; |
| 1927 | |
| 1928 | /* preconditions */ |
| 1929 | assert(p_j2k != 00); |
| 1930 | assert(p_manager != 00); |
| 1931 | assert(p_header_data != 00); |
| 1932 | |
| 1933 | l_image = p_j2k->m_private_image; |
| 1934 | l_cp = &(p_j2k->m_cp); |
| 1935 | |
| 1936 | /* minimum size == 39 - 3 (= minimum component parameter) */ |
| 1937 | if (p_header_size < 36) { |
| 1938 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
| 1939 | return OPJ_FALSE; |
| 1940 | } |
| 1941 | |
| 1942 | l_remaining_size = p_header_size - 36; |
| 1943 | l_nb_comp = l_remaining_size / 3; |
| 1944 | l_nb_comp_remain = l_remaining_size % 3; |
| 1945 | if (l_nb_comp_remain != 0){ |
| 1946 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); |
| 1947 | return OPJ_FALSE; |
| 1948 | } |
| 1949 | |
| 1950 | opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ |
| 1951 | p_header_data+=2; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 1952 | l_cp->rsiz = (OPJ_UINT16) l_tmp; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1953 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ |
| 1954 | p_header_data+=4; |
| 1955 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ |
| 1956 | p_header_data+=4; |
| 1957 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ |
| 1958 | p_header_data+=4; |
| 1959 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ |
| 1960 | p_header_data+=4; |
| 1961 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */ |
| 1962 | p_header_data+=4; |
| 1963 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */ |
| 1964 | p_header_data+=4; |
| 1965 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */ |
| 1966 | p_header_data+=4; |
| 1967 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */ |
| 1968 | p_header_data+=4; |
| 1969 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */ |
| 1970 | p_header_data+=2; |
| 1971 | if (l_tmp < 16385) |
| 1972 | l_image->numcomps = (OPJ_UINT16) l_tmp; |
| 1973 | else { |
| 1974 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); |
| 1975 | return OPJ_FALSE; |
| 1976 | } |
| 1977 | |
| 1978 | if (l_image->numcomps != l_nb_comp) { |
| 1979 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp); |
| 1980 | return OPJ_FALSE; |
| 1981 | } |
| 1982 | |
| 1983 | /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 1984 | /* testcase issue427-null-image-size.jp2 */ |
| 1985 | if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { |
| 1986 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 1987 | return OPJ_FALSE; |
| 1988 | } |
| 1989 | /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ |
| 1990 | if (!(l_cp->tdx * l_cp->tdy)) { |
| 1991 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy); |
| 1992 | return OPJ_FALSE; |
| 1993 | } |
| 1994 | |
| 1995 | /* testcase 1610.pdf.SIGSEGV.59c.681 */ |
| 1996 | if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) { |
| 1997 | opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1); |
| 1998 | return OPJ_FALSE; |
| 1999 | } |
| 2000 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2001 | /* testcase issue427-illegal-tile-offset.jp2 */ |
| 2002 | l_tx1 = l_cp->tx0 + l_cp->tdx; |
| 2003 | if (l_tx1 < l_cp->tx0) { /* manage overflow */ |
| 2004 | l_tx1 = 0xFFFFFFFFU; |
| 2005 | } |
| 2006 | l_ty1 = l_cp->ty0 + l_cp->tdy; |
| 2007 | if (l_ty1 < l_cp->ty0) { /* manage overflow */ |
| 2008 | l_ty1 = 0xFFFFFFFFU; |
| 2009 | } |
| 2010 | if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0) ) { |
| 2011 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: illegal tile offset\n"); |
| 2012 | return OPJ_FALSE; |
| 2013 | } |
| 2014 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2015 | #ifdef USE_JPWL |
| 2016 | if (l_cp->correct) { |
| 2017 | /* if JPWL is on, we check whether TX errors have damaged |
| 2018 | too much the SIZ parameters */ |
| 2019 | if (!(l_image->x1 * l_image->y1)) { |
| 2020 | opj_event_msg(p_manager, EVT_ERROR, |
| 2021 | "JPWL: bad image size (%d x %d)\n", |
| 2022 | l_image->x1, l_image->y1); |
| 2023 | if (!JPWL_ASSUME || JPWL_ASSUME) { |
| 2024 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2025 | return OPJ_FALSE; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? |
| 2030 | if (l_image->numcomps != ((len - 38) / 3)) { |
| 2031 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
| 2032 | "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", |
| 2033 | l_image->numcomps, ((len - 38) / 3)); |
| 2034 | if (!JPWL_ASSUME) { |
| 2035 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2036 | return OPJ_FALSE; |
| 2037 | } |
| 2038 | */ /* we try to correct */ |
| 2039 | /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"); |
| 2040 | if (l_image->numcomps < ((len - 38) / 3)) { |
| 2041 | len = 38 + 3 * l_image->numcomps; |
| 2042 | opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", |
| 2043 | len); |
| 2044 | } else { |
| 2045 | l_image->numcomps = ((len - 38) / 3); |
| 2046 | opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", |
| 2047 | l_image->numcomps); |
| 2048 | } |
| 2049 | } |
| 2050 | */ |
| 2051 | |
| 2052 | /* update components number in the jpwl_exp_comps filed */ |
| 2053 | l_cp->exp_comps = l_image->numcomps; |
| 2054 | } |
| 2055 | #endif /* USE_JPWL */ |
| 2056 | |
| 2057 | /* Allocate the resulting image components */ |
| 2058 | l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t)); |
| 2059 | if (l_image->comps == 00){ |
| 2060 | l_image->numcomps = 0; |
| 2061 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2062 | return OPJ_FALSE; |
| 2063 | } |
| 2064 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2065 | l_img_comp = l_image->comps; |
| 2066 | |
| 2067 | /* Read the component information */ |
| 2068 | for (i = 0; i < l_image->numcomps; ++i){ |
| 2069 | OPJ_UINT32 tmp; |
| 2070 | opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */ |
| 2071 | ++p_header_data; |
| 2072 | l_img_comp->prec = (tmp & 0x7f) + 1; |
| 2073 | l_img_comp->sgnd = tmp >> 7; |
| 2074 | opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */ |
| 2075 | ++p_header_data; |
| 2076 | l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
| 2077 | opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */ |
| 2078 | ++p_header_data; |
| 2079 | l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
| 2080 | if( l_img_comp->dx < 1 || l_img_comp->dx > 255 || |
| 2081 | l_img_comp->dy < 1 || l_img_comp->dy > 255 ) { |
| 2082 | opj_event_msg(p_manager, EVT_ERROR, |
| 2083 | "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)", |
| 2084 | i, l_img_comp->dx, l_img_comp->dy); |
| 2085 | return OPJ_FALSE; |
| 2086 | } |
| 2087 | |
| 2088 | #ifdef USE_JPWL |
| 2089 | if (l_cp->correct) { |
| 2090 | /* if JPWL is on, we check whether TX errors have damaged |
| 2091 | too much the SIZ parameters, again */ |
| 2092 | if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { |
| 2093 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
| 2094 | "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", |
| 2095 | i, i, l_image->comps[i].dx, l_image->comps[i].dy); |
| 2096 | if (!JPWL_ASSUME) { |
| 2097 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2098 | return OPJ_FALSE; |
| 2099 | } |
| 2100 | /* we try to correct */ |
| 2101 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); |
| 2102 | if (!l_image->comps[i].dx) { |
| 2103 | l_image->comps[i].dx = 1; |
| 2104 | opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", |
| 2105 | i, l_image->comps[i].dx); |
| 2106 | } |
| 2107 | if (!l_image->comps[i].dy) { |
| 2108 | l_image->comps[i].dy = 1; |
| 2109 | opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", |
| 2110 | i, l_image->comps[i].dy); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | #endif /* USE_JPWL */ |
| 2115 | l_img_comp->resno_decoded = 0; /* number of resolution decoded */ |
| 2116 | l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ |
| 2117 | ++l_img_comp; |
| 2118 | } |
| 2119 | |
| 2120 | /* Compute the number of tiles */ |
| 2121 | l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx); |
| 2122 | l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy); |
| 2123 | |
| 2124 | /* Check that the number of tiles is valid */ |
| 2125 | if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { |
| 2126 | opj_event_msg( p_manager, EVT_ERROR, |
| 2127 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", |
| 2128 | l_cp->tw, l_cp->th); |
| 2129 | return OPJ_FALSE; |
| 2130 | } |
| 2131 | l_nb_tiles = l_cp->tw * l_cp->th; |
| 2132 | |
| 2133 | /* Define the tiles which will be decoded */ |
| 2134 | if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { |
| 2135 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; |
| 2136 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; |
| 2137 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx); |
| 2138 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy); |
| 2139 | } |
| 2140 | else { |
| 2141 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
| 2142 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
| 2143 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
| 2144 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
| 2145 | } |
| 2146 | |
| 2147 | #ifdef USE_JPWL |
| 2148 | if (l_cp->correct) { |
| 2149 | /* if JPWL is on, we check whether TX errors have damaged |
| 2150 | too much the SIZ parameters */ |
| 2151 | if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) { |
| 2152 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
| 2153 | "JPWL: bad number of tiles (%d x %d)\n", |
| 2154 | l_cp->tw, l_cp->th); |
| 2155 | if (!JPWL_ASSUME) { |
| 2156 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2157 | return OPJ_FALSE; |
| 2158 | } |
| 2159 | /* we try to correct */ |
| 2160 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); |
| 2161 | if (l_cp->tw < 1) { |
| 2162 | l_cp->tw= 1; |
| 2163 | opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", |
| 2164 | l_cp->tw); |
| 2165 | } |
| 2166 | if (l_cp->tw > l_cp->max_tiles) { |
| 2167 | l_cp->tw= 1; |
| 2168 | opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n" |
| 2169 | "- setting %d tiles in x => HYPOTHESIS!!!\n", |
| 2170 | l_cp->max_tiles, l_cp->tw); |
| 2171 | } |
| 2172 | if (l_cp->th < 1) { |
| 2173 | l_cp->th= 1; |
| 2174 | opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", |
| 2175 | l_cp->th); |
| 2176 | } |
| 2177 | if (l_cp->th > l_cp->max_tiles) { |
| 2178 | l_cp->th= 1; |
| 2179 | opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", |
| 2180 | "- setting %d tiles in y => HYPOTHESIS!!!\n", |
| 2181 | l_cp->max_tiles, l_cp->th); |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | #endif /* USE_JPWL */ |
| 2186 | |
| 2187 | /* memory allocations */ |
| 2188 | l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); |
| 2189 | if (l_cp->tcps == 00) { |
| 2190 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2191 | return OPJ_FALSE; |
| 2192 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2193 | |
| 2194 | #ifdef USE_JPWL |
| 2195 | if (l_cp->correct) { |
| 2196 | if (!l_cp->tcps) { |
| 2197 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
| 2198 | "JPWL: could not alloc tcps field of cp\n"); |
| 2199 | if (!JPWL_ASSUME || JPWL_ASSUME) { |
| 2200 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2201 | return OPJ_FALSE; |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | #endif /* USE_JPWL */ |
| 2206 | |
| 2207 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = |
| 2208 | (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
| 2209 | if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { |
| 2210 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2211 | return OPJ_FALSE; |
| 2212 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2213 | |
| 2214 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2215 | (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS ,sizeof(opj_mct_data_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2216 | |
| 2217 | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { |
| 2218 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2219 | return OPJ_FALSE; |
| 2220 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2221 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
| 2222 | |
| 2223 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = |
| 2224 | (opj_simple_mcc_decorrelation_data_t*) |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2225 | opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, sizeof(opj_simple_mcc_decorrelation_data_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2226 | |
| 2227 | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { |
| 2228 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2229 | return OPJ_FALSE; |
| 2230 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2231 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
| 2232 | |
| 2233 | /* set up default dc level shift */ |
| 2234 | for (i=0;i<l_image->numcomps;++i) { |
| 2235 | if (! l_image->comps[i].sgnd) { |
| 2236 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1); |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | l_current_tile_param = l_cp->tcps; |
| 2241 | for (i = 0; i < l_nb_tiles; ++i) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2242 | l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2243 | if (l_current_tile_param->tccps == 00) { |
| 2244 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); |
| 2245 | return OPJ_FALSE; |
| 2246 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2247 | |
| 2248 | ++l_current_tile_param; |
| 2249 | } |
| 2250 | |
| 2251 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */ |
| 2252 | opj_image_comp_header_update(l_image,l_cp); |
| 2253 | |
| 2254 | return OPJ_TRUE; |
| 2255 | } |
| 2256 | |
| 2257 | OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k, |
| 2258 | opj_stream_private_t *p_stream, |
| 2259 | opj_event_mgr_t * p_manager |
| 2260 | ) |
| 2261 | { |
| 2262 | OPJ_UINT32 l_comment_size; |
| 2263 | OPJ_UINT32 l_total_com_size; |
| 2264 | const OPJ_CHAR *l_comment; |
| 2265 | OPJ_BYTE * l_current_ptr = 00; |
| 2266 | |
| 2267 | /* preconditions */ |
| 2268 | assert(p_j2k != 00); |
| 2269 | assert(p_stream != 00); |
| 2270 | assert(p_manager != 00); |
| 2271 | |
| 2272 | l_comment = p_j2k->m_cp.comment; |
| 2273 | l_comment_size = (OPJ_UINT32)strlen(l_comment); |
| 2274 | l_total_com_size = l_comment_size + 6; |
| 2275 | |
| 2276 | if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2277 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); |
| 2278 | if (! new_header_tile_data) { |
| 2279 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2280 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 2281 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 2282 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n"); |
| 2283 | return OPJ_FALSE; |
| 2284 | } |
| 2285 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 2286 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; |
| 2287 | } |
| 2288 | |
| 2289 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 2290 | |
| 2291 | opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */ |
| 2292 | l_current_ptr+=2; |
| 2293 | |
| 2294 | opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */ |
| 2295 | l_current_ptr+=2; |
| 2296 | |
| 2297 | opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */ |
| 2298 | l_current_ptr+=2; |
| 2299 | |
| 2300 | memcpy( l_current_ptr,l_comment,l_comment_size); |
| 2301 | |
| 2302 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) { |
| 2303 | return OPJ_FALSE; |
| 2304 | } |
| 2305 | |
| 2306 | return OPJ_TRUE; |
| 2307 | } |
| 2308 | |
| 2309 | /** |
| 2310 | * Reads a COM marker (comments) |
| 2311 | * @param p_j2k the jpeg2000 file codec. |
| 2312 | * @param p_header_data the data contained in the COM box. |
| 2313 | * @param p_header_size the size of the data contained in the COM marker. |
| 2314 | * @param p_manager the user event manager. |
| 2315 | */ |
| 2316 | static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k, |
| 2317 | OPJ_BYTE * p_header_data, |
| 2318 | OPJ_UINT32 p_header_size, |
| 2319 | opj_event_mgr_t * p_manager |
| 2320 | ) |
| 2321 | { |
| 2322 | /* preconditions */ |
| 2323 | assert(p_j2k != 00); |
| 2324 | assert(p_manager != 00); |
| 2325 | assert(p_header_data != 00); |
| 2326 | (void)p_header_size; |
| 2327 | |
| 2328 | return OPJ_TRUE; |
| 2329 | } |
| 2330 | |
| 2331 | OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k, |
| 2332 | opj_stream_private_t *p_stream, |
| 2333 | opj_event_mgr_t * p_manager ) |
| 2334 | { |
| 2335 | opj_cp_t *l_cp = 00; |
| 2336 | opj_tcp_t *l_tcp = 00; |
| 2337 | OPJ_UINT32 l_code_size,l_remaining_size; |
| 2338 | OPJ_BYTE * l_current_data = 00; |
| 2339 | |
| 2340 | /* preconditions */ |
| 2341 | assert(p_j2k != 00); |
| 2342 | assert(p_manager != 00); |
| 2343 | assert(p_stream != 00); |
| 2344 | |
| 2345 | l_cp = &(p_j2k->m_cp); |
| 2346 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
| 2347 | l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0); |
| 2348 | l_remaining_size = l_code_size; |
| 2349 | |
| 2350 | if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2351 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); |
| 2352 | if (! new_header_tile_data) { |
| 2353 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2354 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 2355 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 2356 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n"); |
| 2357 | return OPJ_FALSE; |
| 2358 | } |
| 2359 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 2360 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; |
| 2361 | } |
| 2362 | |
| 2363 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 2364 | |
| 2365 | opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */ |
| 2366 | l_current_data += 2; |
| 2367 | |
| 2368 | opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */ |
| 2369 | l_current_data += 2; |
| 2370 | |
| 2371 | opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */ |
| 2372 | ++l_current_data; |
| 2373 | |
| 2374 | opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */ |
| 2375 | ++l_current_data; |
| 2376 | |
| 2377 | opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */ |
| 2378 | l_current_data+=2; |
| 2379 | |
| 2380 | opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */ |
| 2381 | ++l_current_data; |
| 2382 | |
| 2383 | l_remaining_size -= 9; |
| 2384 | |
| 2385 | if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { |
| 2386 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); |
| 2387 | return OPJ_FALSE; |
| 2388 | } |
| 2389 | |
| 2390 | if (l_remaining_size != 0) { |
| 2391 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); |
| 2392 | return OPJ_FALSE; |
| 2393 | } |
| 2394 | |
| 2395 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) { |
| 2396 | return OPJ_FALSE; |
| 2397 | } |
| 2398 | |
| 2399 | return OPJ_TRUE; |
| 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * Reads a COD marker (Coding Styke defaults) |
| 2404 | * @param p_header_data the data contained in the COD box. |
| 2405 | * @param p_j2k the jpeg2000 codec. |
| 2406 | * @param p_header_size the size of the data contained in the COD marker. |
| 2407 | * @param p_manager the user event manager. |
| 2408 | */ |
| 2409 | static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, |
| 2410 | OPJ_BYTE * p_header_data, |
| 2411 | OPJ_UINT32 p_header_size, |
| 2412 | opj_event_mgr_t * p_manager |
| 2413 | ) |
| 2414 | { |
| 2415 | /* loop */ |
| 2416 | OPJ_UINT32 i; |
| 2417 | OPJ_UINT32 l_tmp; |
| 2418 | opj_cp_t *l_cp = 00; |
| 2419 | opj_tcp_t *l_tcp = 00; |
| 2420 | opj_image_t *l_image = 00; |
| 2421 | |
| 2422 | /* preconditions */ |
| 2423 | assert(p_header_data != 00); |
| 2424 | assert(p_j2k != 00); |
| 2425 | assert(p_manager != 00); |
| 2426 | |
| 2427 | l_image = p_j2k->m_private_image; |
| 2428 | l_cp = &(p_j2k->m_cp); |
| 2429 | |
| 2430 | /* If we are in the first tile-part header of the current tile */ |
| 2431 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
| 2432 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 2433 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2434 | |
| 2435 | /* Only one COD per tile */ |
| 2436 | if (l_tcp->cod) { |
| 2437 | opj_event_msg(p_manager, EVT_ERROR, "COD marker already read. No more than one COD marker per tile.\n"); |
| 2438 | return OPJ_FALSE; |
| 2439 | } |
| 2440 | l_tcp->cod = 1; |
| 2441 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2442 | /* Make sure room is sufficient */ |
| 2443 | if (p_header_size < 5) { |
| 2444 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
| 2445 | return OPJ_FALSE; |
| 2446 | } |
| 2447 | |
| 2448 | opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */ |
| 2449 | ++p_header_data; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2450 | /* Make sure we know how to decode this */ |
| 2451 | if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | J2K_CP_CSTY_EPH)) != 0U) { |
| 2452 | opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); |
| 2453 | return OPJ_FALSE; |
| 2454 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2455 | opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */ |
| 2456 | ++p_header_data; |
| 2457 | l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2458 | /* Make sure progression order is valid */ |
| 2459 | if (l_tcp->prg > OPJ_CPRL ) { |
| 2460 | opj_event_msg(p_manager, EVT_ERROR, "Unknown progression order in COD marker\n"); |
| 2461 | l_tcp->prg = OPJ_PROG_UNKNOWN; |
| 2462 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2463 | opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */ |
| 2464 | p_header_data+=2; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 2465 | |
| 2466 | if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { |
| 2467 | opj_event_msg(p_manager, EVT_ERROR, "Invalid number of layers in COD marker : %d not in range [1-65535]\n", l_tcp->numlayers); |
| 2468 | return OPJ_FALSE; |
| 2469 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 2470 | |
| 2471 | /* If user didn't set a number layer to decode take the max specify in the codestream. */ |
| 2472 | if (l_cp->m_specific_param.m_dec.m_layer) { |
| 2473 | l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; |
| 2474 | } |
| 2475 | else { |
| 2476 | l_tcp->num_layers_to_decode = l_tcp->numlayers; |
| 2477 | } |
| 2478 | |
| 2479 | opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */ |
| 2480 | ++p_header_data; |
| 2481 | |
| 2482 | p_header_size -= 5; |
| 2483 | for (i = 0; i < l_image->numcomps; ++i) { |
| 2484 | l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; |
| 2485 | } |
| 2486 | |
| 2487 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { |
| 2488 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
| 2489 | return OPJ_FALSE; |
| 2490 | } |
| 2491 | |
| 2492 | if (p_header_size != 0) { |
| 2493 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); |
| 2494 | return OPJ_FALSE; |
| 2495 | } |
| 2496 | |
| 2497 | /* Apply the coding style to other components of the current tile or the m_default_tcp*/ |
| 2498 | opj_j2k_copy_tile_component_parameters(p_j2k); |
| 2499 | |
| 2500 | /* Index */ |
| 2501 | #ifdef WIP_REMOVE_MSD |
| 2502 | if (p_j2k->cstr_info) { |
| 2503 | /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ |
| 2504 | p_j2k->cstr_info->prog = l_tcp->prg; |
| 2505 | p_j2k->cstr_info->numlayers = l_tcp->numlayers; |
| 2506 | p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32)); |
| 2507 | for (i = 0; i < l_image->numcomps; ++i) { |
| 2508 | p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; |
| 2509 | } |
| 2510 | } |
| 2511 | #endif |
| 2512 | |
| 2513 | return OPJ_TRUE; |
| 2514 | } |
| 2515 | |
| 2516 | #if 0 |
| 2517 | OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k, |
| 2518 | OPJ_UINT32 p_comp_no, |
| 2519 | opj_stream_private_t *p_stream, |
| 2520 | opj_event_mgr_t * p_manager ) |
| 2521 | { |
| 2522 | OPJ_UINT32 l_coc_size,l_remaining_size; |
| 2523 | OPJ_UINT32 l_comp_room; |
| 2524 | |
| 2525 | /* preconditions */ |
| 2526 | assert(p_j2k != 00); |
| 2527 | assert(p_manager != 00); |
| 2528 | assert(p_stream != 00); |
| 2529 | |
| 2530 | l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; |
| 2531 | |
| 2532 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); |
| 2533 | |
| 2534 | if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2535 | OPJ_BYTE *new_header_tile_data; |
| 2536 | /*p_j2k->m_specific_param.m_encoder.m_header_tile_data |
| 2537 | = (OPJ_BYTE*)opj_realloc( |
| 2538 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
| 2539 | l_coc_size);*/ |
| 2540 | |
| 2541 | new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); |
| 2542 | if (! new_header_tile_data) { |
| 2543 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2544 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 2545 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 2546 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n"); |
| 2547 | return OPJ_FALSE; |
| 2548 | } |
| 2549 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 2550 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; |
| 2551 | } |
| 2552 | |
| 2553 | opj_j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); |
| 2554 | |
| 2555 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) { |
| 2556 | return OPJ_FALSE; |
| 2557 | } |
| 2558 | |
| 2559 | return OPJ_TRUE; |
| 2560 | } |
| 2561 | #endif |
| 2562 | |
| 2563 | #if 0 |
| 2564 | void opj_j2k_write_coc_in_memory( opj_j2k_t *p_j2k, |
| 2565 | OPJ_UINT32 p_comp_no, |
| 2566 | OPJ_BYTE * p_data, |
| 2567 | OPJ_UINT32 * p_data_written, |
| 2568 | opj_event_mgr_t * p_manager |
| 2569 | ) |
| 2570 | { |
| 2571 | opj_cp_t *l_cp = 00; |
| 2572 | opj_tcp_t *l_tcp = 00; |
| 2573 | OPJ_UINT32 l_coc_size,l_remaining_size; |
| 2574 | OPJ_BYTE * l_current_data = 00; |
| 2575 | opj_image_t *l_image = 00; |
| 2576 | OPJ_UINT32 l_comp_room; |
| 2577 | |
| 2578 | /* preconditions */ |
| 2579 | assert(p_j2k != 00); |
| 2580 | assert(p_manager != 00); |
| 2581 | |
| 2582 | l_cp = &(p_j2k->m_cp); |
| 2583 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
| 2584 | l_image = p_j2k->m_private_image; |
| 2585 | l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; |
| 2586 | |
| 2587 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); |
| 2588 | l_remaining_size = l_coc_size; |
| 2589 | |
| 2590 | l_current_data = p_data; |
| 2591 | |
| 2592 | opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */ |
| 2593 | l_current_data += 2; |
| 2594 | |
| 2595 | opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */ |
| 2596 | l_current_data += 2; |
| 2597 | |
| 2598 | opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */ |
| 2599 | l_current_data+=l_comp_room; |
| 2600 | |
| 2601 | opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */ |
| 2602 | ++l_current_data; |
| 2603 | |
| 2604 | l_remaining_size -= (5 + l_comp_room); |
| 2605 | opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager); |
| 2606 | * p_data_written = l_coc_size; |
| 2607 | } |
| 2608 | #endif |
| 2609 | |
| 2610 | OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) |
| 2611 | { |
| 2612 | OPJ_UINT32 i,j; |
| 2613 | OPJ_UINT32 l_nb_comp; |
| 2614 | OPJ_UINT32 l_nb_tiles; |
| 2615 | OPJ_UINT32 l_max = 0; |
| 2616 | |
| 2617 | /* preconditions */ |
| 2618 | |
| 2619 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
| 2620 | l_nb_comp = p_j2k->m_private_image->numcomps; |
| 2621 | |
| 2622 | for (i=0;i<l_nb_tiles;++i) { |
| 2623 | for (j=0;j<l_nb_comp;++j) { |
| 2624 | l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j)); |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | return 6 + l_max; |
| 2629 | } |
| 2630 | |
| 2631 | /** |
| 2632 | * Reads a COC marker (Coding Style Component) |
| 2633 | * @param p_header_data the data contained in the COC box. |
| 2634 | * @param p_j2k the jpeg2000 codec. |
| 2635 | * @param p_header_size the size of the data contained in the COC marker. |
| 2636 | * @param p_manager the user event manager. |
| 2637 | */ |
| 2638 | static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k, |
| 2639 | OPJ_BYTE * p_header_data, |
| 2640 | OPJ_UINT32 p_header_size, |
| 2641 | opj_event_mgr_t * p_manager |
| 2642 | ) |
| 2643 | { |
| 2644 | opj_cp_t *l_cp = NULL; |
| 2645 | opj_tcp_t *l_tcp = NULL; |
| 2646 | opj_image_t *l_image = NULL; |
| 2647 | OPJ_UINT32 l_comp_room; |
| 2648 | OPJ_UINT32 l_comp_no; |
| 2649 | |
| 2650 | /* preconditions */ |
| 2651 | assert(p_header_data != 00); |
| 2652 | assert(p_j2k != 00); |
| 2653 | assert(p_manager != 00); |
| 2654 | |
| 2655 | l_cp = &(p_j2k->m_cp); |
| 2656 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/ |
| 2657 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 2658 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 2659 | l_image = p_j2k->m_private_image; |
| 2660 | |
| 2661 | l_comp_room = l_image->numcomps <= 256 ? 1 : 2; |
| 2662 | |
| 2663 | /* make sure room is sufficient*/ |
| 2664 | if (p_header_size < l_comp_room + 1) { |
| 2665 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
| 2666 | return OPJ_FALSE; |
| 2667 | } |
| 2668 | p_header_size -= l_comp_room + 1; |
| 2669 | |
| 2670 | opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */ |
| 2671 | p_header_data += l_comp_room; |
| 2672 | if (l_comp_no >= l_image->numcomps) { |
| 2673 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n"); |
| 2674 | return OPJ_FALSE; |
| 2675 | } |
| 2676 | |
| 2677 | opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */ |
| 2678 | ++p_header_data ; |
| 2679 | |
| 2680 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { |
| 2681 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
| 2682 | return OPJ_FALSE; |
| 2683 | } |
| 2684 | |
| 2685 | if (p_header_size != 0) { |
| 2686 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); |
| 2687 | return OPJ_FALSE; |
| 2688 | } |
| 2689 | return OPJ_TRUE; |
| 2690 | } |
| 2691 | |
| 2692 | OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k, |
| 2693 | opj_stream_private_t *p_stream, |
| 2694 | opj_event_mgr_t * p_manager |
| 2695 | ) |
| 2696 | { |
| 2697 | OPJ_UINT32 l_qcd_size,l_remaining_size; |
| 2698 | OPJ_BYTE * l_current_data = 00; |
| 2699 | |
| 2700 | /* preconditions */ |
| 2701 | assert(p_j2k != 00); |
| 2702 | assert(p_manager != 00); |
| 2703 | assert(p_stream != 00); |
| 2704 | |
| 2705 | l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0); |
| 2706 | l_remaining_size = l_qcd_size; |
| 2707 | |
| 2708 | if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2709 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); |
| 2710 | if (! new_header_tile_data) { |
| 2711 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2712 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 2713 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 2714 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n"); |
| 2715 | return OPJ_FALSE; |
| 2716 | } |
| 2717 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 2718 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; |
| 2719 | } |
| 2720 | |
| 2721 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 2722 | |
| 2723 | opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */ |
| 2724 | l_current_data += 2; |
| 2725 | |
| 2726 | opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */ |
| 2727 | l_current_data += 2; |
| 2728 | |
| 2729 | l_remaining_size -= 4; |
| 2730 | |
| 2731 | if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { |
| 2732 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); |
| 2733 | return OPJ_FALSE; |
| 2734 | } |
| 2735 | |
| 2736 | if (l_remaining_size != 0) { |
| 2737 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); |
| 2738 | return OPJ_FALSE; |
| 2739 | } |
| 2740 | |
| 2741 | if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) { |
| 2742 | return OPJ_FALSE; |
| 2743 | } |
| 2744 | |
| 2745 | return OPJ_TRUE; |
| 2746 | } |
| 2747 | |
| 2748 | /** |
| 2749 | * Reads a QCD marker (Quantization defaults) |
| 2750 | * @param p_header_data the data contained in the QCD box. |
| 2751 | * @param p_j2k the jpeg2000 codec. |
| 2752 | * @param p_header_size the size of the data contained in the QCD marker. |
| 2753 | * @param p_manager the user event manager. |
| 2754 | */ |
| 2755 | static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k, |
| 2756 | OPJ_BYTE * p_header_data, |
| 2757 | OPJ_UINT32 p_header_size, |
| 2758 | opj_event_mgr_t * p_manager |
| 2759 | ) |
| 2760 | { |
| 2761 | /* preconditions */ |
| 2762 | assert(p_header_data != 00); |
| 2763 | assert(p_j2k != 00); |
| 2764 | assert(p_manager != 00); |
| 2765 | |
| 2766 | if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { |
| 2767 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
| 2768 | return OPJ_FALSE; |
| 2769 | } |
| 2770 | |
| 2771 | if (p_header_size != 0) { |
| 2772 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); |
| 2773 | return OPJ_FALSE; |
| 2774 | } |
| 2775 | |
| 2776 | /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ |
| 2777 | opj_j2k_copy_tile_quantization_parameters(p_j2k); |
| 2778 | |
| 2779 | return OPJ_TRUE; |
| 2780 | } |
| 2781 | |
| 2782 | #if 0 |
| 2783 | OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k, |
| 2784 | OPJ_UINT32 p_comp_no, |
| 2785 | opj_stream_private_t *p_stream, |
| 2786 | opj_event_mgr_t * p_manager |
| 2787 | ) |
| 2788 | { |
| 2789 | OPJ_UINT32 l_qcc_size,l_remaining_size; |
| 2790 | |
| 2791 | /* preconditions */ |
| 2792 | assert(p_j2k != 00); |
| 2793 | assert(p_manager != 00); |
| 2794 | assert(p_stream != 00); |
| 2795 | |
| 2796 | l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); |
| 2797 | l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1; |
| 2798 | l_remaining_size = l_qcc_size; |
| 2799 | |
| 2800 | if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2801 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); |
| 2802 | if (! new_header_tile_data) { |
| 2803 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2804 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 2805 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 2806 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n"); |
| 2807 | return OPJ_FALSE; |
| 2808 | } |
| 2809 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 2810 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; |
| 2811 | } |
| 2812 | |
| 2813 | opj_j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); |
| 2814 | |
| 2815 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) { |
| 2816 | return OPJ_FALSE; |
| 2817 | } |
| 2818 | |
| 2819 | return OPJ_TRUE; |
| 2820 | } |
| 2821 | #endif |
| 2822 | |
| 2823 | #if 0 |
| 2824 | void opj_j2k_write_qcc_in_memory( opj_j2k_t *p_j2k, |
| 2825 | OPJ_UINT32 p_comp_no, |
| 2826 | OPJ_BYTE * p_data, |
| 2827 | OPJ_UINT32 * p_data_written, |
| 2828 | opj_event_mgr_t * p_manager |
| 2829 | ) |
| 2830 | { |
| 2831 | OPJ_UINT32 l_qcc_size,l_remaining_size; |
| 2832 | OPJ_BYTE * l_current_data = 00; |
| 2833 | |
| 2834 | /* preconditions */ |
| 2835 | assert(p_j2k != 00); |
| 2836 | assert(p_manager != 00); |
| 2837 | |
| 2838 | l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); |
| 2839 | l_remaining_size = l_qcc_size; |
| 2840 | |
| 2841 | l_current_data = p_data; |
| 2842 | |
| 2843 | opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */ |
| 2844 | l_current_data += 2; |
| 2845 | |
| 2846 | if (p_j2k->m_private_image->numcomps <= 256) { |
| 2847 | --l_qcc_size; |
| 2848 | |
| 2849 | opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ |
| 2850 | l_current_data += 2; |
| 2851 | |
| 2852 | opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ |
| 2853 | ++l_current_data; |
| 2854 | |
| 2855 | /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ |
| 2856 | l_remaining_size -= 6; |
| 2857 | } |
| 2858 | else { |
| 2859 | opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ |
| 2860 | l_current_data += 2; |
| 2861 | |
| 2862 | opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ |
| 2863 | l_current_data+=2; |
| 2864 | |
| 2865 | l_remaining_size -= 6; |
| 2866 | } |
| 2867 | |
| 2868 | opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager); |
| 2869 | |
| 2870 | *p_data_written = l_qcc_size; |
| 2871 | } |
| 2872 | #endif |
| 2873 | |
| 2874 | OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k) |
| 2875 | { |
| 2876 | return opj_j2k_get_max_coc_size(p_j2k); |
| 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * Reads a QCC marker (Quantization component) |
| 2881 | * @param p_header_data the data contained in the QCC box. |
| 2882 | * @param p_j2k the jpeg2000 codec. |
| 2883 | * @param p_header_size the size of the data contained in the QCC marker. |
| 2884 | * @param p_manager the user event manager. |
| 2885 | */ |
| 2886 | static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, |
| 2887 | OPJ_BYTE * p_header_data, |
| 2888 | OPJ_UINT32 p_header_size, |
| 2889 | opj_event_mgr_t * p_manager |
| 2890 | ) |
| 2891 | { |
| 2892 | OPJ_UINT32 l_num_comp,l_comp_no; |
| 2893 | |
| 2894 | /* preconditions */ |
| 2895 | assert(p_header_data != 00); |
| 2896 | assert(p_j2k != 00); |
| 2897 | assert(p_manager != 00); |
| 2898 | |
| 2899 | l_num_comp = p_j2k->m_private_image->numcomps; |
| 2900 | |
| 2901 | if (l_num_comp <= 256) { |
| 2902 | if (p_header_size < 1) { |
| 2903 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
| 2904 | return OPJ_FALSE; |
| 2905 | } |
| 2906 | opj_read_bytes(p_header_data,&l_comp_no,1); |
| 2907 | ++p_header_data; |
| 2908 | --p_header_size; |
| 2909 | } |
| 2910 | else { |
| 2911 | if (p_header_size < 2) { |
| 2912 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
| 2913 | return OPJ_FALSE; |
| 2914 | } |
| 2915 | opj_read_bytes(p_header_data,&l_comp_no,2); |
| 2916 | p_header_data+=2; |
| 2917 | p_header_size-=2; |
| 2918 | } |
| 2919 | |
| 2920 | #ifdef USE_JPWL |
| 2921 | if (p_j2k->m_cp.correct) { |
| 2922 | |
| 2923 | static OPJ_UINT32 backup_compno = 0; |
| 2924 | |
| 2925 | /* compno is negative or larger than the number of components!!! */ |
| 2926 | if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { |
| 2927 | opj_event_msg(p_manager, EVT_ERROR, |
| 2928 | "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", |
| 2929 | l_comp_no, l_num_comp); |
| 2930 | if (!JPWL_ASSUME) { |
| 2931 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 2932 | return OPJ_FALSE; |
| 2933 | } |
| 2934 | /* we try to correct */ |
| 2935 | l_comp_no = backup_compno % l_num_comp; |
| 2936 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
| 2937 | "- setting component number to %d\n", |
| 2938 | l_comp_no); |
| 2939 | } |
| 2940 | |
| 2941 | /* keep your private count of tiles */ |
| 2942 | backup_compno++; |
| 2943 | }; |
| 2944 | #endif /* USE_JPWL */ |
| 2945 | |
| 2946 | if (l_comp_no >= p_j2k->m_private_image->numcomps) { |
| 2947 | opj_event_msg(p_manager, EVT_ERROR, |
| 2948 | "Invalid component number: %d, regarding the number of components %d\n", |
| 2949 | l_comp_no, p_j2k->m_private_image->numcomps); |
| 2950 | return OPJ_FALSE; |
| 2951 | } |
| 2952 | |
| 2953 | if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { |
| 2954 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
| 2955 | return OPJ_FALSE; |
| 2956 | } |
| 2957 | |
| 2958 | if (p_header_size != 0) { |
| 2959 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); |
| 2960 | return OPJ_FALSE; |
| 2961 | } |
| 2962 | |
| 2963 | return OPJ_TRUE; |
| 2964 | } |
| 2965 | |
| 2966 | OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k, |
| 2967 | opj_stream_private_t *p_stream, |
| 2968 | opj_event_mgr_t * p_manager |
| 2969 | ) |
| 2970 | { |
| 2971 | OPJ_UINT32 l_nb_comp; |
| 2972 | OPJ_UINT32 l_nb_poc; |
| 2973 | OPJ_UINT32 l_poc_size; |
| 2974 | OPJ_UINT32 l_written_size = 0; |
| 2975 | opj_tcp_t *l_tcp = 00; |
| 2976 | OPJ_UINT32 l_poc_room; |
| 2977 | |
| 2978 | /* preconditions */ |
| 2979 | assert(p_j2k != 00); |
| 2980 | assert(p_manager != 00); |
| 2981 | assert(p_stream != 00); |
| 2982 | |
| 2983 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
| 2984 | l_nb_comp = p_j2k->m_private_image->numcomps; |
| 2985 | l_nb_poc = 1 + l_tcp->numpocs; |
| 2986 | |
| 2987 | if (l_nb_comp <= 256) { |
| 2988 | l_poc_room = 1; |
| 2989 | } |
| 2990 | else { |
| 2991 | l_poc_room = 2; |
| 2992 | } |
| 2993 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
| 2994 | |
| 2995 | if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 2996 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); |
| 2997 | if (! new_header_tile_data) { |
| 2998 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 2999 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 3000 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 3001 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n"); |
| 3002 | return OPJ_FALSE; |
| 3003 | } |
| 3004 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 3005 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; |
| 3006 | } |
| 3007 | |
| 3008 | opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager); |
| 3009 | |
| 3010 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) { |
| 3011 | return OPJ_FALSE; |
| 3012 | } |
| 3013 | |
| 3014 | return OPJ_TRUE; |
| 3015 | } |
| 3016 | |
| 3017 | void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k, |
| 3018 | OPJ_BYTE * p_data, |
| 3019 | OPJ_UINT32 * p_data_written, |
| 3020 | opj_event_mgr_t * p_manager |
| 3021 | ) |
| 3022 | { |
| 3023 | OPJ_UINT32 i; |
| 3024 | OPJ_BYTE * l_current_data = 00; |
| 3025 | OPJ_UINT32 l_nb_comp; |
| 3026 | OPJ_UINT32 l_nb_poc; |
| 3027 | OPJ_UINT32 l_poc_size; |
| 3028 | opj_image_t *l_image = 00; |
| 3029 | opj_tcp_t *l_tcp = 00; |
| 3030 | opj_tccp_t *l_tccp = 00; |
| 3031 | opj_poc_t *l_current_poc = 00; |
| 3032 | OPJ_UINT32 l_poc_room; |
| 3033 | |
| 3034 | /* preconditions */ |
| 3035 | assert(p_j2k != 00); |
| 3036 | assert(p_manager != 00); |
| 3037 | |
| 3038 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
| 3039 | l_tccp = &l_tcp->tccps[0]; |
| 3040 | l_image = p_j2k->m_private_image; |
| 3041 | l_nb_comp = l_image->numcomps; |
| 3042 | l_nb_poc = 1 + l_tcp->numpocs; |
| 3043 | |
| 3044 | if (l_nb_comp <= 256) { |
| 3045 | l_poc_room = 1; |
| 3046 | } |
| 3047 | else { |
| 3048 | l_poc_room = 2; |
| 3049 | } |
| 3050 | |
| 3051 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
| 3052 | |
| 3053 | l_current_data = p_data; |
| 3054 | |
| 3055 | opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */ |
| 3056 | l_current_data += 2; |
| 3057 | |
| 3058 | opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */ |
| 3059 | l_current_data += 2; |
| 3060 | |
| 3061 | l_current_poc = l_tcp->pocs; |
| 3062 | for (i = 0; i < l_nb_poc; ++i) { |
| 3063 | opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */ |
| 3064 | ++l_current_data; |
| 3065 | |
| 3066 | opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */ |
| 3067 | l_current_data+=l_poc_room; |
| 3068 | |
| 3069 | opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */ |
| 3070 | l_current_data+=2; |
| 3071 | |
| 3072 | opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */ |
| 3073 | ++l_current_data; |
| 3074 | |
| 3075 | opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */ |
| 3076 | l_current_data+=l_poc_room; |
| 3077 | |
| 3078 | opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */ |
| 3079 | ++l_current_data; |
| 3080 | |
| 3081 | /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ |
| 3082 | l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers); |
| 3083 | l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions); |
| 3084 | l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp); |
| 3085 | |
| 3086 | ++l_current_poc; |
| 3087 | } |
| 3088 | |
| 3089 | *p_data_written = l_poc_size; |
| 3090 | } |
| 3091 | |
| 3092 | OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k) |
| 3093 | { |
| 3094 | opj_tcp_t * l_tcp = 00; |
| 3095 | OPJ_UINT32 l_nb_tiles = 0; |
| 3096 | OPJ_UINT32 l_max_poc = 0; |
| 3097 | OPJ_UINT32 i; |
| 3098 | |
| 3099 | l_tcp = p_j2k->m_cp.tcps; |
| 3100 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 3101 | |
| 3102 | for (i=0;i<l_nb_tiles;++i) { |
| 3103 | l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs); |
| 3104 | ++l_tcp; |
| 3105 | } |
| 3106 | |
| 3107 | ++l_max_poc; |
| 3108 | |
| 3109 | return 4 + 9 * l_max_poc; |
| 3110 | } |
| 3111 | |
| 3112 | OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k) |
| 3113 | { |
| 3114 | OPJ_UINT32 i; |
| 3115 | OPJ_UINT32 l_nb_tiles; |
| 3116 | OPJ_UINT32 l_max = 0; |
| 3117 | opj_tcp_t * l_tcp = 00; |
| 3118 | |
| 3119 | l_tcp = p_j2k->m_cp.tcps; |
| 3120 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
| 3121 | |
| 3122 | for (i=0;i<l_nb_tiles;++i) { |
| 3123 | l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts); |
| 3124 | |
| 3125 | ++l_tcp; |
| 3126 | } |
| 3127 | |
| 3128 | return 12 * l_max; |
| 3129 | } |
| 3130 | |
| 3131 | OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) |
| 3132 | { |
| 3133 | OPJ_UINT32 l_nb_bytes = 0; |
| 3134 | OPJ_UINT32 l_nb_comps; |
| 3135 | OPJ_UINT32 l_coc_bytes,l_qcc_bytes; |
| 3136 | |
| 3137 | l_nb_comps = p_j2k->m_private_image->numcomps - 1; |
| 3138 | l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); |
| 3139 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3140 | if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3141 | l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); |
| 3142 | l_nb_bytes += l_nb_comps * l_coc_bytes; |
| 3143 | |
| 3144 | l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); |
| 3145 | l_nb_bytes += l_nb_comps * l_qcc_bytes; |
| 3146 | } |
| 3147 | |
| 3148 | l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); |
| 3149 | |
| 3150 | /*** DEVELOPER CORNER, Add room for your headers ***/ |
| 3151 | |
| 3152 | return l_nb_bytes; |
| 3153 | } |
| 3154 | |
| 3155 | /** |
| 3156 | * Reads a POC marker (Progression Order Change) |
| 3157 | * |
| 3158 | * @param p_header_data the data contained in the POC box. |
| 3159 | * @param p_j2k the jpeg2000 codec. |
| 3160 | * @param p_header_size the size of the data contained in the POC marker. |
| 3161 | * @param p_manager the user event manager. |
| 3162 | */ |
| 3163 | static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, |
| 3164 | OPJ_BYTE * p_header_data, |
| 3165 | OPJ_UINT32 p_header_size, |
| 3166 | opj_event_mgr_t * p_manager |
| 3167 | ) |
| 3168 | { |
| 3169 | OPJ_UINT32 i, l_nb_comp, l_tmp; |
| 3170 | opj_image_t * l_image = 00; |
| 3171 | OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; |
| 3172 | OPJ_UINT32 l_chunk_size, l_comp_room; |
| 3173 | |
| 3174 | opj_cp_t *l_cp = 00; |
| 3175 | opj_tcp_t *l_tcp = 00; |
| 3176 | opj_poc_t *l_current_poc = 00; |
| 3177 | |
| 3178 | /* preconditions */ |
| 3179 | assert(p_header_data != 00); |
| 3180 | assert(p_j2k != 00); |
| 3181 | assert(p_manager != 00); |
| 3182 | |
| 3183 | l_image = p_j2k->m_private_image; |
| 3184 | l_nb_comp = l_image->numcomps; |
| 3185 | if (l_nb_comp <= 256) { |
| 3186 | l_comp_room = 1; |
| 3187 | } |
| 3188 | else { |
| 3189 | l_comp_room = 2; |
| 3190 | } |
| 3191 | l_chunk_size = 5 + 2 * l_comp_room; |
| 3192 | l_current_poc_nb = p_header_size / l_chunk_size; |
| 3193 | l_current_poc_remaining = p_header_size % l_chunk_size; |
| 3194 | |
| 3195 | if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { |
| 3196 | opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); |
| 3197 | return OPJ_FALSE; |
| 3198 | } |
| 3199 | |
| 3200 | l_cp = &(p_j2k->m_cp); |
| 3201 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
| 3202 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 3203 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 3204 | l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; |
| 3205 | l_current_poc_nb += l_old_poc_nb; |
| 3206 | |
| 3207 | if(l_current_poc_nb >= 32) |
| 3208 | { |
| 3209 | opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); |
| 3210 | return OPJ_FALSE; |
| 3211 | } |
| 3212 | assert(l_current_poc_nb < 32); |
| 3213 | |
| 3214 | /* now poc is in use.*/ |
| 3215 | l_tcp->POC = 1; |
| 3216 | |
| 3217 | l_current_poc = &l_tcp->pocs[l_old_poc_nb]; |
| 3218 | for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { |
| 3219 | opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */ |
| 3220 | ++p_header_data; |
| 3221 | opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */ |
| 3222 | p_header_data+=l_comp_room; |
| 3223 | opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */ |
| 3224 | /* make sure layer end is in acceptable bounds */ |
| 3225 | l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); |
| 3226 | p_header_data+=2; |
| 3227 | opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */ |
| 3228 | ++p_header_data; |
| 3229 | opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */ |
| 3230 | p_header_data+=l_comp_room; |
| 3231 | opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */ |
| 3232 | ++p_header_data; |
| 3233 | l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; |
| 3234 | /* make sure comp is in acceptable bounds */ |
| 3235 | l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); |
| 3236 | ++l_current_poc; |
| 3237 | } |
| 3238 | |
| 3239 | l_tcp->numpocs = l_current_poc_nb - 1; |
| 3240 | return OPJ_TRUE; |
| 3241 | } |
| 3242 | |
| 3243 | /** |
| 3244 | * Reads a CRG marker (Component registration) |
| 3245 | * |
| 3246 | * @param p_header_data the data contained in the TLM box. |
| 3247 | * @param p_j2k the jpeg2000 codec. |
| 3248 | * @param p_header_size the size of the data contained in the TLM marker. |
| 3249 | * @param p_manager the user event manager. |
| 3250 | */ |
| 3251 | static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k, |
| 3252 | OPJ_BYTE * p_header_data, |
| 3253 | OPJ_UINT32 p_header_size, |
| 3254 | opj_event_mgr_t * p_manager |
| 3255 | ) |
| 3256 | { |
| 3257 | OPJ_UINT32 l_nb_comp; |
| 3258 | /* preconditions */ |
| 3259 | assert(p_header_data != 00); |
| 3260 | assert(p_j2k != 00); |
| 3261 | assert(p_manager != 00); |
| 3262 | |
| 3263 | l_nb_comp = p_j2k->m_private_image->numcomps; |
| 3264 | |
| 3265 | if (p_header_size != l_nb_comp *4) { |
| 3266 | opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); |
| 3267 | return OPJ_FALSE; |
| 3268 | } |
| 3269 | /* Do not care of this at the moment since only local variables are set here */ |
| 3270 | /* |
| 3271 | for |
| 3272 | (i = 0; i < l_nb_comp; ++i) |
| 3273 | { |
| 3274 | opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i |
| 3275 | p_header_data+=2; |
| 3276 | opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i |
| 3277 | p_header_data+=2; |
| 3278 | } |
| 3279 | */ |
| 3280 | return OPJ_TRUE; |
| 3281 | } |
| 3282 | |
| 3283 | /** |
| 3284 | * Reads a TLM marker (Tile Length Marker) |
| 3285 | * |
| 3286 | * @param p_header_data the data contained in the TLM box. |
| 3287 | * @param p_j2k the jpeg2000 codec. |
| 3288 | * @param p_header_size the size of the data contained in the TLM marker. |
| 3289 | * @param p_manager the user event manager. |
| 3290 | */ |
| 3291 | static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k, |
| 3292 | OPJ_BYTE * p_header_data, |
| 3293 | OPJ_UINT32 p_header_size, |
| 3294 | opj_event_mgr_t * p_manager |
| 3295 | ) |
| 3296 | { |
| 3297 | OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size; |
| 3298 | /* preconditions */ |
| 3299 | assert(p_header_data != 00); |
| 3300 | assert(p_j2k != 00); |
| 3301 | assert(p_manager != 00); |
| 3302 | |
| 3303 | if (p_header_size < 2) { |
| 3304 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); |
| 3305 | return OPJ_FALSE; |
| 3306 | } |
| 3307 | p_header_size -= 2; |
| 3308 | |
| 3309 | opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */ |
| 3310 | ++p_header_data; |
| 3311 | opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */ |
| 3312 | ++p_header_data; |
| 3313 | |
| 3314 | l_ST = ((l_Stlm >> 4) & 0x3); |
| 3315 | l_SP = (l_Stlm >> 6) & 0x1; |
| 3316 | |
| 3317 | l_Ptlm_size = (l_SP + 1) * 2; |
| 3318 | l_quotient = l_Ptlm_size + l_ST; |
| 3319 | |
| 3320 | l_tot_num_tp_remaining = p_header_size % l_quotient; |
| 3321 | |
| 3322 | if (l_tot_num_tp_remaining != 0) { |
| 3323 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); |
| 3324 | return OPJ_FALSE; |
| 3325 | } |
| 3326 | /* FIXME Do not care of this at the moment since only local variables are set here */ |
| 3327 | /* |
| 3328 | for |
| 3329 | (i = 0; i < l_tot_num_tp; ++i) |
| 3330 | { |
| 3331 | opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i |
| 3332 | p_header_data += l_ST; |
| 3333 | opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i |
| 3334 | p_header_data += l_Ptlm_size; |
| 3335 | }*/ |
| 3336 | return OPJ_TRUE; |
| 3337 | } |
| 3338 | |
| 3339 | /** |
| 3340 | * Reads a PLM marker (Packet length, main header marker) |
| 3341 | * |
| 3342 | * @param p_header_data the data contained in the TLM box. |
| 3343 | * @param p_j2k the jpeg2000 codec. |
| 3344 | * @param p_header_size the size of the data contained in the TLM marker. |
| 3345 | * @param p_manager the user event manager. |
| 3346 | */ |
| 3347 | static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k, |
| 3348 | OPJ_BYTE * p_header_data, |
| 3349 | OPJ_UINT32 p_header_size, |
| 3350 | opj_event_mgr_t * p_manager |
| 3351 | ) |
| 3352 | { |
| 3353 | /* preconditions */ |
| 3354 | assert(p_header_data != 00); |
| 3355 | assert(p_j2k != 00); |
| 3356 | assert(p_manager != 00); |
| 3357 | |
| 3358 | if (p_header_size < 1) { |
| 3359 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
| 3360 | return OPJ_FALSE; |
| 3361 | } |
| 3362 | /* Do not care of this at the moment since only local variables are set here */ |
| 3363 | /* |
| 3364 | opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm |
| 3365 | ++p_header_data; |
| 3366 | --p_header_size; |
| 3367 | |
| 3368 | while |
| 3369 | (p_header_size > 0) |
| 3370 | { |
| 3371 | opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm |
| 3372 | ++p_header_data; |
| 3373 | p_header_size -= (1+l_Nplm); |
| 3374 | if |
| 3375 | (p_header_size < 0) |
| 3376 | { |
| 3377 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
| 3378 | return false; |
| 3379 | } |
| 3380 | for |
| 3381 | (i = 0; i < l_Nplm; ++i) |
| 3382 | { |
| 3383 | opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij |
| 3384 | ++p_header_data; |
| 3385 | // take only the last seven bytes |
| 3386 | l_packet_len |= (l_tmp & 0x7f); |
| 3387 | if |
| 3388 | (l_tmp & 0x80) |
| 3389 | { |
| 3390 | l_packet_len <<= 7; |
| 3391 | } |
| 3392 | else |
| 3393 | { |
| 3394 | // store packet length and proceed to next packet |
| 3395 | l_packet_len = 0; |
| 3396 | } |
| 3397 | } |
| 3398 | if |
| 3399 | (l_packet_len != 0) |
| 3400 | { |
| 3401 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
| 3402 | return false; |
| 3403 | } |
| 3404 | } |
| 3405 | */ |
| 3406 | return OPJ_TRUE; |
| 3407 | } |
| 3408 | |
| 3409 | /** |
| 3410 | * Reads a PLT marker (Packet length, tile-part header) |
| 3411 | * |
| 3412 | * @param p_header_data the data contained in the PLT box. |
| 3413 | * @param p_j2k the jpeg2000 codec. |
| 3414 | * @param p_header_size the size of the data contained in the PLT marker. |
| 3415 | * @param p_manager the user event manager. |
| 3416 | */ |
| 3417 | static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, |
| 3418 | OPJ_BYTE * p_header_data, |
| 3419 | OPJ_UINT32 p_header_size, |
| 3420 | opj_event_mgr_t * p_manager |
| 3421 | ) |
| 3422 | { |
| 3423 | OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; |
| 3424 | |
| 3425 | /* preconditions */ |
| 3426 | assert(p_header_data != 00); |
| 3427 | assert(p_j2k != 00); |
| 3428 | assert(p_manager != 00); |
| 3429 | |
| 3430 | if (p_header_size < 1) { |
| 3431 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
| 3432 | return OPJ_FALSE; |
| 3433 | } |
| 3434 | |
| 3435 | opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */ |
| 3436 | ++p_header_data; |
| 3437 | --p_header_size; |
| 3438 | |
| 3439 | for (i = 0; i < p_header_size; ++i) { |
| 3440 | opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */ |
| 3441 | ++p_header_data; |
| 3442 | /* take only the last seven bytes */ |
| 3443 | l_packet_len |= (l_tmp & 0x7f); |
| 3444 | if (l_tmp & 0x80) { |
| 3445 | l_packet_len <<= 7; |
| 3446 | } |
| 3447 | else { |
| 3448 | /* store packet length and proceed to next packet */ |
| 3449 | l_packet_len = 0; |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | if (l_packet_len != 0) { |
| 3454 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); |
| 3455 | return OPJ_FALSE; |
| 3456 | } |
| 3457 | |
| 3458 | return OPJ_TRUE; |
| 3459 | } |
| 3460 | |
| 3461 | #if 0 |
| 3462 | OPJ_BOOL j2k_read_ppm_v2 ( |
| 3463 | opj_j2k_t *p_j2k, |
| 3464 | OPJ_BYTE * p_header_data, |
| 3465 | OPJ_UINT32 p_header_size, |
| 3466 | struct opj_event_mgr * p_manager |
| 3467 | ) |
| 3468 | { |
| 3469 | |
| 3470 | opj_cp_t *l_cp = 00; |
| 3471 | OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; |
| 3472 | |
| 3473 | /* preconditions */ |
| 3474 | assert(p_header_data != 00); |
| 3475 | assert(p_j2k != 00); |
| 3476 | assert(p_manager != 00); |
| 3477 | |
| 3478 | if (p_header_size < 1) { |
| 3479 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
| 3480 | return OPJ_FALSE; |
| 3481 | } |
| 3482 | |
| 3483 | l_cp = &(p_j2k->m_cp); |
| 3484 | l_cp->ppm = 1; |
| 3485 | |
| 3486 | opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ |
| 3487 | ++p_header_data; |
| 3488 | --p_header_size; |
| 3489 | |
| 3490 | /* First PPM marker */ |
| 3491 | if (l_Z_ppm == 0) { |
| 3492 | if (p_header_size < 4) { |
| 3493 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
| 3494 | return OPJ_FALSE; |
| 3495 | } |
| 3496 | |
| 3497 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ |
| 3498 | p_header_data+=4; |
| 3499 | p_header_size-=4; |
| 3500 | |
| 3501 | /* First PPM marker: Initialization */ |
| 3502 | l_cp->ppm_len = l_N_ppm; |
| 3503 | l_cp->ppm_data_size = 0; |
| 3504 | |
| 3505 | l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); |
| 3506 | if (l_cp->ppm_buffer == 00) { |
| 3507 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); |
| 3508 | return OPJ_FALSE; |
| 3509 | } |
| 3510 | memset(l_cp->ppm_buffer,0,l_cp->ppm_len); |
| 3511 | |
| 3512 | l_cp->ppm_data = l_cp->ppm_buffer; |
| 3513 | } |
| 3514 | |
| 3515 | while (1) { |
| 3516 | if (l_cp->ppm_data_size == l_cp->ppm_len) { |
| 3517 | if (p_header_size >= 4) { |
| 3518 | /* read a N_ppm */ |
| 3519 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ |
| 3520 | p_header_data+=4; |
| 3521 | p_header_size-=4; |
| 3522 | l_cp->ppm_len += l_N_ppm ; |
| 3523 | |
| 3524 | OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); |
| 3525 | if (! new_ppm_buffer) { |
| 3526 | opj_free(l_cp->ppm_buffer); |
| 3527 | l_cp->ppm_buffer = NULL; |
| 3528 | l_cp->ppm_len = 0; |
| 3529 | l_cp->ppm_data = NULL; |
| 3530 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); |
| 3531 | return OPJ_FALSE; |
| 3532 | } |
| 3533 | l_cp->ppm_buffer = new_ppm_buffer; |
| 3534 | memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); |
| 3535 | l_cp->ppm_data = l_cp->ppm_buffer; |
| 3536 | } |
| 3537 | else { |
| 3538 | return OPJ_FALSE; |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; |
| 3543 | |
| 3544 | if (l_remaining_data <= p_header_size) { |
| 3545 | /* we must store less information than available in the packet */ |
| 3546 | memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); |
| 3547 | l_cp->ppm_data_size = l_cp->ppm_len; |
| 3548 | p_header_size -= l_remaining_data; |
| 3549 | p_header_data += l_remaining_data; |
| 3550 | } |
| 3551 | else { |
| 3552 | memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); |
| 3553 | l_cp->ppm_data_size += p_header_size; |
| 3554 | p_header_data += p_header_size; |
| 3555 | p_header_size = 0; |
| 3556 | break; |
| 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | return OPJ_TRUE; |
| 3561 | } |
| 3562 | #endif |
| 3563 | |
| 3564 | OPJ_BOOL j2k_read_ppm_v3 ( |
| 3565 | opj_j2k_t *p_j2k, |
| 3566 | OPJ_BYTE * p_header_data, |
| 3567 | OPJ_UINT32 p_header_size, |
| 3568 | struct opj_event_mgr * p_manager |
| 3569 | ) |
| 3570 | { |
| 3571 | opj_cp_t *l_cp = 00; |
| 3572 | OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; |
| 3573 | |
| 3574 | /* preconditions */ |
| 3575 | assert(p_header_data != 00); |
| 3576 | assert(p_j2k != 00); |
| 3577 | assert(p_manager != 00); |
| 3578 | |
| 3579 | /* Minimum size of PPM marker is equal to the size of Zppm element */ |
| 3580 | if (p_header_size < 1) { |
| 3581 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
| 3582 | return OPJ_FALSE; |
| 3583 | } |
| 3584 | |
| 3585 | l_cp = &(p_j2k->m_cp); |
| 3586 | l_cp->ppm = 1; |
| 3587 | |
| 3588 | opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ |
| 3589 | ++p_header_data; |
| 3590 | --p_header_size; |
| 3591 | |
| 3592 | /* First PPM marker */ |
| 3593 | if (l_Z_ppm == 0) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3594 | if (l_cp->ppm_data != NULL) { |
| 3595 | opj_event_msg(p_manager, EVT_ERROR, "Zppm O already processed. Found twice.\n"); |
| 3596 | opj_free(l_cp->ppm_data); |
| 3597 | l_cp->ppm_data = NULL; |
| 3598 | l_cp->ppm_buffer = NULL; |
| 3599 | l_cp->ppm = 0; /* do not use PPM */ |
| 3600 | return OPJ_FALSE; |
| 3601 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3602 | /* We need now at least the Nppm^0 element */ |
| 3603 | if (p_header_size < 4) { |
| 3604 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
| 3605 | return OPJ_FALSE; |
| 3606 | } |
| 3607 | |
| 3608 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ |
| 3609 | p_header_data+=4; |
| 3610 | p_header_size-=4; |
| 3611 | |
| 3612 | /* sanity check: how much bytes is left for Ippm */ |
| 3613 | if( p_header_size < l_N_ppm ) |
| 3614 | { |
| 3615 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm ); |
| 3616 | opj_free(l_cp->ppm_data); |
| 3617 | l_cp->ppm_data = NULL; |
| 3618 | l_cp->ppm_buffer = NULL; |
| 3619 | l_cp->ppm = 0; /* do not use PPM */ |
| 3620 | return OPJ_FALSE; |
| 3621 | } |
| 3622 | |
| 3623 | /* First PPM marker: Initialization */ |
| 3624 | l_cp->ppm_len = l_N_ppm; |
| 3625 | l_cp->ppm_data_read = 0; |
| 3626 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3627 | l_cp->ppm_data = (OPJ_BYTE *) opj_calloc(1,l_cp->ppm_len); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3628 | l_cp->ppm_buffer = l_cp->ppm_data; |
| 3629 | if (l_cp->ppm_data == 00) { |
| 3630 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n"); |
| 3631 | return OPJ_FALSE; |
| 3632 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3633 | |
| 3634 | l_cp->ppm_data_current = l_cp->ppm_data; |
| 3635 | |
| 3636 | /*l_cp->ppm_data = l_cp->ppm_buffer;*/ |
| 3637 | } |
| 3638 | else { |
| 3639 | if (p_header_size < 4) { |
| 3640 | opj_event_msg(p_manager, EVT_WARNING, "Empty PPM marker\n"); |
| 3641 | return OPJ_TRUE; |
| 3642 | } |
| 3643 | else { |
| 3644 | /* Uncompleted Ippm series in the previous PPM marker?*/ |
| 3645 | if (l_cp->ppm_data_read < l_cp->ppm_len) { |
| 3646 | /* Get the place where add the remaining Ippm series*/ |
| 3647 | l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]); |
| 3648 | l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read; |
| 3649 | } |
| 3650 | else { |
| 3651 | OPJ_BYTE *new_ppm_data; |
| 3652 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ |
| 3653 | p_header_data+=4; |
| 3654 | p_header_size-=4; |
| 3655 | |
| 3656 | /* sanity check: how much bytes is left for Ippm */ |
| 3657 | if( p_header_size < l_N_ppm ) |
| 3658 | { |
| 3659 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm ); |
| 3660 | opj_free(l_cp->ppm_data); |
| 3661 | l_cp->ppm_data = NULL; |
| 3662 | l_cp->ppm_buffer = NULL; |
| 3663 | l_cp->ppm = 0; /* do not use PPM */ |
| 3664 | return OPJ_FALSE; |
| 3665 | } |
| 3666 | /* Increase the size of ppm_data to add the new Ippm series*/ |
| 3667 | assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating"); |
| 3668 | new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); |
| 3669 | if (! new_ppm_data) { |
| 3670 | opj_free(l_cp->ppm_data); |
| 3671 | l_cp->ppm_data = NULL; |
| 3672 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3673 | l_cp->ppm_len = 0; |
| 3674 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n"); |
| 3675 | return OPJ_FALSE; |
| 3676 | } |
| 3677 | l_cp->ppm_data = new_ppm_data; |
| 3678 | l_cp->ppm_buffer = l_cp->ppm_data; |
| 3679 | |
| 3680 | /* Keep the position of the place where concatenate the new series*/ |
| 3681 | l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); |
| 3682 | l_cp->ppm_len += l_N_ppm; |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | l_remaining_data = p_header_size; |
| 3688 | |
| 3689 | while (l_remaining_data >= l_N_ppm) { |
| 3690 | /* read a complete Ippm series*/ |
| 3691 | memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm); |
| 3692 | p_header_size -= l_N_ppm; |
| 3693 | p_header_data += l_N_ppm; |
| 3694 | |
| 3695 | l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/ |
| 3696 | |
| 3697 | if (p_header_size) |
| 3698 | { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3699 | if (p_header_size < 4) { |
| 3700 | opj_free(l_cp->ppm_data); |
| 3701 | l_cp->ppm_data = NULL; |
| 3702 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3703 | l_cp->ppm_len = 0; |
| 3704 | l_cp->ppm = 0; |
| 3705 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); |
| 3706 | return OPJ_FALSE; |
| 3707 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3708 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */ |
| 3709 | p_header_data+=4; |
| 3710 | p_header_size-=4; |
| 3711 | } |
| 3712 | else { |
| 3713 | l_remaining_data = p_header_size; |
| 3714 | break; |
| 3715 | } |
| 3716 | |
| 3717 | l_remaining_data = p_header_size; |
| 3718 | |
| 3719 | /* Next Ippm series is a complete series ?*/ |
| 3720 | if (l_remaining_data >= l_N_ppm) { |
| 3721 | OPJ_BYTE *new_ppm_data; |
| 3722 | /* Increase the size of ppm_data to add the new Ippm series*/ |
| 3723 | assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating"); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3724 | /* Overflow check */ |
| 3725 | if ((l_cp->ppm_len + l_N_ppm) < l_N_ppm) { |
| 3726 | opj_free(l_cp->ppm_data); |
| 3727 | l_cp->ppm_data = NULL; |
| 3728 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3729 | l_cp->ppm_len = 0; |
| 3730 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n"); |
| 3731 | return OPJ_FALSE; |
| 3732 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3733 | new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); |
| 3734 | if (! new_ppm_data) { |
| 3735 | opj_free(l_cp->ppm_data); |
| 3736 | l_cp->ppm_data = NULL; |
| 3737 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3738 | l_cp->ppm_len = 0; |
| 3739 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n"); |
| 3740 | return OPJ_FALSE; |
| 3741 | } |
| 3742 | l_cp->ppm_data = new_ppm_data; |
| 3743 | l_cp->ppm_buffer = l_cp->ppm_data; |
| 3744 | |
| 3745 | /* Keep the position of the place where concatenate the new series */ |
| 3746 | l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); |
| 3747 | l_cp->ppm_len += l_N_ppm; |
| 3748 | } |
| 3749 | |
| 3750 | } |
| 3751 | |
| 3752 | /* Need to read an incomplete Ippm series*/ |
| 3753 | if (l_remaining_data) { |
| 3754 | OPJ_BYTE *new_ppm_data; |
| 3755 | assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating"); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 3756 | |
| 3757 | /* Overflow check */ |
| 3758 | if ((l_cp->ppm_len + l_N_ppm) < l_N_ppm) { |
| 3759 | opj_free(l_cp->ppm_data); |
| 3760 | l_cp->ppm_data = NULL; |
| 3761 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3762 | l_cp->ppm_len = 0; |
| 3763 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n"); |
| 3764 | return OPJ_FALSE; |
| 3765 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 3766 | new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); |
| 3767 | if (! new_ppm_data) { |
| 3768 | opj_free(l_cp->ppm_data); |
| 3769 | l_cp->ppm_data = NULL; |
| 3770 | l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */ |
| 3771 | l_cp->ppm_len = 0; |
| 3772 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n"); |
| 3773 | return OPJ_FALSE; |
| 3774 | } |
| 3775 | l_cp->ppm_data = new_ppm_data; |
| 3776 | l_cp->ppm_buffer = l_cp->ppm_data; |
| 3777 | |
| 3778 | /* Keep the position of the place where concatenate the new series*/ |
| 3779 | l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); |
| 3780 | l_cp->ppm_len += l_N_ppm; |
| 3781 | |
| 3782 | /* Read incomplete Ippm series*/ |
| 3783 | memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data); |
| 3784 | p_header_size -= l_remaining_data; |
| 3785 | p_header_data += l_remaining_data; |
| 3786 | |
| 3787 | l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/ |
| 3788 | } |
| 3789 | |
| 3790 | #ifdef CLEAN_MSD |
| 3791 | |
| 3792 | if (l_cp->ppm_data_size == l_cp->ppm_len) { |
| 3793 | if (p_header_size >= 4) { |
| 3794 | /* read a N_ppm*/ |
| 3795 | opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ |
| 3796 | p_header_data+=4; |
| 3797 | p_header_size-=4; |
| 3798 | l_cp->ppm_len += l_N_ppm ; |
| 3799 | |
| 3800 | OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); |
| 3801 | if (! new_ppm_buffer) { |
| 3802 | opj_free(l_cp->ppm_buffer); |
| 3803 | l_cp->ppm_buffer = NULL; |
| 3804 | l_cp->ppm_len = 0; |
| 3805 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n"); |
| 3806 | return OPJ_FALSE; |
| 3807 | } |
| 3808 | l_cp->ppm_buffer = new_ppm_buffer; |
| 3809 | memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); |
| 3810 | |
| 3811 | l_cp->ppm_data = l_cp->ppm_buffer; |
| 3812 | } |
| 3813 | else { |
| 3814 | return OPJ_FALSE; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; |
| 3819 | |
| 3820 | if (l_remaining_data <= p_header_size) { |
| 3821 | /* we must store less information than available in the packet */ |
| 3822 | memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); |
| 3823 | l_cp->ppm_data_size = l_cp->ppm_len; |
| 3824 | p_header_size -= l_remaining_data; |
| 3825 | p_header_data += l_remaining_data; |
| 3826 | } |
| 3827 | else { |
| 3828 | memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); |
| 3829 | l_cp->ppm_data_size += p_header_size; |
| 3830 | p_header_data += p_header_size; |
| 3831 | p_header_size = 0; |
| 3832 | break; |
| 3833 | } |
| 3834 | } |
| 3835 | #endif |
| 3836 | return OPJ_TRUE; |
| 3837 | } |
| 3838 | |
| 3839 | /** |
| 3840 | * Reads a PPT marker (Packed packet headers, tile-part header) |
| 3841 | * |
| 3842 | * @param p_header_data the data contained in the PPT box. |
| 3843 | * @param p_j2k the jpeg2000 codec. |
| 3844 | * @param p_header_size the size of the data contained in the PPT marker. |
| 3845 | * @param p_manager the user event manager. |
| 3846 | */ |
| 3847 | static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, |
| 3848 | OPJ_BYTE * p_header_data, |
| 3849 | OPJ_UINT32 p_header_size, |
| 3850 | opj_event_mgr_t * p_manager |
| 3851 | ) |
| 3852 | { |
| 3853 | opj_cp_t *l_cp = 00; |
| 3854 | opj_tcp_t *l_tcp = 00; |
| 3855 | OPJ_UINT32 l_Z_ppt; |
| 3856 | |
| 3857 | /* preconditions */ |
| 3858 | assert(p_header_data != 00); |
| 3859 | assert(p_j2k != 00); |
| 3860 | assert(p_manager != 00); |
| 3861 | |
| 3862 | /* We need to have the Z_ppt element at minimum */ |
| 3863 | if (p_header_size < 1) { |
| 3864 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); |
| 3865 | return OPJ_FALSE; |
| 3866 | } |
| 3867 | |
| 3868 | l_cp = &(p_j2k->m_cp); |
| 3869 | if (l_cp->ppm){ |
| 3870 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); |
| 3871 | return OPJ_FALSE; |
| 3872 | } |
| 3873 | |
| 3874 | l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); |
| 3875 | l_tcp->ppt = 1; |
| 3876 | |
| 3877 | opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */ |
| 3878 | ++p_header_data; |
| 3879 | --p_header_size; |
| 3880 | |
| 3881 | /* Allocate buffer to read the packet header */ |
| 3882 | if (l_Z_ppt == 0) { |
| 3883 | /* First PPT marker */ |
| 3884 | l_tcp->ppt_data_size = 0; |
| 3885 | l_tcp->ppt_len = p_header_size; |
| 3886 | |
| 3887 | opj_free(l_tcp->ppt_buffer); |
| 3888 | l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) ); |
| 3889 | if (l_tcp->ppt_buffer == 00) { |
| 3890 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
| 3891 | return OPJ_FALSE; |
| 3892 | } |
| 3893 | l_tcp->ppt_data = l_tcp->ppt_buffer; |
| 3894 | |
| 3895 | /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */ |
| 3896 | } |
| 3897 | else { |
| 3898 | OPJ_BYTE *new_ppt_buffer; |
| 3899 | l_tcp->ppt_len += p_header_size; |
| 3900 | |
| 3901 | new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len); |
| 3902 | if (! new_ppt_buffer) { |
| 3903 | opj_free(l_tcp->ppt_buffer); |
| 3904 | l_tcp->ppt_buffer = NULL; |
| 3905 | l_tcp->ppt_len = 0; |
| 3906 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); |
| 3907 | return OPJ_FALSE; |
| 3908 | } |
| 3909 | l_tcp->ppt_buffer = new_ppt_buffer; |
| 3910 | l_tcp->ppt_data = l_tcp->ppt_buffer; |
| 3911 | |
| 3912 | memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size); |
| 3913 | } |
| 3914 | |
| 3915 | /* Read packet header from buffer */ |
| 3916 | memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size); |
| 3917 | |
| 3918 | l_tcp->ppt_data_size += p_header_size; |
| 3919 | |
| 3920 | return OPJ_TRUE; |
| 3921 | } |
| 3922 | |
| 3923 | OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k, |
| 3924 | opj_stream_private_t *p_stream, |
| 3925 | opj_event_mgr_t * p_manager |
| 3926 | ) |
| 3927 | { |
| 3928 | OPJ_BYTE * l_current_data = 00; |
| 3929 | OPJ_UINT32 l_tlm_size; |
| 3930 | |
| 3931 | /* preconditions */ |
| 3932 | assert(p_j2k != 00); |
| 3933 | assert(p_manager != 00); |
| 3934 | assert(p_stream != 00); |
| 3935 | |
| 3936 | l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
| 3937 | |
| 3938 | if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 3939 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); |
| 3940 | if (! new_header_tile_data) { |
| 3941 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 3942 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 3943 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 3944 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n"); |
| 3945 | return OPJ_FALSE; |
| 3946 | } |
| 3947 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 3948 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; |
| 3949 | } |
| 3950 | |
| 3951 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 3952 | |
| 3953 | /* change the way data is written to avoid seeking if possible */ |
| 3954 | /* TODO */ |
| 3955 | p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); |
| 3956 | |
| 3957 | opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */ |
| 3958 | l_current_data += 2; |
| 3959 | |
| 3960 | opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */ |
| 3961 | l_current_data += 2; |
| 3962 | |
| 3963 | opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/ |
| 3964 | ++l_current_data; |
| 3965 | |
| 3966 | opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ |
| 3967 | ++l_current_data; |
| 3968 | |
| 3969 | /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ |
| 3970 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) { |
| 3971 | return OPJ_FALSE; |
| 3972 | } |
| 3973 | |
| 3974 | return OPJ_TRUE; |
| 3975 | } |
| 3976 | |
| 3977 | OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k, |
| 3978 | OPJ_BYTE * p_data, |
| 3979 | OPJ_UINT32 * p_data_written, |
| 3980 | const opj_stream_private_t *p_stream, |
| 3981 | opj_event_mgr_t * p_manager |
| 3982 | ) |
| 3983 | { |
| 3984 | /* preconditions */ |
| 3985 | assert(p_j2k != 00); |
| 3986 | assert(p_manager != 00); |
| 3987 | assert(p_stream != 00); |
| 3988 | |
| 3989 | opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */ |
| 3990 | p_data += 2; |
| 3991 | |
| 3992 | opj_write_bytes(p_data,10,2); /* Lsot */ |
| 3993 | p_data += 2; |
| 3994 | |
| 3995 | opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */ |
| 3996 | p_data += 2; |
| 3997 | |
| 3998 | /* Psot */ |
| 3999 | p_data += 4; |
| 4000 | |
| 4001 | opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */ |
| 4002 | ++p_data; |
| 4003 | |
| 4004 | opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */ |
| 4005 | ++p_data; |
| 4006 | |
| 4007 | /* UniPG>> */ |
| 4008 | #ifdef USE_JPWL |
| 4009 | /* update markers struct */ |
| 4010 | /* |
| 4011 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); |
| 4012 | */ |
| 4013 | assert( 0 && "TODO" ); |
| 4014 | #endif /* USE_JPWL */ |
| 4015 | |
| 4016 | * p_data_written = 12; |
| 4017 | |
| 4018 | return OPJ_TRUE; |
| 4019 | } |
| 4020 | |
| 4021 | OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k, |
| 4022 | OPJ_BYTE * p_header_data, |
| 4023 | OPJ_UINT32 p_header_size, |
| 4024 | opj_event_mgr_t * p_manager ) |
| 4025 | { |
| 4026 | opj_cp_t *l_cp = 00; |
| 4027 | opj_tcp_t *l_tcp = 00; |
| 4028 | OPJ_UINT32 l_tot_len, l_num_parts = 0; |
| 4029 | OPJ_UINT32 l_current_part; |
| 4030 | OPJ_UINT32 l_tile_x,l_tile_y; |
| 4031 | |
| 4032 | /* preconditions */ |
| 4033 | assert(p_header_data != 00); |
| 4034 | assert(p_j2k != 00); |
| 4035 | assert(p_manager != 00); |
| 4036 | |
| 4037 | /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ |
| 4038 | if (p_header_size != 8) { |
| 4039 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); |
| 4040 | return OPJ_FALSE; |
| 4041 | } |
| 4042 | |
| 4043 | l_cp = &(p_j2k->m_cp); |
| 4044 | opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */ |
| 4045 | p_header_data+=2; |
| 4046 | |
| 4047 | /* testcase 2.pdf.SIGFPE.706.1112 */ |
| 4048 | if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { |
| 4049 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number); |
| 4050 | return OPJ_FALSE; |
| 4051 | } |
| 4052 | |
| 4053 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
| 4054 | l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; |
| 4055 | l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; |
| 4056 | |
| 4057 | #ifdef USE_JPWL |
| 4058 | if (l_cp->correct) { |
| 4059 | |
| 4060 | OPJ_UINT32 tileno = p_j2k->m_current_tile_number; |
| 4061 | static OPJ_UINT32 backup_tileno = 0; |
| 4062 | |
| 4063 | /* tileno is negative or larger than the number of tiles!!! */ |
| 4064 | if (tileno > (l_cp->tw * l_cp->th)) { |
| 4065 | opj_event_msg(p_manager, EVT_ERROR, |
| 4066 | "JPWL: bad tile number (%d out of a maximum of %d)\n", |
| 4067 | tileno, (l_cp->tw * l_cp->th)); |
| 4068 | if (!JPWL_ASSUME) { |
| 4069 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 4070 | return OPJ_FALSE; |
| 4071 | } |
| 4072 | /* we try to correct */ |
| 4073 | tileno = backup_tileno; |
| 4074 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
| 4075 | "- setting tile number to %d\n", |
| 4076 | tileno); |
| 4077 | } |
| 4078 | |
| 4079 | /* keep your private count of tiles */ |
| 4080 | backup_tileno++; |
| 4081 | }; |
| 4082 | #endif /* USE_JPWL */ |
| 4083 | |
| 4084 | /* look for the tile in the list of already processed tile (in parts). */ |
| 4085 | /* Optimization possible here with a more complex data structure and with the removing of tiles */ |
| 4086 | /* since the time taken by this function can only grow at the time */ |
| 4087 | |
| 4088 | opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */ |
| 4089 | p_header_data+=4; |
| 4090 | |
| 4091 | /* PSot should be equal to zero or >=14 or <= 2^32-1 */ |
| 4092 | if ((l_tot_len !=0 ) && (l_tot_len < 14) ) |
| 4093 | { |
| 4094 | if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/ |
| 4095 | { |
| 4096 | opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len); |
| 4097 | } |
| 4098 | else |
| 4099 | { |
| 4100 | opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); |
| 4101 | return OPJ_FALSE; |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | #ifdef USE_JPWL |
| 4106 | if (l_cp->correct) { |
| 4107 | |
| 4108 | /* totlen is negative or larger than the bytes left!!! */ |
| 4109 | if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ |
| 4110 | opj_event_msg(p_manager, EVT_ERROR, |
| 4111 | "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", |
| 4112 | l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ |
| 4113 | if (!JPWL_ASSUME) { |
| 4114 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 4115 | return OPJ_FALSE; |
| 4116 | } |
| 4117 | /* we try to correct */ |
| 4118 | l_tot_len = 0; |
| 4119 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
| 4120 | "- setting Psot to %d => assuming it is the last tile\n", |
| 4121 | l_tot_len); |
| 4122 | } |
| 4123 | }; |
| 4124 | #endif /* USE_JPWL */ |
| 4125 | |
| 4126 | /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ |
| 4127 | if (!l_tot_len) { |
| 4128 | opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, " |
| 4129 | "we assuming it is the last tile-part of the codestream.\n"); |
| 4130 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
| 4131 | } |
| 4132 | |
| 4133 | opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */ |
| 4134 | ++p_header_data; |
| 4135 | |
| 4136 | opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */ |
| 4137 | ++p_header_data; |
| 4138 | |
| 4139 | if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ |
| 4140 | /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of |
| 4141 | * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ |
| 4142 | if (l_tcp->m_nb_tile_parts) { |
| 4143 | if (l_current_part >= l_tcp->m_nb_tile_parts){ |
| 4144 | opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " |
| 4145 | "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts ); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4146 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
| 4147 | return OPJ_FALSE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | if( l_current_part >= l_num_parts ) { |
| 4151 | /* testcase 451.pdf.SIGSEGV.ce9.3723 */ |
| 4152 | opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " |
| 4153 | "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts ); |
| 4154 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
| 4155 | return OPJ_FALSE; |
| 4156 | } |
| 4157 | l_tcp->m_nb_tile_parts = l_num_parts; |
| 4158 | } |
| 4159 | |
| 4160 | /* If know the number of tile part header we will check if we didn't read the last*/ |
| 4161 | if (l_tcp->m_nb_tile_parts) { |
| 4162 | if (l_tcp->m_nb_tile_parts == (l_current_part+1)) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4163 | p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){ |
| 4168 | /* Keep the size of data to skip after this marker */ |
| 4169 | p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */ |
| 4170 | } |
| 4171 | else { |
| 4172 | /* FIXME: need to be computed from the number of bytes remaining in the codestream */ |
| 4173 | p_j2k->m_specific_param.m_decoder.m_sot_length = 0; |
| 4174 | } |
| 4175 | |
| 4176 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; |
| 4177 | |
| 4178 | /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ |
| 4179 | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { |
| 4180 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
| 4181 | (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) |
| 4182 | || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) |
| 4183 | || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) |
| 4184 | || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); |
| 4185 | } |
| 4186 | else { |
| 4187 | assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 ); |
| 4188 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
| 4189 | (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); |
| 4190 | } |
| 4191 | |
| 4192 | /* Index */ |
| 4193 | if (p_j2k->cstr_index) |
| 4194 | { |
| 4195 | assert(p_j2k->cstr_index->tile_index != 00); |
| 4196 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; |
| 4197 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part; |
| 4198 | |
| 4199 | if (l_num_parts != 0){ |
| 4200 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts; |
| 4201 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts; |
| 4202 | |
| 4203 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
| 4204 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
| 4205 | (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4206 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
| 4207 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
| 4208 | return OPJ_FALSE; |
| 4209 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4210 | } |
| 4211 | else { |
| 4212 | opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( |
| 4213 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t)); |
| 4214 | if (! new_tp_index) { |
| 4215 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
| 4216 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4217 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4218 | return OPJ_FALSE; |
| 4219 | } |
| 4220 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index; |
| 4221 | } |
| 4222 | } |
| 4223 | else{ |
| 4224 | /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { |
| 4225 | |
| 4226 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
| 4227 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; |
| 4228 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
| 4229 | (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, |
| 4230 | sizeof(opj_tp_index_t)); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4231 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
| 4232 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
| 4233 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
| 4234 | return OPJ_FALSE; |
| 4235 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ |
| 4239 | opj_tp_index_t *new_tp_index; |
| 4240 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1; |
| 4241 | new_tp_index = (opj_tp_index_t *) opj_realloc( |
| 4242 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
| 4243 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t)); |
| 4244 | if (! new_tp_index) { |
| 4245 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
| 4246 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
| 4247 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4248 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4249 | return OPJ_FALSE; |
| 4250 | } |
| 4251 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index; |
| 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | } |
| 4256 | |
| 4257 | } |
| 4258 | |
| 4259 | /* 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 */ |
| 4260 | /* if (p_j2k->cstr_info) { |
| 4261 | if (l_tcp->first) { |
| 4262 | if (tileno == 0) { |
| 4263 | p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; |
| 4264 | } |
| 4265 | |
| 4266 | p_j2k->cstr_info->tile[tileno].tileno = tileno; |
| 4267 | p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; |
| 4268 | p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; |
| 4269 | p_j2k->cstr_info->tile[tileno].num_tps = numparts; |
| 4270 | |
| 4271 | if (numparts) { |
| 4272 | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); |
| 4273 | } |
| 4274 | else { |
| 4275 | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) |
| 4276 | } |
| 4277 | } |
| 4278 | else { |
| 4279 | p_j2k->cstr_info->tile[tileno].end_pos += totlen; |
| 4280 | } |
| 4281 | |
| 4282 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; |
| 4283 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = |
| 4284 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; |
| 4285 | }*/ |
| 4286 | return OPJ_TRUE; |
| 4287 | } |
| 4288 | |
| 4289 | OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k, |
| 4290 | opj_tcd_t * p_tile_coder, |
| 4291 | OPJ_BYTE * p_data, |
| 4292 | OPJ_UINT32 * p_data_written, |
| 4293 | OPJ_UINT32 p_total_data_size, |
| 4294 | const opj_stream_private_t *p_stream, |
| 4295 | opj_event_mgr_t * p_manager |
| 4296 | ) |
| 4297 | { |
| 4298 | opj_codestream_info_t *l_cstr_info = 00; |
| 4299 | OPJ_UINT32 l_remaining_data; |
| 4300 | |
| 4301 | /* preconditions */ |
| 4302 | assert(p_j2k != 00); |
| 4303 | assert(p_manager != 00); |
| 4304 | assert(p_stream != 00); |
| 4305 | |
| 4306 | opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */ |
| 4307 | p_data += 2; |
| 4308 | |
| 4309 | /* make room for the EOF marker */ |
| 4310 | l_remaining_data = p_total_data_size - 4; |
| 4311 | |
| 4312 | /* update tile coder */ |
| 4313 | p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; |
| 4314 | p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
| 4315 | |
| 4316 | /* INDEX >> */ |
| 4317 | /* TODO mergeV2: check this part which use cstr_info */ |
| 4318 | /*l_cstr_info = p_j2k->cstr_info; |
| 4319 | if (l_cstr_info) { |
| 4320 | if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { |
| 4321 | //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; |
| 4322 | l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; |
| 4323 | } |
| 4324 | else {*/ |
| 4325 | /* |
| 4326 | TODO |
| 4327 | if |
| 4328 | (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) |
| 4329 | { |
| 4330 | cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); |
| 4331 | }*/ |
| 4332 | /*}*/ |
| 4333 | /* UniPG>> */ |
| 4334 | #ifdef USE_JPWL |
| 4335 | /* update markers struct */ |
| 4336 | /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); |
| 4337 | */ |
| 4338 | assert( 0 && "TODO" ); |
| 4339 | #endif /* USE_JPWL */ |
| 4340 | /* <<UniPG */ |
| 4341 | /*}*/ |
| 4342 | /* << INDEX */ |
| 4343 | |
| 4344 | if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) { |
| 4345 | p_tile_coder->tcd_image->tiles->packno = 0; |
| 4346 | if (l_cstr_info) { |
| 4347 | l_cstr_info->packno = 0; |
| 4348 | } |
| 4349 | } |
| 4350 | |
| 4351 | *p_data_written = 0; |
| 4352 | |
| 4353 | if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) { |
| 4354 | opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); |
| 4355 | return OPJ_FALSE; |
| 4356 | } |
| 4357 | |
| 4358 | *p_data_written += 2; |
| 4359 | |
| 4360 | return OPJ_TRUE; |
| 4361 | } |
| 4362 | |
| 4363 | OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k, |
| 4364 | opj_stream_private_t *p_stream, |
| 4365 | opj_event_mgr_t * p_manager |
| 4366 | ) |
| 4367 | { |
| 4368 | OPJ_SIZE_T l_current_read_size; |
| 4369 | opj_codestream_index_t * l_cstr_index = 00; |
| 4370 | OPJ_BYTE ** l_current_data = 00; |
| 4371 | opj_tcp_t * l_tcp = 00; |
| 4372 | OPJ_UINT32 * l_tile_len = 00; |
| 4373 | OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; |
| 4374 | |
| 4375 | /* preconditions */ |
| 4376 | assert(p_j2k != 00); |
| 4377 | assert(p_manager != 00); |
| 4378 | assert(p_stream != 00); |
| 4379 | |
| 4380 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
| 4381 | |
| 4382 | if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
| 4383 | /* opj_stream_get_number_byte_left returns OPJ_OFF_T |
| 4384 | // but we are in the last tile part, |
| 4385 | // so its result will fit on OPJ_UINT32 unless we find |
| 4386 | // a file with a single tile part of more than 4 GB...*/ |
| 4387 | p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2); |
| 4388 | } |
| 4389 | else { |
| 4390 | /* Check to avoid pass the limit of OPJ_UINT32 */ |
| 4391 | if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 ) |
| 4392 | p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; |
| 4393 | else { |
| 4394 | /* MSD: case commented to support empty SOT marker (PHR data) */ |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | l_current_data = &(l_tcp->m_data); |
| 4399 | l_tile_len = &l_tcp->m_data_size; |
| 4400 | |
| 4401 | /* Patch to support new PHR data */ |
| 4402 | if (p_j2k->m_specific_param.m_decoder.m_sot_length) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4403 | /* If we are here, we'll try to read the data after allocation */ |
| 4404 | /* Check enough bytes left in stream before allocation */ |
| 4405 | if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > opj_stream_get_number_byte_left(p_stream)) { |
| 4406 | opj_event_msg(p_manager, EVT_ERROR, "Tile part length size inconsistent with stream length\n"); |
| 4407 | return OPJ_FALSE; |
| 4408 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4409 | if (! *l_current_data) { |
| 4410 | /* LH: oddly enough, in this path, l_tile_len!=0. |
| 4411 | * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). |
| 4412 | */ |
| 4413 | *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length); |
| 4414 | } |
| 4415 | else { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4416 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4417 | if (! l_new_current_data) { |
| 4418 | opj_free(*l_current_data); |
| 4419 | /*nothing more is done as l_current_data will be set to null, and just |
| 4420 | afterward we enter in the error path |
| 4421 | and the actual tile_len is updated (committed) at the end of the |
| 4422 | function. */ |
| 4423 | } |
| 4424 | *l_current_data = l_new_current_data; |
| 4425 | } |
| 4426 | |
| 4427 | if (*l_current_data == 00) { |
| 4428 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); |
| 4429 | return OPJ_FALSE; |
| 4430 | } |
| 4431 | } |
| 4432 | else { |
| 4433 | l_sot_length_pb_detected = OPJ_TRUE; |
| 4434 | } |
| 4435 | |
| 4436 | /* Index */ |
| 4437 | l_cstr_index = p_j2k->cstr_index; |
| 4438 | if (l_cstr_index) { |
| 4439 | OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; |
| 4440 | |
| 4441 | OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; |
| 4442 | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header = |
| 4443 | l_current_pos; |
| 4444 | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos = |
| 4445 | l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; |
| 4446 | |
| 4447 | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
| 4448 | l_cstr_index, |
| 4449 | J2K_MS_SOD, |
| 4450 | l_current_pos, |
| 4451 | p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { |
| 4452 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
| 4453 | return OPJ_FALSE; |
| 4454 | } |
| 4455 | |
| 4456 | /*l_cstr_index->packno = 0;*/ |
| 4457 | } |
| 4458 | |
| 4459 | /* Patch to support new PHR data */ |
| 4460 | if (!l_sot_length_pb_detected) { |
| 4461 | l_current_read_size = opj_stream_read_data( |
| 4462 | p_stream, |
| 4463 | *l_current_data + *l_tile_len, |
| 4464 | p_j2k->m_specific_param.m_decoder.m_sot_length, |
| 4465 | p_manager); |
| 4466 | } |
| 4467 | else |
| 4468 | { |
| 4469 | l_current_read_size = 0; |
| 4470 | } |
| 4471 | |
| 4472 | if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
| 4473 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 4474 | } |
| 4475 | else { |
| 4476 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
| 4477 | } |
| 4478 | |
| 4479 | *l_tile_len += (OPJ_UINT32)l_current_read_size; |
| 4480 | |
| 4481 | return OPJ_TRUE; |
| 4482 | } |
| 4483 | |
| 4484 | OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
| 4485 | OPJ_UINT32 p_tile_no, |
| 4486 | OPJ_UINT32 p_comp_no, |
| 4487 | OPJ_UINT32 nb_comps, |
| 4488 | opj_stream_private_t *p_stream, |
| 4489 | opj_event_mgr_t * p_manager |
| 4490 | ) |
| 4491 | { |
| 4492 | OPJ_BYTE * l_current_data = 00; |
| 4493 | OPJ_UINT32 l_rgn_size; |
| 4494 | opj_cp_t *l_cp = 00; |
| 4495 | opj_tcp_t *l_tcp = 00; |
| 4496 | opj_tccp_t *l_tccp = 00; |
| 4497 | OPJ_UINT32 l_comp_room; |
| 4498 | |
| 4499 | /* preconditions */ |
| 4500 | assert(p_j2k != 00); |
| 4501 | assert(p_manager != 00); |
| 4502 | assert(p_stream != 00); |
| 4503 | |
| 4504 | l_cp = &(p_j2k->m_cp); |
| 4505 | l_tcp = &l_cp->tcps[p_tile_no]; |
| 4506 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 4507 | |
| 4508 | if (nb_comps <= 256) { |
| 4509 | l_comp_room = 1; |
| 4510 | } |
| 4511 | else { |
| 4512 | l_comp_room = 2; |
| 4513 | } |
| 4514 | |
| 4515 | l_rgn_size = 6 + l_comp_room; |
| 4516 | |
| 4517 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 4518 | |
| 4519 | opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */ |
| 4520 | l_current_data += 2; |
| 4521 | |
| 4522 | opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */ |
| 4523 | l_current_data += 2; |
| 4524 | |
| 4525 | opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */ |
| 4526 | l_current_data+=l_comp_room; |
| 4527 | |
| 4528 | opj_write_bytes(l_current_data, 0,1); /* Srgn */ |
| 4529 | ++l_current_data; |
| 4530 | |
| 4531 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1); /* SPrgn */ |
| 4532 | ++l_current_data; |
| 4533 | |
| 4534 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) { |
| 4535 | return OPJ_FALSE; |
| 4536 | } |
| 4537 | |
| 4538 | return OPJ_TRUE; |
| 4539 | } |
| 4540 | |
| 4541 | OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k, |
| 4542 | opj_stream_private_t *p_stream, |
| 4543 | opj_event_mgr_t * p_manager |
| 4544 | ) |
| 4545 | { |
| 4546 | /* preconditions */ |
| 4547 | assert(p_j2k != 00); |
| 4548 | assert(p_manager != 00); |
| 4549 | assert(p_stream != 00); |
| 4550 | |
| 4551 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */ |
| 4552 | |
| 4553 | /* UniPG>> */ |
| 4554 | #ifdef USE_JPWL |
| 4555 | /* update markers struct */ |
| 4556 | /* |
| 4557 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); |
| 4558 | */ |
| 4559 | #endif /* USE_JPWL */ |
| 4560 | |
| 4561 | if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) { |
| 4562 | return OPJ_FALSE; |
| 4563 | } |
| 4564 | |
| 4565 | if ( ! opj_stream_flush(p_stream,p_manager) ) { |
| 4566 | return OPJ_FALSE; |
| 4567 | } |
| 4568 | |
| 4569 | return OPJ_TRUE; |
| 4570 | } |
| 4571 | |
| 4572 | /** |
| 4573 | * Reads a RGN marker (Region Of Interest) |
| 4574 | * |
| 4575 | * @param p_header_data the data contained in the POC box. |
| 4576 | * @param p_j2k the jpeg2000 codec. |
| 4577 | * @param p_header_size the size of the data contained in the POC marker. |
| 4578 | * @param p_manager the user event manager. |
| 4579 | */ |
| 4580 | static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k, |
| 4581 | OPJ_BYTE * p_header_data, |
| 4582 | OPJ_UINT32 p_header_size, |
| 4583 | opj_event_mgr_t * p_manager |
| 4584 | ) |
| 4585 | { |
| 4586 | OPJ_UINT32 l_nb_comp; |
| 4587 | opj_image_t * l_image = 00; |
| 4588 | |
| 4589 | opj_cp_t *l_cp = 00; |
| 4590 | opj_tcp_t *l_tcp = 00; |
| 4591 | OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; |
| 4592 | |
| 4593 | /* preconditions*/ |
| 4594 | assert(p_header_data != 00); |
| 4595 | assert(p_j2k != 00); |
| 4596 | assert(p_manager != 00); |
| 4597 | |
| 4598 | l_image = p_j2k->m_private_image; |
| 4599 | l_nb_comp = l_image->numcomps; |
| 4600 | |
| 4601 | if (l_nb_comp <= 256) { |
| 4602 | l_comp_room = 1; } |
| 4603 | else { |
| 4604 | l_comp_room = 2; } |
| 4605 | |
| 4606 | if (p_header_size != 2 + l_comp_room) { |
| 4607 | opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); |
| 4608 | return OPJ_FALSE; |
| 4609 | } |
| 4610 | |
| 4611 | l_cp = &(p_j2k->m_cp); |
| 4612 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
| 4613 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 4614 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 4615 | |
| 4616 | opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */ |
| 4617 | p_header_data+=l_comp_room; |
| 4618 | opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */ |
| 4619 | ++p_header_data; |
| 4620 | |
| 4621 | #ifdef USE_JPWL |
| 4622 | if (l_cp->correct) { |
| 4623 | /* totlen is negative or larger than the bytes left!!! */ |
| 4624 | if (l_comp_room >= l_nb_comp) { |
| 4625 | opj_event_msg(p_manager, EVT_ERROR, |
| 4626 | "JPWL: bad component number in RGN (%d when there are only %d)\n", |
| 4627 | l_comp_room, l_nb_comp); |
| 4628 | if (!JPWL_ASSUME || JPWL_ASSUME) { |
| 4629 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 4630 | return OPJ_FALSE; |
| 4631 | } |
| 4632 | } |
| 4633 | }; |
| 4634 | #endif /* USE_JPWL */ |
| 4635 | |
| 4636 | /* testcase 3635.pdf.asan.77.2930 */ |
| 4637 | if (l_comp_no >= l_nb_comp) { |
| 4638 | opj_event_msg(p_manager, EVT_ERROR, |
| 4639 | "bad component number in RGN (%d when there are only %d)\n", |
| 4640 | l_comp_no, l_nb_comp); |
| 4641 | return OPJ_FALSE; |
| 4642 | } |
| 4643 | |
| 4644 | opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ |
| 4645 | ++p_header_data; |
| 4646 | |
| 4647 | return OPJ_TRUE; |
| 4648 | |
| 4649 | } |
| 4650 | |
| 4651 | OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp) |
| 4652 | { |
| 4653 | return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14); |
| 4654 | } |
| 4655 | |
| 4656 | OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp) |
| 4657 | { |
| 4658 | (void)p_tcp; |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
| 4662 | OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k, |
| 4663 | opj_stream_private_t *p_stream, |
| 4664 | opj_event_mgr_t * p_manager ) |
| 4665 | { |
| 4666 | opj_cp_t * l_cp = 00; |
| 4667 | opj_image_t * l_image = 00; |
| 4668 | opj_tcp_t * l_tcp = 00; |
| 4669 | opj_image_comp_t * l_img_comp = 00; |
| 4670 | |
| 4671 | OPJ_UINT32 i,j,k; |
| 4672 | OPJ_INT32 l_x0,l_y0,l_x1,l_y1; |
| 4673 | OPJ_FLOAT32 * l_rates = 0; |
| 4674 | OPJ_FLOAT32 l_sot_remove; |
| 4675 | OPJ_UINT32 l_bits_empty, l_size_pixel; |
| 4676 | OPJ_UINT32 l_tile_size = 0; |
| 4677 | OPJ_UINT32 l_last_res; |
| 4678 | OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00; |
| 4679 | |
| 4680 | /* preconditions */ |
| 4681 | assert(p_j2k != 00); |
| 4682 | assert(p_manager != 00); |
| 4683 | assert(p_stream != 00); |
| 4684 | |
| 4685 | l_cp = &(p_j2k->m_cp); |
| 4686 | l_image = p_j2k->m_private_image; |
| 4687 | l_tcp = l_cp->tcps; |
| 4688 | |
| 4689 | l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; |
| 4690 | l_size_pixel = l_image->numcomps * l_image->comps->prec; |
| 4691 | l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(l_cp->th * l_cp->tw); |
| 4692 | |
| 4693 | if (l_cp->m_specific_param.m_enc.m_tp_on) { |
| 4694 | l_tp_stride_func = opj_j2k_get_tp_stride; |
| 4695 | } |
| 4696 | else { |
| 4697 | l_tp_stride_func = opj_j2k_get_default_stride; |
| 4698 | } |
| 4699 | |
| 4700 | for (i=0;i<l_cp->th;++i) { |
| 4701 | for (j=0;j<l_cp->tw;++j) { |
| 4702 | OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers; |
| 4703 | |
| 4704 | /* 4 borders of the tile rescale on the image if necessary */ |
| 4705 | l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0); |
| 4706 | l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0); |
| 4707 | l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1); |
| 4708 | l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1); |
| 4709 | |
| 4710 | l_rates = l_tcp->rates; |
| 4711 | |
| 4712 | /* Modification of the RATE >> */ |
| 4713 | if (*l_rates) { |
| 4714 | *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0))) |
| 4715 | / |
| 4716 | ((*l_rates) * (OPJ_FLOAT32)l_bits_empty) |
| 4717 | ) |
| 4718 | - |
| 4719 | l_offset; |
| 4720 | } |
| 4721 | |
| 4722 | ++l_rates; |
| 4723 | |
| 4724 | for (k = 1; k < l_tcp->numlayers; ++k) { |
| 4725 | if (*l_rates) { |
| 4726 | *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0))) |
| 4727 | / |
| 4728 | ((*l_rates) * (OPJ_FLOAT32)l_bits_empty) |
| 4729 | ) |
| 4730 | - |
| 4731 | l_offset; |
| 4732 | } |
| 4733 | |
| 4734 | ++l_rates; |
| 4735 | } |
| 4736 | |
| 4737 | ++l_tcp; |
| 4738 | |
| 4739 | } |
| 4740 | } |
| 4741 | |
| 4742 | l_tcp = l_cp->tcps; |
| 4743 | |
| 4744 | for (i=0;i<l_cp->th;++i) { |
| 4745 | for (j=0;j<l_cp->tw;++j) { |
| 4746 | l_rates = l_tcp->rates; |
| 4747 | |
| 4748 | if (*l_rates) { |
| 4749 | *l_rates -= l_sot_remove; |
| 4750 | |
| 4751 | if (*l_rates < 30) { |
| 4752 | *l_rates = 30; |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | ++l_rates; |
| 4757 | |
| 4758 | l_last_res = l_tcp->numlayers - 1; |
| 4759 | |
| 4760 | for (k = 1; k < l_last_res; ++k) { |
| 4761 | |
| 4762 | if (*l_rates) { |
| 4763 | *l_rates -= l_sot_remove; |
| 4764 | |
| 4765 | if (*l_rates < *(l_rates - 1) + 10) { |
| 4766 | *l_rates = (*(l_rates - 1)) + 20; |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | ++l_rates; |
| 4771 | } |
| 4772 | |
| 4773 | if (*l_rates) { |
| 4774 | *l_rates -= (l_sot_remove + 2.f); |
| 4775 | |
| 4776 | if (*l_rates < *(l_rates - 1) + 10) { |
| 4777 | *l_rates = (*(l_rates - 1)) + 20; |
| 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | ++l_tcp; |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | l_img_comp = l_image->comps; |
| 4786 | l_tile_size = 0; |
| 4787 | |
| 4788 | for (i=0;i<l_image->numcomps;++i) { |
| 4789 | l_tile_size += ( opj_uint_ceildiv(l_cp->tdx,l_img_comp->dx) |
| 4790 | * |
| 4791 | opj_uint_ceildiv(l_cp->tdy,l_img_comp->dy) |
| 4792 | * |
| 4793 | l_img_comp->prec |
| 4794 | ); |
| 4795 | |
| 4796 | ++l_img_comp; |
| 4797 | } |
| 4798 | |
| 4799 | l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */ |
| 4800 | |
| 4801 | l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); |
| 4802 | |
| 4803 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size; |
| 4804 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = |
| 4805 | (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); |
| 4806 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { |
| 4807 | return OPJ_FALSE; |
| 4808 | } |
| 4809 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 4810 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 4811 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = |
| 4812 | (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
| 4813 | if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
| 4814 | return OPJ_FALSE; |
| 4815 | } |
| 4816 | |
| 4817 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = |
| 4818 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; |
| 4819 | } |
| 4820 | |
| 4821 | return OPJ_TRUE; |
| 4822 | } |
| 4823 | |
| 4824 | #if 0 |
| 4825 | OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, |
| 4826 | opj_stream_private_t *p_stream, |
| 4827 | opj_event_mgr_t * p_manager ) |
| 4828 | { |
| 4829 | OPJ_UINT32 i; |
| 4830 | opj_tcd_t * l_tcd = 00; |
| 4831 | OPJ_UINT32 l_nb_tiles; |
| 4832 | opj_tcp_t * l_tcp = 00; |
| 4833 | OPJ_BOOL l_success; |
| 4834 | |
| 4835 | /* preconditions */ |
| 4836 | assert(p_j2k != 00); |
| 4837 | assert(p_manager != 00); |
| 4838 | assert(p_stream != 00); |
| 4839 | |
| 4840 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 4841 | l_tcp = p_j2k->m_cp.tcps; |
| 4842 | |
| 4843 | l_tcd = opj_tcd_create(OPJ_TRUE); |
| 4844 | if (l_tcd == 00) { |
| 4845 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
| 4846 | return OPJ_FALSE; |
| 4847 | } |
| 4848 | |
| 4849 | for (i = 0; i < l_nb_tiles; ++i) { |
| 4850 | if (l_tcp->m_data) { |
| 4851 | if (! opj_tcd_init_decode_tile(l_tcd, i)) { |
| 4852 | opj_tcd_destroy(l_tcd); |
| 4853 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
| 4854 | return OPJ_FALSE; |
| 4855 | } |
| 4856 | |
| 4857 | l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index); |
| 4858 | /* cleanup */ |
| 4859 | |
| 4860 | if (! l_success) { |
| 4861 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
| 4862 | break; |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | opj_j2k_tcp_destroy(l_tcp); |
| 4867 | ++l_tcp; |
| 4868 | } |
| 4869 | |
| 4870 | opj_tcd_destroy(l_tcd); |
| 4871 | return OPJ_TRUE; |
| 4872 | } |
| 4873 | #endif |
| 4874 | |
| 4875 | OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
| 4876 | struct opj_stream_private *p_stream, |
| 4877 | struct opj_event_mgr * p_manager ) |
| 4878 | { |
| 4879 | /* preconditions */ |
| 4880 | assert(p_j2k != 00); |
| 4881 | assert(p_manager != 00); |
| 4882 | assert(p_stream != 00); |
| 4883 | |
| 4884 | p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); |
| 4885 | |
| 4886 | return OPJ_TRUE; |
| 4887 | } |
| 4888 | |
| 4889 | OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, |
| 4890 | struct opj_stream_private *p_stream, |
| 4891 | struct opj_event_mgr * p_manager ) |
| 4892 | { |
| 4893 | OPJ_UINT32 i; |
| 4894 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
| 4895 | opj_mct_data_t * l_mct_record; |
| 4896 | opj_tcp_t * l_tcp; |
| 4897 | |
| 4898 | /* preconditions */ |
| 4899 | assert(p_j2k != 00); |
| 4900 | assert(p_stream != 00); |
| 4901 | assert(p_manager != 00); |
| 4902 | |
| 4903 | if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) { |
| 4904 | return OPJ_FALSE; |
| 4905 | } |
| 4906 | |
| 4907 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
| 4908 | l_mct_record = l_tcp->m_mct_records; |
| 4909 | |
| 4910 | for (i=0;i<l_tcp->m_nb_mct_records;++i) { |
| 4911 | |
| 4912 | if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) { |
| 4913 | return OPJ_FALSE; |
| 4914 | } |
| 4915 | |
| 4916 | ++l_mct_record; |
| 4917 | } |
| 4918 | |
| 4919 | l_mcc_record = l_tcp->m_mcc_records; |
| 4920 | |
| 4921 | for (i=0;i<l_tcp->m_nb_mcc_records;++i) { |
| 4922 | |
| 4923 | if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) { |
| 4924 | return OPJ_FALSE; |
| 4925 | } |
| 4926 | |
| 4927 | ++l_mcc_record; |
| 4928 | } |
| 4929 | |
| 4930 | if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) { |
| 4931 | return OPJ_FALSE; |
| 4932 | } |
| 4933 | |
| 4934 | return OPJ_TRUE; |
| 4935 | } |
| 4936 | |
| 4937 | #if 0 |
| 4938 | OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k, |
| 4939 | struct opj_stream_private *p_stream, |
| 4940 | struct opj_event_mgr * p_manager ) |
| 4941 | { |
| 4942 | OPJ_UINT32 compno; |
| 4943 | |
| 4944 | /* preconditions */ |
| 4945 | assert(p_j2k != 00); |
| 4946 | assert(p_manager != 00); |
| 4947 | assert(p_stream != 00); |
| 4948 | |
| 4949 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) |
| 4950 | { |
| 4951 | if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) { |
| 4952 | return OPJ_FALSE; |
| 4953 | } |
| 4954 | } |
| 4955 | |
| 4956 | return OPJ_TRUE; |
| 4957 | } |
| 4958 | #endif |
| 4959 | |
| 4960 | #if 0 |
| 4961 | OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, |
| 4962 | struct opj_stream_private *p_stream, |
| 4963 | struct opj_event_mgr * p_manager ) |
| 4964 | { |
| 4965 | OPJ_UINT32 compno; |
| 4966 | |
| 4967 | /* preconditions */ |
| 4968 | assert(p_j2k != 00); |
| 4969 | assert(p_manager != 00); |
| 4970 | assert(p_stream != 00); |
| 4971 | |
| 4972 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) |
| 4973 | { |
| 4974 | if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) { |
| 4975 | return OPJ_FALSE; |
| 4976 | } |
| 4977 | } |
| 4978 | |
| 4979 | return OPJ_TRUE; |
| 4980 | } |
| 4981 | #endif |
| 4982 | |
| 4983 | |
| 4984 | OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, |
| 4985 | struct opj_stream_private *p_stream, |
| 4986 | struct opj_event_mgr * p_manager ) |
| 4987 | { |
| 4988 | OPJ_UINT32 compno; |
| 4989 | const opj_tccp_t *l_tccp = 00; |
| 4990 | |
| 4991 | /* preconditions */ |
| 4992 | assert(p_j2k != 00); |
| 4993 | assert(p_manager != 00); |
| 4994 | assert(p_stream != 00); |
| 4995 | |
| 4996 | l_tccp = p_j2k->m_cp.tcps->tccps; |
| 4997 | |
| 4998 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
| 4999 | if (l_tccp->roishift) { |
| 5000 | |
| 5001 | if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream,p_manager)) { |
| 5002 | return OPJ_FALSE; |
| 5003 | } |
| 5004 | } |
| 5005 | |
| 5006 | ++l_tccp; |
| 5007 | } |
| 5008 | |
| 5009 | return OPJ_TRUE; |
| 5010 | } |
| 5011 | |
| 5012 | OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, |
| 5013 | struct opj_stream_private *p_stream, |
| 5014 | struct opj_event_mgr * p_manager ) |
| 5015 | { |
| 5016 | opj_codestream_index_t * l_cstr_index = 00; |
| 5017 | |
| 5018 | /* preconditions */ |
| 5019 | assert(p_j2k != 00); |
| 5020 | assert(p_manager != 00); |
| 5021 | assert(p_stream != 00); |
| 5022 | |
| 5023 | l_cstr_index = p_j2k->cstr_index; |
| 5024 | if (l_cstr_index) { |
| 5025 | l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream); |
| 5026 | /* UniPG>> */ |
| 5027 | /* The following adjustment is done to adjust the codestream size */ |
| 5028 | /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ |
| 5029 | /* the first bunch of bytes is not in the codestream */ |
| 5030 | l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start; |
| 5031 | /* <<UniPG */ |
| 5032 | } |
| 5033 | |
| 5034 | #ifdef USE_JPWL |
| 5035 | /* preparation of JPWL marker segments */ |
| 5036 | #if 0 |
| 5037 | if(cp->epc_on) { |
| 5038 | |
| 5039 | /* encode according to JPWL */ |
| 5040 | jpwl_encode(p_j2k, p_stream, image); |
| 5041 | |
| 5042 | } |
| 5043 | #endif |
| 5044 | assert( 0 && "TODO" ); |
| 5045 | #endif /* USE_JPWL */ |
| 5046 | |
| 5047 | return OPJ_TRUE; |
| 5048 | } |
| 5049 | |
| 5050 | OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, |
| 5051 | opj_stream_private_t *p_stream, |
| 5052 | OPJ_UINT32 *output_marker, |
| 5053 | opj_event_mgr_t * p_manager |
| 5054 | ) |
| 5055 | { |
| 5056 | OPJ_UINT32 l_unknown_marker; |
| 5057 | const opj_dec_memory_marker_handler_t * l_marker_handler; |
| 5058 | OPJ_UINT32 l_size_unk = 2; |
| 5059 | |
| 5060 | /* preconditions*/ |
| 5061 | assert(p_j2k != 00); |
| 5062 | assert(p_manager != 00); |
| 5063 | assert(p_stream != 00); |
| 5064 | |
| 5065 | opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); |
| 5066 | |
| 5067 | while(1) { |
| 5068 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
| 5069 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 5070 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 5071 | return OPJ_FALSE; |
| 5072 | } |
| 5073 | |
| 5074 | /* read 2 bytes as the new marker ID*/ |
| 5075 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2); |
| 5076 | |
| 5077 | if (!(l_unknown_marker < 0xff00)) { |
| 5078 | |
| 5079 | /* Get the marker handler from the marker ID*/ |
| 5080 | l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); |
| 5081 | |
| 5082 | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
| 5083 | opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); |
| 5084 | return OPJ_FALSE; |
| 5085 | } |
| 5086 | else { |
| 5087 | if (l_marker_handler->id != J2K_MS_UNK) { |
| 5088 | /* Add the marker to the codestream index*/ |
| 5089 | if (l_marker_handler->id != J2K_MS_SOT) |
| 5090 | { |
| 5091 | OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, |
| 5092 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, |
| 5093 | l_size_unk); |
| 5094 | if (res == OPJ_FALSE) { |
| 5095 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
| 5096 | return OPJ_FALSE; |
| 5097 | } |
| 5098 | } |
| 5099 | break; /* next marker is known and well located */ |
| 5100 | } |
| 5101 | else |
| 5102 | l_size_unk += 2; |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | *output_marker = l_marker_handler->id ; |
| 5108 | |
| 5109 | return OPJ_TRUE; |
| 5110 | } |
| 5111 | |
| 5112 | OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, |
| 5113 | opj_mct_data_t * p_mct_record, |
| 5114 | struct opj_stream_private *p_stream, |
| 5115 | struct opj_event_mgr * p_manager ) |
| 5116 | { |
| 5117 | OPJ_UINT32 l_mct_size; |
| 5118 | OPJ_BYTE * l_current_data = 00; |
| 5119 | OPJ_UINT32 l_tmp; |
| 5120 | |
| 5121 | /* preconditions */ |
| 5122 | assert(p_j2k != 00); |
| 5123 | assert(p_manager != 00); |
| 5124 | assert(p_stream != 00); |
| 5125 | |
| 5126 | l_mct_size = 10 + p_mct_record->m_data_size; |
| 5127 | |
| 5128 | if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 5129 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); |
| 5130 | if (! new_header_tile_data) { |
| 5131 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 5132 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 5133 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 5134 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); |
| 5135 | return OPJ_FALSE; |
| 5136 | } |
| 5137 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 5138 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; |
| 5139 | } |
| 5140 | |
| 5141 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 5142 | |
| 5143 | opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */ |
| 5144 | l_current_data += 2; |
| 5145 | |
| 5146 | opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */ |
| 5147 | l_current_data += 2; |
| 5148 | |
| 5149 | opj_write_bytes(l_current_data,0,2); /* Zmct */ |
| 5150 | l_current_data += 2; |
| 5151 | |
| 5152 | /* only one marker atm */ |
| 5153 | l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10); |
| 5154 | |
| 5155 | opj_write_bytes(l_current_data,l_tmp,2); |
| 5156 | l_current_data += 2; |
| 5157 | |
| 5158 | opj_write_bytes(l_current_data,0,2); /* Ymct */ |
| 5159 | l_current_data+=2; |
| 5160 | |
| 5161 | memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size); |
| 5162 | |
| 5163 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) { |
| 5164 | return OPJ_FALSE; |
| 5165 | } |
| 5166 | |
| 5167 | return OPJ_TRUE; |
| 5168 | } |
| 5169 | |
| 5170 | /** |
| 5171 | * Reads a MCT marker (Multiple Component Transform) |
| 5172 | * |
| 5173 | * @param p_header_data the data contained in the MCT box. |
| 5174 | * @param p_j2k the jpeg2000 codec. |
| 5175 | * @param p_header_size the size of the data contained in the MCT marker. |
| 5176 | * @param p_manager the user event manager. |
| 5177 | */ |
| 5178 | static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, |
| 5179 | OPJ_BYTE * p_header_data, |
| 5180 | OPJ_UINT32 p_header_size, |
| 5181 | opj_event_mgr_t * p_manager |
| 5182 | ) |
| 5183 | { |
| 5184 | OPJ_UINT32 i; |
| 5185 | opj_tcp_t *l_tcp = 00; |
| 5186 | OPJ_UINT32 l_tmp; |
| 5187 | OPJ_UINT32 l_indix; |
| 5188 | opj_mct_data_t * l_mct_data; |
| 5189 | |
| 5190 | /* preconditions */ |
| 5191 | assert(p_header_data != 00); |
| 5192 | assert(p_j2k != 00); |
| 5193 | |
| 5194 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
| 5195 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
| 5196 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 5197 | |
| 5198 | if (p_header_size < 2) { |
| 5199 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
| 5200 | return OPJ_FALSE; |
| 5201 | } |
| 5202 | |
| 5203 | /* first marker */ |
| 5204 | opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */ |
| 5205 | p_header_data += 2; |
| 5206 | if (l_tmp != 0) { |
| 5207 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); |
| 5208 | return OPJ_TRUE; |
| 5209 | } |
| 5210 | |
| 5211 | if(p_header_size <= 6) { |
| 5212 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
| 5213 | return OPJ_FALSE; |
| 5214 | } |
| 5215 | |
| 5216 | /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ |
| 5217 | opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */ |
| 5218 | p_header_data += 2; |
| 5219 | |
| 5220 | l_indix = l_tmp & 0xff; |
| 5221 | l_mct_data = l_tcp->m_mct_records; |
| 5222 | |
| 5223 | for (i=0;i<l_tcp->m_nb_mct_records;++i) { |
| 5224 | if (l_mct_data->m_index == l_indix) { |
| 5225 | break; |
| 5226 | } |
| 5227 | ++l_mct_data; |
| 5228 | } |
| 5229 | |
| 5230 | /* NOT FOUND */ |
| 5231 | if (i == l_tcp->m_nb_mct_records) { |
| 5232 | if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { |
| 5233 | opj_mct_data_t *new_mct_records; |
| 5234 | l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
| 5235 | |
| 5236 | new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
| 5237 | if (! new_mct_records) { |
| 5238 | opj_free(l_tcp->m_mct_records); |
| 5239 | l_tcp->m_mct_records = NULL; |
| 5240 | l_tcp->m_nb_max_mct_records = 0; |
| 5241 | l_tcp->m_nb_mct_records = 0; |
| 5242 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); |
| 5243 | return OPJ_FALSE; |
| 5244 | } |
| 5245 | l_tcp->m_mct_records = new_mct_records; |
| 5246 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
| 5247 | memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); |
| 5248 | } |
| 5249 | |
| 5250 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 5251 | ++l_tcp->m_nb_mct_records; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | if (l_mct_data->m_data) { |
| 5255 | opj_free(l_mct_data->m_data); |
| 5256 | l_mct_data->m_data = 00; |
| 5257 | } |
| 5258 | |
| 5259 | l_mct_data->m_index = l_indix; |
| 5260 | l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); |
| 5261 | l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); |
| 5262 | |
| 5263 | opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */ |
| 5264 | p_header_data+=2; |
| 5265 | if (l_tmp != 0) { |
| 5266 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n"); |
| 5267 | return OPJ_TRUE; |
| 5268 | } |
| 5269 | |
| 5270 | p_header_size -= 6; |
| 5271 | |
| 5272 | l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); |
| 5273 | if (! l_mct_data->m_data) { |
| 5274 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); |
| 5275 | return OPJ_FALSE; |
| 5276 | } |
| 5277 | memcpy(l_mct_data->m_data,p_header_data,p_header_size); |
| 5278 | |
| 5279 | l_mct_data->m_data_size = p_header_size; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5280 | |
| 5281 | return OPJ_TRUE; |
| 5282 | } |
| 5283 | |
| 5284 | OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, |
| 5285 | struct opj_simple_mcc_decorrelation_data * p_mcc_record, |
| 5286 | struct opj_stream_private *p_stream, |
| 5287 | struct opj_event_mgr * p_manager ) |
| 5288 | { |
| 5289 | OPJ_UINT32 i; |
| 5290 | OPJ_UINT32 l_mcc_size; |
| 5291 | OPJ_BYTE * l_current_data = 00; |
| 5292 | OPJ_UINT32 l_nb_bytes_for_comp; |
| 5293 | OPJ_UINT32 l_mask; |
| 5294 | OPJ_UINT32 l_tmcc; |
| 5295 | |
| 5296 | /* preconditions */ |
| 5297 | assert(p_j2k != 00); |
| 5298 | assert(p_manager != 00); |
| 5299 | assert(p_stream != 00); |
| 5300 | |
| 5301 | if (p_mcc_record->m_nb_comps > 255 ) { |
| 5302 | l_nb_bytes_for_comp = 2; |
| 5303 | l_mask = 0x8000; |
| 5304 | } |
| 5305 | else { |
| 5306 | l_nb_bytes_for_comp = 1; |
| 5307 | l_mask = 0; |
| 5308 | } |
| 5309 | |
| 5310 | l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; |
| 5311 | if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) |
| 5312 | { |
| 5313 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); |
| 5314 | if (! new_header_tile_data) { |
| 5315 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 5316 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 5317 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 5318 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); |
| 5319 | return OPJ_FALSE; |
| 5320 | } |
| 5321 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 5322 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; |
| 5323 | } |
| 5324 | |
| 5325 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 5326 | |
| 5327 | opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */ |
| 5328 | l_current_data += 2; |
| 5329 | |
| 5330 | opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */ |
| 5331 | l_current_data += 2; |
| 5332 | |
| 5333 | /* first marker */ |
| 5334 | opj_write_bytes(l_current_data,0,2); /* Zmcc */ |
| 5335 | l_current_data += 2; |
| 5336 | |
| 5337 | opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */ |
| 5338 | ++l_current_data; |
| 5339 | |
| 5340 | /* only one marker atm */ |
| 5341 | opj_write_bytes(l_current_data,0,2); /* Ymcc */ |
| 5342 | l_current_data+=2; |
| 5343 | |
| 5344 | opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */ |
| 5345 | l_current_data+=2; |
| 5346 | |
| 5347 | opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */ |
| 5348 | ++l_current_data; |
| 5349 | |
| 5350 | opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ |
| 5351 | l_current_data+=2; |
| 5352 | |
| 5353 | for (i=0;i<p_mcc_record->m_nb_comps;++i) { |
| 5354 | opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/ |
| 5355 | l_current_data+=l_nb_bytes_for_comp; |
| 5356 | } |
| 5357 | |
| 5358 | opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ |
| 5359 | l_current_data+=2; |
| 5360 | |
| 5361 | for (i=0;i<p_mcc_record->m_nb_comps;++i) |
| 5362 | { |
| 5363 | opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/ |
| 5364 | l_current_data+=l_nb_bytes_for_comp; |
| 5365 | } |
| 5366 | |
| 5367 | l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16; |
| 5368 | |
| 5369 | if (p_mcc_record->m_decorrelation_array) { |
| 5370 | l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; |
| 5371 | } |
| 5372 | |
| 5373 | if (p_mcc_record->m_offset_array) { |
| 5374 | l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8); |
| 5375 | } |
| 5376 | |
| 5377 | opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ |
| 5378 | l_current_data+=3; |
| 5379 | |
| 5380 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) { |
| 5381 | return OPJ_FALSE; |
| 5382 | } |
| 5383 | |
| 5384 | return OPJ_TRUE; |
| 5385 | } |
| 5386 | |
| 5387 | OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, |
| 5388 | OPJ_BYTE * p_header_data, |
| 5389 | OPJ_UINT32 p_header_size, |
| 5390 | opj_event_mgr_t * p_manager ) |
| 5391 | { |
| 5392 | OPJ_UINT32 i,j; |
| 5393 | OPJ_UINT32 l_tmp; |
| 5394 | OPJ_UINT32 l_indix; |
| 5395 | opj_tcp_t * l_tcp; |
| 5396 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
| 5397 | opj_mct_data_t * l_mct_data; |
| 5398 | OPJ_UINT32 l_nb_collections; |
| 5399 | OPJ_UINT32 l_nb_comps; |
| 5400 | OPJ_UINT32 l_nb_bytes_by_comp; |
Matthieu Darbois | 1baea68 | 2016-09-06 22:33:26 +0200 | [diff] [blame] | 5401 | OPJ_BOOL l_new_mcc = OPJ_FALSE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5402 | |
| 5403 | /* preconditions */ |
| 5404 | assert(p_header_data != 00); |
| 5405 | assert(p_j2k != 00); |
| 5406 | assert(p_manager != 00); |
| 5407 | |
| 5408 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
| 5409 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
| 5410 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 5411 | |
| 5412 | if (p_header_size < 2) { |
| 5413 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5414 | return OPJ_FALSE; |
| 5415 | } |
| 5416 | |
| 5417 | /* first marker */ |
| 5418 | opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */ |
| 5419 | p_header_data += 2; |
| 5420 | if (l_tmp != 0) { |
| 5421 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); |
| 5422 | return OPJ_TRUE; |
| 5423 | } |
| 5424 | |
| 5425 | if (p_header_size < 7) { |
| 5426 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5427 | return OPJ_FALSE; |
| 5428 | } |
| 5429 | |
| 5430 | opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */ |
| 5431 | ++p_header_data; |
| 5432 | |
| 5433 | l_mcc_record = l_tcp->m_mcc_records; |
| 5434 | |
| 5435 | for(i=0;i<l_tcp->m_nb_mcc_records;++i) { |
| 5436 | if (l_mcc_record->m_index == l_indix) { |
| 5437 | break; |
| 5438 | } |
| 5439 | ++l_mcc_record; |
| 5440 | } |
| 5441 | |
| 5442 | /** NOT FOUND */ |
| 5443 | if (i == l_tcp->m_nb_mcc_records) { |
| 5444 | if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { |
| 5445 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
| 5446 | l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
| 5447 | |
| 5448 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
| 5449 | l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); |
| 5450 | if (! new_mcc_records) { |
| 5451 | opj_free(l_tcp->m_mcc_records); |
| 5452 | l_tcp->m_mcc_records = NULL; |
| 5453 | l_tcp->m_nb_max_mcc_records = 0; |
| 5454 | l_tcp->m_nb_mcc_records = 0; |
| 5455 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); |
| 5456 | return OPJ_FALSE; |
| 5457 | } |
| 5458 | l_tcp->m_mcc_records = new_mcc_records; |
| 5459 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
| 5460 | memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); |
| 5461 | } |
| 5462 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
Matthieu Darbois | 1baea68 | 2016-09-06 22:33:26 +0200 | [diff] [blame] | 5463 | l_new_mcc = OPJ_TRUE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5464 | } |
| 5465 | l_mcc_record->m_index = l_indix; |
| 5466 | |
| 5467 | /* only one marker atm */ |
| 5468 | opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */ |
| 5469 | p_header_data+=2; |
| 5470 | if (l_tmp != 0) { |
| 5471 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); |
| 5472 | return OPJ_TRUE; |
| 5473 | } |
| 5474 | |
| 5475 | opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */ |
| 5476 | p_header_data+=2; |
| 5477 | |
| 5478 | if (l_nb_collections > 1) { |
| 5479 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n"); |
| 5480 | return OPJ_TRUE; |
| 5481 | } |
| 5482 | |
| 5483 | p_header_size -= 7; |
| 5484 | |
| 5485 | for (i=0;i<l_nb_collections;++i) { |
| 5486 | if (p_header_size < 3) { |
| 5487 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5488 | return OPJ_FALSE; |
| 5489 | } |
| 5490 | |
| 5491 | opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */ |
| 5492 | ++p_header_data; |
| 5493 | |
| 5494 | if (l_tmp != 1) { |
| 5495 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); |
| 5496 | return OPJ_TRUE; |
| 5497 | } |
| 5498 | |
| 5499 | opj_read_bytes(p_header_data,&l_nb_comps,2); |
| 5500 | |
| 5501 | p_header_data+=2; |
| 5502 | p_header_size-=3; |
| 5503 | |
| 5504 | l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); |
| 5505 | l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; |
| 5506 | |
| 5507 | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { |
| 5508 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5509 | return OPJ_FALSE; |
| 5510 | } |
| 5511 | |
| 5512 | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); |
| 5513 | |
| 5514 | for (j=0;j<l_mcc_record->m_nb_comps;++j) { |
| 5515 | opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/ |
| 5516 | p_header_data+=l_nb_bytes_by_comp; |
| 5517 | |
| 5518 | if (l_tmp != j) { |
| 5519 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); |
| 5520 | return OPJ_TRUE; |
| 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | opj_read_bytes(p_header_data,&l_nb_comps,2); |
| 5525 | p_header_data+=2; |
| 5526 | |
| 5527 | l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); |
| 5528 | l_nb_comps &= 0x7fff; |
| 5529 | |
| 5530 | if (l_nb_comps != l_mcc_record->m_nb_comps) { |
| 5531 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n"); |
| 5532 | return OPJ_TRUE; |
| 5533 | } |
| 5534 | |
| 5535 | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { |
| 5536 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5537 | return OPJ_FALSE; |
| 5538 | } |
| 5539 | |
| 5540 | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); |
| 5541 | |
| 5542 | for (j=0;j<l_mcc_record->m_nb_comps;++j) { |
| 5543 | opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/ |
| 5544 | p_header_data+=l_nb_bytes_by_comp; |
| 5545 | |
| 5546 | if (l_tmp != j) { |
| 5547 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); |
| 5548 | return OPJ_TRUE; |
| 5549 | } |
| 5550 | } |
| 5551 | |
| 5552 | opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/ |
| 5553 | p_header_data += 3; |
| 5554 | |
| 5555 | l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1); |
| 5556 | l_mcc_record->m_decorrelation_array = 00; |
| 5557 | l_mcc_record->m_offset_array = 00; |
| 5558 | |
| 5559 | l_indix = l_tmp & 0xff; |
| 5560 | if (l_indix != 0) { |
| 5561 | l_mct_data = l_tcp->m_mct_records; |
| 5562 | for (j=0;j<l_tcp->m_nb_mct_records;++j) { |
| 5563 | if (l_mct_data->m_index == l_indix) { |
| 5564 | l_mcc_record->m_decorrelation_array = l_mct_data; |
| 5565 | break; |
| 5566 | } |
| 5567 | ++l_mct_data; |
| 5568 | } |
| 5569 | |
| 5570 | if (l_mcc_record->m_decorrelation_array == 00) { |
| 5571 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5572 | return OPJ_FALSE; |
| 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | l_indix = (l_tmp >> 8) & 0xff; |
| 5577 | if (l_indix != 0) { |
| 5578 | l_mct_data = l_tcp->m_mct_records; |
| 5579 | for (j=0;j<l_tcp->m_nb_mct_records;++j) { |
| 5580 | if (l_mct_data->m_index == l_indix) { |
| 5581 | l_mcc_record->m_offset_array = l_mct_data; |
| 5582 | break; |
| 5583 | } |
| 5584 | ++l_mct_data; |
| 5585 | } |
| 5586 | |
| 5587 | if (l_mcc_record->m_offset_array == 00) { |
| 5588 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5589 | return OPJ_FALSE; |
| 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | if (p_header_size != 0) { |
| 5595 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); |
| 5596 | return OPJ_FALSE; |
| 5597 | } |
| 5598 | |
Matthieu Darbois | 1baea68 | 2016-09-06 22:33:26 +0200 | [diff] [blame] | 5599 | if (l_new_mcc) { |
| 5600 | ++l_tcp->m_nb_mcc_records; |
| 5601 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5602 | |
| 5603 | return OPJ_TRUE; |
| 5604 | } |
| 5605 | |
| 5606 | OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, |
| 5607 | struct opj_stream_private *p_stream, |
| 5608 | struct opj_event_mgr * p_manager |
| 5609 | ) |
| 5610 | { |
| 5611 | OPJ_BYTE * l_current_data = 00; |
| 5612 | OPJ_UINT32 l_mco_size; |
| 5613 | opj_tcp_t * l_tcp = 00; |
| 5614 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
| 5615 | OPJ_UINT32 i; |
| 5616 | |
| 5617 | /* preconditions */ |
| 5618 | assert(p_j2k != 00); |
| 5619 | assert(p_manager != 00); |
| 5620 | assert(p_stream != 00); |
| 5621 | |
| 5622 | l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
mayeut | 6457183 | 2015-09-06 17:24:03 +0200 | [diff] [blame] | 5623 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5624 | l_mco_size = 5 + l_tcp->m_nb_mcc_records; |
| 5625 | if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 5626 | |
| 5627 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); |
| 5628 | if (! new_header_tile_data) { |
| 5629 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 5630 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 5631 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 5632 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); |
| 5633 | return OPJ_FALSE; |
| 5634 | } |
| 5635 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 5636 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; |
| 5637 | } |
mayeut | 6457183 | 2015-09-06 17:24:03 +0200 | [diff] [blame] | 5638 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 5639 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5640 | |
| 5641 | opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */ |
| 5642 | l_current_data += 2; |
| 5643 | |
| 5644 | opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */ |
| 5645 | l_current_data += 2; |
| 5646 | |
| 5647 | opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/ |
| 5648 | ++l_current_data; |
| 5649 | |
| 5650 | l_mcc_record = l_tcp->m_mcc_records; |
mayeut | 6457183 | 2015-09-06 17:24:03 +0200 | [diff] [blame] | 5651 | for (i=0;i<l_tcp->m_nb_mcc_records;++i) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5652 | opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/ |
| 5653 | ++l_current_data; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5654 | ++l_mcc_record; |
| 5655 | } |
| 5656 | |
| 5657 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) { |
| 5658 | return OPJ_FALSE; |
| 5659 | } |
| 5660 | |
| 5661 | return OPJ_TRUE; |
| 5662 | } |
| 5663 | |
| 5664 | /** |
| 5665 | * Reads a MCO marker (Multiple Component Transform Ordering) |
| 5666 | * |
| 5667 | * @param p_header_data the data contained in the MCO box. |
| 5668 | * @param p_j2k the jpeg2000 codec. |
| 5669 | * @param p_header_size the size of the data contained in the MCO marker. |
| 5670 | * @param p_manager the user event manager. |
| 5671 | */ |
| 5672 | static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, |
| 5673 | OPJ_BYTE * p_header_data, |
| 5674 | OPJ_UINT32 p_header_size, |
| 5675 | opj_event_mgr_t * p_manager |
| 5676 | ) |
| 5677 | { |
| 5678 | OPJ_UINT32 l_tmp, i; |
| 5679 | OPJ_UINT32 l_nb_stages; |
| 5680 | opj_tcp_t * l_tcp; |
| 5681 | opj_tccp_t * l_tccp; |
| 5682 | opj_image_t * l_image; |
| 5683 | |
| 5684 | /* preconditions */ |
| 5685 | assert(p_header_data != 00); |
| 5686 | assert(p_j2k != 00); |
| 5687 | assert(p_manager != 00); |
| 5688 | |
| 5689 | l_image = p_j2k->m_private_image; |
| 5690 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
| 5691 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
| 5692 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 5693 | |
| 5694 | if (p_header_size < 1) { |
| 5695 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); |
| 5696 | return OPJ_FALSE; |
| 5697 | } |
| 5698 | |
| 5699 | opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/ |
| 5700 | ++p_header_data; |
| 5701 | |
| 5702 | if (l_nb_stages > 1) { |
| 5703 | opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); |
| 5704 | return OPJ_TRUE; |
| 5705 | } |
| 5706 | |
| 5707 | if (p_header_size != l_nb_stages + 1) { |
| 5708 | opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); |
| 5709 | return OPJ_FALSE; |
| 5710 | } |
| 5711 | |
| 5712 | l_tccp = l_tcp->tccps; |
| 5713 | |
| 5714 | for (i=0;i<l_image->numcomps;++i) { |
| 5715 | l_tccp->m_dc_level_shift = 0; |
| 5716 | ++l_tccp; |
| 5717 | } |
| 5718 | |
| 5719 | if (l_tcp->m_mct_decoding_matrix) { |
| 5720 | opj_free(l_tcp->m_mct_decoding_matrix); |
| 5721 | l_tcp->m_mct_decoding_matrix = 00; |
| 5722 | } |
| 5723 | |
| 5724 | for (i=0;i<l_nb_stages;++i) { |
| 5725 | opj_read_bytes(p_header_data,&l_tmp,1); |
| 5726 | ++p_header_data; |
| 5727 | |
| 5728 | if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) { |
| 5729 | return OPJ_FALSE; |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | return OPJ_TRUE; |
| 5734 | } |
| 5735 | |
| 5736 | OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index) |
| 5737 | { |
| 5738 | OPJ_UINT32 i; |
| 5739 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
| 5740 | opj_mct_data_t * l_deco_array, * l_offset_array; |
| 5741 | OPJ_UINT32 l_data_size,l_mct_size, l_offset_size; |
| 5742 | OPJ_UINT32 l_nb_elem; |
| 5743 | OPJ_UINT32 * l_offset_data, * l_current_offset_data; |
| 5744 | opj_tccp_t * l_tccp; |
| 5745 | |
| 5746 | /* preconditions */ |
| 5747 | assert(p_tcp != 00); |
| 5748 | |
| 5749 | l_mcc_record = p_tcp->m_mcc_records; |
| 5750 | |
| 5751 | for (i=0;i<p_tcp->m_nb_mcc_records;++i) { |
| 5752 | if (l_mcc_record->m_index == p_index) { |
| 5753 | break; |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | if (i==p_tcp->m_nb_mcc_records) { |
| 5758 | /** element discarded **/ |
| 5759 | return OPJ_TRUE; |
| 5760 | } |
| 5761 | |
| 5762 | if (l_mcc_record->m_nb_comps != p_image->numcomps) { |
| 5763 | /** do not support number of comps != image */ |
| 5764 | return OPJ_TRUE; |
| 5765 | } |
| 5766 | |
| 5767 | l_deco_array = l_mcc_record->m_decorrelation_array; |
| 5768 | |
| 5769 | if (l_deco_array) { |
| 5770 | l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps; |
| 5771 | if (l_deco_array->m_data_size != l_data_size) { |
| 5772 | return OPJ_FALSE; |
| 5773 | } |
| 5774 | |
| 5775 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
| 5776 | l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
| 5777 | p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
| 5778 | |
| 5779 | if (! p_tcp->m_mct_decoding_matrix ) { |
| 5780 | return OPJ_FALSE; |
| 5781 | } |
| 5782 | |
| 5783 | j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem); |
| 5784 | } |
| 5785 | |
| 5786 | l_offset_array = l_mcc_record->m_offset_array; |
| 5787 | |
| 5788 | if (l_offset_array) { |
| 5789 | l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps; |
| 5790 | if (l_offset_array->m_data_size != l_data_size) { |
| 5791 | return OPJ_FALSE; |
| 5792 | } |
| 5793 | |
| 5794 | l_nb_elem = p_image->numcomps; |
| 5795 | l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); |
| 5796 | l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); |
| 5797 | |
| 5798 | if (! l_offset_data ) { |
| 5799 | return OPJ_FALSE; |
| 5800 | } |
| 5801 | |
| 5802 | j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem); |
| 5803 | |
| 5804 | l_tccp = p_tcp->tccps; |
| 5805 | l_current_offset_data = l_offset_data; |
| 5806 | |
| 5807 | for (i=0;i<p_image->numcomps;++i) { |
| 5808 | l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++); |
| 5809 | ++l_tccp; |
| 5810 | } |
| 5811 | |
| 5812 | opj_free(l_offset_data); |
| 5813 | } |
| 5814 | |
| 5815 | return OPJ_TRUE; |
| 5816 | } |
| 5817 | |
| 5818 | OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, |
| 5819 | struct opj_stream_private *p_stream, |
| 5820 | struct opj_event_mgr * p_manager ) |
| 5821 | { |
| 5822 | OPJ_UINT32 i; |
| 5823 | OPJ_UINT32 l_cbd_size; |
| 5824 | OPJ_BYTE * l_current_data = 00; |
| 5825 | opj_image_t *l_image = 00; |
| 5826 | opj_image_comp_t * l_comp = 00; |
| 5827 | |
| 5828 | /* preconditions */ |
| 5829 | assert(p_j2k != 00); |
| 5830 | assert(p_manager != 00); |
| 5831 | assert(p_stream != 00); |
| 5832 | |
| 5833 | l_image = p_j2k->m_private_image; |
| 5834 | l_cbd_size = 6 + p_j2k->m_private_image->numcomps; |
| 5835 | |
| 5836 | if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
| 5837 | OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); |
| 5838 | if (! new_header_tile_data) { |
| 5839 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 5840 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
| 5841 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 5842 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); |
| 5843 | return OPJ_FALSE; |
| 5844 | } |
| 5845 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
| 5846 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; |
| 5847 | } |
| 5848 | |
| 5849 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
| 5850 | |
| 5851 | opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */ |
| 5852 | l_current_data += 2; |
| 5853 | |
| 5854 | opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */ |
| 5855 | l_current_data += 2; |
| 5856 | |
| 5857 | opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */ |
| 5858 | l_current_data+=2; |
| 5859 | |
| 5860 | l_comp = l_image->comps; |
| 5861 | |
| 5862 | for (i=0;i<l_image->numcomps;++i) { |
| 5863 | opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */ |
| 5864 | ++l_current_data; |
| 5865 | |
| 5866 | ++l_comp; |
| 5867 | } |
| 5868 | |
| 5869 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) { |
| 5870 | return OPJ_FALSE; |
| 5871 | } |
| 5872 | |
| 5873 | return OPJ_TRUE; |
| 5874 | } |
| 5875 | |
| 5876 | /** |
| 5877 | * Reads a CBD marker (Component bit depth definition) |
| 5878 | * @param p_header_data the data contained in the CBD box. |
| 5879 | * @param p_j2k the jpeg2000 codec. |
| 5880 | * @param p_header_size the size of the data contained in the CBD marker. |
| 5881 | * @param p_manager the user event manager. |
| 5882 | */ |
| 5883 | static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, |
| 5884 | OPJ_BYTE * p_header_data, |
| 5885 | OPJ_UINT32 p_header_size, |
| 5886 | opj_event_mgr_t * p_manager |
| 5887 | ) |
| 5888 | { |
| 5889 | OPJ_UINT32 l_nb_comp,l_num_comp; |
| 5890 | OPJ_UINT32 l_comp_def; |
| 5891 | OPJ_UINT32 i; |
| 5892 | opj_image_comp_t * l_comp = 00; |
| 5893 | |
| 5894 | /* preconditions */ |
| 5895 | assert(p_header_data != 00); |
| 5896 | assert(p_j2k != 00); |
| 5897 | assert(p_manager != 00); |
| 5898 | |
| 5899 | l_num_comp = p_j2k->m_private_image->numcomps; |
| 5900 | |
| 5901 | if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { |
| 5902 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
| 5903 | return OPJ_FALSE; |
| 5904 | } |
| 5905 | |
| 5906 | opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */ |
| 5907 | p_header_data+=2; |
| 5908 | |
| 5909 | if (l_nb_comp != l_num_comp) { |
| 5910 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); |
| 5911 | return OPJ_FALSE; |
| 5912 | } |
| 5913 | |
| 5914 | l_comp = p_j2k->m_private_image->comps; |
| 5915 | for (i=0;i<l_num_comp;++i) { |
| 5916 | opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */ |
| 5917 | ++p_header_data; |
| 5918 | l_comp->sgnd = (l_comp_def>>7) & 1; |
| 5919 | l_comp->prec = (l_comp_def&0x7f) + 1; |
| 5920 | ++l_comp; |
| 5921 | } |
| 5922 | |
| 5923 | return OPJ_TRUE; |
| 5924 | } |
| 5925 | |
| 5926 | /* ----------------------------------------------------------------------- */ |
| 5927 | /* J2K / JPT decoder interface */ |
| 5928 | /* ----------------------------------------------------------------------- */ |
| 5929 | |
| 5930 | void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) |
| 5931 | { |
| 5932 | if(j2k && parameters) { |
| 5933 | j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; |
| 5934 | j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; |
| 5935 | |
| 5936 | #ifdef USE_JPWL |
| 5937 | j2k->m_cp.correct = parameters->jpwl_correct; |
| 5938 | j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; |
| 5939 | j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; |
| 5940 | #endif /* USE_JPWL */ |
| 5941 | } |
| 5942 | } |
| 5943 | |
| 5944 | /* ----------------------------------------------------------------------- */ |
| 5945 | /* J2K encoder interface */ |
| 5946 | /* ----------------------------------------------------------------------- */ |
| 5947 | |
| 5948 | opj_j2k_t* opj_j2k_create_compress(void) |
| 5949 | { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 5950 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5951 | if (!l_j2k) { |
| 5952 | return NULL; |
| 5953 | } |
| 5954 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 5955 | |
| 5956 | l_j2k->m_is_decoder = 0; |
| 5957 | l_j2k->m_cp.m_is_decoder = 0; |
| 5958 | |
| 5959 | l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE); |
| 5960 | if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
| 5961 | opj_j2k_destroy(l_j2k); |
| 5962 | return NULL; |
| 5963 | } |
| 5964 | |
| 5965 | l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE; |
| 5966 | |
| 5967 | /* validation list creation*/ |
| 5968 | l_j2k->m_validation_list = opj_procedure_list_create(); |
| 5969 | if (! l_j2k->m_validation_list) { |
| 5970 | opj_j2k_destroy(l_j2k); |
| 5971 | return NULL; |
| 5972 | } |
| 5973 | |
| 5974 | /* execution list creation*/ |
| 5975 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
| 5976 | if (! l_j2k->m_procedure_list) { |
| 5977 | opj_j2k_destroy(l_j2k); |
| 5978 | return NULL; |
| 5979 | } |
| 5980 | |
| 5981 | return l_j2k; |
| 5982 | } |
| 5983 | |
| 5984 | int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){ |
| 5985 | POC[0].tile = 1; |
| 5986 | POC[0].resno0 = 0; |
| 5987 | POC[0].compno0 = 0; |
| 5988 | POC[0].layno1 = 1; |
| 5989 | POC[0].resno1 = (OPJ_UINT32)(numres-1); |
| 5990 | POC[0].compno1 = 3; |
| 5991 | POC[0].prg1 = OPJ_CPRL; |
| 5992 | POC[1].tile = 1; |
| 5993 | POC[1].resno0 = (OPJ_UINT32)(numres-1); |
| 5994 | POC[1].compno0 = 0; |
| 5995 | POC[1].layno1 = 1; |
| 5996 | POC[1].resno1 = (OPJ_UINT32)numres; |
| 5997 | POC[1].compno1 = 3; |
| 5998 | POC[1].prg1 = OPJ_CPRL; |
| 5999 | return 2; |
| 6000 | } |
| 6001 | |
| 6002 | void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager) |
| 6003 | { |
| 6004 | /* Configure cinema parameters */ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6005 | int i; |
| 6006 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6007 | /* No tiling */ |
| 6008 | parameters->tile_size_on = OPJ_FALSE; |
| 6009 | parameters->cp_tdx=1; |
| 6010 | parameters->cp_tdy=1; |
| 6011 | |
| 6012 | /* One tile part for each component */ |
| 6013 | parameters->tp_flag = 'C'; |
| 6014 | parameters->tp_on = 1; |
| 6015 | |
| 6016 | /* Tile and Image shall be at (0,0) */ |
| 6017 | parameters->cp_tx0 = 0; |
| 6018 | parameters->cp_ty0 = 0; |
| 6019 | parameters->image_offset_x0 = 0; |
| 6020 | parameters->image_offset_y0 = 0; |
| 6021 | |
| 6022 | /* Codeblock size= 32*32 */ |
| 6023 | parameters->cblockw_init = 32; |
| 6024 | parameters->cblockh_init = 32; |
| 6025 | |
| 6026 | /* Codeblock style: no mode switch enabled */ |
| 6027 | parameters->mode = 0; |
| 6028 | |
| 6029 | /* No ROI */ |
| 6030 | parameters->roi_compno = -1; |
| 6031 | |
| 6032 | /* No subsampling */ |
| 6033 | parameters->subsampling_dx = 1; |
| 6034 | parameters->subsampling_dy = 1; |
| 6035 | |
| 6036 | /* 9-7 transform */ |
| 6037 | parameters->irreversible = 1; |
| 6038 | |
| 6039 | /* Number of layers */ |
| 6040 | if (parameters->tcp_numlayers > 1){ |
| 6041 | opj_event_msg(p_manager, EVT_WARNING, |
| 6042 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
| 6043 | "1 single quality layer" |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6044 | "-> Number of layers forced to 1 (rather than %d)\n" |
| 6045 | "-> Rate of the last layer (%3.1f) will be used", |
| 6046 | parameters->tcp_numlayers, parameters->tcp_rates[parameters->tcp_numlayers-1]); |
| 6047 | parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers-1]; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6048 | parameters->tcp_numlayers = 1; |
| 6049 | } |
| 6050 | |
| 6051 | /* Resolution levels */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6052 | switch (parameters->rsiz){ |
| 6053 | case OPJ_PROFILE_CINEMA_2K: |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6054 | if(parameters->numresolution > 6){ |
| 6055 | opj_event_msg(p_manager, EVT_WARNING, |
| 6056 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
| 6057 | "Number of decomposition levels <= 5\n" |
| 6058 | "-> Number of decomposition levels forced to 5 (rather than %d)\n", |
| 6059 | parameters->numresolution+1); |
| 6060 | parameters->numresolution = 6; |
| 6061 | } |
| 6062 | break; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6063 | case OPJ_PROFILE_CINEMA_4K: |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6064 | if(parameters->numresolution < 2){ |
| 6065 | opj_event_msg(p_manager, EVT_WARNING, |
| 6066 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
| 6067 | "Number of decomposition levels >= 1 && <= 6\n" |
| 6068 | "-> Number of decomposition levels forced to 1 (rather than %d)\n", |
| 6069 | parameters->numresolution+1); |
| 6070 | parameters->numresolution = 1; |
| 6071 | }else if(parameters->numresolution > 7){ |
| 6072 | opj_event_msg(p_manager, EVT_WARNING, |
| 6073 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
| 6074 | "Number of decomposition levels >= 1 && <= 6\n" |
| 6075 | "-> Number of decomposition levels forced to 6 (rather than %d)\n", |
| 6076 | parameters->numresolution+1); |
| 6077 | parameters->numresolution = 7; |
| 6078 | } |
| 6079 | break; |
| 6080 | default : |
| 6081 | break; |
| 6082 | } |
| 6083 | |
| 6084 | /* Precincts */ |
| 6085 | parameters->csty |= 0x01; |
| 6086 | parameters->res_spec = parameters->numresolution-1; |
| 6087 | for (i = 0; i<parameters->res_spec; i++) { |
| 6088 | parameters->prcw_init[i] = 256; |
| 6089 | parameters->prch_init[i] = 256; |
| 6090 | } |
| 6091 | |
| 6092 | /* The progression order shall be CPRL */ |
| 6093 | parameters->prog_order = OPJ_CPRL; |
| 6094 | |
| 6095 | /* Progression order changes for 4K, disallowed for 2K */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6096 | if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6097 | parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution); |
| 6098 | } else { |
| 6099 | parameters->numpocs = 0; |
| 6100 | } |
| 6101 | |
| 6102 | /* Limited bit-rate */ |
| 6103 | parameters->cp_disto_alloc = 1; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6104 | if (parameters->max_cs_size <= 0) { |
| 6105 | /* No rate has been introduced, 24 fps is assumed */ |
| 6106 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
| 6107 | opj_event_msg(p_manager, EVT_WARNING, |
| 6108 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
| 6109 | "Maximum 1302083 compressed bytes @ 24fps\n" |
| 6110 | "As no rate has been given, this limit will be used.\n"); |
| 6111 | } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { |
| 6112 | opj_event_msg(p_manager, EVT_WARNING, |
| 6113 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
| 6114 | "Maximum 1302083 compressed bytes @ 24fps\n" |
| 6115 | "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n"); |
| 6116 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6117 | } |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6118 | |
| 6119 | if (parameters->max_comp_size <= 0) { |
| 6120 | /* No rate has been introduced, 24 fps is assumed */ |
| 6121 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
| 6122 | opj_event_msg(p_manager, EVT_WARNING, |
| 6123 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
| 6124 | "Maximum 1041666 compressed bytes @ 24fps\n" |
| 6125 | "As no rate has been given, this limit will be used.\n"); |
| 6126 | } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { |
| 6127 | opj_event_msg(p_manager, EVT_WARNING, |
| 6128 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
| 6129 | "Maximum 1041666 compressed bytes @ 24fps\n" |
| 6130 | "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n"); |
| 6131 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
| 6132 | } |
| 6133 | |
| 6134 | parameters->tcp_rates[0] = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/ |
| 6135 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy); |
| 6136 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6137 | } |
| 6138 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6139 | OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager) |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6140 | { |
| 6141 | OPJ_UINT32 i; |
| 6142 | |
| 6143 | /* Number of components */ |
| 6144 | if (image->numcomps != 3){ |
| 6145 | opj_event_msg(p_manager, EVT_WARNING, |
| 6146 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
| 6147 | "3 components" |
| 6148 | "-> Number of components of input image (%d) is not compliant\n" |
| 6149 | "-> Non-profile-3 codestream will be generated\n", |
| 6150 | image->numcomps); |
| 6151 | return OPJ_FALSE; |
| 6152 | } |
| 6153 | |
| 6154 | /* Bitdepth */ |
| 6155 | for (i = 0; i < image->numcomps; i++) { |
| 6156 | if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){ |
| 6157 | char signed_str[] = "signed"; |
| 6158 | char unsigned_str[] = "unsigned"; |
| 6159 | char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str; |
| 6160 | opj_event_msg(p_manager, EVT_WARNING, |
| 6161 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
| 6162 | "Precision of each component shall be 12 bits unsigned" |
| 6163 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
| 6164 | "-> Non-profile-3 codestream will be generated\n", |
| 6165 | i,image->comps[i].bpp, tmp_str); |
| 6166 | return OPJ_FALSE; |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | /* Image size */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6171 | switch (rsiz){ |
| 6172 | case OPJ_PROFILE_CINEMA_2K: |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6173 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){ |
| 6174 | opj_event_msg(p_manager, EVT_WARNING, |
| 6175 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
| 6176 | "width <= 2048 and height <= 1080\n" |
| 6177 | "-> Input image size %d x %d is not compliant\n" |
| 6178 | "-> Non-profile-3 codestream will be generated\n", |
| 6179 | image->comps[0].w,image->comps[0].h); |
| 6180 | return OPJ_FALSE; |
| 6181 | } |
| 6182 | break; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6183 | case OPJ_PROFILE_CINEMA_4K: |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6184 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){ |
| 6185 | opj_event_msg(p_manager, EVT_WARNING, |
| 6186 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
| 6187 | "width <= 4096 and height <= 2160\n" |
| 6188 | "-> Image size %d x %d is not compliant\n" |
| 6189 | "-> Non-profile-4 codestream will be generated\n", |
| 6190 | image->comps[0].w,image->comps[0].h); |
| 6191 | return OPJ_FALSE; |
| 6192 | } |
| 6193 | break; |
| 6194 | default : |
| 6195 | break; |
| 6196 | } |
| 6197 | |
| 6198 | return OPJ_TRUE; |
| 6199 | } |
| 6200 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6201 | OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6202 | opj_cparameters_t *parameters, |
| 6203 | opj_image_t *image, |
| 6204 | opj_event_mgr_t * p_manager) |
| 6205 | { |
| 6206 | OPJ_UINT32 i, j, tileno, numpocs_tile; |
| 6207 | opj_cp_t *cp = 00; |
| 6208 | |
| 6209 | if(!p_j2k || !parameters || ! image) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6210 | return OPJ_FALSE; |
| 6211 | } |
| 6212 | |
| 6213 | if ((parameters->numresolution <= 0) || (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { |
| 6214 | opj_event_msg(p_manager, EVT_ERROR, "Invalid number of resolutions : %d not in range [1,%d]\n", parameters->numresolution, OPJ_J2K_MAXRLVLS); |
| 6215 | return OPJ_FALSE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ |
| 6219 | cp = &(p_j2k->m_cp); |
| 6220 | |
| 6221 | /* set default values for cp */ |
| 6222 | cp->tw = 1; |
| 6223 | cp->th = 1; |
| 6224 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6225 | /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ |
| 6226 | if (parameters->rsiz == OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ |
| 6227 | OPJ_BOOL deprecated_used = OPJ_FALSE; |
| 6228 | switch (parameters->cp_cinema){ |
| 6229 | case OPJ_CINEMA2K_24: |
| 6230 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
| 6231 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
| 6232 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
| 6233 | deprecated_used = OPJ_TRUE; |
| 6234 | break; |
| 6235 | case OPJ_CINEMA2K_48: |
| 6236 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
| 6237 | parameters->max_cs_size = OPJ_CINEMA_48_CS; |
| 6238 | parameters->max_comp_size = OPJ_CINEMA_48_COMP; |
| 6239 | deprecated_used = OPJ_TRUE; |
| 6240 | break; |
| 6241 | case OPJ_CINEMA4K_24: |
| 6242 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
| 6243 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
| 6244 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
| 6245 | deprecated_used = OPJ_TRUE; |
| 6246 | break; |
| 6247 | case OPJ_OFF: |
| 6248 | default: |
| 6249 | break; |
| 6250 | } |
| 6251 | switch (parameters->cp_rsiz){ |
| 6252 | case OPJ_CINEMA2K: |
| 6253 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
| 6254 | deprecated_used = OPJ_TRUE; |
| 6255 | break; |
| 6256 | case OPJ_CINEMA4K: |
| 6257 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
| 6258 | deprecated_used = OPJ_TRUE; |
| 6259 | break; |
| 6260 | case OPJ_MCT: |
| 6261 | parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; |
| 6262 | deprecated_used = OPJ_TRUE; |
| 6263 | case OPJ_STD_RSIZ: |
| 6264 | default: |
| 6265 | break; |
| 6266 | } |
| 6267 | if (deprecated_used) { |
| 6268 | opj_event_msg(p_manager, EVT_WARNING, |
| 6269 | "Deprecated fields cp_cinema or cp_rsiz are used\n" |
| 6270 | "Please consider using only the rsiz field\n" |
| 6271 | "See openjpeg.h documentation for more details\n"); |
| 6272 | } |
| 6273 | } |
| 6274 | |
| 6275 | /* see if max_codestream_size does limit input rate */ |
| 6276 | if (parameters->max_cs_size <= 0) { |
| 6277 | if (parameters->tcp_rates[parameters->tcp_numlayers-1] > 0) { |
| 6278 | OPJ_FLOAT32 temp_size; |
| 6279 | temp_size =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/ |
| 6280 | (parameters->tcp_rates[parameters->tcp_numlayers-1] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy); |
| 6281 | parameters->max_cs_size = (int) floor(temp_size); |
| 6282 | } else { |
| 6283 | parameters->max_cs_size = 0; |
| 6284 | } |
| 6285 | } else { |
| 6286 | OPJ_FLOAT32 temp_rate; |
| 6287 | OPJ_BOOL cap = OPJ_FALSE; |
| 6288 | temp_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/ |
| 6289 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy); |
| 6290 | for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
| 6291 | if (parameters->tcp_rates[i] < temp_rate) { |
| 6292 | parameters->tcp_rates[i] = temp_rate; |
| 6293 | cap = OPJ_TRUE; |
| 6294 | } |
| 6295 | } |
| 6296 | if (cap) { |
| 6297 | opj_event_msg(p_manager, EVT_WARNING, |
| 6298 | "The desired maximum codestream size has limited\n" |
| 6299 | "at least one of the desired quality layers\n"); |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | /* Manage profiles and applications and set RSIZ */ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6304 | /* set cinema parameters if required */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6305 | if (OPJ_IS_CINEMA(parameters->rsiz)){ |
| 6306 | if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) |
| 6307 | || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)){ |
| 6308 | opj_event_msg(p_manager, EVT_WARNING, |
| 6309 | "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); |
| 6310 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6311 | } else { |
| 6312 | opj_j2k_set_cinema_parameters(parameters,image,p_manager); |
| 6313 | if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,p_manager)) { |
| 6314 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6315 | } |
| 6316 | } |
| 6317 | } else if (OPJ_IS_STORAGE(parameters->rsiz)) { |
| 6318 | opj_event_msg(p_manager, EVT_WARNING, |
| 6319 | "JPEG 2000 Long Term Storage profile not yet supported\n"); |
| 6320 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6321 | } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { |
| 6322 | opj_event_msg(p_manager, EVT_WARNING, |
| 6323 | "JPEG 2000 Broadcast profiles not yet supported\n"); |
| 6324 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6325 | } else if (OPJ_IS_IMF(parameters->rsiz)) { |
| 6326 | opj_event_msg(p_manager, EVT_WARNING, |
| 6327 | "JPEG 2000 IMF profiles not yet supported\n"); |
| 6328 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6329 | } else if (OPJ_IS_PART2(parameters->rsiz)) { |
| 6330 | if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { |
| 6331 | opj_event_msg(p_manager, EVT_WARNING, |
| 6332 | "JPEG 2000 Part-2 profile defined\n" |
| 6333 | "but no Part-2 extension enabled.\n" |
| 6334 | "Profile set to NONE.\n"); |
| 6335 | parameters->rsiz = OPJ_PROFILE_NONE; |
| 6336 | } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { |
| 6337 | opj_event_msg(p_manager, EVT_WARNING, |
| 6338 | "Unsupported Part-2 extension enabled\n" |
| 6339 | "Profile set to NONE.\n"); |
| 6340 | parameters->rsiz = OPJ_PROFILE_NONE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | /* |
| 6345 | copy user encoding parameters |
| 6346 | */ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6347 | cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6348 | cp->rsiz = parameters->rsiz; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6349 | cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u; |
| 6350 | cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u; |
| 6351 | cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u; |
| 6352 | |
| 6353 | /* mod fixed_quality */ |
| 6354 | if (parameters->cp_fixed_alloc && parameters->cp_matrice) { |
| 6355 | size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); |
| 6356 | cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6357 | if (!cp->m_specific_param.m_enc.m_matrice) { |
| 6358 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of user encoding parameters matrix \n"); |
| 6359 | return OPJ_FALSE; |
| 6360 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6361 | memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size); |
| 6362 | } |
| 6363 | |
| 6364 | /* tiles */ |
| 6365 | cp->tdx = (OPJ_UINT32)parameters->cp_tdx; |
| 6366 | cp->tdy = (OPJ_UINT32)parameters->cp_tdy; |
| 6367 | |
| 6368 | /* tile offset */ |
| 6369 | cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; |
| 6370 | cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; |
| 6371 | |
| 6372 | /* comment string */ |
| 6373 | if(parameters->cp_comment) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6374 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); |
| 6375 | if(!cp->comment) { |
| 6376 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of comment string\n"); |
| 6377 | return OPJ_FALSE; |
| 6378 | } |
| 6379 | strcpy(cp->comment, parameters->cp_comment); |
| 6380 | } else { |
| 6381 | /* Create default comment for codestream */ |
| 6382 | const char comment[] = "Created by OpenJPEG version "; |
| 6383 | const size_t clen = strlen(comment); |
| 6384 | const char *version = opj_version(); |
| 6385 | |
| 6386 | /* UniPG>> */ |
| 6387 | #ifdef USE_JPWL |
| 6388 | cp->comment = (char*)opj_malloc(clen+strlen(version)+11); |
| 6389 | if(!cp->comment) { |
| 6390 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n"); |
| 6391 | return OPJ_FALSE; |
| 6392 | } |
| 6393 | sprintf(cp->comment,"%s%s with JPWL", comment, version); |
| 6394 | #else |
| 6395 | cp->comment = (char*)opj_malloc(clen+strlen(version)+1); |
| 6396 | if(!cp->comment) { |
| 6397 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n"); |
| 6398 | return OPJ_FALSE; |
| 6399 | } |
| 6400 | sprintf(cp->comment,"%s%s", comment, version); |
| 6401 | #endif |
| 6402 | /* <<UniPG */ |
| 6403 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6404 | |
| 6405 | /* |
| 6406 | calculate other encoding parameters |
| 6407 | */ |
| 6408 | |
| 6409 | if (parameters->tile_size_on) { |
| 6410 | cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), (OPJ_INT32)cp->tdx); |
| 6411 | cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), (OPJ_INT32)cp->tdy); |
| 6412 | } else { |
| 6413 | cp->tdx = image->x1 - cp->tx0; |
| 6414 | cp->tdy = image->y1 - cp->ty0; |
| 6415 | } |
| 6416 | |
| 6417 | if (parameters->tp_on) { |
| 6418 | cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; |
| 6419 | cp->m_specific_param.m_enc.m_tp_on = 1; |
| 6420 | } |
| 6421 | |
| 6422 | #ifdef USE_JPWL |
| 6423 | /* |
| 6424 | calculate JPWL encoding parameters |
| 6425 | */ |
| 6426 | |
| 6427 | if (parameters->jpwl_epc_on) { |
| 6428 | OPJ_INT32 i; |
| 6429 | |
| 6430 | /* set JPWL on */ |
| 6431 | cp->epc_on = OPJ_TRUE; |
| 6432 | cp->info_on = OPJ_FALSE; /* no informative technique */ |
| 6433 | |
| 6434 | /* set EPB on */ |
| 6435 | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { |
| 6436 | cp->epb_on = OPJ_TRUE; |
| 6437 | |
| 6438 | cp->hprot_MH = parameters->jpwl_hprot_MH; |
| 6439 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
| 6440 | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; |
| 6441 | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; |
| 6442 | } |
| 6443 | /* if tile specs are not specified, copy MH specs */ |
| 6444 | if (cp->hprot_TPH[0] == -1) { |
| 6445 | cp->hprot_TPH_tileno[0] = 0; |
| 6446 | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; |
| 6447 | } |
| 6448 | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { |
| 6449 | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; |
| 6450 | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; |
| 6451 | cp->pprot[i] = parameters->jpwl_pprot[i]; |
| 6452 | } |
| 6453 | } |
| 6454 | |
| 6455 | /* set ESD writing */ |
| 6456 | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { |
| 6457 | cp->esd_on = OPJ_TRUE; |
| 6458 | |
| 6459 | cp->sens_size = parameters->jpwl_sens_size; |
| 6460 | cp->sens_addr = parameters->jpwl_sens_addr; |
| 6461 | cp->sens_range = parameters->jpwl_sens_range; |
| 6462 | |
| 6463 | cp->sens_MH = parameters->jpwl_sens_MH; |
| 6464 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
| 6465 | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; |
| 6466 | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | /* always set RED writing to false: we are at the encoder */ |
| 6471 | cp->red_on = OPJ_FALSE; |
| 6472 | |
| 6473 | } else { |
| 6474 | cp->epc_on = OPJ_FALSE; |
| 6475 | } |
| 6476 | #endif /* USE_JPWL */ |
| 6477 | |
| 6478 | /* initialize the mutiple tiles */ |
| 6479 | /* ---------------------------- */ |
| 6480 | cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6481 | if (!cp->tcps) { |
| 6482 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile coding parameters\n"); |
| 6483 | return OPJ_FALSE; |
| 6484 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6485 | if (parameters->numpocs) { |
| 6486 | /* initialisation of POC */ |
| 6487 | opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager); |
| 6488 | /* TODO MSD use the return value*/ |
| 6489 | } |
| 6490 | |
| 6491 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
| 6492 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
| 6493 | tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; |
| 6494 | |
| 6495 | for (j = 0; j < tcp->numlayers; j++) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6496 | if(OPJ_IS_CINEMA(cp->rsiz)){ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6497 | if (cp->m_specific_param.m_enc.m_fixed_quality) { |
| 6498 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
| 6499 | } |
| 6500 | tcp->rates[j] = parameters->tcp_rates[j]; |
| 6501 | }else{ |
| 6502 | if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ |
| 6503 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
| 6504 | } else { |
| 6505 | tcp->rates[j] = parameters->tcp_rates[j]; |
| 6506 | } |
| 6507 | } |
| 6508 | } |
| 6509 | |
| 6510 | tcp->csty = (OPJ_UINT32)parameters->csty; |
| 6511 | tcp->prg = parameters->prog_order; |
| 6512 | tcp->mct = (OPJ_UINT32)parameters->tcp_mct; |
| 6513 | |
| 6514 | numpocs_tile = 0; |
| 6515 | tcp->POC = 0; |
| 6516 | |
| 6517 | if (parameters->numpocs) { |
| 6518 | /* initialisation of POC */ |
| 6519 | tcp->POC = 1; |
| 6520 | for (i = 0; i < parameters->numpocs; i++) { |
| 6521 | if (tileno + 1 == parameters->POC[i].tile ) { |
| 6522 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; |
| 6523 | |
| 6524 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; |
| 6525 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; |
| 6526 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; |
| 6527 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; |
| 6528 | tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; |
| 6529 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; |
| 6530 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; |
| 6531 | |
| 6532 | numpocs_tile++; |
| 6533 | } |
| 6534 | } |
| 6535 | |
| 6536 | tcp->numpocs = numpocs_tile -1 ; |
| 6537 | }else{ |
| 6538 | tcp->numpocs = 0; |
| 6539 | } |
| 6540 | |
| 6541 | tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6542 | if (!tcp->tccps) { |
| 6543 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile component coding parameters\n"); |
| 6544 | return OPJ_FALSE; |
| 6545 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6546 | if (parameters->mct_data) { |
| 6547 | |
| 6548 | OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
| 6549 | OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
| 6550 | OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize); |
| 6551 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6552 | if (!lTmpBuf) { |
| 6553 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate temp buffer\n"); |
| 6554 | return OPJ_FALSE; |
| 6555 | } |
| 6556 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6557 | tcp->mct = 2; |
| 6558 | tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6559 | if (! tcp->m_mct_coding_matrix) { |
| 6560 | opj_free(lTmpBuf); |
| 6561 | lTmpBuf = NULL; |
| 6562 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT coding matrix \n"); |
| 6563 | return OPJ_FALSE; |
| 6564 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6565 | memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize); |
| 6566 | memcpy(lTmpBuf,parameters->mct_data,lMctSize); |
| 6567 | |
| 6568 | tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6569 | if (! tcp->m_mct_decoding_matrix) { |
| 6570 | opj_free(lTmpBuf); |
| 6571 | lTmpBuf = NULL; |
| 6572 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT decoding matrix \n"); |
| 6573 | return OPJ_FALSE; |
| 6574 | } |
| 6575 | if(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps) == OPJ_FALSE) { |
| 6576 | opj_free(lTmpBuf); |
| 6577 | lTmpBuf = NULL; |
| 6578 | opj_event_msg(p_manager, EVT_ERROR, "Failed to inverse encoder MCT decoding matrix \n"); |
| 6579 | return OPJ_FALSE; |
| 6580 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6581 | |
| 6582 | tcp->mct_norms = (OPJ_FLOAT64*) |
| 6583 | opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6584 | if (! tcp->mct_norms) { |
| 6585 | opj_free(lTmpBuf); |
| 6586 | lTmpBuf = NULL; |
| 6587 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT norms \n"); |
| 6588 | return OPJ_FALSE; |
| 6589 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6590 | opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix); |
| 6591 | opj_free(lTmpBuf); |
| 6592 | |
| 6593 | for (i = 0; i < image->numcomps; i++) { |
| 6594 | opj_tccp_t *tccp = &tcp->tccps[i]; |
| 6595 | tccp->m_dc_level_shift = l_dc_shift[i]; |
| 6596 | } |
| 6597 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6598 | if (opj_j2k_setup_mct_encoding(tcp,image) == OPJ_FALSE) { |
| 6599 | /* free will be handled by opj_j2k_destroy */ |
| 6600 | opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); |
| 6601 | return OPJ_FALSE; |
| 6602 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6603 | } |
| 6604 | else { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6605 | if(tcp->mct==1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ |
| 6606 | if ((image->comps[0].dx != image->comps[1].dx) || |
| 6607 | (image->comps[0].dx != image->comps[2].dx) || |
| 6608 | (image->comps[0].dy != image->comps[1].dy) || |
| 6609 | (image->comps[0].dy != image->comps[2].dy)) { |
| 6610 | opj_event_msg(p_manager, EVT_WARNING, "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); |
| 6611 | tcp->mct = 0; |
| 6612 | } |
| 6613 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6614 | for (i = 0; i < image->numcomps; i++) { |
| 6615 | opj_tccp_t *tccp = &tcp->tccps[i]; |
| 6616 | opj_image_comp_t * l_comp = &(image->comps[i]); |
| 6617 | |
| 6618 | if (! l_comp->sgnd) { |
| 6619 | tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); |
| 6620 | } |
| 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | for (i = 0; i < image->numcomps; i++) { |
| 6625 | opj_tccp_t *tccp = &tcp->tccps[i]; |
| 6626 | |
| 6627 | tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */ |
| 6628 | tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; |
| 6629 | tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
| 6630 | tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
| 6631 | tccp->cblksty = (OPJ_UINT32)parameters->mode; |
| 6632 | tccp->qmfbid = parameters->irreversible ? 0 : 1; |
| 6633 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; |
| 6634 | tccp->numgbits = 2; |
| 6635 | |
| 6636 | if ((OPJ_INT32)i == parameters->roi_compno) { |
| 6637 | tccp->roishift = parameters->roi_shift; |
| 6638 | } else { |
| 6639 | tccp->roishift = 0; |
| 6640 | } |
| 6641 | |
| 6642 | if (parameters->csty & J2K_CCP_CSTY_PRT) { |
| 6643 | OPJ_INT32 p = 0, it_res; |
| 6644 | assert( tccp->numresolutions > 0 ); |
| 6645 | for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { |
| 6646 | if (p < parameters->res_spec) { |
| 6647 | |
| 6648 | if (parameters->prcw_init[p] < 1) { |
| 6649 | tccp->prcw[it_res] = 1; |
| 6650 | } else { |
| 6651 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); |
| 6652 | } |
| 6653 | |
| 6654 | if (parameters->prch_init[p] < 1) { |
| 6655 | tccp->prch[it_res] = 1; |
| 6656 | }else { |
| 6657 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); |
| 6658 | } |
| 6659 | |
| 6660 | } else { |
| 6661 | OPJ_INT32 res_spec = parameters->res_spec; |
| 6662 | OPJ_INT32 size_prcw = 0; |
| 6663 | OPJ_INT32 size_prch = 0; |
| 6664 | |
| 6665 | assert(res_spec>0); /* issue 189 */ |
| 6666 | size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); |
| 6667 | size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); |
| 6668 | |
| 6669 | |
| 6670 | if (size_prcw < 1) { |
| 6671 | tccp->prcw[it_res] = 1; |
| 6672 | } else { |
| 6673 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); |
| 6674 | } |
| 6675 | |
| 6676 | if (size_prch < 1) { |
| 6677 | tccp->prch[it_res] = 1; |
| 6678 | } else { |
| 6679 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); |
| 6680 | } |
| 6681 | } |
| 6682 | p++; |
| 6683 | /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ |
| 6684 | } /*end for*/ |
| 6685 | } else { |
| 6686 | for (j = 0; j < tccp->numresolutions; j++) { |
| 6687 | tccp->prcw[j] = 15; |
| 6688 | tccp->prch[j] = 15; |
| 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); |
| 6693 | } |
| 6694 | } |
| 6695 | |
| 6696 | if (parameters->mct_data) { |
| 6697 | opj_free(parameters->mct_data); |
| 6698 | parameters->mct_data = 00; |
| 6699 | } |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 6700 | return OPJ_TRUE; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
| 6704 | { |
| 6705 | assert(cstr_index != 00); |
| 6706 | |
| 6707 | /* expand the list? */ |
| 6708 | if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { |
| 6709 | opj_marker_info_t *new_marker; |
| 6710 | cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum); |
| 6711 | new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t)); |
| 6712 | if (! new_marker) { |
| 6713 | opj_free(cstr_index->marker); |
| 6714 | cstr_index->marker = NULL; |
| 6715 | cstr_index->maxmarknum = 0; |
| 6716 | cstr_index->marknum = 0; |
| 6717 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ |
| 6718 | return OPJ_FALSE; |
| 6719 | } |
| 6720 | cstr_index->marker = new_marker; |
| 6721 | } |
| 6722 | |
| 6723 | /* add the marker */ |
| 6724 | cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; |
| 6725 | cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; |
| 6726 | cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; |
| 6727 | cstr_index->marknum++; |
| 6728 | return OPJ_TRUE; |
| 6729 | } |
| 6730 | |
| 6731 | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
| 6732 | { |
| 6733 | assert(cstr_index != 00); |
| 6734 | assert(cstr_index->tile_index != 00); |
| 6735 | |
| 6736 | /* expand the list? */ |
| 6737 | if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) { |
| 6738 | opj_marker_info_t *new_marker; |
| 6739 | cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); |
| 6740 | new_marker = (opj_marker_info_t *) opj_realloc( |
| 6741 | cstr_index->tile_index[tileno].marker, |
| 6742 | cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t)); |
| 6743 | if (! new_marker) { |
| 6744 | opj_free(cstr_index->tile_index[tileno].marker); |
| 6745 | cstr_index->tile_index[tileno].marker = NULL; |
| 6746 | cstr_index->tile_index[tileno].maxmarknum = 0; |
| 6747 | cstr_index->tile_index[tileno].marknum = 0; |
| 6748 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ |
| 6749 | return OPJ_FALSE; |
| 6750 | } |
| 6751 | cstr_index->tile_index[tileno].marker = new_marker; |
| 6752 | } |
| 6753 | |
| 6754 | /* add the marker */ |
| 6755 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type; |
| 6756 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos; |
| 6757 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len; |
| 6758 | cstr_index->tile_index[tileno].marknum++; |
| 6759 | |
| 6760 | if (type == J2K_MS_SOT) { |
| 6761 | OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; |
| 6762 | |
| 6763 | if (cstr_index->tile_index[tileno].tp_index) |
| 6764 | cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; |
| 6765 | |
| 6766 | } |
| 6767 | return OPJ_TRUE; |
| 6768 | } |
| 6769 | |
| 6770 | /* |
| 6771 | * ----------------------------------------------------------------------- |
| 6772 | * ----------------------------------------------------------------------- |
| 6773 | * ----------------------------------------------------------------------- |
| 6774 | */ |
| 6775 | |
| 6776 | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, |
| 6777 | opj_stream_private_t *p_stream, |
| 6778 | opj_event_mgr_t * p_manager |
| 6779 | ) |
| 6780 | { |
| 6781 | (void)p_j2k; |
| 6782 | (void)p_stream; |
| 6783 | (void)p_manager; |
| 6784 | return OPJ_TRUE; |
| 6785 | } |
| 6786 | |
| 6787 | OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream, |
| 6788 | opj_j2k_t* p_j2k, |
| 6789 | opj_image_t** p_image, |
| 6790 | opj_event_mgr_t* p_manager ) |
| 6791 | { |
| 6792 | /* preconditions */ |
| 6793 | assert(p_j2k != 00); |
| 6794 | assert(p_stream != 00); |
| 6795 | assert(p_manager != 00); |
| 6796 | |
| 6797 | /* create an empty image header */ |
| 6798 | p_j2k->m_private_image = opj_image_create0(); |
| 6799 | if (! p_j2k->m_private_image) { |
| 6800 | return OPJ_FALSE; |
| 6801 | } |
| 6802 | |
| 6803 | /* customization of the validation */ |
| 6804 | opj_j2k_setup_decoding_validation(p_j2k); |
| 6805 | |
| 6806 | /* validation of the parameters codec */ |
| 6807 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) { |
| 6808 | opj_image_destroy(p_j2k->m_private_image); |
| 6809 | p_j2k->m_private_image = NULL; |
| 6810 | return OPJ_FALSE; |
| 6811 | } |
| 6812 | |
| 6813 | /* customization of the encoding */ |
| 6814 | opj_j2k_setup_header_reading(p_j2k); |
| 6815 | |
| 6816 | /* read header */ |
| 6817 | if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { |
| 6818 | opj_image_destroy(p_j2k->m_private_image); |
| 6819 | p_j2k->m_private_image = NULL; |
| 6820 | return OPJ_FALSE; |
| 6821 | } |
| 6822 | |
| 6823 | *p_image = opj_image_create0(); |
| 6824 | if (! (*p_image)) { |
| 6825 | return OPJ_FALSE; |
| 6826 | } |
| 6827 | |
| 6828 | /* Copy codestream image information to the output image */ |
| 6829 | opj_copy_image_header(p_j2k->m_private_image, *p_image); |
| 6830 | |
| 6831 | /*Allocate and initialize some elements of codestrem index*/ |
| 6832 | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ |
| 6833 | return OPJ_FALSE; |
| 6834 | } |
| 6835 | |
| 6836 | return OPJ_TRUE; |
| 6837 | } |
| 6838 | |
| 6839 | void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k) |
| 6840 | { |
| 6841 | /* preconditions*/ |
| 6842 | assert(p_j2k != 00); |
| 6843 | |
| 6844 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure); |
| 6845 | |
| 6846 | /* DEVELOPER CORNER, add your custom procedures */ |
| 6847 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd); |
| 6848 | |
| 6849 | } |
| 6850 | |
| 6851 | void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k) |
| 6852 | { |
| 6853 | /* preconditions*/ |
| 6854 | assert(p_j2k != 00); |
| 6855 | |
| 6856 | opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder); |
| 6857 | opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation); |
| 6858 | /* DEVELOPER CORNER, add your custom validation procedure */ |
| 6859 | |
| 6860 | } |
| 6861 | |
| 6862 | OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k, |
| 6863 | opj_stream_private_t *p_stream, |
| 6864 | opj_event_mgr_t * p_manager ) |
| 6865 | { |
| 6866 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
| 6867 | OPJ_UINT32 i,j; |
| 6868 | |
| 6869 | /* preconditions */ |
| 6870 | assert(p_j2k != 00); |
| 6871 | assert(p_stream != 00); |
| 6872 | assert(p_manager != 00); |
| 6873 | |
| 6874 | if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { |
| 6875 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 6876 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
| 6877 | |
| 6878 | for (i=0;i<l_nb_tiles;++i) { |
| 6879 | if (l_tcp->mct == 2) { |
| 6880 | opj_tccp_t * l_tccp = l_tcp->tccps; |
| 6881 | l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); |
| 6882 | |
| 6883 | for (j=0;j<p_j2k->m_private_image->numcomps;++j) { |
| 6884 | l_is_valid &= ! (l_tccp->qmfbid & 1); |
| 6885 | ++l_tccp; |
| 6886 | } |
| 6887 | } |
| 6888 | ++l_tcp; |
| 6889 | } |
| 6890 | } |
| 6891 | |
| 6892 | return l_is_valid; |
| 6893 | } |
| 6894 | |
| 6895 | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) |
| 6896 | { |
| 6897 | OPJ_UINT32 i; |
| 6898 | OPJ_UINT32 l_indix = 1; |
| 6899 | opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00; |
| 6900 | opj_simple_mcc_decorrelation_data_t * l_mcc_data; |
| 6901 | OPJ_UINT32 l_mct_size,l_nb_elem; |
| 6902 | OPJ_FLOAT32 * l_data, * l_current_data; |
| 6903 | opj_tccp_t * l_tccp; |
| 6904 | |
| 6905 | /* preconditions */ |
| 6906 | assert(p_tcp != 00); |
| 6907 | |
| 6908 | if (p_tcp->mct != 2) { |
| 6909 | return OPJ_TRUE; |
| 6910 | } |
| 6911 | |
| 6912 | if (p_tcp->m_mct_decoding_matrix) { |
| 6913 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
| 6914 | opj_mct_data_t *new_mct_records; |
| 6915 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
| 6916 | |
| 6917 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
| 6918 | if (! new_mct_records) { |
| 6919 | opj_free(p_tcp->m_mct_records); |
| 6920 | p_tcp->m_mct_records = NULL; |
| 6921 | p_tcp->m_nb_max_mct_records = 0; |
| 6922 | p_tcp->m_nb_mct_records = 0; |
| 6923 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
| 6924 | return OPJ_FALSE; |
| 6925 | } |
| 6926 | p_tcp->m_mct_records = new_mct_records; |
| 6927 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
| 6928 | |
| 6929 | memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); |
| 6930 | } |
| 6931 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
| 6932 | |
| 6933 | if (l_mct_deco_data->m_data) { |
| 6934 | opj_free(l_mct_deco_data->m_data); |
| 6935 | l_mct_deco_data->m_data = 00; |
| 6936 | } |
| 6937 | |
| 6938 | l_mct_deco_data->m_index = l_indix++; |
| 6939 | l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; |
| 6940 | l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; |
| 6941 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
| 6942 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; |
| 6943 | l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); |
| 6944 | |
| 6945 | if (! l_mct_deco_data->m_data) { |
| 6946 | return OPJ_FALSE; |
| 6947 | } |
| 6948 | |
| 6949 | j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem); |
| 6950 | |
| 6951 | l_mct_deco_data->m_data_size = l_mct_size; |
| 6952 | ++p_tcp->m_nb_mct_records; |
| 6953 | } |
| 6954 | |
| 6955 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
| 6956 | opj_mct_data_t *new_mct_records; |
| 6957 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
| 6958 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
| 6959 | if (! new_mct_records) { |
| 6960 | opj_free(p_tcp->m_mct_records); |
| 6961 | p_tcp->m_mct_records = NULL; |
| 6962 | p_tcp->m_nb_max_mct_records = 0; |
| 6963 | p_tcp->m_nb_mct_records = 0; |
| 6964 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
| 6965 | return OPJ_FALSE; |
| 6966 | } |
| 6967 | p_tcp->m_mct_records = new_mct_records; |
| 6968 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
| 6969 | |
| 6970 | memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); |
| 6971 | |
| 6972 | if (l_mct_deco_data) { |
| 6973 | l_mct_deco_data = l_mct_offset_data - 1; |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
| 6978 | |
| 6979 | if (l_mct_offset_data->m_data) { |
| 6980 | opj_free(l_mct_offset_data->m_data); |
| 6981 | l_mct_offset_data->m_data = 00; |
| 6982 | } |
| 6983 | |
| 6984 | l_mct_offset_data->m_index = l_indix++; |
| 6985 | l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; |
| 6986 | l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; |
| 6987 | l_nb_elem = p_image->numcomps; |
| 6988 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; |
| 6989 | l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); |
| 6990 | |
| 6991 | if (! l_mct_offset_data->m_data) { |
| 6992 | return OPJ_FALSE; |
| 6993 | } |
| 6994 | |
| 6995 | l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); |
| 6996 | if (! l_data) { |
| 6997 | opj_free(l_mct_offset_data->m_data); |
| 6998 | l_mct_offset_data->m_data = 00; |
| 6999 | return OPJ_FALSE; |
| 7000 | } |
| 7001 | |
| 7002 | l_tccp = p_tcp->tccps; |
| 7003 | l_current_data = l_data; |
| 7004 | |
| 7005 | for (i=0;i<l_nb_elem;++i) { |
| 7006 | *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift); |
| 7007 | ++l_tccp; |
| 7008 | } |
| 7009 | |
| 7010 | j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem); |
| 7011 | |
| 7012 | opj_free(l_data); |
| 7013 | |
| 7014 | l_mct_offset_data->m_data_size = l_mct_size; |
| 7015 | |
| 7016 | ++p_tcp->m_nb_mct_records; |
| 7017 | |
| 7018 | if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { |
| 7019 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
| 7020 | p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
| 7021 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
| 7022 | p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); |
| 7023 | if (! new_mcc_records) { |
| 7024 | opj_free(p_tcp->m_mcc_records); |
| 7025 | p_tcp->m_mcc_records = NULL; |
| 7026 | p_tcp->m_nb_max_mcc_records = 0; |
| 7027 | p_tcp->m_nb_mcc_records = 0; |
| 7028 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
| 7029 | return OPJ_FALSE; |
| 7030 | } |
| 7031 | p_tcp->m_mcc_records = new_mcc_records; |
| 7032 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
| 7033 | memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); |
| 7034 | |
| 7035 | } |
| 7036 | |
| 7037 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
| 7038 | l_mcc_data->m_decorrelation_array = l_mct_deco_data; |
| 7039 | l_mcc_data->m_is_irreversible = 1; |
| 7040 | l_mcc_data->m_nb_comps = p_image->numcomps; |
| 7041 | l_mcc_data->m_index = l_indix++; |
| 7042 | l_mcc_data->m_offset_array = l_mct_offset_data; |
| 7043 | ++p_tcp->m_nb_mcc_records; |
| 7044 | |
| 7045 | return OPJ_TRUE; |
| 7046 | } |
| 7047 | |
| 7048 | OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k, |
| 7049 | opj_stream_private_t *p_stream, |
| 7050 | opj_event_mgr_t * p_manager ) |
| 7051 | { |
| 7052 | /* add here initialization of cp |
| 7053 | copy paste of setup_decoder */ |
| 7054 | (void)p_j2k; |
| 7055 | (void)p_stream; |
| 7056 | (void)p_manager; |
| 7057 | return OPJ_TRUE; |
| 7058 | } |
| 7059 | |
| 7060 | OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k, |
| 7061 | opj_stream_private_t *p_stream, |
| 7062 | opj_event_mgr_t * p_manager ) |
| 7063 | { |
| 7064 | /* add here initialization of cp |
| 7065 | copy paste of setup_encoder */ |
| 7066 | (void)p_j2k; |
| 7067 | (void)p_stream; |
| 7068 | (void)p_manager; |
| 7069 | return OPJ_TRUE; |
| 7070 | } |
| 7071 | |
| 7072 | OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, |
| 7073 | opj_stream_private_t *p_stream, |
| 7074 | opj_event_mgr_t * p_manager ) |
| 7075 | { |
| 7076 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
| 7077 | |
| 7078 | /* preconditions */ |
| 7079 | assert(p_j2k != 00); |
| 7080 | assert(p_stream != 00); |
| 7081 | assert(p_manager != 00); |
| 7082 | |
| 7083 | /* STATE checking */ |
| 7084 | /* make sure the state is at 0 */ |
| 7085 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); |
| 7086 | |
| 7087 | /* POINTER validation */ |
| 7088 | /* make sure a p_j2k codec is present */ |
| 7089 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
| 7090 | /* make sure a validation list is present */ |
| 7091 | l_is_valid &= (p_j2k->m_validation_list != 00); |
| 7092 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7093 | /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ |
| 7094 | /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ |
| 7095 | /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ |
| 7096 | if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7097 | opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); |
| 7098 | return OPJ_FALSE; |
| 7099 | } |
| 7100 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7101 | if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
| 7102 | opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); |
| 7103 | return OPJ_FALSE; |
| 7104 | } |
| 7105 | |
| 7106 | if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7107 | opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); |
| 7108 | return OPJ_FALSE; |
| 7109 | } |
| 7110 | |
| 7111 | /* PARAMETER VALIDATION */ |
| 7112 | return l_is_valid; |
| 7113 | } |
| 7114 | |
| 7115 | OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t *p_j2k, |
| 7116 | opj_stream_private_t *p_stream, |
| 7117 | opj_event_mgr_t * p_manager |
| 7118 | ) |
| 7119 | { |
| 7120 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
| 7121 | |
| 7122 | /* preconditions*/ |
| 7123 | assert(p_j2k != 00); |
| 7124 | assert(p_stream != 00); |
| 7125 | assert(p_manager != 00); |
| 7126 | |
| 7127 | /* STATE checking */ |
| 7128 | /* make sure the state is at 0 */ |
| 7129 | #ifdef TODO_MSD |
| 7130 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); |
| 7131 | #endif |
| 7132 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); |
| 7133 | |
| 7134 | /* POINTER validation */ |
| 7135 | /* make sure a p_j2k codec is present */ |
| 7136 | /* make sure a procedure list is present */ |
| 7137 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
| 7138 | /* make sure a validation list is present */ |
| 7139 | l_is_valid &= (p_j2k->m_validation_list != 00); |
| 7140 | |
| 7141 | /* PARAMETER VALIDATION */ |
| 7142 | return l_is_valid; |
| 7143 | } |
| 7144 | |
| 7145 | OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, |
| 7146 | opj_stream_private_t *p_stream, |
| 7147 | opj_event_mgr_t * p_manager) |
| 7148 | { |
| 7149 | OPJ_UINT32 l_current_marker; |
| 7150 | OPJ_UINT32 l_marker_size; |
| 7151 | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7152 | OPJ_BOOL l_has_siz = 0; |
| 7153 | OPJ_BOOL l_has_cod = 0; |
| 7154 | OPJ_BOOL l_has_qcd = 0; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7155 | |
| 7156 | /* preconditions */ |
| 7157 | assert(p_stream != 00); |
| 7158 | assert(p_j2k != 00); |
| 7159 | assert(p_manager != 00); |
| 7160 | |
| 7161 | /* We enter in the main header */ |
| 7162 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; |
| 7163 | |
| 7164 | /* Try to read the SOC marker, the codestream must begin with SOC marker */ |
| 7165 | if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) { |
| 7166 | opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); |
| 7167 | return OPJ_FALSE; |
| 7168 | } |
| 7169 | |
| 7170 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
| 7171 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7172 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7173 | return OPJ_FALSE; |
| 7174 | } |
| 7175 | |
| 7176 | /* Read 2 bytes as the new marker ID */ |
| 7177 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); |
| 7178 | |
| 7179 | /* Try to read until the SOT is detected */ |
| 7180 | while (l_current_marker != J2K_MS_SOT) { |
| 7181 | |
| 7182 | /* Check if the current marker ID is valid */ |
| 7183 | if (l_current_marker < 0xff00) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7184 | opj_event_msg(p_manager, EVT_ERROR, "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7185 | return OPJ_FALSE; |
| 7186 | } |
| 7187 | |
| 7188 | /* Get the marker handler from the marker ID */ |
| 7189 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
| 7190 | |
| 7191 | /* Manage case where marker is unknown */ |
| 7192 | if (l_marker_handler->id == J2K_MS_UNK) { |
| 7193 | if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){ |
| 7194 | opj_event_msg(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n"); |
| 7195 | return OPJ_FALSE; |
| 7196 | } |
| 7197 | |
| 7198 | if (l_current_marker == J2K_MS_SOT) |
| 7199 | break; /* SOT marker is detected main header is completely read */ |
| 7200 | else /* Get the marker handler from the marker ID */ |
| 7201 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
| 7202 | } |
| 7203 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7204 | if (l_marker_handler->id == J2K_MS_SIZ) { |
| 7205 | /* Mark required SIZ marker as found */ |
| 7206 | l_has_siz = 1; |
| 7207 | } |
| 7208 | if (l_marker_handler->id == J2K_MS_COD) { |
| 7209 | /* Mark required COD marker as found */ |
| 7210 | l_has_cod = 1; |
| 7211 | } |
| 7212 | if (l_marker_handler->id == J2K_MS_QCD) { |
| 7213 | /* Mark required QCD marker as found */ |
| 7214 | l_has_qcd = 1; |
| 7215 | } |
| 7216 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7217 | /* Check if the marker is known and if it is the right place to find it */ |
| 7218 | if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { |
| 7219 | opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); |
| 7220 | return OPJ_FALSE; |
| 7221 | } |
| 7222 | |
| 7223 | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
| 7224 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7225 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7226 | return OPJ_FALSE; |
| 7227 | } |
| 7228 | |
| 7229 | /* read 2 bytes as the marker size */ |
| 7230 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); |
| 7231 | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
| 7232 | |
| 7233 | /* Check if the marker size is compatible with the header data size */ |
| 7234 | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
| 7235 | OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
| 7236 | if (! new_header_data) { |
| 7237 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
| 7238 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
| 7239 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
| 7240 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
| 7241 | return OPJ_FALSE; |
| 7242 | } |
| 7243 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
| 7244 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
| 7245 | } |
| 7246 | |
| 7247 | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
| 7248 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { |
| 7249 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7250 | return OPJ_FALSE; |
| 7251 | } |
| 7252 | |
| 7253 | /* Read the marker segment with the correct marker handler */ |
| 7254 | if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { |
| 7255 | opj_event_msg(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n"); |
| 7256 | return OPJ_FALSE; |
| 7257 | } |
| 7258 | |
| 7259 | /* Add the marker to the codestream index*/ |
| 7260 | if (OPJ_FALSE == opj_j2k_add_mhmarker( |
| 7261 | p_j2k->cstr_index, |
| 7262 | l_marker_handler->id, |
| 7263 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
| 7264 | l_marker_size + 4 )) { |
| 7265 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); |
| 7266 | return OPJ_FALSE; |
| 7267 | } |
| 7268 | |
| 7269 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
| 7270 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7271 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7272 | return OPJ_FALSE; |
| 7273 | } |
| 7274 | |
| 7275 | /* read 2 bytes as the new marker ID */ |
| 7276 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); |
| 7277 | } |
| 7278 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7279 | if (l_has_siz == 0) { |
| 7280 | opj_event_msg(p_manager, EVT_ERROR, "required SIZ marker not found in main header\n"); |
| 7281 | return OPJ_FALSE; |
| 7282 | } |
| 7283 | if (l_has_cod == 0) { |
| 7284 | opj_event_msg(p_manager, EVT_ERROR, "required COD marker not found in main header\n"); |
| 7285 | return OPJ_FALSE; |
| 7286 | } |
| 7287 | if (l_has_qcd == 0) { |
| 7288 | opj_event_msg(p_manager, EVT_ERROR, "required QCD marker not found in main header\n"); |
| 7289 | return OPJ_FALSE; |
| 7290 | } |
| 7291 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7292 | opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); |
| 7293 | |
| 7294 | /* Position of the last element if the main header */ |
| 7295 | p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; |
| 7296 | |
| 7297 | /* Next step: read a tile-part header */ |
| 7298 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
| 7299 | |
| 7300 | return OPJ_TRUE; |
| 7301 | } |
| 7302 | |
| 7303 | OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k, |
| 7304 | opj_procedure_list_t * p_procedure_list, |
| 7305 | opj_stream_private_t *p_stream, |
| 7306 | opj_event_mgr_t * p_manager ) |
| 7307 | { |
| 7308 | OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00; |
| 7309 | OPJ_BOOL l_result = OPJ_TRUE; |
| 7310 | OPJ_UINT32 l_nb_proc, i; |
| 7311 | |
| 7312 | /* preconditions*/ |
| 7313 | assert(p_procedure_list != 00); |
| 7314 | assert(p_j2k != 00); |
| 7315 | assert(p_stream != 00); |
| 7316 | assert(p_manager != 00); |
| 7317 | |
| 7318 | l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); |
| 7319 | l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); |
| 7320 | |
| 7321 | for (i=0;i<l_nb_proc;++i) { |
| 7322 | l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager)); |
| 7323 | ++l_procedure; |
| 7324 | } |
| 7325 | |
| 7326 | /* and clear the procedure list at the end.*/ |
| 7327 | opj_procedure_list_clear(p_procedure_list); |
| 7328 | return l_result; |
| 7329 | } |
| 7330 | |
| 7331 | /* FIXME DOC*/ |
| 7332 | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k, |
| 7333 | opj_stream_private_t *p_stream, |
| 7334 | opj_event_mgr_t * p_manager |
| 7335 | ) |
| 7336 | { |
| 7337 | opj_tcp_t * l_tcp = 00; |
| 7338 | opj_tcp_t * l_default_tcp = 00; |
| 7339 | OPJ_UINT32 l_nb_tiles; |
| 7340 | OPJ_UINT32 i,j; |
| 7341 | opj_tccp_t *l_current_tccp = 00; |
| 7342 | OPJ_UINT32 l_tccp_size; |
| 7343 | OPJ_UINT32 l_mct_size; |
| 7344 | opj_image_t * l_image; |
| 7345 | OPJ_UINT32 l_mcc_records_size,l_mct_records_size; |
| 7346 | opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; |
| 7347 | opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; |
| 7348 | OPJ_UINT32 l_offset; |
| 7349 | |
| 7350 | /* preconditions */ |
| 7351 | assert(p_j2k != 00); |
| 7352 | assert(p_stream != 00); |
| 7353 | assert(p_manager != 00); |
| 7354 | |
| 7355 | l_image = p_j2k->m_private_image; |
| 7356 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 7357 | l_tcp = p_j2k->m_cp.tcps; |
| 7358 | l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); |
| 7359 | l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 7360 | l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
| 7361 | |
| 7362 | /* For each tile */ |
| 7363 | for (i=0; i<l_nb_tiles; ++i) { |
| 7364 | /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ |
| 7365 | l_current_tccp = l_tcp->tccps; |
| 7366 | /*Copy default coding parameters into the current tile coding parameters*/ |
| 7367 | memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); |
| 7368 | /* Initialize some values of the current tile coding parameters*/ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7369 | l_tcp->cod = 0; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7370 | l_tcp->ppt = 0; |
| 7371 | l_tcp->ppt_data = 00; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7372 | /* Remove memory not owned by this tile in case of early error return. */ |
| 7373 | l_tcp->m_mct_decoding_matrix = 00; |
| 7374 | l_tcp->m_nb_max_mct_records = 0; |
| 7375 | l_tcp->m_mct_records = 00; |
| 7376 | l_tcp->m_nb_max_mcc_records = 0; |
| 7377 | l_tcp->m_mcc_records = 00; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7378 | /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ |
| 7379 | l_tcp->tccps = l_current_tccp; |
| 7380 | |
| 7381 | /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ |
| 7382 | if (l_default_tcp->m_mct_decoding_matrix) { |
| 7383 | l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
| 7384 | if (! l_tcp->m_mct_decoding_matrix ) { |
| 7385 | return OPJ_FALSE; |
| 7386 | } |
| 7387 | memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size); |
| 7388 | } |
| 7389 | |
| 7390 | /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ |
| 7391 | l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(opj_mct_data_t); |
| 7392 | l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); |
| 7393 | if (! l_tcp->m_mct_records) { |
| 7394 | return OPJ_FALSE; |
| 7395 | } |
| 7396 | memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size); |
| 7397 | |
| 7398 | /* Copy the mct record data from dflt_tile_cp to the current tile*/ |
| 7399 | l_src_mct_rec = l_default_tcp->m_mct_records; |
| 7400 | l_dest_mct_rec = l_tcp->m_mct_records; |
| 7401 | |
| 7402 | for (j=0;j<l_default_tcp->m_nb_mct_records;++j) { |
| 7403 | |
| 7404 | if (l_src_mct_rec->m_data) { |
| 7405 | |
| 7406 | l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); |
| 7407 | if(! l_dest_mct_rec->m_data) { |
| 7408 | return OPJ_FALSE; |
| 7409 | } |
| 7410 | memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size); |
| 7411 | } |
| 7412 | |
| 7413 | ++l_src_mct_rec; |
| 7414 | ++l_dest_mct_rec; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7415 | /* Update with each pass to free exactly what has been allocated on early return. */ |
| 7416 | l_tcp->m_nb_max_mct_records += 1; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7417 | } |
| 7418 | |
| 7419 | /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ |
| 7420 | l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t); |
| 7421 | l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size); |
| 7422 | if (! l_tcp->m_mcc_records) { |
| 7423 | return OPJ_FALSE; |
| 7424 | } |
| 7425 | memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7426 | l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7427 | |
| 7428 | /* Copy the mcc record data from dflt_tile_cp to the current tile*/ |
| 7429 | l_src_mcc_rec = l_default_tcp->m_mcc_records; |
| 7430 | l_dest_mcc_rec = l_tcp->m_mcc_records; |
| 7431 | |
| 7432 | for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) { |
| 7433 | |
| 7434 | if (l_src_mcc_rec->m_decorrelation_array) { |
| 7435 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records); |
| 7436 | l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; |
| 7437 | } |
| 7438 | |
| 7439 | if (l_src_mcc_rec->m_offset_array) { |
| 7440 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records); |
| 7441 | l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; |
| 7442 | } |
| 7443 | |
| 7444 | ++l_src_mcc_rec; |
| 7445 | ++l_dest_mcc_rec; |
| 7446 | } |
| 7447 | |
| 7448 | /* Copy all the dflt_tile_compo_cp to the current tile cp */ |
| 7449 | memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size); |
| 7450 | |
| 7451 | /* Move to next tile cp*/ |
| 7452 | ++l_tcp; |
| 7453 | } |
| 7454 | |
| 7455 | /* Create the current tile decoder*/ |
| 7456 | p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */ |
| 7457 | if (! p_j2k->m_tcd ) { |
| 7458 | return OPJ_FALSE; |
| 7459 | } |
| 7460 | |
| 7461 | if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) { |
| 7462 | opj_tcd_destroy(p_j2k->m_tcd); |
| 7463 | p_j2k->m_tcd = 00; |
| 7464 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
| 7465 | return OPJ_FALSE; |
| 7466 | } |
| 7467 | |
| 7468 | return OPJ_TRUE; |
| 7469 | } |
| 7470 | |
| 7471 | const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id) |
| 7472 | { |
| 7473 | const opj_dec_memory_marker_handler_t *e; |
| 7474 | for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { |
| 7475 | if (e->id == p_id) { |
| 7476 | break; /* we find a handler corresponding to the marker ID*/ |
| 7477 | } |
| 7478 | } |
| 7479 | return e; |
| 7480 | } |
| 7481 | |
| 7482 | void opj_j2k_destroy (opj_j2k_t *p_j2k) |
| 7483 | { |
| 7484 | if (p_j2k == 00) { |
| 7485 | return; |
| 7486 | } |
| 7487 | |
| 7488 | if (p_j2k->m_is_decoder) { |
| 7489 | |
| 7490 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { |
| 7491 | opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
| 7492 | opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
| 7493 | p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; |
| 7494 | } |
| 7495 | |
| 7496 | if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { |
| 7497 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
| 7498 | p_j2k->m_specific_param.m_decoder.m_header_data = 00; |
| 7499 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
| 7500 | } |
| 7501 | } |
| 7502 | else { |
| 7503 | |
| 7504 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
| 7505 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
| 7506 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; |
| 7507 | } |
| 7508 | |
| 7509 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
| 7510 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
| 7511 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; |
| 7512 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; |
| 7513 | } |
| 7514 | |
| 7515 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
| 7516 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 7517 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; |
| 7518 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 7519 | } |
| 7520 | } |
| 7521 | |
| 7522 | opj_tcd_destroy(p_j2k->m_tcd); |
| 7523 | |
| 7524 | opj_j2k_cp_destroy(&(p_j2k->m_cp)); |
| 7525 | memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t)); |
| 7526 | |
| 7527 | opj_procedure_list_destroy(p_j2k->m_procedure_list); |
| 7528 | p_j2k->m_procedure_list = 00; |
| 7529 | |
| 7530 | opj_procedure_list_destroy(p_j2k->m_validation_list); |
| 7531 | p_j2k->m_procedure_list = 00; |
| 7532 | |
| 7533 | j2k_destroy_cstr_index(p_j2k->cstr_index); |
| 7534 | p_j2k->cstr_index = NULL; |
| 7535 | |
| 7536 | opj_image_destroy(p_j2k->m_private_image); |
| 7537 | p_j2k->m_private_image = NULL; |
| 7538 | |
| 7539 | opj_image_destroy(p_j2k->m_output_image); |
| 7540 | p_j2k->m_output_image = NULL; |
| 7541 | |
| 7542 | opj_free(p_j2k); |
| 7543 | } |
| 7544 | |
| 7545 | void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind) |
| 7546 | { |
| 7547 | if (p_cstr_ind) { |
| 7548 | |
| 7549 | if (p_cstr_ind->marker) { |
| 7550 | opj_free(p_cstr_ind->marker); |
| 7551 | p_cstr_ind->marker = NULL; |
| 7552 | } |
| 7553 | |
| 7554 | if (p_cstr_ind->tile_index) { |
| 7555 | OPJ_UINT32 it_tile = 0; |
| 7556 | |
| 7557 | for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { |
| 7558 | |
| 7559 | if(p_cstr_ind->tile_index[it_tile].packet_index) { |
| 7560 | opj_free(p_cstr_ind->tile_index[it_tile].packet_index); |
| 7561 | p_cstr_ind->tile_index[it_tile].packet_index = NULL; |
| 7562 | } |
| 7563 | |
| 7564 | if(p_cstr_ind->tile_index[it_tile].tp_index){ |
| 7565 | opj_free(p_cstr_ind->tile_index[it_tile].tp_index); |
| 7566 | p_cstr_ind->tile_index[it_tile].tp_index = NULL; |
| 7567 | } |
| 7568 | |
| 7569 | if(p_cstr_ind->tile_index[it_tile].marker){ |
| 7570 | opj_free(p_cstr_ind->tile_index[it_tile].marker); |
| 7571 | p_cstr_ind->tile_index[it_tile].marker = NULL; |
| 7572 | |
| 7573 | } |
| 7574 | } |
| 7575 | |
| 7576 | opj_free( p_cstr_ind->tile_index); |
| 7577 | p_cstr_ind->tile_index = NULL; |
| 7578 | } |
| 7579 | |
| 7580 | opj_free(p_cstr_ind); |
| 7581 | } |
| 7582 | } |
| 7583 | |
| 7584 | void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp) |
| 7585 | { |
| 7586 | if (p_tcp == 00) { |
| 7587 | return; |
| 7588 | } |
| 7589 | |
| 7590 | if (p_tcp->ppt_buffer != 00) { |
| 7591 | opj_free(p_tcp->ppt_buffer); |
| 7592 | p_tcp->ppt_buffer = 00; |
| 7593 | } |
| 7594 | |
| 7595 | if (p_tcp->tccps != 00) { |
| 7596 | opj_free(p_tcp->tccps); |
| 7597 | p_tcp->tccps = 00; |
| 7598 | } |
| 7599 | |
| 7600 | if (p_tcp->m_mct_coding_matrix != 00) { |
| 7601 | opj_free(p_tcp->m_mct_coding_matrix); |
| 7602 | p_tcp->m_mct_coding_matrix = 00; |
| 7603 | } |
| 7604 | |
| 7605 | if (p_tcp->m_mct_decoding_matrix != 00) { |
| 7606 | opj_free(p_tcp->m_mct_decoding_matrix); |
| 7607 | p_tcp->m_mct_decoding_matrix = 00; |
| 7608 | } |
| 7609 | |
| 7610 | if (p_tcp->m_mcc_records) { |
| 7611 | opj_free(p_tcp->m_mcc_records); |
| 7612 | p_tcp->m_mcc_records = 00; |
| 7613 | p_tcp->m_nb_max_mcc_records = 0; |
| 7614 | p_tcp->m_nb_mcc_records = 0; |
| 7615 | } |
| 7616 | |
| 7617 | if (p_tcp->m_mct_records) { |
| 7618 | opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; |
| 7619 | OPJ_UINT32 i; |
| 7620 | |
| 7621 | for (i=0;i<p_tcp->m_nb_mct_records;++i) { |
| 7622 | if (l_mct_data->m_data) { |
| 7623 | opj_free(l_mct_data->m_data); |
| 7624 | l_mct_data->m_data = 00; |
| 7625 | } |
| 7626 | |
| 7627 | ++l_mct_data; |
| 7628 | } |
| 7629 | |
| 7630 | opj_free(p_tcp->m_mct_records); |
| 7631 | p_tcp->m_mct_records = 00; |
| 7632 | } |
| 7633 | |
| 7634 | if (p_tcp->mct_norms != 00) { |
| 7635 | opj_free(p_tcp->mct_norms); |
| 7636 | p_tcp->mct_norms = 00; |
| 7637 | } |
| 7638 | |
| 7639 | opj_j2k_tcp_data_destroy(p_tcp); |
| 7640 | |
| 7641 | } |
| 7642 | |
| 7643 | void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp) |
| 7644 | { |
| 7645 | if (p_tcp->m_data) { |
| 7646 | opj_free(p_tcp->m_data); |
| 7647 | p_tcp->m_data = NULL; |
| 7648 | p_tcp->m_data_size = 0; |
| 7649 | } |
| 7650 | } |
| 7651 | |
| 7652 | void opj_j2k_cp_destroy (opj_cp_t *p_cp) |
| 7653 | { |
| 7654 | OPJ_UINT32 l_nb_tiles; |
| 7655 | opj_tcp_t * l_current_tile = 00; |
| 7656 | OPJ_UINT32 i; |
| 7657 | |
| 7658 | if (p_cp == 00) |
| 7659 | { |
| 7660 | return; |
| 7661 | } |
| 7662 | if (p_cp->tcps != 00) |
| 7663 | { |
| 7664 | l_current_tile = p_cp->tcps; |
| 7665 | l_nb_tiles = p_cp->th * p_cp->tw; |
| 7666 | |
| 7667 | for (i = 0; i < l_nb_tiles; ++i) |
| 7668 | { |
| 7669 | opj_j2k_tcp_destroy(l_current_tile); |
| 7670 | ++l_current_tile; |
| 7671 | } |
| 7672 | opj_free(p_cp->tcps); |
| 7673 | p_cp->tcps = 00; |
| 7674 | } |
| 7675 | opj_free(p_cp->ppm_buffer); |
| 7676 | p_cp->ppm_buffer = 00; |
| 7677 | p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ |
| 7678 | opj_free(p_cp->comment); |
| 7679 | p_cp->comment = 00; |
| 7680 | if (! p_cp->m_is_decoder) |
| 7681 | { |
| 7682 | opj_free(p_cp->m_specific_param.m_enc.m_matrice); |
| 7683 | p_cp->m_specific_param.m_enc.m_matrice = 00; |
| 7684 | } |
| 7685 | } |
| 7686 | |
| 7687 | OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, |
| 7688 | OPJ_UINT32 * p_tile_index, |
| 7689 | OPJ_UINT32 * p_data_size, |
| 7690 | OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, |
| 7691 | OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, |
| 7692 | OPJ_UINT32 * p_nb_comps, |
| 7693 | OPJ_BOOL * p_go_on, |
| 7694 | opj_stream_private_t *p_stream, |
| 7695 | opj_event_mgr_t * p_manager ) |
| 7696 | { |
| 7697 | OPJ_UINT32 l_current_marker = J2K_MS_SOT; |
| 7698 | OPJ_UINT32 l_marker_size; |
| 7699 | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
| 7700 | opj_tcp_t * l_tcp = NULL; |
| 7701 | OPJ_UINT32 l_nb_tiles; |
| 7702 | |
| 7703 | /* preconditions */ |
| 7704 | assert(p_stream != 00); |
| 7705 | assert(p_j2k != 00); |
| 7706 | assert(p_manager != 00); |
| 7707 | |
| 7708 | /* Reach the End Of Codestream ?*/ |
| 7709 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){ |
| 7710 | l_current_marker = J2K_MS_EOC; |
| 7711 | } |
| 7712 | /* We need to encounter a SOT marker (a new tile-part header) */ |
| 7713 | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){ |
| 7714 | return OPJ_FALSE; |
| 7715 | } |
| 7716 | |
| 7717 | /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ |
| 7718 | while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) { |
| 7719 | |
| 7720 | /* Try to read until the Start Of Data is detected */ |
| 7721 | while (l_current_marker != J2K_MS_SOD) { |
| 7722 | |
| 7723 | if(opj_stream_get_number_byte_left(p_stream) == 0) |
| 7724 | { |
| 7725 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 7726 | break; |
| 7727 | } |
| 7728 | |
| 7729 | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
| 7730 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7731 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7732 | return OPJ_FALSE; |
| 7733 | } |
| 7734 | |
| 7735 | /* Read 2 bytes from the buffer as the marker size */ |
| 7736 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); |
| 7737 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7738 | /* Check marker size (does not include marker ID but includes marker size) */ |
| 7739 | if (l_marker_size < 2) { |
| 7740 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); |
| 7741 | return OPJ_FALSE; |
| 7742 | } |
| 7743 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7744 | /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
| 7745 | if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) { |
| 7746 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 7747 | break; |
| 7748 | } |
| 7749 | |
| 7750 | /* Why this condition? FIXME */ |
| 7751 | if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){ |
| 7752 | p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); |
| 7753 | } |
| 7754 | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
| 7755 | |
| 7756 | /* Get the marker handler from the marker ID */ |
| 7757 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
| 7758 | |
| 7759 | /* Check if the marker is known and if it is the right place to find it */ |
| 7760 | if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { |
| 7761 | opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); |
| 7762 | return OPJ_FALSE; |
| 7763 | } |
| 7764 | /* FIXME manage case of unknown marker as in the main header ? */ |
| 7765 | |
| 7766 | /* Check if the marker size is compatible with the header data size */ |
| 7767 | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7768 | OPJ_BYTE *new_header_data = NULL; |
| 7769 | /* If we are here, this means we consider this marker as known & we will read it */ |
| 7770 | /* Check enough bytes left in stream before allocation */ |
| 7771 | if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { |
| 7772 | opj_event_msg(p_manager, EVT_ERROR, "Marker size inconsistent with stream length\n"); |
| 7773 | return OPJ_FALSE; |
| 7774 | } |
| 7775 | new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7776 | if (! new_header_data) { |
| 7777 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
| 7778 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
| 7779 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
| 7780 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); |
| 7781 | return OPJ_FALSE; |
| 7782 | } |
| 7783 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
| 7784 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
| 7785 | } |
| 7786 | |
| 7787 | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
| 7788 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { |
| 7789 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7790 | return OPJ_FALSE; |
| 7791 | } |
| 7792 | |
| 7793 | if (!l_marker_handler->handler) { |
| 7794 | /* See issue #175 */ |
| 7795 | opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); |
| 7796 | return OPJ_FALSE; |
| 7797 | } |
| 7798 | /* Read the marker segment with the correct marker handler */ |
| 7799 | if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { |
| 7800 | opj_event_msg(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker); |
| 7801 | return OPJ_FALSE; |
| 7802 | } |
| 7803 | |
| 7804 | /* Add the marker to the codestream index*/ |
| 7805 | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
| 7806 | p_j2k->cstr_index, |
| 7807 | l_marker_handler->id, |
| 7808 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
| 7809 | l_marker_size + 4 )) { |
| 7810 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); |
| 7811 | return OPJ_FALSE; |
| 7812 | } |
| 7813 | |
| 7814 | /* Keep the position of the last SOT marker read */ |
| 7815 | if ( l_marker_handler->id == J2K_MS_SOT ) { |
| 7816 | OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ; |
| 7817 | if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) |
| 7818 | { |
| 7819 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; |
| 7820 | } |
| 7821 | } |
| 7822 | |
| 7823 | if (p_j2k->m_specific_param.m_decoder.m_skip_data) { |
| 7824 | /* Skip the rest of the tile part header*/ |
| 7825 | if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
| 7826 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7827 | return OPJ_FALSE; |
| 7828 | } |
| 7829 | l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ |
| 7830 | } |
| 7831 | else { |
| 7832 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
| 7833 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7834 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7835 | return OPJ_FALSE; |
| 7836 | } |
| 7837 | /* Read 2 bytes from the buffer as the new marker ID */ |
| 7838 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); |
| 7839 | } |
| 7840 | } |
| 7841 | if(opj_stream_get_number_byte_left(p_stream) == 0 |
| 7842 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) |
| 7843 | break; |
| 7844 | |
| 7845 | /* If we didn't skip data before, we need to read the SOD marker*/ |
| 7846 | if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { |
| 7847 | /* Try to read the SOD marker and skip data ? FIXME */ |
| 7848 | if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { |
| 7849 | return OPJ_FALSE; |
| 7850 | } |
| 7851 | |
| 7852 | if (! p_j2k->m_specific_param.m_decoder.m_can_decode){ |
| 7853 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7854 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7855 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7856 | return OPJ_FALSE; |
| 7857 | } |
| 7858 | |
| 7859 | /* Read 2 bytes from buffer as the new marker ID */ |
| 7860 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7861 | } |
| 7862 | } |
| 7863 | else { |
| 7864 | /* Indicate we will try to read a new tile-part header*/ |
| 7865 | p_j2k->m_specific_param.m_decoder.m_skip_data = 0; |
| 7866 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
| 7867 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
| 7868 | |
| 7869 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
| 7870 | if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { |
| 7871 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7872 | return OPJ_FALSE; |
| 7873 | } |
| 7874 | |
| 7875 | /* Read 2 bytes from buffer as the new marker ID */ |
| 7876 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); |
| 7877 | } |
| 7878 | } |
| 7879 | |
| 7880 | /* Current marker is the EOC marker ?*/ |
| 7881 | if (l_current_marker == J2K_MS_EOC) { |
| 7882 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){ |
| 7883 | p_j2k->m_current_tile_number = 0; |
| 7884 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
| 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | /* FIXME DOC ???*/ |
| 7889 | if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
| 7890 | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
| 7891 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 7892 | |
| 7893 | while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) { |
| 7894 | ++p_j2k->m_current_tile_number; |
| 7895 | ++l_tcp; |
| 7896 | } |
| 7897 | |
| 7898 | if (p_j2k->m_current_tile_number == l_nb_tiles) { |
| 7899 | *p_go_on = OPJ_FALSE; |
| 7900 | return OPJ_TRUE; |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | /*FIXME ???*/ |
| 7905 | if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { |
| 7906 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); |
| 7907 | return OPJ_FALSE; |
| 7908 | } |
| 7909 | |
| 7910 | opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7911 | p_j2k->m_current_tile_number+1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7912 | |
| 7913 | *p_tile_index = p_j2k->m_current_tile_number; |
| 7914 | *p_go_on = OPJ_TRUE; |
| 7915 | *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd); |
| 7916 | *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; |
| 7917 | *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; |
| 7918 | *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; |
| 7919 | *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; |
| 7920 | *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; |
| 7921 | |
| 7922 | p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/ |
| 7923 | |
| 7924 | return OPJ_TRUE; |
| 7925 | } |
| 7926 | |
| 7927 | OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, |
| 7928 | OPJ_UINT32 p_tile_index, |
| 7929 | OPJ_BYTE * p_data, |
| 7930 | OPJ_UINT32 p_data_size, |
| 7931 | opj_stream_private_t *p_stream, |
| 7932 | opj_event_mgr_t * p_manager ) |
| 7933 | { |
| 7934 | OPJ_UINT32 l_current_marker; |
| 7935 | OPJ_BYTE l_data [2]; |
| 7936 | opj_tcp_t * l_tcp; |
| 7937 | |
| 7938 | /* preconditions */ |
| 7939 | assert(p_stream != 00); |
| 7940 | assert(p_j2k != 00); |
| 7941 | assert(p_manager != 00); |
| 7942 | |
| 7943 | if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) |
| 7944 | || (p_tile_index != p_j2k->m_current_tile_number) ) { |
| 7945 | return OPJ_FALSE; |
| 7946 | } |
| 7947 | |
| 7948 | l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); |
| 7949 | if (! l_tcp->m_data) { |
| 7950 | opj_j2k_tcp_destroy(l_tcp); |
| 7951 | return OPJ_FALSE; |
| 7952 | } |
| 7953 | |
| 7954 | if (! opj_tcd_decode_tile( p_j2k->m_tcd, |
| 7955 | l_tcp->m_data, |
| 7956 | l_tcp->m_data_size, |
| 7957 | p_tile_index, |
| 7958 | p_j2k->cstr_index) ) { |
| 7959 | opj_j2k_tcp_destroy(l_tcp); |
| 7960 | p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/ |
| 7961 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); |
| 7962 | return OPJ_FALSE; |
| 7963 | } |
| 7964 | |
| 7965 | if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { |
| 7966 | return OPJ_FALSE; |
| 7967 | } |
| 7968 | |
| 7969 | /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) |
| 7970 | * we destroy just the data which will be re-read in read_tile_header*/ |
| 7971 | /*opj_j2k_tcp_destroy(l_tcp); |
| 7972 | p_j2k->m_tcd->tcp = 0;*/ |
| 7973 | opj_j2k_tcp_data_destroy(l_tcp); |
| 7974 | |
| 7975 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
| 7976 | p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/ |
| 7977 | |
| 7978 | if(opj_stream_get_number_byte_left(p_stream) == 0 |
| 7979 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){ |
| 7980 | return OPJ_TRUE; |
| 7981 | } |
| 7982 | |
| 7983 | if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/ |
| 7984 | if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { |
| 7985 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); |
| 7986 | return OPJ_FALSE; |
| 7987 | } |
| 7988 | |
| 7989 | opj_read_bytes(l_data,&l_current_marker,2); |
| 7990 | |
| 7991 | if (l_current_marker == J2K_MS_EOC) { |
| 7992 | p_j2k->m_current_tile_number = 0; |
| 7993 | p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/ |
| 7994 | } |
| 7995 | else if (l_current_marker != J2K_MS_SOT) |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7996 | { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 7997 | if(opj_stream_get_number_byte_left(p_stream) == 0) { |
| 7998 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 7999 | opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8000 | return OPJ_TRUE; |
| 8001 | } |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8002 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8003 | return OPJ_FALSE; |
| 8004 | } |
| 8005 | } |
| 8006 | |
| 8007 | return OPJ_TRUE; |
| 8008 | } |
| 8009 | |
| 8010 | OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image) |
| 8011 | { |
| 8012 | OPJ_UINT32 i,j,k = 0; |
| 8013 | OPJ_UINT32 l_width_src,l_height_src; |
| 8014 | OPJ_UINT32 l_width_dest,l_height_dest; |
| 8015 | OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; |
| 8016 | OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ; |
| 8017 | OPJ_UINT32 l_start_x_dest , l_start_y_dest; |
| 8018 | OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; |
| 8019 | OPJ_INT32 l_start_offset_dest, l_line_offset_dest; |
| 8020 | |
| 8021 | opj_image_comp_t * l_img_comp_src = 00; |
| 8022 | opj_image_comp_t * l_img_comp_dest = 00; |
| 8023 | |
| 8024 | opj_tcd_tilecomp_t * l_tilec = 00; |
| 8025 | opj_image_t * l_image_src = 00; |
| 8026 | OPJ_UINT32 l_size_comp, l_remaining; |
| 8027 | OPJ_INT32 * l_dest_ptr; |
| 8028 | opj_tcd_resolution_t* l_res= 00; |
| 8029 | |
| 8030 | l_tilec = p_tcd->tcd_image->tiles->comps; |
| 8031 | l_image_src = p_tcd->image; |
| 8032 | l_img_comp_src = l_image_src->comps; |
| 8033 | |
| 8034 | l_img_comp_dest = p_output_image->comps; |
| 8035 | |
| 8036 | for (i=0; i<l_image_src->numcomps; i++) { |
| 8037 | |
| 8038 | /* Allocate output component buffer if necessary */ |
| 8039 | if (!l_img_comp_dest->data) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8040 | OPJ_UINT32 width = l_img_comp_dest->w; |
| 8041 | OPJ_UINT32 height = l_img_comp_dest->h; |
| 8042 | const OPJ_UINT32 MAX_SIZE = UINT32_MAX / sizeof(OPJ_INT32); |
| 8043 | if (height == 0 || width > MAX_SIZE / height) { |
| 8044 | return OPJ_FALSE; |
| 8045 | } |
| 8046 | l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(width * height, sizeof(OPJ_INT32)); |
| 8047 | if (!l_img_comp_dest->data) { |
| 8048 | return OPJ_FALSE; |
| 8049 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | /* Copy info from decoded comp image to output image */ |
| 8053 | l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; |
| 8054 | |
| 8055 | /*-----*/ |
| 8056 | /* Compute the precision of the output buffer */ |
| 8057 | l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/ |
| 8058 | l_remaining = l_img_comp_src->prec & 7; /* (%8) */ |
| 8059 | l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded; |
| 8060 | |
| 8061 | if (l_remaining) { |
| 8062 | ++l_size_comp; |
| 8063 | } |
| 8064 | |
| 8065 | if (l_size_comp == 3) { |
| 8066 | l_size_comp = 4; |
| 8067 | } |
| 8068 | /*-----*/ |
| 8069 | |
| 8070 | /* Current tile component size*/ |
| 8071 | /*if (i == 0) { |
| 8072 | fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", |
| 8073 | l_res->x0, l_res->x1, l_res->y0, l_res->y1); |
| 8074 | }*/ |
| 8075 | |
| 8076 | l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0); |
| 8077 | l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0); |
| 8078 | |
| 8079 | /* Border of the current output component*/ |
| 8080 | l_x0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->x0, (OPJ_INT32)l_img_comp_dest->factor); |
| 8081 | l_y0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->y0, (OPJ_INT32)l_img_comp_dest->factor); |
| 8082 | l_x1_dest = l_x0_dest + l_img_comp_dest->w; |
| 8083 | l_y1_dest = l_y0_dest + l_img_comp_dest->h; |
| 8084 | |
| 8085 | /*if (i == 0) { |
| 8086 | fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", |
| 8087 | l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); |
| 8088 | }*/ |
| 8089 | |
| 8090 | /*-----*/ |
| 8091 | /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) |
| 8092 | * of the input buffer (decoded tile component) which will be move |
| 8093 | * in the output buffer. Compute the area of the output buffer (l_start_x_dest, |
| 8094 | * l_start_y_dest, l_width_dest, l_height_dest) which will be modified |
| 8095 | * by this input area. |
| 8096 | * */ |
| 8097 | assert( l_res->x0 >= 0); |
| 8098 | assert( l_res->x1 >= 0); |
| 8099 | if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) { |
| 8100 | l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest; |
| 8101 | l_offset_x0_src = 0; |
| 8102 | |
| 8103 | if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { |
| 8104 | l_width_dest = l_width_src; |
| 8105 | l_offset_x1_src = 0; |
| 8106 | } |
| 8107 | else { |
| 8108 | l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ; |
| 8109 | l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); |
| 8110 | } |
| 8111 | } |
| 8112 | else { |
| 8113 | l_start_x_dest = 0 ; |
| 8114 | l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0; |
| 8115 | |
| 8116 | if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { |
| 8117 | l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; |
| 8118 | l_offset_x1_src = 0; |
| 8119 | } |
| 8120 | else { |
| 8121 | l_width_dest = l_img_comp_dest->w ; |
| 8122 | l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest; |
| 8123 | } |
| 8124 | } |
| 8125 | |
| 8126 | if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) { |
| 8127 | l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest; |
| 8128 | l_offset_y0_src = 0; |
| 8129 | |
| 8130 | if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { |
| 8131 | l_height_dest = l_height_src; |
| 8132 | l_offset_y1_src = 0; |
| 8133 | } |
| 8134 | else { |
| 8135 | l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ; |
| 8136 | l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); |
| 8137 | } |
| 8138 | } |
| 8139 | else { |
| 8140 | l_start_y_dest = 0 ; |
| 8141 | l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0; |
| 8142 | |
| 8143 | if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { |
| 8144 | l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; |
| 8145 | l_offset_y1_src = 0; |
| 8146 | } |
| 8147 | else { |
| 8148 | l_height_dest = l_img_comp_dest->h ; |
| 8149 | l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest; |
| 8150 | } |
| 8151 | } |
| 8152 | |
| 8153 | if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){ |
| 8154 | return OPJ_FALSE; |
| 8155 | } |
| 8156 | /* testcase 2977.pdf.asan.67.2198 */ |
| 8157 | if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { |
| 8158 | return OPJ_FALSE; |
| 8159 | } |
| 8160 | /*-----*/ |
| 8161 | |
| 8162 | /* Compute the input buffer offset */ |
| 8163 | l_start_offset_src = l_offset_x0_src + l_offset_y0_src * (OPJ_INT32)l_width_src; |
| 8164 | l_line_offset_src = l_offset_x1_src + l_offset_x0_src; |
| 8165 | l_end_offset_src = l_offset_y1_src * (OPJ_INT32)l_width_src - l_offset_x0_src; |
| 8166 | |
| 8167 | /* Compute the output buffer offset */ |
| 8168 | l_start_offset_dest = (OPJ_INT32)(l_start_x_dest + l_start_y_dest * l_img_comp_dest->w); |
| 8169 | l_line_offset_dest = (OPJ_INT32)(l_img_comp_dest->w - l_width_dest); |
| 8170 | |
| 8171 | /* Move the output buffer to the first place where we will write*/ |
| 8172 | l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; |
| 8173 | |
| 8174 | /*if (i == 0) { |
| 8175 | fprintf(stdout, "COMPO[%d]:\n",i); |
| 8176 | fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n" |
| 8177 | "\t tile offset:%d, %d, %d, %d\n" |
| 8178 | "\t buffer offset: %d; %d, %d\n", |
| 8179 | l_res->x0, l_res->y0, l_width_src, l_height_src, |
| 8180 | l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src, |
| 8181 | l_start_offset_src, l_line_offset_src, l_end_offset_src); |
| 8182 | |
| 8183 | fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n" |
| 8184 | "\t start offset: %d, line offset= %d\n", |
| 8185 | l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest); |
| 8186 | }*/ |
| 8187 | |
| 8188 | switch (l_size_comp) { |
| 8189 | case 1: |
| 8190 | { |
| 8191 | OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data; |
| 8192 | l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/ |
| 8193 | |
| 8194 | if (l_img_comp_src->sgnd) { |
| 8195 | for (j = 0 ; j < l_height_dest ; ++j) { |
| 8196 | for ( k = 0 ; k < l_width_dest ; ++k) { |
| 8197 | *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */ |
| 8198 | } |
| 8199 | |
| 8200 | l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */ |
| 8201 | l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */ |
| 8202 | } |
| 8203 | } |
| 8204 | else { |
| 8205 | for ( j = 0 ; j < l_height_dest ; ++j ) { |
| 8206 | for ( k = 0 ; k < l_width_dest ; ++k) { |
| 8207 | *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff); |
| 8208 | } |
| 8209 | |
| 8210 | l_dest_ptr+= l_line_offset_dest; |
| 8211 | l_src_ptr += l_line_offset_src; |
| 8212 | } |
| 8213 | } |
| 8214 | |
| 8215 | l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */ |
| 8216 | p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */ |
| 8217 | } |
| 8218 | break; |
| 8219 | case 2: |
| 8220 | { |
| 8221 | OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data; |
| 8222 | l_src_ptr += l_start_offset_src; |
| 8223 | |
| 8224 | if (l_img_comp_src->sgnd) { |
| 8225 | for (j=0;j<l_height_dest;++j) { |
| 8226 | for (k=0;k<l_width_dest;++k) { |
| 8227 | *(l_dest_ptr++) = *(l_src_ptr++); |
| 8228 | } |
| 8229 | |
| 8230 | l_dest_ptr+= l_line_offset_dest; |
| 8231 | l_src_ptr += l_line_offset_src ; |
| 8232 | } |
| 8233 | } |
| 8234 | else { |
| 8235 | for (j=0;j<l_height_dest;++j) { |
| 8236 | for (k=0;k<l_width_dest;++k) { |
| 8237 | *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff; |
| 8238 | } |
| 8239 | |
| 8240 | l_dest_ptr+= l_line_offset_dest; |
| 8241 | l_src_ptr += l_line_offset_src ; |
| 8242 | } |
| 8243 | } |
| 8244 | |
| 8245 | l_src_ptr += l_end_offset_src; |
| 8246 | p_data = (OPJ_BYTE*) l_src_ptr; |
| 8247 | } |
| 8248 | break; |
| 8249 | case 4: |
| 8250 | { |
| 8251 | OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data; |
| 8252 | l_src_ptr += l_start_offset_src; |
| 8253 | |
| 8254 | for (j=0;j<l_height_dest;++j) { |
| 8255 | for (k=0;k<l_width_dest;++k) { |
| 8256 | *(l_dest_ptr++) = (*(l_src_ptr++)); |
| 8257 | } |
| 8258 | |
| 8259 | l_dest_ptr+= l_line_offset_dest; |
| 8260 | l_src_ptr += l_line_offset_src ; |
| 8261 | } |
| 8262 | |
| 8263 | l_src_ptr += l_end_offset_src; |
| 8264 | p_data = (OPJ_BYTE*) l_src_ptr; |
| 8265 | } |
| 8266 | break; |
| 8267 | } |
| 8268 | |
| 8269 | ++l_img_comp_dest; |
| 8270 | ++l_img_comp_src; |
| 8271 | ++l_tilec; |
| 8272 | } |
| 8273 | |
| 8274 | return OPJ_TRUE; |
| 8275 | } |
| 8276 | |
| 8277 | OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, |
| 8278 | opj_image_t* p_image, |
| 8279 | OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, |
| 8280 | OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, |
| 8281 | opj_event_mgr_t * p_manager ) |
| 8282 | { |
| 8283 | opj_cp_t * l_cp = &(p_j2k->m_cp); |
| 8284 | opj_image_t * l_image = p_j2k->m_private_image; |
| 8285 | |
| 8286 | OPJ_UINT32 it_comp; |
| 8287 | OPJ_INT32 l_comp_x1, l_comp_y1; |
| 8288 | opj_image_comp_t* l_img_comp = NULL; |
| 8289 | |
| 8290 | /* Check if we are read the main header */ |
| 8291 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/ |
| 8292 | opj_event_msg(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream"); |
| 8293 | return OPJ_FALSE; |
| 8294 | } |
| 8295 | |
| 8296 | if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){ |
| 8297 | opj_event_msg(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n"); |
| 8298 | |
| 8299 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
| 8300 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
| 8301 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
| 8302 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
| 8303 | |
| 8304 | return OPJ_TRUE; |
| 8305 | } |
| 8306 | |
| 8307 | /* ----- */ |
| 8308 | /* Check if the positions provided by the user are correct */ |
| 8309 | |
| 8310 | /* Left */ |
| 8311 | assert(p_start_x >= 0 ); |
| 8312 | assert(p_start_y >= 0 ); |
| 8313 | |
| 8314 | if ((OPJ_UINT32)p_start_x > l_image->x1 ) { |
| 8315 | opj_event_msg(p_manager, EVT_ERROR, |
| 8316 | "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", |
| 8317 | p_start_x, l_image->x1); |
| 8318 | return OPJ_FALSE; |
| 8319 | } |
| 8320 | else if ((OPJ_UINT32)p_start_x < l_image->x0){ |
| 8321 | opj_event_msg(p_manager, EVT_WARNING, |
| 8322 | "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", |
| 8323 | p_start_x, l_image->x0); |
| 8324 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
| 8325 | p_image->x0 = l_image->x0; |
| 8326 | } |
| 8327 | else { |
| 8328 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx; |
| 8329 | p_image->x0 = (OPJ_UINT32)p_start_x; |
| 8330 | } |
| 8331 | |
| 8332 | /* Up */ |
| 8333 | if ((OPJ_UINT32)p_start_y > l_image->y1){ |
| 8334 | opj_event_msg(p_manager, EVT_ERROR, |
| 8335 | "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", |
| 8336 | p_start_y, l_image->y1); |
| 8337 | return OPJ_FALSE; |
| 8338 | } |
| 8339 | else if ((OPJ_UINT32)p_start_y < l_image->y0){ |
| 8340 | opj_event_msg(p_manager, EVT_WARNING, |
| 8341 | "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", |
| 8342 | p_start_y, l_image->y0); |
| 8343 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
| 8344 | p_image->y0 = l_image->y0; |
| 8345 | } |
| 8346 | else { |
| 8347 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy; |
| 8348 | p_image->y0 = (OPJ_UINT32)p_start_y; |
| 8349 | } |
| 8350 | |
| 8351 | /* Right */ |
| 8352 | assert((OPJ_UINT32)p_end_x > 0); |
| 8353 | assert((OPJ_UINT32)p_end_y > 0); |
| 8354 | if ((OPJ_UINT32)p_end_x < l_image->x0) { |
| 8355 | opj_event_msg(p_manager, EVT_ERROR, |
| 8356 | "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", |
| 8357 | p_end_x, l_image->x0); |
| 8358 | return OPJ_FALSE; |
| 8359 | } |
| 8360 | else if ((OPJ_UINT32)p_end_x > l_image->x1) { |
| 8361 | opj_event_msg(p_manager, EVT_WARNING, |
| 8362 | "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", |
| 8363 | p_end_x, l_image->x1); |
| 8364 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
| 8365 | p_image->x1 = l_image->x1; |
| 8366 | } |
| 8367 | else { |
| 8368 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx); |
| 8369 | p_image->x1 = (OPJ_UINT32)p_end_x; |
| 8370 | } |
| 8371 | |
| 8372 | /* Bottom */ |
| 8373 | if ((OPJ_UINT32)p_end_y < l_image->y0) { |
| 8374 | opj_event_msg(p_manager, EVT_ERROR, |
| 8375 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", |
| 8376 | p_end_y, l_image->y0); |
| 8377 | return OPJ_FALSE; |
| 8378 | } |
| 8379 | if ((OPJ_UINT32)p_end_y > l_image->y1){ |
| 8380 | opj_event_msg(p_manager, EVT_WARNING, |
| 8381 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", |
| 8382 | p_end_y, l_image->y1); |
| 8383 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
| 8384 | p_image->y1 = l_image->y1; |
| 8385 | } |
| 8386 | else{ |
| 8387 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy); |
| 8388 | p_image->y1 = (OPJ_UINT32)p_end_y; |
| 8389 | } |
| 8390 | /* ----- */ |
| 8391 | |
| 8392 | p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; |
| 8393 | |
| 8394 | l_img_comp = p_image->comps; |
| 8395 | for (it_comp=0; it_comp < p_image->numcomps; ++it_comp) |
| 8396 | { |
| 8397 | OPJ_INT32 l_h,l_w; |
| 8398 | |
| 8399 | l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx); |
| 8400 | l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy); |
| 8401 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
| 8402 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
| 8403 | |
| 8404 | l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) |
| 8405 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); |
| 8406 | if (l_w < 0){ |
| 8407 | opj_event_msg(p_manager, EVT_ERROR, |
| 8408 | "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", |
| 8409 | it_comp, l_w); |
| 8410 | return OPJ_FALSE; |
| 8411 | } |
| 8412 | l_img_comp->w = (OPJ_UINT32)l_w; |
| 8413 | |
| 8414 | l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) |
| 8415 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); |
| 8416 | if (l_h < 0){ |
| 8417 | opj_event_msg(p_manager, EVT_ERROR, |
| 8418 | "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", |
| 8419 | it_comp, l_h); |
| 8420 | return OPJ_FALSE; |
| 8421 | } |
| 8422 | l_img_comp->h = (OPJ_UINT32)l_h; |
| 8423 | |
| 8424 | l_img_comp++; |
| 8425 | } |
| 8426 | |
| 8427 | opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n", |
| 8428 | p_image->x0, p_image->y0, p_image->x1, p_image->y1); |
| 8429 | |
| 8430 | return OPJ_TRUE; |
| 8431 | } |
| 8432 | |
| 8433 | opj_j2k_t* opj_j2k_create_decompress(void) |
| 8434 | { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8435 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8436 | if (!l_j2k) { |
| 8437 | return 00; |
| 8438 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8439 | |
| 8440 | l_j2k->m_is_decoder = 1; |
| 8441 | l_j2k->m_cp.m_is_decoder = 1; |
| 8442 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8443 | l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,sizeof(opj_tcp_t)); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8444 | if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { |
| 8445 | opj_j2k_destroy(l_j2k); |
| 8446 | return 00; |
| 8447 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8448 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8449 | l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,OPJ_J2K_DEFAULT_HEADER_SIZE); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8450 | if (! l_j2k->m_specific_param.m_decoder.m_header_data) { |
| 8451 | opj_j2k_destroy(l_j2k); |
| 8452 | return 00; |
| 8453 | } |
| 8454 | |
| 8455 | l_j2k->m_specific_param.m_decoder.m_header_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE; |
| 8456 | |
| 8457 | l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; |
| 8458 | |
| 8459 | l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; |
| 8460 | |
| 8461 | /* codestream index creation */ |
| 8462 | l_j2k->cstr_index = opj_j2k_create_cstr_index(); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8463 | if (!l_j2k->cstr_index){ |
| 8464 | opj_j2k_destroy(l_j2k); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8465 | return 00; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8466 | } |
| 8467 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8468 | /* validation list creation */ |
| 8469 | l_j2k->m_validation_list = opj_procedure_list_create(); |
| 8470 | if (! l_j2k->m_validation_list) { |
| 8471 | opj_j2k_destroy(l_j2k); |
| 8472 | return 00; |
| 8473 | } |
| 8474 | |
| 8475 | /* execution list creation */ |
| 8476 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
| 8477 | if (! l_j2k->m_procedure_list) { |
| 8478 | opj_j2k_destroy(l_j2k); |
| 8479 | return 00; |
| 8480 | } |
| 8481 | |
| 8482 | return l_j2k; |
| 8483 | } |
| 8484 | |
| 8485 | opj_codestream_index_t* opj_j2k_create_cstr_index(void) |
| 8486 | { |
| 8487 | opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) |
| 8488 | opj_calloc(1,sizeof(opj_codestream_index_t)); |
| 8489 | if (!cstr_index) |
| 8490 | return NULL; |
| 8491 | |
| 8492 | cstr_index->maxmarknum = 100; |
| 8493 | cstr_index->marknum = 0; |
| 8494 | cstr_index->marker = (opj_marker_info_t*) |
| 8495 | opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); |
| 8496 | if (!cstr_index-> marker) |
| 8497 | return NULL; |
| 8498 | |
| 8499 | cstr_index->tile_index = NULL; |
| 8500 | |
| 8501 | return cstr_index; |
| 8502 | } |
| 8503 | |
| 8504 | OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_t *p_j2k, |
| 8505 | OPJ_UINT32 p_tile_no, |
| 8506 | OPJ_UINT32 p_comp_no ) |
| 8507 | { |
| 8508 | opj_cp_t *l_cp = 00; |
| 8509 | opj_tcp_t *l_tcp = 00; |
| 8510 | opj_tccp_t *l_tccp = 00; |
| 8511 | |
| 8512 | /* preconditions */ |
| 8513 | assert(p_j2k != 00); |
| 8514 | |
| 8515 | l_cp = &(p_j2k->m_cp); |
| 8516 | l_tcp = &l_cp->tcps[p_tile_no]; |
| 8517 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 8518 | |
| 8519 | /* preconditions again */ |
| 8520 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
| 8521 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
| 8522 | |
| 8523 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
| 8524 | return 5 + l_tccp->numresolutions; |
| 8525 | } |
| 8526 | else { |
| 8527 | return 5; |
| 8528 | } |
| 8529 | } |
| 8530 | |
| 8531 | OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, |
| 8532 | OPJ_UINT32 p_tile_no, |
| 8533 | OPJ_UINT32 p_comp_no, |
| 8534 | OPJ_BYTE * p_data, |
| 8535 | OPJ_UINT32 * p_header_size, |
| 8536 | struct opj_event_mgr * p_manager ) |
| 8537 | { |
| 8538 | OPJ_UINT32 i; |
| 8539 | opj_cp_t *l_cp = 00; |
| 8540 | opj_tcp_t *l_tcp = 00; |
| 8541 | opj_tccp_t *l_tccp = 00; |
| 8542 | |
| 8543 | /* preconditions */ |
| 8544 | assert(p_j2k != 00); |
| 8545 | assert(p_header_size != 00); |
| 8546 | assert(p_manager != 00); |
| 8547 | assert(p_data != 00); |
| 8548 | |
| 8549 | l_cp = &(p_j2k->m_cp); |
| 8550 | l_tcp = &l_cp->tcps[p_tile_no]; |
| 8551 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 8552 | |
| 8553 | /* preconditions again */ |
| 8554 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
| 8555 | assert(p_comp_no <(p_j2k->m_private_image->numcomps)); |
| 8556 | |
| 8557 | if (*p_header_size < 5) { |
| 8558 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
| 8559 | return OPJ_FALSE; |
| 8560 | } |
| 8561 | |
| 8562 | opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ |
| 8563 | ++p_data; |
| 8564 | |
| 8565 | opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */ |
| 8566 | ++p_data; |
| 8567 | |
| 8568 | opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */ |
| 8569 | ++p_data; |
| 8570 | |
| 8571 | opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */ |
| 8572 | ++p_data; |
| 8573 | |
| 8574 | opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */ |
| 8575 | ++p_data; |
| 8576 | |
| 8577 | *p_header_size = *p_header_size - 5; |
| 8578 | |
| 8579 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
| 8580 | |
| 8581 | if (*p_header_size < l_tccp->numresolutions) { |
| 8582 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); |
| 8583 | return OPJ_FALSE; |
| 8584 | } |
| 8585 | |
| 8586 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
| 8587 | opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */ |
| 8588 | ++p_data; |
| 8589 | } |
| 8590 | |
| 8591 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
| 8592 | } |
| 8593 | |
| 8594 | return OPJ_TRUE; |
| 8595 | } |
| 8596 | |
| 8597 | OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, |
| 8598 | OPJ_UINT32 compno, |
| 8599 | OPJ_BYTE * p_header_data, |
| 8600 | OPJ_UINT32 * p_header_size, |
| 8601 | opj_event_mgr_t * p_manager) |
| 8602 | { |
| 8603 | OPJ_UINT32 i, l_tmp; |
| 8604 | opj_cp_t *l_cp = NULL; |
| 8605 | opj_tcp_t *l_tcp = NULL; |
| 8606 | opj_tccp_t *l_tccp = NULL; |
| 8607 | OPJ_BYTE * l_current_ptr = NULL; |
| 8608 | |
| 8609 | /* preconditions */ |
| 8610 | assert(p_j2k != 00); |
| 8611 | assert(p_manager != 00); |
| 8612 | assert(p_header_data != 00); |
| 8613 | |
| 8614 | l_cp = &(p_j2k->m_cp); |
| 8615 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
| 8616 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 8617 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 8618 | |
| 8619 | /* precondition again */ |
| 8620 | assert(compno < p_j2k->m_private_image->numcomps); |
| 8621 | |
| 8622 | l_tccp = &l_tcp->tccps[compno]; |
| 8623 | l_current_ptr = p_header_data; |
| 8624 | |
| 8625 | /* make sure room is sufficient */ |
| 8626 | if (*p_header_size < 5) { |
| 8627 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
| 8628 | return OPJ_FALSE; |
| 8629 | } |
| 8630 | |
| 8631 | opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */ |
| 8632 | ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ |
| 8633 | if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { |
| 8634 | opj_event_msg(p_manager, EVT_ERROR, |
| 8635 | "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", |
| 8636 | l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); |
| 8637 | return OPJ_FALSE; |
| 8638 | } |
| 8639 | ++l_current_ptr; |
| 8640 | |
| 8641 | /* If user wants to remove more resolutions than the codestream contains, return error */ |
| 8642 | if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { |
| 8643 | opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " |
| 8644 | "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno); |
| 8645 | p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ |
| 8646 | return OPJ_FALSE; |
| 8647 | } |
| 8648 | |
| 8649 | opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */ |
| 8650 | ++l_current_ptr; |
| 8651 | l_tccp->cblkw += 2; |
| 8652 | |
| 8653 | opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */ |
| 8654 | ++l_current_ptr; |
| 8655 | l_tccp->cblkh += 2; |
| 8656 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8657 | if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { |
| 8658 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); |
| 8659 | return OPJ_FALSE; |
| 8660 | } |
| 8661 | |
| 8662 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8663 | opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */ |
| 8664 | ++l_current_ptr; |
| 8665 | |
| 8666 | opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */ |
| 8667 | ++l_current_ptr; |
| 8668 | |
| 8669 | *p_header_size = *p_header_size - 5; |
| 8670 | |
| 8671 | /* use custom precinct size ? */ |
| 8672 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
| 8673 | if (*p_header_size < l_tccp->numresolutions) { |
| 8674 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); |
| 8675 | return OPJ_FALSE; |
| 8676 | } |
| 8677 | |
| 8678 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
| 8679 | opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */ |
| 8680 | ++l_current_ptr; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 8681 | /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ |
| 8682 | if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { |
| 8683 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); |
| 8684 | return OPJ_FALSE; |
| 8685 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 8686 | l_tccp->prcw[i] = l_tmp & 0xf; |
| 8687 | l_tccp->prch[i] = l_tmp >> 4; |
| 8688 | } |
| 8689 | |
| 8690 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
| 8691 | } |
| 8692 | else { |
| 8693 | /* set default size for the precinct width and height */ |
| 8694 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
| 8695 | l_tccp->prcw[i] = 15; |
| 8696 | l_tccp->prch[i] = 15; |
| 8697 | } |
| 8698 | } |
| 8699 | |
| 8700 | #ifdef WIP_REMOVE_MSD |
| 8701 | /* INDEX >> */ |
| 8702 | if (p_j2k->cstr_info && compno == 0) { |
| 8703 | OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); |
| 8704 | |
| 8705 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh; |
| 8706 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw; |
| 8707 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions; |
| 8708 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty; |
| 8709 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid; |
| 8710 | |
| 8711 | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size); |
| 8712 | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size); |
| 8713 | } |
| 8714 | /* << INDEX */ |
| 8715 | #endif |
| 8716 | |
| 8717 | return OPJ_TRUE; |
| 8718 | } |
| 8719 | |
| 8720 | void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k ) |
| 8721 | { |
| 8722 | /* loop */ |
| 8723 | OPJ_UINT32 i; |
| 8724 | opj_cp_t *l_cp = NULL; |
| 8725 | opj_tcp_t *l_tcp = NULL; |
| 8726 | opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; |
| 8727 | OPJ_UINT32 l_prc_size; |
| 8728 | |
| 8729 | /* preconditions */ |
| 8730 | assert(p_j2k != 00); |
| 8731 | |
| 8732 | l_cp = &(p_j2k->m_cp); |
| 8733 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/ |
| 8734 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 8735 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 8736 | |
| 8737 | l_ref_tccp = &l_tcp->tccps[0]; |
| 8738 | l_copied_tccp = l_ref_tccp + 1; |
| 8739 | l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); |
| 8740 | |
| 8741 | for (i=1; i<p_j2k->m_private_image->numcomps; ++i) { |
| 8742 | l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; |
| 8743 | l_copied_tccp->cblkw = l_ref_tccp->cblkw; |
| 8744 | l_copied_tccp->cblkh = l_ref_tccp->cblkh; |
| 8745 | l_copied_tccp->cblksty = l_ref_tccp->cblksty; |
| 8746 | l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; |
| 8747 | memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size); |
| 8748 | memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size); |
| 8749 | ++l_copied_tccp; |
| 8750 | } |
| 8751 | } |
| 8752 | |
| 8753 | OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k, |
| 8754 | OPJ_UINT32 p_tile_no, |
| 8755 | OPJ_UINT32 p_comp_no ) |
| 8756 | { |
| 8757 | OPJ_UINT32 l_num_bands; |
| 8758 | |
| 8759 | opj_cp_t *l_cp = 00; |
| 8760 | opj_tcp_t *l_tcp = 00; |
| 8761 | opj_tccp_t *l_tccp = 00; |
| 8762 | |
| 8763 | /* preconditions */ |
| 8764 | assert(p_j2k != 00); |
| 8765 | |
| 8766 | l_cp = &(p_j2k->m_cp); |
| 8767 | l_tcp = &l_cp->tcps[p_tile_no]; |
| 8768 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 8769 | |
| 8770 | /* preconditions again */ |
| 8771 | assert(p_tile_no < l_cp->tw * l_cp->th); |
| 8772 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
| 8773 | |
| 8774 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); |
| 8775 | |
| 8776 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
| 8777 | return 1 + l_num_bands; |
| 8778 | } |
| 8779 | else { |
| 8780 | return 1 + 2*l_num_bands; |
| 8781 | } |
| 8782 | } |
| 8783 | |
| 8784 | OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k, |
| 8785 | OPJ_UINT32 p_tile_no, |
| 8786 | OPJ_UINT32 p_comp_no, |
| 8787 | OPJ_BYTE * p_data, |
| 8788 | OPJ_UINT32 * p_header_size, |
| 8789 | struct opj_event_mgr * p_manager ) |
| 8790 | { |
| 8791 | OPJ_UINT32 l_header_size; |
| 8792 | OPJ_UINT32 l_band_no, l_num_bands; |
| 8793 | OPJ_UINT32 l_expn,l_mant; |
| 8794 | |
| 8795 | opj_cp_t *l_cp = 00; |
| 8796 | opj_tcp_t *l_tcp = 00; |
| 8797 | opj_tccp_t *l_tccp = 00; |
| 8798 | |
| 8799 | /* preconditions */ |
| 8800 | assert(p_j2k != 00); |
| 8801 | assert(p_header_size != 00); |
| 8802 | assert(p_manager != 00); |
| 8803 | assert(p_data != 00); |
| 8804 | |
| 8805 | l_cp = &(p_j2k->m_cp); |
| 8806 | l_tcp = &l_cp->tcps[p_tile_no]; |
| 8807 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 8808 | |
| 8809 | /* preconditions again */ |
| 8810 | assert(p_tile_no < l_cp->tw * l_cp->th); |
| 8811 | assert(p_comp_no <p_j2k->m_private_image->numcomps); |
| 8812 | |
| 8813 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); |
| 8814 | |
| 8815 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
| 8816 | l_header_size = 1 + l_num_bands; |
| 8817 | |
| 8818 | if (*p_header_size < l_header_size) { |
| 8819 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
| 8820 | return OPJ_FALSE; |
| 8821 | } |
| 8822 | |
| 8823 | opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ |
| 8824 | ++p_data; |
| 8825 | |
| 8826 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
| 8827 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
| 8828 | opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ |
| 8829 | ++p_data; |
| 8830 | } |
| 8831 | } |
| 8832 | else { |
| 8833 | l_header_size = 1 + 2*l_num_bands; |
| 8834 | |
| 8835 | if (*p_header_size < l_header_size) { |
| 8836 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); |
| 8837 | return OPJ_FALSE; |
| 8838 | } |
| 8839 | |
| 8840 | opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ |
| 8841 | ++p_data; |
| 8842 | |
| 8843 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
| 8844 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
| 8845 | l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; |
| 8846 | |
| 8847 | opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ |
| 8848 | p_data += 2; |
| 8849 | } |
| 8850 | } |
| 8851 | |
| 8852 | *p_header_size = *p_header_size - l_header_size; |
| 8853 | |
| 8854 | return OPJ_TRUE; |
| 8855 | } |
| 8856 | |
| 8857 | OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
| 8858 | OPJ_UINT32 p_comp_no, |
| 8859 | OPJ_BYTE* p_header_data, |
| 8860 | OPJ_UINT32 * p_header_size, |
| 8861 | opj_event_mgr_t * p_manager |
| 8862 | ) |
| 8863 | { |
| 8864 | /* loop*/ |
| 8865 | OPJ_UINT32 l_band_no; |
| 8866 | opj_cp_t *l_cp = 00; |
| 8867 | opj_tcp_t *l_tcp = 00; |
| 8868 | opj_tccp_t *l_tccp = 00; |
| 8869 | OPJ_BYTE * l_current_ptr = 00; |
| 8870 | OPJ_UINT32 l_tmp, l_num_band; |
| 8871 | |
| 8872 | /* preconditions*/ |
| 8873 | assert(p_j2k != 00); |
| 8874 | assert(p_manager != 00); |
| 8875 | assert(p_header_data != 00); |
| 8876 | |
| 8877 | l_cp = &(p_j2k->m_cp); |
| 8878 | /* come from tile part header or main header ?*/ |
| 8879 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/ |
| 8880 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 8881 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 8882 | |
| 8883 | /* precondition again*/ |
| 8884 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
| 8885 | |
| 8886 | l_tccp = &l_tcp->tccps[p_comp_no]; |
| 8887 | l_current_ptr = p_header_data; |
| 8888 | |
| 8889 | if (*p_header_size < 1) { |
| 8890 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); |
| 8891 | return OPJ_FALSE; |
| 8892 | } |
| 8893 | *p_header_size -= 1; |
| 8894 | |
| 8895 | opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */ |
| 8896 | ++l_current_ptr; |
| 8897 | |
| 8898 | l_tccp->qntsty = l_tmp & 0x1f; |
| 8899 | l_tccp->numgbits = l_tmp >> 5; |
| 8900 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
| 8901 | l_num_band = 1; |
| 8902 | } |
| 8903 | else { |
| 8904 | l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? |
| 8905 | (*p_header_size) : |
| 8906 | (*p_header_size) / 2; |
| 8907 | |
| 8908 | if( l_num_band > OPJ_J2K_MAXBANDS ) { |
| 8909 | opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " |
| 8910 | "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " |
| 8911 | "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS); |
| 8912 | /*return OPJ_FALSE;*/ |
| 8913 | } |
| 8914 | } |
| 8915 | |
| 8916 | #ifdef USE_JPWL |
| 8917 | if (l_cp->correct) { |
| 8918 | |
| 8919 | /* if JPWL is on, we check whether there are too many subbands */ |
| 8920 | if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { |
| 8921 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
| 8922 | "JPWL: bad number of subbands in Sqcx (%d)\n", |
| 8923 | l_num_band); |
| 8924 | if (!JPWL_ASSUME) { |
| 8925 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
| 8926 | return OPJ_FALSE; |
| 8927 | } |
| 8928 | /* we try to correct */ |
| 8929 | l_num_band = 1; |
| 8930 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" |
| 8931 | "- setting number of bands to %d => HYPOTHESIS!!!\n", |
| 8932 | l_num_band); |
| 8933 | }; |
| 8934 | |
| 8935 | }; |
| 8936 | #endif /* USE_JPWL */ |
| 8937 | |
| 8938 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
| 8939 | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
| 8940 | opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */ |
| 8941 | ++l_current_ptr; |
| 8942 | if (l_band_no < OPJ_J2K_MAXBANDS){ |
| 8943 | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); |
| 8944 | l_tccp->stepsizes[l_band_no].mant = 0; |
| 8945 | } |
| 8946 | } |
| 8947 | *p_header_size = *p_header_size - l_num_band; |
| 8948 | } |
| 8949 | else { |
| 8950 | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
| 8951 | opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */ |
| 8952 | l_current_ptr+=2; |
| 8953 | if (l_band_no < OPJ_J2K_MAXBANDS){ |
| 8954 | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); |
| 8955 | l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; |
| 8956 | } |
| 8957 | } |
| 8958 | *p_header_size = *p_header_size - 2*l_num_band; |
| 8959 | } |
| 8960 | |
| 8961 | /* Add Antonin : if scalar_derived -> compute other stepsizes */ |
| 8962 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
| 8963 | for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { |
| 8964 | l_tccp->stepsizes[l_band_no].expn = |
| 8965 | ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ? |
| 8966 | (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; |
| 8967 | l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; |
| 8968 | } |
| 8969 | } |
| 8970 | |
| 8971 | return OPJ_TRUE; |
| 8972 | } |
| 8973 | |
| 8974 | void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k ) |
| 8975 | { |
| 8976 | OPJ_UINT32 i; |
| 8977 | opj_cp_t *l_cp = NULL; |
| 8978 | opj_tcp_t *l_tcp = NULL; |
| 8979 | opj_tccp_t *l_ref_tccp = NULL; |
| 8980 | opj_tccp_t *l_copied_tccp = NULL; |
| 8981 | OPJ_UINT32 l_size; |
| 8982 | |
| 8983 | /* preconditions */ |
| 8984 | assert(p_j2k != 00); |
| 8985 | |
| 8986 | l_cp = &(p_j2k->m_cp); |
| 8987 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
| 8988 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
| 8989 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 8990 | |
| 8991 | l_ref_tccp = &l_tcp->tccps[0]; |
| 8992 | l_copied_tccp = l_ref_tccp + 1; |
| 8993 | l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); |
| 8994 | |
| 8995 | for (i=1;i<p_j2k->m_private_image->numcomps;++i) { |
| 8996 | l_copied_tccp->qntsty = l_ref_tccp->qntsty; |
| 8997 | l_copied_tccp->numgbits = l_ref_tccp->numgbits; |
| 8998 | memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size); |
| 8999 | ++l_copied_tccp; |
| 9000 | } |
| 9001 | } |
| 9002 | |
| 9003 | static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream) |
| 9004 | { |
| 9005 | if (l_default_tile) |
| 9006 | { |
| 9007 | OPJ_INT32 compno; |
| 9008 | |
| 9009 | fprintf(out_stream, "\t default tile {\n"); |
| 9010 | fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); |
| 9011 | fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); |
| 9012 | fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); |
| 9013 | fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); |
| 9014 | |
| 9015 | for (compno = 0; compno < numcomps; compno++) { |
| 9016 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
| 9017 | OPJ_UINT32 resno; |
| 9018 | OPJ_INT32 bandno, numbands; |
| 9019 | |
| 9020 | /* coding style*/ |
| 9021 | fprintf(out_stream, "\t\t comp %d {\n", compno); |
| 9022 | fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); |
| 9023 | fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); |
| 9024 | fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); |
| 9025 | fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); |
| 9026 | fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); |
| 9027 | fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); |
| 9028 | |
| 9029 | fprintf(out_stream, "\t\t\t preccintsize (w,h)="); |
| 9030 | for (resno = 0; resno < l_tccp->numresolutions; resno++) { |
| 9031 | fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); |
| 9032 | } |
| 9033 | fprintf(out_stream, "\n"); |
| 9034 | |
| 9035 | /* quantization style*/ |
| 9036 | fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); |
| 9037 | fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); |
| 9038 | fprintf(out_stream, "\t\t\t stepsizes (m,e)="); |
| 9039 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
| 9040 | for (bandno = 0; bandno < numbands; bandno++) { |
| 9041 | fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, |
| 9042 | l_tccp->stepsizes[bandno].expn); |
| 9043 | } |
| 9044 | fprintf(out_stream, "\n"); |
| 9045 | |
| 9046 | /* RGN value*/ |
| 9047 | fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); |
| 9048 | |
| 9049 | fprintf(out_stream, "\t\t }\n"); |
| 9050 | } /*end of component of default tile*/ |
| 9051 | fprintf(out_stream, "\t }\n"); /*end of default tile*/ |
| 9052 | } |
| 9053 | } |
| 9054 | |
| 9055 | void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) |
| 9056 | { |
| 9057 | /* Check if the flag is compatible with j2k file*/ |
| 9058 | if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){ |
| 9059 | fprintf(out_stream, "Wrong flag\n"); |
| 9060 | return; |
| 9061 | } |
| 9062 | |
| 9063 | /* Dump the image_header */ |
| 9064 | if (flag & OPJ_IMG_INFO){ |
| 9065 | if (p_j2k->m_private_image) |
| 9066 | j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); |
| 9067 | } |
| 9068 | |
| 9069 | /* Dump the codestream info from main header */ |
| 9070 | if (flag & OPJ_J2K_MH_INFO){ |
| 9071 | opj_j2k_dump_MH_info(p_j2k, out_stream); |
| 9072 | } |
| 9073 | /* Dump all tile/codestream info */ |
| 9074 | if (flag & OPJ_J2K_TCH_INFO){ |
| 9075 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 9076 | OPJ_UINT32 i; |
| 9077 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
| 9078 | for (i=0;i<l_nb_tiles;++i) { |
| 9079 | opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
| 9080 | ++l_tcp; |
| 9081 | } |
| 9082 | } |
| 9083 | |
| 9084 | /* Dump the codestream info of the current tile */ |
| 9085 | if (flag & OPJ_J2K_TH_INFO){ |
| 9086 | |
| 9087 | } |
| 9088 | |
| 9089 | /* Dump the codestream index from main header */ |
| 9090 | if (flag & OPJ_J2K_MH_IND){ |
| 9091 | opj_j2k_dump_MH_index(p_j2k, out_stream); |
| 9092 | } |
| 9093 | |
| 9094 | /* Dump the codestream index of the current tile */ |
| 9095 | if (flag & OPJ_J2K_TH_IND){ |
| 9096 | |
| 9097 | } |
| 9098 | |
| 9099 | } |
| 9100 | |
| 9101 | void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) |
| 9102 | { |
| 9103 | opj_codestream_index_t* cstr_index = p_j2k->cstr_index; |
| 9104 | OPJ_UINT32 it_marker, it_tile, it_tile_part; |
| 9105 | |
| 9106 | fprintf(out_stream, "Codestream index from main header: {\n"); |
| 9107 | |
| 9108 | fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" |
| 9109 | "\t Main header end position=%" PRIi64 "\n", |
| 9110 | cstr_index->main_head_start, cstr_index->main_head_end); |
| 9111 | |
| 9112 | fprintf(out_stream, "\t Marker list: {\n"); |
| 9113 | |
| 9114 | if (cstr_index->marker){ |
| 9115 | for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){ |
| 9116 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
| 9117 | cstr_index->marker[it_marker].type, |
| 9118 | cstr_index->marker[it_marker].pos, |
| 9119 | cstr_index->marker[it_marker].len ); |
| 9120 | } |
| 9121 | } |
| 9122 | |
| 9123 | fprintf(out_stream, "\t }\n"); |
| 9124 | |
| 9125 | if (cstr_index->tile_index){ |
| 9126 | |
| 9127 | /* Simple test to avoid to write empty information*/ |
| 9128 | OPJ_UINT32 l_acc_nb_of_tile_part = 0; |
| 9129 | for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ |
| 9130 | l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; |
| 9131 | } |
| 9132 | |
| 9133 | if (l_acc_nb_of_tile_part) |
| 9134 | { |
| 9135 | fprintf(out_stream, "\t Tile index: {\n"); |
| 9136 | |
| 9137 | for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ |
| 9138 | OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; |
| 9139 | |
| 9140 | fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part); |
| 9141 | |
| 9142 | if (cstr_index->tile_index[it_tile].tp_index){ |
| 9143 | for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){ |
| 9144 | fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n", |
| 9145 | it_tile_part, |
| 9146 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, |
| 9147 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, |
| 9148 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); |
| 9149 | } |
| 9150 | } |
| 9151 | |
| 9152 | if (cstr_index->tile_index[it_tile].marker){ |
| 9153 | for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){ |
| 9154 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", |
| 9155 | cstr_index->tile_index[it_tile].marker[it_marker].type, |
| 9156 | cstr_index->tile_index[it_tile].marker[it_marker].pos, |
| 9157 | cstr_index->tile_index[it_tile].marker[it_marker].len ); |
| 9158 | } |
| 9159 | } |
| 9160 | } |
| 9161 | fprintf(out_stream,"\t }\n"); |
| 9162 | } |
| 9163 | } |
| 9164 | |
| 9165 | fprintf(out_stream,"}\n"); |
| 9166 | |
| 9167 | } |
| 9168 | |
| 9169 | |
| 9170 | void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) |
| 9171 | { |
| 9172 | |
| 9173 | fprintf(out_stream, "Codestream info from main header: {\n"); |
| 9174 | |
| 9175 | fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0); |
| 9176 | fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy); |
| 9177 | fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th); |
| 9178 | opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
| 9179 | fprintf(out_stream, "}\n"); |
| 9180 | } |
| 9181 | |
| 9182 | void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream) |
| 9183 | { |
| 9184 | char tab[2]; |
| 9185 | |
| 9186 | if (dev_dump_flag){ |
| 9187 | fprintf(stdout, "[DEV] Dump an image_header struct {\n"); |
| 9188 | tab[0] = '\0'; |
| 9189 | } |
| 9190 | else { |
| 9191 | fprintf(out_stream, "Image info {\n"); |
| 9192 | tab[0] = '\t';tab[1] = '\0'; |
| 9193 | } |
| 9194 | |
| 9195 | fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); |
| 9196 | fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1); |
| 9197 | fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); |
| 9198 | |
| 9199 | if (img_header->comps){ |
| 9200 | OPJ_UINT32 compno; |
| 9201 | for (compno = 0; compno < img_header->numcomps; compno++) { |
| 9202 | fprintf(out_stream, "%s\t component %d {\n", tab, compno); |
| 9203 | j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream); |
| 9204 | fprintf(out_stream,"%s}\n",tab); |
| 9205 | } |
| 9206 | } |
| 9207 | |
| 9208 | fprintf(out_stream, "}\n"); |
| 9209 | } |
| 9210 | |
| 9211 | void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream) |
| 9212 | { |
| 9213 | char tab[3]; |
| 9214 | |
| 9215 | if (dev_dump_flag){ |
| 9216 | fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); |
| 9217 | tab[0] = '\0'; |
| 9218 | } else { |
| 9219 | tab[0] = '\t';tab[1] = '\t';tab[2] = '\0'; |
| 9220 | } |
| 9221 | |
| 9222 | fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); |
| 9223 | fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); |
| 9224 | fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); |
| 9225 | |
| 9226 | if (dev_dump_flag) |
| 9227 | fprintf(out_stream, "}\n"); |
| 9228 | } |
| 9229 | |
| 9230 | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) |
| 9231 | { |
| 9232 | OPJ_UINT32 compno; |
| 9233 | OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; |
| 9234 | opj_tcp_t *l_default_tile; |
| 9235 | opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t)); |
| 9236 | if (!cstr_info) |
| 9237 | return NULL; |
| 9238 | |
| 9239 | cstr_info->nbcomps = p_j2k->m_private_image->numcomps; |
| 9240 | |
| 9241 | cstr_info->tx0 = p_j2k->m_cp.tx0; |
| 9242 | cstr_info->ty0 = p_j2k->m_cp.ty0; |
| 9243 | cstr_info->tdx = p_j2k->m_cp.tdx; |
| 9244 | cstr_info->tdy = p_j2k->m_cp.tdy; |
| 9245 | cstr_info->tw = p_j2k->m_cp.tw; |
| 9246 | cstr_info->th = p_j2k->m_cp.th; |
| 9247 | |
| 9248 | cstr_info->tile_info = NULL; /* Not fill from the main header*/ |
| 9249 | |
| 9250 | l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
| 9251 | |
| 9252 | cstr_info->m_default_tile_info.csty = l_default_tile->csty; |
| 9253 | cstr_info->m_default_tile_info.prg = l_default_tile->prg; |
| 9254 | cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; |
| 9255 | cstr_info->m_default_tile_info.mct = l_default_tile->mct; |
| 9256 | |
| 9257 | cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t)); |
| 9258 | if (!cstr_info->m_default_tile_info.tccp_info) |
| 9259 | { |
| 9260 | opj_destroy_cstr_info(&cstr_info); |
| 9261 | return NULL; |
| 9262 | } |
| 9263 | |
| 9264 | for (compno = 0; compno < numcomps; compno++) { |
| 9265 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
| 9266 | opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]); |
| 9267 | OPJ_INT32 bandno, numbands; |
| 9268 | |
| 9269 | /* coding style*/ |
| 9270 | l_tccp_info->csty = l_tccp->csty; |
| 9271 | l_tccp_info->numresolutions = l_tccp->numresolutions; |
| 9272 | l_tccp_info->cblkw = l_tccp->cblkw; |
| 9273 | l_tccp_info->cblkh = l_tccp->cblkh; |
| 9274 | l_tccp_info->cblksty = l_tccp->cblksty; |
| 9275 | l_tccp_info->qmfbid = l_tccp->qmfbid; |
| 9276 | if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) |
| 9277 | { |
| 9278 | memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); |
| 9279 | memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); |
| 9280 | } |
| 9281 | |
| 9282 | /* quantization style*/ |
| 9283 | l_tccp_info->qntsty = l_tccp->qntsty; |
| 9284 | l_tccp_info->numgbits = l_tccp->numgbits; |
| 9285 | |
| 9286 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
| 9287 | if (numbands < OPJ_J2K_MAXBANDS) { |
| 9288 | for (bandno = 0; bandno < numbands; bandno++) { |
| 9289 | l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant; |
| 9290 | l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn; |
| 9291 | } |
| 9292 | } |
| 9293 | |
| 9294 | /* RGN value*/ |
| 9295 | l_tccp_info->roishift = l_tccp->roishift; |
| 9296 | } |
| 9297 | |
| 9298 | return cstr_info; |
| 9299 | } |
| 9300 | |
| 9301 | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) |
| 9302 | { |
| 9303 | opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) |
| 9304 | opj_calloc(1,sizeof(opj_codestream_index_t)); |
| 9305 | if (!l_cstr_index) |
| 9306 | return NULL; |
| 9307 | |
| 9308 | l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; |
| 9309 | l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; |
| 9310 | l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; |
| 9311 | |
| 9312 | l_cstr_index->marknum = p_j2k->cstr_index->marknum; |
| 9313 | l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t)); |
| 9314 | if (!l_cstr_index->marker){ |
| 9315 | opj_free( l_cstr_index); |
| 9316 | return NULL; |
| 9317 | } |
| 9318 | |
| 9319 | if (p_j2k->cstr_index->marker) |
| 9320 | memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) ); |
| 9321 | else{ |
| 9322 | opj_free(l_cstr_index->marker); |
| 9323 | l_cstr_index->marker = NULL; |
| 9324 | } |
| 9325 | |
| 9326 | l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; |
| 9327 | l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) ); |
| 9328 | if (!l_cstr_index->tile_index){ |
| 9329 | opj_free( l_cstr_index->marker); |
| 9330 | opj_free( l_cstr_index); |
| 9331 | return NULL; |
| 9332 | } |
| 9333 | |
| 9334 | if (!p_j2k->cstr_index->tile_index){ |
| 9335 | opj_free(l_cstr_index->tile_index); |
| 9336 | l_cstr_index->tile_index = NULL; |
| 9337 | } |
| 9338 | else { |
| 9339 | OPJ_UINT32 it_tile = 0; |
| 9340 | for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){ |
| 9341 | |
| 9342 | /* Tile Marker*/ |
| 9343 | l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum; |
| 9344 | |
| 9345 | l_cstr_index->tile_index[it_tile].marker = |
| 9346 | (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t)); |
| 9347 | |
| 9348 | if (!l_cstr_index->tile_index[it_tile].marker) { |
| 9349 | OPJ_UINT32 it_tile_free; |
| 9350 | |
| 9351 | for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ |
| 9352 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
| 9353 | } |
| 9354 | |
| 9355 | opj_free( l_cstr_index->tile_index); |
| 9356 | opj_free( l_cstr_index->marker); |
| 9357 | opj_free( l_cstr_index); |
| 9358 | return NULL; |
| 9359 | } |
| 9360 | |
| 9361 | if (p_j2k->cstr_index->tile_index[it_tile].marker) |
| 9362 | memcpy( l_cstr_index->tile_index[it_tile].marker, |
| 9363 | p_j2k->cstr_index->tile_index[it_tile].marker, |
| 9364 | l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) ); |
| 9365 | else{ |
| 9366 | opj_free(l_cstr_index->tile_index[it_tile].marker); |
| 9367 | l_cstr_index->tile_index[it_tile].marker = NULL; |
| 9368 | } |
| 9369 | |
| 9370 | /* Tile part index*/ |
| 9371 | l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps; |
| 9372 | |
| 9373 | l_cstr_index->tile_index[it_tile].tp_index = |
| 9374 | (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t)); |
| 9375 | |
| 9376 | if(!l_cstr_index->tile_index[it_tile].tp_index){ |
| 9377 | OPJ_UINT32 it_tile_free; |
| 9378 | |
| 9379 | for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ |
| 9380 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
| 9381 | opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); |
| 9382 | } |
| 9383 | |
| 9384 | opj_free( l_cstr_index->tile_index); |
| 9385 | opj_free( l_cstr_index->marker); |
| 9386 | opj_free( l_cstr_index); |
| 9387 | return NULL; |
| 9388 | } |
| 9389 | |
| 9390 | if (p_j2k->cstr_index->tile_index[it_tile].tp_index){ |
| 9391 | memcpy( l_cstr_index->tile_index[it_tile].tp_index, |
| 9392 | p_j2k->cstr_index->tile_index[it_tile].tp_index, |
| 9393 | l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) ); |
| 9394 | } |
| 9395 | else{ |
| 9396 | opj_free(l_cstr_index->tile_index[it_tile].tp_index); |
| 9397 | l_cstr_index->tile_index[it_tile].tp_index = NULL; |
| 9398 | } |
| 9399 | |
| 9400 | /* Packet index (NOT USED)*/ |
| 9401 | l_cstr_index->tile_index[it_tile].nb_packet = 0; |
| 9402 | l_cstr_index->tile_index[it_tile].packet_index = NULL; |
| 9403 | |
| 9404 | } |
| 9405 | } |
| 9406 | |
| 9407 | return l_cstr_index; |
| 9408 | } |
| 9409 | |
| 9410 | OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) |
| 9411 | { |
| 9412 | OPJ_UINT32 it_tile=0; |
| 9413 | |
| 9414 | p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
| 9415 | p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
| 9416 | if (!p_j2k->cstr_index->tile_index) |
| 9417 | return OPJ_FALSE; |
| 9418 | |
| 9419 | for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){ |
| 9420 | p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; |
| 9421 | p_j2k->cstr_index->tile_index[it_tile].marknum = 0; |
| 9422 | p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) |
| 9423 | opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t)); |
| 9424 | if (!p_j2k->cstr_index->tile_index[it_tile].marker) |
| 9425 | return OPJ_FALSE; |
| 9426 | } |
| 9427 | |
| 9428 | return OPJ_TRUE; |
| 9429 | } |
| 9430 | |
| 9431 | OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, |
| 9432 | opj_stream_private_t *p_stream, |
| 9433 | opj_event_mgr_t * p_manager) |
| 9434 | { |
| 9435 | OPJ_BOOL l_go_on = OPJ_TRUE; |
| 9436 | OPJ_UINT32 l_current_tile_no; |
| 9437 | OPJ_UINT32 l_data_size,l_max_data_size; |
| 9438 | OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; |
| 9439 | OPJ_UINT32 l_nb_comps; |
| 9440 | OPJ_BYTE * l_current_data; |
| 9441 | OPJ_UINT32 nr_tiles = 0; |
| 9442 | |
| 9443 | l_current_data = (OPJ_BYTE*)opj_malloc(1000); |
| 9444 | if (! l_current_data) { |
| 9445 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n"); |
| 9446 | return OPJ_FALSE; |
| 9447 | } |
| 9448 | l_max_data_size = 1000; |
| 9449 | |
| 9450 | while (OPJ_TRUE) { |
| 9451 | if (! opj_j2k_read_tile_header( p_j2k, |
| 9452 | &l_current_tile_no, |
| 9453 | &l_data_size, |
| 9454 | &l_tile_x0, &l_tile_y0, |
| 9455 | &l_tile_x1, &l_tile_y1, |
| 9456 | &l_nb_comps, |
| 9457 | &l_go_on, |
| 9458 | p_stream, |
| 9459 | p_manager)) { |
| 9460 | opj_free(l_current_data); |
| 9461 | return OPJ_FALSE; |
| 9462 | } |
| 9463 | |
| 9464 | if (! l_go_on) { |
| 9465 | break; |
| 9466 | } |
| 9467 | |
| 9468 | if (l_data_size > l_max_data_size) { |
| 9469 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); |
| 9470 | if (! l_new_current_data) { |
| 9471 | opj_free(l_current_data); |
| 9472 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
| 9473 | return OPJ_FALSE; |
| 9474 | } |
| 9475 | l_current_data = l_new_current_data; |
| 9476 | l_max_data_size = l_data_size; |
| 9477 | } |
| 9478 | |
| 9479 | if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { |
| 9480 | opj_free(l_current_data); |
| 9481 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
| 9482 | return OPJ_FALSE; |
| 9483 | } |
| 9484 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
| 9485 | |
| 9486 | if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { |
| 9487 | opj_free(l_current_data); |
| 9488 | return OPJ_FALSE; |
| 9489 | } |
| 9490 | opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
| 9491 | |
| 9492 | if(opj_stream_get_number_byte_left(p_stream) == 0 |
| 9493 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) |
| 9494 | break; |
| 9495 | if(++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) |
| 9496 | break; |
| 9497 | } |
| 9498 | |
| 9499 | opj_free(l_current_data); |
| 9500 | |
| 9501 | return OPJ_TRUE; |
| 9502 | } |
| 9503 | |
| 9504 | /** |
| 9505 | * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures. |
| 9506 | */ |
| 9507 | static void opj_j2k_setup_decoding (opj_j2k_t *p_j2k) |
| 9508 | { |
| 9509 | /* preconditions*/ |
| 9510 | assert(p_j2k != 00); |
| 9511 | |
| 9512 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles); |
| 9513 | /* DEVELOPER CORNER, add your custom procedures */ |
| 9514 | |
| 9515 | } |
| 9516 | |
| 9517 | /* |
| 9518 | * Read and decode one tile. |
| 9519 | */ |
| 9520 | static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, |
| 9521 | opj_stream_private_t *p_stream, |
| 9522 | opj_event_mgr_t * p_manager) |
| 9523 | { |
| 9524 | OPJ_BOOL l_go_on = OPJ_TRUE; |
| 9525 | OPJ_UINT32 l_current_tile_no; |
| 9526 | OPJ_UINT32 l_tile_no_to_dec; |
| 9527 | OPJ_UINT32 l_data_size,l_max_data_size; |
| 9528 | OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; |
| 9529 | OPJ_UINT32 l_nb_comps; |
| 9530 | OPJ_BYTE * l_current_data; |
| 9531 | |
| 9532 | l_current_data = (OPJ_BYTE*)opj_malloc(1000); |
| 9533 | if (! l_current_data) { |
| 9534 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n"); |
| 9535 | return OPJ_FALSE; |
| 9536 | } |
| 9537 | l_max_data_size = 1000; |
| 9538 | |
| 9539 | /*Allocate and initialize some elements of codestrem index if not already done*/ |
| 9540 | if( !p_j2k->cstr_index->tile_index) |
| 9541 | { |
| 9542 | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ |
| 9543 | opj_free(l_current_data); |
| 9544 | return OPJ_FALSE; |
| 9545 | } |
| 9546 | } |
| 9547 | /* Move into the codestream to the first SOT used to decode the desired tile */ |
| 9548 | l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; |
| 9549 | if (p_j2k->cstr_index->tile_index) |
| 9550 | if(p_j2k->cstr_index->tile_index->tp_index) |
| 9551 | { |
| 9552 | if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { |
| 9553 | /* the index for this tile has not been built, |
| 9554 | * so move to the last SOT read */ |
| 9555 | if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){ |
| 9556 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
| 9557 | opj_free(l_current_data); |
| 9558 | return OPJ_FALSE; |
| 9559 | } |
| 9560 | } |
| 9561 | else{ |
| 9562 | if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) { |
| 9563 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
| 9564 | opj_free(l_current_data); |
| 9565 | return OPJ_FALSE; |
| 9566 | } |
| 9567 | } |
| 9568 | /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ |
| 9569 | if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) |
| 9570 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
| 9571 | } |
| 9572 | |
| 9573 | while (OPJ_TRUE) { |
| 9574 | if (! opj_j2k_read_tile_header( p_j2k, |
| 9575 | &l_current_tile_no, |
| 9576 | &l_data_size, |
| 9577 | &l_tile_x0, &l_tile_y0, |
| 9578 | &l_tile_x1, &l_tile_y1, |
| 9579 | &l_nb_comps, |
| 9580 | &l_go_on, |
| 9581 | p_stream, |
| 9582 | p_manager)) { |
| 9583 | opj_free(l_current_data); |
| 9584 | return OPJ_FALSE; |
| 9585 | } |
| 9586 | |
| 9587 | if (! l_go_on) { |
| 9588 | break; |
| 9589 | } |
| 9590 | |
| 9591 | if (l_data_size > l_max_data_size) { |
| 9592 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); |
| 9593 | if (! l_new_current_data) { |
| 9594 | opj_free(l_current_data); |
| 9595 | l_current_data = NULL; |
| 9596 | /* TODO: LH: why tile numbering policy used in messages differs from |
| 9597 | the one used in opj_j2k_decode_tiles() ? */ |
| 9598 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); |
| 9599 | return OPJ_FALSE; |
| 9600 | } |
| 9601 | l_current_data = l_new_current_data; |
| 9602 | l_max_data_size = l_data_size; |
| 9603 | } |
| 9604 | |
| 9605 | if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { |
| 9606 | opj_free(l_current_data); |
| 9607 | return OPJ_FALSE; |
| 9608 | } |
| 9609 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); |
| 9610 | |
| 9611 | if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { |
| 9612 | opj_free(l_current_data); |
| 9613 | return OPJ_FALSE; |
| 9614 | } |
| 9615 | opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no); |
| 9616 | |
| 9617 | if(l_current_tile_no == l_tile_no_to_dec) |
| 9618 | { |
| 9619 | /* move into the codestream to the the first SOT (FIXME or not move?)*/ |
| 9620 | if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) { |
| 9621 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); |
| 9622 | return OPJ_FALSE; |
| 9623 | } |
| 9624 | break; |
| 9625 | } |
| 9626 | else { |
| 9627 | opj_event_msg(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec); |
| 9628 | } |
| 9629 | |
| 9630 | } |
| 9631 | |
| 9632 | opj_free(l_current_data); |
| 9633 | |
| 9634 | return OPJ_TRUE; |
| 9635 | } |
| 9636 | |
| 9637 | /** |
| 9638 | * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures. |
| 9639 | */ |
| 9640 | static void opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k) |
| 9641 | { |
| 9642 | /* preconditions*/ |
| 9643 | assert(p_j2k != 00); |
| 9644 | |
| 9645 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile); |
| 9646 | /* DEVELOPER CORNER, add your custom procedures */ |
| 9647 | |
| 9648 | } |
| 9649 | |
| 9650 | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, |
| 9651 | opj_stream_private_t * p_stream, |
| 9652 | opj_image_t * p_image, |
| 9653 | opj_event_mgr_t * p_manager) |
| 9654 | { |
| 9655 | OPJ_UINT32 compno; |
| 9656 | |
| 9657 | if (!p_image) |
| 9658 | return OPJ_FALSE; |
| 9659 | |
| 9660 | p_j2k->m_output_image = opj_image_create0(); |
| 9661 | if (! (p_j2k->m_output_image)) { |
| 9662 | return OPJ_FALSE; |
| 9663 | } |
| 9664 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
| 9665 | |
| 9666 | /* customization of the decoding */ |
| 9667 | opj_j2k_setup_decoding(p_j2k); |
| 9668 | |
| 9669 | /* Decode the codestream */ |
| 9670 | if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { |
| 9671 | opj_image_destroy(p_j2k->m_private_image); |
| 9672 | p_j2k->m_private_image = NULL; |
| 9673 | return OPJ_FALSE; |
| 9674 | } |
| 9675 | |
| 9676 | /* Move data and copy one information from codec to output image*/ |
| 9677 | for (compno = 0; compno < p_image->numcomps; compno++) { |
| 9678 | p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; |
| 9679 | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
| 9680 | #if 0 |
| 9681 | char fn[256]; |
| 9682 | sprintf( fn, "/tmp/%d.raw", compno ); |
| 9683 | FILE *debug = fopen( fn, "wb" ); |
| 9684 | fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug ); |
| 9685 | fclose( debug ); |
| 9686 | #endif |
| 9687 | p_j2k->m_output_image->comps[compno].data = NULL; |
| 9688 | } |
| 9689 | |
| 9690 | return OPJ_TRUE; |
| 9691 | } |
| 9692 | |
| 9693 | OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, |
| 9694 | opj_stream_private_t *p_stream, |
| 9695 | opj_image_t* p_image, |
| 9696 | opj_event_mgr_t * p_manager, |
| 9697 | OPJ_UINT32 tile_index ) |
| 9698 | { |
| 9699 | OPJ_UINT32 compno; |
| 9700 | OPJ_UINT32 l_tile_x, l_tile_y; |
| 9701 | opj_image_comp_t* l_img_comp; |
| 9702 | |
| 9703 | if (!p_image) { |
| 9704 | opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); |
| 9705 | return OPJ_FALSE; |
| 9706 | } |
| 9707 | |
| 9708 | if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){ |
| 9709 | opj_event_msg(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); |
| 9710 | return OPJ_FALSE; |
| 9711 | } |
| 9712 | |
| 9713 | /* Compute the dimension of the desired tile*/ |
| 9714 | l_tile_x = tile_index % p_j2k->m_cp.tw; |
| 9715 | l_tile_y = tile_index / p_j2k->m_cp.tw; |
| 9716 | |
| 9717 | p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
| 9718 | if (p_image->x0 < p_j2k->m_private_image->x0) |
| 9719 | p_image->x0 = p_j2k->m_private_image->x0; |
| 9720 | p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
| 9721 | if (p_image->x1 > p_j2k->m_private_image->x1) |
| 9722 | p_image->x1 = p_j2k->m_private_image->x1; |
| 9723 | |
| 9724 | p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
| 9725 | if (p_image->y0 < p_j2k->m_private_image->y0) |
| 9726 | p_image->y0 = p_j2k->m_private_image->y0; |
| 9727 | p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
| 9728 | if (p_image->y1 > p_j2k->m_private_image->y1) |
| 9729 | p_image->y1 = p_j2k->m_private_image->y1; |
| 9730 | |
| 9731 | l_img_comp = p_image->comps; |
| 9732 | for (compno=0; compno < p_image->numcomps; ++compno) |
| 9733 | { |
| 9734 | OPJ_INT32 l_comp_x1, l_comp_y1; |
| 9735 | |
| 9736 | l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; |
| 9737 | |
| 9738 | l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx); |
| 9739 | l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy); |
| 9740 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
| 9741 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
| 9742 | |
| 9743 | l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor)); |
| 9744 | l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor)); |
| 9745 | |
| 9746 | l_img_comp++; |
| 9747 | } |
| 9748 | |
| 9749 | /* Destroy the previous output image*/ |
| 9750 | if (p_j2k->m_output_image) |
| 9751 | opj_image_destroy(p_j2k->m_output_image); |
| 9752 | |
| 9753 | /* Create the ouput image from the information previously computed*/ |
| 9754 | p_j2k->m_output_image = opj_image_create0(); |
| 9755 | if (! (p_j2k->m_output_image)) { |
| 9756 | return OPJ_FALSE; |
| 9757 | } |
| 9758 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
| 9759 | |
| 9760 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; |
| 9761 | |
| 9762 | /* customization of the decoding */ |
| 9763 | opj_j2k_setup_decoding_tile(p_j2k); |
| 9764 | |
| 9765 | /* Decode the codestream */ |
| 9766 | if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { |
| 9767 | opj_image_destroy(p_j2k->m_private_image); |
| 9768 | p_j2k->m_private_image = NULL; |
| 9769 | return OPJ_FALSE; |
| 9770 | } |
| 9771 | |
| 9772 | /* Move data and copy one information from codec to output image*/ |
| 9773 | for (compno = 0; compno < p_image->numcomps; compno++) { |
| 9774 | p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; |
| 9775 | |
| 9776 | if (p_image->comps[compno].data) |
| 9777 | opj_free(p_image->comps[compno].data); |
| 9778 | |
| 9779 | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
| 9780 | |
| 9781 | p_j2k->m_output_image->comps[compno].data = NULL; |
| 9782 | } |
| 9783 | |
| 9784 | return OPJ_TRUE; |
| 9785 | } |
| 9786 | |
| 9787 | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, |
| 9788 | OPJ_UINT32 res_factor, |
| 9789 | opj_event_mgr_t * p_manager) |
| 9790 | { |
| 9791 | OPJ_UINT32 it_comp; |
| 9792 | |
| 9793 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; |
| 9794 | |
| 9795 | if (p_j2k->m_private_image) { |
| 9796 | if (p_j2k->m_private_image->comps) { |
| 9797 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { |
| 9798 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { |
| 9799 | for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { |
| 9800 | OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; |
| 9801 | if ( res_factor >= max_res){ |
| 9802 | opj_event_msg(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n"); |
| 9803 | return OPJ_FALSE; |
| 9804 | } |
| 9805 | p_j2k->m_private_image->comps[it_comp].factor = res_factor; |
| 9806 | } |
| 9807 | return OPJ_TRUE; |
| 9808 | } |
| 9809 | } |
| 9810 | } |
| 9811 | } |
| 9812 | |
| 9813 | return OPJ_FALSE; |
| 9814 | } |
| 9815 | |
| 9816 | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, |
| 9817 | opj_stream_private_t *p_stream, |
| 9818 | opj_event_mgr_t * p_manager ) |
| 9819 | { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9820 | OPJ_UINT32 i, j; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9821 | OPJ_UINT32 l_nb_tiles; |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9822 | OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size; |
| 9823 | OPJ_BYTE * l_current_data = 00; |
| 9824 | opj_tcd_t* p_tcd = 00; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9825 | |
| 9826 | /* preconditions */ |
| 9827 | assert(p_j2k != 00); |
| 9828 | assert(p_stream != 00); |
| 9829 | assert(p_manager != 00); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9830 | |
| 9831 | p_tcd = p_j2k->m_tcd; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9832 | |
| 9833 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
| 9834 | for (i=0;i<l_nb_tiles;++i) { |
| 9835 | if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9836 | if (l_current_data) { |
| 9837 | opj_free(l_current_data); |
| 9838 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9839 | return OPJ_FALSE; |
| 9840 | } |
| 9841 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9842 | /* if we only have one tile, then simply set tile component data equal to image component data */ |
| 9843 | /* otherwise, allocate the data */ |
| 9844 | for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) { |
| 9845 | opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; |
| 9846 | if (l_nb_tiles == 1) { |
| 9847 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
| 9848 | l_tilec->data = l_img_comp->data; |
| 9849 | l_tilec->ownsData = OPJ_FALSE; |
| 9850 | } else { |
| 9851 | if(! opj_alloc_tile_component_data(l_tilec)) { |
| 9852 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); |
| 9853 | if (l_current_data) { |
| 9854 | opj_free(l_current_data); |
| 9855 | } |
| 9856 | return OPJ_FALSE; |
| 9857 | } |
| 9858 | opj_alloc_tile_component_data(l_tilec); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9859 | } |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9860 | } |
| 9861 | l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); |
| 9862 | if (l_nb_tiles > 1) { |
| 9863 | if (l_current_tile_size > l_max_tile_size) { |
| 9864 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size); |
| 9865 | if (! l_new_current_data) { |
| 9866 | if (l_current_data) { |
| 9867 | opj_free(l_current_data); |
| 9868 | } |
| 9869 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); |
| 9870 | return OPJ_FALSE; |
| 9871 | } |
| 9872 | l_current_data = l_new_current_data; |
| 9873 | l_max_tile_size = l_current_tile_size; |
| 9874 | } |
| 9875 | |
| 9876 | /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ |
| 9877 | /* 32 bit components @ 8 bit precision get converted to 8 bit */ |
| 9878 | /* 32 bit components @ 16 bit precision get converted to 16 bit */ |
| 9879 | opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data); |
| 9880 | |
| 9881 | /* now copy this data into the tile component */ |
| 9882 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,l_current_data,l_current_tile_size)) { |
| 9883 | opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); |
| 9884 | return OPJ_FALSE; |
| 9885 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9886 | } |
| 9887 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9888 | if (! opj_j2k_post_write_tile (p_j2k,p_stream,p_manager)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9889 | return OPJ_FALSE; |
| 9890 | } |
| 9891 | } |
| 9892 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9893 | if (l_current_data) { |
| 9894 | opj_free(l_current_data); |
| 9895 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9896 | return OPJ_TRUE; |
| 9897 | } |
| 9898 | |
| 9899 | OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k, |
| 9900 | opj_stream_private_t *p_stream, |
| 9901 | opj_event_mgr_t * p_manager) |
| 9902 | { |
| 9903 | /* customization of the encoding */ |
| 9904 | opj_j2k_setup_end_compress(p_j2k); |
| 9905 | |
| 9906 | if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) |
| 9907 | { |
| 9908 | return OPJ_FALSE; |
| 9909 | } |
| 9910 | |
| 9911 | return OPJ_TRUE; |
| 9912 | } |
| 9913 | |
| 9914 | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, |
| 9915 | opj_stream_private_t *p_stream, |
| 9916 | opj_image_t * p_image, |
| 9917 | opj_event_mgr_t * p_manager) |
| 9918 | { |
| 9919 | /* preconditions */ |
| 9920 | assert(p_j2k != 00); |
| 9921 | assert(p_stream != 00); |
| 9922 | assert(p_manager != 00); |
| 9923 | |
| 9924 | p_j2k->m_private_image = opj_image_create0(); |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9925 | if (! p_j2k->m_private_image) { |
| 9926 | opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." ); |
| 9927 | return OPJ_FALSE; |
| 9928 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 9929 | opj_copy_image_header(p_image, p_j2k->m_private_image); |
| 9930 | |
| 9931 | /* TODO_MSD: Find a better way */ |
| 9932 | if (p_image->comps) { |
| 9933 | OPJ_UINT32 it_comp; |
| 9934 | for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { |
| 9935 | if (p_image->comps[it_comp].data) { |
| 9936 | p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data; |
| 9937 | p_image->comps[it_comp].data = NULL; |
| 9938 | |
| 9939 | } |
| 9940 | } |
| 9941 | } |
| 9942 | |
| 9943 | /* customization of the validation */ |
| 9944 | opj_j2k_setup_encoding_validation (p_j2k); |
| 9945 | |
| 9946 | /* validation of the parameters codec */ |
| 9947 | if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) { |
| 9948 | return OPJ_FALSE; |
| 9949 | } |
| 9950 | |
| 9951 | /* customization of the encoding */ |
| 9952 | opj_j2k_setup_header_writing(p_j2k); |
| 9953 | |
| 9954 | /* write header */ |
| 9955 | if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { |
| 9956 | return OPJ_FALSE; |
| 9957 | } |
| 9958 | |
| 9959 | return OPJ_TRUE; |
| 9960 | } |
| 9961 | |
| 9962 | OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k, |
| 9963 | OPJ_UINT32 p_tile_index, |
| 9964 | opj_stream_private_t *p_stream, |
| 9965 | opj_event_mgr_t * p_manager ) |
| 9966 | { |
| 9967 | (void)p_stream; |
| 9968 | if (p_tile_index != p_j2k->m_current_tile_number) { |
| 9969 | opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." ); |
| 9970 | return OPJ_FALSE; |
| 9971 | } |
| 9972 | |
| 9973 | opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); |
| 9974 | |
| 9975 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; |
| 9976 | p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; |
| 9977 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
| 9978 | |
| 9979 | /* initialisation before tile encoding */ |
| 9980 | if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { |
| 9981 | return OPJ_FALSE; |
| 9982 | } |
| 9983 | |
| 9984 | return OPJ_TRUE; |
| 9985 | } |
| 9986 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 9987 | void opj_get_tile_dimensions(opj_image_t * l_image, |
| 9988 | opj_tcd_tilecomp_t * l_tilec, |
| 9989 | opj_image_comp_t * l_img_comp, |
| 9990 | OPJ_UINT32* l_size_comp, |
| 9991 | OPJ_UINT32* l_width, |
| 9992 | OPJ_UINT32* l_height, |
| 9993 | OPJ_UINT32* l_offset_x, |
| 9994 | OPJ_UINT32* l_offset_y, |
| 9995 | OPJ_UINT32* l_image_width, |
| 9996 | OPJ_UINT32* l_stride, |
| 9997 | OPJ_UINT32* l_tile_offset) { |
| 9998 | OPJ_UINT32 l_remaining; |
| 9999 | *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ |
| 10000 | l_remaining = l_img_comp->prec & 7; /* (%8) */ |
| 10001 | if (l_remaining) { |
| 10002 | *l_size_comp += 1; |
| 10003 | } |
| 10004 | |
| 10005 | if (*l_size_comp == 3) { |
| 10006 | *l_size_comp = 4; |
| 10007 | } |
| 10008 | |
| 10009 | *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); |
| 10010 | *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); |
| 10011 | *l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx); |
| 10012 | *l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, (OPJ_INT32)l_img_comp->dy); |
| 10013 | *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx); |
| 10014 | *l_stride = *l_image_width - *l_width; |
| 10015 | *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; |
| 10016 | } |
| 10017 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10018 | void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data) |
| 10019 | { |
| 10020 | OPJ_UINT32 i,j,k = 0; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10021 | |
| 10022 | for (i=0;i<p_tcd->image->numcomps;++i) { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10023 | opj_image_t * l_image = p_tcd->image; |
| 10024 | OPJ_INT32 * l_src_ptr; |
| 10025 | opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; |
| 10026 | opj_image_comp_t * l_img_comp = l_image->comps + i; |
| 10027 | OPJ_UINT32 l_size_comp,l_width,l_height,l_offset_x,l_offset_y, l_image_width,l_stride,l_tile_offset; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10028 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10029 | opj_get_tile_dimensions(l_image, |
| 10030 | l_tilec, |
| 10031 | l_img_comp, |
| 10032 | &l_size_comp, |
| 10033 | &l_width, |
| 10034 | &l_height, |
| 10035 | &l_offset_x, |
| 10036 | &l_offset_y, |
| 10037 | &l_image_width, |
| 10038 | &l_stride, |
| 10039 | &l_tile_offset); |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10040 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10041 | l_src_ptr = l_img_comp->data + l_tile_offset; |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10042 | |
| 10043 | switch (l_size_comp) { |
| 10044 | case 1: |
| 10045 | { |
| 10046 | OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; |
| 10047 | if (l_img_comp->sgnd) { |
| 10048 | for (j=0;j<l_height;++j) { |
| 10049 | for (k=0;k<l_width;++k) { |
| 10050 | *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr); |
| 10051 | ++l_dest_ptr; |
| 10052 | ++l_src_ptr; |
| 10053 | } |
| 10054 | l_src_ptr += l_stride; |
| 10055 | } |
| 10056 | } |
| 10057 | else { |
| 10058 | for (j=0;j<l_height;++j) { |
| 10059 | for (k=0;k<l_width;++k) { |
| 10060 | *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr)&0xff); |
| 10061 | ++l_dest_ptr; |
| 10062 | ++l_src_ptr; |
| 10063 | } |
| 10064 | l_src_ptr += l_stride; |
| 10065 | } |
| 10066 | } |
| 10067 | |
| 10068 | p_data = (OPJ_BYTE*) l_dest_ptr; |
| 10069 | } |
| 10070 | break; |
| 10071 | case 2: |
| 10072 | { |
| 10073 | OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; |
| 10074 | if (l_img_comp->sgnd) { |
| 10075 | for (j=0;j<l_height;++j) { |
| 10076 | for (k=0;k<l_width;++k) { |
| 10077 | *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++)); |
| 10078 | } |
| 10079 | l_src_ptr += l_stride; |
| 10080 | } |
| 10081 | } |
| 10082 | else { |
| 10083 | for (j=0;j<l_height;++j) { |
| 10084 | for (k=0;k<l_width;++k) { |
| 10085 | *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); |
| 10086 | } |
| 10087 | l_src_ptr += l_stride; |
| 10088 | } |
| 10089 | } |
| 10090 | |
| 10091 | p_data = (OPJ_BYTE*) l_dest_ptr; |
| 10092 | } |
| 10093 | break; |
| 10094 | case 4: |
| 10095 | { |
| 10096 | OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; |
| 10097 | for (j=0;j<l_height;++j) { |
| 10098 | for (k=0;k<l_width;++k) { |
| 10099 | *(l_dest_ptr++) = *(l_src_ptr++); |
| 10100 | } |
| 10101 | l_src_ptr += l_stride; |
| 10102 | } |
| 10103 | |
| 10104 | p_data = (OPJ_BYTE*) l_dest_ptr; |
| 10105 | } |
| 10106 | break; |
| 10107 | } |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10108 | } |
| 10109 | } |
| 10110 | |
| 10111 | OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k, |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10112 | opj_stream_private_t *p_stream, |
| 10113 | opj_event_mgr_t * p_manager ) |
| 10114 | { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10115 | OPJ_UINT32 l_nb_bytes_written; |
| 10116 | OPJ_BYTE * l_current_data = 00; |
| 10117 | OPJ_UINT32 l_tile_size = 0; |
| 10118 | OPJ_UINT32 l_available_data; |
| 10119 | |
| 10120 | /* preconditions */ |
| 10121 | assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
| 10122 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10123 | l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; |
| 10124 | l_available_data = l_tile_size; |
| 10125 | l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; |
| 10126 | |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10127 | l_nb_bytes_written = 0; |
| 10128 | if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { |
| 10129 | return OPJ_FALSE; |
| 10130 | } |
| 10131 | l_current_data += l_nb_bytes_written; |
| 10132 | l_available_data -= l_nb_bytes_written; |
| 10133 | |
| 10134 | l_nb_bytes_written = 0; |
| 10135 | if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { |
| 10136 | return OPJ_FALSE; |
| 10137 | } |
| 10138 | |
| 10139 | l_available_data -= l_nb_bytes_written; |
| 10140 | l_nb_bytes_written = l_tile_size - l_available_data; |
| 10141 | |
| 10142 | if ( opj_stream_write_data( p_stream, |
| 10143 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, |
| 10144 | l_nb_bytes_written,p_manager) != l_nb_bytes_written) { |
| 10145 | return OPJ_FALSE; |
| 10146 | } |
| 10147 | |
| 10148 | ++p_j2k->m_current_tile_number; |
| 10149 | |
| 10150 | return OPJ_TRUE; |
| 10151 | } |
| 10152 | |
| 10153 | void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k) |
| 10154 | { |
| 10155 | /* preconditions */ |
| 10156 | assert(p_j2k != 00); |
| 10157 | |
| 10158 | /* DEVELOPER CORNER, insert your custom procedures */ |
| 10159 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc ); |
| 10160 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10161 | if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10162 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm); |
| 10163 | } |
| 10164 | |
| 10165 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc ); |
| 10166 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding ); |
| 10167 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory); |
| 10168 | } |
| 10169 | |
| 10170 | void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k) |
| 10171 | { |
| 10172 | /* preconditions */ |
| 10173 | assert(p_j2k != 00); |
| 10174 | |
| 10175 | opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder); |
| 10176 | opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation); |
| 10177 | |
| 10178 | /* DEVELOPER CORNER, add your custom validation procedure */ |
| 10179 | opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation); |
| 10180 | } |
| 10181 | |
| 10182 | void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k) |
| 10183 | { |
| 10184 | /* preconditions */ |
| 10185 | assert(p_j2k != 00); |
| 10186 | |
| 10187 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info ); |
| 10188 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc ); |
| 10189 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz ); |
| 10190 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod ); |
| 10191 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd ); |
| 10192 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10193 | if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10194 | /* No need for COC or QCC, QCD and COD are used |
| 10195 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc ); |
| 10196 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc ); |
| 10197 | */ |
| 10198 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm ); |
| 10199 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10200 | if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10201 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc ); |
| 10202 | } |
| 10203 | } |
| 10204 | |
| 10205 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions); |
| 10206 | |
| 10207 | if (p_j2k->m_cp.comment != 00) { |
| 10208 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com); |
| 10209 | } |
| 10210 | |
| 10211 | /* DEVELOPER CORNER, insert your custom procedures */ |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10212 | if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10213 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group ); |
| 10214 | } |
| 10215 | /* End of Developer Corner */ |
| 10216 | |
| 10217 | if (p_j2k->cstr_index) { |
| 10218 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header ); |
| 10219 | } |
| 10220 | |
| 10221 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd); |
| 10222 | opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates); |
| 10223 | } |
| 10224 | |
| 10225 | OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k, |
| 10226 | OPJ_BYTE * p_data, |
| 10227 | OPJ_UINT32 * p_data_written, |
| 10228 | OPJ_UINT32 p_total_data_size, |
| 10229 | opj_stream_private_t *p_stream, |
| 10230 | struct opj_event_mgr * p_manager ) |
| 10231 | { |
| 10232 | OPJ_UINT32 l_nb_bytes_written = 0; |
| 10233 | OPJ_UINT32 l_current_nb_bytes_written; |
| 10234 | OPJ_BYTE * l_begin_data = 00; |
| 10235 | |
| 10236 | opj_tcd_t * l_tcd = 00; |
| 10237 | opj_cp_t * l_cp = 00; |
| 10238 | |
| 10239 | l_tcd = p_j2k->m_tcd; |
| 10240 | l_cp = &(p_j2k->m_cp); |
| 10241 | |
| 10242 | l_tcd->cur_pino = 0; |
| 10243 | |
| 10244 | /*Get number of tile parts*/ |
| 10245 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
| 10246 | |
| 10247 | /* INDEX >> */ |
| 10248 | /* << INDEX */ |
| 10249 | |
| 10250 | l_current_nb_bytes_written = 0; |
| 10251 | l_begin_data = p_data; |
| 10252 | if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) |
| 10253 | { |
| 10254 | return OPJ_FALSE; |
| 10255 | } |
| 10256 | |
| 10257 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10258 | p_data += l_current_nb_bytes_written; |
| 10259 | p_total_data_size -= l_current_nb_bytes_written; |
| 10260 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10261 | if (!OPJ_IS_CINEMA(l_cp->rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10262 | #if 0 |
| 10263 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { |
| 10264 | l_current_nb_bytes_written = 0; |
| 10265 | opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); |
| 10266 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10267 | p_data += l_current_nb_bytes_written; |
| 10268 | p_total_data_size -= l_current_nb_bytes_written; |
| 10269 | |
| 10270 | l_current_nb_bytes_written = 0; |
| 10271 | opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); |
| 10272 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10273 | p_data += l_current_nb_bytes_written; |
| 10274 | p_total_data_size -= l_current_nb_bytes_written; |
| 10275 | } |
| 10276 | #endif |
| 10277 | |
| 10278 | if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { |
| 10279 | l_current_nb_bytes_written = 0; |
| 10280 | opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager); |
| 10281 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10282 | p_data += l_current_nb_bytes_written; |
| 10283 | p_total_data_size -= l_current_nb_bytes_written; |
| 10284 | } |
| 10285 | } |
| 10286 | |
| 10287 | l_current_nb_bytes_written = 0; |
| 10288 | if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { |
| 10289 | return OPJ_FALSE; |
| 10290 | } |
| 10291 | |
| 10292 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10293 | * p_data_written = l_nb_bytes_written; |
| 10294 | |
| 10295 | /* Writing Psot in SOT marker */ |
| 10296 | opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */ |
| 10297 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10298 | if (OPJ_IS_CINEMA(l_cp->rsiz)){ |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10299 | opj_j2k_update_tlm(p_j2k,l_nb_bytes_written); |
| 10300 | } |
| 10301 | |
| 10302 | return OPJ_TRUE; |
| 10303 | } |
| 10304 | |
| 10305 | OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, |
| 10306 | OPJ_BYTE * p_data, |
| 10307 | OPJ_UINT32 * p_data_written, |
| 10308 | OPJ_UINT32 p_total_data_size, |
| 10309 | opj_stream_private_t *p_stream, |
| 10310 | struct opj_event_mgr * p_manager |
| 10311 | ) |
| 10312 | { |
| 10313 | OPJ_UINT32 tilepartno=0; |
| 10314 | OPJ_UINT32 l_nb_bytes_written = 0; |
| 10315 | OPJ_UINT32 l_current_nb_bytes_written; |
| 10316 | OPJ_UINT32 l_part_tile_size; |
| 10317 | OPJ_UINT32 tot_num_tp; |
| 10318 | OPJ_UINT32 pino; |
| 10319 | |
| 10320 | OPJ_BYTE * l_begin_data; |
| 10321 | opj_tcp_t *l_tcp = 00; |
| 10322 | opj_tcd_t * l_tcd = 00; |
| 10323 | opj_cp_t * l_cp = 00; |
| 10324 | |
| 10325 | l_tcd = p_j2k->m_tcd; |
| 10326 | l_cp = &(p_j2k->m_cp); |
| 10327 | l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; |
| 10328 | |
| 10329 | /*Get number of tile parts*/ |
| 10330 | tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number); |
| 10331 | |
| 10332 | /* start writing remaining tile parts */ |
| 10333 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
| 10334 | for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { |
| 10335 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
| 10336 | l_current_nb_bytes_written = 0; |
| 10337 | l_part_tile_size = 0; |
| 10338 | l_begin_data = p_data; |
| 10339 | |
| 10340 | if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { |
| 10341 | return OPJ_FALSE; |
| 10342 | } |
| 10343 | |
| 10344 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10345 | p_data += l_current_nb_bytes_written; |
| 10346 | p_total_data_size -= l_current_nb_bytes_written; |
| 10347 | l_part_tile_size += l_current_nb_bytes_written; |
| 10348 | |
| 10349 | l_current_nb_bytes_written = 0; |
| 10350 | if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { |
| 10351 | return OPJ_FALSE; |
| 10352 | } |
| 10353 | |
| 10354 | p_data += l_current_nb_bytes_written; |
| 10355 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10356 | p_total_data_size -= l_current_nb_bytes_written; |
| 10357 | l_part_tile_size += l_current_nb_bytes_written; |
| 10358 | |
| 10359 | /* Writing Psot in SOT marker */ |
| 10360 | opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ |
| 10361 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10362 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10363 | opj_j2k_update_tlm(p_j2k,l_part_tile_size); |
| 10364 | } |
| 10365 | |
| 10366 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
| 10367 | } |
| 10368 | |
| 10369 | for (pino = 1; pino <= l_tcp->numpocs; ++pino) { |
| 10370 | l_tcd->cur_pino = pino; |
| 10371 | |
| 10372 | /*Get number of tile parts*/ |
| 10373 | tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number); |
| 10374 | for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { |
| 10375 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
| 10376 | l_current_nb_bytes_written = 0; |
| 10377 | l_part_tile_size = 0; |
| 10378 | l_begin_data = p_data; |
| 10379 | |
| 10380 | if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { |
| 10381 | return OPJ_FALSE; |
| 10382 | } |
| 10383 | |
| 10384 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10385 | p_data += l_current_nb_bytes_written; |
| 10386 | p_total_data_size -= l_current_nb_bytes_written; |
| 10387 | l_part_tile_size += l_current_nb_bytes_written; |
| 10388 | |
| 10389 | l_current_nb_bytes_written = 0; |
| 10390 | |
| 10391 | if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { |
| 10392 | return OPJ_FALSE; |
| 10393 | } |
| 10394 | |
| 10395 | l_nb_bytes_written += l_current_nb_bytes_written; |
| 10396 | p_data += l_current_nb_bytes_written; |
| 10397 | p_total_data_size -= l_current_nb_bytes_written; |
| 10398 | l_part_tile_size += l_current_nb_bytes_written; |
| 10399 | |
| 10400 | /* Writing Psot in SOT marker */ |
| 10401 | opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ |
| 10402 | |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10403 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10404 | opj_j2k_update_tlm(p_j2k,l_part_tile_size); |
| 10405 | } |
| 10406 | |
| 10407 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
| 10408 | } |
| 10409 | } |
| 10410 | |
| 10411 | *p_data_written = l_nb_bytes_written; |
| 10412 | |
| 10413 | return OPJ_TRUE; |
| 10414 | } |
| 10415 | |
| 10416 | OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k, |
| 10417 | struct opj_stream_private *p_stream, |
| 10418 | struct opj_event_mgr * p_manager ) |
| 10419 | { |
| 10420 | OPJ_UINT32 l_tlm_size; |
| 10421 | OPJ_OFF_T l_tlm_position, l_current_position; |
| 10422 | |
| 10423 | /* preconditions */ |
| 10424 | assert(p_j2k != 00); |
| 10425 | assert(p_manager != 00); |
| 10426 | assert(p_stream != 00); |
| 10427 | |
| 10428 | l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; |
| 10429 | l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; |
| 10430 | l_current_position = opj_stream_tell(p_stream); |
| 10431 | |
| 10432 | if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) { |
| 10433 | return OPJ_FALSE; |
| 10434 | } |
| 10435 | |
| 10436 | if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) { |
| 10437 | return OPJ_FALSE; |
| 10438 | } |
| 10439 | |
| 10440 | if (! opj_stream_seek(p_stream,l_current_position,p_manager)) { |
| 10441 | return OPJ_FALSE; |
| 10442 | } |
| 10443 | |
| 10444 | return OPJ_TRUE; |
| 10445 | } |
| 10446 | |
| 10447 | OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k, |
| 10448 | struct opj_stream_private *p_stream, |
| 10449 | struct opj_event_mgr * p_manager ) |
| 10450 | { |
| 10451 | /* preconditions */ |
| 10452 | assert(p_j2k != 00); |
| 10453 | assert(p_manager != 00); |
| 10454 | assert(p_stream != 00); |
| 10455 | |
| 10456 | opj_tcd_destroy(p_j2k->m_tcd); |
| 10457 | p_j2k->m_tcd = 00; |
| 10458 | |
| 10459 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
| 10460 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
| 10461 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; |
| 10462 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; |
| 10463 | } |
| 10464 | |
| 10465 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
| 10466 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
| 10467 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; |
| 10468 | } |
| 10469 | |
| 10470 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; |
| 10471 | |
| 10472 | return OPJ_TRUE; |
| 10473 | } |
| 10474 | |
| 10475 | /** |
| 10476 | * Destroys the memory associated with the decoding of headers. |
| 10477 | */ |
| 10478 | static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k, |
| 10479 | opj_stream_private_t *p_stream, |
| 10480 | opj_event_mgr_t * p_manager |
| 10481 | ) |
| 10482 | { |
| 10483 | /* preconditions */ |
| 10484 | assert(p_j2k != 00); |
| 10485 | assert(p_stream != 00); |
| 10486 | assert(p_manager != 00); |
| 10487 | |
| 10488 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
| 10489 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
| 10490 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; |
| 10491 | } |
| 10492 | |
| 10493 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
| 10494 | |
| 10495 | return OPJ_TRUE; |
| 10496 | } |
| 10497 | |
| 10498 | OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, |
| 10499 | struct opj_stream_private *p_stream, |
| 10500 | struct opj_event_mgr * p_manager ) |
| 10501 | { |
| 10502 | opj_codestream_info_t * l_cstr_info = 00; |
| 10503 | |
| 10504 | /* preconditions */ |
| 10505 | assert(p_j2k != 00); |
| 10506 | assert(p_manager != 00); |
| 10507 | assert(p_stream != 00); |
| 10508 | (void)l_cstr_info; |
| 10509 | |
| 10510 | /* TODO mergeV2: check this part which use cstr_info */ |
| 10511 | /*l_cstr_info = p_j2k->cstr_info; |
| 10512 | |
| 10513 | if (l_cstr_info) { |
| 10514 | OPJ_UINT32 compno; |
| 10515 | 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)); |
| 10516 | |
| 10517 | l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; |
| 10518 | l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; |
| 10519 | |
| 10520 | l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; |
| 10521 | |
| 10522 | l_cstr_info->tw = p_j2k->m_cp.tw; |
| 10523 | l_cstr_info->th = p_j2k->m_cp.th; |
| 10524 | |
| 10525 | l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ |
| 10526 | /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ |
| 10527 | /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ |
| 10528 | /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ |
| 10529 | |
| 10530 | /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; |
| 10531 | |
| 10532 | l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; |
| 10533 | |
| 10534 | l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); |
| 10535 | |
| 10536 | for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { |
| 10537 | l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; |
| 10538 | } |
| 10539 | |
| 10540 | l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ |
| 10541 | |
| 10542 | /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ |
| 10543 | |
| 10544 | /*l_cstr_info->maxmarknum = 100; |
| 10545 | l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); |
| 10546 | l_cstr_info->marknum = 0; |
| 10547 | }*/ |
| 10548 | |
| 10549 | return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager); |
| 10550 | } |
| 10551 | |
| 10552 | /** |
| 10553 | * Creates a tile-coder decoder. |
| 10554 | * |
| 10555 | * @param p_stream the stream to write data to. |
| 10556 | * @param p_j2k J2K codec. |
| 10557 | * @param p_manager the user event manager. |
| 10558 | */ |
| 10559 | static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k, |
| 10560 | opj_stream_private_t *p_stream, |
| 10561 | opj_event_mgr_t * p_manager |
| 10562 | ) |
| 10563 | { |
| 10564 | /* preconditions */ |
| 10565 | assert(p_j2k != 00); |
| 10566 | assert(p_manager != 00); |
| 10567 | assert(p_stream != 00); |
| 10568 | |
| 10569 | p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); |
| 10570 | |
| 10571 | if (! p_j2k->m_tcd) { |
| 10572 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); |
| 10573 | return OPJ_FALSE; |
| 10574 | } |
| 10575 | |
| 10576 | if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) { |
| 10577 | opj_tcd_destroy(p_j2k->m_tcd); |
| 10578 | p_j2k->m_tcd = 00; |
| 10579 | return OPJ_FALSE; |
| 10580 | } |
| 10581 | |
| 10582 | return OPJ_TRUE; |
| 10583 | } |
| 10584 | |
| 10585 | OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k, |
| 10586 | OPJ_UINT32 p_tile_index, |
| 10587 | OPJ_BYTE * p_data, |
| 10588 | OPJ_UINT32 p_data_size, |
| 10589 | opj_stream_private_t *p_stream, |
| 10590 | opj_event_mgr_t * p_manager ) |
| 10591 | { |
| 10592 | if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) { |
| 10593 | opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); |
| 10594 | return OPJ_FALSE; |
| 10595 | } |
| 10596 | else { |
Svet Ganov | e6986e1 | 2015-06-04 14:52:15 -0700 | [diff] [blame] | 10597 | OPJ_UINT32 j; |
| 10598 | /* Allocate data */ |
| 10599 | for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) { |
| 10600 | opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; |
| 10601 | |
| 10602 | if(! opj_alloc_tile_component_data(l_tilec)) { |
| 10603 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); |
| 10604 | return OPJ_FALSE; |
| 10605 | } |
| 10606 | } |
| 10607 | |
| 10608 | /* now copy data into the the tile component */ |
| 10609 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { |
| 10610 | opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); |
| 10611 | return OPJ_FALSE; |
| 10612 | } |
| 10613 | if (! opj_j2k_post_write_tile(p_j2k,p_stream,p_manager)) { |
Svet Ganov | ee451cb | 2014-05-21 09:38:09 -0700 | [diff] [blame] | 10614 | opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); |
| 10615 | return OPJ_FALSE; |
| 10616 | } |
| 10617 | } |
| 10618 | |
| 10619 | return OPJ_TRUE; |
| 10620 | } |