-
Notifications
You must be signed in to change notification settings - Fork 23
/
jpeg_forward_dct.cs
1329 lines (1157 loc) · 69.7 KB
/
jpeg_forward_dct.cs
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
/*
* This file contains the forward-DCT management logic.
* This code selects a particular DCT implementation to be used,
* and it performs related housekeeping chores including coefficient
* quantization.
*/
namespace BitMiracle.LibJpeg.Classic.Internal
{
/// <summary>
/// Forward DCT (also controls coefficient quantization)
///
/// A forward DCT routine is given a pointer to an input sample array and
/// a pointer to a work area of type DCTELEM[]; the DCT is to be performed
/// in-place in that buffer. Type DCTELEM is int for 8-bit samples, INT32
/// for 12-bit samples. (NOTE: Floating-point DCT implementations use an
/// array of type FAST_FLOAT, instead.)
/// The input data is to be fetched from the sample array starting at a
/// specified column. (Any row offset needed will be applied to the array
/// pointer before it is passed to the FDCT code.)
/// Note that the number of samples fetched by the FDCT routine is
/// DCT_h_scaled_size * DCT_v_scaled_size.
/// The DCT outputs are returned scaled up by a factor of 8; they therefore
/// have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
/// convention improves accuracy in integer implementations and saves some
/// work in floating-point ones.
///
/// Each IDCT routine has its own ideas about the best dct_table element type.
/// </summary>
class jpeg_forward_dct
{
private const int FAST_INTEGER_CONST_BITS = 8;
/* We use the following pre-calculated constants.
* If you change FAST_INTEGER_CONST_BITS you may want to add appropriate values.
*
* Convert a positive real constant to an integer scaled by CONST_SCALE.
* static int FAST_INTEGER_FIX(double x)
*{
* return ((int) ((x) * (((int) 1) << FAST_INTEGER_CONST_BITS) + 0.5));
*}
*/
private const int FAST_INTEGER_FIX_0_382683433 = 98; /* FIX(0.382683433) */
private const int FAST_INTEGER_FIX_0_541196100 = 139; /* FIX(0.541196100) */
private const int FAST_INTEGER_FIX_0_707106781 = 181; /* FIX(0.707106781) */
private const int FAST_INTEGER_FIX_1_306562965 = 334; /* FIX(1.306562965) */
private const int SLOW_INTEGER_CONST_BITS = 13;
private const int SLOW_INTEGER_PASS1_BITS = 2;
/* We use the following pre-calculated constants.
* If you change SLOW_INTEGER_CONST_BITS you may want to add appropriate values.
*
* Convert a positive real constant to an integer scaled by CONST_SCALE.
*
* static int SLOW_INTEGER_FIX(double x)
* {
* return ((int) ((x) * (((int) 1) << SLOW_INTEGER_CONST_BITS) + 0.5));
* }
*/
private const int SLOW_INTEGER_FIX_0_298631336 = 2446; /* FIX(0.298631336) */
private const int SLOW_INTEGER_FIX_0_390180644 = 3196; /* FIX(0.390180644) */
private const int SLOW_INTEGER_FIX_0_541196100 = 4433; /* FIX(0.541196100) */
private const int SLOW_INTEGER_FIX_0_765366865 = 6270; /* FIX(0.765366865) */
private const int SLOW_INTEGER_FIX_0_899976223 = 7373; /* FIX(0.899976223) */
private const int SLOW_INTEGER_FIX_1_175875602 = 9633; /* FIX(1.175875602) */
private const int SLOW_INTEGER_FIX_1_501321110 = 12299; /* FIX(1.501321110) */
private const int SLOW_INTEGER_FIX_1_847759065 = 15137; /* FIX(1.847759065) */
private const int SLOW_INTEGER_FIX_1_961570560 = 16069; /* FIX(1.961570560) */
private const int SLOW_INTEGER_FIX_2_053119869 = 16819; /* FIX(2.053119869) */
private const int SLOW_INTEGER_FIX_2_562915447 = 20995; /* FIX(2.562915447) */
private const int SLOW_INTEGER_FIX_3_072711026 = 25172; /* FIX(3.072711026) */
/* For AA&N IDCT method, divisors are equal to quantization
* coefficients scaled by scalefactor[row]*scalefactor[col], where
* scalefactor[0] = 1
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
* We apply a further scale factor of 8.
*/
private const int CONST_BITS = 14;
/* precomputed values scaled up by 14 bits */
private static readonly short[] aanscales = {
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 22725, 31521, 29692, 26722, 22725, 17855,
12299, 6270, 21407, 29692, 27969, 25172, 21407, 16819, 11585,
5906, 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 12873,
17855, 16819, 15137, 12873, 10114, 6967, 3552, 8867, 12299,
11585, 10426, 8867, 6967, 4799, 2446, 4520, 6270, 5906, 5315,
4520, 3552, 2446, 1247 };
/* For float AA&N IDCT method, divisors are equal to quantization
* coefficients scaled by scalefactor[row]*scalefactor[col], where
* scalefactor[0] = 1
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
* We apply a further scale factor of 8.
* What's actually stored is 1/divisor so that the inner loop can
* use a multiplication rather than a division.
*/
private static readonly double[] aanscalefactor = {
1.0, 1.387039845, 1.306562965, 1.175875602, 1.0,
0.785694958, 0.541196100, 0.275899379 };
private jpeg_compress_struct m_cinfo;
private delegate void forward_DCT_method_ptr(int[] data, byte[][] sample_data, int start_row, int start_col);
private forward_DCT_method_ptr[] do_dct = new forward_DCT_method_ptr[JpegConstants.MAX_COMPONENTS];
/* Same as above for the floating-point case. */
private delegate void float_DCT_method_ptr(float[] data, byte[][] sample_data, int start_row, int start_col);
private float_DCT_method_ptr[] do_float_dct = new float_DCT_method_ptr[JpegConstants.MAX_COMPONENTS];
/// <summary>
/// Perform forward DCT on one or more blocks of a component.
///
/// The input samples are taken from the sample_data[] array starting at
/// position start_row/start_col, and moving to the right for any additional
/// blocks. The quantized coefficients are returned in coef_blocks[].
/// </summary>
public delegate void forward_DCT_ptr(jpeg_component_info compptr, byte[][] sample_data, JBLOCK[] coef_blocks, int start_row, int start_col, int num_blocks);
/* It is useful to allow each component to have a separate FDCT method. */
public forward_DCT_ptr[] forward_DCT = new forward_DCT_ptr[JpegConstants.MAX_COMPONENTS];
/* The allocated post-DCT divisor tables - big enough for any supported variant and not
identical to the quant table entries, because of scaling (especially for an
unnormalized DCT) - are pointed to by dct_table in the per-component comp_info
structures. Each table is given in normal array order.
*/
private class divisor_table
{
public int[] int_array = new int[JpegConstants.DCTSIZE2];
public float[] float_array = new float[JpegConstants.DCTSIZE2];
};
private divisor_table[] m_dctTables;
public jpeg_forward_dct(jpeg_compress_struct cinfo)
{
m_cinfo = cinfo;
m_dctTables = new divisor_table[m_cinfo.m_num_components];
for (int ci = 0; ci < m_cinfo.m_num_components; ci++)
{
/* Allocate a divisor table for each component */
m_dctTables[ci] = new divisor_table();
}
}
/// <summary>
/// Initialize for a processing pass.
/// Verify that all referenced Q-tables are present, and set up
/// the divisor table for each one.
/// In the current implementation, DCT of all components is done during
/// the first pass, even if only some components will be output in the
/// first scan. Hence all components should be examined here.
/// </summary>
public virtual void start_pass()
{
J_DCT_METHOD method = 0;
for (int ci = 0; ci < m_cinfo.m_num_components; ci++)
{
/* Select the proper DCT routine for this component's scaling */
jpeg_component_info compptr = m_cinfo.Component_info[ci];
switch ((compptr.DCT_h_scaled_size << 8) + compptr.DCT_v_scaled_size)
{
case ((1 << 8) + 1):
do_dct[ci] = jpeg_fdct_1x1;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((2 << 8) + 2):
do_dct[ci] = jpeg_fdct_2x2;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((3 << 8) + 3):
do_dct[ci] = jpeg_fdct_3x3;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((4 << 8) + 4):
do_dct[ci] = jpeg_fdct_4x4;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((5 << 8) + 5):
do_dct[ci] = jpeg_fdct_5x5;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((6 << 8) + 6):
do_dct[ci] = jpeg_fdct_6x6;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((7 << 8) + 7):
do_dct[ci] = jpeg_fdct_7x7;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((9 << 8) + 9):
do_dct[ci] = jpeg_fdct_9x9;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((10 << 8) + 10):
do_dct[ci] = jpeg_fdct_10x10;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((11 << 8) + 11):
do_dct[ci] = jpeg_fdct_11x11;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((12 << 8) + 12):
do_dct[ci] = jpeg_fdct_12x12;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((13 << 8) + 13):
do_dct[ci] = jpeg_fdct_13x13;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((14 << 8) + 14):
do_dct[ci] = jpeg_fdct_14x14;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((15 << 8) + 15):
do_dct[ci] = jpeg_fdct_15x15;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((16 << 8) + 16):
do_dct[ci] = jpeg_fdct_16x16;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((16 << 8) + 8):
do_dct[ci] = jpeg_fdct_16x8;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((14 << 8) + 7):
do_dct[ci] = jpeg_fdct_14x7;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((12 << 8) + 6):
do_dct[ci] = jpeg_fdct_12x6;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((10 << 8) + 5):
do_dct[ci] = jpeg_fdct_10x5;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((8 << 8) + 4):
do_dct[ci] = jpeg_fdct_8x4;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((6 << 8) + 3):
do_dct[ci] = jpeg_fdct_6x3;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((4 << 8) + 2):
do_dct[ci] = jpeg_fdct_4x2;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((2 << 8) + 1):
do_dct[ci] = jpeg_fdct_2x1;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((8 << 8) + 16):
do_dct[ci] = jpeg_fdct_8x16;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((7 << 8) + 14):
do_dct[ci] = jpeg_fdct_7x14;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((6 << 8) + 12):
do_dct[ci] = jpeg_fdct_6x12;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((5 << 8) + 10):
do_dct[ci] = jpeg_fdct_5x10;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((4 << 8) + 8):
do_dct[ci] = jpeg_fdct_4x8;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((3 << 8) + 6):
do_dct[ci] = jpeg_fdct_3x6;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((2 << 8) + 4):
do_dct[ci] = jpeg_fdct_2x4;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((1 << 8) + 2):
do_dct[ci] = jpeg_fdct_1x2;
method = J_DCT_METHOD.JDCT_ISLOW; /* jfdctint uses islow-style table */
break;
case ((JpegConstants.DCTSIZE << 8) + JpegConstants.DCTSIZE):
switch (m_cinfo.m_dct_method)
{
case J_DCT_METHOD.JDCT_ISLOW:
do_dct[ci] = jpeg_fdct_islow;
method = J_DCT_METHOD.JDCT_ISLOW;
break;
case J_DCT_METHOD.JDCT_IFAST:
do_dct[ci] = jpeg_fdct_ifast;
method = J_DCT_METHOD.JDCT_IFAST;
break;
case J_DCT_METHOD.JDCT_FLOAT:
do_float_dct[ci] = jpeg_fdct_float;
method = J_DCT_METHOD.JDCT_FLOAT;
break;
default:
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOT_COMPILED);
break;
}
break;
default:
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_DCTSIZE, compptr.DCT_h_scaled_size, compptr.DCT_v_scaled_size);
break;
}
int qtblno = m_cinfo.Component_info[ci].Quant_tbl_no;
/* Make sure specified quantization table is present */
if (qtblno < 0 || qtblno >= JpegConstants.NUM_QUANT_TBLS || m_cinfo.m_quant_tbl_ptrs[qtblno] == null)
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NO_QUANT_TABLE, qtblno);
JQUANT_TBL qtbl = m_cinfo.m_quant_tbl_ptrs[qtblno];
int[] dtbl;
/* Create divisor table from quant table */
int i = 0;
switch (method)
{
case J_DCT_METHOD.JDCT_ISLOW:
/* For LL&M IDCT method, divisors are equal to raw quantization
* coefficients multiplied by 8 (to counteract scaling).
*/
dtbl = m_dctTables[ci].int_array;
for (i = 0; i < JpegConstants.DCTSIZE2; i++)
dtbl[i] = ((int)qtbl.quantval[i]) << (compptr.component_needed ? 4 : 3);
forward_DCT[ci] = forwardDCTImpl;
break;
case J_DCT_METHOD.JDCT_IFAST:
dtbl = m_dctTables[ci].int_array;
for (i = 0; i < JpegConstants.DCTSIZE2; i++)
dtbl[i] = JpegUtils.DESCALE((int)qtbl.quantval[i] * (int)aanscales[i], compptr.component_needed ? CONST_BITS - 4 : CONST_BITS - 3);
forward_DCT[ci] = forwardDCTImpl;
break;
case J_DCT_METHOD.JDCT_FLOAT:
float[] fdtbl = m_dctTables[ci].float_array;
i = 0;
for (int row = 0; row < JpegConstants.DCTSIZE; row++)
{
for (int col = 0; col < JpegConstants.DCTSIZE; col++)
{
fdtbl[i] = (float)(1.0 / (((double)qtbl.quantval[i] * aanscalefactor[row] * aanscalefactor[col] * (compptr.component_needed ? 16.0 : 8.0))));
i++;
}
}
forward_DCT[ci] = forwardDCTFloatImpl;
break;
default:
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOT_COMPILED);
break;
}
}
}
// This version is used for integer DCT implementations.
private void forwardDCTImpl(jpeg_component_info compptr, byte[][] sample_data, JBLOCK[] coef_blocks, int start_row, int start_col, int num_blocks)
{
/* This routine is heavily used, so it's worth coding it tightly. */
forward_DCT_method_ptr do_dct = this.do_dct[compptr.Component_index];
int[] divisors = m_dctTables[compptr.Component_index].int_array;
int[] workspace = new int[JpegConstants.DCTSIZE2]; /* work area for FDCT subroutine */
for (int bi = 0; bi < num_blocks; bi++, start_col += compptr.DCT_h_scaled_size)
{
/* Perform the DCT */
do_dct(workspace, sample_data, start_row, start_col);
/* Quantize/descale the coefficients, and store into coef_blocks[] */
var coeffBlock = coef_blocks[bi].data;
for (int i = 0; i < JpegConstants.DCTSIZE2; i++)
{
int qval = divisors[i];
int temp = workspace[i];
if (temp < 0)
{
temp = -temp;
temp += qval >> 1; /* for rounding */
if (temp >= qval)
temp /= qval;
else
temp = 0;
temp = -temp;
}
else
{
temp += qval >> 1; /* for rounding */
if (temp >= qval)
temp /= qval;
else
temp = 0;
}
coeffBlock[i] = (short)temp;
}
}
}
// This version is used for floating-point DCT implementations.
private void forwardDCTFloatImpl(jpeg_component_info compptr, byte[][] sample_data, JBLOCK[] coef_blocks, int start_row, int start_col, int num_blocks)
{
/* This routine is heavily used, so it's worth coding it tightly. */
float_DCT_method_ptr do_dct = do_float_dct[compptr.Component_index];
float[] divisors = m_dctTables[compptr.Component_index].float_array;
float[] workspace = new float[JpegConstants.DCTSIZE2]; /* work area for FDCT subroutine */
for (int bi = 0; bi < num_blocks; bi++, start_col += compptr.DCT_h_scaled_size)
{
/* Perform the DCT */
do_dct(workspace, sample_data, start_row, start_col);
/* Quantize/descale the coefficients, and store into coef_blocks[] */
var coeffBlock = coef_blocks[bi].data;
for (int i = 0; i < JpegConstants.DCTSIZE2; i++)
{
/* Apply the quantization and scaling factor */
float temp = workspace[i] * divisors[i];
/* Round to nearest integer.
* Since C does not specify the direction of rounding for negative
* quotients, we have to force the dividend positive for portability.
* The maximum coefficient size is +-16K (for 12-bit data), so this
* code should work for either 16-bit or 32-bit ints.
*/
coeffBlock[i] = (short)((int)(temp + (float)16384.5) - 16384);
}
}
}
/// <summary>
/// Perform the forward DCT on one block of samples.
/// NOTE: this code only copes with 8x8 DCTs.
///
/// A floating-point implementation of the
/// forward DCT (Discrete Cosine Transform).
///
/// This implementation should be more accurate than either of the integer
/// DCT implementations. However, it may not give the same results on all
/// machines because of differences in roundoff behavior. Speed will depend
/// on the hardware's floating point capacity.
///
/// A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
/// on each column. Direct algorithms are also available, but they are
/// much more complex and seem not to be any faster when reduced to code.
///
/// This implementation is based on Arai, Agui, and Nakajima's algorithm for
/// scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
/// Japanese, but the algorithm is described in the Pennebaker & Mitchell
/// JPEG textbook (see REFERENCES section in file README). The following code
/// is based directly on figure 4-8 in P&M.
/// While an 8-point DCT cannot be done in less than 11 multiplies, it is
/// possible to arrange the computation so that many of the multiplies are
/// simple scalings of the final outputs. These multiplies can then be
/// folded into the multiplications or divisions by the JPEG quantization
/// table entries. The AA&N method leaves only 5 multiplies and 29 adds
/// to be done in the DCT itself.
/// The primary disadvantage of this method is that with a fixed-point
/// implementation, accuracy is lost due to imprecise representation of the
/// scaled quantization values. However, that problem does not arise if
/// we use floating point arithmetic.
/// </summary>
private static void jpeg_fdct_float(float[] data, byte[][] sample_data, int start_row, int start_col)
{
/* Pass 1: process rows. */
int dataIndex = 0;
for (int ctr = 0; ctr < JpegConstants.DCTSIZE; ctr++)
{
byte[] elem = sample_data[start_row + ctr];
int elemIndex = start_col;
/* Load data into workspace */
float tmp0 = elem[elemIndex + 0] + elem[elemIndex + 7];
float tmp7 = elem[elemIndex + 0] - elem[elemIndex + 7];
float tmp1 = elem[elemIndex + 1] + elem[elemIndex + 6];
float tmp6 = elem[elemIndex + 1] - elem[elemIndex + 6];
float tmp2 = elem[elemIndex + 2] + elem[elemIndex + 5];
float tmp5 = elem[elemIndex + 2] - elem[elemIndex + 5];
float tmp3 = elem[elemIndex + 3] + elem[elemIndex + 4];
float tmp4 = elem[elemIndex + 3] - elem[elemIndex + 4];
/* Even part */
float tmp10 = tmp0 + tmp3; /* phase 2 */
float tmp13 = tmp0 - tmp3;
float tmp11 = tmp1 + tmp2;
float tmp12 = tmp1 - tmp2;
/* Apply unsigned->signed conversion. */
data[dataIndex + 0] = tmp10 + tmp11 - 8 * JpegConstants.CENTERJSAMPLE; /* phase 3 */
data[dataIndex + 4] = tmp10 - tmp11;
float z1 = (tmp12 + tmp13) * ((float)0.707106781); /* c4 */
data[dataIndex + 2] = tmp13 + z1; /* phase 5 */
data[dataIndex + 6] = tmp13 - z1;
/* Odd part */
tmp10 = tmp4 + tmp5; /* phase 2 */
tmp11 = tmp5 + tmp6;
tmp12 = tmp6 + tmp7;
/* The rotator is modified from fig 4-8 to avoid extra negations. */
float z5 = (tmp10 - tmp12) * ((float)0.382683433); /* c6 */
float z2 = ((float)0.541196100) * tmp10 + z5; /* c2-c6 */
float z4 = ((float)1.306562965) * tmp12 + z5; /* c2+c6 */
float z3 = tmp11 * ((float)0.707106781); /* c4 */
float z11 = tmp7 + z3; /* phase 5 */
float z13 = tmp7 - z3;
data[dataIndex + 5] = z13 + z2; /* phase 6 */
data[dataIndex + 3] = z13 - z2;
data[dataIndex + 1] = z11 + z4;
data[dataIndex + 7] = z11 - z4;
dataIndex += JpegConstants.DCTSIZE; /* advance pointer to next row */
}
/* Pass 2: process columns. */
dataIndex = 0;
for (int ctr = JpegConstants.DCTSIZE - 1; ctr >= 0; ctr--)
{
float tmp0 = data[dataIndex + JpegConstants.DCTSIZE * 0] + data[dataIndex + JpegConstants.DCTSIZE * 7];
float tmp7 = data[dataIndex + JpegConstants.DCTSIZE * 0] - data[dataIndex + JpegConstants.DCTSIZE * 7];
float tmp1 = data[dataIndex + JpegConstants.DCTSIZE * 1] + data[dataIndex + JpegConstants.DCTSIZE * 6];
float tmp6 = data[dataIndex + JpegConstants.DCTSIZE * 1] - data[dataIndex + JpegConstants.DCTSIZE * 6];
float tmp2 = data[dataIndex + JpegConstants.DCTSIZE * 2] + data[dataIndex + JpegConstants.DCTSIZE * 5];
float tmp5 = data[dataIndex + JpegConstants.DCTSIZE * 2] - data[dataIndex + JpegConstants.DCTSIZE * 5];
float tmp3 = data[dataIndex + JpegConstants.DCTSIZE * 3] + data[dataIndex + JpegConstants.DCTSIZE * 4];
float tmp4 = data[dataIndex + JpegConstants.DCTSIZE * 3] - data[dataIndex + JpegConstants.DCTSIZE * 4];
/* Even part */
float tmp10 = tmp0 + tmp3; /* phase 2 */
float tmp13 = tmp0 - tmp3;
float tmp11 = tmp1 + tmp2;
float tmp12 = tmp1 - tmp2;
data[dataIndex + JpegConstants.DCTSIZE * 0] = tmp10 + tmp11; /* phase 3 */
data[dataIndex + JpegConstants.DCTSIZE * 4] = tmp10 - tmp11;
float z1 = (tmp12 + tmp13) * ((float)0.707106781); /* c4 */
data[dataIndex + JpegConstants.DCTSIZE * 2] = tmp13 + z1; /* phase 5 */
data[dataIndex + JpegConstants.DCTSIZE * 6] = tmp13 - z1;
/* Odd part */
tmp10 = tmp4 + tmp5; /* phase 2 */
tmp11 = tmp5 + tmp6;
tmp12 = tmp6 + tmp7;
/* The rotator is modified from fig 4-8 to avoid extra negations. */
float z5 = (tmp10 - tmp12) * ((float)0.382683433); /* c6 */
float z2 = ((float)0.541196100) * tmp10 + z5; /* c2-c6 */
float z4 = ((float)1.306562965) * tmp12 + z5; /* c2+c6 */
float z3 = tmp11 * ((float)0.707106781); /* c4 */
float z11 = tmp7 + z3; /* phase 5 */
float z13 = tmp7 - z3;
data[dataIndex + JpegConstants.DCTSIZE * 5] = z13 + z2; /* phase 6 */
data[dataIndex + JpegConstants.DCTSIZE * 3] = z13 - z2;
data[dataIndex + JpegConstants.DCTSIZE * 1] = z11 + z4;
data[dataIndex + JpegConstants.DCTSIZE * 7] = z11 - z4;
dataIndex++; /* advance pointer to next column */
}
}
/// <summary>
/// Perform the forward DCT on one block of samples.
/// NOTE: this code only copes with 8x8 DCTs.
/// This file contains a fast, not so accurate integer implementation of the
/// forward DCT (Discrete Cosine Transform).
///
/// A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
/// on each column. Direct algorithms are also available, but they are
/// much more complex and seem not to be any faster when reduced to code.
///
/// This implementation is based on Arai, Agui, and Nakajima's algorithm for
/// scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
/// Japanese, but the algorithm is described in the Pennebaker & Mitchell
/// JPEG textbook (see REFERENCES section in file README). The following code
/// is based directly on figure 4-8 in P&M.
/// While an 8-point DCT cannot be done in less than 11 multiplies, it is
/// possible to arrange the computation so that many of the multiplies are
/// simple scalings of the final outputs. These multiplies can then be
/// folded into the multiplications or divisions by the JPEG quantization
/// table entries. The AA&N method leaves only 5 multiplies and 29 adds
/// to be done in the DCT itself.
/// The primary disadvantage of this method is that with fixed-point math,
/// accuracy is lost due to imprecise representation of the scaled
/// quantization values. The smaller the quantization table entry, the less
/// precise the scaled value, so this implementation does worse with high-
/// quality-setting files than with low-quality ones.
///
/// Scaling decisions are generally the same as in the LL&M algorithm;
/// see jpeg_fdct_islow for more details. However, we choose to descale
/// (right shift) multiplication products as soon as they are formed,
/// rather than carrying additional fractional bits into subsequent additions.
/// This compromises accuracy slightly, but it lets us save a few shifts.
/// More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
/// everywhere except in the multiplications proper; this saves a good deal
/// of work on 16-bit-int machines.
///
/// Again to save a few shifts, the intermediate results between pass 1 and
/// pass 2 are not upscaled, but are represented only to integral precision.
///
/// A final compromise is to represent the multiplicative constants to only
/// 8 fractional bits, rather than 13. This saves some shifting work on some
/// machines, and may also reduce the cost of multiplication (since there
/// are fewer one-bits in the constants).
/// </summary>
private static void jpeg_fdct_ifast(int[] data, byte[][] sample_data, int start_row, int start_col)
{
/* Pass 1: process rows. */
int dataIndex = 0;
for (int ctr = 0; ctr < JpegConstants.DCTSIZE; ctr++)
{
byte[] elem = sample_data[start_row + ctr];
int elemIndex = start_col;
/* Load data into workspace */
int tmp0 = elem[elemIndex + 0] + elem[elemIndex + 7];
int tmp7 = elem[elemIndex + 0] - elem[elemIndex + 7];
int tmp1 = elem[elemIndex + 1] + elem[elemIndex + 6];
int tmp6 = elem[elemIndex + 1] - elem[elemIndex + 6];
int tmp2 = elem[elemIndex + 2] + elem[elemIndex + 5];
int tmp5 = elem[elemIndex + 2] - elem[elemIndex + 5];
int tmp3 = elem[elemIndex + 3] + elem[elemIndex + 4];
int tmp4 = elem[elemIndex + 3] - elem[elemIndex + 4];
/* Even part */
int tmp10 = tmp0 + tmp3; /* phase 2 */
int tmp13 = tmp0 - tmp3;
int tmp11 = tmp1 + tmp2;
int tmp12 = tmp1 - tmp2;
/* Apply unsigned->signed conversion. */
data[dataIndex + 0] = tmp10 + tmp11 - 8 * JpegConstants.CENTERJSAMPLE; /* phase 3 */
data[dataIndex + 4] = tmp10 - tmp11;
int z1 = FAST_INTEGER_MULTIPLY(tmp12 + tmp13, FAST_INTEGER_FIX_0_707106781); /* c4 */
data[dataIndex + 2] = tmp13 + z1; /* phase 5 */
data[dataIndex + 6] = tmp13 - z1;
/* Odd part */
tmp10 = tmp4 + tmp5; /* phase 2 */
tmp11 = tmp5 + tmp6;
tmp12 = tmp6 + tmp7;
/* The rotator is modified from fig 4-8 to avoid extra negations. */
int z5 = FAST_INTEGER_MULTIPLY(tmp10 - tmp12, FAST_INTEGER_FIX_0_382683433); /* c6 */
int z2 = FAST_INTEGER_MULTIPLY(tmp10, FAST_INTEGER_FIX_0_541196100) + z5; /* c2-c6 */
int z4 = FAST_INTEGER_MULTIPLY(tmp12, FAST_INTEGER_FIX_1_306562965) + z5; /* c2+c6 */
int z3 = FAST_INTEGER_MULTIPLY(tmp11, FAST_INTEGER_FIX_0_707106781); /* c4 */
int z11 = tmp7 + z3; /* phase 5 */
int z13 = tmp7 - z3;
data[dataIndex + 5] = z13 + z2; /* phase 6 */
data[dataIndex + 3] = z13 - z2;
data[dataIndex + 1] = z11 + z4;
data[dataIndex + 7] = z11 - z4;
dataIndex += JpegConstants.DCTSIZE; /* advance pointer to next row */
}
/* Pass 2: process columns. */
dataIndex = 0;
for (int ctr = JpegConstants.DCTSIZE - 1; ctr >= 0; ctr--)
{
int tmp0 = data[dataIndex + JpegConstants.DCTSIZE * 0] + data[dataIndex + JpegConstants.DCTSIZE * 7];
int tmp7 = data[dataIndex + JpegConstants.DCTSIZE * 0] - data[dataIndex + JpegConstants.DCTSIZE * 7];
int tmp1 = data[dataIndex + JpegConstants.DCTSIZE * 1] + data[dataIndex + JpegConstants.DCTSIZE * 6];
int tmp6 = data[dataIndex + JpegConstants.DCTSIZE * 1] - data[dataIndex + JpegConstants.DCTSIZE * 6];
int tmp2 = data[dataIndex + JpegConstants.DCTSIZE * 2] + data[dataIndex + JpegConstants.DCTSIZE * 5];
int tmp5 = data[dataIndex + JpegConstants.DCTSIZE * 2] - data[dataIndex + JpegConstants.DCTSIZE * 5];
int tmp3 = data[dataIndex + JpegConstants.DCTSIZE * 3] + data[dataIndex + JpegConstants.DCTSIZE * 4];
int tmp4 = data[dataIndex + JpegConstants.DCTSIZE * 3] - data[dataIndex + JpegConstants.DCTSIZE * 4];
/* Even part */
int tmp10 = tmp0 + tmp3; /* phase 2 */
int tmp13 = tmp0 - tmp3;
int tmp11 = tmp1 + tmp2;
int tmp12 = tmp1 - tmp2;
data[dataIndex + JpegConstants.DCTSIZE * 0] = tmp10 + tmp11; /* phase 3 */
data[dataIndex + JpegConstants.DCTSIZE * 4] = tmp10 - tmp11;
int z1 = FAST_INTEGER_MULTIPLY(tmp12 + tmp13, FAST_INTEGER_FIX_0_707106781); /* c4 */
data[dataIndex + JpegConstants.DCTSIZE * 2] = tmp13 + z1; /* phase 5 */
data[dataIndex + JpegConstants.DCTSIZE * 6] = tmp13 - z1;
/* Odd part */
tmp10 = tmp4 + tmp5; /* phase 2 */
tmp11 = tmp5 + tmp6;
tmp12 = tmp6 + tmp7;
/* The rotator is modified from fig 4-8 to avoid extra negations. */
int z5 = FAST_INTEGER_MULTIPLY(tmp10 - tmp12, FAST_INTEGER_FIX_0_382683433); /* c6 */
int z2 = FAST_INTEGER_MULTIPLY(tmp10, FAST_INTEGER_FIX_0_541196100) + z5; /* c2-c6 */
int z4 = FAST_INTEGER_MULTIPLY(tmp12, FAST_INTEGER_FIX_1_306562965) + z5; /* c2+c6 */
int z3 = FAST_INTEGER_MULTIPLY(tmp11, FAST_INTEGER_FIX_0_707106781); /* c4 */
int z11 = tmp7 + z3; /* phase 5 */
int z13 = tmp7 - z3;
data[dataIndex + JpegConstants.DCTSIZE * 5] = z13 + z2; /* phase 6 */
data[dataIndex + JpegConstants.DCTSIZE * 3] = z13 - z2;
data[dataIndex + JpegConstants.DCTSIZE * 1] = z11 + z4;
data[dataIndex + JpegConstants.DCTSIZE * 7] = z11 - z4;
dataIndex++; /* advance pointer to next column */
}
}
/// <summary>
/// Perform the forward DCT on one block of samples.
/// NOTE: this code only copes with 8x8 DCTs.
///
/// A slow-but-accurate integer implementation of the
/// forward DCT (Discrete Cosine Transform).
///
/// A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
/// on each column. Direct algorithms are also available, but they are
/// much more complex and seem not to be any faster when reduced to code.
///
/// This implementation is based on an algorithm described in
/// C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
/// Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
/// Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
/// The primary algorithm described there uses 11 multiplies and 29 adds.
/// We use their alternate method with 12 multiplies and 32 adds.
/// The advantage of this method is that no data path contains more than one
/// multiplication; this allows a very simple and accurate implementation in
/// scaled fixed-point arithmetic, with a minimal number of shifts.
///
/// The poop on this scaling stuff is as follows:
///
/// Each 1-D DCT step produces outputs which are a factor of sqrt(N)
/// larger than the true DCT outputs. The final outputs are therefore
/// a factor of N larger than desired; since N=8 this can be cured by
/// a simple right shift at the end of the algorithm. The advantage of
/// this arrangement is that we save two multiplications per 1-D DCT,
/// because the y0 and y4 outputs need not be divided by sqrt(N).
/// In the IJG code, this factor of 8 is removed by the quantization
/// step, NOT here.
///
/// We have to do addition and subtraction of the integer inputs, which
/// is no problem, and multiplication by fractional constants, which is
/// a problem to do in integer arithmetic. We multiply all the constants
/// by CONST_SCALE and convert them to integer constants (thus retaining
/// SLOW_INTEGER_CONST_BITS bits of precision in the constants). After doing a
/// multiplication we have to divide the product by CONST_SCALE, with proper
/// rounding, to produce the correct output. This division can be done
/// cheaply as a right shift of SLOW_INTEGER_CONST_BITS bits. We postpone shifting
/// as long as possible so that partial sums can be added together with
/// full fractional precision.
///
/// The outputs of the first pass are scaled up by SLOW_INTEGER_PASS1_BITS bits so that
/// they are represented to better-than-integral precision. These outputs
/// require BITS_IN_JSAMPLE + SLOW_INTEGER_PASS1_BITS + 3 bits; this fits in a 16-bit word
/// with the recommended scaling. (For 12-bit sample data, the intermediate
/// array is int anyway.)
///
/// To avoid overflow of the 32-bit intermediate results in pass 2, we must
/// have BITS_IN_JSAMPLE + SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS <= 26. Error analysis
/// shows that the values given below are the most effective.
/// </summary>
private static void jpeg_fdct_islow(int[] data, byte[][] sample_data, int start_row, int start_col)
{
/* Pass 1: process rows. */
/* Note results are scaled up by sqrt(8) compared to a true DCT; */
/* furthermore, we scale the results by 2**SLOW_INTEGER_PASS1_BITS. */
int dataIndex = 0;
for (int ctr = 0; ctr < JpegConstants.DCTSIZE; ctr++)
{
byte[] elem = sample_data[start_row + ctr];
int elemIndex = start_col;
int tmp0 = elem[elemIndex + 0] + elem[elemIndex + 7];
int tmp1 = elem[elemIndex + 1] + elem[elemIndex + 6];
int tmp2 = elem[elemIndex + 2] + elem[elemIndex + 5];
int tmp3 = elem[elemIndex + 3] + elem[elemIndex + 4];
/* Even part per LL&M figure 1 --- note that published figure is faulty;
* rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
*/
int tmp10 = tmp0 + tmp3;
int tmp12 = tmp0 - tmp3;
int tmp11 = tmp1 + tmp2;
int tmp13 = tmp1 - tmp2;
tmp0 = elem[elemIndex + 0] - elem[elemIndex + 7];
tmp1 = elem[elemIndex + 1] - elem[elemIndex + 6];
tmp2 = elem[elemIndex + 2] - elem[elemIndex + 5];
tmp3 = elem[elemIndex + 3] - elem[elemIndex + 4];
data[dataIndex + 0] = (tmp10 + tmp11 - 8 * JpegConstants.CENTERJSAMPLE) << SLOW_INTEGER_PASS1_BITS;
data[dataIndex + 4] = (tmp10 - tmp11) << SLOW_INTEGER_PASS1_BITS;
int z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_0_541196100;
/* Add fudge factor here for final descale. */
z1 += 1 << (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS - 1);
/* c2-c6 */
data[dataIndex + 2] =
(z1 + tmp12 * SLOW_INTEGER_FIX_0_765366865) >> (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
data[dataIndex + 6] = JpegUtils.DESCALE(z1 - tmp13 * SLOW_INTEGER_FIX_1_847759065,
SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
/* Odd part per figure 8 --- note paper omits factor of sqrt(2).
* cK represents cos(K*pi/16).
* i0..i3 in the paper are tmp4..tmp7 here.
*/
tmp12 = tmp0 + tmp2;
tmp13 = tmp1 + tmp3;
z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_1_175875602; /* c3 */
/* Add fudge factor here for final descale. */
z1 += 1 << (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS - 1);
tmp12 = tmp12 * (-SLOW_INTEGER_FIX_0_390180644); /* -c3+c5 */
tmp13 = tmp13 * (-SLOW_INTEGER_FIX_1_961570560); /* -c3-c5 */
tmp12 += z1;
tmp13 += z1;
z1 = (tmp0 + tmp3) * (-SLOW_INTEGER_FIX_0_899976223); /* -c3+c7 */
tmp0 = tmp0 * SLOW_INTEGER_FIX_1_501321110; /* c1+c3-c5-c7 */
tmp3 = tmp3 * SLOW_INTEGER_FIX_0_298631336; /* -c1+c3+c5-c7 */
tmp0 += z1 + tmp12;
tmp3 += z1 + tmp13;
z1 = (tmp1 + tmp2) * (-SLOW_INTEGER_FIX_2_562915447); /* -c1-c3 */
tmp1 = tmp1 * SLOW_INTEGER_FIX_3_072711026; /* c1+c3+c5-c7 */
tmp2 = tmp2 * SLOW_INTEGER_FIX_2_053119869; /* c1+c3-c5+c7 */
tmp1 += z1 + tmp13;
tmp2 += z1 + tmp12;
data[dataIndex + 1] = tmp0 >> (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
data[dataIndex + 3] = tmp1 >> (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
data[dataIndex + 5] = tmp2 >> (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
data[dataIndex + 7] = tmp3 >> (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
dataIndex += JpegConstants.DCTSIZE; /* advance pointer to next row */
}
/* Pass 2: process columns.
* We remove the SLOW_INTEGER_PASS1_BITS scaling, but leave the results scaled up
* by an overall factor of 8.
* cK represents sqrt(2) * cos(K*pi/16).
*/
dataIndex = 0;
for (int ctr = JpegConstants.DCTSIZE - 1; ctr >= 0; ctr--)
{
/* Even part per LL&M figure 1 --- note that published figure is faulty;
* rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
*/
int tmp0 = data[dataIndex + JpegConstants.DCTSIZE * 0] + data[dataIndex + JpegConstants.DCTSIZE * 7];
int tmp1 = data[dataIndex + JpegConstants.DCTSIZE * 1] + data[dataIndex + JpegConstants.DCTSIZE * 6];
int tmp2 = data[dataIndex + JpegConstants.DCTSIZE * 2] + data[dataIndex + JpegConstants.DCTSIZE * 5];
int tmp3 = data[dataIndex + JpegConstants.DCTSIZE * 3] + data[dataIndex + JpegConstants.DCTSIZE * 4];
/* Add fudge factor here for final descale. */
int tmp10 = tmp0 + tmp3 + (1 << (SLOW_INTEGER_PASS1_BITS - 1));
int tmp12 = tmp0 - tmp3;
int tmp11 = tmp1 + tmp2;
int tmp13 = tmp1 - tmp2;
tmp0 = data[dataIndex + JpegConstants.DCTSIZE * 0] - data[dataIndex + JpegConstants.DCTSIZE * 7];
tmp1 = data[dataIndex + JpegConstants.DCTSIZE * 1] - data[dataIndex + JpegConstants.DCTSIZE * 6];
tmp2 = data[dataIndex + JpegConstants.DCTSIZE * 2] - data[dataIndex + JpegConstants.DCTSIZE * 5];
tmp3 = data[dataIndex + JpegConstants.DCTSIZE * 3] - data[dataIndex + JpegConstants.DCTSIZE * 4];
data[dataIndex + JpegConstants.DCTSIZE * 0] = (tmp10 + tmp11) >> SLOW_INTEGER_PASS1_BITS;
data[dataIndex + JpegConstants.DCTSIZE * 4] = (tmp10 - tmp11) >> SLOW_INTEGER_PASS1_BITS;
int z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_0_541196100; /* c6 */
/* Add fudge factor here for final descale. */
z1 += 1 << (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS - 1);
data[dataIndex + JpegConstants.DCTSIZE * 2] =
(z1 + tmp12 * SLOW_INTEGER_FIX_0_765366865) >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
data[dataIndex + JpegConstants.DCTSIZE * 6] =
(z1 - tmp13 * SLOW_INTEGER_FIX_1_847759065) >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
/* Odd part per figure 8 --- note paper omits factor of sqrt(2).
* i0..i3 in the paper are tmp4..tmp7 here.
*/
tmp12 = tmp0 + tmp2;
tmp13 = tmp1 + tmp3;
z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_1_175875602; /* c3 */
/* Add fudge factor here for final descale. */
z1 += 1 << (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS - 1);
tmp12 = tmp12 * (-SLOW_INTEGER_FIX_0_390180644); /* -c3+c5 */
tmp13 = tmp13 * (-SLOW_INTEGER_FIX_1_961570560); /* -c3-c5 */
tmp12 += z1;
tmp13 += z1;
z1 = (tmp0 + tmp3) * (-SLOW_INTEGER_FIX_0_899976223); /* -c3+c7 */
tmp0 = tmp0 * SLOW_INTEGER_FIX_1_501321110; /* c1+c3-c5-c7 */
tmp3 = tmp3 * SLOW_INTEGER_FIX_0_298631336; /* -c1+c3+c5-c7 */
tmp0 += z1 + tmp12;
tmp3 += z1 + tmp13;
z1 = (tmp1 + tmp2) * (-SLOW_INTEGER_FIX_2_562915447); /* -c1-c3 */
tmp1 = tmp1 * SLOW_INTEGER_FIX_3_072711026; /* c1+c3+c5-c7 */
tmp2 = tmp2 * SLOW_INTEGER_FIX_2_053119869; /* c1+c3-c5+c7 */
tmp1 += z1 + tmp13;
tmp2 += z1 + tmp12;
data[dataIndex + JpegConstants.DCTSIZE * 1] = tmp0 >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
data[dataIndex + JpegConstants.DCTSIZE * 3] = tmp1 >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
data[dataIndex + JpegConstants.DCTSIZE * 5] = tmp2 >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
data[dataIndex + JpegConstants.DCTSIZE * 7] = tmp3 >> (SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS);
dataIndex++; /* advance pointer to next column */
}
}
/// <summary>
/// Multiply a DCTELEM variable by an int constant, and immediately
/// descale to yield a DCTELEM result.
/// </summary>
private static int FAST_INTEGER_MULTIPLY(int var, int c)
{
#if !USE_ACCURATE_ROUNDING
return (var * c) >> FAST_INTEGER_CONST_BITS;
#else
return (JpegUtils.DESCALE((var) * (c), FAST_INTEGER_CONST_BITS));
#endif
}
static int SLOW_INTEGER_FIX(double x)
{
return ((int)((x) * (((int)1) << SLOW_INTEGER_CONST_BITS) + 0.5));
}
private void jpeg_fdct_1x1(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_2x2(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_3x3(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_4x4(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_5x5(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_6x6(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_7x7(int[] data, byte[][] sample_data, int start_row, int start_col)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOTIMPL);
}
private void jpeg_fdct_9x9(int[] data, byte[][] sample_data, int start_row, int start_col)