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);
75 } else if (diff < 0) {
82 int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
83 opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot,
84 double place_pos, int tileno,
85 unsigned long int pre_len, unsigned long int post_len)
88 jpwl_epb_ms_t *epb_mark = NULL;
90 int k_pre, k_post, n_pre, n_post;
92 unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0;
94 /* We find RS(n,k) for EPB parms and pre-data, if any */
95 if (insideMH && (*idx == 0)) {
99 } else if (!insideMH && (*idx == 0)) {
100 /* First EPB in TH */
104 /* Following EPBs in MH or TH */
109 /* Find lengths, Figs. B3 and B4 */
110 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
113 /* size of pre-data redundancy */
114 /* (redundancy per codeword) * (number of codewords, rounded up) */
115 L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre);
117 /* Find protection type for post data and its associated redundancy field length*/
118 if ((hprot == 16) || (hprot == 32)) {
119 /* there is a CRC for post-data */
121 n_post = post_len + (hprot >> 3);
122 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
124 } else if ((hprot >= 37) && (hprot <= 128)) {
125 /* there is a RS for post-data */
130 /* Use predefined codes */
135 /* Create the EPB(s) */
136 while (post_len > 0) {
138 /* maximum postlen in order to respect EPB size
139 (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/
140 /* (message word size) * (number of containable parity words) */
141 max_postlen = k_post * (unsigned long int) floor((double)
142 JPWL_MAXIMUM_EPB_ROOM / (double)(n_post - k_post));
144 /* maximum postlen in order to respect EPB size */
146 /* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */
147 /* (message word size) * (number of containable parity words) */
149 max_postlen = k_post * (unsigned long int) floor((double)(
150 JPWL_MAXIMUM_EPB_ROOM - L2) / (double)(n_post - k_post));
154 /* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */
155 /* (message word size) * (number of containable parity words) */
157 max_postlen = k_post * (unsigned long int) floor((double)
158 JPWL_MAXIMUM_EPB_ROOM / (double)(n_post - k_post));
161 /* null protection case */
162 /* the max post length can be as large as the LDPepb field can host */
164 max_postlen = INT_MAX;
168 dL4 = min(max_postlen, post_len);
170 if ((epb_mark = jpwl_epb_create(
171 j2k, /* this encoder handle */
172 latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */
173 packed, /* is it packed? */
174 tileno, /* we are in TPH */
175 *idx, /* its index */
176 hprot, /* protection type parameters of following data */
177 0, /* pre-data: nothing for now */
178 dL4 /* post-data: the stub computed previously */
181 /* Add this marker to the 'insertanda' list */
182 if (*jwmarker_num < JPWL_MAX_NO_MARKERS) {
183 jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */
184 jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */
185 jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */
186 jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(
187 *idx); /* not very first! */
188 jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */
189 jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */
190 jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */
191 jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */
192 jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */
196 /* increment epb index */
199 /* decrease postlen */
202 /* increase the total length of EPBs */
203 epbs_len += epb_mark->Lepb + 2;
206 /* ooops, problems */
207 opj_event_msg(j2k->cinfo, EVT_ERROR,
208 "Could not create TPH EPB for UEP in tile %d\n", tileno);
216 jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed,
217 int tileno, int idx, int hprot,
218 unsigned long int pre_len, unsigned long int post_len)
221 jpwl_epb_ms_t *epb = NULL;
222 /*unsigned short int data_len = 0;*/
223 unsigned short int L2, L3;
224 unsigned long int L1, L4;
225 /*unsigned char *predata_in = NULL;*/
227 opj_bool insideMH = (tileno == -1);
230 if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof(jpwl_epb_ms_t)))) {
231 opj_event_msg(j2k->cinfo, EVT_ERROR,
232 "Could not allocate room for one EPB MS\n");
236 /* We set RS(n,k) for EPB parms and pre-data, if any */
237 if (insideMH && (idx == 0)) {
238 /* First EPB in MH */
241 } else if (!insideMH && (idx == 0)) {
242 /* First EPB in TH */
246 /* Following EPBs in MH or TH */
251 /* Find lengths, Figs. B3 and B4 */
252 /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
254 epb->pre_len = pre_len;
256 /* size of pre-data redundancy */
257 /* (redundancy per codeword) * (number of codewords, rounded up) */
258 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 /
259 (double) epb->k_pre);
261 /* length of post-data */
263 epb->post_len = post_len;
265 /* Find protection type for post data and its associated redundancy field length*/
266 if ((hprot == 16) || (hprot == 32)) {
267 /* there is a CRC for post-data */
268 epb->Pepb = 0x10000000 | ((unsigned long int) hprot >>
269 5); /* 0=CRC-16, 1=CRC-32 */
270 epb->k_post = post_len;
271 epb->n_post = post_len + (hprot >> 3);
272 /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
274 } else if ((hprot >= 37) && (hprot <= 128)) {
275 /* there is a RS for post-data */
276 epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8);
280 } else if (hprot == 1) {
281 /* Use predefined codes */
282 epb->Pepb = (unsigned long int) 0x00000000;
283 epb->n_post = epb->n_pre;
284 epb->k_post = epb->k_pre;
286 } else if (hprot == 0) {
287 /* Placeholder EPB: only protects its parameters, no protection method */
288 epb->Pepb = (unsigned long int) 0xFFFFFFFF;
293 opj_event_msg(j2k->cinfo, EVT_ERROR,
294 "Invalid protection value for EPB h = %d\n", hprot);
300 /* (redundancy per codeword) * (number of codewords, rounded up) */
301 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 /
302 (double) epb->k_post);
305 epb->tileno = tileno;
307 /* Fill some fields of the EPB */
309 /* total length of the EPB MS (less the EPB marker itself): */
310 /* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */
311 epb->Lepb = 11 + L2 + L3;
314 epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) |
317 /* length of data protected by EPB: */
318 epb->LDPepb = L1 + L4;
323 void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf)
327 *(buf++) = (unsigned char)(J2K_MS_EPB >> 8);
328 *(buf++) = (unsigned char)(J2K_MS_EPB >> 0);
331 *(buf++) = (unsigned char)(epb->Lepb >> 8);
332 *(buf++) = (unsigned char)(epb->Lepb >> 0);
335 *(buf++) = (unsigned char)(epb->Depb >> 0);
338 *(buf++) = (unsigned char)(epb->LDPepb >> 24);
339 *(buf++) = (unsigned char)(epb->LDPepb >> 16);
340 *(buf++) = (unsigned char)(epb->LDPepb >> 8);
341 *(buf++) = (unsigned char)(epb->LDPepb >> 0);
344 *(buf++) = (unsigned char)(epb->Pepb >> 24);
345 *(buf++) = (unsigned char)(epb->Pepb >> 16);
346 *(buf++) = (unsigned char)(epb->Pepb >> 8);
347 *(buf++) = (unsigned char)(epb->Pepb >> 0);
350 /*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/
351 memset(buf, 0, (size_t) epb->Lepb - 11);
353 /* update markers struct */
354 j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2);
359 jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on,
360 opj_bool epb_on, opj_bool info_on)
363 jpwl_epc_ms_t *epc = NULL;
366 if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof(jpwl_epc_ms_t)))) {
367 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n");
371 /* Set the EPC parameters */
372 epc->esd_on = esd_on;
373 epc->epb_on = epb_on;
374 epc->red_on = red_on;
375 epc->info_on = info_on;
377 /* Fill the EPC fields with default values */
380 epc->DL = 0x00000000;
381 epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) <<
383 ((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7);
388 opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf,
389 unsigned char *post_buf)
392 unsigned long int L1, L2, L3, L4;
394 unsigned long int P, NN_P;
396 /* Operating buffer */
397 static unsigned char codeword[NN], *parityword;
399 unsigned char *L1_buf, *L2_buf;
400 /* these ones are static, since we need to keep memory of
401 the exact place from one call to the other */
402 static unsigned char *L3_buf, *L4_buf;
404 /* some consistency check */
406 opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n");
410 if (!post_buf && !L4_buf) {
411 opj_event_msg(j2k->cinfo, EVT_ERROR,
412 "There is no operating buffer for EPBs data\n");
417 * Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms)
420 /* Initialize RS structures */
421 P = epb->n_pre - epb->k_pre;
423 memset(codeword, 0, NN);
424 parityword = codeword + NN_P;
427 /* pre-data begins pre_len bytes before of EPB buf */
428 L1_buf = buf - epb->pre_len;
429 L1 = epb->pre_len + 13;
431 /* redundancy for pre-data begins immediately after EPB parms */
433 L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 /
434 (double) epb->k_pre);
437 the position of L4 buffer can be:
438 1) passed as a parameter: in that case use it
439 2) null: in that case use the previous (static) one
446 /* post-data redundancy begins immediately after pre-data redundancy */
447 L3_buf = L2_buf + L2;
448 L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 /
449 (double) epb->k_post);
451 /* let's check whether EPB length is sufficient to contain all these data */
452 if (epb->Lepb < (11 + L2 + L3)) {
453 opj_event_msg(j2k->cinfo, EVT_ERROR,
454 "There is no room in EPB data field for writing redundancy data\n");
456 /*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/
458 /* Compute redundancy of pre-data message words */
462 /* copy message data into codeword buffer */
463 if (remaining < epb->k_pre) {
464 /* the last message word is zero-padded */
465 memset(codeword, 0, NN);
466 memcpy(codeword, L1_buf, remaining);
471 memcpy(codeword, L1_buf, epb->k_pre);
472 L1_buf += epb->k_pre;
473 remaining -= epb->k_pre;
477 /* Encode the buffer and obtain parity bytes */
478 if (encode_rs(codeword, parityword))
479 opj_event_msg(j2k->cinfo, EVT_WARNING,
480 "Possible encoding error in codeword @ position #%d\n",
481 (L1_buf - buf) / epb->k_pre);
483 /* copy parity bytes only in redundancy buffer */
484 memcpy(L2_buf, parityword, P);
486 /* advance parity buffer */
491 * Compute parity bytes on post-data, may be absent if there are no data
493 /*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",
494 epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len,
496 if (epb->hprot < 0) {
498 /* there should be no EPB */
500 } else if (epb->hprot == 0) {
502 /* no protection for the data */
504 L4_buf += epb->post_len;
506 } else if (epb->hprot == 16) {
509 unsigned short int mycrc = 0x0000;
511 /* compute the CRC field (excluding itself) */
513 while (remaining--) {
514 jpwl_updateCRC16(&mycrc, *(L4_buf++));
517 /* write the CRC field */
518 *(L3_buf++) = (unsigned char)(mycrc >> 8);
519 *(L3_buf++) = (unsigned char)(mycrc >> 0);
521 } else if (epb->hprot == 32) {
524 unsigned long int mycrc = 0x00000000;
526 /* compute the CRC field (excluding itself) */
528 while (remaining--) {
529 jpwl_updateCRC32(&mycrc, *(L4_buf++));
532 /* write the CRC field */
533 *(L3_buf++) = (unsigned char)(mycrc >> 24);
534 *(L3_buf++) = (unsigned char)(mycrc >> 16);
535 *(L3_buf++) = (unsigned char)(mycrc >> 8);
536 *(L3_buf++) = (unsigned char)(mycrc >> 0);
542 /* Initialize RS structures */
543 P = epb->n_post - epb->k_post;
545 memset(codeword, 0, NN);
546 parityword = codeword + NN_P;
549 /* Compute redundancy of post-data message words */
553 /* copy message data into codeword buffer */
554 if (remaining < epb->k_post) {
555 /* the last message word is zero-padded */
556 memset(codeword, 0, NN);
557 memcpy(codeword, L4_buf, remaining);
562 memcpy(codeword, L4_buf, epb->k_post);
563 L4_buf += epb->k_post;
564 remaining -= epb->k_post;
568 /* Encode the buffer and obtain parity bytes */
569 if (encode_rs(codeword, parityword))
570 opj_event_msg(j2k->cinfo, EVT_WARNING,
571 "Possible encoding error in codeword @ position #%d\n",
572 (L4_buf - buf) / epb->k_post);
574 /* copy parity bytes only in redundancy buffer */
575 memcpy(L3_buf, parityword, P);
577 /* advance parity buffer */
587 opj_bool jpwl_correct(opj_j2k_t *j2k)
590 opj_cio_t *cio = j2k->cio;
592 static opj_bool mh_done = OPJ_FALSE;
593 int mark_pos, id, len, skips, sot_pos;
594 unsigned long int Psot = 0;
596 /* go back to marker position */
597 mark_pos = cio_tell(cio) - 2;
598 cio_seek(cio, mark_pos);
600 if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) {
602 int mark_val = 0, skipnum = 0;
606 first thing to do, if we are here, is to look whether
607 51 (skipnum) positions ahead there is an EPB, in case of MH
611 first thing to do, if we are here, is to look whether
612 45 (skipnum) positions ahead there is an EPB, in case of MH
614 /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */
615 skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2;
616 if ((cio->bp + skipnum) < cio->end) {
618 cio_skip(cio, skipnum);
620 /* check that you are not going beyond the end of codestream */
622 /* call EPB corrector */
623 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
624 cio->bp, /* pointer to EPB in codestream buffer */
625 0, /* EPB type: MH */
626 skipnum, /* length of pre-data */
627 -1, /* length of post-data: -1 means auto */
632 /* read the marker value */
633 mark_val = (*(cio->bp) << 8) | *(cio->bp + 1);
635 if (status && (mark_val == J2K_MS_EPB)) {
641 /* Disable correction in case of missing or bad head EPB */
642 /* We can't do better! */
643 /* PATCHED: 2008-01-25 */
644 /* MOVED UP: 2008-02-01 */
646 j2k->cp->correct = OPJ_FALSE;
647 opj_event_msg(j2k->cinfo, EVT_WARNING,
648 "Couldn't find the MH EPB: disabling JPWL\n");
655 if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) {
656 /* else, look if 12 positions ahead there is an EPB, in case of TPH */
657 cio_seek(cio, mark_pos);
658 if ((cio->bp + 12) < cio->end) {
662 /* call EPB corrector */
663 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
664 cio->bp, /* pointer to EPB in codestream buffer */
665 1, /* EPB type: TPH */
666 12, /* length of pre-data */
667 -1, /* length of post-data: -1 means auto */
681 /* for now, don't use this code */
683 /* else, look if here is an EPB, in case of other */
685 /* it cannot stay before the first MH EPB */
686 cio_seek(cio, mark_pos);
689 /* call EPB corrector */
690 status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
691 cio->bp, /* pointer to EPB in codestream buffer */
692 2, /* EPB type: TPH */
693 0, /* length of pre-data */
694 -1, /* length of post-data: -1 means auto */
705 /* nope, no EPBs probably, or they are so damaged that we can give up */
710 /* AN ATTEMPT OF PARSER */
711 /* NOT USED ACTUALLY */
713 /* go to the beginning of the file */
717 j2k->state = J2K_STATE_MHSOC;
719 /* cycle all over the markers */
720 while (cio_tell(cio) < cio->length) {
722 /* read the marker */
723 mark_pos = cio_tell(cio);
724 id = cio_read(cio, 2);
727 printf("Marker@%d: %X\n", cio_tell(cio) - 2, id);
729 /* do an action in response to the read marker */
736 j2k->state = J2K_STATE_MHSIZ;
743 j2k->state = J2K_STATE_MT;
748 /* particular case of SOD */
750 len = Psot - (mark_pos - sot_pos) - 2;
758 j2k->state = J2K_STATE_TPH;
759 sot_pos = mark_pos; /* position of SOT */
760 len = cio_read(cio, 2); /* read the length field */
761 cio_skip(cio, 2); /* this field is unnecessary */
762 Psot = cio_read(cio, 4); /* tile length */
768 j2k->state = J2K_STATE_MH;
769 /* read the length field */
770 len = cio_read(cio, 2);
776 /* read the length field */
777 len = cio_read(cio, 2);
783 /* skip to marker's end */
784 cio_skip(cio, skips);
791 opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type,
792 int pre_len, int post_len, int *conn,
793 unsigned char **L4_bufp)
796 /* Operating buffer */
797 unsigned char codeword[NN], *parityword;
799 unsigned long int P, NN_P;
800 unsigned long int L1, L4;
801 int remaining, n_pre, k_pre, n_post, k_post;
805 int orig_pos = cio_tell(j2k->cio);
807 unsigned char *L1_buf, *L2_buf;
808 unsigned char *L3_buf, *L4_buf;
810 unsigned long int LDPepb, Pepb;
811 unsigned short int Lepb;
814 int myconn, errnum = 0;
815 opj_bool errflag = OPJ_FALSE;
817 opj_cio_t *cio = j2k->cio;
819 /* check for common errors */
821 opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n");
828 /* pre-data correction */
850 /* automatic setup */
851 opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n");
857 opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n");
863 /* Initialize RS structures */
866 tt = (int) floor((float) P / 2.0F); /* correction capability of the code */
867 memset(codeword, 0, NN);
868 parityword = codeword + NN_P;
871 /* Correct pre-data message words */
872 L1_buf = buffer - pre_len;
873 L2_buf = buffer + 13;
877 /* always zero-pad codewords */
878 /* (this is required, since after decoding the zeros in the long codeword
879 could change, and keep unchanged in subsequent calls) */
880 memset(codeword, 0, NN);
882 /* copy codeword buffer into message bytes */
883 if (remaining < k_pre) {
884 memcpy(codeword, L1_buf, remaining);
886 memcpy(codeword, L1_buf, k_pre);
889 /* copy redundancy buffer in parity bytes */
890 memcpy(parityword, L2_buf, P);
892 /* Decode the buffer and possibly obtain corrected bytes */
893 status = eras_dec_rs(codeword, NULL, 0);
896 opj_event_msg(j2k->cinfo, EVT_WARNING,
897 "Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/
899 /* we can try to safely get out from the function:
900 if we are here, either this is not an EPB or the first codeword
901 is too damaged to be helpful */
902 /*return OPJ_FALSE;*/
904 } else if (status == 0) {
906 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
908 } else if (status <= tt) {
909 /* it has corrected 0 <= errs <= tt */
911 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
916 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
922 /* advance parity buffer */
923 if ((status >= 0) && (status <= tt))
924 /* copy back corrected parity only if all is OK */
926 memcpy(L2_buf, parityword, P);
930 /* advance message buffer */
931 if (remaining < k_pre) {
932 if ((status >= 0) && (status <= tt))
933 /* copy back corrected data only if all is OK */
935 memcpy(L1_buf, codeword, remaining);
941 if ((status >= 0) && (status <= tt))
942 /* copy back corrected data only if all is OK */
944 memcpy(L1_buf, codeword, k_pre);
956 opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum,
957 (float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/
959 /*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/
965 /* presumably, now, EPB parameters are correct */
968 /* Simply read the EPB parameters */
972 cio_skip(cio, 2); /* the marker */
973 Lepb = cio_read(cio, 2);
974 Depb = cio_read(cio, 1);
975 LDPepb = cio_read(cio, 4);
976 Pepb = cio_read(cio, 4);
978 /* What does Pepb tells us about the protection method? */
979 if (((Pepb & 0xF0000000) >> 28) == 0) {
980 sprintf(str1, "pred"); /* predefined */
981 } else if (((Pepb & 0xF0000000) >> 28) == 1) {
982 sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */
983 } else if (((Pepb & 0xF0000000) >> 28) == 2) {
984 sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */
985 } else if (Pepb == 0xFFFFFFFF) {
986 sprintf(str1, "nometh"); /* RS mode */
988 sprintf(str1, "unknown"); /* unknown */
991 /* Now we write them to screen */
992 if (!conn && post_len)
993 opj_event_msg(j2k->cinfo, EVT_INFO,
994 "EPB(%d): (%sl, %sp, %u), %lu, %s\n",
996 (Depb & 0x40) ? "" : "n", /* latest EPB or not? */
997 (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */
998 (Depb & 0x3F), /* EPB index value */
999 LDPepb, /*length of the data protected by the EPB */
1000 str1); /* protection method */
1003 /* well, we need to investigate how long is the connected length of packed EPBs */
1005 if ((Depb & 0x40) == 0) /* not latest in header */
1006 jpwl_epb_correct(j2k, /* J2K decompressor handle */
1007 buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */
1008 2, /* EPB type: should be of other type */
1009 0, /* only EPB fields */
1010 0, /* do not look after */
1019 printf("connected = %d\n", myconn);*/
1021 /*cio_seek(j2k->cio, orig_pos);
1025 the position of L4 buffer is at the end of currently connected EPBs
1028 L4_buf = buffer + myconn;
1029 } else if (!(*L4_bufp)) {
1030 L4_buf = buffer + myconn;
1034 if (post_len == -1) {
1035 L4 = LDPepb - pre_len - 13;
1036 } else if (post_len == 0) {
1044 /* Do a further check here on the read parameters */
1045 if (L4 > (unsigned long) cio_numbytesleft(j2k->cio))
1051 /* we are ready for decoding the remaining data */
1052 if (((Pepb & 0xF0000000) >> 28) == 1) {
1054 if ((16 * ((Pepb & 0x00000001) + 1)) == 16) {
1057 unsigned short int mycrc = 0x0000, filecrc = 0x0000;
1059 /* compute the CRC field */
1061 while (remaining--) {
1062 jpwl_updateCRC16(&mycrc, *(L4_buf++));
1065 /* read the CRC field */
1066 filecrc = *(L3_buf++) << 8;
1067 filecrc |= *(L3_buf++);
1069 /* check the CRC field */
1070 if (mycrc == filecrc) {
1072 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1076 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc,
1083 if ((16 * ((Pepb & 0x00000001) + 1)) == 32) {
1086 unsigned long int mycrc = 0x00000000, filecrc = 0x00000000;
1088 /* compute the CRC field */
1090 while (remaining--) {
1091 jpwl_updateCRC32(&mycrc, *(L4_buf++));
1094 /* read the CRC field */
1095 filecrc = *(L3_buf++) << 24;
1096 filecrc |= *(L3_buf++) << 16;
1097 filecrc |= *(L3_buf++) << 8;
1098 filecrc |= *(L3_buf++);
1100 /* check the CRC field */
1101 if (mycrc == filecrc) {
1103 opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
1107 opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc,
1114 } else if (Pepb == 0xFFFFFFFF) {
1117 /* advance without doing anything */
1119 while (remaining--) {
1123 } else if ((((Pepb & 0xF0000000) >> 28) == 2) ||
1124 (((Pepb & 0xF0000000) >> 28) == 0)) {
1125 /* RS coding here */
1127 if (((Pepb & 0xF0000000) >> 28) == 0) {
1135 n_post = (Pepb & 0x0000FF00) >> 8;
1138 /* Initialize RS structures */
1139 P = n_post - k_post;
1141 tt = (int) floor((float) P / 2.0F); /* again, correction capability */
1142 memset(codeword, 0, NN);
1143 parityword = codeword + NN_P;
1146 /* Correct post-data message words */
1147 /*L4_buf = buffer + Lepb + 2;*/
1152 /* always zero-pad codewords */
1153 /* (this is required, since after decoding the zeros in the long codeword
1154 could change, and keep unchanged in subsequent calls) */
1155 memset(codeword, 0, NN);
1157 /* copy codeword buffer into message bytes */
1158 if (remaining < k_post) {
1159 memcpy(codeword, L4_buf, remaining);
1161 memcpy(codeword, L4_buf, k_post);
1164 /* copy redundancy buffer in parity bytes */
1165 memcpy(parityword, L3_buf, P);
1167 /* Decode the buffer and possibly obtain corrected bytes */
1168 status = eras_dec_rs(codeword, NULL, 0);
1171 opj_event_msg(j2k->cinfo, EVT_WARNING,
1172 "Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/
1175 } else if (status == 0) {
1177 opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
1179 } else if (status <= tt) {
1181 opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
1186 opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
1192 /* advance parity buffer */
1193 if ((status >= 0) && (status <= tt))
1194 /* copy back corrected data only if all is OK */
1196 memcpy(L3_buf, parityword, P);
1200 /* advance message buffer */
1201 if (remaining < k_post) {
1202 if ((status >= 0) && (status <= tt))
1203 /* copy back corrected data only if all is OK */
1205 memcpy(L4_buf, codeword, remaining);
1207 L4_buf += remaining;
1211 if ((status >= 0) && (status <= tt))
1212 /* copy back corrected data only if all is OK */
1214 memcpy(L4_buf, codeword, k_post);
1217 remaining -= k_post;
1223 /* give back the L4_buf address */
1232 opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n",
1234 (float) errnum / (float) LDPepb);
1236 opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n");
1241 cio_seek(j2k->cio, orig_pos);
1246 void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf)
1250 *(buf++) = (unsigned char)(J2K_MS_EPC >> 8);
1251 *(buf++) = (unsigned char)(J2K_MS_EPC >> 0);
1254 *(buf++) = (unsigned char)(epc->Lepc >> 8);
1255 *(buf++) = (unsigned char)(epc->Lepc >> 0);
1258 *(buf++) = (unsigned char)(epc->Pcrc >> 8);
1259 *(buf++) = (unsigned char)(epc->Pcrc >> 0);
1262 *(buf++) = (unsigned char)(epc->DL >> 24);
1263 *(buf++) = (unsigned char)(epc->DL >> 16);
1264 *(buf++) = (unsigned char)(epc->DL >> 8);
1265 *(buf++) = (unsigned char)(epc->DL >> 0);
1268 *(buf++) = (unsigned char)(epc->Pepc >> 0);
1271 /*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/
1272 memset(buf, 0, (size_t) epc->Lepc - 9);
1274 /* update markers struct */
1275 j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2);
1279 int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
1280 int comps, unsigned char addrm, unsigned char ad_size,
1281 unsigned char senst, unsigned char se_size,
1282 double place_pos, int tileno)
1288 jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp,
1289 unsigned char addrm, unsigned char ad_size,
1290 unsigned char senst, int se_size, int tileno,
1291 unsigned long int svalnum, void *sensval)
1294 jpwl_esd_ms_t *esd = NULL;
1297 if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof(jpwl_esd_ms_t)))) {
1298 opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n");
1302 /* if relative sensitivity, activate byte range mode */
1307 /* size of sensval's ... */
1308 if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) {
1309 opj_event_msg(j2k->cinfo, EVT_ERROR,
1310 "Address size %d for ESD MS is forbidden\n", ad_size);
1313 if ((se_size != 1) && (se_size != 2)) {
1314 opj_event_msg(j2k->cinfo, EVT_ERROR,
1315 "Sensitivity size %d for ESD MS is forbidden\n", se_size);
1319 /* ... depends on the addressing mode */
1324 ad_size = 0; /* as per the standard */
1325 esd->sensval_size = (unsigned int)se_size;
1330 /* auto sense address size */
1332 /* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes
1333 (we keep space for possible EPBs being inserted) */
1335 ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2;
1337 esd->sensval_size = ad_size + ad_size + se_size;
1342 /* auto sense address size */
1344 /* if there are more than 2^16 - 1 packets, switch to 4 bytes */
1346 ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2;
1348 esd->sensval_size = ad_size + ad_size + se_size;
1352 opj_event_msg(j2k->cinfo, EVT_ERROR,
1353 "Address mode %d for ESD MS is unimplemented\n", addrm);
1357 opj_event_msg(j2k->cinfo, EVT_ERROR,
1358 "Address mode %d for ESD MS is forbidden\n", addrm);
1362 /* set or unset sensitivity values */
1367 /* just based on the portions of a codestream */
1369 /* MH + no. of THs + no. of packets */
1370 svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) *
1371 (1 + j2k->cstr_info->packno);
1374 /* all the ones that are based on the packets */
1377 /* MH: all the packets and all the tiles info is written */
1379 svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno;
1381 /* TPH: only that tile info is written */
1383 svalnum = j2k->cstr_info->packno;
1390 /* fill private fields */
1392 esd->ad_size = ad_size;
1393 esd->se_size = se_size;
1395 esd->svalnum = svalnum;
1396 esd->numcomps = j2k->image->numcomps;
1397 esd->tileno = tileno;
1399 /* Set the ESD parameters */
1400 /* length, excluding data field */
1401 if (esd->numcomps < 257) {
1402 esd->Lesd = 4 + (unsigned short int)(esd->svalnum * esd->sensval_size);
1404 esd->Lesd = 5 + (unsigned short int)(esd->svalnum * esd->sensval_size);
1407 /* component data field */
1411 /* we are averaging */
1418 esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */
1419 esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */
1420 esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */
1421 esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */
1422 esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */
1424 /* if pointer to sensval is NULL, we can fill data field by ourselves */
1427 /* old code moved to jpwl_esd_fill() */
1431 /* we set the data field as the sensitivity values poinnter passed to the function */
1432 esd->data = (unsigned char *) sensval;
1438 opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf)
1442 unsigned long int vv;
1443 unsigned long int addr1 = 0L, addr2 = 0L;
1444 double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR,
1446 unsigned short int pfpvalue;
1447 unsigned long int addrmask = 0x00000000;
1448 opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE;
1450 /* sensitivity values in image info are as follows:
1451 - for each tile, distotile is the starting distortion for that tile, sum of all components
1452 - for each packet in a tile, disto is the distortion reduction caused by that packet to that tile
1453 - the TSE for a single tile should be given by distotile - sum(disto) , for all components
1454 - the MSE for a single tile is given by TSE / nbpix , for all components
1455 - the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components
1456 (Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images
1459 /* browse all components and find Omax */
1461 for (i = 0; i < j2k->image->numcomps; i++) {
1462 tmp = pow(2.0, (double)(j2k->image->comps[i].sgnd ?
1463 (j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1;
1468 Omax2 = Omax2 * Omax2;
1470 /* if pointer of esd->data is not null, simply write down all the values byte by byte */
1472 for (i = 0; i < (int) esd->svalnum; i++) {
1473 *(buf++) = esd->data[i];
1478 /* addressing mask */
1479 if (esd->ad_size == 2) {
1480 addrmask = 0x0000FFFF; /* two bytes */
1482 addrmask = 0xFFFFFFFF; /* four bytes */
1485 /* set on precise point where sensitivity starts */
1486 if (esd->numcomps < 257) {
1492 /* let's fill the data fields */
1493 for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno);
1494 vv < esd->svalnum; vv++) {
1496 int thistile = vv / j2k->cstr_info->packno,
1497 thispacket = vv % j2k->cstr_info->packno;
1499 /* skip for the hack some lines below */
1500 if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th) {
1504 /* starting tile distortion */
1505 if (thispacket == 0) {
1506 TSE = j2k->cstr_info->tile[thistile].distotile;
1507 oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1508 oldPSNR = 10.0 * log10(Omax2 / oldMSE);
1512 TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto;
1515 MSE = TSE / j2k->cstr_info->tile[thistile].numpix;
1518 PSNR = 10.0 * log10(Omax2 / MSE);
1520 /* fill the address range */
1521 switch (esd->addrm) {
1525 /* nothing, there is none */
1530 /* start address of packet */
1531 addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) &
1533 /* end address of packet */
1534 addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask;
1539 /* not implemented here */
1540 opj_event_msg(j2k->cinfo, EVT_WARNING,
1541 "Addressing mode packet_range is not implemented\n");
1544 /* unknown addressing method */
1546 /* not implemented here */
1547 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n");
1552 /* hack for writing relative sensitivity of MH and TPHs */
1553 if ((esd->senst == 0) && (thispacket == 0)) {
1556 if ((thistile == 0) && !doneMH) {
1557 /* we have to manage MH addresses */
1558 addr1 = 0; /* start of MH */
1559 addr2 = j2k->cstr_info->main_head_end; /* end of MH */
1560 /* set special dvalue for this MH */
1562 doneMH = OPJ_TRUE; /* don't come here anymore */
1563 vv--; /* wrap back loop counter */
1565 } else if (!doneTPH) {
1566 /* we have to manage TPH addresses */
1567 addr1 = j2k->cstr_info->tile[thistile].start_pos;
1568 addr2 = j2k->cstr_info->tile[thistile].end_header;
1569 /* set special dvalue for this TPH */
1571 doneTPH = OPJ_TRUE; /* don't come here till the next tile */
1572 vv--; /* wrap back loop counter */
1576 doneTPH = OPJ_FALSE; /* reset TPH counter */
1579 /* write the addresses to the buffer */
1580 switch (esd->ad_size) {
1588 *(buf++) = (unsigned char)(addr1 >> 8);
1589 *(buf++) = (unsigned char)(addr1 >> 0);
1590 *(buf++) = (unsigned char)(addr2 >> 8);
1591 *(buf++) = (unsigned char)(addr2 >> 0);
1596 *(buf++) = (unsigned char)(addr1 >> 24);
1597 *(buf++) = (unsigned char)(addr1 >> 16);
1598 *(buf++) = (unsigned char)(addr1 >> 8);
1599 *(buf++) = (unsigned char)(addr1 >> 0);
1600 *(buf++) = (unsigned char)(addr2 >> 24);
1601 *(buf++) = (unsigned char)(addr2 >> 16);
1602 *(buf++) = (unsigned char)(addr2 >> 8);
1603 *(buf++) = (unsigned char)(addr2 >> 0);
1612 /* let's fill the value field */
1613 switch (esd->senst) {
1615 /* relative sensitivity */
1617 /* we just write down the packet ordering */
1621 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1622 } else if (dvalue == -1)
1625 dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
1627 /* packet: first is most important, and then in decreasing order
1628 down to the last, which counts for 1 */
1630 dvalue = jpwl_pfp_to_double((unsigned short)(j2k->cstr_info->packno -
1631 thispacket), esd->se_size);
1637 /* !!! WRONG: let's put here disto field of packets !!! */
1643 dvalue = oldMSE - MSE;
1654 dvalue = PSNR - oldPSNR;
1661 opj_event_msg(j2k->cinfo, EVT_WARNING,
1662 "MAXERR sensitivity mode is not implemented\n");
1673 opj_event_msg(j2k->cinfo, EVT_WARNING,
1674 "Reserved sensitivity mode is not implemented\n");
1682 /* compute the pseudo-floating point value */
1683 pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size);
1685 /* write the pfp value to the buffer */
1686 switch (esd->se_size) {
1690 *(buf++) = (unsigned char)(pfpvalue >> 0);
1695 *(buf++) = (unsigned char)(pfpvalue >> 8);
1696 *(buf++) = (unsigned char)(pfpvalue >> 0);
1705 opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf)
1709 *(buf++) = (unsigned char)(J2K_MS_ESD >> 8);
1710 *(buf++) = (unsigned char)(J2K_MS_ESD >> 0);
1713 *(buf++) = (unsigned char)(esd->Lesd >> 8);
1714 *(buf++) = (unsigned char)(esd->Lesd >> 0);
1717 if (esd->numcomps >= 257) {
1718 *(buf++) = (unsigned char)(esd->Cesd >> 8);
1720 *(buf++) = (unsigned char)(esd->Cesd >> 0);
1723 *(buf++) = (unsigned char)(esd->Pesd >> 0);
1726 if (esd->numcomps < 257) {
1727 memset(buf, 0xAA, (size_t) esd->Lesd - 4);
1729 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/
1731 memset(buf, 0xAA, (size_t) esd->Lesd - 5);
1733 /*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/
1735 /* update markers struct */
1736 j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2);
1741 unsigned short int jpwl_double_to_pfp(double V, int bytes)
1744 unsigned short int em, e, m;
1753 } else if (V > MAX_V1) {
1757 e = (unsigned short int)(floor(log(V) * 1.44269504088896) / 4.0);
1758 m = (unsigned short int)(0.5 + (V / (pow(2.0, (double)(4 * e)))));
1760 em = ((e & 0x000F) << 4) + (m & 0x000F);
1768 } else if (V > MAX_V2) {
1772 e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15;
1773 m = (unsigned short int)(0.5 + 2048.0 * ((V / (pow(2.0,
1774 (double) e - 15.0))) - 1.0));
1776 em = ((e & 0x001F) << 11) + (m & 0x07FF);
1788 double jpwl_pfp_to_double(unsigned short int em, int bytes)
1796 V = (double)(em & 0x0F) * pow(2.0, (double)(em & 0xF0));
1801 V = pow(2.0, (double)((em & 0xF800) >> 11) - 15.0) * (1.0 + (double)(
1802 em & 0x07FF) / 2048.0);
1815 opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker,
1820 unsigned long int addlen;
1822 opj_codestream_info_t *info = j2k->cstr_info;
1823 int tileno, tpno, packno, numtiles = info->th * info->tw,
1824 numpacks = info->packno;
1826 if (!j2k || !jwmarker) {
1827 opj_event_msg(j2k->cinfo, EVT_ERROR,
1828 "J2K handle or JPWL markers list badly allocated\n");
1832 /* main_head_end: how many markers are there before? */
1834 for (mm = 0; mm < jwmarker_num; mm++)
1835 if (jwmarker[mm].pos < (unsigned long int) info->main_head_end) {
1836 addlen += jwmarker[mm].len + 2;
1838 info->main_head_end += addlen;
1840 /* codestream_size: always increment with all markers */
1842 for (mm = 0; mm < jwmarker_num; mm++) {
1843 addlen += jwmarker[mm].len + 2;
1845 info->codestream_size += addlen;
1847 /* navigate through all the tiles */
1848 for (tileno = 0; tileno < numtiles; tileno++) {
1850 /* start_pos: increment with markers before SOT */
1852 for (mm = 0; mm < jwmarker_num; mm++)
1853 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos) {
1854 addlen += jwmarker[mm].len + 2;
1856 info->tile[tileno].start_pos += addlen;
1858 /* end_header: increment with markers before of it */
1860 for (mm = 0; mm < jwmarker_num; mm++)
1861 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header) {
1862 addlen += jwmarker[mm].len + 2;
1864 info->tile[tileno].end_header += addlen;
1866 /* end_pos: increment with markers before the end of this tile */
1867 /* code is disabled, since according to JPWL no markers can be beyond TPH */
1869 for (mm = 0; mm < jwmarker_num; mm++)
1870 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos) {
1871 addlen += jwmarker[mm].len + 2;
1873 info->tile[tileno].end_pos += addlen;
1875 /* navigate through all the tile parts */
1876 for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) {
1878 /* start_pos: increment with markers before SOT */
1880 for (mm = 0; mm < jwmarker_num; mm++)
1881 if (jwmarker[mm].pos < (unsigned long int)
1882 info->tile[tileno].tp[tpno].tp_start_pos) {
1883 addlen += jwmarker[mm].len + 2;
1885 info->tile[tileno].tp[tpno].tp_start_pos += addlen;
1887 /* end_header: increment with markers before of it */
1889 for (mm = 0; mm < jwmarker_num; mm++)
1890 if (jwmarker[mm].pos < (unsigned long int)
1891 info->tile[tileno].tp[tpno].tp_end_header) {
1892 addlen += jwmarker[mm].len + 2;
1894 info->tile[tileno].tp[tpno].tp_end_header += addlen;
1896 /* end_pos: increment with markers before the end of this tile part */
1898 for (mm = 0; mm < jwmarker_num; mm++)
1899 if (jwmarker[mm].pos < (unsigned long int)
1900 info->tile[tileno].tp[tpno].tp_end_pos) {
1901 addlen += jwmarker[mm].len + 2;
1903 info->tile[tileno].tp[tpno].tp_end_pos += addlen;
1907 /* navigate through all the packets in this tile */
1908 for (packno = 0; packno < numpacks; packno++) {
1910 /* start_pos: increment with markers before the packet */
1911 /* disabled for the same reason as before */
1913 for (mm = 0; mm < jwmarker_num; mm++)
1914 if (jwmarker[mm].pos <= (unsigned long int)
1915 info->tile[tileno].packet[packno].start_pos) {
1916 addlen += jwmarker[mm].len + 2;
1918 info->tile[tileno].packet[packno].start_pos += addlen;
1920 /* end_ph_pos: increment with markers before the packet */
1921 /* disabled for the same reason as before */
1923 for (mm = 0; mm < jwmarker_num; mm++)
1924 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos)
1925 addlen += jwmarker[mm].len + 2;*/
1926 info->tile[tileno].packet[packno].end_ph_pos += addlen;
1928 /* end_pos: increment if marker is before the end of packet */
1929 /* disabled for the same reason as before */
1931 for (mm = 0; mm < jwmarker_num; mm++)
1932 if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos)
1933 addlen += jwmarker[mm].len + 2;*/
1934 info->tile[tileno].packet[packno].end_pos += addlen;
1939 /* reorder the markers list */
1944 #endif /* USE_JPWL */