-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbojpegipp.c
1250 lines (1096 loc) · 38.9 KB
/
turbojpegipp.c
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
/* Copyright (C)2004 Landmark Graphics
* Copyright (C)2005, 2006 Sun Microsystems, Inc.
*
* This library is free software and may be redistributed and/or modified under
* the terms of the wxWindows Library License, Version 3.1 or (at your option)
* any later version. The full license is in the LICENSE.txt file included
* with this distribution.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* wxWindows Library License for more details.
*/
// This implements a JPEG compressor/decompressor using the Intel Performance Primitives
#ifdef _WIN32
#include <windows.h>
#define snprintf _snprintf
#if defined(_X86_) || defined(_AMD64_)
#define USECPUID
#endif
#else
#if defined(__i386__) || defined(__x86_64__)
#define USECPUID
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ippcore.h"
#include "ippi.h"
#include "ippj.h"
#include "ipps.h"
#include "turbojpeg.h"
#define CACHE_LINE 32
static char lasterror[1024]="No error";
static const int _mcuw[NUMSUBOPT]={8, 16, 16, 8};
static const int _mcuh[NUMSUBOPT]={8, 8, 16, 8};
static int ippstaticinitcalled=0;
#define checkhandle(h) jpgstruct *jpg=(jpgstruct *)h; \
if(!jpg) {snprintf(lasterror, 1023, "%d: Invalid handle", __LINE__); return -1;}
#ifndef IPP_VERSION_MAJOR
#include "ippversion.h"
#endif
#if IPP_VERSION_MAJOR>=5
#define ippCoreGetStatusString ippGetStatusString
#define ippCoreGetCpuType ippGetCpuType
#define ippStaticInitBest ippStaticInit
#endif
#if IPP_VERSION_MAJOR>=7
#define ippStaticInitCpu ippInitCpu
#endif
#define _throw(c) {snprintf(lasterror, 1023, "%d: %s", __LINE__, c); return -1;}
#define _ipp(a) {IppStatus __err; if((__err=(a))<ippStsNoErr) _throw(ippCoreGetStatusString(__err));}
#define _ippn(a) {IppStatus __err; if((__err=(a))<ippStsNoErr) {snprintf(lasterror, 1023, "%d: %s", __LINE__, ippCoreGetStatusString(__err)); return NULL;}}
#define _catch(a) {if((a)==-1) return -1;}
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
typedef struct _jpgstruct
{
Ipp8u *bmpbuf, *bmpptr, *jpgbuf, *jpgptr;
int xmcus, ymcus, width, height, pitch, ps, subsamp, qual, flags;
unsigned long bytesprocessed, bytesleft;
IppiEncodeHuffmanState *e_huffstate;
IppiDecodeHuffmanState *d_huffstate;
IppiEncodeHuffmanSpec *e_dclumtable, *e_aclumtable, *e_dcchromtable, *e_acchromtable;
IppiDecodeHuffmanSpec *d_dclumtable, *d_aclumtable, *d_dcchromtable, *d_acchromtable;
Ipp16u __chromqtable[64+7], __lumqtable[64+7], *chromqtable, *lumqtable;
Ipp8u __mcubuf[384*2+CACHE_LINE-1]; Ipp16s *mcubuf;
IppCpuType cpu;
int initc, initd;
} jpgstruct;
// If IPP CPU type is unknown (i.e. non-Intel), use CPUID instruction to figure
// out what type of vector code is supported
#define MMXMASK (1<<23)
#define SSEMASK (1<<25)
#define SSE2MASK (1<<26)
#define SSE3MASK 1
static __inline void _cpuid(int *flags, int *flags2)
{
int a=0, c=0, d=0;
#ifdef USECPUID
#ifdef _MSC_VER
__asm {
xor eax, eax
cpuid
mov a, eax
}
if(a<1) return;
__asm {
mov eax, 1
cpuid
mov c, ecx
mov d, edx
}
#else
__asm__ __volatile__ (
#ifdef __i386__
"pushl %%ebx\n"
#endif
"cpuid\n"
#ifdef __i386__
"popl %%ebx\n"
#endif
: "=a" (a) : "a" (0)
#ifndef __i386__
: "bx"
#endif
);
if(a<1) return;
__asm__ __volatile__ (
#ifdef __i386__
"pushl %%ebx\n"
#endif
"cpuid\n"
#ifdef __i386__
"popl %%ebx\n"
#endif
: "=c" (c), "=d" (d) : "a" (1)
#ifndef __i386__
: "bx"
#endif
);
#endif // _MSC_VER
*flags=d; *flags2=c;
#endif // USECPUID
}
static __inline IppCpuType AutoDetect(void)
{
int flags=0, flags2=0; IppCpuType cpu=ippCpuUnknown;
_cpuid(&flags, &flags2);
if(flags2&SSE3MASK) cpu=ippCpuEM64T;
else if(flags&SSE2MASK) cpu=ippCpuP4;
else if(flags&SSEMASK) cpu=ippCpuPIII;
else if(flags&MMXMASK) cpu=ippCpuPII;
return cpu;
}
#define forcecpu(c) { \
char *env=NULL; \
if(jpg->cpu!=c) \
{ \
ippStaticInitCpu(c); \
if((env=getenv("VGL_VERBOSE"))!=NULL && strlen(env)>0 \
&& !strncmp(env, "1", 1)) \
fprintf(stderr, "[TurboJPEG] Forcing CPU type to %d\n", c); \
jpg->cpu=c; \
}}
//////////////////////////////////////////////////////////////////////////////
// COMPRESSOR
//////////////////////////////////////////////////////////////////////////////
// Default quantization tables per JPEG spec
static const Ipp8u lumqtable[64]=
{
16, 11, 12, 14, 12, 10, 16, 14,
13, 14, 18, 17, 16, 19, 24, 40,
26, 24, 22, 22, 24, 49, 35, 37,
29, 40, 58, 51, 61, 60, 57, 51,
56, 55, 64, 72, 92, 78, 64, 68,
87, 69, 55, 56, 80, 109, 81, 87,
95, 98, 103, 104, 103, 62, 77, 113,
121, 112, 100, 120, 92, 101, 103, 99
};
static const Ipp8u chromqtable[64]=
{
17, 18, 18, 24, 21, 24, 47, 26,
26, 47, 99, 66, 56, 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, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
// Huffman tables per JPEG spec
static const Ipp8u dclumbits[16]=
{
0, 1, 5, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0
};
static const Ipp8u dclumvals[]=
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
static const Ipp8u dcchrombits[16]=
{
0, 3, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0
};
static const Ipp8u dcchromvals[]=
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
static const Ipp8u aclumbits[16]=
{
0, 2, 1, 3, 3, 2, 4, 3,
5, 5, 4, 4, 0, 0, 1, 0x7d
};
static const Ipp8u aclumvals[]=
{
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa
};
static const Ipp8u acchrombits[16]=
{
0, 2, 1, 2, 4, 4, 3, 4,
7, 5, 4, 4, 0, 1, 2, 0x77
};
static const Ipp8u acchromvals[]=
{
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa
};
const Ipp16s redlut[256] =
{
0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4,
5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9,
10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14,
14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19,
19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 24,
24, 24, 25, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28,
29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33,
33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 38, 38,
38, 39, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 42, 43,
43, 43, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 47, 47, 47, 48,
48, 48, 48, 49, 49, 49, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52,
53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57,
57, 58, 58, 58, 59, 59, 59, 60, 60, 60, 60, 61, 61, 61, 62, 62,
62, 62, 63, 63, 63, 64, 64, 64, 65, 65, 65, 65, 66, 66, 66, 67,
67, 67, 68, 68, 68, 68, 69, 69, 69, 70, 70, 70, 71, 71, 71, 71,
72, 72, 72, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 76, 76, 76
};
const Ipp16s greenlut[256] =
{
-128, -127, -127, -126, -126, -125, -124, -124, -123, -123, -122, -122,
-121, -120, -120, -119, -119, -118, -117, -117, -116, -116, -115, -114,
-114, -113, -113, -112, -112, -111, -110, -110, -109, -109, -108, -107,
-107, -106, -106, -105, -105, -104, -103, -103, -102, -102, -101, -100,
-100, -99, -99, -98, -97, -97, -96, -96, -95, -95, -94, -93,
-93, -92, -92, -91, -90, -90, -89, -89, -88, -87, -87, -86,
-86, -85, -85, -84, -83, -83, -82, -82, -81, -80, -80, -79,
-79, -78, -78, -77, -76, -76, -75, -75, -74, -73, -73, -72,
-72, -71, -70, -70, -69, -69, -68, -68, -67, -66, -66, -65,
-65, -64, -63, -63, -62, -62, -61, -60, -60, -59, -59, -58,
-58, -57, -56, -56, -55, -55, -54, -53, -53, -52, -52, -51,
-51, -50, -49, -49, -48, -48, -47, -46, -46, -45, -45, -44,
-43, -43, -42, -42, -41, -41, -40, -39, -39, -38, -38, -37,
-36, -36, -35, -35, -34, -33, -33, -32, -32, -31, -31, -30,
-29, -29, -28, -28, -27, -26, -26, -25, -25, -24, -24, -23,
-22, -22, -21, -21, -20, -19, -19, -18, -18, -17, -16, -16,
-15, -15, -14, -14, -13, -12, -12, -11, -11, -10, -9, -9,
-8, -8, -7, -6, -6, -5, -5, -4, -4, -3, -2, -2,
-1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5,
6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12,
13, 13, 14, 15, 15, 16, 16, 17, 18, 18, 19, 19,
20, 21, 21, 22
};
const Ipp16s bluelut[256] =
{
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,
9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13,
13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14,
15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18,
18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20,
20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22,
22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24,
24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27,
27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29
};
static int e_mcu_color_convert(jpgstruct *jpg, int curxmcu, int curymcu)
{
Ipp8u __tmpbuf[16*16*3+CACHE_LINE-1];
Ipp8u *tmpbuf, *srcptr, *dstptr, *bmpptr=jpg->bmpptr;
Ipp16s *mcuptr[3];
IppStatus (__STDCALL *ccfct)(const Ipp8u *, int, Ipp16s *[3]);
int mcuw=_mcuw[jpg->subsamp], mcuh=_mcuh[jpg->subsamp], w, h, i, j;
IppiSize imgsize;
w=mcuw; h=mcuh;
if(curxmcu==jpg->xmcus-1 && jpg->width%mcuw!=0) w=jpg->width%mcuw;
if(curymcu==jpg->ymcus-1 && jpg->height%mcuh!=0) h=jpg->height%mcuh;
if(jpg->subsamp==TJ_GRAYSCALE)
{
int rindex=jpg->flags&TJ_BGR? 2:0, bindex=jpg->flags&TJ_BGR? 0:2;
unsigned char *tmpptr=bmpptr, *tmpptr2;
Ipp16s *dptr=jpg->mcubuf, *dptr2;
int pitch=jpg->pitch;
if(jpg->flags&TJ_BOTTOMUP) pitch=-pitch;
for(j=0; j<h; j++, dptr+=mcuw, tmpptr+=pitch)
{
for(i=0, dptr2=dptr, tmpptr2=tmpptr; i<w; i++, dptr2++,
tmpptr2+=jpg->ps)
{
*dptr2=redlut[tmpptr2[rindex]]+greenlut[tmpptr2[1]]
+bluelut[tmpptr2[bindex]];
}
}
if(w<mcuw)
{
dptr=&jpg->mcubuf[w-1];
for(j=0; j<h; j++, dptr+=mcuw)
for(i=w, dptr2=dptr+1; i<mcuw; i++, dptr2++)
*dptr2=*dptr;
}
if(h<mcuh)
{
dptr=&jpg->mcubuf[mcuw*(h-1)];
for(j=h, dptr2=dptr+mcuw; j<mcuh; j++, dptr2+=mcuw)
_ipp(ippsCopy_16s(dptr, dptr2, mcuw));
}
}
else
{
tmpbuf=(Ipp8u *)ippAlignPtr(__tmpbuf, CACHE_LINE);
switch(jpg->subsamp)
{
case TJ_411:
if(jpg->flags&TJ_BGR) ccfct=ippiBGRToYCbCr411LS_MCU_8u16s_C3P3R;
else ccfct=ippiRGBToYCbCr411LS_MCU_8u16s_C3P3R;
break;
case TJ_422:
if(jpg->flags&TJ_BGR) ccfct=ippiBGRToYCbCr422LS_MCU_8u16s_C3P3R;
else ccfct=ippiRGBToYCbCr422LS_MCU_8u16s_C3P3R;
break;
case TJ_444:
if(jpg->flags&TJ_BGR) ccfct=ippiBGRToYCbCr444LS_MCU_8u16s_C3P3R;
else ccfct=ippiRGBToYCbCr444LS_MCU_8u16s_C3P3R;
break;
default:
_throw("Invalid argument to mcu_color_convert()");
}
imgsize.width=w; imgsize.height=h;
if(jpg->flags&TJ_BOTTOMUP)
{
bmpptr=bmpptr-(h-1)*jpg->pitch;
if(jpg->ps==4)
{
_ipp(ippiCopy_8u_AC4C3R(bmpptr, jpg->pitch, tmpbuf, mcuw*3, imgsize));
if(h>1) {_ipp(ippiMirror_8u_C3IR(tmpbuf, mcuw*3, imgsize, ippAxsHorizontal));}
}
else
{
if(h>1) {_ipp(ippiMirror_8u_C3R(bmpptr, jpg->pitch, tmpbuf, mcuw*3, imgsize, ippAxsHorizontal));}
else {_ipp(ippiCopy_8u_C3R(bmpptr, jpg->pitch, tmpbuf, mcuw*3, imgsize));}
}
if(mcuw>w)
{
for(j=0; j<h; j++)
{
srcptr=&tmpbuf[j*mcuw*3];
for(i=w; i<mcuw; i++)
_ipp(ippsCopy_8u(&srcptr[(w-1)*3], &srcptr[i*3], 3));
}
}
}
else
{
if(jpg->ps==4)
{
_ipp(ippiCopy_8u_AC4C3R(bmpptr, jpg->pitch, tmpbuf, mcuw*3, imgsize));
}
else
{
_ipp(ippiCopy_8u_C3R(bmpptr, jpg->pitch, tmpbuf, mcuw*3, imgsize));
}
if(mcuw>w)
{
for(j=0; j<h; j++)
{
srcptr=bmpptr+j*jpg->pitch+(w-1)*jpg->ps;
dstptr=&tmpbuf[j*mcuw*3];
for(i=w; i<mcuw; i++)
_ipp(ippsCopy_8u(srcptr, &dstptr[i*3], 3));
}
}
}
if(mcuh>h)
{
srcptr=&tmpbuf[(h-1)*mcuw*3];
for(j=h; j<mcuh; j++)
{
dstptr=&tmpbuf[j*mcuw*3];
_ipp(ippsCopy_8u(srcptr, dstptr, mcuw*3));
}
}
mcuptr[0]=jpg->mcubuf;
mcuptr[1]=&jpg->mcubuf[mcuw*mcuh];
mcuptr[2]=&jpg->mcubuf[mcuw*mcuh+8*8];
_ipp(ccfct(tmpbuf, mcuw*3, mcuptr));
}
if(curxmcu==jpg->xmcus-1)
{
if(jpg->flags&TJ_BOTTOMUP) jpg->bmpptr=&jpg->bmpbuf[(jpg->height-1-mcuh*(curymcu+1))*jpg->pitch];
else jpg->bmpptr=&jpg->bmpbuf[mcuh*(curymcu+1)*jpg->pitch];
}
else jpg->bmpptr+=mcuw*jpg->ps;
return 0;
}
#define write_byte(j, b) {if(j->bytesleft<=0) _throw("Not enough space in buffer"); \
*j->jpgptr=b; j->jpgptr++; j->bytesprocessed++; j->bytesleft--;}
#define write_word(j, w) {write_byte(j, (w>>8)&0xff); write_byte(j, w&0xff);}
static int encode_jpeg_init(jpgstruct *jpg)
{
Ipp8u rawqtable[64]; int i, nval, len;
int mcuw=_mcuw[jpg->subsamp], mcuh=_mcuh[jpg->subsamp];
jpg->xmcus=(jpg->width+mcuw-1)/mcuw;
jpg->ymcus=(jpg->height+mcuh-1)/mcuh;
jpg->bytesprocessed=0;
if(jpg->flags&TJ_BOTTOMUP) jpg->bmpptr=&jpg->bmpbuf[jpg->pitch*(jpg->height-1)];
else jpg->bmpptr=jpg->bmpbuf;
jpg->jpgptr=jpg->jpgbuf;
_ipp(ippiEncodeHuffmanStateInit_JPEG_8u(jpg->e_huffstate));
write_byte(jpg, 0xff); write_byte(jpg, 0xd8); // Start of image marker
// JFIF header
write_byte(jpg, 0xff); write_byte(jpg, 0xe0); // JFIF marker
write_word(jpg, 16); // JFIF header length
write_byte(jpg, 'J'); write_byte(jpg, 'F');
write_byte(jpg, 'I'); write_byte(jpg, 'F'); write_byte(jpg, 0);
write_word(jpg, 0x0101); // JFIF version
write_byte(jpg, 0); // JFIF density units
write_word(jpg, 1); // JFIF X density
write_word(jpg, 1); // JFIF Y density
write_byte(jpg, 0); // thumbnail width
write_byte(jpg, 0); // thumbnail height
// Generate and write quant. tables
memcpy(rawqtable, lumqtable, 64);
_ipp(ippiQuantFwdRawTableInit_JPEG_8u(rawqtable, jpg->qual));
_ipp(ippiQuantFwdTableInit_JPEG_8u16u(rawqtable, jpg->lumqtable));
write_byte(jpg, 0xff); write_byte(jpg, 0xdb); // DQT marker
write_word(jpg, 67); // DQT length
write_byte(jpg, 0); // Index for luminance
for(i=0; i<64; i++) write_byte(jpg, rawqtable[i]);
if(jpg->subsamp!=TJ_GRAYSCALE)
{
memcpy(rawqtable, chromqtable, 64);
_ipp(ippiQuantFwdRawTableInit_JPEG_8u(rawqtable, jpg->qual));
_ipp(ippiQuantFwdTableInit_JPEG_8u16u(rawqtable, jpg->chromqtable));
write_byte(jpg, 0xff); write_byte(jpg, 0xdb); // DQT marker
write_word(jpg, 67); // DQT length
write_byte(jpg, 1); // Index for chrominance
for(i=0; i<64; i++) write_byte(jpg, rawqtable[i]);
}
// Write default Huffman tables
write_byte(jpg, 0xff); write_byte(jpg, 0xc4); // DHT marker
nval=0; for(i=0; i<16; i++) nval+=dclumbits[i];
len=19+nval;
write_word(jpg, len); // DHT length
write_byte(jpg, 0x00); // Huffman class
for(i=0; i<16; i++) write_byte(jpg, dclumbits[i]);
for(i=0; i<nval; i++) write_byte(jpg, dclumvals[i]);
write_byte(jpg, 0xff); write_byte(jpg, 0xc4); // DHT marker
nval=0; for(i=0; i<16; i++) nval+=aclumbits[i];
len=19+nval;
write_word(jpg, len); // DHT length
write_byte(jpg, 0x10); // Huffman class
for(i=0; i<16; i++) write_byte(jpg, aclumbits[i]);
for(i=0; i<nval; i++) write_byte(jpg, aclumvals[i]);
if(jpg->subsamp!=TJ_GRAYSCALE)
{
write_byte(jpg, 0xff); write_byte(jpg, 0xc4); // DHT marker
nval=0; for(i=0; i<16; i++) nval+=dcchrombits[i];
len=19+nval;
write_word(jpg, len); // DHT length
write_byte(jpg, 0x01); // Huffman class
for(i=0; i<16; i++) write_byte(jpg, dcchrombits[i]);
for(i=0; i<nval; i++) write_byte(jpg, dcchromvals[i]);
write_byte(jpg, 0xff); write_byte(jpg, 0xc4); // DHT marker
nval=0; for(i=0; i<16; i++) nval+=acchrombits[i];
len=19+nval;
write_word(jpg, len); // DHT length
write_byte(jpg, 0x11); // Huffman class
for(i=0; i<16; i++) write_byte(jpg, acchrombits[i]);
for(i=0; i<nval; i++) write_byte(jpg, acchromvals[i]);
}
// Initialize Huffman tables
_ipp(ippiEncodeHuffmanSpecInit_JPEG_8u(dclumbits, dclumvals, jpg->e_dclumtable));
_ipp(ippiEncodeHuffmanSpecInit_JPEG_8u(aclumbits, aclumvals, jpg->e_aclumtable));
if(jpg->subsamp!=TJ_GRAYSCALE)
{
_ipp(ippiEncodeHuffmanSpecInit_JPEG_8u(dcchrombits, dcchromvals, jpg->e_dcchromtable));
_ipp(ippiEncodeHuffmanSpecInit_JPEG_8u(acchrombits, acchromvals, jpg->e_acchromtable));
}
// Write Start Of Frame
write_byte(jpg, 0xff); write_byte(jpg, 0xc0); // SOF marker
write_word(jpg, (jpg->subsamp==TJ_GRAYSCALE? 11:17)); // SOF length
write_byte(jpg, 8); // precision
write_word(jpg, jpg->height);
write_word(jpg, jpg->width);
write_byte(jpg, (jpg->subsamp==TJ_GRAYSCALE? 1:3)); // Number of components
write_byte(jpg, 1); // Y Component ID
write_byte(jpg, ((mcuw/8)<<4)+(mcuh/8)); // Horiz. and Vert. sampling factors
write_byte(jpg, 0); // Quantization table selector
if(jpg->subsamp!=TJ_GRAYSCALE)
{
for(i=2; i<=3; i++)
{
write_byte(jpg, i); // Component ID
write_byte(jpg, 0x11); // Horiz. and Vert. sampling factors
write_byte(jpg, 1); // Quantization table selector
}
}
// Write Start of Scan
write_byte(jpg, 0xff); write_byte(jpg, 0xda); // SOS marker
write_word(jpg, (jpg->subsamp==TJ_GRAYSCALE? 8:12)); // SOS length
write_byte(jpg, (jpg->subsamp==TJ_GRAYSCALE? 1:3)); // Number of components
for(i=1; i<=(jpg->subsamp==TJ_GRAYSCALE? 1:3); i++)
{
write_byte(jpg, i); // Component ID
write_byte(jpg, i==1? 0 : 0x11); // Huffman table selector
}
write_byte(jpg, 0); // Spectral start
write_byte(jpg, 63); // Spectral end
write_byte(jpg, 0); // Successive approximation (N/A unless progressive)
return 0;
}
static int encode_jpeg(jpgstruct *jpg)
{
int i, j, k, pos;
int mcuw=_mcuw[jpg->subsamp], mcuh=_mcuh[jpg->subsamp];
int mcusize=mcuw*mcuh+(jpg->subsamp==TJ_GRAYSCALE? 0:128);
Ipp16s lastdc[3]={0, 0, 0};
_catch(encode_jpeg_init(jpg));
for(j=0; j<jpg->ymcus; j++)
{
for(i=0; i<jpg->xmcus; i++)
{
_catch(e_mcu_color_convert(jpg, i, j));
for(k=0; k<mcusize; k+=64) // Perform DCT on all 8x8 blocks
_ipp(ippiDCT8x8Fwd_16s_C1I(&jpg->mcubuf[k]));
for(k=0; k<mcuw*mcuh; k+=64) // Quantize the luminance blocks
_ipp(ippiQuantFwd8x8_JPEG_16s_C1I(&jpg->mcubuf[k], jpg->lumqtable));
if(jpg->subsamp!=TJ_GRAYSCALE)
{
for(k=mcuw*mcuh; k<mcusize; k+=64) // Quantize the chrominance blocks
_ipp(ippiQuantFwd8x8_JPEG_16s_C1I(&jpg->mcubuf[k], jpg->chromqtable));
}
for(k=0; k<mcuw*mcuh; k+=64) // Huffman encode the luminance blocks
{
pos=0;
_ipp(ippiEncodeHuffman8x8_JPEG_16s1u_C1(&jpg->mcubuf[k], jpg->jpgptr,
jpg->bytesleft, &pos, &lastdc[0], jpg->e_dclumtable, jpg->e_aclumtable,
jpg->e_huffstate, 0));
jpg->jpgptr+=pos; jpg->bytesprocessed+=pos; jpg->bytesleft-=pos;
}
if(jpg->subsamp!=TJ_GRAYSCALE)
{
// Huffman encode the Cb block
pos=0;
_ipp(ippiEncodeHuffman8x8_JPEG_16s1u_C1(&jpg->mcubuf[k], jpg->jpgptr,
jpg->bytesleft, &pos, &lastdc[1], jpg->e_dcchromtable, jpg->e_acchromtable,
jpg->e_huffstate, 0));
jpg->jpgptr+=pos; jpg->bytesprocessed+=pos; jpg->bytesleft-=pos;
k+=64;
// Huffman encode the Cr block
pos=0;
_ipp(ippiEncodeHuffman8x8_JPEG_16s1u_C1(&jpg->mcubuf[k], jpg->jpgptr,
jpg->bytesleft, &pos, &lastdc[2], jpg->e_dcchromtable, jpg->e_acchromtable,
jpg->e_huffstate, 0));
jpg->jpgptr+=pos; jpg->bytesprocessed+=pos; jpg->bytesleft-=pos;
}
} // xmcus
} // ymcus
// Flush Huffman state
pos=0;
_ipp(ippiEncodeHuffman8x8_JPEG_16s1u_C1(0, jpg->jpgptr, jpg->bytesleft, &pos,
0, jpg->e_dclumtable, jpg->e_aclumtable, jpg->e_huffstate, 1));
jpg->jpgptr+=pos; jpg->bytesprocessed+=pos; jpg->bytesleft-=pos;
write_byte(jpg, 0xff); write_byte(jpg, 0xd9); // EOI marker
return 0;
}
DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
{
jpgstruct *jpg; int huffbufsize;
if((jpg=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
{snprintf(lasterror, 1023, "%d: Memory allocation failure", __LINE__); return NULL;}
memset(jpg, 0, sizeof(jpgstruct));
if(!ippstaticinitcalled)
{
char *env=NULL;
jpg->cpu=ippCoreGetCpuType();
if(jpg->cpu==ippCpuUnknown) {jpg->cpu=AutoDetect(); ippStaticInitCpu(jpg->cpu);}
#if IPP_VERSION_MAJOR>=5
else if(jpg->cpu==ippCpuX8664) {jpg->cpu=AutoDetect(); ippStaticInitCpu(jpg->cpu);}
#endif
else ippStaticInitBest(); ippstaticinitcalled=1;
if((env=getenv("VGL_VERBOSE"))!=NULL && strlen(env)>0
&& !strncmp(env, "1", 1))
fprintf(stderr, "[TurboJPEG] Detected CPU type %d\n", jpg->cpu);
}
_ippn(ippiEncodeHuffmanStateGetBufSize_JPEG_8u(&huffbufsize));
if((jpg->e_huffstate=(IppiEncodeHuffmanState *)ippMalloc(huffbufsize))==NULL)
{snprintf(lasterror, 1023, "%d: Memory Allocation failure", __LINE__); return NULL;}
jpg->lumqtable=(Ipp16u *)ippAlignPtr(jpg->__lumqtable, 8);
jpg->chromqtable=(Ipp16u *)ippAlignPtr(jpg->__chromqtable, 8);
jpg->mcubuf=(Ipp16s *)ippAlignPtr(jpg->__mcubuf, CACHE_LINE);
_ippn(ippiEncodeHuffmanSpecGetBufSize_JPEG_8u(&huffbufsize));
if((jpg->e_dclumtable=(IppiEncodeHuffmanSpec *)ippMalloc(huffbufsize))==NULL
|| (jpg->e_aclumtable=(IppiEncodeHuffmanSpec *)ippMalloc(huffbufsize))==NULL
|| (jpg->e_dcchromtable=(IppiEncodeHuffmanSpec *)ippMalloc(huffbufsize))==NULL
|| (jpg->e_acchromtable=(IppiEncodeHuffmanSpec *)ippMalloc(huffbufsize))==NULL)
{snprintf(lasterror, 1023, "%d: Memory Allocation failure", __LINE__); return NULL;}
jpg->initc=1;
return (tjhandle)jpg;
}
DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
{
// This allows enough room in case the image doesn't compress
return ((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
}
DLLEXPORT int DLLCALL tjCompress(tjhandle h,
unsigned char *srcbuf, int width, int pitch, int height, int ps,
unsigned char *dstbuf, unsigned long *size,
int jpegsub, int qual, int flags)
{
checkhandle(h);
if(srcbuf==NULL || width<=0 || height<=0
|| dstbuf==NULL || size==NULL
|| jpegsub<0 || jpegsub>=NUMSUBOPT || qual<0 || qual>100)
_throw("Invalid argument in tjCompress()");
if(qual<1) qual=1;
if(!jpg->initc) _throw("Instance has not been initialized for compression");
if(ps!=3 && ps!=4) _throw("This JPEG codec supports only 24-bit or 32-bit true color");
if(pitch==0) pitch=width*ps;
if(flags&TJ_FORCEMMX) forcecpu(ippCpuPII);
if(flags&TJ_FORCESSE) forcecpu(ippCpuPIII);
if(flags&TJ_FORCESSE2) forcecpu(ippCpuP4);
if(flags&TJ_FORCESSE3) forcecpu(ippCpuEM64T);
if(flags&TJ_ALPHAFIRST) jpg->bmpbuf=&srcbuf[1];
else jpg->bmpbuf=srcbuf;
jpg->jpgbuf=dstbuf;
jpg->width=width; jpg->height=height; jpg->pitch=pitch;
jpg->ps=ps; jpg->subsamp=jpegsub; jpg->qual=qual; jpg->flags=flags;
jpg->bytesleft=TJBUFSIZE(width, height);
_catch(encode_jpeg(jpg));
*size=jpg->bytesprocessed;
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// DECOMPRESSOR
//////////////////////////////////////////////////////////////////////////////
#define read_byte(j, b) {if(j->bytesleft<=0) _throw("Unexpected end of image"); \
b=*j->jpgptr; j->jpgptr++; j->bytesleft--; j->bytesprocessed++;}
#define read_word(j, w) {Ipp8u __b; read_byte(j, __b); w=(__b<<8); \
read_byte(j, __b); w|=(__b&0xff);}
static int find_marker(jpgstruct *jpg, unsigned char *marker)
{
unsigned char b;
while(1)
{
do {read_byte(jpg, b);} while(b!=0xff);
read_byte(jpg, b);
if(b!=0 && b!=0xff) {*marker=b; return 0;}
else {jpg->jpgptr--; jpg->bytesleft++; jpg->bytesprocessed--;}
}
return 0;
}
#define check_byte(j, b) {unsigned char __b; read_byte(j, __b); \
if(__b!=(b)) _throw("JPEG bitstream error");}
static int d_mcu_color_convert(jpgstruct *jpg, int curxmcu, int curymcu)
{
Ipp8u __tmpbuf[16*16*3+CACHE_LINE-1];
Ipp8u *tmpbuf, *bmpptr=jpg->bmpptr;
const Ipp16s *mcuptr[3];
IppStatus (__STDCALL *ccfct)(const Ipp16s *[3], Ipp8u *, int);
int mcuw=_mcuw[jpg->subsamp], mcuh=_mcuh[jpg->subsamp], w, h;
IppiSize imgsize;
tmpbuf=(Ipp8u *)ippAlignPtr(__tmpbuf, CACHE_LINE);
w=mcuw; h=mcuh;
if(curxmcu==jpg->xmcus-1 && jpg->width%mcuw!=0) w=jpg->width%mcuw;
if(curymcu==jpg->ymcus-1 && jpg->height%mcuh!=0) h=jpg->height%mcuh;
imgsize.width=w; imgsize.height=h;
if(jpg->subsamp==TJ_GRAYSCALE)
{
int pitch=jpg->pitch, i, j;
if(jpg->flags&TJ_BOTTOMUP) pitch=-pitch;
_ipp(ippiAdd128_JPEG_16s8u_C1R(jpg->mcubuf, mcuw*2, tmpbuf, mcuw, imgsize));
if(jpg->ps==3)
{
_ipp(ippiDup_8u_C1C3R(tmpbuf, mcuw, bmpptr, pitch, imgsize));
}
else
{
Ipp8u *sptr=tmpbuf, *sptr2, *tmpptr=bmpptr;
Ipp32u *tmpptr2;
for(j=0; j<h; j++, sptr+=mcuw, tmpptr+=pitch)
{
for(i=0, sptr2=sptr, tmpptr2=(Ipp32u *)tmpptr; i<w; i++, sptr2++,
tmpptr2++)
{
*tmpptr2=(*sptr2)*0x010101;
}
}
}
}
else
{
switch(jpg->subsamp)
{
case TJ_411:
if(jpg->flags&TJ_BGR) ccfct=ippiYCbCr411ToBGRLS_MCU_16s8u_P3C3R;
else ccfct=ippiYCbCr411ToRGBLS_MCU_16s8u_P3C3R;
break;
case TJ_422:
if(jpg->flags&TJ_BGR) ccfct=ippiYCbCr422ToBGRLS_MCU_16s8u_P3C3R;
else ccfct=ippiYCbCr422ToRGBLS_MCU_16s8u_P3C3R;
break;
case TJ_444:
if(jpg->flags&TJ_BGR) ccfct=ippiYCbCr444ToBGRLS_MCU_16s8u_P3C3R;
else ccfct=ippiYCbCr444ToRGBLS_MCU_16s8u_P3C3R;
break;
default:
_throw("Invalid argument to mcu_color_convert()");
}
mcuptr[0]=jpg->mcubuf;
mcuptr[1]=&jpg->mcubuf[mcuw*mcuh];
mcuptr[2]=&jpg->mcubuf[mcuw*mcuh+8*8];
_ipp(ccfct(mcuptr, tmpbuf, mcuw*3));
if(jpg->flags&TJ_BOTTOMUP)
{
bmpptr=bmpptr-(h-1)*jpg->pitch;
if(jpg->ps==4)
{
if(h>1) {_ipp(ippiMirror_8u_C3IR(tmpbuf, mcuw*3, imgsize, ippAxsHorizontal));}
_ipp(ippiCopy_8u_C3AC4R(tmpbuf, mcuw*3, bmpptr, jpg->pitch, imgsize));
}
else
{
if(h>1) {_ipp(ippiMirror_8u_C3R(tmpbuf, mcuw*3, bmpptr, jpg->pitch, imgsize, ippAxsHorizontal));}
else {_ipp(ippiCopy_8u_C3R(tmpbuf, mcuw*3, bmpptr, jpg->pitch, imgsize));}
}
}
else
{
if(jpg->ps==4)
{
_ipp(ippiCopy_8u_C3AC4R(tmpbuf, mcuw*3, bmpptr, jpg->pitch, imgsize));
}
else
{
_ipp(ippiCopy_8u_C3R(tmpbuf, mcuw*3, bmpptr, jpg->pitch, imgsize));
}
}
}
if(curxmcu==jpg->xmcus-1)
{
if(jpg->flags&TJ_BOTTOMUP) jpg->bmpptr=&jpg->bmpbuf[(jpg->height-1-mcuh*(curymcu+1))*jpg->pitch];
else jpg->bmpptr=&jpg->bmpbuf[mcuh*(curymcu+1)*jpg->pitch];
}
else jpg->bmpptr+=mcuw*jpg->ps;
return 0;
}
static int decode_jpeg_init(jpgstruct *jpg)
{
Ipp8u rawqtable[64], rawhuffbits[16], rawhuffvalues[256];
int i; unsigned char tempbyte, marker; unsigned short tempword, length;
int markerread=0; unsigned char compid[3], ncomp;
jpg->bytesprocessed=0;
jpg->jpgptr=jpg->jpgbuf;
check_byte(jpg, 0xff); check_byte(jpg, 0xd8); // SOI
while(1)
{
_catch(find_marker(jpg, &marker));
switch(marker)
{
case 0xe0: // JFIF
read_word(jpg, length);
if(length<8) _throw("JPEG bitstream error");
check_byte(jpg, 'J'); check_byte(jpg, 'F');
check_byte(jpg, 'I'); check_byte(jpg, 'F'); check_byte(jpg, 0);
for(i=7; i<length; i++) read_byte(jpg, tempbyte) // We don't care about the rest
markerread+=1;
break;
case 0xdb: // DQT
{
int dqtbytecount;
read_word(jpg, length);
if(length<67 || length>(64+1)*2+2) _throw("JPEG bitstream error");
dqtbytecount=2;
while(dqtbytecount<length)
{
read_byte(jpg, tempbyte); // Quant. table index
dqtbytecount++;
for(i=0; i<64; i++) read_byte(jpg, rawqtable[i]);
dqtbytecount+=64;
if(tempbyte==0)
{
_ipp(ippiQuantInvTableInit_JPEG_8u16u(rawqtable, jpg->lumqtable));
markerread+=2;
}
else if(tempbyte==1)
{
_ipp(ippiQuantInvTableInit_JPEG_8u16u(rawqtable, jpg->chromqtable));
markerread+=4;
}
}
break;
}
case 0xc4: // DHT
{
int nval, dhtbytecount;
read_word(jpg, length);
if(length<19 || length>(17+256)*4+2) _throw("JPEG bitstream error");
dhtbytecount=2;
while(dhtbytecount<length)
{
read_byte(jpg, tempbyte); // Huffman class
dhtbytecount++;
_ipp(ippsZero_8u(rawhuffbits, 16));
_ipp(ippsZero_8u(rawhuffvalues, 256));
nval=0;
for(i=0; i<16; i++) {read_byte(jpg, rawhuffbits[i]); nval+=rawhuffbits[i];}
dhtbytecount+=16;
if(nval>256) _throw("JPEG bitstream error");
for(i=0; i<nval; i++) read_byte(jpg, rawhuffvalues[i]);
dhtbytecount+=nval;
if(tempbyte==0x00) // DC luminance
{
_ipp(ippiDecodeHuffmanSpecInit_JPEG_8u(rawhuffbits, rawhuffvalues,
jpg->d_dclumtable));
markerread+=8;
}
else if(tempbyte==0x10) // AC luminance
{
_ipp(ippiDecodeHuffmanSpecInit_JPEG_8u(rawhuffbits, rawhuffvalues,
jpg->d_aclumtable));
markerread+=16;
}
else if(tempbyte==0x01) // DC chrominance
{
_ipp(ippiDecodeHuffmanSpecInit_JPEG_8u(rawhuffbits, rawhuffvalues,
jpg->d_dcchromtable));
markerread+=32;
}
else if(tempbyte==0x11) // AC chrominance
{
_ipp(ippiDecodeHuffmanSpecInit_JPEG_8u(rawhuffbits, rawhuffvalues,
jpg->d_acchromtable));
markerread+=64;
}
}
break;
}
case 0xc0: // SOF