1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
|
/*
* The copyright in this software is being made available under the 2-clauses
* BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef USE_JPWL
#include "opj_includes.h"
#include <limits.h>
/** Minimum and maximum values for the double->pfp conversion */
#define MIN_V1 0.0
#define MAX_V1 17293822569102704640.0
#define MIN_V2 0.000030517578125
#define MAX_V2 131040.0
/** conversion between a double precision floating point
number and the corresponding pseudo-floating point used
to represent sensitivity values
@param V the double precision value
@param bytes the number of bytes of the representation
@return the pseudo-floating point value (cast accordingly)
*/
unsigned short int jpwl_double_to_pfp(double V, int bytes);
/** conversion between a pseudo-floating point used
to represent sensitivity values and the corresponding
double precision floating point number
@param em the pseudo-floating point value (cast accordingly)
@param bytes the number of bytes of the representation
@return the double precision value
*/
double jpwl_pfp_to_double(unsigned short int em, int bytes);
/*-------------------------------------------------------------*/
int jpwl_markcomp(const void *arg1, const void *arg2)
{
/* Compare the two markers' positions */
double diff = (((jpwl_marker_t *) arg1)->dpos - ((jpwl_marker_t *) arg2)->dpos);
if (diff == 0.0)
return (0);
else if (diff < 0)
return (-1);
else
return (+1);
}
int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot,
double place_pos, int tileno,
unsigned long int pre_len, unsigned long int post_len)
{
jpwl_epb_ms_t *epb_mark = NULL;
int k_pre, k_post, n_pre, n_post;
unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0;
/* We find RS(n,k) for EPB parms and pre-data, if any */
if (insideMH && (*idx == 0)) {
/* First EPB in MH */
k_pre = 64;
n_pre = 160;
} else if (!insideMH && (*idx == 0)) {
/* First EPB in TH */
k_pre = 25;
n_pre = 80;
} else {
/* Following EPBs in MH or TH */
k_pre = 13;
n_pre = 40;
};
/* Find lengths, Figs. B3 and B4 */
/* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
L1 = pre_len + 13;
/* size of pre-data redundancy */
/* (redundancy per codeword) * (number of codewords, rounded up) */
L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre);
/* Find protection type for post data and its associated redundancy field length*/
if ((hprot == 16) || (hprot == 32)) {
/* there is a CRC for post-data */
k_post = post_len;
n_post = post_len + (hprot >> 3);
/*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
} else if ((hprot >= 37) && (hprot <= 128)) {
/* there is a RS for post-data */
k_post = 32;
n_post = hprot;
} else {
/* Use predefined codes */
n_post = n_pre;
k_post = k_pre;
};
/* Create the EPB(s) */
while (post_len > 0) {
/* maximum postlen in order to respect EPB size
(we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/
/* (message word size) * (number of containable parity words) */
max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
/* maximum postlen in order to respect EPB size */
if (*idx == 0)
/* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */
/* (message word size) * (number of containable parity words) */
max_postlen = k_post * (unsigned long int) floor((double) (JPWL_MAXIMUM_EPB_ROOM - L2) / (double) (n_post - k_post));
else
/* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */
/* (message word size) * (number of containable parity words) */
max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post));
/* null protection case */
/* the max post length can be as large as the LDPepb field can host */
if (hprot == 0)
max_postlen = INT_MAX;
/* length to use */
dL4 = min(max_postlen, post_len);
if ((epb_mark = jpwl_epb_create(
j2k, /* this encoder handle */
latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */
packed, /* is it packed? */
tileno, /* we are in TPH */
*idx, /* its index */
hprot, /* protection type parameters of following data */
0, /* pre-data: nothing for now */
dL4 /* post-data: the stub computed previously */
))) {
/* Add this marker to the 'insertanda' list */
if (*jwmarker_num < JPWL_MAX_NO_MARKERS) {
jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */
jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */
jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */
jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(*idx); /* not very first! */
jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */
jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */
jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */
jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */
jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */
(*jwmarker_num)++;
}
/* increment epb index */
(*idx)++;
/* decrease postlen */
post_len -= dL4;
/* increase the total length of EPBs */
epbs_len += epb_mark->Lepb + 2;
} else {
/* ooops, problems */
opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB for UEP in tile %d\n", tileno);
};
}
return epbs_len;
}
jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, int tileno, int idx, int hprot,
unsigned long int pre_len, unsigned long int post_len)
{
jpwl_epb_ms_t *epb = NULL;
/*unsigned short int data_len = 0;*/
unsigned short int L2, L3;
unsigned long int L1, L4;
/*unsigned char *predata_in = NULL;*/
opj_bool insideMH = (tileno == -1);
/* Alloc space */
if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epb_ms_t)))) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for one EPB MS\n");
return NULL;
};
/* We set RS(n,k) for EPB parms and pre-data, if any */
if (insideMH && (idx == 0)) {
/* First EPB in MH */
epb->k_pre = 64;
epb->n_pre = 160;
} else if (!insideMH && (idx == 0)) {
/* First EPB in TH */
epb->k_pre = 25;
epb->n_pre = 80;
} else {
/* Following EPBs in MH or TH */
epb->k_pre = 13;
epb->n_pre = 40;
};
/* Find lengths, Figs. B3 and B4 */
/* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */
L1 = pre_len + 13;
epb->pre_len = pre_len;
/* size of pre-data redundancy */
/* (redundancy per codeword) * (number of codewords, rounded up) */
L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
/* length of post-data */
L4 = post_len;
epb->post_len = post_len;
/* Find protection type for post data and its associated redundancy field length*/
if ((hprot == 16) || (hprot == 32)) {
/* there is a CRC for post-data */
epb->Pepb = 0x10000000 | ((unsigned long int) hprot >> 5); /* 0=CRC-16, 1=CRC-32 */
epb->k_post = post_len;
epb->n_post = post_len + (hprot >> 3);
/*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */
} else if ((hprot >= 37) && (hprot <= 128)) {
/* there is a RS for post-data */
epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8);
epb->k_post = 32;
epb->n_post = hprot;
} else if (hprot == 1) {
/* Use predefined codes */
epb->Pepb = (unsigned long int) 0x00000000;
epb->n_post = epb->n_pre;
epb->k_post = epb->k_pre;
} else if (hprot == 0) {
/* Placeholder EPB: only protects its parameters, no protection method */
epb->Pepb = (unsigned long int) 0xFFFFFFFF;
epb->n_post = 1;
epb->k_post = 1;
} else {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Invalid protection value for EPB h = %d\n", hprot);
return NULL;
}
epb->hprot = hprot;
/* (redundancy per codeword) * (number of codewords, rounded up) */
L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
/* private fields */
epb->tileno = tileno;
/* Fill some fields of the EPB */
/* total length of the EPB MS (less the EPB marker itself): */
/* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */
epb->Lepb = 11 + L2 + L3;
/* EPB style */
epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) | (idx & 0x003F);
/* length of data protected by EPB: */
epb->LDPepb = L1 + L4;
return epb;
}
void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf)
{
/* Marker */
*(buf++) = (unsigned char) (J2K_MS_EPB >> 8);
*(buf++) = (unsigned char) (J2K_MS_EPB >> 0);
/* Lepb */
*(buf++) = (unsigned char) (epb->Lepb >> 8);
*(buf++) = (unsigned char) (epb->Lepb >> 0);
/* Depb */
*(buf++) = (unsigned char) (epb->Depb >> 0);
/* LDPepb */
*(buf++) = (unsigned char) (epb->LDPepb >> 24);
*(buf++) = (unsigned char) (epb->LDPepb >> 16);
*(buf++) = (unsigned char) (epb->LDPepb >> 8);
*(buf++) = (unsigned char) (epb->LDPepb >> 0);
/* Pepb */
*(buf++) = (unsigned char) (epb->Pepb >> 24);
*(buf++) = (unsigned char) (epb->Pepb >> 16);
*(buf++) = (unsigned char) (epb->Pepb >> 8);
*(buf++) = (unsigned char) (epb->Pepb >> 0);
/* Data */
/*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/
memset(buf, 0, (size_t) epb->Lepb - 11);
/* update markers struct */
j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2);
}
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)
{
jpwl_epc_ms_t *epc = NULL;
/* Alloc space */
if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epc_ms_t)))) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n");
return NULL;
};
/* Set the EPC parameters */
epc->esd_on = esd_on;
epc->epb_on = epb_on;
epc->red_on = red_on;
epc->info_on = info_on;
/* Fill the EPC fields with default values */
epc->Lepc = 9;
epc->Pcrc = 0x0000;
epc->DL = 0x00000000;
epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) << 5) |
((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7);
return (epc);
}
opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf, unsigned char *post_buf)
{
unsigned long int L1, L2, L3, L4;
int remaining;
unsigned long int P, NN_P;
/* Operating buffer */
static unsigned char codeword[NN], *parityword;
unsigned char *L1_buf, *L2_buf;
/* these ones are static, since we need to keep memory of
the exact place from one call to the other */
static unsigned char *L3_buf, *L4_buf;
/* some consistency check */
if (!buf) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n");
return OPJ_FALSE;
}
if (!post_buf && !L4_buf) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs data\n");
return OPJ_FALSE;
}
/*
* Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms)
*/
/* Initialize RS structures */
P = epb->n_pre - epb->k_pre;
NN_P = NN - P;
memset(codeword, 0, NN);
parityword = codeword + NN_P;
init_rs(NN_P);
/* pre-data begins pre_len bytes before of EPB buf */
L1_buf = buf - epb->pre_len;
L1 = epb->pre_len + 13;
/* redundancy for pre-data begins immediately after EPB parms */
L2_buf = buf + 13;
L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre);
/* post-data
the position of L4 buffer can be:
1) passed as a parameter: in that case use it
2) null: in that case use the previous (static) one
*/
if (post_buf)
L4_buf = post_buf;
L4 = epb->post_len;
/* post-data redundancy begins immediately after pre-data redundancy */
L3_buf = L2_buf + L2;
L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post);
/* let's check whether EPB length is sufficient to contain all these data */
if (epb->Lepb < (11 + L2 + L3))
opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no room in EPB data field for writing redundancy data\n");
/*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/
/* Compute redundancy of pre-data message words */
remaining = L1;
while (remaining) {
/* copy message data into codeword buffer */
if (remaining < epb->k_pre) {
/* the last message word is zero-padded */
memset(codeword, 0, NN);
memcpy(codeword, L1_buf, remaining);
L1_buf += remaining;
remaining = 0;
} else {
memcpy(codeword, L1_buf, epb->k_pre);
L1_buf += epb->k_pre;
remaining -= epb->k_pre;
}
/* Encode the buffer and obtain parity bytes */
if (encode_rs(codeword, parityword))
opj_event_msg(j2k->cinfo, EVT_WARNING,
"Possible encoding error in codeword @ position #%d\n", (L1_buf - buf) / epb->k_pre);
/* copy parity bytes only in redundancy buffer */
memcpy(L2_buf, parityword, P);
/* advance parity buffer */
L2_buf += P;
}
/*
* Compute parity bytes on post-data, may be absent if there are no data
*/
/*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",
epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len,
epb->post_len);*/
if (epb->hprot < 0) {
/* there should be no EPB */
} else if (epb->hprot == 0) {
/* no protection for the data */
/* advance anyway */
L4_buf += epb->post_len;
} else if (epb->hprot == 16) {
/* CRC-16 */
unsigned short int mycrc = 0x0000;
/* compute the CRC field (excluding itself) */
remaining = L4;
while (remaining--)
jpwl_updateCRC16(&mycrc, *(L4_buf++));
/* write the CRC field */
*(L3_buf++) = (unsigned char) (mycrc >> 8);
*(L3_buf++) = (unsigned char) (mycrc >> 0);
} else if (epb->hprot == 32) {
/* CRC-32 */
unsigned long int mycrc = 0x00000000;
/* compute the CRC field (excluding itself) */
remaining = L4;
while (remaining--)
jpwl_updateCRC32(&mycrc, *(L4_buf++));
/* write the CRC field */
*(L3_buf++) = (unsigned char) (mycrc >> 24);
*(L3_buf++) = (unsigned char) (mycrc >> 16);
*(L3_buf++) = (unsigned char) (mycrc >> 8);
*(L3_buf++) = (unsigned char) (mycrc >> 0);
} else {
/* RS */
/* Initialize RS structures */
P = epb->n_post - epb->k_post;
NN_P = NN - P;
memset(codeword, 0, NN);
parityword = codeword + NN_P;
init_rs(NN_P);
/* Compute redundancy of post-data message words */
remaining = L4;
while (remaining) {
/* copy message data into codeword buffer */
if (remaining < epb->k_post) {
/* the last message word is zero-padded */
memset(codeword, 0, NN);
memcpy(codeword, L4_buf, remaining);
L4_buf += remaining;
remaining = 0;
} else {
memcpy(codeword, L4_buf, epb->k_post);
L4_buf += epb->k_post;
remaining -= epb->k_post;
}
/* Encode the buffer and obtain parity bytes */
if (encode_rs(codeword, parityword))
opj_event_msg(j2k->cinfo, EVT_WARNING,
"Possible encoding error in codeword @ position #%d\n", (L4_buf - buf) / epb->k_post);
/* copy parity bytes only in redundancy buffer */
memcpy(L3_buf, parityword, P);
/* advance parity buffer */
L3_buf += P;
}
}
return OPJ_TRUE;
}
opj_bool jpwl_correct(opj_j2k_t *j2k)
{
opj_cio_t *cio = j2k->cio;
opj_bool status;
static opj_bool mh_done = OPJ_FALSE;
int mark_pos, id, len, skips, sot_pos;
unsigned long int Psot = 0;
/* go back to marker position */
mark_pos = cio_tell(cio) - 2;
cio_seek(cio, mark_pos);
if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) {
int mark_val = 0, skipnum = 0;
/*
COLOR IMAGE
first thing to do, if we are here, is to look whether
51 (skipnum) positions ahead there is an EPB, in case of MH
*/
/*
B/W IMAGE
first thing to do, if we are here, is to look whether
45 (skipnum) positions ahead there is an EPB, in case of MH
*/
/* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */
skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2;
if ((cio->bp + skipnum) < cio->end) {
cio_skip(cio, skipnum);
/* check that you are not going beyond the end of codestream */
/* call EPB corrector */
status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
cio->bp, /* pointer to EPB in codestream buffer */
0, /* EPB type: MH */
skipnum, /* length of pre-data */
-1, /* length of post-data: -1 means auto */
NULL,
NULL
);
/* read the marker value */
mark_val = (*(cio->bp) << 8) | *(cio->bp + 1);
if (status && (mark_val == J2K_MS_EPB)) {
/* we found it! */
mh_done = OPJ_TRUE;
return OPJ_TRUE;
}
/* Disable correction in case of missing or bad head EPB */
/* We can't do better! */
/* PATCHED: 2008-01-25 */
/* MOVED UP: 2008-02-01 */
if (!status) {
j2k->cp->correct = OPJ_FALSE;
opj_event_msg(j2k->cinfo, EVT_WARNING, "Couldn't find the MH EPB: disabling JPWL\n");
}
}
}
if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) {
/* else, look if 12 positions ahead there is an EPB, in case of TPH */
cio_seek(cio, mark_pos);
if ((cio->bp + 12) < cio->end) {
cio_skip(cio, 12);
/* call EPB corrector */
status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
cio->bp, /* pointer to EPB in codestream buffer */
1, /* EPB type: TPH */
12, /* length of pre-data */
-1, /* length of post-data: -1 means auto */
NULL,
NULL
);
if (status)
/* we found it! */
return OPJ_TRUE;
}
}
return OPJ_FALSE;
/* for now, don't use this code */
/* else, look if here is an EPB, in case of other */
if (mark_pos > 64) {
/* it cannot stay before the first MH EPB */
cio_seek(cio, mark_pos);
cio_skip(cio, 0);
/* call EPB corrector */
status = jpwl_epb_correct(j2k, /* J2K decompressor handle */
cio->bp, /* pointer to EPB in codestream buffer */
2, /* EPB type: TPH */
0, /* length of pre-data */
-1, /* length of post-data: -1 means auto */
NULL,
NULL
);
if (status)
/* we found it! */
return OPJ_TRUE;
}
/* nope, no EPBs probably, or they are so damaged that we can give up */
return OPJ_FALSE;
return OPJ_TRUE;
/* AN ATTEMPT OF PARSER */
/* NOT USED ACTUALLY */
/* go to the beginning of the file */
cio_seek(cio, 0);
/* let's begin */
j2k->state = J2K_STATE_MHSOC;
/* cycle all over the markers */
while (cio_tell(cio) < cio->length) {
/* read the marker */
mark_pos = cio_tell(cio);
id = cio_read(cio, 2);
/* details */
printf("Marker@%d: %X\n", cio_tell(cio) - 2, id);
/* do an action in response to the read marker */
switch (id) {
/* short markers */
/* SOC */
case J2K_MS_SOC:
j2k->state = J2K_STATE_MHSIZ;
len = 0;
skips = 0;
break;
/* EOC */
case J2K_MS_EOC:
j2k->state = J2K_STATE_MT;
len = 0;
skips = 0;
break;
/* particular case of SOD */
case J2K_MS_SOD:
len = Psot - (mark_pos - sot_pos) - 2;
skips = len;
break;
/* long markers */
/* SOT */
case J2K_MS_SOT:
j2k->state = J2K_STATE_TPH;
sot_pos = mark_pos; /* position of SOT */
len = cio_read(cio, 2); /* read the length field */
cio_skip(cio, 2); /* this field is unnecessary */
Psot = cio_read(cio, 4); /* tile length */
skips = len - 8;
break;
/* remaining */
case J2K_MS_SIZ:
j2k->state = J2K_STATE_MH;
/* read the length field */
len = cio_read(cio, 2);
skips = len - 2;
break;
/* remaining */
default:
/* read the length field */
len = cio_read(cio, 2);
skips = len - 2;
break;
}
/* skip to marker's end */
cio_skip(cio, skips);
}
}
opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type, int pre_len, int post_len, int *conn,
unsigned char **L4_bufp)
{
/* Operating buffer */
unsigned char codeword[NN], *parityword;
unsigned long int P, NN_P;
unsigned long int L1, L4;
int remaining, n_pre, k_pre, n_post, k_post;
int status, tt;
int orig_pos = cio_tell(j2k->cio);
unsigned char *L1_buf, *L2_buf;
unsigned char *L3_buf, *L4_buf;
unsigned long int LDPepb, Pepb;
unsigned short int Lepb;
unsigned char Depb;
char str1[25] = "";
int myconn, errnum = 0;
opj_bool errflag = OPJ_FALSE;
opj_cio_t *cio = j2k->cio;
/* check for common errors */
if (!buffer) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n");
return OPJ_FALSE;
}
/* set bignesses */
L1 = pre_len + 13;
/* pre-data correction */
switch (type) {
case 0:
/* MH EPB */
k_pre = 64;
n_pre = 160;
break;
case 1:
/* TPH EPB */
k_pre = 25;
n_pre = 80;
break;
case 2:
/* other EPBs */
k_pre = 13;
n_pre = 40;
break;
case 3:
/* automatic setup */
opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n");
return OPJ_FALSE;
break;
default:
/* unknown type */
opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n");
return OPJ_FALSE;
break;
}
/* Initialize RS structures */
P = n_pre - k_pre;
NN_P = NN - P;
tt = (int) floor((float) P / 2.0F); /* correction capability of the code */
memset(codeword, 0, NN);
parityword = codeword + NN_P;
init_rs(NN_P);
/* Correct pre-data message words */
L1_buf = buffer - pre_len;
L2_buf = buffer + 13;
remaining = L1;
while (remaining) {
/* always zero-pad codewords */
/* (this is required, since after decoding the zeros in the long codeword
could change, and keep unchanged in subsequent calls) */
memset(codeword, 0, NN);
/* copy codeword buffer into message bytes */
if (remaining < k_pre)
memcpy(codeword, L1_buf, remaining);
else
memcpy(codeword, L1_buf, k_pre);
/* copy redundancy buffer in parity bytes */
memcpy(parityword, L2_buf, P);
/* Decode the buffer and possibly obtain corrected bytes */
status = eras_dec_rs(codeword, NULL, 0);
if (status == -1) {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING,
"Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/
errflag = OPJ_TRUE;
/* we can try to safely get out from the function:
if we are here, either this is not an EPB or the first codeword
is too damaged to be helpful */
/*return OPJ_FALSE;*/
} else if (status == 0) {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
} else if (status <= tt) {
/* it has corrected 0 <= errs <= tt */
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
errnum += status;
} else {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
return OPJ_FALSE;*/
errflag = OPJ_TRUE;
}
/* advance parity buffer */
if ((status >= 0) && (status <= tt))
/* copy back corrected parity only if all is OK */
memcpy(L2_buf, parityword, P);
L2_buf += P;
/* advance message buffer */
if (remaining < k_pre) {
if ((status >= 0) && (status <= tt))
/* copy back corrected data only if all is OK */
memcpy(L1_buf, codeword, remaining);
L1_buf += remaining;
remaining = 0;
} else {
if ((status >= 0) && (status <= tt))
/* copy back corrected data only if all is OK */
memcpy(L1_buf, codeword, k_pre);
L1_buf += k_pre;
remaining -= k_pre;
}
}
/* print summary */
if (!conn) {
/*if (errnum)
opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum,
(float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/
if (errflag) {
/*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/
return OPJ_FALSE;
}
}
/* presumably, now, EPB parameters are correct */
/* let's get them */
/* Simply read the EPB parameters */
if (conn)
cio->bp = buffer;
cio_skip(cio, 2); /* the marker */
Lepb = cio_read(cio, 2);
Depb = cio_read(cio, 1);
LDPepb = cio_read(cio, 4);
Pepb = cio_read(cio, 4);
/* What does Pepb tells us about the protection method? */
if (((Pepb & 0xF0000000) >> 28) == 0)
sprintf(str1, "pred"); /* predefined */
else if (((Pepb & 0xF0000000) >> 28) == 1)
sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */
else if (((Pepb & 0xF0000000) >> 28) == 2)
sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */
else if (Pepb == 0xFFFFFFFF)
sprintf(str1, "nometh"); /* RS mode */
else
sprintf(str1, "unknown"); /* unknown */
/* Now we write them to screen */
if (!conn && post_len)
opj_event_msg(j2k->cinfo, EVT_INFO,
"EPB(%d): (%sl, %sp, %u), %lu, %s\n",
cio_tell(cio) - 13,
(Depb & 0x40) ? "" : "n", /* latest EPB or not? */
(Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */
(Depb & 0x3F), /* EPB index value */
LDPepb, /*length of the data protected by the EPB */
str1); /* protection method */
/* well, we need to investigate how long is the connected length of packed EPBs */
myconn = Lepb + 2;
if ((Depb & 0x40) == 0) /* not latest in header */
jpwl_epb_correct(j2k, /* J2K decompressor handle */
buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */
2, /* EPB type: should be of other type */
0, /* only EPB fields */
0, /* do not look after */
&myconn,
NULL
);
if (conn)
*conn += myconn;
/*if (!conn)
printf("connected = %d\n", myconn);*/
/*cio_seek(j2k->cio, orig_pos);
return OPJ_TRUE;*/
/* post-data
the position of L4 buffer is at the end of currently connected EPBs
*/
if (!(L4_bufp))
L4_buf = buffer + myconn;
else if (!(*L4_bufp))
L4_buf = buffer + myconn;
else
L4_buf = *L4_bufp;
if (post_len == -1)
L4 = LDPepb - pre_len - 13;
else if (post_len == 0)
L4 = 0;
else
L4 = post_len;
L3_buf = L2_buf;
/* Do a further check here on the read parameters */
if (L4 > (unsigned long) cio_numbytesleft(j2k->cio))
/* overflow */
return OPJ_FALSE;
/* we are ready for decoding the remaining data */
if (((Pepb & 0xF0000000) >> 28) == 1) {
/* CRC here */
if ((16 * ((Pepb & 0x00000001) + 1)) == 16) {
/* CRC-16 */
unsigned short int mycrc = 0x0000, filecrc = 0x0000;
/* compute the CRC field */
remaining = L4;
while (remaining--)
jpwl_updateCRC16(&mycrc, *(L4_buf++));
/* read the CRC field */
filecrc = *(L3_buf++) << 8;
filecrc |= *(L3_buf++);
/* check the CRC field */
if (mycrc == filecrc) {
if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
} else {
if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
errflag = OPJ_TRUE;
}
}
if ((16 * ((Pepb & 0x00000001) + 1)) == 32) {
/* CRC-32 */
unsigned long int mycrc = 0x00000000, filecrc = 0x00000000;
/* compute the CRC field */
remaining = L4;
while (remaining--)
jpwl_updateCRC32(&mycrc, *(L4_buf++));
/* read the CRC field */
filecrc = *(L3_buf++) << 24;
filecrc |= *(L3_buf++) << 16;
filecrc |= *(L3_buf++) << 8;
filecrc |= *(L3_buf++);
/* check the CRC field */
if (mycrc == filecrc) {
if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n");
} else {
if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc);
errflag = OPJ_TRUE;
}
}
} else if (Pepb == 0xFFFFFFFF) {
/* no method */
/* advance without doing anything */
remaining = L4;
while (remaining--)
L4_buf++;
} else if ((((Pepb & 0xF0000000) >> 28) == 2) || (((Pepb & 0xF0000000) >> 28) == 0)) {
/* RS coding here */
if (((Pepb & 0xF0000000) >> 28) == 0) {
k_post = k_pre;
n_post = n_pre;
} else {
k_post = 32;
n_post = (Pepb & 0x0000FF00) >> 8;
}
/* Initialize RS structures */
P = n_post - k_post;
NN_P = NN - P;
tt = (int) floor((float) P / 2.0F); /* again, correction capability */
memset(codeword, 0, NN);
parityword = codeword + NN_P;
init_rs(NN_P);
/* Correct post-data message words */
/*L4_buf = buffer + Lepb + 2;*/
L3_buf = L2_buf;
remaining = L4;
while (remaining) {
/* always zero-pad codewords */
/* (this is required, since after decoding the zeros in the long codeword
could change, and keep unchanged in subsequent calls) */
memset(codeword, 0, NN);
/* copy codeword buffer into message bytes */
if (remaining < k_post)
memcpy(codeword, L4_buf, remaining);
else
memcpy(codeword, L4_buf, k_post);
/* copy redundancy buffer in parity bytes */
memcpy(parityword, L3_buf, P);
/* Decode the buffer and possibly obtain corrected bytes */
status = eras_dec_rs(codeword, NULL, 0);
if (status == -1) {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING,
"Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/
errflag = OPJ_TRUE;
} else if (status == 0) {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/
} else if (status <= tt) {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/
errnum += status;
} else {
/*if (conn == NULL)
opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n");
return OPJ_FALSE;*/
errflag = OPJ_TRUE;
}
/* advance parity buffer */
if ((status >= 0) && (status <= tt))
/* copy back corrected data only if all is OK */
memcpy(L3_buf, parityword, P);
L3_buf += P;
/* advance message buffer */
if (remaining < k_post) {
if ((status >= 0) && (status <= tt))
/* copy back corrected data only if all is OK */
memcpy(L4_buf, codeword, remaining);
L4_buf += remaining;
remaining = 0;
} else {
if ((status >= 0) && (status <= tt))
/* copy back corrected data only if all is OK */
memcpy(L4_buf, codeword, k_post);
L4_buf += k_post;
remaining -= k_post;
}
}
}
/* give back the L4_buf address */
if (L4_bufp)
*L4_bufp = L4_buf;
/* print summary */
if (!conn) {
if (errnum)
opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n", errnum,
(float) errnum / (float) LDPepb);
if (errflag)
opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n");
}
cio_seek(j2k->cio, orig_pos);
return OPJ_TRUE;
}
void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf)
{
/* Marker */
*(buf++) = (unsigned char) (J2K_MS_EPC >> 8);
*(buf++) = (unsigned char) (J2K_MS_EPC >> 0);
/* Lepc */
*(buf++) = (unsigned char) (epc->Lepc >> 8);
*(buf++) = (unsigned char) (epc->Lepc >> 0);
/* Pcrc */
*(buf++) = (unsigned char) (epc->Pcrc >> 8);
*(buf++) = (unsigned char) (epc->Pcrc >> 0);
/* DL */
*(buf++) = (unsigned char) (epc->DL >> 24);
*(buf++) = (unsigned char) (epc->DL >> 16);
*(buf++) = (unsigned char) (epc->DL >> 8);
*(buf++) = (unsigned char) (epc->DL >> 0);
/* Pepc */
*(buf++) = (unsigned char) (epc->Pepc >> 0);
/* Data */
/*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/
memset(buf, 0, (size_t) epc->Lepc - 9);
/* update markers struct */
j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2);
}
int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num,
int comps, unsigned char addrm, unsigned char ad_size,
unsigned char senst, unsigned char se_size,
double place_pos, int tileno)
{
return 0;
}
jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp,
unsigned char addrm, unsigned char ad_size,
unsigned char senst, int se_size, int tileno,
unsigned long int svalnum, void *sensval)
{
jpwl_esd_ms_t *esd = NULL;
/* Alloc space */
if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_esd_ms_t)))) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n");
return NULL;
};
/* if relative sensitivity, activate byte range mode */
if (senst == 0)
addrm = 1;
/* size of sensval's ... */
if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Address size %d for ESD MS is forbidden\n", ad_size);
return NULL;
}
if ((se_size != 1) && (se_size != 2)) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "Sensitivity size %d for ESD MS is forbidden\n", se_size);
return NULL;
}
/* ... depends on the addressing mode */
switch (addrm) {
/* packet mode */
case (0):
ad_size = 0; /* as per the standard */
esd->sensval_size = (unsigned int)se_size;
break;
/* byte range */
case (1):
/* auto sense address size */
if (ad_size == 0)
/* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes
(we keep space for possible EPBs being inserted) */
ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2;
esd->sensval_size = ad_size + ad_size + se_size;
break;
/* packet range */
case (2):
/* auto sense address size */
if (ad_size == 0)
/* if there are more than 2^16 - 1 packets, switch to 4 bytes */
ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2;
esd->sensval_size = ad_size + ad_size + se_size;
break;
case (3):
opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is unimplemented\n", addrm);
return NULL;
default:
opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is forbidden\n", addrm);
return NULL;
}
/* set or unset sensitivity values */
if (svalnum <= 0) {
switch (senst) {
/* just based on the portions of a codestream */
case (0):
/* MH + no. of THs + no. of packets */
svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) * (1 + j2k->cstr_info->packno);
break;
/* all the ones that are based on the packets */
default:
if (tileno < 0)
/* MH: all the packets and all the tiles info is written */
svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno;
else
/* TPH: only that tile info is written */
svalnum = j2k->cstr_info->packno;
break;
}
}
/* fill private fields */
esd->senst = senst;
esd->ad_size = ad_size;
esd->se_size = se_size;
esd->addrm = addrm;
esd->svalnum = svalnum;
esd->numcomps = j2k->image->numcomps;
esd->tileno = tileno;
/* Set the ESD parameters */
/* length, excluding data field */
if (esd->numcomps < 257)
esd->Lesd = 4 + (unsigned short int) (esd->svalnum * esd->sensval_size);
else
esd->Lesd = 5 + (unsigned short int) (esd->svalnum * esd->sensval_size);
/* component data field */
if (comp >= 0)
esd->Cesd = comp;
else
/* we are averaging */
esd->Cesd = 0;
/* Pesd field */
esd->Pesd = 0x00;
esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */
esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */
esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */
esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */
esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */
/* if pointer to sensval is NULL, we can fill data field by ourselves */
if (!sensval) {
/* old code moved to jpwl_esd_fill() */
esd->data = NULL;
} else {
/* we set the data field as the sensitivity values poinnter passed to the function */
esd->data = (unsigned char *) sensval;
}
return (esd);
}
opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf)
{
int i;
unsigned long int vv;
unsigned long int addr1 = 0L, addr2 = 0L;
double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR, oldPSNR = 0.0;
unsigned short int pfpvalue;
unsigned long int addrmask = 0x00000000;
opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE;
/* sensitivity values in image info are as follows:
- for each tile, distotile is the starting distortion for that tile, sum of all components
- for each packet in a tile, disto is the distortion reduction caused by that packet to that tile
- the TSE for a single tile should be given by distotile - sum(disto) , for all components
- the MSE for a single tile is given by TSE / nbpix , for all components
- the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components
(Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images
*/
/* browse all components and find Omax */
Omax2 = 0.0;
for (i = 0; i < j2k->image->numcomps; i++) {
tmp = pow(2.0, (double) (j2k->image->comps[i].sgnd ?
(j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1;
if (tmp > Omax2)
Omax2 = tmp;
}
Omax2 = Omax2 * Omax2;
/* if pointer of esd->data is not null, simply write down all the values byte by byte */
if (esd->data) {
for (i = 0; i < (int) esd->svalnum; i++)
*(buf++) = esd->data[i];
return OPJ_TRUE;
}
/* addressing mask */
if (esd->ad_size == 2)
addrmask = 0x0000FFFF; /* two bytes */
else
addrmask = 0xFFFFFFFF; /* four bytes */
/* set on precise point where sensitivity starts */
if (esd->numcomps < 257)
buf += 6;
else
buf += 7;
/* let's fill the data fields */
for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno); vv < esd->svalnum; vv++) {
int thistile = vv / j2k->cstr_info->packno, thispacket = vv % j2k->cstr_info->packno;
/* skip for the hack some lines below */
if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th)
break;
/* starting tile distortion */
if (thispacket == 0) {
TSE = j2k->cstr_info->tile[thistile].distotile;
oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix;
oldPSNR = 10.0 * log10(Omax2 / oldMSE);
}
/* TSE */
TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto;
/* MSE */
MSE = TSE / j2k->cstr_info->tile[thistile].numpix;
/* PSNR */
PSNR = 10.0 * log10(Omax2 / MSE);
/* fill the address range */
switch (esd->addrm) {
/* packet mode */
case (0):
/* nothing, there is none */
break;
/* byte range */
case (1):
/* start address of packet */
addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) & addrmask;
/* end address of packet */
addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask;
break;
/* packet range */
case (2):
/* not implemented here */
opj_event_msg(j2k->cinfo, EVT_WARNING, "Addressing mode packet_range is not implemented\n");
break;
/* unknown addressing method */
default:
/* not implemented here */
opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n");
break;
}
/* hack for writing relative sensitivity of MH and TPHs */
if ((esd->senst == 0) && (thispacket == 0)) {
/* possible MH */
if ((thistile == 0) && !doneMH) {
/* we have to manage MH addresses */
addr1 = 0; /* start of MH */
addr2 = j2k->cstr_info->main_head_end; /* end of MH */
/* set special dvalue for this MH */
dvalue = -10.0;
doneMH = OPJ_TRUE; /* don't come here anymore */
vv--; /* wrap back loop counter */
} else if (!doneTPH) {
/* we have to manage TPH addresses */
addr1 = j2k->cstr_info->tile[thistile].start_pos;
addr2 = j2k->cstr_info->tile[thistile].end_header;
/* set special dvalue for this TPH */
dvalue = -1.0;
doneTPH = OPJ_TRUE; /* don't come here till the next tile */
vv--; /* wrap back loop counter */
}
} else
doneTPH = OPJ_FALSE; /* reset TPH counter */
/* write the addresses to the buffer */
switch (esd->ad_size) {
case (0):
/* do nothing */
break;
case (2):
/* two bytes */
*(buf++) = (unsigned char) (addr1 >> 8);
*(buf++) = (unsigned char) (addr1 >> 0);
*(buf++) = (unsigned char) (addr2 >> 8);
*(buf++) = (unsigned char) (addr2 >> 0);
break;
case (4):
/* four bytes */
*(buf++) = (unsigned char) (addr1 >> 24);
*(buf++) = (unsigned char) (addr1 >> 16);
*(buf++) = (unsigned char) (addr1 >> 8);
*(buf++) = (unsigned char) (addr1 >> 0);
*(buf++) = (unsigned char) (addr2 >> 24);
*(buf++) = (unsigned char) (addr2 >> 16);
*(buf++) = (unsigned char) (addr2 >> 8);
*(buf++) = (unsigned char) (addr2 >> 0);
break;
default:
/* do nothing */
break;
}
/* let's fill the value field */
switch (esd->senst) {
/* relative sensitivity */
case (0):
/* we just write down the packet ordering */
if (dvalue == -10)
/* MH */
dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
else if (dvalue == -1)
/* TPH */
dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */
else
/* packet: first is most important, and then in decreasing order
down to the last, which counts for 1 */
dvalue = jpwl_pfp_to_double((unsigned short) (j2k->cstr_info->packno - thispacket), esd->se_size);
break;
/* MSE */
case (1):
/* !!! WRONG: let's put here disto field of packets !!! */
dvalue = MSE;
break;
/* MSE reduction */
case (2):
dvalue = oldMSE - MSE;
oldMSE = MSE;
break;
/* PSNR */
case (3):
dvalue = PSNR;
break;
/* PSNR increase */
case (4):
dvalue = PSNR - oldPSNR;
oldPSNR = PSNR;
break;
/* MAXERR */
case (5):
dvalue = 0.0;
opj_event_msg(j2k->cinfo, EVT_WARNING, "MAXERR sensitivity mode is not implemented\n");
break;
/* TSE */
case (6):
dvalue = TSE;
break;
/* reserved */
case (7):
dvalue = 0.0;
opj_event_msg(j2k->cinfo, EVT_WARNING, "Reserved sensitivity mode is not implemented\n");
break;
default:
dvalue = 0.0;
break;
}
/* compute the pseudo-floating point value */
pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size);
/* write the pfp value to the buffer */
switch (esd->se_size) {
case (1):
/* one byte */
*(buf++) = (unsigned char) (pfpvalue >> 0);
break;
case (2):
/* two bytes */
*(buf++) = (unsigned char) (pfpvalue >> 8);
*(buf++) = (unsigned char) (pfpvalue >> 0);
break;
}
}
return OPJ_TRUE;
}
opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf)
{
/* Marker */
*(buf++) = (unsigned char) (J2K_MS_ESD >> 8);
*(buf++) = (unsigned char) (J2K_MS_ESD >> 0);
/* Lesd */
*(buf++) = (unsigned char) (esd->Lesd >> 8);
*(buf++) = (unsigned char) (esd->Lesd >> 0);
/* Cesd */
if (esd->numcomps >= 257)
*(buf++) = (unsigned char) (esd->Cesd >> 8);
*(buf++) = (unsigned char) (esd->Cesd >> 0);
/* Pesd */
*(buf++) = (unsigned char) (esd->Pesd >> 0);
/* Data */
if (esd->numcomps < 257)
memset(buf, 0xAA, (size_t) esd->Lesd - 4);
/*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/
else
memset(buf, 0xAA, (size_t) esd->Lesd - 5);
/*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/
/* update markers struct */
j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2);
return OPJ_TRUE;
}
unsigned short int jpwl_double_to_pfp(double V, int bytes)
{
unsigned short int em, e, m;
switch (bytes) {
case (1):
if (V < MIN_V1) {
e = 0x0000;
m = 0x0000;
} else if (V > MAX_V1) {
e = 0x000F;
m = 0x000F;
} else {
e = (unsigned short int) (floor(log(V) * 1.44269504088896) / 4.0);
m = (unsigned short int) (0.5 + (V / (pow(2.0, (double) (4 * e)))));
}
em = ((e & 0x000F) << 4) + (m & 0x000F);
break;
case (2):
if (V < MIN_V2) {
e = 0x0000;
m = 0x0000;
} else if (V > MAX_V2) {
e = 0x001F;
m = 0x07FF;
} else {
e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15;
m = (unsigned short int) (0.5 + 2048.0 * ((V / (pow(2.0, (double) e - 15.0))) - 1.0));
}
em = ((e & 0x001F) << 11) + (m & 0x07FF);
break;
default:
em = 0x0000;
break;
};
return em;
}
double jpwl_pfp_to_double(unsigned short int em, int bytes)
{
double V;
switch (bytes) {
case 1:
V = (double) (em & 0x0F) * pow(2.0, (double) (em & 0xF0));
break;
case 2:
V = pow(2.0, (double) ((em & 0xF800) >> 11) - 15.0) * (1.0 + (double) (em & 0x07FF) / 2048.0);
break;
default:
V = 0.0;
break;
}
return V;
}
opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num)
{
int mm;
unsigned long int addlen;
opj_codestream_info_t *info = j2k->cstr_info;
int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->packno;
if (!j2k || !jwmarker ) {
opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n");
return OPJ_FALSE;
}
/* main_head_end: how many markers are there before? */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->main_head_end)
addlen += jwmarker[mm].len + 2;
info->main_head_end += addlen;
/* codestream_size: always increment with all markers */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
addlen += jwmarker[mm].len + 2;
info->codestream_size += addlen;
/* navigate through all the tiles */
for (tileno = 0; tileno < numtiles; tileno++) {
/* start_pos: increment with markers before SOT */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].start_pos += addlen;
/* end_header: increment with markers before of it */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].end_header += addlen;
/* end_pos: increment with markers before the end of this tile */
/* code is disabled, since according to JPWL no markers can be beyond TPH */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].end_pos += addlen;
/* navigate through all the tile parts */
for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) {
/* start_pos: increment with markers before SOT */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_start_pos)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].tp[tpno].tp_start_pos += addlen;
/* end_header: increment with markers before of it */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_header)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].tp[tpno].tp_end_header += addlen;
/* end_pos: increment with markers before the end of this tile part */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_pos)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].tp[tpno].tp_end_pos += addlen;
}
/* navigate through all the packets in this tile */
for (packno = 0; packno < numpacks; packno++) {
/* start_pos: increment with markers before the packet */
/* disabled for the same reason as before */
addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos <= (unsigned long int) info->tile[tileno].packet[packno].start_pos)
addlen += jwmarker[mm].len + 2;
info->tile[tileno].packet[packno].start_pos += addlen;
/* end_ph_pos: increment with markers before the packet */
/* disabled for the same reason as before */
/*addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos)
addlen += jwmarker[mm].len + 2;*/
info->tile[tileno].packet[packno].end_ph_pos += addlen;
/* end_pos: increment if marker is before the end of packet */
/* disabled for the same reason as before */
/*addlen = 0;
for (mm = 0; mm < jwmarker_num; mm++)
if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos)
addlen += jwmarker[mm].len + 2;*/
info->tile[tileno].packet[packno].end_pos += addlen;
}
}
/* reorder the markers list */
return OPJ_TRUE;
}
#endif /* USE_JPWL */
|