-
Notifications
You must be signed in to change notification settings - Fork 1
/
kplib.cpp
3421 lines (3089 loc) · 105 KB
/
kplib.cpp
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
/**************************************************************************************************
KPLIB.C: Ken's Picture LIBrary written by Ken Silverman
Copyright (c) 1998-2008 Ken Silverman
Ken Silverman's official web site: http://advsys.net/ken
Features of KPLIB.C:
* Routines for decoding JPG/PNG/GIF/PCX/TGA/BMP/DDS/CEL.
See kpgetdim(), kprender(), and optional helper function: kpzload().
* Routines for reading files out of ZIP/GRP files. All ZIP/GRP functions start with "kz".
* Multi-platform support: Dos/Windows/Linux/Mac/etc..
* Compact code, all in a single source file. Yeah, bad design on my part... but makes life
easier for everyone else - you simply add a single C file to your project, throw a few
externs in there, add the function calls, and you're done!
Brief history:
1998?: Wrote KPEG, a JPEG viewer for DOS
2000: Wrote KPNG, a PNG viewer for DOS
2001: Combined KPEG & KPNG, ported to Visual C, and made it into a library called KPLIB.C
2002: Added support for TGA,GIF,CEL,ZIP
2003: Added support for BMP
05/18/2004: Added support for 8&24 bit PCX
12/09/2005: Added support for progressive JPEG
01/05/2006: Added support for DDS
07/28/2007: Added support for GRP (Build Engine archive)
I offer this code to the community for free use - all I ask is that my name be included in the
credits.
-Ken S.
**************************************************************************************************/
#include "msvc.h"
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#if defined(__POWERPC__)
#define BIGENDIAN 1
#endif
#ifdef BIGENDIAN
static unsigned long LSWAPIB (unsigned long a) { return(((a>>8)&0xff00)+((a&0xff00)<<8)+(a<<24)+(a>>24)); }
static unsigned short SSWAPIB (unsigned short a) { return((a>>8)+(a<<8)); }
#define LSWAPIL(a) (a)
#define SSWAPIL(a) (a)
#else
#define LSWAPIB(a) (a)
#define SSWAPIB(a) (a)
static unsigned long LSWAPIL (unsigned long a) { return(((a>>8)&0xff00)+((a&0xff00)<<8)+(a<<24)+(a>>24)); }
static unsigned short SSWAPIL (unsigned short a) { return((a>>8)+(a<<8)); }
#endif
#if !defined(_WIN32) && !defined(__DOS__)
#include <unistd.h>
#include <dirent.h>
typedef long long __int64;
static __inline long _lrotl (long i, int sh)
{ return((i>>(-sh))|(i<<sh)); }
static __inline long filelength (int h)
{
struct stat st;
if (fstat(h,&st) < 0) return(-1);
return(st.st_size);
}
#define _fileno fileno
#else
#include <io.h>
#endif
#if defined(__DOS__)
#include <dos.h>
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if !defined(max)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#if !defined(min)
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#if defined(__GNUC__)
#define _inline inline
#endif
//use GCC-specific extension to force symbol name to be something in particular to override underscoring.
#if defined(__GNUC__) && defined(__i386__) && !defined(NOASM)
#define ASMNAME(x) asm(x)
#else
#define ASMNAME(x)
#endif
static long frameplace, bytesperline, xres, yres, globxoffs, globyoffs;
static const long pow2mask[32] =
{
0x00000000,0x00000001,0x00000003,0x00000007,
0x0000000f,0x0000001f,0x0000003f,0x0000007f,
0x000000ff,0x000001ff,0x000003ff,0x000007ff,
0x00000fff,0x00001fff,0x00003fff,0x00007fff,
0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
0x000fffff,0x001fffff,0x003fffff,0x007fffff,
0x00ffffff,0x01ffffff,0x03ffffff,0x07ffffff,
0x0fffffff,0x1fffffff,0x3fffffff,0x7fffffff,
};
static const long pow2long[32] =
{
0x00000001,0x00000002,0x00000004,0x00000008,
0x00000010,0x00000020,0x00000040,0x00000080,
0x00000100,0x00000200,0x00000400,0x00000800,
0x00001000,0x00002000,0x00004000,0x00008000,
0x00010000,0x00020000,0x00040000,0x00080000,
0x00100000,0x00200000,0x00400000,0x00800000,
0x01000000,0x02000000,0x04000000,0x08000000,
0x10000000,0x20000000,0x40000000,0x80000000,
};
//Hack for peekbits,getbits,suckbits (to prevent lots of duplicate code)
// 0: PNG: do 12-byte chunk_header removal hack
// !=0: ZIP: use 64K buffer (olinbuf)
static long zipfilmode;
typedef struct
{
FILE *fil; //0:no file open, !=0:open file (either stand-alone or zip)
long comptyp; //0:raw data (can be ZIP or stand-alone), 8:PKZIP LZ77 *flate
long seek0; //0:stand-alone file, !=0: start of zip compressed stream data
long compleng;//Global variable for compression FIFO
long comptell;//Global variable for compression FIFO
long leng; //Uncompressed file size (bytes)
long pos; //Current uncompressed relative file position (0<=pos<=leng)
long endpos; //Temp global variable for kzread
long jmpplc; //Store place where decompression paused
long i; //For stand-alone/ZIP comptyp#0, this is like "uncomptell"
//For ZIP comptyp#8&btype==0 "<64K store", this saves i state
long bfinal; //LZ77 decompression state (for later calls)
} kzfilestate;
static kzfilestate kzfs;
//Initialized tables (can't be in union)
//jpg: png:
// crmul 16384 abstab10 4096
// cbmul 16384 hxbit 472
// dct 4608 pow2mask 128*
// colclip 4096
// colclipup8 4096
// colclipup16 4096
// unzig 256
// pow2mask 128*
// dcflagor 64
long palcol[256] ASMNAME("palcol"), paleng, bakcol, numhufblocks, zlibcompflags;
signed char coltype, filtype, bitdepth;
//============================ KPNGILIB begins ===============================
//07/31/2000: KPNG.C first ported to C from READPNG.BAS
//10/11/2000: KPNG.C split into 2 files: KPNG.C and PNGINLIB.C
//11/24/2000: Finished adding support for coltypes 4&6
//03/31/2001: Added support for Adam7-type interlaced images
//Currently, there is no support for:
// * 16-bit color depth
// * Some useless ancillary chunks, like: gAMA(gamma) & pHYs(aspect ratio)
//.PNG specific variables:
static long bakr = 0x80, bakg = 0x80, bakb = 0x80; //this used to be public...
static long gslidew = 0, gslider = 0, xm, xmn[4], xr0, xr1, xplc, yplc, nfplace;
static long clen[320], cclen[19], bitpos, filt, xsiz, ysiz;
static long xsizbpl, ixsiz, ixoff, iyoff, ixstp, iystp, intlac, nbpl, trnsrgb ASMNAME("trnsrgb");
static long ccind[19] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
static long hxbit[59][2], ibuf0[288], nbuf0[32], ibuf1[32], nbuf1[32];
static const unsigned char *filptr;
static unsigned char slidebuf[32768], opixbuf0[4], opixbuf1[4];
static unsigned char pnginited = 0, olinbuf[65536] ASMNAME("olinbuf"); //WARNING:max xres is: 65536/bpp-1
static long gotcmov = -2, abstab10[1024] ASMNAME("abstab10");
//Variables to speed up dynamic Huffman decoding:
#define LOGQHUFSIZ0 9
#define LOGQHUFSIZ1 6
static long qhufval0[1<<LOGQHUFSIZ0], qhufval1[1<<LOGQHUFSIZ1];
static unsigned char qhufbit0[1<<LOGQHUFSIZ0], qhufbit1[1<<LOGQHUFSIZ1];
#if defined(__WATCOMC__) && !defined(NOASM)
long bswap (long);
#pragma aux bswap =\
".586"\
"bswap eax"\
parm [eax]\
modify nomemory exact [eax]\
value [eax]
long bitrev (long, long);
#pragma aux bitrev =\
"xor eax, eax"\
"beg: shr ebx, 1"\
"adc eax, eax"\
"dec ecx"\
"jnz short beg"\
parm [ebx][ecx]\
modify nomemory exact [eax ebx ecx]\
value [eax]
long testflag (long);
#pragma aux testflag =\
"pushfd"\
"pop eax"\
"mov ebx, eax"\
"xor eax, ecx"\
"push eax"\
"popfd"\
"pushfd"\
"pop eax"\
"xor eax, ebx"\
"mov eax, 1"\
"jne menostinx"\
"xor eax, eax"\
"menostinx:"\
parm nomemory [ecx]\
modify exact [eax ebx]\
value [eax]
void cpuid (long, long *);
#pragma aux cpuid =\
".586"\
"cpuid"\
"mov dword ptr [esi], eax"\
"mov dword ptr [esi+4], ebx"\
"mov dword ptr [esi+8], ecx"\
"mov dword ptr [esi+12], edx"\
parm [eax][esi]\
modify exact [eax ebx ecx edx]\
value
#elif defined(_MSC_VER) && !defined(NOASM)
static _inline unsigned long bswap (unsigned long a)
{
_asm
{
mov eax, a
bswap eax
}
}
static _inline long bitrev (long b, long c)
{
_asm
{
mov edx, b
mov ecx, c
xor eax, eax
beg: shr edx, 1
adc eax, eax
sub ecx, 1
jnz short beg
}
}
static _inline long testflag (long c)
{
_asm
{
mov ecx, c
pushfd
pop eax
mov edx, eax
xor eax, ecx
push eax
popfd
pushfd
pop eax
xor eax, edx
mov eax, 1
jne menostinx
xor eax, eax
menostinx:
}
}
static _inline void cpuid (long a, long *s)
{
_asm
{
push ebx
push esi
mov eax, a
cpuid
mov esi, s
mov dword ptr [esi+0], eax
mov dword ptr [esi+4], ebx
mov dword ptr [esi+8], ecx
mov dword ptr [esi+12], edx
pop esi
pop ebx
}
}
#elif defined(__GNUC__) && defined(__i386__) && !defined(NOASM)
static inline unsigned long bswap (unsigned long a)
{
__asm__ __volatile__ ("bswap %0" : "+r" (a) : : "cc" );
return a;
}
static inline long bitrev (long b, long c)
{
long a;
__asm__ __volatile__ (
"xorl %%eax, %%eax\n\t0:\n\tshrl $1, %%ebx\n\tadcl %%eax, %%eax\n\tsubl $1, %%ecx\n\tjnz 0b"
: "+a" (a), "+b" (b), "+c" (c) : : "cc");
return a;
}
static inline long testflag (long c)
{
long a;
__asm__ __volatile__ (
"pushf\n\tpopl %%eax\n\tmovl %%eax, %%ebx\n\txorl %%ecx, %%eax\n\tpushl %%eax\n\t"
"popf\n\tpushf\n\tpopl %%eax\n\txorl %%ebx, %%eax\n\tmovl $1, %%eax\n\tjne 0f\n\t"
"xorl %%eax, %%eax\n\t0:"
: "=a" (a) : "c" (c) : "ebx","cc" );
return a;
}
static inline void cpuid (long a, long *s)
{
__asm__ __volatile__ (
"cpuid\n\tmovl %%eax, (%%esi)\n\tmovl %%ebx, 4(%%esi)\n\t"
"movl %%ecx, 8(%%esi)\n\tmovl %%edx, 12(%%esi)"
: "+a" (a) : "S" (s) : "ebx","ecx","edx","memory","cc");
}
#else
static inline unsigned long bswap (unsigned long a)
{
return(((a&0xff0000)>>8) + ((a&0xff00)<<8) + (a<<24) + (a>>24));
}
static inline long bitrev (long b, long c)
{
long i, j;
for(i=1,j=0,c=(1<<c);i<c;i+=i) { j += j; if (b&i) j++; }
return(j);
}
static inline long testflag (long c) { return(0); }
static inline void cpuid(long a, long *s) {}
#endif
//Bit numbers of return value:
//0:FPU, 4:RDTSC, 15:CMOV, 22:MMX+, 23:MMX, 25:SSE, 26:SSE2, 30:3DNow!+, 31:3DNow!
static long getcputype ()
{
long i, cpb[4], cpid[4];
if (!testflag(0x200000)) return(0);
cpuid(0,cpid); if (!cpid[0]) return(0);
cpuid(1,cpb); i = (cpb[3]&~((1<<22)|(1<<30)|(1<<31)));
cpuid(0x80000000,cpb);
if (((unsigned long)cpb[0]) > 0x80000000)
{
cpuid(0x80000001,cpb);
i |= (cpb[3]&(1<<31));
if (!((cpid[1]^0x68747541)|(cpid[3]^0x69746e65)|(cpid[2]^0x444d4163))) //AuthenticAMD
i |= (cpb[3]&((1<<22)|(1<<30)));
}
if (i&(1<<25)) i |= (1<<22); //SSE implies MMX+ support
return(i);
}
static unsigned char fakebuf[8], *nfilptr;
static long nbitpos;
static void suckbitsnextblock ()
{
long n;
if (!zipfilmode)
{
if (!nfilptr)
{ //|===|===|crc|lng|typ|===|===|
// \ fakebuf: /
// |===|===|
//----x O---x O--------
nbitpos = LSWAPIL(*(long *)&filptr[8]);
nfilptr = (unsigned char *)&filptr[nbitpos+12];
*(long *)&fakebuf[0] = *(long *)&filptr[0]; //Copy last dword of IDAT chunk
if (*(long *)&filptr[12] == LSWAPIB(0x54414449)) //Copy 1st dword of next IDAT chunk
*(long *)&fakebuf[4] = *(long *)&filptr[16];
filptr = &fakebuf[4]; bitpos -= 32;
}
else
{
filptr = nfilptr; nfilptr = 0;
bitpos -= ((nbitpos-4)<<3);
}
//if (n_from_suckbits < 4) will it crash?
}
else
{
//NOTE: should only read bytes inside compsize, not 64K!!! :/
*(long *)&olinbuf[0] = *(long *)&olinbuf[sizeof(olinbuf)-4];
n = min((unsigned)(kzfs.compleng-kzfs.comptell),sizeof(olinbuf)-4);
fread(&olinbuf[4],n,1,kzfs.fil);
kzfs.comptell += n;
bitpos -= ((sizeof(olinbuf)-4)<<3);
}
}
static _inline long peekbits (long n) { return((LSWAPIB(*(long *)&filptr[bitpos>>3])>>(bitpos&7))&pow2mask[n]); }
static _inline void suckbits (long n) { bitpos += n; if (bitpos >= 0) suckbitsnextblock(); }
static _inline long getbits (long n) { long i = peekbits(n); suckbits(n); return(i); }
static long hufgetsym (long *hitab, long *hbmax)
{
long v, n;
v = n = 0;
do { v = (v<<1)+getbits(1)+hbmax[n]-hbmax[n+1]; n++; } while (v >= 0);
return(hitab[hbmax[n]+v]);
}
//This did not result in a speed-up on P4-3.6Ghz (02/22/2005)
//static long hufgetsym_skipb (long *hitab, long *hbmax, long n, long addit)
//{
// long v;
//
// v = bitrev(getbits(n),n)+addit;
// do { v = (v<<1)+getbits(1)+hbmax[n]-hbmax[n+1]; n++; } while (v >= 0);
// return(hitab[hbmax[n]+v]);
//}
static void qhufgencode (long *hitab, long *hbmax, long *qhval, unsigned char *qhbit, long numbits)
{
long i, j, k, n, r;
//r is the bit reverse of i. Ex: if: i = 1011100111, r = 1110011101
i = r = 0;
for(n=1;n<=numbits;n++)
for(k=hbmax[n-1];k<hbmax[n];k++)
for(j=i+pow2mask[numbits-n];i<=j;i++)
{
r = bitrev(i,numbits);
qhval[r] = hitab[k];
qhbit[r] = (char)n;
}
for(j=pow2mask[numbits];i<=j;i++)
{
r = bitrev(i,numbits);
//k = 0;
//for(n=0;n<numbits;n++)
// k = (k<<1) + ((r>>n)&1) + hbmax[n]-hbmax[n+1];
//
//n = numbits;
//k = hbmax[n]-r;
//
//j = peekbits(LOGQHUFSIZ); i = qhufval[j]; j = qhufbit[j];
//
//i = j = 0;
//do
//{
// i = (i<<1)+getbits(1)+nbuf0[j]-nbuf0[j+1]; j++;
//} while (i >= 0);
//i = ibuf0[nbuf0[j]+i];
//qhval[r] = k;
qhbit[r] = 0; //n-32;
}
// //hufgetsym_skipb related code:
//for(k=n=0;n<numbits;n++) k = (k<<1)+hbmax[n]-hbmax[n+1];
//return(k);
}
//inbuf[inum] : Bit length of each symbol
//inum : Number of indices
//hitab[inum] : Indices from size-ordered list to original symbol
//hbmax[0-31] : Highest index (+1) of n-bit symbol
static void hufgencode (long *inbuf, long inum, long *hitab, long *hbmax)
{
long i, tbuf[31];
for(i=30;i;i--) tbuf[i] = 0;
for(i=inum-1;i>=0;i--) tbuf[inbuf[i]]++;
tbuf[0] = hbmax[0] = 0; //Hack to remove symbols of length 0?
for(i=0;i<31;i++) hbmax[i+1] = hbmax[i]+tbuf[i];
for(i=0;i<inum;i++) if (inbuf[i]) hitab[hbmax[inbuf[i]]++] = i;
}
static long initpass () //Interlaced images have 7 "passes", non-interlaced have 1
{
long i, j, k;
do
{
i = (intlac<<2);
ixoff = ((0x04020100>>i)&15);
iyoff = ((0x00402010>>i)&15);
if (((ixoff >= xsiz) || (iyoff >= ysiz)) && (intlac >= 2)) { i = -1; intlac--; }
} while (i < 0);
j = ((0x33221100>>i)&15); ixstp = (1<<j);
k = ((0x33322110>>i)&15); iystp = (1<<k);
//xsiz=12 0123456789ab
//j=3,ixoff=0 0 1 ((12+(1<<3)-1 - 0)>>3) = 2
//j=3,ixoff=4 2 ((12+(1<<3)-1 - 4)>>3) = 1
//j=2,ixoff=2 3 4 5 ((12+(1<<2)-1 - 2)>>2) = 3
//j=1,ixoff=1 6 7 8 9 a b ((12+(1<<1)-1 - 1)>>1) = 6
ixsiz = ((xsiz+ixstp-1-ixoff)>>j); //It's confusing! See the above example.
nbpl = (bytesperline<<k);
//Initialize this to make filters fast:
xsizbpl = ((0x04021301>>(coltype<<2))&15)*ixsiz;
switch (bitdepth)
{
case 1: xsizbpl = ((xsizbpl+7)>>3); break;
case 2: xsizbpl = ((xsizbpl+3)>>2); break;
case 4: xsizbpl = ((xsizbpl+1)>>1); break;
}
memset(olinbuf,0,(xsizbpl+1)*sizeof(olinbuf[0]));
*(long *)&opixbuf0[0] = *(long *)&opixbuf1[0] = 0;
xplc = xsizbpl; yplc = globyoffs+iyoff; xm = 0; filt = -1;
i = globxoffs+ixoff; i = (((-(i>=0))|(ixstp-1))&i);
k = (((-(yplc>=0))|(iystp-1))&yplc);
nfplace = k*bytesperline + (i<<2) + frameplace;
//Precalculate x-clipping to screen borders (speeds up putbuf)
//Equation: (0 <= xr <= ixsiz) && (0 <= xr*ixstp+globxoffs+ixoff <= xres)
xr0 = max((-globxoffs-ixoff+(1<<j)-1)>>j,0);
xr1 = min((xres-globxoffs-ixoff+(1<<j)-1)>>j,ixsiz);
xr0 = ixsiz-xr0;
xr1 = ixsiz-xr1;
if (coltype == 4) { xr0 = xr0*2; xr1 = xr1*2; }
else if (coltype == 2) { xr0 = xr0*3-2; xr1 = xr1*3-2; }
else if (coltype == 6) { xr0 = xr0*4-2; xr1 = xr1*4-2; }
else
{
switch(bitdepth)
{
case 1: xr0 += ((-ixsiz)&7)+7;
xr1 += ((-ixsiz)&7)+7; break;
case 2: xr0 = ((xr0+((-ixsiz)&3)+3)<<1);
xr1 = ((xr1+((-ixsiz)&3)+3)<<1); break;
case 4: xr0 = ((xr0+((-ixsiz)&1)+1)<<2);
xr1 = ((xr1+((-ixsiz)&1)+1)<<2); break;
}
}
ixstp <<= 2;
return(0);
}
static long Paeth (long a, long b, long c)
{
long pa, pb, pc;
pa = b-c; pb = a-c; pc = labs(pa+pb); pa = labs(pa); pb = labs(pb);
if ((pa <= pb) && (pa <= pc)) return(a);
if (pb <= pc) return(b); else return(c);
}
#if defined(__WATCOMC__) && !defined(NOASM)
//NOTE: cmov now has correctly ordered registers (thx to bug fix in 11.0c!)
long Paeth686 (long, long, long);
#pragma aux Paeth686 =\
".686"\
"mov edx, ecx"\
"sub edx, eax"\
"sub edx, ebx"\
"lea edx, abstab10[edx*4+2048]"\
"mov esi, [ebx*4+edx]"\
"mov edi, [ecx*4+edx]"\
"cmp edi, esi"\
"cmovge edi, esi"\
"cmovge ecx, ebx"\
"cmp edi, [eax*4+edx]"\
"cmovge ecx, eax"\
parm nomemory [eax][ebx][ecx]\
modify exact [ecx edx esi edi]\
value [ecx]
//Note: "cmove eax,?" may be faster than "jne ?:and eax,?" but who cares
void rgbhlineasm (long, long, long, long);
#pragma aux rgbhlineasm =\
"sub ecx, edx"\
"jle short endit"\
"add edx, offset olinbuf"\
"cmp dword ptr trnsrgb, 0"\
"jz short begit2"\
"begit: mov eax, dword ptr [ecx+edx]"\
"or eax, 0xff000000"\
"cmp eax, dword ptr trnsrgb"\
"jne short skipit"\
"and eax, 0xffffff"\
"skipit: sub ecx, 3"\
"mov [edi], eax"\
"lea edi, [edi+ebx]"\
"jnz short begit"\
"jmp short endit"\
"begit2: mov eax, dword ptr [ecx+edx]"\
"or eax, 0xff000000"\
"sub ecx, 3"\
"mov [edi], eax"\
"lea edi, [edi+ebx]"\
"jnz short begit2"\
"endit:"\
parm [ecx][edx][edi][ebx]\
modify exact [eax ecx edi]\
value
void pal8hlineasm (long, long, long, long);
#pragma aux pal8hlineasm =\
"sub ecx, edx"\
"jle short endit"\
"add edx, offset olinbuf"\
"begit: movzx eax, byte ptr [ecx+edx]"\
"mov eax, dword ptr palcol[eax*4]"\
"dec ecx"\
"mov [edi], eax"\
"lea edi, [edi+ebx]"\
"jnz short begit"\
"endit:"\
parm [ecx][edx][edi][ebx]\
modify exact [eax ecx edi]\
value
#elif defined(_MSC_VER) && !defined(NOASM)
static _inline long Paeth686 (long a, long b, long c)
{
_asm
{
push ebx
push esi
push edi
mov eax, a
mov ebx, b
mov ecx, c
mov edx, ecx
sub edx, eax
sub edx, ebx
lea edx, abstab10[edx*4+2048]
mov esi, [ebx*4+edx]
mov edi, [ecx*4+edx]
cmp edi, esi
cmovge edi, esi
cmovge ecx, ebx
cmp edi, [eax*4+edx]
cmovl eax, ecx
pop edi
pop esi
pop ebx
}
}
static _inline void rgbhlineasm (long c, long d, long t, long b)
{
_asm
{
push ebx
push edi
mov ecx, c
mov edx, d
mov edi, t
mov ebx, b
sub ecx, edx
jle short endit
add edx, offset olinbuf
cmp dword ptr trnsrgb, 0
jz short begit2
begit:
mov eax, dword ptr [ecx+edx]
or eax, 0xff000000
cmp eax, dword ptr trnsrgb
jne short skipit
and eax, 0xffffff
skipit:
sub ecx, 3
mov [edi], eax
lea edi, [edi+ebx]
jnz short begit
jmp short endit
begit2:
mov eax, dword ptr [ecx+edx]
or eax, 0xff000000
sub ecx, 3
mov [edi], eax
lea edi, [edi+ebx]
jnz short begit2
endit:
pop edi
pop ebx
}
}
static _inline void pal8hlineasm (long c, long d, long t, long b)
{
_asm
{
mov ecx, c
mov edx, d
sub ecx, edx
jle short endit
push ebx
push edi
mov edi, t
mov ebx, b
add edx, offset olinbuf
begit:movzx eax, byte ptr [ecx+edx]
mov eax, dword ptr palcol[eax*4]
sub ecx, 1
mov [edi], eax
lea edi, [edi+ebx]
jnz short begit
pop edi
pop ebx
endit:
}
}
#elif defined(__GNUC__) && defined(__i386__) && !defined(NOASM)
static inline long Paeth686 (long a, long b, long c)
{
__asm__ __volatile__ (
"movl %%ecx, %%edx \n"
"subl %%eax, %%edx \n"
"subl %%ebx, %%edx \n"
"leal (abstab10+2048)(,%%edx,4), %%edx \n"
"movl (%%edx,%%ebx,4), %%esi \n"
"movl (%%edx,%%ecx,4), %%edi \n"
"cmpl %%esi, %%edi \n"
"cmovgel %%esi, %%edi \n"
"cmovgel %%ebx, %%ecx \n"
"cmpl (%%edx,%%eax,4), %%edi \n"
"cmovgel %%eax, %%ecx \n"
: "+c" (c) : "a" (a), "b" (b) : "edx","esi","edi","memory","cc"
);
return c;
}
//Note: "cmove eax,?" may be faster than "jne ?:and eax,?" but who cares
static inline void rgbhlineasm (long c, long d, long t, long b)
{
__asm__ __volatile__ (
"subl %%edx, %%ecx \n"
"jle 3f \n"
"addl $olinbuf, %%edx \n"
"cmpl $0, trnsrgb(,1) \n"
"jz 2f \n"
"0: movl (%%ecx,%%edx,1), %%eax \n"
"orl $0xff000000, %%eax \n"
"cmpl trnsrgb(,1), %%eax \n"
"jne 1f \n"
"andl $0xffffff, %%eax \n"
"1: subl $3, %%ecx \n"
"movl %%eax, (%%edi) \n"
"leal (%%edi,%%ebx,1), %%edi \n"
"jnz 0b \n"
"jmp 3f \n"
"2: movl (%%ecx,%%edx,1), %%eax \n"
"orl $0xff000000, %%eax \n"
"subl $3, %%ecx \n"
"movl %%eax, (%%edi) \n"
"leal (%%edi,%%ebx,1), %%edi \n"
"jnz 2b \n"
"3: \n"
: "+c" (c), "+d" (d), "+D" (t) : "b" (b) : "eax","memory","cc"
);
}
static inline void pal8hlineasm (long c, long d, long t, long b)
{
__asm__ __volatile__ (
"subl %%edx, %%ecx \n"
"jle 1f \n"
"addl $olinbuf, %%edx \n"
"0: movzbl (%%ecx,%%edx,1), %%eax \n"
"movl palcol(,%%eax,4), %%eax \n"
"subl $1, %%ecx \n"
"movl %%eax, (%%edi) \n"
"leal (%%edi,%%ebx,1), %%edi \n"
"jnz 0b \n"
"1: \n"
: "+c" (c), "+d" (d), "+D" (t) : "b" (b) : "eax","memory","cc"
);
}
#else
static inline long Paeth686 (long a, long b, long c)
{
return(Paeth(a,b,c));
}
static inline void rgbhlineasm (long x, long xr1, long p, long ixstp)
{
long i;
if (!trnsrgb)
{
for(;x>xr1;p+=ixstp,x-=3) *(long *)p = (*(long *)&olinbuf[x])|LSWAPIB(0xff000000);
return;
}
for(;x>xr1;p+=ixstp,x-=3)
{
i = (*(long *)&olinbuf[x])|LSWAPIB(0xff000000);
if (i == trnsrgb) i &= LSWAPIB(0xffffff);
*(long *)p = i;
}
}
static inline void pal8hlineasm (long x, long xr1, long p, long ixstp)
{
for(;x>xr1;p+=ixstp,x--) *(long *)p = palcol[olinbuf[x]];
}
#endif
//Autodetect filter
// /f0: 0000000...
// /f1: 1111111...
// /f2: 2222222...
// /f3: 1333333...
// /f3: 3333333...
// /f4: 4444444...
// /f5: 0142321...
static long filter1st, filterest;
static void putbuf (const unsigned char *buf, long leng)
{
long i, x, p;
if (filt < 0)
{
if (leng <= 0) return;
filt = buf[0];
if (filter1st < 0) filter1st = filt; else filterest |= (1<<filt);
if (filt == gotcmov) filt = 5;
i = 1;
} else i = 0;
while (i < leng)
{
x = i+xplc; if (x > leng) x = leng;
switch (filt)
{
case 0:
while (i < x) { olinbuf[xplc] = buf[i]; xplc--; i++; }
break;
case 1:
while (i < x)
{
olinbuf[xplc] = (unsigned char)(opixbuf1[xm] += buf[i]);
xm = xmn[xm]; xplc--; i++;
}
break;
case 2:
while (i < x) { olinbuf[xplc] += (unsigned char)buf[i]; xplc--; i++; }
break;
case 3:
while (i < x)
{
opixbuf1[xm] = olinbuf[xplc] = (unsigned char)(((opixbuf1[xm]+olinbuf[xplc])>>1)+buf[i]);
xm = xmn[xm]; xplc--; i++;
}
break;
case 4:
while (i < x)
{
opixbuf1[xm] = (char)(Paeth(opixbuf1[xm],olinbuf[xplc],opixbuf0[xm])+buf[i]);
opixbuf0[xm] = olinbuf[xplc];
olinbuf[xplc] = opixbuf1[xm];
xm = xmn[xm]; xplc--; i++;
}
break;
case 5: //Special hack for Paeth686 (Doesn't have to be case 5)
while (i < x)
{
opixbuf1[xm] = (char)(Paeth686(opixbuf1[xm],olinbuf[xplc],opixbuf0[xm])+buf[i]);
opixbuf0[xm] = olinbuf[xplc];
olinbuf[xplc] = opixbuf1[xm];
xm = xmn[xm]; xplc--; i++;
}
break;
}
if (xplc > 0) return;
//Draw line!
if ((unsigned long)yplc < (unsigned long)yres)
{
x = xr0; p = nfplace;
switch (coltype)
{
case 2: rgbhlineasm(x,xr1,p,ixstp); break;
case 4:
for(;x>xr1;p+=ixstp,x-=2)
*(long *)p = (palcol[olinbuf[x]]&LSWAPIB(0xffffff))|LSWAPIL((long)olinbuf[x-1]);
break;
case 6:
for(;x>xr1;p+=ixstp,x-=4)
{
*(char *)(p ) = olinbuf[x ]; //B
*(char *)(p+1) = olinbuf[x+1]; //G
*(char *)(p+2) = olinbuf[x+2]; //R
*(char *)(p+3) = olinbuf[x-1]; //A
}
break;
default:
switch(bitdepth)
{
case 1: for(;x>xr1;p+=ixstp,x-- ) *(long *)p = palcol[olinbuf[x>>3]>>(x&7)]; break;
case 2: for(;x>xr1;p+=ixstp,x-=2) *(long *)p = palcol[olinbuf[x>>3]>>(x&6)]; break;
case 4: for(;x>xr1;p+=ixstp,x-=4) *(long *)p = palcol[olinbuf[x>>3]>>(x&4)]; break;
case 8: pal8hlineasm(x,xr1,p,ixstp); break; //for(;x>xr1;p+=ixstp,x-- ) *(long *)p = palcol[olinbuf[x]]; break;
}
break;
}
nfplace += nbpl;
}
*(long *)&opixbuf0[0] = *(long *)&opixbuf1[0] = 0;
xplc = xsizbpl; yplc += iystp;
if ((intlac) && (yplc >= globyoffs+ysiz)) { intlac--; initpass(); }
if (i < leng)
{
filt = buf[i++];
if (filter1st < 0) filter1st = filt; else filterest |= (1<<filt);
if (filt == gotcmov) filt = 5;
} else filt = -1;
}
}
static void initpngtables()
{
long i, j, k;
//hxbit[0-58][0-1] is a combination of 4 different tables:
// 1st parameter: [0-29] are distances, [30-58] are lengths
// 2nd parameter: [0]: extra bits, [1]: base number
j = 1; k = 0;
for(i=0;i<30;i++)
{
hxbit[i][1] = j; j += (1<<k);
hxbit[i][0] = k; k += ((i&1) && (i >= 2));
}
j = 3; k = 0;
for(i=257;i<285;i++)
{
hxbit[i+30-257][1] = j; j += (1<<k);
hxbit[i+30-257][0] = k; k += ((!(i&3)) && (i >= 264));
}
hxbit[285+30-257][1] = 258; hxbit[285+30-257][0] = 0;
k = getcputype();
if (k&(1<<15))
{
gotcmov = 4;
for(i=0;i<512;i++) abstab10[512+i] = abstab10[512-i] = i;
}
}
static long kpngrend (const char *kfilebuf, long kfilength,
long daframeplace, long dabytesperline, long daxres, long dayres,
long daglobxoffs, long daglobyoffs)
{
long i, j, k, bfinal, btype, hlit, hdist, leng;
long slidew, slider;
//long qhuf0v, qhuf1v;
if (!pnginited) { pnginited = 1; initpngtables(); }
if ((*(long *)&kfilebuf[0] != LSWAPIB(0x474e5089)) || (*(long *)&kfilebuf[4] != LSWAPIB(0x0a1a0a0d)))
return(-1); //"Invalid PNG file signature"
filptr = (unsigned char *)&kfilebuf[8];
trnsrgb = 0; filter1st = -1; filterest = 0;
while (1)
{
leng = LSWAPIL(*(long *)&filptr[0]); i = *(long *)&filptr[4];
filptr = &filptr[8];