2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
34 #include "opj_includes.h"
37 /** Minimum and maximum values for the double->pfp conversion */
39 #define MAX_V1 17293822569102704640.0
40 #define MIN_V2 0.000030517578125
41 #define MAX_V2 131040.0
43 /** conversion between a double precision floating point
44 number and the corresponding pseudo-floating point used
45 to represent sensitivity values
46 @param V the double precision value
47 @param bytes the number of bytes of the representation
48 @return the pseudo-floating point value (cast accordingly)
50 unsigned short int jpwl_double_to_pfp(double V, int bytes);
52 /** conversion between a pseudo-floating point used
53 to represent sensitivity values and the corresponding
54 double precision floating point number
55 @param em the pseudo-floating point value (cast accordingly)
56 @param bytes the number of bytes of the representation
57 @return the double precision value
59 double jpwl_pfp_to_double(unsigned short int em, int bytes);
61 /*-------------------------------------------------------------*/
63 int jpwl_markcomp(const void *arg1, const void *arg2)
65 /* Compare the two markers' positions */
66 double diff = (((jpwl_marker_t *) arg1)->dpos - ((jpwl_marker_t *) arg2)->dpos);
76 int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
77 opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot,
78 double place_pos, int tileno,
79 unsigned long int pre_len, unsigned long int post_len) {
81 jpwl_epb_ms_t *epb_mark = NULL;
83 int k_pre, k_post, n_pre, n_post;
85 unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0;
87 /* We find RS(n,k) for EPB parms and pre-data, if any */
88 if (insideMH && (*idx == 0)) {
92 } else if (!insideMH && (*idx == 0)) {
97 /* Following EPBs in MH or TH */
102 /* Find lengths, Figs. B3 and B4 */
103 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
106 /* size of pre-data redundancy */
107 /* (redundancy per codeword) * (number of codewords, rounded up) */
108 L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre);
110 /* Find protection type for post data and its associated redundancy field length*/
111 if ((hprot == 16) || (hprot == 32)) {
112 /* there is a CRC for post-data */
114 n_post = post_len + (hprot >> 3);
115 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
117 } else if ((hprot >= 37) && (hprot <= 128)) {
118 /* there is a RS for post-data */
123 /* Use predefined codes */
128 /* Create the EPB(s) */
129 while (post_len > 0) {
131 /* maximum postlen in order to respect EPB size
132 (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/
133 /* (message word size) * (number of containable parity words) */
134 max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
136 /* maximum postlen in order to respect EPB size */
138 /* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */
139 /* (message word size) * (number of containable parity words) */
140 max_postlen = k_post * (unsigned long int) floor((double) (JPWL_MAXIMUM_EPB_ROOM - L2) / (double) (n_post - k_post));
143 /* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */
144 /* (message word size) * (number of containable parity words) */
145 max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
147 /* null protection case */
148 /* the max post length can be as large as the LDPepb field can host */
150 max_postlen = INT_MAX;
153 dL4 = min(max_postlen, post_len);
155 if ((epb_mark = jpwl_epb_create(
156 j2k, /* this encoder handle */
157 latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */
158 packed, /* is it packed? */
159 tileno, /* we are in TPH */
160 *idx, /* its index */
161 hprot, /* protection type parameters of following data */
162 0, /* pre-data: nothing for now */
163 dL4 /* post-data: the stub computed previously */
166 /* Add this marker to the 'insertanda' list */
167 if (*jwmarker_num < JPWL_MAX_NO_MARKERS) {
168 jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */
169 jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */
170 jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */
171 jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(*idx); /* not very first! */
172 jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */
173 jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */
174 jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */
175 jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */
176 jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */
180 /* increment epb index */
183 /* decrease postlen */
186 /* increase the total length of EPBs */
187 epbs_len += epb_mark->Lepb + 2;
190 /* ooops, problems */
191 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB for UEP in tile %d\n", tileno);
199 jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, int tileno, int idx, int hprot,
200 unsigned long int pre_len, unsigned long int post_len) {
202 jpwl_epb_ms_t *epb = NULL;
203 /*unsigned short int data_len = 0;*/
204 unsigned short int L2, L3;
205 unsigned long int L1, L4;
206 /*unsigned char *predata_in = NULL;*/
208 opj_bool insideMH = (tileno == -1);
211 if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epb_ms_t)))) {
212 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for one EPB MS\n");
216 /* We set RS(n,k) for EPB parms and pre-data, if any */
217 if (insideMH && (idx == 0)) {
218 /* First EPB in MH */
221 } else if (!insideMH && (idx == 0)) {
222 /* First EPB in TH */
226 /* Following EPBs in MH or TH */
231 /* Find lengths, Figs. B3 and B4 */
232 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
234 epb->pre_len = pre_len;
236 /* size of pre-data redundancy */
237 /* (redundancy per codeword) * (number of codewords, rounded up) */
238 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
240 /* length of post-data */
242 epb->post_len = post_len;
244 /* Find protection type for post data and its associated redundancy field length*/
245 if ((hprot == 16) || (hprot == 32)) {
246 /* there is a CRC for post-data */
247 epb->Pepb = 0x10000000 | ((unsigned long int) hprot >> 5); /* 0=CRC-16, 1=CRC-32 */
248 epb->k_post = post_len;
249 epb->n_post = post_len + (hprot >> 3);
250 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
252 } else if ((hprot >= 37) && (hprot <= 128)) {
253 /* there is a RS for post-data */
254 epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8);
258 } else if (hprot == 1) {
259 /* Use predefined codes */
260 epb->Pepb = (unsigned long int) 0x00000000;
261 epb->n_post = epb->n_pre;
262 epb->k_post = epb->k_pre;
264 } else if (hprot == 0) {
265 /* Placeholder EPB: only protects its parameters, no protection method */
266 epb->Pepb = (unsigned long int) 0xFFFFFFFF;
271 opj_event_msg(j2k->cinfo, EVT_ERROR, "Invalid protection value for EPB h = %d\n", hprot);
277 /* (redundancy per codeword) * (number of codewords, rounded up) */
278 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
281 epb->tileno = tileno;
283 /* Fill some fields of the EPB */
285 /* total length of the EPB MS (less the EPB marker itself): */
286 /* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */
287 epb->Lepb = 11 + L2 + L3;
290 epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) | (idx & 0x003F);
292 /* length of data protected by EPB: */
293 epb->LDPepb = L1 + L4;
298 void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf) {
301 *(buf++) = (unsigned char) (J2K_MS_EPB >> 8);
302 *(buf++) = (unsigned char) (J2K_MS_EPB >> 0);
305 *(buf++) = (unsigned char) (epb->Lepb >> 8);
306 *(buf++) = (unsigned char) (epb->Lepb >> 0);
309 *(buf++) = (unsigned char) (epb->Depb >> 0);
312 *(buf++) = (unsigned char) (epb->LDPepb >> 24);
313 *(buf++) = (unsigned char) (epb->LDPepb >> 16);
314 *(buf++) = (unsigned char) (epb->LDPepb >> 8);
315 *(buf++) = (unsigned char) (epb->LDPepb >> 0);
318 *(buf++) = (unsigned char) (epb->Pepb >> 24);
319 *(buf++) = (unsigned char) (epb->Pepb >> 16);
320 *(buf++) = (unsigned char) (epb->Pepb >> 8);
321 *(buf++) = (unsigned char) (epb->Pepb >> 0);
324 /*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/
325 memset(buf, 0, (size_t) epb->Lepb - 11);
327 /* update markers struct */
328 j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2);
333 jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on, opj_bool epb_on, opj_bool info_on) {
335 jpwl_epc_ms_t *epc = NULL;
338 if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epc_ms_t)))) {
339 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n");
343 /* Set the EPC parameters */
344 epc->esd_on = esd_on;
345 epc->epb_on = epb_on;
346 epc->red_on = red_on;
347 epc->info_on = info_on;
349 /* Fill the EPC fields with default values */
352 epc->DL = 0x00000000;
353 epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) << 5) |
354 ((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7);
359 opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf, unsigned char *post_buf) {
361 unsigned long int L1, L2, L3, L4;
363 unsigned long int P, NN_P;
365 /* Operating buffer */
366 static unsigned char codeword[NN], *parityword;
368 unsigned char *L1_buf, *L2_buf;
369 /* these ones are static, since we need to keep memory of
370 the exact place from one call to the other */
371 static unsigned char *L3_buf, *L4_buf;
373 /* some consistency check */
375 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n");
379 if (!post_buf && !L4_buf) {
380 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs data\n");
385 * Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms)
388 /* Initialize RS structures */
389 P = epb->n_pre - epb->k_pre;
391 memset(codeword, 0, NN);
392 parityword = codeword + NN_P;
395 /* pre-data begins pre_len bytes before of EPB buf */
396 L1_buf = buf - epb->pre_len;
397 L1 = epb->pre_len + 13;
399 /* redundancy for pre-data begins immediately after EPB parms */
401 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
404 the position of L4 buffer can be:
405 1) passed as a parameter: in that case use it
406 2) null: in that case use the previous (static) one
412 /* post-data redundancy begins immediately after pre-data redundancy */
413 L3_buf = L2_buf + L2;
414 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
416 /* let's check whether EPB length is sufficient to contain all these data */
417 if (epb->Lepb < (11 + L2 + L3))
418 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no room in EPB data field for writing redundancy data\n");
419 /*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/
421 /* Compute redundancy of pre-data message words */
425 /* copy message data into codeword buffer */
426 if (remaining < epb->k_pre) {
427 /* the last message word is zero-padded */
428 memset(codeword, 0, NN);
429 memcpy(codeword, L1_buf, remaining);
434 memcpy(codeword, L1_buf, epb->k_pre);
435 L1_buf += epb->k_pre;
436 remaining -= epb->k_pre;
440 /* Encode the buffer and obtain parity bytes */
441 if (encode_rs(codeword, parityword))
442 opj_event_msg(j2k->cinfo, EVT_WARNING,
443 "Possible encoding error in codeword @ position #%d\n", (L1_buf - buf) / epb->k_pre);
445 /* copy parity bytes only in redundancy buffer */
446 memcpy(L2_buf, parityword, P);
448 /* advance parity buffer */
453 * Compute parity bytes on post-data, may be absent if there are no data
455 /*printf("Hprot is %d (tileno=%d, k_pre=%d, n_pre=%d, k_post=%d, n_post=%d, pre_len=%d, post_len=%d)\n",
456 epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len,
458 if (epb->hprot < 0) {
460 /* there should be no EPB */
462 } else if (epb->hprot == 0) {
464 /* no protection for the data */
466 L4_buf += epb->post_len;
468 } else if (epb->hprot == 16) {
471 unsigned short int mycrc = 0x0000;
473 /* compute the CRC field (excluding itself) */
476 jpwl_updateCRC16(&mycrc, *(L4_buf++));
478 /* write the CRC field */
479 *(L3_buf++) = (unsigned char) (mycrc >> 8);
480 *(L3_buf++) = (unsigned char) (mycrc >> 0);
482 } else if (epb->hprot == 32) {
485 unsigned long int mycrc = 0x00000000;
487 /* compute the CRC field (excluding itself) */
490 jpwl_updateCRC32(&mycrc, *(L4_buf++));
492 /* write the CRC field */
493 *(L3_buf++) = (unsigned char) (mycrc >> 24);
494 *(L3_buf++) = (unsigned char) (mycrc >> 16);
495 *(L3_buf++) = (unsigned char) (mycrc >> 8);
496 *(L3_buf++) = (unsigned char) (mycrc >> 0);
502 /* Initialize RS structures */
503 P = epb->n_post - epb->k_post;
505 memset(codeword, 0, NN);
506 parityword = codeword + NN_P;
509 /* Compute redundancy of post-data message words */
513 /* copy message data into codeword buffer */
514 if (remaining < epb->k_post) {
515 /* the last message word is zero-padded */
516 memset(codeword, 0, NN);
517 memcpy(codeword, L4_buf, remaining);
522 memcpy(codeword, L4_buf, epb->k_post);
523 L4_buf += epb->k_post;
524 remaining -= epb->k_post;
528 /* Encode the buffer and obtain parity bytes */
529 if (encode_rs(codeword, parityword))
530 opj_event_msg(j2k->cinfo, EVT_WARNING,
531 "Possible encoding error in codeword @ position #%d\n", (L4_buf - buf) / epb->k_post);
533 /* copy parity bytes only in redundancy buffer */
534 memcpy(L3_buf, parityword, P);
536 /* advance parity buffer */
546 opj_bool jpwl_correct(opj_j2k_t *j2k) {
548 opj_cio_t *cio = j2k->cio;
550 static opj_bool mh_done = OPJ_FALSE;
551 int mark_pos, id, len, skips, sot_pos;
552 unsigned long int Psot = 0;
554 /* go back to marker position */
555 mark_pos = cio_tell(cio) - 2;
556 cio_seek(cio, mark_pos);
558 if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) {
560 int mark_val = 0, skipnum = 0;
564 first thing to do, if we are here, is to look whether
565 51 (skipnum) positions ahead there is an EPB, in case of MH
569 first thing to do, if we are here, is to look whether
570 45 (skipnum) positions ahead there is an EPB, in case of MH
572 /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */
573 skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2;
574 if ((cio->bp + skipnum) < cio->end) {
576 cio_skip(cio, skipnum);
578 /* check that you are not going beyond the end of codestream */
580 /* call EPB corrector */
581 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
582 cio->bp, /* pointer to EPB in codestream buffer */
583 0, /* EPB type: MH */
584 skipnum, /* length of pre-data */
585 -1, /* length of post-data: -1 means auto */
590 /* read the marker value */
591 mark_val = (*(cio->bp) << 8) | *(cio->bp + 1);
593 if (status && (mark_val == J2K_MS_EPB)) {
599 /* Disable correction in case of missing or bad head EPB */
600 /* We can't do better! */
601 /* PATCHED: 2008-01-25 */
602 /* MOVED UP: 2008-02-01 */
604 j2k->cp->correct = OPJ_FALSE;
605 opj_event_msg(j2k->cinfo, EVT_WARNING, "Couldn't find the MH EPB: disabling JPWL\n");
612 if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) {
613 /* else, look if 12 positions ahead there is an EPB, in case of TPH */
614 cio_seek(cio, mark_pos);
615 if ((cio->bp + 12) < cio->end) {
619 /* call EPB corrector */
620 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
621 cio->bp, /* pointer to EPB in codestream buffer */
622 1, /* EPB type: TPH */
623 12, /* length of pre-data */
624 -1, /* length of post-data: -1 means auto */
636 /* for now, don't use this code */
638 /* else, look if here is an EPB, in case of other */
640 /* it cannot stay before the first MH EPB */
641 cio_seek(cio, mark_pos);
644 /* call EPB corrector */
645 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
646 cio->bp, /* pointer to EPB in codestream buffer */
647 2, /* EPB type: TPH */
648 0, /* length of pre-data */
649 -1, /* length of post-data: -1 means auto */
658 /* nope, no EPBs probably, or they are so damaged that we can give up */
663 /* AN ATTEMPT OF PARSER */
664 /* NOT USED ACTUALLY */
666 /* go to the beginning of the file */
670 j2k->state = J2K_STATE_MHSOC;
672 /* cycle all over the markers */
673 while (cio_tell(cio) < cio->length) {
675 /* read the marker */
676 mark_pos = cio_tell(cio);
677 id = cio_read(cio, 2);
680 printf("Marker@%d: %X\n", cio_tell(cio) - 2, id);
682 /* do an action in response to the read marker */
689 j2k->state = J2K_STATE_MHSIZ;
696 j2k->state = J2K_STATE_MT;
701 /* particular case of SOD */
703 len = Psot - (mark_pos - sot_pos) - 2;
711 j2k->state = J2K_STATE_TPH;
712 sot_pos = mark_pos; /* position of SOT */
713 len = cio_read(cio, 2); /* read the length field */
714 cio_skip(cio, 2); /* this field is unnecessary */
715 Psot = cio_read(cio, 4); /* tile length */
721 j2k->state = J2K_STATE_MH;
722 /* read the length field */
723 len = cio_read(cio, 2);
729 /* read the length field */
730 len = cio_read(cio, 2);
736 /* skip to marker's end */
737 cio_skip(cio, skips);
744 opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type, int pre_len, int post_len, int *conn,
745 unsigned char **L4_bufp) {
747 /* Operating buffer */
748 unsigned char codeword[NN], *parityword;
750 unsigned long int P, NN_P;
751 unsigned long int L1, L4;
752 int remaining, n_pre, k_pre, n_post, k_post;
756 int orig_pos = cio_tell(j2k->cio);
758 unsigned char *L1_buf, *L2_buf;
759 unsigned char *L3_buf, *L4_buf;
761 unsigned long int LDPepb, Pepb;
762 unsigned short int Lepb;
765 int myconn, errnum = 0;
766 opj_bool errflag = OPJ_FALSE;
768 opj_cio_t *cio = j2k->cio;
770 /* check for common errors */
772 opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n");
779 /* pre-data correction */
801 /* automatic setup */
802 opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n");
808 opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n");
814 /* Initialize RS structures */
817 tt = (int) floor((float) P / 2.0F); /* correction capability of the code */
818 memset(codeword, 0, NN);
819 parityword = codeword + NN_P;
822 /* Correct pre-data message words */
823 L1_buf = buffer - pre_len;
824 L2_buf = buffer + 13;
828 /* always zero-pad codewords */
829 /* (this is required, since after decoding the zeros in the long codeword
830 could change, and keep unchanged in subsequent calls) */
831 memset(codeword, 0, NN);
833 /* copy codeword buffer into message bytes */
834 if (remaining < k_pre)
835 memcpy(codeword, L1_buf, remaining);
837 memcpy(codeword, L1_buf, k_pre);
839 /* copy redundancy buffer in parity bytes */
840 memcpy(parityword, L2_buf, P);
842 /* Decode the buffer and possibly obtain corrected bytes */
843 status = eras_dec_rs(codeword, NULL, 0);
846 opj_event_msg(j2k->cinfo, EVT_WARNING,
847 "Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/
849 /* we can try to safely get out from the function:
850 if we are here, either this is not an EPB or the first codeword
851 is too damaged to be helpful */
852 /*return OPJ_FALSE;*/
854 } else if (status == 0) {
856 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
858 } else if (status <= tt) {
859 /* it has corrected 0 <= errs <= tt */
861 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
866 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
872 /* advance parity buffer */
873 if ((status >= 0) && (status <= tt))
874 /* copy back corrected parity only if all is OK */
875 memcpy(L2_buf, parityword, P);
878 /* advance message buffer */
879 if (remaining < k_pre) {
880 if ((status >= 0) && (status <= tt))
881 /* copy back corrected data only if all is OK */
882 memcpy(L1_buf, codeword, remaining);
887 if ((status >= 0) && (status <= tt))
888 /* copy back corrected data only if all is OK */
889 memcpy(L1_buf, codeword, k_pre);
900 opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum,
901 (float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/
903 /*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/
909 /* presumably, now, EPB parameters are correct */
912 /* Simply read the EPB parameters */
915 cio_skip(cio, 2); /* the marker */
916 Lepb = cio_read(cio, 2);
917 Depb = cio_read(cio, 1);
918 LDPepb = cio_read(cio, 4);
919 Pepb = cio_read(cio, 4);
921 /* What does Pepb tells us about the protection method? */
922 if (((Pepb & 0xF0000000) >> 28) == 0)
923 sprintf(str1, "pred"); /* predefined */
924 else if (((Pepb & 0xF0000000) >> 28) == 1)
925 sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */
926 else if (((Pepb & 0xF0000000) >> 28) == 2)
927 sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */
928 else if (Pepb == 0xFFFFFFFF)
929 sprintf(str1, "nometh"); /* RS mode */
931 sprintf(str1, "unknown"); /* unknown */
933 /* Now we write them to screen */
934 if (!conn && post_len)
935 opj_event_msg(j2k->cinfo, EVT_INFO,
936 "EPB(%d): (%sl, %sp, %u), %lu, %s\n",
938 (Depb & 0x40) ? "" : "n", /* latest EPB or not? */
939 (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */
940 (Depb & 0x3F), /* EPB index value */
941 LDPepb, /*length of the data protected by the EPB */
942 str1); /* protection method */
945 /* well, we need to investigate how long is the connected length of packed EPBs */
947 if ((Depb & 0x40) == 0) /* not latest in header */
948 jpwl_epb_correct(j2k, /* J2K decompressor handle */
949 buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */
950 2, /* EPB type: should be of other type */
951 0, /* only EPB fields */
952 0, /* do not look after */
960 printf("connected = %d\n", myconn);*/
962 /*cio_seek(j2k->cio, orig_pos);
966 the position of L4 buffer is at the end of currently connected EPBs
969 L4_buf = buffer + myconn;
970 else if (!(*L4_bufp))
971 L4_buf = buffer + myconn;
975 L4 = LDPepb - pre_len - 13;
976 else if (post_len == 0)
983 /* Do a further check here on the read parameters */
984 if (L4 > (unsigned long) cio_numbytesleft(j2k->cio))
988 /* we are ready for decoding the remaining data */
989 if (((Pepb & 0xF0000000) >> 28) == 1) {
991 if ((16 * ((Pepb & 0x00000001) + 1)) == 16) {
994 unsigned short int mycrc = 0x0000, filecrc = 0x0000;
996 /* compute the CRC field */
999 jpwl_updateCRC16(&mycrc, *(L4_buf++));
1001 /* read the CRC field */
1002 filecrc = *(L3_buf++) << 8;
1003 filecrc |= *(L3_buf++);
1005 /* check the CRC field */
1006 if (mycrc == filecrc) {
1008 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1011 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
1016 if ((16 * ((Pepb & 0x00000001) + 1)) == 32) {
1019 unsigned long int mycrc = 0x00000000, filecrc = 0x00000000;
1021 /* compute the CRC field */
1024 jpwl_updateCRC32(&mycrc, *(L4_buf++));
1026 /* read the CRC field */
1027 filecrc = *(L3_buf++) << 24;
1028 filecrc |= *(L3_buf++) << 16;
1029 filecrc |= *(L3_buf++) << 8;
1030 filecrc |= *(L3_buf++);
1032 /* check the CRC field */
1033 if (mycrc == filecrc) {
1035 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1038 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
1043 } else if (Pepb == 0xFFFFFFFF) {
1046 /* advance without doing anything */
1051 } else if ((((Pepb & 0xF0000000) >> 28) == 2) || (((Pepb & 0xF0000000) >> 28) == 0)) {
1052 /* RS coding here */
1054 if (((Pepb & 0xF0000000) >> 28) == 0) {
1062 n_post = (Pepb & 0x0000FF00) >> 8;
1065 /* Initialize RS structures */
1066 P = n_post - k_post;
1068 tt = (int) floor((float) P / 2.0F); /* again, correction capability */
1069 memset(codeword, 0, NN);
1070 parityword = codeword + NN_P;
1073 /* Correct post-data message words */
1074 /*L4_buf = buffer + Lepb + 2;*/
1079 /* always zero-pad codewords */
1080 /* (this is required, since after decoding the zeros in the long codeword
1081 could change, and keep unchanged in subsequent calls) */
1082 memset(codeword, 0, NN);
1084 /* copy codeword buffer into message bytes */
1085 if (remaining < k_post)
1086 memcpy(codeword, L4_buf, remaining);
1088 memcpy(codeword, L4_buf, k_post);
1090 /* copy redundancy buffer in parity bytes */
1091 memcpy(parityword, L3_buf, P);
1093 /* Decode the buffer and possibly obtain corrected bytes */
1094 status = eras_dec_rs(codeword, NULL, 0);
1097 opj_event_msg(j2k->cinfo, EVT_WARNING,
1098 "Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/
1101 } else if (status == 0) {
1103 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
1105 } else if (status <= tt) {
1107 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
1112 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
1118 /* advance parity buffer */
1119 if ((status >= 0) && (status <= tt))
1120 /* copy back corrected data only if all is OK */
1121 memcpy(L3_buf, parityword, P);
1124 /* advance message buffer */
1125 if (remaining < k_post) {
1126 if ((status >= 0) && (status <= tt))
1127 /* copy back corrected data only if all is OK */
1128 memcpy(L4_buf, codeword, remaining);
1129 L4_buf += remaining;
1133 if ((status >= 0) && (status <= tt))
1134 /* copy back corrected data only if all is OK */
1135 memcpy(L4_buf, codeword, k_post);
1137 remaining -= k_post;
1143 /* give back the L4_buf address */
1151 opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n", errnum,
1152 (float) errnum / (float) LDPepb);
1154 opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n");
1158 cio_seek(j2k->cio, orig_pos);
1163 void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf) {
1166 *(buf++) = (unsigned char) (J2K_MS_EPC >> 8);
1167 *(buf++) = (unsigned char) (J2K_MS_EPC >> 0);
1170 *(buf++) = (unsigned char) (epc->Lepc >> 8);
1171 *(buf++) = (unsigned char) (epc->Lepc >> 0);
1174 *(buf++) = (unsigned char) (epc->Pcrc >> 8);
1175 *(buf++) = (unsigned char) (epc->Pcrc >> 0);
1178 *(buf++) = (unsigned char) (epc->DL >> 24);
1179 *(buf++) = (unsigned char) (epc->DL >> 16);
1180 *(buf++) = (unsigned char) (epc->DL >> 8);
1181 *(buf++) = (unsigned char) (epc->DL >> 0);
1184 *(buf++) = (unsigned char) (epc->Pepc >> 0);
1187 /*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/
1188 memset(buf, 0, (size_t) epc->Lepc - 9);
1190 /* update markers struct */
1191 j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2);
1195 int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
1196 int comps, unsigned char addrm, unsigned char ad_size,
1197 unsigned char senst, unsigned char se_size,
1198 double place_pos, int tileno) {
1203 jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp,
1204 unsigned char addrm, unsigned char ad_size,
1205 unsigned char senst, int se_size, int tileno,
1206 unsigned long int svalnum, void *sensval) {
1208 jpwl_esd_ms_t *esd = NULL;
1211 if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_esd_ms_t)))) {
1212 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n");
1216 /* if relative sensitivity, activate byte range mode */
1220 /* size of sensval's ... */
1221 if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) {
1222 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address size %d for ESD MS is forbidden\n", ad_size);
1225 if ((se_size != 1) && (se_size != 2)) {
1226 opj_event_msg(j2k->cinfo, EVT_ERROR, "Sensitivity size %d for ESD MS is forbidden\n", se_size);
1230 /* ... depends on the addressing mode */
1235 ad_size = 0; /* as per the standard */
1236 esd->sensval_size = (unsigned int)se_size;
1241 /* auto sense address size */
1243 /* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes
1244 (we keep space for possible EPBs being inserted) */
1245 ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2;
1246 esd->sensval_size = ad_size + ad_size + se_size;
1251 /* auto sense address size */
1253 /* if there are more than 2^16 - 1 packets, switch to 4 bytes */
1254 ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2;
1255 esd->sensval_size = ad_size + ad_size + se_size;
1259 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is unimplemented\n", addrm);
1263 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is forbidden\n", addrm);
1267 /* set or unset sensitivity values */
1272 /* just based on the portions of a codestream */
1274 /* MH + no. of THs + no. of packets */
1275 svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) * (1 + j2k->cstr_info->packno);
1278 /* all the ones that are based on the packets */
1281 /* MH: all the packets and all the tiles info is written */
1282 svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno;
1284 /* TPH: only that tile info is written */
1285 svalnum = j2k->cstr_info->packno;
1291 /* fill private fields */
1293 esd->ad_size = ad_size;
1294 esd->se_size = se_size;
1296 esd->svalnum = svalnum;
1297 esd->numcomps = j2k->image->numcomps;
1298 esd->tileno = tileno;
1300 /* Set the ESD parameters */
1301 /* length, excluding data field */
1302 if (esd->numcomps < 257)
1303 esd->Lesd = 4 + (unsigned short int) (esd->svalnum * esd->sensval_size);
1305 esd->Lesd = 5 + (unsigned short int) (esd->svalnum * esd->sensval_size);
1307 /* component data field */
1311 /* we are averaging */
1316 esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */
1317 esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */
1318 esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */
1319 esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */
1320 esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */
1322 /* if pointer to sensval is NULL, we can fill data field by ourselves */
1325 /* old code moved to jpwl_esd_fill() */
1329 /* we set the data field as the sensitivity values poinnter passed to the function */
1330 esd->data = (unsigned char *) sensval;
1336 opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) {
1339 unsigned long int vv;
1340 unsigned long int addr1 = 0L, addr2 = 0L;
1341 double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR, oldPSNR = 0.0;
1342 unsigned short int pfpvalue;
1343 unsigned long int addrmask = 0x00000000;
1344 opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE;
1346 /* sensitivity values in image info are as follows:
1347 - for each tile, distotile is the starting distortion for that tile, sum of all components
1348 - for each packet in a tile, disto is the distortion reduction caused by that packet to that tile
1349 - the TSE for a single tile should be given by distotile - sum(disto) , for all components
1350 - the MSE for a single tile is given by TSE / nbpix , for all components
1351 - the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components
1352 (Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images
1355 /* browse all components and find Omax */
1357 for (i = 0; i < j2k->image->numcomps; i++) {
1358 tmp = pow(2.0, (double) (j2k->image->comps[i].sgnd ?
1359 (j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1;
1363 Omax2 = Omax2 * Omax2;
1365 /* if pointer of esd->data is not null, simply write down all the values byte by byte */
1367 for (i = 0; i < (int) esd->svalnum; i++)
1368 *(buf++) = esd->data[i];
1372 /* addressing mask */
1373 if (esd->ad_size == 2)
1374 addrmask = 0x0000FFFF; /* two bytes */
1376 addrmask = 0xFFFFFFFF; /* four bytes */
1378 /* set on precise point where sensitivity starts */
1379 if (esd->numcomps < 257)
1384 /* let's fill the data fields */
1385 for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno); vv < esd->svalnum; vv++) {
1387 int thistile = vv / j2k->cstr_info->packno, thispacket = vv % j2k->cstr_info->packno;
1389 /* skip for the hack some lines below */
1390 if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th)
1393 /* starting tile distortion */
1394 if (thispacket == 0) {
1395 TSE = j2k->cstr_info->tile[thistile].distotile;
1396 oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1397 oldPSNR = 10.0 * log10(Omax2 / oldMSE);
1401 TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto;
1404 MSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1407 PSNR = 10.0 * log10(Omax2 / MSE);
1409 /* fill the address range */
1410 switch (esd->addrm) {
1414 /* nothing, there is none */
1419 /* start address of packet */
1420 addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) & addrmask;
1421 /* end address of packet */
1422 addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask;
1427 /* not implemented here */
1428 opj_event_msg(j2k->cinfo, EVT_WARNING, "Addressing mode packet_range is not implemented\n");
1431 /* unknown addressing method */
1433 /* not implemented here */
1434 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n");
1439 /* hack for writing relative sensitivity of MH and TPHs */
1440 if ((esd->senst == 0) && (thispacket == 0)) {
1443 if ((thistile == 0) && !doneMH) {
1444 /* we have to manage MH addresses */
1445 addr1 = 0; /* start of MH */
1446 addr2 = j2k->cstr_info->main_head_end; /* end of MH */
1447 /* set special dvalue for this MH */
1449 doneMH = OPJ_TRUE; /* don't come here anymore */
1450 vv--; /* wrap back loop counter */
1452 } else if (!doneTPH) {
1453 /* we have to manage TPH addresses */
1454 addr1 = j2k->cstr_info->tile[thistile].start_pos;
1455 addr2 = j2k->cstr_info->tile[thistile].end_header;
1456 /* set special dvalue for this TPH */
1458 doneTPH = OPJ_TRUE; /* don't come here till the next tile */
1459 vv--; /* wrap back loop counter */
1463 doneTPH = OPJ_FALSE; /* reset TPH counter */
1465 /* write the addresses to the buffer */
1466 switch (esd->ad_size) {
1474 *(buf++) = (unsigned char) (addr1 >> 8);
1475 *(buf++) = (unsigned char) (addr1 >> 0);
1476 *(buf++) = (unsigned char) (addr2 >> 8);
1477 *(buf++) = (unsigned char) (addr2 >> 0);
1482 *(buf++) = (unsigned char) (addr1 >> 24);
1483 *(buf++) = (unsigned char) (addr1 >> 16);
1484 *(buf++) = (unsigned char) (addr1 >> 8);
1485 *(buf++) = (unsigned char) (addr1 >> 0);
1486 *(buf++) = (unsigned char) (addr2 >> 24);
1487 *(buf++) = (unsigned char) (addr2 >> 16);
1488 *(buf++) = (unsigned char) (addr2 >> 8);
1489 *(buf++) = (unsigned char) (addr2 >> 0);
1498 /* let's fill the value field */
1499 switch (esd->senst) {
1501 /* relative sensitivity */
1503 /* we just write down the packet ordering */
1506 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1507 else if (dvalue == -1)
1509 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1511 /* packet: first is most important, and then in decreasing order
1512 down to the last, which counts for 1 */
1513 dvalue = jpwl_pfp_to_double((unsigned short) (j2k->cstr_info->packno - thispacket), esd->se_size);
1518 /* !!! WRONG: let's put here disto field of packets !!! */
1524 dvalue = oldMSE - MSE;
1535 dvalue = PSNR - oldPSNR;
1542 opj_event_msg(j2k->cinfo, EVT_WARNING, "MAXERR sensitivity mode is not implemented\n");
1553 opj_event_msg(j2k->cinfo, EVT_WARNING, "Reserved sensitivity mode is not implemented\n");
1561 /* compute the pseudo-floating point value */
1562 pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size);
1564 /* write the pfp value to the buffer */
1565 switch (esd->se_size) {
1569 *(buf++) = (unsigned char) (pfpvalue >> 0);
1574 *(buf++) = (unsigned char) (pfpvalue >> 8);
1575 *(buf++) = (unsigned char) (pfpvalue >> 0);
1584 opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) {
1587 *(buf++) = (unsigned char) (J2K_MS_ESD >> 8);
1588 *(buf++) = (unsigned char) (J2K_MS_ESD >> 0);
1591 *(buf++) = (unsigned char) (esd->Lesd >> 8);
1592 *(buf++) = (unsigned char) (esd->Lesd >> 0);
1595 if (esd->numcomps >= 257)
1596 *(buf++) = (unsigned char) (esd->Cesd >> 8);
1597 *(buf++) = (unsigned char) (esd->Cesd >> 0);
1600 *(buf++) = (unsigned char) (esd->Pesd >> 0);
1603 if (esd->numcomps < 257)
1604 memset(buf, 0xAA, (size_t) esd->Lesd - 4);
1605 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/
1607 memset(buf, 0xAA, (size_t) esd->Lesd - 5);
1608 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/
1610 /* update markers struct */
1611 j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2);
1616 unsigned short int jpwl_double_to_pfp(double V, int bytes) {
1618 unsigned short int em, e, m;
1627 } else if (V > MAX_V1) {
1631 e = (unsigned short int) (floor(log(V) * 1.44269504088896) / 4.0);
1632 m = (unsigned short int) (0.5 + (V / (pow(2.0, (double) (4 * e)))));
1634 em = ((e & 0x000F) << 4) + (m & 0x000F);
1642 } else if (V > MAX_V2) {
1646 e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15;
1647 m = (unsigned short int) (0.5 + 2048.0 * ((V / (pow(2.0, (double) e - 15.0))) - 1.0));
1649 em = ((e & 0x001F) << 11) + (m & 0x07FF);
1661 double jpwl_pfp_to_double(unsigned short int em, int bytes) {
1668 V = (double) (em & 0x0F) * pow(2.0, (double) (em & 0xF0));
1673 V = pow(2.0, (double) ((em & 0xF800) >> 11) - 15.0) * (1.0 + (double) (em & 0x07FF) / 2048.0);
1686 opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num) {
1689 unsigned long int addlen;
1691 opj_codestream_info_t *info = j2k->cstr_info;
1692 int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->packno;
1694 if (!j2k || !jwmarker ) {
1695 opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n");
1699 /* main_head_end: how many markers are there before? */
1701 for (mm = 0; mm < jwmarker_num; mm++)
1702 if (jwmarker[mm].pos < (unsigned long int) info->main_head_end)
1703 addlen += jwmarker[mm].len + 2;
1704 info->main_head_end += addlen;
1706 /* codestream_size: always increment with all markers */
1708 for (mm = 0; mm < jwmarker_num; mm++)
1709 addlen += jwmarker[mm].len + 2;
1710 info->codestream_size += addlen;
1712 /* navigate through all the tiles */
1713 for (tileno = 0; tileno < numtiles; tileno++) {
1715 /* start_pos: increment with markers before SOT */
1717 for (mm = 0; mm < jwmarker_num; mm++)
1718 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos)
1719 addlen += jwmarker[mm].len + 2;
1720 info->tile[tileno].start_pos += addlen;
1722 /* end_header: increment with markers before of it */
1724 for (mm = 0; mm < jwmarker_num; mm++)
1725 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header)
1726 addlen += jwmarker[mm].len + 2;
1727 info->tile[tileno].end_header += addlen;
1729 /* end_pos: increment with markers before the end of this tile */
1730 /* code is disabled, since according to JPWL no markers can be beyond TPH */
1732 for (mm = 0; mm < jwmarker_num; mm++)
1733 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos)
1734 addlen += jwmarker[mm].len + 2;
1735 info->tile[tileno].end_pos += addlen;
1737 /* navigate through all the tile parts */
1738 for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) {
1740 /* start_pos: increment with markers before SOT */
1742 for (mm = 0; mm < jwmarker_num; mm++)
1743 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_start_pos)
1744 addlen += jwmarker[mm].len + 2;
1745 info->tile[tileno].tp[tpno].tp_start_pos += addlen;
1747 /* end_header: increment with markers before of it */
1749 for (mm = 0; mm < jwmarker_num; mm++)
1750 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_header)
1751 addlen += jwmarker[mm].len + 2;
1752 info->tile[tileno].tp[tpno].tp_end_header += addlen;
1754 /* end_pos: increment with markers before the end of this tile part */
1756 for (mm = 0; mm < jwmarker_num; mm++)
1757 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_pos)
1758 addlen += jwmarker[mm].len + 2;
1759 info->tile[tileno].tp[tpno].tp_end_pos += addlen;
1763 /* navigate through all the packets in this tile */
1764 for (packno = 0; packno < numpacks; packno++) {
1766 /* start_pos: increment with markers before the packet */
1767 /* disabled for the same reason as before */
1769 for (mm = 0; mm < jwmarker_num; mm++)
1770 if (jwmarker[mm].pos <= (unsigned long int) info->tile[tileno].packet[packno].start_pos)
1771 addlen += jwmarker[mm].len + 2;
1772 info->tile[tileno].packet[packno].start_pos += addlen;
1774 /* end_ph_pos: increment with markers before the packet */
1775 /* disabled for the same reason as before */
1777 for (mm = 0; mm < jwmarker_num; mm++)
1778 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos)
1779 addlen += jwmarker[mm].len + 2;*/
1780 info->tile[tileno].packet[packno].end_ph_pos += addlen;
1782 /* end_pos: increment if marker is before the end of packet */
1783 /* disabled for the same reason as before */
1785 for (mm = 0; mm < jwmarker_num; mm++)
1786 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos)
1787 addlen += jwmarker[mm].len + 2;*/
1788 info->tile[tileno].packet[packno].end_pos += addlen;
1793 /* reorder the markers list */
1798 #endif /* USE_JPWL */