2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
42 /** Minimum and maximum values for the double->pfp conversion */
44 #define MAX_V1 17293822569102704640.0
45 #define MIN_V2 0.000030517578125
46 #define MAX_V2 131040.0
48 /** conversion between a double precision floating point
49 number and the corresponding pseudo-floating point used
50 to represent sensitivity values
51 @param V the double precision value
52 @param bytes the number of bytes of the representation
53 @return the pseudo-floating point value (cast accordingly)
55 unsigned short int jpwl_double_to_pfp(double V, int bytes);
57 /** conversion between a pseudo-floating point used
58 to represent sensitivity values and the corresponding
59 double precision floating point number
60 @param em the pseudo-floating point value (cast accordingly)
61 @param bytes the number of bytes of the representation
62 @return the double precision value
64 double jpwl_pfp_to_double(unsigned short int em, int bytes);
66 /*-------------------------------------------------------------*/
68 int jpwl_markcomp(const void *arg1, const void *arg2)
70 /* Compare the two markers' positions */
71 double diff = (((jpwl_marker_t *) arg1)->dpos - ((jpwl_marker_t *) arg2)->dpos);
81 int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
82 opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot,
83 double place_pos, int tileno,
84 unsigned long int pre_len, unsigned long int post_len) {
86 jpwl_epb_ms_t *epb_mark = NULL;
88 int k_pre, k_post, n_pre, n_post;
90 unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0;
92 /* We find RS(n,k) for EPB parms and pre-data, if any */
93 if (insideMH && (*idx == 0)) {
97 } else if (!insideMH && (*idx == 0)) {
102 /* Following EPBs in MH or TH */
107 /* Find lengths, Figs. B3 and B4 */
108 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
111 /* size of pre-data redundancy */
112 /* (redundancy per codeword) * (number of codewords, rounded up) */
113 L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre);
115 /* Find protection type for post data and its associated redundancy field length*/
116 if ((hprot == 16) || (hprot == 32)) {
117 /* there is a CRC for post-data */
119 n_post = post_len + (hprot >> 3);
120 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
122 } else if ((hprot >= 37) && (hprot <= 128)) {
123 /* there is a RS for post-data */
128 /* Use predefined codes */
133 /* Create the EPB(s) */
134 while (post_len > 0) {
136 /* maximum postlen in order to respect EPB size
137 (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/
138 /* (message word size) * (number of containable parity words) */
139 max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
141 /* maximum postlen in order to respect EPB size */
143 /* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */
144 /* (message word size) * (number of containable parity words) */
145 max_postlen = k_post * (unsigned long int) floor((double) (JPWL_MAXIMUM_EPB_ROOM - L2) / (double) (n_post - k_post));
148 /* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */
149 /* (message word size) * (number of containable parity words) */
150 max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
152 /* null protection case */
153 /* the max post length can be as large as the LDPepb field can host */
155 max_postlen = INT_MAX;
158 dL4 = min(max_postlen, post_len);
160 if ((epb_mark = jpwl_epb_create(
161 j2k, /* this encoder handle */
162 latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */
163 packed, /* is it packed? */
164 tileno, /* we are in TPH */
165 *idx, /* its index */
166 hprot, /* protection type parameters of following data */
167 0, /* pre-data: nothing for now */
168 dL4 /* post-data: the stub computed previously */
171 /* Add this marker to the 'insertanda' list */
172 if (*jwmarker_num < JPWL_MAX_NO_MARKERS) {
173 jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */
174 jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */
175 jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */
176 jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(*idx); /* not very first! */
177 jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */
178 jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */
179 jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */
180 jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */
181 jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */
185 /* increment epb index */
188 /* decrease postlen */
191 /* increase the total length of EPBs */
192 epbs_len += epb_mark->Lepb + 2;
195 /* ooops, problems */
196 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB for UEP in tile %d\n", tileno);
204 jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, int tileno, int idx, int hprot,
205 unsigned long int pre_len, unsigned long int post_len) {
207 jpwl_epb_ms_t *epb = NULL;
208 /*unsigned short int data_len = 0;*/
209 unsigned short int L2, L3;
210 unsigned long int L1, L4;
211 /*unsigned char *predata_in = NULL;*/
213 opj_bool insideMH = (tileno == -1);
216 if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epb_ms_t)))) {
217 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for one EPB MS\n");
221 /* We set RS(n,k) for EPB parms and pre-data, if any */
222 if (insideMH && (idx == 0)) {
223 /* First EPB in MH */
226 } else if (!insideMH && (idx == 0)) {
227 /* First EPB in TH */
231 /* Following EPBs in MH or TH */
236 /* Find lengths, Figs. B3 and B4 */
237 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
239 epb->pre_len = pre_len;
241 /* size of pre-data redundancy */
242 /* (redundancy per codeword) * (number of codewords, rounded up) */
243 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
245 /* length of post-data */
247 epb->post_len = post_len;
249 /* Find protection type for post data and its associated redundancy field length*/
250 if ((hprot == 16) || (hprot == 32)) {
251 /* there is a CRC for post-data */
252 epb->Pepb = 0x10000000 | ((unsigned long int) hprot >> 5); /* 0=CRC-16, 1=CRC-32 */
253 epb->k_post = post_len;
254 epb->n_post = post_len + (hprot >> 3);
255 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
257 } else if ((hprot >= 37) && (hprot <= 128)) {
258 /* there is a RS for post-data */
259 epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8);
263 } else if (hprot == 1) {
264 /* Use predefined codes */
265 epb->Pepb = (unsigned long int) 0x00000000;
266 epb->n_post = epb->n_pre;
267 epb->k_post = epb->k_pre;
269 } else if (hprot == 0) {
270 /* Placeholder EPB: only protects its parameters, no protection method */
271 epb->Pepb = (unsigned long int) 0xFFFFFFFF;
276 opj_event_msg(j2k->cinfo, EVT_ERROR, "Invalid protection value for EPB h = %d\n", hprot);
282 /* (redundancy per codeword) * (number of codewords, rounded up) */
283 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
286 epb->tileno = tileno;
288 /* Fill some fields of the EPB */
290 /* total length of the EPB MS (less the EPB marker itself): */
291 /* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */
292 epb->Lepb = 11 + L2 + L3;
295 epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) | (idx & 0x003F);
297 /* length of data protected by EPB: */
298 epb->LDPepb = L1 + L4;
303 void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf) {
306 *(buf++) = (unsigned char) (J2K_MS_EPB >> 8);
307 *(buf++) = (unsigned char) (J2K_MS_EPB >> 0);
310 *(buf++) = (unsigned char) (epb->Lepb >> 8);
311 *(buf++) = (unsigned char) (epb->Lepb >> 0);
314 *(buf++) = (unsigned char) (epb->Depb >> 0);
317 *(buf++) = (unsigned char) (epb->LDPepb >> 24);
318 *(buf++) = (unsigned char) (epb->LDPepb >> 16);
319 *(buf++) = (unsigned char) (epb->LDPepb >> 8);
320 *(buf++) = (unsigned char) (epb->LDPepb >> 0);
323 *(buf++) = (unsigned char) (epb->Pepb >> 24);
324 *(buf++) = (unsigned char) (epb->Pepb >> 16);
325 *(buf++) = (unsigned char) (epb->Pepb >> 8);
326 *(buf++) = (unsigned char) (epb->Pepb >> 0);
329 /*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/
330 memset(buf, 0, (size_t) epb->Lepb - 11);
332 /* update markers struct */
333 j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2);
338 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) {
340 jpwl_epc_ms_t *epc = NULL;
343 if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epc_ms_t)))) {
344 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n");
348 /* Set the EPC parameters */
349 epc->esd_on = esd_on;
350 epc->epb_on = epb_on;
351 epc->red_on = red_on;
352 epc->info_on = info_on;
354 /* Fill the EPC fields with default values */
357 epc->DL = 0x00000000;
358 epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) << 5) |
359 ((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7);
364 opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf, unsigned char *post_buf) {
366 unsigned long int L1, L2, L3, L4;
368 unsigned long int P, NN_P;
370 /* Operating buffer */
371 static unsigned char codeword[NN], *parityword;
373 unsigned char *L1_buf, *L2_buf;
374 /* these ones are static, since we need to keep memory of
375 the exact place from one call to the other */
376 static unsigned char *L3_buf, *L4_buf;
378 /* some consistency check */
380 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n");
384 if (!post_buf && !L4_buf) {
385 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs data\n");
390 * Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms)
393 /* Initialize RS structures */
394 P = epb->n_pre - epb->k_pre;
396 memset(codeword, 0, NN);
397 parityword = codeword + NN_P;
400 /* pre-data begins pre_len bytes before of EPB buf */
401 L1_buf = buf - epb->pre_len;
402 L1 = epb->pre_len + 13;
404 /* redundancy for pre-data begins immediately after EPB parms */
406 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
409 the position of L4 buffer can be:
410 1) passed as a parameter: in that case use it
411 2) null: in that case use the previous (static) one
417 /* post-data redundancy begins immediately after pre-data redundancy */
418 L3_buf = L2_buf + L2;
419 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
421 /* let's check whether EPB length is sufficient to contain all these data */
422 if (epb->Lepb < (11 + L2 + L3))
423 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no room in EPB data field for writing redundancy data\n");
424 /*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/
426 /* Compute redundancy of pre-data message words */
430 /* copy message data into codeword buffer */
431 if (remaining < epb->k_pre) {
432 /* the last message word is zero-padded */
433 memset(codeword, 0, NN);
434 memcpy(codeword, L1_buf, remaining);
439 memcpy(codeword, L1_buf, epb->k_pre);
440 L1_buf += epb->k_pre;
441 remaining -= epb->k_pre;
445 /* Encode the buffer and obtain parity bytes */
446 if (encode_rs(codeword, parityword))
447 opj_event_msg(j2k->cinfo, EVT_WARNING,
448 "Possible encoding error in codeword @ position #%d\n", (L1_buf - buf) / epb->k_pre);
450 /* copy parity bytes only in redundancy buffer */
451 memcpy(L2_buf, parityword, P);
453 /* advance parity buffer */
458 * Compute parity bytes on post-data, may be absent if there are no data
460 /*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",
461 epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len,
463 if (epb->hprot < 0) {
465 /* there should be no EPB */
467 } else if (epb->hprot == 0) {
469 /* no protection for the data */
471 L4_buf += epb->post_len;
473 } else if (epb->hprot == 16) {
476 unsigned short int mycrc = 0x0000;
478 /* compute the CRC field (excluding itself) */
481 jpwl_updateCRC16(&mycrc, *(L4_buf++));
483 /* write the CRC field */
484 *(L3_buf++) = (unsigned char) (mycrc >> 8);
485 *(L3_buf++) = (unsigned char) (mycrc >> 0);
487 } else if (epb->hprot == 32) {
490 unsigned long int mycrc = 0x00000000;
492 /* compute the CRC field (excluding itself) */
495 jpwl_updateCRC32(&mycrc, *(L4_buf++));
497 /* write the CRC field */
498 *(L3_buf++) = (unsigned char) (mycrc >> 24);
499 *(L3_buf++) = (unsigned char) (mycrc >> 16);
500 *(L3_buf++) = (unsigned char) (mycrc >> 8);
501 *(L3_buf++) = (unsigned char) (mycrc >> 0);
507 /* Initialize RS structures */
508 P = epb->n_post - epb->k_post;
510 memset(codeword, 0, NN);
511 parityword = codeword + NN_P;
514 /* Compute redundancy of post-data message words */
518 /* copy message data into codeword buffer */
519 if (remaining < epb->k_post) {
520 /* the last message word is zero-padded */
521 memset(codeword, 0, NN);
522 memcpy(codeword, L4_buf, remaining);
527 memcpy(codeword, L4_buf, epb->k_post);
528 L4_buf += epb->k_post;
529 remaining -= epb->k_post;
533 /* Encode the buffer and obtain parity bytes */
534 if (encode_rs(codeword, parityword))
535 opj_event_msg(j2k->cinfo, EVT_WARNING,
536 "Possible encoding error in codeword @ position #%d\n", (L4_buf - buf) / epb->k_post);
538 /* copy parity bytes only in redundancy buffer */
539 memcpy(L3_buf, parityword, P);
541 /* advance parity buffer */
551 opj_bool jpwl_correct(opj_j2k_t *j2k) {
553 opj_cio_t *cio = j2k->cio;
555 static opj_bool mh_done = OPJ_FALSE;
556 int mark_pos, id, len, skips, sot_pos;
557 unsigned long int Psot = 0;
559 /* go back to marker position */
560 mark_pos = cio_tell(cio) - 2;
561 cio_seek(cio, mark_pos);
563 if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) {
565 int mark_val = 0, skipnum = 0;
569 first thing to do, if we are here, is to look whether
570 51 (skipnum) positions ahead there is an EPB, in case of MH
574 first thing to do, if we are here, is to look whether
575 45 (skipnum) positions ahead there is an EPB, in case of MH
577 /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */
578 skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2;
579 if ((cio->bp + skipnum) < cio->end) {
581 cio_skip(cio, skipnum);
583 /* check that you are not going beyond the end of codestream */
585 /* call EPB corrector */
586 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
587 cio->bp, /* pointer to EPB in codestream buffer */
588 0, /* EPB type: MH */
589 skipnum, /* length of pre-data */
590 -1, /* length of post-data: -1 means auto */
595 /* read the marker value */
596 mark_val = (*(cio->bp) << 8) | *(cio->bp + 1);
598 if (status && (mark_val == J2K_MS_EPB)) {
604 /* Disable correction in case of missing or bad head EPB */
605 /* We can't do better! */
606 /* PATCHED: 2008-01-25 */
607 /* MOVED UP: 2008-02-01 */
609 j2k->cp->correct = OPJ_FALSE;
610 opj_event_msg(j2k->cinfo, EVT_WARNING, "Couldn't find the MH EPB: disabling JPWL\n");
617 if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) {
618 /* else, look if 12 positions ahead there is an EPB, in case of TPH */
619 cio_seek(cio, mark_pos);
620 if ((cio->bp + 12) < cio->end) {
624 /* call EPB corrector */
625 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
626 cio->bp, /* pointer to EPB in codestream buffer */
627 1, /* EPB type: TPH */
628 12, /* length of pre-data */
629 -1, /* length of post-data: -1 means auto */
641 /* for now, don't use this code */
643 /* else, look if here is an EPB, in case of other */
645 /* it cannot stay before the first MH EPB */
646 cio_seek(cio, mark_pos);
649 /* call EPB corrector */
650 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
651 cio->bp, /* pointer to EPB in codestream buffer */
652 2, /* EPB type: TPH */
653 0, /* length of pre-data */
654 -1, /* length of post-data: -1 means auto */
663 /* nope, no EPBs probably, or they are so damaged that we can give up */
668 /* AN ATTEMPT OF PARSER */
669 /* NOT USED ACTUALLY */
671 /* go to the beginning of the file */
675 j2k->state = J2K_STATE_MHSOC;
677 /* cycle all over the markers */
678 while (cio_tell(cio) < cio->length) {
680 /* read the marker */
681 mark_pos = cio_tell(cio);
682 id = cio_read(cio, 2);
685 printf("Marker@%d: %X\n", cio_tell(cio) - 2, id);
687 /* do an action in response to the read marker */
694 j2k->state = J2K_STATE_MHSIZ;
701 j2k->state = J2K_STATE_MT;
706 /* particular case of SOD */
708 len = Psot - (mark_pos - sot_pos) - 2;
716 j2k->state = J2K_STATE_TPH;
717 sot_pos = mark_pos; /* position of SOT */
718 len = cio_read(cio, 2); /* read the length field */
719 cio_skip(cio, 2); /* this field is unnecessary */
720 Psot = cio_read(cio, 4); /* tile length */
726 j2k->state = J2K_STATE_MH;
727 /* read the length field */
728 len = cio_read(cio, 2);
734 /* read the length field */
735 len = cio_read(cio, 2);
741 /* skip to marker's end */
742 cio_skip(cio, skips);
749 opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type, int pre_len, int post_len, int *conn,
750 unsigned char **L4_bufp) {
752 /* Operating buffer */
753 unsigned char codeword[NN], *parityword;
755 unsigned long int P, NN_P;
756 unsigned long int L1, L4;
757 int remaining, n_pre, k_pre, n_post, k_post;
761 int orig_pos = cio_tell(j2k->cio);
763 unsigned char *L1_buf, *L2_buf;
764 unsigned char *L3_buf, *L4_buf;
766 unsigned long int LDPepb, Pepb;
767 unsigned short int Lepb;
770 int myconn, errnum = 0;
771 opj_bool errflag = OPJ_FALSE;
773 opj_cio_t *cio = j2k->cio;
775 /* check for common errors */
777 opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n");
784 /* pre-data correction */
806 /* automatic setup */
807 opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n");
813 opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n");
819 /* Initialize RS structures */
822 tt = (int) floor((float) P / 2.0F); /* correction capability of the code */
823 memset(codeword, 0, NN);
824 parityword = codeword + NN_P;
827 /* Correct pre-data message words */
828 L1_buf = buffer - pre_len;
829 L2_buf = buffer + 13;
833 /* always zero-pad codewords */
834 /* (this is required, since after decoding the zeros in the long codeword
835 could change, and keep unchanged in subsequent calls) */
836 memset(codeword, 0, NN);
838 /* copy codeword buffer into message bytes */
839 if (remaining < k_pre)
840 memcpy(codeword, L1_buf, remaining);
842 memcpy(codeword, L1_buf, k_pre);
844 /* copy redundancy buffer in parity bytes */
845 memcpy(parityword, L2_buf, P);
847 /* Decode the buffer and possibly obtain corrected bytes */
848 status = eras_dec_rs(codeword, NULL, 0);
851 opj_event_msg(j2k->cinfo, EVT_WARNING,
852 "Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/
854 /* we can try to safely get out from the function:
855 if we are here, either this is not an EPB or the first codeword
856 is too damaged to be helpful */
857 /*return OPJ_FALSE;*/
859 } else if (status == 0) {
861 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
863 } else if (status <= tt) {
864 /* it has corrected 0 <= errs <= tt */
866 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
871 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
877 /* advance parity buffer */
878 if ((status >= 0) && (status <= tt))
879 /* copy back corrected parity only if all is OK */
880 memcpy(L2_buf, parityword, P);
883 /* advance message buffer */
884 if (remaining < k_pre) {
885 if ((status >= 0) && (status <= tt))
886 /* copy back corrected data only if all is OK */
887 memcpy(L1_buf, codeword, remaining);
892 if ((status >= 0) && (status <= tt))
893 /* copy back corrected data only if all is OK */
894 memcpy(L1_buf, codeword, k_pre);
905 opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum,
906 (float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/
908 /*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/
914 /* presumably, now, EPB parameters are correct */
917 /* Simply read the EPB parameters */
920 cio_skip(cio, 2); /* the marker */
921 Lepb = cio_read(cio, 2);
922 Depb = cio_read(cio, 1);
923 LDPepb = cio_read(cio, 4);
924 Pepb = cio_read(cio, 4);
926 /* What does Pepb tells us about the protection method? */
927 if (((Pepb & 0xF0000000) >> 28) == 0)
928 sprintf(str1, "pred"); /* predefined */
929 else if (((Pepb & 0xF0000000) >> 28) == 1)
930 sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */
931 else if (((Pepb & 0xF0000000) >> 28) == 2)
932 sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */
933 else if (Pepb == 0xFFFFFFFF)
934 sprintf(str1, "nometh"); /* RS mode */
936 sprintf(str1, "unknown"); /* unknown */
938 /* Now we write them to screen */
939 if (!conn && post_len)
940 opj_event_msg(j2k->cinfo, EVT_INFO,
941 "EPB(%d): (%sl, %sp, %u), %lu, %s\n",
943 (Depb & 0x40) ? "" : "n", /* latest EPB or not? */
944 (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */
945 (Depb & 0x3F), /* EPB index value */
946 LDPepb, /*length of the data protected by the EPB */
947 str1); /* protection method */
950 /* well, we need to investigate how long is the connected length of packed EPBs */
952 if ((Depb & 0x40) == 0) /* not latest in header */
953 jpwl_epb_correct(j2k, /* J2K decompressor handle */
954 buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */
955 2, /* EPB type: should be of other type */
956 0, /* only EPB fields */
957 0, /* do not look after */
965 printf("connected = %d\n", myconn);*/
967 /*cio_seek(j2k->cio, orig_pos);
971 the position of L4 buffer is at the end of currently connected EPBs
974 L4_buf = buffer + myconn;
975 else if (!(*L4_bufp))
976 L4_buf = buffer + myconn;
980 L4 = LDPepb - pre_len - 13;
981 else if (post_len == 0)
988 /* Do a further check here on the read parameters */
989 if (L4 > (unsigned long) cio_numbytesleft(j2k->cio))
993 /* we are ready for decoding the remaining data */
994 if (((Pepb & 0xF0000000) >> 28) == 1) {
996 if ((16 * ((Pepb & 0x00000001) + 1)) == 16) {
999 unsigned short int mycrc = 0x0000, filecrc = 0x0000;
1001 /* compute the CRC field */
1004 jpwl_updateCRC16(&mycrc, *(L4_buf++));
1006 /* read the CRC field */
1007 filecrc = *(L3_buf++) << 8;
1008 filecrc |= *(L3_buf++);
1010 /* check the CRC field */
1011 if (mycrc == filecrc) {
1013 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1016 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
1021 if ((16 * ((Pepb & 0x00000001) + 1)) == 32) {
1024 unsigned long int mycrc = 0x00000000, filecrc = 0x00000000;
1026 /* compute the CRC field */
1029 jpwl_updateCRC32(&mycrc, *(L4_buf++));
1031 /* read the CRC field */
1032 filecrc = *(L3_buf++) << 24;
1033 filecrc |= *(L3_buf++) << 16;
1034 filecrc |= *(L3_buf++) << 8;
1035 filecrc |= *(L3_buf++);
1037 /* check the CRC field */
1038 if (mycrc == filecrc) {
1040 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1043 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
1048 } else if (Pepb == 0xFFFFFFFF) {
1051 /* advance without doing anything */
1056 } else if ((((Pepb & 0xF0000000) >> 28) == 2) || (((Pepb & 0xF0000000) >> 28) == 0)) {
1057 /* RS coding here */
1059 if (((Pepb & 0xF0000000) >> 28) == 0) {
1067 n_post = (Pepb & 0x0000FF00) >> 8;
1070 /* Initialize RS structures */
1071 P = n_post - k_post;
1073 tt = (int) floor((float) P / 2.0F); /* again, correction capability */
1074 memset(codeword, 0, NN);
1075 parityword = codeword + NN_P;
1078 /* Correct post-data message words */
1079 /*L4_buf = buffer + Lepb + 2;*/
1084 /* always zero-pad codewords */
1085 /* (this is required, since after decoding the zeros in the long codeword
1086 could change, and keep unchanged in subsequent calls) */
1087 memset(codeword, 0, NN);
1089 /* copy codeword buffer into message bytes */
1090 if (remaining < k_post)
1091 memcpy(codeword, L4_buf, remaining);
1093 memcpy(codeword, L4_buf, k_post);
1095 /* copy redundancy buffer in parity bytes */
1096 memcpy(parityword, L3_buf, P);
1098 /* Decode the buffer and possibly obtain corrected bytes */
1099 status = eras_dec_rs(codeword, NULL, 0);
1102 opj_event_msg(j2k->cinfo, EVT_WARNING,
1103 "Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/
1106 } else if (status == 0) {
1108 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
1110 } else if (status <= tt) {
1112 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
1117 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
1123 /* advance parity buffer */
1124 if ((status >= 0) && (status <= tt))
1125 /* copy back corrected data only if all is OK */
1126 memcpy(L3_buf, parityword, P);
1129 /* advance message buffer */
1130 if (remaining < k_post) {
1131 if ((status >= 0) && (status <= tt))
1132 /* copy back corrected data only if all is OK */
1133 memcpy(L4_buf, codeword, remaining);
1134 L4_buf += remaining;
1138 if ((status >= 0) && (status <= tt))
1139 /* copy back corrected data only if all is OK */
1140 memcpy(L4_buf, codeword, k_post);
1142 remaining -= k_post;
1148 /* give back the L4_buf address */
1156 opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n", errnum,
1157 (float) errnum / (float) LDPepb);
1159 opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n");
1163 cio_seek(j2k->cio, orig_pos);
1168 void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf) {
1171 *(buf++) = (unsigned char) (J2K_MS_EPC >> 8);
1172 *(buf++) = (unsigned char) (J2K_MS_EPC >> 0);
1175 *(buf++) = (unsigned char) (epc->Lepc >> 8);
1176 *(buf++) = (unsigned char) (epc->Lepc >> 0);
1179 *(buf++) = (unsigned char) (epc->Pcrc >> 8);
1180 *(buf++) = (unsigned char) (epc->Pcrc >> 0);
1183 *(buf++) = (unsigned char) (epc->DL >> 24);
1184 *(buf++) = (unsigned char) (epc->DL >> 16);
1185 *(buf++) = (unsigned char) (epc->DL >> 8);
1186 *(buf++) = (unsigned char) (epc->DL >> 0);
1189 *(buf++) = (unsigned char) (epc->Pepc >> 0);
1192 /*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/
1193 memset(buf, 0, (size_t) epc->Lepc - 9);
1195 /* update markers struct */
1196 j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2);
1200 int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
1201 int comps, unsigned char addrm, unsigned char ad_size,
1202 unsigned char senst, unsigned char se_size,
1203 double place_pos, int tileno) {
1208 jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp,
1209 unsigned char addrm, unsigned char ad_size,
1210 unsigned char senst, int se_size, int tileno,
1211 unsigned long int svalnum, void *sensval) {
1213 jpwl_esd_ms_t *esd = NULL;
1216 if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_esd_ms_t)))) {
1217 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n");
1221 /* if relative sensitivity, activate byte range mode */
1225 /* size of sensval's ... */
1226 if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) {
1227 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address size %d for ESD MS is forbidden\n", ad_size);
1230 if ((se_size != 1) && (se_size != 2)) {
1231 opj_event_msg(j2k->cinfo, EVT_ERROR, "Sensitivity size %d for ESD MS is forbidden\n", se_size);
1235 /* ... depends on the addressing mode */
1240 ad_size = 0; /* as per the standard */
1241 esd->sensval_size = (unsigned int)se_size;
1246 /* auto sense address size */
1248 /* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes
1249 (we keep space for possible EPBs being inserted) */
1250 ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2;
1251 esd->sensval_size = ad_size + ad_size + se_size;
1256 /* auto sense address size */
1258 /* if there are more than 2^16 - 1 packets, switch to 4 bytes */
1259 ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2;
1260 esd->sensval_size = ad_size + ad_size + se_size;
1264 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is unimplemented\n", addrm);
1268 opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is forbidden\n", addrm);
1272 /* set or unset sensitivity values */
1277 /* just based on the portions of a codestream */
1279 /* MH + no. of THs + no. of packets */
1280 svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) * (1 + j2k->cstr_info->packno);
1283 /* all the ones that are based on the packets */
1286 /* MH: all the packets and all the tiles info is written */
1287 svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno;
1289 /* TPH: only that tile info is written */
1290 svalnum = j2k->cstr_info->packno;
1296 /* fill private fields */
1298 esd->ad_size = ad_size;
1299 esd->se_size = se_size;
1301 esd->svalnum = svalnum;
1302 esd->numcomps = j2k->image->numcomps;
1303 esd->tileno = tileno;
1305 /* Set the ESD parameters */
1306 /* length, excluding data field */
1307 if (esd->numcomps < 257)
1308 esd->Lesd = 4 + (unsigned short int) (esd->svalnum * esd->sensval_size);
1310 esd->Lesd = 5 + (unsigned short int) (esd->svalnum * esd->sensval_size);
1312 /* component data field */
1316 /* we are averaging */
1321 esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */
1322 esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */
1323 esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */
1324 esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */
1325 esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */
1327 /* if pointer to sensval is NULL, we can fill data field by ourselves */
1330 /* old code moved to jpwl_esd_fill() */
1334 /* we set the data field as the sensitivity values poinnter passed to the function */
1335 esd->data = (unsigned char *) sensval;
1341 opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) {
1344 unsigned long int vv;
1345 unsigned long int addr1 = 0L, addr2 = 0L;
1346 double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR, oldPSNR = 0.0;
1347 unsigned short int pfpvalue;
1348 unsigned long int addrmask = 0x00000000;
1349 opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE;
1351 /* sensitivity values in image info are as follows:
1352 - for each tile, distotile is the starting distortion for that tile, sum of all components
1353 - for each packet in a tile, disto is the distortion reduction caused by that packet to that tile
1354 - the TSE for a single tile should be given by distotile - sum(disto) , for all components
1355 - the MSE for a single tile is given by TSE / nbpix , for all components
1356 - the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components
1357 (Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images
1360 /* browse all components and find Omax */
1362 for (i = 0; i < j2k->image->numcomps; i++) {
1363 tmp = pow(2.0, (double) (j2k->image->comps[i].sgnd ?
1364 (j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1;
1368 Omax2 = Omax2 * Omax2;
1370 /* if pointer of esd->data is not null, simply write down all the values byte by byte */
1372 for (i = 0; i < (int) esd->svalnum; i++)
1373 *(buf++) = esd->data[i];
1377 /* addressing mask */
1378 if (esd->ad_size == 2)
1379 addrmask = 0x0000FFFF; /* two bytes */
1381 addrmask = 0xFFFFFFFF; /* four bytes */
1383 /* set on precise point where sensitivity starts */
1384 if (esd->numcomps < 257)
1389 /* let's fill the data fields */
1390 for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno); vv < esd->svalnum; vv++) {
1392 int thistile = vv / j2k->cstr_info->packno, thispacket = vv % j2k->cstr_info->packno;
1394 /* skip for the hack some lines below */
1395 if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th)
1398 /* starting tile distortion */
1399 if (thispacket == 0) {
1400 TSE = j2k->cstr_info->tile[thistile].distotile;
1401 oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1402 oldPSNR = 10.0 * log10(Omax2 / oldMSE);
1406 TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto;
1409 MSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1412 PSNR = 10.0 * log10(Omax2 / MSE);
1414 /* fill the address range */
1415 switch (esd->addrm) {
1419 /* nothing, there is none */
1424 /* start address of packet */
1425 addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) & addrmask;
1426 /* end address of packet */
1427 addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask;
1432 /* not implemented here */
1433 opj_event_msg(j2k->cinfo, EVT_WARNING, "Addressing mode packet_range is not implemented\n");
1436 /* unknown addressing method */
1438 /* not implemented here */
1439 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n");
1444 /* hack for writing relative sensitivity of MH and TPHs */
1445 if ((esd->senst == 0) && (thispacket == 0)) {
1448 if ((thistile == 0) && !doneMH) {
1449 /* we have to manage MH addresses */
1450 addr1 = 0; /* start of MH */
1451 addr2 = j2k->cstr_info->main_head_end; /* end of MH */
1452 /* set special dvalue for this MH */
1454 doneMH = OPJ_TRUE; /* don't come here anymore */
1455 vv--; /* wrap back loop counter */
1457 } else if (!doneTPH) {
1458 /* we have to manage TPH addresses */
1459 addr1 = j2k->cstr_info->tile[thistile].start_pos;
1460 addr2 = j2k->cstr_info->tile[thistile].end_header;
1461 /* set special dvalue for this TPH */
1463 doneTPH = OPJ_TRUE; /* don't come here till the next tile */
1464 vv--; /* wrap back loop counter */
1468 doneTPH = OPJ_FALSE; /* reset TPH counter */
1470 /* write the addresses to the buffer */
1471 switch (esd->ad_size) {
1479 *(buf++) = (unsigned char) (addr1 >> 8);
1480 *(buf++) = (unsigned char) (addr1 >> 0);
1481 *(buf++) = (unsigned char) (addr2 >> 8);
1482 *(buf++) = (unsigned char) (addr2 >> 0);
1487 *(buf++) = (unsigned char) (addr1 >> 24);
1488 *(buf++) = (unsigned char) (addr1 >> 16);
1489 *(buf++) = (unsigned char) (addr1 >> 8);
1490 *(buf++) = (unsigned char) (addr1 >> 0);
1491 *(buf++) = (unsigned char) (addr2 >> 24);
1492 *(buf++) = (unsigned char) (addr2 >> 16);
1493 *(buf++) = (unsigned char) (addr2 >> 8);
1494 *(buf++) = (unsigned char) (addr2 >> 0);
1503 /* let's fill the value field */
1504 switch (esd->senst) {
1506 /* relative sensitivity */
1508 /* we just write down the packet ordering */
1511 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1512 else if (dvalue == -1)
1514 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1516 /* packet: first is most important, and then in decreasing order
1517 down to the last, which counts for 1 */
1518 dvalue = jpwl_pfp_to_double((unsigned short) (j2k->cstr_info->packno - thispacket), esd->se_size);
1523 /* !!! WRONG: let's put here disto field of packets !!! */
1529 dvalue = oldMSE - MSE;
1540 dvalue = PSNR - oldPSNR;
1547 opj_event_msg(j2k->cinfo, EVT_WARNING, "MAXERR sensitivity mode is not implemented\n");
1558 opj_event_msg(j2k->cinfo, EVT_WARNING, "Reserved sensitivity mode is not implemented\n");
1566 /* compute the pseudo-floating point value */
1567 pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size);
1569 /* write the pfp value to the buffer */
1570 switch (esd->se_size) {
1574 *(buf++) = (unsigned char) (pfpvalue >> 0);
1579 *(buf++) = (unsigned char) (pfpvalue >> 8);
1580 *(buf++) = (unsigned char) (pfpvalue >> 0);
1589 opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) {
1592 *(buf++) = (unsigned char) (J2K_MS_ESD >> 8);
1593 *(buf++) = (unsigned char) (J2K_MS_ESD >> 0);
1596 *(buf++) = (unsigned char) (esd->Lesd >> 8);
1597 *(buf++) = (unsigned char) (esd->Lesd >> 0);
1600 if (esd->numcomps >= 257)
1601 *(buf++) = (unsigned char) (esd->Cesd >> 8);
1602 *(buf++) = (unsigned char) (esd->Cesd >> 0);
1605 *(buf++) = (unsigned char) (esd->Pesd >> 0);
1608 if (esd->numcomps < 257)
1609 memset(buf, 0xAA, (size_t) esd->Lesd - 4);
1610 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/
1612 memset(buf, 0xAA, (size_t) esd->Lesd - 5);
1613 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/
1615 /* update markers struct */
1616 j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2);
1621 unsigned short int jpwl_double_to_pfp(double V, int bytes) {
1623 unsigned short int em, e, m;
1632 } else if (V > MAX_V1) {
1636 e = (unsigned short int) (floor(log(V) * 1.44269504088896) / 4.0);
1637 m = (unsigned short int) (0.5 + (V / (pow(2.0, (double) (4 * e)))));
1639 em = ((e & 0x000F) << 4) + (m & 0x000F);
1647 } else if (V > MAX_V2) {
1651 e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15;
1652 m = (unsigned short int) (0.5 + 2048.0 * ((V / (pow(2.0, (double) e - 15.0))) - 1.0));
1654 em = ((e & 0x001F) << 11) + (m & 0x07FF);
1666 double jpwl_pfp_to_double(unsigned short int em, int bytes) {
1673 V = (double) (em & 0x0F) * pow(2.0, (double) (em & 0xF0));
1678 V = pow(2.0, (double) ((em & 0xF800) >> 11) - 15.0) * (1.0 + (double) (em & 0x07FF) / 2048.0);
1691 opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num) {
1694 unsigned long int addlen;
1696 opj_codestream_info_t *info = j2k->cstr_info;
1697 int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->packno;
1699 if (!j2k || !jwmarker ) {
1700 opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n");
1704 /* main_head_end: how many markers are there before? */
1706 for (mm = 0; mm < jwmarker_num; mm++)
1707 if (jwmarker[mm].pos < (unsigned long int) info->main_head_end)
1708 addlen += jwmarker[mm].len + 2;
1709 info->main_head_end += addlen;
1711 /* codestream_size: always increment with all markers */
1713 for (mm = 0; mm < jwmarker_num; mm++)
1714 addlen += jwmarker[mm].len + 2;
1715 info->codestream_size += addlen;
1717 /* navigate through all the tiles */
1718 for (tileno = 0; tileno < numtiles; tileno++) {
1720 /* start_pos: increment with markers before SOT */
1722 for (mm = 0; mm < jwmarker_num; mm++)
1723 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos)
1724 addlen += jwmarker[mm].len + 2;
1725 info->tile[tileno].start_pos += addlen;
1727 /* end_header: increment with markers before of it */
1729 for (mm = 0; mm < jwmarker_num; mm++)
1730 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header)
1731 addlen += jwmarker[mm].len + 2;
1732 info->tile[tileno].end_header += addlen;
1734 /* end_pos: increment with markers before the end of this tile */
1735 /* code is disabled, since according to JPWL no markers can be beyond TPH */
1737 for (mm = 0; mm < jwmarker_num; mm++)
1738 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos)
1739 addlen += jwmarker[mm].len + 2;
1740 info->tile[tileno].end_pos += addlen;
1742 /* navigate through all the tile parts */
1743 for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) {
1745 /* start_pos: increment with markers before SOT */
1747 for (mm = 0; mm < jwmarker_num; mm++)
1748 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_start_pos)
1749 addlen += jwmarker[mm].len + 2;
1750 info->tile[tileno].tp[tpno].tp_start_pos += addlen;
1752 /* end_header: increment with markers before of it */
1754 for (mm = 0; mm < jwmarker_num; mm++)
1755 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_header)
1756 addlen += jwmarker[mm].len + 2;
1757 info->tile[tileno].tp[tpno].tp_end_header += addlen;
1759 /* end_pos: increment with markers before the end of this tile part */
1761 for (mm = 0; mm < jwmarker_num; mm++)
1762 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_pos)
1763 addlen += jwmarker[mm].len + 2;
1764 info->tile[tileno].tp[tpno].tp_end_pos += addlen;
1768 /* navigate through all the packets in this tile */
1769 for (packno = 0; packno < numpacks; packno++) {
1771 /* start_pos: increment with markers before the packet */
1772 /* disabled for the same reason as before */
1774 for (mm = 0; mm < jwmarker_num; mm++)
1775 if (jwmarker[mm].pos <= (unsigned long int) info->tile[tileno].packet[packno].start_pos)
1776 addlen += jwmarker[mm].len + 2;
1777 info->tile[tileno].packet[packno].start_pos += addlen;
1779 /* end_ph_pos: increment with markers before the packet */
1780 /* disabled for the same reason as before */
1782 for (mm = 0; mm < jwmarker_num; mm++)
1783 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos)
1784 addlen += jwmarker[mm].len + 2;*/
1785 info->tile[tileno].packet[packno].end_ph_pos += addlen;
1787 /* end_pos: increment if marker is before the end of packet */
1788 /* disabled for the same reason as before */
1790 for (mm = 0; mm < jwmarker_num; mm++)
1791 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos)
1792 addlen += jwmarker[mm].len + 2;*/
1793 info->tile[tileno].packet[packno].end_pos += addlen;
1798 /* reorder the markers list */
1803 #endif /* USE_JPWL */