-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjpeg_encoder.c
More file actions
1267 lines (1113 loc) · 34.1 KB
/
jpeg_encoder.c
File metadata and controls
1267 lines (1113 loc) · 34.1 KB
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
/**
* Minimalistic JPEG Encoder
* baseline-DCT profile
* fixed 4:2:0 chroma subsampling
* dynamic luma/chroma quantization
* only dimensions which are multiples of 16 are supported
* dynamic huffman table creation
*
* Schier Michael, April 2011
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <sys/time.h>
// ====================================================================================================================
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define CLIP(n, min, max) MIN((MAX((n),(min))), (max))
// ====================================================================================================================
/*
* ISO/IEC 10918-1/ K.2
*/
typedef struct __huff_code
{
int sym_freq[257]; // frequency of occurrence of symbol i
int code_len[257]; // code length of symbol i
int next[257]; // index to next symbol in chain of all symbols in current branch of code tree
int code_len_freq[32]; // the frequencies of huff-symbols of length i
int sym_sorted[256]; // the symbols to be encoded
int sym_code_len[256]; // the huffman code length of symbol i
int sym_code[256]; // the huffman code of the symbol i
} huff_code;
typedef struct __jpeg_data
{
// image dimensions
int width;
int height;
int num_pixel; // = width*height
// RGB data of the input image
int* red;
int* green;
int* blue;
// YCbCr for the colorspace-conversion
int* y;
int* cb;
int* cr;
// sub-sampled chroma
int* cb_sub;
int* cr_sub;
// dct coefficients: 64 coefficients of the first block, then the second block...
double* dct_y;
double* dct_cb;
double* dct_cr;
// quantized dct coefficients
int* dct_y_quant;
int* dct_cb_quant;
int* dct_cr_quant;
// huffman entropy coding parameters
huff_code luma_dc;
huff_code luma_ac;
huff_code chroma_dc;
huff_code chroma_ac;
} jpeg_data;
// ====================================================================================================================
/*
* stop-watch used for time measurements
*/
double timer()
{
static double last_call = 0;
static struct timeval arg;
gettimeofday(&arg, NULL);
double ret = arg.tv_sec*1000.0 + arg.tv_usec/1000.0 - last_call;
last_call = arg.tv_sec*1000.0 + arg.tv_usec/1000.0;
return ret;
}
// ====================================================================================================================
/*
* print the DCT coefficients of a color channel
*/
void print_dct_coeffs(int num_pixel, int dct[])
{
int i;
for (i=0; i<num_pixel; i++)
{
int j = i;
while(dct[j++] == 0 && j <= num_pixel)
if (j%64 == 0)
i=j;
if(i == num_pixel)
break;
if(i%64 == 0)
printf("\nBlock %2d: ", i/64);
printf("%3d ", dct[i]);
}
printf("\n\n");
}
/*
* print the DCT coefficients of a color channel
*/
void print_dct_coeffs_double(int num_pixel, double dct[])
{
int i;
for (i=0; i<num_pixel; i++)
{
int j = i;
while(dct[j++] == 0 && j <= num_pixel)
if (j%64 == 0)
i=j;
if(i == num_pixel)
break;
if(i%64 == 0)
printf("\nBlock %2d: ", i/64);
printf("%3.0f ", dct[i]);
}
printf("\n\n");
}
/*
* return the binary string representation of an unsigned 16-bit integer
*/
char *binary_string(int x, int digits)
{
// static memory, use different parts of it for parallel calls (e.g. multiple arguments in printf)
static char b[1024];
static int call = 0;
call = (call+1)%32;
char* c = b + call*32 - 1;
*c = '\0';
if (x<0)
x = 65536 + x;
while (digits-->0)
{
*(--c) = (x%2) ? '1' : '0';
x/=2;
}
return c;
}
// ====================================================================================================================
/*
* allocates the space needed for the globally used struct jpeg_data
*/
int alloc_jpeg_data(jpeg_data* data)
{
data->red = malloc(data->num_pixel*sizeof(int));
data->green = malloc(data->num_pixel*sizeof(int));
data->blue = malloc(data->num_pixel*sizeof(int));
data->y = malloc(data->num_pixel*sizeof(int));
data->cb = malloc(data->num_pixel*sizeof(int));
data->cr = malloc(data->num_pixel*sizeof(int));
data->cb_sub = malloc(data->num_pixel/4*sizeof(int));
data->cr_sub = malloc(data->num_pixel/4*sizeof(int));
data->dct_y = malloc(data->num_pixel*sizeof(double));
data->dct_cb = malloc(data->num_pixel/4*sizeof(double));
data->dct_cr = malloc(data->num_pixel/4*sizeof(double));
data->dct_y_quant = malloc(data->num_pixel*sizeof(int));
data->dct_cb_quant = malloc(data->num_pixel/4*sizeof(int));
data->dct_cr_quant = malloc(data->num_pixel/4*sizeof(int));
if (data->red == NULL || data->green == NULL || data->blue == NULL || data->y == NULL || data->cb == NULL || data->cr == NULL || data->cb_sub == NULL || data->cr_sub == NULL
|| data->dct_y == NULL || data->dct_cb == NULL || data->dct_cr == NULL || data->dct_y_quant == NULL || data->dct_cb_quant == NULL || data->dct_cr_quant == NULL)
{
fprintf(stderr, "Could not allocate enough memory for image processing\n");
return -1;
}
return 0;
}
// ====================================================================================================================
/*
* for illustration purposes, saves all color channels in a separate ppm file
*/
void save_channels(jpeg_data* data)
{
FILE* f = fopen("channels.ppm", "w");
fprintf(f, "P6\n%d %d\n255\n", data->width*3, data->height*3);
int r,c;
for (r=0; r<data->height*3; r++)
for (c=0; c<data->width*3; c++)
{
int i = (r%data->height)*data->width + (c%data->width);
int j = (r%data->height)/2*data->width/2 + (c%data->width)/2;
if (r<data->height)
{
if (c<data->width)
{
fputc(data->red[i], f);
fputc(0, f);
fputc(0, f);
}
else if (c<2*data->width)
{
fputc(0, f);
fputc(data->green[i], f);
fputc(0, f);
}
else
{
fputc(0, f);
fputc(0, f);
fputc(data->blue[i], f);
}
}
else if (r<data->height*2)
{
if (c<data->width)
{
fputc(data->y[i], f);
fputc(data->y[i], f);
fputc(data->y[i], f);
}
else if (c<2*data->width)
{
fputc(data->cb[i], f);
fputc(data->cb[i], f);
fputc(data->cb[i], f);
}
else
{
fputc(data->cr[i], f);
fputc(data->cr[i], f);
fputc(data->cr[i], f);
}
}
else
{
if (c<data->width)
{
fputc(data->red[i], f);
fputc(data->green[i], f);
fputc(data->blue[i], f);
}
else if (c<2*data->width)
{
fputc(data->cb_sub[j], f);
fputc(data->cb_sub[j], f);
fputc(data->cb_sub[j], f);
}
else
{
fputc(data->cr_sub[j], f);
fputc(data->cr_sub[j], f);
fputc(data->cr_sub[j], f);
}
}
}
fclose(f);
}
// ====================================================================================================================
/*
* Typical PPM File:
* P6\n
* # comment (optional)\n
* width height\n
* bit_depth_per_channel\n
* binary_data
*/
int read_ppm(FILE* f, jpeg_data* data)
{
if( fgetc(f) != 'P' || fgetc(f) != '6' )
{
fprintf(stderr, "Could not find magic number for this PPM!\n");
return -1;
}
if (fgetc(f) != '\n' )
goto X;
char buf[1024];
while(1)
{
char* p = buf;
while ((*p = fgetc(f)) != '\n')
p++;
*p = '\0';
if (buf[0] != '#')
break;
#ifdef DEBUG
else
printf("PPM Comment: %s\n", buf+1);
#endif
}
if (sscanf(buf, "%d %d\n", &data->width, &data->height) != 2)
goto X;
#ifdef DEBUG
printf("Dimension: %dx%d\n", data->width, data->height);
#endif
data->num_pixel = data->width * data->height;
if (data->width%16 != 0 || data->height%16 != 0)
{
fprintf(stderr, "Only pictures with dimensions which are multiples of 16 are supported!\n");
return -1;
}
int depth;
if (fscanf(f, "%d\n", &depth) != 1)
goto X;
if (depth != 255)
{
printf("For simplicity, only a bit-depth of 256 is supported!\n");
return -1;
}
long len = ftell(f);
fseek(f, 0L, SEEK_END);
len = ftell(f) - len;
fseek(f, -len, SEEK_END);
if (len != 3*data->num_pixel) // 3 color channels
goto X;
if (alloc_jpeg_data(data))
return -1;
int i;
for (i=0; i<data->num_pixel; i++)
{
data->red[i] = fgetc(f);
data->green[i] = fgetc(f);
data->blue[i] = fgetc(f);
}
return 0;
X: fprintf(stderr, "Could not parse the PPM file properly\n");
return -1;
}
// ====================================================================================================================
/*
* converts the rgb data to ycbcr data
*/
int rgb_to_ycbcr(jpeg_data* data)
{
int i;
for (i=0; i<data->num_pixel; i++)
{
data->y[i] = 0.299 * data->red[i] + 0.587 * data->green[i] + 0.114 * data->blue[i];
data->cb[i] = 128 - 0.168736 * data->red[i] - 0.331264 * data->green[i] + 0.5 * data->blue[i];
data->cr[i] = 128 + 0.5 * data->red[i] - 0.418688 * data->green[i] - 0.081312 * data->blue[i];
assert( 0<=data->y[i] && data->y[i]<=255);
assert( 0<=data->cb[i] && data->cb[i]<=255 );
assert( 0<=data->cr[i] && data->cr[i]<=255 );
}
return 0;
}
// ====================================================================================================================
/*
* subsamples the cb and cr channels (to c?_sub)
*/
void subsample_chroma(jpeg_data* data)
{
int h,w;
for (h=0; h<data->height/2; h++)
for (w=0; w<data->width/2; w++)
{
int i = 2*h*data->width + 2*w;
data->cb_sub[h*data->width/2+w] = ( data->cb[i] + data->cb[i+1] + data->cb[i+data->width] + data->cb[i+data->width+1] ) / 4;
data->cr_sub[h*data->width/2+w] = ( data->cr[i] + data->cr[i+1] + data->cr[i+data->width] + data->cr[i+data->width+1] ) / 4;
assert( 0<=data->cb_sub[h*data->width/2+w] && data->cb_sub[h*data->width/2+w]<=255 );
assert( 0<=data->cr_sub[h*data->width/2+w] && data->cr_sub[h*data->width/2+w]<=255 );
}
}
// ====================================================================================================================
/*
* builds a lookup table to speed up the discrete cosine transform
*/
double cos_lookup[8][8];
void init_dct_lookup()
{
int i, j;
for (i=0; i<8; i++)
for (j=0; j<8; j++)
{
cos_lookup[i][j] = cos( (2*i+1)*j*M_PI/16 );
assert( -1<=cos_lookup[i][j] && cos_lookup[i][j]<=1 );
}
}
/*
* the discrete cosine transform per 8x8 block - outputs floating point values
* optimized by using lookup tables and splitting the terms
*/
inline void dct_block(int gap, int in[], double out[])
{
int x_f, y_f; // frequency domain coordinates
int x_t, y_t; // time domain coordinates
double inner_lookup[8][8];
for (x_t=0; x_t<8; x_t++)
for (y_f=0; y_f<8; y_f++)
{
inner_lookup[x_t][y_f] = 0;
for (y_t=0; y_t<8; y_t++)
inner_lookup[x_t][y_f] += ( in[y_t*gap+x_t] - 128 ) * cos_lookup[y_t][y_f];
}
// freq(x_f,y_f) = ...
double freq;
for (y_f=0; y_f<8; y_f++)
for (x_f=0; x_f<8; x_f++)
{
freq = 0;
for(x_t=0; x_t<8; x_t++)
freq += inner_lookup[x_t][y_f] * cos_lookup[x_t][x_f];
if (x_f == 0)
freq *= M_SQRT1_2;
if (y_f == 0)
freq *= M_SQRT1_2;
freq /= 4;
out[y_f*8+x_f] = freq;
}
}
/*
* perform the discrete cosine transform
*/
void dct(int blocks_horiz, int blocks_vert, int in[], double out[])
{
int h,v;
for (v=0; v<blocks_vert; v++)
for (h=0; h<blocks_horiz; h++)
dct_block(8*blocks_horiz, in + v*blocks_horiz*64 + h*8, out + (v*blocks_horiz+h)*64);
}
// ====================================================================================================================
/*
* Luma quantization matrix
* taken from CCITT Rec. T.81
*/
int luma_quantizer[] = {
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99
};
/*
* Chroma quantization matrix
*/
int chroma_quantizer[] = {
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
void set_quality(int quantizer[], int quality)
{
int i;
for (i=0; i<64; i++)
quantizer[i] = CLIP((100-quality)/50.0 * quantizer[i], 1, 255);
}
/*
* quantize the DCT coefficients
* and reorder them according to the zig-zag scan mode
*/
void quantize(int num_pixel, double in[], int out[], int quantizer[])
{
int i;
for (i=0; i<num_pixel; i++)
{
out[i] = in[i] / quantizer[i%64];
out[i] = CLIP(out[i], -2048, 2047);
}
}
// ====================================================================================================================
/*
* Reorder elements in zig-zag
* new[i] = old[scan_order[i]]
*/
int scan_order[] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63};
/*
* Reorders the dct coefficients in zig-zag order using the scan_order matrix
*/
void zigzag(int num_pixel, int dct[])
{
int i,j;
int tmp[64];
for (i=0; i<num_pixel; i+=64)
{
for (j=0; j<64; j++)
tmp[j] = dct[i+j];
for (j=0; j<64; j++)
dct[i+j] = tmp[scan_order[j]];
}
}
// ====================================================================================================================
/*
* turn the absolute values of DC coefficients to differential values
* diff(i) = dc(i) - dc(i-1)
*/
void diff_dc(int num_pixel, int dct_quant[])
{
int i;
int last = dct_quant[0];
for (i=64; i<num_pixel; i+=64)
{
dct_quant[i] -= last;
last += dct_quant[i];
}
}
// ====================================================================================================================
/*
* construct the huffman table from the derived frequency distribution
*/
void init_huff_table(huff_code* hc)
{
int i;
for (i=0; i<257; i++)
{
hc->code_len[i] = 0;
hc->next[i] = -1;
}
// derive the code length for each symbol
while (1)
{
int v1 = -1;
int v2 = -1;
int i;
// find least value of freq(v1) and next least value of freq(v2)
for (i=0; i<257; i++)
{
if (hc->sym_freq[i] == 0)
continue;
if ( v1 == -1 || hc->sym_freq[i] <= hc->sym_freq[v1] )
{
v2 = v1;
v1 = i;
}
else if (v2 == -1 || hc->sym_freq[i] <= hc->sym_freq[v2])
v2 = i;
}
if (v2 == -1)
break;
hc->sym_freq[v1] += hc->sym_freq[v2];
hc->sym_freq[v2] = 0;
while (1)
{
hc->code_len[v1]++;
if (hc->next[v1] == -1)
break;
v1 = hc->next[v1];
}
hc->next[v1] = v2;
while (1)
{
hc->code_len[v2]++;
if (hc->next[v2] == -1)
break;
v2 = hc->next[v2];
}
}
for (i=0; i<32; i++)
hc->code_len_freq[i] = 0;
// derive code length frequencies
for (i=0; i<257; i++)
if (hc->code_len[i] != 0)
hc->code_len_freq[hc->code_len[i]]++;
// limit the huffman code length to 16 bits
i=31;
while (1)
{
if (hc->code_len_freq[i] > 0) // if the code is too long ...
{
int j = i-1;
while (hc->code_len_freq[--j] <= 0); // ... we search for an upper layer containing leaves
hc->code_len_freq[i] -= 2; // we remove the two leaves from the lowest layer
hc->code_len_freq[i-1]++; // and put one of them one position higher
hc->code_len_freq[j+1] += 2; // the other one goes at a new branch ...
hc->code_len_freq[j]--; // ... together with the leave node which was there before
continue;
}
i--;
if (i!=16)
continue;
while (hc->code_len_freq[i] == 0)
i--;
hc->code_len_freq[i]--; // remove one leave from the lowest layer (the bottom symbol '111...111')
break;
}
// sort the input symbols according to their code size
for (i=0; i<256; i++)
hc->sym_sorted[i] = -1;
int j;
int k = 0;
for (i=1; i<32; i++)
for (j=0; j<256; j++)
if (hc->code_len[j] == i)
hc->sym_sorted[k++] = j;
// determine the size of the huffman code symbols - this may differ from code_len because
// of the 16 bit limit
for (i=0; i<256; i++)
hc->sym_code_len[i] = 0;
k=0;
for (i=1; i<=16; i++)
for (j=1; j<=hc->code_len_freq[i]; j++)
hc->sym_code_len[hc->sym_sorted[k++]] = i;
hc->sym_code_len[hc->sym_sorted[k]] = 0;
// generate the codes for the symbols
for (i=0; i<256; i++)
hc->sym_code[i] = -1;
k = 0;
int code = 0;
int si = hc->sym_code_len[hc->sym_sorted[0]];
while (1)
{
do
{
hc->sym_code[hc->sym_sorted[k]] = code;
k++;
code++;
} while (hc->sym_code_len[hc->sym_sorted[k]] == si);
if (hc->sym_code_len[hc->sym_sorted[k]] == 0)
break;
do
{
code <<= 1;
si++;
} while (hc->sym_code_len[hc->sym_sorted[k]] != si);
}
}
/*
* map the value to its class
* Class-id DC/AC-coeff delta
* -----------------------------------------
* 0 0
* 1 -1,1
* 2 -3,-2,2,3
* 3 -7,-6,-5,-4,4,5,6,7
* 4 -15,...,-8,8,...,15
* 5 -31,...,-16,16,...31
* 6 -63,...,-32,32,...63
* 7 -127,...,-64,64,...,127
* 8 -255,...,-128,128,...,255
* 9 -511,...,-256,256,...,511
* 10 -1023,...,-512,512,...,1023
* 11 -2047,...,-1024,1024,...,2047
*/
int huff_class(int value)
{
value = value<0 ? -value : value;
int class = 0;
while (value>0)
{
value = value>>1;
class++;
}
return class;
}
/*
* determine frequencies (DC) - mapped to classes
*/
void calc_dc_freq(int num_pixel, int dct_quant[], int freq[])
{
int i;
for (i=0; i<num_pixel; i+=64)
freq[huff_class(dct_quant[i])]++;
}
/*
* determine frequencies (num_zeros + AC) - bit0-3=num_preceding_zeros, bit4-7=class-id
*/
void calc_ac_freq(int num_pixel, int dct_quant[], int freq[])
{
int i;
int num_zeros = 0;
int last_nonzero;
for (i=0; i<num_pixel; i++)
{
if (i%64 == 0)
{
for (last_nonzero = i+63; last_nonzero>i; last_nonzero--)
if (dct_quant[last_nonzero] != 0)
break;
continue;
}
if (i == last_nonzero + 1)
{
freq[0x00]++; // EOB byte
// jump to the next block
i = (i/64+1)*64-1;
continue;
}
if (dct_quant[i] == 0)
{
num_zeros++;
if (num_zeros == 16)
{
freq[0xF0]++; // ZRL byte
num_zeros = 0;
}
continue;
}
freq[ ((num_zeros<<4)&0xF0) | (huff_class(dct_quant[i])&0x0F) ]++;
num_zeros = 0;
}
}
/*
* construct 4 huffman tables for DC/(num_zeros+AC) luma/chroma coefficients
*/
void init_huffman(jpeg_data* data)
{
int i;
huff_code* luma_dc = &data->luma_dc;
huff_code* luma_ac = &data->luma_ac;
huff_code* chroma_dc = &data->chroma_dc;
huff_code* chroma_ac = &data->chroma_ac;
// initialize
for (i=0; i<257; i++)
luma_dc->sym_freq[i] = luma_ac->sym_freq[i] = chroma_dc->sym_freq[i] = chroma_ac->sym_freq[i] = 0;
// reserve one code point
luma_dc->sym_freq[256] = luma_ac->sym_freq[256] = chroma_dc->sym_freq[256] = chroma_ac->sym_freq[256] = 1;
// calculate frequencies as basis for the huffman table construction
calc_dc_freq(data->num_pixel, data->dct_y_quant, luma_dc->sym_freq);
calc_ac_freq(data->num_pixel, data->dct_y_quant, luma_ac->sym_freq);
calc_dc_freq(data->num_pixel/4, data->dct_cb_quant, chroma_dc->sym_freq);
calc_dc_freq(data->num_pixel/4, data->dct_cr_quant, chroma_dc->sym_freq);
calc_ac_freq(data->num_pixel/4, data->dct_cb_quant, chroma_ac->sym_freq);
calc_ac_freq(data->num_pixel/4, data->dct_cr_quant, chroma_ac->sym_freq);
#ifdef DEBUG
printf("********** Symbol frequencies ( i, sym_freq[i] )\n");
printf(" Code Luma-DC Luma-AC Chroma-DC Chroma-AC\n");
for (i=0; i<256; i++)
if (luma_dc->sym_freq[i] || luma_ac->sym_freq[i] || chroma_dc->sym_freq[i] || chroma_ac->sym_freq[i])
printf(" %2x %10.d %10.d %10.d %10.d\n", i, luma_dc->sym_freq[i], luma_ac->sym_freq[i], chroma_dc->sym_freq[i], chroma_ac->sym_freq[i]);
#endif
timer();
printf("Initializing the luma DC Huffman tables ");
init_huff_table(luma_dc);
printf("%10.3f ms\n", timer());
printf("Initializing the luma AC Huffman tables ");
init_huff_table(luma_ac);
printf("%10.3f ms\n", timer());
printf("Initializing the chroma DC Huffman tables");
init_huff_table(chroma_dc);
printf("%10.3f ms\n", timer());
printf("Initializing the chroma AC Huffman tables");
init_huff_table(chroma_ac);
printf("%10.3f ms\n", timer());
#ifdef INFO
printf("\n********** Encoded symbol lengths ( i, code_len[i] )\n");
printf(" Code Luma-DC Luma-AC Chroma-DC Chroma-AC\n");
for (i=0; i<256; i++)
if (luma_dc->code_len[i] || luma_ac->code_len[i] || chroma_dc->code_len[i] || chroma_ac->code_len[i])
printf(" %2x %10.d %10.d %10.d %10.d\n", i, luma_dc->code_len[i], luma_ac->code_len[i], chroma_dc->code_len[i], chroma_ac->code_len[i]);
printf("\n********** Encoded symbol length frequencies ( i, code_len_freq[i] )\n");
printf("Length Luma-DC Luma-AC Chroma-DC Chroma-AC\n");
for (i=0; i<32; i++)
if (luma_dc->code_len_freq[i] || luma_ac->code_len_freq[i] || chroma_dc->code_len_freq[i] || chroma_ac->code_len_freq[i])
printf("%6d %10.d %10.d %10.d %10.d\n", i, luma_dc->code_len_freq[i], luma_ac->code_len_freq[i], chroma_dc->code_len_freq[i], chroma_ac->code_len_freq[i]);
printf("\n********** Symbols ordered by code length ( i, sym_sorted[i], code_len[sym_sorted[i]] )\n");
printf(" Rank Luma-DC Luma-AC Chroma-DC Chroma-AC\n");
for (i=0; i<256; i++)
if (luma_dc->sym_sorted[i]>=0 || luma_ac->sym_sorted[i]>=0 || chroma_dc->sym_sorted[i]>=0 || chroma_ac->sym_sorted[i]>=0)
printf("%6d %8x %2.d %8x %2.d %8x %2.d %8x %2.d\n", i,
luma_dc->sym_sorted[i], luma_dc->sym_sorted[i] ==-1 ? 0 : luma_dc->code_len[luma_dc->sym_sorted[i]],
luma_ac->sym_sorted[i], luma_ac->sym_sorted[i] ==-1 ? 0 : luma_ac->code_len[luma_ac->sym_sorted[i]],
chroma_dc->sym_sorted[i], chroma_dc->sym_sorted[i]==-1 ? 0 : chroma_dc->code_len[chroma_dc->sym_sorted[i]],
chroma_ac->sym_sorted[i], chroma_ac->sym_sorted[i]==-1 ? 0 : chroma_ac->code_len[chroma_ac->sym_sorted[i]]);
printf("\n********** Huffman code words sorted (i, sym_code[i], sym_code_len[i])\n");
printf(" Code Luma-DC Luma-AC Chroma-DC Chroma-AC\n");
for (i=0; i<256; i++)
if (luma_dc->sym_code_len[i] || luma_ac->sym_code_len[i] || chroma_dc->sym_code_len[i] || chroma_ac->sym_code_len[i])
printf(" %2x %16s %2.d %16s %2.d %16s %2.d %16s %2.d\n", i,
binary_string(luma_dc->sym_code[i], luma_dc->sym_code_len[i]), luma_dc->sym_code_len[i],
binary_string(luma_ac->sym_code[i], luma_ac->sym_code_len[i]), luma_ac->sym_code_len[i],
binary_string(chroma_dc->sym_code[i], chroma_dc->sym_code_len[i]), chroma_dc->sym_code_len[i],
binary_string(chroma_ac->sym_code[i], chroma_ac->sym_code_len[i]), chroma_ac->sym_code_len[i]);
#endif
}
// ====================================================================================================================
unsigned char byte_buffer;
int bits_written;
void write_byte(FILE* f, int code_word, int start, int end)
{
if (start == end)
return;
if (end>0) // we just write into the buffer
{
code_word <<= end;
code_word &= (1<<start)-1;
byte_buffer |= code_word;
bits_written += start-end;
}
else // we have to split & write to the disk
{
int part2 = code_word & ((1<<(-end))-1);
code_word >>= (-end);
code_word &= (1<<start)-1;
byte_buffer |= code_word;
fputc(byte_buffer, f);
if (byte_buffer == 0xFF)
fputc(0, f); // stuffing bit
bits_written = 0;
byte_buffer = 0;
write_byte(f, part2, 8, 8+end);
return;
}
}
/**
* write code_len bits to the file (using a buffer inbetween) starting with the MSB (leftmost one)
* example: write_bits(14, 6) writes the bits '001110'
*/
void write_bits(FILE* f, int code_word, int code_len)
{
if (code_len == 0)
return;
#ifdef DEBUG
printf("** write_bits(%s)\n", binary_string(code_word, code_len));
#endif
write_byte(f, code_word, 8-bits_written, 8-bits_written-code_len);
}
/*
* extend the bitstream to 8-bit precision - fill missing bits with '1'
*/
void fill_last_byte(FILE* f)
{
byte_buffer |= (1<<(8-bits_written))-1;
fputc(byte_buffer, f);
byte_buffer = 0;
bits_written = 0;
}
/*
* write the bit representation of this DC coefficient
*/
void encode_dc_value(FILE* f, int dc_val, huff_code* huff)
{
#ifdef DEBUG
printf("writing dc_val=%d\n", dc_val);
#endif
int class = huff_class(dc_val);
int class_code = huff->sym_code[class];
int class_size = huff->sym_code_len[class];
write_bits(f, class_code, class_size);
unsigned int id = abs(dc_val);
if (dc_val < 0)
id = ~id;
write_bits(f, id, class);
}
/*
* write the bit representation of this AC coefficient, which has num_zeros preceeding zeros
*/
void encode_ac_value(FILE* f, int ac_val, int num_zeros, huff_code* huff)
{
#ifdef DEBUG
printf("writing ac_val=%d, num_zeros=%d\n", ac_val, num_zeros);
#endif
int class = huff_class(ac_val);
int v = ((num_zeros<<4)&0xF0) | (class&0x0F);
int code = huff->sym_code[v];
int size = huff->sym_code_len[v];
write_bits(f, code, size);
unsigned int id = abs(ac_val);
if (ac_val < 0)
id = ~id;
write_bits(f, id, class);
}
/*
* write the DC and AC coefficients of one color channel
*/
void write_coefficients(FILE* f, int num_pixel, int dct_quant[], huff_code* huff_dc, huff_code* huff_ac)
{
int num_zeros = 0;
int last_nonzero;
int i;
for (i=0; i<num_pixel; i++)
{
if (i%64 == 0)
{
encode_dc_value(f, dct_quant[i], huff_dc);
for (last_nonzero = i+63; last_nonzero>i; last_nonzero--)
if (dct_quant[last_nonzero] != 0)
break;
continue;
}
if (i == last_nonzero + 1)
{
write_bits(f, huff_ac->sym_code[0x00], huff_ac->sym_code_len[0x00]); // EOB symbol
// jump to the next block
i = (i/64+1)*64-1;
continue;
}
if (dct_quant[i] == 0)
{
num_zeros++;
if (num_zeros == 16)
{
write_bits(f, huff_ac->sym_code[0xF0], huff_ac->sym_code_len[0xF0]); // ZRL symbol