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
33
34
35
36
37
38
39
40
41
42
47
48
49
54
55
60
61
65
66
67
68
69
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
122
123
129
130
135
136
137
138
142
143
144
150
153
154
155
159
160
161
162
163
164
165
166
167
168
169
170
171
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
200
201
202
203
204
205
206
207
208
209
210
214
217
218
219
220
221
222
223
224
225
226
230
231
232
237
238
239
246
247
248
249
253
254
255
260
261
262
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
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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
403
404
405
408
409
410
411
420
421
422
423
424
433
434
435
438
439
440
441
452
453
454
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
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
522
523
532
533
534
535
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
569
570
571
572
573
574
575
576
577
583
584
585
586
590
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
617
618
619
620
621
622
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
662
667
676
677
681
682
683
684
685
686
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
714
715
716
717
718
719
727
728
729
730
731
732
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
763
764
765
766
767
768
776
777
778
779
780
781
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
817
818
819
820
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
862
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
895
896
897
898
899
900
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
935
936
937
938
942
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
966
967
972
973
974
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1063
1064
1065
1066
1067
1068
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
1143
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1160
1161
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
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
1278
1279
1280
1281
1282
1283
1284
1285
1286
1292
1293
1294
1295
1296
1297
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1318
1319
1320
1321
1322
1323
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
1377
1378
1386
1387
1388
1389
1390
1391
1392
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
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
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1573
1574
1575
1576
/* ... */
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
/* ... */
#if BITS_IN_JSAMPLE == 8
#define MAX_COEF_BITS 10
#else
#define MAX_COEF_BITS 14
#endif
typedef struct {
unsigned int ehufco[256];
char ehufsi[256];
...} c_derived_tbl;
/* ... */
typedef struct {
INT32 put_buffer;
int put_bits;
int last_dc_val[MAX_COMPS_IN_SCAN];
...} savable_state;
/* ... */
#ifndef NO_STRUCT_ASSIGN
#define ASSIGN_STATE(dest,src) ((dest) = (src))
#else
#if MAX_COMPS_IN_SCAN == 4
#define ASSIGN_STATE(dest,src) \
((dest).put_buffer = (src).put_buffer, \
(dest).put_bits = (src).put_bits, \
(dest).last_dc_val[0] = (src).last_dc_val[0], \
(dest).last_dc_val[1] = (src).last_dc_val[1], \
(dest).last_dc_val[2] = (src).last_dc_val[2], \
(dest).last_dc_val[3] = (src).last_dc_val[3])...
/* ... */#endif/* ... */
#endif
typedef struct {
struct jpeg_entropy_encoder pub;
savable_state saved;
unsigned int restarts_to_go;
int next_restart_num;
c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
long * dc_count_ptrs[NUM_HUFF_TBLS];
long * ac_count_ptrs[NUM_HUFF_TBLS];
boolean gather_statistics;
/* ... */
JOCTET * next_output_byte;
size_t free_in_buffer;
j_compress_ptr cinfo;
int ac_tbl_no;
unsigned int EOBRUN;
unsigned int BE;
char * bit_buffer;
...} huff_entropy_encoder;
typedef huff_entropy_encoder * huff_entropy_ptr;
/* ... */
typedef struct {
JOCTET * next_output_byte;
size_t free_in_buffer;
savable_state cur;
j_compress_ptr cinfo;
...} working_state;
/* ... */
#define MAX_CORR_BITS 1000
/* ... */
#ifdef RIGHT_SHIFT_IS_UNSIGNED
#define ISHIFT_TEMPS int ishift_temp;
#define IRIGHT_SHIFT(x,shft) \
((ishift_temp = (x)) < 0 ? \
(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
(ishift_temp >> (shft)))...
/* ... */#else
#define ISHIFT_TEMPS
#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
/* ... */#endif
/* ... */
LOCAL(void)
jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
c_derived_tbl ** pdtbl)
{
JHUFF_TBL *htbl;
c_derived_tbl *dtbl;
int p, i, l, lastp, si, maxsymbol;
char huffsize[257];
unsigned int huffcode[257];
unsigned int code;
/* ... */
if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
htbl =
isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
if (htbl == NULL)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
if (*pdtbl == NULL)
*pdtbl = (c_derived_tbl *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(c_derived_tbl));
dtbl = *pdtbl;
p = 0;
for (l = 1; l <= 16; l++) {
i = (int) htbl->bits[l];
if (i < 0 || p + i > 256)
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
while (i--)
huffsize[p++] = (char) l;
}for (l = 1; l <= 16; l++) { ... }
huffsize[p] = 0;
lastp = p;
code = 0;
si = huffsize[0];
p = 0;
while (huffsize[p]) {
while (((int) huffsize[p]) == si) {
huffcode[p++] = code;
code++;
}while (((int) huffsize[p]) == si) { ... }
/* ... */
if (((INT32) code) >= (((INT32) 1) << si))
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
code <<= 1;
si++;
}while (huffsize[p]) { ... }
/* ... */
MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
/* ... */
maxsymbol = isDC ? 15 : 255;
for (p = 0; p < lastp; p++) {
i = htbl->huffval[p];
if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
dtbl->ehufco[i] = huffcode[p];
dtbl->ehufsi[i] = huffsize[p];
}for (p = 0; p < lastp; p++) { ... }
}{ ... }
/* ... */
#define emit_byte_s(state,val,action) \
{ *(state)->next_output_byte++ = (JOCTET) (val); \
if (--(state)->free_in_buffer == 0) \
if (! dump_buffer_s(state)) \
{ action; } ...}...
#define emit_byte_e(entropy,val) \
{ *(entropy)->next_output_byte++ = (JOCTET) (val); \
if (--(entropy)->free_in_buffer == 0) \
dump_buffer_e(entropy); ...}...
LOCAL(boolean)
dump_buffer_s (working_state * state)
{
struct jpeg_destination_mgr * dest = state->cinfo->dest;
if (! (*dest->empty_output_buffer) (state->cinfo))
return FALSE;
state->next_output_byte = dest->next_output_byte;
state->free_in_buffer = dest->free_in_buffer;
return TRUE;
...}
LOCAL(void)
dump_buffer_e (huff_entropy_ptr entropy)
{
struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
if (! (*dest->empty_output_buffer) (entropy->cinfo))
ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
entropy->next_output_byte = dest->next_output_byte;
entropy->free_in_buffer = dest->free_in_buffer;
...}
/* ... */
INLINE
LOCAL(boolean)
emit_bits_s (working_state * state, unsigned int code, int size)
{
register INT32 put_buffer = (INT32) code;
register int put_bits = state->cur.put_bits;
if (size == 0)
ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
put_buffer &= (((INT32) 1)<<size) - 1;
put_bits += size;
put_buffer <<= 24 - put_bits;
put_buffer |= state->cur.put_buffer;
while (put_bits >= 8) {
int c = (int) ((put_buffer >> 16) & 0xFF);
emit_byte_s(state, c, return FALSE);
if (c == 0xFF) {
emit_byte_s(state, 0, return FALSE);
}if (c == 0xFF) { ... }
put_buffer <<= 8;
put_bits -= 8;
}while (put_bits >= 8) { ... }
state->cur.put_buffer = put_buffer;
state->cur.put_bits = put_bits;
return TRUE;
...}
INLINE
LOCAL(void)
emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
{
register INT32 put_buffer = (INT32) code;
register int put_bits = entropy->saved.put_bits;
if (size == 0)
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
if (entropy->gather_statistics)
return;
put_buffer &= (((INT32) 1)<<size) - 1;
put_bits += size;
put_buffer <<= 24 - put_bits;
put_buffer |= entropy->saved.put_buffer;
while (put_bits >= 8) {
int c = (int) ((put_buffer >> 16) & 0xFF);
emit_byte_e(entropy, c);
if (c == 0xFF) {
emit_byte_e(entropy, 0);
}if (c == 0xFF) { ... }
put_buffer <<= 8;
put_bits -= 8;
}while (put_bits >= 8) { ... }
entropy->saved.put_buffer = put_buffer;
entropy->saved.put_bits = put_bits;
...}
LOCAL(boolean)
flush_bits_s (working_state * state)
{
if (! emit_bits_s(state, 0x7F, 7))
return FALSE;
state->cur.put_buffer = 0;
state->cur.put_bits = 0;
return TRUE;
}{ ... }
LOCAL(void)
flush_bits_e (huff_entropy_ptr entropy)
{
emit_bits_e(entropy, 0x7F, 7);
entropy->saved.put_buffer = 0;
entropy->saved.put_bits = 0;
}{ ... }
/* ... */
INLINE
LOCAL(void)
emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
{
if (entropy->gather_statistics)
entropy->dc_count_ptrs[tbl_no][symbol]++;
else {
c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
}else { ... }
}{ ... }
INLINE
LOCAL(void)
emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
{
if (entropy->gather_statistics)
entropy->ac_count_ptrs[tbl_no][symbol]++;
else {
c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
}else { ... }
}{ ... }
/* ... */
LOCAL(void)
emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
unsigned int nbits)
{
if (entropy->gather_statistics)
return;
while (nbits > 0) {
emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
bufstart++;
nbits--;
}while (nbits > 0) { ... }
}{ ... }
/* ... */
LOCAL(void)
emit_eobrun (huff_entropy_ptr entropy)
{
register int temp, nbits;
if (entropy->EOBRUN > 0) {
temp = entropy->EOBRUN;
nbits = 0;
while ((temp >>= 1))
nbits++;
if (nbits > 14)
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
if (nbits)
emit_bits_e(entropy, entropy->EOBRUN, nbits);
entropy->EOBRUN = 0;
emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
entropy->BE = 0;
}if (entropy->EOBRUN > 0) { ... }
}{ ... }
/* ... */
LOCAL(boolean)
emit_restart_s (working_state * state, int restart_num)
{
int ci;
if (! flush_bits_s(state))
return FALSE;
emit_byte_s(state, 0xFF, return FALSE);
emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
state->cur.last_dc_val[ci] = 0;
return TRUE;
}{ ... }
LOCAL(void)
emit_restart_e (huff_entropy_ptr entropy, int restart_num)
{
int ci;
emit_eobrun(entropy);
if (! entropy->gather_statistics) {
flush_bits_e(entropy);
emit_byte_e(entropy, 0xFF);
emit_byte_e(entropy, JPEG_RST0 + restart_num);
}if (! entropy->gather_statistics) { ... }
if (entropy->cinfo->Ss == 0) {
for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
entropy->saved.last_dc_val[ci] = 0;
}if (entropy->cinfo->Ss == 0) { ... } else {
entropy->EOBRUN = 0;
entropy->BE = 0;
}else { ... }
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int temp, temp2;
register int nbits;
int blkn, ci;
int Al = cinfo->Al;
JBLOCKROW block;
jpeg_component_info * compptr;
ISHIFT_TEMPS
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
emit_restart_e(entropy, entropy->next_restart_num);
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
block = MCU_data[blkn];
ci = cinfo->MCU_membership[blkn];
compptr = cinfo->cur_comp_info[ci];
/* ... */
temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
temp = temp2 - entropy->saved.last_dc_val[ci];
entropy->saved.last_dc_val[ci] = temp2;
temp2 = temp;
if (temp < 0) {
temp = -temp;
temp2--;
}if (temp < 0) { ... }
nbits = 0;
while (temp) {
nbits++;
temp >>= 1;
}while (temp) { ... }
/* ... */
if (nbits > MAX_COEF_BITS+1)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
emit_dc_symbol(entropy, compptr->dc_tbl_no, nbits);
if (nbits)
emit_bits_e(entropy, (unsigned int) temp2, nbits);
}for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ... }
cinfo->dest->next_output_byte = entropy->next_output_byte;
cinfo->dest->free_in_buffer = entropy->free_in_buffer;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num++;
entropy->next_restart_num &= 7;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
return TRUE;
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int temp, temp2;
register int nbits;
register int r, k;
int Se, Al;
const int * natural_order;
JBLOCKROW block;
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
emit_restart_e(entropy, entropy->next_restart_num);
Se = cinfo->Se;
Al = cinfo->Al;
natural_order = cinfo->natural_order;
block = MCU_data[0];
r = 0;
for (k = cinfo->Ss; k <= Se; k++) {
if ((temp = (*block)[natural_order[k]]) == 0) {
r++;
continue;
}if ((temp = (*block)[natural_order[k]]) == 0) { ... }
/* ... */
if (temp < 0) {
temp = -temp;
temp >>= Al;
temp2 = ~temp;
}if (temp < 0) { ... } else {
temp >>= Al;
temp2 = temp;
}else { ... }
if (temp == 0) {
r++;
continue;
}if (temp == 0) { ... }
if (entropy->EOBRUN > 0)
emit_eobrun(entropy);
while (r > 15) {
emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
r -= 16;
}while (r > 15) { ... }
nbits = 1;
while ((temp >>= 1))
nbits++;
if (nbits > MAX_COEF_BITS)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
emit_bits_e(entropy, (unsigned int) temp2, nbits);
r = 0;
}for (k = cinfo->Ss; k <= Se; k++) { ... }
if (r > 0) {
entropy->EOBRUN++;
if (entropy->EOBRUN == 0x7FFF)
emit_eobrun(entropy);
}if (r > 0) { ... }
cinfo->dest->next_output_byte = entropy->next_output_byte;
cinfo->dest->free_in_buffer = entropy->free_in_buffer;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num++;
entropy->next_restart_num &= 7;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
return TRUE;
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int temp;
int blkn;
int Al = cinfo->Al;
JBLOCKROW block;
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
emit_restart_e(entropy, entropy->next_restart_num);
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
block = MCU_data[blkn];
temp = (*block)[0];
emit_bits_e(entropy, (unsigned int) (temp >> Al), 1);
}for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ... }
cinfo->dest->next_output_byte = entropy->next_output_byte;
cinfo->dest->free_in_buffer = entropy->free_in_buffer;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num++;
entropy->next_restart_num &= 7;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
return TRUE;
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int temp;
register int r, k;
int EOB;
char *BR_buffer;
unsigned int BR;
int Se, Al;
const int * natural_order;
JBLOCKROW block;
int absvalues[DCTSIZE2];
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
emit_restart_e(entropy, entropy->next_restart_num);
Se = cinfo->Se;
Al = cinfo->Al;
natural_order = cinfo->natural_order;
block = MCU_data[0];
/* ... */
EOB = 0;
for (k = cinfo->Ss; k <= Se; k++) {
temp = (*block)[natural_order[k]];
/* ... */
if (temp < 0)
temp = -temp;
temp >>= Al;
absvalues[k] = temp;
if (temp == 1)
EOB = k;
}for (k = cinfo->Ss; k <= Se; k++) { ... }
r = 0;
BR = 0;
BR_buffer = entropy->bit_buffer + entropy->BE;
for (k = cinfo->Ss; k <= Se; k++) {
if ((temp = absvalues[k]) == 0) {
r++;
continue;
}if ((temp = absvalues[k]) == 0) { ... }
while (r > 15 && k <= EOB) {
emit_eobrun(entropy);
emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
r -= 16;
emit_buffered_bits(entropy, BR_buffer, BR);
BR_buffer = entropy->bit_buffer;
BR = 0;
}while (r > 15 && k <= EOB) { ... }
/* ... */
if (temp > 1) {
BR_buffer[BR++] = (char) (temp & 1);
continue;
}if (temp > 1) { ... }
emit_eobrun(entropy);
emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
emit_bits_e(entropy, (unsigned int) temp, 1);
emit_buffered_bits(entropy, BR_buffer, BR);
BR_buffer = entropy->bit_buffer;
BR = 0;
r = 0;
}for (k = cinfo->Ss; k <= Se; k++) { ... }
if (r > 0 || BR > 0) {
entropy->EOBRUN++;
entropy->BE += BR;
/* ... */
if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
emit_eobrun(entropy);
}if (r > 0 || BR > 0) { ... }
cinfo->dest->next_output_byte = entropy->next_output_byte;
cinfo->dest->free_in_buffer = entropy->free_in_buffer;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num++;
entropy->next_restart_num &= 7;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
return TRUE;
}{ ... }
LOCAL(boolean)
encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
c_derived_tbl *dctbl, c_derived_tbl *actbl)
{
register int temp, temp2;
register int nbits;
register int k, r, i;
int Se = state->cinfo->lim_Se;
const int * natural_order = state->cinfo->natural_order;
temp = temp2 = block[0] - last_dc_val;
if (temp < 0) {
temp = -temp;
temp2--;
}if (temp < 0) { ... }
nbits = 0;
while (temp) {
nbits++;
temp >>= 1;
}while (temp) { ... }
/* ... */
if (nbits > MAX_COEF_BITS+1)
ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
return FALSE;
if (nbits)
if (! emit_bits_s(state, (unsigned int) temp2, nbits))
return FALSE;
r = 0;
for (k = 1; k <= Se; k++) {
if ((temp = block[natural_order[k]]) == 0) {
r++;
}if ((temp = block[natural_order[k]]) == 0) { ... } else {
while (r > 15) {
if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
return FALSE;
r -= 16;
}while (r > 15) { ... }
temp2 = temp;
if (temp < 0) {
temp = -temp;
temp2--;
}if (temp < 0) { ... }
nbits = 1;
while ((temp >>= 1))
nbits++;
if (nbits > MAX_COEF_BITS)
ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
i = (r << 4) + nbits;
if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i]))
return FALSE;
if (! emit_bits_s(state, (unsigned int) temp2, nbits))
return FALSE;
r = 0;
}else { ... }
}for (k = 1; k <= Se; k++) { ... }
if (r > 0)
if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
return FALSE;
return TRUE;
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
working_state state;
int blkn, ci;
jpeg_component_info * compptr;
state.next_output_byte = cinfo->dest->next_output_byte;
state.free_in_buffer = cinfo->dest->free_in_buffer;
ASSIGN_STATE(state.cur, entropy->saved);
state.cinfo = cinfo;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0)
if (! emit_restart_s(&state, entropy->next_restart_num))
return FALSE;
}if (cinfo->restart_interval) { ... }
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
ci = cinfo->MCU_membership[blkn];
compptr = cinfo->cur_comp_info[ci];
if (! encode_one_block(&state,
MCU_data[blkn][0], state.cur.last_dc_val[ci],
entropy->dc_derived_tbls[compptr->dc_tbl_no],
entropy->ac_derived_tbls[compptr->ac_tbl_no]))
return FALSE;
state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
}for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ... }
cinfo->dest->next_output_byte = state.next_output_byte;
cinfo->dest->free_in_buffer = state.free_in_buffer;
ASSIGN_STATE(entropy->saved, state.cur);
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num++;
entropy->next_restart_num &= 7;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
return TRUE;
}{ ... }
/* ... */
METHODDEF(void)
finish_pass_huff (j_compress_ptr cinfo)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
working_state state;
if (cinfo->progressive_mode) {
entropy->next_output_byte = cinfo->dest->next_output_byte;
entropy->free_in_buffer = cinfo->dest->free_in_buffer;
emit_eobrun(entropy);
flush_bits_e(entropy);
cinfo->dest->next_output_byte = entropy->next_output_byte;
cinfo->dest->free_in_buffer = entropy->free_in_buffer;
}if (cinfo->progressive_mode) { ... } else {
state.next_output_byte = cinfo->dest->next_output_byte;
state.free_in_buffer = cinfo->dest->free_in_buffer;
ASSIGN_STATE(state.cur, entropy->saved);
state.cinfo = cinfo;
if (! flush_bits_s(&state))
ERREXIT(cinfo, JERR_CANT_SUSPEND);
cinfo->dest->next_output_byte = state.next_output_byte;
cinfo->dest->free_in_buffer = state.free_in_buffer;
ASSIGN_STATE(entropy->saved, state.cur);
}else { ... }
}{ ... }
/* ... */
LOCAL(void)
htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
long dc_counts[], long ac_counts[])
{
register int temp;
register int nbits;
register int k, r;
int Se = cinfo->lim_Se;
const int * natural_order = cinfo->natural_order;
temp = block[0] - last_dc_val;
if (temp < 0)
temp = -temp;
nbits = 0;
while (temp) {
nbits++;
temp >>= 1;
}while (temp) { ... }
/* ... */
if (nbits > MAX_COEF_BITS+1)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
dc_counts[nbits]++;
r = 0;
for (k = 1; k <= Se; k++) {
if ((temp = block[natural_order[k]]) == 0) {
r++;
}if ((temp = block[natural_order[k]]) == 0) { ... } else {
while (r > 15) {
ac_counts[0xF0]++;
r -= 16;
}while (r > 15) { ... }
if (temp < 0)
temp = -temp;
nbits = 1;
while ((temp >>= 1))
nbits++;
if (nbits > MAX_COEF_BITS)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
ac_counts[(r << 4) + nbits]++;
r = 0;
}else { ... }
}for (k = 1; k <= Se; k++) { ... }
if (r > 0)
ac_counts[0]++;
}{ ... }
/* ... */
METHODDEF(boolean)
encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int blkn, ci;
jpeg_component_info * compptr;
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
for (ci = 0; ci < cinfo->comps_in_scan; ci++)
entropy->saved.last_dc_val[ci] = 0;
entropy->restarts_to_go = cinfo->restart_interval;
}if (entropy->restarts_to_go == 0) { ... }
entropy->restarts_to_go--;
}if (cinfo->restart_interval) { ... }
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
ci = cinfo->MCU_membership[blkn];
compptr = cinfo->cur_comp_info[ci];
htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
entropy->dc_count_ptrs[compptr->dc_tbl_no],
entropy->ac_count_ptrs[compptr->ac_tbl_no]);
entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
}for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ... }
return TRUE;
}{ ... }
/* ... */
LOCAL(void)
jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
{
#define MAX_CLEN 32
UINT8 bits[MAX_CLEN+1];
int codesize[257];
int others[257];
int c1, c2;
int p, i, j;
long v;
MEMZERO(bits, SIZEOF(bits));
MEMZERO(codesize, SIZEOF(codesize));
for (i = 0; i < 257; i++)
others[i] = -1;
freq[256] = 1;
/* ... */
for (;;) {
c1 = -1;
v = 1000000000L;
for (i = 0; i <= 256; i++) {
if (freq[i] && freq[i] <= v) {
v = freq[i];
c1 = i;
}if (freq[i] && freq[i] <= v) { ... }
}for (i = 0; i <= 256; i++) { ... }
c2 = -1;
v = 1000000000L;
for (i = 0; i <= 256; i++) {
if (freq[i] && freq[i] <= v && i != c1) {
v = freq[i];
c2 = i;
}if (freq[i] && freq[i] <= v && i != c1) { ... }
}for (i = 0; i <= 256; i++) { ... }
if (c2 < 0)
break;
freq[c1] += freq[c2];
freq[c2] = 0;
codesize[c1]++;
while (others[c1] >= 0) {
c1 = others[c1];
codesize[c1]++;
}while (others[c1] >= 0) { ... }
others[c1] = c2;
codesize[c2]++;
while (others[c2] >= 0) {
c2 = others[c2];
codesize[c2]++;
}while (others[c2] >= 0) { ... }
}for (;;) { ... }
for (i = 0; i <= 256; i++) {
if (codesize[i]) {
if (codesize[i] > MAX_CLEN)
ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
bits[codesize[i]]++;
}if (codesize[i]) { ... }
}for (i = 0; i <= 256; i++) { ... }
/* ... */
for (i = MAX_CLEN; i > 16; i--) {
while (bits[i] > 0) {
j = i - 2;
while (bits[j] == 0)
j--;
bits[i] -= 2;
bits[i-1]++;
bits[j+1] += 2;
bits[j]--;
}while (bits[i] > 0) { ... }
}for (i = MAX_CLEN; i > 16; i--) { ... }
while (bits[i] == 0)
i--;
bits[i]--;
MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
/* ... */
p = 0;
for (i = 1; i <= MAX_CLEN; i++) {
for (j = 0; j <= 255; j++) {
if (codesize[j] == i) {
htbl->huffval[p] = (UINT8) j;
p++;
}if (codesize[j] == i) { ... }
}for (j = 0; j <= 255; j++) { ... }
}for (i = 1; i <= MAX_CLEN; i++) { ... }
htbl->sent_table = FALSE;
}{ ... }
/* ... */
METHODDEF(void)
finish_pass_gather (j_compress_ptr cinfo)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int ci, tbl;
jpeg_component_info * compptr;
JHUFF_TBL **htblptr;
boolean did_dc[NUM_HUFF_TBLS];
boolean did_ac[NUM_HUFF_TBLS];
/* ... */
if (cinfo->progressive_mode)
emit_eobrun(entropy);
MEMZERO(did_dc, SIZEOF(did_dc));
MEMZERO(did_ac, SIZEOF(did_ac));
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
compptr = cinfo->cur_comp_info[ci];
if (cinfo->Ss == 0 && cinfo->Ah == 0) {
tbl = compptr->dc_tbl_no;
if (! did_dc[tbl]) {
htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
if (*htblptr == NULL)
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
did_dc[tbl] = TRUE;
}if (! did_dc[tbl]) { ... }
}if (cinfo->Ss == 0 && cinfo->Ah == 0) { ... }
if (cinfo->Se) {
tbl = compptr->ac_tbl_no;
if (! did_ac[tbl]) {
htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
if (*htblptr == NULL)
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
did_ac[tbl] = TRUE;
}if (! did_ac[tbl]) { ... }
}if (cinfo->Se) { ... }
}for (ci = 0; ci < cinfo->comps_in_scan; ci++) { ... }
}{ ... }
/* ... */
METHODDEF(void)
start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int ci, tbl;
jpeg_component_info * compptr;
if (gather_statistics)
entropy->pub.finish_pass = finish_pass_gather;
else
entropy->pub.finish_pass = finish_pass_huff;
if (cinfo->progressive_mode) {
entropy->cinfo = cinfo;
entropy->gather_statistics = gather_statistics;
if (cinfo->Ah == 0) {
if (cinfo->Ss == 0)
entropy->pub.encode_mcu = encode_mcu_DC_first;
else
entropy->pub.encode_mcu = encode_mcu_AC_first;
}if (cinfo->Ah == 0) { ... } else {
if (cinfo->Ss == 0)
entropy->pub.encode_mcu = encode_mcu_DC_refine;
else {
entropy->pub.encode_mcu = encode_mcu_AC_refine;
if (entropy->bit_buffer == NULL)
entropy->bit_buffer = (char *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
MAX_CORR_BITS * SIZEOF(char));
}else { ... }
}else { ... }
entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
entropy->EOBRUN = 0;
entropy->BE = 0;
}if (cinfo->progressive_mode) { ... } else {
if (gather_statistics)
entropy->pub.encode_mcu = encode_mcu_gather;
else
entropy->pub.encode_mcu = encode_mcu_huff;
}else { ... }
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
compptr = cinfo->cur_comp_info[ci];
if (cinfo->Ss == 0 && cinfo->Ah == 0) {
tbl = compptr->dc_tbl_no;
if (gather_statistics) {
if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
if (entropy->dc_count_ptrs[tbl] == NULL)
entropy->dc_count_ptrs[tbl] = (long *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
257 * SIZEOF(long));
MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
}if (gather_statistics) { ... } else {
jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
& entropy->dc_derived_tbls[tbl]);
}else { ... }
entropy->saved.last_dc_val[ci] = 0;
}if (cinfo->Ss == 0 && cinfo->Ah == 0) { ... }
if (cinfo->Se) {
tbl = compptr->ac_tbl_no;
if (gather_statistics) {
if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
if (entropy->ac_count_ptrs[tbl] == NULL)
entropy->ac_count_ptrs[tbl] = (long *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
257 * SIZEOF(long));
MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
}if (gather_statistics) { ... } else {
jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
& entropy->ac_derived_tbls[tbl]);
}else { ... }
}if (cinfo->Se) { ... }
}for (ci = 0; ci < cinfo->comps_in_scan; ci++) { ... }
entropy->saved.put_buffer = 0;
entropy->saved.put_bits = 0;
entropy->restarts_to_go = cinfo->restart_interval;
entropy->next_restart_num = 0;
}{ ... }
/* ... */
GLOBAL(void)
jinit_huff_encoder (j_compress_ptr cinfo)
{
huff_entropy_ptr entropy;
int i;
entropy = (huff_entropy_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(huff_entropy_encoder));
cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
entropy->pub.start_pass = start_pass_huff;
for (i = 0; i < NUM_HUFF_TBLS; i++) {
entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
}for (i = 0; i < NUM_HUFF_TBLS; i++) { ... }
if (cinfo->progressive_mode)
entropy->bit_buffer = NULL;
}{ ... }