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
|
/*
* Copyright (c) 1999-2000 Image Power, Inc. and the University of
* British Columbia.
* Copyright (c) 2001-2002 Michael David Adams.
* All rights reserved.
*/
/* __START_OF_JASPER_LICENSE__
*
* JasPer Software License
*
* IMAGE POWER JPEG-2000 PUBLIC LICENSE
* ************************************
*
* GRANT:
*
* Permission is hereby granted, free of charge, to any person (the "User")
* obtaining a copy of this software and associated documentation, to deal
* in the JasPer Software without restriction, including without limitation
* the right to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the JasPer Software (in source and binary forms),
* and to permit persons to whom the JasPer Software is furnished to do so,
* provided further that the License Conditions below are met.
*
* License Conditions
* ******************
*
* A. Redistributions of source code must retain the above copyright notice,
* and this list of conditions, and the following disclaimer.
*
* B. Redistributions in binary form must reproduce the above copyright
* notice, and this list of conditions, and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* C. Neither the name of Image Power, Inc. nor any other contributor
* (including, but not limited to, the University of British Columbia and
* Michael David Adams) may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* D. User agrees that it shall not commence any action against Image Power,
* Inc., the University of British Columbia, Michael David Adams, or any
* other contributors (collectively "Licensors") for infringement of any
* intellectual property rights ("IPR") held by the User in respect of any
* technology that User owns or has a right to license or sublicense and
* which is an element required in order to claim compliance with ISO/IEC
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
* rights worldwide arising under statutory or common law, and whether
* or not perfected, including, without limitation, all (i) patents and
* patent applications owned or licensable by User; (ii) rights associated
* with works of authorship including copyrights, copyright applications,
* copyright registrations, mask work rights, mask work applications,
* mask work registrations; (iii) rights relating to the protection of
* trade secrets and confidential information; (iv) any right analogous
* to those set forth in subsections (i), (ii), or (iii) and any other
* proprietary rights relating to intangible property (other than trademark,
* trade dress, or service mark rights); and (v) divisions, continuations,
* renewals, reissues and extensions of the foregoing (as and to the extent
* applicable) now existing, hereafter filed, issued or acquired.
*
* E. If User commences an infringement action against any Licensor(s) then
* such Licensor(s) shall have the right to terminate User's license and
* all sublicenses that have been granted hereunder by User to other parties.
*
* F. This software is for use only in hardware or software products that
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
* or right to this Software is granted for products that do not comply
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
* from the ISO.
*
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
* NOTICE SPECIFIED IN THIS SECTION.
*
* __END_OF_JASPER_LICENSE__
*/
/*
* Tier 1 Encoder
*
* $Id$
*/
/******************************************************************************\
* Includes.
\******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "jasper/jas_fix.h"
#include "jasper/jas_malloc.h"
#include "jasper/jas_math.h"
#include "jpc_t1enc.h"
#include "jpc_t1cod.h"
#include "jpc_enc.h"
#include "jpc_cod.h"
#include "jpc_math.h"
static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int,
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec);
static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int, jas_matrix_t *flags,
jas_matrix_t *data, int term, long *nmsedec);
static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int,
int, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec);
static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int,
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec);
static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int,
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec);
/******************************************************************************\
* Code for encoding code blocks.
\******************************************************************************/
/* Encode all of the code blocks associated with the current tile. */
int jpc_enc_enccblks(jpc_enc_t *enc)
{
jpc_enc_tcmpt_t *tcmpt;
jpc_enc_tcmpt_t *endcomps;
jpc_enc_rlvl_t *lvl;
jpc_enc_rlvl_t *endlvls;
jpc_enc_band_t *band;
jpc_enc_band_t *endbands;
jpc_enc_cblk_t *cblk;
jpc_enc_cblk_t *endcblks;
int i;
int j;
int mx;
int bmx;
int v;
jpc_enc_tile_t *tile;
uint_fast32_t prcno;
jpc_enc_prc_t *prc;
tile = enc->curtile;
endcomps = &tile->tcmpts[tile->numtcmpts];
for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) {
endlvls = &tcmpt->rlvls[tcmpt->numrlvls];
for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) {
if (!lvl->bands) {
continue;
}
endbands = &lvl->bands[lvl->numbands];
for (band = lvl->bands; band != endbands; ++band) {
if (!band->data) {
continue;
}
for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
if (!prc->cblks) {
continue;
}
bmx = 0;
endcblks = &prc->cblks[prc->numcblks];
for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
mx = 0;
for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) {
for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) {
v = abs(jas_matrix_get(cblk->data, i, j));
if (v > mx) {
mx = v;
}
}
}
if (mx > bmx) {
bmx = mx;
}
cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0);
}
for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
cblk->numimsbs = band->numbps - cblk->numbps;
assert(cblk->numimsbs >= 0);
}
for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) {
return -1;
}
}
}
}
}
}
return 0;
}
static int getthebyte(jas_stream_t *in, long off)
{
int c;
long oldpos;
oldpos = jas_stream_tell(in);
assert(oldpos >= 0);
jas_stream_seek(in, off, SEEK_SET);
c = jas_stream_peekc(in);
jas_stream_seek(in, oldpos, SEEK_SET);
return c;
}
/* Encode a single code block. */
int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, jpc_enc_band_t *band, jpc_enc_cblk_t *cblk)
{
jpc_enc_pass_t *pass;
jpc_enc_pass_t *endpasses;
int bitpos;
int n;
int adjust;
int ret;
int passtype;
int t;
jpc_bitstream_t *bout;
jpc_enc_pass_t *termpass;
jpc_enc_rlvl_t *rlvl;
int vcausal;
int segsym;
int termmode;
int c;
bout = 0;
rlvl = band->rlvl;
cblk->stream = jas_stream_memopen(0, 0);
assert(cblk->stream);
cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream);
assert(cblk->mqenc);
jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0;
if (cblk->numpasses > 0) {
cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t));
assert(cblk->passes);
} else {
cblk->passes = 0;
}
endpasses = &cblk->passes[cblk->numpasses];
for (pass = cblk->passes; pass != endpasses; ++pass) {
pass->start = 0;
pass->end = 0;
pass->term = JPC_ISTERMINATED(pass - cblk->passes, 0, cblk->numpasses, (tcmpt->cblksty & JPC_COX_TERMALL) != 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0);
pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0);
pass->lyrno = -1;
if (pass == endpasses - 1) {
assert(pass->term == 1);
pass->term = 1;
}
}
cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2,
jas_matrix_numcols(cblk->data) + 2);
assert(cblk->flags);
bitpos = cblk->numbps - 1;
pass = cblk->passes;
n = cblk->numpasses;
while (--n >= 0) {
if (pass->type == JPC_SEG_MQ) {
/* NOP */
} else {
assert(pass->type == JPC_SEG_RAW);
if (!bout) {
bout = jpc_bitstream_sopen(cblk->stream, "w");
assert(bout);
}
}
#if 1
passtype = (pass - cblk->passes + 2) % 3;
#else
passtype = JPC_PASSTYPE(pass - cblk->passes + 2);
#endif
pass->start = jas_stream_tell(cblk->stream);
#if 0
assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream));
#endif
assert(bitpos >= 0);
vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0;
segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0;
if (pass->term) {
termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ?
JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1;
} else {
termmode = 0;
}
switch (passtype) {
case JPC_SIGPASS:
ret = (pass->type == JPC_SEG_MQ) ? jpc_encsigpass(cblk->mqenc,
bitpos, band->orient, vcausal, cblk->flags,
cblk->data, termmode, &pass->nmsedec) :
jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags,
cblk->data, termmode, &pass->nmsedec);
break;
case JPC_REFPASS:
ret = (pass->type == JPC_SEG_MQ) ? jpc_encrefpass(cblk->mqenc,
bitpos, vcausal, cblk->flags, cblk->data, termmode,
&pass->nmsedec) : jpc_encrawrefpass(bout, bitpos,
vcausal, cblk->flags, cblk->data, termmode,
&pass->nmsedec);
break;
case JPC_CLNPASS:
assert(pass->type == JPC_SEG_MQ);
ret = jpc_encclnpass(cblk->mqenc, bitpos, band->orient,
vcausal, segsym, cblk->flags, cblk->data, termmode,
&pass->nmsedec);
break;
default:
assert(0);
break;
}
if (pass->type == JPC_SEG_MQ) {
if (pass->term) {
jpc_mqenc_init(cblk->mqenc);
}
jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate);
pass->end = jas_stream_tell(cblk->stream);
if (tcmpt->cblksty & JPC_COX_RESET) {
jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs);
}
} else {
if (pass->term) {
if (jpc_bitstream_pending(bout)) {
jpc_bitstream_outalign(bout, 0x2a);
}
jpc_bitstream_close(bout);
bout = 0;
pass->end = jas_stream_tell(cblk->stream);
} else {
pass->end = jas_stream_tell(cblk->stream) +
jpc_bitstream_pending(bout);
/* NOTE - This will not work. need to adjust by # of pending output bytes */
}
}
#if 0
/* XXX - This assertion fails sometimes when various coding modes are used.
This seems to be harmless, but why does it happen at all? */
assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream));
#endif
pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
jpc_fixtodbl(band->rlvl->tcmpt->synweight) *
jpc_fixtodbl(band->synweight) *
jpc_fixtodbl(band->synweight) *
jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) *
((double) (1 << bitpos)) * ((double)(1 << bitpos)) *
jpc_fixtodbl(pass->nmsedec);
pass->cumwmsedec = pass->wmsedec;
if (pass != cblk->passes) {
pass->cumwmsedec += pass[-1].cumwmsedec;
}
if (passtype == JPC_CLNPASS) {
--bitpos;
}
++pass;
}
#if 0
dump_passes(cblk->passes, cblk->numpasses, cblk);
#endif
n = 0;
endpasses = &cblk->passes[cblk->numpasses];
for (pass = cblk->passes; pass != endpasses; ++pass) {
if (pass->start < n) {
pass->start = n;
}
if (pass->end < n) {
pass->end = n;
}
if (!pass->term) {
termpass = pass;
while (termpass - pass < cblk->numpasses &&
!termpass->term) {
++termpass;
}
if (pass->type == JPC_SEG_MQ) {
t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0;
if (pass->mqencstate.ctreg >= 5) {
adjust = 4 + t;
} else {
adjust = 5 + t;
}
pass->end += adjust;
}
if (pass->end > termpass->end) {
pass->end = termpass->end;
}
if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) {
abort();
}
if (c == 0xff) {
++pass->end;
}
n = JAS_MAX(n, pass->end);
} else {
n = JAS_MAX(n, pass->end);
}
}
#if 0
dump_passes(cblk->passes, cblk->numpasses, cblk);
#endif
if (bout) {
jpc_bitstream_close(bout);
}
return 0;
}
/******************************************************************************\
* Code for significance pass.
\******************************************************************************/
#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \
{ \
int f; \
int v; \
f = *(fp); \
if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
v = (abs(*(dp)) & (one)) ? 1 : 0; \
jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
jpc_mqenc_putbit(mqenc, v); \
if (v) { \
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
v = ((*(dp) < 0) ? 1 : 0); \
jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \
jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \
JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
*(fp) |= JPC_SIG; \
} \
*(fp) |= JPC_VISIT; \
} \
}
static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag,
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec)
{
int i;
int j;
int one;
int vscanlen;
int width;
int height;
int frowstep;
int drowstep;
int fstripestep;
int dstripestep;
jpc_fix_t *fstripestart;
jpc_fix_t *dstripestart;
jpc_fix_t *fp;
jpc_fix_t *dp;
jpc_fix_t *fvscanstart;
jpc_fix_t *dvscanstart;
int k;
*nmsedec = 0;
width = jas_matrix_numcols(data);
height = jas_matrix_numrows(data);
frowstep = jas_matrix_rowstep(flags);
drowstep = jas_matrix_rowstep(data);
fstripestep = frowstep << 2;
dstripestep = drowstep << 2;
one = 1 << (bitpos + JPC_NUMEXTRABITS);
fstripestart = jas_matrix_getref(flags, 1, 1);
dstripestart = jas_matrix_getref(data, 0, 0);
for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
dstripestart += dstripestep) {
fvscanstart = fstripestart;
dvscanstart = dstripestart;
vscanlen = JAS_MIN(i, 4);
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
fp = fvscanstart;
dp = dvscanstart;
k = vscanlen;
sigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, orient, mqenc, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
sigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, orient, mqenc, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
sigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, orient, mqenc, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
sigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, orient, mqenc, 0);
}
}
if (term) {
jpc_mqenc_flush(mqenc, term - 1);
}
return jpc_mqenc_error(mqenc) ? (-1) : 0;
}
#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \
{ \
jpc_fix_t f = *(fp); \
jpc_fix_t v; \
if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
v = (abs(*(dp)) & (one)) ? 1 : 0; \
if ((jpc_bitstream_putbit((out), v)) == EOF) { \
return -1; \
} \
if (v) { \
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
v = ((*(dp) < 0) ? 1 : 0); \
if (jpc_bitstream_putbit(out, v) == EOF) { \
return -1; \
} \
JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \
*(fp) |= JPC_SIG; \
} \
*(fp) |= JPC_VISIT; \
} \
}
static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags,
jas_matrix_t *data, int term, long *nmsedec)
{
int i;
int j;
int k;
int one;
int vscanlen;
int width;
int height;
int frowstep;
int drowstep;
int fstripestep;
int dstripestep;
jpc_fix_t *fstripestart;
jpc_fix_t *dstripestart;
jpc_fix_t *fp;
jpc_fix_t *dp;
jpc_fix_t *fvscanstart;
jpc_fix_t *dvscanstart;
*nmsedec = 0;
width = jas_matrix_numcols(data);
height = jas_matrix_numrows(data);
frowstep = jas_matrix_rowstep(flags);
drowstep = jas_matrix_rowstep(data);
fstripestep = frowstep << 2;
dstripestep = drowstep << 2;
one = 1 << (bitpos + JPC_NUMEXTRABITS);
fstripestart = jas_matrix_getref(flags, 1, 1);
dstripestart = jas_matrix_getref(data, 0, 0);
for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
dstripestart += dstripestep) {
fvscanstart = fstripestart;
dvscanstart = dstripestart;
vscanlen = JAS_MIN(i, 4);
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
fp = fvscanstart;
dp = dvscanstart;
k = vscanlen;
rawsigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, out, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawsigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, out, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawsigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, out, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawsigpass_step(fp, frowstep, dp, bitpos, one,
nmsedec, out, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
}
}
if (term) {
jpc_bitstream_outalign(out, 0x2a);
}
return 0;
}
/******************************************************************************\
* Code for refinement pass.
\******************************************************************************/
#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \
{ \
int v; \
if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
(d) = *(dp); \
*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \
v = (abs(d) & (one)) ? 1 : 0; \
jpc_mqenc_putbit((mqenc), v); \
*(fp) |= JPC_REFINE; \
} \
}
static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data,
int term, long *nmsedec)
{
int i;
int j;
int one;
int vscanlen;
int d;
int width;
int height;
int frowstep;
int drowstep;
int fstripestep;
int dstripestep;
jpc_fix_t *fstripestart;
jpc_fix_t *dstripestart;
jpc_fix_t *fvscanstart;
jpc_fix_t *dvscanstart;
jpc_fix_t *dp;
jpc_fix_t *fp;
int k;
*nmsedec = 0;
width = jas_matrix_numcols(data);
height = jas_matrix_numrows(data);
frowstep = jas_matrix_rowstep(flags);
drowstep = jas_matrix_rowstep(data);
fstripestep = frowstep << 2;
dstripestep = drowstep << 2;
one = 1 << (bitpos + JPC_NUMEXTRABITS);
fstripestart = jas_matrix_getref(flags, 1, 1);
dstripestart = jas_matrix_getref(data, 0, 0);
for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
dstripestart += dstripestep) {
fvscanstart = fstripestart;
dvscanstart = dstripestart;
vscanlen = JAS_MIN(i, 4);
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
fp = fvscanstart;
dp = dvscanstart;
k = vscanlen;
refpass_step(fp, dp, bitpos, one, nmsedec,
mqenc, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
refpass_step(fp, dp, bitpos, one, nmsedec,
mqenc, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
refpass_step(fp, dp, bitpos, one, nmsedec,
mqenc, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
refpass_step(fp, dp, bitpos, one, nmsedec,
mqenc, 0);
}
}
if (term) {
jpc_mqenc_flush(mqenc, term - 1);
}
return jpc_mqenc_error(mqenc) ? (-1) : 0;
}
#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \
{ \
jpc_fix_t d; \
jpc_fix_t v; \
if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
d = *(dp); \
*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \
v = (abs(d) & (one)) ? 1 : 0; \
if (jpc_bitstream_putbit((out), v) == EOF) { \
return -1; \
} \
*(fp) |= JPC_REFINE; \
} \
}
static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags,
jas_matrix_t *data, int term, long *nmsedec)
{
int i;
int j;
int k;
int one;
int vscanlen;
int width;
int height;
int frowstep;
int drowstep;
int fstripestep;
int dstripestep;
jpc_fix_t *fstripestart;
jpc_fix_t *dstripestart;
jpc_fix_t *fvscanstart;
jpc_fix_t *dvscanstart;
jpc_fix_t *dp;
jpc_fix_t *fp;
*nmsedec = 0;
width = jas_matrix_numcols(data);
height = jas_matrix_numrows(data);
frowstep = jas_matrix_rowstep(flags);
drowstep = jas_matrix_rowstep(data);
fstripestep = frowstep << 2;
dstripestep = drowstep << 2;
one = 1 << (bitpos + JPC_NUMEXTRABITS);
fstripestart = jas_matrix_getref(flags, 1, 1);
dstripestart = jas_matrix_getref(data, 0, 0);
for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
dstripestart += dstripestep) {
fvscanstart = fstripestart;
dvscanstart = dstripestart;
vscanlen = JAS_MIN(i, 4);
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
fp = fvscanstart;
dp = dvscanstart;
k = vscanlen;
rawrefpass_step(fp, dp, bitpos, one, nmsedec,
out, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawrefpass_step(fp, dp, bitpos, one, nmsedec,
out, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawrefpass_step(fp, dp, bitpos, one, nmsedec,
out, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
rawrefpass_step(fp, dp, bitpos, one, nmsedec,
out, vcausalflag);
}
}
if (term) {
jpc_bitstream_outalign(out, 0x2a);
}
return 0;
}
/******************************************************************************\
* Code for cleanup pass.
\******************************************************************************/
#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \
{ \
int f; \
int v; \
label1 \
f = *(fp); \
if (!(f & (JPC_SIG | JPC_VISIT))) { \
jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \
v = (abs(*(dp)) & (one)) ? 1 : 0; \
jpc_mqenc_putbit((mqenc), v); \
if (v) { \
label2 \
f = *(fp); \
/* Coefficient is significant. */ \
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \
jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \
v = ((*(dp) < 0) ? 1 : 0); \
jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \
JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \
*(fp) |= JPC_SIG; \
} \
} \
*(fp) &= ~JPC_VISIT; \
}
static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags,
jas_matrix_t *data, int term, long *nmsedec)
{
int i;
int j;
int k;
int vscanlen;
int v;
int runlen;
jpc_fix_t *fp;
int width;
int height;
jpc_fix_t *dp;
int one;
int frowstep;
int drowstep;
int fstripestep;
int dstripestep;
jpc_fix_t *fstripestart;
jpc_fix_t *dstripestart;
jpc_fix_t *fvscanstart;
jpc_fix_t *dvscanstart;
*nmsedec = 0;
width = jas_matrix_numcols(data);
height = jas_matrix_numrows(data);
frowstep = jas_matrix_rowstep(flags);
drowstep = jas_matrix_rowstep(data);
fstripestep = frowstep << 2;
dstripestep = drowstep << 2;
one = 1 << (bitpos + JPC_NUMEXTRABITS);
fstripestart = jas_matrix_getref(flags, 1, 1);
dstripestart = jas_matrix_getref(data, 0, 0);
for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
dstripestart += dstripestep) {
fvscanstart = fstripestart;
dvscanstart = dstripestart;
vscanlen = JAS_MIN(i, 4);
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
fp = fvscanstart;
if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT |
JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG |
JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) &
(JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep,
!((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) {
dp = dvscanstart;
for (k = 0; k < vscanlen; ++k) {
v = (abs(*dp) & one) ? 1 : 0;
if (v) {
break;
}
dp += drowstep;
}
runlen = k;
if (runlen >= 4) {
jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
jpc_mqenc_putbit(mqenc, 0);
continue;
}
jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO);
jpc_mqenc_putbit(mqenc, 1);
jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
jpc_mqenc_putbit(mqenc, runlen >> 1);
jpc_mqenc_putbit(mqenc, runlen & 1);
fp = fvscanstart + frowstep * runlen;
dp = dvscanstart + drowstep * runlen;
k = vscanlen - runlen;
switch (runlen) {
case 0:
goto clnpass_partial0;
break;
case 1:
goto clnpass_partial1;
break;
case 2:
goto clnpass_partial2;
break;
case 3:
goto clnpass_partial3;
break;
}
} else {
runlen = 0;
fp = fvscanstart;
dp = dvscanstart;
k = vscanlen;
goto clnpass_full0;
}
clnpass_step(fp, frowstep, dp, bitpos, one,
orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
clnpass_step(fp, frowstep, dp, bitpos, one,
orient, nmsedec, mqenc, ;, clnpass_partial1:, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
clnpass_step(fp, frowstep, dp, bitpos, one,
orient, nmsedec, mqenc, ;, clnpass_partial2:, 0);
if (--k <= 0) {
continue;
}
fp += frowstep;
dp += drowstep;
clnpass_step(fp, frowstep, dp, bitpos, one,
orient, nmsedec, mqenc, ;, clnpass_partial3:, 0);
}
}
if (segsymflag) {
jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO);
jpc_mqenc_putbit(mqenc, 1);
jpc_mqenc_putbit(mqenc, 0);
jpc_mqenc_putbit(mqenc, 1);
jpc_mqenc_putbit(mqenc, 0);
}
if (term) {
jpc_mqenc_flush(mqenc, term - 1);
}
return jpc_mqenc_error(mqenc) ? (-1) : 0;
}
|