-
Notifications
You must be signed in to change notification settings - Fork 65
/
JPEGReaderPlugin.c
1824 lines (1732 loc) · 46.7 KB
/
JPEGReaderPlugin.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
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
from
JPEGReaderPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
*/
static char __buildInfo[] = "JPEGReaderPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1 " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "sqMemoryAccess.h"
/*** Constants ***/
#define BlockWidthIndex 5
#define BlueIndex 2
#define ConstBits 13
#define CurrentXIndex 0
#define CurrentYIndex 1
#define DCTSize 8
#define DCTSize2 64
#define FIXn0n298631336 2446
#define FIXn0n34414 22554
#define FIXn0n390180644 0xC7C
#define FIXn0n541196100 4433
#define FIXn0n71414 46802
#define FIXn0n765366865 6270
#define FIXn0n899976223 7373
#define FIXn1n175875602 9633
#define FIXn1n40200 91881
#define FIXn1n501321110 12299
#define FIXn1n77200 116130
#define FIXn1n847759065 15137
#define FIXn1n961570560 16069
#define FIXn2n053119869 16819
#define FIXn2n562915447 20995
#define FIXn3n072711026 25172
#define GreenIndex 1
#define HScaleIndex 2
#define MaxBits 16
#define MaxMCUBlocks 128
#define MaxSample 255
#define MCUBlockIndex 4
#define MCUWidthIndex 8
#define MinComponentSize 11
#define Pass1Bits 2
#define Pass1Div 0x800
#define Pass2Div 0x40000
#define PriorDCValueIndex 10
#define RedIndex 0
#define SampleOffset 127
#define VScaleIndex 3
/*** Function Prototypes ***/
static sqInt cbColorComponentFrom(sqInt oop);
static sqInt colorComponentBlocksfrom(int **blocks, sqInt oop);
static sqInt colorComponentfrom(int *aColorComponent, sqInt oop);
static sqInt colorConvertGrayscaleMCU(void);
static sqInt colorConvertMCU(void);
static sqInt crColorComponentFrom(sqInt oop);
static sqInt decodeBlockIntocomponent(int *anArray, int *aColorComponent);
static sqInt fillBuffer(void);
static sqInt getBits(sqInt requestedBits);
EXPORT(const char*) getModuleName(void);
static sqInt idctBlockIntqt(int *anArray, int *qt);
static sqInt jpegDecodeValueFromsize(int *table, sqInt tableSize);
static sqInt loadJPEGStreamFrom(sqInt streamOop);
static sqInt nextSampleCb(void);
static sqInt nextSampleCr(void);
static sqInt nextSampleFromblocks(int *aComponent, int **aBlockArray);
static sqInt nextSampleY(void);
EXPORT(sqInt) primitiveColorConvertGrayscaleMCU(void);
EXPORT(sqInt) primitiveColorConvertMCU(void);
EXPORT(sqInt) primitiveDecodeMCU(void);
EXPORT(sqInt) primitiveIdctInt(void);
static sqInt scaleAndSignExtendinFieldWidth(sqInt aNumber, sqInt w);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
static sqInt stInit(void);
static sqInt storeJPEGStreamOn(sqInt streamOop);
static sqInt yColorComponentFrom(sqInt oop);
/*** Variables ***/
static int *acTable;
static sqInt acTableSize;
static int *cbBlocks[128];
static int cbComponent[11];
static sqInt cbSampleStream;
static int *crBlocks[128];
static int crComponent[11];
static sqInt crSampleStream;
static int *dcTable;
static sqInt dcTableSize;
static sqInt ditherMask;
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static sqInt (*byteSizeOf)(sqInt oop);
static sqInt (*failed)(void);
static sqInt (*fetchIntegerofObject)(sqInt fieldIndex, sqInt objectPointer);
static sqInt (*fetchPointerofObject)(sqInt index, sqInt oop);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*isPointers)(sqInt oop);
static sqInt (*isWords)(sqInt oop);
static sqInt (*methodArgumentCount)(void);
static sqInt (*pop)(sqInt nItems);
static sqInt (*primitiveFail)(void);
static sqInt (*slotSizeOf)(sqInt oop);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*storeIntegerofObjectwithValue)(sqInt index, sqInt oop, sqInt integer);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern sqInt byteSizeOf(sqInt oop);
extern sqInt failed(void);
extern sqInt fetchIntegerofObject(sqInt fieldIndex, sqInt objectPointer);
extern sqInt fetchPointerofObject(sqInt index, sqInt oop);
extern void * firstIndexableField(sqInt oop);
extern sqInt isBytes(sqInt oop);
extern sqInt isPointers(sqInt oop);
extern sqInt isWords(sqInt oop);
extern sqInt methodArgumentCount(void);
extern sqInt pop(sqInt nItems);
extern sqInt primitiveFail(void);
extern sqInt slotSizeOf(sqInt oop);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern sqInt storeIntegerofObjectwithValue(sqInt index, sqInt oop, sqInt integer);
extern
#endif
struct VirtualMachine* interpreterProxy;
static int *jpegBits;
static sqInt jpegBitsSize;
static int jpegNaturalOrder[64] = {
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
};
static sqInt jsBitBuffer;
static sqInt jsBitCount;
static unsigned char *jsCollection;
static sqInt jsPosition;
static sqInt jsReadLimit;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"JPEGReaderPlugin VMMaker.oscog-eem.2480 (i)"
#else
"JPEGReaderPlugin VMMaker.oscog-eem.2480 (e)"
#endif
;
static int *residuals;
static int *yBlocks[128];
static int yComponent[11];
static sqInt ySampleStream;
/* JPEGReaderPlugin>>#cbColorComponentFrom: */
static sqInt
cbColorComponentFrom(sqInt oop)
{
return (colorComponentfrom(cbComponent, oop))
&& (colorComponentBlocksfrom(cbBlocks, oop));
}
/* JPEGReaderPlugin>>#colorComponentBlocks:from: */
static sqInt
colorComponentBlocksfrom(int **blocks, sqInt oop)
{
sqInt arrayOop;
sqInt blockOop;
sqInt i;
sqInt max;
if (!(isPointers(oop))) {
return 0;
}
if ((slotSizeOf(oop)) < MinComponentSize) {
return 0;
}
arrayOop = fetchPointerofObject(MCUBlockIndex, oop);
if (!(isPointers(arrayOop))) {
return 0;
}
max = slotSizeOf(arrayOop);
if (max > MaxMCUBlocks) {
return 0;
}
for (i = 0; i < max; i += 1) {
blockOop = fetchPointerofObject(i, arrayOop);
if (!(isWords(blockOop))) {
return 0;
}
if (!((slotSizeOf(blockOop)) == DCTSize2)) {
return 0;
}
blocks[i] = (firstIndexableField(blockOop));
}
return !(failed());
}
/* JPEGReaderPlugin>>#colorComponent:from: */
static sqInt
colorComponentfrom(int *aColorComponent, sqInt oop)
{
if (!(isPointers(oop))) {
return 0;
}
if ((slotSizeOf(oop)) < MinComponentSize) {
return 0;
}
aColorComponent[CurrentXIndex] = (fetchIntegerofObject(CurrentXIndex, oop));
aColorComponent[CurrentYIndex] = (fetchIntegerofObject(CurrentYIndex, oop));
aColorComponent[HScaleIndex] = (fetchIntegerofObject(HScaleIndex, oop));
aColorComponent[VScaleIndex] = (fetchIntegerofObject(VScaleIndex, oop));
aColorComponent[BlockWidthIndex] = (fetchIntegerofObject(BlockWidthIndex, oop));
aColorComponent[MCUWidthIndex] = (fetchIntegerofObject(MCUWidthIndex, oop));
aColorComponent[PriorDCValueIndex] = (fetchIntegerofObject(PriorDCValueIndex, oop));
return !(failed());
}
/* JPEGReaderPlugin>>#colorConvertGrayscaleMCU */
static sqInt
colorConvertGrayscaleMCU(void)
{
sqInt blockIndex;
int curX;
int dx;
int dy;
sqInt i;
int sample;
sqInt sampleIndex;
int sx;
int sy;
sqInt y;
yComponent[CurrentXIndex] = 0;
yComponent[CurrentYIndex] = 0;
for (i = 0; i < jpegBitsSize; i += 1) {
/* begin nextSampleY */
dx = (curX = yComponent[CurrentXIndex]);
dy = yComponent[CurrentYIndex];
sx = yComponent[HScaleIndex];
sy = yComponent[VScaleIndex];
if (!((sx == 0)
&& (sy == 0))) {
dx = dx / sx;
dy = dy / sy;
}
blockIndex = ((((usqInt) dy >> 3)) * (yComponent[BlockWidthIndex])) + (((usqInt) dx >> 3));
sampleIndex = (((usqInt) (dy & 7) << 3)) + (dx & 7);
sample = (yBlocks[blockIndex])[sampleIndex];
curX += 1;
if (curX < ((yComponent[MCUWidthIndex]) * 8)) {
yComponent[CurrentXIndex] = curX;
}
else {
yComponent[CurrentXIndex] = 0;
yComponent[CurrentYIndex] = ((yComponent[CurrentYIndex]) + 1);
}
y = sample;
y += residuals[GreenIndex];
y = ((y < MaxSample) ? y : MaxSample);
residuals[GreenIndex] = (y & ditherMask);
y = y & (MaxSample - ditherMask);
y = ((y < 1) ? 1 : y);
jpegBits[i] = (((0xFF000000U + (((sqInt)((usqInt)(y) << 16)))) + (((sqInt)((usqInt)(y) << 8)))) + y);
}
return 0;
}
/* JPEGReaderPlugin>>#colorConvertMCU */
static sqInt
colorConvertMCU(void)
{
sqInt blockIndex;
sqInt blockIndex1;
sqInt blockIndex2;
sqInt blue;
sqInt cb;
sqInt cr;
int curX;
int curX1;
int curX2;
int dx;
int dx1;
int dx2;
int dy;
int dy1;
int dy2;
sqInt green;
sqInt i;
sqInt red;
int sample;
int sample1;
int sample2;
sqInt sampleIndex;
sqInt sampleIndex1;
sqInt sampleIndex2;
int sx;
int sx1;
int sx2;
int sy;
int sy1;
int sy2;
sqInt y;
yComponent[CurrentXIndex] = 0;
yComponent[CurrentYIndex] = 0;
cbComponent[CurrentXIndex] = 0;
cbComponent[CurrentYIndex] = 0;
crComponent[CurrentXIndex] = 0;
crComponent[CurrentYIndex] = 0;
for (i = 0; i < jpegBitsSize; i += 1) {
/* begin nextSampleY */
dx = (curX = yComponent[CurrentXIndex]);
dy = yComponent[CurrentYIndex];
sx = yComponent[HScaleIndex];
sy = yComponent[VScaleIndex];
if (!((sx == 0)
&& (sy == 0))) {
dx = dx / sx;
dy = dy / sy;
}
blockIndex = ((((usqInt) dy >> 3)) * (yComponent[BlockWidthIndex])) + (((usqInt) dx >> 3));
sampleIndex = (((usqInt) (dy & 7) << 3)) + (dx & 7);
sample = (yBlocks[blockIndex])[sampleIndex];
curX += 1;
if (curX < ((yComponent[MCUWidthIndex]) * 8)) {
yComponent[CurrentXIndex] = curX;
}
else {
yComponent[CurrentXIndex] = 0;
yComponent[CurrentYIndex] = ((yComponent[CurrentYIndex]) + 1);
}
y = sample;
/* begin nextSampleCb */
dx1 = (curX1 = cbComponent[CurrentXIndex]);
dy1 = cbComponent[CurrentYIndex];
sx1 = cbComponent[HScaleIndex];
sy1 = cbComponent[VScaleIndex];
if (!((sx1 == 0)
&& (sy1 == 0))) {
dx1 = dx1 / sx1;
dy1 = dy1 / sy1;
}
blockIndex1 = ((((usqInt) dy1 >> 3)) * (cbComponent[BlockWidthIndex])) + (((usqInt) dx1 >> 3));
sampleIndex1 = (((usqInt) (dy1 & 7) << 3)) + (dx1 & 7);
sample1 = (cbBlocks[blockIndex1])[sampleIndex1];
curX1 += 1;
if (curX1 < ((cbComponent[MCUWidthIndex]) * 8)) {
cbComponent[CurrentXIndex] = curX1;
}
else {
cbComponent[CurrentXIndex] = 0;
cbComponent[CurrentYIndex] = ((cbComponent[CurrentYIndex]) + 1);
}
cb = sample1;
cb -= SampleOffset;
/* begin nextSampleCr */
dx2 = (curX2 = crComponent[CurrentXIndex]);
dy2 = crComponent[CurrentYIndex];
sx2 = crComponent[HScaleIndex];
sy2 = crComponent[VScaleIndex];
if (!((sx2 == 0)
&& (sy2 == 0))) {
dx2 = dx2 / sx2;
dy2 = dy2 / sy2;
}
blockIndex2 = ((((usqInt) dy2 >> 3)) * (crComponent[BlockWidthIndex])) + (((usqInt) dx2 >> 3));
sampleIndex2 = (((usqInt) (dy2 & 7) << 3)) + (dx2 & 7);
sample2 = (crBlocks[blockIndex2])[sampleIndex2];
curX2 += 1;
if (curX2 < ((crComponent[MCUWidthIndex]) * 8)) {
crComponent[CurrentXIndex] = curX2;
}
else {
crComponent[CurrentXIndex] = 0;
crComponent[CurrentYIndex] = ((crComponent[CurrentYIndex]) + 1);
}
cr = sample2;
cr -= SampleOffset;
red = (y + ((FIXn1n40200 * cr) / 65536)) + (residuals[RedIndex]);
red = ((red < MaxSample) ? red : MaxSample);
red = ((red < 0) ? 0 : red);
residuals[RedIndex] = (red & ditherMask);
red = red & (MaxSample - ditherMask);
red = ((red < 1) ? 1 : red);
green = ((y - ((FIXn0n34414 * cb) / 65536)) - ((FIXn0n71414 * cr) / 65536)) + (residuals[GreenIndex]);
green = ((green < MaxSample) ? green : MaxSample);
green = ((green < 0) ? 0 : green);
residuals[GreenIndex] = (green & ditherMask);
green = green & (MaxSample - ditherMask);
green = ((green < 1) ? 1 : green);
blue = (y + ((FIXn1n77200 * cb) / 65536)) + (residuals[BlueIndex]);
blue = ((blue < MaxSample) ? blue : MaxSample);
blue = ((blue < 0) ? 0 : blue);
residuals[BlueIndex] = (blue & ditherMask);
blue = blue & (MaxSample - ditherMask);
blue = ((blue < 1) ? 1 : blue);
jpegBits[i] = (((0xFF000000U + (((usqInt) red << 16))) + (((usqInt) green << 8))) + blue);
}
return 0;
}
/* JPEGReaderPlugin>>#crColorComponentFrom: */
static sqInt
crColorComponentFrom(sqInt oop)
{
return (colorComponentfrom(crComponent, oop))
&& (colorComponentBlocksfrom(crBlocks, oop));
}
/* JPEGReaderPlugin>>#decodeBlockInto:component: */
static sqInt
decodeBlockIntocomponent(int *anArray, int *aColorComponent)
{
sqInt bits;
sqInt byte;
unsigned char byte1;
unsigned char byte2;
sqInt i;
sqInt index;
sqInt value;
sqInt value1;
sqInt zeroCount;
byte = jpegDecodeValueFromsize(dcTable, dcTableSize);
if (byte < 0) {
return primitiveFail();
}
if (byte != 0) {
/* begin getBits: */
if (byte > jsBitCount) {
/* begin fillBuffer */
while (jsBitCount <= 16) {
if (!(jsPosition < jsReadLimit)) {
goto l1;
}
byte1 = jsCollection[jsPosition];
jsPosition += 1;
if (byte1 == 0xFF) {
/* peek for 00 */
if (!((jsPosition < jsReadLimit)
&& ((jsCollection[jsPosition]) == 0))) {
jsPosition -= 1;
goto l1;
}
jsPosition += 1;
}
jsBitBuffer = (((usqInt) jsBitBuffer << 8)) | byte1;
jsBitCount += 8;
}
l1: /* end fillBuffer */;
if (byte > jsBitCount) {
bits = -1;
goto l2;
}
}
jsBitCount -= byte;
value = ((usqInt) jsBitBuffer) >> jsBitCount;
jsBitBuffer = jsBitBuffer & ((1U << jsBitCount) - 1);
bits = value;
l2: /* end getBits: */;
/* begin scaleAndSignExtend:inFieldWidth: */
if (bits < (1U << (byte - 1))) {
byte = (bits - (1U << byte)) + 1;
goto l3;
}
else {
byte = bits;
goto l3;
}
l3: /* end scaleAndSignExtend:inFieldWidth: */;
}
byte = aColorComponent[PriorDCValueIndex] = ((aColorComponent[PriorDCValueIndex]) + byte);
anArray[0] = byte;
for (i = 1; i < DCTSize2; i += 1) {
anArray[i] = 0;
}
index = 1;
while (index < DCTSize2) {
byte = jpegDecodeValueFromsize(acTable, acTableSize);
if (byte < 0) {
return primitiveFail();
}
zeroCount = ((usqInt) byte) >> 4;
byte = byte & 15;
if (byte != 0) {
index += zeroCount;
/* begin getBits: */
if (byte > jsBitCount) {
/* begin fillBuffer */
while (jsBitCount <= 16) {
if (!(jsPosition < jsReadLimit)) {
goto l4;
}
byte2 = jsCollection[jsPosition];
jsPosition += 1;
if (byte2 == 0xFF) {
/* peek for 00 */
if (!((jsPosition < jsReadLimit)
&& ((jsCollection[jsPosition]) == 0))) {
jsPosition -= 1;
goto l4;
}
jsPosition += 1;
}
jsBitBuffer = (((usqInt) jsBitBuffer << 8)) | byte2;
jsBitCount += 8;
}
l4: /* end fillBuffer */;
if (byte > jsBitCount) {
bits = -1;
goto l5;
}
}
jsBitCount -= byte;
value1 = ((usqInt) jsBitBuffer) >> jsBitCount;
jsBitBuffer = jsBitBuffer & ((1U << jsBitCount) - 1);
bits = value1;
l5: /* end getBits: */;
/* begin scaleAndSignExtend:inFieldWidth: */
if (bits < (1U << (byte - 1))) {
byte = (bits - (1U << byte)) + 1;
goto l6;
}
else {
byte = bits;
goto l6;
}
l6: /* end scaleAndSignExtend:inFieldWidth: */;
if ((index < 0)
|| (index >= DCTSize2)) {
return primitiveFail();
}
anArray[jpegNaturalOrder[index]] = byte;
}
else {
if (zeroCount == 15) {
index += zeroCount;
}
else {
return null;
}
}
index += 1;
}
return 0;
}
/* JPEGReaderPlugin>>#fillBuffer */
static sqInt
fillBuffer(void)
{
unsigned char byte;
while (jsBitCount <= 16) {
if (!(jsPosition < jsReadLimit)) {
return jsBitCount;
}
byte = jsCollection[jsPosition];
jsPosition += 1;
if (byte == 0xFF) {
/* peek for 00 */
if (!((jsPosition < jsReadLimit)
&& ((jsCollection[jsPosition]) == 0))) {
jsPosition -= 1;
return jsBitCount;
}
jsPosition += 1;
}
jsBitBuffer = (((usqInt) jsBitBuffer << 8)) | byte;
jsBitCount += 8;
}
return jsBitCount;
}
/* JPEGReaderPlugin>>#getBits: */
static sqInt
getBits(sqInt requestedBits)
{
unsigned char byte;
sqInt value;
if (requestedBits > jsBitCount) {
/* begin fillBuffer */
while (jsBitCount <= 16) {
if (!(jsPosition < jsReadLimit)) {
goto l1;
}
byte = jsCollection[jsPosition];
jsPosition += 1;
if (byte == 0xFF) {
/* peek for 00 */
if (!((jsPosition < jsReadLimit)
&& ((jsCollection[jsPosition]) == 0))) {
jsPosition -= 1;
goto l1;
}
jsPosition += 1;
}
jsBitBuffer = (((usqInt) jsBitBuffer << 8)) | byte;
jsBitCount += 8;
}
l1: /* end fillBuffer */;
if (requestedBits > jsBitCount) {
return -1;
}
}
jsBitCount -= requestedBits;
value = ((usqInt) jsBitBuffer) >> jsBitCount;
jsBitBuffer = jsBitBuffer & ((1U << jsBitCount) - 1);
return value;
}
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
return moduleName;
}
/* JPEGReaderPlugin>>#idctBlockInt:qt: */
static sqInt
idctBlockIntqt(int *anArray, int *qt)
{
sqInt anACTerm;
int dcval;
sqInt i;
sqInt j;
sqInt row;
sqInt t0;
sqInt t1;
sqInt t10;
sqInt t11;
sqInt t12;
sqInt t13;
int t2;
int t3;
sqInt v;
int ws[64];
int z1;
int z2;
int z3;
sqInt z4;
sqInt z5;
for (i = 0; i < DCTSize; i += 1) {
anACTerm = -1;
for (row = 1; row < DCTSize; row += 1) {
if (anACTerm == -1) {
if (!((anArray[(row * DCTSize) + i]) == 0)) {
anACTerm = row;
}
}
}
if (anACTerm == -1) {
dcval = ((sqInt)((usqInt)(((anArray[i]) * (qt[0]))) << Pass1Bits));
for (j = 0; j < DCTSize; j += 1) {
ws[(j * DCTSize) + i] = dcval;
}
}
else {
z2 = (anArray[(DCTSize * 2) + i]) * (qt[(DCTSize * 2) + i]);
z3 = (anArray[(DCTSize * 6) + i]) * (qt[(DCTSize * 6) + i]);
z1 = (z2 + z3) * FIXn0n541196100;
t2 = z1 + (z3 * (0 - FIXn1n847759065));
t3 = z1 + (z2 * FIXn0n765366865);
z2 = (anArray[i]) * (qt[i]);
z3 = (anArray[(DCTSize * 4) + i]) * (qt[(DCTSize * 4) + i]);
t0 = ((sqInt)((usqInt)((z2 + z3)) << ConstBits));
t1 = ((sqInt)((usqInt)((z2 - z3)) << ConstBits));
t10 = t0 + t3;
t13 = t0 - t3;
t11 = t1 + t2;
t12 = t1 - t2;
t0 = (anArray[(DCTSize * 7) + i]) * (qt[(DCTSize * 7) + i]);
t1 = (anArray[(DCTSize * 5) + i]) * (qt[(DCTSize * 5) + i]);
t2 = (anArray[(DCTSize * 3) + i]) * (qt[(DCTSize * 3) + i]);
t3 = (anArray[DCTSize + i]) * (qt[DCTSize + i]);
z1 = t0 + t3;
z2 = t1 + t2;
z3 = t0 + t2;
z4 = t1 + t3;
z5 = (z3 + z4) * FIXn1n175875602;
t0 = t0 * FIXn0n298631336;
t1 = t1 * FIXn2n053119869;
t2 = t2 * FIXn3n072711026;
t3 = t3 * FIXn1n501321110;
z1 = z1 * (0 - FIXn0n899976223);
z2 = z2 * (0 - FIXn2n562915447);
z3 = z3 * (0 - FIXn1n961570560);
z4 = z4 * (0 - FIXn0n390180644);
z3 += z5;
z4 += z5;
t0 = (t0 + z1) + z3;
t1 = (t1 + z2) + z4;
t2 = (t2 + z2) + z3;
t3 = (t3 + z1) + z4;
ws[i] = ((t10 + t3) / Pass1Div);
ws[(DCTSize * 7) + i] = ((t10 - t3) / Pass1Div);
ws[(DCTSize) + i] = ((t11 + t2) / Pass1Div);
ws[(DCTSize * 6) + i] = ((t11 - t2) / Pass1Div);
ws[(DCTSize * 2) + i] = ((t12 + t1) / Pass1Div);
ws[(DCTSize * 5) + i] = ((t12 - t1) / Pass1Div);
ws[(DCTSize * 3) + i] = ((t13 + t0) / Pass1Div);
ws[(DCTSize * 4) + i] = ((t13 - t0) / Pass1Div);
}
}
for (i = 0; i <= (DCTSize2 - DCTSize); i += DCTSize) {
z2 = ws[i + 2];
z3 = ws[i + 6];
z1 = (z2 + z3) * FIXn0n541196100;
t2 = z1 + (z3 * (0 - FIXn1n847759065));
t3 = z1 + (z2 * FIXn0n765366865);
t0 = ((sqInt)((usqInt)(((ws[i]) + (ws[i + 4]))) << ConstBits));
t1 = ((sqInt)((usqInt)(((ws[i]) - (ws[i + 4]))) << ConstBits));
t10 = t0 + t3;
t13 = t0 - t3;
t11 = t1 + t2;
t12 = t1 - t2;
t0 = ws[i + 7];
t1 = ws[i + 5];
t2 = ws[i + 3];
t3 = ws[i + 1];
z1 = t0 + t3;
z2 = t1 + t2;
z3 = t0 + t2;
z4 = t1 + t3;
z5 = (z3 + z4) * FIXn1n175875602;
t0 = t0 * FIXn0n298631336;
t1 = t1 * FIXn2n053119869;
t2 = t2 * FIXn3n072711026;
t3 = t3 * FIXn1n501321110;
z1 = z1 * (0 - FIXn0n899976223);
z2 = z2 * (0 - FIXn2n562915447);
z3 = z3 * (0 - FIXn1n961570560);
z4 = z4 * (0 - FIXn0n390180644);
z3 += z5;
z4 += z5;
t0 = (t0 + z1) + z3;
t1 = (t1 + z2) + z4;
t2 = (t2 + z2) + z3;
t3 = (t3 + z1) + z4;
v = ((t10 + t3) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i] = v;
v = ((t10 - t3) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 7] = v;
v = ((t11 + t2) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 1] = v;
v = ((t11 - t2) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 6] = v;
v = ((t12 + t1) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 2] = v;
v = ((t12 - t1) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 5] = v;
v = ((t13 + t0) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 3] = v;
v = ((t13 - t0) / Pass2Div) + SampleOffset;
v = ((v < MaxSample) ? v : MaxSample);
v = ((v < 0) ? 0 : v);
anArray[i + 4] = v;
}
return 0;
}
/* Decode the next value in the receiver using the given huffman table. */
/* JPEGReaderPlugin>>#jpegDecodeValueFrom:size: */
static sqInt
jpegDecodeValueFromsize(int *table, sqInt tableSize)
{
sqInt bits;
sqInt bitsNeeded;
unsigned char byte;
sqInt index;
sqInt tableIndex;
int value;
sqInt value1;
/* Initial bits needed */
bitsNeeded = ((usqInt) (table[0])) >> 24;
if (bitsNeeded > MaxBits) {
return -1;
}
/* First real table */
tableIndex = 2;
while (1) {
/* begin getBits: */
if (bitsNeeded > jsBitCount) {
/* begin fillBuffer */
while (jsBitCount <= 16) {
if (!(jsPosition < jsReadLimit)) {
goto l1;
}
byte = jsCollection[jsPosition];
jsPosition += 1;
if (byte == 0xFF) {
/* peek for 00 */
if (!((jsPosition < jsReadLimit)
&& ((jsCollection[jsPosition]) == 0))) {
jsPosition -= 1;
goto l1;
}
jsPosition += 1;
}
jsBitBuffer = (((usqInt) jsBitBuffer << 8)) | byte;
jsBitCount += 8;
}
l1: /* end fillBuffer */;
if (bitsNeeded > jsBitCount) {
bits = -1;
goto l2;
}
}
jsBitCount -= bitsNeeded;
value1 = ((usqInt) jsBitBuffer) >> jsBitCount;
jsBitBuffer = jsBitBuffer & ((1U << jsBitCount) - 1);
bits = value1;
l2: /* end getBits: */;
if (bits < 0) {
return -1;
}
index = (tableIndex + bits) - 1;
if (index >= tableSize) {
return -1;
}
/* Lookup entry in table */
value = table[index];
if ((value & 0x3F000000) == 0) {
return value;
}
/* Table offset in low 16 bit */
tableIndex = value & 0xFFFF;
/* Additional bits in high 8 bit */
bitsNeeded = (((usqInt) value) >> 24) & 0xFF;
if (bitsNeeded > MaxBits) {
return -1;
}
}
return -1;
}
/* JPEGReaderPlugin>>#loadJPEGStreamFrom: */
static sqInt
loadJPEGStreamFrom(sqInt streamOop)
{
sqInt oop;
sqInt sz;
if (!(isPointers(streamOop))) {
return 0;
}
if ((slotSizeOf(streamOop)) < 5) {
return 0;
}
oop = fetchPointerofObject(0, streamOop);
if (!(isBytes(oop))) {
return 0;
}
jsCollection = firstIndexableField(oop);
sz = byteSizeOf(oop);
jsPosition = fetchIntegerofObject(1, streamOop);
jsReadLimit = fetchIntegerofObject(2, streamOop);
jsBitBuffer = fetchIntegerofObject(3, streamOop);
jsBitCount = fetchIntegerofObject(4, streamOop);
if (failed()) {
return 0;
}
if (sz < jsReadLimit) {
return 0;
}
if ((jsPosition < 0)
|| (jsPosition >= jsReadLimit)) {
return 0;
}
return 1;
}
/* JPEGReaderPlugin>>#nextSampleCb */
static sqInt
nextSampleCb(void)
{
sqInt blockIndex;
int curX;
int dx;
int dy;
int sample;
sqInt sampleIndex;
int sx;
int sy;
dx = (curX = cbComponent[CurrentXIndex]);
dy = cbComponent[CurrentYIndex];
sx = cbComponent[HScaleIndex];
sy = cbComponent[VScaleIndex];
if (!((sx == 0)
&& (sy == 0))) {
dx = dx / sx;
dy = dy / sy;
}
blockIndex = ((((usqInt) dy >> 3)) * (cbComponent[BlockWidthIndex])) + (((usqInt) dx >> 3));
sampleIndex = (((usqInt) (dy & 7) << 3)) + (dx & 7);
sample = (cbBlocks[blockIndex])[sampleIndex];
curX += 1;
if (curX < ((cbComponent[MCUWidthIndex]) * 8)) {
cbComponent[CurrentXIndex] = curX;
}
else {
cbComponent[CurrentXIndex] = 0;
cbComponent[CurrentYIndex] = ((cbComponent[CurrentYIndex]) + 1);
}
return sample;
}
/* JPEGReaderPlugin>>#nextSampleCr */
static sqInt
nextSampleCr(void)
{
sqInt blockIndex;
int curX;
int dx;
int dy;
int sample;
sqInt sampleIndex;
int sx;
int sy;
dx = (curX = crComponent[CurrentXIndex]);
dy = crComponent[CurrentYIndex];
sx = crComponent[HScaleIndex];
sy = crComponent[VScaleIndex];
if (!((sx == 0)
&& (sy == 0))) {
dx = dx / sx;
dy = dy / sy;
}
blockIndex = ((((usqInt) dy >> 3)) * (crComponent[BlockWidthIndex])) + (((usqInt) dx >> 3));
sampleIndex = (((usqInt) (dy & 7) << 3)) + (dx & 7);
sample = (crBlocks[blockIndex])[sampleIndex];
curX += 1;
if (curX < ((crComponent[MCUWidthIndex]) * 8)) {