-
Notifications
You must be signed in to change notification settings - Fork 0
/
picojpeg.c
executable file
·2316 lines (1968 loc) · 59.1 KB
/
picojpeg.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
//------------------------------------------------------------------------------
// picojpeg.c v1.1 - Public domain, Rich Geldreich <[email protected]>
// Nov. 27, 2010 - Initial release
// Feb. 9, 2013 - Added H1V2/H2V1 support, cleaned up macros, signed shift fixes
// Also integrated and tested changes from Chris Phoenix <[email protected]>.
//------------------------------------------------------------------------------
#include "picojpeg.h"
//------------------------------------------------------------------------------
// Set to 1 if right shifts on signed ints are always unsigned (logical) shifts
// When 1, arithmetic right shifts will be emulated by using a logical shift
// with special case code to ensure the sign bit is replicated.
#define PJPG_RIGHT_SHIFT_IS_ALWAYS_UNSIGNED 0
// Define PJPG_INLINE to "inline" if your C compiler supports explicit inlining
#define PJPG_INLINE
//------------------------------------------------------------------------------
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef signed char int8;
typedef signed short int16;
//------------------------------------------------------------------------------
#if PJPG_RIGHT_SHIFT_IS_ALWAYS_UNSIGNED
static int16 replicateSignBit16(int8 n)
{
switch (n)
{
case 0: return 0x0000;
case 1: return 0x8000;
case 2: return 0xC000;
case 3: return 0xE000;
case 4: return 0xF000;
case 5: return 0xF800;
case 6: return 0xFC00;
case 7: return 0xFE00;
case 8: return 0xFF00;
case 9: return 0xFF80;
case 10: return 0xFFC0;
case 11: return 0xFFE0;
case 12: return 0xFFF0;
case 13: return 0xFFF8;
case 14: return 0xFFFC;
case 15: return 0xFFFE;
default: return 0xFFFF;
}
}
static PJPG_INLINE int16 arithmeticRightShiftN16(int16 x, int8 n)
{
int16 r = (uint16)x >> (uint8)n;
if (x < 0)
r |= replicateSignBit16(n);
return r;
}
static PJPG_INLINE long arithmeticRightShift8L(long x)
{
long r = (unsigned long)x >> 8U;
if (x < 0)
r |= ~(~(unsigned long)0U >> 8U);
return r;
}
#define PJPG_ARITH_SHIFT_RIGHT_N_16(x, n) arithmeticRightShiftN16(x, n)
#define PJPG_ARITH_SHIFT_RIGHT_8_L(x) arithmeticRightShift8L(x)
#else
#define PJPG_ARITH_SHIFT_RIGHT_N_16(x, n) ((x) >> (n))
#define PJPG_ARITH_SHIFT_RIGHT_8_L(x) ((x) >> 8)
#endif
//------------------------------------------------------------------------------
// Change as needed - the PJPG_MAX_WIDTH/PJPG_MAX_HEIGHT checks are only present
// to quickly detect bogus files.
#define PJPG_MAX_WIDTH 2560//16384
#define PJPG_MAX_HEIGHT 2048//16384
#define PJPG_MAXCOMPSINSCAN 3
//------------------------------------------------------------------------------
typedef enum
{
M_SOF0 = 0xC0,
M_SOF1 = 0xC1,
M_SOF2 = 0xC2,
M_SOF3 = 0xC3,
M_SOF5 = 0xC5,
M_SOF6 = 0xC6,
M_SOF7 = 0xC7,
M_JPG = 0xC8,
M_SOF9 = 0xC9,
M_SOF10 = 0xCA,
M_SOF11 = 0xCB,
M_SOF13 = 0xCD,
M_SOF14 = 0xCE,
M_SOF15 = 0xCF,
M_DHT = 0xC4,
M_DAC = 0xCC,
M_RST0 = 0xD0,
M_RST1 = 0xD1,
M_RST2 = 0xD2,
M_RST3 = 0xD3,
M_RST4 = 0xD4,
M_RST5 = 0xD5,
M_RST6 = 0xD6,
M_RST7 = 0xD7,
M_SOI = 0xD8,
M_EOI = 0xD9,
M_SOS = 0xDA,
M_DQT = 0xDB,
M_DNL = 0xDC,
M_DRI = 0xDD,
M_DHP = 0xDE,
M_EXP = 0xDF,
M_APP0 = 0xE0,
M_APP15 = 0xEF,
M_JPG0 = 0xF0,
M_JPG13 = 0xFD,
M_COM = 0xFE,
M_TEM = 0x01,
M_ERROR = 0x100,
RST0 = 0xD0
} JPEG_MARKER;
//------------------------------------------------------------------------------
static const int8 ZAG[] =
{
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,
};
//------------------------------------------------------------------------------
// 128 bytes
static int16 gCoeffBuf[8*8];
// 8*8*4 bytes * 3 = 768
static uint8 gMCUBufR[256];
static uint8 gMCUBufG[256];
static uint8 gMCUBufB[256];
// 256 bytes
static int16 gQuant0[8*8];
static int16 gQuant1[8*8];
// 6 bytes
static int16 gLastDC[3];
typedef struct HuffTableT
{
uint16 mMinCode[16];
uint16 mMaxCode[16];
uint8 mValPtr[16];
} HuffTable;
// DC - 192
static HuffTable gHuffTab0;
static uint8 gHuffVal0[16];
static HuffTable gHuffTab1;
static uint8 gHuffVal1[16];
// AC - 672
static HuffTable gHuffTab2;
static uint8 gHuffVal2[256];
static HuffTable gHuffTab3;
static uint8 gHuffVal3[256];
static uint8 gValidHuffTables;
static uint8 gValidQuantTables;
static uint8 gTemFlag;
#define PJPG_MAX_IN_BUF_SIZE 256
static uint8 gInBuf[PJPG_MAX_IN_BUF_SIZE];
static uint8 gInBufOfs;
static uint8 gInBufLeft;
static uint16 gBitBuf;
static uint8 gBitsLeft;
//------------------------------------------------------------------------------
static uint16 gImageXSize;
static uint16 gImageYSize;
static uint8 gCompsInFrame;
static uint8 gCompIdent[3];
static uint8 gCompHSamp[3];
static uint8 gCompVSamp[3];
static uint8 gCompQuant[3];
static uint16 gRestartInterval;
static uint16 gNextRestartNum;
static uint16 gRestartsLeft;
static uint8 gCompsInScan;
static uint8 gCompList[3];
static uint8 gCompDCTab[3]; // 0,1
static uint8 gCompACTab[3]; // 0,1
static pjpeg_scan_type_t gScanType;
static uint8 gMaxBlocksPerMCU;
static uint8 gMaxMCUXSize;
static uint8 gMaxMCUYSize;
static uint16 gMaxMCUSPerRow;
static uint16 gMaxMCUSPerCol;
static uint16 gNumMCUSRemaining;
static uint8 gMCUOrg[6];
static pjpeg_need_bytes_callback_t g_pNeedBytesCallback;
static void *g_pCallback_data;
static uint8 gCallbackStatus;
static uint8 gReduce;
//------------------------------------------------------------------------------
static void fillInBuf(void)
{
unsigned char status;
// Reserve a few bytes at the beginning of the buffer for putting back ("stuffing") chars.
gInBufOfs = 4;
gInBufLeft = 0;
status = (*g_pNeedBytesCallback)(gInBuf + gInBufOfs, PJPG_MAX_IN_BUF_SIZE - gInBufOfs, &gInBufLeft, g_pCallback_data);
if (status)
{
// The user provided need bytes callback has indicated an error, so record the error and continue trying to decode.
// The highest level pjpeg entrypoints will catch the error and return the non-zero status.
gCallbackStatus = status;
}
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint8 getChar(void)
{
if (!gInBufLeft)
{
fillInBuf();
if (!gInBufLeft)
{
gTemFlag = ~gTemFlag;
return gTemFlag ? 0xFF : 0xD9;
}
}
gInBufLeft--;
return gInBuf[gInBufOfs++];
}
//------------------------------------------------------------------------------
static PJPG_INLINE void stuffChar(uint8 i)
{
gInBufOfs--;
gInBuf[gInBufOfs] = i;
gInBufLeft++;
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint8 getOctet(uint8 FFCheck)
{
uint8 c = getChar();
if ((FFCheck) && (c == 0xFF))
{
uint8 n = getChar();
if (n)
{
stuffChar(n);
stuffChar(0xFF);
}
}
return c;
}
//------------------------------------------------------------------------------
static uint16 getBits(uint8 numBits, uint8 FFCheck)
{
uint8 origBits = numBits;
uint16 ret = gBitBuf;
if (numBits > 8)
{
numBits -= 8;
gBitBuf <<= gBitsLeft;
gBitBuf |= getOctet(FFCheck);
gBitBuf <<= (8 - gBitsLeft);
ret = (ret & 0xFF00) | (gBitBuf >> 8);
}
if (gBitsLeft < numBits)
{
gBitBuf <<= gBitsLeft;
gBitBuf |= getOctet(FFCheck);
gBitBuf <<= (numBits - gBitsLeft);
gBitsLeft = 8 - (numBits - gBitsLeft);
}
else
{
gBitsLeft = (uint8)(gBitsLeft - numBits);
gBitBuf <<= numBits;
}
return ret >> (16 - origBits);
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint16 getBits1(uint8 numBits)
{
return getBits(numBits, 0);
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint16 getBits2(uint8 numBits)
{
return getBits(numBits, 1);
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint8 getBit(void)
{
uint8 ret = 0;
if (gBitBuf & 0x8000)
ret = 1;
if (!gBitsLeft)
{
gBitBuf |= getOctet(1);
gBitsLeft += 8;
}
gBitsLeft--;
gBitBuf <<= 1;
return ret;
}
//------------------------------------------------------------------------------
static uint16 getExtendTest(uint8 i)
{
switch (i)
{
case 0: return 0;
case 1: return 0x0001;
case 2: return 0x0002;
case 3: return 0x0004;
case 4: return 0x0008;
case 5: return 0x0010;
case 6: return 0x0020;
case 7: return 0x0040;
case 8: return 0x0080;
case 9: return 0x0100;
case 10: return 0x0200;
case 11: return 0x0400;
case 12: return 0x0800;
case 13: return 0x1000;
case 14: return 0x2000;
case 15: return 0x4000;
default: return 0;
}
}
//------------------------------------------------------------------------------
static int16 getExtendOffset(uint8 i)
{
switch (i)
{
case 0: return 0;
case 1: return ((-1)<<1) + 1;
case 2: return ((-1)<<2) + 1;
case 3: return ((-1)<<3) + 1;
case 4: return ((-1)<<4) + 1;
case 5: return ((-1)<<5) + 1;
case 6: return ((-1)<<6) + 1;
case 7: return ((-1)<<7) + 1;
case 8: return ((-1)<<8) + 1;
case 9: return ((-1)<<9) + 1;
case 10: return ((-1)<<10) + 1;
case 11: return ((-1)<<11) + 1;
case 12: return ((-1)<<12) + 1;
case 13: return ((-1)<<13) + 1;
case 14: return ((-1)<<14) + 1;
case 15: return ((-1)<<15) + 1;
default: return 0;
}
};
//------------------------------------------------------------------------------
static PJPG_INLINE int16 huffExtend(uint16 x, uint8 s)
{
return ((x < getExtendTest(s)) ? ((int16)x + getExtendOffset(s)) : (int16)x);
}
//------------------------------------------------------------------------------
static PJPG_INLINE uint8 huffDecode(const HuffTable* pHuffTable, const uint8* pHuffVal)
{
uint8 i = 0;
uint8 j;
uint16 code = getBit();
// This func only reads a bit at a time, which on modern CPU's is not terribly efficient.
// But on microcontrollers without strong integer shifting support this seems like a
// more reasonable approach.
for ( ; ; )
{
uint16 maxCode;
if (i == 16)
return 0;
maxCode = pHuffTable->mMaxCode[i];
if ((code <= maxCode) && (maxCode != 0xFFFF))
break;
i++;
code <<= 1;
code |= getBit();
}
j = pHuffTable->mValPtr[i];
j = (uint8)(j + (code - pHuffTable->mMinCode[i]));
return pHuffVal[j];
}
//------------------------------------------------------------------------------
static void huffCreate(const uint8* pBits, HuffTable* pHuffTable)
{
uint8 i = 0;
uint8 j = 0;
uint16 code = 0;
for ( ; ; )
{
uint8 num = pBits[i];
if (!num)
{
pHuffTable->mMinCode[i] = 0x0000;
pHuffTable->mMaxCode[i] = 0xFFFF;
pHuffTable->mValPtr[i] = 0;
}
else
{
pHuffTable->mMinCode[i] = code;
pHuffTable->mMaxCode[i] = code + num - 1;
pHuffTable->mValPtr[i] = j;
j = (uint8)(j + num);
code = (uint16)(code + num);
}
code <<= 1;
i++;
if (i > 15)
break;
}
}
//------------------------------------------------------------------------------
static HuffTable* getHuffTable(uint8 index)
{
// 0-1 = DC
// 2-3 = AC
switch (index)
{
case 0: return &gHuffTab0;
case 1: return &gHuffTab1;
case 2: return &gHuffTab2;
case 3: return &gHuffTab3;
default: return 0;
}
}
//------------------------------------------------------------------------------
static uint8* getHuffVal(uint8 index)
{
// 0-1 = DC
// 2-3 = AC
switch (index)
{
case 0: return gHuffVal0;
case 1: return gHuffVal1;
case 2: return gHuffVal2;
case 3: return gHuffVal3;
default: return 0;
}
}
//------------------------------------------------------------------------------
static uint16 getMaxHuffCodes(uint8 index)
{
return (index < 2) ? 12 : 255;
}
//------------------------------------------------------------------------------
static uint8 readDHTMarker(void)
{
uint8 bits[16];
uint16 left = getBits1(16);
if (left < 2)
return PJPG_BAD_DHT_MARKER;
left -= 2;
while (left)
{
uint8 i, tableIndex, index;
uint8* pHuffVal;
HuffTable* pHuffTable;
uint16 count, totalRead;
index = (uint8)getBits1(8);
if ( ((index & 0xF) > 1) || ((index & 0xF0) > 0x10) )
return PJPG_BAD_DHT_INDEX;
tableIndex = ((index >> 3) & 2) + (index & 1);
pHuffTable = getHuffTable(tableIndex);
pHuffVal = getHuffVal(tableIndex);
gValidHuffTables |= (1 << tableIndex);
count = 0;
for (i = 0; i <= 15; i++)
{
uint8 n = (uint8)getBits1(8);
bits[i] = n;
count = (uint16)(count + n);
}
if (count > getMaxHuffCodes(tableIndex))
return PJPG_BAD_DHT_COUNTS;
for (i = 0; i < count; i++)
pHuffVal[i] = (uint8)getBits1(8);
totalRead = 1 + 16 + count;
if (left < totalRead)
return PJPG_BAD_DHT_MARKER;
left = (uint16)(left - totalRead);
huffCreate(bits, pHuffTable);
}
return 0;
}
//------------------------------------------------------------------------------
static void createWinogradQuant(int16* pQuant);
static uint8 readDQTMarker(void)
{
uint16 left = getBits1(16);
if (left < 2)
return PJPG_BAD_DQT_MARKER;
left -= 2;
while (left)
{
uint8 i;
uint8 n = (uint8)getBits1(8);
uint8 prec = n >> 4;
uint16 totalRead;
n &= 0x0F;
if (n > 1)
return PJPG_BAD_DQT_TABLE;
gValidQuantTables |= (n ? 2 : 1);
// read quantization entries, in zag order
for (i = 0; i < 64; i++)
{
uint16 temp = getBits1(8);
if (prec)
temp = (temp << 8) + getBits1(8);
if (n)
gQuant1[i] = (int16)temp;
else
gQuant0[i] = (int16)temp;
}
createWinogradQuant(n ? gQuant1 : gQuant0);
totalRead = 64 + 1;
if (prec)
totalRead += 64;
if (left < totalRead)
return PJPG_BAD_DQT_LENGTH;
left = (uint16)(left - totalRead);
}
return 0;
}
//------------------------------------------------------------------------------
static uint8 readSOFMarker(void)
{
uint8 i;
uint16 left = getBits1(16);
if (getBits1(8) != 8)
return PJPG_BAD_PRECISION;
gImageYSize = getBits1(16);
if ((!gImageYSize) || (gImageYSize > PJPG_MAX_HEIGHT))
return PJPG_BAD_HEIGHT;
gImageXSize = getBits1(16);
if ((!gImageXSize) || (gImageXSize > PJPG_MAX_WIDTH))
return PJPG_BAD_WIDTH;
gCompsInFrame = (uint8)getBits1(8);
if (gCompsInFrame > 3)
return PJPG_TOO_MANY_COMPONENTS;
if (left != (gCompsInFrame + gCompsInFrame + gCompsInFrame + 8))
return PJPG_BAD_SOF_LENGTH;
for (i = 0; i < gCompsInFrame; i++)
{
gCompIdent[i] = (uint8)getBits1(8);
gCompHSamp[i] = (uint8)getBits1(4);
gCompVSamp[i] = (uint8)getBits1(4);
gCompQuant[i] = (uint8)getBits1(8);
if (gCompQuant[i] > 1)
return PJPG_UNSUPPORTED_QUANT_TABLE;
}
return 0;
}
//------------------------------------------------------------------------------
// Used to skip unrecognized markers.
static uint8 skipVariableMarker(void)
{
uint16 left = getBits1(16);
if (left < 2)
return PJPG_BAD_VARIABLE_MARKER;
left -= 2;
while (left)
{
getBits1(8);
left--;
}
return 0;
}
//------------------------------------------------------------------------------
// Read a define restart interval (DRI) marker.
static uint8 readDRIMarker(void)
{
if (getBits1(16) != 4)
return PJPG_BAD_DRI_LENGTH;
gRestartInterval = getBits1(16);
return 0;
}
//------------------------------------------------------------------------------
// Read a start of scan (SOS) marker.
static uint8 readSOSMarker(void)
{
uint8 i;
uint16 left = getBits1(16);
uint8 spectral_start, spectral_end, successive_high, successive_low;
gCompsInScan = (uint8)getBits1(8);
left -= 3;
if ( (left != (gCompsInScan + gCompsInScan + 3)) || (gCompsInScan < 1) || (gCompsInScan > PJPG_MAXCOMPSINSCAN) )
return PJPG_BAD_SOS_LENGTH;
for (i = 0; i < gCompsInScan; i++)
{
uint8 cc = (uint8)getBits1(8);
uint8 c = (uint8)getBits1(8);
uint8 ci;
left -= 2;
for (ci = 0; ci < gCompsInFrame; ci++)
if (cc == gCompIdent[ci])
break;
if (ci >= gCompsInFrame)
return PJPG_BAD_SOS_COMP_ID;
gCompList[i] = ci;
gCompDCTab[ci] = (c >> 4) & 15;
gCompACTab[ci] = (c & 15);
}
spectral_start = (uint8)getBits1(8);
spectral_end = (uint8)getBits1(8);
successive_high = (uint8)getBits1(4);
successive_low = (uint8)getBits1(4);
left -= 3;
while (left)
{
getBits1(8);
left--;
}
return 0;
}
//------------------------------------------------------------------------------
static uint8 nextMarker(void)
{
uint8 c;
uint8 bytes = 0;
do
{
do
{
bytes++;
c = (uint8)getBits1(8);
} while (c != 0xFF);
do
{
c = (uint8)getBits1(8);
} while (c == 0xFF);
} while (c == 0);
// If bytes > 0 here, there where extra bytes before the marker (not good).
return c;
}
//------------------------------------------------------------------------------
// Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
// encountered.
static uint8 processMarkers(uint8* pMarker)
{
for ( ; ; )
{
uint8 c = nextMarker();
switch (c)
{
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
// case M_JPG:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
case M_SOI:
case M_EOI:
case M_SOS:
{
*pMarker = c;
return 0;
}
case M_DHT:
{
readDHTMarker();
break;
}
// Sorry, no arithmetic support at this time. Dumb patents!
case M_DAC:
{
return PJPG_NO_ARITHMITIC_SUPPORT;
}
case M_DQT:
{
readDQTMarker();
break;
}
case M_DRI:
{
readDRIMarker();
break;
}
//case M_APP0: /* no need to read the JFIF marker */
case M_JPG:
case M_RST0: /* no parameters */
case M_RST1:
case M_RST2:
case M_RST3:
case M_RST4:
case M_RST5:
case M_RST6:
case M_RST7:
case M_TEM:
{
return PJPG_UNEXPECTED_MARKER;
}
default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
{
skipVariableMarker();
break;
}
}
}
// return 0;
}
//------------------------------------------------------------------------------
// Finds the start of image (SOI) marker.
static uint8 locateSOIMarker(void)
{
uint16 bytesleft;
uint8 lastchar = (uint8)getBits1(8);
uint8 thischar = (uint8)getBits1(8);
/* ok if it's a normal JPEG file without a special header */
if ((lastchar == 0xFF) && (thischar == M_SOI))
return 0;
bytesleft = 4096; //512;
// extern uint32_t g_pInFileSize;
// bytesleft = g_pInFileSize;
for ( ; ; )
{
if (--bytesleft == 0)
return PJPG_NOT_JPEG;
lastchar = thischar;
thischar = (uint8)getBits1(8);
if (lastchar == 0xFF)
{
if (thischar == M_SOI)
break;
else if (thischar == M_EOI) //getBits1 will keep returning M_EOI if we read past the end
return PJPG_NOT_JPEG;
}
}
/* Check the next character after marker: if it's not 0xFF, it can't
be the start of the next marker, so the file is bad */
thischar = (uint8)((gBitBuf >> 8) & 0xFF);
if (thischar != 0xFF)
return PJPG_NOT_JPEG;
return 0;
}
//------------------------------------------------------------------------------
// Find a start of frame (SOF) marker.
static uint8 locateSOFMarker(void)
{
uint8 c;
uint8 status = locateSOIMarker();
if (status)
return status;
status = processMarkers(&c);
if (status)
return status;
switch (c)
{
case M_SOF2:
{
// Progressive JPEG - not supported by picojpeg (would require too
// much memory, or too many IDCT's for embedded systems).
return PJPG_UNSUPPORTED_MODE;
}
case M_SOF0: /* baseline DCT */
{
status = readSOFMarker();
if (status)
return status;
break;
}
case M_SOF9:
{
return PJPG_NO_ARITHMITIC_SUPPORT;
}
case M_SOF1: /* extended sequential DCT */
default:
{
return PJPG_UNSUPPORTED_MARKER;
}
}
return 0;
}
//------------------------------------------------------------------------------
// Find a start of scan (SOS) marker.
static uint8 locateSOSMarker(uint8* pFoundEOI)
{
uint8 c;
uint8 status;
*pFoundEOI = 0;
status = processMarkers(&c);
if (status)
return status;
if (c == M_EOI)
{
*pFoundEOI = 1;
return 0;
}
else if (c != M_SOS)
return PJPG_UNEXPECTED_MARKER;
return readSOSMarker();
}
//------------------------------------------------------------------------------
static uint8 init(void)
{
gImageXSize = 0;
gImageYSize = 0;
gCompsInFrame = 0;
gRestartInterval = 0;
gCompsInScan = 0;
gValidHuffTables = 0;
gValidQuantTables = 0;
gTemFlag = 0;
gInBufOfs = 0;
gInBufLeft = 0;
gBitBuf = 0;
gBitsLeft = 8;
getBits1(8);
getBits1(8);
return 0;
}
//------------------------------------------------------------------------------
// This method throws back into the stream any bytes that where read
// into the bit buffer during initial marker scanning.
static void fixInBuffer(void)
{
/* In case any 0xFF's where pulled into the buffer during marker scanning */
if (gBitsLeft > 0)
stuffChar((uint8)gBitBuf);
stuffChar((uint8)(gBitBuf >> 8));
gBitsLeft = 8;
getBits2(8);
getBits2(8);
}
//------------------------------------------------------------------------------
// Restart interval processing.
static uint8 processRestart(void)
{
// Let's scan a little bit to find the marker, but not _too_ far.
// 1536 is a "fudge factor" that determines how much to scan.
uint16 i;
uint8 c = 0;
for (i = 1536; i > 0; i--)
if (getChar() == 0xFF)
break;
if (i == 0)
return PJPG_BAD_RESTART_MARKER;
for ( ; i > 0; i--)
if ((c = getChar()) != 0xFF)