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) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2010-2011, Kaori Hagihara
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include "opj_includes.h"
41
42 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
43 /*@{*/
44
45 /** @name Local static functions */
46 /*@{*/
47
48 /**
49 Write the SOC marker (Start Of Codestream)
50 @param j2k J2K handle
51 */
52 static void j2k_write_soc(opj_j2k_t *j2k);
53 /**
54 Read the SOC marker (Start of Codestream)
55 @param j2k J2K handle
56 */
57 static void j2k_read_soc(opj_j2k_t *j2k);
58 /**
59 Write the SIZ marker (image and tile size)
60 @param j2k J2K handle
61 */
62 static void j2k_write_siz(opj_j2k_t *j2k);
63 /**
64 Read the SIZ marker (image and tile size)
65 @param j2k J2K handle
66 */
67 static void j2k_read_siz(opj_j2k_t *j2k);
68 /**
69 Write the COM marker (comment)
70 @param j2k J2K handle
71 */
72 static void j2k_write_com(opj_j2k_t *j2k);
73 /**
74 Read the COM marker (comment)
75 @param j2k J2K handle
76 */
77 static void j2k_read_com(opj_j2k_t *j2k);
78 /**
79 Write the value concerning the specified component in the marker COD and COC
80 @param j2k J2K handle
81 @param compno Number of the component concerned by the information written
82 */
83 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
84 /**
85 Read the value concerning the specified component in the marker COD and COC
86 @param j2k J2K handle
87 @param compno Number of the component concerned by the information read
88 */
89 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
90 /**
91 Write the COD marker (coding style default)
92 @param j2k J2K handle
93 */
94 static void j2k_write_cod(opj_j2k_t *j2k);
95 /**
96 Read the COD marker (coding style default)
97 @param j2k J2K handle
98 */
99 static void j2k_read_cod(opj_j2k_t *j2k);
100 /**
101 Write the COC marker (coding style component)
102 @param j2k J2K handle
103 @param compno Number of the component concerned by the information written
104 */
105 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
106 /**
107 Read the COC marker (coding style component)
108 @param j2k J2K handle
109 */
110 static void j2k_read_coc(opj_j2k_t *j2k);
111 /**
112 Write the value concerning the specified component in the marker QCD and QCC
113 @param j2k J2K handle
114 @param compno Number of the component concerned by the information written
115 */
116 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
117 /**
118 Read the value concerning the specified component in the marker QCD and QCC
119 @param j2k J2K handle
120 @param compno Number of the component concern by the information read
121 @param len Length of the information in the QCX part of the marker QCD/QCC
122 */
123 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
124 /**
125 Write the QCD marker (quantization default)
126 @param j2k J2K handle
127 */
128 static void j2k_write_qcd(opj_j2k_t *j2k);
129 /**
130 Read the QCD marker (quantization default)
131 @param j2k J2K handle
132 */
133 static void j2k_read_qcd(opj_j2k_t *j2k);
134 /**
135 Write the QCC marker (quantization component)
136 @param j2k J2K handle
137 @param compno Number of the component concerned by the information written
138 */
139 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
140 /**
141 Read the QCC marker (quantization component)
142 @param j2k J2K handle
143 */
144 static void j2k_read_qcc(opj_j2k_t *j2k);
145 /**
146 Write the POC marker (progression order change)
147 @param j2k J2K handle
148 */
149 static void j2k_write_poc(opj_j2k_t *j2k);
150 /**
151 Read the POC marker (progression order change)
152 @param j2k J2K handle
153 */
154 static void j2k_read_poc(opj_j2k_t *j2k);
155 /**
156 Read the CRG marker (component registration)
157 @param j2k J2K handle
158 */
159 static void j2k_read_crg(opj_j2k_t *j2k);
160 /**
161 Read the TLM marker (tile-part lengths)
162 @param j2k J2K handle
163 */
164 static void j2k_read_tlm(opj_j2k_t *j2k);
165 /**
166 Read the PLM marker (packet length, main header)
167 @param j2k J2K handle
168 */
169 static void j2k_read_plm(opj_j2k_t *j2k);
170 /**
171 Read the PLT marker (packet length, tile-part header)
172 @param j2k J2K handle
173 */
174 static void j2k_read_plt(opj_j2k_t *j2k);
175 /**
176 Read the PPM marker (packet packet headers, main header)
177 @param j2k J2K handle
178 */
179 static void j2k_read_ppm(opj_j2k_t *j2k);
180 /**
181 Read the PPT marker (packet packet headers, tile-part header)
182 @param j2k J2K handle
183 */
184 static void j2k_read_ppt(opj_j2k_t *j2k);
185 /**
186 Write the TLM marker (Mainheader)
187 @param j2k J2K handle
188 */
189 static void j2k_write_tlm(opj_j2k_t *j2k);
190 /**
191 Write the SOT marker (start of tile-part)
192 @param j2k J2K handle
193 */
194 static void j2k_write_sot(opj_j2k_t *j2k);
195 /**
196 Read the SOT marker (start of tile-part)
197 @param j2k J2K handle
198 */
199 static void j2k_read_sot(opj_j2k_t *j2k);
200 /**
201 Write the SOD marker (start of data)
202 @param j2k J2K handle
203 @param tile_coder Pointer to a TCD handle
204 */
205 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
206 /**
207 Read the SOD marker (start of data)
208 @param j2k J2K handle
209 */
210 static void j2k_read_sod(opj_j2k_t *j2k);
211 /**
212 Write the RGN marker (region-of-interest)
213 @param j2k J2K handle
214 @param compno Number of the component concerned by the information written
215 @param tileno Number of the tile concerned by the information written
216 */
217 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
218 /**
219 Read the RGN marker (region-of-interest)
220 @param j2k J2K handle
221 */
222 static void j2k_read_rgn(opj_j2k_t *j2k);
223 /**
224 Write the EOC marker (end of codestream)
225 @param j2k J2K handle
226 */
227 static void j2k_write_eoc(opj_j2k_t *j2k);
228 /**
229 Read the EOC marker (end of codestream)
230 @param j2k J2K handle
231 */
232 static void j2k_read_eoc(opj_j2k_t *j2k);
233 /**
234 Read an unknown marker
235 @param j2k J2K handle
236 */
237 static void j2k_read_unk(opj_j2k_t *j2k);
238 /**
239 Add main header marker information
240 @param cstr_info Codestream information structure
241 @param type marker type
242 @param pos byte offset of marker segment
243 @param len length of marker segment
244 */
245 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info,
246 unsigned short int type, int pos, int len);
247 /**
248 Add tile header marker information
249 @param tileno tile index number
250 @param cstr_info Codestream information structure
251 @param type marker type
252 @param pos byte offset of marker segment
253 @param len length of marker segment
254 */
255 static void j2k_add_tlmarker(int tileno, opj_codestream_info_t *cstr_info,
256 unsigned short int type, int pos, int len);
257
258 /*@}*/
259
260 /*@}*/
261
262 /* ----------------------------------------------------------------------- */
263 typedef struct j2k_prog_order {
264 OPJ_PROG_ORDER enum_prog;
265 char str_prog[5];
266 } j2k_prog_order_t;
267
268 j2k_prog_order_t j2k_prog_order_list[] = {
269 {CPRL, "CPRL"},
270 {LRCP, "LRCP"},
271 {PCRL, "PCRL"},
272 {RLCP, "RLCP"},
273 {RPCL, "RPCL"},
274 {(OPJ_PROG_ORDER) - 1, ""}
275 };
276
j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)277 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)
278 {
279 j2k_prog_order_t *po;
280 for (po = j2k_prog_order_list; po->enum_prog != -1; po++) {
281 if (po->enum_prog == prg_order) {
282 break;
283 }
284 }
285 return po->str_prog;
286 }
287
288 /* ----------------------------------------------------------------------- */
j2k_get_num_tp(opj_cp_t * cp,int pino,int tileno)289 static int j2k_get_num_tp(opj_cp_t *cp, int pino, int tileno)
290 {
291 char *prog;
292 int i;
293 int tpnum = 1, tpend = 0;
294 opj_tcp_t *tcp = &cp->tcps[tileno];
295 prog = j2k_convert_progression_order(tcp->prg);
296
297 if (cp->tp_on == 1) {
298 for (i = 0; i < 4; i++) {
299 if (tpend != 1) {
300 if (cp->tp_flag == prog[i]) {
301 tpend = 1;
302 cp->tp_pos = i;
303 }
304 switch (prog[i]) {
305 case 'C':
306 tpnum = tpnum * tcp->pocs[pino].compE;
307 break;
308 case 'R':
309 tpnum = tpnum * tcp->pocs[pino].resE;
310 break;
311 case 'P':
312 tpnum = tpnum * tcp->pocs[pino].prcE;
313 break;
314 case 'L':
315 tpnum = tpnum * tcp->pocs[pino].layE;
316 break;
317 }
318 }
319 }
320 } else {
321 tpnum = 1;
322 }
323 return tpnum;
324 }
325
326 /** mem allocation for TLM marker*/
j2k_calculate_tp(opj_cp_t * cp,int img_numcomp,opj_image_t * image,opj_j2k_t * j2k)327 static int j2k_calculate_tp(opj_cp_t *cp, int img_numcomp, opj_image_t *image,
328 opj_j2k_t *j2k)
329 {
330 int pino, tileno, totnum_tp = 0;
331
332 OPJ_ARG_NOT_USED(img_numcomp);
333
334 j2k->cur_totnum_tp = (int *) opj_malloc(sizeof(int) * cp->tw * cp->th);
335 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
336 int cur_totnum_tp = 0;
337 opj_tcp_t *tcp = &cp->tcps[tileno];
338 for (pino = 0; pino <= tcp->numpocs; pino++) {
339 int tp_num = 0;
340 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno, FINAL_PASS);
341 if (!pi) {
342 return -1;
343 }
344 tp_num = j2k_get_num_tp(cp, pino, tileno);
345 totnum_tp = totnum_tp + tp_num;
346 cur_totnum_tp = cur_totnum_tp + tp_num;
347 pi_destroy(pi, cp, tileno);
348 }
349 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
350 /* INDEX >> */
351 if (j2k->cstr_info) {
352 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
353 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(
354 cur_totnum_tp * sizeof(opj_tp_info_t));
355 }
356 /* << INDEX */
357 }
358 return totnum_tp;
359 }
360
j2k_write_soc(opj_j2k_t * j2k)361 static void j2k_write_soc(opj_j2k_t *j2k)
362 {
363 opj_cio_t *cio = j2k->cio;
364 cio_write(cio, J2K_MS_SOC, 2);
365
366 if (j2k->cstr_info) {
367 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
368 }
369
370 /* UniPG>> */
371 #ifdef USE_JPWL
372
373 /* update markers struct */
374 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
375 #endif /* USE_JPWL */
376 /* <<UniPG */
377 }
378
j2k_read_soc(opj_j2k_t * j2k)379 static void j2k_read_soc(opj_j2k_t *j2k)
380 {
381 j2k->state = J2K_STATE_MHSIZ;
382 /* Index */
383 if (j2k->cstr_info) {
384 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
385 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 -
386 j2k->cstr_info->main_head_start;
387 }
388 }
389
j2k_write_siz(opj_j2k_t * j2k)390 static void j2k_write_siz(opj_j2k_t *j2k)
391 {
392 int i;
393 int lenp, len;
394
395 opj_cio_t *cio = j2k->cio;
396 opj_image_t *image = j2k->image;
397 opj_cp_t *cp = j2k->cp;
398
399 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
400 lenp = cio_tell(cio);
401 cio_skip(cio, 2);
402 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
403 cio_write(cio, image->x1, 4); /* Xsiz */
404 cio_write(cio, image->y1, 4); /* Ysiz */
405 cio_write(cio, image->x0, 4); /* X0siz */
406 cio_write(cio, image->y0, 4); /* Y0siz */
407 cio_write(cio, cp->tdx, 4); /* XTsiz */
408 cio_write(cio, cp->tdy, 4); /* YTsiz */
409 cio_write(cio, cp->tx0, 4); /* XT0siz */
410 cio_write(cio, cp->ty0, 4); /* YT0siz */
411 cio_write(cio, image->numcomps, 2); /* Csiz */
412 for (i = 0; i < image->numcomps; i++) {
413 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7),
414 1); /* Ssiz_i */
415 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
416 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
417 }
418 len = cio_tell(cio) - lenp;
419 cio_seek(cio, lenp);
420 cio_write(cio, len, 2); /* Lsiz */
421 cio_seek(cio, lenp + len);
422
423 if (j2k->cstr_info) {
424 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
425 }
426 }
427
j2k_read_siz(opj_j2k_t * j2k)428 static void j2k_read_siz(opj_j2k_t *j2k)
429 {
430 int len, i;
431
432 opj_cio_t *cio = j2k->cio;
433 opj_image_t *image = j2k->image;
434 opj_cp_t *cp = j2k->cp;
435
436 len = cio_read(cio, 2); /* Lsiz */
437 cio_read(cio, 2); /* Rsiz (capabilities) */
438 image->x1 = cio_read(cio, 4); /* Xsiz */
439 image->y1 = cio_read(cio, 4); /* Ysiz */
440 image->x0 = cio_read(cio, 4); /* X0siz */
441 image->y0 = cio_read(cio, 4); /* Y0siz */
442 cp->tdx = cio_read(cio, 4); /* XTsiz */
443 cp->tdy = cio_read(cio, 4); /* YTsiz */
444 cp->tx0 = cio_read(cio, 4); /* XT0siz */
445 cp->ty0 = cio_read(cio, 4); /* YT0siz */
446
447 if ((image->x0 < 0) || (image->x1 < 0) || (image->y0 < 0) || (image->y1 < 0)) {
448 opj_event_msg(j2k->cinfo, EVT_ERROR,
449 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
450 image->x0, image->x1, image->y0, image->y1);
451 return;
452 }
453
454 image->numcomps = cio_read(cio, 2); /* Csiz */
455
456 #ifdef USE_JPWL
457 if (j2k->cp->correct) {
458 /* if JPWL is on, we check whether TX errors have damaged
459 too much the SIZ parameters */
460 if (!(image->x1 * image->y1)) {
461 opj_event_msg(j2k->cinfo, EVT_ERROR,
462 "JPWL: bad image size (%d x %d)\n",
463 image->x1, image->y1);
464 if (!JPWL_ASSUME || JPWL_ASSUME) {
465 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
466 return;
467 }
468 }
469 if (image->numcomps != ((len - 38) / 3)) {
470 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
471 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
472 image->numcomps, ((len - 38) / 3));
473 if (!JPWL_ASSUME) {
474 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
475 return;
476 }
477 /* we try to correct */
478 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
479 if (image->numcomps < ((len - 38) / 3)) {
480 len = 38 + 3 * image->numcomps;
481 opj_event_msg(j2k->cinfo, EVT_WARNING,
482 "- setting Lsiz to %d => HYPOTHESIS!!!\n",
483 len);
484 } else {
485 image->numcomps = ((len - 38) / 3);
486 opj_event_msg(j2k->cinfo, EVT_WARNING,
487 "- setting Csiz to %d => HYPOTHESIS!!!\n",
488 image->numcomps);
489 }
490 }
491
492 /* update components number in the jpwl_exp_comps filed */
493 cp->exp_comps = image->numcomps;
494 }
495 #endif /* USE_JPWL */
496
497 /* prevent division by zero */
498 if (!(cp->tdx * cp->tdy)) {
499 opj_event_msg(j2k->cinfo, EVT_ERROR,
500 "JPWL: invalid tile size (tdx: %d, tdy: %d)\n", cp->tdx, cp->tdy);
501 return;
502 }
503
504 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps,
505 sizeof(opj_image_comp_t));
506 for (i = 0; i < image->numcomps; i++) {
507 int tmp, w, h;
508 tmp = cio_read(cio, 1); /* Ssiz_i */
509 image->comps[i].prec = (tmp & 0x7f) + 1;
510 image->comps[i].sgnd = tmp >> 7;
511 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
512 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
513
514 #ifdef USE_JPWL
515 if (j2k->cp->correct) {
516 /* if JPWL is on, we check whether TX errors have damaged
517 too much the SIZ parameters, again */
518 if (!(image->comps[i].dx * image->comps[i].dy)) {
519 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
520 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
521 i, i, image->comps[i].dx, image->comps[i].dy);
522 if (!JPWL_ASSUME) {
523 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
524 return;
525 }
526 /* we try to correct */
527 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
528 if (!image->comps[i].dx) {
529 image->comps[i].dx = 1;
530 opj_event_msg(j2k->cinfo, EVT_WARNING,
531 "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
532 i, image->comps[i].dx);
533 }
534 if (!image->comps[i].dy) {
535 image->comps[i].dy = 1;
536 opj_event_msg(j2k->cinfo, EVT_WARNING,
537 "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
538 i, image->comps[i].dy);
539 }
540 }
541
542 }
543 #endif /* USE_JPWL */
544
545 /* prevent division by zero */
546 if (!(image->comps[i].dx * image->comps[i].dy)) {
547 opj_event_msg(j2k->cinfo, EVT_ERROR,
548 "JPWL: invalid component size (dx: %d, dy: %d)\n", image->comps[i].dx,
549 image->comps[i].dy);
550 return;
551 }
552
553 /* TODO: unused ? */
554 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
555 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
556
557 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
558 image->comps[i].factor = cp->reduce; /* reducing factor per component */
559 }
560
561 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
562 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
563
564 #ifdef USE_JPWL
565 if (j2k->cp->correct) {
566 /* if JPWL is on, we check whether TX errors have damaged
567 too much the SIZ parameters */
568 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) ||
569 (cp->th > cp->max_tiles)) {
570 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
571 "JPWL: bad number of tiles (%d x %d)\n",
572 cp->tw, cp->th);
573 if (!JPWL_ASSUME) {
574 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
575 return;
576 }
577 /* we try to correct */
578 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
579 if (cp->tw < 1) {
580 cp->tw = 1;
581 opj_event_msg(j2k->cinfo, EVT_WARNING,
582 "- setting %d tiles in x => HYPOTHESIS!!!\n",
583 cp->tw);
584 }
585 if (cp->tw > cp->max_tiles) {
586 cp->tw = 1;
587 opj_event_msg(j2k->cinfo, EVT_WARNING,
588 "- too large x, increase expectance of %d\n"
589 "- setting %d tiles in x => HYPOTHESIS!!!\n",
590 cp->max_tiles, cp->tw);
591 }
592 if (cp->th < 1) {
593 cp->th = 1;
594 opj_event_msg(j2k->cinfo, EVT_WARNING,
595 "- setting %d tiles in y => HYPOTHESIS!!!\n",
596 cp->th);
597 }
598 if (cp->th > cp->max_tiles) {
599 cp->th = 1;
600 opj_event_msg(j2k->cinfo, EVT_WARNING,
601 "- too large y, increase expectance of %d to continue\n",
602 "- setting %d tiles in y => HYPOTHESIS!!!\n",
603 cp->max_tiles, cp->th);
604 }
605 }
606 }
607 #endif /* USE_JPWL */
608
609 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
610 if (cp->tcps == NULL) {
611 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
612 return;
613 }
614 cp->tileno = (int*) opj_malloc(sizeof(int) * cp->tw * cp->th);
615 if (cp->tileno == NULL) {
616 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
617 return;
618 }
619 cp->tileno_size = 0;
620
621 #ifdef USE_JPWL
622 if (j2k->cp->correct) {
623 if (!cp->tcps) {
624 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
625 "JPWL: could not alloc tcps field of cp\n");
626 if (!JPWL_ASSUME || JPWL_ASSUME) {
627 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
628 return;
629 }
630 }
631 }
632 #endif /* USE_JPWL */
633
634 for (i = 0; i < cp->tw * cp->th; i++) {
635 cp->tcps[i].POC = 0;
636 cp->tcps[i].numpocs = 0;
637 cp->tcps[i].first = 1;
638 }
639
640 /* Initialization for PPM marker */
641 cp->ppm = 0;
642 cp->ppm_data = NULL;
643 cp->ppm_data_first = NULL;
644 cp->ppm_previous = 0;
645 cp->ppm_store = 0;
646
647 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps,
648 sizeof(opj_tccp_t));
649 for (i = 0; i < cp->tw * cp->th; i++) {
650 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(
651 opj_tccp_t));
652 }
653 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th,
654 sizeof(unsigned char*));
655 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
656 j2k->state = J2K_STATE_MH;
657
658 /* Index */
659 if (j2k->cstr_info) {
660 opj_codestream_info_t *cstr_info = j2k->cstr_info;
661 cstr_info->image_w = image->x1 - image->x0;
662 cstr_info->image_h = image->y1 - image->y0;
663 cstr_info->numcomps = image->numcomps;
664 cstr_info->tw = cp->tw;
665 cstr_info->th = cp->th;
666 cstr_info->tile_x = cp->tdx;
667 cstr_info->tile_y = cp->tdy;
668 cstr_info->tile_Ox = cp->tx0;
669 cstr_info->tile_Oy = cp->ty0;
670 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th,
671 sizeof(opj_tile_info_t));
672 }
673 }
674
j2k_write_com(opj_j2k_t * j2k)675 static void j2k_write_com(opj_j2k_t *j2k)
676 {
677 unsigned int i;
678 int lenp, len;
679
680 if (j2k->cp->comment) {
681 opj_cio_t *cio = j2k->cio;
682 char *comment = j2k->cp->comment;
683
684 cio_write(cio, J2K_MS_COM, 2);
685 lenp = cio_tell(cio);
686 cio_skip(cio, 2);
687 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
688 for (i = 0; i < strlen(comment); i++) {
689 cio_write(cio, comment[i], 1);
690 }
691 len = cio_tell(cio) - lenp;
692 cio_seek(cio, lenp);
693 cio_write(cio, len, 2);
694 cio_seek(cio, lenp + len);
695
696
697 if (j2k->cstr_info) {
698 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
699 }
700
701 }
702 }
703
j2k_read_com(opj_j2k_t * j2k)704 static void j2k_read_com(opj_j2k_t *j2k)
705 {
706 int len;
707
708 opj_cio_t *cio = j2k->cio;
709
710 len = cio_read(cio, 2);
711 cio_skip(cio, len - 2);
712 }
713
j2k_write_cox(opj_j2k_t * j2k,int compno)714 static void j2k_write_cox(opj_j2k_t *j2k, int compno)
715 {
716 int i;
717
718 opj_cp_t *cp = j2k->cp;
719 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
720 opj_tccp_t *tccp = &tcp->tccps[compno];
721 opj_cio_t *cio = j2k->cio;
722
723 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
724 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
725 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
726 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
727 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
728
729 if (tccp->csty & J2K_CCP_CSTY_PRT) {
730 for (i = 0; i < tccp->numresolutions; i++) {
731 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
732 }
733 }
734 }
735
j2k_read_cox(opj_j2k_t * j2k,int compno)736 static void j2k_read_cox(opj_j2k_t *j2k, int compno)
737 {
738 int i;
739
740 opj_cp_t *cp = j2k->cp;
741 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
742 j2k->default_tcp;
743 opj_tccp_t *tccp = &tcp->tccps[compno];
744 opj_cio_t *cio = j2k->cio;
745
746 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
747
748 /* If user wants to remove more resolutions than the codestream contains, return error*/
749 if (cp->reduce >= tccp->numresolutions) {
750 opj_event_msg(j2k->cinfo, EVT_ERROR,
751 "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
752 "of resolutions of this component\nModify the cp_reduce parameter.\n\n",
753 compno);
754 j2k->state |= J2K_STATE_ERR;
755 }
756 if (tccp->numresolutions > J2K_MAXRLVLS) {
757 opj_event_msg(j2k->cinfo, EVT_ERROR,
758 "Error decoding component %d.\nThe number of resolutions is too big: %d vs max= %d. Truncating.\n\n",
759 compno, tccp->numresolutions, J2K_MAXRLVLS);
760 j2k->state |= J2K_STATE_ERR;
761 tccp->numresolutions = J2K_MAXRLVLS;
762 }
763
764 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
765 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
766 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
767 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
768 if (tccp->csty & J2K_CP_CSTY_PRT) {
769 for (i = 0; i < tccp->numresolutions; i++) {
770 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
771 tccp->prcw[i] = tmp & 0xf;
772 tccp->prch[i] = tmp >> 4;
773 }
774 }
775
776 /* INDEX >> */
777 if (j2k->cstr_info && compno == 0) {
778 for (i = 0; i < tccp->numresolutions; i++) {
779 if (tccp->csty & J2K_CP_CSTY_PRT) {
780 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
781 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
782 } else {
783 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
784 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
785 }
786 }
787 }
788 /* << INDEX */
789 }
790
j2k_write_cod(opj_j2k_t * j2k)791 static void j2k_write_cod(opj_j2k_t *j2k)
792 {
793 opj_cp_t *cp = NULL;
794 opj_tcp_t *tcp = NULL;
795 int lenp, len;
796
797 opj_cio_t *cio = j2k->cio;
798
799 cio_write(cio, J2K_MS_COD, 2); /* COD */
800
801 lenp = cio_tell(cio);
802 cio_skip(cio, 2);
803
804 cp = j2k->cp;
805 tcp = &cp->tcps[j2k->curtileno];
806
807 cio_write(cio, tcp->csty, 1); /* Scod */
808 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
809 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
810 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
811
812 j2k_write_cox(j2k, 0);
813 len = cio_tell(cio) - lenp;
814 cio_seek(cio, lenp);
815 cio_write(cio, len, 2); /* Lcod */
816 cio_seek(cio, lenp + len);
817
818 if (j2k->cstr_info) {
819 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
820 }
821
822 }
823
j2k_read_cod(opj_j2k_t * j2k)824 static void j2k_read_cod(opj_j2k_t *j2k)
825 {
826 int len, i, pos;
827
828 opj_cio_t *cio = j2k->cio;
829 opj_cp_t *cp = j2k->cp;
830 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
831 j2k->default_tcp;
832 opj_image_t *image = j2k->image;
833
834 len = cio_read(cio, 2); /* Lcod */
835 tcp->csty = cio_read(cio, 1); /* Scod */
836 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
837 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
838 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
839
840 pos = cio_tell(cio);
841 for (i = 0; i < image->numcomps; i++) {
842 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
843 cio_seek(cio, pos);
844 j2k_read_cox(j2k, i);
845 }
846
847 /* Index */
848 if (j2k->cstr_info) {
849 opj_codestream_info_t *cstr_info = j2k->cstr_info;
850 cstr_info->prog = tcp->prg;
851 cstr_info->numlayers = tcp->numlayers;
852 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
853 for (i = 0; i < image->numcomps; i++) {
854 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
855 }
856 }
857 }
858
j2k_write_coc(opj_j2k_t * j2k,int compno)859 static void j2k_write_coc(opj_j2k_t *j2k, int compno)
860 {
861 int lenp, len;
862
863 opj_cp_t *cp = j2k->cp;
864 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
865 opj_image_t *image = j2k->image;
866 opj_cio_t *cio = j2k->cio;
867
868 cio_write(cio, J2K_MS_COC, 2); /* COC */
869 lenp = cio_tell(cio);
870 cio_skip(cio, 2);
871 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
872 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
873 j2k_write_cox(j2k, compno);
874 len = cio_tell(cio) - lenp;
875 cio_seek(cio, lenp);
876 cio_write(cio, len, 2); /* Lcoc */
877 cio_seek(cio, lenp + len);
878 }
879
j2k_read_coc(opj_j2k_t * j2k)880 static void j2k_read_coc(opj_j2k_t *j2k)
881 {
882 int len, compno;
883
884 opj_cp_t *cp = j2k->cp;
885 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
886 j2k->default_tcp;
887 opj_image_t *image = j2k->image;
888 opj_cio_t *cio = j2k->cio;
889
890 len = cio_read(cio, 2); /* Lcoc */
891 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
892 if (compno >= image->numcomps) {
893 opj_event_msg(j2k->cinfo, EVT_ERROR,
894 "bad component number in COC (%d out of a maximum of %d)\n",
895 compno, image->numcomps);
896 return;
897 }
898 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
899 j2k_read_cox(j2k, compno);
900 }
901
j2k_write_qcx(opj_j2k_t * j2k,int compno)902 static void j2k_write_qcx(opj_j2k_t *j2k, int compno)
903 {
904 int bandno, numbands;
905 int expn, mant;
906
907 opj_cp_t *cp = j2k->cp;
908 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
909 opj_tccp_t *tccp = &tcp->tccps[compno];
910 opj_cio_t *cio = j2k->cio;
911
912 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
913 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 -
914 2;
915
916 for (bandno = 0; bandno < numbands; bandno++) {
917 expn = tccp->stepsizes[bandno].expn;
918 mant = tccp->stepsizes[bandno].mant;
919
920 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
921 cio_write(cio, expn << 3, 1); /* SPqcx_i */
922 } else {
923 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
924 }
925 }
926 }
927
j2k_read_qcx(opj_j2k_t * j2k,int compno,int len)928 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len)
929 {
930 int tmp;
931 int bandno, numbands;
932
933 opj_cp_t *cp = j2k->cp;
934 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
935 j2k->default_tcp;
936 opj_tccp_t *tccp = &tcp->tccps[compno];
937 opj_cio_t *cio = j2k->cio;
938
939 tmp = cio_read(cio, 1); /* Sqcx */
940 tccp->qntsty = tmp & 0x1f;
941 tccp->numgbits = tmp >> 5;
942 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
943 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
944
945 #ifdef USE_JPWL
946 if (j2k->cp->correct) {
947
948 /* if JPWL is on, we check whether there are too many subbands */
949 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
950 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
951 "JPWL: bad number of subbands in Sqcx (%d)\n",
952 numbands);
953 if (!JPWL_ASSUME) {
954 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
955 return;
956 }
957 /* we try to correct */
958 numbands = 1;
959 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
960 "- setting number of bands to %d => HYPOTHESIS!!!\n",
961 numbands);
962 };
963
964 };
965
966 #else
967 /* We check whether there are too many subbands */
968 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
969 opj_event_msg(j2k->cinfo, EVT_WARNING,
970 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
971 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n",
972 numbands, J2K_MAXBANDS);
973 }
974
975 #endif /* USE_JPWL */
976
977 for (bandno = 0; bandno < numbands; bandno++) {
978 int expn, mant;
979 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
980 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
981 mant = 0;
982 } else {
983 tmp = cio_read(cio, 2); /* SPqcx_i */
984 expn = tmp >> 11;
985 mant = tmp & 0x7ff;
986 }
987 if (bandno < J2K_MAXBANDS) {
988 tccp->stepsizes[bandno].expn = expn;
989 tccp->stepsizes[bandno].mant = mant;
990 }
991 }
992
993 /* Add Antonin : if scalar_derived -> compute other stepsizes */
994 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
995 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
996 tccp->stepsizes[bandno].expn =
997 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
998 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
999 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1000 }
1001 }
1002 /* ddA */
1003 }
1004
j2k_write_qcd(opj_j2k_t * j2k)1005 static void j2k_write_qcd(opj_j2k_t *j2k)
1006 {
1007 int lenp, len;
1008
1009 opj_cio_t *cio = j2k->cio;
1010
1011 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
1012 lenp = cio_tell(cio);
1013 cio_skip(cio, 2);
1014 j2k_write_qcx(j2k, 0);
1015 len = cio_tell(cio) - lenp;
1016 cio_seek(cio, lenp);
1017 cio_write(cio, len, 2); /* Lqcd */
1018 cio_seek(cio, lenp + len);
1019
1020 if (j2k->cstr_info) {
1021 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
1022 }
1023 }
1024
j2k_read_qcd(opj_j2k_t * j2k)1025 static void j2k_read_qcd(opj_j2k_t *j2k)
1026 {
1027 int len, i, pos;
1028
1029 opj_cio_t *cio = j2k->cio;
1030 opj_image_t *image = j2k->image;
1031
1032 len = cio_read(cio, 2); /* Lqcd */
1033 pos = cio_tell(cio);
1034 for (i = 0; i < image->numcomps; i++) {
1035 cio_seek(cio, pos);
1036 j2k_read_qcx(j2k, i, len - 2);
1037 }
1038 }
1039
j2k_write_qcc(opj_j2k_t * j2k,int compno)1040 static void j2k_write_qcc(opj_j2k_t *j2k, int compno)
1041 {
1042 int lenp, len;
1043
1044 opj_cio_t *cio = j2k->cio;
1045
1046 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
1047 lenp = cio_tell(cio);
1048 cio_skip(cio, 2);
1049 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
1050 j2k_write_qcx(j2k, compno);
1051 len = cio_tell(cio) - lenp;
1052 cio_seek(cio, lenp);
1053 cio_write(cio, len, 2); /* Lqcc */
1054 cio_seek(cio, lenp + len);
1055 }
1056
j2k_read_qcc(opj_j2k_t * j2k)1057 static void j2k_read_qcc(opj_j2k_t *j2k)
1058 {
1059 int len, compno;
1060 int numcomp = j2k->image->numcomps;
1061 opj_cio_t *cio = j2k->cio;
1062
1063 len = cio_read(cio, 2); /* Lqcc */
1064 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1065
1066 #ifdef USE_JPWL
1067 if (j2k->cp->correct) {
1068
1069 static int backup_compno = 0;
1070
1071 /* compno is negative or larger than the number of components!!! */
1072 if ((compno < 0) || (compno >= numcomp)) {
1073 opj_event_msg(j2k->cinfo, EVT_ERROR,
1074 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
1075 compno, numcomp);
1076 if (!JPWL_ASSUME) {
1077 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1078 return;
1079 }
1080 /* we try to correct */
1081 compno = backup_compno % numcomp;
1082 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1083 "- setting component number to %d\n",
1084 compno);
1085 }
1086
1087 /* keep your private count of tiles */
1088 backup_compno++;
1089 }
1090 #endif /* USE_JPWL */
1091
1092 if ((compno < 0) || (compno >= numcomp)) {
1093 opj_event_msg(j2k->cinfo, EVT_ERROR,
1094 "bad component number in QCC (%d out of a maximum of %d)\n",
1095 compno, j2k->image->numcomps);
1096 return;
1097 }
1098
1099 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1100 }
1101
j2k_write_poc(opj_j2k_t * j2k)1102 static void j2k_write_poc(opj_j2k_t *j2k)
1103 {
1104 int len, numpchgs, i;
1105
1106 int numcomps = j2k->image->numcomps;
1107
1108 opj_cp_t *cp = j2k->cp;
1109 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1110 opj_tccp_t *tccp = &tcp->tccps[0];
1111 opj_cio_t *cio = j2k->cio;
1112
1113 numpchgs = 1 + tcp->numpocs;
1114 cio_write(cio, J2K_MS_POC, 2); /* POC */
1115 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1116 cio_write(cio, len, 2); /* Lpoc */
1117 for (i = 0; i < numpchgs; i++) {
1118 opj_poc_t *poc = &tcp->pocs[i];
1119 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1120 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1121 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1122 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1123 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1124 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
1125 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1126 poc->compno1 = int_min(poc->compno1, numcomps);
1127 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1128 }
1129 }
1130
j2k_read_poc(opj_j2k_t * j2k)1131 static void j2k_read_poc(opj_j2k_t *j2k)
1132 {
1133 int len, numpchgs, i, old_poc;
1134
1135 int numcomps = j2k->image->numcomps;
1136
1137 opj_cp_t *cp = j2k->cp;
1138 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
1139 j2k->default_tcp;
1140 opj_cio_t *cio = j2k->cio;
1141
1142 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1143 tcp->POC = 1;
1144 len = cio_read(cio, 2); /* Lpoc */
1145 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1146
1147 for (i = old_poc; i < numpchgs + old_poc; i++) {
1148 opj_poc_t *poc;
1149 poc = &tcp->pocs[i];
1150 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1151 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1152 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
1153 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1154 poc->compno1 = int_min(
1155 cio_read(cio, numcomps <= 256 ? 1 : 2),
1156 (unsigned int) numcomps); /* CEpoc_i */
1157 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1158 }
1159
1160 tcp->numpocs = numpchgs + old_poc - 1;
1161 }
1162
j2k_read_crg(opj_j2k_t * j2k)1163 static void j2k_read_crg(opj_j2k_t *j2k)
1164 {
1165 int len, i, Xcrg_i, Ycrg_i;
1166
1167 opj_cio_t *cio = j2k->cio;
1168 int numcomps = j2k->image->numcomps;
1169
1170 len = cio_read(cio, 2); /* Lcrg */
1171 for (i = 0; i < numcomps; i++) {
1172 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1173 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1174 }
1175 }
1176
j2k_read_tlm(opj_j2k_t * j2k)1177 static void j2k_read_tlm(opj_j2k_t *j2k)
1178 {
1179 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1180 long int Ttlm_i, Ptlm_i;
1181
1182 opj_cio_t *cio = j2k->cio;
1183
1184 len = cio_read(cio, 2); /* Ltlm */
1185 Ztlm = cio_read(cio, 1); /* Ztlm */
1186 Stlm = cio_read(cio, 1); /* Stlm */
1187 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1188 SP = (Stlm >> 6) & 0x01;
1189 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1190 for (i = 0; i < tile_tlm; i++) {
1191 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1192 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1193 }
1194 }
1195
j2k_read_plm(opj_j2k_t * j2k)1196 static void j2k_read_plm(opj_j2k_t *j2k)
1197 {
1198 int len, i, Zplm, Nplm, add, packet_len = 0;
1199
1200 opj_cio_t *cio = j2k->cio;
1201
1202 len = cio_read(cio, 2); /* Lplm */
1203 Zplm = cio_read(cio, 1); /* Zplm */
1204 len -= 3;
1205 while (len > 0) {
1206 Nplm = cio_read(cio, 4); /* Nplm */
1207 len -= 4;
1208 for (i = Nplm; i > 0; i--) {
1209 add = cio_read(cio, 1);
1210 len--;
1211 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1212 if ((add & 0x80) == 0) {
1213 /* New packet */
1214 packet_len = 0;
1215 }
1216 if (len <= 0) {
1217 break;
1218 }
1219 }
1220 }
1221 }
1222
j2k_read_plt(opj_j2k_t * j2k)1223 static void j2k_read_plt(opj_j2k_t *j2k)
1224 {
1225 int len, i, Zplt, packet_len = 0, add;
1226
1227 opj_cio_t *cio = j2k->cio;
1228
1229 len = cio_read(cio, 2); /* Lplt */
1230 Zplt = cio_read(cio, 1); /* Zplt */
1231 for (i = len - 3; i > 0; i--) {
1232 add = cio_read(cio, 1);
1233 packet_len = (packet_len << 7) + add; /* Iplt_i */
1234 if ((add & 0x80) == 0) {
1235 /* New packet */
1236 packet_len = 0;
1237 }
1238 }
1239 }
1240
j2k_read_ppm(opj_j2k_t * j2k)1241 static void j2k_read_ppm(opj_j2k_t *j2k)
1242 {
1243 int len, Z_ppm, i, j;
1244 int N_ppm;
1245
1246 opj_cp_t *cp = j2k->cp;
1247 opj_cio_t *cio = j2k->cio;
1248
1249 len = cio_read(cio, 2);
1250 cp->ppm = 1;
1251
1252 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1253 len -= 3;
1254 while (len > 0) {
1255 if (cp->ppm_previous == 0) {
1256 N_ppm = cio_read(cio, 4); /* N_ppm */
1257 len -= 4;
1258 } else {
1259 N_ppm = cp->ppm_previous;
1260 }
1261 j = cp->ppm_store;
1262 if (Z_ppm == 0) { /* First PPM marker */
1263 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1264 cp->ppm_data_first = cp->ppm_data;
1265 cp->ppm_len = N_ppm;
1266 } else { /* NON-first PPM marker */
1267 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data,
1268 (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1269
1270 #ifdef USE_JPWL
1271 /* this memory allocation check could be done even in non-JPWL cases */
1272 if (cp->correct) {
1273 if (!cp->ppm_data) {
1274 opj_event_msg(j2k->cinfo, EVT_ERROR,
1275 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1276 cio_tell(cio));
1277 if (!JPWL_ASSUME || JPWL_ASSUME) {
1278 opj_free(cp->ppm_data);
1279 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1280 return;
1281 }
1282 }
1283 }
1284 #endif
1285
1286 cp->ppm_data_first = cp->ppm_data;
1287 cp->ppm_len = N_ppm + cp->ppm_store;
1288 }
1289 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1290 cp->ppm_data[j] = cio_read(cio, 1);
1291 j++;
1292 len--;
1293 if (len == 0) {
1294 break; /* Case of non-finished packet header in present marker but finished in next one */
1295 }
1296 }
1297 cp->ppm_previous = i - 1;
1298 cp->ppm_store = j;
1299 }
1300 }
1301
j2k_read_ppt(opj_j2k_t * j2k)1302 static void j2k_read_ppt(opj_j2k_t *j2k)
1303 {
1304 int len, Z_ppt, i, j = 0;
1305
1306 opj_cp_t *cp = j2k->cp;
1307 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1308 opj_cio_t *cio = j2k->cio;
1309
1310 len = cio_read(cio, 2);
1311 Z_ppt = cio_read(cio, 1);
1312 tcp->ppt = 1;
1313 if (Z_ppt == 0) { /* First PPT marker */
1314 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1315 tcp->ppt_data_first = tcp->ppt_data;
1316 tcp->ppt_store = 0;
1317 tcp->ppt_len = len - 3;
1318 } else { /* NON-first PPT marker */
1319 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data,
1320 (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1321 tcp->ppt_data_first = tcp->ppt_data;
1322 tcp->ppt_len = len - 3 + tcp->ppt_store;
1323 }
1324 j = tcp->ppt_store;
1325 for (i = len - 3; i > 0; i--) {
1326 tcp->ppt_data[j] = cio_read(cio, 1);
1327 j++;
1328 }
1329 tcp->ppt_store = j;
1330 }
1331
j2k_write_tlm(opj_j2k_t * j2k)1332 static void j2k_write_tlm(opj_j2k_t *j2k)
1333 {
1334 int lenp;
1335 opj_cio_t *cio = j2k->cio;
1336 j2k->tlm_start = cio_tell(cio);
1337 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1338 lenp = 4 + (5 * j2k->totnum_tp);
1339 cio_write(cio, lenp, 2); /* Ltlm */
1340 cio_write(cio, 0, 1); /* Ztlm=0*/
1341 cio_write(cio, 80,
1342 1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1343 cio_skip(cio, 5 * j2k->totnum_tp);
1344 }
1345
j2k_write_sot(opj_j2k_t * j2k)1346 static void j2k_write_sot(opj_j2k_t *j2k)
1347 {
1348 int lenp, len;
1349
1350 opj_cio_t *cio = j2k->cio;
1351
1352 j2k->sot_start = cio_tell(cio);
1353 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1354 lenp = cio_tell(cio);
1355 cio_skip(cio, 2); /* Lsot (further) */
1356 cio_write(cio, j2k->curtileno, 2); /* Isot */
1357 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1358 cio_write(cio, j2k->cur_tp_num, 1); /* TPsot */
1359 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1360 len = cio_tell(cio) - lenp;
1361 cio_seek(cio, lenp);
1362 cio_write(cio, len, 2); /* Lsot */
1363 cio_seek(cio, lenp + len);
1364
1365 /* UniPG>> */
1366 #ifdef USE_JPWL
1367 /* update markers struct */
1368 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1369 #endif /* USE_JPWL */
1370 /* <<UniPG */
1371
1372 if (j2k->cstr_info && j2k->cur_tp_num == 0) {
1373 j2k_add_tlmarker(j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
1374 }
1375 }
1376
j2k_read_sot(opj_j2k_t * j2k)1377 static void j2k_read_sot(opj_j2k_t *j2k)
1378 {
1379 int len, tileno, totlen, partno, numparts, i;
1380 opj_tcp_t *tcp = NULL;
1381 char status = 0;
1382
1383 opj_cp_t *cp = j2k->cp;
1384 opj_cio_t *cio = j2k->cio;
1385
1386 len = cio_read(cio, 2);
1387 tileno = cio_read(cio, 2);
1388
1389 #ifdef USE_JPWL
1390 if (j2k->cp->correct) {
1391
1392 static int backup_tileno = 0;
1393
1394 /* tileno is negative or larger than the number of tiles!!! */
1395 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1396 opj_event_msg(j2k->cinfo, EVT_ERROR,
1397 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1398 tileno, (cp->tw * cp->th));
1399 if (!JPWL_ASSUME) {
1400 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1401 return;
1402 }
1403 /* we try to correct */
1404 tileno = backup_tileno;
1405 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1406 "- setting tile number to %d\n",
1407 tileno);
1408 }
1409
1410 /* keep your private count of tiles */
1411 backup_tileno++;
1412 } else
1413 #endif /* USE_JPWL */
1414 {
1415 /* tileno is negative or larger than the number of tiles!!! */
1416 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1417 opj_event_msg(j2k->cinfo, EVT_ERROR,
1418 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1419 tileno, (cp->tw * cp->th));
1420 return;
1421 }
1422 }
1423
1424 if (cp->tileno_size == 0) {
1425 cp->tileno[cp->tileno_size] = tileno;
1426 cp->tileno_size++;
1427 } else {
1428 i = 0;
1429 while (i < cp->tileno_size && status == 0) {
1430 status = cp->tileno[i] == tileno ? 1 : 0;
1431 i++;
1432 }
1433 if (status == 0) {
1434 cp->tileno[cp->tileno_size] = tileno;
1435 cp->tileno_size++;
1436 }
1437 }
1438
1439 totlen = cio_read(cio, 4);
1440
1441 #ifdef USE_JPWL
1442 if (j2k->cp->correct) {
1443
1444 /* totlen is negative or larger than the bytes left!!! */
1445 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1446 opj_event_msg(j2k->cinfo, EVT_ERROR,
1447 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1448 totlen, cio_numbytesleft(cio) + 8);
1449 if (!JPWL_ASSUME) {
1450 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1451 return;
1452 }
1453 /* we try to correct */
1454 totlen = 0;
1455 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1456 "- setting Psot to %d => assuming it is the last tile\n",
1457 totlen);
1458 }
1459
1460 } else
1461 #endif /* USE_JPWL */
1462 {
1463 /* totlen is negative or larger than the bytes left!!! */
1464 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1465 opj_event_msg(j2k->cinfo, EVT_ERROR,
1466 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1467 totlen, cio_numbytesleft(cio) + 8);
1468 return;
1469 }
1470 }
1471
1472 if (!totlen) {
1473 totlen = cio_numbytesleft(cio) + 8;
1474 }
1475
1476 partno = cio_read(cio, 1);
1477 numparts = cio_read(cio, 1);
1478
1479 if (partno >= numparts) {
1480 opj_event_msg(j2k->cinfo, EVT_WARNING,
1481 "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n",
1482 tileno, partno, numparts);
1483 numparts = partno + 1;
1484 }
1485
1486 j2k->curtileno = tileno;
1487 j2k->cur_tp_num = partno;
1488 j2k->eot = cio_getbp(cio) - 12 + totlen;
1489 j2k->state = J2K_STATE_TPH;
1490 tcp = &cp->tcps[j2k->curtileno];
1491
1492 /* Index */
1493 if (j2k->cstr_info) {
1494 if (tcp->first) {
1495 if (tileno == 0) {
1496 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1497 }
1498 j2k->cstr_info->tile[tileno].tileno = tileno;
1499 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1500 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos +
1501 totlen - 1;
1502 } else {
1503 j2k->cstr_info->tile[tileno].end_pos += totlen;
1504 }
1505 j2k->cstr_info->tile[tileno].num_tps = numparts;
1506 if (numparts) {
1507 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(
1508 j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
1509 } else {
1510 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(
1511 j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
1512 }
1513 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1514 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1515 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1516 }
1517
1518 if (tcp->first == 1) {
1519 /* Initialization PPT */
1520 opj_tccp_t *tmp = tcp->tccps;
1521 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1522 tcp->ppt = 0;
1523 tcp->ppt_data = NULL;
1524 tcp->ppt_data_first = NULL;
1525 tcp->tccps = tmp;
1526
1527 for (i = 0; i < j2k->image->numcomps; i++) {
1528 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1529 }
1530 cp->tcps[j2k->curtileno].first = 0;
1531 }
1532 }
1533
j2k_write_sod(opj_j2k_t * j2k,void * tile_coder)1534 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder)
1535 {
1536 int l, layno;
1537 int totlen;
1538 opj_tcp_t *tcp = NULL;
1539 opj_codestream_info_t *cstr_info = NULL;
1540
1541 opj_tcd_t *tcd = (opj_tcd_t*)
1542 tile_coder; /* cast is needed because of conflicts in header inclusions */
1543 opj_cp_t *cp = j2k->cp;
1544 opj_cio_t *cio = j2k->cio;
1545
1546 tcd->tp_num = j2k->tp_num ;
1547 tcd->cur_tp_num = j2k->cur_tp_num;
1548
1549 cio_write(cio, J2K_MS_SOD, 2);
1550
1551 if (j2k->cstr_info && j2k->cur_tp_num == 0) {
1552 j2k_add_tlmarker(j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
1553 }
1554
1555 if (j2k->curtileno == 0) {
1556 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1557 }
1558
1559 /* INDEX >> */
1560 cstr_info = j2k->cstr_info;
1561 if (cstr_info) {
1562 if (!j2k->cur_tp_num) {
1563 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction
1564 - 1;
1565 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1566 } else {
1567 if (cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos <
1568 cio_tell(cio)) {
1569 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(
1570 cio);
1571 }
1572 }
1573 /* UniPG>> */
1574 #ifdef USE_JPWL
1575 /* update markers struct */
1576 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1577 #endif /* USE_JPWL */
1578 /* <<UniPG */
1579 }
1580 /* << INDEX */
1581
1582 tcp = &cp->tcps[j2k->curtileno];
1583 for (layno = 0; layno < tcp->numlayers; layno++) {
1584 if (tcp->rates[layno] > (j2k->sod_start / (cp->th * cp->tw))) {
1585 tcp->rates[layno] -= (j2k->sod_start / (cp->th * cp->tw));
1586 } else if (tcp->rates[layno]) {
1587 tcp->rates[layno] = 1;
1588 }
1589 }
1590 if (j2k->cur_tp_num == 0) {
1591 tcd->tcd_image->tiles->packno = 0;
1592 if (cstr_info) {
1593 cstr_info->packno = 0;
1594 }
1595 }
1596
1597 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio),
1598 cio_numbytesleft(cio) - 2, cstr_info);
1599
1600 /* Writing Psot in SOT marker */
1601 totlen = cio_tell(cio) + l - j2k->sot_start;
1602 cio_seek(cio, j2k->sot_start + 6);
1603 cio_write(cio, totlen, 4);
1604 cio_seek(cio, j2k->sot_start + totlen);
1605 /* Writing Ttlm and Ptlm in TLM marker */
1606 if (cp->cinema) {
1607 cio_seek(cio, j2k->tlm_start + 6 + (5 * j2k->cur_tp_num));
1608 cio_write(cio, j2k->curtileno, 1);
1609 cio_write(cio, totlen, 4);
1610 }
1611 cio_seek(cio, j2k->sot_start + totlen);
1612 }
1613
j2k_read_sod(opj_j2k_t * j2k)1614 static void j2k_read_sod(opj_j2k_t *j2k)
1615 {
1616 int len, truncate = 0, i;
1617 unsigned char *data = NULL, *data_ptr = NULL;
1618
1619 opj_cio_t *cio = j2k->cio;
1620 int curtileno = j2k->curtileno;
1621
1622 /* Index */
1623 if (j2k->cstr_info) {
1624 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1625 cio_tell(cio) + j2k->pos_correction - 1;
1626 if (j2k->cur_tp_num == 0) {
1627 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(
1628 cio) + j2k->pos_correction - 1;
1629 }
1630 j2k->cstr_info->packno = 0;
1631 }
1632
1633 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1634
1635 if (len == cio_numbytesleft(cio) + 1) {
1636 truncate = 1; /* Case of a truncate codestream */
1637 }
1638
1639 data = j2k->tile_data[curtileno];
1640 data = (unsigned char*) opj_realloc(data,
1641 (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1642
1643 data_ptr = data + j2k->tile_len[curtileno];
1644 for (i = 0; i < len; i++) {
1645 data_ptr[i] = cio_read(cio, 1);
1646 }
1647
1648 j2k->tile_len[curtileno] += len;
1649 j2k->tile_data[curtileno] = data;
1650
1651 if (!truncate) {
1652 j2k->state = J2K_STATE_TPHSOT;
1653 } else {
1654 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1655 }
1656 j2k->cur_tp_num++;
1657 }
1658
j2k_write_rgn(opj_j2k_t * j2k,int compno,int tileno)1659 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno)
1660 {
1661 opj_cp_t *cp = j2k->cp;
1662 opj_tcp_t *tcp = &cp->tcps[tileno];
1663 opj_cio_t *cio = j2k->cio;
1664 int numcomps = j2k->image->numcomps;
1665
1666 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1667 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1668 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1669 cio_write(cio, 0, 1); /* Srgn */
1670 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1671 }
1672
j2k_read_rgn(opj_j2k_t * j2k)1673 static void j2k_read_rgn(opj_j2k_t *j2k)
1674 {
1675 int len, compno, roisty;
1676
1677 opj_cp_t *cp = j2k->cp;
1678 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
1679 j2k->default_tcp;
1680 opj_cio_t *cio = j2k->cio;
1681 int numcomps = j2k->image->numcomps;
1682
1683 len = cio_read(cio, 2); /* Lrgn */
1684 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1685 roisty = cio_read(cio, 1); /* Srgn */
1686
1687 #ifdef USE_JPWL
1688 if (j2k->cp->correct) {
1689 /* totlen is negative or larger than the bytes left!!! */
1690 if (compno >= numcomps) {
1691 opj_event_msg(j2k->cinfo, EVT_ERROR,
1692 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1693 compno, numcomps);
1694 if (!JPWL_ASSUME || JPWL_ASSUME) {
1695 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1696 return;
1697 }
1698 }
1699 };
1700 #endif /* USE_JPWL */
1701
1702 if (compno >= numcomps) {
1703 opj_event_msg(j2k->cinfo, EVT_ERROR,
1704 "bad component number in RGN (%d out of a maximum of %d)\n",
1705 compno, j2k->image->numcomps);
1706 return;
1707 }
1708
1709 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1710 }
1711
j2k_write_eoc(opj_j2k_t * j2k)1712 static void j2k_write_eoc(opj_j2k_t *j2k)
1713 {
1714 opj_cio_t *cio = j2k->cio;
1715 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1716 cio_write(cio, J2K_MS_EOC, 2);
1717
1718 /* UniPG>> */
1719 #ifdef USE_JPWL
1720 /* update markers struct */
1721 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1722 #endif /* USE_JPWL */
1723 /* <<UniPG */
1724 }
1725
j2k_read_eoc(opj_j2k_t * j2k)1726 static void j2k_read_eoc(opj_j2k_t *j2k)
1727 {
1728 int i, tileno;
1729 opj_bool success = OPJ_FALSE;
1730
1731 /* if packets should be decoded */
1732 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1733 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1734 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1735 for (i = 0; i < j2k->cp->tileno_size; i++) {
1736 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1737 if (j2k->cp->tileno[i] != -1) {
1738 tileno = j2k->cp->tileno[i];
1739 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno],
1740 tileno, j2k->cstr_info);
1741 opj_free(j2k->tile_data[tileno]);
1742 j2k->tile_data[tileno] = NULL;
1743 tcd_free_decode_tile(tcd, i);
1744 } else {
1745 success = OPJ_FALSE;
1746 }
1747 if (success == OPJ_FALSE) {
1748 j2k->state |= J2K_STATE_ERR;
1749 break;
1750 }
1751 }
1752 tcd_free_decode(tcd);
1753 tcd_destroy(tcd);
1754 }
1755 /* if packets should not be decoded */
1756 else {
1757 for (i = 0; i < j2k->cp->tileno_size; i++) {
1758 tileno = j2k->cp->tileno[i];
1759 opj_free(j2k->tile_data[tileno]);
1760 j2k->tile_data[tileno] = NULL;
1761 }
1762 }
1763 if (j2k->state & J2K_STATE_ERR) {
1764 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1765 } else {
1766 j2k->state = J2K_STATE_MT;
1767 }
1768 }
1769
1770 typedef struct opj_dec_mstabent {
1771 /** marker value */
1772 int id;
1773 /** value of the state when the marker can appear */
1774 int states;
1775 /** action linked to the marker */
1776 void (*handler)(opj_j2k_t *j2k);
1777 } opj_dec_mstabent_t;
1778
1779 opj_dec_mstabent_t j2k_dec_mstab[] = {
1780 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1781 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1782 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1783 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1784 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1785 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1786 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1787 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1788 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1789 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1790 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1791 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1792 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1793 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1794 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1795 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1796 {J2K_MS_SOP, 0, 0},
1797 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1798 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1799
1800 #ifdef USE_JPWL
1801 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1802 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1803 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1804 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1805 #endif /* USE_JPWL */
1806 #ifdef USE_JPSEC
1807 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1808 {J2K_MS_INSEC, 0, j2k_read_insec},
1809 #endif /* USE_JPSEC */
1810
1811 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1812 };
1813
j2k_read_unk(opj_j2k_t * j2k)1814 static void j2k_read_unk(opj_j2k_t *j2k)
1815 {
1816 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1817
1818 #ifdef USE_JPWL
1819 if (j2k->cp->correct) {
1820 int m = 0, id, i;
1821 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1822 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1823 id = cio_read(j2k->cio, 2);
1824 opj_event_msg(j2k->cinfo, EVT_ERROR,
1825 "JPWL: really don't know this marker %x\n",
1826 id);
1827 if (!JPWL_ASSUME) {
1828 opj_event_msg(j2k->cinfo, EVT_ERROR,
1829 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1830 return;
1831 }
1832 /* OK, activate this at your own risk!!! */
1833 /* we look for the marker at the minimum hamming distance from this */
1834 while (j2k_dec_mstab[m].id) {
1835
1836 /* 1's where they differ */
1837 tmp_id = j2k_dec_mstab[m].id ^ id;
1838
1839 /* compute the hamming distance between our id and the current */
1840 cur_dist = 0;
1841 for (i = 0; i < 16; i++) {
1842 if ((tmp_id >> i) & 0x0001) {
1843 cur_dist++;
1844 }
1845 }
1846
1847 /* if current distance is smaller, set the minimum */
1848 if (cur_dist < min_dist) {
1849 min_dist = cur_dist;
1850 min_id = j2k_dec_mstab[m].id;
1851 }
1852
1853 /* jump to the next marker */
1854 m++;
1855 }
1856
1857 /* do we substitute the marker? */
1858 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1859 opj_event_msg(j2k->cinfo, EVT_ERROR,
1860 "- marker %x is at distance %d from the read %x\n",
1861 min_id, min_dist, id);
1862 opj_event_msg(j2k->cinfo, EVT_ERROR,
1863 "- trying to substitute in place and crossing fingers!\n");
1864 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1865 cio_write(j2k->cio, min_id, 2);
1866
1867 /* rewind */
1868 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1869
1870 }
1871
1872 };
1873 #endif /* USE_JPWL */
1874
1875 }
1876
1877 /**
1878 Read the lookup table containing all the marker, status and action
1879 @param id Marker value
1880 */
j2k_dec_mstab_lookup(int id)1881 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id)
1882 {
1883 opj_dec_mstabent_t *e;
1884 for (e = j2k_dec_mstab; e->id != 0; e++) {
1885 if (e->id == id) {
1886 break;
1887 }
1888 }
1889 return e;
1890 }
1891
1892 /* ----------------------------------------------------------------------- */
1893 /* J2K / JPT decoder interface */
1894 /* ----------------------------------------------------------------------- */
1895
j2k_create_decompress(opj_common_ptr cinfo)1896 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo)
1897 {
1898 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1899 if (!j2k) {
1900 return NULL;
1901 }
1902
1903 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1904 if (!j2k->default_tcp) {
1905 opj_free(j2k);
1906 return NULL;
1907 }
1908
1909 j2k->cinfo = cinfo;
1910 j2k->tile_data = NULL;
1911
1912 return j2k;
1913 }
1914
j2k_destroy_decompress(opj_j2k_t * j2k)1915 void j2k_destroy_decompress(opj_j2k_t *j2k)
1916 {
1917 int i = 0;
1918
1919 if (j2k->tile_len != NULL) {
1920 opj_free(j2k->tile_len);
1921 }
1922 if (j2k->tile_data != NULL) {
1923 if (j2k->cp != NULL) {
1924 for (i = 0; i < j2k->cp->tileno_size; i++) {
1925 int tileno = j2k->cp->tileno[i];
1926 opj_free(j2k->tile_data[tileno]);
1927 j2k->tile_data[tileno] = NULL;
1928 }
1929 }
1930
1931 opj_free(j2k->tile_data);
1932 }
1933 if (j2k->default_tcp != NULL) {
1934 opj_tcp_t *default_tcp = j2k->default_tcp;
1935 if (default_tcp->ppt_data_first != NULL) {
1936 opj_free(default_tcp->ppt_data_first);
1937 }
1938 if (j2k->default_tcp->tccps != NULL) {
1939 opj_free(j2k->default_tcp->tccps);
1940 }
1941 opj_free(j2k->default_tcp);
1942 }
1943 if (j2k->cp != NULL) {
1944 opj_cp_t *cp = j2k->cp;
1945 if (cp->tcps != NULL) {
1946 for (i = 0; i < cp->tw * cp->th; i++) {
1947 if (cp->tcps[i].ppt_data_first != NULL) {
1948 opj_free(cp->tcps[i].ppt_data_first);
1949 }
1950 if (cp->tcps[i].tccps != NULL) {
1951 opj_free(cp->tcps[i].tccps);
1952 }
1953 }
1954 opj_free(cp->tcps);
1955 }
1956 if (cp->ppm_data_first != NULL) {
1957 opj_free(cp->ppm_data_first);
1958 }
1959 if (cp->tileno != NULL) {
1960 opj_free(cp->tileno);
1961 }
1962 if (cp->comment != NULL) {
1963 opj_free(cp->comment);
1964 }
1965
1966 opj_free(cp);
1967 }
1968 opj_free(j2k);
1969 }
1970
j2k_setup_decoder(opj_j2k_t * j2k,opj_dparameters_t * parameters)1971 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
1972 {
1973 if (j2k && parameters) {
1974 /* create and initialize the coding parameters structure */
1975 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1976 cp->reduce = parameters->cp_reduce;
1977 cp->layer = parameters->cp_layer;
1978 cp->limit_decoding = parameters->cp_limit_decoding;
1979
1980 #ifdef USE_JPWL
1981 cp->correct = parameters->jpwl_correct;
1982 cp->exp_comps = parameters->jpwl_exp_comps;
1983 cp->max_tiles = parameters->jpwl_max_tiles;
1984 #endif /* USE_JPWL */
1985
1986
1987 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1988 j2k->cp = cp;
1989 }
1990 }
1991
j2k_decode(opj_j2k_t * j2k,opj_cio_t * cio,opj_codestream_info_t * cstr_info)1992 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio,
1993 opj_codestream_info_t *cstr_info)
1994 {
1995 opj_image_t *image = NULL;
1996
1997 opj_common_ptr cinfo = j2k->cinfo;
1998
1999 j2k->cio = cio;
2000 j2k->cstr_info = cstr_info;
2001 if (cstr_info) {
2002 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
2003 }
2004
2005 /* create an empty image */
2006 image = opj_image_create0();
2007 j2k->image = image;
2008
2009 j2k->state = J2K_STATE_MHSOC;
2010
2011 for (;;) {
2012 opj_dec_mstabent_t *e;
2013 int id = cio_read(cio, 2);
2014
2015 #ifdef USE_JPWL
2016 /* we try to honor JPWL correction power */
2017 if (j2k->cp->correct) {
2018
2019 int orig_pos = cio_tell(cio);
2020 opj_bool status;
2021
2022 /* call the corrector */
2023 status = jpwl_correct(j2k);
2024
2025 /* go back to where you were */
2026 cio_seek(cio, orig_pos - 2);
2027
2028 /* re-read the marker */
2029 id = cio_read(cio, 2);
2030
2031 /* check whether it begins with ff */
2032 if (id >> 8 != 0xff) {
2033 opj_event_msg(cinfo, EVT_ERROR,
2034 "JPWL: possible bad marker %x at %d\n",
2035 id, cio_tell(cio) - 2);
2036 if (!JPWL_ASSUME) {
2037 opj_image_destroy(image);
2038 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
2039 return 0;
2040 }
2041 /* we try to correct */
2042 id = id | 0xff00;
2043 cio_seek(cio, cio_tell(cio) - 2);
2044 cio_write(cio, id, 2);
2045 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
2046 "- setting marker to %x\n",
2047 id);
2048 }
2049
2050 }
2051 #endif /* USE_JPWL */
2052
2053 if (id >> 8 != 0xff) {
2054 if (cio_numbytesleft(cio) != 0) { /* not end of file reached and no EOC */
2055 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n",
2056 cio_tell(cio) - 2, id);
2057 opj_image_destroy(image);
2058 return 0;
2059 }
2060 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n",
2061 cio_tell(cio) - 2, id);
2062 j2k->state = J2K_STATE_NEOC;
2063 break;
2064 }
2065 e = j2k_dec_mstab_lookup(id);
2066 /* Check if the marker is known*/
2067 if (!(j2k->state & e->states)) {
2068 opj_image_destroy(image);
2069 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n",
2070 cio_tell(cio) - 2, id);
2071 return 0;
2072 }
2073 /* Check if the decoding is limited to the main header*/
2074 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
2075 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
2076 return image;
2077 }
2078
2079 if (e->handler) {
2080 (*e->handler)(j2k);
2081 }
2082 if (j2k->state & J2K_STATE_ERR) {
2083 opj_image_destroy(image);
2084 return NULL;
2085 }
2086
2087 if (j2k->state == J2K_STATE_MT) {
2088 break;
2089 }
2090 if (j2k->state == J2K_STATE_NEOC) {
2091 break;
2092 }
2093 }
2094 if (j2k->state == J2K_STATE_NEOC) {
2095 j2k_read_eoc(j2k);
2096 }
2097
2098 if (j2k->state != J2K_STATE_MT) {
2099 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2100 }
2101 return image;
2102 }
2103
2104 /*
2105 * Read a JPT-stream and decode file
2106 *
2107 */
j2k_decode_jpt_stream(opj_j2k_t * j2k,opj_cio_t * cio,opj_codestream_info_t * cstr_info)2108 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,
2109 opj_codestream_info_t *cstr_info)
2110 {
2111 opj_image_t *image = NULL;
2112 opj_jpt_msg_header_t header;
2113 int position;
2114 opj_common_ptr cinfo = j2k->cinfo;
2115
2116 OPJ_ARG_NOT_USED(cstr_info);
2117
2118 j2k->cio = cio;
2119
2120 /* create an empty image */
2121 image = opj_image_create0();
2122 j2k->image = image;
2123
2124 j2k->state = J2K_STATE_MHSOC;
2125
2126 /* Initialize the header */
2127 jpt_init_msg_header(&header);
2128 /* Read the first header of the message */
2129 jpt_read_msg_header(cinfo, cio, &header);
2130
2131 position = cio_tell(cio);
2132 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
2133 opj_image_destroy(image);
2134 opj_event_msg(cinfo, EVT_ERROR,
2135 "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",
2136 header.Class_Id);
2137 return 0;
2138 }
2139
2140 for (;;) {
2141 opj_dec_mstabent_t *e = NULL;
2142 int id;
2143
2144 if (!cio_numbytesleft(cio)) {
2145 j2k_read_eoc(j2k);
2146 return image;
2147 }
2148 /* data-bin read -> need to read a new header */
2149 if ((unsigned int)(cio_tell(cio) - position) == header.Msg_length) {
2150 jpt_read_msg_header(cinfo, cio, &header);
2151 position = cio_tell(cio);
2152 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
2153 opj_image_destroy(image);
2154 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
2155 return 0;
2156 }
2157 }
2158
2159 id = cio_read(cio, 2);
2160 if (id >> 8 != 0xff) {
2161 if (cio_numbytesleft(cio) != 0) { /* no end of file reached and no EOC */
2162 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n",
2163 cio_tell(cio) - 2, id);
2164 opj_image_destroy(image);
2165 return 0;
2166 }
2167 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n",
2168 cio_tell(cio) - 2, id);
2169 j2k->state = J2K_STATE_NEOC;
2170 break;
2171 }
2172 e = j2k_dec_mstab_lookup(id);
2173 if (!(j2k->state & e->states)) {
2174 opj_image_destroy(image);
2175 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n",
2176 cio_tell(cio) - 2, id);
2177 return 0;
2178 }
2179 if (e->handler) {
2180 (*e->handler)(j2k);
2181 }
2182 if (j2k->state == J2K_STATE_MT) {
2183 break;
2184 }
2185 if (j2k->state == J2K_STATE_NEOC) {
2186 break;
2187 }
2188 }
2189 if (j2k->state == J2K_STATE_NEOC) {
2190 j2k_read_eoc(j2k);
2191 }
2192
2193 if (j2k->state != J2K_STATE_MT) {
2194 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2195 }
2196
2197 return image;
2198 }
2199
2200 /* ----------------------------------------------------------------------- */
2201 /* J2K encoder interface */
2202 /* ----------------------------------------------------------------------- */
2203
j2k_create_compress(opj_common_ptr cinfo)2204 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo)
2205 {
2206 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
2207 if (j2k) {
2208 j2k->cinfo = cinfo;
2209 }
2210 return j2k;
2211 }
2212
j2k_destroy_compress(opj_j2k_t * j2k)2213 void j2k_destroy_compress(opj_j2k_t *j2k)
2214 {
2215 int tileno;
2216
2217 if (!j2k) {
2218 return;
2219 }
2220 if (j2k->cp != NULL) {
2221 opj_cp_t *cp = j2k->cp;
2222
2223 if (cp->comment) {
2224 opj_free(cp->comment);
2225 }
2226 if (cp->matrice) {
2227 opj_free(cp->matrice);
2228 }
2229 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2230 opj_free(cp->tcps[tileno].tccps);
2231 }
2232 opj_free(cp->tcps);
2233 opj_free(cp);
2234 }
2235
2236 opj_free(j2k);
2237 }
2238
j2k_setup_encoder(opj_j2k_t * j2k,opj_cparameters_t * parameters,opj_image_t * image)2239 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters,
2240 opj_image_t *image)
2241 {
2242 int i, j, tileno, numpocs_tile;
2243 opj_cp_t *cp = NULL;
2244
2245 if (!j2k || !parameters || ! image) {
2246 return;
2247 }
2248
2249 /* create and initialize the coding parameters structure */
2250 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
2251
2252 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
2253 j2k->cp = cp;
2254
2255 /* set default values for cp */
2256 cp->tw = 1;
2257 cp->th = 1;
2258
2259 /*
2260 copy user encoding parameters
2261 */
2262 cp->cinema = parameters->cp_cinema;
2263 cp->max_comp_size = parameters->max_comp_size;
2264 cp->rsiz = parameters->cp_rsiz;
2265 cp->disto_alloc = parameters->cp_disto_alloc;
2266 cp->fixed_alloc = parameters->cp_fixed_alloc;
2267 cp->fixed_quality = parameters->cp_fixed_quality;
2268
2269 /* mod fixed_quality */
2270 if (parameters->cp_matrice) {
2271 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 *
2272 sizeof(int);
2273 cp->matrice = (int *) opj_malloc(array_size);
2274 memcpy(cp->matrice, parameters->cp_matrice, array_size);
2275 }
2276
2277 /* tiles */
2278 cp->tdx = parameters->cp_tdx;
2279 cp->tdy = parameters->cp_tdy;
2280
2281 /* tile offset */
2282 cp->tx0 = parameters->cp_tx0;
2283 cp->ty0 = parameters->cp_ty0;
2284
2285 /* comment string */
2286 if (parameters->cp_comment) {
2287 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2288 if (cp->comment) {
2289 strcpy(cp->comment, parameters->cp_comment);
2290 }
2291 }
2292
2293 /*
2294 calculate other encoding parameters
2295 */
2296
2297 if (parameters->tile_size_on) {
2298 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2299 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2300 } else {
2301 cp->tdx = image->x1 - cp->tx0;
2302 cp->tdy = image->y1 - cp->ty0;
2303 }
2304
2305 if (parameters->tp_on) {
2306 cp->tp_flag = parameters->tp_flag;
2307 cp->tp_on = 1;
2308 }
2309
2310 cp->img_size = 0;
2311 for (i = 0; i < image->numcomps ; i++) {
2312 cp->img_size += (image->comps[i].w * image->comps[i].h * image->comps[i].prec);
2313 }
2314
2315
2316 #ifdef USE_JPWL
2317 /*
2318 calculate JPWL encoding parameters
2319 */
2320
2321 if (parameters->jpwl_epc_on) {
2322 int i;
2323
2324 /* set JPWL on */
2325 cp->epc_on = OPJ_TRUE;
2326 cp->info_on = OPJ_FALSE; /* no informative technique */
2327
2328 /* set EPB on */
2329 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2330 cp->epb_on = OPJ_TRUE;
2331
2332 cp->hprot_MH = parameters->jpwl_hprot_MH;
2333 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2334 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2335 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2336 }
2337 /* if tile specs are not specified, copy MH specs */
2338 if (cp->hprot_TPH[0] == -1) {
2339 cp->hprot_TPH_tileno[0] = 0;
2340 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2341 }
2342 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2343 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2344 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2345 cp->pprot[i] = parameters->jpwl_pprot[i];
2346 }
2347 }
2348
2349 /* set ESD writing */
2350 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2351 cp->esd_on = OPJ_TRUE;
2352
2353 cp->sens_size = parameters->jpwl_sens_size;
2354 cp->sens_addr = parameters->jpwl_sens_addr;
2355 cp->sens_range = parameters->jpwl_sens_range;
2356
2357 cp->sens_MH = parameters->jpwl_sens_MH;
2358 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2359 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2360 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2361 }
2362 }
2363
2364 /* always set RED writing to false: we are at the encoder */
2365 cp->red_on = OPJ_FALSE;
2366
2367 } else {
2368 cp->epc_on = OPJ_FALSE;
2369 }
2370 #endif /* USE_JPWL */
2371
2372
2373 /* initialize the multiple tiles */
2374 /* ---------------------------- */
2375 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2376
2377 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2378 opj_tcp_t *tcp = &cp->tcps[tileno];
2379 tcp->numlayers = parameters->tcp_numlayers;
2380 for (j = 0; j < tcp->numlayers; j++) {
2381 if (cp->cinema) {
2382 if (cp->fixed_quality) {
2383 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2384 }
2385 tcp->rates[j] = parameters->tcp_rates[j];
2386 } else {
2387 if (cp->fixed_quality) { /* add fixed_quality */
2388 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2389 } else {
2390 tcp->rates[j] = parameters->tcp_rates[j];
2391 }
2392 }
2393 }
2394 tcp->csty = parameters->csty;
2395 tcp->prg = parameters->prog_order;
2396 tcp->mct = parameters->tcp_mct;
2397
2398 numpocs_tile = 0;
2399 tcp->POC = 0;
2400 if (parameters->numpocs) {
2401 /* initialisation of POC */
2402 tcp->POC = 1;
2403 for (i = 0; i < parameters->numpocs; i++) {
2404 if ((tileno == parameters->POC[i].tile - 1) ||
2405 (parameters->POC[i].tile == -1)) {
2406 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2407 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2408 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2409 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2410 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2411 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2412 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2413 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2414 numpocs_tile++;
2415 }
2416 }
2417 tcp->numpocs = numpocs_tile - 1 ;
2418 } else {
2419 tcp->numpocs = 0;
2420 }
2421
2422 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2423
2424 for (i = 0; i < image->numcomps; i++) {
2425 opj_tccp_t *tccp = &tcp->tccps[i];
2426 tccp->csty = parameters->csty &
2427 0x01; /* 0 => one precinct || 1 => custom precinct */
2428 tccp->numresolutions = parameters->numresolution;
2429 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2430 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2431 tccp->cblksty = parameters->mode;
2432 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2433 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT :
2434 J2K_CCP_QNTSTY_NOQNT;
2435 tccp->numgbits = 2;
2436 if (i == parameters->roi_compno) {
2437 tccp->roishift = parameters->roi_shift;
2438 } else {
2439 tccp->roishift = 0;
2440 }
2441
2442 if (parameters->cp_cinema) {
2443 /*Precinct size for lowest frequency subband=128*/
2444 tccp->prcw[0] = 7;
2445 tccp->prch[0] = 7;
2446 /*Precinct size at all other resolutions = 256*/
2447 for (j = 1; j < tccp->numresolutions; j++) {
2448 tccp->prcw[j] = 8;
2449 tccp->prch[j] = 8;
2450 }
2451 } else {
2452 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2453 int p = 0;
2454 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2455 if (p < parameters->res_spec) {
2456
2457 if (parameters->prcw_init[p] < 1) {
2458 tccp->prcw[j] = 1;
2459 } else {
2460 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2461 }
2462
2463 if (parameters->prch_init[p] < 1) {
2464 tccp->prch[j] = 1;
2465 } else {
2466 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2467 }
2468
2469 } else {
2470 int res_spec = parameters->res_spec;
2471 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2472 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2473
2474 if (size_prcw < 1) {
2475 tccp->prcw[j] = 1;
2476 } else {
2477 tccp->prcw[j] = int_floorlog2(size_prcw);
2478 }
2479
2480 if (size_prch < 1) {
2481 tccp->prch[j] = 1;
2482 } else {
2483 tccp->prch[j] = int_floorlog2(size_prch);
2484 }
2485 }
2486 p++;
2487 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2488 } /*end for*/
2489 } else {
2490 for (j = 0; j < tccp->numresolutions; j++) {
2491 tccp->prcw[j] = 15;
2492 tccp->prch[j] = 15;
2493 }
2494 }
2495 }
2496
2497 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2498 }
2499 }
2500 }
2501
j2k_encode(opj_j2k_t * j2k,opj_cio_t * cio,opj_image_t * image,opj_codestream_info_t * cstr_info)2502 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image,
2503 opj_codestream_info_t *cstr_info)
2504 {
2505 int tileno, compno;
2506 opj_cp_t *cp = NULL;
2507
2508 opj_tcd_t *tcd = NULL; /* TCD component */
2509
2510 j2k->cio = cio;
2511 j2k->image = image;
2512
2513 cp = j2k->cp;
2514
2515 /* INDEX >> */
2516 j2k->cstr_info = cstr_info;
2517 if (cstr_info) {
2518 int compno;
2519 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(
2520 opj_tile_info_t));
2521 cstr_info->image_w = image->x1 - image->x0;
2522 cstr_info->image_h = image->y1 - image->y0;
2523 cstr_info->prog = (&cp->tcps[0])->prg;
2524 cstr_info->tw = cp->tw;
2525 cstr_info->th = cp->th;
2526 cstr_info->tile_x = cp->tdx; /* new version parser */
2527 cstr_info->tile_y = cp->tdy; /* new version parser */
2528 cstr_info->tile_Ox = cp->tx0; /* new version parser */
2529 cstr_info->tile_Oy = cp->ty0; /* new version parser */
2530 cstr_info->numcomps = image->numcomps;
2531 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2532 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2533 for (compno = 0; compno < image->numcomps; compno++) {
2534 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2535 }
2536 cstr_info->D_max = 0.0; /* ADD Marcela */
2537 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2538 cstr_info->maxmarknum = 100;
2539 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum *
2540 sizeof(opj_marker_info_t));
2541 cstr_info->marknum = 0;
2542 }
2543 /* << INDEX */
2544
2545 j2k_write_soc(j2k);
2546 j2k_write_siz(j2k);
2547 j2k_write_cod(j2k);
2548 j2k_write_qcd(j2k);
2549
2550 if (cp->cinema) {
2551 for (compno = 1; compno < image->numcomps; compno++) {
2552 j2k_write_coc(j2k, compno);
2553 j2k_write_qcc(j2k, compno);
2554 }
2555 }
2556
2557 for (compno = 0; compno < image->numcomps; compno++) {
2558 opj_tcp_t *tcp = &cp->tcps[0];
2559 if (tcp->tccps[compno].roishift) {
2560 j2k_write_rgn(j2k, compno, 0);
2561 }
2562 }
2563 if (cp->comment != NULL) {
2564 j2k_write_com(j2k);
2565 }
2566
2567 j2k->totnum_tp = j2k_calculate_tp(cp, image->numcomps, image, j2k);
2568 /* TLM Marker*/
2569 if (cp->cinema) {
2570 j2k_write_tlm(j2k);
2571 if (cp->cinema == CINEMA4K_24) {
2572 j2k_write_poc(j2k);
2573 }
2574 }
2575
2576 /* uncomment only for testing JPSEC marker writing */
2577 /* j2k_write_sec(j2k); */
2578
2579 /* INDEX >> */
2580 if (cstr_info) {
2581 cstr_info->main_head_end = cio_tell(cio) - 1;
2582 }
2583 /* << INDEX */
2584 /**** Main Header ENDS here ***/
2585
2586 /* create the tile encoder */
2587 tcd = tcd_create(j2k->cinfo);
2588
2589 /* encode each tile */
2590 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2591 int pino;
2592 int tilepartno = 0;
2593 /* UniPG>> */
2594 int acc_pack_num = 0;
2595 /* <<UniPG */
2596
2597
2598 opj_tcp_t *tcp = &cp->tcps[tileno];
2599 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1,
2600 cp->tw * cp->th);
2601
2602 j2k->curtileno = tileno;
2603 j2k->cur_tp_num = 0;
2604 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2605 /* initialisation before tile encoding */
2606 if (tileno == 0) {
2607 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2608 } else {
2609 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2610 }
2611
2612 /* INDEX >> */
2613 if (cstr_info) {
2614 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2615 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
2616 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(
2617 cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
2618 cstr_info->tile[j2k->curtileno].marknum = 0;
2619 }
2620 /* << INDEX */
2621
2622 for (pino = 0; pino <= tcp->numpocs; pino++) {
2623 int tot_num_tp;
2624 tcd->cur_pino = pino;
2625
2626 /*Get number of tile parts*/
2627 tot_num_tp = j2k_get_num_tp(cp, pino, tileno);
2628 tcd->tp_pos = cp->tp_pos;
2629
2630 for (tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++) {
2631 j2k->tp_num = tilepartno;
2632 /* INDEX >> */
2633 if (cstr_info)
2634 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2635 cio_tell(cio) + j2k->pos_correction;
2636 /* << INDEX */
2637 j2k_write_sot(j2k);
2638
2639 if (j2k->cur_tp_num == 0 && cp->cinema == 0) {
2640 for (compno = 1; compno < image->numcomps; compno++) {
2641 j2k_write_coc(j2k, compno);
2642 j2k_write_qcc(j2k, compno);
2643 }
2644 if (cp->tcps[tileno].numpocs) {
2645 j2k_write_poc(j2k);
2646 }
2647 }
2648
2649 /* INDEX >> */
2650 if (cstr_info)
2651 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2652 cio_tell(cio) + j2k->pos_correction + 1;
2653 /* << INDEX */
2654
2655 j2k_write_sod(j2k, tcd);
2656
2657 /* INDEX >> */
2658 if (cstr_info) {
2659 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2660 cio_tell(cio) + j2k->pos_correction - 1;
2661 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2662 acc_pack_num;
2663 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2664 cstr_info->packno - acc_pack_num;
2665 acc_pack_num = cstr_info->packno;
2666 }
2667 /* << INDEX */
2668
2669 j2k->cur_tp_num++;
2670 }
2671 }
2672 if (cstr_info) {
2673 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction -
2674 1;
2675 }
2676
2677
2678 /*
2679 if (tile->PPT) { // BAD PPT !!!
2680 FILE *PPT_file;
2681 int i;
2682 PPT_file=fopen("PPT","rb");
2683 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2684 for (i=0;i<tile->len_ppt;i++) {
2685 unsigned char elmt;
2686 fread(&elmt, 1, 1, PPT_file);
2687 fwrite(&elmt,1,1,f);
2688 }
2689 fclose(PPT_file);
2690 unlink("PPT");
2691 }
2692 */
2693
2694 }
2695
2696 /* destroy the tile encoder */
2697 tcd_free_encode(tcd);
2698 tcd_destroy(tcd);
2699
2700 opj_free(j2k->cur_totnum_tp);
2701
2702 j2k_write_eoc(j2k);
2703
2704 if (cstr_info) {
2705 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2706 /* UniPG>> */
2707 /* The following adjustment is done to adjust the codestream size */
2708 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2709 /* the first bunch of bytes is not in the codestream */
2710 cstr_info->codestream_size -= cstr_info->main_head_start;
2711 /* <<UniPG */
2712 }
2713
2714 #ifdef USE_JPWL
2715 /*
2716 preparation of JPWL marker segments
2717 */
2718 if (cp->epc_on) {
2719
2720 /* encode according to JPWL */
2721 jpwl_encode(j2k, cio, image);
2722
2723 }
2724 #endif /* USE_JPWL */
2725
2726 return OPJ_TRUE;
2727 }
2728
j2k_add_mhmarker(opj_codestream_info_t * cstr_info,unsigned short int type,int pos,int len)2729 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info,
2730 unsigned short int type, int pos, int len)
2731 {
2732
2733 if (!cstr_info) {
2734 return;
2735 }
2736
2737 /* expand the list? */
2738 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
2739 cstr_info->maxmarknum = 100 + (int)((float) cstr_info->maxmarknum * 1.0F);
2740 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker,
2741 cstr_info->maxmarknum);
2742 }
2743
2744 /* add the marker */
2745 cstr_info->marker[cstr_info->marknum].type = type;
2746 cstr_info->marker[cstr_info->marknum].pos = pos;
2747 cstr_info->marker[cstr_info->marknum].len = len;
2748 cstr_info->marknum++;
2749
2750 }
2751
j2k_add_tlmarker(int tileno,opj_codestream_info_t * cstr_info,unsigned short int type,int pos,int len)2752 static void j2k_add_tlmarker(int tileno, opj_codestream_info_t *cstr_info,
2753 unsigned short int type, int pos, int len)
2754 {
2755
2756 opj_marker_info_t *marker;
2757
2758 if (!cstr_info) {
2759 return;
2760 }
2761
2762 /* expand the list? */
2763 if ((cstr_info->tile[tileno].marknum + 1) >
2764 cstr_info->tile[tileno].maxmarknum) {
2765 cstr_info->tile[tileno].maxmarknum = 100 + (int)((float)
2766 cstr_info->tile[tileno].maxmarknum * 1.0F);
2767 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(
2768 cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
2769 }
2770
2771 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
2772
2773 /* add the marker */
2774 marker->type = type;
2775 marker->pos = pos;
2776 marker->len = len;
2777 cstr_info->tile[tileno].marknum++;
2778 }
2779