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 | * Copyright (c) 2017, IntoPIX SA <support@intopix.com> |
20 | * All rights reserved. |
21 | * |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions |
24 | * are met: |
25 | * 1. Redistributions of source code must retain the above copyright |
26 | * notice, this list of conditions and the following disclaimer. |
27 | * 2. Redistributions in binary form must reproduce the above copyright |
28 | * notice, this list of conditions and the following disclaimer in the |
29 | * documentation and/or other materials provided with the distribution. |
30 | * |
31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
32 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
35 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
36 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
37 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
38 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
39 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
41 | * POSSIBILITY OF SUCH DAMAGE. |
42 | */ |
43 | |
44 | #include "opj_includes.h" |
45 | |
46 | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ |
47 | /*@{*/ |
48 | |
49 | /** @name Local static functions */ |
50 | /*@{*/ |
51 | |
52 | /** |
53 | * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures. |
54 | */ |
55 | static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, |
56 | opj_event_mgr_t * p_manager); |
57 | |
58 | /** |
59 | * The read header procedure. |
60 | */ |
61 | static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, |
62 | opj_stream_private_t *p_stream, |
63 | opj_event_mgr_t * p_manager); |
64 | |
65 | /** |
66 | * The default encoding validation procedure without any extension. |
67 | * |
68 | * @param p_j2k the jpeg2000 codec to validate. |
69 | * @param p_stream the input stream to validate. |
70 | * @param p_manager the user event manager. |
71 | * |
72 | * @return true if the parameters are correct. |
73 | */ |
74 | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
75 | opj_stream_private_t *p_stream, |
76 | opj_event_mgr_t * p_manager); |
77 | |
78 | /** |
79 | * The default decoding validation procedure without any extension. |
80 | * |
81 | * @param p_j2k the jpeg2000 codec to validate. |
82 | * @param p_stream the input stream to validate. |
83 | * @param p_manager the user event manager. |
84 | * |
85 | * @return true if the parameters are correct. |
86 | */ |
87 | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k, |
88 | opj_stream_private_t *p_stream, |
89 | opj_event_mgr_t * p_manager); |
90 | |
91 | /** |
92 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
93 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
94 | */ |
95 | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
96 | opj_event_mgr_t * p_manager); |
97 | |
98 | /** |
99 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
100 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
101 | */ |
102 | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
103 | opj_event_mgr_t * p_manager); |
104 | |
105 | /** |
106 | * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters |
107 | * are valid. Developpers wanting to extend the library can add their own validation procedures. |
108 | */ |
109 | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
110 | opj_event_mgr_t * p_manager); |
111 | |
112 | /** |
113 | * The mct encoding validation procedure. |
114 | * |
115 | * @param p_j2k the jpeg2000 codec to validate. |
116 | * @param p_stream the input stream to validate. |
117 | * @param p_manager the user event manager. |
118 | * |
119 | * @return true if the parameters are correct. |
120 | */ |
121 | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
122 | opj_stream_private_t *p_stream, |
123 | opj_event_mgr_t * p_manager); |
124 | |
125 | /** |
126 | * Builds the tcd decoder to use to decode tile. |
127 | */ |
128 | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
129 | opj_stream_private_t *p_stream, |
130 | opj_event_mgr_t * p_manager); |
131 | /** |
132 | * Builds the tcd encoder to use to encode tile. |
133 | */ |
134 | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
135 | opj_stream_private_t *p_stream, |
136 | opj_event_mgr_t * p_manager); |
137 | |
138 | /** |
139 | * Creates a tile-coder encoder. |
140 | * |
141 | * @param p_stream the stream to write data to. |
142 | * @param p_j2k J2K codec. |
143 | * @param p_manager the user event manager. |
144 | */ |
145 | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
146 | opj_stream_private_t *p_stream, |
147 | opj_event_mgr_t * p_manager); |
148 | |
149 | /** |
150 | * Excutes the given procedures on the given codec. |
151 | * |
152 | * @param p_procedure_list the list of procedures to execute |
153 | * @param p_j2k the jpeg2000 codec to execute the procedures on. |
154 | * @param p_stream the stream to execute the procedures on. |
155 | * @param p_manager the user manager. |
156 | * |
157 | * @return true if all the procedures were successfully executed. |
158 | */ |
159 | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
160 | opj_procedure_list_t * p_procedure_list, |
161 | opj_stream_private_t *p_stream, |
162 | opj_event_mgr_t * p_manager); |
163 | |
164 | /** |
165 | * Updates the rates of the tcp. |
166 | * |
167 | * @param p_stream the stream to write data to. |
168 | * @param p_j2k J2K codec. |
169 | * @param p_manager the user event manager. |
170 | */ |
171 | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
172 | opj_stream_private_t *p_stream, |
173 | opj_event_mgr_t * p_manager); |
174 | |
175 | /** |
176 | * Copies the decoding tile parameters onto all the tile parameters. |
177 | * Creates also the tile decoder. |
178 | */ |
179 | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
180 | opj_stream_private_t *p_stream, |
181 | opj_event_mgr_t * p_manager); |
182 | |
183 | /** |
184 | * Destroys the memory associated with the decoding of headers. |
185 | */ |
186 | static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, |
187 | opj_stream_private_t *p_stream, |
188 | opj_event_mgr_t * p_manager); |
189 | |
190 | /** |
191 | * Reads the lookup table containing all the marker, status and action, and returns the handler associated |
192 | * with the marker value. |
193 | * @param p_id Marker value to look up |
194 | * |
195 | * @return the handler associated with the id. |
196 | */ |
197 | static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler( |
198 | OPJ_UINT32 p_id); |
199 | |
200 | /** |
201 | * Destroys a tile coding parameter structure. |
202 | * |
203 | * @param p_tcp the tile coding parameter to destroy. |
204 | */ |
205 | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp); |
206 | |
207 | /** |
208 | * Destroys the data inside a tile coding parameter structure. |
209 | * |
210 | * @param p_tcp the tile coding parameter which contain data to destroy. |
211 | */ |
212 | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp); |
213 | |
214 | /** |
215 | * Destroys a coding parameter structure. |
216 | * |
217 | * @param p_cp the coding parameter to destroy. |
218 | */ |
219 | static void opj_j2k_cp_destroy(opj_cp_t *p_cp); |
220 | |
221 | /** |
222 | * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile. |
223 | * |
224 | * @param p_j2k J2K codec. |
225 | * @param p_tile_no Tile number |
226 | * @param p_first_comp_no The 1st component number to compare. |
227 | * @param p_second_comp_no The 1st component number to compare. |
228 | * |
229 | * @return OPJ_TRUE if SPCdod are equals. |
230 | */ |
231 | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
232 | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
233 | |
234 | /** |
235 | * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
236 | * |
237 | * @param p_j2k J2K codec. |
238 | * @param p_tile_no FIXME DOC |
239 | * @param p_comp_no the component number to output. |
240 | * @param p_data FIXME DOC |
241 | * @param p_header_size FIXME DOC |
242 | * @param p_manager the user event manager. |
243 | * |
244 | * @return FIXME DOC |
245 | */ |
246 | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
247 | OPJ_UINT32 p_tile_no, |
248 | OPJ_UINT32 p_comp_no, |
249 | OPJ_BYTE * p_data, |
250 | OPJ_UINT32 * , |
251 | opj_event_mgr_t * p_manager); |
252 | |
253 | /** |
254 | * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. |
255 | * |
256 | * @param p_j2k the J2K codec. |
257 | * @param p_tile_no the tile index. |
258 | * @param p_comp_no the component being outputted. |
259 | * |
260 | * @return the number of bytes taken by the SPCod element. |
261 | */ |
262 | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
263 | OPJ_UINT32 p_tile_no, |
264 | OPJ_UINT32 p_comp_no); |
265 | |
266 | /** |
267 | * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. |
268 | * @param p_j2k the jpeg2000 codec. |
269 | * @param compno FIXME DOC |
270 | * @param p_header_data the data contained in the COM box. |
271 | * @param p_header_size the size of the data contained in the COM marker. |
272 | * @param p_manager the user event manager. |
273 | */ |
274 | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
275 | OPJ_UINT32 compno, |
276 | OPJ_BYTE * , |
277 | OPJ_UINT32 * , |
278 | opj_event_mgr_t * p_manager); |
279 | |
280 | /** |
281 | * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
282 | * |
283 | * @param p_tile_no the tile index. |
284 | * @param p_comp_no the component being outputted. |
285 | * @param p_j2k the J2K codec. |
286 | * |
287 | * @return the number of bytes taken by the SPCod element. |
288 | */ |
289 | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
290 | OPJ_UINT32 p_tile_no, |
291 | OPJ_UINT32 p_comp_no); |
292 | |
293 | /** |
294 | * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
295 | * |
296 | * @param p_j2k J2K codec. |
297 | * @param p_tile_no the tile to output. |
298 | * @param p_first_comp_no the first component number to compare. |
299 | * @param p_second_comp_no the second component number to compare. |
300 | * |
301 | * @return OPJ_TRUE if equals. |
302 | */ |
303 | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
304 | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
305 | |
306 | |
307 | /** |
308 | * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
309 | * |
310 | * @param p_tile_no the tile to output. |
311 | * @param p_comp_no the component number to output. |
312 | * @param p_data the data buffer. |
313 | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
314 | * @param p_j2k J2K codec. |
315 | * @param p_manager the user event manager. |
316 | * |
317 | */ |
318 | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
319 | OPJ_UINT32 p_tile_no, |
320 | OPJ_UINT32 p_comp_no, |
321 | OPJ_BYTE * p_data, |
322 | OPJ_UINT32 * , |
323 | opj_event_mgr_t * p_manager); |
324 | |
325 | /** |
326 | * Updates the Tile Length Marker. |
327 | */ |
328 | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size); |
329 | |
330 | /** |
331 | * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. |
332 | * |
333 | * @param p_j2k J2K codec. |
334 | * @param compno the component number to output. |
335 | * @param p_header_data the data buffer. |
336 | * @param p_header_size pointer to the size of the data buffer, it is changed by the function. |
337 | * @param p_manager the user event manager. |
338 | * |
339 | */ |
340 | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
341 | OPJ_UINT32 compno, |
342 | OPJ_BYTE * , |
343 | OPJ_UINT32 * , |
344 | opj_event_mgr_t * p_manager); |
345 | |
346 | /** |
347 | * Copies the tile component parameters of all the component from the first tile component. |
348 | * |
349 | * @param p_j2k the J2k codec. |
350 | */ |
351 | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k); |
352 | |
353 | /** |
354 | * Copies the tile quantization parameters of all the component from the first tile component. |
355 | * |
356 | * @param p_j2k the J2k codec. |
357 | */ |
358 | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k); |
359 | |
360 | /** |
361 | * Reads the tiles. |
362 | */ |
363 | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
364 | opj_stream_private_t *p_stream, |
365 | opj_event_mgr_t * p_manager); |
366 | |
367 | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
368 | OPJ_UINT32 p_tile_index, |
369 | opj_stream_private_t *p_stream, |
370 | opj_event_mgr_t * p_manager); |
371 | |
372 | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
373 | opj_image_t* p_output_image); |
374 | |
375 | static void opj_get_tile_dimensions(opj_image_t * l_image, |
376 | opj_tcd_tilecomp_t * l_tilec, |
377 | opj_image_comp_t * l_img_comp, |
378 | OPJ_UINT32* l_size_comp, |
379 | OPJ_UINT32* l_width, |
380 | OPJ_UINT32* l_height, |
381 | OPJ_UINT32* l_offset_x, |
382 | OPJ_UINT32* l_offset_y, |
383 | OPJ_UINT32* l_image_width, |
384 | OPJ_UINT32* l_stride, |
385 | OPJ_UINT32* l_tile_offset); |
386 | |
387 | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data); |
388 | |
389 | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
390 | opj_stream_private_t *p_stream, |
391 | opj_event_mgr_t * p_manager); |
392 | |
393 | /** |
394 | * Sets up the procedures to do on writing header. |
395 | * Developers wanting to extend the library can add their own writing procedures. |
396 | */ |
397 | static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, |
398 | opj_event_mgr_t * p_manager); |
399 | |
400 | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
401 | OPJ_BYTE * p_data, |
402 | OPJ_UINT32 * p_data_written, |
403 | OPJ_UINT32 p_total_data_size, |
404 | opj_stream_private_t *p_stream, |
405 | struct opj_event_mgr * p_manager); |
406 | |
407 | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
408 | OPJ_BYTE * p_data, |
409 | OPJ_UINT32 * p_data_written, |
410 | OPJ_UINT32 p_total_data_size, |
411 | opj_stream_private_t *p_stream, |
412 | struct opj_event_mgr * p_manager); |
413 | |
414 | /** |
415 | * Gets the offset of the header. |
416 | * |
417 | * @param p_stream the stream to write data to. |
418 | * @param p_j2k J2K codec. |
419 | * @param p_manager the user event manager. |
420 | */ |
421 | static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, |
422 | opj_stream_private_t *p_stream, |
423 | opj_event_mgr_t * p_manager); |
424 | |
425 | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); |
426 | |
427 | /* |
428 | * ----------------------------------------------------------------------- |
429 | * ----------------------------------------------------------------------- |
430 | * ----------------------------------------------------------------------- |
431 | */ |
432 | |
433 | /** |
434 | * Writes the SOC marker (Start Of Codestream) |
435 | * |
436 | * @param p_stream the stream to write data to. |
437 | * @param p_j2k J2K codec. |
438 | * @param p_manager the user event manager. |
439 | */ |
440 | static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, |
441 | opj_stream_private_t *p_stream, |
442 | opj_event_mgr_t * p_manager); |
443 | |
444 | /** |
445 | * Reads a SOC marker (Start of Codestream) |
446 | * @param p_j2k the jpeg2000 file codec. |
447 | * @param p_stream XXX needs data |
448 | * @param p_manager the user event manager. |
449 | */ |
450 | static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, |
451 | opj_stream_private_t *p_stream, |
452 | opj_event_mgr_t * p_manager); |
453 | |
454 | /** |
455 | * Writes the SIZ marker (image and tile size) |
456 | * |
457 | * @param p_j2k J2K codec. |
458 | * @param p_stream the stream to write data to. |
459 | * @param p_manager the user event manager. |
460 | */ |
461 | static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, |
462 | opj_stream_private_t *p_stream, |
463 | opj_event_mgr_t * p_manager); |
464 | |
465 | /** |
466 | * Reads a SIZ marker (image and tile size) |
467 | * @param p_j2k the jpeg2000 file codec. |
468 | * @param p_header_data the data contained in the SIZ box. |
469 | * @param p_header_size the size of the data contained in the SIZ marker. |
470 | * @param p_manager the user event manager. |
471 | */ |
472 | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
473 | OPJ_BYTE * , |
474 | OPJ_UINT32 , |
475 | opj_event_mgr_t * p_manager); |
476 | |
477 | /** |
478 | * Writes the COM marker (comment) |
479 | * |
480 | * @param p_stream the stream to write data to. |
481 | * @param p_j2k J2K codec. |
482 | * @param p_manager the user event manager. |
483 | */ |
484 | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
485 | opj_stream_private_t *p_stream, |
486 | opj_event_mgr_t * p_manager); |
487 | |
488 | /** |
489 | * Reads a COM marker (comments) |
490 | * @param p_j2k the jpeg2000 file codec. |
491 | * @param p_header_data the data contained in the COM box. |
492 | * @param p_header_size the size of the data contained in the COM marker. |
493 | * @param p_manager the user event manager. |
494 | */ |
495 | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
496 | OPJ_BYTE * , |
497 | OPJ_UINT32 , |
498 | opj_event_mgr_t * p_manager); |
499 | /** |
500 | * Writes the COD marker (Coding style default) |
501 | * |
502 | * @param p_stream the stream to write data to. |
503 | * @param p_j2k J2K codec. |
504 | * @param p_manager the user event manager. |
505 | */ |
506 | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
507 | opj_stream_private_t *p_stream, |
508 | opj_event_mgr_t * p_manager); |
509 | |
510 | /** |
511 | * Reads a COD marker (Coding Styke defaults) |
512 | * @param p_header_data the data contained in the COD box. |
513 | * @param p_j2k the jpeg2000 codec. |
514 | * @param p_header_size the size of the data contained in the COD marker. |
515 | * @param p_manager the user event manager. |
516 | */ |
517 | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
518 | OPJ_BYTE * , |
519 | OPJ_UINT32 , |
520 | opj_event_mgr_t * p_manager); |
521 | |
522 | /** |
523 | * Compares 2 COC markers (Coding style component) |
524 | * |
525 | * @param p_j2k J2K codec. |
526 | * @param p_first_comp_no the index of the first component to compare. |
527 | * @param p_second_comp_no the index of the second component to compare. |
528 | * |
529 | * @return OPJ_TRUE if equals |
530 | */ |
531 | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
532 | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
533 | |
534 | /** |
535 | * Writes the COC marker (Coding style component) |
536 | * |
537 | * @param p_j2k J2K codec. |
538 | * @param p_comp_no the index of the component to output. |
539 | * @param p_stream the stream to write data to. |
540 | * @param p_manager the user event manager. |
541 | */ |
542 | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
543 | OPJ_UINT32 p_comp_no, |
544 | opj_stream_private_t *p_stream, |
545 | opj_event_mgr_t * p_manager); |
546 | |
547 | /** |
548 | * Writes the COC marker (Coding style component) |
549 | * |
550 | * @param p_j2k J2K codec. |
551 | * @param p_comp_no the index of the component to output. |
552 | * @param p_data FIXME DOC |
553 | * @param p_data_written FIXME DOC |
554 | * @param p_manager the user event manager. |
555 | */ |
556 | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
557 | OPJ_UINT32 p_comp_no, |
558 | OPJ_BYTE * p_data, |
559 | OPJ_UINT32 * p_data_written, |
560 | opj_event_mgr_t * p_manager); |
561 | |
562 | /** |
563 | * Gets the maximum size taken by a coc. |
564 | * |
565 | * @param p_j2k the jpeg2000 codec to use. |
566 | */ |
567 | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); |
568 | |
569 | /** |
570 | * Reads a COC marker (Coding Style Component) |
571 | * @param p_header_data the data contained in the COC box. |
572 | * @param p_j2k the jpeg2000 codec. |
573 | * @param p_header_size the size of the data contained in the COC marker. |
574 | * @param p_manager the user event manager. |
575 | */ |
576 | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
577 | OPJ_BYTE * , |
578 | OPJ_UINT32 , |
579 | opj_event_mgr_t * p_manager); |
580 | |
581 | /** |
582 | * Writes the QCD marker (quantization default) |
583 | * |
584 | * @param p_j2k J2K codec. |
585 | * @param p_stream the stream to write data to. |
586 | * @param p_manager the user event manager. |
587 | */ |
588 | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
589 | opj_stream_private_t *p_stream, |
590 | opj_event_mgr_t * p_manager); |
591 | |
592 | /** |
593 | * Reads a QCD marker (Quantization defaults) |
594 | * @param p_header_data the data contained in the QCD box. |
595 | * @param p_j2k the jpeg2000 codec. |
596 | * @param p_header_size the size of the data contained in the QCD marker. |
597 | * @param p_manager the user event manager. |
598 | */ |
599 | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
600 | OPJ_BYTE * , |
601 | OPJ_UINT32 , |
602 | opj_event_mgr_t * p_manager); |
603 | |
604 | /** |
605 | * Compare QCC markers (quantization component) |
606 | * |
607 | * @param p_j2k J2K codec. |
608 | * @param p_first_comp_no the index of the first component to compare. |
609 | * @param p_second_comp_no the index of the second component to compare. |
610 | * |
611 | * @return OPJ_TRUE if equals. |
612 | */ |
613 | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
614 | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); |
615 | |
616 | /** |
617 | * Writes the QCC marker (quantization component) |
618 | * |
619 | * @param p_comp_no the index of the component to output. |
620 | * @param p_stream the stream to write data to. |
621 | * @param p_j2k J2K codec. |
622 | * @param p_manager the user event manager. |
623 | */ |
624 | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
625 | OPJ_UINT32 p_comp_no, |
626 | opj_stream_private_t *p_stream, |
627 | opj_event_mgr_t * p_manager); |
628 | |
629 | /** |
630 | * Writes the QCC marker (quantization component) |
631 | * |
632 | * @param p_j2k J2K codec. |
633 | * @param p_comp_no the index of the component to output. |
634 | * @param p_data FIXME DOC |
635 | * @param p_data_written the stream to write data to. |
636 | * @param p_manager the user event manager. |
637 | */ |
638 | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
639 | OPJ_UINT32 p_comp_no, |
640 | OPJ_BYTE * p_data, |
641 | OPJ_UINT32 * p_data_written, |
642 | opj_event_mgr_t * p_manager); |
643 | |
644 | /** |
645 | * Gets the maximum size taken by a qcc. |
646 | */ |
647 | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k); |
648 | |
649 | /** |
650 | * Reads a QCC marker (Quantization component) |
651 | * @param p_header_data the data contained in the QCC box. |
652 | * @param p_j2k the jpeg2000 codec. |
653 | * @param p_header_size the size of the data contained in the QCC marker. |
654 | * @param p_manager the user event manager. |
655 | */ |
656 | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
657 | OPJ_BYTE * , |
658 | OPJ_UINT32 , |
659 | opj_event_mgr_t * p_manager); |
660 | /** |
661 | * Writes the POC marker (Progression Order Change) |
662 | * |
663 | * @param p_stream the stream to write data to. |
664 | * @param p_j2k J2K codec. |
665 | * @param p_manager the user event manager. |
666 | */ |
667 | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
668 | opj_stream_private_t *p_stream, |
669 | opj_event_mgr_t * p_manager); |
670 | /** |
671 | * Writes the POC marker (Progression Order Change) |
672 | * |
673 | * @param p_j2k J2K codec. |
674 | * @param p_data FIXME DOC |
675 | * @param p_data_written the stream to write data to. |
676 | * @param p_manager the user event manager. |
677 | */ |
678 | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
679 | OPJ_BYTE * p_data, |
680 | OPJ_UINT32 * p_data_written, |
681 | opj_event_mgr_t * p_manager); |
682 | /** |
683 | * Gets the maximum size taken by the writing of a POC. |
684 | */ |
685 | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); |
686 | |
687 | /** |
688 | * Reads a POC marker (Progression Order Change) |
689 | * |
690 | * @param p_header_data the data contained in the POC box. |
691 | * @param p_j2k the jpeg2000 codec. |
692 | * @param p_header_size the size of the data contained in the POC marker. |
693 | * @param p_manager the user event manager. |
694 | */ |
695 | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
696 | OPJ_BYTE * , |
697 | OPJ_UINT32 , |
698 | opj_event_mgr_t * p_manager); |
699 | |
700 | /** |
701 | * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. |
702 | */ |
703 | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k); |
704 | |
705 | /** |
706 | * Gets the maximum size taken by the headers of the SOT. |
707 | * |
708 | * @param p_j2k the jpeg2000 codec to use. |
709 | */ |
710 | static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); |
711 | |
712 | /** |
713 | * Reads a CRG marker (Component registration) |
714 | * |
715 | * @param p_header_data the data contained in the TLM box. |
716 | * @param p_j2k the jpeg2000 codec. |
717 | * @param p_header_size the size of the data contained in the TLM marker. |
718 | * @param p_manager the user event manager. |
719 | */ |
720 | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
721 | OPJ_BYTE * , |
722 | OPJ_UINT32 , |
723 | opj_event_mgr_t * p_manager); |
724 | /** |
725 | * Reads a TLM marker (Tile Length Marker) |
726 | * |
727 | * @param p_header_data the data contained in the TLM box. |
728 | * @param p_j2k the jpeg2000 codec. |
729 | * @param p_header_size the size of the data contained in the TLM marker. |
730 | * @param p_manager the user event manager. |
731 | */ |
732 | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
733 | OPJ_BYTE * , |
734 | OPJ_UINT32 , |
735 | opj_event_mgr_t * p_manager); |
736 | |
737 | /** |
738 | * Writes the updated tlm. |
739 | * |
740 | * @param p_stream the stream to write data to. |
741 | * @param p_j2k J2K codec. |
742 | * @param p_manager the user event manager. |
743 | */ |
744 | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
745 | opj_stream_private_t *p_stream, |
746 | opj_event_mgr_t * p_manager); |
747 | |
748 | /** |
749 | * Reads a PLM marker (Packet length, main header marker) |
750 | * |
751 | * @param p_header_data the data contained in the TLM box. |
752 | * @param p_j2k the jpeg2000 codec. |
753 | * @param p_header_size the size of the data contained in the TLM marker. |
754 | * @param p_manager the user event manager. |
755 | */ |
756 | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
757 | OPJ_BYTE * , |
758 | OPJ_UINT32 , |
759 | opj_event_mgr_t * p_manager); |
760 | /** |
761 | * Reads a PLT marker (Packet length, tile-part header) |
762 | * |
763 | * @param p_header_data the data contained in the PLT box. |
764 | * @param p_j2k the jpeg2000 codec. |
765 | * @param p_header_size the size of the data contained in the PLT marker. |
766 | * @param p_manager the user event manager. |
767 | */ |
768 | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
769 | OPJ_BYTE * , |
770 | OPJ_UINT32 , |
771 | opj_event_mgr_t * p_manager); |
772 | |
773 | /** |
774 | * Reads a PPM marker (Packed headers, main header) |
775 | * |
776 | * @param p_header_data the data contained in the POC box. |
777 | * @param p_j2k the jpeg2000 codec. |
778 | * @param p_header_size the size of the data contained in the POC marker. |
779 | * @param p_manager the user event manager. |
780 | */ |
781 | |
782 | static OPJ_BOOL opj_j2k_read_ppm( |
783 | opj_j2k_t *p_j2k, |
784 | OPJ_BYTE * , |
785 | OPJ_UINT32 , |
786 | opj_event_mgr_t * p_manager); |
787 | |
788 | /** |
789 | * Merges all PPM markers read (Packed headers, main header) |
790 | * |
791 | * @param p_cp main coding parameters. |
792 | * @param p_manager the user event manager. |
793 | */ |
794 | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager); |
795 | |
796 | /** |
797 | * Reads a PPT marker (Packed packet headers, tile-part header) |
798 | * |
799 | * @param p_header_data the data contained in the PPT box. |
800 | * @param p_j2k the jpeg2000 codec. |
801 | * @param p_header_size the size of the data contained in the PPT marker. |
802 | * @param p_manager the user event manager. |
803 | */ |
804 | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
805 | OPJ_BYTE * , |
806 | OPJ_UINT32 , |
807 | opj_event_mgr_t * p_manager); |
808 | |
809 | /** |
810 | * Merges all PPT markers read (Packed headers, tile-part header) |
811 | * |
812 | * @param p_tcp the tile. |
813 | * @param p_manager the user event manager. |
814 | */ |
815 | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, |
816 | opj_event_mgr_t * p_manager); |
817 | |
818 | |
819 | /** |
820 | * Writes the TLM marker (Tile Length Marker) |
821 | * |
822 | * @param p_stream the stream to write data to. |
823 | * @param p_j2k J2K codec. |
824 | * @param p_manager the user event manager. |
825 | */ |
826 | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
827 | opj_stream_private_t *p_stream, |
828 | opj_event_mgr_t * p_manager); |
829 | |
830 | /** |
831 | * Writes the SOT marker (Start of tile-part) |
832 | * |
833 | * @param p_j2k J2K codec. |
834 | * @param p_data Output buffer |
835 | * @param p_total_data_size Output buffer size |
836 | * @param p_data_written Number of bytes written into stream |
837 | * @param p_stream the stream to write data to. |
838 | * @param p_manager the user event manager. |
839 | */ |
840 | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
841 | OPJ_BYTE * p_data, |
842 | OPJ_UINT32 p_total_data_size, |
843 | OPJ_UINT32 * p_data_written, |
844 | const opj_stream_private_t *p_stream, |
845 | opj_event_mgr_t * p_manager); |
846 | |
847 | /** |
848 | * Reads values from a SOT marker (Start of tile-part) |
849 | * |
850 | * the j2k decoder state is not affected. No side effects, no checks except for p_header_size. |
851 | * |
852 | * @param p_header_data the data contained in the SOT marker. |
853 | * @param p_header_size the size of the data contained in the SOT marker. |
854 | * @param p_tile_no Isot. |
855 | * @param p_tot_len Psot. |
856 | * @param p_current_part TPsot. |
857 | * @param p_num_parts TNsot. |
858 | * @param p_manager the user event manager. |
859 | */ |
860 | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * , |
861 | OPJ_UINT32 , |
862 | OPJ_UINT32* p_tile_no, |
863 | OPJ_UINT32* p_tot_len, |
864 | OPJ_UINT32* p_current_part, |
865 | OPJ_UINT32* p_num_parts, |
866 | opj_event_mgr_t * p_manager); |
867 | /** |
868 | * Reads a SOT marker (Start of tile-part) |
869 | * |
870 | * @param p_header_data the data contained in the SOT marker. |
871 | * @param p_j2k the jpeg2000 codec. |
872 | * @param p_header_size the size of the data contained in the PPT marker. |
873 | * @param p_manager the user event manager. |
874 | */ |
875 | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
876 | OPJ_BYTE * , |
877 | OPJ_UINT32 , |
878 | opj_event_mgr_t * p_manager); |
879 | /** |
880 | * Writes the SOD marker (Start of data) |
881 | * |
882 | * @param p_j2k J2K codec. |
883 | * @param p_tile_coder FIXME DOC |
884 | * @param p_data FIXME DOC |
885 | * @param p_data_written FIXME DOC |
886 | * @param p_total_data_size FIXME DOC |
887 | * @param p_stream the stream to write data to. |
888 | * @param p_manager the user event manager. |
889 | */ |
890 | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
891 | opj_tcd_t * p_tile_coder, |
892 | OPJ_BYTE * p_data, |
893 | OPJ_UINT32 * p_data_written, |
894 | OPJ_UINT32 p_total_data_size, |
895 | const opj_stream_private_t *p_stream, |
896 | opj_event_mgr_t * p_manager); |
897 | |
898 | /** |
899 | * Reads a SOD marker (Start Of Data) |
900 | * |
901 | * @param p_j2k the jpeg2000 codec. |
902 | * @param p_stream FIXME DOC |
903 | * @param p_manager the user event manager. |
904 | */ |
905 | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
906 | opj_stream_private_t *p_stream, |
907 | opj_event_mgr_t * p_manager); |
908 | |
909 | static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size) |
910 | { |
911 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
912 | p_j2k->m_current_tile_number, 1); /* PSOT */ |
913 | ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; |
914 | |
915 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, |
916 | p_tile_part_size, 4); /* PSOT */ |
917 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; |
918 | } |
919 | |
920 | /** |
921 | * Writes the RGN marker (Region Of Interest) |
922 | * |
923 | * @param p_tile_no the tile to output |
924 | * @param p_comp_no the component to output |
925 | * @param nb_comps the number of components |
926 | * @param p_stream the stream to write data to. |
927 | * @param p_j2k J2K codec. |
928 | * @param p_manager the user event manager. |
929 | */ |
930 | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
931 | OPJ_UINT32 p_tile_no, |
932 | OPJ_UINT32 p_comp_no, |
933 | OPJ_UINT32 nb_comps, |
934 | opj_stream_private_t *p_stream, |
935 | opj_event_mgr_t * p_manager); |
936 | |
937 | /** |
938 | * Reads a RGN marker (Region Of Interest) |
939 | * |
940 | * @param p_header_data the data contained in the POC box. |
941 | * @param p_j2k the jpeg2000 codec. |
942 | * @param p_header_size the size of the data contained in the POC marker. |
943 | * @param p_manager the user event manager. |
944 | */ |
945 | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
946 | OPJ_BYTE * , |
947 | OPJ_UINT32 , |
948 | opj_event_mgr_t * p_manager); |
949 | |
950 | /** |
951 | * Writes the EOC marker (End of Codestream) |
952 | * |
953 | * @param p_stream the stream to write data to. |
954 | * @param p_j2k J2K codec. |
955 | * @param p_manager the user event manager. |
956 | */ |
957 | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
958 | opj_stream_private_t *p_stream, |
959 | opj_event_mgr_t * p_manager); |
960 | |
961 | #if 0 |
962 | /** |
963 | * Reads a EOC marker (End Of Codestream) |
964 | * |
965 | * @param p_j2k the jpeg2000 codec. |
966 | * @param p_stream FIXME DOC |
967 | * @param p_manager the user event manager. |
968 | */ |
969 | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
970 | opj_stream_private_t *p_stream, |
971 | opj_event_mgr_t * p_manager); |
972 | #endif |
973 | |
974 | /** |
975 | * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) |
976 | * |
977 | * @param p_stream the stream to write data to. |
978 | * @param p_j2k J2K codec. |
979 | * @param p_manager the user event manager. |
980 | */ |
981 | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
982 | opj_stream_private_t *p_stream, |
983 | opj_event_mgr_t * p_manager); |
984 | |
985 | /** |
986 | * Inits the Info |
987 | * |
988 | * @param p_stream the stream to write data to. |
989 | * @param p_j2k J2K codec. |
990 | * @param p_manager the user event manager. |
991 | */ |
992 | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
993 | opj_stream_private_t *p_stream, |
994 | opj_event_mgr_t * p_manager); |
995 | |
996 | /** |
997 | Add main header marker information |
998 | @param cstr_index Codestream information structure |
999 | @param type marker type |
1000 | @param pos byte offset of marker segment |
1001 | @param len length of marker segment |
1002 | */ |
1003 | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
1004 | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; |
1005 | /** |
1006 | Add tile header marker information |
1007 | @param tileno tile index number |
1008 | @param cstr_index Codestream information structure |
1009 | @param type marker type |
1010 | @param pos byte offset of marker segment |
1011 | @param len length of marker segment |
1012 | */ |
1013 | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
1014 | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
1015 | OPJ_UINT32 len); |
1016 | |
1017 | /** |
1018 | * Reads an unknown marker |
1019 | * |
1020 | * @param p_j2k the jpeg2000 codec. |
1021 | * @param p_stream the stream object to read from. |
1022 | * @param output_marker FIXME DOC |
1023 | * @param p_manager the user event manager. |
1024 | * |
1025 | * @return true if the marker could be deduced. |
1026 | */ |
1027 | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
1028 | opj_stream_private_t *p_stream, |
1029 | OPJ_UINT32 *output_marker, |
1030 | opj_event_mgr_t * p_manager); |
1031 | |
1032 | /** |
1033 | * Writes the MCT marker (Multiple Component Transform) |
1034 | * |
1035 | * @param p_j2k J2K codec. |
1036 | * @param p_mct_record FIXME DOC |
1037 | * @param p_stream the stream to write data to. |
1038 | * @param p_manager the user event manager. |
1039 | */ |
1040 | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
1041 | opj_mct_data_t * p_mct_record, |
1042 | opj_stream_private_t *p_stream, |
1043 | opj_event_mgr_t * p_manager); |
1044 | |
1045 | /** |
1046 | * Reads a MCT marker (Multiple Component Transform) |
1047 | * |
1048 | * @param p_header_data the data contained in the MCT box. |
1049 | * @param p_j2k the jpeg2000 codec. |
1050 | * @param p_header_size the size of the data contained in the MCT marker. |
1051 | * @param p_manager the user event manager. |
1052 | */ |
1053 | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
1054 | OPJ_BYTE * , |
1055 | OPJ_UINT32 , |
1056 | opj_event_mgr_t * p_manager); |
1057 | |
1058 | /** |
1059 | * Writes the MCC marker (Multiple Component Collection) |
1060 | * |
1061 | * @param p_j2k J2K codec. |
1062 | * @param p_mcc_record FIXME DOC |
1063 | * @param p_stream the stream to write data to. |
1064 | * @param p_manager the user event manager. |
1065 | */ |
1066 | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
1067 | opj_simple_mcc_decorrelation_data_t * p_mcc_record, |
1068 | opj_stream_private_t *p_stream, |
1069 | opj_event_mgr_t * p_manager); |
1070 | |
1071 | /** |
1072 | * Reads a MCC marker (Multiple Component Collection) |
1073 | * |
1074 | * @param p_header_data the data contained in the MCC box. |
1075 | * @param p_j2k the jpeg2000 codec. |
1076 | * @param p_header_size the size of the data contained in the MCC marker. |
1077 | * @param p_manager the user event manager. |
1078 | */ |
1079 | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
1080 | OPJ_BYTE * , |
1081 | OPJ_UINT32 , |
1082 | opj_event_mgr_t * p_manager); |
1083 | |
1084 | /** |
1085 | * Writes the MCO marker (Multiple component transformation ordering) |
1086 | * |
1087 | * @param p_stream the stream to write data to. |
1088 | * @param p_j2k J2K codec. |
1089 | * @param p_manager the user event manager. |
1090 | */ |
1091 | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
1092 | opj_stream_private_t *p_stream, |
1093 | opj_event_mgr_t * p_manager); |
1094 | |
1095 | /** |
1096 | * Reads a MCO marker (Multiple Component Transform Ordering) |
1097 | * |
1098 | * @param p_header_data the data contained in the MCO box. |
1099 | * @param p_j2k the jpeg2000 codec. |
1100 | * @param p_header_size the size of the data contained in the MCO marker. |
1101 | * @param p_manager the user event manager. |
1102 | */ |
1103 | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
1104 | OPJ_BYTE * , |
1105 | OPJ_UINT32 , |
1106 | opj_event_mgr_t * p_manager); |
1107 | |
1108 | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
1109 | OPJ_UINT32 p_index); |
1110 | |
1111 | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1112 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1113 | static void opj_j2k_read_int32_to_float(const void * p_src_data, |
1114 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1115 | static void opj_j2k_read_float32_to_float(const void * p_src_data, |
1116 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1117 | static void opj_j2k_read_float64_to_float(const void * p_src_data, |
1118 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1119 | |
1120 | static void opj_j2k_read_int16_to_int32(const void * p_src_data, |
1121 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1122 | static void opj_j2k_read_int32_to_int32(const void * p_src_data, |
1123 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1124 | static void opj_j2k_read_float32_to_int32(const void * p_src_data, |
1125 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1126 | static void opj_j2k_read_float64_to_int32(const void * p_src_data, |
1127 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1128 | |
1129 | static void opj_j2k_write_float_to_int16(const void * p_src_data, |
1130 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1131 | static void opj_j2k_write_float_to_int32(const void * p_src_data, |
1132 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1133 | static void opj_j2k_write_float_to_float(const void * p_src_data, |
1134 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1135 | static void opj_j2k_write_float_to_float64(const void * p_src_data, |
1136 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1137 | |
1138 | /** |
1139 | * Ends the encoding, i.e. frees memory. |
1140 | * |
1141 | * @param p_stream the stream to write data to. |
1142 | * @param p_j2k J2K codec. |
1143 | * @param p_manager the user event manager. |
1144 | */ |
1145 | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
1146 | opj_stream_private_t *p_stream, |
1147 | opj_event_mgr_t * p_manager); |
1148 | |
1149 | /** |
1150 | * Writes the CBD marker (Component bit depth definition) |
1151 | * |
1152 | * @param p_stream the stream to write data to. |
1153 | * @param p_j2k J2K codec. |
1154 | * @param p_manager the user event manager. |
1155 | */ |
1156 | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
1157 | opj_stream_private_t *p_stream, |
1158 | opj_event_mgr_t * p_manager); |
1159 | |
1160 | /** |
1161 | * Reads a CBD marker (Component bit depth definition) |
1162 | * @param p_header_data the data contained in the CBD box. |
1163 | * @param p_j2k the jpeg2000 codec. |
1164 | * @param p_header_size the size of the data contained in the CBD marker. |
1165 | * @param p_manager the user event manager. |
1166 | */ |
1167 | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
1168 | OPJ_BYTE * , |
1169 | OPJ_UINT32 , |
1170 | opj_event_mgr_t * p_manager); |
1171 | |
1172 | |
1173 | /** |
1174 | * Writes COC marker for each component. |
1175 | * |
1176 | * @param p_stream the stream to write data to. |
1177 | * @param p_j2k J2K codec. |
1178 | * @param p_manager the user event manager. |
1179 | */ |
1180 | static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k, |
1181 | opj_stream_private_t *p_stream, |
1182 | opj_event_mgr_t * p_manager); |
1183 | |
1184 | /** |
1185 | * Writes QCC marker for each component. |
1186 | * |
1187 | * @param p_stream the stream to write data to. |
1188 | * @param p_j2k J2K codec. |
1189 | * @param p_manager the user event manager. |
1190 | */ |
1191 | static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, |
1192 | opj_stream_private_t *p_stream, |
1193 | opj_event_mgr_t * p_manager); |
1194 | |
1195 | /** |
1196 | * Writes regions of interests. |
1197 | * |
1198 | * @param p_stream the stream to write data to. |
1199 | * @param p_j2k J2K codec. |
1200 | * @param p_manager the user event manager. |
1201 | */ |
1202 | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
1203 | opj_stream_private_t *p_stream, |
1204 | opj_event_mgr_t * p_manager); |
1205 | |
1206 | /** |
1207 | * Writes EPC ???? |
1208 | * |
1209 | * @param p_stream the stream to write data to. |
1210 | * @param p_j2k J2K codec. |
1211 | * @param p_manager the user event manager. |
1212 | */ |
1213 | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
1214 | opj_stream_private_t *p_stream, |
1215 | opj_event_mgr_t * p_manager); |
1216 | |
1217 | /** |
1218 | * Checks the progression order changes values. Tells of the poc given as input are valid. |
1219 | * A nice message is outputted at errors. |
1220 | * |
1221 | * @param p_pocs the progression order changes. |
1222 | * @param p_nb_pocs the number of progression order changes. |
1223 | * @param p_nb_resolutions the number of resolutions. |
1224 | * @param numcomps the number of components |
1225 | * @param numlayers the number of layers. |
1226 | * @param p_manager the user event manager. |
1227 | * |
1228 | * @return true if the pocs are valid. |
1229 | */ |
1230 | static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, |
1231 | OPJ_UINT32 p_nb_pocs, |
1232 | OPJ_UINT32 p_nb_resolutions, |
1233 | OPJ_UINT32 numcomps, |
1234 | OPJ_UINT32 numlayers, |
1235 | opj_event_mgr_t * p_manager); |
1236 | |
1237 | /** |
1238 | * Gets the number of tile parts used for the given change of progression (if any) and the given tile. |
1239 | * |
1240 | * @param cp the coding parameters. |
1241 | * @param pino the offset of the given poc (i.e. its position in the coding parameter). |
1242 | * @param tileno the given tile. |
1243 | * |
1244 | * @return the number of tile parts. |
1245 | */ |
1246 | static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, |
1247 | OPJ_UINT32 tileno); |
1248 | |
1249 | /** |
1250 | * Calculates the total number of tile parts needed by the encoder to |
1251 | * encode such an image. If not enough memory is available, then the function return false. |
1252 | * |
1253 | * @param p_nb_tiles pointer that will hold the number of tile parts. |
1254 | * @param cp the coding parameters for the image. |
1255 | * @param image the image to encode. |
1256 | * @param p_j2k the p_j2k encoder. |
1257 | * @param p_manager the user event manager. |
1258 | * |
1259 | * @return true if the function was successful, false else. |
1260 | */ |
1261 | static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, |
1262 | opj_cp_t *cp, |
1263 | OPJ_UINT32 * p_nb_tiles, |
1264 | opj_image_t *image, |
1265 | opj_event_mgr_t * p_manager); |
1266 | |
1267 | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); |
1268 | |
1269 | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); |
1270 | |
1271 | static opj_codestream_index_t* opj_j2k_create_cstr_index(void); |
1272 | |
1273 | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp); |
1274 | |
1275 | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp); |
1276 | |
1277 | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres); |
1278 | |
1279 | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
1280 | opj_image_t *image, opj_event_mgr_t *p_manager); |
1281 | |
1282 | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
1283 | opj_event_mgr_t *p_manager); |
1284 | |
1285 | /** |
1286 | * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254. |
1287 | * |
1288 | * @param p_stream the stream to read data from. |
1289 | * @param tile_no tile number we're looking for. |
1290 | * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction. |
1291 | * @param p_manager the user event manager. |
1292 | * |
1293 | * @return true if the function was successful, false else. |
1294 | */ |
1295 | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
1296 | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
1297 | opj_event_mgr_t * p_manager); |
1298 | |
1299 | /*@}*/ |
1300 | |
1301 | /*@}*/ |
1302 | |
1303 | /* ----------------------------------------------------------------------- */ |
1304 | typedef struct j2k_prog_order { |
1305 | OPJ_PROG_ORDER enum_prog; |
1306 | char str_prog[5]; |
1307 | } j2k_prog_order_t; |
1308 | |
1309 | static const j2k_prog_order_t j2k_prog_order_list[] = { |
1310 | {OPJ_CPRL, "CPRL" }, |
1311 | {OPJ_LRCP, "LRCP" }, |
1312 | {OPJ_PCRL, "PCRL" }, |
1313 | {OPJ_RLCP, "RLCP" }, |
1314 | {OPJ_RPCL, "RPCL" }, |
1315 | {(OPJ_PROG_ORDER) - 1, "" } |
1316 | }; |
1317 | |
1318 | /** |
1319 | * FIXME DOC |
1320 | */ |
1321 | static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = { |
1322 | 2, |
1323 | 4, |
1324 | 4, |
1325 | 8 |
1326 | }; |
1327 | |
1328 | typedef void (* opj_j2k_mct_function)(const void * p_src_data, |
1329 | void * p_dest_data, OPJ_UINT32 p_nb_elem); |
1330 | |
1331 | static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = { |
1332 | opj_j2k_read_int16_to_float, |
1333 | opj_j2k_read_int32_to_float, |
1334 | opj_j2k_read_float32_to_float, |
1335 | opj_j2k_read_float64_to_float |
1336 | }; |
1337 | |
1338 | static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = { |
1339 | opj_j2k_read_int16_to_int32, |
1340 | opj_j2k_read_int32_to_int32, |
1341 | opj_j2k_read_float32_to_int32, |
1342 | opj_j2k_read_float64_to_int32 |
1343 | }; |
1344 | |
1345 | static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = { |
1346 | opj_j2k_write_float_to_int16, |
1347 | opj_j2k_write_float_to_int32, |
1348 | opj_j2k_write_float_to_float, |
1349 | opj_j2k_write_float_to_float64 |
1350 | }; |
1351 | |
1352 | typedef struct opj_dec_memory_marker_handler { |
1353 | /** marker value */ |
1354 | OPJ_UINT32 id; |
1355 | /** value of the state when the marker can appear */ |
1356 | OPJ_UINT32 states; |
1357 | /** action linked to the marker */ |
1358 | OPJ_BOOL(*handler)(opj_j2k_t *p_j2k, |
1359 | OPJ_BYTE * , |
1360 | OPJ_UINT32 , |
1361 | opj_event_mgr_t * p_manager); |
1362 | } |
1363 | opj_dec_memory_marker_handler_t; |
1364 | |
1365 | static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = |
1366 | { |
1367 | {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, |
1368 | {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, |
1369 | {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, |
1370 | {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, |
1371 | {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, |
1372 | {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, |
1373 | {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, |
1374 | {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, |
1375 | {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, |
1376 | {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, |
1377 | {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, |
1378 | {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm}, |
1379 | {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, |
1380 | {J2K_MS_SOP, 0, 0}, |
1381 | {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, |
1382 | {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, |
1383 | {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, |
1384 | {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd}, |
1385 | {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, |
1386 | {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, |
1387 | #ifdef USE_JPWL |
1388 | #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */ |
1389 | {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, |
1390 | {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, |
1391 | {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, |
1392 | {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, |
1393 | #endif |
1394 | #endif /* USE_JPWL */ |
1395 | #ifdef USE_JPSEC |
1396 | {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, |
1397 | {J2K_MS_INSEC, 0, j2k_read_insec} |
1398 | #endif /* USE_JPSEC */ |
1399 | {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ |
1400 | }; |
1401 | |
1402 | static void opj_j2k_read_int16_to_float(const void * p_src_data, |
1403 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1404 | { |
1405 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1406 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1407 | OPJ_UINT32 i; |
1408 | OPJ_UINT32 l_temp; |
1409 | |
1410 | for (i = 0; i < p_nb_elem; ++i) { |
1411 | opj_read_bytes(l_src_data, &l_temp, 2); |
1412 | |
1413 | l_src_data += sizeof(OPJ_INT16); |
1414 | |
1415 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1416 | } |
1417 | } |
1418 | |
1419 | static void opj_j2k_read_int32_to_float(const void * p_src_data, |
1420 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1421 | { |
1422 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1423 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1424 | OPJ_UINT32 i; |
1425 | OPJ_UINT32 l_temp; |
1426 | |
1427 | for (i = 0; i < p_nb_elem; ++i) { |
1428 | opj_read_bytes(l_src_data, &l_temp, 4); |
1429 | |
1430 | l_src_data += sizeof(OPJ_INT32); |
1431 | |
1432 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1433 | } |
1434 | } |
1435 | |
1436 | static void opj_j2k_read_float32_to_float(const void * p_src_data, |
1437 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1438 | { |
1439 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1440 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1441 | OPJ_UINT32 i; |
1442 | OPJ_FLOAT32 l_temp; |
1443 | |
1444 | for (i = 0; i < p_nb_elem; ++i) { |
1445 | opj_read_float(l_src_data, &l_temp); |
1446 | |
1447 | l_src_data += sizeof(OPJ_FLOAT32); |
1448 | |
1449 | *(l_dest_data++) = l_temp; |
1450 | } |
1451 | } |
1452 | |
1453 | static void opj_j2k_read_float64_to_float(const void * p_src_data, |
1454 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1455 | { |
1456 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1457 | OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; |
1458 | OPJ_UINT32 i; |
1459 | OPJ_FLOAT64 l_temp; |
1460 | |
1461 | for (i = 0; i < p_nb_elem; ++i) { |
1462 | opj_read_double(l_src_data, &l_temp); |
1463 | |
1464 | l_src_data += sizeof(OPJ_FLOAT64); |
1465 | |
1466 | *(l_dest_data++) = (OPJ_FLOAT32) l_temp; |
1467 | } |
1468 | } |
1469 | |
1470 | static void opj_j2k_read_int16_to_int32(const void * p_src_data, |
1471 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1472 | { |
1473 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1474 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1475 | OPJ_UINT32 i; |
1476 | OPJ_UINT32 l_temp; |
1477 | |
1478 | for (i = 0; i < p_nb_elem; ++i) { |
1479 | opj_read_bytes(l_src_data, &l_temp, 2); |
1480 | |
1481 | l_src_data += sizeof(OPJ_INT16); |
1482 | |
1483 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1484 | } |
1485 | } |
1486 | |
1487 | static void opj_j2k_read_int32_to_int32(const void * p_src_data, |
1488 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1489 | { |
1490 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1491 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1492 | OPJ_UINT32 i; |
1493 | OPJ_UINT32 l_temp; |
1494 | |
1495 | for (i = 0; i < p_nb_elem; ++i) { |
1496 | opj_read_bytes(l_src_data, &l_temp, 4); |
1497 | |
1498 | l_src_data += sizeof(OPJ_INT32); |
1499 | |
1500 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1501 | } |
1502 | } |
1503 | |
1504 | static void opj_j2k_read_float32_to_int32(const void * p_src_data, |
1505 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1506 | { |
1507 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1508 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1509 | OPJ_UINT32 i; |
1510 | OPJ_FLOAT32 l_temp; |
1511 | |
1512 | for (i = 0; i < p_nb_elem; ++i) { |
1513 | opj_read_float(l_src_data, &l_temp); |
1514 | |
1515 | l_src_data += sizeof(OPJ_FLOAT32); |
1516 | |
1517 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1518 | } |
1519 | } |
1520 | |
1521 | static void opj_j2k_read_float64_to_int32(const void * p_src_data, |
1522 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1523 | { |
1524 | OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; |
1525 | OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; |
1526 | OPJ_UINT32 i; |
1527 | OPJ_FLOAT64 l_temp; |
1528 | |
1529 | for (i = 0; i < p_nb_elem; ++i) { |
1530 | opj_read_double(l_src_data, &l_temp); |
1531 | |
1532 | l_src_data += sizeof(OPJ_FLOAT64); |
1533 | |
1534 | *(l_dest_data++) = (OPJ_INT32) l_temp; |
1535 | } |
1536 | } |
1537 | |
1538 | static void opj_j2k_write_float_to_int16(const void * p_src_data, |
1539 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1540 | { |
1541 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1542 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1543 | OPJ_UINT32 i; |
1544 | OPJ_UINT32 l_temp; |
1545 | |
1546 | for (i = 0; i < p_nb_elem; ++i) { |
1547 | l_temp = (OPJ_UINT32) * (l_src_data++); |
1548 | |
1549 | opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16)); |
1550 | |
1551 | l_dest_data += sizeof(OPJ_INT16); |
1552 | } |
1553 | } |
1554 | |
1555 | static void opj_j2k_write_float_to_int32(const void * p_src_data, |
1556 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1557 | { |
1558 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1559 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1560 | OPJ_UINT32 i; |
1561 | OPJ_UINT32 l_temp; |
1562 | |
1563 | for (i = 0; i < p_nb_elem; ++i) { |
1564 | l_temp = (OPJ_UINT32) * (l_src_data++); |
1565 | |
1566 | opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32)); |
1567 | |
1568 | l_dest_data += sizeof(OPJ_INT32); |
1569 | } |
1570 | } |
1571 | |
1572 | static void opj_j2k_write_float_to_float(const void * p_src_data, |
1573 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1574 | { |
1575 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1576 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1577 | OPJ_UINT32 i; |
1578 | OPJ_FLOAT32 l_temp; |
1579 | |
1580 | for (i = 0; i < p_nb_elem; ++i) { |
1581 | l_temp = (OPJ_FLOAT32) * (l_src_data++); |
1582 | |
1583 | opj_write_float(l_dest_data, l_temp); |
1584 | |
1585 | l_dest_data += sizeof(OPJ_FLOAT32); |
1586 | } |
1587 | } |
1588 | |
1589 | static void opj_j2k_write_float_to_float64(const void * p_src_data, |
1590 | void * p_dest_data, OPJ_UINT32 p_nb_elem) |
1591 | { |
1592 | OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; |
1593 | OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; |
1594 | OPJ_UINT32 i; |
1595 | OPJ_FLOAT64 l_temp; |
1596 | |
1597 | for (i = 0; i < p_nb_elem; ++i) { |
1598 | l_temp = (OPJ_FLOAT64) * (l_src_data++); |
1599 | |
1600 | opj_write_double(l_dest_data, l_temp); |
1601 | |
1602 | l_dest_data += sizeof(OPJ_FLOAT64); |
1603 | } |
1604 | } |
1605 | |
1606 | const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order) |
1607 | { |
1608 | const j2k_prog_order_t *po; |
1609 | for (po = j2k_prog_order_list; po->enum_prog != -1; po++) { |
1610 | if (po->enum_prog == prg_order) { |
1611 | return po->str_prog; |
1612 | } |
1613 | } |
1614 | return po->str_prog; |
1615 | } |
1616 | |
1617 | static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, |
1618 | OPJ_UINT32 p_nb_pocs, |
1619 | OPJ_UINT32 p_nb_resolutions, |
1620 | OPJ_UINT32 p_num_comps, |
1621 | OPJ_UINT32 p_num_layers, |
1622 | opj_event_mgr_t * p_manager) |
1623 | { |
1624 | OPJ_UINT32* packet_array; |
1625 | OPJ_UINT32 index, resno, compno, layno; |
1626 | OPJ_UINT32 i; |
1627 | OPJ_UINT32 step_c = 1; |
1628 | OPJ_UINT32 step_r = p_num_comps * step_c; |
1629 | OPJ_UINT32 step_l = p_nb_resolutions * step_r; |
1630 | OPJ_BOOL loss = OPJ_FALSE; |
1631 | OPJ_UINT32 layno0 = 0; |
1632 | |
1633 | packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, |
1634 | sizeof(OPJ_UINT32)); |
1635 | if (packet_array == 00) { |
1636 | opj_event_msg(p_manager, EVT_ERROR, |
1637 | "Not enough memory for checking the poc values.\n" ); |
1638 | return OPJ_FALSE; |
1639 | } |
1640 | |
1641 | if (p_nb_pocs == 0) { |
1642 | opj_free(packet_array); |
1643 | return OPJ_TRUE; |
1644 | } |
1645 | |
1646 | index = step_r * p_pocs->resno0; |
1647 | /* take each resolution for each poc */ |
1648 | for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { |
1649 | OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; |
1650 | |
1651 | /* take each comp of each resolution for each poc */ |
1652 | for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { |
1653 | OPJ_UINT32 comp_index = res_index + layno0 * step_l; |
1654 | |
1655 | /* and finally take each layer of each res of ... */ |
1656 | for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { |
1657 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
1658 | packet_array[comp_index] = 1; |
1659 | comp_index += step_l; |
1660 | } |
1661 | |
1662 | res_index += step_c; |
1663 | } |
1664 | |
1665 | index += step_r; |
1666 | } |
1667 | ++p_pocs; |
1668 | |
1669 | /* iterate through all the pocs */ |
1670 | for (i = 1; i < p_nb_pocs ; ++i) { |
1671 | OPJ_UINT32 l_last_layno1 = (p_pocs - 1)->layno1 ; |
1672 | |
1673 | layno0 = (p_pocs->layno1 > l_last_layno1) ? l_last_layno1 : 0; |
1674 | index = step_r * p_pocs->resno0; |
1675 | |
1676 | /* take each resolution for each poc */ |
1677 | for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { |
1678 | OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; |
1679 | |
1680 | /* take each comp of each resolution for each poc */ |
1681 | for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { |
1682 | OPJ_UINT32 comp_index = res_index + layno0 * step_l; |
1683 | |
1684 | /* and finally take each layer of each res of ... */ |
1685 | for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { |
1686 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
1687 | packet_array[comp_index] = 1; |
1688 | comp_index += step_l; |
1689 | } |
1690 | |
1691 | res_index += step_c; |
1692 | } |
1693 | |
1694 | index += step_r; |
1695 | } |
1696 | |
1697 | ++p_pocs; |
1698 | } |
1699 | |
1700 | index = 0; |
1701 | for (layno = 0; layno < p_num_layers ; ++layno) { |
1702 | for (resno = 0; resno < p_nb_resolutions; ++resno) { |
1703 | for (compno = 0; compno < p_num_comps; ++compno) { |
1704 | loss |= (packet_array[index] != 1); |
1705 | /*index = step_r * resno + step_c * compno + step_l * layno;*/ |
1706 | index += step_c; |
1707 | } |
1708 | } |
1709 | } |
1710 | |
1711 | if (loss) { |
1712 | opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n" ); |
1713 | } |
1714 | |
1715 | opj_free(packet_array); |
1716 | |
1717 | return !loss; |
1718 | } |
1719 | |
1720 | /* ----------------------------------------------------------------------- */ |
1721 | |
1722 | static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, |
1723 | OPJ_UINT32 tileno) |
1724 | { |
1725 | const OPJ_CHAR *prog = 00; |
1726 | OPJ_INT32 i; |
1727 | OPJ_UINT32 tpnum = 1; |
1728 | opj_tcp_t *tcp = 00; |
1729 | opj_poc_t * l_current_poc = 00; |
1730 | |
1731 | /* preconditions */ |
1732 | assert(tileno < (cp->tw * cp->th)); |
1733 | assert(pino < (cp->tcps[tileno].numpocs + 1)); |
1734 | |
1735 | /* get the given tile coding parameter */ |
1736 | tcp = &cp->tcps[tileno]; |
1737 | assert(tcp != 00); |
1738 | |
1739 | l_current_poc = &(tcp->pocs[pino]); |
1740 | assert(l_current_poc != 0); |
1741 | |
1742 | /* get the progression order as a character string */ |
1743 | prog = opj_j2k_convert_progression_order(tcp->prg); |
1744 | assert(strlen(prog) > 0); |
1745 | |
1746 | if (cp->m_specific_param.m_enc.m_tp_on == 1) { |
1747 | for (i = 0; i < 4; ++i) { |
1748 | switch (prog[i]) { |
1749 | /* component wise */ |
1750 | case 'C': |
1751 | tpnum *= l_current_poc->compE; |
1752 | break; |
1753 | /* resolution wise */ |
1754 | case 'R': |
1755 | tpnum *= l_current_poc->resE; |
1756 | break; |
1757 | /* precinct wise */ |
1758 | case 'P': |
1759 | tpnum *= l_current_poc->prcE; |
1760 | break; |
1761 | /* layer wise */ |
1762 | case 'L': |
1763 | tpnum *= l_current_poc->layE; |
1764 | break; |
1765 | } |
1766 | /* whould we split here ? */ |
1767 | if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) { |
1768 | cp->m_specific_param.m_enc.m_tp_pos = i; |
1769 | break; |
1770 | } |
1771 | } |
1772 | } else { |
1773 | tpnum = 1; |
1774 | } |
1775 | |
1776 | return tpnum; |
1777 | } |
1778 | |
1779 | static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, |
1780 | opj_cp_t *cp, |
1781 | OPJ_UINT32 * p_nb_tiles, |
1782 | opj_image_t *image, |
1783 | opj_event_mgr_t * p_manager |
1784 | ) |
1785 | { |
1786 | OPJ_UINT32 pino, tileno; |
1787 | OPJ_UINT32 l_nb_tiles; |
1788 | opj_tcp_t *tcp; |
1789 | |
1790 | /* preconditions */ |
1791 | assert(p_nb_tiles != 00); |
1792 | assert(cp != 00); |
1793 | assert(image != 00); |
1794 | assert(p_j2k != 00); |
1795 | assert(p_manager != 00); |
1796 | |
1797 | OPJ_UNUSED(p_j2k); |
1798 | OPJ_UNUSED(p_manager); |
1799 | |
1800 | l_nb_tiles = cp->tw * cp->th; |
1801 | * p_nb_tiles = 0; |
1802 | tcp = cp->tcps; |
1803 | |
1804 | /* INDEX >> */ |
1805 | /* TODO mergeV2: check this part which use cstr_info */ |
1806 | /*if (p_j2k->cstr_info) { |
1807 | opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; |
1808 | |
1809 | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
1810 | OPJ_UINT32 cur_totnum_tp = 0; |
1811 | |
1812 | opj_pi_update_encoding_parameters(image,cp,tileno); |
1813 | |
1814 | for (pino = 0; pino <= tcp->numpocs; ++pino) |
1815 | { |
1816 | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); |
1817 | |
1818 | *p_nb_tiles = *p_nb_tiles + tp_num; |
1819 | |
1820 | cur_totnum_tp += tp_num; |
1821 | } |
1822 | |
1823 | tcp->m_nb_tile_parts = cur_totnum_tp; |
1824 | |
1825 | l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); |
1826 | if (l_info_tile_ptr->tp == 00) { |
1827 | return OPJ_FALSE; |
1828 | } |
1829 | |
1830 | memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); |
1831 | |
1832 | l_info_tile_ptr->num_tps = cur_totnum_tp; |
1833 | |
1834 | ++l_info_tile_ptr; |
1835 | ++tcp; |
1836 | } |
1837 | } |
1838 | else */{ |
1839 | for (tileno = 0; tileno < l_nb_tiles; ++tileno) { |
1840 | OPJ_UINT32 cur_totnum_tp = 0; |
1841 | |
1842 | opj_pi_update_encoding_parameters(image, cp, tileno); |
1843 | |
1844 | for (pino = 0; pino <= tcp->numpocs; ++pino) { |
1845 | OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno); |
1846 | |
1847 | *p_nb_tiles = *p_nb_tiles + tp_num; |
1848 | |
1849 | cur_totnum_tp += tp_num; |
1850 | } |
1851 | tcp->m_nb_tile_parts = cur_totnum_tp; |
1852 | |
1853 | ++tcp; |
1854 | } |
1855 | } |
1856 | |
1857 | return OPJ_TRUE; |
1858 | } |
1859 | |
1860 | static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, |
1861 | opj_stream_private_t *p_stream, |
1862 | opj_event_mgr_t * p_manager) |
1863 | { |
1864 | /* 2 bytes will be written */ |
1865 | OPJ_BYTE * l_start_stream = 00; |
1866 | |
1867 | /* preconditions */ |
1868 | assert(p_stream != 00); |
1869 | assert(p_j2k != 00); |
1870 | assert(p_manager != 00); |
1871 | |
1872 | l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
1873 | |
1874 | /* write SOC identifier */ |
1875 | opj_write_bytes(l_start_stream, J2K_MS_SOC, 2); |
1876 | |
1877 | if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) { |
1878 | return OPJ_FALSE; |
1879 | } |
1880 | |
1881 | /* UniPG>> */ |
1882 | #ifdef USE_JPWL |
1883 | /* update markers struct */ |
1884 | /* |
1885 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); |
1886 | */ |
1887 | assert(0 && "TODO" ); |
1888 | #endif /* USE_JPWL */ |
1889 | /* <<UniPG */ |
1890 | |
1891 | return OPJ_TRUE; |
1892 | } |
1893 | |
1894 | /** |
1895 | * Reads a SOC marker (Start of Codestream) |
1896 | * @param p_j2k the jpeg2000 file codec. |
1897 | * @param p_stream FIXME DOC |
1898 | * @param p_manager the user event manager. |
1899 | */ |
1900 | static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, |
1901 | opj_stream_private_t *p_stream, |
1902 | opj_event_mgr_t * p_manager |
1903 | ) |
1904 | { |
1905 | OPJ_BYTE l_data [2]; |
1906 | OPJ_UINT32 l_marker; |
1907 | |
1908 | /* preconditions */ |
1909 | assert(p_j2k != 00); |
1910 | assert(p_manager != 00); |
1911 | assert(p_stream != 00); |
1912 | |
1913 | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
1914 | return OPJ_FALSE; |
1915 | } |
1916 | |
1917 | opj_read_bytes(l_data, &l_marker, 2); |
1918 | if (l_marker != J2K_MS_SOC) { |
1919 | return OPJ_FALSE; |
1920 | } |
1921 | |
1922 | /* Next marker should be a SIZ marker in the main header */ |
1923 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; |
1924 | |
1925 | /* FIXME move it in a index structure included in p_j2k*/ |
1926 | p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; |
1927 | |
1928 | opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n" , |
1929 | p_j2k->cstr_index->main_head_start); |
1930 | |
1931 | /* Add the marker to the codestream index*/ |
1932 | if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, |
1933 | p_j2k->cstr_index->main_head_start, 2)) { |
1934 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n" ); |
1935 | return OPJ_FALSE; |
1936 | } |
1937 | return OPJ_TRUE; |
1938 | } |
1939 | |
1940 | static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, |
1941 | opj_stream_private_t *p_stream, |
1942 | opj_event_mgr_t * p_manager) |
1943 | { |
1944 | OPJ_UINT32 i; |
1945 | OPJ_UINT32 l_size_len; |
1946 | OPJ_BYTE * l_current_ptr; |
1947 | opj_image_t * l_image = 00; |
1948 | opj_cp_t *cp = 00; |
1949 | opj_image_comp_t * l_img_comp = 00; |
1950 | |
1951 | /* preconditions */ |
1952 | assert(p_stream != 00); |
1953 | assert(p_j2k != 00); |
1954 | assert(p_manager != 00); |
1955 | |
1956 | l_image = p_j2k->m_private_image; |
1957 | cp = &(p_j2k->m_cp); |
1958 | l_size_len = 40 + 3 * l_image->numcomps; |
1959 | l_img_comp = l_image->comps; |
1960 | |
1961 | if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
1962 | |
1963 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
1964 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); |
1965 | if (! new_header_tile_data) { |
1966 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
1967 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
1968 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
1969 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n" ); |
1970 | return OPJ_FALSE; |
1971 | } |
1972 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
1973 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; |
1974 | } |
1975 | |
1976 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
1977 | |
1978 | /* write SOC identifier */ |
1979 | opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2); /* SIZ */ |
1980 | l_current_ptr += 2; |
1981 | |
1982 | opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */ |
1983 | l_current_ptr += 2; |
1984 | |
1985 | opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ |
1986 | l_current_ptr += 2; |
1987 | |
1988 | opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ |
1989 | l_current_ptr += 4; |
1990 | |
1991 | opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ |
1992 | l_current_ptr += 4; |
1993 | |
1994 | opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ |
1995 | l_current_ptr += 4; |
1996 | |
1997 | opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ |
1998 | l_current_ptr += 4; |
1999 | |
2000 | opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ |
2001 | l_current_ptr += 4; |
2002 | |
2003 | opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ |
2004 | l_current_ptr += 4; |
2005 | |
2006 | opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ |
2007 | l_current_ptr += 4; |
2008 | |
2009 | opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ |
2010 | l_current_ptr += 4; |
2011 | |
2012 | opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ |
2013 | l_current_ptr += 2; |
2014 | |
2015 | for (i = 0; i < l_image->numcomps; ++i) { |
2016 | /* TODO here with MCT ? */ |
2017 | opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), |
2018 | 1); /* Ssiz_i */ |
2019 | ++l_current_ptr; |
2020 | |
2021 | opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ |
2022 | ++l_current_ptr; |
2023 | |
2024 | opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ |
2025 | ++l_current_ptr; |
2026 | |
2027 | ++l_img_comp; |
2028 | } |
2029 | |
2030 | if (opj_stream_write_data(p_stream, |
2031 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len, |
2032 | p_manager) != l_size_len) { |
2033 | return OPJ_FALSE; |
2034 | } |
2035 | |
2036 | return OPJ_TRUE; |
2037 | } |
2038 | |
2039 | /** |
2040 | * Reads a SIZ marker (image and tile size) |
2041 | * @param p_j2k the jpeg2000 file codec. |
2042 | * @param p_header_data the data contained in the SIZ box. |
2043 | * @param p_header_size the size of the data contained in the SIZ marker. |
2044 | * @param p_manager the user event manager. |
2045 | */ |
2046 | static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, |
2047 | OPJ_BYTE * , |
2048 | OPJ_UINT32 , |
2049 | opj_event_mgr_t * p_manager |
2050 | ) |
2051 | { |
2052 | OPJ_UINT32 i; |
2053 | OPJ_UINT32 l_nb_comp; |
2054 | OPJ_UINT32 l_nb_comp_remain; |
2055 | OPJ_UINT32 l_remaining_size; |
2056 | OPJ_UINT32 l_nb_tiles; |
2057 | OPJ_UINT32 l_tmp, l_tx1, l_ty1; |
2058 | OPJ_UINT32 l_prec0, l_sgnd0; |
2059 | opj_image_t *l_image = 00; |
2060 | opj_cp_t *l_cp = 00; |
2061 | opj_image_comp_t * l_img_comp = 00; |
2062 | opj_tcp_t * l_current_tile_param = 00; |
2063 | |
2064 | /* preconditions */ |
2065 | assert(p_j2k != 00); |
2066 | assert(p_manager != 00); |
2067 | assert(p_header_data != 00); |
2068 | |
2069 | l_image = p_j2k->m_private_image; |
2070 | l_cp = &(p_j2k->m_cp); |
2071 | |
2072 | /* minimum size == 39 - 3 (= minimum component parameter) */ |
2073 | if (p_header_size < 36) { |
2074 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n" ); |
2075 | return OPJ_FALSE; |
2076 | } |
2077 | |
2078 | l_remaining_size = p_header_size - 36; |
2079 | l_nb_comp = l_remaining_size / 3; |
2080 | l_nb_comp_remain = l_remaining_size % 3; |
2081 | if (l_nb_comp_remain != 0) { |
2082 | opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n" ); |
2083 | return OPJ_FALSE; |
2084 | } |
2085 | |
2086 | opj_read_bytes(p_header_data, &l_tmp, |
2087 | 2); /* Rsiz (capabilities) */ |
2088 | p_header_data += 2; |
2089 | l_cp->rsiz = (OPJ_UINT16) l_tmp; |
2090 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ |
2091 | p_header_data += 4; |
2092 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ |
2093 | p_header_data += 4; |
2094 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ |
2095 | p_header_data += 4; |
2096 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ |
2097 | p_header_data += 4; |
2098 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, |
2099 | 4); /* XTsiz */ |
2100 | p_header_data += 4; |
2101 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, |
2102 | 4); /* YTsiz */ |
2103 | p_header_data += 4; |
2104 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, |
2105 | 4); /* XT0siz */ |
2106 | p_header_data += 4; |
2107 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, |
2108 | 4); /* YT0siz */ |
2109 | p_header_data += 4; |
2110 | opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, |
2111 | 2); /* Csiz */ |
2112 | p_header_data += 2; |
2113 | if (l_tmp < 16385) { |
2114 | l_image->numcomps = (OPJ_UINT16) l_tmp; |
2115 | } else { |
2116 | opj_event_msg(p_manager, EVT_ERROR, |
2117 | "Error with SIZ marker: number of component is illegal -> %d\n" , l_tmp); |
2118 | return OPJ_FALSE; |
2119 | } |
2120 | |
2121 | if (l_image->numcomps != l_nb_comp) { |
2122 | opj_event_msg(p_manager, EVT_ERROR, |
2123 | "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n" , |
2124 | l_image->numcomps, l_nb_comp); |
2125 | return OPJ_FALSE; |
2126 | } |
2127 | |
2128 | /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ |
2129 | /* testcase issue427-null-image-size.jp2 */ |
2130 | if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { |
2131 | opj_event_msg(p_manager, EVT_ERROR, |
2132 | "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 |
2133 | ")\n" , (OPJ_INT64)l_image->x1 - l_image->x0, |
2134 | (OPJ_INT64)l_image->y1 - l_image->y0); |
2135 | return OPJ_FALSE; |
2136 | } |
2137 | /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ |
2138 | if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) { |
2139 | opj_event_msg(p_manager, EVT_ERROR, |
2140 | "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n" , l_cp->tdx, |
2141 | l_cp->tdy); |
2142 | return OPJ_FALSE; |
2143 | } |
2144 | |
2145 | /* testcase issue427-illegal-tile-offset.jp2 */ |
2146 | l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ |
2147 | l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ |
2148 | if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || |
2149 | (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) { |
2150 | opj_event_msg(p_manager, EVT_ERROR, |
2151 | "Error with SIZ marker: illegal tile offset\n" ); |
2152 | return OPJ_FALSE; |
2153 | } |
2154 | if (!p_j2k->dump_state) { |
2155 | OPJ_UINT32 siz_w, siz_h; |
2156 | |
2157 | siz_w = l_image->x1 - l_image->x0; |
2158 | siz_h = l_image->y1 - l_image->y0; |
2159 | |
2160 | if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0 |
2161 | && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) { |
2162 | opj_event_msg(p_manager, EVT_ERROR, |
2163 | "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n" , p_j2k->ihdr_w, |
2164 | p_j2k->ihdr_h, siz_w, siz_h); |
2165 | return OPJ_FALSE; |
2166 | } |
2167 | } |
2168 | #ifdef USE_JPWL |
2169 | if (l_cp->correct) { |
2170 | /* if JPWL is on, we check whether TX errors have damaged |
2171 | too much the SIZ parameters */ |
2172 | if (!(l_image->x1 * l_image->y1)) { |
2173 | opj_event_msg(p_manager, EVT_ERROR, |
2174 | "JPWL: bad image size (%d x %d)\n" , |
2175 | l_image->x1, l_image->y1); |
2176 | if (!JPWL_ASSUME) { |
2177 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
2178 | return OPJ_FALSE; |
2179 | } |
2180 | } |
2181 | |
2182 | /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? |
2183 | if (l_image->numcomps != ((len - 38) / 3)) { |
2184 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2185 | "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", |
2186 | l_image->numcomps, ((len - 38) / 3)); |
2187 | if (!JPWL_ASSUME) { |
2188 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); |
2189 | return OPJ_FALSE; |
2190 | } |
2191 | */ /* we try to correct */ |
2192 | /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"); |
2193 | if (l_image->numcomps < ((len - 38) / 3)) { |
2194 | len = 38 + 3 * l_image->numcomps; |
2195 | opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", |
2196 | len); |
2197 | } else { |
2198 | l_image->numcomps = ((len - 38) / 3); |
2199 | opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", |
2200 | l_image->numcomps); |
2201 | } |
2202 | } |
2203 | */ |
2204 | |
2205 | /* update components number in the jpwl_exp_comps filed */ |
2206 | l_cp->exp_comps = l_image->numcomps; |
2207 | } |
2208 | #endif /* USE_JPWL */ |
2209 | |
2210 | /* Allocate the resulting image components */ |
2211 | l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, |
2212 | sizeof(opj_image_comp_t)); |
2213 | if (l_image->comps == 00) { |
2214 | l_image->numcomps = 0; |
2215 | opj_event_msg(p_manager, EVT_ERROR, |
2216 | "Not enough memory to take in charge SIZ marker\n" ); |
2217 | return OPJ_FALSE; |
2218 | } |
2219 | |
2220 | l_img_comp = l_image->comps; |
2221 | |
2222 | l_prec0 = 0; |
2223 | l_sgnd0 = 0; |
2224 | /* Read the component information */ |
2225 | for (i = 0; i < l_image->numcomps; ++i) { |
2226 | OPJ_UINT32 tmp; |
2227 | opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */ |
2228 | ++p_header_data; |
2229 | l_img_comp->prec = (tmp & 0x7f) + 1; |
2230 | l_img_comp->sgnd = tmp >> 7; |
2231 | |
2232 | if (p_j2k->dump_state == 0) { |
2233 | if (i == 0) { |
2234 | l_prec0 = l_img_comp->prec; |
2235 | l_sgnd0 = l_img_comp->sgnd; |
2236 | } else if (!l_cp->allow_different_bit_depth_sign |
2237 | && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) { |
2238 | opj_event_msg(p_manager, EVT_WARNING, |
2239 | "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n" |
2240 | " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n" , i, l_prec0, l_sgnd0, |
2241 | i, l_img_comp->prec, l_img_comp->sgnd); |
2242 | } |
2243 | /* TODO: we should perhaps also check against JP2 BPCC values */ |
2244 | } |
2245 | opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ |
2246 | ++p_header_data; |
2247 | l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2248 | opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */ |
2249 | ++p_header_data; |
2250 | l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ |
2251 | if (l_img_comp->dx < 1 || l_img_comp->dx > 255 || |
2252 | l_img_comp->dy < 1 || l_img_comp->dy > 255) { |
2253 | opj_event_msg(p_manager, EVT_ERROR, |
2254 | "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n" , |
2255 | i, l_img_comp->dx, l_img_comp->dy); |
2256 | return OPJ_FALSE; |
2257 | } |
2258 | /* Avoids later undefined shift in computation of */ |
2259 | /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2260 | << (l_image->comps[i].prec - 1); */ |
2261 | if (l_img_comp->prec > 31) { |
2262 | opj_event_msg(p_manager, EVT_ERROR, |
2263 | "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n" , |
2264 | i, l_img_comp->prec); |
2265 | return OPJ_FALSE; |
2266 | } |
2267 | #ifdef USE_JPWL |
2268 | if (l_cp->correct) { |
2269 | /* if JPWL is on, we check whether TX errors have damaged |
2270 | too much the SIZ parameters, again */ |
2271 | if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { |
2272 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2273 | "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n" , |
2274 | i, i, l_image->comps[i].dx, l_image->comps[i].dy); |
2275 | if (!JPWL_ASSUME) { |
2276 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
2277 | return OPJ_FALSE; |
2278 | } |
2279 | /* we try to correct */ |
2280 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" ); |
2281 | if (!l_image->comps[i].dx) { |
2282 | l_image->comps[i].dx = 1; |
2283 | opj_event_msg(p_manager, EVT_WARNING, |
2284 | "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n" , |
2285 | i, l_image->comps[i].dx); |
2286 | } |
2287 | if (!l_image->comps[i].dy) { |
2288 | l_image->comps[i].dy = 1; |
2289 | opj_event_msg(p_manager, EVT_WARNING, |
2290 | "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n" , |
2291 | i, l_image->comps[i].dy); |
2292 | } |
2293 | } |
2294 | } |
2295 | #endif /* USE_JPWL */ |
2296 | l_img_comp->resno_decoded = |
2297 | 0; /* number of resolution decoded */ |
2298 | l_img_comp->factor = |
2299 | l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ |
2300 | ++l_img_comp; |
2301 | } |
2302 | |
2303 | if (l_cp->tdx == 0 || l_cp->tdy == 0) { |
2304 | return OPJ_FALSE; |
2305 | } |
2306 | |
2307 | /* Compute the number of tiles */ |
2308 | l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), |
2309 | (OPJ_INT32)l_cp->tdx); |
2310 | l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), |
2311 | (OPJ_INT32)l_cp->tdy); |
2312 | |
2313 | /* Check that the number of tiles is valid */ |
2314 | if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { |
2315 | opj_event_msg(p_manager, EVT_ERROR, |
2316 | "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n" , |
2317 | l_cp->tw, l_cp->th); |
2318 | return OPJ_FALSE; |
2319 | } |
2320 | l_nb_tiles = l_cp->tw * l_cp->th; |
2321 | |
2322 | /* Define the tiles which will be decoded */ |
2323 | if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { |
2324 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = |
2325 | (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; |
2326 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = |
2327 | (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; |
2328 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(( |
2329 | OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), |
2330 | (OPJ_INT32)l_cp->tdx); |
2331 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(( |
2332 | OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), |
2333 | (OPJ_INT32)l_cp->tdy); |
2334 | } else { |
2335 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
2336 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
2337 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
2338 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
2339 | } |
2340 | |
2341 | #ifdef USE_JPWL |
2342 | if (l_cp->correct) { |
2343 | /* if JPWL is on, we check whether TX errors have damaged |
2344 | too much the SIZ parameters */ |
2345 | if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || |
2346 | (l_cp->th > l_cp->max_tiles)) { |
2347 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2348 | "JPWL: bad number of tiles (%d x %d)\n" , |
2349 | l_cp->tw, l_cp->th); |
2350 | if (!JPWL_ASSUME) { |
2351 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
2352 | return OPJ_FALSE; |
2353 | } |
2354 | /* we try to correct */ |
2355 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" ); |
2356 | if (l_cp->tw < 1) { |
2357 | l_cp->tw = 1; |
2358 | opj_event_msg(p_manager, EVT_WARNING, |
2359 | "- setting %d tiles in x => HYPOTHESIS!!!\n" , |
2360 | l_cp->tw); |
2361 | } |
2362 | if (l_cp->tw > l_cp->max_tiles) { |
2363 | l_cp->tw = 1; |
2364 | opj_event_msg(p_manager, EVT_WARNING, |
2365 | "- too large x, increase expectance of %d\n" |
2366 | "- setting %d tiles in x => HYPOTHESIS!!!\n" , |
2367 | l_cp->max_tiles, l_cp->tw); |
2368 | } |
2369 | if (l_cp->th < 1) { |
2370 | l_cp->th = 1; |
2371 | opj_event_msg(p_manager, EVT_WARNING, |
2372 | "- setting %d tiles in y => HYPOTHESIS!!!\n" , |
2373 | l_cp->th); |
2374 | } |
2375 | if (l_cp->th > l_cp->max_tiles) { |
2376 | l_cp->th = 1; |
2377 | opj_event_msg(p_manager, EVT_WARNING, |
2378 | "- too large y, increase expectance of %d to continue\n" , |
2379 | "- setting %d tiles in y => HYPOTHESIS!!!\n" , |
2380 | l_cp->max_tiles, l_cp->th); |
2381 | } |
2382 | } |
2383 | } |
2384 | #endif /* USE_JPWL */ |
2385 | |
2386 | /* memory allocations */ |
2387 | l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); |
2388 | if (l_cp->tcps == 00) { |
2389 | opj_event_msg(p_manager, EVT_ERROR, |
2390 | "Not enough memory to take in charge SIZ marker\n" ); |
2391 | return OPJ_FALSE; |
2392 | } |
2393 | |
2394 | #ifdef USE_JPWL |
2395 | if (l_cp->correct) { |
2396 | if (!l_cp->tcps) { |
2397 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
2398 | "JPWL: could not alloc tcps field of cp\n" ); |
2399 | if (!JPWL_ASSUME) { |
2400 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
2401 | return OPJ_FALSE; |
2402 | } |
2403 | } |
2404 | } |
2405 | #endif /* USE_JPWL */ |
2406 | |
2407 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = |
2408 | (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); |
2409 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { |
2410 | opj_event_msg(p_manager, EVT_ERROR, |
2411 | "Not enough memory to take in charge SIZ marker\n" ); |
2412 | return OPJ_FALSE; |
2413 | } |
2414 | |
2415 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = |
2416 | (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS, |
2417 | sizeof(opj_mct_data_t)); |
2418 | |
2419 | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { |
2420 | opj_event_msg(p_manager, EVT_ERROR, |
2421 | "Not enough memory to take in charge SIZ marker\n" ); |
2422 | return OPJ_FALSE; |
2423 | } |
2424 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = |
2425 | OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
2426 | |
2427 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = |
2428 | (opj_simple_mcc_decorrelation_data_t*) |
2429 | opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, |
2430 | sizeof(opj_simple_mcc_decorrelation_data_t)); |
2431 | |
2432 | if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { |
2433 | opj_event_msg(p_manager, EVT_ERROR, |
2434 | "Not enough memory to take in charge SIZ marker\n" ); |
2435 | return OPJ_FALSE; |
2436 | } |
2437 | p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = |
2438 | OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
2439 | |
2440 | /* set up default dc level shift */ |
2441 | for (i = 0; i < l_image->numcomps; ++i) { |
2442 | if (! l_image->comps[i].sgnd) { |
2443 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 |
2444 | << (l_image->comps[i].prec - 1); |
2445 | } |
2446 | } |
2447 | |
2448 | l_current_tile_param = l_cp->tcps; |
2449 | for (i = 0; i < l_nb_tiles; ++i) { |
2450 | l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, |
2451 | sizeof(opj_tccp_t)); |
2452 | if (l_current_tile_param->tccps == 00) { |
2453 | opj_event_msg(p_manager, EVT_ERROR, |
2454 | "Not enough memory to take in charge SIZ marker\n" ); |
2455 | return OPJ_FALSE; |
2456 | } |
2457 | |
2458 | ++l_current_tile_param; |
2459 | } |
2460 | |
2461 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; |
2462 | opj_image_comp_header_update(l_image, l_cp); |
2463 | |
2464 | return OPJ_TRUE; |
2465 | } |
2466 | |
2467 | static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, |
2468 | opj_stream_private_t *p_stream, |
2469 | opj_event_mgr_t * p_manager |
2470 | ) |
2471 | { |
2472 | OPJ_UINT32 ; |
2473 | OPJ_UINT32 l_total_com_size; |
2474 | const OPJ_CHAR *; |
2475 | OPJ_BYTE * l_current_ptr = 00; |
2476 | |
2477 | /* preconditions */ |
2478 | assert(p_j2k != 00); |
2479 | assert(p_stream != 00); |
2480 | assert(p_manager != 00); |
2481 | |
2482 | l_comment = p_j2k->m_cp.comment; |
2483 | l_comment_size = (OPJ_UINT32)strlen(l_comment); |
2484 | l_total_com_size = l_comment_size + 6; |
2485 | |
2486 | if (l_total_com_size > |
2487 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2488 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
2489 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); |
2490 | if (! new_header_tile_data) { |
2491 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2492 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2493 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2494 | opj_event_msg(p_manager, EVT_ERROR, |
2495 | "Not enough memory to write the COM marker\n" ); |
2496 | return OPJ_FALSE; |
2497 | } |
2498 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2499 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; |
2500 | } |
2501 | |
2502 | l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2503 | |
2504 | opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */ |
2505 | l_current_ptr += 2; |
2506 | |
2507 | opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */ |
2508 | l_current_ptr += 2; |
2509 | |
2510 | opj_write_bytes(l_current_ptr, 1, |
2511 | 2); /* General use (IS 8859-15:1999 (Latin) values) */ |
2512 | l_current_ptr += 2; |
2513 | |
2514 | memcpy(l_current_ptr, l_comment, l_comment_size); |
2515 | |
2516 | if (opj_stream_write_data(p_stream, |
2517 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size, |
2518 | p_manager) != l_total_com_size) { |
2519 | return OPJ_FALSE; |
2520 | } |
2521 | |
2522 | return OPJ_TRUE; |
2523 | } |
2524 | |
2525 | /** |
2526 | * Reads a COM marker (comments) |
2527 | * @param p_j2k the jpeg2000 file codec. |
2528 | * @param p_header_data the data contained in the COM box. |
2529 | * @param p_header_size the size of the data contained in the COM marker. |
2530 | * @param p_manager the user event manager. |
2531 | */ |
2532 | static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, |
2533 | OPJ_BYTE * , |
2534 | OPJ_UINT32 , |
2535 | opj_event_mgr_t * p_manager |
2536 | ) |
2537 | { |
2538 | /* preconditions */ |
2539 | assert(p_j2k != 00); |
2540 | assert(p_manager != 00); |
2541 | assert(p_header_data != 00); |
2542 | |
2543 | OPJ_UNUSED(p_j2k); |
2544 | OPJ_UNUSED(p_header_data); |
2545 | OPJ_UNUSED(p_header_size); |
2546 | OPJ_UNUSED(p_manager); |
2547 | |
2548 | return OPJ_TRUE; |
2549 | } |
2550 | |
2551 | static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, |
2552 | opj_stream_private_t *p_stream, |
2553 | opj_event_mgr_t * p_manager) |
2554 | { |
2555 | opj_cp_t *l_cp = 00; |
2556 | opj_tcp_t *l_tcp = 00; |
2557 | OPJ_UINT32 l_code_size, l_remaining_size; |
2558 | OPJ_BYTE * l_current_data = 00; |
2559 | |
2560 | /* preconditions */ |
2561 | assert(p_j2k != 00); |
2562 | assert(p_manager != 00); |
2563 | assert(p_stream != 00); |
2564 | |
2565 | l_cp = &(p_j2k->m_cp); |
2566 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2567 | l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2568 | p_j2k->m_current_tile_number, 0); |
2569 | l_remaining_size = l_code_size; |
2570 | |
2571 | if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2572 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
2573 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); |
2574 | if (! new_header_tile_data) { |
2575 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2576 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2577 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2578 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n" ); |
2579 | return OPJ_FALSE; |
2580 | } |
2581 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2582 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; |
2583 | } |
2584 | |
2585 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2586 | |
2587 | opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */ |
2588 | l_current_data += 2; |
2589 | |
2590 | opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */ |
2591 | l_current_data += 2; |
2592 | |
2593 | opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */ |
2594 | ++l_current_data; |
2595 | |
2596 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */ |
2597 | ++l_current_data; |
2598 | |
2599 | opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */ |
2600 | l_current_data += 2; |
2601 | |
2602 | opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */ |
2603 | ++l_current_data; |
2604 | |
2605 | l_remaining_size -= 9; |
2606 | |
2607 | if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2608 | l_current_data, &l_remaining_size, p_manager)) { |
2609 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n" ); |
2610 | return OPJ_FALSE; |
2611 | } |
2612 | |
2613 | if (l_remaining_size != 0) { |
2614 | opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n" ); |
2615 | return OPJ_FALSE; |
2616 | } |
2617 | |
2618 | if (opj_stream_write_data(p_stream, |
2619 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size, |
2620 | p_manager) != l_code_size) { |
2621 | return OPJ_FALSE; |
2622 | } |
2623 | |
2624 | return OPJ_TRUE; |
2625 | } |
2626 | |
2627 | /** |
2628 | * Reads a COD marker (Coding Styke defaults) |
2629 | * @param p_header_data the data contained in the COD box. |
2630 | * @param p_j2k the jpeg2000 codec. |
2631 | * @param p_header_size the size of the data contained in the COD marker. |
2632 | * @param p_manager the user event manager. |
2633 | */ |
2634 | static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, |
2635 | OPJ_BYTE * , |
2636 | OPJ_UINT32 , |
2637 | opj_event_mgr_t * p_manager |
2638 | ) |
2639 | { |
2640 | /* loop */ |
2641 | OPJ_UINT32 i; |
2642 | OPJ_UINT32 l_tmp; |
2643 | opj_cp_t *l_cp = 00; |
2644 | opj_tcp_t *l_tcp = 00; |
2645 | opj_image_t *l_image = 00; |
2646 | |
2647 | /* preconditions */ |
2648 | assert(p_header_data != 00); |
2649 | assert(p_j2k != 00); |
2650 | assert(p_manager != 00); |
2651 | |
2652 | l_image = p_j2k->m_private_image; |
2653 | l_cp = &(p_j2k->m_cp); |
2654 | |
2655 | /* If we are in the first tile-part header of the current tile */ |
2656 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
2657 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2658 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2659 | |
2660 | /* Only one COD per tile */ |
2661 | if (l_tcp->cod) { |
2662 | opj_event_msg(p_manager, EVT_ERROR, |
2663 | "COD marker already read. No more than one COD marker per tile.\n" ); |
2664 | return OPJ_FALSE; |
2665 | } |
2666 | l_tcp->cod = 1; |
2667 | |
2668 | /* Make sure room is sufficient */ |
2669 | if (p_header_size < 5) { |
2670 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n" ); |
2671 | return OPJ_FALSE; |
2672 | } |
2673 | |
2674 | opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */ |
2675 | ++p_header_data; |
2676 | /* Make sure we know how to decode this */ |
2677 | if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | |
2678 | J2K_CP_CSTY_EPH)) != 0U) { |
2679 | opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n" ); |
2680 | return OPJ_FALSE; |
2681 | } |
2682 | opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */ |
2683 | ++p_header_data; |
2684 | l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; |
2685 | /* Make sure progression order is valid */ |
2686 | if (l_tcp->prg > OPJ_CPRL) { |
2687 | opj_event_msg(p_manager, EVT_ERROR, |
2688 | "Unknown progression order in COD marker\n" ); |
2689 | l_tcp->prg = OPJ_PROG_UNKNOWN; |
2690 | } |
2691 | opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */ |
2692 | p_header_data += 2; |
2693 | |
2694 | if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { |
2695 | opj_event_msg(p_manager, EVT_ERROR, |
2696 | "Invalid number of layers in COD marker : %d not in range [1-65535]\n" , |
2697 | l_tcp->numlayers); |
2698 | return OPJ_FALSE; |
2699 | } |
2700 | |
2701 | /* If user didn't set a number layer to decode take the max specify in the codestream. */ |
2702 | if (l_cp->m_specific_param.m_dec.m_layer) { |
2703 | l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; |
2704 | } else { |
2705 | l_tcp->num_layers_to_decode = l_tcp->numlayers; |
2706 | } |
2707 | |
2708 | opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */ |
2709 | ++p_header_data; |
2710 | |
2711 | p_header_size -= 5; |
2712 | for (i = 0; i < l_image->numcomps; ++i) { |
2713 | l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; |
2714 | } |
2715 | |
2716 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size, |
2717 | p_manager)) { |
2718 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n" ); |
2719 | return OPJ_FALSE; |
2720 | } |
2721 | |
2722 | if (p_header_size != 0) { |
2723 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n" ); |
2724 | return OPJ_FALSE; |
2725 | } |
2726 | |
2727 | /* Apply the coding style to other components of the current tile or the m_default_tcp*/ |
2728 | opj_j2k_copy_tile_component_parameters(p_j2k); |
2729 | |
2730 | /* Index */ |
2731 | #ifdef WIP_REMOVE_MSD |
2732 | if (p_j2k->cstr_info) { |
2733 | /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ |
2734 | p_j2k->cstr_info->prog = l_tcp->prg; |
2735 | p_j2k->cstr_info->numlayers = l_tcp->numlayers; |
2736 | p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc( |
2737 | l_image->numcomps * sizeof(OPJ_UINT32)); |
2738 | if (!p_j2k->cstr_info->numdecompos) { |
2739 | return OPJ_FALSE; |
2740 | } |
2741 | for (i = 0; i < l_image->numcomps; ++i) { |
2742 | p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; |
2743 | } |
2744 | } |
2745 | #endif |
2746 | |
2747 | return OPJ_TRUE; |
2748 | } |
2749 | |
2750 | static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, |
2751 | OPJ_UINT32 p_comp_no, |
2752 | opj_stream_private_t *p_stream, |
2753 | opj_event_mgr_t * p_manager) |
2754 | { |
2755 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2756 | OPJ_UINT32 l_comp_room; |
2757 | |
2758 | /* preconditions */ |
2759 | assert(p_j2k != 00); |
2760 | assert(p_manager != 00); |
2761 | assert(p_stream != 00); |
2762 | |
2763 | l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; |
2764 | |
2765 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2766 | p_j2k->m_current_tile_number, p_comp_no); |
2767 | |
2768 | if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2769 | OPJ_BYTE *; |
2770 | /*p_j2k->m_specific_param.m_encoder.m_header_tile_data |
2771 | = (OPJ_BYTE*)opj_realloc( |
2772 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
2773 | l_coc_size);*/ |
2774 | |
2775 | new_header_tile_data = (OPJ_BYTE *) opj_realloc( |
2776 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); |
2777 | if (! new_header_tile_data) { |
2778 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2779 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2780 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2781 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n" ); |
2782 | return OPJ_FALSE; |
2783 | } |
2784 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2785 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; |
2786 | } |
2787 | |
2788 | opj_j2k_write_coc_in_memory(p_j2k, p_comp_no, |
2789 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
2790 | p_manager); |
2791 | |
2792 | if (opj_stream_write_data(p_stream, |
2793 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size, |
2794 | p_manager) != l_coc_size) { |
2795 | return OPJ_FALSE; |
2796 | } |
2797 | |
2798 | return OPJ_TRUE; |
2799 | } |
2800 | |
2801 | static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, |
2802 | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
2803 | { |
2804 | opj_cp_t *l_cp = NULL; |
2805 | opj_tcp_t *l_tcp = NULL; |
2806 | |
2807 | /* preconditions */ |
2808 | assert(p_j2k != 00); |
2809 | |
2810 | l_cp = &(p_j2k->m_cp); |
2811 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2812 | |
2813 | if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) { |
2814 | return OPJ_FALSE; |
2815 | } |
2816 | |
2817 | |
2818 | return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, |
2819 | p_first_comp_no, p_second_comp_no); |
2820 | } |
2821 | |
2822 | static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, |
2823 | OPJ_UINT32 p_comp_no, |
2824 | OPJ_BYTE * p_data, |
2825 | OPJ_UINT32 * p_data_written, |
2826 | opj_event_mgr_t * p_manager |
2827 | ) |
2828 | { |
2829 | opj_cp_t *l_cp = 00; |
2830 | opj_tcp_t *l_tcp = 00; |
2831 | OPJ_UINT32 l_coc_size, l_remaining_size; |
2832 | OPJ_BYTE * l_current_data = 00; |
2833 | opj_image_t *l_image = 00; |
2834 | OPJ_UINT32 l_comp_room; |
2835 | |
2836 | /* preconditions */ |
2837 | assert(p_j2k != 00); |
2838 | assert(p_manager != 00); |
2839 | |
2840 | l_cp = &(p_j2k->m_cp); |
2841 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
2842 | l_image = p_j2k->m_private_image; |
2843 | l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; |
2844 | |
2845 | l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, |
2846 | p_j2k->m_current_tile_number, p_comp_no); |
2847 | l_remaining_size = l_coc_size; |
2848 | |
2849 | l_current_data = p_data; |
2850 | |
2851 | opj_write_bytes(l_current_data, J2K_MS_COC, |
2852 | 2); /* COC */ |
2853 | l_current_data += 2; |
2854 | |
2855 | opj_write_bytes(l_current_data, l_coc_size - 2, |
2856 | 2); /* L_COC */ |
2857 | l_current_data += 2; |
2858 | |
2859 | opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */ |
2860 | l_current_data += l_comp_room; |
2861 | |
2862 | opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, |
2863 | 1); /* Scoc */ |
2864 | ++l_current_data; |
2865 | |
2866 | l_remaining_size -= (5 + l_comp_room); |
2867 | opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, |
2868 | l_current_data, &l_remaining_size, p_manager); |
2869 | * p_data_written = l_coc_size; |
2870 | } |
2871 | |
2872 | static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) |
2873 | { |
2874 | OPJ_UINT32 i, j; |
2875 | OPJ_UINT32 l_nb_comp; |
2876 | OPJ_UINT32 l_nb_tiles; |
2877 | OPJ_UINT32 l_max = 0; |
2878 | |
2879 | /* preconditions */ |
2880 | |
2881 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
2882 | l_nb_comp = p_j2k->m_private_image->numcomps; |
2883 | |
2884 | for (i = 0; i < l_nb_tiles; ++i) { |
2885 | for (j = 0; j < l_nb_comp; ++j) { |
2886 | l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j)); |
2887 | } |
2888 | } |
2889 | |
2890 | return 6 + l_max; |
2891 | } |
2892 | |
2893 | /** |
2894 | * Reads a COC marker (Coding Style Component) |
2895 | * @param p_header_data the data contained in the COC box. |
2896 | * @param p_j2k the jpeg2000 codec. |
2897 | * @param p_header_size the size of the data contained in the COC marker. |
2898 | * @param p_manager the user event manager. |
2899 | */ |
2900 | static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, |
2901 | OPJ_BYTE * , |
2902 | OPJ_UINT32 , |
2903 | opj_event_mgr_t * p_manager |
2904 | ) |
2905 | { |
2906 | opj_cp_t *l_cp = NULL; |
2907 | opj_tcp_t *l_tcp = NULL; |
2908 | opj_image_t *l_image = NULL; |
2909 | OPJ_UINT32 l_comp_room; |
2910 | OPJ_UINT32 l_comp_no; |
2911 | |
2912 | /* preconditions */ |
2913 | assert(p_header_data != 00); |
2914 | assert(p_j2k != 00); |
2915 | assert(p_manager != 00); |
2916 | |
2917 | l_cp = &(p_j2k->m_cp); |
2918 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
2919 | ? |
2920 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
2921 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
2922 | l_image = p_j2k->m_private_image; |
2923 | |
2924 | l_comp_room = l_image->numcomps <= 256 ? 1 : 2; |
2925 | |
2926 | /* make sure room is sufficient*/ |
2927 | if (p_header_size < l_comp_room + 1) { |
2928 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n" ); |
2929 | return OPJ_FALSE; |
2930 | } |
2931 | p_header_size -= l_comp_room + 1; |
2932 | |
2933 | opj_read_bytes(p_header_data, &l_comp_no, |
2934 | l_comp_room); /* Ccoc */ |
2935 | p_header_data += l_comp_room; |
2936 | if (l_comp_no >= l_image->numcomps) { |
2937 | opj_event_msg(p_manager, EVT_ERROR, |
2938 | "Error reading COC marker (bad number of components)\n" ); |
2939 | return OPJ_FALSE; |
2940 | } |
2941 | |
2942 | opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty, |
2943 | 1); /* Scoc */ |
2944 | ++p_header_data ; |
2945 | |
2946 | if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
2947 | p_manager)) { |
2948 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n" ); |
2949 | return OPJ_FALSE; |
2950 | } |
2951 | |
2952 | if (p_header_size != 0) { |
2953 | opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n" ); |
2954 | return OPJ_FALSE; |
2955 | } |
2956 | return OPJ_TRUE; |
2957 | } |
2958 | |
2959 | static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, |
2960 | opj_stream_private_t *p_stream, |
2961 | opj_event_mgr_t * p_manager |
2962 | ) |
2963 | { |
2964 | OPJ_UINT32 l_qcd_size, l_remaining_size; |
2965 | OPJ_BYTE * l_current_data = 00; |
2966 | |
2967 | /* preconditions */ |
2968 | assert(p_j2k != 00); |
2969 | assert(p_manager != 00); |
2970 | assert(p_stream != 00); |
2971 | |
2972 | l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
2973 | 0); |
2974 | l_remaining_size = l_qcd_size; |
2975 | |
2976 | if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
2977 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
2978 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); |
2979 | if (! new_header_tile_data) { |
2980 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
2981 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
2982 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
2983 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n" ); |
2984 | return OPJ_FALSE; |
2985 | } |
2986 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
2987 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; |
2988 | } |
2989 | |
2990 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
2991 | |
2992 | opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */ |
2993 | l_current_data += 2; |
2994 | |
2995 | opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */ |
2996 | l_current_data += 2; |
2997 | |
2998 | l_remaining_size -= 4; |
2999 | |
3000 | if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0, |
3001 | l_current_data, &l_remaining_size, p_manager)) { |
3002 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n" ); |
3003 | return OPJ_FALSE; |
3004 | } |
3005 | |
3006 | if (l_remaining_size != 0) { |
3007 | opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n" ); |
3008 | return OPJ_FALSE; |
3009 | } |
3010 | |
3011 | if (opj_stream_write_data(p_stream, |
3012 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size, |
3013 | p_manager) != l_qcd_size) { |
3014 | return OPJ_FALSE; |
3015 | } |
3016 | |
3017 | return OPJ_TRUE; |
3018 | } |
3019 | |
3020 | /** |
3021 | * Reads a QCD marker (Quantization defaults) |
3022 | * @param p_header_data the data contained in the QCD box. |
3023 | * @param p_j2k the jpeg2000 codec. |
3024 | * @param p_header_size the size of the data contained in the QCD marker. |
3025 | * @param p_manager the user event manager. |
3026 | */ |
3027 | static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, |
3028 | OPJ_BYTE * , |
3029 | OPJ_UINT32 , |
3030 | opj_event_mgr_t * p_manager |
3031 | ) |
3032 | { |
3033 | /* preconditions */ |
3034 | assert(p_header_data != 00); |
3035 | assert(p_j2k != 00); |
3036 | assert(p_manager != 00); |
3037 | |
3038 | if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size, |
3039 | p_manager)) { |
3040 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n" ); |
3041 | return OPJ_FALSE; |
3042 | } |
3043 | |
3044 | if (p_header_size != 0) { |
3045 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n" ); |
3046 | return OPJ_FALSE; |
3047 | } |
3048 | |
3049 | /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ |
3050 | opj_j2k_copy_tile_quantization_parameters(p_j2k); |
3051 | |
3052 | return OPJ_TRUE; |
3053 | } |
3054 | |
3055 | static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, |
3056 | OPJ_UINT32 p_comp_no, |
3057 | opj_stream_private_t *p_stream, |
3058 | opj_event_mgr_t * p_manager |
3059 | ) |
3060 | { |
3061 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3062 | |
3063 | /* preconditions */ |
3064 | assert(p_j2k != 00); |
3065 | assert(p_manager != 00); |
3066 | assert(p_stream != 00); |
3067 | |
3068 | l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3069 | p_comp_no); |
3070 | l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1; |
3071 | l_remaining_size = l_qcc_size; |
3072 | |
3073 | if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3074 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
3075 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); |
3076 | if (! new_header_tile_data) { |
3077 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3078 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3079 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3080 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n" ); |
3081 | return OPJ_FALSE; |
3082 | } |
3083 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3084 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; |
3085 | } |
3086 | |
3087 | opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no, |
3088 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, |
3089 | p_manager); |
3090 | |
3091 | if (opj_stream_write_data(p_stream, |
3092 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size, |
3093 | p_manager) != l_qcc_size) { |
3094 | return OPJ_FALSE; |
3095 | } |
3096 | |
3097 | return OPJ_TRUE; |
3098 | } |
3099 | |
3100 | static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, |
3101 | OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
3102 | { |
3103 | return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, |
3104 | p_first_comp_no, p_second_comp_no); |
3105 | } |
3106 | |
3107 | static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, |
3108 | OPJ_UINT32 p_comp_no, |
3109 | OPJ_BYTE * p_data, |
3110 | OPJ_UINT32 * p_data_written, |
3111 | opj_event_mgr_t * p_manager |
3112 | ) |
3113 | { |
3114 | OPJ_UINT32 l_qcc_size, l_remaining_size; |
3115 | OPJ_BYTE * l_current_data = 00; |
3116 | |
3117 | /* preconditions */ |
3118 | assert(p_j2k != 00); |
3119 | assert(p_manager != 00); |
3120 | |
3121 | l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, |
3122 | p_comp_no); |
3123 | l_remaining_size = l_qcc_size; |
3124 | |
3125 | l_current_data = p_data; |
3126 | |
3127 | opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */ |
3128 | l_current_data += 2; |
3129 | |
3130 | if (p_j2k->m_private_image->numcomps <= 256) { |
3131 | --l_qcc_size; |
3132 | |
3133 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3134 | l_current_data += 2; |
3135 | |
3136 | opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ |
3137 | ++l_current_data; |
3138 | |
3139 | /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ |
3140 | l_remaining_size -= 6; |
3141 | } else { |
3142 | opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ |
3143 | l_current_data += 2; |
3144 | |
3145 | opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ |
3146 | l_current_data += 2; |
3147 | |
3148 | l_remaining_size -= 6; |
3149 | } |
3150 | |
3151 | opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no, |
3152 | l_current_data, &l_remaining_size, p_manager); |
3153 | |
3154 | *p_data_written = l_qcc_size; |
3155 | } |
3156 | |
3157 | static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k) |
3158 | { |
3159 | return opj_j2k_get_max_coc_size(p_j2k); |
3160 | } |
3161 | |
3162 | /** |
3163 | * Reads a QCC marker (Quantization component) |
3164 | * @param p_header_data the data contained in the QCC box. |
3165 | * @param p_j2k the jpeg2000 codec. |
3166 | * @param p_header_size the size of the data contained in the QCC marker. |
3167 | * @param p_manager the user event manager. |
3168 | */ |
3169 | static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, |
3170 | OPJ_BYTE * , |
3171 | OPJ_UINT32 , |
3172 | opj_event_mgr_t * p_manager |
3173 | ) |
3174 | { |
3175 | OPJ_UINT32 l_num_comp, l_comp_no; |
3176 | |
3177 | /* preconditions */ |
3178 | assert(p_header_data != 00); |
3179 | assert(p_j2k != 00); |
3180 | assert(p_manager != 00); |
3181 | |
3182 | l_num_comp = p_j2k->m_private_image->numcomps; |
3183 | |
3184 | if (l_num_comp <= 256) { |
3185 | if (p_header_size < 1) { |
3186 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n" ); |
3187 | return OPJ_FALSE; |
3188 | } |
3189 | opj_read_bytes(p_header_data, &l_comp_no, 1); |
3190 | ++p_header_data; |
3191 | --p_header_size; |
3192 | } else { |
3193 | if (p_header_size < 2) { |
3194 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n" ); |
3195 | return OPJ_FALSE; |
3196 | } |
3197 | opj_read_bytes(p_header_data, &l_comp_no, 2); |
3198 | p_header_data += 2; |
3199 | p_header_size -= 2; |
3200 | } |
3201 | |
3202 | #ifdef USE_JPWL |
3203 | if (p_j2k->m_cp.correct) { |
3204 | |
3205 | static OPJ_UINT32 backup_compno = 0; |
3206 | |
3207 | /* compno is negative or larger than the number of components!!! */ |
3208 | if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { |
3209 | opj_event_msg(p_manager, EVT_ERROR, |
3210 | "JPWL: bad component number in QCC (%d out of a maximum of %d)\n" , |
3211 | l_comp_no, l_num_comp); |
3212 | if (!JPWL_ASSUME) { |
3213 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
3214 | return OPJ_FALSE; |
3215 | } |
3216 | /* we try to correct */ |
3217 | l_comp_no = backup_compno % l_num_comp; |
3218 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
3219 | "- setting component number to %d\n" , |
3220 | l_comp_no); |
3221 | } |
3222 | |
3223 | /* keep your private count of tiles */ |
3224 | backup_compno++; |
3225 | }; |
3226 | #endif /* USE_JPWL */ |
3227 | |
3228 | if (l_comp_no >= p_j2k->m_private_image->numcomps) { |
3229 | opj_event_msg(p_manager, EVT_ERROR, |
3230 | "Invalid component number: %d, regarding the number of components %d\n" , |
3231 | l_comp_no, p_j2k->m_private_image->numcomps); |
3232 | return OPJ_FALSE; |
3233 | } |
3234 | |
3235 | if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size, |
3236 | p_manager)) { |
3237 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n" ); |
3238 | return OPJ_FALSE; |
3239 | } |
3240 | |
3241 | if (p_header_size != 0) { |
3242 | opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n" ); |
3243 | return OPJ_FALSE; |
3244 | } |
3245 | |
3246 | return OPJ_TRUE; |
3247 | } |
3248 | |
3249 | static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, |
3250 | opj_stream_private_t *p_stream, |
3251 | opj_event_mgr_t * p_manager |
3252 | ) |
3253 | { |
3254 | OPJ_UINT32 l_nb_comp; |
3255 | OPJ_UINT32 l_nb_poc; |
3256 | OPJ_UINT32 l_poc_size; |
3257 | OPJ_UINT32 l_written_size = 0; |
3258 | opj_tcp_t *l_tcp = 00; |
3259 | OPJ_UINT32 l_poc_room; |
3260 | |
3261 | /* preconditions */ |
3262 | assert(p_j2k != 00); |
3263 | assert(p_manager != 00); |
3264 | assert(p_stream != 00); |
3265 | |
3266 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3267 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3268 | l_nb_poc = 1 + l_tcp->numpocs; |
3269 | |
3270 | if (l_nb_comp <= 256) { |
3271 | l_poc_room = 1; |
3272 | } else { |
3273 | l_poc_room = 2; |
3274 | } |
3275 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3276 | |
3277 | if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
3278 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
3279 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); |
3280 | if (! new_header_tile_data) { |
3281 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
3282 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
3283 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
3284 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n" ); |
3285 | return OPJ_FALSE; |
3286 | } |
3287 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
3288 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; |
3289 | } |
3290 | |
3291 | opj_j2k_write_poc_in_memory(p_j2k, |
3292 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size, |
3293 | p_manager); |
3294 | |
3295 | if (opj_stream_write_data(p_stream, |
3296 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size, |
3297 | p_manager) != l_poc_size) { |
3298 | return OPJ_FALSE; |
3299 | } |
3300 | |
3301 | return OPJ_TRUE; |
3302 | } |
3303 | |
3304 | static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, |
3305 | OPJ_BYTE * p_data, |
3306 | OPJ_UINT32 * p_data_written, |
3307 | opj_event_mgr_t * p_manager |
3308 | ) |
3309 | { |
3310 | OPJ_UINT32 i; |
3311 | OPJ_BYTE * l_current_data = 00; |
3312 | OPJ_UINT32 l_nb_comp; |
3313 | OPJ_UINT32 l_nb_poc; |
3314 | OPJ_UINT32 l_poc_size; |
3315 | opj_image_t *l_image = 00; |
3316 | opj_tcp_t *l_tcp = 00; |
3317 | opj_tccp_t *l_tccp = 00; |
3318 | opj_poc_t *l_current_poc = 00; |
3319 | OPJ_UINT32 l_poc_room; |
3320 | |
3321 | /* preconditions */ |
3322 | assert(p_j2k != 00); |
3323 | assert(p_manager != 00); |
3324 | |
3325 | OPJ_UNUSED(p_manager); |
3326 | |
3327 | l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; |
3328 | l_tccp = &l_tcp->tccps[0]; |
3329 | l_image = p_j2k->m_private_image; |
3330 | l_nb_comp = l_image->numcomps; |
3331 | l_nb_poc = 1 + l_tcp->numpocs; |
3332 | |
3333 | if (l_nb_comp <= 256) { |
3334 | l_poc_room = 1; |
3335 | } else { |
3336 | l_poc_room = 2; |
3337 | } |
3338 | |
3339 | l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; |
3340 | |
3341 | l_current_data = p_data; |
3342 | |
3343 | opj_write_bytes(l_current_data, J2K_MS_POC, |
3344 | 2); /* POC */ |
3345 | l_current_data += 2; |
3346 | |
3347 | opj_write_bytes(l_current_data, l_poc_size - 2, |
3348 | 2); /* Lpoc */ |
3349 | l_current_data += 2; |
3350 | |
3351 | l_current_poc = l_tcp->pocs; |
3352 | for (i = 0; i < l_nb_poc; ++i) { |
3353 | opj_write_bytes(l_current_data, l_current_poc->resno0, |
3354 | 1); /* RSpoc_i */ |
3355 | ++l_current_data; |
3356 | |
3357 | opj_write_bytes(l_current_data, l_current_poc->compno0, |
3358 | l_poc_room); /* CSpoc_i */ |
3359 | l_current_data += l_poc_room; |
3360 | |
3361 | opj_write_bytes(l_current_data, l_current_poc->layno1, |
3362 | 2); /* LYEpoc_i */ |
3363 | l_current_data += 2; |
3364 | |
3365 | opj_write_bytes(l_current_data, l_current_poc->resno1, |
3366 | 1); /* REpoc_i */ |
3367 | ++l_current_data; |
3368 | |
3369 | opj_write_bytes(l_current_data, l_current_poc->compno1, |
3370 | l_poc_room); /* CEpoc_i */ |
3371 | l_current_data += l_poc_room; |
3372 | |
3373 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg, |
3374 | 1); /* Ppoc_i */ |
3375 | ++l_current_data; |
3376 | |
3377 | /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ |
3378 | l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3379 | l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers); |
3380 | l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3381 | l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions); |
3382 | l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) |
3383 | l_current_poc->compno1, (OPJ_INT32)l_nb_comp); |
3384 | |
3385 | ++l_current_poc; |
3386 | } |
3387 | |
3388 | *p_data_written = l_poc_size; |
3389 | } |
3390 | |
3391 | static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k) |
3392 | { |
3393 | opj_tcp_t * l_tcp = 00; |
3394 | OPJ_UINT32 l_nb_tiles = 0; |
3395 | OPJ_UINT32 l_max_poc = 0; |
3396 | OPJ_UINT32 i; |
3397 | |
3398 | l_tcp = p_j2k->m_cp.tcps; |
3399 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
3400 | |
3401 | for (i = 0; i < l_nb_tiles; ++i) { |
3402 | l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs); |
3403 | ++l_tcp; |
3404 | } |
3405 | |
3406 | ++l_max_poc; |
3407 | |
3408 | return 4 + 9 * l_max_poc; |
3409 | } |
3410 | |
3411 | static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k) |
3412 | { |
3413 | OPJ_UINT32 i; |
3414 | OPJ_UINT32 l_nb_tiles; |
3415 | OPJ_UINT32 l_max = 0; |
3416 | opj_tcp_t * l_tcp = 00; |
3417 | |
3418 | l_tcp = p_j2k->m_cp.tcps; |
3419 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; |
3420 | |
3421 | for (i = 0; i < l_nb_tiles; ++i) { |
3422 | l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts); |
3423 | |
3424 | ++l_tcp; |
3425 | } |
3426 | |
3427 | return 12 * l_max; |
3428 | } |
3429 | |
3430 | static OPJ_UINT32 (opj_j2k_t *p_j2k) |
3431 | { |
3432 | OPJ_UINT32 l_nb_bytes = 0; |
3433 | OPJ_UINT32 l_nb_comps; |
3434 | OPJ_UINT32 l_coc_bytes, l_qcc_bytes; |
3435 | |
3436 | l_nb_comps = p_j2k->m_private_image->numcomps - 1; |
3437 | l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); |
3438 | |
3439 | if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) { |
3440 | l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); |
3441 | l_nb_bytes += l_nb_comps * l_coc_bytes; |
3442 | |
3443 | l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); |
3444 | l_nb_bytes += l_nb_comps * l_qcc_bytes; |
3445 | } |
3446 | |
3447 | l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); |
3448 | |
3449 | /*** DEVELOPER CORNER, Add room for your headers ***/ |
3450 | |
3451 | return l_nb_bytes; |
3452 | } |
3453 | |
3454 | /** |
3455 | * Reads a POC marker (Progression Order Change) |
3456 | * |
3457 | * @param p_header_data the data contained in the POC box. |
3458 | * @param p_j2k the jpeg2000 codec. |
3459 | * @param p_header_size the size of the data contained in the POC marker. |
3460 | * @param p_manager the user event manager. |
3461 | */ |
3462 | static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, |
3463 | OPJ_BYTE * , |
3464 | OPJ_UINT32 , |
3465 | opj_event_mgr_t * p_manager |
3466 | ) |
3467 | { |
3468 | OPJ_UINT32 i, l_nb_comp, l_tmp; |
3469 | opj_image_t * l_image = 00; |
3470 | OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; |
3471 | OPJ_UINT32 l_chunk_size, l_comp_room; |
3472 | |
3473 | opj_cp_t *l_cp = 00; |
3474 | opj_tcp_t *l_tcp = 00; |
3475 | opj_poc_t *l_current_poc = 00; |
3476 | |
3477 | /* preconditions */ |
3478 | assert(p_header_data != 00); |
3479 | assert(p_j2k != 00); |
3480 | assert(p_manager != 00); |
3481 | |
3482 | l_image = p_j2k->m_private_image; |
3483 | l_nb_comp = l_image->numcomps; |
3484 | if (l_nb_comp <= 256) { |
3485 | l_comp_room = 1; |
3486 | } else { |
3487 | l_comp_room = 2; |
3488 | } |
3489 | l_chunk_size = 5 + 2 * l_comp_room; |
3490 | l_current_poc_nb = p_header_size / l_chunk_size; |
3491 | l_current_poc_remaining = p_header_size % l_chunk_size; |
3492 | |
3493 | if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { |
3494 | opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n" ); |
3495 | return OPJ_FALSE; |
3496 | } |
3497 | |
3498 | l_cp = &(p_j2k->m_cp); |
3499 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
3500 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
3501 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
3502 | l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; |
3503 | l_current_poc_nb += l_old_poc_nb; |
3504 | |
3505 | if (l_current_poc_nb >= sizeof(l_tcp->pocs) / sizeof(l_tcp->pocs[0])) { |
3506 | opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n" , l_current_poc_nb); |
3507 | return OPJ_FALSE; |
3508 | } |
3509 | |
3510 | /* now poc is in use.*/ |
3511 | l_tcp->POC = 1; |
3512 | |
3513 | l_current_poc = &l_tcp->pocs[l_old_poc_nb]; |
3514 | for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { |
3515 | opj_read_bytes(p_header_data, &(l_current_poc->resno0), |
3516 | 1); /* RSpoc_i */ |
3517 | ++p_header_data; |
3518 | opj_read_bytes(p_header_data, &(l_current_poc->compno0), |
3519 | l_comp_room); /* CSpoc_i */ |
3520 | p_header_data += l_comp_room; |
3521 | opj_read_bytes(p_header_data, &(l_current_poc->layno1), |
3522 | 2); /* LYEpoc_i */ |
3523 | /* make sure layer end is in acceptable bounds */ |
3524 | l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); |
3525 | p_header_data += 2; |
3526 | opj_read_bytes(p_header_data, &(l_current_poc->resno1), |
3527 | 1); /* REpoc_i */ |
3528 | ++p_header_data; |
3529 | opj_read_bytes(p_header_data, &(l_current_poc->compno1), |
3530 | l_comp_room); /* CEpoc_i */ |
3531 | p_header_data += l_comp_room; |
3532 | opj_read_bytes(p_header_data, &l_tmp, |
3533 | 1); /* Ppoc_i */ |
3534 | ++p_header_data; |
3535 | l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; |
3536 | /* make sure comp is in acceptable bounds */ |
3537 | l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); |
3538 | ++l_current_poc; |
3539 | } |
3540 | |
3541 | l_tcp->numpocs = l_current_poc_nb - 1; |
3542 | return OPJ_TRUE; |
3543 | } |
3544 | |
3545 | /** |
3546 | * Reads a CRG marker (Component registration) |
3547 | * |
3548 | * @param p_header_data the data contained in the TLM box. |
3549 | * @param p_j2k the jpeg2000 codec. |
3550 | * @param p_header_size the size of the data contained in the TLM marker. |
3551 | * @param p_manager the user event manager. |
3552 | */ |
3553 | static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, |
3554 | OPJ_BYTE * , |
3555 | OPJ_UINT32 , |
3556 | opj_event_mgr_t * p_manager |
3557 | ) |
3558 | { |
3559 | OPJ_UINT32 l_nb_comp; |
3560 | /* preconditions */ |
3561 | assert(p_header_data != 00); |
3562 | assert(p_j2k != 00); |
3563 | assert(p_manager != 00); |
3564 | |
3565 | OPJ_UNUSED(p_header_data); |
3566 | |
3567 | l_nb_comp = p_j2k->m_private_image->numcomps; |
3568 | |
3569 | if (p_header_size != l_nb_comp * 4) { |
3570 | opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n" ); |
3571 | return OPJ_FALSE; |
3572 | } |
3573 | /* Do not care of this at the moment since only local variables are set here */ |
3574 | /* |
3575 | for |
3576 | (i = 0; i < l_nb_comp; ++i) |
3577 | { |
3578 | opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i |
3579 | p_header_data+=2; |
3580 | opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i |
3581 | p_header_data+=2; |
3582 | } |
3583 | */ |
3584 | return OPJ_TRUE; |
3585 | } |
3586 | |
3587 | /** |
3588 | * Reads a TLM marker (Tile Length Marker) |
3589 | * |
3590 | * @param p_header_data the data contained in the TLM box. |
3591 | * @param p_j2k the jpeg2000 codec. |
3592 | * @param p_header_size the size of the data contained in the TLM marker. |
3593 | * @param p_manager the user event manager. |
3594 | */ |
3595 | static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, |
3596 | OPJ_BYTE * , |
3597 | OPJ_UINT32 , |
3598 | opj_event_mgr_t * p_manager |
3599 | ) |
3600 | { |
3601 | OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, |
3602 | l_Ptlm_size; |
3603 | /* preconditions */ |
3604 | assert(p_header_data != 00); |
3605 | assert(p_j2k != 00); |
3606 | assert(p_manager != 00); |
3607 | |
3608 | OPJ_UNUSED(p_j2k); |
3609 | |
3610 | if (p_header_size < 2) { |
3611 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n" ); |
3612 | return OPJ_FALSE; |
3613 | } |
3614 | p_header_size -= 2; |
3615 | |
3616 | opj_read_bytes(p_header_data, &l_Ztlm, |
3617 | 1); /* Ztlm */ |
3618 | ++p_header_data; |
3619 | opj_read_bytes(p_header_data, &l_Stlm, |
3620 | 1); /* Stlm */ |
3621 | ++p_header_data; |
3622 | |
3623 | l_ST = ((l_Stlm >> 4) & 0x3); |
3624 | l_SP = (l_Stlm >> 6) & 0x1; |
3625 | |
3626 | l_Ptlm_size = (l_SP + 1) * 2; |
3627 | l_quotient = l_Ptlm_size + l_ST; |
3628 | |
3629 | l_tot_num_tp_remaining = p_header_size % l_quotient; |
3630 | |
3631 | if (l_tot_num_tp_remaining != 0) { |
3632 | opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n" ); |
3633 | return OPJ_FALSE; |
3634 | } |
3635 | /* FIXME Do not care of this at the moment since only local variables are set here */ |
3636 | /* |
3637 | for |
3638 | (i = 0; i < l_tot_num_tp; ++i) |
3639 | { |
3640 | opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i |
3641 | p_header_data += l_ST; |
3642 | opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i |
3643 | p_header_data += l_Ptlm_size; |
3644 | }*/ |
3645 | return OPJ_TRUE; |
3646 | } |
3647 | |
3648 | /** |
3649 | * Reads a PLM marker (Packet length, main header marker) |
3650 | * |
3651 | * @param p_header_data the data contained in the TLM box. |
3652 | * @param p_j2k the jpeg2000 codec. |
3653 | * @param p_header_size the size of the data contained in the TLM marker. |
3654 | * @param p_manager the user event manager. |
3655 | */ |
3656 | static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, |
3657 | OPJ_BYTE * , |
3658 | OPJ_UINT32 , |
3659 | opj_event_mgr_t * p_manager |
3660 | ) |
3661 | { |
3662 | /* preconditions */ |
3663 | assert(p_header_data != 00); |
3664 | assert(p_j2k != 00); |
3665 | assert(p_manager != 00); |
3666 | |
3667 | OPJ_UNUSED(p_j2k); |
3668 | OPJ_UNUSED(p_header_data); |
3669 | |
3670 | if (p_header_size < 1) { |
3671 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n" ); |
3672 | return OPJ_FALSE; |
3673 | } |
3674 | /* Do not care of this at the moment since only local variables are set here */ |
3675 | /* |
3676 | opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm |
3677 | ++p_header_data; |
3678 | --p_header_size; |
3679 | |
3680 | while |
3681 | (p_header_size > 0) |
3682 | { |
3683 | opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm |
3684 | ++p_header_data; |
3685 | p_header_size -= (1+l_Nplm); |
3686 | if |
3687 | (p_header_size < 0) |
3688 | { |
3689 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3690 | return false; |
3691 | } |
3692 | for |
3693 | (i = 0; i < l_Nplm; ++i) |
3694 | { |
3695 | opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij |
3696 | ++p_header_data; |
3697 | // take only the last seven bytes |
3698 | l_packet_len |= (l_tmp & 0x7f); |
3699 | if |
3700 | (l_tmp & 0x80) |
3701 | { |
3702 | l_packet_len <<= 7; |
3703 | } |
3704 | else |
3705 | { |
3706 | // store packet length and proceed to next packet |
3707 | l_packet_len = 0; |
3708 | } |
3709 | } |
3710 | if |
3711 | (l_packet_len != 0) |
3712 | { |
3713 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); |
3714 | return false; |
3715 | } |
3716 | } |
3717 | */ |
3718 | return OPJ_TRUE; |
3719 | } |
3720 | |
3721 | /** |
3722 | * Reads a PLT marker (Packet length, tile-part header) |
3723 | * |
3724 | * @param p_header_data the data contained in the PLT box. |
3725 | * @param p_j2k the jpeg2000 codec. |
3726 | * @param p_header_size the size of the data contained in the PLT marker. |
3727 | * @param p_manager the user event manager. |
3728 | */ |
3729 | static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, |
3730 | OPJ_BYTE * , |
3731 | OPJ_UINT32 , |
3732 | opj_event_mgr_t * p_manager |
3733 | ) |
3734 | { |
3735 | OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; |
3736 | |
3737 | /* preconditions */ |
3738 | assert(p_header_data != 00); |
3739 | assert(p_j2k != 00); |
3740 | assert(p_manager != 00); |
3741 | |
3742 | OPJ_UNUSED(p_j2k); |
3743 | |
3744 | if (p_header_size < 1) { |
3745 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n" ); |
3746 | return OPJ_FALSE; |
3747 | } |
3748 | |
3749 | opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */ |
3750 | ++p_header_data; |
3751 | --p_header_size; |
3752 | |
3753 | for (i = 0; i < p_header_size; ++i) { |
3754 | opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */ |
3755 | ++p_header_data; |
3756 | /* take only the last seven bytes */ |
3757 | l_packet_len |= (l_tmp & 0x7f); |
3758 | if (l_tmp & 0x80) { |
3759 | l_packet_len <<= 7; |
3760 | } else { |
3761 | /* store packet length and proceed to next packet */ |
3762 | l_packet_len = 0; |
3763 | } |
3764 | } |
3765 | |
3766 | if (l_packet_len != 0) { |
3767 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n" ); |
3768 | return OPJ_FALSE; |
3769 | } |
3770 | |
3771 | return OPJ_TRUE; |
3772 | } |
3773 | |
3774 | /** |
3775 | * Reads a PPM marker (Packed packet headers, main header) |
3776 | * |
3777 | * @param p_header_data the data contained in the POC box. |
3778 | * @param p_j2k the jpeg2000 codec. |
3779 | * @param p_header_size the size of the data contained in the POC marker. |
3780 | * @param p_manager the user event manager. |
3781 | */ |
3782 | |
3783 | static OPJ_BOOL opj_j2k_read_ppm( |
3784 | opj_j2k_t *p_j2k, |
3785 | OPJ_BYTE * , |
3786 | OPJ_UINT32 , |
3787 | opj_event_mgr_t * p_manager) |
3788 | { |
3789 | opj_cp_t *l_cp = 00; |
3790 | OPJ_UINT32 l_Z_ppm; |
3791 | |
3792 | /* preconditions */ |
3793 | assert(p_header_data != 00); |
3794 | assert(p_j2k != 00); |
3795 | assert(p_manager != 00); |
3796 | |
3797 | /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ |
3798 | if (p_header_size < 2) { |
3799 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n" ); |
3800 | return OPJ_FALSE; |
3801 | } |
3802 | |
3803 | l_cp = &(p_j2k->m_cp); |
3804 | l_cp->ppm = 1; |
3805 | |
3806 | opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */ |
3807 | ++p_header_data; |
3808 | --p_header_size; |
3809 | |
3810 | /* check allocation needed */ |
3811 | if (l_cp->ppm_markers == NULL) { /* first PPM marker */ |
3812 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3813 | assert(l_cp->ppm_markers_count == 0U); |
3814 | |
3815 | l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
3816 | if (l_cp->ppm_markers == NULL) { |
3817 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n" ); |
3818 | return OPJ_FALSE; |
3819 | } |
3820 | l_cp->ppm_markers_count = l_newCount; |
3821 | } else if (l_cp->ppm_markers_count <= l_Z_ppm) { |
3822 | OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ |
3823 | opj_ppx *new_ppm_markers; |
3824 | new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, |
3825 | l_newCount * sizeof(opj_ppx)); |
3826 | if (new_ppm_markers == NULL) { |
3827 | /* clean up to be done on l_cp destruction */ |
3828 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n" ); |
3829 | return OPJ_FALSE; |
3830 | } |
3831 | l_cp->ppm_markers = new_ppm_markers; |
3832 | memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, |
3833 | (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx)); |
3834 | l_cp->ppm_markers_count = l_newCount; |
3835 | } |
3836 | |
3837 | if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { |
3838 | /* clean up to be done on l_cp destruction */ |
3839 | opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n" , l_Z_ppm); |
3840 | return OPJ_FALSE; |
3841 | } |
3842 | |
3843 | l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
3844 | if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) { |
3845 | /* clean up to be done on l_cp destruction */ |
3846 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n" ); |
3847 | return OPJ_FALSE; |
3848 | } |
3849 | l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; |
3850 | memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size); |
3851 | |
3852 | return OPJ_TRUE; |
3853 | } |
3854 | |
3855 | /** |
3856 | * Merges all PPM markers read (Packed headers, main header) |
3857 | * |
3858 | * @param p_cp main coding parameters. |
3859 | * @param p_manager the user event manager. |
3860 | */ |
3861 | static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager) |
3862 | { |
3863 | OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; |
3864 | |
3865 | /* preconditions */ |
3866 | assert(p_cp != 00); |
3867 | assert(p_manager != 00); |
3868 | assert(p_cp->ppm_buffer == NULL); |
3869 | |
3870 | if (p_cp->ppm == 0U) { |
3871 | return OPJ_TRUE; |
3872 | } |
3873 | |
3874 | l_ppm_data_size = 0U; |
3875 | l_N_ppm_remaining = 0U; |
3876 | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
3877 | if (p_cp->ppm_markers[i].m_data != |
3878 | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
3879 | OPJ_UINT32 l_N_ppm; |
3880 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
3881 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
3882 | |
3883 | if (l_N_ppm_remaining >= l_data_size) { |
3884 | l_N_ppm_remaining -= l_data_size; |
3885 | l_data_size = 0U; |
3886 | } else { |
3887 | l_data += l_N_ppm_remaining; |
3888 | l_data_size -= l_N_ppm_remaining; |
3889 | l_N_ppm_remaining = 0U; |
3890 | } |
3891 | |
3892 | if (l_data_size > 0U) { |
3893 | do { |
3894 | /* read Nppm */ |
3895 | if (l_data_size < 4U) { |
3896 | /* clean up to be done on l_cp destruction */ |
3897 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n" ); |
3898 | return OPJ_FALSE; |
3899 | } |
3900 | opj_read_bytes(l_data, &l_N_ppm, 4); |
3901 | l_data += 4; |
3902 | l_data_size -= 4; |
3903 | l_ppm_data_size += |
3904 | l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */ |
3905 | |
3906 | if (l_data_size >= l_N_ppm) { |
3907 | l_data_size -= l_N_ppm; |
3908 | l_data += l_N_ppm; |
3909 | } else { |
3910 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
3911 | l_data_size = 0U; |
3912 | } |
3913 | } while (l_data_size > 0U); |
3914 | } |
3915 | } |
3916 | } |
3917 | |
3918 | if (l_N_ppm_remaining != 0U) { |
3919 | /* clean up to be done on l_cp destruction */ |
3920 | opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n" ); |
3921 | return OPJ_FALSE; |
3922 | } |
3923 | |
3924 | p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); |
3925 | if (p_cp->ppm_buffer == 00) { |
3926 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n" ); |
3927 | return OPJ_FALSE; |
3928 | } |
3929 | p_cp->ppm_len = l_ppm_data_size; |
3930 | l_ppm_data_size = 0U; |
3931 | l_N_ppm_remaining = 0U; |
3932 | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
3933 | if (p_cp->ppm_markers[i].m_data != |
3934 | NULL) { /* standard doesn't seem to require contiguous Zppm */ |
3935 | OPJ_UINT32 l_N_ppm; |
3936 | OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; |
3937 | const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; |
3938 | |
3939 | if (l_N_ppm_remaining >= l_data_size) { |
3940 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
3941 | l_ppm_data_size += l_data_size; |
3942 | l_N_ppm_remaining -= l_data_size; |
3943 | l_data_size = 0U; |
3944 | } else { |
3945 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining); |
3946 | l_ppm_data_size += l_N_ppm_remaining; |
3947 | l_data += l_N_ppm_remaining; |
3948 | l_data_size -= l_N_ppm_remaining; |
3949 | l_N_ppm_remaining = 0U; |
3950 | } |
3951 | |
3952 | if (l_data_size > 0U) { |
3953 | do { |
3954 | /* read Nppm */ |
3955 | if (l_data_size < 4U) { |
3956 | /* clean up to be done on l_cp destruction */ |
3957 | opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n" ); |
3958 | return OPJ_FALSE; |
3959 | } |
3960 | opj_read_bytes(l_data, &l_N_ppm, 4); |
3961 | l_data += 4; |
3962 | l_data_size -= 4; |
3963 | |
3964 | if (l_data_size >= l_N_ppm) { |
3965 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm); |
3966 | l_ppm_data_size += l_N_ppm; |
3967 | l_data_size -= l_N_ppm; |
3968 | l_data += l_N_ppm; |
3969 | } else { |
3970 | memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); |
3971 | l_ppm_data_size += l_data_size; |
3972 | l_N_ppm_remaining = l_N_ppm - l_data_size; |
3973 | l_data_size = 0U; |
3974 | } |
3975 | } while (l_data_size > 0U); |
3976 | } |
3977 | opj_free(p_cp->ppm_markers[i].m_data); |
3978 | p_cp->ppm_markers[i].m_data = NULL; |
3979 | p_cp->ppm_markers[i].m_data_size = 0U; |
3980 | } |
3981 | } |
3982 | |
3983 | p_cp->ppm_data = p_cp->ppm_buffer; |
3984 | p_cp->ppm_data_size = p_cp->ppm_len; |
3985 | |
3986 | p_cp->ppm_markers_count = 0U; |
3987 | opj_free(p_cp->ppm_markers); |
3988 | p_cp->ppm_markers = NULL; |
3989 | |
3990 | return OPJ_TRUE; |
3991 | } |
3992 | |
3993 | /** |
3994 | * Reads a PPT marker (Packed packet headers, tile-part header) |
3995 | * |
3996 | * @param p_header_data the data contained in the PPT box. |
3997 | * @param p_j2k the jpeg2000 codec. |
3998 | * @param p_header_size the size of the data contained in the PPT marker. |
3999 | * @param p_manager the user event manager. |
4000 | */ |
4001 | static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, |
4002 | OPJ_BYTE * , |
4003 | OPJ_UINT32 , |
4004 | opj_event_mgr_t * p_manager |
4005 | ) |
4006 | { |
4007 | opj_cp_t *l_cp = 00; |
4008 | opj_tcp_t *l_tcp = 00; |
4009 | OPJ_UINT32 l_Z_ppt; |
4010 | |
4011 | /* preconditions */ |
4012 | assert(p_header_data != 00); |
4013 | assert(p_j2k != 00); |
4014 | assert(p_manager != 00); |
4015 | |
4016 | /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ |
4017 | if (p_header_size < 2) { |
4018 | opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n" ); |
4019 | return OPJ_FALSE; |
4020 | } |
4021 | |
4022 | l_cp = &(p_j2k->m_cp); |
4023 | if (l_cp->ppm) { |
4024 | opj_event_msg(p_manager, EVT_ERROR, |
4025 | "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n" ); |
4026 | return OPJ_FALSE; |
4027 | } |
4028 | |
4029 | l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); |
4030 | l_tcp->ppt = 1; |
4031 | |
4032 | opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */ |
4033 | ++p_header_data; |
4034 | --p_header_size; |
4035 | |
4036 | /* check allocation needed */ |
4037 | if (l_tcp->ppt_markers == NULL) { /* first PPT marker */ |
4038 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4039 | assert(l_tcp->ppt_markers_count == 0U); |
4040 | |
4041 | l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); |
4042 | if (l_tcp->ppt_markers == NULL) { |
4043 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n" ); |
4044 | return OPJ_FALSE; |
4045 | } |
4046 | l_tcp->ppt_markers_count = l_newCount; |
4047 | } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { |
4048 | OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ |
4049 | opj_ppx *new_ppt_markers; |
4050 | new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, |
4051 | l_newCount * sizeof(opj_ppx)); |
4052 | if (new_ppt_markers == NULL) { |
4053 | /* clean up to be done on l_tcp destruction */ |
4054 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n" ); |
4055 | return OPJ_FALSE; |
4056 | } |
4057 | l_tcp->ppt_markers = new_ppt_markers; |
4058 | memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, |
4059 | (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx)); |
4060 | l_tcp->ppt_markers_count = l_newCount; |
4061 | } |
4062 | |
4063 | if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { |
4064 | /* clean up to be done on l_tcp destruction */ |
4065 | opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n" , l_Z_ppt); |
4066 | return OPJ_FALSE; |
4067 | } |
4068 | |
4069 | l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); |
4070 | if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) { |
4071 | /* clean up to be done on l_tcp destruction */ |
4072 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n" ); |
4073 | return OPJ_FALSE; |
4074 | } |
4075 | l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; |
4076 | memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size); |
4077 | return OPJ_TRUE; |
4078 | } |
4079 | |
4080 | /** |
4081 | * Merges all PPT markers read (Packed packet headers, tile-part header) |
4082 | * |
4083 | * @param p_tcp the tile. |
4084 | * @param p_manager the user event manager. |
4085 | */ |
4086 | static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) |
4087 | { |
4088 | OPJ_UINT32 i, l_ppt_data_size; |
4089 | /* preconditions */ |
4090 | assert(p_tcp != 00); |
4091 | assert(p_manager != 00); |
4092 | assert(p_tcp->ppt_buffer == NULL); |
4093 | |
4094 | if (p_tcp->ppt == 0U) { |
4095 | return OPJ_TRUE; |
4096 | } |
4097 | |
4098 | l_ppt_data_size = 0U; |
4099 | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4100 | l_ppt_data_size += |
4101 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4102 | } |
4103 | |
4104 | p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); |
4105 | if (p_tcp->ppt_buffer == 00) { |
4106 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n" ); |
4107 | return OPJ_FALSE; |
4108 | } |
4109 | p_tcp->ppt_len = l_ppt_data_size; |
4110 | l_ppt_data_size = 0U; |
4111 | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
4112 | if (p_tcp->ppt_markers[i].m_data != |
4113 | NULL) { /* standard doesn't seem to require contiguous Zppt */ |
4114 | memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, |
4115 | p_tcp->ppt_markers[i].m_data_size); |
4116 | l_ppt_data_size += |
4117 | p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ |
4118 | |
4119 | opj_free(p_tcp->ppt_markers[i].m_data); |
4120 | p_tcp->ppt_markers[i].m_data = NULL; |
4121 | p_tcp->ppt_markers[i].m_data_size = 0U; |
4122 | } |
4123 | } |
4124 | |
4125 | p_tcp->ppt_markers_count = 0U; |
4126 | opj_free(p_tcp->ppt_markers); |
4127 | p_tcp->ppt_markers = NULL; |
4128 | |
4129 | p_tcp->ppt_data = p_tcp->ppt_buffer; |
4130 | p_tcp->ppt_data_size = p_tcp->ppt_len; |
4131 | return OPJ_TRUE; |
4132 | } |
4133 | |
4134 | static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, |
4135 | opj_stream_private_t *p_stream, |
4136 | opj_event_mgr_t * p_manager |
4137 | ) |
4138 | { |
4139 | OPJ_BYTE * l_current_data = 00; |
4140 | OPJ_UINT32 l_tlm_size; |
4141 | |
4142 | /* preconditions */ |
4143 | assert(p_j2k != 00); |
4144 | assert(p_manager != 00); |
4145 | assert(p_stream != 00); |
4146 | |
4147 | l_tlm_size = 6 + (5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
4148 | |
4149 | if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
4150 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
4151 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); |
4152 | if (! new_header_tile_data) { |
4153 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
4154 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
4155 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
4156 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n" ); |
4157 | return OPJ_FALSE; |
4158 | } |
4159 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
4160 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; |
4161 | } |
4162 | |
4163 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
4164 | |
4165 | /* change the way data is written to avoid seeking if possible */ |
4166 | /* TODO */ |
4167 | p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); |
4168 | |
4169 | opj_write_bytes(l_current_data, J2K_MS_TLM, |
4170 | 2); /* TLM */ |
4171 | l_current_data += 2; |
4172 | |
4173 | opj_write_bytes(l_current_data, l_tlm_size - 2, |
4174 | 2); /* Lpoc */ |
4175 | l_current_data += 2; |
4176 | |
4177 | opj_write_bytes(l_current_data, 0, |
4178 | 1); /* Ztlm=0*/ |
4179 | ++l_current_data; |
4180 | |
4181 | opj_write_bytes(l_current_data, 0x50, |
4182 | 1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ |
4183 | ++l_current_data; |
4184 | |
4185 | /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ |
4186 | if (opj_stream_write_data(p_stream, |
4187 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size, |
4188 | p_manager) != l_tlm_size) { |
4189 | return OPJ_FALSE; |
4190 | } |
4191 | |
4192 | return OPJ_TRUE; |
4193 | } |
4194 | |
4195 | static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, |
4196 | OPJ_BYTE * p_data, |
4197 | OPJ_UINT32 p_total_data_size, |
4198 | OPJ_UINT32 * p_data_written, |
4199 | const opj_stream_private_t *p_stream, |
4200 | opj_event_mgr_t * p_manager |
4201 | ) |
4202 | { |
4203 | /* preconditions */ |
4204 | assert(p_j2k != 00); |
4205 | assert(p_manager != 00); |
4206 | assert(p_stream != 00); |
4207 | |
4208 | OPJ_UNUSED(p_stream); |
4209 | |
4210 | if (p_total_data_size < 12) { |
4211 | opj_event_msg(p_manager, EVT_ERROR, |
4212 | "Not enough bytes in output buffer to write SOT marker\n" ); |
4213 | return OPJ_FALSE; |
4214 | } |
4215 | |
4216 | opj_write_bytes(p_data, J2K_MS_SOT, |
4217 | 2); /* SOT */ |
4218 | p_data += 2; |
4219 | |
4220 | opj_write_bytes(p_data, 10, |
4221 | 2); /* Lsot */ |
4222 | p_data += 2; |
4223 | |
4224 | opj_write_bytes(p_data, p_j2k->m_current_tile_number, |
4225 | 2); /* Isot */ |
4226 | p_data += 2; |
4227 | |
4228 | /* Psot */ |
4229 | p_data += 4; |
4230 | |
4231 | opj_write_bytes(p_data, |
4232 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number, |
4233 | 1); /* TPsot */ |
4234 | ++p_data; |
4235 | |
4236 | opj_write_bytes(p_data, |
4237 | p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts, |
4238 | 1); /* TNsot */ |
4239 | ++p_data; |
4240 | |
4241 | /* UniPG>> */ |
4242 | #ifdef USE_JPWL |
4243 | /* update markers struct */ |
4244 | /* |
4245 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); |
4246 | */ |
4247 | assert(0 && "TODO" ); |
4248 | #endif /* USE_JPWL */ |
4249 | |
4250 | * p_data_written = 12; |
4251 | |
4252 | return OPJ_TRUE; |
4253 | } |
4254 | |
4255 | static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * , |
4256 | OPJ_UINT32 , |
4257 | OPJ_UINT32* p_tile_no, |
4258 | OPJ_UINT32* p_tot_len, |
4259 | OPJ_UINT32* p_current_part, |
4260 | OPJ_UINT32* p_num_parts, |
4261 | opj_event_mgr_t * p_manager) |
4262 | { |
4263 | /* preconditions */ |
4264 | assert(p_header_data != 00); |
4265 | assert(p_manager != 00); |
4266 | |
4267 | /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ |
4268 | if (p_header_size != 8) { |
4269 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n" ); |
4270 | return OPJ_FALSE; |
4271 | } |
4272 | |
4273 | opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */ |
4274 | p_header_data += 2; |
4275 | opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */ |
4276 | p_header_data += 4; |
4277 | opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */ |
4278 | ++p_header_data; |
4279 | opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */ |
4280 | ++p_header_data; |
4281 | return OPJ_TRUE; |
4282 | } |
4283 | |
4284 | static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, |
4285 | OPJ_BYTE * , |
4286 | OPJ_UINT32 , |
4287 | opj_event_mgr_t * p_manager) |
4288 | { |
4289 | opj_cp_t *l_cp = 00; |
4290 | opj_tcp_t *l_tcp = 00; |
4291 | OPJ_UINT32 l_tot_len, l_num_parts = 0; |
4292 | OPJ_UINT32 l_current_part; |
4293 | OPJ_UINT32 l_tile_x, l_tile_y; |
4294 | |
4295 | /* preconditions */ |
4296 | |
4297 | assert(p_j2k != 00); |
4298 | assert(p_manager != 00); |
4299 | |
4300 | if (! opj_j2k_get_sot_values(p_header_data, p_header_size, |
4301 | &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, |
4302 | p_manager)) { |
4303 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n" ); |
4304 | return OPJ_FALSE; |
4305 | } |
4306 | #ifdef DEBUG_VERBOSE |
4307 | fprintf(stderr, "SOT %d %d %d %d\n" , |
4308 | p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts); |
4309 | #endif |
4310 | |
4311 | l_cp = &(p_j2k->m_cp); |
4312 | |
4313 | /* testcase 2.pdf.SIGFPE.706.1112 */ |
4314 | if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { |
4315 | opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n" , |
4316 | p_j2k->m_current_tile_number); |
4317 | return OPJ_FALSE; |
4318 | } |
4319 | |
4320 | l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; |
4321 | l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; |
4322 | l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; |
4323 | |
4324 | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 || |
4325 | p_j2k->m_current_tile_number == (OPJ_UINT32) |
4326 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) { |
4327 | /* Do only this check if we decode all tile part headers, or if */ |
4328 | /* we decode one precise tile. Otherwise the m_current_tile_part_number */ |
4329 | /* might not be valid */ |
4330 | /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */ |
4331 | /* of https://github.com/uclouvain/openjpeg/issues/939 */ |
4332 | /* We must avoid reading twice the same tile part number for a given tile */ |
4333 | /* so as to avoid various issues, like opj_j2k_merge_ppt being called */ |
4334 | /* several times. */ |
4335 | /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */ |
4336 | /* should appear in increasing order. */ |
4337 | if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) { |
4338 | opj_event_msg(p_manager, EVT_ERROR, |
4339 | "Invalid tile part index for tile number %d. " |
4340 | "Got %d, expected %d\n" , |
4341 | p_j2k->m_current_tile_number, |
4342 | l_current_part, |
4343 | l_tcp->m_current_tile_part_number + 1); |
4344 | return OPJ_FALSE; |
4345 | } |
4346 | } |
4347 | |
4348 | l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part; |
4349 | |
4350 | #ifdef USE_JPWL |
4351 | if (l_cp->correct) { |
4352 | |
4353 | OPJ_UINT32 tileno = p_j2k->m_current_tile_number; |
4354 | static OPJ_UINT32 backup_tileno = 0; |
4355 | |
4356 | /* tileno is negative or larger than the number of tiles!!! */ |
4357 | if (tileno > (l_cp->tw * l_cp->th)) { |
4358 | opj_event_msg(p_manager, EVT_ERROR, |
4359 | "JPWL: bad tile number (%d out of a maximum of %d)\n" , |
4360 | tileno, (l_cp->tw * l_cp->th)); |
4361 | if (!JPWL_ASSUME) { |
4362 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
4363 | return OPJ_FALSE; |
4364 | } |
4365 | /* we try to correct */ |
4366 | tileno = backup_tileno; |
4367 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4368 | "- setting tile number to %d\n" , |
4369 | tileno); |
4370 | } |
4371 | |
4372 | /* keep your private count of tiles */ |
4373 | backup_tileno++; |
4374 | }; |
4375 | #endif /* USE_JPWL */ |
4376 | |
4377 | /* look for the tile in the list of already processed tile (in parts). */ |
4378 | /* Optimization possible here with a more complex data structure and with the removing of tiles */ |
4379 | /* since the time taken by this function can only grow at the time */ |
4380 | |
4381 | /* PSot should be equal to zero or >=14 or <= 2^32-1 */ |
4382 | if ((l_tot_len != 0) && (l_tot_len < 14)) { |
4383 | if (l_tot_len == |
4384 | 12) { /* MSD: Special case for the PHR data which are read by kakadu*/ |
4385 | opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n" , |
4386 | l_tot_len); |
4387 | } else { |
4388 | opj_event_msg(p_manager, EVT_ERROR, |
4389 | "Psot value is not correct regards to the JPEG2000 norm: %d.\n" , l_tot_len); |
4390 | return OPJ_FALSE; |
4391 | } |
4392 | } |
4393 | |
4394 | #ifdef USE_JPWL |
4395 | if (l_cp->correct) { |
4396 | |
4397 | /* totlen is negative or larger than the bytes left!!! */ |
4398 | if (/*(l_tot_len < 0) ||*/ (l_tot_len > |
4399 | p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ |
4400 | opj_event_msg(p_manager, EVT_ERROR, |
4401 | "JPWL: bad tile byte size (%d bytes against %d bytes left)\n" , |
4402 | l_tot_len, |
4403 | p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ |
4404 | if (!JPWL_ASSUME) { |
4405 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
4406 | return OPJ_FALSE; |
4407 | } |
4408 | /* we try to correct */ |
4409 | l_tot_len = 0; |
4410 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" |
4411 | "- setting Psot to %d => assuming it is the last tile\n" , |
4412 | l_tot_len); |
4413 | } |
4414 | }; |
4415 | #endif /* USE_JPWL */ |
4416 | |
4417 | /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ |
4418 | if (!l_tot_len) { |
4419 | opj_event_msg(p_manager, EVT_INFO, |
4420 | "Psot value of the current tile-part is equal to zero, " |
4421 | "we assuming it is the last tile-part of the codestream.\n" ); |
4422 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4423 | } |
4424 | |
4425 | if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) { |
4426 | /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */ |
4427 | opj_event_msg(p_manager, EVT_ERROR, |
4428 | "In SOT marker, TPSot (%d) is not valid regards to the previous " |
4429 | "number of tile-part (%d), giving up\n" , l_current_part, |
4430 | l_tcp->m_nb_tile_parts); |
4431 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4432 | return OPJ_FALSE; |
4433 | } |
4434 | |
4435 | if (l_num_parts != |
4436 | 0) { /* Number of tile-part header is provided by this tile-part header */ |
4437 | l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; |
4438 | /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of |
4439 | * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ |
4440 | if (l_tcp->m_nb_tile_parts) { |
4441 | if (l_current_part >= l_tcp->m_nb_tile_parts) { |
4442 | opj_event_msg(p_manager, EVT_ERROR, |
4443 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4444 | "number of tile-part (%d), giving up\n" , l_current_part, |
4445 | l_tcp->m_nb_tile_parts); |
4446 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4447 | return OPJ_FALSE; |
4448 | } |
4449 | } |
4450 | if (l_current_part >= l_num_parts) { |
4451 | /* testcase 451.pdf.SIGSEGV.ce9.3723 */ |
4452 | opj_event_msg(p_manager, EVT_ERROR, |
4453 | "In SOT marker, TPSot (%d) is not valid regards to the current " |
4454 | "number of tile-part (header) (%d), giving up\n" , l_current_part, l_num_parts); |
4455 | p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; |
4456 | return OPJ_FALSE; |
4457 | } |
4458 | l_tcp->m_nb_tile_parts = l_num_parts; |
4459 | } |
4460 | |
4461 | /* If know the number of tile part header we will check if we didn't read the last*/ |
4462 | if (l_tcp->m_nb_tile_parts) { |
4463 | if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { |
4464 | p_j2k->m_specific_param.m_decoder.m_can_decode = |
4465 | 1; /* Process the last tile-part header*/ |
4466 | } |
4467 | } |
4468 | |
4469 | if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4470 | /* Keep the size of data to skip after this marker */ |
4471 | p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - |
4472 | 12; /* SOT_marker_size = 12 */ |
4473 | } else { |
4474 | /* FIXME: need to be computed from the number of bytes remaining in the codestream */ |
4475 | p_j2k->m_specific_param.m_decoder.m_sot_length = 0; |
4476 | } |
4477 | |
4478 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; |
4479 | |
4480 | /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ |
4481 | if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { |
4482 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4483 | (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) |
4484 | || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) |
4485 | || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) |
4486 | || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); |
4487 | } else { |
4488 | assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0); |
4489 | p_j2k->m_specific_param.m_decoder.m_skip_data = |
4490 | (p_j2k->m_current_tile_number != (OPJ_UINT32) |
4491 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); |
4492 | } |
4493 | |
4494 | /* Index */ |
4495 | if (p_j2k->cstr_index) { |
4496 | assert(p_j2k->cstr_index->tile_index != 00); |
4497 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = |
4498 | p_j2k->m_current_tile_number; |
4499 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = |
4500 | l_current_part; |
4501 | |
4502 | if (l_num_parts != 0) { |
4503 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = |
4504 | l_num_parts; |
4505 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4506 | l_num_parts; |
4507 | |
4508 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4509 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4510 | (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); |
4511 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4512 | opj_event_msg(p_manager, EVT_ERROR, |
4513 | "Not enough memory to read SOT marker. Tile index allocation failed\n" ); |
4514 | return OPJ_FALSE; |
4515 | } |
4516 | } else { |
4517 | opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( |
4518 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4519 | l_num_parts * sizeof(opj_tp_index_t)); |
4520 | if (! new_tp_index) { |
4521 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4522 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4523 | opj_event_msg(p_manager, EVT_ERROR, |
4524 | "Not enough memory to read SOT marker. Tile index allocation failed\n" ); |
4525 | return OPJ_FALSE; |
4526 | } |
4527 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4528 | new_tp_index; |
4529 | } |
4530 | } else { |
4531 | /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { |
4532 | |
4533 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4534 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; |
4535 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4536 | (opj_tp_index_t*)opj_calloc( |
4537 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, |
4538 | sizeof(opj_tp_index_t)); |
4539 | if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { |
4540 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4541 | opj_event_msg(p_manager, EVT_ERROR, |
4542 | "Not enough memory to read SOT marker. Tile index allocation failed\n" ); |
4543 | return OPJ_FALSE; |
4544 | } |
4545 | } |
4546 | |
4547 | if (l_current_part >= |
4548 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) { |
4549 | opj_tp_index_t *new_tp_index; |
4550 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = |
4551 | l_current_part + 1; |
4552 | new_tp_index = (opj_tp_index_t *) opj_realloc( |
4553 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, |
4554 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * |
4555 | sizeof(opj_tp_index_t)); |
4556 | if (! new_tp_index) { |
4557 | opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); |
4558 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; |
4559 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; |
4560 | opj_event_msg(p_manager, EVT_ERROR, |
4561 | "Not enough memory to read SOT marker. Tile index allocation failed\n" ); |
4562 | return OPJ_FALSE; |
4563 | } |
4564 | p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = |
4565 | new_tp_index; |
4566 | } |
4567 | } |
4568 | |
4569 | } |
4570 | |
4571 | } |
4572 | |
4573 | /* 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 */ |
4574 | /* if (p_j2k->cstr_info) { |
4575 | if (l_tcp->first) { |
4576 | if (tileno == 0) { |
4577 | p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; |
4578 | } |
4579 | |
4580 | p_j2k->cstr_info->tile[tileno].tileno = tileno; |
4581 | p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; |
4582 | p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; |
4583 | p_j2k->cstr_info->tile[tileno].num_tps = numparts; |
4584 | |
4585 | if (numparts) { |
4586 | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); |
4587 | } |
4588 | else { |
4589 | p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) |
4590 | } |
4591 | } |
4592 | else { |
4593 | p_j2k->cstr_info->tile[tileno].end_pos += totlen; |
4594 | } |
4595 | |
4596 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; |
4597 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = |
4598 | p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; |
4599 | }*/ |
4600 | return OPJ_TRUE; |
4601 | } |
4602 | |
4603 | static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, |
4604 | opj_tcd_t * p_tile_coder, |
4605 | OPJ_BYTE * p_data, |
4606 | OPJ_UINT32 * p_data_written, |
4607 | OPJ_UINT32 p_total_data_size, |
4608 | const opj_stream_private_t *p_stream, |
4609 | opj_event_mgr_t * p_manager |
4610 | ) |
4611 | { |
4612 | opj_codestream_info_t *l_cstr_info = 00; |
4613 | OPJ_UINT32 l_remaining_data; |
4614 | |
4615 | /* preconditions */ |
4616 | assert(p_j2k != 00); |
4617 | assert(p_manager != 00); |
4618 | assert(p_stream != 00); |
4619 | |
4620 | OPJ_UNUSED(p_stream); |
4621 | |
4622 | if (p_total_data_size < 4) { |
4623 | opj_event_msg(p_manager, EVT_ERROR, |
4624 | "Not enough bytes in output buffer to write SOD marker\n" ); |
4625 | return OPJ_FALSE; |
4626 | } |
4627 | |
4628 | opj_write_bytes(p_data, J2K_MS_SOD, |
4629 | 2); /* SOD */ |
4630 | p_data += 2; |
4631 | |
4632 | /* make room for the EOF marker */ |
4633 | l_remaining_data = p_total_data_size - 4; |
4634 | |
4635 | /* update tile coder */ |
4636 | p_tile_coder->tp_num = |
4637 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; |
4638 | p_tile_coder->cur_tp_num = |
4639 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
4640 | |
4641 | /* INDEX >> */ |
4642 | /* TODO mergeV2: check this part which use cstr_info */ |
4643 | /*l_cstr_info = p_j2k->cstr_info; |
4644 | if (l_cstr_info) { |
4645 | if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { |
4646 | //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; |
4647 | l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; |
4648 | } |
4649 | else {*/ |
4650 | /* |
4651 | TODO |
4652 | if |
4653 | (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) |
4654 | { |
4655 | cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); |
4656 | }*/ |
4657 | /*}*/ |
4658 | /* UniPG>> */ |
4659 | #ifdef USE_JPWL |
4660 | /* update markers struct */ |
4661 | /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); |
4662 | */ |
4663 | assert(0 && "TODO" ); |
4664 | #endif /* USE_JPWL */ |
4665 | /* <<UniPG */ |
4666 | /*}*/ |
4667 | /* << INDEX */ |
4668 | |
4669 | if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) { |
4670 | p_tile_coder->tcd_image->tiles->packno = 0; |
4671 | #ifdef deadcode |
4672 | if (l_cstr_info) { |
4673 | l_cstr_info->packno = 0; |
4674 | } |
4675 | #endif |
4676 | } |
4677 | |
4678 | *p_data_written = 0; |
4679 | |
4680 | if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, |
4681 | p_data_written, l_remaining_data, l_cstr_info, |
4682 | p_manager)) { |
4683 | opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n" ); |
4684 | return OPJ_FALSE; |
4685 | } |
4686 | |
4687 | *p_data_written += 2; |
4688 | |
4689 | return OPJ_TRUE; |
4690 | } |
4691 | |
4692 | static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
4693 | opj_stream_private_t *p_stream, |
4694 | opj_event_mgr_t * p_manager |
4695 | ) |
4696 | { |
4697 | OPJ_SIZE_T l_current_read_size; |
4698 | opj_codestream_index_t * l_cstr_index = 00; |
4699 | OPJ_BYTE ** l_current_data = 00; |
4700 | opj_tcp_t * l_tcp = 00; |
4701 | OPJ_UINT32 * l_tile_len = 00; |
4702 | OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; |
4703 | |
4704 | /* preconditions */ |
4705 | assert(p_j2k != 00); |
4706 | assert(p_manager != 00); |
4707 | assert(p_stream != 00); |
4708 | |
4709 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
4710 | |
4711 | if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
4712 | /* opj_stream_get_number_byte_left returns OPJ_OFF_T |
4713 | // but we are in the last tile part, |
4714 | // so its result will fit on OPJ_UINT32 unless we find |
4715 | // a file with a single tile part of more than 4 GB...*/ |
4716 | p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( |
4717 | opj_stream_get_number_byte_left(p_stream) - 2); |
4718 | } else { |
4719 | /* Check to avoid pass the limit of OPJ_UINT32 */ |
4720 | if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { |
4721 | p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; |
4722 | } else { |
4723 | /* MSD: case commented to support empty SOT marker (PHR data) */ |
4724 | } |
4725 | } |
4726 | |
4727 | l_current_data = &(l_tcp->m_data); |
4728 | l_tile_len = &l_tcp->m_data_size; |
4729 | |
4730 | /* Patch to support new PHR data */ |
4731 | if (p_j2k->m_specific_param.m_decoder.m_sot_length) { |
4732 | /* If we are here, we'll try to read the data after allocation */ |
4733 | /* Check enough bytes left in stream before allocation */ |
4734 | if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > |
4735 | opj_stream_get_number_byte_left(p_stream)) { |
4736 | opj_event_msg(p_manager, EVT_ERROR, |
4737 | "Tile part length size inconsistent with stream length\n" ); |
4738 | return OPJ_FALSE; |
4739 | } |
4740 | if (p_j2k->m_specific_param.m_decoder.m_sot_length > |
4741 | UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) { |
4742 | opj_event_msg(p_manager, EVT_ERROR, |
4743 | "p_j2k->m_specific_param.m_decoder.m_sot_length > " |
4744 | "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA" ); |
4745 | return OPJ_FALSE; |
4746 | } |
4747 | /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */ |
4748 | /* do so that opj_mqc_init_dec_common() can safely add a synthetic */ |
4749 | /* 0xFFFF marker. */ |
4750 | if (! *l_current_data) { |
4751 | /* LH: oddly enough, in this path, l_tile_len!=0. |
4752 | * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). |
4753 | */ |
4754 | *l_current_data = (OPJ_BYTE*) opj_malloc( |
4755 | p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA); |
4756 | } else { |
4757 | OPJ_BYTE *l_new_current_data; |
4758 | if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - |
4759 | p_j2k->m_specific_param.m_decoder.m_sot_length) { |
4760 | opj_event_msg(p_manager, EVT_ERROR, |
4761 | "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - " |
4762 | "p_j2k->m_specific_param.m_decoder.m_sot_length" ); |
4763 | return OPJ_FALSE; |
4764 | } |
4765 | |
4766 | l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, |
4767 | *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length + |
4768 | OPJ_COMMON_CBLK_DATA_EXTRA); |
4769 | if (! l_new_current_data) { |
4770 | opj_free(*l_current_data); |
4771 | /*nothing more is done as l_current_data will be set to null, and just |
4772 | afterward we enter in the error path |
4773 | and the actual tile_len is updated (committed) at the end of the |
4774 | function. */ |
4775 | } |
4776 | *l_current_data = l_new_current_data; |
4777 | } |
4778 | |
4779 | if (*l_current_data == 00) { |
4780 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n" ); |
4781 | return OPJ_FALSE; |
4782 | } |
4783 | } else { |
4784 | l_sot_length_pb_detected = OPJ_TRUE; |
4785 | } |
4786 | |
4787 | /* Index */ |
4788 | l_cstr_index = p_j2k->cstr_index; |
4789 | if (l_cstr_index) { |
4790 | OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; |
4791 | |
4792 | OPJ_UINT32 l_current_tile_part = |
4793 | l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; |
4794 | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header |
4795 | = |
4796 | l_current_pos; |
4797 | l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos |
4798 | = |
4799 | l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; |
4800 | |
4801 | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
4802 | l_cstr_index, |
4803 | J2K_MS_SOD, |
4804 | l_current_pos, |
4805 | p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { |
4806 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n" ); |
4807 | return OPJ_FALSE; |
4808 | } |
4809 | |
4810 | /*l_cstr_index->packno = 0;*/ |
4811 | } |
4812 | |
4813 | /* Patch to support new PHR data */ |
4814 | if (!l_sot_length_pb_detected) { |
4815 | l_current_read_size = opj_stream_read_data( |
4816 | p_stream, |
4817 | *l_current_data + *l_tile_len, |
4818 | p_j2k->m_specific_param.m_decoder.m_sot_length, |
4819 | p_manager); |
4820 | } else { |
4821 | l_current_read_size = 0; |
4822 | } |
4823 | |
4824 | if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
4825 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
4826 | } else { |
4827 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
4828 | } |
4829 | |
4830 | *l_tile_len += (OPJ_UINT32)l_current_read_size; |
4831 | |
4832 | return OPJ_TRUE; |
4833 | } |
4834 | |
4835 | static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, |
4836 | OPJ_UINT32 p_tile_no, |
4837 | OPJ_UINT32 p_comp_no, |
4838 | OPJ_UINT32 nb_comps, |
4839 | opj_stream_private_t *p_stream, |
4840 | opj_event_mgr_t * p_manager |
4841 | ) |
4842 | { |
4843 | OPJ_BYTE * l_current_data = 00; |
4844 | OPJ_UINT32 l_rgn_size; |
4845 | opj_cp_t *l_cp = 00; |
4846 | opj_tcp_t *l_tcp = 00; |
4847 | opj_tccp_t *l_tccp = 00; |
4848 | OPJ_UINT32 l_comp_room; |
4849 | |
4850 | /* preconditions */ |
4851 | assert(p_j2k != 00); |
4852 | assert(p_manager != 00); |
4853 | assert(p_stream != 00); |
4854 | |
4855 | l_cp = &(p_j2k->m_cp); |
4856 | l_tcp = &l_cp->tcps[p_tile_no]; |
4857 | l_tccp = &l_tcp->tccps[p_comp_no]; |
4858 | |
4859 | if (nb_comps <= 256) { |
4860 | l_comp_room = 1; |
4861 | } else { |
4862 | l_comp_room = 2; |
4863 | } |
4864 | |
4865 | l_rgn_size = 6 + l_comp_room; |
4866 | |
4867 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
4868 | |
4869 | opj_write_bytes(l_current_data, J2K_MS_RGN, |
4870 | 2); /* RGN */ |
4871 | l_current_data += 2; |
4872 | |
4873 | opj_write_bytes(l_current_data, l_rgn_size - 2, |
4874 | 2); /* Lrgn */ |
4875 | l_current_data += 2; |
4876 | |
4877 | opj_write_bytes(l_current_data, p_comp_no, |
4878 | l_comp_room); /* Crgn */ |
4879 | l_current_data += l_comp_room; |
4880 | |
4881 | opj_write_bytes(l_current_data, 0, |
4882 | 1); /* Srgn */ |
4883 | ++l_current_data; |
4884 | |
4885 | opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift, |
4886 | 1); /* SPrgn */ |
4887 | ++l_current_data; |
4888 | |
4889 | if (opj_stream_write_data(p_stream, |
4890 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size, |
4891 | p_manager) != l_rgn_size) { |
4892 | return OPJ_FALSE; |
4893 | } |
4894 | |
4895 | return OPJ_TRUE; |
4896 | } |
4897 | |
4898 | static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, |
4899 | opj_stream_private_t *p_stream, |
4900 | opj_event_mgr_t * p_manager |
4901 | ) |
4902 | { |
4903 | /* preconditions */ |
4904 | assert(p_j2k != 00); |
4905 | assert(p_manager != 00); |
4906 | assert(p_stream != 00); |
4907 | |
4908 | opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data, |
4909 | J2K_MS_EOC, 2); /* EOC */ |
4910 | |
4911 | /* UniPG>> */ |
4912 | #ifdef USE_JPWL |
4913 | /* update markers struct */ |
4914 | /* |
4915 | OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); |
4916 | */ |
4917 | #endif /* USE_JPWL */ |
4918 | |
4919 | if (opj_stream_write_data(p_stream, |
4920 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) { |
4921 | return OPJ_FALSE; |
4922 | } |
4923 | |
4924 | if (! opj_stream_flush(p_stream, p_manager)) { |
4925 | return OPJ_FALSE; |
4926 | } |
4927 | |
4928 | return OPJ_TRUE; |
4929 | } |
4930 | |
4931 | /** |
4932 | * Reads a RGN marker (Region Of Interest) |
4933 | * |
4934 | * @param p_header_data the data contained in the POC box. |
4935 | * @param p_j2k the jpeg2000 codec. |
4936 | * @param p_header_size the size of the data contained in the POC marker. |
4937 | * @param p_manager the user event manager. |
4938 | */ |
4939 | static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, |
4940 | OPJ_BYTE * , |
4941 | OPJ_UINT32 , |
4942 | opj_event_mgr_t * p_manager |
4943 | ) |
4944 | { |
4945 | OPJ_UINT32 l_nb_comp; |
4946 | opj_image_t * l_image = 00; |
4947 | |
4948 | opj_cp_t *l_cp = 00; |
4949 | opj_tcp_t *l_tcp = 00; |
4950 | OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; |
4951 | |
4952 | /* preconditions*/ |
4953 | assert(p_header_data != 00); |
4954 | assert(p_j2k != 00); |
4955 | assert(p_manager != 00); |
4956 | |
4957 | l_image = p_j2k->m_private_image; |
4958 | l_nb_comp = l_image->numcomps; |
4959 | |
4960 | if (l_nb_comp <= 256) { |
4961 | l_comp_room = 1; |
4962 | } else { |
4963 | l_comp_room = 2; |
4964 | } |
4965 | |
4966 | if (p_header_size != 2 + l_comp_room) { |
4967 | opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n" ); |
4968 | return OPJ_FALSE; |
4969 | } |
4970 | |
4971 | l_cp = &(p_j2k->m_cp); |
4972 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
4973 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
4974 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
4975 | |
4976 | opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */ |
4977 | p_header_data += l_comp_room; |
4978 | opj_read_bytes(p_header_data, &l_roi_sty, |
4979 | 1); /* Srgn */ |
4980 | ++p_header_data; |
4981 | |
4982 | #ifdef USE_JPWL |
4983 | if (l_cp->correct) { |
4984 | /* totlen is negative or larger than the bytes left!!! */ |
4985 | if (l_comp_room >= l_nb_comp) { |
4986 | opj_event_msg(p_manager, EVT_ERROR, |
4987 | "JPWL: bad component number in RGN (%d when there are only %d)\n" , |
4988 | l_comp_room, l_nb_comp); |
4989 | if (!JPWL_ASSUME) { |
4990 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
4991 | return OPJ_FALSE; |
4992 | } |
4993 | } |
4994 | }; |
4995 | #endif /* USE_JPWL */ |
4996 | |
4997 | /* testcase 3635.pdf.asan.77.2930 */ |
4998 | if (l_comp_no >= l_nb_comp) { |
4999 | opj_event_msg(p_manager, EVT_ERROR, |
5000 | "bad component number in RGN (%d when there are only %d)\n" , |
5001 | l_comp_no, l_nb_comp); |
5002 | return OPJ_FALSE; |
5003 | } |
5004 | |
5005 | opj_read_bytes(p_header_data, |
5006 | (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */ |
5007 | ++p_header_data; |
5008 | |
5009 | return OPJ_TRUE; |
5010 | |
5011 | } |
5012 | |
5013 | static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp) |
5014 | { |
5015 | return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14); |
5016 | } |
5017 | |
5018 | static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp) |
5019 | { |
5020 | (void)p_tcp; |
5021 | return 0; |
5022 | } |
5023 | |
5024 | static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, |
5025 | opj_stream_private_t *p_stream, |
5026 | opj_event_mgr_t * p_manager) |
5027 | { |
5028 | opj_cp_t * l_cp = 00; |
5029 | opj_image_t * l_image = 00; |
5030 | opj_tcp_t * l_tcp = 00; |
5031 | opj_image_comp_t * l_img_comp = 00; |
5032 | |
5033 | OPJ_UINT32 i, j, k; |
5034 | OPJ_INT32 l_x0, l_y0, l_x1, l_y1; |
5035 | OPJ_FLOAT32 * l_rates = 0; |
5036 | OPJ_FLOAT32 l_sot_remove; |
5037 | OPJ_UINT32 l_bits_empty, l_size_pixel; |
5038 | OPJ_UINT32 l_tile_size = 0; |
5039 | OPJ_UINT32 l_last_res; |
5040 | OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00; |
5041 | |
5042 | /* preconditions */ |
5043 | assert(p_j2k != 00); |
5044 | assert(p_manager != 00); |
5045 | assert(p_stream != 00); |
5046 | |
5047 | OPJ_UNUSED(p_manager); |
5048 | |
5049 | l_cp = &(p_j2k->m_cp); |
5050 | l_image = p_j2k->m_private_image; |
5051 | l_tcp = l_cp->tcps; |
5052 | |
5053 | l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; |
5054 | l_size_pixel = l_image->numcomps * l_image->comps->prec; |
5055 | l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)( |
5056 | l_cp->th * l_cp->tw); |
5057 | |
5058 | if (l_cp->m_specific_param.m_enc.m_tp_on) { |
5059 | l_tp_stride_func = opj_j2k_get_tp_stride; |
5060 | } else { |
5061 | l_tp_stride_func = opj_j2k_get_default_stride; |
5062 | } |
5063 | |
5064 | for (i = 0; i < l_cp->th; ++i) { |
5065 | for (j = 0; j < l_cp->tw; ++j) { |
5066 | OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / |
5067 | (OPJ_FLOAT32)l_tcp->numlayers; |
5068 | |
5069 | /* 4 borders of the tile rescale on the image if necessary */ |
5070 | l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), |
5071 | (OPJ_INT32)l_image->x0); |
5072 | l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), |
5073 | (OPJ_INT32)l_image->y0); |
5074 | l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), |
5075 | (OPJ_INT32)l_image->x1); |
5076 | l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), |
5077 | (OPJ_INT32)l_image->y1); |
5078 | |
5079 | l_rates = l_tcp->rates; |
5080 | |
5081 | /* Modification of the RATE >> */ |
5082 | if (*l_rates > 0.0f) { |
5083 | *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * |
5084 | (OPJ_UINT32)(l_y1 - l_y0))) |
5085 | / |
5086 | ((*l_rates) * (OPJ_FLOAT32)l_bits_empty) |
5087 | ) |
5088 | - |
5089 | l_offset; |
5090 | } |
5091 | |
5092 | ++l_rates; |
5093 | |
5094 | for (k = 1; k < l_tcp->numlayers; ++k) { |
5095 | if (*l_rates > 0.0f) { |
5096 | *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * |
5097 | (OPJ_UINT32)(l_y1 - l_y0))) |
5098 | / |
5099 | ((*l_rates) * (OPJ_FLOAT32)l_bits_empty) |
5100 | ) |
5101 | - |
5102 | l_offset; |
5103 | } |
5104 | |
5105 | ++l_rates; |
5106 | } |
5107 | |
5108 | ++l_tcp; |
5109 | |
5110 | } |
5111 | } |
5112 | |
5113 | l_tcp = l_cp->tcps; |
5114 | |
5115 | for (i = 0; i < l_cp->th; ++i) { |
5116 | for (j = 0; j < l_cp->tw; ++j) { |
5117 | l_rates = l_tcp->rates; |
5118 | |
5119 | if (*l_rates > 0.0f) { |
5120 | *l_rates -= l_sot_remove; |
5121 | |
5122 | if (*l_rates < 30.0f) { |
5123 | *l_rates = 30.0f; |
5124 | } |
5125 | } |
5126 | |
5127 | ++l_rates; |
5128 | |
5129 | l_last_res = l_tcp->numlayers - 1; |
5130 | |
5131 | for (k = 1; k < l_last_res; ++k) { |
5132 | |
5133 | if (*l_rates > 0.0f) { |
5134 | *l_rates -= l_sot_remove; |
5135 | |
5136 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5137 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5138 | } |
5139 | } |
5140 | |
5141 | ++l_rates; |
5142 | } |
5143 | |
5144 | if (*l_rates > 0.0f) { |
5145 | *l_rates -= (l_sot_remove + 2.f); |
5146 | |
5147 | if (*l_rates < * (l_rates - 1) + 10.0f) { |
5148 | *l_rates = (*(l_rates - 1)) + 20.0f; |
5149 | } |
5150 | } |
5151 | |
5152 | ++l_tcp; |
5153 | } |
5154 | } |
5155 | |
5156 | l_img_comp = l_image->comps; |
5157 | l_tile_size = 0; |
5158 | |
5159 | for (i = 0; i < l_image->numcomps; ++i) { |
5160 | l_tile_size += (opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx) |
5161 | * |
5162 | opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy) |
5163 | * |
5164 | l_img_comp->prec |
5165 | ); |
5166 | |
5167 | ++l_img_comp; |
5168 | } |
5169 | |
5170 | /* TODO: where does this magic value come from ? */ |
5171 | /* This used to be 1.3 / 8, but with random data and very small code */ |
5172 | /* block sizes, this is not enough. For example with */ |
5173 | /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */ |
5174 | /* TODO revise this to take into account the overhead linked to the */ |
5175 | /* number of packets and number of code blocks in packets */ |
5176 | l_tile_size = (OPJ_UINT32)(l_tile_size * 1.4 / 8); |
5177 | |
5178 | /* Arbitrary amount to make the following work: */ |
5179 | /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */ |
5180 | l_tile_size += 500; |
5181 | |
5182 | l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); |
5183 | |
5184 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size; |
5185 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = |
5186 | (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); |
5187 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { |
5188 | return OPJ_FALSE; |
5189 | } |
5190 | |
5191 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
5192 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = |
5193 | (OPJ_BYTE *) opj_malloc(5 * |
5194 | p_j2k->m_specific_param.m_encoder.m_total_tile_parts); |
5195 | if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
5196 | return OPJ_FALSE; |
5197 | } |
5198 | |
5199 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = |
5200 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; |
5201 | } |
5202 | |
5203 | return OPJ_TRUE; |
5204 | } |
5205 | |
5206 | #if 0 |
5207 | static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, |
5208 | opj_stream_private_t *p_stream, |
5209 | opj_event_mgr_t * p_manager) |
5210 | { |
5211 | OPJ_UINT32 i; |
5212 | opj_tcd_t * l_tcd = 00; |
5213 | OPJ_UINT32 l_nb_tiles; |
5214 | opj_tcp_t * l_tcp = 00; |
5215 | OPJ_BOOL l_success; |
5216 | |
5217 | /* preconditions */ |
5218 | assert(p_j2k != 00); |
5219 | assert(p_manager != 00); |
5220 | assert(p_stream != 00); |
5221 | |
5222 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
5223 | l_tcp = p_j2k->m_cp.tcps; |
5224 | |
5225 | l_tcd = opj_tcd_create(OPJ_TRUE); |
5226 | if (l_tcd == 00) { |
5227 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n" ); |
5228 | return OPJ_FALSE; |
5229 | } |
5230 | |
5231 | for (i = 0; i < l_nb_tiles; ++i) { |
5232 | if (l_tcp->m_data) { |
5233 | if (! opj_tcd_init_decode_tile(l_tcd, i)) { |
5234 | opj_tcd_destroy(l_tcd); |
5235 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n" ); |
5236 | return OPJ_FALSE; |
5237 | } |
5238 | |
5239 | l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, |
5240 | p_j2k->cstr_index); |
5241 | /* cleanup */ |
5242 | |
5243 | if (! l_success) { |
5244 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
5245 | break; |
5246 | } |
5247 | } |
5248 | |
5249 | opj_j2k_tcp_destroy(l_tcp); |
5250 | ++l_tcp; |
5251 | } |
5252 | |
5253 | opj_tcd_destroy(l_tcd); |
5254 | return OPJ_TRUE; |
5255 | } |
5256 | #endif |
5257 | |
5258 | static OPJ_BOOL (opj_j2k_t *p_j2k, |
5259 | struct opj_stream_private *p_stream, |
5260 | struct opj_event_mgr * p_manager) |
5261 | { |
5262 | /* preconditions */ |
5263 | assert(p_j2k != 00); |
5264 | assert(p_manager != 00); |
5265 | assert(p_stream != 00); |
5266 | |
5267 | OPJ_UNUSED(p_manager); |
5268 | |
5269 | p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); |
5270 | |
5271 | return OPJ_TRUE; |
5272 | } |
5273 | |
5274 | static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, |
5275 | struct opj_stream_private *p_stream, |
5276 | struct opj_event_mgr * p_manager) |
5277 | { |
5278 | OPJ_UINT32 i; |
5279 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
5280 | opj_mct_data_t * l_mct_record; |
5281 | opj_tcp_t * l_tcp; |
5282 | |
5283 | /* preconditions */ |
5284 | assert(p_j2k != 00); |
5285 | assert(p_stream != 00); |
5286 | assert(p_manager != 00); |
5287 | |
5288 | if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) { |
5289 | return OPJ_FALSE; |
5290 | } |
5291 | |
5292 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
5293 | l_mct_record = l_tcp->m_mct_records; |
5294 | |
5295 | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5296 | |
5297 | if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) { |
5298 | return OPJ_FALSE; |
5299 | } |
5300 | |
5301 | ++l_mct_record; |
5302 | } |
5303 | |
5304 | l_mcc_record = l_tcp->m_mcc_records; |
5305 | |
5306 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5307 | |
5308 | if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) { |
5309 | return OPJ_FALSE; |
5310 | } |
5311 | |
5312 | ++l_mcc_record; |
5313 | } |
5314 | |
5315 | if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) { |
5316 | return OPJ_FALSE; |
5317 | } |
5318 | |
5319 | return OPJ_TRUE; |
5320 | } |
5321 | |
5322 | static OPJ_BOOL opj_j2k_write_all_coc( |
5323 | opj_j2k_t *p_j2k, |
5324 | struct opj_stream_private *p_stream, |
5325 | struct opj_event_mgr * p_manager) |
5326 | { |
5327 | OPJ_UINT32 compno; |
5328 | |
5329 | /* preconditions */ |
5330 | assert(p_j2k != 00); |
5331 | assert(p_manager != 00); |
5332 | assert(p_stream != 00); |
5333 | |
5334 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5335 | /* cod is first component of first tile */ |
5336 | if (! opj_j2k_compare_coc(p_j2k, 0, compno)) { |
5337 | if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) { |
5338 | return OPJ_FALSE; |
5339 | } |
5340 | } |
5341 | } |
5342 | |
5343 | return OPJ_TRUE; |
5344 | } |
5345 | |
5346 | static OPJ_BOOL opj_j2k_write_all_qcc( |
5347 | opj_j2k_t *p_j2k, |
5348 | struct opj_stream_private *p_stream, |
5349 | struct opj_event_mgr * p_manager) |
5350 | { |
5351 | OPJ_UINT32 compno; |
5352 | |
5353 | /* preconditions */ |
5354 | assert(p_j2k != 00); |
5355 | assert(p_manager != 00); |
5356 | assert(p_stream != 00); |
5357 | |
5358 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5359 | /* qcd is first component of first tile */ |
5360 | if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) { |
5361 | if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) { |
5362 | return OPJ_FALSE; |
5363 | } |
5364 | } |
5365 | } |
5366 | return OPJ_TRUE; |
5367 | } |
5368 | |
5369 | static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, |
5370 | struct opj_stream_private *p_stream, |
5371 | struct opj_event_mgr * p_manager) |
5372 | { |
5373 | OPJ_UINT32 compno; |
5374 | const opj_tccp_t *l_tccp = 00; |
5375 | |
5376 | /* preconditions */ |
5377 | assert(p_j2k != 00); |
5378 | assert(p_manager != 00); |
5379 | assert(p_stream != 00); |
5380 | |
5381 | l_tccp = p_j2k->m_cp.tcps->tccps; |
5382 | |
5383 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
5384 | if (l_tccp->roishift) { |
5385 | |
5386 | if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps, |
5387 | p_stream, p_manager)) { |
5388 | return OPJ_FALSE; |
5389 | } |
5390 | } |
5391 | |
5392 | ++l_tccp; |
5393 | } |
5394 | |
5395 | return OPJ_TRUE; |
5396 | } |
5397 | |
5398 | static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, |
5399 | struct opj_stream_private *p_stream, |
5400 | struct opj_event_mgr * p_manager) |
5401 | { |
5402 | opj_codestream_index_t * l_cstr_index = 00; |
5403 | |
5404 | /* preconditions */ |
5405 | assert(p_j2k != 00); |
5406 | assert(p_manager != 00); |
5407 | assert(p_stream != 00); |
5408 | |
5409 | OPJ_UNUSED(p_manager); |
5410 | |
5411 | l_cstr_index = p_j2k->cstr_index; |
5412 | if (l_cstr_index) { |
5413 | l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream); |
5414 | /* UniPG>> */ |
5415 | /* The following adjustment is done to adjust the codestream size */ |
5416 | /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ |
5417 | /* the first bunch of bytes is not in the codestream */ |
5418 | l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start; |
5419 | /* <<UniPG */ |
5420 | } |
5421 | |
5422 | #ifdef USE_JPWL |
5423 | /* preparation of JPWL marker segments */ |
5424 | #if 0 |
5425 | if (cp->epc_on) { |
5426 | |
5427 | /* encode according to JPWL */ |
5428 | jpwl_encode(p_j2k, p_stream, image); |
5429 | |
5430 | } |
5431 | #endif |
5432 | assert(0 && "TODO" ); |
5433 | #endif /* USE_JPWL */ |
5434 | |
5435 | return OPJ_TRUE; |
5436 | } |
5437 | |
5438 | static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, |
5439 | opj_stream_private_t *p_stream, |
5440 | OPJ_UINT32 *output_marker, |
5441 | opj_event_mgr_t * p_manager |
5442 | ) |
5443 | { |
5444 | OPJ_UINT32 l_unknown_marker; |
5445 | const opj_dec_memory_marker_handler_t * l_marker_handler; |
5446 | OPJ_UINT32 l_size_unk = 2; |
5447 | |
5448 | /* preconditions*/ |
5449 | assert(p_j2k != 00); |
5450 | assert(p_manager != 00); |
5451 | assert(p_stream != 00); |
5452 | |
5453 | opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n" ); |
5454 | |
5455 | for (;;) { |
5456 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
5457 | if (opj_stream_read_data(p_stream, |
5458 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
5459 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
5460 | return OPJ_FALSE; |
5461 | } |
5462 | |
5463 | /* read 2 bytes as the new marker ID*/ |
5464 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
5465 | &l_unknown_marker, 2); |
5466 | |
5467 | if (!(l_unknown_marker < 0xff00)) { |
5468 | |
5469 | /* Get the marker handler from the marker ID*/ |
5470 | l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); |
5471 | |
5472 | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
5473 | opj_event_msg(p_manager, EVT_ERROR, |
5474 | "Marker is not compliant with its position\n" ); |
5475 | return OPJ_FALSE; |
5476 | } else { |
5477 | if (l_marker_handler->id != J2K_MS_UNK) { |
5478 | /* Add the marker to the codestream index*/ |
5479 | if (l_marker_handler->id != J2K_MS_SOT) { |
5480 | OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, |
5481 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, |
5482 | l_size_unk); |
5483 | if (res == OPJ_FALSE) { |
5484 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n" ); |
5485 | return OPJ_FALSE; |
5486 | } |
5487 | } |
5488 | break; /* next marker is known and well located */ |
5489 | } else { |
5490 | l_size_unk += 2; |
5491 | } |
5492 | } |
5493 | } |
5494 | } |
5495 | |
5496 | *output_marker = l_marker_handler->id ; |
5497 | |
5498 | return OPJ_TRUE; |
5499 | } |
5500 | |
5501 | static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, |
5502 | opj_mct_data_t * p_mct_record, |
5503 | struct opj_stream_private *p_stream, |
5504 | struct opj_event_mgr * p_manager) |
5505 | { |
5506 | OPJ_UINT32 l_mct_size; |
5507 | OPJ_BYTE * l_current_data = 00; |
5508 | OPJ_UINT32 l_tmp; |
5509 | |
5510 | /* preconditions */ |
5511 | assert(p_j2k != 00); |
5512 | assert(p_manager != 00); |
5513 | assert(p_stream != 00); |
5514 | |
5515 | l_mct_size = 10 + p_mct_record->m_data_size; |
5516 | |
5517 | if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
5518 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
5519 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); |
5520 | if (! new_header_tile_data) { |
5521 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
5522 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
5523 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
5524 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n" ); |
5525 | return OPJ_FALSE; |
5526 | } |
5527 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
5528 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; |
5529 | } |
5530 | |
5531 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5532 | |
5533 | opj_write_bytes(l_current_data, J2K_MS_MCT, |
5534 | 2); /* MCT */ |
5535 | l_current_data += 2; |
5536 | |
5537 | opj_write_bytes(l_current_data, l_mct_size - 2, |
5538 | 2); /* Lmct */ |
5539 | l_current_data += 2; |
5540 | |
5541 | opj_write_bytes(l_current_data, 0, |
5542 | 2); /* Zmct */ |
5543 | l_current_data += 2; |
5544 | |
5545 | /* only one marker atm */ |
5546 | l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | |
5547 | (p_mct_record->m_element_type << 10); |
5548 | |
5549 | opj_write_bytes(l_current_data, l_tmp, 2); |
5550 | l_current_data += 2; |
5551 | |
5552 | opj_write_bytes(l_current_data, 0, |
5553 | 2); /* Ymct */ |
5554 | l_current_data += 2; |
5555 | |
5556 | memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size); |
5557 | |
5558 | if (opj_stream_write_data(p_stream, |
5559 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size, |
5560 | p_manager) != l_mct_size) { |
5561 | return OPJ_FALSE; |
5562 | } |
5563 | |
5564 | return OPJ_TRUE; |
5565 | } |
5566 | |
5567 | /** |
5568 | * Reads a MCT marker (Multiple Component Transform) |
5569 | * |
5570 | * @param p_header_data the data contained in the MCT box. |
5571 | * @param p_j2k the jpeg2000 codec. |
5572 | * @param p_header_size the size of the data contained in the MCT marker. |
5573 | * @param p_manager the user event manager. |
5574 | */ |
5575 | static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, |
5576 | OPJ_BYTE * , |
5577 | OPJ_UINT32 , |
5578 | opj_event_mgr_t * p_manager |
5579 | ) |
5580 | { |
5581 | OPJ_UINT32 i; |
5582 | opj_tcp_t *l_tcp = 00; |
5583 | OPJ_UINT32 l_tmp; |
5584 | OPJ_UINT32 l_indix; |
5585 | opj_mct_data_t * l_mct_data; |
5586 | |
5587 | /* preconditions */ |
5588 | assert(p_header_data != 00); |
5589 | assert(p_j2k != 00); |
5590 | |
5591 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
5592 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
5593 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5594 | |
5595 | if (p_header_size < 2) { |
5596 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n" ); |
5597 | return OPJ_FALSE; |
5598 | } |
5599 | |
5600 | /* first marker */ |
5601 | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */ |
5602 | p_header_data += 2; |
5603 | if (l_tmp != 0) { |
5604 | opj_event_msg(p_manager, EVT_WARNING, |
5605 | "Cannot take in charge mct data within multiple MCT records\n" ); |
5606 | return OPJ_TRUE; |
5607 | } |
5608 | |
5609 | if (p_header_size <= 6) { |
5610 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n" ); |
5611 | return OPJ_FALSE; |
5612 | } |
5613 | |
5614 | /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ |
5615 | opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */ |
5616 | p_header_data += 2; |
5617 | |
5618 | l_indix = l_tmp & 0xff; |
5619 | l_mct_data = l_tcp->m_mct_records; |
5620 | |
5621 | for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { |
5622 | if (l_mct_data->m_index == l_indix) { |
5623 | break; |
5624 | } |
5625 | ++l_mct_data; |
5626 | } |
5627 | |
5628 | /* NOT FOUND */ |
5629 | if (i == l_tcp->m_nb_mct_records) { |
5630 | if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { |
5631 | opj_mct_data_t *new_mct_records; |
5632 | l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
5633 | |
5634 | new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, |
5635 | l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
5636 | if (! new_mct_records) { |
5637 | opj_free(l_tcp->m_mct_records); |
5638 | l_tcp->m_mct_records = NULL; |
5639 | l_tcp->m_nb_max_mct_records = 0; |
5640 | l_tcp->m_nb_mct_records = 0; |
5641 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n" ); |
5642 | return OPJ_FALSE; |
5643 | } |
5644 | |
5645 | /* Update m_mcc_records[].m_offset_array and m_decorrelation_array |
5646 | * to point to the new addresses */ |
5647 | if (new_mct_records != l_tcp->m_mct_records) { |
5648 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5649 | opj_simple_mcc_decorrelation_data_t* l_mcc_record = |
5650 | &(l_tcp->m_mcc_records[i]); |
5651 | if (l_mcc_record->m_decorrelation_array) { |
5652 | l_mcc_record->m_decorrelation_array = |
5653 | new_mct_records + |
5654 | (l_mcc_record->m_decorrelation_array - |
5655 | l_tcp->m_mct_records); |
5656 | } |
5657 | if (l_mcc_record->m_offset_array) { |
5658 | l_mcc_record->m_offset_array = |
5659 | new_mct_records + |
5660 | (l_mcc_record->m_offset_array - |
5661 | l_tcp->m_mct_records); |
5662 | } |
5663 | } |
5664 | } |
5665 | |
5666 | l_tcp->m_mct_records = new_mct_records; |
5667 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5668 | memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * |
5669 | sizeof(opj_mct_data_t)); |
5670 | } |
5671 | |
5672 | l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; |
5673 | ++l_tcp->m_nb_mct_records; |
5674 | } |
5675 | |
5676 | if (l_mct_data->m_data) { |
5677 | opj_free(l_mct_data->m_data); |
5678 | l_mct_data->m_data = 00; |
5679 | l_mct_data->m_data_size = 0; |
5680 | } |
5681 | |
5682 | l_mct_data->m_index = l_indix; |
5683 | l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); |
5684 | l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); |
5685 | |
5686 | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */ |
5687 | p_header_data += 2; |
5688 | if (l_tmp != 0) { |
5689 | opj_event_msg(p_manager, EVT_WARNING, |
5690 | "Cannot take in charge multiple MCT markers\n" ); |
5691 | return OPJ_TRUE; |
5692 | } |
5693 | |
5694 | p_header_size -= 6; |
5695 | |
5696 | l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); |
5697 | if (! l_mct_data->m_data) { |
5698 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n" ); |
5699 | return OPJ_FALSE; |
5700 | } |
5701 | memcpy(l_mct_data->m_data, p_header_data, p_header_size); |
5702 | |
5703 | l_mct_data->m_data_size = p_header_size; |
5704 | |
5705 | return OPJ_TRUE; |
5706 | } |
5707 | |
5708 | static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, |
5709 | struct opj_simple_mcc_decorrelation_data * p_mcc_record, |
5710 | struct opj_stream_private *p_stream, |
5711 | struct opj_event_mgr * p_manager) |
5712 | { |
5713 | OPJ_UINT32 i; |
5714 | OPJ_UINT32 l_mcc_size; |
5715 | OPJ_BYTE * l_current_data = 00; |
5716 | OPJ_UINT32 l_nb_bytes_for_comp; |
5717 | OPJ_UINT32 l_mask; |
5718 | OPJ_UINT32 l_tmcc; |
5719 | |
5720 | /* preconditions */ |
5721 | assert(p_j2k != 00); |
5722 | assert(p_manager != 00); |
5723 | assert(p_stream != 00); |
5724 | |
5725 | if (p_mcc_record->m_nb_comps > 255) { |
5726 | l_nb_bytes_for_comp = 2; |
5727 | l_mask = 0x8000; |
5728 | } else { |
5729 | l_nb_bytes_for_comp = 1; |
5730 | l_mask = 0; |
5731 | } |
5732 | |
5733 | l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; |
5734 | if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
5735 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
5736 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); |
5737 | if (! new_header_tile_data) { |
5738 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
5739 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
5740 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
5741 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n" ); |
5742 | return OPJ_FALSE; |
5743 | } |
5744 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
5745 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; |
5746 | } |
5747 | |
5748 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
5749 | |
5750 | opj_write_bytes(l_current_data, J2K_MS_MCC, |
5751 | 2); /* MCC */ |
5752 | l_current_data += 2; |
5753 | |
5754 | opj_write_bytes(l_current_data, l_mcc_size - 2, |
5755 | 2); /* Lmcc */ |
5756 | l_current_data += 2; |
5757 | |
5758 | /* first marker */ |
5759 | opj_write_bytes(l_current_data, 0, |
5760 | 2); /* Zmcc */ |
5761 | l_current_data += 2; |
5762 | |
5763 | opj_write_bytes(l_current_data, p_mcc_record->m_index, |
5764 | 1); /* Imcc -> no need for other values, take the first */ |
5765 | ++l_current_data; |
5766 | |
5767 | /* only one marker atm */ |
5768 | opj_write_bytes(l_current_data, 0, |
5769 | 2); /* Ymcc */ |
5770 | l_current_data += 2; |
5771 | |
5772 | opj_write_bytes(l_current_data, 1, |
5773 | 2); /* Qmcc -> number of collections -> 1 */ |
5774 | l_current_data += 2; |
5775 | |
5776 | opj_write_bytes(l_current_data, 0x1, |
5777 | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
5778 | ++l_current_data; |
5779 | |
5780 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
5781 | 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ |
5782 | l_current_data += 2; |
5783 | |
5784 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
5785 | opj_write_bytes(l_current_data, i, |
5786 | l_nb_bytes_for_comp); /* Cmccij Component offset*/ |
5787 | l_current_data += l_nb_bytes_for_comp; |
5788 | } |
5789 | |
5790 | opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, |
5791 | 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ |
5792 | l_current_data += 2; |
5793 | |
5794 | for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { |
5795 | opj_write_bytes(l_current_data, i, |
5796 | l_nb_bytes_for_comp); /* Wmccij Component offset*/ |
5797 | l_current_data += l_nb_bytes_for_comp; |
5798 | } |
5799 | |
5800 | l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16; |
5801 | |
5802 | if (p_mcc_record->m_decorrelation_array) { |
5803 | l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; |
5804 | } |
5805 | |
5806 | if (p_mcc_record->m_offset_array) { |
5807 | l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8); |
5808 | } |
5809 | |
5810 | opj_write_bytes(l_current_data, l_tmcc, |
5811 | 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ |
5812 | l_current_data += 3; |
5813 | |
5814 | if (opj_stream_write_data(p_stream, |
5815 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size, |
5816 | p_manager) != l_mcc_size) { |
5817 | return OPJ_FALSE; |
5818 | } |
5819 | |
5820 | return OPJ_TRUE; |
5821 | } |
5822 | |
5823 | static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, |
5824 | OPJ_BYTE * , |
5825 | OPJ_UINT32 , |
5826 | opj_event_mgr_t * p_manager) |
5827 | { |
5828 | OPJ_UINT32 i, j; |
5829 | OPJ_UINT32 l_tmp; |
5830 | OPJ_UINT32 l_indix; |
5831 | opj_tcp_t * l_tcp; |
5832 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
5833 | opj_mct_data_t * l_mct_data; |
5834 | OPJ_UINT32 l_nb_collections; |
5835 | OPJ_UINT32 l_nb_comps; |
5836 | OPJ_UINT32 l_nb_bytes_by_comp; |
5837 | OPJ_BOOL l_new_mcc = OPJ_FALSE; |
5838 | |
5839 | /* preconditions */ |
5840 | assert(p_header_data != 00); |
5841 | assert(p_j2k != 00); |
5842 | assert(p_manager != 00); |
5843 | |
5844 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
5845 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
5846 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
5847 | |
5848 | if (p_header_size < 2) { |
5849 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
5850 | return OPJ_FALSE; |
5851 | } |
5852 | |
5853 | /* first marker */ |
5854 | opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */ |
5855 | p_header_data += 2; |
5856 | if (l_tmp != 0) { |
5857 | opj_event_msg(p_manager, EVT_WARNING, |
5858 | "Cannot take in charge multiple data spanning\n" ); |
5859 | return OPJ_TRUE; |
5860 | } |
5861 | |
5862 | if (p_header_size < 7) { |
5863 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
5864 | return OPJ_FALSE; |
5865 | } |
5866 | |
5867 | opj_read_bytes(p_header_data, &l_indix, |
5868 | 1); /* Imcc -> no need for other values, take the first */ |
5869 | ++p_header_data; |
5870 | |
5871 | l_mcc_record = l_tcp->m_mcc_records; |
5872 | |
5873 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
5874 | if (l_mcc_record->m_index == l_indix) { |
5875 | break; |
5876 | } |
5877 | ++l_mcc_record; |
5878 | } |
5879 | |
5880 | /** NOT FOUND */ |
5881 | if (i == l_tcp->m_nb_mcc_records) { |
5882 | if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { |
5883 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
5884 | l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; |
5885 | |
5886 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
5887 | l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof( |
5888 | opj_simple_mcc_decorrelation_data_t)); |
5889 | if (! new_mcc_records) { |
5890 | opj_free(l_tcp->m_mcc_records); |
5891 | l_tcp->m_mcc_records = NULL; |
5892 | l_tcp->m_nb_max_mcc_records = 0; |
5893 | l_tcp->m_nb_mcc_records = 0; |
5894 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n" ); |
5895 | return OPJ_FALSE; |
5896 | } |
5897 | l_tcp->m_mcc_records = new_mcc_records; |
5898 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
5899 | memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records) |
5900 | * sizeof(opj_simple_mcc_decorrelation_data_t)); |
5901 | } |
5902 | l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; |
5903 | l_new_mcc = OPJ_TRUE; |
5904 | } |
5905 | l_mcc_record->m_index = l_indix; |
5906 | |
5907 | /* only one marker atm */ |
5908 | opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */ |
5909 | p_header_data += 2; |
5910 | if (l_tmp != 0) { |
5911 | opj_event_msg(p_manager, EVT_WARNING, |
5912 | "Cannot take in charge multiple data spanning\n" ); |
5913 | return OPJ_TRUE; |
5914 | } |
5915 | |
5916 | opj_read_bytes(p_header_data, &l_nb_collections, |
5917 | 2); /* Qmcc -> number of collections -> 1 */ |
5918 | p_header_data += 2; |
5919 | |
5920 | if (l_nb_collections > 1) { |
5921 | opj_event_msg(p_manager, EVT_WARNING, |
5922 | "Cannot take in charge multiple collections\n" ); |
5923 | return OPJ_TRUE; |
5924 | } |
5925 | |
5926 | p_header_size -= 7; |
5927 | |
5928 | for (i = 0; i < l_nb_collections; ++i) { |
5929 | if (p_header_size < 3) { |
5930 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
5931 | return OPJ_FALSE; |
5932 | } |
5933 | |
5934 | opj_read_bytes(p_header_data, &l_tmp, |
5935 | 1); /* Xmcci type of component transformation -> array based decorrelation */ |
5936 | ++p_header_data; |
5937 | |
5938 | if (l_tmp != 1) { |
5939 | opj_event_msg(p_manager, EVT_WARNING, |
5940 | "Cannot take in charge collections other than array decorrelation\n" ); |
5941 | return OPJ_TRUE; |
5942 | } |
5943 | |
5944 | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
5945 | |
5946 | p_header_data += 2; |
5947 | p_header_size -= 3; |
5948 | |
5949 | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
5950 | l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; |
5951 | |
5952 | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { |
5953 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
5954 | return OPJ_FALSE; |
5955 | } |
5956 | |
5957 | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); |
5958 | |
5959 | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
5960 | opj_read_bytes(p_header_data, &l_tmp, |
5961 | l_nb_bytes_by_comp); /* Cmccij Component offset*/ |
5962 | p_header_data += l_nb_bytes_by_comp; |
5963 | |
5964 | if (l_tmp != j) { |
5965 | opj_event_msg(p_manager, EVT_WARNING, |
5966 | "Cannot take in charge collections with indix shuffle\n" ); |
5967 | return OPJ_TRUE; |
5968 | } |
5969 | } |
5970 | |
5971 | opj_read_bytes(p_header_data, &l_nb_comps, 2); |
5972 | p_header_data += 2; |
5973 | |
5974 | l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); |
5975 | l_nb_comps &= 0x7fff; |
5976 | |
5977 | if (l_nb_comps != l_mcc_record->m_nb_comps) { |
5978 | opj_event_msg(p_manager, EVT_WARNING, |
5979 | "Cannot take in charge collections without same number of indixes\n" ); |
5980 | return OPJ_TRUE; |
5981 | } |
5982 | |
5983 | if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { |
5984 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
5985 | return OPJ_FALSE; |
5986 | } |
5987 | |
5988 | p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); |
5989 | |
5990 | for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { |
5991 | opj_read_bytes(p_header_data, &l_tmp, |
5992 | l_nb_bytes_by_comp); /* Wmccij Component offset*/ |
5993 | p_header_data += l_nb_bytes_by_comp; |
5994 | |
5995 | if (l_tmp != j) { |
5996 | opj_event_msg(p_manager, EVT_WARNING, |
5997 | "Cannot take in charge collections with indix shuffle\n" ); |
5998 | return OPJ_TRUE; |
5999 | } |
6000 | } |
6001 | |
6002 | opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/ |
6003 | p_header_data += 3; |
6004 | |
6005 | l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1); |
6006 | l_mcc_record->m_decorrelation_array = 00; |
6007 | l_mcc_record->m_offset_array = 00; |
6008 | |
6009 | l_indix = l_tmp & 0xff; |
6010 | if (l_indix != 0) { |
6011 | l_mct_data = l_tcp->m_mct_records; |
6012 | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6013 | if (l_mct_data->m_index == l_indix) { |
6014 | l_mcc_record->m_decorrelation_array = l_mct_data; |
6015 | break; |
6016 | } |
6017 | ++l_mct_data; |
6018 | } |
6019 | |
6020 | if (l_mcc_record->m_decorrelation_array == 00) { |
6021 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
6022 | return OPJ_FALSE; |
6023 | } |
6024 | } |
6025 | |
6026 | l_indix = (l_tmp >> 8) & 0xff; |
6027 | if (l_indix != 0) { |
6028 | l_mct_data = l_tcp->m_mct_records; |
6029 | for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { |
6030 | if (l_mct_data->m_index == l_indix) { |
6031 | l_mcc_record->m_offset_array = l_mct_data; |
6032 | break; |
6033 | } |
6034 | ++l_mct_data; |
6035 | } |
6036 | |
6037 | if (l_mcc_record->m_offset_array == 00) { |
6038 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
6039 | return OPJ_FALSE; |
6040 | } |
6041 | } |
6042 | } |
6043 | |
6044 | if (p_header_size != 0) { |
6045 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n" ); |
6046 | return OPJ_FALSE; |
6047 | } |
6048 | |
6049 | if (l_new_mcc) { |
6050 | ++l_tcp->m_nb_mcc_records; |
6051 | } |
6052 | |
6053 | return OPJ_TRUE; |
6054 | } |
6055 | |
6056 | static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, |
6057 | struct opj_stream_private *p_stream, |
6058 | struct opj_event_mgr * p_manager |
6059 | ) |
6060 | { |
6061 | OPJ_BYTE * l_current_data = 00; |
6062 | OPJ_UINT32 l_mco_size; |
6063 | opj_tcp_t * l_tcp = 00; |
6064 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6065 | OPJ_UINT32 i; |
6066 | |
6067 | /* preconditions */ |
6068 | assert(p_j2k != 00); |
6069 | assert(p_manager != 00); |
6070 | assert(p_stream != 00); |
6071 | |
6072 | l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
6073 | |
6074 | l_mco_size = 5 + l_tcp->m_nb_mcc_records; |
6075 | if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6076 | |
6077 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
6078 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); |
6079 | if (! new_header_tile_data) { |
6080 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6081 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6082 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6083 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n" ); |
6084 | return OPJ_FALSE; |
6085 | } |
6086 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6087 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; |
6088 | } |
6089 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6090 | |
6091 | |
6092 | opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */ |
6093 | l_current_data += 2; |
6094 | |
6095 | opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */ |
6096 | l_current_data += 2; |
6097 | |
6098 | opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records, |
6099 | 1); /* Nmco : only one transform stage*/ |
6100 | ++l_current_data; |
6101 | |
6102 | l_mcc_record = l_tcp->m_mcc_records; |
6103 | for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { |
6104 | opj_write_bytes(l_current_data, l_mcc_record->m_index, |
6105 | 1); /* Imco -> use the mcc indicated by 1*/ |
6106 | ++l_current_data; |
6107 | ++l_mcc_record; |
6108 | } |
6109 | |
6110 | if (opj_stream_write_data(p_stream, |
6111 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size, |
6112 | p_manager) != l_mco_size) { |
6113 | return OPJ_FALSE; |
6114 | } |
6115 | |
6116 | return OPJ_TRUE; |
6117 | } |
6118 | |
6119 | /** |
6120 | * Reads a MCO marker (Multiple Component Transform Ordering) |
6121 | * |
6122 | * @param p_header_data the data contained in the MCO box. |
6123 | * @param p_j2k the jpeg2000 codec. |
6124 | * @param p_header_size the size of the data contained in the MCO marker. |
6125 | * @param p_manager the user event manager. |
6126 | */ |
6127 | static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, |
6128 | OPJ_BYTE * , |
6129 | OPJ_UINT32 , |
6130 | opj_event_mgr_t * p_manager |
6131 | ) |
6132 | { |
6133 | OPJ_UINT32 l_tmp, i; |
6134 | OPJ_UINT32 l_nb_stages; |
6135 | opj_tcp_t * l_tcp; |
6136 | opj_tccp_t * l_tccp; |
6137 | opj_image_t * l_image; |
6138 | |
6139 | /* preconditions */ |
6140 | assert(p_header_data != 00); |
6141 | assert(p_j2k != 00); |
6142 | assert(p_manager != 00); |
6143 | |
6144 | l_image = p_j2k->m_private_image; |
6145 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
6146 | &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : |
6147 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
6148 | |
6149 | if (p_header_size < 1) { |
6150 | opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n" ); |
6151 | return OPJ_FALSE; |
6152 | } |
6153 | |
6154 | opj_read_bytes(p_header_data, &l_nb_stages, |
6155 | 1); /* Nmco : only one transform stage*/ |
6156 | ++p_header_data; |
6157 | |
6158 | if (l_nb_stages > 1) { |
6159 | opj_event_msg(p_manager, EVT_WARNING, |
6160 | "Cannot take in charge multiple transformation stages.\n" ); |
6161 | return OPJ_TRUE; |
6162 | } |
6163 | |
6164 | if (p_header_size != l_nb_stages + 1) { |
6165 | opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n" ); |
6166 | return OPJ_FALSE; |
6167 | } |
6168 | |
6169 | l_tccp = l_tcp->tccps; |
6170 | |
6171 | for (i = 0; i < l_image->numcomps; ++i) { |
6172 | l_tccp->m_dc_level_shift = 0; |
6173 | ++l_tccp; |
6174 | } |
6175 | |
6176 | if (l_tcp->m_mct_decoding_matrix) { |
6177 | opj_free(l_tcp->m_mct_decoding_matrix); |
6178 | l_tcp->m_mct_decoding_matrix = 00; |
6179 | } |
6180 | |
6181 | for (i = 0; i < l_nb_stages; ++i) { |
6182 | opj_read_bytes(p_header_data, &l_tmp, 1); |
6183 | ++p_header_data; |
6184 | |
6185 | if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) { |
6186 | return OPJ_FALSE; |
6187 | } |
6188 | } |
6189 | |
6190 | return OPJ_TRUE; |
6191 | } |
6192 | |
6193 | static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, |
6194 | OPJ_UINT32 p_index) |
6195 | { |
6196 | OPJ_UINT32 i; |
6197 | opj_simple_mcc_decorrelation_data_t * l_mcc_record; |
6198 | opj_mct_data_t * l_deco_array, * l_offset_array; |
6199 | OPJ_UINT32 l_data_size, l_mct_size, l_offset_size; |
6200 | OPJ_UINT32 l_nb_elem; |
6201 | OPJ_UINT32 * l_offset_data, * l_current_offset_data; |
6202 | opj_tccp_t * l_tccp; |
6203 | |
6204 | /* preconditions */ |
6205 | assert(p_tcp != 00); |
6206 | |
6207 | l_mcc_record = p_tcp->m_mcc_records; |
6208 | |
6209 | for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) { |
6210 | if (l_mcc_record->m_index == p_index) { |
6211 | break; |
6212 | } |
6213 | } |
6214 | |
6215 | if (i == p_tcp->m_nb_mcc_records) { |
6216 | /** element discarded **/ |
6217 | return OPJ_TRUE; |
6218 | } |
6219 | |
6220 | if (l_mcc_record->m_nb_comps != p_image->numcomps) { |
6221 | /** do not support number of comps != image */ |
6222 | return OPJ_TRUE; |
6223 | } |
6224 | |
6225 | l_deco_array = l_mcc_record->m_decorrelation_array; |
6226 | |
6227 | if (l_deco_array) { |
6228 | l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps |
6229 | * p_image->numcomps; |
6230 | if (l_deco_array->m_data_size != l_data_size) { |
6231 | return OPJ_FALSE; |
6232 | } |
6233 | |
6234 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
6235 | l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); |
6236 | p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
6237 | |
6238 | if (! p_tcp->m_mct_decoding_matrix) { |
6239 | return OPJ_FALSE; |
6240 | } |
6241 | |
6242 | j2k_mct_read_functions_to_float[l_deco_array->m_element_type]( |
6243 | l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem); |
6244 | } |
6245 | |
6246 | l_offset_array = l_mcc_record->m_offset_array; |
6247 | |
6248 | if (l_offset_array) { |
6249 | l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * |
6250 | p_image->numcomps; |
6251 | if (l_offset_array->m_data_size != l_data_size) { |
6252 | return OPJ_FALSE; |
6253 | } |
6254 | |
6255 | l_nb_elem = p_image->numcomps; |
6256 | l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); |
6257 | l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); |
6258 | |
6259 | if (! l_offset_data) { |
6260 | return OPJ_FALSE; |
6261 | } |
6262 | |
6263 | j2k_mct_read_functions_to_int32[l_offset_array->m_element_type]( |
6264 | l_offset_array->m_data, l_offset_data, l_nb_elem); |
6265 | |
6266 | l_tccp = p_tcp->tccps; |
6267 | l_current_offset_data = l_offset_data; |
6268 | |
6269 | for (i = 0; i < p_image->numcomps; ++i) { |
6270 | l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++); |
6271 | ++l_tccp; |
6272 | } |
6273 | |
6274 | opj_free(l_offset_data); |
6275 | } |
6276 | |
6277 | return OPJ_TRUE; |
6278 | } |
6279 | |
6280 | static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, |
6281 | struct opj_stream_private *p_stream, |
6282 | struct opj_event_mgr * p_manager) |
6283 | { |
6284 | OPJ_UINT32 i; |
6285 | OPJ_UINT32 l_cbd_size; |
6286 | OPJ_BYTE * l_current_data = 00; |
6287 | opj_image_t *l_image = 00; |
6288 | opj_image_comp_t * l_comp = 00; |
6289 | |
6290 | /* preconditions */ |
6291 | assert(p_j2k != 00); |
6292 | assert(p_manager != 00); |
6293 | assert(p_stream != 00); |
6294 | |
6295 | l_image = p_j2k->m_private_image; |
6296 | l_cbd_size = 6 + p_j2k->m_private_image->numcomps; |
6297 | |
6298 | if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { |
6299 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
6300 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); |
6301 | if (! new_header_tile_data) { |
6302 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
6303 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; |
6304 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
6305 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n" ); |
6306 | return OPJ_FALSE; |
6307 | } |
6308 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; |
6309 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; |
6310 | } |
6311 | |
6312 | l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; |
6313 | |
6314 | opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */ |
6315 | l_current_data += 2; |
6316 | |
6317 | opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */ |
6318 | l_current_data += 2; |
6319 | |
6320 | opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */ |
6321 | l_current_data += 2; |
6322 | |
6323 | l_comp = l_image->comps; |
6324 | |
6325 | for (i = 0; i < l_image->numcomps; ++i) { |
6326 | opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), |
6327 | 1); /* Component bit depth */ |
6328 | ++l_current_data; |
6329 | |
6330 | ++l_comp; |
6331 | } |
6332 | |
6333 | if (opj_stream_write_data(p_stream, |
6334 | p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size, |
6335 | p_manager) != l_cbd_size) { |
6336 | return OPJ_FALSE; |
6337 | } |
6338 | |
6339 | return OPJ_TRUE; |
6340 | } |
6341 | |
6342 | /** |
6343 | * Reads a CBD marker (Component bit depth definition) |
6344 | * @param p_header_data the data contained in the CBD box. |
6345 | * @param p_j2k the jpeg2000 codec. |
6346 | * @param p_header_size the size of the data contained in the CBD marker. |
6347 | * @param p_manager the user event manager. |
6348 | */ |
6349 | static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, |
6350 | OPJ_BYTE * , |
6351 | OPJ_UINT32 , |
6352 | opj_event_mgr_t * p_manager |
6353 | ) |
6354 | { |
6355 | OPJ_UINT32 l_nb_comp, l_num_comp; |
6356 | OPJ_UINT32 l_comp_def; |
6357 | OPJ_UINT32 i; |
6358 | opj_image_comp_t * l_comp = 00; |
6359 | |
6360 | /* preconditions */ |
6361 | assert(p_header_data != 00); |
6362 | assert(p_j2k != 00); |
6363 | assert(p_manager != 00); |
6364 | |
6365 | l_num_comp = p_j2k->m_private_image->numcomps; |
6366 | |
6367 | if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { |
6368 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n" ); |
6369 | return OPJ_FALSE; |
6370 | } |
6371 | |
6372 | opj_read_bytes(p_header_data, &l_nb_comp, |
6373 | 2); /* Ncbd */ |
6374 | p_header_data += 2; |
6375 | |
6376 | if (l_nb_comp != l_num_comp) { |
6377 | opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n" ); |
6378 | return OPJ_FALSE; |
6379 | } |
6380 | |
6381 | l_comp = p_j2k->m_private_image->comps; |
6382 | for (i = 0; i < l_num_comp; ++i) { |
6383 | opj_read_bytes(p_header_data, &l_comp_def, |
6384 | 1); /* Component bit depth */ |
6385 | ++p_header_data; |
6386 | l_comp->sgnd = (l_comp_def >> 7) & 1; |
6387 | l_comp->prec = (l_comp_def & 0x7f) + 1; |
6388 | |
6389 | if (l_comp->prec > 31) { |
6390 | opj_event_msg(p_manager, EVT_ERROR, |
6391 | "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n" , |
6392 | i, l_comp->prec); |
6393 | return OPJ_FALSE; |
6394 | } |
6395 | ++l_comp; |
6396 | } |
6397 | |
6398 | return OPJ_TRUE; |
6399 | } |
6400 | |
6401 | /* ----------------------------------------------------------------------- */ |
6402 | /* J2K / JPT decoder interface */ |
6403 | /* ----------------------------------------------------------------------- */ |
6404 | |
6405 | void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) |
6406 | { |
6407 | if (j2k && parameters) { |
6408 | j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; |
6409 | j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; |
6410 | |
6411 | j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG); |
6412 | #ifdef USE_JPWL |
6413 | j2k->m_cp.correct = parameters->jpwl_correct; |
6414 | j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; |
6415 | j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; |
6416 | #endif /* USE_JPWL */ |
6417 | } |
6418 | } |
6419 | |
6420 | OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads) |
6421 | { |
6422 | if (opj_has_thread_support()) { |
6423 | opj_thread_pool_destroy(j2k->m_tp); |
6424 | j2k->m_tp = NULL; |
6425 | if (num_threads <= (OPJ_UINT32)INT_MAX) { |
6426 | j2k->m_tp = opj_thread_pool_create((int)num_threads); |
6427 | } |
6428 | if (j2k->m_tp == NULL) { |
6429 | j2k->m_tp = opj_thread_pool_create(0); |
6430 | return OPJ_FALSE; |
6431 | } |
6432 | return OPJ_TRUE; |
6433 | } |
6434 | return OPJ_FALSE; |
6435 | } |
6436 | |
6437 | static int opj_j2k_get_default_thread_count(void) |
6438 | { |
6439 | #if defined(MUTEX_win32) || defined(MUTEX_pthread) |
6440 | const char* num_threads_str = getenv("OPJ_NUM_THREADS" ); |
6441 | int num_cpus; |
6442 | int num_threads; |
6443 | |
6444 | if (num_threads_str == NULL || !opj_has_thread_support()) { |
6445 | return 0; |
6446 | } |
6447 | num_cpus = opj_get_num_cpus(); |
6448 | if (strcmp(num_threads_str, "ALL_CPUS" ) == 0) { |
6449 | return num_cpus; |
6450 | } |
6451 | if (num_cpus == 0) { |
6452 | num_cpus = 32; |
6453 | } |
6454 | num_threads = atoi(num_threads_str); |
6455 | if (num_threads < 0) { |
6456 | num_threads = 0; |
6457 | } else if (num_threads > 2 * num_cpus) { |
6458 | num_threads = 2 * num_cpus; |
6459 | } |
6460 | return num_threads; |
6461 | #else |
6462 | return 0; |
6463 | #endif |
6464 | } |
6465 | |
6466 | /* ----------------------------------------------------------------------- */ |
6467 | /* J2K encoder interface */ |
6468 | /* ----------------------------------------------------------------------- */ |
6469 | |
6470 | opj_j2k_t* opj_j2k_create_compress(void) |
6471 | { |
6472 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
6473 | if (!l_j2k) { |
6474 | return NULL; |
6475 | } |
6476 | |
6477 | |
6478 | l_j2k->m_is_decoder = 0; |
6479 | l_j2k->m_cp.m_is_decoder = 0; |
6480 | |
6481 | l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc( |
6482 | OPJ_J2K_DEFAULT_HEADER_SIZE); |
6483 | if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
6484 | opj_j2k_destroy(l_j2k); |
6485 | return NULL; |
6486 | } |
6487 | |
6488 | l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = |
6489 | OPJ_J2K_DEFAULT_HEADER_SIZE; |
6490 | |
6491 | /* validation list creation*/ |
6492 | l_j2k->m_validation_list = opj_procedure_list_create(); |
6493 | if (! l_j2k->m_validation_list) { |
6494 | opj_j2k_destroy(l_j2k); |
6495 | return NULL; |
6496 | } |
6497 | |
6498 | /* execution list creation*/ |
6499 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
6500 | if (! l_j2k->m_procedure_list) { |
6501 | opj_j2k_destroy(l_j2k); |
6502 | return NULL; |
6503 | } |
6504 | |
6505 | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
6506 | if (!l_j2k->m_tp) { |
6507 | l_j2k->m_tp = opj_thread_pool_create(0); |
6508 | } |
6509 | if (!l_j2k->m_tp) { |
6510 | opj_j2k_destroy(l_j2k); |
6511 | return NULL; |
6512 | } |
6513 | |
6514 | return l_j2k; |
6515 | } |
6516 | |
6517 | static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres) |
6518 | { |
6519 | POC[0].tile = 1; |
6520 | POC[0].resno0 = 0; |
6521 | POC[0].compno0 = 0; |
6522 | POC[0].layno1 = 1; |
6523 | POC[0].resno1 = (OPJ_UINT32)(numres - 1); |
6524 | POC[0].compno1 = 3; |
6525 | POC[0].prg1 = OPJ_CPRL; |
6526 | POC[1].tile = 1; |
6527 | POC[1].resno0 = (OPJ_UINT32)(numres - 1); |
6528 | POC[1].compno0 = 0; |
6529 | POC[1].layno1 = 1; |
6530 | POC[1].resno1 = (OPJ_UINT32)numres; |
6531 | POC[1].compno1 = 3; |
6532 | POC[1].prg1 = OPJ_CPRL; |
6533 | return 2; |
6534 | } |
6535 | |
6536 | static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, |
6537 | opj_image_t *image, opj_event_mgr_t *p_manager) |
6538 | { |
6539 | /* Configure cinema parameters */ |
6540 | int i; |
6541 | |
6542 | /* No tiling */ |
6543 | parameters->tile_size_on = OPJ_FALSE; |
6544 | parameters->cp_tdx = 1; |
6545 | parameters->cp_tdy = 1; |
6546 | |
6547 | /* One tile part for each component */ |
6548 | parameters->tp_flag = 'C'; |
6549 | parameters->tp_on = 1; |
6550 | |
6551 | /* Tile and Image shall be at (0,0) */ |
6552 | parameters->cp_tx0 = 0; |
6553 | parameters->cp_ty0 = 0; |
6554 | parameters->image_offset_x0 = 0; |
6555 | parameters->image_offset_y0 = 0; |
6556 | |
6557 | /* Codeblock size= 32*32 */ |
6558 | parameters->cblockw_init = 32; |
6559 | parameters->cblockh_init = 32; |
6560 | |
6561 | /* Codeblock style: no mode switch enabled */ |
6562 | parameters->mode = 0; |
6563 | |
6564 | /* No ROI */ |
6565 | parameters->roi_compno = -1; |
6566 | |
6567 | /* No subsampling */ |
6568 | parameters->subsampling_dx = 1; |
6569 | parameters->subsampling_dy = 1; |
6570 | |
6571 | /* 9-7 transform */ |
6572 | parameters->irreversible = 1; |
6573 | |
6574 | /* Number of layers */ |
6575 | if (parameters->tcp_numlayers > 1) { |
6576 | opj_event_msg(p_manager, EVT_WARNING, |
6577 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6578 | "1 single quality layer" |
6579 | "-> Number of layers forced to 1 (rather than %d)\n" |
6580 | "-> Rate of the last layer (%3.1f) will be used" , |
6581 | parameters->tcp_numlayers, |
6582 | parameters->tcp_rates[parameters->tcp_numlayers - 1]); |
6583 | parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1]; |
6584 | parameters->tcp_numlayers = 1; |
6585 | } |
6586 | |
6587 | /* Resolution levels */ |
6588 | switch (parameters->rsiz) { |
6589 | case OPJ_PROFILE_CINEMA_2K: |
6590 | if (parameters->numresolution > 6) { |
6591 | opj_event_msg(p_manager, EVT_WARNING, |
6592 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6593 | "Number of decomposition levels <= 5\n" |
6594 | "-> Number of decomposition levels forced to 5 (rather than %d)\n" , |
6595 | parameters->numresolution + 1); |
6596 | parameters->numresolution = 6; |
6597 | } |
6598 | break; |
6599 | case OPJ_PROFILE_CINEMA_4K: |
6600 | if (parameters->numresolution < 2) { |
6601 | opj_event_msg(p_manager, EVT_WARNING, |
6602 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6603 | "Number of decomposition levels >= 1 && <= 6\n" |
6604 | "-> Number of decomposition levels forced to 1 (rather than %d)\n" , |
6605 | parameters->numresolution + 1); |
6606 | parameters->numresolution = 1; |
6607 | } else if (parameters->numresolution > 7) { |
6608 | opj_event_msg(p_manager, EVT_WARNING, |
6609 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6610 | "Number of decomposition levels >= 1 && <= 6\n" |
6611 | "-> Number of decomposition levels forced to 6 (rather than %d)\n" , |
6612 | parameters->numresolution + 1); |
6613 | parameters->numresolution = 7; |
6614 | } |
6615 | break; |
6616 | default : |
6617 | break; |
6618 | } |
6619 | |
6620 | /* Precincts */ |
6621 | parameters->csty |= 0x01; |
6622 | if (parameters->numresolution == 1) { |
6623 | parameters->res_spec = 1; |
6624 | parameters->prcw_init[0] = 128; |
6625 | parameters->prch_init[0] = 128; |
6626 | } else { |
6627 | parameters->res_spec = parameters->numresolution - 1; |
6628 | for (i = 0; i < parameters->res_spec; i++) { |
6629 | parameters->prcw_init[i] = 256; |
6630 | parameters->prch_init[i] = 256; |
6631 | } |
6632 | } |
6633 | |
6634 | /* The progression order shall be CPRL */ |
6635 | parameters->prog_order = OPJ_CPRL; |
6636 | |
6637 | /* Progression order changes for 4K, disallowed for 2K */ |
6638 | if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { |
6639 | parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC, |
6640 | parameters->numresolution); |
6641 | } else { |
6642 | parameters->numpocs = 0; |
6643 | } |
6644 | |
6645 | /* Limited bit-rate */ |
6646 | parameters->cp_disto_alloc = 1; |
6647 | if (parameters->max_cs_size <= 0) { |
6648 | /* No rate has been introduced, 24 fps is assumed */ |
6649 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6650 | opj_event_msg(p_manager, EVT_WARNING, |
6651 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6652 | "Maximum 1302083 compressed bytes @ 24fps\n" |
6653 | "As no rate has been given, this limit will be used.\n" ); |
6654 | } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { |
6655 | opj_event_msg(p_manager, EVT_WARNING, |
6656 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6657 | "Maximum 1302083 compressed bytes @ 24fps\n" |
6658 | "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n" ); |
6659 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6660 | } |
6661 | |
6662 | if (parameters->max_comp_size <= 0) { |
6663 | /* No rate has been introduced, 24 fps is assumed */ |
6664 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6665 | opj_event_msg(p_manager, EVT_WARNING, |
6666 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6667 | "Maximum 1041666 compressed bytes @ 24fps\n" |
6668 | "As no rate has been given, this limit will be used.\n" ); |
6669 | } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { |
6670 | opj_event_msg(p_manager, EVT_WARNING, |
6671 | "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" |
6672 | "Maximum 1041666 compressed bytes @ 24fps\n" |
6673 | "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n" ); |
6674 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6675 | } |
6676 | |
6677 | parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * |
6678 | image->comps[0].h * image->comps[0].prec) / |
6679 | (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * |
6680 | image->comps[0].dy); |
6681 | |
6682 | } |
6683 | |
6684 | static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, |
6685 | opj_event_mgr_t *p_manager) |
6686 | { |
6687 | OPJ_UINT32 i; |
6688 | |
6689 | /* Number of components */ |
6690 | if (image->numcomps != 3) { |
6691 | opj_event_msg(p_manager, EVT_WARNING, |
6692 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6693 | "3 components" |
6694 | "-> Number of components of input image (%d) is not compliant\n" |
6695 | "-> Non-profile-3 codestream will be generated\n" , |
6696 | image->numcomps); |
6697 | return OPJ_FALSE; |
6698 | } |
6699 | |
6700 | /* Bitdepth */ |
6701 | for (i = 0; i < image->numcomps; i++) { |
6702 | if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)) { |
6703 | char signed_str[] = "signed" ; |
6704 | char unsigned_str[] = "unsigned" ; |
6705 | char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; |
6706 | opj_event_msg(p_manager, EVT_WARNING, |
6707 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6708 | "Precision of each component shall be 12 bits unsigned" |
6709 | "-> At least component %d of input image (%d bits, %s) is not compliant\n" |
6710 | "-> Non-profile-3 codestream will be generated\n" , |
6711 | i, image->comps[i].bpp, tmp_str); |
6712 | return OPJ_FALSE; |
6713 | } |
6714 | } |
6715 | |
6716 | /* Image size */ |
6717 | switch (rsiz) { |
6718 | case OPJ_PROFILE_CINEMA_2K: |
6719 | if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) { |
6720 | opj_event_msg(p_manager, EVT_WARNING, |
6721 | "JPEG 2000 Profile-3 (2k dc profile) requires:\n" |
6722 | "width <= 2048 and height <= 1080\n" |
6723 | "-> Input image size %d x %d is not compliant\n" |
6724 | "-> Non-profile-3 codestream will be generated\n" , |
6725 | image->comps[0].w, image->comps[0].h); |
6726 | return OPJ_FALSE; |
6727 | } |
6728 | break; |
6729 | case OPJ_PROFILE_CINEMA_4K: |
6730 | if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) { |
6731 | opj_event_msg(p_manager, EVT_WARNING, |
6732 | "JPEG 2000 Profile-4 (4k dc profile) requires:\n" |
6733 | "width <= 4096 and height <= 2160\n" |
6734 | "-> Image size %d x %d is not compliant\n" |
6735 | "-> Non-profile-4 codestream will be generated\n" , |
6736 | image->comps[0].w, image->comps[0].h); |
6737 | return OPJ_FALSE; |
6738 | } |
6739 | break; |
6740 | default : |
6741 | break; |
6742 | } |
6743 | |
6744 | return OPJ_TRUE; |
6745 | } |
6746 | |
6747 | OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, |
6748 | opj_cparameters_t *parameters, |
6749 | opj_image_t *image, |
6750 | opj_event_mgr_t * p_manager) |
6751 | { |
6752 | OPJ_UINT32 i, j, tileno, numpocs_tile; |
6753 | opj_cp_t *cp = 00; |
6754 | OPJ_UINT32 cblkw, cblkh; |
6755 | |
6756 | if (!p_j2k || !parameters || ! image) { |
6757 | return OPJ_FALSE; |
6758 | } |
6759 | |
6760 | if ((parameters->numresolution <= 0) || |
6761 | (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { |
6762 | opj_event_msg(p_manager, EVT_ERROR, |
6763 | "Invalid number of resolutions : %d not in range [1,%d]\n" , |
6764 | parameters->numresolution, OPJ_J2K_MAXRLVLS); |
6765 | return OPJ_FALSE; |
6766 | } |
6767 | |
6768 | if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) { |
6769 | opj_event_msg(p_manager, EVT_ERROR, |
6770 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n" , |
6771 | parameters->cblockw_init); |
6772 | return OPJ_FALSE; |
6773 | } |
6774 | if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) { |
6775 | opj_event_msg(p_manager, EVT_ERROR, |
6776 | "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n" , |
6777 | parameters->cblockh_init); |
6778 | return OPJ_FALSE; |
6779 | } |
6780 | if (parameters->cblockw_init * parameters->cblockh_init > 4096) { |
6781 | opj_event_msg(p_manager, EVT_ERROR, |
6782 | "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n" ); |
6783 | return OPJ_FALSE; |
6784 | } |
6785 | cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
6786 | cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
6787 | if (parameters->cblockw_init != (1 << cblkw)) { |
6788 | opj_event_msg(p_manager, EVT_ERROR, |
6789 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n" , |
6790 | parameters->cblockw_init); |
6791 | return OPJ_FALSE; |
6792 | } |
6793 | if (parameters->cblockh_init != (1 << cblkh)) { |
6794 | opj_event_msg(p_manager, EVT_ERROR, |
6795 | "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n" , |
6796 | parameters->cblockh_init); |
6797 | return OPJ_FALSE; |
6798 | } |
6799 | |
6800 | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ |
6801 | cp = &(p_j2k->m_cp); |
6802 | |
6803 | /* set default values for cp */ |
6804 | cp->tw = 1; |
6805 | cp->th = 1; |
6806 | |
6807 | /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ |
6808 | if (parameters->rsiz == |
6809 | OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ |
6810 | OPJ_BOOL deprecated_used = OPJ_FALSE; |
6811 | switch (parameters->cp_cinema) { |
6812 | case OPJ_CINEMA2K_24: |
6813 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
6814 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6815 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6816 | deprecated_used = OPJ_TRUE; |
6817 | break; |
6818 | case OPJ_CINEMA2K_48: |
6819 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
6820 | parameters->max_cs_size = OPJ_CINEMA_48_CS; |
6821 | parameters->max_comp_size = OPJ_CINEMA_48_COMP; |
6822 | deprecated_used = OPJ_TRUE; |
6823 | break; |
6824 | case OPJ_CINEMA4K_24: |
6825 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
6826 | parameters->max_cs_size = OPJ_CINEMA_24_CS; |
6827 | parameters->max_comp_size = OPJ_CINEMA_24_COMP; |
6828 | deprecated_used = OPJ_TRUE; |
6829 | break; |
6830 | case OPJ_OFF: |
6831 | default: |
6832 | break; |
6833 | } |
6834 | switch (parameters->cp_rsiz) { |
6835 | case OPJ_CINEMA2K: |
6836 | parameters->rsiz = OPJ_PROFILE_CINEMA_2K; |
6837 | deprecated_used = OPJ_TRUE; |
6838 | break; |
6839 | case OPJ_CINEMA4K: |
6840 | parameters->rsiz = OPJ_PROFILE_CINEMA_4K; |
6841 | deprecated_used = OPJ_TRUE; |
6842 | break; |
6843 | case OPJ_MCT: |
6844 | parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; |
6845 | deprecated_used = OPJ_TRUE; |
6846 | case OPJ_STD_RSIZ: |
6847 | default: |
6848 | break; |
6849 | } |
6850 | if (deprecated_used) { |
6851 | opj_event_msg(p_manager, EVT_WARNING, |
6852 | "Deprecated fields cp_cinema or cp_rsiz are used\n" |
6853 | "Please consider using only the rsiz field\n" |
6854 | "See openjpeg.h documentation for more details\n" ); |
6855 | } |
6856 | } |
6857 | |
6858 | /* If no explicit layers are provided, use lossless settings */ |
6859 | if (parameters->tcp_numlayers == 0) { |
6860 | parameters->tcp_numlayers = 1; |
6861 | parameters->cp_disto_alloc = 1; |
6862 | parameters->tcp_rates[0] = 0; |
6863 | } |
6864 | |
6865 | if (parameters->cp_disto_alloc) { |
6866 | /* Emit warnings if tcp_rates are not decreasing */ |
6867 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
6868 | OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i]; |
6869 | OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1]; |
6870 | if (rate_i_corr <= 1.0) { |
6871 | rate_i_corr = 1.0; |
6872 | } |
6873 | if (rate_i_m_1_corr <= 1.0) { |
6874 | rate_i_m_1_corr = 1.0; |
6875 | } |
6876 | if (rate_i_corr >= rate_i_m_1_corr) { |
6877 | if (rate_i_corr != parameters->tcp_rates[i] && |
6878 | rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
6879 | opj_event_msg(p_manager, EVT_WARNING, |
6880 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
6881 | "than tcp_rates[%d]=%f (corrected as %f)\n" , |
6882 | i, parameters->tcp_rates[i], rate_i_corr, |
6883 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
6884 | } else if (rate_i_corr != parameters->tcp_rates[i]) { |
6885 | opj_event_msg(p_manager, EVT_WARNING, |
6886 | "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " |
6887 | "than tcp_rates[%d]=%f\n" , |
6888 | i, parameters->tcp_rates[i], rate_i_corr, |
6889 | i - 1, parameters->tcp_rates[i - 1]); |
6890 | } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { |
6891 | opj_event_msg(p_manager, EVT_WARNING, |
6892 | "tcp_rates[%d]=%f should be strictly lesser " |
6893 | "than tcp_rates[%d]=%f (corrected as %f)\n" , |
6894 | i, parameters->tcp_rates[i], |
6895 | i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); |
6896 | } else { |
6897 | opj_event_msg(p_manager, EVT_WARNING, |
6898 | "tcp_rates[%d]=%f should be strictly lesser " |
6899 | "than tcp_rates[%d]=%f\n" , |
6900 | i, parameters->tcp_rates[i], |
6901 | i - 1, parameters->tcp_rates[i - 1]); |
6902 | } |
6903 | } |
6904 | } |
6905 | } else if (parameters->cp_fixed_quality) { |
6906 | /* Emit warnings if tcp_distoratio are not increasing */ |
6907 | for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
6908 | if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] && |
6909 | !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 && |
6910 | parameters->tcp_distoratio[i] == 0)) { |
6911 | opj_event_msg(p_manager, EVT_WARNING, |
6912 | "tcp_distoratio[%d]=%f should be strictly greater " |
6913 | "than tcp_distoratio[%d]=%f\n" , |
6914 | i, parameters->tcp_distoratio[i], i - 1, |
6915 | parameters->tcp_distoratio[i - 1]); |
6916 | } |
6917 | } |
6918 | } |
6919 | |
6920 | /* see if max_codestream_size does limit input rate */ |
6921 | if (parameters->max_cs_size <= 0) { |
6922 | if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) { |
6923 | OPJ_FLOAT32 temp_size; |
6924 | temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
6925 | image->comps[0].h * image->comps[0].prec) / |
6926 | ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 * |
6927 | image->comps[0].dx * image->comps[0].dy)); |
6928 | if (temp_size > INT_MAX) { |
6929 | parameters->max_cs_size = INT_MAX; |
6930 | } else { |
6931 | parameters->max_cs_size = (int) floor(temp_size); |
6932 | } |
6933 | } else { |
6934 | parameters->max_cs_size = 0; |
6935 | } |
6936 | } else { |
6937 | OPJ_FLOAT32 temp_rate; |
6938 | OPJ_BOOL cap = OPJ_FALSE; |
6939 | temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * |
6940 | image->comps[0].h * image->comps[0].prec) / |
6941 | (((double)parameters->max_cs_size) * 8 * image->comps[0].dx * |
6942 | image->comps[0].dy)); |
6943 | for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { |
6944 | if (parameters->tcp_rates[i] < temp_rate) { |
6945 | parameters->tcp_rates[i] = temp_rate; |
6946 | cap = OPJ_TRUE; |
6947 | } |
6948 | } |
6949 | if (cap) { |
6950 | opj_event_msg(p_manager, EVT_WARNING, |
6951 | "The desired maximum codestream size has limited\n" |
6952 | "at least one of the desired quality layers\n" ); |
6953 | } |
6954 | } |
6955 | |
6956 | /* Manage profiles and applications and set RSIZ */ |
6957 | /* set cinema parameters if required */ |
6958 | if (OPJ_IS_CINEMA(parameters->rsiz)) { |
6959 | if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) |
6960 | || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) { |
6961 | opj_event_msg(p_manager, EVT_WARNING, |
6962 | "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n" ); |
6963 | parameters->rsiz = OPJ_PROFILE_NONE; |
6964 | } else { |
6965 | opj_j2k_set_cinema_parameters(parameters, image, p_manager); |
6966 | if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) { |
6967 | parameters->rsiz = OPJ_PROFILE_NONE; |
6968 | } |
6969 | } |
6970 | } else if (OPJ_IS_STORAGE(parameters->rsiz)) { |
6971 | opj_event_msg(p_manager, EVT_WARNING, |
6972 | "JPEG 2000 Long Term Storage profile not yet supported\n" ); |
6973 | parameters->rsiz = OPJ_PROFILE_NONE; |
6974 | } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { |
6975 | opj_event_msg(p_manager, EVT_WARNING, |
6976 | "JPEG 2000 Broadcast profiles not yet supported\n" ); |
6977 | parameters->rsiz = OPJ_PROFILE_NONE; |
6978 | } else if (OPJ_IS_IMF(parameters->rsiz)) { |
6979 | opj_event_msg(p_manager, EVT_WARNING, |
6980 | "JPEG 2000 IMF profiles not yet supported\n" ); |
6981 | parameters->rsiz = OPJ_PROFILE_NONE; |
6982 | } else if (OPJ_IS_PART2(parameters->rsiz)) { |
6983 | if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { |
6984 | opj_event_msg(p_manager, EVT_WARNING, |
6985 | "JPEG 2000 Part-2 profile defined\n" |
6986 | "but no Part-2 extension enabled.\n" |
6987 | "Profile set to NONE.\n" ); |
6988 | parameters->rsiz = OPJ_PROFILE_NONE; |
6989 | } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { |
6990 | opj_event_msg(p_manager, EVT_WARNING, |
6991 | "Unsupported Part-2 extension enabled\n" |
6992 | "Profile set to NONE.\n" ); |
6993 | parameters->rsiz = OPJ_PROFILE_NONE; |
6994 | } |
6995 | } |
6996 | |
6997 | /* |
6998 | copy user encoding parameters |
6999 | */ |
7000 | cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32) |
7001 | parameters->max_comp_size; |
7002 | cp->rsiz = parameters->rsiz; |
7003 | cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32) |
7004 | parameters->cp_disto_alloc & 1u; |
7005 | cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32) |
7006 | parameters->cp_fixed_alloc & 1u; |
7007 | cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32) |
7008 | parameters->cp_fixed_quality & 1u; |
7009 | |
7010 | /* mod fixed_quality */ |
7011 | if (parameters->cp_fixed_alloc && parameters->cp_matrice) { |
7012 | size_t array_size = (size_t)parameters->tcp_numlayers * |
7013 | (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); |
7014 | cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); |
7015 | if (!cp->m_specific_param.m_enc.m_matrice) { |
7016 | opj_event_msg(p_manager, EVT_ERROR, |
7017 | "Not enough memory to allocate copy of user encoding parameters matrix \n" ); |
7018 | return OPJ_FALSE; |
7019 | } |
7020 | memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, |
7021 | array_size); |
7022 | } |
7023 | |
7024 | /* tiles */ |
7025 | cp->tdx = (OPJ_UINT32)parameters->cp_tdx; |
7026 | cp->tdy = (OPJ_UINT32)parameters->cp_tdy; |
7027 | |
7028 | /* tile offset */ |
7029 | cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; |
7030 | cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; |
7031 | |
7032 | /* comment string */ |
7033 | if (parameters->cp_comment) { |
7034 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); |
7035 | if (!cp->comment) { |
7036 | opj_event_msg(p_manager, EVT_ERROR, |
7037 | "Not enough memory to allocate copy of comment string\n" ); |
7038 | return OPJ_FALSE; |
7039 | } |
7040 | strcpy(cp->comment, parameters->cp_comment); |
7041 | } else { |
7042 | /* Create default comment for codestream */ |
7043 | const char [] = "Created by OpenJPEG version " ; |
7044 | const size_t clen = strlen(comment); |
7045 | const char *version = opj_version(); |
7046 | |
7047 | /* UniPG>> */ |
7048 | #ifdef USE_JPWL |
7049 | cp->comment = (char*)opj_malloc(clen + strlen(version) + 11); |
7050 | if (!cp->comment) { |
7051 | opj_event_msg(p_manager, EVT_ERROR, |
7052 | "Not enough memory to allocate comment string\n" ); |
7053 | return OPJ_FALSE; |
7054 | } |
7055 | sprintf(cp->comment, "%s%s with JPWL" , comment, version); |
7056 | #else |
7057 | cp->comment = (char*)opj_malloc(clen + strlen(version) + 1); |
7058 | if (!cp->comment) { |
7059 | opj_event_msg(p_manager, EVT_ERROR, |
7060 | "Not enough memory to allocate comment string\n" ); |
7061 | return OPJ_FALSE; |
7062 | } |
7063 | sprintf(cp->comment, "%s%s" , comment, version); |
7064 | #endif |
7065 | /* <<UniPG */ |
7066 | } |
7067 | |
7068 | /* |
7069 | calculate other encoding parameters |
7070 | */ |
7071 | |
7072 | if (parameters->tile_size_on) { |
7073 | cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), |
7074 | (OPJ_INT32)cp->tdx); |
7075 | cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), |
7076 | (OPJ_INT32)cp->tdy); |
7077 | } else { |
7078 | cp->tdx = image->x1 - cp->tx0; |
7079 | cp->tdy = image->y1 - cp->ty0; |
7080 | } |
7081 | |
7082 | if (parameters->tp_on) { |
7083 | cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; |
7084 | cp->m_specific_param.m_enc.m_tp_on = 1; |
7085 | } |
7086 | |
7087 | #ifdef USE_JPWL |
7088 | /* |
7089 | calculate JPWL encoding parameters |
7090 | */ |
7091 | |
7092 | if (parameters->jpwl_epc_on) { |
7093 | OPJ_INT32 i; |
7094 | |
7095 | /* set JPWL on */ |
7096 | cp->epc_on = OPJ_TRUE; |
7097 | cp->info_on = OPJ_FALSE; /* no informative technique */ |
7098 | |
7099 | /* set EPB on */ |
7100 | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { |
7101 | cp->epb_on = OPJ_TRUE; |
7102 | |
7103 | cp->hprot_MH = parameters->jpwl_hprot_MH; |
7104 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
7105 | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; |
7106 | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; |
7107 | } |
7108 | /* if tile specs are not specified, copy MH specs */ |
7109 | if (cp->hprot_TPH[0] == -1) { |
7110 | cp->hprot_TPH_tileno[0] = 0; |
7111 | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; |
7112 | } |
7113 | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { |
7114 | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; |
7115 | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; |
7116 | cp->pprot[i] = parameters->jpwl_pprot[i]; |
7117 | } |
7118 | } |
7119 | |
7120 | /* set ESD writing */ |
7121 | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { |
7122 | cp->esd_on = OPJ_TRUE; |
7123 | |
7124 | cp->sens_size = parameters->jpwl_sens_size; |
7125 | cp->sens_addr = parameters->jpwl_sens_addr; |
7126 | cp->sens_range = parameters->jpwl_sens_range; |
7127 | |
7128 | cp->sens_MH = parameters->jpwl_sens_MH; |
7129 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { |
7130 | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; |
7131 | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; |
7132 | } |
7133 | } |
7134 | |
7135 | /* always set RED writing to false: we are at the encoder */ |
7136 | cp->red_on = OPJ_FALSE; |
7137 | |
7138 | } else { |
7139 | cp->epc_on = OPJ_FALSE; |
7140 | } |
7141 | #endif /* USE_JPWL */ |
7142 | |
7143 | /* initialize the mutiple tiles */ |
7144 | /* ---------------------------- */ |
7145 | cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); |
7146 | if (!cp->tcps) { |
7147 | opj_event_msg(p_manager, EVT_ERROR, |
7148 | "Not enough memory to allocate tile coding parameters\n" ); |
7149 | return OPJ_FALSE; |
7150 | } |
7151 | if (parameters->numpocs) { |
7152 | /* initialisation of POC */ |
7153 | opj_j2k_check_poc_val(parameters->POC, parameters->numpocs, |
7154 | (OPJ_UINT32)parameters->numresolution, image->numcomps, |
7155 | (OPJ_UINT32)parameters->tcp_numlayers, p_manager); |
7156 | /* TODO MSD use the return value*/ |
7157 | } |
7158 | |
7159 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { |
7160 | opj_tcp_t *tcp = &cp->tcps[tileno]; |
7161 | tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; |
7162 | |
7163 | for (j = 0; j < tcp->numlayers; j++) { |
7164 | if (OPJ_IS_CINEMA(cp->rsiz)) { |
7165 | if (cp->m_specific_param.m_enc.m_fixed_quality) { |
7166 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
7167 | } |
7168 | tcp->rates[j] = parameters->tcp_rates[j]; |
7169 | } else { |
7170 | if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ |
7171 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; |
7172 | } else { |
7173 | tcp->rates[j] = parameters->tcp_rates[j]; |
7174 | } |
7175 | } |
7176 | if (!cp->m_specific_param.m_enc.m_fixed_quality && |
7177 | tcp->rates[j] <= 1.0) { |
7178 | tcp->rates[j] = 0.0; /* force lossless */ |
7179 | } |
7180 | } |
7181 | |
7182 | tcp->csty = (OPJ_UINT32)parameters->csty; |
7183 | tcp->prg = parameters->prog_order; |
7184 | tcp->mct = (OPJ_UINT32)parameters->tcp_mct; |
7185 | |
7186 | numpocs_tile = 0; |
7187 | tcp->POC = 0; |
7188 | |
7189 | if (parameters->numpocs) { |
7190 | /* initialisation of POC */ |
7191 | tcp->POC = 1; |
7192 | for (i = 0; i < parameters->numpocs; i++) { |
7193 | if (tileno + 1 == parameters->POC[i].tile) { |
7194 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; |
7195 | |
7196 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; |
7197 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; |
7198 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; |
7199 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; |
7200 | tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; |
7201 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; |
7202 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; |
7203 | |
7204 | numpocs_tile++; |
7205 | } |
7206 | } |
7207 | |
7208 | tcp->numpocs = numpocs_tile - 1 ; |
7209 | } else { |
7210 | tcp->numpocs = 0; |
7211 | } |
7212 | |
7213 | tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); |
7214 | if (!tcp->tccps) { |
7215 | opj_event_msg(p_manager, EVT_ERROR, |
7216 | "Not enough memory to allocate tile component coding parameters\n" ); |
7217 | return OPJ_FALSE; |
7218 | } |
7219 | if (parameters->mct_data) { |
7220 | |
7221 | OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof( |
7222 | OPJ_FLOAT32); |
7223 | OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
7224 | OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data + |
7225 | lMctSize); |
7226 | |
7227 | if (!lTmpBuf) { |
7228 | opj_event_msg(p_manager, EVT_ERROR, |
7229 | "Not enough memory to allocate temp buffer\n" ); |
7230 | return OPJ_FALSE; |
7231 | } |
7232 | |
7233 | tcp->mct = 2; |
7234 | tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
7235 | if (! tcp->m_mct_coding_matrix) { |
7236 | opj_free(lTmpBuf); |
7237 | lTmpBuf = NULL; |
7238 | opj_event_msg(p_manager, EVT_ERROR, |
7239 | "Not enough memory to allocate encoder MCT coding matrix \n" ); |
7240 | return OPJ_FALSE; |
7241 | } |
7242 | memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize); |
7243 | memcpy(lTmpBuf, parameters->mct_data, lMctSize); |
7244 | |
7245 | tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); |
7246 | if (! tcp->m_mct_decoding_matrix) { |
7247 | opj_free(lTmpBuf); |
7248 | lTmpBuf = NULL; |
7249 | opj_event_msg(p_manager, EVT_ERROR, |
7250 | "Not enough memory to allocate encoder MCT decoding matrix \n" ); |
7251 | return OPJ_FALSE; |
7252 | } |
7253 | if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix), |
7254 | image->numcomps) == OPJ_FALSE) { |
7255 | opj_free(lTmpBuf); |
7256 | lTmpBuf = NULL; |
7257 | opj_event_msg(p_manager, EVT_ERROR, |
7258 | "Failed to inverse encoder MCT decoding matrix \n" ); |
7259 | return OPJ_FALSE; |
7260 | } |
7261 | |
7262 | tcp->mct_norms = (OPJ_FLOAT64*) |
7263 | opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); |
7264 | if (! tcp->mct_norms) { |
7265 | opj_free(lTmpBuf); |
7266 | lTmpBuf = NULL; |
7267 | opj_event_msg(p_manager, EVT_ERROR, |
7268 | "Not enough memory to allocate encoder MCT norms \n" ); |
7269 | return OPJ_FALSE; |
7270 | } |
7271 | opj_calculate_norms(tcp->mct_norms, image->numcomps, |
7272 | tcp->m_mct_decoding_matrix); |
7273 | opj_free(lTmpBuf); |
7274 | |
7275 | for (i = 0; i < image->numcomps; i++) { |
7276 | opj_tccp_t *tccp = &tcp->tccps[i]; |
7277 | tccp->m_dc_level_shift = l_dc_shift[i]; |
7278 | } |
7279 | |
7280 | if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) { |
7281 | /* free will be handled by opj_j2k_destroy */ |
7282 | opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n" ); |
7283 | return OPJ_FALSE; |
7284 | } |
7285 | } else { |
7286 | if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ |
7287 | if ((image->comps[0].dx != image->comps[1].dx) || |
7288 | (image->comps[0].dx != image->comps[2].dx) || |
7289 | (image->comps[0].dy != image->comps[1].dy) || |
7290 | (image->comps[0].dy != image->comps[2].dy)) { |
7291 | opj_event_msg(p_manager, EVT_WARNING, |
7292 | "Cannot perform MCT on components with different sizes. Disabling MCT.\n" ); |
7293 | tcp->mct = 0; |
7294 | } |
7295 | } |
7296 | for (i = 0; i < image->numcomps; i++) { |
7297 | opj_tccp_t *tccp = &tcp->tccps[i]; |
7298 | opj_image_comp_t * l_comp = &(image->comps[i]); |
7299 | |
7300 | if (! l_comp->sgnd) { |
7301 | tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); |
7302 | } |
7303 | } |
7304 | } |
7305 | |
7306 | for (i = 0; i < image->numcomps; i++) { |
7307 | opj_tccp_t *tccp = &tcp->tccps[i]; |
7308 | |
7309 | tccp->csty = parameters->csty & |
7310 | 0x01; /* 0 => one precinct || 1 => custom precinct */ |
7311 | tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; |
7312 | tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); |
7313 | tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); |
7314 | tccp->cblksty = (OPJ_UINT32)parameters->mode; |
7315 | tccp->qmfbid = parameters->irreversible ? 0 : 1; |
7316 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : |
7317 | J2K_CCP_QNTSTY_NOQNT; |
7318 | tccp->numgbits = 2; |
7319 | |
7320 | if ((OPJ_INT32)i == parameters->roi_compno) { |
7321 | tccp->roishift = parameters->roi_shift; |
7322 | } else { |
7323 | tccp->roishift = 0; |
7324 | } |
7325 | |
7326 | if (parameters->csty & J2K_CCP_CSTY_PRT) { |
7327 | OPJ_INT32 p = 0, it_res; |
7328 | assert(tccp->numresolutions > 0); |
7329 | for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { |
7330 | if (p < parameters->res_spec) { |
7331 | |
7332 | if (parameters->prcw_init[p] < 1) { |
7333 | tccp->prcw[it_res] = 1; |
7334 | } else { |
7335 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); |
7336 | } |
7337 | |
7338 | if (parameters->prch_init[p] < 1) { |
7339 | tccp->prch[it_res] = 1; |
7340 | } else { |
7341 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); |
7342 | } |
7343 | |
7344 | } else { |
7345 | OPJ_INT32 res_spec = parameters->res_spec; |
7346 | OPJ_INT32 size_prcw = 0; |
7347 | OPJ_INT32 size_prch = 0; |
7348 | |
7349 | assert(res_spec > 0); /* issue 189 */ |
7350 | size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); |
7351 | size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); |
7352 | |
7353 | |
7354 | if (size_prcw < 1) { |
7355 | tccp->prcw[it_res] = 1; |
7356 | } else { |
7357 | tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); |
7358 | } |
7359 | |
7360 | if (size_prch < 1) { |
7361 | tccp->prch[it_res] = 1; |
7362 | } else { |
7363 | tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); |
7364 | } |
7365 | } |
7366 | p++; |
7367 | /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ |
7368 | } /*end for*/ |
7369 | } else { |
7370 | for (j = 0; j < tccp->numresolutions; j++) { |
7371 | tccp->prcw[j] = 15; |
7372 | tccp->prch[j] = 15; |
7373 | } |
7374 | } |
7375 | |
7376 | opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); |
7377 | } |
7378 | } |
7379 | |
7380 | if (parameters->mct_data) { |
7381 | opj_free(parameters->mct_data); |
7382 | parameters->mct_data = 00; |
7383 | } |
7384 | return OPJ_TRUE; |
7385 | } |
7386 | |
7387 | static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, |
7388 | OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) |
7389 | { |
7390 | assert(cstr_index != 00); |
7391 | |
7392 | /* expand the list? */ |
7393 | if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { |
7394 | opj_marker_info_t *new_marker; |
7395 | cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) |
7396 | cstr_index->maxmarknum); |
7397 | new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, |
7398 | cstr_index->maxmarknum * sizeof(opj_marker_info_t)); |
7399 | if (! new_marker) { |
7400 | opj_free(cstr_index->marker); |
7401 | cstr_index->marker = NULL; |
7402 | cstr_index->maxmarknum = 0; |
7403 | cstr_index->marknum = 0; |
7404 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ |
7405 | return OPJ_FALSE; |
7406 | } |
7407 | cstr_index->marker = new_marker; |
7408 | } |
7409 | |
7410 | /* add the marker */ |
7411 | cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; |
7412 | cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; |
7413 | cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; |
7414 | cstr_index->marknum++; |
7415 | return OPJ_TRUE; |
7416 | } |
7417 | |
7418 | static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, |
7419 | opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, |
7420 | OPJ_UINT32 len) |
7421 | { |
7422 | assert(cstr_index != 00); |
7423 | assert(cstr_index->tile_index != 00); |
7424 | |
7425 | /* expand the list? */ |
7426 | if ((cstr_index->tile_index[tileno].marknum + 1) > |
7427 | cstr_index->tile_index[tileno].maxmarknum) { |
7428 | opj_marker_info_t *new_marker; |
7429 | cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + |
7430 | (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); |
7431 | new_marker = (opj_marker_info_t *) opj_realloc( |
7432 | cstr_index->tile_index[tileno].marker, |
7433 | cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t)); |
7434 | if (! new_marker) { |
7435 | opj_free(cstr_index->tile_index[tileno].marker); |
7436 | cstr_index->tile_index[tileno].marker = NULL; |
7437 | cstr_index->tile_index[tileno].maxmarknum = 0; |
7438 | cstr_index->tile_index[tileno].marknum = 0; |
7439 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ |
7440 | return OPJ_FALSE; |
7441 | } |
7442 | cstr_index->tile_index[tileno].marker = new_marker; |
7443 | } |
7444 | |
7445 | /* add the marker */ |
7446 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type |
7447 | = (OPJ_UINT16)type; |
7448 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos |
7449 | = (OPJ_INT32)pos; |
7450 | cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len |
7451 | = (OPJ_INT32)len; |
7452 | cstr_index->tile_index[tileno].marknum++; |
7453 | |
7454 | if (type == J2K_MS_SOT) { |
7455 | OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; |
7456 | |
7457 | if (cstr_index->tile_index[tileno].tp_index) { |
7458 | cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; |
7459 | } |
7460 | |
7461 | } |
7462 | return OPJ_TRUE; |
7463 | } |
7464 | |
7465 | /* |
7466 | * ----------------------------------------------------------------------- |
7467 | * ----------------------------------------------------------------------- |
7468 | * ----------------------------------------------------------------------- |
7469 | */ |
7470 | |
7471 | OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, |
7472 | opj_stream_private_t *p_stream, |
7473 | opj_event_mgr_t * p_manager |
7474 | ) |
7475 | { |
7476 | (void)p_j2k; |
7477 | (void)p_stream; |
7478 | (void)p_manager; |
7479 | return OPJ_TRUE; |
7480 | } |
7481 | |
7482 | OPJ_BOOL (opj_stream_private_t *p_stream, |
7483 | opj_j2k_t* p_j2k, |
7484 | opj_image_t** p_image, |
7485 | opj_event_mgr_t* p_manager) |
7486 | { |
7487 | /* preconditions */ |
7488 | assert(p_j2k != 00); |
7489 | assert(p_stream != 00); |
7490 | assert(p_manager != 00); |
7491 | |
7492 | /* create an empty image header */ |
7493 | p_j2k->m_private_image = opj_image_create0(); |
7494 | if (! p_j2k->m_private_image) { |
7495 | return OPJ_FALSE; |
7496 | } |
7497 | |
7498 | /* customization of the validation */ |
7499 | if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { |
7500 | opj_image_destroy(p_j2k->m_private_image); |
7501 | p_j2k->m_private_image = NULL; |
7502 | return OPJ_FALSE; |
7503 | } |
7504 | |
7505 | /* validation of the parameters codec */ |
7506 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
7507 | opj_image_destroy(p_j2k->m_private_image); |
7508 | p_j2k->m_private_image = NULL; |
7509 | return OPJ_FALSE; |
7510 | } |
7511 | |
7512 | /* customization of the encoding */ |
7513 | if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { |
7514 | opj_image_destroy(p_j2k->m_private_image); |
7515 | p_j2k->m_private_image = NULL; |
7516 | return OPJ_FALSE; |
7517 | } |
7518 | |
7519 | /* read header */ |
7520 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
7521 | opj_image_destroy(p_j2k->m_private_image); |
7522 | p_j2k->m_private_image = NULL; |
7523 | return OPJ_FALSE; |
7524 | } |
7525 | |
7526 | *p_image = opj_image_create0(); |
7527 | if (!(*p_image)) { |
7528 | return OPJ_FALSE; |
7529 | } |
7530 | |
7531 | /* Copy codestream image information to the output image */ |
7532 | opj_copy_image_header(p_j2k->m_private_image, *p_image); |
7533 | |
7534 | /*Allocate and initialize some elements of codestrem index*/ |
7535 | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
7536 | return OPJ_FALSE; |
7537 | } |
7538 | |
7539 | return OPJ_TRUE; |
7540 | } |
7541 | |
7542 | static OPJ_BOOL (opj_j2k_t *p_j2k, |
7543 | opj_event_mgr_t * p_manager) |
7544 | { |
7545 | /* preconditions*/ |
7546 | assert(p_j2k != 00); |
7547 | assert(p_manager != 00); |
7548 | |
7549 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
7550 | (opj_procedure)opj_j2k_read_header_procedure, p_manager)) { |
7551 | return OPJ_FALSE; |
7552 | } |
7553 | |
7554 | /* DEVELOPER CORNER, add your custom procedures */ |
7555 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
7556 | (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) { |
7557 | return OPJ_FALSE; |
7558 | } |
7559 | |
7560 | return OPJ_TRUE; |
7561 | } |
7562 | |
7563 | static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, |
7564 | opj_event_mgr_t * p_manager) |
7565 | { |
7566 | /* preconditions*/ |
7567 | assert(p_j2k != 00); |
7568 | assert(p_manager != 00); |
7569 | |
7570 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
7571 | (opj_procedure)opj_j2k_build_decoder, p_manager)) { |
7572 | return OPJ_FALSE; |
7573 | } |
7574 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
7575 | (opj_procedure)opj_j2k_decoding_validation, p_manager)) { |
7576 | return OPJ_FALSE; |
7577 | } |
7578 | |
7579 | /* DEVELOPER CORNER, add your custom validation procedure */ |
7580 | return OPJ_TRUE; |
7581 | } |
7582 | |
7583 | static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, |
7584 | opj_stream_private_t *p_stream, |
7585 | opj_event_mgr_t * p_manager) |
7586 | { |
7587 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
7588 | OPJ_UINT32 i, j; |
7589 | |
7590 | /* preconditions */ |
7591 | assert(p_j2k != 00); |
7592 | assert(p_stream != 00); |
7593 | assert(p_manager != 00); |
7594 | |
7595 | OPJ_UNUSED(p_stream); |
7596 | OPJ_UNUSED(p_manager); |
7597 | |
7598 | if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { |
7599 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
7600 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
7601 | |
7602 | for (i = 0; i < l_nb_tiles; ++i) { |
7603 | if (l_tcp->mct == 2) { |
7604 | opj_tccp_t * l_tccp = l_tcp->tccps; |
7605 | l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); |
7606 | |
7607 | for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) { |
7608 | l_is_valid &= !(l_tccp->qmfbid & 1); |
7609 | ++l_tccp; |
7610 | } |
7611 | } |
7612 | ++l_tcp; |
7613 | } |
7614 | } |
7615 | |
7616 | return l_is_valid; |
7617 | } |
7618 | |
7619 | OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) |
7620 | { |
7621 | OPJ_UINT32 i; |
7622 | OPJ_UINT32 l_indix = 1; |
7623 | opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00; |
7624 | opj_simple_mcc_decorrelation_data_t * l_mcc_data; |
7625 | OPJ_UINT32 l_mct_size, l_nb_elem; |
7626 | OPJ_FLOAT32 * l_data, * l_current_data; |
7627 | opj_tccp_t * l_tccp; |
7628 | |
7629 | /* preconditions */ |
7630 | assert(p_tcp != 00); |
7631 | |
7632 | if (p_tcp->mct != 2) { |
7633 | return OPJ_TRUE; |
7634 | } |
7635 | |
7636 | if (p_tcp->m_mct_decoding_matrix) { |
7637 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
7638 | opj_mct_data_t *new_mct_records; |
7639 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
7640 | |
7641 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
7642 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
7643 | if (! new_mct_records) { |
7644 | opj_free(p_tcp->m_mct_records); |
7645 | p_tcp->m_mct_records = NULL; |
7646 | p_tcp->m_nb_max_mct_records = 0; |
7647 | p_tcp->m_nb_mct_records = 0; |
7648 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
7649 | return OPJ_FALSE; |
7650 | } |
7651 | p_tcp->m_mct_records = new_mct_records; |
7652 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
7653 | |
7654 | memset(l_mct_deco_data, 0, |
7655 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
7656 | opj_mct_data_t)); |
7657 | } |
7658 | l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
7659 | |
7660 | if (l_mct_deco_data->m_data) { |
7661 | opj_free(l_mct_deco_data->m_data); |
7662 | l_mct_deco_data->m_data = 00; |
7663 | } |
7664 | |
7665 | l_mct_deco_data->m_index = l_indix++; |
7666 | l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; |
7667 | l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; |
7668 | l_nb_elem = p_image->numcomps * p_image->numcomps; |
7669 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; |
7670 | l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
7671 | |
7672 | if (! l_mct_deco_data->m_data) { |
7673 | return OPJ_FALSE; |
7674 | } |
7675 | |
7676 | j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type]( |
7677 | p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem); |
7678 | |
7679 | l_mct_deco_data->m_data_size = l_mct_size; |
7680 | ++p_tcp->m_nb_mct_records; |
7681 | } |
7682 | |
7683 | if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { |
7684 | opj_mct_data_t *new_mct_records; |
7685 | p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
7686 | new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, |
7687 | p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); |
7688 | if (! new_mct_records) { |
7689 | opj_free(p_tcp->m_mct_records); |
7690 | p_tcp->m_mct_records = NULL; |
7691 | p_tcp->m_nb_max_mct_records = 0; |
7692 | p_tcp->m_nb_mct_records = 0; |
7693 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
7694 | return OPJ_FALSE; |
7695 | } |
7696 | p_tcp->m_mct_records = new_mct_records; |
7697 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
7698 | |
7699 | memset(l_mct_offset_data, 0, |
7700 | (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( |
7701 | opj_mct_data_t)); |
7702 | |
7703 | if (l_mct_deco_data) { |
7704 | l_mct_deco_data = l_mct_offset_data - 1; |
7705 | } |
7706 | } |
7707 | |
7708 | l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; |
7709 | |
7710 | if (l_mct_offset_data->m_data) { |
7711 | opj_free(l_mct_offset_data->m_data); |
7712 | l_mct_offset_data->m_data = 00; |
7713 | } |
7714 | |
7715 | l_mct_offset_data->m_index = l_indix++; |
7716 | l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; |
7717 | l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; |
7718 | l_nb_elem = p_image->numcomps; |
7719 | l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; |
7720 | l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); |
7721 | |
7722 | if (! l_mct_offset_data->m_data) { |
7723 | return OPJ_FALSE; |
7724 | } |
7725 | |
7726 | l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); |
7727 | if (! l_data) { |
7728 | opj_free(l_mct_offset_data->m_data); |
7729 | l_mct_offset_data->m_data = 00; |
7730 | return OPJ_FALSE; |
7731 | } |
7732 | |
7733 | l_tccp = p_tcp->tccps; |
7734 | l_current_data = l_data; |
7735 | |
7736 | for (i = 0; i < l_nb_elem; ++i) { |
7737 | *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift); |
7738 | ++l_tccp; |
7739 | } |
7740 | |
7741 | j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data, |
7742 | l_mct_offset_data->m_data, l_nb_elem); |
7743 | |
7744 | opj_free(l_data); |
7745 | |
7746 | l_mct_offset_data->m_data_size = l_mct_size; |
7747 | |
7748 | ++p_tcp->m_nb_mct_records; |
7749 | |
7750 | if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { |
7751 | opj_simple_mcc_decorrelation_data_t *new_mcc_records; |
7752 | p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; |
7753 | new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( |
7754 | p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof( |
7755 | opj_simple_mcc_decorrelation_data_t)); |
7756 | if (! new_mcc_records) { |
7757 | opj_free(p_tcp->m_mcc_records); |
7758 | p_tcp->m_mcc_records = NULL; |
7759 | p_tcp->m_nb_max_mcc_records = 0; |
7760 | p_tcp->m_nb_mcc_records = 0; |
7761 | /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ |
7762 | return OPJ_FALSE; |
7763 | } |
7764 | p_tcp->m_mcc_records = new_mcc_records; |
7765 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
7766 | memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * |
7767 | sizeof(opj_simple_mcc_decorrelation_data_t)); |
7768 | |
7769 | } |
7770 | |
7771 | l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; |
7772 | l_mcc_data->m_decorrelation_array = l_mct_deco_data; |
7773 | l_mcc_data->m_is_irreversible = 1; |
7774 | l_mcc_data->m_nb_comps = p_image->numcomps; |
7775 | l_mcc_data->m_index = l_indix++; |
7776 | l_mcc_data->m_offset_array = l_mct_offset_data; |
7777 | ++p_tcp->m_nb_mcc_records; |
7778 | |
7779 | return OPJ_TRUE; |
7780 | } |
7781 | |
7782 | static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, |
7783 | opj_stream_private_t *p_stream, |
7784 | opj_event_mgr_t * p_manager) |
7785 | { |
7786 | /* add here initialization of cp |
7787 | copy paste of setup_decoder */ |
7788 | (void)p_j2k; |
7789 | (void)p_stream; |
7790 | (void)p_manager; |
7791 | return OPJ_TRUE; |
7792 | } |
7793 | |
7794 | static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, |
7795 | opj_stream_private_t *p_stream, |
7796 | opj_event_mgr_t * p_manager) |
7797 | { |
7798 | /* add here initialization of cp |
7799 | copy paste of setup_encoder */ |
7800 | (void)p_j2k; |
7801 | (void)p_stream; |
7802 | (void)p_manager; |
7803 | return OPJ_TRUE; |
7804 | } |
7805 | |
7806 | static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, |
7807 | opj_stream_private_t *p_stream, |
7808 | opj_event_mgr_t * p_manager) |
7809 | { |
7810 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
7811 | |
7812 | /* preconditions */ |
7813 | assert(p_j2k != 00); |
7814 | assert(p_stream != 00); |
7815 | assert(p_manager != 00); |
7816 | |
7817 | OPJ_UNUSED(p_stream); |
7818 | |
7819 | /* STATE checking */ |
7820 | /* make sure the state is at 0 */ |
7821 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); |
7822 | |
7823 | /* POINTER validation */ |
7824 | /* make sure a p_j2k codec is present */ |
7825 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
7826 | /* make sure a validation list is present */ |
7827 | l_is_valid &= (p_j2k->m_validation_list != 00); |
7828 | |
7829 | /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ |
7830 | /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ |
7831 | /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ |
7832 | if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || |
7833 | (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { |
7834 | opj_event_msg(p_manager, EVT_ERROR, |
7835 | "Number of resolutions is too high in comparison to the size of tiles\n" ); |
7836 | return OPJ_FALSE; |
7837 | } |
7838 | |
7839 | if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 << |
7840 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
7841 | opj_event_msg(p_manager, EVT_ERROR, |
7842 | "Number of resolutions is too high in comparison to the size of tiles\n" ); |
7843 | return OPJ_FALSE; |
7844 | } |
7845 | |
7846 | if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 << |
7847 | (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { |
7848 | opj_event_msg(p_manager, EVT_ERROR, |
7849 | "Number of resolutions is too high in comparison to the size of tiles\n" ); |
7850 | return OPJ_FALSE; |
7851 | } |
7852 | |
7853 | /* PARAMETER VALIDATION */ |
7854 | return l_is_valid; |
7855 | } |
7856 | |
7857 | static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k, |
7858 | opj_stream_private_t *p_stream, |
7859 | opj_event_mgr_t * p_manager |
7860 | ) |
7861 | { |
7862 | OPJ_BOOL l_is_valid = OPJ_TRUE; |
7863 | |
7864 | /* preconditions*/ |
7865 | assert(p_j2k != 00); |
7866 | assert(p_stream != 00); |
7867 | assert(p_manager != 00); |
7868 | |
7869 | OPJ_UNUSED(p_stream); |
7870 | OPJ_UNUSED(p_manager); |
7871 | |
7872 | /* STATE checking */ |
7873 | /* make sure the state is at 0 */ |
7874 | #ifdef TODO_MSD |
7875 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); |
7876 | #endif |
7877 | l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); |
7878 | |
7879 | /* POINTER validation */ |
7880 | /* make sure a p_j2k codec is present */ |
7881 | /* make sure a procedure list is present */ |
7882 | l_is_valid &= (p_j2k->m_procedure_list != 00); |
7883 | /* make sure a validation list is present */ |
7884 | l_is_valid &= (p_j2k->m_validation_list != 00); |
7885 | |
7886 | /* PARAMETER VALIDATION */ |
7887 | return l_is_valid; |
7888 | } |
7889 | |
7890 | static OPJ_BOOL (opj_j2k_t *p_j2k, |
7891 | opj_stream_private_t *p_stream, |
7892 | opj_event_mgr_t * p_manager) |
7893 | { |
7894 | OPJ_UINT32 l_current_marker; |
7895 | OPJ_UINT32 l_marker_size; |
7896 | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
7897 | OPJ_BOOL l_has_siz = 0; |
7898 | OPJ_BOOL l_has_cod = 0; |
7899 | OPJ_BOOL l_has_qcd = 0; |
7900 | |
7901 | /* preconditions */ |
7902 | assert(p_stream != 00); |
7903 | assert(p_j2k != 00); |
7904 | assert(p_manager != 00); |
7905 | |
7906 | /* We enter in the main header */ |
7907 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; |
7908 | |
7909 | /* Try to read the SOC marker, the codestream must begin with SOC marker */ |
7910 | if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) { |
7911 | opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n" ); |
7912 | return OPJ_FALSE; |
7913 | } |
7914 | |
7915 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
7916 | if (opj_stream_read_data(p_stream, |
7917 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
7918 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
7919 | return OPJ_FALSE; |
7920 | } |
7921 | |
7922 | /* Read 2 bytes as the new marker ID */ |
7923 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
7924 | &l_current_marker, 2); |
7925 | |
7926 | /* Try to read until the SOT is detected */ |
7927 | while (l_current_marker != J2K_MS_SOT) { |
7928 | |
7929 | /* Check if the current marker ID is valid */ |
7930 | if (l_current_marker < 0xff00) { |
7931 | opj_event_msg(p_manager, EVT_ERROR, |
7932 | "A marker ID was expected (0xff--) instead of %.8x\n" , l_current_marker); |
7933 | return OPJ_FALSE; |
7934 | } |
7935 | |
7936 | /* Get the marker handler from the marker ID */ |
7937 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
7938 | |
7939 | /* Manage case where marker is unknown */ |
7940 | if (l_marker_handler->id == J2K_MS_UNK) { |
7941 | if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) { |
7942 | opj_event_msg(p_manager, EVT_ERROR, |
7943 | "Unknow marker have been detected and generated error.\n" ); |
7944 | return OPJ_FALSE; |
7945 | } |
7946 | |
7947 | if (l_current_marker == J2K_MS_SOT) { |
7948 | break; /* SOT marker is detected main header is completely read */ |
7949 | } else { /* Get the marker handler from the marker ID */ |
7950 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
7951 | } |
7952 | } |
7953 | |
7954 | if (l_marker_handler->id == J2K_MS_SIZ) { |
7955 | /* Mark required SIZ marker as found */ |
7956 | l_has_siz = 1; |
7957 | } |
7958 | if (l_marker_handler->id == J2K_MS_COD) { |
7959 | /* Mark required COD marker as found */ |
7960 | l_has_cod = 1; |
7961 | } |
7962 | if (l_marker_handler->id == J2K_MS_QCD) { |
7963 | /* Mark required QCD marker as found */ |
7964 | l_has_qcd = 1; |
7965 | } |
7966 | |
7967 | /* Check if the marker is known and if it is the right place to find it */ |
7968 | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
7969 | opj_event_msg(p_manager, EVT_ERROR, |
7970 | "Marker is not compliant with its position\n" ); |
7971 | return OPJ_FALSE; |
7972 | } |
7973 | |
7974 | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
7975 | if (opj_stream_read_data(p_stream, |
7976 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
7977 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
7978 | return OPJ_FALSE; |
7979 | } |
7980 | |
7981 | /* read 2 bytes as the marker size */ |
7982 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
7983 | 2); |
7984 | if (l_marker_size < 2) { |
7985 | opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n" ); |
7986 | return OPJ_FALSE; |
7987 | } |
7988 | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
7989 | |
7990 | /* Check if the marker size is compatible with the header data size */ |
7991 | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
7992 | OPJ_BYTE * = (OPJ_BYTE *) opj_realloc( |
7993 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
7994 | if (! new_header_data) { |
7995 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
7996 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
7997 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
7998 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n" ); |
7999 | return OPJ_FALSE; |
8000 | } |
8001 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
8002 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
8003 | } |
8004 | |
8005 | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
8006 | if (opj_stream_read_data(p_stream, |
8007 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
8008 | p_manager) != l_marker_size) { |
8009 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8010 | return OPJ_FALSE; |
8011 | } |
8012 | |
8013 | /* Read the marker segment with the correct marker handler */ |
8014 | if (!(*(l_marker_handler->handler))(p_j2k, |
8015 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
8016 | opj_event_msg(p_manager, EVT_ERROR, |
8017 | "Marker handler function failed to read the marker segment\n" ); |
8018 | return OPJ_FALSE; |
8019 | } |
8020 | |
8021 | /* Add the marker to the codestream index*/ |
8022 | if (OPJ_FALSE == opj_j2k_add_mhmarker( |
8023 | p_j2k->cstr_index, |
8024 | l_marker_handler->id, |
8025 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
8026 | l_marker_size + 4)) { |
8027 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n" ); |
8028 | return OPJ_FALSE; |
8029 | } |
8030 | |
8031 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8032 | if (opj_stream_read_data(p_stream, |
8033 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8034 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8035 | return OPJ_FALSE; |
8036 | } |
8037 | |
8038 | /* read 2 bytes as the new marker ID */ |
8039 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8040 | &l_current_marker, 2); |
8041 | } |
8042 | |
8043 | if (l_has_siz == 0) { |
8044 | opj_event_msg(p_manager, EVT_ERROR, |
8045 | "required SIZ marker not found in main header\n" ); |
8046 | return OPJ_FALSE; |
8047 | } |
8048 | if (l_has_cod == 0) { |
8049 | opj_event_msg(p_manager, EVT_ERROR, |
8050 | "required COD marker not found in main header\n" ); |
8051 | return OPJ_FALSE; |
8052 | } |
8053 | if (l_has_qcd == 0) { |
8054 | opj_event_msg(p_manager, EVT_ERROR, |
8055 | "required QCD marker not found in main header\n" ); |
8056 | return OPJ_FALSE; |
8057 | } |
8058 | |
8059 | if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { |
8060 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n" ); |
8061 | return OPJ_FALSE; |
8062 | } |
8063 | |
8064 | opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n" ); |
8065 | |
8066 | /* Position of the last element if the main header */ |
8067 | p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; |
8068 | |
8069 | /* Next step: read a tile-part header */ |
8070 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
8071 | |
8072 | return OPJ_TRUE; |
8073 | } |
8074 | |
8075 | static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, |
8076 | opj_procedure_list_t * p_procedure_list, |
8077 | opj_stream_private_t *p_stream, |
8078 | opj_event_mgr_t * p_manager) |
8079 | { |
8080 | OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *, |
8081 | opj_event_mgr_t *) = 00; |
8082 | OPJ_BOOL l_result = OPJ_TRUE; |
8083 | OPJ_UINT32 l_nb_proc, i; |
8084 | |
8085 | /* preconditions*/ |
8086 | assert(p_procedure_list != 00); |
8087 | assert(p_j2k != 00); |
8088 | assert(p_stream != 00); |
8089 | assert(p_manager != 00); |
8090 | |
8091 | l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); |
8092 | l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *, |
8093 | opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); |
8094 | |
8095 | for (i = 0; i < l_nb_proc; ++i) { |
8096 | l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager)); |
8097 | ++l_procedure; |
8098 | } |
8099 | |
8100 | /* and clear the procedure list at the end.*/ |
8101 | opj_procedure_list_clear(p_procedure_list); |
8102 | return l_result; |
8103 | } |
8104 | |
8105 | /* FIXME DOC*/ |
8106 | static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, |
8107 | opj_stream_private_t *p_stream, |
8108 | opj_event_mgr_t * p_manager |
8109 | ) |
8110 | { |
8111 | opj_tcp_t * l_tcp = 00; |
8112 | opj_tcp_t * l_default_tcp = 00; |
8113 | OPJ_UINT32 l_nb_tiles; |
8114 | OPJ_UINT32 i, j; |
8115 | opj_tccp_t *l_current_tccp = 00; |
8116 | OPJ_UINT32 l_tccp_size; |
8117 | OPJ_UINT32 l_mct_size; |
8118 | opj_image_t * l_image; |
8119 | OPJ_UINT32 l_mcc_records_size, l_mct_records_size; |
8120 | opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; |
8121 | opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; |
8122 | OPJ_UINT32 l_offset; |
8123 | |
8124 | /* preconditions */ |
8125 | assert(p_j2k != 00); |
8126 | assert(p_stream != 00); |
8127 | assert(p_manager != 00); |
8128 | |
8129 | OPJ_UNUSED(p_stream); |
8130 | |
8131 | l_image = p_j2k->m_private_image; |
8132 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
8133 | l_tcp = p_j2k->m_cp.tcps; |
8134 | l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); |
8135 | l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
8136 | l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof( |
8137 | OPJ_FLOAT32); |
8138 | |
8139 | /* For each tile */ |
8140 | for (i = 0; i < l_nb_tiles; ++i) { |
8141 | /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ |
8142 | l_current_tccp = l_tcp->tccps; |
8143 | /*Copy default coding parameters into the current tile coding parameters*/ |
8144 | memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); |
8145 | /* Initialize some values of the current tile coding parameters*/ |
8146 | l_tcp->cod = 0; |
8147 | l_tcp->ppt = 0; |
8148 | l_tcp->ppt_data = 00; |
8149 | l_tcp->m_current_tile_part_number = -1; |
8150 | /* Remove memory not owned by this tile in case of early error return. */ |
8151 | l_tcp->m_mct_decoding_matrix = 00; |
8152 | l_tcp->m_nb_max_mct_records = 0; |
8153 | l_tcp->m_mct_records = 00; |
8154 | l_tcp->m_nb_max_mcc_records = 0; |
8155 | l_tcp->m_mcc_records = 00; |
8156 | /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ |
8157 | l_tcp->tccps = l_current_tccp; |
8158 | |
8159 | /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ |
8160 | if (l_default_tcp->m_mct_decoding_matrix) { |
8161 | l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); |
8162 | if (! l_tcp->m_mct_decoding_matrix) { |
8163 | return OPJ_FALSE; |
8164 | } |
8165 | memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix, |
8166 | l_mct_size); |
8167 | } |
8168 | |
8169 | /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ |
8170 | l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof( |
8171 | opj_mct_data_t); |
8172 | l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); |
8173 | if (! l_tcp->m_mct_records) { |
8174 | return OPJ_FALSE; |
8175 | } |
8176 | memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size); |
8177 | |
8178 | /* Copy the mct record data from dflt_tile_cp to the current tile*/ |
8179 | l_src_mct_rec = l_default_tcp->m_mct_records; |
8180 | l_dest_mct_rec = l_tcp->m_mct_records; |
8181 | |
8182 | for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) { |
8183 | |
8184 | if (l_src_mct_rec->m_data) { |
8185 | |
8186 | l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); |
8187 | if (! l_dest_mct_rec->m_data) { |
8188 | return OPJ_FALSE; |
8189 | } |
8190 | memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data, |
8191 | l_src_mct_rec->m_data_size); |
8192 | } |
8193 | |
8194 | ++l_src_mct_rec; |
8195 | ++l_dest_mct_rec; |
8196 | /* Update with each pass to free exactly what has been allocated on early return. */ |
8197 | l_tcp->m_nb_max_mct_records += 1; |
8198 | } |
8199 | |
8200 | /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ |
8201 | l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof( |
8202 | opj_simple_mcc_decorrelation_data_t); |
8203 | l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc( |
8204 | l_mcc_records_size); |
8205 | if (! l_tcp->m_mcc_records) { |
8206 | return OPJ_FALSE; |
8207 | } |
8208 | memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size); |
8209 | l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; |
8210 | |
8211 | /* Copy the mcc record data from dflt_tile_cp to the current tile*/ |
8212 | l_src_mcc_rec = l_default_tcp->m_mcc_records; |
8213 | l_dest_mcc_rec = l_tcp->m_mcc_records; |
8214 | |
8215 | for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) { |
8216 | |
8217 | if (l_src_mcc_rec->m_decorrelation_array) { |
8218 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - |
8219 | l_default_tcp->m_mct_records); |
8220 | l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; |
8221 | } |
8222 | |
8223 | if (l_src_mcc_rec->m_offset_array) { |
8224 | l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - |
8225 | l_default_tcp->m_mct_records); |
8226 | l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; |
8227 | } |
8228 | |
8229 | ++l_src_mcc_rec; |
8230 | ++l_dest_mcc_rec; |
8231 | } |
8232 | |
8233 | /* Copy all the dflt_tile_compo_cp to the current tile cp */ |
8234 | memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size); |
8235 | |
8236 | /* Move to next tile cp*/ |
8237 | ++l_tcp; |
8238 | } |
8239 | |
8240 | /* Create the current tile decoder*/ |
8241 | p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE); |
8242 | if (! p_j2k->m_tcd) { |
8243 | return OPJ_FALSE; |
8244 | } |
8245 | |
8246 | if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) { |
8247 | opj_tcd_destroy(p_j2k->m_tcd); |
8248 | p_j2k->m_tcd = 00; |
8249 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n" ); |
8250 | return OPJ_FALSE; |
8251 | } |
8252 | |
8253 | return OPJ_TRUE; |
8254 | } |
8255 | |
8256 | static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler( |
8257 | OPJ_UINT32 p_id) |
8258 | { |
8259 | const opj_dec_memory_marker_handler_t *e; |
8260 | for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { |
8261 | if (e->id == p_id) { |
8262 | break; /* we find a handler corresponding to the marker ID*/ |
8263 | } |
8264 | } |
8265 | return e; |
8266 | } |
8267 | |
8268 | void opj_j2k_destroy(opj_j2k_t *p_j2k) |
8269 | { |
8270 | if (p_j2k == 00) { |
8271 | return; |
8272 | } |
8273 | |
8274 | if (p_j2k->m_is_decoder) { |
8275 | |
8276 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { |
8277 | opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
8278 | opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); |
8279 | p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; |
8280 | } |
8281 | |
8282 | if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { |
8283 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
8284 | p_j2k->m_specific_param.m_decoder.m_header_data = 00; |
8285 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
8286 | } |
8287 | |
8288 | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
8289 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00; |
8290 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
8291 | |
8292 | } else { |
8293 | |
8294 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
8295 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
8296 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; |
8297 | } |
8298 | |
8299 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
8300 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
8301 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; |
8302 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; |
8303 | } |
8304 | |
8305 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
8306 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
8307 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; |
8308 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
8309 | } |
8310 | } |
8311 | |
8312 | opj_tcd_destroy(p_j2k->m_tcd); |
8313 | |
8314 | opj_j2k_cp_destroy(&(p_j2k->m_cp)); |
8315 | memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t)); |
8316 | |
8317 | opj_procedure_list_destroy(p_j2k->m_procedure_list); |
8318 | p_j2k->m_procedure_list = 00; |
8319 | |
8320 | opj_procedure_list_destroy(p_j2k->m_validation_list); |
8321 | p_j2k->m_procedure_list = 00; |
8322 | |
8323 | j2k_destroy_cstr_index(p_j2k->cstr_index); |
8324 | p_j2k->cstr_index = NULL; |
8325 | |
8326 | opj_image_destroy(p_j2k->m_private_image); |
8327 | p_j2k->m_private_image = NULL; |
8328 | |
8329 | opj_image_destroy(p_j2k->m_output_image); |
8330 | p_j2k->m_output_image = NULL; |
8331 | |
8332 | opj_thread_pool_destroy(p_j2k->m_tp); |
8333 | p_j2k->m_tp = NULL; |
8334 | |
8335 | opj_free(p_j2k); |
8336 | } |
8337 | |
8338 | void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind) |
8339 | { |
8340 | if (p_cstr_ind) { |
8341 | |
8342 | if (p_cstr_ind->marker) { |
8343 | opj_free(p_cstr_ind->marker); |
8344 | p_cstr_ind->marker = NULL; |
8345 | } |
8346 | |
8347 | if (p_cstr_ind->tile_index) { |
8348 | OPJ_UINT32 it_tile = 0; |
8349 | |
8350 | for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { |
8351 | |
8352 | if (p_cstr_ind->tile_index[it_tile].packet_index) { |
8353 | opj_free(p_cstr_ind->tile_index[it_tile].packet_index); |
8354 | p_cstr_ind->tile_index[it_tile].packet_index = NULL; |
8355 | } |
8356 | |
8357 | if (p_cstr_ind->tile_index[it_tile].tp_index) { |
8358 | opj_free(p_cstr_ind->tile_index[it_tile].tp_index); |
8359 | p_cstr_ind->tile_index[it_tile].tp_index = NULL; |
8360 | } |
8361 | |
8362 | if (p_cstr_ind->tile_index[it_tile].marker) { |
8363 | opj_free(p_cstr_ind->tile_index[it_tile].marker); |
8364 | p_cstr_ind->tile_index[it_tile].marker = NULL; |
8365 | |
8366 | } |
8367 | } |
8368 | |
8369 | opj_free(p_cstr_ind->tile_index); |
8370 | p_cstr_ind->tile_index = NULL; |
8371 | } |
8372 | |
8373 | opj_free(p_cstr_ind); |
8374 | } |
8375 | } |
8376 | |
8377 | static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp) |
8378 | { |
8379 | if (p_tcp == 00) { |
8380 | return; |
8381 | } |
8382 | |
8383 | if (p_tcp->ppt_markers != 00) { |
8384 | OPJ_UINT32 i; |
8385 | for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { |
8386 | if (p_tcp->ppt_markers[i].m_data != NULL) { |
8387 | opj_free(p_tcp->ppt_markers[i].m_data); |
8388 | } |
8389 | } |
8390 | p_tcp->ppt_markers_count = 0U; |
8391 | opj_free(p_tcp->ppt_markers); |
8392 | p_tcp->ppt_markers = NULL; |
8393 | } |
8394 | |
8395 | if (p_tcp->ppt_buffer != 00) { |
8396 | opj_free(p_tcp->ppt_buffer); |
8397 | p_tcp->ppt_buffer = 00; |
8398 | } |
8399 | |
8400 | if (p_tcp->tccps != 00) { |
8401 | opj_free(p_tcp->tccps); |
8402 | p_tcp->tccps = 00; |
8403 | } |
8404 | |
8405 | if (p_tcp->m_mct_coding_matrix != 00) { |
8406 | opj_free(p_tcp->m_mct_coding_matrix); |
8407 | p_tcp->m_mct_coding_matrix = 00; |
8408 | } |
8409 | |
8410 | if (p_tcp->m_mct_decoding_matrix != 00) { |
8411 | opj_free(p_tcp->m_mct_decoding_matrix); |
8412 | p_tcp->m_mct_decoding_matrix = 00; |
8413 | } |
8414 | |
8415 | if (p_tcp->m_mcc_records) { |
8416 | opj_free(p_tcp->m_mcc_records); |
8417 | p_tcp->m_mcc_records = 00; |
8418 | p_tcp->m_nb_max_mcc_records = 0; |
8419 | p_tcp->m_nb_mcc_records = 0; |
8420 | } |
8421 | |
8422 | if (p_tcp->m_mct_records) { |
8423 | opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; |
8424 | OPJ_UINT32 i; |
8425 | |
8426 | for (i = 0; i < p_tcp->m_nb_mct_records; ++i) { |
8427 | if (l_mct_data->m_data) { |
8428 | opj_free(l_mct_data->m_data); |
8429 | l_mct_data->m_data = 00; |
8430 | } |
8431 | |
8432 | ++l_mct_data; |
8433 | } |
8434 | |
8435 | opj_free(p_tcp->m_mct_records); |
8436 | p_tcp->m_mct_records = 00; |
8437 | } |
8438 | |
8439 | if (p_tcp->mct_norms != 00) { |
8440 | opj_free(p_tcp->mct_norms); |
8441 | p_tcp->mct_norms = 00; |
8442 | } |
8443 | |
8444 | opj_j2k_tcp_data_destroy(p_tcp); |
8445 | |
8446 | } |
8447 | |
8448 | static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp) |
8449 | { |
8450 | if (p_tcp->m_data) { |
8451 | opj_free(p_tcp->m_data); |
8452 | p_tcp->m_data = NULL; |
8453 | p_tcp->m_data_size = 0; |
8454 | } |
8455 | } |
8456 | |
8457 | static void opj_j2k_cp_destroy(opj_cp_t *p_cp) |
8458 | { |
8459 | OPJ_UINT32 l_nb_tiles; |
8460 | opj_tcp_t * l_current_tile = 00; |
8461 | |
8462 | if (p_cp == 00) { |
8463 | return; |
8464 | } |
8465 | if (p_cp->tcps != 00) { |
8466 | OPJ_UINT32 i; |
8467 | l_current_tile = p_cp->tcps; |
8468 | l_nb_tiles = p_cp->th * p_cp->tw; |
8469 | |
8470 | for (i = 0U; i < l_nb_tiles; ++i) { |
8471 | opj_j2k_tcp_destroy(l_current_tile); |
8472 | ++l_current_tile; |
8473 | } |
8474 | opj_free(p_cp->tcps); |
8475 | p_cp->tcps = 00; |
8476 | } |
8477 | if (p_cp->ppm_markers != 00) { |
8478 | OPJ_UINT32 i; |
8479 | for (i = 0U; i < p_cp->ppm_markers_count; ++i) { |
8480 | if (p_cp->ppm_markers[i].m_data != NULL) { |
8481 | opj_free(p_cp->ppm_markers[i].m_data); |
8482 | } |
8483 | } |
8484 | p_cp->ppm_markers_count = 0U; |
8485 | opj_free(p_cp->ppm_markers); |
8486 | p_cp->ppm_markers = NULL; |
8487 | } |
8488 | opj_free(p_cp->ppm_buffer); |
8489 | p_cp->ppm_buffer = 00; |
8490 | p_cp->ppm_data = |
8491 | NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ |
8492 | opj_free(p_cp->comment); |
8493 | p_cp->comment = 00; |
8494 | if (! p_cp->m_is_decoder) { |
8495 | opj_free(p_cp->m_specific_param.m_enc.m_matrice); |
8496 | p_cp->m_specific_param.m_enc.m_matrice = 00; |
8497 | } |
8498 | } |
8499 | |
8500 | static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t |
8501 | *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, |
8502 | opj_event_mgr_t * p_manager) |
8503 | { |
8504 | OPJ_BYTE [10]; |
8505 | OPJ_OFF_T l_stream_pos_backup; |
8506 | OPJ_UINT32 l_current_marker; |
8507 | OPJ_UINT32 l_marker_size; |
8508 | OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts; |
8509 | |
8510 | /* initialize to no correction needed */ |
8511 | *p_correction_needed = OPJ_FALSE; |
8512 | |
8513 | if (!opj_stream_has_seek(p_stream)) { |
8514 | /* We can't do much in this case, seek is needed */ |
8515 | return OPJ_TRUE; |
8516 | } |
8517 | |
8518 | l_stream_pos_backup = opj_stream_tell(p_stream); |
8519 | if (l_stream_pos_backup == -1) { |
8520 | /* let's do nothing */ |
8521 | return OPJ_TRUE; |
8522 | } |
8523 | |
8524 | for (;;) { |
8525 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8526 | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
8527 | /* assume all is OK */ |
8528 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
8529 | return OPJ_FALSE; |
8530 | } |
8531 | return OPJ_TRUE; |
8532 | } |
8533 | |
8534 | /* Read 2 bytes from buffer as the new marker ID */ |
8535 | opj_read_bytes(l_header_data, &l_current_marker, 2); |
8536 | |
8537 | if (l_current_marker != J2K_MS_SOT) { |
8538 | /* assume all is OK */ |
8539 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
8540 | return OPJ_FALSE; |
8541 | } |
8542 | return OPJ_TRUE; |
8543 | } |
8544 | |
8545 | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
8546 | if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { |
8547 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8548 | return OPJ_FALSE; |
8549 | } |
8550 | |
8551 | /* Read 2 bytes from the buffer as the marker size */ |
8552 | opj_read_bytes(l_header_data, &l_marker_size, 2); |
8553 | |
8554 | /* Check marker size for SOT Marker */ |
8555 | if (l_marker_size != 10) { |
8556 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n" ); |
8557 | return OPJ_FALSE; |
8558 | } |
8559 | l_marker_size -= 2; |
8560 | |
8561 | if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, |
8562 | p_manager) != l_marker_size) { |
8563 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8564 | return OPJ_FALSE; |
8565 | } |
8566 | |
8567 | if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, |
8568 | &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { |
8569 | return OPJ_FALSE; |
8570 | } |
8571 | |
8572 | if (l_tile_no == tile_no) { |
8573 | /* we found what we were looking for */ |
8574 | break; |
8575 | } |
8576 | |
8577 | if (l_tot_len < 14U) { |
8578 | /* last SOT until EOC or invalid Psot value */ |
8579 | /* assume all is OK */ |
8580 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
8581 | return OPJ_FALSE; |
8582 | } |
8583 | return OPJ_TRUE; |
8584 | } |
8585 | l_tot_len -= 12U; |
8586 | /* look for next SOT marker */ |
8587 | if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), |
8588 | p_manager) != (OPJ_OFF_T)(l_tot_len)) { |
8589 | /* assume all is OK */ |
8590 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
8591 | return OPJ_FALSE; |
8592 | } |
8593 | return OPJ_TRUE; |
8594 | } |
8595 | } |
8596 | |
8597 | /* check for correction */ |
8598 | if (l_current_part == l_num_parts) { |
8599 | *p_correction_needed = OPJ_TRUE; |
8600 | } |
8601 | |
8602 | if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { |
8603 | return OPJ_FALSE; |
8604 | } |
8605 | return OPJ_TRUE; |
8606 | } |
8607 | |
8608 | OPJ_BOOL (opj_j2k_t * p_j2k, |
8609 | OPJ_UINT32 * p_tile_index, |
8610 | OPJ_UINT32 * p_data_size, |
8611 | OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, |
8612 | OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, |
8613 | OPJ_UINT32 * p_nb_comps, |
8614 | OPJ_BOOL * p_go_on, |
8615 | opj_stream_private_t *p_stream, |
8616 | opj_event_mgr_t * p_manager) |
8617 | { |
8618 | OPJ_UINT32 l_current_marker = J2K_MS_SOT; |
8619 | OPJ_UINT32 l_marker_size; |
8620 | const opj_dec_memory_marker_handler_t * l_marker_handler = 00; |
8621 | opj_tcp_t * l_tcp = NULL; |
8622 | |
8623 | /* preconditions */ |
8624 | assert(p_stream != 00); |
8625 | assert(p_j2k != 00); |
8626 | assert(p_manager != 00); |
8627 | |
8628 | /* Reach the End Of Codestream ?*/ |
8629 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
8630 | l_current_marker = J2K_MS_EOC; |
8631 | } |
8632 | /* We need to encounter a SOT marker (a new tile-part header) */ |
8633 | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
8634 | return OPJ_FALSE; |
8635 | } |
8636 | |
8637 | /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ |
8638 | while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) && |
8639 | (l_current_marker != J2K_MS_EOC)) { |
8640 | |
8641 | /* Try to read until the Start Of Data is detected */ |
8642 | while (l_current_marker != J2K_MS_SOD) { |
8643 | |
8644 | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
8645 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
8646 | break; |
8647 | } |
8648 | |
8649 | /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ |
8650 | if (opj_stream_read_data(p_stream, |
8651 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8652 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8653 | return OPJ_FALSE; |
8654 | } |
8655 | |
8656 | /* Read 2 bytes from the buffer as the marker size */ |
8657 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, |
8658 | 2); |
8659 | |
8660 | /* Check marker size (does not include marker ID but includes marker size) */ |
8661 | if (l_marker_size < 2) { |
8662 | opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n" ); |
8663 | return OPJ_FALSE; |
8664 | } |
8665 | |
8666 | /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
8667 | if (l_current_marker == 0x8080 && |
8668 | opj_stream_get_number_byte_left(p_stream) == 0) { |
8669 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
8670 | break; |
8671 | } |
8672 | |
8673 | /* Why this condition? FIXME */ |
8674 | if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) { |
8675 | p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); |
8676 | } |
8677 | l_marker_size -= 2; /* Subtract the size of the marker ID already read */ |
8678 | |
8679 | /* Get the marker handler from the marker ID */ |
8680 | l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); |
8681 | |
8682 | /* Check if the marker is known and if it is the right place to find it */ |
8683 | if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { |
8684 | opj_event_msg(p_manager, EVT_ERROR, |
8685 | "Marker is not compliant with its position\n" ); |
8686 | return OPJ_FALSE; |
8687 | } |
8688 | /* FIXME manage case of unknown marker as in the main header ? */ |
8689 | |
8690 | /* Check if the marker size is compatible with the header data size */ |
8691 | if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { |
8692 | OPJ_BYTE * = NULL; |
8693 | /* If we are here, this means we consider this marker as known & we will read it */ |
8694 | /* Check enough bytes left in stream before allocation */ |
8695 | if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { |
8696 | opj_event_msg(p_manager, EVT_ERROR, |
8697 | "Marker size inconsistent with stream length\n" ); |
8698 | return OPJ_FALSE; |
8699 | } |
8700 | new_header_data = (OPJ_BYTE *) opj_realloc( |
8701 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); |
8702 | if (! new_header_data) { |
8703 | opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); |
8704 | p_j2k->m_specific_param.m_decoder.m_header_data = NULL; |
8705 | p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; |
8706 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n" ); |
8707 | return OPJ_FALSE; |
8708 | } |
8709 | p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; |
8710 | p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; |
8711 | } |
8712 | |
8713 | /* Try to read the rest of the marker segment from stream and copy them into the buffer */ |
8714 | if (opj_stream_read_data(p_stream, |
8715 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, |
8716 | p_manager) != l_marker_size) { |
8717 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8718 | return OPJ_FALSE; |
8719 | } |
8720 | |
8721 | if (!l_marker_handler->handler) { |
8722 | /* See issue #175 */ |
8723 | opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n" ); |
8724 | return OPJ_FALSE; |
8725 | } |
8726 | /* Read the marker segment with the correct marker handler */ |
8727 | if (!(*(l_marker_handler->handler))(p_j2k, |
8728 | p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { |
8729 | opj_event_msg(p_manager, EVT_ERROR, |
8730 | "Fail to read the current marker segment (%#x)\n" , l_current_marker); |
8731 | return OPJ_FALSE; |
8732 | } |
8733 | |
8734 | /* Add the marker to the codestream index*/ |
8735 | if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, |
8736 | p_j2k->cstr_index, |
8737 | l_marker_handler->id, |
8738 | (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, |
8739 | l_marker_size + 4)) { |
8740 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n" ); |
8741 | return OPJ_FALSE; |
8742 | } |
8743 | |
8744 | /* Keep the position of the last SOT marker read */ |
8745 | if (l_marker_handler->id == J2K_MS_SOT) { |
8746 | OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 |
8747 | ; |
8748 | if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) { |
8749 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; |
8750 | } |
8751 | } |
8752 | |
8753 | if (p_j2k->m_specific_param.m_decoder.m_skip_data) { |
8754 | /* Skip the rest of the tile part header*/ |
8755 | if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length, |
8756 | p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { |
8757 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8758 | return OPJ_FALSE; |
8759 | } |
8760 | l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ |
8761 | } else { |
8762 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ |
8763 | if (opj_stream_read_data(p_stream, |
8764 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8765 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8766 | return OPJ_FALSE; |
8767 | } |
8768 | /* Read 2 bytes from the buffer as the new marker ID */ |
8769 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8770 | &l_current_marker, 2); |
8771 | } |
8772 | } |
8773 | if (opj_stream_get_number_byte_left(p_stream) == 0 |
8774 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
8775 | break; |
8776 | } |
8777 | |
8778 | /* If we didn't skip data before, we need to read the SOD marker*/ |
8779 | if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { |
8780 | /* Try to read the SOD marker and skip data ? FIXME */ |
8781 | if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { |
8782 | return OPJ_FALSE; |
8783 | } |
8784 | if (p_j2k->m_specific_param.m_decoder.m_can_decode && |
8785 | !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { |
8786 | /* Issue 254 */ |
8787 | OPJ_BOOL l_correction_needed; |
8788 | |
8789 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
8790 | if (!opj_j2k_need_nb_tile_parts_correction(p_stream, |
8791 | p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { |
8792 | opj_event_msg(p_manager, EVT_ERROR, |
8793 | "opj_j2k_apply_nb_tile_parts_correction error\n" ); |
8794 | return OPJ_FALSE; |
8795 | } |
8796 | if (l_correction_needed) { |
8797 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
8798 | OPJ_UINT32 l_tile_no; |
8799 | |
8800 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
8801 | p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1; |
8802 | /* correct tiles */ |
8803 | for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { |
8804 | if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) { |
8805 | p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1; |
8806 | } |
8807 | } |
8808 | opj_event_msg(p_manager, EVT_WARNING, |
8809 | "Non conformant codestream TPsot==TNsot.\n" ); |
8810 | } |
8811 | } |
8812 | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
8813 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8814 | if (opj_stream_read_data(p_stream, |
8815 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8816 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8817 | return OPJ_FALSE; |
8818 | } |
8819 | |
8820 | /* Read 2 bytes from buffer as the new marker ID */ |
8821 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8822 | &l_current_marker, 2); |
8823 | } |
8824 | } else { |
8825 | /* Indicate we will try to read a new tile-part header*/ |
8826 | p_j2k->m_specific_param.m_decoder.m_skip_data = 0; |
8827 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
8828 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
8829 | |
8830 | /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ |
8831 | if (opj_stream_read_data(p_stream, |
8832 | p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { |
8833 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8834 | return OPJ_FALSE; |
8835 | } |
8836 | |
8837 | /* Read 2 bytes from buffer as the new marker ID */ |
8838 | opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, |
8839 | &l_current_marker, 2); |
8840 | } |
8841 | } |
8842 | |
8843 | /* Current marker is the EOC marker ?*/ |
8844 | if (l_current_marker == J2K_MS_EOC) { |
8845 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
8846 | } |
8847 | |
8848 | /* FIXME DOC ???*/ |
8849 | if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { |
8850 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
8851 | l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; |
8852 | |
8853 | while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) { |
8854 | ++p_j2k->m_current_tile_number; |
8855 | ++l_tcp; |
8856 | } |
8857 | |
8858 | if (p_j2k->m_current_tile_number == l_nb_tiles) { |
8859 | *p_go_on = OPJ_FALSE; |
8860 | return OPJ_TRUE; |
8861 | } |
8862 | } |
8863 | |
8864 | if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, |
8865 | p_manager)) { |
8866 | opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n" ); |
8867 | return OPJ_FALSE; |
8868 | } |
8869 | /*FIXME ???*/ |
8870 | if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
8871 | p_manager)) { |
8872 | opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n" ); |
8873 | return OPJ_FALSE; |
8874 | } |
8875 | |
8876 | opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n" , |
8877 | p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); |
8878 | |
8879 | *p_tile_index = p_j2k->m_current_tile_number; |
8880 | *p_go_on = OPJ_TRUE; |
8881 | if (p_data_size) { |
8882 | /* For internal use in j2k.c, we don't need this */ |
8883 | /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */ |
8884 | *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE); |
8885 | if (*p_data_size == UINT_MAX) { |
8886 | return OPJ_FALSE; |
8887 | } |
8888 | } |
8889 | *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; |
8890 | *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; |
8891 | *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; |
8892 | *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; |
8893 | *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; |
8894 | |
8895 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
8896 | |
8897 | return OPJ_TRUE; |
8898 | } |
8899 | |
8900 | OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
8901 | OPJ_UINT32 p_tile_index, |
8902 | OPJ_BYTE * p_data, |
8903 | OPJ_UINT32 p_data_size, |
8904 | opj_stream_private_t *p_stream, |
8905 | opj_event_mgr_t * p_manager) |
8906 | { |
8907 | OPJ_UINT32 l_current_marker; |
8908 | OPJ_BYTE l_data [2]; |
8909 | opj_tcp_t * l_tcp; |
8910 | opj_image_t* l_image_for_bounds; |
8911 | |
8912 | /* preconditions */ |
8913 | assert(p_stream != 00); |
8914 | assert(p_j2k != 00); |
8915 | assert(p_manager != 00); |
8916 | |
8917 | if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA) |
8918 | || (p_tile_index != p_j2k->m_current_tile_number)) { |
8919 | return OPJ_FALSE; |
8920 | } |
8921 | |
8922 | l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); |
8923 | if (! l_tcp->m_data) { |
8924 | opj_j2k_tcp_destroy(l_tcp); |
8925 | return OPJ_FALSE; |
8926 | } |
8927 | |
8928 | /* When using the opj_read_tile_header / opj_decode_tile_data API */ |
8929 | /* such as in test_tile_decoder, m_output_image is NULL, so fall back */ |
8930 | /* to the full image dimension. This is a bit surprising that */ |
8931 | /* opj_set_decode_area() is only used to determinte intersecting tiles, */ |
8932 | /* but full tile decoding is done */ |
8933 | l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image : |
8934 | p_j2k->m_private_image; |
8935 | if (! opj_tcd_decode_tile(p_j2k->m_tcd, |
8936 | l_image_for_bounds->x0, |
8937 | l_image_for_bounds->y0, |
8938 | l_image_for_bounds->x1, |
8939 | l_image_for_bounds->y1, |
8940 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode, |
8941 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
8942 | l_tcp->m_data, |
8943 | l_tcp->m_data_size, |
8944 | p_tile_index, |
8945 | p_j2k->cstr_index, p_manager)) { |
8946 | opj_j2k_tcp_destroy(l_tcp); |
8947 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; |
8948 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n" ); |
8949 | return OPJ_FALSE; |
8950 | } |
8951 | |
8952 | /* p_data can be set to NULL when the call will take care of using */ |
8953 | /* itself the TCD data. This is typically the case for whole single */ |
8954 | /* tile decoding optimization. */ |
8955 | if (p_data != NULL) { |
8956 | if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
8957 | return OPJ_FALSE; |
8958 | } |
8959 | |
8960 | /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) |
8961 | * we destroy just the data which will be re-read in read_tile_header*/ |
8962 | /*opj_j2k_tcp_destroy(l_tcp); |
8963 | p_j2k->m_tcd->tcp = 0;*/ |
8964 | opj_j2k_tcp_data_destroy(l_tcp); |
8965 | } |
8966 | |
8967 | p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
8968 | p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); |
8969 | |
8970 | if (opj_stream_get_number_byte_left(p_stream) == 0 |
8971 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
8972 | return OPJ_TRUE; |
8973 | } |
8974 | |
8975 | if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { |
8976 | if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { |
8977 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n" ); |
8978 | return OPJ_FALSE; |
8979 | } |
8980 | |
8981 | opj_read_bytes(l_data, &l_current_marker, 2); |
8982 | |
8983 | if (l_current_marker == J2K_MS_EOC) { |
8984 | p_j2k->m_current_tile_number = 0; |
8985 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
8986 | } else if (l_current_marker != J2K_MS_SOT) { |
8987 | if (opj_stream_get_number_byte_left(p_stream) == 0) { |
8988 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
8989 | opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n" ); |
8990 | return OPJ_TRUE; |
8991 | } |
8992 | opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n" ); |
8993 | return OPJ_FALSE; |
8994 | } |
8995 | } |
8996 | |
8997 | return OPJ_TRUE; |
8998 | } |
8999 | |
9000 | static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, |
9001 | opj_image_t* p_output_image) |
9002 | { |
9003 | OPJ_UINT32 i, j; |
9004 | OPJ_UINT32 l_width_src, l_height_src; |
9005 | OPJ_UINT32 l_width_dest, l_height_dest; |
9006 | OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; |
9007 | OPJ_SIZE_T l_start_offset_src; |
9008 | OPJ_UINT32 l_start_x_dest, l_start_y_dest; |
9009 | OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; |
9010 | OPJ_SIZE_T l_start_offset_dest; |
9011 | |
9012 | opj_image_comp_t * l_img_comp_src = 00; |
9013 | opj_image_comp_t * l_img_comp_dest = 00; |
9014 | |
9015 | opj_tcd_tilecomp_t * l_tilec = 00; |
9016 | opj_image_t * l_image_src = 00; |
9017 | OPJ_INT32 * l_dest_ptr; |
9018 | |
9019 | l_tilec = p_tcd->tcd_image->tiles->comps; |
9020 | l_image_src = p_tcd->image; |
9021 | l_img_comp_src = l_image_src->comps; |
9022 | |
9023 | l_img_comp_dest = p_output_image->comps; |
9024 | |
9025 | for (i = 0; i < l_image_src->numcomps; |
9026 | i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) { |
9027 | OPJ_INT32 res_x0, res_x1, res_y0, res_y1; |
9028 | OPJ_UINT32 src_data_stride; |
9029 | const OPJ_INT32* p_src_data; |
9030 | |
9031 | /* Copy info from decoded comp image to output image */ |
9032 | l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; |
9033 | |
9034 | if (p_tcd->whole_tile_decoding) { |
9035 | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
9036 | l_img_comp_src->resno_decoded; |
9037 | res_x0 = l_res->x0; |
9038 | res_y0 = l_res->y0; |
9039 | res_x1 = l_res->x1; |
9040 | res_y1 = l_res->y1; |
9041 | src_data_stride = (OPJ_UINT32)( |
9042 | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 - |
9043 | l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0); |
9044 | p_src_data = l_tilec->data; |
9045 | } else { |
9046 | opj_tcd_resolution_t* l_res = l_tilec->resolutions + |
9047 | l_img_comp_src->resno_decoded; |
9048 | res_x0 = (OPJ_INT32)l_res->win_x0; |
9049 | res_y0 = (OPJ_INT32)l_res->win_y0; |
9050 | res_x1 = (OPJ_INT32)l_res->win_x1; |
9051 | res_y1 = (OPJ_INT32)l_res->win_y1; |
9052 | src_data_stride = l_res->win_x1 - l_res->win_x0; |
9053 | p_src_data = l_tilec->data_win; |
9054 | } |
9055 | |
9056 | if (p_src_data == NULL) { |
9057 | /* Happens for partial component decoding */ |
9058 | continue; |
9059 | } |
9060 | |
9061 | l_width_src = (OPJ_UINT32)(res_x1 - res_x0); |
9062 | l_height_src = (OPJ_UINT32)(res_y1 - res_y0); |
9063 | |
9064 | |
9065 | /* Current tile component size*/ |
9066 | /*if (i == 0) { |
9067 | fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", |
9068 | res_x0, res_x1, res_y0, res_y1); |
9069 | }*/ |
9070 | |
9071 | |
9072 | /* Border of the current output component*/ |
9073 | l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); |
9074 | l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); |
9075 | l_x1_dest = l_x0_dest + |
9076 | l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */ |
9077 | l_y1_dest = l_y0_dest + l_img_comp_dest->h; |
9078 | |
9079 | /*if (i == 0) { |
9080 | fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", |
9081 | l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); |
9082 | }*/ |
9083 | |
9084 | /*-----*/ |
9085 | /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) |
9086 | * of the input buffer (decoded tile component) which will be move |
9087 | * in the output buffer. Compute the area of the output buffer (l_start_x_dest, |
9088 | * l_start_y_dest, l_width_dest, l_height_dest) which will be modified |
9089 | * by this input area. |
9090 | * */ |
9091 | assert(res_x0 >= 0); |
9092 | assert(res_x1 >= 0); |
9093 | if (l_x0_dest < (OPJ_UINT32)res_x0) { |
9094 | l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest; |
9095 | l_offset_x0_src = 0; |
9096 | |
9097 | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
9098 | l_width_dest = l_width_src; |
9099 | l_offset_x1_src = 0; |
9100 | } else { |
9101 | l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ; |
9102 | l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); |
9103 | } |
9104 | } else { |
9105 | l_start_x_dest = 0U; |
9106 | l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0; |
9107 | |
9108 | if (l_x1_dest >= (OPJ_UINT32)res_x1) { |
9109 | l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; |
9110 | l_offset_x1_src = 0; |
9111 | } else { |
9112 | l_width_dest = l_img_comp_dest->w ; |
9113 | l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest; |
9114 | } |
9115 | } |
9116 | |
9117 | if (l_y0_dest < (OPJ_UINT32)res_y0) { |
9118 | l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest; |
9119 | l_offset_y0_src = 0; |
9120 | |
9121 | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
9122 | l_height_dest = l_height_src; |
9123 | l_offset_y1_src = 0; |
9124 | } else { |
9125 | l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ; |
9126 | l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); |
9127 | } |
9128 | } else { |
9129 | l_start_y_dest = 0U; |
9130 | l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0; |
9131 | |
9132 | if (l_y1_dest >= (OPJ_UINT32)res_y1) { |
9133 | l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; |
9134 | l_offset_y1_src = 0; |
9135 | } else { |
9136 | l_height_dest = l_img_comp_dest->h ; |
9137 | l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest; |
9138 | } |
9139 | } |
9140 | |
9141 | if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) || |
9142 | (l_offset_y1_src < 0)) { |
9143 | return OPJ_FALSE; |
9144 | } |
9145 | /* testcase 2977.pdf.asan.67.2198 */ |
9146 | if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { |
9147 | return OPJ_FALSE; |
9148 | } |
9149 | /*-----*/ |
9150 | |
9151 | /* Compute the input buffer offset */ |
9152 | l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src |
9153 | * (OPJ_SIZE_T)src_data_stride; |
9154 | |
9155 | /* Compute the output buffer offset */ |
9156 | l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest |
9157 | * (OPJ_SIZE_T)l_img_comp_dest->w; |
9158 | |
9159 | /* Allocate output component buffer if necessary */ |
9160 | if (l_img_comp_dest->data == NULL && |
9161 | l_start_offset_src == 0 && l_start_offset_dest == 0 && |
9162 | src_data_stride == l_img_comp_dest->w && |
9163 | l_width_dest == l_img_comp_dest->w && |
9164 | l_height_dest == l_img_comp_dest->h) { |
9165 | /* If the final image matches the tile buffer, then borrow it */ |
9166 | /* directly to save a copy */ |
9167 | if (p_tcd->whole_tile_decoding) { |
9168 | l_img_comp_dest->data = l_tilec->data; |
9169 | l_tilec->data = NULL; |
9170 | } else { |
9171 | l_img_comp_dest->data = l_tilec->data_win; |
9172 | l_tilec->data_win = NULL; |
9173 | } |
9174 | continue; |
9175 | } else if (l_img_comp_dest->data == NULL) { |
9176 | OPJ_SIZE_T l_width = l_img_comp_dest->w; |
9177 | OPJ_SIZE_T l_height = l_img_comp_dest->h; |
9178 | |
9179 | if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) || |
9180 | l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) { |
9181 | /* would overflow */ |
9182 | return OPJ_FALSE; |
9183 | } |
9184 | l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height * |
9185 | sizeof(OPJ_INT32)); |
9186 | if (! l_img_comp_dest->data) { |
9187 | return OPJ_FALSE; |
9188 | } |
9189 | |
9190 | if (l_img_comp_dest->w != l_width_dest || |
9191 | l_img_comp_dest->h != l_height_dest) { |
9192 | memset(l_img_comp_dest->data, 0, |
9193 | (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32)); |
9194 | } |
9195 | } |
9196 | |
9197 | /* Move the output buffer to the first place where we will write*/ |
9198 | l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; |
9199 | |
9200 | { |
9201 | const OPJ_INT32 * l_src_ptr = p_src_data; |
9202 | l_src_ptr += l_start_offset_src; |
9203 | |
9204 | for (j = 0; j < l_height_dest; ++j) { |
9205 | memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32)); |
9206 | l_dest_ptr += l_img_comp_dest->w; |
9207 | l_src_ptr += src_data_stride; |
9208 | } |
9209 | } |
9210 | |
9211 | |
9212 | } |
9213 | |
9214 | return OPJ_TRUE; |
9215 | } |
9216 | |
9217 | static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image, |
9218 | opj_event_mgr_t * p_manager) |
9219 | { |
9220 | OPJ_UINT32 it_comp; |
9221 | OPJ_INT32 l_comp_x1, l_comp_y1; |
9222 | opj_image_comp_t* l_img_comp = NULL; |
9223 | |
9224 | l_img_comp = p_image->comps; |
9225 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
9226 | OPJ_INT32 l_h, l_w; |
9227 | |
9228 | l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, |
9229 | (OPJ_INT32)l_img_comp->dx); |
9230 | l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, |
9231 | (OPJ_INT32)l_img_comp->dy); |
9232 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
9233 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
9234 | |
9235 | l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) |
9236 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); |
9237 | if (l_w < 0) { |
9238 | opj_event_msg(p_manager, EVT_ERROR, |
9239 | "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n" , |
9240 | it_comp, l_w); |
9241 | return OPJ_FALSE; |
9242 | } |
9243 | l_img_comp->w = (OPJ_UINT32)l_w; |
9244 | |
9245 | l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) |
9246 | - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); |
9247 | if (l_h < 0) { |
9248 | opj_event_msg(p_manager, EVT_ERROR, |
9249 | "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n" , |
9250 | it_comp, l_h); |
9251 | return OPJ_FALSE; |
9252 | } |
9253 | l_img_comp->h = (OPJ_UINT32)l_h; |
9254 | |
9255 | l_img_comp++; |
9256 | } |
9257 | |
9258 | return OPJ_TRUE; |
9259 | } |
9260 | |
9261 | OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, |
9262 | OPJ_UINT32 numcomps, |
9263 | const OPJ_UINT32* comps_indices, |
9264 | opj_event_mgr_t * p_manager) |
9265 | { |
9266 | OPJ_UINT32 i; |
9267 | OPJ_BOOL* already_mapped; |
9268 | |
9269 | if (p_j2k->m_private_image == NULL) { |
9270 | opj_event_msg(p_manager, EVT_ERROR, |
9271 | "opj_read_header() should be called before " |
9272 | "opj_set_decoded_components().\n" ); |
9273 | return OPJ_FALSE; |
9274 | } |
9275 | |
9276 | already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), |
9277 | p_j2k->m_private_image->numcomps); |
9278 | if (already_mapped == NULL) { |
9279 | return OPJ_FALSE; |
9280 | } |
9281 | |
9282 | for (i = 0; i < numcomps; i++) { |
9283 | if (comps_indices[i] >= p_j2k->m_private_image->numcomps) { |
9284 | opj_event_msg(p_manager, EVT_ERROR, |
9285 | "Invalid component index: %u\n" , |
9286 | comps_indices[i]); |
9287 | opj_free(already_mapped); |
9288 | return OPJ_FALSE; |
9289 | } |
9290 | if (already_mapped[comps_indices[i]]) { |
9291 | opj_event_msg(p_manager, EVT_ERROR, |
9292 | "Component index %u used several times\n" , |
9293 | comps_indices[i]); |
9294 | opj_free(already_mapped); |
9295 | return OPJ_FALSE; |
9296 | } |
9297 | already_mapped[comps_indices[i]] = OPJ_TRUE; |
9298 | } |
9299 | opj_free(already_mapped); |
9300 | |
9301 | opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); |
9302 | if (numcomps) { |
9303 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = |
9304 | (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32)); |
9305 | if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) { |
9306 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; |
9307 | return OPJ_FALSE; |
9308 | } |
9309 | memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, |
9310 | comps_indices, |
9311 | numcomps * sizeof(OPJ_UINT32)); |
9312 | } else { |
9313 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL; |
9314 | } |
9315 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps; |
9316 | |
9317 | return OPJ_TRUE; |
9318 | } |
9319 | |
9320 | |
9321 | OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, |
9322 | opj_image_t* p_image, |
9323 | OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, |
9324 | OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, |
9325 | opj_event_mgr_t * p_manager) |
9326 | { |
9327 | opj_cp_t * l_cp = &(p_j2k->m_cp); |
9328 | opj_image_t * l_image = p_j2k->m_private_image; |
9329 | OPJ_BOOL ret; |
9330 | OPJ_UINT32 it_comp; |
9331 | |
9332 | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
9333 | p_j2k->m_cp.tcps[0].m_data != NULL) { |
9334 | /* In the case of a single-tiled image whose codestream we have already */ |
9335 | /* ingested, go on */ |
9336 | } |
9337 | /* Check if we are read the main header */ |
9338 | else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { |
9339 | opj_event_msg(p_manager, EVT_ERROR, |
9340 | "Need to decode the main header before begin to decode the remaining codestream.\n" ); |
9341 | return OPJ_FALSE; |
9342 | } |
9343 | |
9344 | /* Update the comps[].factor member of the output image with the one */ |
9345 | /* of m_reduce */ |
9346 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
9347 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
9348 | } |
9349 | |
9350 | if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) { |
9351 | opj_event_msg(p_manager, EVT_INFO, |
9352 | "No decoded area parameters, set the decoded area to the whole image\n" ); |
9353 | |
9354 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
9355 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
9356 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
9357 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
9358 | |
9359 | p_image->x0 = l_image->x0; |
9360 | p_image->y0 = l_image->y0; |
9361 | p_image->x1 = l_image->x1; |
9362 | p_image->y1 = l_image->y1; |
9363 | |
9364 | return opj_j2k_update_image_dimensions(p_image, p_manager); |
9365 | } |
9366 | |
9367 | /* ----- */ |
9368 | /* Check if the positions provided by the user are correct */ |
9369 | |
9370 | /* Left */ |
9371 | if (p_start_x < 0) { |
9372 | opj_event_msg(p_manager, EVT_ERROR, |
9373 | "Left position of the decoded area (region_x0=%d) should be >= 0.\n" , |
9374 | p_start_x); |
9375 | return OPJ_FALSE; |
9376 | } else if ((OPJ_UINT32)p_start_x > l_image->x1) { |
9377 | opj_event_msg(p_manager, EVT_ERROR, |
9378 | "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n" , |
9379 | p_start_x, l_image->x1); |
9380 | return OPJ_FALSE; |
9381 | } else if ((OPJ_UINT32)p_start_x < l_image->x0) { |
9382 | opj_event_msg(p_manager, EVT_WARNING, |
9383 | "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n" , |
9384 | p_start_x, l_image->x0); |
9385 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; |
9386 | p_image->x0 = l_image->x0; |
9387 | } else { |
9388 | p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - |
9389 | l_cp->tx0) / l_cp->tdx; |
9390 | p_image->x0 = (OPJ_UINT32)p_start_x; |
9391 | } |
9392 | |
9393 | /* Up */ |
9394 | if (p_start_y < 0) { |
9395 | opj_event_msg(p_manager, EVT_ERROR, |
9396 | "Up position of the decoded area (region_y0=%d) should be >= 0.\n" , |
9397 | p_start_y); |
9398 | return OPJ_FALSE; |
9399 | } else if ((OPJ_UINT32)p_start_y > l_image->y1) { |
9400 | opj_event_msg(p_manager, EVT_ERROR, |
9401 | "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n" , |
9402 | p_start_y, l_image->y1); |
9403 | return OPJ_FALSE; |
9404 | } else if ((OPJ_UINT32)p_start_y < l_image->y0) { |
9405 | opj_event_msg(p_manager, EVT_WARNING, |
9406 | "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n" , |
9407 | p_start_y, l_image->y0); |
9408 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; |
9409 | p_image->y0 = l_image->y0; |
9410 | } else { |
9411 | p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - |
9412 | l_cp->ty0) / l_cp->tdy; |
9413 | p_image->y0 = (OPJ_UINT32)p_start_y; |
9414 | } |
9415 | |
9416 | /* Right */ |
9417 | if (p_end_x <= 0) { |
9418 | opj_event_msg(p_manager, EVT_ERROR, |
9419 | "Right position of the decoded area (region_x1=%d) should be > 0.\n" , |
9420 | p_end_x); |
9421 | return OPJ_FALSE; |
9422 | } else if ((OPJ_UINT32)p_end_x < l_image->x0) { |
9423 | opj_event_msg(p_manager, EVT_ERROR, |
9424 | "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n" , |
9425 | p_end_x, l_image->x0); |
9426 | return OPJ_FALSE; |
9427 | } else if ((OPJ_UINT32)p_end_x > l_image->x1) { |
9428 | opj_event_msg(p_manager, EVT_WARNING, |
9429 | "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n" , |
9430 | p_end_x, l_image->x1); |
9431 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; |
9432 | p_image->x1 = l_image->x1; |
9433 | } else { |
9434 | p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv( |
9435 | p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx); |
9436 | p_image->x1 = (OPJ_UINT32)p_end_x; |
9437 | } |
9438 | |
9439 | /* Bottom */ |
9440 | if (p_end_y <= 0) { |
9441 | opj_event_msg(p_manager, EVT_ERROR, |
9442 | "Bottom position of the decoded area (region_y1=%d) should be > 0.\n" , |
9443 | p_end_y); |
9444 | return OPJ_FALSE; |
9445 | } else if ((OPJ_UINT32)p_end_y < l_image->y0) { |
9446 | opj_event_msg(p_manager, EVT_ERROR, |
9447 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n" , |
9448 | p_end_y, l_image->y0); |
9449 | return OPJ_FALSE; |
9450 | } |
9451 | if ((OPJ_UINT32)p_end_y > l_image->y1) { |
9452 | opj_event_msg(p_manager, EVT_WARNING, |
9453 | "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n" , |
9454 | p_end_y, l_image->y1); |
9455 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; |
9456 | p_image->y1 = l_image->y1; |
9457 | } else { |
9458 | p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv( |
9459 | p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy); |
9460 | p_image->y1 = (OPJ_UINT32)p_end_y; |
9461 | } |
9462 | /* ----- */ |
9463 | |
9464 | p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; |
9465 | |
9466 | ret = opj_j2k_update_image_dimensions(p_image, p_manager); |
9467 | |
9468 | if (ret) { |
9469 | opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n" , |
9470 | p_image->x0, p_image->y0, p_image->x1, p_image->y1); |
9471 | } |
9472 | |
9473 | return ret; |
9474 | } |
9475 | |
9476 | opj_j2k_t* opj_j2k_create_decompress(void) |
9477 | { |
9478 | opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); |
9479 | if (!l_j2k) { |
9480 | return 00; |
9481 | } |
9482 | |
9483 | l_j2k->m_is_decoder = 1; |
9484 | l_j2k->m_cp.m_is_decoder = 1; |
9485 | /* in the absence of JP2 boxes, consider different bit depth / sign */ |
9486 | /* per component is allowed */ |
9487 | l_j2k->m_cp.allow_different_bit_depth_sign = 1; |
9488 | |
9489 | #ifdef OPJ_DISABLE_TPSOT_FIX |
9490 | l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; |
9491 | #endif |
9492 | |
9493 | l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1, |
9494 | sizeof(opj_tcp_t)); |
9495 | if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { |
9496 | opj_j2k_destroy(l_j2k); |
9497 | return 00; |
9498 | } |
9499 | |
9500 | l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1, |
9501 | OPJ_J2K_DEFAULT_HEADER_SIZE); |
9502 | if (! l_j2k->m_specific_param.m_decoder.m_header_data) { |
9503 | opj_j2k_destroy(l_j2k); |
9504 | return 00; |
9505 | } |
9506 | |
9507 | l_j2k->m_specific_param.m_decoder.m_header_data_size = |
9508 | OPJ_J2K_DEFAULT_HEADER_SIZE; |
9509 | |
9510 | l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; |
9511 | |
9512 | l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; |
9513 | |
9514 | /* codestream index creation */ |
9515 | l_j2k->cstr_index = opj_j2k_create_cstr_index(); |
9516 | if (!l_j2k->cstr_index) { |
9517 | opj_j2k_destroy(l_j2k); |
9518 | return 00; |
9519 | } |
9520 | |
9521 | /* validation list creation */ |
9522 | l_j2k->m_validation_list = opj_procedure_list_create(); |
9523 | if (! l_j2k->m_validation_list) { |
9524 | opj_j2k_destroy(l_j2k); |
9525 | return 00; |
9526 | } |
9527 | |
9528 | /* execution list creation */ |
9529 | l_j2k->m_procedure_list = opj_procedure_list_create(); |
9530 | if (! l_j2k->m_procedure_list) { |
9531 | opj_j2k_destroy(l_j2k); |
9532 | return 00; |
9533 | } |
9534 | |
9535 | l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); |
9536 | if (!l_j2k->m_tp) { |
9537 | l_j2k->m_tp = opj_thread_pool_create(0); |
9538 | } |
9539 | if (!l_j2k->m_tp) { |
9540 | opj_j2k_destroy(l_j2k); |
9541 | return NULL; |
9542 | } |
9543 | |
9544 | return l_j2k; |
9545 | } |
9546 | |
9547 | static opj_codestream_index_t* opj_j2k_create_cstr_index(void) |
9548 | { |
9549 | opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) |
9550 | opj_calloc(1, sizeof(opj_codestream_index_t)); |
9551 | if (!cstr_index) { |
9552 | return NULL; |
9553 | } |
9554 | |
9555 | cstr_index->maxmarknum = 100; |
9556 | cstr_index->marknum = 0; |
9557 | cstr_index->marker = (opj_marker_info_t*) |
9558 | opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); |
9559 | if (!cstr_index-> marker) { |
9560 | opj_free(cstr_index); |
9561 | return NULL; |
9562 | } |
9563 | |
9564 | cstr_index->tile_index = NULL; |
9565 | |
9566 | return cstr_index; |
9567 | } |
9568 | |
9569 | static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, |
9570 | OPJ_UINT32 p_tile_no, |
9571 | OPJ_UINT32 p_comp_no) |
9572 | { |
9573 | opj_cp_t *l_cp = 00; |
9574 | opj_tcp_t *l_tcp = 00; |
9575 | opj_tccp_t *l_tccp = 00; |
9576 | |
9577 | /* preconditions */ |
9578 | assert(p_j2k != 00); |
9579 | |
9580 | l_cp = &(p_j2k->m_cp); |
9581 | l_tcp = &l_cp->tcps[p_tile_no]; |
9582 | l_tccp = &l_tcp->tccps[p_comp_no]; |
9583 | |
9584 | /* preconditions again */ |
9585 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
9586 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
9587 | |
9588 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
9589 | return 5 + l_tccp->numresolutions; |
9590 | } else { |
9591 | return 5; |
9592 | } |
9593 | } |
9594 | |
9595 | static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, |
9596 | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
9597 | { |
9598 | OPJ_UINT32 i; |
9599 | opj_cp_t *l_cp = NULL; |
9600 | opj_tcp_t *l_tcp = NULL; |
9601 | opj_tccp_t *l_tccp0 = NULL; |
9602 | opj_tccp_t *l_tccp1 = NULL; |
9603 | |
9604 | /* preconditions */ |
9605 | assert(p_j2k != 00); |
9606 | |
9607 | l_cp = &(p_j2k->m_cp); |
9608 | l_tcp = &l_cp->tcps[p_tile_no]; |
9609 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
9610 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
9611 | |
9612 | if (l_tccp0->numresolutions != l_tccp1->numresolutions) { |
9613 | return OPJ_FALSE; |
9614 | } |
9615 | if (l_tccp0->cblkw != l_tccp1->cblkw) { |
9616 | return OPJ_FALSE; |
9617 | } |
9618 | if (l_tccp0->cblkh != l_tccp1->cblkh) { |
9619 | return OPJ_FALSE; |
9620 | } |
9621 | if (l_tccp0->cblksty != l_tccp1->cblksty) { |
9622 | return OPJ_FALSE; |
9623 | } |
9624 | if (l_tccp0->qmfbid != l_tccp1->qmfbid) { |
9625 | return OPJ_FALSE; |
9626 | } |
9627 | if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) { |
9628 | return OPJ_FALSE; |
9629 | } |
9630 | |
9631 | for (i = 0U; i < l_tccp0->numresolutions; ++i) { |
9632 | if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) { |
9633 | return OPJ_FALSE; |
9634 | } |
9635 | if (l_tccp0->prch[i] != l_tccp1->prch[i]) { |
9636 | return OPJ_FALSE; |
9637 | } |
9638 | } |
9639 | return OPJ_TRUE; |
9640 | } |
9641 | |
9642 | static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, |
9643 | OPJ_UINT32 p_tile_no, |
9644 | OPJ_UINT32 p_comp_no, |
9645 | OPJ_BYTE * p_data, |
9646 | OPJ_UINT32 * , |
9647 | struct opj_event_mgr * p_manager) |
9648 | { |
9649 | OPJ_UINT32 i; |
9650 | opj_cp_t *l_cp = 00; |
9651 | opj_tcp_t *l_tcp = 00; |
9652 | opj_tccp_t *l_tccp = 00; |
9653 | |
9654 | /* preconditions */ |
9655 | assert(p_j2k != 00); |
9656 | assert(p_header_size != 00); |
9657 | assert(p_manager != 00); |
9658 | assert(p_data != 00); |
9659 | |
9660 | l_cp = &(p_j2k->m_cp); |
9661 | l_tcp = &l_cp->tcps[p_tile_no]; |
9662 | l_tccp = &l_tcp->tccps[p_comp_no]; |
9663 | |
9664 | /* preconditions again */ |
9665 | assert(p_tile_no < (l_cp->tw * l_cp->th)); |
9666 | assert(p_comp_no < (p_j2k->m_private_image->numcomps)); |
9667 | |
9668 | if (*p_header_size < 5) { |
9669 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n" ); |
9670 | return OPJ_FALSE; |
9671 | } |
9672 | |
9673 | opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ |
9674 | ++p_data; |
9675 | |
9676 | opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */ |
9677 | ++p_data; |
9678 | |
9679 | opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */ |
9680 | ++p_data; |
9681 | |
9682 | opj_write_bytes(p_data, l_tccp->cblksty, |
9683 | 1); /* SPcoc (G) */ |
9684 | ++p_data; |
9685 | |
9686 | opj_write_bytes(p_data, l_tccp->qmfbid, |
9687 | 1); /* SPcoc (H) */ |
9688 | ++p_data; |
9689 | |
9690 | *p_header_size = *p_header_size - 5; |
9691 | |
9692 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
9693 | |
9694 | if (*p_header_size < l_tccp->numresolutions) { |
9695 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n" ); |
9696 | return OPJ_FALSE; |
9697 | } |
9698 | |
9699 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
9700 | opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4), |
9701 | 1); /* SPcoc (I_i) */ |
9702 | ++p_data; |
9703 | } |
9704 | |
9705 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
9706 | } |
9707 | |
9708 | return OPJ_TRUE; |
9709 | } |
9710 | |
9711 | static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, |
9712 | OPJ_UINT32 compno, |
9713 | OPJ_BYTE * , |
9714 | OPJ_UINT32 * , |
9715 | opj_event_mgr_t * p_manager) |
9716 | { |
9717 | OPJ_UINT32 i, l_tmp; |
9718 | opj_cp_t *l_cp = NULL; |
9719 | opj_tcp_t *l_tcp = NULL; |
9720 | opj_tccp_t *l_tccp = NULL; |
9721 | OPJ_BYTE * l_current_ptr = NULL; |
9722 | |
9723 | /* preconditions */ |
9724 | assert(p_j2k != 00); |
9725 | assert(p_manager != 00); |
9726 | assert(p_header_data != 00); |
9727 | |
9728 | l_cp = &(p_j2k->m_cp); |
9729 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? |
9730 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
9731 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
9732 | |
9733 | /* precondition again */ |
9734 | assert(compno < p_j2k->m_private_image->numcomps); |
9735 | |
9736 | l_tccp = &l_tcp->tccps[compno]; |
9737 | l_current_ptr = p_header_data; |
9738 | |
9739 | /* make sure room is sufficient */ |
9740 | if (*p_header_size < 5) { |
9741 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n" ); |
9742 | return OPJ_FALSE; |
9743 | } |
9744 | |
9745 | opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, |
9746 | 1); /* SPcox (D) */ |
9747 | ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ |
9748 | if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { |
9749 | opj_event_msg(p_manager, EVT_ERROR, |
9750 | "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n" , |
9751 | l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); |
9752 | return OPJ_FALSE; |
9753 | } |
9754 | ++l_current_ptr; |
9755 | |
9756 | /* If user wants to remove more resolutions than the codestream contains, return error */ |
9757 | if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { |
9758 | opj_event_msg(p_manager, EVT_ERROR, |
9759 | "Error decoding component %d.\nThe number of resolutions " |
9760 | "to remove (%d) is greater or equal than the number " |
9761 | "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n" , |
9762 | compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions); |
9763 | p_j2k->m_specific_param.m_decoder.m_state |= |
9764 | 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ |
9765 | return OPJ_FALSE; |
9766 | } |
9767 | |
9768 | opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); /* SPcoc (E) */ |
9769 | ++l_current_ptr; |
9770 | l_tccp->cblkw += 2; |
9771 | |
9772 | opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); /* SPcoc (F) */ |
9773 | ++l_current_ptr; |
9774 | l_tccp->cblkh += 2; |
9775 | |
9776 | if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || |
9777 | ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { |
9778 | opj_event_msg(p_manager, EVT_ERROR, |
9779 | "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n" ); |
9780 | return OPJ_FALSE; |
9781 | } |
9782 | |
9783 | |
9784 | opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); /* SPcoc (G) */ |
9785 | ++l_current_ptr; |
9786 | if (l_tccp->cblksty & 0xC0U) { /* 2 msb are reserved, assume we can't read */ |
9787 | opj_event_msg(p_manager, EVT_ERROR, |
9788 | "Error reading SPCod SPCoc element, Invalid code-block style found\n" ); |
9789 | return OPJ_FALSE; |
9790 | } |
9791 | |
9792 | opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); /* SPcoc (H) */ |
9793 | ++l_current_ptr; |
9794 | |
9795 | *p_header_size = *p_header_size - 5; |
9796 | |
9797 | /* use custom precinct size ? */ |
9798 | if (l_tccp->csty & J2K_CCP_CSTY_PRT) { |
9799 | if (*p_header_size < l_tccp->numresolutions) { |
9800 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n" ); |
9801 | return OPJ_FALSE; |
9802 | } |
9803 | |
9804 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
9805 | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPcoc (I_i) */ |
9806 | ++l_current_ptr; |
9807 | /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ |
9808 | if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { |
9809 | opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n" ); |
9810 | return OPJ_FALSE; |
9811 | } |
9812 | l_tccp->prcw[i] = l_tmp & 0xf; |
9813 | l_tccp->prch[i] = l_tmp >> 4; |
9814 | } |
9815 | |
9816 | *p_header_size = *p_header_size - l_tccp->numresolutions; |
9817 | } else { |
9818 | /* set default size for the precinct width and height */ |
9819 | for (i = 0; i < l_tccp->numresolutions; ++i) { |
9820 | l_tccp->prcw[i] = 15; |
9821 | l_tccp->prch[i] = 15; |
9822 | } |
9823 | } |
9824 | |
9825 | #ifdef WIP_REMOVE_MSD |
9826 | /* INDEX >> */ |
9827 | if (p_j2k->cstr_info && compno == 0) { |
9828 | OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); |
9829 | |
9830 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = |
9831 | l_tccp->cblkh; |
9832 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = |
9833 | l_tccp->cblkw; |
9834 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions |
9835 | = l_tccp->numresolutions; |
9836 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = |
9837 | l_tccp->cblksty; |
9838 | p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = |
9839 | l_tccp->qmfbid; |
9840 | |
9841 | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw, |
9842 | l_data_size); |
9843 | memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch, |
9844 | l_data_size); |
9845 | } |
9846 | /* << INDEX */ |
9847 | #endif |
9848 | |
9849 | return OPJ_TRUE; |
9850 | } |
9851 | |
9852 | static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k) |
9853 | { |
9854 | /* loop */ |
9855 | OPJ_UINT32 i; |
9856 | opj_cp_t *l_cp = NULL; |
9857 | opj_tcp_t *l_tcp = NULL; |
9858 | opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; |
9859 | OPJ_UINT32 l_prc_size; |
9860 | |
9861 | /* preconditions */ |
9862 | assert(p_j2k != 00); |
9863 | |
9864 | l_cp = &(p_j2k->m_cp); |
9865 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
9866 | ? |
9867 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
9868 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
9869 | |
9870 | l_ref_tccp = &l_tcp->tccps[0]; |
9871 | l_copied_tccp = l_ref_tccp + 1; |
9872 | l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); |
9873 | |
9874 | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
9875 | l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; |
9876 | l_copied_tccp->cblkw = l_ref_tccp->cblkw; |
9877 | l_copied_tccp->cblkh = l_ref_tccp->cblkh; |
9878 | l_copied_tccp->cblksty = l_ref_tccp->cblksty; |
9879 | l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; |
9880 | memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size); |
9881 | memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size); |
9882 | ++l_copied_tccp; |
9883 | } |
9884 | } |
9885 | |
9886 | static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, |
9887 | OPJ_UINT32 p_tile_no, |
9888 | OPJ_UINT32 p_comp_no) |
9889 | { |
9890 | OPJ_UINT32 l_num_bands; |
9891 | |
9892 | opj_cp_t *l_cp = 00; |
9893 | opj_tcp_t *l_tcp = 00; |
9894 | opj_tccp_t *l_tccp = 00; |
9895 | |
9896 | /* preconditions */ |
9897 | assert(p_j2k != 00); |
9898 | |
9899 | l_cp = &(p_j2k->m_cp); |
9900 | l_tcp = &l_cp->tcps[p_tile_no]; |
9901 | l_tccp = &l_tcp->tccps[p_comp_no]; |
9902 | |
9903 | /* preconditions again */ |
9904 | assert(p_tile_no < l_cp->tw * l_cp->th); |
9905 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
9906 | |
9907 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
9908 | (l_tccp->numresolutions * 3 - 2); |
9909 | |
9910 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
9911 | return 1 + l_num_bands; |
9912 | } else { |
9913 | return 1 + 2 * l_num_bands; |
9914 | } |
9915 | } |
9916 | |
9917 | static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, |
9918 | OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) |
9919 | { |
9920 | opj_cp_t *l_cp = NULL; |
9921 | opj_tcp_t *l_tcp = NULL; |
9922 | opj_tccp_t *l_tccp0 = NULL; |
9923 | opj_tccp_t *l_tccp1 = NULL; |
9924 | OPJ_UINT32 l_band_no, l_num_bands; |
9925 | |
9926 | /* preconditions */ |
9927 | assert(p_j2k != 00); |
9928 | |
9929 | l_cp = &(p_j2k->m_cp); |
9930 | l_tcp = &l_cp->tcps[p_tile_no]; |
9931 | l_tccp0 = &l_tcp->tccps[p_first_comp_no]; |
9932 | l_tccp1 = &l_tcp->tccps[p_second_comp_no]; |
9933 | |
9934 | if (l_tccp0->qntsty != l_tccp1->qntsty) { |
9935 | return OPJ_FALSE; |
9936 | } |
9937 | if (l_tccp0->numgbits != l_tccp1->numgbits) { |
9938 | return OPJ_FALSE; |
9939 | } |
9940 | if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
9941 | l_num_bands = 1U; |
9942 | } else { |
9943 | l_num_bands = l_tccp0->numresolutions * 3U - 2U; |
9944 | if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) { |
9945 | return OPJ_FALSE; |
9946 | } |
9947 | } |
9948 | |
9949 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
9950 | if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) { |
9951 | return OPJ_FALSE; |
9952 | } |
9953 | } |
9954 | if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) { |
9955 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
9956 | if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) { |
9957 | return OPJ_FALSE; |
9958 | } |
9959 | } |
9960 | } |
9961 | return OPJ_TRUE; |
9962 | } |
9963 | |
9964 | |
9965 | static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, |
9966 | OPJ_UINT32 p_tile_no, |
9967 | OPJ_UINT32 p_comp_no, |
9968 | OPJ_BYTE * p_data, |
9969 | OPJ_UINT32 * , |
9970 | struct opj_event_mgr * p_manager) |
9971 | { |
9972 | OPJ_UINT32 ; |
9973 | OPJ_UINT32 l_band_no, l_num_bands; |
9974 | OPJ_UINT32 l_expn, l_mant; |
9975 | |
9976 | opj_cp_t *l_cp = 00; |
9977 | opj_tcp_t *l_tcp = 00; |
9978 | opj_tccp_t *l_tccp = 00; |
9979 | |
9980 | /* preconditions */ |
9981 | assert(p_j2k != 00); |
9982 | assert(p_header_size != 00); |
9983 | assert(p_manager != 00); |
9984 | assert(p_data != 00); |
9985 | |
9986 | l_cp = &(p_j2k->m_cp); |
9987 | l_tcp = &l_cp->tcps[p_tile_no]; |
9988 | l_tccp = &l_tcp->tccps[p_comp_no]; |
9989 | |
9990 | /* preconditions again */ |
9991 | assert(p_tile_no < l_cp->tw * l_cp->th); |
9992 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
9993 | |
9994 | l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
9995 | (l_tccp->numresolutions * 3 - 2); |
9996 | |
9997 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
9998 | l_header_size = 1 + l_num_bands; |
9999 | |
10000 | if (*p_header_size < l_header_size) { |
10001 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n" ); |
10002 | return OPJ_FALSE; |
10003 | } |
10004 | |
10005 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
10006 | 1); /* Sqcx */ |
10007 | ++p_data; |
10008 | |
10009 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
10010 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
10011 | opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ |
10012 | ++p_data; |
10013 | } |
10014 | } else { |
10015 | l_header_size = 1 + 2 * l_num_bands; |
10016 | |
10017 | if (*p_header_size < l_header_size) { |
10018 | opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n" ); |
10019 | return OPJ_FALSE; |
10020 | } |
10021 | |
10022 | opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), |
10023 | 1); /* Sqcx */ |
10024 | ++p_data; |
10025 | |
10026 | for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { |
10027 | l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; |
10028 | l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; |
10029 | |
10030 | opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ |
10031 | p_data += 2; |
10032 | } |
10033 | } |
10034 | |
10035 | *p_header_size = *p_header_size - l_header_size; |
10036 | |
10037 | return OPJ_TRUE; |
10038 | } |
10039 | |
10040 | static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, |
10041 | OPJ_UINT32 p_comp_no, |
10042 | OPJ_BYTE* , |
10043 | OPJ_UINT32 * , |
10044 | opj_event_mgr_t * p_manager |
10045 | ) |
10046 | { |
10047 | /* loop*/ |
10048 | OPJ_UINT32 l_band_no; |
10049 | opj_cp_t *l_cp = 00; |
10050 | opj_tcp_t *l_tcp = 00; |
10051 | opj_tccp_t *l_tccp = 00; |
10052 | OPJ_BYTE * l_current_ptr = 00; |
10053 | OPJ_UINT32 l_tmp, l_num_band; |
10054 | |
10055 | /* preconditions*/ |
10056 | assert(p_j2k != 00); |
10057 | assert(p_manager != 00); |
10058 | assert(p_header_data != 00); |
10059 | |
10060 | l_cp = &(p_j2k->m_cp); |
10061 | /* come from tile part header or main header ?*/ |
10062 | l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) |
10063 | ? |
10064 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10065 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10066 | |
10067 | /* precondition again*/ |
10068 | assert(p_comp_no < p_j2k->m_private_image->numcomps); |
10069 | |
10070 | l_tccp = &l_tcp->tccps[p_comp_no]; |
10071 | l_current_ptr = p_header_data; |
10072 | |
10073 | if (*p_header_size < 1) { |
10074 | opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n" ); |
10075 | return OPJ_FALSE; |
10076 | } |
10077 | *p_header_size -= 1; |
10078 | |
10079 | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */ |
10080 | ++l_current_ptr; |
10081 | |
10082 | l_tccp->qntsty = l_tmp & 0x1f; |
10083 | l_tccp->numgbits = l_tmp >> 5; |
10084 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
10085 | l_num_band = 1; |
10086 | } else { |
10087 | l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? |
10088 | (*p_header_size) : |
10089 | (*p_header_size) / 2; |
10090 | |
10091 | if (l_num_band > OPJ_J2K_MAXBANDS) { |
10092 | opj_event_msg(p_manager, EVT_WARNING, |
10093 | "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " |
10094 | "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " |
10095 | "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n" , l_num_band, OPJ_J2K_MAXBANDS, |
10096 | OPJ_J2K_MAXBANDS); |
10097 | /*return OPJ_FALSE;*/ |
10098 | } |
10099 | } |
10100 | |
10101 | #ifdef USE_JPWL |
10102 | if (l_cp->correct) { |
10103 | |
10104 | /* if JPWL is on, we check whether there are too many subbands */ |
10105 | if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { |
10106 | opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, |
10107 | "JPWL: bad number of subbands in Sqcx (%d)\n" , |
10108 | l_num_band); |
10109 | if (!JPWL_ASSUME) { |
10110 | opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n" ); |
10111 | return OPJ_FALSE; |
10112 | } |
10113 | /* we try to correct */ |
10114 | l_num_band = 1; |
10115 | opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" |
10116 | "- setting number of bands to %d => HYPOTHESIS!!!\n" , |
10117 | l_num_band); |
10118 | }; |
10119 | |
10120 | }; |
10121 | #endif /* USE_JPWL */ |
10122 | |
10123 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { |
10124 | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
10125 | opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */ |
10126 | ++l_current_ptr; |
10127 | if (l_band_no < OPJ_J2K_MAXBANDS) { |
10128 | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); |
10129 | l_tccp->stepsizes[l_band_no].mant = 0; |
10130 | } |
10131 | } |
10132 | *p_header_size = *p_header_size - l_num_band; |
10133 | } else { |
10134 | for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { |
10135 | opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */ |
10136 | l_current_ptr += 2; |
10137 | if (l_band_no < OPJ_J2K_MAXBANDS) { |
10138 | l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); |
10139 | l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; |
10140 | } |
10141 | } |
10142 | *p_header_size = *p_header_size - 2 * l_num_band; |
10143 | } |
10144 | |
10145 | /* Add Antonin : if scalar_derived -> compute other stepsizes */ |
10146 | if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { |
10147 | for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { |
10148 | l_tccp->stepsizes[l_band_no].expn = |
10149 | ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) |
10150 | ? |
10151 | (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; |
10152 | l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; |
10153 | } |
10154 | } |
10155 | |
10156 | return OPJ_TRUE; |
10157 | } |
10158 | |
10159 | static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k) |
10160 | { |
10161 | OPJ_UINT32 i; |
10162 | opj_cp_t *l_cp = NULL; |
10163 | opj_tcp_t *l_tcp = NULL; |
10164 | opj_tccp_t *l_ref_tccp = NULL; |
10165 | opj_tccp_t *l_copied_tccp = NULL; |
10166 | OPJ_UINT32 l_size; |
10167 | |
10168 | /* preconditions */ |
10169 | assert(p_j2k != 00); |
10170 | |
10171 | l_cp = &(p_j2k->m_cp); |
10172 | l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? |
10173 | &l_cp->tcps[p_j2k->m_current_tile_number] : |
10174 | p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10175 | |
10176 | l_ref_tccp = &l_tcp->tccps[0]; |
10177 | l_copied_tccp = l_ref_tccp + 1; |
10178 | l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); |
10179 | |
10180 | for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { |
10181 | l_copied_tccp->qntsty = l_ref_tccp->qntsty; |
10182 | l_copied_tccp->numgbits = l_ref_tccp->numgbits; |
10183 | memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size); |
10184 | ++l_copied_tccp; |
10185 | } |
10186 | } |
10187 | |
10188 | static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile, |
10189 | OPJ_INT32 numcomps, FILE* out_stream) |
10190 | { |
10191 | if (l_default_tile) { |
10192 | OPJ_INT32 compno; |
10193 | |
10194 | fprintf(out_stream, "\t default tile {\n" ); |
10195 | fprintf(out_stream, "\t\t csty=%#x\n" , l_default_tile->csty); |
10196 | fprintf(out_stream, "\t\t prg=%#x\n" , l_default_tile->prg); |
10197 | fprintf(out_stream, "\t\t numlayers=%d\n" , l_default_tile->numlayers); |
10198 | fprintf(out_stream, "\t\t mct=%x\n" , l_default_tile->mct); |
10199 | |
10200 | for (compno = 0; compno < numcomps; compno++) { |
10201 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
10202 | OPJ_UINT32 resno; |
10203 | OPJ_INT32 bandno, numbands; |
10204 | |
10205 | /* coding style*/ |
10206 | fprintf(out_stream, "\t\t comp %d {\n" , compno); |
10207 | fprintf(out_stream, "\t\t\t csty=%#x\n" , l_tccp->csty); |
10208 | fprintf(out_stream, "\t\t\t numresolutions=%d\n" , l_tccp->numresolutions); |
10209 | fprintf(out_stream, "\t\t\t cblkw=2^%d\n" , l_tccp->cblkw); |
10210 | fprintf(out_stream, "\t\t\t cblkh=2^%d\n" , l_tccp->cblkh); |
10211 | fprintf(out_stream, "\t\t\t cblksty=%#x\n" , l_tccp->cblksty); |
10212 | fprintf(out_stream, "\t\t\t qmfbid=%d\n" , l_tccp->qmfbid); |
10213 | |
10214 | fprintf(out_stream, "\t\t\t preccintsize (w,h)=" ); |
10215 | for (resno = 0; resno < l_tccp->numresolutions; resno++) { |
10216 | fprintf(out_stream, "(%d,%d) " , l_tccp->prcw[resno], l_tccp->prch[resno]); |
10217 | } |
10218 | fprintf(out_stream, "\n" ); |
10219 | |
10220 | /* quantization style*/ |
10221 | fprintf(out_stream, "\t\t\t qntsty=%d\n" , l_tccp->qntsty); |
10222 | fprintf(out_stream, "\t\t\t numgbits=%d\n" , l_tccp->numgbits); |
10223 | fprintf(out_stream, "\t\t\t stepsizes (m,e)=" ); |
10224 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
10225 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
10226 | for (bandno = 0; bandno < numbands; bandno++) { |
10227 | fprintf(out_stream, "(%d,%d) " , l_tccp->stepsizes[bandno].mant, |
10228 | l_tccp->stepsizes[bandno].expn); |
10229 | } |
10230 | fprintf(out_stream, "\n" ); |
10231 | |
10232 | /* RGN value*/ |
10233 | fprintf(out_stream, "\t\t\t roishift=%d\n" , l_tccp->roishift); |
10234 | |
10235 | fprintf(out_stream, "\t\t }\n" ); |
10236 | } /*end of component of default tile*/ |
10237 | fprintf(out_stream, "\t }\n" ); /*end of default tile*/ |
10238 | } |
10239 | } |
10240 | |
10241 | void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) |
10242 | { |
10243 | /* Check if the flag is compatible with j2k file*/ |
10244 | if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) { |
10245 | fprintf(out_stream, "Wrong flag\n" ); |
10246 | return; |
10247 | } |
10248 | |
10249 | /* Dump the image_header */ |
10250 | if (flag & OPJ_IMG_INFO) { |
10251 | if (p_j2k->m_private_image) { |
10252 | j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); |
10253 | } |
10254 | } |
10255 | |
10256 | /* Dump the codestream info from main header */ |
10257 | if (flag & OPJ_J2K_MH_INFO) { |
10258 | if (p_j2k->m_private_image) { |
10259 | opj_j2k_dump_MH_info(p_j2k, out_stream); |
10260 | } |
10261 | } |
10262 | /* Dump all tile/codestream info */ |
10263 | if (flag & OPJ_J2K_TCH_INFO) { |
10264 | OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
10265 | OPJ_UINT32 i; |
10266 | opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; |
10267 | if (p_j2k->m_private_image) { |
10268 | for (i = 0; i < l_nb_tiles; ++i) { |
10269 | opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps, |
10270 | out_stream); |
10271 | ++l_tcp; |
10272 | } |
10273 | } |
10274 | } |
10275 | |
10276 | /* Dump the codestream info of the current tile */ |
10277 | if (flag & OPJ_J2K_TH_INFO) { |
10278 | |
10279 | } |
10280 | |
10281 | /* Dump the codestream index from main header */ |
10282 | if (flag & OPJ_J2K_MH_IND) { |
10283 | opj_j2k_dump_MH_index(p_j2k, out_stream); |
10284 | } |
10285 | |
10286 | /* Dump the codestream index of the current tile */ |
10287 | if (flag & OPJ_J2K_TH_IND) { |
10288 | |
10289 | } |
10290 | |
10291 | } |
10292 | |
10293 | static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) |
10294 | { |
10295 | opj_codestream_index_t* cstr_index = p_j2k->cstr_index; |
10296 | OPJ_UINT32 it_marker, it_tile, it_tile_part; |
10297 | |
10298 | fprintf(out_stream, "Codestream index from main header: {\n" ); |
10299 | |
10300 | fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" |
10301 | "\t Main header end position=%" PRIi64 "\n" , |
10302 | cstr_index->main_head_start, cstr_index->main_head_end); |
10303 | |
10304 | fprintf(out_stream, "\t Marker list: {\n" ); |
10305 | |
10306 | if (cstr_index->marker) { |
10307 | for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) { |
10308 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n" , |
10309 | cstr_index->marker[it_marker].type, |
10310 | cstr_index->marker[it_marker].pos, |
10311 | cstr_index->marker[it_marker].len); |
10312 | } |
10313 | } |
10314 | |
10315 | fprintf(out_stream, "\t }\n" ); |
10316 | |
10317 | if (cstr_index->tile_index) { |
10318 | |
10319 | /* Simple test to avoid to write empty information*/ |
10320 | OPJ_UINT32 l_acc_nb_of_tile_part = 0; |
10321 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
10322 | l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; |
10323 | } |
10324 | |
10325 | if (l_acc_nb_of_tile_part) { |
10326 | fprintf(out_stream, "\t Tile index: {\n" ); |
10327 | |
10328 | for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { |
10329 | OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; |
10330 | |
10331 | fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n" , it_tile, |
10332 | nb_of_tile_part); |
10333 | |
10334 | if (cstr_index->tile_index[it_tile].tp_index) { |
10335 | for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) { |
10336 | fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" |
10337 | PRIi64 ", end_pos=%" PRIi64 ".\n" , |
10338 | it_tile_part, |
10339 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, |
10340 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, |
10341 | cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); |
10342 | } |
10343 | } |
10344 | |
10345 | if (cstr_index->tile_index[it_tile].marker) { |
10346 | for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ; |
10347 | it_marker++) { |
10348 | fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n" , |
10349 | cstr_index->tile_index[it_tile].marker[it_marker].type, |
10350 | cstr_index->tile_index[it_tile].marker[it_marker].pos, |
10351 | cstr_index->tile_index[it_tile].marker[it_marker].len); |
10352 | } |
10353 | } |
10354 | } |
10355 | fprintf(out_stream, "\t }\n" ); |
10356 | } |
10357 | } |
10358 | |
10359 | fprintf(out_stream, "}\n" ); |
10360 | |
10361 | } |
10362 | |
10363 | |
10364 | static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) |
10365 | { |
10366 | |
10367 | fprintf(out_stream, "Codestream info from main header: {\n" ); |
10368 | |
10369 | fprintf(out_stream, "\t tx0=%d, ty0=%d\n" , p_j2k->m_cp.tx0, p_j2k->m_cp.ty0); |
10370 | fprintf(out_stream, "\t tdx=%d, tdy=%d\n" , p_j2k->m_cp.tdx, p_j2k->m_cp.tdy); |
10371 | fprintf(out_stream, "\t tw=%d, th=%d\n" , p_j2k->m_cp.tw, p_j2k->m_cp.th); |
10372 | opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp, |
10373 | (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); |
10374 | fprintf(out_stream, "}\n" ); |
10375 | } |
10376 | |
10377 | void (opj_image_t* , OPJ_BOOL dev_dump_flag, |
10378 | FILE* out_stream) |
10379 | { |
10380 | char tab[2]; |
10381 | |
10382 | if (dev_dump_flag) { |
10383 | fprintf(stdout, "[DEV] Dump an image_header struct {\n" ); |
10384 | tab[0] = '\0'; |
10385 | } else { |
10386 | fprintf(out_stream, "Image info {\n" ); |
10387 | tab[0] = '\t'; |
10388 | tab[1] = '\0'; |
10389 | } |
10390 | |
10391 | fprintf(out_stream, "%s x0=%d, y0=%d\n" , tab, img_header->x0, img_header->y0); |
10392 | fprintf(out_stream, "%s x1=%d, y1=%d\n" , tab, img_header->x1, |
10393 | img_header->y1); |
10394 | fprintf(out_stream, "%s numcomps=%d\n" , tab, img_header->numcomps); |
10395 | |
10396 | if (img_header->comps) { |
10397 | OPJ_UINT32 compno; |
10398 | for (compno = 0; compno < img_header->numcomps; compno++) { |
10399 | fprintf(out_stream, "%s\t component %d {\n" , tab, compno); |
10400 | j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, |
10401 | out_stream); |
10402 | fprintf(out_stream, "%s}\n" , tab); |
10403 | } |
10404 | } |
10405 | |
10406 | fprintf(out_stream, "}\n" ); |
10407 | } |
10408 | |
10409 | void (opj_image_comp_t* , |
10410 | OPJ_BOOL dev_dump_flag, FILE* out_stream) |
10411 | { |
10412 | char tab[3]; |
10413 | |
10414 | if (dev_dump_flag) { |
10415 | fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n" ); |
10416 | tab[0] = '\0'; |
10417 | } else { |
10418 | tab[0] = '\t'; |
10419 | tab[1] = '\t'; |
10420 | tab[2] = '\0'; |
10421 | } |
10422 | |
10423 | fprintf(out_stream, "%s dx=%d, dy=%d\n" , tab, comp_header->dx, comp_header->dy); |
10424 | fprintf(out_stream, "%s prec=%d\n" , tab, comp_header->prec); |
10425 | fprintf(out_stream, "%s sgnd=%d\n" , tab, comp_header->sgnd); |
10426 | |
10427 | if (dev_dump_flag) { |
10428 | fprintf(out_stream, "}\n" ); |
10429 | } |
10430 | } |
10431 | |
10432 | opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) |
10433 | { |
10434 | OPJ_UINT32 compno; |
10435 | OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; |
10436 | opj_tcp_t *l_default_tile; |
10437 | opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1, |
10438 | sizeof(opj_codestream_info_v2_t)); |
10439 | if (!cstr_info) { |
10440 | return NULL; |
10441 | } |
10442 | |
10443 | cstr_info->nbcomps = p_j2k->m_private_image->numcomps; |
10444 | |
10445 | cstr_info->tx0 = p_j2k->m_cp.tx0; |
10446 | cstr_info->ty0 = p_j2k->m_cp.ty0; |
10447 | cstr_info->tdx = p_j2k->m_cp.tdx; |
10448 | cstr_info->tdy = p_j2k->m_cp.tdy; |
10449 | cstr_info->tw = p_j2k->m_cp.tw; |
10450 | cstr_info->th = p_j2k->m_cp.th; |
10451 | |
10452 | cstr_info->tile_info = NULL; /* Not fill from the main header*/ |
10453 | |
10454 | l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; |
10455 | |
10456 | cstr_info->m_default_tile_info.csty = l_default_tile->csty; |
10457 | cstr_info->m_default_tile_info.prg = l_default_tile->prg; |
10458 | cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; |
10459 | cstr_info->m_default_tile_info.mct = l_default_tile->mct; |
10460 | |
10461 | cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc( |
10462 | cstr_info->nbcomps, sizeof(opj_tccp_info_t)); |
10463 | if (!cstr_info->m_default_tile_info.tccp_info) { |
10464 | opj_destroy_cstr_info(&cstr_info); |
10465 | return NULL; |
10466 | } |
10467 | |
10468 | for (compno = 0; compno < numcomps; compno++) { |
10469 | opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); |
10470 | opj_tccp_info_t *l_tccp_info = & |
10471 | (cstr_info->m_default_tile_info.tccp_info[compno]); |
10472 | OPJ_INT32 bandno, numbands; |
10473 | |
10474 | /* coding style*/ |
10475 | l_tccp_info->csty = l_tccp->csty; |
10476 | l_tccp_info->numresolutions = l_tccp->numresolutions; |
10477 | l_tccp_info->cblkw = l_tccp->cblkw; |
10478 | l_tccp_info->cblkh = l_tccp->cblkh; |
10479 | l_tccp_info->cblksty = l_tccp->cblksty; |
10480 | l_tccp_info->qmfbid = l_tccp->qmfbid; |
10481 | if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) { |
10482 | memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); |
10483 | memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); |
10484 | } |
10485 | |
10486 | /* quantization style*/ |
10487 | l_tccp_info->qntsty = l_tccp->qntsty; |
10488 | l_tccp_info->numgbits = l_tccp->numgbits; |
10489 | |
10490 | numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : |
10491 | (OPJ_INT32)l_tccp->numresolutions * 3 - 2; |
10492 | if (numbands < OPJ_J2K_MAXBANDS) { |
10493 | for (bandno = 0; bandno < numbands; bandno++) { |
10494 | l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32) |
10495 | l_tccp->stepsizes[bandno].mant; |
10496 | l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32) |
10497 | l_tccp->stepsizes[bandno].expn; |
10498 | } |
10499 | } |
10500 | |
10501 | /* RGN value*/ |
10502 | l_tccp_info->roishift = l_tccp->roishift; |
10503 | } |
10504 | |
10505 | return cstr_info; |
10506 | } |
10507 | |
10508 | opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) |
10509 | { |
10510 | opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) |
10511 | opj_calloc(1, sizeof(opj_codestream_index_t)); |
10512 | if (!l_cstr_index) { |
10513 | return NULL; |
10514 | } |
10515 | |
10516 | l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; |
10517 | l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; |
10518 | l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; |
10519 | |
10520 | l_cstr_index->marknum = p_j2k->cstr_index->marknum; |
10521 | l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum * |
10522 | sizeof(opj_marker_info_t)); |
10523 | if (!l_cstr_index->marker) { |
10524 | opj_free(l_cstr_index); |
10525 | return NULL; |
10526 | } |
10527 | |
10528 | if (p_j2k->cstr_index->marker) { |
10529 | memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, |
10530 | l_cstr_index->marknum * sizeof(opj_marker_info_t)); |
10531 | } else { |
10532 | opj_free(l_cstr_index->marker); |
10533 | l_cstr_index->marker = NULL; |
10534 | } |
10535 | |
10536 | l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; |
10537 | l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
10538 | l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
10539 | if (!l_cstr_index->tile_index) { |
10540 | opj_free(l_cstr_index->marker); |
10541 | opj_free(l_cstr_index); |
10542 | return NULL; |
10543 | } |
10544 | |
10545 | if (!p_j2k->cstr_index->tile_index) { |
10546 | opj_free(l_cstr_index->tile_index); |
10547 | l_cstr_index->tile_index = NULL; |
10548 | } else { |
10549 | OPJ_UINT32 it_tile = 0; |
10550 | for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) { |
10551 | |
10552 | /* Tile Marker*/ |
10553 | l_cstr_index->tile_index[it_tile].marknum = |
10554 | p_j2k->cstr_index->tile_index[it_tile].marknum; |
10555 | |
10556 | l_cstr_index->tile_index[it_tile].marker = |
10557 | (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum * |
10558 | sizeof(opj_marker_info_t)); |
10559 | |
10560 | if (!l_cstr_index->tile_index[it_tile].marker) { |
10561 | OPJ_UINT32 it_tile_free; |
10562 | |
10563 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
10564 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
10565 | } |
10566 | |
10567 | opj_free(l_cstr_index->tile_index); |
10568 | opj_free(l_cstr_index->marker); |
10569 | opj_free(l_cstr_index); |
10570 | return NULL; |
10571 | } |
10572 | |
10573 | if (p_j2k->cstr_index->tile_index[it_tile].marker) |
10574 | memcpy(l_cstr_index->tile_index[it_tile].marker, |
10575 | p_j2k->cstr_index->tile_index[it_tile].marker, |
10576 | l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t)); |
10577 | else { |
10578 | opj_free(l_cstr_index->tile_index[it_tile].marker); |
10579 | l_cstr_index->tile_index[it_tile].marker = NULL; |
10580 | } |
10581 | |
10582 | /* Tile part index*/ |
10583 | l_cstr_index->tile_index[it_tile].nb_tps = |
10584 | p_j2k->cstr_index->tile_index[it_tile].nb_tps; |
10585 | |
10586 | l_cstr_index->tile_index[it_tile].tp_index = |
10587 | (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof( |
10588 | opj_tp_index_t)); |
10589 | |
10590 | if (!l_cstr_index->tile_index[it_tile].tp_index) { |
10591 | OPJ_UINT32 it_tile_free; |
10592 | |
10593 | for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { |
10594 | opj_free(l_cstr_index->tile_index[it_tile_free].marker); |
10595 | opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); |
10596 | } |
10597 | |
10598 | opj_free(l_cstr_index->tile_index); |
10599 | opj_free(l_cstr_index->marker); |
10600 | opj_free(l_cstr_index); |
10601 | return NULL; |
10602 | } |
10603 | |
10604 | if (p_j2k->cstr_index->tile_index[it_tile].tp_index) { |
10605 | memcpy(l_cstr_index->tile_index[it_tile].tp_index, |
10606 | p_j2k->cstr_index->tile_index[it_tile].tp_index, |
10607 | l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t)); |
10608 | } else { |
10609 | opj_free(l_cstr_index->tile_index[it_tile].tp_index); |
10610 | l_cstr_index->tile_index[it_tile].tp_index = NULL; |
10611 | } |
10612 | |
10613 | /* Packet index (NOT USED)*/ |
10614 | l_cstr_index->tile_index[it_tile].nb_packet = 0; |
10615 | l_cstr_index->tile_index[it_tile].packet_index = NULL; |
10616 | |
10617 | } |
10618 | } |
10619 | |
10620 | return l_cstr_index; |
10621 | } |
10622 | |
10623 | static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) |
10624 | { |
10625 | OPJ_UINT32 it_tile = 0; |
10626 | |
10627 | p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
10628 | p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( |
10629 | p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); |
10630 | if (!p_j2k->cstr_index->tile_index) { |
10631 | return OPJ_FALSE; |
10632 | } |
10633 | |
10634 | for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) { |
10635 | p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; |
10636 | p_j2k->cstr_index->tile_index[it_tile].marknum = 0; |
10637 | p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) |
10638 | opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, |
10639 | sizeof(opj_marker_info_t)); |
10640 | if (!p_j2k->cstr_index->tile_index[it_tile].marker) { |
10641 | return OPJ_FALSE; |
10642 | } |
10643 | } |
10644 | |
10645 | return OPJ_TRUE; |
10646 | } |
10647 | |
10648 | static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
10649 | opj_stream_private_t *p_stream, |
10650 | opj_event_mgr_t * p_manager) |
10651 | { |
10652 | OPJ_BOOL l_go_on = OPJ_TRUE; |
10653 | OPJ_UINT32 l_current_tile_no; |
10654 | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
10655 | OPJ_UINT32 l_nb_comps; |
10656 | OPJ_UINT32 nr_tiles = 0; |
10657 | |
10658 | /* Particular case for whole single tile decoding */ |
10659 | /* We can avoid allocating intermediate tile buffers */ |
10660 | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10661 | p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 && |
10662 | p_j2k->m_output_image->x0 == 0 && |
10663 | p_j2k->m_output_image->y0 == 0 && |
10664 | p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx && |
10665 | p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) { |
10666 | OPJ_UINT32 i; |
10667 | if (! opj_j2k_read_tile_header(p_j2k, |
10668 | &l_current_tile_no, |
10669 | NULL, |
10670 | &l_tile_x0, &l_tile_y0, |
10671 | &l_tile_x1, &l_tile_y1, |
10672 | &l_nb_comps, |
10673 | &l_go_on, |
10674 | p_stream, |
10675 | p_manager)) { |
10676 | return OPJ_FALSE; |
10677 | } |
10678 | |
10679 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
10680 | p_stream, p_manager)) { |
10681 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n" ); |
10682 | return OPJ_FALSE; |
10683 | } |
10684 | |
10685 | /* Transfer TCD data to output image data */ |
10686 | for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { |
10687 | opj_image_data_free(p_j2k->m_output_image->comps[i].data); |
10688 | p_j2k->m_output_image->comps[i].data = |
10689 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data; |
10690 | p_j2k->m_output_image->comps[i].resno_decoded = |
10691 | p_j2k->m_tcd->image->comps[i].resno_decoded; |
10692 | p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL; |
10693 | } |
10694 | |
10695 | return OPJ_TRUE; |
10696 | } |
10697 | |
10698 | for (;;) { |
10699 | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10700 | p_j2k->m_cp.tcps[0].m_data != NULL) { |
10701 | l_current_tile_no = 0; |
10702 | p_j2k->m_current_tile_number = 0; |
10703 | p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; |
10704 | } else { |
10705 | if (! opj_j2k_read_tile_header(p_j2k, |
10706 | &l_current_tile_no, |
10707 | NULL, |
10708 | &l_tile_x0, &l_tile_y0, |
10709 | &l_tile_x1, &l_tile_y1, |
10710 | &l_nb_comps, |
10711 | &l_go_on, |
10712 | p_stream, |
10713 | p_manager)) { |
10714 | return OPJ_FALSE; |
10715 | } |
10716 | |
10717 | if (! l_go_on) { |
10718 | break; |
10719 | } |
10720 | } |
10721 | |
10722 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
10723 | p_stream, p_manager)) { |
10724 | opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n" , |
10725 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
10726 | return OPJ_FALSE; |
10727 | } |
10728 | |
10729 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n" , |
10730 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
10731 | |
10732 | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
10733 | p_j2k->m_output_image)) { |
10734 | return OPJ_FALSE; |
10735 | } |
10736 | |
10737 | if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && |
10738 | !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 && |
10739 | p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 && |
10740 | p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 && |
10741 | p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) { |
10742 | /* Keep current tcp data */ |
10743 | } else { |
10744 | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
10745 | } |
10746 | |
10747 | opj_event_msg(p_manager, EVT_INFO, |
10748 | "Image data has been updated with tile %d.\n\n" , l_current_tile_no + 1); |
10749 | |
10750 | if (opj_stream_get_number_byte_left(p_stream) == 0 |
10751 | && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
10752 | break; |
10753 | } |
10754 | if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) { |
10755 | break; |
10756 | } |
10757 | } |
10758 | |
10759 | return OPJ_TRUE; |
10760 | } |
10761 | |
10762 | /** |
10763 | * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures. |
10764 | */ |
10765 | static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k, |
10766 | opj_event_mgr_t * p_manager) |
10767 | { |
10768 | /* preconditions*/ |
10769 | assert(p_j2k != 00); |
10770 | assert(p_manager != 00); |
10771 | |
10772 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
10773 | (opj_procedure)opj_j2k_decode_tiles, p_manager)) { |
10774 | return OPJ_FALSE; |
10775 | } |
10776 | /* DEVELOPER CORNER, add your custom procedures */ |
10777 | |
10778 | return OPJ_TRUE; |
10779 | } |
10780 | |
10781 | /* |
10782 | * Read and decode one tile. |
10783 | */ |
10784 | static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k, |
10785 | opj_stream_private_t *p_stream, |
10786 | opj_event_mgr_t * p_manager) |
10787 | { |
10788 | OPJ_BOOL l_go_on = OPJ_TRUE; |
10789 | OPJ_UINT32 l_current_tile_no; |
10790 | OPJ_UINT32 l_tile_no_to_dec; |
10791 | OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; |
10792 | OPJ_UINT32 l_nb_comps; |
10793 | OPJ_UINT32 l_nb_tiles; |
10794 | OPJ_UINT32 i; |
10795 | |
10796 | /*Allocate and initialize some elements of codestrem index if not already done*/ |
10797 | if (!p_j2k->cstr_index->tile_index) { |
10798 | if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { |
10799 | return OPJ_FALSE; |
10800 | } |
10801 | } |
10802 | /* Move into the codestream to the first SOT used to decode the desired tile */ |
10803 | l_tile_no_to_dec = (OPJ_UINT32) |
10804 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; |
10805 | if (p_j2k->cstr_index->tile_index) |
10806 | if (p_j2k->cstr_index->tile_index->tp_index) { |
10807 | if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { |
10808 | /* the index for this tile has not been built, |
10809 | * so move to the last SOT read */ |
10810 | if (!(opj_stream_read_seek(p_stream, |
10811 | p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) { |
10812 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n" ); |
10813 | return OPJ_FALSE; |
10814 | } |
10815 | } else { |
10816 | if (!(opj_stream_read_seek(p_stream, |
10817 | p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2, |
10818 | p_manager))) { |
10819 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n" ); |
10820 | return OPJ_FALSE; |
10821 | } |
10822 | } |
10823 | /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ |
10824 | if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { |
10825 | p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; |
10826 | } |
10827 | } |
10828 | |
10829 | /* Reset current tile part number for all tiles, and not only the one */ |
10830 | /* of interest. */ |
10831 | /* Not completely sure this is always correct but required for */ |
10832 | /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */ |
10833 | l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; |
10834 | for (i = 0; i < l_nb_tiles; ++i) { |
10835 | p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1; |
10836 | } |
10837 | |
10838 | for (;;) { |
10839 | if (! opj_j2k_read_tile_header(p_j2k, |
10840 | &l_current_tile_no, |
10841 | NULL, |
10842 | &l_tile_x0, &l_tile_y0, |
10843 | &l_tile_x1, &l_tile_y1, |
10844 | &l_nb_comps, |
10845 | &l_go_on, |
10846 | p_stream, |
10847 | p_manager)) { |
10848 | return OPJ_FALSE; |
10849 | } |
10850 | |
10851 | if (! l_go_on) { |
10852 | break; |
10853 | } |
10854 | |
10855 | if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, |
10856 | p_stream, p_manager)) { |
10857 | return OPJ_FALSE; |
10858 | } |
10859 | opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n" , |
10860 | l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); |
10861 | |
10862 | if (! opj_j2k_update_image_data(p_j2k->m_tcd, |
10863 | p_j2k->m_output_image)) { |
10864 | return OPJ_FALSE; |
10865 | } |
10866 | opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); |
10867 | |
10868 | opj_event_msg(p_manager, EVT_INFO, |
10869 | "Image data has been updated with tile %d.\n\n" , l_current_tile_no + 1); |
10870 | |
10871 | if (l_current_tile_no == l_tile_no_to_dec) { |
10872 | /* move into the codestream to the first SOT (FIXME or not move?)*/ |
10873 | if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, |
10874 | p_manager))) { |
10875 | opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n" ); |
10876 | return OPJ_FALSE; |
10877 | } |
10878 | break; |
10879 | } else { |
10880 | opj_event_msg(p_manager, EVT_WARNING, |
10881 | "Tile read, decoded and updated is not the desired one (%d vs %d).\n" , |
10882 | l_current_tile_no + 1, l_tile_no_to_dec + 1); |
10883 | } |
10884 | |
10885 | } |
10886 | |
10887 | return OPJ_TRUE; |
10888 | } |
10889 | |
10890 | /** |
10891 | * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures. |
10892 | */ |
10893 | static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k, |
10894 | opj_event_mgr_t * p_manager) |
10895 | { |
10896 | /* preconditions*/ |
10897 | assert(p_j2k != 00); |
10898 | assert(p_manager != 00); |
10899 | |
10900 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
10901 | (opj_procedure)opj_j2k_decode_one_tile, p_manager)) { |
10902 | return OPJ_FALSE; |
10903 | } |
10904 | /* DEVELOPER CORNER, add your custom procedures */ |
10905 | |
10906 | return OPJ_TRUE; |
10907 | } |
10908 | |
10909 | static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k, |
10910 | opj_image_t * p_image) |
10911 | { |
10912 | OPJ_UINT32 compno; |
10913 | |
10914 | /* Move data and copy one information from codec to output image*/ |
10915 | if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) { |
10916 | opj_image_comp_t* newcomps = |
10917 | (opj_image_comp_t*) opj_malloc( |
10918 | p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode * |
10919 | sizeof(opj_image_comp_t)); |
10920 | if (newcomps == NULL) { |
10921 | opj_image_destroy(p_j2k->m_private_image); |
10922 | p_j2k->m_private_image = NULL; |
10923 | return OPJ_FALSE; |
10924 | } |
10925 | for (compno = 0; compno < p_image->numcomps; compno++) { |
10926 | opj_image_data_free(p_image->comps[compno].data); |
10927 | p_image->comps[compno].data = NULL; |
10928 | } |
10929 | for (compno = 0; |
10930 | compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { |
10931 | OPJ_UINT32 src_compno = |
10932 | p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; |
10933 | memcpy(&(newcomps[compno]), |
10934 | &(p_j2k->m_output_image->comps[src_compno]), |
10935 | sizeof(opj_image_comp_t)); |
10936 | newcomps[compno].resno_decoded = |
10937 | p_j2k->m_output_image->comps[src_compno].resno_decoded; |
10938 | newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data; |
10939 | p_j2k->m_output_image->comps[src_compno].data = NULL; |
10940 | } |
10941 | for (compno = 0; compno < p_image->numcomps; compno++) { |
10942 | assert(p_j2k->m_output_image->comps[compno].data == NULL); |
10943 | opj_image_data_free(p_j2k->m_output_image->comps[compno].data); |
10944 | p_j2k->m_output_image->comps[compno].data = NULL; |
10945 | } |
10946 | p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; |
10947 | opj_free(p_image->comps); |
10948 | p_image->comps = newcomps; |
10949 | } else { |
10950 | for (compno = 0; compno < p_image->numcomps; compno++) { |
10951 | p_image->comps[compno].resno_decoded = |
10952 | p_j2k->m_output_image->comps[compno].resno_decoded; |
10953 | opj_image_data_free(p_image->comps[compno].data); |
10954 | p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; |
10955 | #if 0 |
10956 | char fn[256]; |
10957 | sprintf(fn, "/tmp/%d.raw" , compno); |
10958 | FILE *debug = fopen(fn, "wb" ); |
10959 | fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32), |
10960 | p_image->comps[compno].w * p_image->comps[compno].h, debug); |
10961 | fclose(debug); |
10962 | #endif |
10963 | p_j2k->m_output_image->comps[compno].data = NULL; |
10964 | } |
10965 | } |
10966 | return OPJ_TRUE; |
10967 | } |
10968 | |
10969 | OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, |
10970 | opj_stream_private_t * p_stream, |
10971 | opj_image_t * p_image, |
10972 | opj_event_mgr_t * p_manager) |
10973 | { |
10974 | if (!p_image) { |
10975 | return OPJ_FALSE; |
10976 | } |
10977 | |
10978 | /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */ |
10979 | /* and finally opj_decode_image() without manual setting of comps[].factor */ |
10980 | /* We could potentially always execute it, if we don't allow people to do */ |
10981 | /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */ |
10982 | if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 && |
10983 | p_j2k->m_private_image != NULL && |
10984 | p_j2k->m_private_image->numcomps > 0 && |
10985 | p_j2k->m_private_image->comps[0].factor == |
10986 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce && |
10987 | p_image->numcomps > 0 && |
10988 | p_image->comps[0].factor == 0 && |
10989 | /* Don't mess with image dimension if the user has allocated it */ |
10990 | p_image->comps[0].data == NULL) { |
10991 | OPJ_UINT32 it_comp; |
10992 | |
10993 | /* Update the comps[].factor member of the output image with the one */ |
10994 | /* of m_reduce */ |
10995 | for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { |
10996 | p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; |
10997 | } |
10998 | if (!opj_j2k_update_image_dimensions(p_image, p_manager)) { |
10999 | return OPJ_FALSE; |
11000 | } |
11001 | } |
11002 | |
11003 | if (p_j2k->m_output_image == NULL) { |
11004 | p_j2k->m_output_image = opj_image_create0(); |
11005 | if (!(p_j2k->m_output_image)) { |
11006 | return OPJ_FALSE; |
11007 | } |
11008 | } |
11009 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
11010 | |
11011 | /* customization of the decoding */ |
11012 | if (!opj_j2k_setup_decoding(p_j2k, p_manager)) { |
11013 | return OPJ_FALSE; |
11014 | } |
11015 | |
11016 | /* Decode the codestream */ |
11017 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
11018 | opj_image_destroy(p_j2k->m_private_image); |
11019 | p_j2k->m_private_image = NULL; |
11020 | return OPJ_FALSE; |
11021 | } |
11022 | |
11023 | /* Move data and copy one information from codec to output image*/ |
11024 | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
11025 | } |
11026 | |
11027 | OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, |
11028 | opj_stream_private_t *p_stream, |
11029 | opj_image_t* p_image, |
11030 | opj_event_mgr_t * p_manager, |
11031 | OPJ_UINT32 tile_index) |
11032 | { |
11033 | OPJ_UINT32 compno; |
11034 | OPJ_UINT32 l_tile_x, l_tile_y; |
11035 | opj_image_comp_t* l_img_comp; |
11036 | |
11037 | if (!p_image) { |
11038 | opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n" ); |
11039 | return OPJ_FALSE; |
11040 | } |
11041 | |
11042 | if (p_image->numcomps < p_j2k->m_private_image->numcomps) { |
11043 | opj_event_msg(p_manager, EVT_ERROR, |
11044 | "Image has less components than codestream.\n" ); |
11045 | return OPJ_FALSE; |
11046 | } |
11047 | |
11048 | if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) { |
11049 | opj_event_msg(p_manager, EVT_ERROR, |
11050 | "Tile index provided by the user is incorrect %d (max = %d) \n" , tile_index, |
11051 | (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); |
11052 | return OPJ_FALSE; |
11053 | } |
11054 | |
11055 | /* Compute the dimension of the desired tile*/ |
11056 | l_tile_x = tile_index % p_j2k->m_cp.tw; |
11057 | l_tile_y = tile_index / p_j2k->m_cp.tw; |
11058 | |
11059 | p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
11060 | if (p_image->x0 < p_j2k->m_private_image->x0) { |
11061 | p_image->x0 = p_j2k->m_private_image->x0; |
11062 | } |
11063 | p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; |
11064 | if (p_image->x1 > p_j2k->m_private_image->x1) { |
11065 | p_image->x1 = p_j2k->m_private_image->x1; |
11066 | } |
11067 | |
11068 | p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
11069 | if (p_image->y0 < p_j2k->m_private_image->y0) { |
11070 | p_image->y0 = p_j2k->m_private_image->y0; |
11071 | } |
11072 | p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; |
11073 | if (p_image->y1 > p_j2k->m_private_image->y1) { |
11074 | p_image->y1 = p_j2k->m_private_image->y1; |
11075 | } |
11076 | |
11077 | l_img_comp = p_image->comps; |
11078 | for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { |
11079 | OPJ_INT32 l_comp_x1, l_comp_y1; |
11080 | |
11081 | l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; |
11082 | |
11083 | l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, |
11084 | (OPJ_INT32)l_img_comp->dx); |
11085 | l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, |
11086 | (OPJ_INT32)l_img_comp->dy); |
11087 | l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); |
11088 | l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); |
11089 | |
11090 | l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, |
11091 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, |
11092 | (OPJ_INT32)l_img_comp->factor)); |
11093 | l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, |
11094 | (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, |
11095 | (OPJ_INT32)l_img_comp->factor)); |
11096 | |
11097 | l_img_comp++; |
11098 | } |
11099 | |
11100 | if (p_image->numcomps > p_j2k->m_private_image->numcomps) { |
11101 | /* Can happen when calling repeatdly opj_get_decoded_tile() on an |
11102 | * image with a color palette, where color palette expansion is done |
11103 | * later in jp2.c */ |
11104 | for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps; |
11105 | ++compno) { |
11106 | opj_image_data_free(p_image->comps[compno].data); |
11107 | p_image->comps[compno].data = NULL; |
11108 | } |
11109 | p_image->numcomps = p_j2k->m_private_image->numcomps; |
11110 | } |
11111 | |
11112 | /* Destroy the previous output image*/ |
11113 | if (p_j2k->m_output_image) { |
11114 | opj_image_destroy(p_j2k->m_output_image); |
11115 | } |
11116 | |
11117 | /* Create the ouput image from the information previously computed*/ |
11118 | p_j2k->m_output_image = opj_image_create0(); |
11119 | if (!(p_j2k->m_output_image)) { |
11120 | return OPJ_FALSE; |
11121 | } |
11122 | opj_copy_image_header(p_image, p_j2k->m_output_image); |
11123 | |
11124 | p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; |
11125 | |
11126 | /* customization of the decoding */ |
11127 | if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) { |
11128 | return OPJ_FALSE; |
11129 | } |
11130 | |
11131 | /* Decode the codestream */ |
11132 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
11133 | opj_image_destroy(p_j2k->m_private_image); |
11134 | p_j2k->m_private_image = NULL; |
11135 | return OPJ_FALSE; |
11136 | } |
11137 | |
11138 | /* Move data and copy one information from codec to output image*/ |
11139 | return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); |
11140 | } |
11141 | |
11142 | OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, |
11143 | OPJ_UINT32 res_factor, |
11144 | opj_event_mgr_t * p_manager) |
11145 | { |
11146 | OPJ_UINT32 it_comp; |
11147 | |
11148 | p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; |
11149 | |
11150 | if (p_j2k->m_private_image) { |
11151 | if (p_j2k->m_private_image->comps) { |
11152 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { |
11153 | if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { |
11154 | for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { |
11155 | OPJ_UINT32 max_res = |
11156 | p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; |
11157 | if (res_factor >= max_res) { |
11158 | opj_event_msg(p_manager, EVT_ERROR, |
11159 | "Resolution factor is greater than the maximum resolution in the component.\n" ); |
11160 | return OPJ_FALSE; |
11161 | } |
11162 | p_j2k->m_private_image->comps[it_comp].factor = res_factor; |
11163 | } |
11164 | return OPJ_TRUE; |
11165 | } |
11166 | } |
11167 | } |
11168 | } |
11169 | |
11170 | return OPJ_FALSE; |
11171 | } |
11172 | |
11173 | OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, |
11174 | opj_stream_private_t *p_stream, |
11175 | opj_event_mgr_t * p_manager) |
11176 | { |
11177 | OPJ_UINT32 i, j; |
11178 | OPJ_UINT32 l_nb_tiles; |
11179 | OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size; |
11180 | OPJ_BYTE * l_current_data = 00; |
11181 | OPJ_BOOL l_reuse_data = OPJ_FALSE; |
11182 | opj_tcd_t* p_tcd = 00; |
11183 | |
11184 | /* preconditions */ |
11185 | assert(p_j2k != 00); |
11186 | assert(p_stream != 00); |
11187 | assert(p_manager != 00); |
11188 | |
11189 | p_tcd = p_j2k->m_tcd; |
11190 | |
11191 | l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; |
11192 | if (l_nb_tiles == 1) { |
11193 | l_reuse_data = OPJ_TRUE; |
11194 | #ifdef __SSE__ |
11195 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
11196 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
11197 | if (((size_t)l_img_comp->data & 0xFU) != |
11198 | 0U) { /* tile data shall be aligned on 16 bytes */ |
11199 | l_reuse_data = OPJ_FALSE; |
11200 | } |
11201 | } |
11202 | #endif |
11203 | } |
11204 | for (i = 0; i < l_nb_tiles; ++i) { |
11205 | if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) { |
11206 | if (l_current_data) { |
11207 | opj_free(l_current_data); |
11208 | } |
11209 | return OPJ_FALSE; |
11210 | } |
11211 | |
11212 | /* if we only have one tile, then simply set tile component data equal to image component data */ |
11213 | /* otherwise, allocate the data */ |
11214 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
11215 | opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; |
11216 | if (l_reuse_data) { |
11217 | opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; |
11218 | l_tilec->data = l_img_comp->data; |
11219 | l_tilec->ownsData = OPJ_FALSE; |
11220 | } else { |
11221 | if (! opj_alloc_tile_component_data(l_tilec)) { |
11222 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); |
11223 | if (l_current_data) { |
11224 | opj_free(l_current_data); |
11225 | } |
11226 | return OPJ_FALSE; |
11227 | } |
11228 | } |
11229 | } |
11230 | l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); |
11231 | if (!l_reuse_data) { |
11232 | if (l_current_tile_size > l_max_tile_size) { |
11233 | OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, |
11234 | l_current_tile_size); |
11235 | if (! l_new_current_data) { |
11236 | if (l_current_data) { |
11237 | opj_free(l_current_data); |
11238 | } |
11239 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n" ); |
11240 | return OPJ_FALSE; |
11241 | } |
11242 | l_current_data = l_new_current_data; |
11243 | l_max_tile_size = l_current_tile_size; |
11244 | } |
11245 | if (l_current_data == NULL) { |
11246 | /* Should not happen in practice, but will avoid Coverity to */ |
11247 | /* complain about a null pointer dereference */ |
11248 | assert(0); |
11249 | return OPJ_FALSE; |
11250 | } |
11251 | |
11252 | /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ |
11253 | /* 32 bit components @ 8 bit precision get converted to 8 bit */ |
11254 | /* 32 bit components @ 16 bit precision get converted to 16 bit */ |
11255 | opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data); |
11256 | |
11257 | /* now copy this data into the tile component */ |
11258 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data, |
11259 | l_current_tile_size)) { |
11260 | opj_event_msg(p_manager, EVT_ERROR, |
11261 | "Size mismatch between tile data and sent data." ); |
11262 | opj_free(l_current_data); |
11263 | return OPJ_FALSE; |
11264 | } |
11265 | } |
11266 | |
11267 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
11268 | if (l_current_data) { |
11269 | opj_free(l_current_data); |
11270 | } |
11271 | return OPJ_FALSE; |
11272 | } |
11273 | } |
11274 | |
11275 | if (l_current_data) { |
11276 | opj_free(l_current_data); |
11277 | } |
11278 | return OPJ_TRUE; |
11279 | } |
11280 | |
11281 | OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, |
11282 | opj_stream_private_t *p_stream, |
11283 | opj_event_mgr_t * p_manager) |
11284 | { |
11285 | /* customization of the encoding */ |
11286 | if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { |
11287 | return OPJ_FALSE; |
11288 | } |
11289 | |
11290 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
11291 | return OPJ_FALSE; |
11292 | } |
11293 | |
11294 | return OPJ_TRUE; |
11295 | } |
11296 | |
11297 | OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, |
11298 | opj_stream_private_t *p_stream, |
11299 | opj_image_t * p_image, |
11300 | opj_event_mgr_t * p_manager) |
11301 | { |
11302 | /* preconditions */ |
11303 | assert(p_j2k != 00); |
11304 | assert(p_stream != 00); |
11305 | assert(p_manager != 00); |
11306 | |
11307 | p_j2k->m_private_image = opj_image_create0(); |
11308 | if (! p_j2k->m_private_image) { |
11309 | opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." ); |
11310 | return OPJ_FALSE; |
11311 | } |
11312 | opj_copy_image_header(p_image, p_j2k->m_private_image); |
11313 | |
11314 | /* TODO_MSD: Find a better way */ |
11315 | if (p_image->comps) { |
11316 | OPJ_UINT32 it_comp; |
11317 | for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { |
11318 | if (p_image->comps[it_comp].data) { |
11319 | p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data; |
11320 | p_image->comps[it_comp].data = NULL; |
11321 | |
11322 | } |
11323 | } |
11324 | } |
11325 | |
11326 | /* customization of the validation */ |
11327 | if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) { |
11328 | return OPJ_FALSE; |
11329 | } |
11330 | |
11331 | /* validation of the parameters codec */ |
11332 | if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { |
11333 | return OPJ_FALSE; |
11334 | } |
11335 | |
11336 | /* customization of the encoding */ |
11337 | if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { |
11338 | return OPJ_FALSE; |
11339 | } |
11340 | |
11341 | /* write header */ |
11342 | if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { |
11343 | return OPJ_FALSE; |
11344 | } |
11345 | |
11346 | return OPJ_TRUE; |
11347 | } |
11348 | |
11349 | static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, |
11350 | OPJ_UINT32 p_tile_index, |
11351 | opj_stream_private_t *p_stream, |
11352 | opj_event_mgr_t * p_manager) |
11353 | { |
11354 | (void)p_stream; |
11355 | if (p_tile_index != p_j2k->m_current_tile_number) { |
11356 | opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." ); |
11357 | return OPJ_FALSE; |
11358 | } |
11359 | |
11360 | opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n" , |
11361 | p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); |
11362 | |
11363 | p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; |
11364 | p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; |
11365 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
11366 | |
11367 | /* initialisation before tile encoding */ |
11368 | if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, |
11369 | p_manager)) { |
11370 | return OPJ_FALSE; |
11371 | } |
11372 | |
11373 | return OPJ_TRUE; |
11374 | } |
11375 | |
11376 | static void opj_get_tile_dimensions(opj_image_t * l_image, |
11377 | opj_tcd_tilecomp_t * l_tilec, |
11378 | opj_image_comp_t * l_img_comp, |
11379 | OPJ_UINT32* l_size_comp, |
11380 | OPJ_UINT32* l_width, |
11381 | OPJ_UINT32* l_height, |
11382 | OPJ_UINT32* l_offset_x, |
11383 | OPJ_UINT32* l_offset_y, |
11384 | OPJ_UINT32* l_image_width, |
11385 | OPJ_UINT32* l_stride, |
11386 | OPJ_UINT32* l_tile_offset) |
11387 | { |
11388 | OPJ_UINT32 l_remaining; |
11389 | *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ |
11390 | l_remaining = l_img_comp->prec & 7; /* (%8) */ |
11391 | if (l_remaining) { |
11392 | *l_size_comp += 1; |
11393 | } |
11394 | |
11395 | if (*l_size_comp == 3) { |
11396 | *l_size_comp = 4; |
11397 | } |
11398 | |
11399 | *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); |
11400 | *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); |
11401 | *l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, |
11402 | (OPJ_INT32)l_img_comp->dx); |
11403 | *l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, |
11404 | (OPJ_INT32)l_img_comp->dy); |
11405 | *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - |
11406 | (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx); |
11407 | *l_stride = *l_image_width - *l_width; |
11408 | *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + (( |
11409 | OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; |
11410 | } |
11411 | |
11412 | static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data) |
11413 | { |
11414 | OPJ_UINT32 i, j, k = 0; |
11415 | |
11416 | for (i = 0; i < p_tcd->image->numcomps; ++i) { |
11417 | opj_image_t * l_image = p_tcd->image; |
11418 | OPJ_INT32 * l_src_ptr; |
11419 | opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; |
11420 | opj_image_comp_t * l_img_comp = l_image->comps + i; |
11421 | OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y, |
11422 | l_image_width, l_stride, l_tile_offset; |
11423 | |
11424 | opj_get_tile_dimensions(l_image, |
11425 | l_tilec, |
11426 | l_img_comp, |
11427 | &l_size_comp, |
11428 | &l_width, |
11429 | &l_height, |
11430 | &l_offset_x, |
11431 | &l_offset_y, |
11432 | &l_image_width, |
11433 | &l_stride, |
11434 | &l_tile_offset); |
11435 | |
11436 | l_src_ptr = l_img_comp->data + l_tile_offset; |
11437 | |
11438 | switch (l_size_comp) { |
11439 | case 1: { |
11440 | OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; |
11441 | if (l_img_comp->sgnd) { |
11442 | for (j = 0; j < l_height; ++j) { |
11443 | for (k = 0; k < l_width; ++k) { |
11444 | *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr); |
11445 | ++l_dest_ptr; |
11446 | ++l_src_ptr; |
11447 | } |
11448 | l_src_ptr += l_stride; |
11449 | } |
11450 | } else { |
11451 | for (j = 0; j < l_height; ++j) { |
11452 | for (k = 0; k < l_width; ++k) { |
11453 | *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff); |
11454 | ++l_dest_ptr; |
11455 | ++l_src_ptr; |
11456 | } |
11457 | l_src_ptr += l_stride; |
11458 | } |
11459 | } |
11460 | |
11461 | p_data = (OPJ_BYTE*) l_dest_ptr; |
11462 | } |
11463 | break; |
11464 | case 2: { |
11465 | OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; |
11466 | if (l_img_comp->sgnd) { |
11467 | for (j = 0; j < l_height; ++j) { |
11468 | for (k = 0; k < l_width; ++k) { |
11469 | *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++)); |
11470 | } |
11471 | l_src_ptr += l_stride; |
11472 | } |
11473 | } else { |
11474 | for (j = 0; j < l_height; ++j) { |
11475 | for (k = 0; k < l_width; ++k) { |
11476 | *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); |
11477 | } |
11478 | l_src_ptr += l_stride; |
11479 | } |
11480 | } |
11481 | |
11482 | p_data = (OPJ_BYTE*) l_dest_ptr; |
11483 | } |
11484 | break; |
11485 | case 4: { |
11486 | OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; |
11487 | for (j = 0; j < l_height; ++j) { |
11488 | for (k = 0; k < l_width; ++k) { |
11489 | *(l_dest_ptr++) = *(l_src_ptr++); |
11490 | } |
11491 | l_src_ptr += l_stride; |
11492 | } |
11493 | |
11494 | p_data = (OPJ_BYTE*) l_dest_ptr; |
11495 | } |
11496 | break; |
11497 | } |
11498 | } |
11499 | } |
11500 | |
11501 | static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, |
11502 | opj_stream_private_t *p_stream, |
11503 | opj_event_mgr_t * p_manager) |
11504 | { |
11505 | OPJ_UINT32 l_nb_bytes_written; |
11506 | OPJ_BYTE * l_current_data = 00; |
11507 | OPJ_UINT32 l_tile_size = 0; |
11508 | OPJ_UINT32 l_available_data; |
11509 | |
11510 | /* preconditions */ |
11511 | assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
11512 | |
11513 | l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; |
11514 | l_available_data = l_tile_size; |
11515 | l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; |
11516 | |
11517 | l_nb_bytes_written = 0; |
11518 | if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written, |
11519 | l_available_data, p_stream, p_manager)) { |
11520 | return OPJ_FALSE; |
11521 | } |
11522 | l_current_data += l_nb_bytes_written; |
11523 | l_available_data -= l_nb_bytes_written; |
11524 | |
11525 | l_nb_bytes_written = 0; |
11526 | if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written, |
11527 | l_available_data, p_stream, p_manager)) { |
11528 | return OPJ_FALSE; |
11529 | } |
11530 | |
11531 | l_available_data -= l_nb_bytes_written; |
11532 | l_nb_bytes_written = l_tile_size - l_available_data; |
11533 | |
11534 | if (opj_stream_write_data(p_stream, |
11535 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, |
11536 | l_nb_bytes_written, p_manager) != l_nb_bytes_written) { |
11537 | return OPJ_FALSE; |
11538 | } |
11539 | |
11540 | ++p_j2k->m_current_tile_number; |
11541 | |
11542 | return OPJ_TRUE; |
11543 | } |
11544 | |
11545 | static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, |
11546 | opj_event_mgr_t * p_manager) |
11547 | { |
11548 | /* preconditions */ |
11549 | assert(p_j2k != 00); |
11550 | assert(p_manager != 00); |
11551 | |
11552 | /* DEVELOPER CORNER, insert your custom procedures */ |
11553 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11554 | (opj_procedure)opj_j2k_write_eoc, p_manager)) { |
11555 | return OPJ_FALSE; |
11556 | } |
11557 | |
11558 | if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) { |
11559 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11560 | (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) { |
11561 | return OPJ_FALSE; |
11562 | } |
11563 | } |
11564 | |
11565 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11566 | (opj_procedure)opj_j2k_write_epc, p_manager)) { |
11567 | return OPJ_FALSE; |
11568 | } |
11569 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11570 | (opj_procedure)opj_j2k_end_encoding, p_manager)) { |
11571 | return OPJ_FALSE; |
11572 | } |
11573 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11574 | (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) { |
11575 | return OPJ_FALSE; |
11576 | } |
11577 | return OPJ_TRUE; |
11578 | } |
11579 | |
11580 | static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, |
11581 | opj_event_mgr_t * p_manager) |
11582 | { |
11583 | /* preconditions */ |
11584 | assert(p_j2k != 00); |
11585 | assert(p_manager != 00); |
11586 | |
11587 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
11588 | (opj_procedure)opj_j2k_build_encoder, p_manager)) { |
11589 | return OPJ_FALSE; |
11590 | } |
11591 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
11592 | (opj_procedure)opj_j2k_encoding_validation, p_manager)) { |
11593 | return OPJ_FALSE; |
11594 | } |
11595 | |
11596 | /* DEVELOPER CORNER, add your custom validation procedure */ |
11597 | if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, |
11598 | (opj_procedure)opj_j2k_mct_validation, p_manager)) { |
11599 | return OPJ_FALSE; |
11600 | } |
11601 | |
11602 | return OPJ_TRUE; |
11603 | } |
11604 | |
11605 | static OPJ_BOOL (opj_j2k_t *p_j2k, |
11606 | opj_event_mgr_t * p_manager) |
11607 | { |
11608 | /* preconditions */ |
11609 | assert(p_j2k != 00); |
11610 | assert(p_manager != 00); |
11611 | |
11612 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11613 | (opj_procedure)opj_j2k_init_info, p_manager)) { |
11614 | return OPJ_FALSE; |
11615 | } |
11616 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11617 | (opj_procedure)opj_j2k_write_soc, p_manager)) { |
11618 | return OPJ_FALSE; |
11619 | } |
11620 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11621 | (opj_procedure)opj_j2k_write_siz, p_manager)) { |
11622 | return OPJ_FALSE; |
11623 | } |
11624 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11625 | (opj_procedure)opj_j2k_write_cod, p_manager)) { |
11626 | return OPJ_FALSE; |
11627 | } |
11628 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11629 | (opj_procedure)opj_j2k_write_qcd, p_manager)) { |
11630 | return OPJ_FALSE; |
11631 | } |
11632 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11633 | (opj_procedure)opj_j2k_write_all_coc, p_manager)) { |
11634 | return OPJ_FALSE; |
11635 | } |
11636 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11637 | (opj_procedure)opj_j2k_write_all_qcc, p_manager)) { |
11638 | return OPJ_FALSE; |
11639 | } |
11640 | |
11641 | if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) { |
11642 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11643 | (opj_procedure)opj_j2k_write_tlm, p_manager)) { |
11644 | return OPJ_FALSE; |
11645 | } |
11646 | |
11647 | if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { |
11648 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11649 | (opj_procedure)opj_j2k_write_poc, p_manager)) { |
11650 | return OPJ_FALSE; |
11651 | } |
11652 | } |
11653 | } |
11654 | |
11655 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11656 | (opj_procedure)opj_j2k_write_regions, p_manager)) { |
11657 | return OPJ_FALSE; |
11658 | } |
11659 | |
11660 | if (p_j2k->m_cp.comment != 00) { |
11661 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11662 | (opj_procedure)opj_j2k_write_com, p_manager)) { |
11663 | return OPJ_FALSE; |
11664 | } |
11665 | } |
11666 | |
11667 | /* DEVELOPER CORNER, insert your custom procedures */ |
11668 | if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) { |
11669 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11670 | (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) { |
11671 | return OPJ_FALSE; |
11672 | } |
11673 | } |
11674 | /* End of Developer Corner */ |
11675 | |
11676 | if (p_j2k->cstr_index) { |
11677 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11678 | (opj_procedure)opj_j2k_get_end_header, p_manager)) { |
11679 | return OPJ_FALSE; |
11680 | } |
11681 | } |
11682 | |
11683 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11684 | (opj_procedure)opj_j2k_create_tcd, p_manager)) { |
11685 | return OPJ_FALSE; |
11686 | } |
11687 | if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, |
11688 | (opj_procedure)opj_j2k_update_rates, p_manager)) { |
11689 | return OPJ_FALSE; |
11690 | } |
11691 | |
11692 | return OPJ_TRUE; |
11693 | } |
11694 | |
11695 | static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, |
11696 | OPJ_BYTE * p_data, |
11697 | OPJ_UINT32 * p_data_written, |
11698 | OPJ_UINT32 p_total_data_size, |
11699 | opj_stream_private_t *p_stream, |
11700 | struct opj_event_mgr * p_manager) |
11701 | { |
11702 | OPJ_UINT32 l_nb_bytes_written = 0; |
11703 | OPJ_UINT32 l_current_nb_bytes_written; |
11704 | OPJ_BYTE * l_begin_data = 00; |
11705 | |
11706 | opj_tcd_t * l_tcd = 00; |
11707 | opj_cp_t * l_cp = 00; |
11708 | |
11709 | l_tcd = p_j2k->m_tcd; |
11710 | l_cp = &(p_j2k->m_cp); |
11711 | |
11712 | l_tcd->cur_pino = 0; |
11713 | |
11714 | /*Get number of tile parts*/ |
11715 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; |
11716 | |
11717 | /* INDEX >> */ |
11718 | /* << INDEX */ |
11719 | |
11720 | l_current_nb_bytes_written = 0; |
11721 | l_begin_data = p_data; |
11722 | if (! opj_j2k_write_sot(p_j2k, p_data, p_total_data_size, |
11723 | &l_current_nb_bytes_written, p_stream, |
11724 | p_manager)) { |
11725 | return OPJ_FALSE; |
11726 | } |
11727 | |
11728 | l_nb_bytes_written += l_current_nb_bytes_written; |
11729 | p_data += l_current_nb_bytes_written; |
11730 | p_total_data_size -= l_current_nb_bytes_written; |
11731 | |
11732 | if (!OPJ_IS_CINEMA(l_cp->rsiz)) { |
11733 | #if 0 |
11734 | for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { |
11735 | l_current_nb_bytes_written = 0; |
11736 | opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
11737 | p_manager); |
11738 | l_nb_bytes_written += l_current_nb_bytes_written; |
11739 | p_data += l_current_nb_bytes_written; |
11740 | p_total_data_size -= l_current_nb_bytes_written; |
11741 | |
11742 | l_current_nb_bytes_written = 0; |
11743 | opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, |
11744 | p_manager); |
11745 | l_nb_bytes_written += l_current_nb_bytes_written; |
11746 | p_data += l_current_nb_bytes_written; |
11747 | p_total_data_size -= l_current_nb_bytes_written; |
11748 | } |
11749 | #endif |
11750 | if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { |
11751 | l_current_nb_bytes_written = 0; |
11752 | opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, |
11753 | p_manager); |
11754 | l_nb_bytes_written += l_current_nb_bytes_written; |
11755 | p_data += l_current_nb_bytes_written; |
11756 | p_total_data_size -= l_current_nb_bytes_written; |
11757 | } |
11758 | } |
11759 | |
11760 | l_current_nb_bytes_written = 0; |
11761 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
11762 | p_total_data_size, p_stream, p_manager)) { |
11763 | return OPJ_FALSE; |
11764 | } |
11765 | |
11766 | l_nb_bytes_written += l_current_nb_bytes_written; |
11767 | * p_data_written = l_nb_bytes_written; |
11768 | |
11769 | /* Writing Psot in SOT marker */ |
11770 | opj_write_bytes(l_begin_data + 6, l_nb_bytes_written, |
11771 | 4); /* PSOT */ |
11772 | |
11773 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
11774 | opj_j2k_update_tlm(p_j2k, l_nb_bytes_written); |
11775 | } |
11776 | |
11777 | return OPJ_TRUE; |
11778 | } |
11779 | |
11780 | static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, |
11781 | OPJ_BYTE * p_data, |
11782 | OPJ_UINT32 * p_data_written, |
11783 | OPJ_UINT32 p_total_data_size, |
11784 | opj_stream_private_t *p_stream, |
11785 | struct opj_event_mgr * p_manager |
11786 | ) |
11787 | { |
11788 | OPJ_UINT32 tilepartno = 0; |
11789 | OPJ_UINT32 l_nb_bytes_written = 0; |
11790 | OPJ_UINT32 l_current_nb_bytes_written; |
11791 | OPJ_UINT32 l_part_tile_size; |
11792 | OPJ_UINT32 tot_num_tp; |
11793 | OPJ_UINT32 pino; |
11794 | |
11795 | OPJ_BYTE * l_begin_data; |
11796 | opj_tcp_t *l_tcp = 00; |
11797 | opj_tcd_t * l_tcd = 00; |
11798 | opj_cp_t * l_cp = 00; |
11799 | |
11800 | l_tcd = p_j2k->m_tcd; |
11801 | l_cp = &(p_j2k->m_cp); |
11802 | l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; |
11803 | |
11804 | /*Get number of tile parts*/ |
11805 | tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number); |
11806 | |
11807 | /* start writing remaining tile parts */ |
11808 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
11809 | for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { |
11810 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
11811 | l_current_nb_bytes_written = 0; |
11812 | l_part_tile_size = 0; |
11813 | l_begin_data = p_data; |
11814 | |
11815 | if (! opj_j2k_write_sot(p_j2k, p_data, |
11816 | p_total_data_size, |
11817 | &l_current_nb_bytes_written, |
11818 | p_stream, |
11819 | p_manager)) { |
11820 | return OPJ_FALSE; |
11821 | } |
11822 | |
11823 | l_nb_bytes_written += l_current_nb_bytes_written; |
11824 | p_data += l_current_nb_bytes_written; |
11825 | p_total_data_size -= l_current_nb_bytes_written; |
11826 | l_part_tile_size += l_current_nb_bytes_written; |
11827 | |
11828 | l_current_nb_bytes_written = 0; |
11829 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
11830 | p_total_data_size, p_stream, p_manager)) { |
11831 | return OPJ_FALSE; |
11832 | } |
11833 | |
11834 | p_data += l_current_nb_bytes_written; |
11835 | l_nb_bytes_written += l_current_nb_bytes_written; |
11836 | p_total_data_size -= l_current_nb_bytes_written; |
11837 | l_part_tile_size += l_current_nb_bytes_written; |
11838 | |
11839 | /* Writing Psot in SOT marker */ |
11840 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
11841 | 4); /* PSOT */ |
11842 | |
11843 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
11844 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
11845 | } |
11846 | |
11847 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
11848 | } |
11849 | |
11850 | for (pino = 1; pino <= l_tcp->numpocs; ++pino) { |
11851 | l_tcd->cur_pino = pino; |
11852 | |
11853 | /*Get number of tile parts*/ |
11854 | tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number); |
11855 | for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { |
11856 | p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; |
11857 | l_current_nb_bytes_written = 0; |
11858 | l_part_tile_size = 0; |
11859 | l_begin_data = p_data; |
11860 | |
11861 | if (! opj_j2k_write_sot(p_j2k, p_data, |
11862 | p_total_data_size, |
11863 | &l_current_nb_bytes_written, p_stream, |
11864 | p_manager)) { |
11865 | return OPJ_FALSE; |
11866 | } |
11867 | |
11868 | l_nb_bytes_written += l_current_nb_bytes_written; |
11869 | p_data += l_current_nb_bytes_written; |
11870 | p_total_data_size -= l_current_nb_bytes_written; |
11871 | l_part_tile_size += l_current_nb_bytes_written; |
11872 | |
11873 | l_current_nb_bytes_written = 0; |
11874 | |
11875 | if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, |
11876 | p_total_data_size, p_stream, p_manager)) { |
11877 | return OPJ_FALSE; |
11878 | } |
11879 | |
11880 | l_nb_bytes_written += l_current_nb_bytes_written; |
11881 | p_data += l_current_nb_bytes_written; |
11882 | p_total_data_size -= l_current_nb_bytes_written; |
11883 | l_part_tile_size += l_current_nb_bytes_written; |
11884 | |
11885 | /* Writing Psot in SOT marker */ |
11886 | opj_write_bytes(l_begin_data + 6, l_part_tile_size, |
11887 | 4); /* PSOT */ |
11888 | |
11889 | if (OPJ_IS_CINEMA(l_cp->rsiz)) { |
11890 | opj_j2k_update_tlm(p_j2k, l_part_tile_size); |
11891 | } |
11892 | |
11893 | ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; |
11894 | } |
11895 | } |
11896 | |
11897 | *p_data_written = l_nb_bytes_written; |
11898 | |
11899 | return OPJ_TRUE; |
11900 | } |
11901 | |
11902 | static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, |
11903 | struct opj_stream_private *p_stream, |
11904 | struct opj_event_mgr * p_manager) |
11905 | { |
11906 | OPJ_UINT32 l_tlm_size; |
11907 | OPJ_OFF_T l_tlm_position, l_current_position; |
11908 | |
11909 | /* preconditions */ |
11910 | assert(p_j2k != 00); |
11911 | assert(p_manager != 00); |
11912 | assert(p_stream != 00); |
11913 | |
11914 | l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; |
11915 | l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; |
11916 | l_current_position = opj_stream_tell(p_stream); |
11917 | |
11918 | if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) { |
11919 | return OPJ_FALSE; |
11920 | } |
11921 | |
11922 | if (opj_stream_write_data(p_stream, |
11923 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, |
11924 | p_manager) != l_tlm_size) { |
11925 | return OPJ_FALSE; |
11926 | } |
11927 | |
11928 | if (! opj_stream_seek(p_stream, l_current_position, p_manager)) { |
11929 | return OPJ_FALSE; |
11930 | } |
11931 | |
11932 | return OPJ_TRUE; |
11933 | } |
11934 | |
11935 | static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, |
11936 | struct opj_stream_private *p_stream, |
11937 | struct opj_event_mgr * p_manager) |
11938 | { |
11939 | /* preconditions */ |
11940 | assert(p_j2k != 00); |
11941 | assert(p_manager != 00); |
11942 | assert(p_stream != 00); |
11943 | |
11944 | OPJ_UNUSED(p_stream); |
11945 | OPJ_UNUSED(p_manager); |
11946 | |
11947 | opj_tcd_destroy(p_j2k->m_tcd); |
11948 | p_j2k->m_tcd = 00; |
11949 | |
11950 | if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { |
11951 | opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); |
11952 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; |
11953 | p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; |
11954 | } |
11955 | |
11956 | if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { |
11957 | opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); |
11958 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; |
11959 | } |
11960 | |
11961 | p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; |
11962 | |
11963 | return OPJ_TRUE; |
11964 | } |
11965 | |
11966 | /** |
11967 | * Destroys the memory associated with the decoding of headers. |
11968 | */ |
11969 | static OPJ_BOOL (opj_j2k_t * p_j2k, |
11970 | opj_stream_private_t *p_stream, |
11971 | opj_event_mgr_t * p_manager |
11972 | ) |
11973 | { |
11974 | /* preconditions */ |
11975 | assert(p_j2k != 00); |
11976 | assert(p_stream != 00); |
11977 | assert(p_manager != 00); |
11978 | |
11979 | OPJ_UNUSED(p_stream); |
11980 | OPJ_UNUSED(p_manager); |
11981 | |
11982 | if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { |
11983 | opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); |
11984 | p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; |
11985 | } |
11986 | |
11987 | p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; |
11988 | |
11989 | return OPJ_TRUE; |
11990 | } |
11991 | |
11992 | static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, |
11993 | struct opj_stream_private *p_stream, |
11994 | struct opj_event_mgr * p_manager) |
11995 | { |
11996 | opj_codestream_info_t * l_cstr_info = 00; |
11997 | |
11998 | /* preconditions */ |
11999 | assert(p_j2k != 00); |
12000 | assert(p_manager != 00); |
12001 | assert(p_stream != 00); |
12002 | (void)l_cstr_info; |
12003 | |
12004 | OPJ_UNUSED(p_stream); |
12005 | |
12006 | /* TODO mergeV2: check this part which use cstr_info */ |
12007 | /*l_cstr_info = p_j2k->cstr_info; |
12008 | |
12009 | if (l_cstr_info) { |
12010 | OPJ_UINT32 compno; |
12011 | 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)); |
12012 | |
12013 | l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; |
12014 | l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; |
12015 | |
12016 | l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; |
12017 | |
12018 | l_cstr_info->tw = p_j2k->m_cp.tw; |
12019 | l_cstr_info->th = p_j2k->m_cp.th; |
12020 | |
12021 | l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ |
12022 | /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ |
12023 | /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ |
12024 | /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ |
12025 | |
12026 | /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; |
12027 | |
12028 | l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; |
12029 | |
12030 | l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); |
12031 | |
12032 | for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { |
12033 | l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; |
12034 | } |
12035 | |
12036 | l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ |
12037 | |
12038 | /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ |
12039 | |
12040 | /*l_cstr_info->maxmarknum = 100; |
12041 | l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); |
12042 | l_cstr_info->marknum = 0; |
12043 | }*/ |
12044 | |
12045 | return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp), |
12046 | &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image, |
12047 | p_manager); |
12048 | } |
12049 | |
12050 | /** |
12051 | * Creates a tile-coder encoder. |
12052 | * |
12053 | * @param p_stream the stream to write data to. |
12054 | * @param p_j2k J2K codec. |
12055 | * @param p_manager the user event manager. |
12056 | */ |
12057 | static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, |
12058 | opj_stream_private_t *p_stream, |
12059 | opj_event_mgr_t * p_manager |
12060 | ) |
12061 | { |
12062 | /* preconditions */ |
12063 | assert(p_j2k != 00); |
12064 | assert(p_manager != 00); |
12065 | assert(p_stream != 00); |
12066 | |
12067 | OPJ_UNUSED(p_stream); |
12068 | |
12069 | p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); |
12070 | |
12071 | if (! p_j2k->m_tcd) { |
12072 | opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n" ); |
12073 | return OPJ_FALSE; |
12074 | } |
12075 | |
12076 | if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp, |
12077 | p_j2k->m_tp)) { |
12078 | opj_tcd_destroy(p_j2k->m_tcd); |
12079 | p_j2k->m_tcd = 00; |
12080 | return OPJ_FALSE; |
12081 | } |
12082 | |
12083 | return OPJ_TRUE; |
12084 | } |
12085 | |
12086 | OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, |
12087 | OPJ_UINT32 p_tile_index, |
12088 | OPJ_BYTE * p_data, |
12089 | OPJ_UINT32 p_data_size, |
12090 | opj_stream_private_t *p_stream, |
12091 | opj_event_mgr_t * p_manager) |
12092 | { |
12093 | if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) { |
12094 | opj_event_msg(p_manager, EVT_ERROR, |
12095 | "Error while opj_j2k_pre_write_tile with tile index = %d\n" , p_tile_index); |
12096 | return OPJ_FALSE; |
12097 | } else { |
12098 | OPJ_UINT32 j; |
12099 | /* Allocate data */ |
12100 | for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { |
12101 | opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; |
12102 | |
12103 | if (! opj_alloc_tile_component_data(l_tilec)) { |
12104 | opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); |
12105 | return OPJ_FALSE; |
12106 | } |
12107 | } |
12108 | |
12109 | /* now copy data into the tile component */ |
12110 | if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { |
12111 | opj_event_msg(p_manager, EVT_ERROR, |
12112 | "Size mismatch between tile data and sent data." ); |
12113 | return OPJ_FALSE; |
12114 | } |
12115 | if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { |
12116 | opj_event_msg(p_manager, EVT_ERROR, |
12117 | "Error while opj_j2k_post_write_tile with tile index = %d\n" , p_tile_index); |
12118 | return OPJ_FALSE; |
12119 | } |
12120 | } |
12121 | |
12122 | return OPJ_TRUE; |
12123 | } |
12124 | |