2
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3
* Copyright (c) 2002-2007, Professor Benoit Macq
4
* Copyright (c) 2001-2003, David Janssens
5
* Copyright (c) 2002-2003, Yannick Verschueren
6
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7
* Copyright (c) 2005, Herve Drolon, FreeImage Team
8
* Copyright (c) 2006-2007, Parvatha Elangovan
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
33
#include "opj_includes.h"
35
/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
38
/** @name Local static functions */
42
Write the SOC marker (Start Of Codestream)
45
static void j2k_write_soc(opj_j2k_t *j2k);
47
Read the SOC marker (Start of Codestream)
50
static void j2k_read_soc(opj_j2k_t *j2k);
52
Write the SIZ marker (image and tile size)
55
static void j2k_write_siz(opj_j2k_t *j2k);
57
Read the SIZ marker (image and tile size)
60
static void j2k_read_siz(opj_j2k_t *j2k);
62
Write the COM marker (comment)
65
static void j2k_write_com(opj_j2k_t *j2k);
67
Read the COM marker (comment)
70
static void j2k_read_com(opj_j2k_t *j2k);
72
Write the value concerning the specified component in the marker COD and COC
74
@param compno Number of the component concerned by the information written
76
static void j2k_write_cox(opj_j2k_t *j2k, int compno);
78
Read the value concerning the specified component in the marker COD and COC
80
@param compno Number of the component concerned by the information read
82
static void j2k_read_cox(opj_j2k_t *j2k, int compno);
84
Write the COD marker (coding style default)
87
static void j2k_write_cod(opj_j2k_t *j2k);
89
Read the COD marker (coding style default)
92
static void j2k_read_cod(opj_j2k_t *j2k);
94
Write the COC marker (coding style component)
96
@param compno Number of the component concerned by the information written
98
static void j2k_write_coc(opj_j2k_t *j2k, int compno);
100
Read the COC marker (coding style component)
101
@param j2k J2K handle
103
static void j2k_read_coc(opj_j2k_t *j2k);
105
Write the value concerning the specified component in the marker QCD and QCC
106
@param j2k J2K handle
107
@param compno Number of the component concerned by the information written
109
static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
111
Read the value concerning the specified component in the marker QCD and QCC
112
@param j2k J2K handle
113
@param compno Number of the component concern by the information read
114
@param len Length of the information in the QCX part of the marker QCD/QCC
116
static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
118
Write the QCD marker (quantization default)
119
@param j2k J2K handle
121
static void j2k_write_qcd(opj_j2k_t *j2k);
123
Read the QCD marker (quantization default)
124
@param j2k J2K handle
126
static void j2k_read_qcd(opj_j2k_t *j2k);
128
Write the QCC marker (quantization component)
129
@param j2k J2K handle
130
@param compno Number of the component concerned by the information written
132
static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
134
Read the QCC marker (quantization component)
135
@param j2k J2K handle
137
static void j2k_read_qcc(opj_j2k_t *j2k);
139
Write the POC marker (progression order change)
140
@param j2k J2K handle
142
static void j2k_write_poc(opj_j2k_t *j2k);
144
Read the POC marker (progression order change)
145
@param j2k J2K handle
147
static void j2k_read_poc(opj_j2k_t *j2k);
149
Read the CRG marker (component registration)
150
@param j2k J2K handle
152
static void j2k_read_crg(opj_j2k_t *j2k);
154
Read the TLM marker (tile-part lengths)
155
@param j2k J2K handle
157
static void j2k_read_tlm(opj_j2k_t *j2k);
159
Read the PLM marker (packet length, main header)
160
@param j2k J2K handle
162
static void j2k_read_plm(opj_j2k_t *j2k);
164
Read the PLT marker (packet length, tile-part header)
165
@param j2k J2K handle
167
static void j2k_read_plt(opj_j2k_t *j2k);
169
Read the PPM marker (packet packet headers, main header)
170
@param j2k J2K handle
172
static void j2k_read_ppm(opj_j2k_t *j2k);
174
Read the PPT marker (packet packet headers, tile-part header)
175
@param j2k J2K handle
177
static void j2k_read_ppt(opj_j2k_t *j2k);
179
Write the TLM marker (Mainheader)
180
@param j2k J2K handle
182
static void j2k_write_tlm(opj_j2k_t *j2k);
184
Write the SOT marker (start of tile-part)
185
@param j2k J2K handle
187
static void j2k_write_sot(opj_j2k_t *j2k);
189
Read the SOT marker (start of tile-part)
190
@param j2k J2K handle
192
static void j2k_read_sot(opj_j2k_t *j2k);
194
Write the SOD marker (start of data)
195
@param j2k J2K handle
196
@param tile_coder Pointer to a TCD handle
198
static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
200
Read the SOD marker (start of data)
201
@param j2k J2K handle
203
static void j2k_read_sod(opj_j2k_t *j2k);
205
Write the RGN marker (region-of-interest)
206
@param j2k J2K handle
207
@param compno Number of the component concerned by the information written
208
@param tileno Number of the tile concerned by the information written
210
static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
212
Read the RGN marker (region-of-interest)
213
@param j2k J2K handle
215
static void j2k_read_rgn(opj_j2k_t *j2k);
217
Write the EOC marker (end of codestream)
218
@param j2k J2K handle
220
static void j2k_write_eoc(opj_j2k_t *j2k);
222
Read the EOC marker (end of codestream)
223
@param j2k J2K handle
225
static void j2k_read_eoc(opj_j2k_t *j2k);
227
Read an unknown marker
228
@param j2k J2K handle
230
static void j2k_read_unk(opj_j2k_t *j2k);
236
/* ----------------------------------------------------------------------- */
237
typedef struct j2k_prog_order{
238
OPJ_PROG_ORDER enum_prog;
242
j2k_prog_order_t j2k_prog_order_list[] = {
248
{(OPJ_PROG_ORDER)-1, ""}
251
char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
252
j2k_prog_order_t *po;
253
for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
254
if(po->enum_prog == prg_order){
261
/* ----------------------------------------------------------------------- */
262
static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
266
opj_tcp_t *tcp = &cp->tcps[tileno];
267
prog = j2k_convert_progression_order(tcp->prg);
272
if( cp->tp_flag == prog[i] ){
273
tpend=1;cp->tp_pos=i;
277
tpnum= tpnum * tcp->pocs[pino].compE;
280
tpnum= tpnum * tcp->pocs[pino].resE;
283
tpnum= tpnum * tcp->pocs[pino].prcE;
286
tpnum= tpnum * tcp->pocs[pino].layE;
297
/** mem allocation for TLM marker*/
298
int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
299
int pino,tileno,totnum_tp=0;
300
j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
301
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
302
int cur_totnum_tp = 0;
303
opj_tcp_t *tcp = &cp->tcps[tileno];
304
for(pino = 0; pino <= tcp->numpocs; pino++) {
306
opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
307
if(!pi) { return -1;}
308
tp_num = j2k_get_num_tp(cp,pino,tileno);
309
totnum_tp = totnum_tp + tp_num;
310
cur_totnum_tp = cur_totnum_tp + tp_num;
311
pi_destroy(pi, cp, tileno);
313
j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
315
if (j2k->cstr_info) {
316
j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
317
j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
324
static void j2k_write_soc(opj_j2k_t *j2k) {
325
opj_cio_t *cio = j2k->cio;
326
cio_write(cio, J2K_MS_SOC, 2);
331
/* update markers struct */
332
j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
334
#endif /* USE_JPWL */
338
static void j2k_read_soc(opj_j2k_t *j2k) {
339
j2k->state = J2K_STATE_MHSIZ;
341
if (j2k->cstr_info) {
342
j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
343
j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
347
static void j2k_write_siz(opj_j2k_t *j2k) {
351
opj_cio_t *cio = j2k->cio;
352
opj_image_t *image = j2k->image;
353
opj_cp_t *cp = j2k->cp;
355
cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
356
lenp = cio_tell(cio);
358
cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
359
cio_write(cio, image->x1, 4); /* Xsiz */
360
cio_write(cio, image->y1, 4); /* Ysiz */
361
cio_write(cio, image->x0, 4); /* X0siz */
362
cio_write(cio, image->y0, 4); /* Y0siz */
363
cio_write(cio, cp->tdx, 4); /* XTsiz */
364
cio_write(cio, cp->tdy, 4); /* YTsiz */
365
cio_write(cio, cp->tx0, 4); /* XT0siz */
366
cio_write(cio, cp->ty0, 4); /* YT0siz */
367
cio_write(cio, image->numcomps, 2); /* Csiz */
368
for (i = 0; i < image->numcomps; i++) {
369
cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
370
cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
371
cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
373
len = cio_tell(cio) - lenp;
375
cio_write(cio, len, 2); /* Lsiz */
376
cio_seek(cio, lenp + len);
379
static void j2k_read_siz(opj_j2k_t *j2k) {
382
opj_cio_t *cio = j2k->cio;
383
opj_image_t *image = j2k->image;
384
opj_cp_t *cp = j2k->cp;
386
len = cio_read(cio, 2); /* Lsiz */
387
cio_read(cio, 2); /* Rsiz (capabilities) */
388
image->x1 = cio_read(cio, 4); /* Xsiz */
389
image->y1 = cio_read(cio, 4); /* Ysiz */
390
image->x0 = cio_read(cio, 4); /* X0siz */
391
image->y0 = cio_read(cio, 4); /* Y0siz */
392
cp->tdx = cio_read(cio, 4); /* XTsiz */
393
cp->tdy = cio_read(cio, 4); /* YTsiz */
394
cp->tx0 = cio_read(cio, 4); /* XT0siz */
395
cp->ty0 = cio_read(cio, 4); /* YT0siz */
397
if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
398
opj_event_msg(j2k->cinfo, EVT_ERROR,
399
"%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
400
image->x0,image->x1,image->y0,image->y1);
404
image->numcomps = cio_read(cio, 2); /* Csiz */
407
if (j2k->cp->correct) {
408
/* if JPWL is on, we check whether TX errors have damaged
409
too much the SIZ parameters */
410
if (!(image->x1 * image->y1)) {
411
opj_event_msg(j2k->cinfo, EVT_ERROR,
412
"JPWL: bad image size (%d x %d)\n",
413
image->x1, image->y1);
414
if (!JPWL_ASSUME || JPWL_ASSUME) {
415
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
419
if (image->numcomps != ((len - 38) / 3)) {
420
opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
421
"JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
422
image->numcomps, ((len - 38) / 3));
424
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
427
/* we try to correct */
428
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
429
if (image->numcomps < ((len - 38) / 3)) {
430
len = 38 + 3 * image->numcomps;
431
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
434
image->numcomps = ((len - 38) / 3);
435
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
440
/* update components number in the jpwl_exp_comps filed */
441
cp->exp_comps = image->numcomps;
443
#endif /* USE_JPWL */
445
image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
446
for (i = 0; i < image->numcomps; i++) {
448
tmp = cio_read(cio, 1); /* Ssiz_i */
449
image->comps[i].prec = (tmp & 0x7f) + 1;
450
image->comps[i].sgnd = tmp >> 7;
451
image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
452
image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
455
if (j2k->cp->correct) {
456
/* if JPWL is on, we check whether TX errors have damaged
457
too much the SIZ parameters, again */
458
if (!(image->comps[i].dx * image->comps[i].dy)) {
459
opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
460
"JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
461
i, i, image->comps[i].dx, image->comps[i].dy);
463
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
466
/* we try to correct */
467
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
468
if (!image->comps[i].dx) {
469
image->comps[i].dx = 1;
470
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
471
i, image->comps[i].dx);
473
if (!image->comps[i].dy) {
474
image->comps[i].dy = 1;
475
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
476
i, image->comps[i].dy);
481
#endif /* USE_JPWL */
484
w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
485
h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
487
image->comps[i].resno_decoded = 0; /* number of resolution decoded */
488
image->comps[i].factor = cp->reduce; /* reducing factor per component */
491
cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
492
cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
495
if (j2k->cp->correct) {
496
/* if JPWL is on, we check whether TX errors have damaged
497
too much the SIZ parameters */
498
if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
499
opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
500
"JPWL: bad number of tiles (%d x %d)\n",
503
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
506
/* we try to correct */
507
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
510
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
513
if (cp->tw > cp->max_tiles) {
515
opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
516
"- setting %d tiles in x => HYPOTHESIS!!!\n",
517
cp->max_tiles, cp->tw);
521
opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
524
if (cp->th > cp->max_tiles) {
526
opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
527
"- setting %d tiles in y => HYPOTHESIS!!!\n",
528
cp->max_tiles, cp->th);
532
#endif /* USE_JPWL */
534
cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
535
cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
539
if (j2k->cp->correct) {
541
opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
542
"JPWL: could not alloc tcps field of cp\n");
543
if (!JPWL_ASSUME || JPWL_ASSUME) {
544
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
549
#endif /* USE_JPWL */
551
for (i = 0; i < cp->tw * cp->th; i++) {
553
cp->tcps[i].numpocs = 0;
554
cp->tcps[i].first = 1;
557
/* Initialization for PPM marker */
560
cp->ppm_data_first = NULL;
561
cp->ppm_previous = 0;
564
j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
565
for (i = 0; i < cp->tw * cp->th; i++) {
566
cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
568
j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
569
j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
570
j2k->state = J2K_STATE_MH;
573
if (j2k->cstr_info) {
574
opj_codestream_info_t *cstr_info = j2k->cstr_info;
575
cstr_info->image_w = image->x1 - image->x0;
576
cstr_info->image_h = image->y1 - image->y0;
577
cstr_info->numcomps = image->numcomps;
578
cstr_info->tw = cp->tw;
579
cstr_info->th = cp->th;
580
cstr_info->tile_x = cp->tdx;
581
cstr_info->tile_y = cp->tdy;
582
cstr_info->tile_Ox = cp->tx0;
583
cstr_info->tile_Oy = cp->ty0;
584
cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
588
static void j2k_write_com(opj_j2k_t *j2k) {
592
if(j2k->cp->comment) {
593
opj_cio_t *cio = j2k->cio;
594
char *comment = j2k->cp->comment;
596
cio_write(cio, J2K_MS_COM, 2);
597
lenp = cio_tell(cio);
599
cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
600
for (i = 0; i < strlen(comment); i++) {
601
cio_write(cio, comment[i], 1);
603
len = cio_tell(cio) - lenp;
605
cio_write(cio, len, 2);
606
cio_seek(cio, lenp + len);
610
static void j2k_read_com(opj_j2k_t *j2k) {
613
opj_cio_t *cio = j2k->cio;
615
len = cio_read(cio, 2);
616
cio_skip(cio, len - 2);
619
static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
622
opj_cp_t *cp = j2k->cp;
623
opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
624
opj_tccp_t *tccp = &tcp->tccps[compno];
625
opj_cio_t *cio = j2k->cio;
627
cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
628
cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
629
cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
630
cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
631
cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
633
if (tccp->csty & J2K_CCP_CSTY_PRT) {
634
for (i = 0; i < tccp->numresolutions; i++) {
635
cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
640
static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
643
opj_cp_t *cp = j2k->cp;
644
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
645
opj_tccp_t *tccp = &tcp->tccps[compno];
646
opj_cio_t *cio = j2k->cio;
648
tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
650
// If user wants to remove more resolutions than the codestream contains, return error
651
if (cp->reduce >= tccp->numresolutions) {
652
opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
653
"of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
654
j2k->state |= J2K_STATE_ERR;
657
tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
658
tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
659
tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
660
tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
661
if (tccp->csty & J2K_CP_CSTY_PRT) {
662
for (i = 0; i < tccp->numresolutions; i++) {
663
int tmp = cio_read(cio, 1); /* SPcox (I_i) */
664
tccp->prcw[i] = tmp & 0xf;
665
tccp->prch[i] = tmp >> 4;
670
if(j2k->cstr_info && compno == 0) {
671
for (i = 0; i < tccp->numresolutions; i++) {
672
if (tccp->csty & J2K_CP_CSTY_PRT) {
673
j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
674
j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
677
j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
678
j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
685
static void j2k_write_cod(opj_j2k_t *j2k) {
687
opj_tcp_t *tcp = NULL;
690
opj_cio_t *cio = j2k->cio;
692
cio_write(cio, J2K_MS_COD, 2); /* COD */
694
lenp = cio_tell(cio);
698
tcp = &cp->tcps[j2k->curtileno];
700
cio_write(cio, tcp->csty, 1); /* Scod */
701
cio_write(cio, tcp->prg, 1); /* SGcod (A) */
702
cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
703
cio_write(cio, tcp->mct, 1); /* SGcod (C) */
705
j2k_write_cox(j2k, 0);
706
len = cio_tell(cio) - lenp;
708
cio_write(cio, len, 2); /* Lcod */
709
cio_seek(cio, lenp + len);
712
static void j2k_read_cod(opj_j2k_t *j2k) {
715
opj_cio_t *cio = j2k->cio;
716
opj_cp_t *cp = j2k->cp;
717
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
718
opj_image_t *image = j2k->image;
720
len = cio_read(cio, 2); /* Lcod */
721
tcp->csty = cio_read(cio, 1); /* Scod */
722
tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
723
tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
724
tcp->mct = cio_read(cio, 1); /* SGcod (C) */
727
for (i = 0; i < image->numcomps; i++) {
728
tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
730
j2k_read_cox(j2k, i);
734
if (j2k->cstr_info) {
735
opj_codestream_info_t *cstr_info = j2k->cstr_info;
736
cstr_info->prog = tcp->prg;
737
cstr_info->numlayers = tcp->numlayers;
738
cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
739
for (i = 0; i < image->numcomps; i++) {
740
cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
745
static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
748
opj_cp_t *cp = j2k->cp;
749
opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
750
opj_image_t *image = j2k->image;
751
opj_cio_t *cio = j2k->cio;
753
cio_write(cio, J2K_MS_COC, 2); /* COC */
754
lenp = cio_tell(cio);
756
cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
757
cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
758
j2k_write_cox(j2k, compno);
759
len = cio_tell(cio) - lenp;
761
cio_write(cio, len, 2); /* Lcoc */
762
cio_seek(cio, lenp + len);
765
static void j2k_read_coc(opj_j2k_t *j2k) {
768
opj_cp_t *cp = j2k->cp;
769
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
770
opj_image_t *image = j2k->image;
771
opj_cio_t *cio = j2k->cio;
773
len = cio_read(cio, 2); /* Lcoc */
774
compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
775
tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
776
j2k_read_cox(j2k, compno);
779
static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
780
int bandno, numbands;
783
opj_cp_t *cp = j2k->cp;
784
opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
785
opj_tccp_t *tccp = &tcp->tccps[compno];
786
opj_cio_t *cio = j2k->cio;
788
cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
789
numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
791
for (bandno = 0; bandno < numbands; bandno++) {
792
expn = tccp->stepsizes[bandno].expn;
793
mant = tccp->stepsizes[bandno].mant;
795
if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
796
cio_write(cio, expn << 3, 1); /* SPqcx_i */
798
cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
803
static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
805
int bandno, numbands;
807
opj_cp_t *cp = j2k->cp;
808
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
809
opj_tccp_t *tccp = &tcp->tccps[compno];
810
opj_cio_t *cio = j2k->cio;
812
tmp = cio_read(cio, 1); /* Sqcx */
813
tccp->qntsty = tmp & 0x1f;
814
tccp->numgbits = tmp >> 5;
815
numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
816
1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
819
if (j2k->cp->correct) {
821
/* if JPWL is on, we check whether there are too many subbands */
822
if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
823
opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
824
"JPWL: bad number of subbands in Sqcx (%d)\n",
827
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
830
/* we try to correct */
832
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
833
"- setting number of bands to %d => HYPOTHESIS!!!\n",
838
#endif /* USE_JPWL */
840
for (bandno = 0; bandno < numbands; bandno++) {
842
if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
843
expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
846
tmp = cio_read(cio, 2); /* SPqcx_i */
850
tccp->stepsizes[bandno].expn = expn;
851
tccp->stepsizes[bandno].mant = mant;
854
/* Add Antonin : if scalar_derived -> compute other stepsizes */
855
if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
856
for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
857
tccp->stepsizes[bandno].expn =
858
((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
859
(tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
860
tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
866
static void j2k_write_qcd(opj_j2k_t *j2k) {
869
opj_cio_t *cio = j2k->cio;
871
cio_write(cio, J2K_MS_QCD, 2); /* QCD */
872
lenp = cio_tell(cio);
874
j2k_write_qcx(j2k, 0);
875
len = cio_tell(cio) - lenp;
877
cio_write(cio, len, 2); /* Lqcd */
878
cio_seek(cio, lenp + len);
881
static void j2k_read_qcd(opj_j2k_t *j2k) {
884
opj_cio_t *cio = j2k->cio;
885
opj_image_t *image = j2k->image;
887
len = cio_read(cio, 2); /* Lqcd */
889
for (i = 0; i < image->numcomps; i++) {
891
j2k_read_qcx(j2k, i, len - 2);
895
static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
898
opj_cio_t *cio = j2k->cio;
900
cio_write(cio, J2K_MS_QCC, 2); /* QCC */
901
lenp = cio_tell(cio);
903
cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
904
j2k_write_qcx(j2k, compno);
905
len = cio_tell(cio) - lenp;
907
cio_write(cio, len, 2); /* Lqcc */
908
cio_seek(cio, lenp + len);
911
static void j2k_read_qcc(opj_j2k_t *j2k) {
913
int numcomp = j2k->image->numcomps;
914
opj_cio_t *cio = j2k->cio;
916
len = cio_read(cio, 2); /* Lqcc */
917
compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
920
if (j2k->cp->correct) {
922
static int backup_compno = 0;
924
/* compno is negative or larger than the number of components!!! */
925
if ((compno < 0) || (compno >= numcomp)) {
926
opj_event_msg(j2k->cinfo, EVT_ERROR,
927
"JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
930
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
933
/* we try to correct */
934
compno = backup_compno % numcomp;
935
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
936
"- setting component number to %d\n",
940
/* keep your private count of tiles */
943
#endif /* USE_JPWL */
945
j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
948
static void j2k_write_poc(opj_j2k_t *j2k) {
949
int len, numpchgs, i;
951
int numcomps = j2k->image->numcomps;
953
opj_cp_t *cp = j2k->cp;
954
opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
955
opj_tccp_t *tccp = &tcp->tccps[0];
956
opj_cio_t *cio = j2k->cio;
958
numpchgs = 1 + tcp->numpocs;
959
cio_write(cio, J2K_MS_POC, 2); /* POC */
960
len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
961
cio_write(cio, len, 2); /* Lpoc */
962
for (i = 0; i < numpchgs; i++) {
963
opj_poc_t *poc = &tcp->pocs[i];
964
cio_write(cio, poc->resno0, 1); /* RSpoc_i */
965
cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
966
cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
967
poc->layno1 = int_min(poc->layno1, tcp->numlayers);
968
cio_write(cio, poc->resno1, 1); /* REpoc_i */
969
poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
970
cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
971
poc->compno1 = int_min(poc->compno1, numcomps);
972
cio_write(cio, poc->prg, 1); /* Ppoc_i */
976
static void j2k_read_poc(opj_j2k_t *j2k) {
977
int len, numpchgs, i, old_poc;
979
int numcomps = j2k->image->numcomps;
981
opj_cp_t *cp = j2k->cp;
982
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
983
opj_cio_t *cio = j2k->cio;
985
old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
987
len = cio_read(cio, 2); /* Lpoc */
988
numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
990
for (i = old_poc; i < numpchgs + old_poc; i++) {
993
poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
994
poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
995
poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
996
poc->resno1 = cio_read(cio, 1); /* REpoc_i */
997
poc->compno1 = int_min(
998
cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
999
poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1002
tcp->numpocs = numpchgs + old_poc - 1;
1005
static void j2k_read_crg(opj_j2k_t *j2k) {
1006
int len, i, Xcrg_i, Ycrg_i;
1008
opj_cio_t *cio = j2k->cio;
1009
int numcomps = j2k->image->numcomps;
1011
len = cio_read(cio, 2); /* Lcrg */
1012
for (i = 0; i < numcomps; i++) {
1013
Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1014
Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1018
static void j2k_read_tlm(opj_j2k_t *j2k) {
1019
int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1020
long int Ttlm_i, Ptlm_i;
1022
opj_cio_t *cio = j2k->cio;
1024
len = cio_read(cio, 2); /* Ltlm */
1025
Ztlm = cio_read(cio, 1); /* Ztlm */
1026
Stlm = cio_read(cio, 1); /* Stlm */
1027
ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1028
SP = (Stlm >> 6) & 0x01;
1029
tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1030
for (i = 0; i < tile_tlm; i++) {
1031
Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1032
Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1036
static void j2k_read_plm(opj_j2k_t *j2k) {
1037
int len, i, Zplm, Nplm, add, packet_len = 0;
1039
opj_cio_t *cio = j2k->cio;
1041
len = cio_read(cio, 2); /* Lplm */
1042
Zplm = cio_read(cio, 1); /* Zplm */
1045
Nplm = cio_read(cio, 4); /* Nplm */
1047
for (i = Nplm; i > 0; i--) {
1048
add = cio_read(cio, 1);
1050
packet_len = (packet_len << 7) + add; /* Iplm_ij */
1051
if ((add & 0x80) == 0) {
1061
static void j2k_read_plt(opj_j2k_t *j2k) {
1062
int len, i, Zplt, packet_len = 0, add;
1064
opj_cio_t *cio = j2k->cio;
1066
len = cio_read(cio, 2); /* Lplt */
1067
Zplt = cio_read(cio, 1); /* Zplt */
1068
for (i = len - 3; i > 0; i--) {
1069
add = cio_read(cio, 1);
1070
packet_len = (packet_len << 7) + add; /* Iplt_i */
1071
if ((add & 0x80) == 0) {
1078
static void j2k_read_ppm(opj_j2k_t *j2k) {
1079
int len, Z_ppm, i, j;
1082
opj_cp_t *cp = j2k->cp;
1083
opj_cio_t *cio = j2k->cio;
1085
len = cio_read(cio, 2);
1088
Z_ppm = cio_read(cio, 1); /* Z_ppm */
1091
if (cp->ppm_previous == 0) {
1092
N_ppm = cio_read(cio, 4); /* N_ppm */
1095
N_ppm = cp->ppm_previous;
1098
if (Z_ppm == 0) { /* First PPM marker */
1099
cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1100
cp->ppm_data_first = cp->ppm_data;
1101
cp->ppm_len = N_ppm;
1102
} else { /* NON-first PPM marker */
1103
cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1106
/* this memory allocation check could be done even in non-JPWL cases */
1108
if (!cp->ppm_data) {
1109
opj_event_msg(j2k->cinfo, EVT_ERROR,
1110
"JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1112
if (!JPWL_ASSUME || JPWL_ASSUME) {
1113
opj_free(cp->ppm_data);
1114
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1121
cp->ppm_data_first = cp->ppm_data;
1122
cp->ppm_len = N_ppm + cp->ppm_store;
1124
for (i = N_ppm; i > 0; i--) { /* Read packet header */
1125
cp->ppm_data[j] = cio_read(cio, 1);
1129
break; /* Case of non-finished packet header in present marker but finished in next one */
1131
cp->ppm_previous = i - 1;
1136
static void j2k_read_ppt(opj_j2k_t *j2k) {
1137
int len, Z_ppt, i, j = 0;
1139
opj_cp_t *cp = j2k->cp;
1140
opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1141
opj_cio_t *cio = j2k->cio;
1143
len = cio_read(cio, 2);
1144
Z_ppt = cio_read(cio, 1);
1146
if (Z_ppt == 0) { /* First PPT marker */
1147
tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1148
tcp->ppt_data_first = tcp->ppt_data;
1150
tcp->ppt_len = len - 3;
1151
} else { /* NON-first PPT marker */
1152
tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1153
tcp->ppt_data_first = tcp->ppt_data;
1154
tcp->ppt_len = len - 3 + tcp->ppt_store;
1157
for (i = len - 3; i > 0; i--) {
1158
tcp->ppt_data[j] = cio_read(cio, 1);
1164
static void j2k_write_tlm(opj_j2k_t *j2k){
1166
opj_cio_t *cio = j2k->cio;
1167
j2k->tlm_start = cio_tell(cio);
1168
cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1169
lenp = 4 + (5*j2k->totnum_tp);
1170
cio_write(cio,lenp,2); /* Ltlm */
1171
cio_write(cio, 0,1); /* Ztlm=0*/
1172
cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1173
cio_skip(cio,5*j2k->totnum_tp);
1176
static void j2k_write_sot(opj_j2k_t *j2k) {
1179
opj_cio_t *cio = j2k->cio;
1181
j2k->sot_start = cio_tell(cio);
1182
cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1183
lenp = cio_tell(cio);
1184
cio_skip(cio, 2); /* Lsot (further) */
1185
cio_write(cio, j2k->curtileno, 2); /* Isot */
1186
cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1187
cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
1188
cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1189
len = cio_tell(cio) - lenp;
1190
cio_seek(cio, lenp);
1191
cio_write(cio, len, 2); /* Lsot */
1192
cio_seek(cio, lenp + len);
1196
/* update markers struct */
1197
j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1198
#endif /* USE_JPWL */
1202
static void j2k_read_sot(opj_j2k_t *j2k) {
1203
int len, tileno, totlen, partno, numparts, i;
1204
opj_tcp_t *tcp = NULL;
1207
opj_cp_t *cp = j2k->cp;
1208
opj_cio_t *cio = j2k->cio;
1210
len = cio_read(cio, 2);
1211
tileno = cio_read(cio, 2);
1214
if (j2k->cp->correct) {
1216
static int backup_tileno = 0;
1218
/* tileno is negative or larger than the number of tiles!!! */
1219
if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
1220
opj_event_msg(j2k->cinfo, EVT_ERROR,
1221
"JPWL: bad tile number (%d out of a maximum of %d)\n",
1222
tileno, (cp->tw * cp->th));
1224
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1227
/* we try to correct */
1228
tileno = backup_tileno;
1229
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1230
"- setting tile number to %d\n",
1234
/* keep your private count of tiles */
1237
#endif /* USE_JPWL */
1239
if (cp->tileno_size == 0) {
1240
cp->tileno[cp->tileno_size] = tileno;
1244
while (i < cp->tileno_size && status == 0) {
1245
status = cp->tileno[i] == tileno ? 1 : 0;
1249
cp->tileno[cp->tileno_size] = tileno;
1254
totlen = cio_read(cio, 4);
1257
if (j2k->cp->correct) {
1259
/* totlen is negative or larger than the bytes left!!! */
1260
if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1261
opj_event_msg(j2k->cinfo, EVT_ERROR,
1262
"JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1263
totlen, cio_numbytesleft(cio) + 8);
1265
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1268
/* we try to correct */
1270
opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1271
"- setting Psot to %d => assuming it is the last tile\n",
1276
#endif /* USE_JPWL */
1279
totlen = cio_numbytesleft(cio) + 8;
1281
partno = cio_read(cio, 1);
1282
numparts = cio_read(cio, 1);
1284
j2k->curtileno = tileno;
1285
j2k->cur_tp_num = partno;
1286
j2k->eot = cio_getbp(cio) - 12 + totlen;
1287
j2k->state = J2K_STATE_TPH;
1288
tcp = &cp->tcps[j2k->curtileno];
1291
if (j2k->cstr_info) {
1294
j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1295
j2k->cstr_info->tile[tileno].tileno = tileno;
1296
j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1297
j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
1298
j2k->cstr_info->tile[tileno].num_tps = numparts;
1300
j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
1302
j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
1305
j2k->cstr_info->tile[tileno].end_pos += totlen;
1307
j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1308
j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1309
j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1312
if (tcp->first == 1) {
1313
/* Initialization PPT */
1314
opj_tccp_t *tmp = tcp->tccps;
1315
memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1317
tcp->ppt_data = NULL;
1318
tcp->ppt_data_first = NULL;
1321
for (i = 0; i < j2k->image->numcomps; i++) {
1322
tcp->tccps[i] = j2k->default_tcp->tccps[i];
1324
cp->tcps[j2k->curtileno].first = 0;
1328
static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1331
opj_tcp_t *tcp = NULL;
1332
opj_codestream_info_t *cstr_info = NULL;
1334
opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1335
opj_cp_t *cp = j2k->cp;
1336
opj_cio_t *cio = j2k->cio;
1338
tcd->tp_num = j2k->tp_num ;
1339
tcd->cur_tp_num = j2k->cur_tp_num;
1341
cio_write(cio, J2K_MS_SOD, 2);
1342
if (j2k->curtileno == 0) {
1343
j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1347
cstr_info = j2k->cstr_info;
1349
if (!j2k->cur_tp_num ) {
1350
cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1351
j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1354
if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
1355
cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
1359
/* update markers struct */
1360
j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1361
#endif /* USE_JPWL */
1366
tcp = &cp->tcps[j2k->curtileno];
1367
for (layno = 0; layno < tcp->numlayers; layno++) {
1368
if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
1369
tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
1370
} else if (tcp->rates[layno]) {
1371
tcp->rates[layno]=1;
1374
if(j2k->cur_tp_num == 0){
1375
tcd->tcd_image->tiles->packno = 0;
1377
cstr_info->packno = 0;
1380
l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1382
/* Writing Psot in SOT marker */
1383
totlen = cio_tell(cio) + l - j2k->sot_start;
1384
cio_seek(cio, j2k->sot_start + 6);
1385
cio_write(cio, totlen, 4);
1386
cio_seek(cio, j2k->sot_start + totlen);
1387
/* Writing Ttlm and Ptlm in TLM marker */
1389
cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1390
cio_write(cio, j2k->curtileno, 1);
1391
cio_write(cio, totlen, 4);
1393
cio_seek(cio, j2k->sot_start + totlen);
1396
static void j2k_read_sod(opj_j2k_t *j2k) {
1397
int len, truncate = 0, i;
1398
unsigned char *data = NULL, *data_ptr = NULL;
1400
opj_cio_t *cio = j2k->cio;
1401
int curtileno = j2k->curtileno;
1404
if (j2k->cstr_info) {
1405
j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1406
cio_tell(cio) + j2k->pos_correction - 1;
1407
if (j2k->cur_tp_num == 0)
1408
j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1409
j2k->cstr_info->packno = 0;
1412
len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1414
if (len == cio_numbytesleft(cio) + 1) {
1415
truncate = 1; /* Case of a truncate codestream */
1418
data = j2k->tile_data[curtileno];
1419
data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1421
data_ptr = data + j2k->tile_len[curtileno];
1422
for (i = 0; i < len; i++) {
1423
data_ptr[i] = cio_read(cio, 1);
1426
j2k->tile_len[curtileno] += len;
1427
j2k->tile_data[curtileno] = data;
1430
j2k->state = J2K_STATE_TPHSOT;
1432
j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1437
static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1438
opj_cp_t *cp = j2k->cp;
1439
opj_tcp_t *tcp = &cp->tcps[tileno];
1440
opj_cio_t *cio = j2k->cio;
1441
int numcomps = j2k->image->numcomps;
1443
cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1444
cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1445
cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1446
cio_write(cio, 0, 1); /* Srgn */
1447
cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1450
static void j2k_read_rgn(opj_j2k_t *j2k) {
1451
int len, compno, roisty;
1453
opj_cp_t *cp = j2k->cp;
1454
opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1455
opj_cio_t *cio = j2k->cio;
1456
int numcomps = j2k->image->numcomps;
1458
len = cio_read(cio, 2); /* Lrgn */
1459
compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1460
roisty = cio_read(cio, 1); /* Srgn */
1463
if (j2k->cp->correct) {
1464
/* totlen is negative or larger than the bytes left!!! */
1465
if (compno >= numcomps) {
1466
opj_event_msg(j2k->cinfo, EVT_ERROR,
1467
"JPWL: bad component number in RGN (%d when there are only %d)\n",
1469
if (!JPWL_ASSUME || JPWL_ASSUME) {
1470
opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1475
#endif /* USE_JPWL */
1477
tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1480
static void j2k_write_eoc(opj_j2k_t *j2k) {
1481
opj_cio_t *cio = j2k->cio;
1482
/* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1483
cio_write(cio, J2K_MS_EOC, 2);
1487
/* update markers struct */
1488
j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1489
#endif /* USE_JPWL */
1493
static void j2k_read_eoc(opj_j2k_t *j2k) {
1497
/* if packets should be decoded */
1498
if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1499
opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1500
tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1501
for (i = 0; i < j2k->cp->tileno_size; i++) {
1502
tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1503
tileno = j2k->cp->tileno[i];
1504
success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1505
opj_free(j2k->tile_data[tileno]);
1506
j2k->tile_data[tileno] = NULL;
1507
tcd_free_decode_tile(tcd, i);
1508
if (success == false) {
1509
j2k->state |= J2K_STATE_ERR;
1513
tcd_free_decode(tcd);
1516
/* if packets should not be decoded */
1518
for (i = 0; i < j2k->cp->tileno_size; i++) {
1519
tileno = j2k->cp->tileno[i];
1520
opj_free(j2k->tile_data[tileno]);
1521
j2k->tile_data[tileno] = NULL;
1524
if (j2k->state & J2K_STATE_ERR)
1525
j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1527
j2k->state = J2K_STATE_MT;
1530
typedef struct opj_dec_mstabent {
1533
/** value of the state when the marker can appear */
1535
/** action linked to the marker */
1536
void (*handler) (opj_j2k_t *j2k);
1537
} opj_dec_mstabent_t;
1539
opj_dec_mstabent_t j2k_dec_mstab[] = {
1540
{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1541
{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1542
{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1543
{J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1544
{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1545
{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1546
{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1547
{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1548
{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1549
{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1550
{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1551
{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1552
{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1553
{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1554
{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1555
{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1557
{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1558
{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1561
{J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1562
{J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1563
{J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1564
{J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1565
#endif /* USE_JPWL */
1567
{J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1568
{J2K_MS_INSEC, 0, j2k_read_insec},
1569
#endif /* USE_JPSEC */
1571
{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1574
static void j2k_read_unk(opj_j2k_t *j2k) {
1575
opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1578
if (j2k->cp->correct) {
1580
int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1581
cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1582
id = cio_read(j2k->cio, 2);
1583
opj_event_msg(j2k->cinfo, EVT_ERROR,
1584
"JPWL: really don't know this marker %x\n",
1587
opj_event_msg(j2k->cinfo, EVT_ERROR,
1588
"- possible synch loss due to uncorrectable codestream errors => giving up\n");
1591
/* OK, activate this at your own risk!!! */
1592
/* we look for the marker at the minimum hamming distance from this */
1593
while (j2k_dec_mstab[m].id) {
1595
/* 1's where they differ */
1596
tmp_id = j2k_dec_mstab[m].id ^ id;
1598
/* compute the hamming distance between our id and the current */
1600
for (i = 0; i < 16; i++) {
1601
if ((tmp_id >> i) & 0x0001) {
1606
/* if current distance is smaller, set the minimum */
1607
if (cur_dist < min_dist) {
1608
min_dist = cur_dist;
1609
min_id = j2k_dec_mstab[m].id;
1612
/* jump to the next marker */
1616
/* do we substitute the marker? */
1617
if (min_dist < JPWL_MAXIMUM_HAMMING) {
1618
opj_event_msg(j2k->cinfo, EVT_ERROR,
1619
"- marker %x is at distance %d from the read %x\n",
1620
min_id, min_dist, id);
1621
opj_event_msg(j2k->cinfo, EVT_ERROR,
1622
"- trying to substitute in place and crossing fingers!\n");
1623
cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1624
cio_write(j2k->cio, min_id, 2);
1627
cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1632
#endif /* USE_JPWL */
1637
Read the lookup table containing all the marker, status and action
1638
@param id Marker value
1640
static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1641
opj_dec_mstabent_t *e;
1642
for (e = j2k_dec_mstab; e->id != 0; e++) {
1650
/* ----------------------------------------------------------------------- */
1651
/* J2K / JPT decoder interface */
1652
/* ----------------------------------------------------------------------- */
1654
opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1655
opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1659
j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1660
if(!j2k->default_tcp) {
1666
j2k->tile_data = NULL;
1671
void j2k_destroy_decompress(opj_j2k_t *j2k) {
1674
if(j2k->tile_len != NULL) {
1675
opj_free(j2k->tile_len);
1677
if(j2k->tile_data != NULL) {
1678
opj_free(j2k->tile_data);
1680
if(j2k->default_tcp != NULL) {
1681
opj_tcp_t *default_tcp = j2k->default_tcp;
1682
if(default_tcp->ppt_data_first != NULL) {
1683
opj_free(default_tcp->ppt_data_first);
1685
if(j2k->default_tcp->tccps != NULL) {
1686
opj_free(j2k->default_tcp->tccps);
1688
opj_free(j2k->default_tcp);
1690
if(j2k->cp != NULL) {
1691
opj_cp_t *cp = j2k->cp;
1692
if(cp->tcps != NULL) {
1693
for(i = 0; i < cp->tw * cp->th; i++) {
1694
if(cp->tcps[i].ppt_data_first != NULL) {
1695
opj_free(cp->tcps[i].ppt_data_first);
1697
if(cp->tcps[i].tccps != NULL) {
1698
opj_free(cp->tcps[i].tccps);
1703
if(cp->ppm_data_first != NULL) {
1704
opj_free(cp->ppm_data_first);
1706
if(cp->tileno != NULL) {
1707
opj_free(cp->tileno);
1709
if(cp->comment != NULL) {
1710
opj_free(cp->comment);
1718
void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1719
if(j2k && parameters) {
1720
/* create and initialize the coding parameters structure */
1721
opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1722
cp->reduce = parameters->cp_reduce;
1723
cp->layer = parameters->cp_layer;
1724
cp->limit_decoding = parameters->cp_limit_decoding;
1727
cp->correct = parameters->jpwl_correct;
1728
cp->exp_comps = parameters->jpwl_exp_comps;
1729
cp->max_tiles = parameters->jpwl_max_tiles;
1730
#endif /* USE_JPWL */
1733
/* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1738
opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1739
opj_image_t *image = NULL;
1741
opj_common_ptr cinfo = j2k->cinfo;
1744
j2k->cstr_info = cstr_info;
1746
memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1748
/* create an empty image */
1749
image = opj_image_create0();
1752
j2k->state = J2K_STATE_MHSOC;
1755
opj_dec_mstabent_t *e;
1756
int id = cio_read(cio, 2);
1759
/* we try to honor JPWL correction power */
1760
if (j2k->cp->correct) {
1762
int orig_pos = cio_tell(cio);
1765
/* call the corrector */
1766
status = jpwl_correct(j2k);
1768
/* go back to where you were */
1769
cio_seek(cio, orig_pos - 2);
1771
/* re-read the marker */
1772
id = cio_read(cio, 2);
1774
/* check whether it begins with ff */
1775
if (id >> 8 != 0xff) {
1776
opj_event_msg(cinfo, EVT_ERROR,
1777
"JPWL: possible bad marker %x at %d\n",
1778
id, cio_tell(cio) - 2);
1780
opj_image_destroy(image);
1781
opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1784
/* we try to correct */
1786
cio_seek(cio, cio_tell(cio) - 2);
1787
cio_write(cio, id, 2);
1788
opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1789
"- setting marker to %x\n",
1794
#endif /* USE_JPWL */
1796
if (id >> 8 != 0xff) {
1797
opj_image_destroy(image);
1798
opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1801
e = j2k_dec_mstab_lookup(id);
1802
// Check if the marker is known
1803
if (!(j2k->state & e->states)) {
1804
opj_image_destroy(image);
1805
opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1808
// Check if the decoding is limited to the main header
1809
if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1810
opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1817
if (j2k->state & J2K_STATE_ERR)
1820
if (j2k->state == J2K_STATE_MT) {
1823
if (j2k->state == J2K_STATE_NEOC) {
1827
if (j2k->state == J2K_STATE_NEOC) {
1831
if (j2k->state != J2K_STATE_MT) {
1832
opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1839
* Read a JPT-stream and decode file
1842
opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1843
opj_image_t *image = NULL;
1844
opj_jpt_msg_header_t header;
1847
opj_common_ptr cinfo = j2k->cinfo;
1851
/* create an empty image */
1852
image = opj_image_create0();
1855
j2k->state = J2K_STATE_MHSOC;
1857
/* Initialize the header */
1858
jpt_init_msg_header(&header);
1859
/* Read the first header of the message */
1860
jpt_read_msg_header(cinfo, cio, &header);
1862
position = cio_tell(cio);
1863
if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1864
opj_image_destroy(image);
1865
opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1870
opj_dec_mstabent_t *e = NULL;
1873
if (!cio_numbytesleft(cio)) {
1877
/* data-bin read -> need to read a new header */
1878
if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1879
jpt_read_msg_header(cinfo, cio, &header);
1880
position = cio_tell(cio);
1881
if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1882
opj_image_destroy(image);
1883
opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1888
id = cio_read(cio, 2);
1889
if (id >> 8 != 0xff) {
1890
opj_image_destroy(image);
1891
opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1894
e = j2k_dec_mstab_lookup(id);
1895
if (!(j2k->state & e->states)) {
1896
opj_image_destroy(image);
1897
opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1903
if (j2k->state == J2K_STATE_MT) {
1906
if (j2k->state == J2K_STATE_NEOC) {
1910
if (j2k->state == J2K_STATE_NEOC) {
1914
if (j2k->state != J2K_STATE_MT) {
1915
opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1921
/* ----------------------------------------------------------------------- */
1922
/* J2K encoder interface */
1923
/* ----------------------------------------------------------------------- */
1925
opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1926
opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1933
void j2k_destroy_compress(opj_j2k_t *j2k) {
1937
if(j2k->cp != NULL) {
1938
opj_cp_t *cp = j2k->cp;
1941
opj_free(cp->comment);
1944
opj_free(cp->matrice);
1946
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1947
opj_free(cp->tcps[tileno].tccps);
1956
void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1957
int i, j, tileno, numpocs_tile;
1958
opj_cp_t *cp = NULL;
1960
if(!j2k || !parameters || ! image) {
1964
/* create and initialize the coding parameters structure */
1965
cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1967
/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1970
/* set default values for cp */
1975
copy user encoding parameters
1977
cp->cinema = parameters->cp_cinema;
1978
cp->max_comp_size = parameters->max_comp_size;
1979
cp->rsiz = parameters->cp_rsiz;
1980
cp->disto_alloc = parameters->cp_disto_alloc;
1981
cp->fixed_alloc = parameters->cp_fixed_alloc;
1982
cp->fixed_quality = parameters->cp_fixed_quality;
1984
/* mod fixed_quality */
1985
if(parameters->cp_matrice) {
1986
size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
1987
cp->matrice = (int *) opj_malloc(array_size);
1988
memcpy(cp->matrice, parameters->cp_matrice, array_size);
1992
cp->tdx = parameters->cp_tdx;
1993
cp->tdy = parameters->cp_tdy;
1996
cp->tx0 = parameters->cp_tx0;
1997
cp->ty0 = parameters->cp_ty0;
1999
/* comment string */
2000
if(parameters->cp_comment) {
2001
cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2003
strcpy(cp->comment, parameters->cp_comment);
2008
calculate other encoding parameters
2011
if (parameters->tile_size_on) {
2012
cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2013
cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2015
cp->tdx = image->x1 - cp->tx0;
2016
cp->tdy = image->y1 - cp->ty0;
2019
if(parameters->tp_on){
2020
cp->tp_flag = parameters->tp_flag;
2025
for(i=0;i<image->numcomps ;i++){
2026
cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2032
calculate JPWL encoding parameters
2035
if (parameters->jpwl_epc_on) {
2040
cp->info_on = false; /* no informative technique */
2043
if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2046
cp->hprot_MH = parameters->jpwl_hprot_MH;
2047
for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2048
cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2049
cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2051
/* if tile specs are not specified, copy MH specs */
2052
if (cp->hprot_TPH[0] == -1) {
2053
cp->hprot_TPH_tileno[0] = 0;
2054
cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2056
for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2057
cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2058
cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2059
cp->pprot[i] = parameters->jpwl_pprot[i];
2063
/* set ESD writing */
2064
if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2067
cp->sens_size = parameters->jpwl_sens_size;
2068
cp->sens_addr = parameters->jpwl_sens_addr;
2069
cp->sens_range = parameters->jpwl_sens_range;
2071
cp->sens_MH = parameters->jpwl_sens_MH;
2072
for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2073
cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2074
cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2078
/* always set RED writing to false: we are at the encoder */
2084
#endif /* USE_JPWL */
2087
/* initialize the mutiple tiles */
2088
/* ---------------------------- */
2089
cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2091
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2092
opj_tcp_t *tcp = &cp->tcps[tileno];
2093
tcp->numlayers = parameters->tcp_numlayers;
2094
for (j = 0; j < tcp->numlayers; j++) {
2096
if (cp->fixed_quality) {
2097
tcp->distoratio[j] = parameters->tcp_distoratio[j];
2099
tcp->rates[j] = parameters->tcp_rates[j];
2101
if (cp->fixed_quality) { /* add fixed_quality */
2102
tcp->distoratio[j] = parameters->tcp_distoratio[j];
2104
tcp->rates[j] = parameters->tcp_rates[j];
2108
tcp->csty = parameters->csty;
2109
tcp->prg = parameters->prog_order;
2110
tcp->mct = parameters->tcp_mct;
2114
if (parameters->numpocs) {
2115
/* initialisation of POC */
2117
for (i = 0; i < parameters->numpocs; i++) {
2118
if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2119
opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2120
tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2121
tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2122
tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2123
tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2124
tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2125
tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2126
tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2130
tcp->numpocs = numpocs_tile -1 ;
2135
tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2137
for (i = 0; i < image->numcomps; i++) {
2138
opj_tccp_t *tccp = &tcp->tccps[i];
2139
tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
2140
tccp->numresolutions = parameters->numresolution;
2141
tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2142
tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2143
tccp->cblksty = parameters->mode;
2144
tccp->qmfbid = parameters->irreversible ? 0 : 1;
2145
tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2147
if (i == parameters->roi_compno) {
2148
tccp->roishift = parameters->roi_shift;
2153
if(parameters->cp_cinema)
2155
//Precinct size for lowest frequency subband=128
2158
//Precinct size at all other resolutions = 256
2159
for (j = 1; j < tccp->numresolutions; j++) {
2164
if (parameters->csty & J2K_CCP_CSTY_PRT) {
2166
for (j = tccp->numresolutions - 1; j >= 0; j--) {
2167
if (p < parameters->res_spec) {
2169
if (parameters->prcw_init[p] < 1) {
2172
tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2175
if (parameters->prch_init[p] < 1) {
2178
tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2182
int res_spec = parameters->res_spec;
2183
int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2184
int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2186
if (size_prcw < 1) {
2189
tccp->prcw[j] = int_floorlog2(size_prcw);
2192
if (size_prch < 1) {
2195
tccp->prch[j] = int_floorlog2(size_prch);
2199
/*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2202
for (j = 0; j < tccp->numresolutions; j++) {
2209
dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2214
bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2216
opj_cp_t *cp = NULL;
2218
opj_tcd_t *tcd = NULL; /* TCD component */
2226
j2k->cstr_info = cstr_info;
2229
cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2230
cstr_info->image_w = image->x1 - image->x0;
2231
cstr_info->image_h = image->y1 - image->y0;
2232
cstr_info->prog = (&cp->tcps[0])->prg;
2233
cstr_info->tw = cp->tw;
2234
cstr_info->th = cp->th;
2235
cstr_info->tile_x = cp->tdx; /* new version parser */
2236
cstr_info->tile_y = cp->tdy; /* new version parser */
2237
cstr_info->tile_Ox = cp->tx0; /* new version parser */
2238
cstr_info->tile_Oy = cp->ty0; /* new version parser */
2239
cstr_info->numcomps = image->numcomps;
2240
cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2241
cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2242
for (compno=0; compno < image->numcomps; compno++) {
2243
cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2245
cstr_info->D_max = 0.0; /* ADD Marcela */
2246
cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2247
cstr_info->maxmarknum = 100;
2248
cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
2249
cstr_info->marknum = 0;
2259
for (compno = 1; compno < image->numcomps; compno++) {
2260
j2k_write_coc(j2k, compno);
2261
j2k_write_qcc(j2k, compno);
2265
for (compno = 0; compno < image->numcomps; compno++) {
2266
opj_tcp_t *tcp = &cp->tcps[0];
2267
if (tcp->tccps[compno].roishift)
2268
j2k_write_rgn(j2k, compno, 0);
2270
if (cp->comment != NULL) {
2274
j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2278
if (cp->cinema == CINEMA4K_24) {
2283
/* uncomment only for testing JPSEC marker writing */
2284
/* j2k_write_sec(j2k); */
2288
cstr_info->main_head_end = cio_tell(cio) - 1;
2291
/**** Main Header ENDS here ***/
2293
/* create the tile encoder */
2294
tcd = tcd_create(j2k->cinfo);
2296
/* encode each tile */
2297
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2301
int acc_pack_num = 0;
2305
opj_tcp_t *tcp = &cp->tcps[tileno];
2306
opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2308
j2k->curtileno = tileno;
2309
j2k->cur_tp_num = 0;
2310
tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2311
/* initialisation before tile encoding */
2313
tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2315
tcd_init_encode(tcd, image, cp, j2k->curtileno);
2320
cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2324
for(pino = 0; pino <= tcp->numpocs; pino++) {
2328
/*Get number of tile parts*/
2329
tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2330
tcd->tp_pos = cp->tp_pos;
2332
for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2333
j2k->tp_num = tilepartno;
2336
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2337
cio_tell(cio) + j2k->pos_correction;
2341
if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2342
for (compno = 1; compno < image->numcomps; compno++) {
2343
j2k_write_coc(j2k, compno);
2344
j2k_write_qcc(j2k, compno);
2346
if (cp->tcps[tileno].numpocs) {
2353
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2354
cio_tell(cio) + j2k->pos_correction + 1;
2357
j2k_write_sod(j2k, tcd);
2361
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2362
cio_tell(cio) + j2k->pos_correction - 1;
2363
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2365
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2366
cstr_info->packno - acc_pack_num;
2367
acc_pack_num = cstr_info->packno;
2375
cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2380
if (tile->PPT) { // BAD PPT !!!
2383
PPT_file=fopen("PPT","rb");
2384
fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2385
for (i=0;i<tile->len_ppt;i++) {
2387
fread(&elmt, 1, 1, PPT_file);
2388
fwrite(&elmt,1,1,f);
2397
/* destroy the tile encoder */
2398
tcd_free_encode(tcd);
2401
opj_free(j2k->cur_totnum_tp);
2406
cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2408
/* The following adjustment is done to adjust the codestream size */
2409
/* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2410
/* the first bunch of bytes is not in the codestream */
2411
cstr_info->codestream_size -= cstr_info->main_head_start;
2417
preparation of JPWL marker segments
2421
/* encode according to JPWL */
2422
jpwl_encode(j2k, cio, image);
2425
#endif /* USE_JPWL */