diff --git a/source/build/include/kplib.h b/source/build/include/kplib.h index c3033a283..dd25c3189 100644 --- a/source/build/include/kplib.h +++ b/source/build/include/kplib.h @@ -23,13 +23,6 @@ typedef struct extern kzfilestate kzfs; - //High-level (easy) picture loading function: -extern void kpzdecode (int32_t, intptr_t *, int32_t *, int32_t *); -extern void kpzload (const char *, intptr_t *, int32_t *, int32_t *); - //Low-level PNG/JPG functions: -extern void kpgetdim (const char *, int32_t, int32_t *, int32_t *); -extern int32_t kprender (const char *, int32_t, intptr_t, int32_t, int32_t, int32_t); - //ZIP functions: extern int32_t kzaddstack (const char *); extern void kzuninit (); diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index d9ee17c46..f3b90864b 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -16,6 +16,7 @@ #include "common.h" #include "mdsprite.h" // md3model_t #include "colmatch.h" +#include "textures.h" #ifdef USE_OPENGL # include "hightile.h" @@ -254,14 +255,14 @@ static int32_t Defs_ImportTileFromTexture(char const * const fn, int32_t const t int32_t xsiz = 0, ysiz = 0; palette_t *picptr = NULL; - int32_t const length = kpzbufload(fn); -#ifdef WITHKPLIB - kpzdecode(length, (intptr_t *)&picptr, &xsiz, &ysiz); -#endif + FTexture* tex = FTexture::GetTexture(fn); - if (!picptr) + // This should be implemented as a separate texture type to avoid maintenance problems elsewhere. + if (!tex) { - int32_t const artstatus = artCheckUnitFileHeader((uint8_t *)kpzbuf, length); + auto fr = kopenFileReader(fn, 0); + auto data = fr.Read(); + int32_t const artstatus = artCheckUnitFileHeader(data.Data(), data.Size()); if (artstatus < 0) return artstatus<<8; @@ -282,7 +283,7 @@ static int32_t Defs_ImportTileFromTexture(char const * const fn, int32_t const t tileSetSize(tile, xsiz, ysiz); int32_t const dasiz = xsiz * ysiz; - if (EDUKE32_PREDICT_FALSE(ARTv1_UNITOFFSET + dasiz > length)) + if (EDUKE32_PREDICT_FALSE(ARTv1_UNITOFFSET + dasiz > (int)data.Size())) { tileSetupDummy(tile); return 3; @@ -304,7 +305,7 @@ static int32_t Defs_ImportTileFromTexture(char const * const fn, int32_t const t if (!(paletteloaded & PALETTE_MAIN)) return -3; - tileSetSize(tile, xsiz, ysiz); + tileSetSize(tile, tex->GetWidth(), tex->GetHeight()); tile_from_truecolpic(tile, picptr, alphacut); @@ -2057,45 +2058,6 @@ static int32_t defsparser(scriptfile *script) if (check_file_exist(fn)) break; -#ifdef POLYMER - fd = kopen4load(fn, 0); - - // load the highpalookup and send it to polymer - highpaldata = (char *)Xmalloc(PR_HIGHPALOOKUP_DATA_SIZE); - - { - char *filebuf; - int32_t xsiz, ysiz, filesize, i; - - filesize = kfilelength(fd); - - filebuf = (char *)Xmalloc(filesize); - - klseek(fd, 0, SEEK_SET); - if (kread(fd, filebuf, filesize)!=filesize) - { kclose(fd); Xfree(highpaldata); initprintf("Error: didn't read all of \"%s\".\n", fn); break; } - - kclose(fd); - kpgetdim(filebuf, filesize, &xsiz, &ysiz); - - if (EDUKE32_PREDICT_FALSE(xsiz != PR_HIGHPALOOKUP_DIM*PR_HIGHPALOOKUP_DIM || ysiz != PR_HIGHPALOOKUP_DIM)) - { - initprintf("Error: image dimensions of \"%s\" must be %dx%d.\n", - fn, PR_HIGHPALOOKUP_DIM*PR_HIGHPALOOKUP_DIM, PR_HIGHPALOOKUP_DIM); - Xfree(filebuf); Xfree(highpaldata); - break; - } - - i = kprender(filebuf, filesize, (intptr_t)highpaldata, xsiz*sizeof(coltype), xsiz, ysiz); - Xfree(filebuf); - if (EDUKE32_PREDICT_FALSE(i)) - { Xfree(highpaldata); initprintf("Error: failed rendering \"%s\".\n", fn); break; } - } - - polymer_definehighpalookup(basepal, pal, highpaldata); - - Xfree(highpaldata); -#endif } break; case T_TINT: diff --git a/source/build/src/kplib.cpp b/source/build/src/kplib.cpp index 67cd9c46a..0811ed0a7 100644 --- a/source/build/src/kplib.cpp +++ b/source/build/src/kplib.cpp @@ -4,8 +4,6 @@ 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 @@ -544,1583 +542,8 @@ static void initpngtables() for (i=0; i<512; i++) abstab10[512+i] = abstab10[512-i] = i; } -static int32_t kpngrend(const char *kfilebuf, int32_t kfilength, - intptr_t dakpframeplace, int32_t dakpbytesperline, int32_t daxres, int32_t dayres) -{ - int32_t i, j, k, bfinal, btype, hlit, hdist, leng; - int32_t slidew, slider; - //int32_t qhuf0v, qhuf1v; - - UNREFERENCED_PARAMETER(kfilength); - - if (!pnginited) { pnginited = 1; initpngtables(); } - - if ((B_UNBUF32(&kfilebuf[0]) != B_LITTLE32(0x474e5089)) || (B_UNBUF32(&kfilebuf[4]) != B_LITTLE32(0x0a1a0a0d))) - return -1; //"Invalid PNG file signature" - filptr = (uint8_t const *)&kfilebuf[8]; - - trnsrgb = 0; filter1st = -1; filterest = 0; - - while (1) - { - leng = B_BIG32(B_UNBUF32(&filptr[0])); i = B_UNBUF32(&filptr[4]); - filptr = &filptr[8]; - - if (i == (int32_t)B_LITTLE32(0x52444849)) //IHDR (must be first) - { - xsiz = B_BIG32(B_UNBUF32(&filptr[0])); if (xsiz <= 0) return -1; - ysiz = B_BIG32(B_UNBUF32(&filptr[4])); if (ysiz <= 0) return -1; - bitdepth = filptr[8]; if (!((1<= 2) return -1; //"Unsupported interlace type" - intlac = filptr[12]*7; //0=no interlace/1=Adam7 interlace - - //Save code by making grayscale look like a palette color scheme - if ((!kcoltype) || (kcoltype == 4)) - { - j = 0xff000000; k = (tabledivide32(255, ((1<=0; i--) palcol[i] = B_LITTLE32((B_BIG32(B_UNBUF32(&filptr[i*3]))>>8)|0xff000000); - } - else if (i == (int32_t)B_LITTLE32(0x44474b62)) //bKGD (must be after PLTE and before IDAT) - { - switch (kcoltype) - { - case 0: case 4: - bakcol = (((int32_t)filptr[0]<<8)+(int32_t)filptr[1])*tabledivide32(255, ((1<>16)&255); - bakg = ((bakcol>>8)&255); - bakb = (bakcol&255); - bakcol = B_LITTLE32(bakcol); - } - else if (i == (int32_t)B_LITTLE32(0x534e5274)) //tRNS (must be after PLTE and before IDAT) - { - switch (kcoltype) - { - case 0: - if (bitdepth <= 8) - palcol[(int32_t)filptr[1]] &= B_LITTLE32(0xffffff); - //else {} // /c0 /d16 not yet supported - break; - case 2: - if (bitdepth == 8) - { trnsrgb = B_LITTLE32((((int32_t)filptr[1])<<16)+(((int32_t)filptr[3])<<8)+((int32_t)filptr[5])+0xff000000); } - //else {} //WARNING: PNG docs say: MUST compare all 48 bits :( - break; - case 3: - for (i=min(leng,paleng)-1; i>=0; i--) - palcol[i] &= B_LITTLE32((((int32_t)filptr[i])<<24)|0xffffff); - break; - default:; - EDUKE32_UNREACHABLE_SECTION(); - } - } - else if (i == (int32_t)B_LITTLE32(0x54414449)) { break; } //IDAT - - filptr = &filptr[leng+4]; //crc = B_BIG32(B_UNBUF32(&filptr[-4])); - } - - //Initialize this for the getbits() function - zipfilmode = 0; - filptr = &filptr[leng-4]; bitpos = -((leng-4)<<3); nfilptr = 0; - //if (leng < 4) will it crash? - - kp_frameplace = dakpframeplace; - kp_bytesperline = dakpbytesperline; - kp_xres = daxres; - kp_yres = dayres; - switch (kcoltype) - { - case 4: xmn[0] = 1; xmn[1] = 0; break; - case 2: xmn[0] = 1; xmn[1] = 2; xmn[2] = 0; break; - case 6: xmn[0] = 1; xmn[1] = 2; xmn[2] = 3; xmn[3] = 0; break; - default: xmn[0] = 0; break; - } - switch (bitdepth) - { - case 1: for (i=2; i<256; i++) palcol[i] = palcol[i&1]; break; - case 2: for (i=4; i<256; i++) palcol[i] = palcol[i&3]; break; - case 4: for (i=16; i<256; i++) palcol[i] = palcol[i&15]; break; - } - - //coltype: bitdepth: format: - // 0 1,2,4,8,16 I - // 2 8,16 RGB - // 3 1,2,4,8 P - // 4 8,16 IA - // 6 8,16 RGBA - xsizbpl = ((0x04021301>>(kcoltype<<2))&15)*xsiz; - switch (bitdepth) - { - case 1: xsizbpl = ((xsizbpl+7)>>3); break; - case 2: xsizbpl = ((xsizbpl+3)>>2); break; - case 4: xsizbpl = ((xsizbpl+1)>>1); break; - } - //Tests to see if xsiz > allocated space in olinbuf - //Note: xsizbpl gets re-written inside initpass() - if ((xsizbpl+1)*sizeof(olinbuf[0]) > sizeof(olinbuf)) return -1; - - initpass(); - - slidew = 0; slider = 16384; - zlibcompflags = getbits(16); //Actually 2 fields: 8:compmethflags, 8:addflagscheck - do - { - bfinal = getbits(1); btype = getbits(2); - if (btype == 0) - { - //Raw (uncompressed) - suckbits((-bitpos)&7); //Synchronize to start of next byte - i = getbits(16); if ((getbits(16)^i) != 0xffff) return -1; - for (; i; i--) - { - if (slidew >= slider) - { - putbuf(&slidebuf[(slider-16384)&32767],16384); slider += 16384; - if ((yplc >= kp_yres) && (intlac < 2)) goto kpngrend_goodret; - } - slidebuf[(slidew++)&32767] = (uint8_t)getbits(8); - } - continue; - } - if (btype == 3) continue; - - if (btype == 1) //Fixed Huffman - { - hlit = 288; hdist = 32; i = 0; - for (; i<144; i++) clen[i] = 8; //Fixed bit sizes (literals) - for (; i<256; i++) clen[i] = 9; //Fixed bit sizes (literals) - for (; i<280; i++) clen[i] = 7; //Fixed bit sizes (EOI,lengths) - for (; i<288; i++) clen[i] = 8; //Fixed bit sizes (lengths) - for (; i<320; i++) clen[i] = 5; //Fixed bit sizes (distances) - } - else //Dynamic Huffman - { - numhufblocks++; - hlit = getbits(5)+257; hdist = getbits(5)+1; j = getbits(4)+4; - for (i=0; i= slider) - { - putbuf(&slidebuf[(slider-16384)&32767],16384); slider += 16384; - if ((yplc >= kp_yres) && (intlac < 2)) goto kpngrend_goodret; - } - - k = peekbits(LOGQHUFSIZ0); - if (qhufbit0[k]) { i = qhufval0[k]; suckbits((int32_t)qhufbit0[k]); } - else i = hufgetsym(ibuf0,nbuf0); - //else i = hufgetsym_skipb(ibuf0,nbuf0,LOGQHUFSIZ0,qhuf0v); //hufgetsym_skipb related code - - if (i < 256) { slidebuf[(slidew++)&32767] = (uint8_t)i; continue; } - if (i == 256) break; - i = getbits(hxbit[i+30-257][0]) + hxbit[i+30-257][1]; - - k = peekbits(LOGQHUFSIZ1); - if (qhufbit1[k]) { j = qhufval1[k]; suckbits((int32_t)qhufbit1[k]); } - else j = hufgetsym(ibuf1,nbuf1); - //else j = hufgetsym_skipb(ibuf1,nbuf1,LOGQHUFSIZ1,qhuf1v); //hufgetsym_skipb related code - - j = getbits(hxbit[j][0]) + hxbit[j][1]; - i += slidew; do { slidebuf[slidew&32767] = slidebuf[(slidew-j)&32767]; slidew++; } - while (slidew < i); - } - } - while (!bfinal); - - slider -= 16384; - if (!((slider^slidew)&32768)) - putbuf(&slidebuf[slider&32767],slidew-slider); - else - { - putbuf(&slidebuf[slider&32767],(-slider)&32767); - putbuf(slidebuf,slidew&32767); - } - -kpngrend_goodret: - if (!(filterest&~(1<>24); -} - -static inline int32_t mulshr32(int32_t a, int32_t b) -{ - return (int32_t)((((int64_t)a)*((int64_t)b))>>32); -} - -static int32_t cosqr16[8] = //cosqr16[i] = ((cos(PI*i/16)*sqrt(2))<<24); -{23726566,23270667,21920489,19727919,16777216,13181774,9079764,4628823}; -static int32_t crmul[4096], cbmul[4096]; - -static void initkpeg() -{ - int32_t i, j, y; - - int32_t x = 0; //Back & forth diagonal pattern (aligning bytes for best compression) - for (i=0; i<16; i+=2) - { - for (y=8-1; y>=0; y--) - if ((unsigned)(i-y) < (unsigned)8) unzig[x++] = (y<<3)+i-y; - for (y=0; y<8; y++) - if ((unsigned)(i+1-y) < (unsigned)8) unzig[x++] = (y<<3)+i+1-y; - } - for (i=64-1; i>=0; i--) zigit[unzig[i]] = i; - for (i=64-1; i>=0; i--) dcflagor[i] = (uint8_t)(1<<(unzig[i]>>3)); - - for (i=0; i<128; i++) colclip[i] = i+128; - for (i=128; i<512; i++) colclip[i] = 255; - for (i=512; i<896; i++) colclip[i] = 0; - for (i=896; i<1024; i++) colclip[i] = i-896; - for (i=0; i<1024; i++) - { - colclipup8[i] = (colclip[i]<<8); - colclipup16[i] = (colclip[i]<<16)+0xff000000; //Hack: set alphas to 255 - } -#if B_BIG_ENDIAN == 1 - for (i=0; i<1024; i++) - { - colclip[i] = B_SWAP32(colclip[i]); - colclipup8[i] = B_SWAP32(colclipup8[i]); - colclipup16[i] = B_SWAP32(colclipup16[i]); - } -#endif - - for (i=0; i<2048; i++) - { - j = i-1024; - crmul[(i<<1)+0] = j*1470104; //1.402*1048576 - crmul[(i<<1)+1] = j*-748830; //-0.71414*1048576 - cbmul[(i<<1)+0] = j*-360857; //-0.34414*1048576 - cbmul[(i<<1)+1] = j*1858077; //1.772*1048576 - } - - Bmemset((void *)&dct[10][0],0,64*2*sizeof(dct[0][0])); -} - -static void huffgetval(int32_t index, int32_t curbits, int32_t num, int32_t *daval, int32_t *dabits) -{ - int32_t b, v, pow2, *hmax; - - hmax = &hufmaxatbit[index][0]; - pow2 = pow2long[curbits-1]; - if (num&pow2) v = 1; else v = 0; - for (b=1; b<=16; b++) - { - if (v < hmax[b]) - { - *dabits = b; - *daval = huftable[index][hufvalatbit[index][b]+v]; - return; - } - pow2 >>= 1; v <<= 1; - if (num&pow2) v++; - } - *dabits = 16; *daval = 0; -} - -static void invdct8x8(int32_t *dc, uint8_t dcflag) -{ -#define SQRT2 23726566 //(sqrt(2))<<24 -#define C182 31000253 //(cos(PI/8)*2)<<24 -#define C18S22 43840978 //(cos(PI/8)*sqrt(2)*2)<<24 -#define C38S22 18159528 //(cos(PI*3/8)*sqrt(2)*2)<<24 - int32_t *edc, t0, t1, t2, t3, t4, t5, t6, t7; - - edc = dc+64; - do - { - if (dcflag&1) //pow2char[z]) - { - t3 = dc[2] + dc[6]; - t2 = (mulshr32(dc[2]-dc[6],SQRT2<<6)<<2) - t3; - t4 = dc[0] + dc[4]; t5 = dc[0] - dc[4]; - t0 = t4+t3; t3 = t4-t3; t1 = t5+t2; t2 = t5-t2; - t4 = (mulshr32(dc[5]-dc[3]+dc[1]-dc[7],C182<<6)<<2); - t7 = dc[1] + dc[7] + dc[5] + dc[3]; - t6 = (mulshr32(dc[3]-dc[5],C18S22<<5)<<3) + t4 - t7; - t5 = (mulshr32(dc[1]+dc[7]-dc[5]-dc[3],SQRT2<<6)<<2) - t6; - t4 = (mulshr32(dc[1]-dc[7],C38S22<<6)<<2) - t4 + t5; - dc[0] = t0+t7; dc[7] = t0-t7; dc[1] = t1+t6; dc[6] = t1-t6; - dc[2] = t2+t5; dc[5] = t2-t5; dc[4] = t3+t4; dc[3] = t3-t4; - } - dc += 8; dcflag >>= 1; - } - while (dc < edc); - dc -= 32; edc -= 24; - do - { - t3 = dc[2*8-32] + dc[6*8-32]; - t2 = (mulshr32(dc[2*8-32]-dc[6*8-32],SQRT2<<6)<<2) - t3; - t4 = dc[0*8-32] + dc[4*8-32]; t5 = dc[0*8-32] - dc[4*8-32]; - t0 = t4+t3; t3 = t4-t3; t1 = t5+t2; t2 = t5-t2; - t4 = (mulshr32(dc[5*8-32]-dc[3*8-32]+dc[1*8-32]-dc[7*8-32],C182<<6)<<2); - t7 = dc[1*8-32] + dc[7*8-32] + dc[5*8-32] + dc[3*8-32]; - t6 = (mulshr32(dc[3*8-32]-dc[5*8-32],C18S22<<5)<<3) + t4 - t7; - t5 = (mulshr32(dc[1*8-32]+dc[7*8-32]-dc[5*8-32]-dc[3*8-32],SQRT2<<6)<<2) - t6; - t4 = (mulshr32(dc[1*8-32]-dc[7*8-32],C38S22<<6)<<2) - t4 + t5; - dc[0*8-32] = t0+t7; dc[7*8-32] = t0-t7; dc[1*8-32] = t1+t6; dc[6*8-32] = t1-t6; - dc[2*8-32] = t2+t5; dc[5*8-32] = t2-t5; dc[4*8-32] = t3+t4; dc[3*8-32] = t3-t4; - dc++; - } - while (dc < edc); -} - -static void yrbrend(int32_t x, int32_t y, int32_t *ldct) -{ - int32_t i, j, ox, oy, xx, yy, xxx, yyy, xxxend, yyyend, yv, cr = 0, cb = 0, *odc, *dc, *dc2; - intptr_t p, pp; - - odc = ldct; dc2 = &ldct[10<<6]; - for (yy=0; yy<(lcompvsamp[0]<<3); yy+=8) - { - oy = y+yy; if ((unsigned)oy >= (unsigned)clipydim) { odc += (lcomphsamp[0]<<6); continue; } - pp = oy*kp_bytesperline + ((x)<<2) + kp_frameplace; - for (xx=0; xx<(lcomphsamp[0]<<3); xx+=8,odc+=64) - { - ox = x+xx; if ((unsigned)ox >= (unsigned)clipxdim) continue; - p = pp+(xx<<2); - dc = odc; - if (lnumcomponents > 1) dc2 = &ldct[(lcomphvsamp0<<6)+((yy>>lcompvsampshift0)<<3)+(xx>>lcomphsampshift0)]; - xxxend = min(clipxdim-ox,8); - yyyend = min(clipydim-oy,8); - if ((lcomphsamp[0] == 1) && (xxxend == 8)) - { - for (yyy=0; yyy>(20-1))&~1; - cb = (dc2[xxx ]>>(20-1))&~1; - ((int32_t *)p)[xxx] = colclipup16[(unsigned)(yv+crmul[cr+2048])>>22]+ - colclipup8[(unsigned)(yv+crmul[cr+2049]+cbmul[cb+2048])>>22]+ - colclip[(unsigned)(yv+cbmul[cb+2049])>>22]; - } - p += kp_bytesperline; - dc += 8; - if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; - } - } - else if ((lcomphsamp[0] == 2) && (xxxend == 8)) - { - for (yyy=0; yyy>1)+64]>>(20-1))&~1; - cb = (dc2[(xxx>>1)]>>(20-1))&~1; - i = crmul[cr+2049]+cbmul[cb+2048]; - cr = crmul[cr+2048]; - cb = cbmul[cb+2049]; - ((int32_t *)p)[xxx] = colclipup16[(unsigned)(yv+cr)>>22]+ - colclipup8[(unsigned)(yv+ i)>>22]+ - colclip[(unsigned)(yv+cb)>>22]; - yv = dc[xxx+1]; - ((int32_t *)p)[xxx+1] = colclipup16[(unsigned)(yv+cr)>>22]+ - colclipup8[(unsigned)(yv+ i)>>22]+ - colclip[(unsigned)(yv+cb)>>22]; - } - p += kp_bytesperline; - dc += 8; - if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; - } - } - else - { - for (yyy=0; yyy>(20-1))&~1; - cb = (dc2[i ]>>(20-1))&~1; - i++; - } - ((int32_t *)p)[xxx] = colclipup16[(unsigned)(yv+crmul[cr+2048])>>22]+ - colclipup8[(unsigned)(yv+crmul[cr+2049]+cbmul[cb+2048])>>22]+ - colclip[(unsigned)(yv+cbmul[cb+2049])>>22]; - } - p += kp_bytesperline; - dc += 8; - if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; - } - } - } - } -} -void (*kplib_yrbrend_func)(int32_t,int32_t,int32_t *) = yrbrend; - -#define KPEG_GETBITS(curbits, minbits, num, kfileptr, kfileend)\ - while (curbits < minbits)\ - {\ - ch = *kfileptr++; num = (num<<8)+((int)ch); curbits += 8;\ - if (ch == 255) { kfileptr++; if (kfileptr >= kfileend) { num <<= 8; curbits += 8; /*Hack to prevent read overrun on valid JPG by stuffing extra byte*/ } }\ - } - - -static int32_t kpegrend(const char *kfilebuf, int32_t kfilength, - intptr_t dakpframeplace, int32_t dakpbytesperline, int32_t daxres, int32_t dayres) -{ - int32_t i, j, v, leng = 0, xdim = 0, ydim = 0, index, prec, restartcnt, restartinterval; - int32_t x, y, z, xx, yy, zz, *dc = NULL, num, curbits, c, daval, dabits, *hqval, *hqbits, hqcnt, *quanptr = NULL; - int32_t passcnt = 0, ghsampmax = 0, gvsampmax = 0, glhsampmax = 0, glvsampmax = 0, glhstep, glvstep; - int32_t eobrun, Ss, Se, Ah, Al, Alut[2], dctx[12], dcty[12], ldctx[12], /* ldcty[12], */ lshx[4], lshy[4]; - int16_t *dctbuf = 0, *dctptr[12], *ldctptr[12], *dcs = NULL; - uint8_t ch, marker, dcflag; - const uint8_t *kfileptr, *kfileend; - - if (!kpeginited) { kpeginited = 1; initkpeg(); } - - kfileptr = (uint8_t const *)kfilebuf; - kfileend = &kfileptr[kfilength]; - - if (B_UNBUF16(kfileptr) == B_LITTLE16(0xD8FFu)) kfileptr += 2; - else return -1; //"%s is not a JPEG file\n",filename - - restartinterval = 0; - for (i=0; i<4; i++) lastdc[i] = 0; - for (i=0; i<8; i++) hufcnt[i] = 0; - - kcoltype = 0; bitdepth = 8; //For PNGOUT - do - { - ch = *kfileptr++; if (ch != 255) continue; - do { marker = *kfileptr++; } - while (marker == 255); - if (marker != 0xd9) //Don't read past end of buffer - { - leng = ((intptr_t)kfileptr[0]<<8)+(intptr_t)kfileptr[1]-2; - kfileptr += 2; - } - //printf("fileoffs=%08x, marker=%02x,leng=%d",((int32_t)kfileptr)-((int32_t)kfilebuf)-2,marker,leng); - switch (marker) - { - case 0xc0: case 0xc1: case 0xc2: - //processit! - kfileptr++; //numbits = *kfileptr++; - - ydim = B_BIG16(B_UNBUF16(&kfileptr[0])); - xdim = B_BIG16(B_UNBUF16(&kfileptr[2])); - //printf("%s: %ld / %ld = %ld\n",filename,xdim*ydim*3,kfilength,(xdim*ydim*3)/kfilength); - - kp_frameplace = dakpframeplace; - kp_bytesperline = dakpbytesperline; - kp_xres = daxres; - kp_yres = dayres; - gnumcomponents = kfileptr[4]; - kfileptr += 5; - ghsampmax = gvsampmax = glhsampmax = glvsampmax = 0; - for (z=0; z>4); - gcompvsamp[z] = (kfileptr[1]&15); - gcompquantab[z] = kfileptr[2]; - for (i=0; i<8; i++) if (gcomphsamp[z] == pow2long[i]) { gcomphsampshift[z] = i; break; } - for (i=0; i<8; i++) if (gcompvsamp[z] == pow2long[i]) { gcompvsampshift[z] = i; break; } - if (gcomphsamp[z] > ghsampmax) { ghsampmax = gcomphsamp[z]; glhsampmax = gcomphsampshift[z]; } - if (gcompvsamp[z] > gvsampmax) { gvsampmax = gcompvsamp[z]; glvsampmax = gcompvsampshift[z]; } - kfileptr += 3; - } - - break; - case 0xc4: //Huffman table - do - { - ch = *kfileptr++; leng--; - if (ch >= 16) { index = ch-12; } - else { index = ch; } - Bmemcpy((void *)&hufnumatbit[index][1],(void const *)kfileptr,16); kfileptr += 16; - leng -= 16; - - v = 0; hufcnt[index] = 0; - hufquickcnt[index] = 0; - for (i=1; i<=16; i++) - { - hufmaxatbit[index][i] = v+hufnumatbit[index][i]; - hufvalatbit[index][i] = hufcnt[index]-v; - Bmemcpy((void *)&huftable[index][hufcnt[index]],(void const *)kfileptr,(int32_t)hufnumatbit[index][i]); - if (i <= 10) - for (c=0; c0; j--) - { - hufquickval[index][hufquickcnt[index]] = huftable[index][hufcnt[index]+c]; - hufquickbits[index][hufquickcnt[index]] = i; - hufquickcnt[index]++; - } - kfileptr += hufnumatbit[index][i]; - leng -= hufnumatbit[index][i]; - hufcnt[index] += hufnumatbit[index][i]; - v = ((v+hufnumatbit[index][i])<<1); - } - - } - while (leng > 0); - break; - case 0xdb: - do - { - ch = *kfileptr++; leng--; - index = (ch&15); - prec = (ch>>4); - for (z=0; z<64; z++) - { - v = (int32_t)(*kfileptr++); - if (prec) v = (v<<8)+((int32_t)(*kfileptr++)); - v <<= 19; - if (unzig[z]&7) v = mulshr24(v,cosqr16[unzig[z]&7 ]); - if (unzig[z]>>3) v = mulshr24(v,cosqr16[unzig[z]>>3]); - quantab[index][unzig[z]] = v; - } - leng -= 64; - if (prec) leng -= 64; - } - while (leng > 0); - break; - case 0xdd: - restartinterval = B_BIG16(B_UNBUF16(&kfileptr[0])); - kfileptr += leng; - break; - case 0xda: - if ((xdim <= 0) || (ydim <= 0)) { Xfree(dctbuf); return -1; } - - lnumcomponents = (int32_t)(*kfileptr++); if (!lnumcomponents) { Xfree(dctbuf); return -1; } - if (lnumcomponents > 1) kcoltype = 2; - for (z=0; z>4); - lcompac[z] = (kfileptr[1]&15); - kfileptr += 2; - } - - Ss = kfileptr[0]; - Se = kfileptr[1]; - Ah = (kfileptr[2]>>4); - Al = (kfileptr[2]&15); - kfileptr += 3; - //printf("passcnt=%d, Ss=%d, Se=%d, Ah=%d, Al=%d\n",passcnt,Ss,Se,Ah,Al); - - if ((!passcnt) && ((Ss) || (Se != 63) || (Ah) || (Al))) - { - for (z=zz=0; z>(glhsampmax+3)) << gcomphsampshift[z]; - dcty[z] = ((ydim+(gvsampmax<<3)-1)>>(glvsampmax+3)) << gcompvsampshift[z]; - zz += dctx[z]*dcty[z]; - } - z = zz*64*sizeof(int16_t); - dctbuf = (int16_t *)Xmalloc(z); if (!dctbuf) return -1; - Bmemset(dctbuf,0,z); - for (z=zz=0; z>glhstep); lcomphsamp[0] = min(lcomphsamp[0],glhstep); glhstep <<= 3; - glvstep = (gvsampmax>>glvstep); lcompvsamp[0] = min(lcompvsamp[0],glvstep); glvstep <<= 3; - lcomphvsamp0 = lcomphsamp[0]*lcompvsamp[0]; - - clipxdim = min(xdim,kp_xres); - clipydim = min(ydim,kp_yres); - - Alut[0] = (1<= kfilength) goto kpegrend_break2; //rest of file is missing! - - if (!dctbuf) dc = dct[0]; - for (c=0; c>lshy[c])*ldctx[c] + ((x+xx)>>lshx[c]))<<6]; - - //Get DC - if (!Ss) - { - KPEG_GETBITS(curbits, 16, num, kfileptr, kfileend); - if (!Ah) - { - i = ((num>>(curbits-10))&1023); - if (i < hufquickcnt[lcompdc[c]]) - { daval = hufquickval[lcompdc[c]][i]; curbits -= hufquickbits[lcompdc[c]][i]; } - else { huffgetval(lcompdc[c],curbits,num,&daval,&dabits); curbits -= dabits; } - - if (daval) - { - KPEG_GETBITS(curbits, daval, num, kfileptr, kfileend); - curbits -= daval; v = ((unsigned)num >> curbits) & pow2mask[daval]; - if (v <= pow2mask[daval-1]) v -= pow2mask[daval]; - lastdc[c] += v; - } - if (!dctbuf) dc[0] = lastdc[c]; else dcs[0] = (int16_t)(lastdc[c]<>(curbits-10))&1023); - if (i < hqcnt) - { daval = hqval[i]; curbits -= hqbits[i]; } - else { huffgetval(lcompac[c]+4,curbits,num,&daval,&dabits); curbits -= dabits; } - - zz = (daval>>4); daval &= 15; - if (daval) - { - if (Ah) - { - KPEG_GETBITS(curbits, 8, num, kfileptr, kfileend); - if (num&(pow2long[--curbits])) daval = Alut[0]; else daval = Alut[1]; - } - } - else if (zz < 15) - { - eobrun = pow2long[zz]; - if (zz) - { - KPEG_GETBITS(curbits, zz, num, kfileptr, kfileend); - curbits -= zz; eobrun += ((unsigned)num >> curbits) & pow2mask[zz]; - } - if (!Ah) eobrun--; - break; - } - if (Ah) - { - do - { - if (dcs[z]) - { - KPEG_GETBITS(curbits, 8, num, kfileptr, kfileend); - if (num&(pow2long[--curbits])) dcs[z] += (int16_t)Alut[dcs[z] < 0]; - } - else if (--zz < 0) break; - z++; - } - while (z <= Se); - if (daval) dcs[z] = (int16_t)daval; - } - else - { - z += zz; if (z > Se) break; - - KPEG_GETBITS(curbits, daval, num, kfileptr, kfileend); - curbits -= daval; v = ((unsigned)num >> curbits) & pow2mask[daval]; - if (daval>=1 /* FIXME ? */ && v <= pow2mask[daval-1]) v -= pow2mask[daval]; - dcflag |= dcflagor[z]; - if (!dctbuf) dc[unzig[z]] = v; else dcs[z] = (int16_t)(v< 0)) - { - eobrun--; - for (; z<=Se; z++) - { - if (!dcs[z]) continue; - KPEG_GETBITS(curbits, 8, num, kfileptr, kfileend); - if (num&(pow2long[--curbits])) dcs[z] += ((int16_t)Alut[dcs[z] < 0]); - } - } - - if (!dctbuf) - { - for (z=64-1; z>=0; z--) dc[z] *= quanptr[z]; - invdct8x8(dc,dcflag); dc += 64; - } - } - } - - if (!dctbuf) kplib_yrbrend_func(x,y,&dct[0][0]); - - restartcnt--; - if (!restartcnt) - { - kfileptr += 1-(curbits>>3); curbits = 0; - if ((kfileptr[-2] != 255) || (kfileptr[-1] != marker)) kfileptr--; - marker++; if (marker >= 0xd8) marker = 0xd0; - restartcnt = restartinterval; - for (i=0; i<4; i++) lastdc[i] = 0; - eobrun = 0; - } - } - kpegrend_break2:; - if (!dctbuf) return 0; - passcnt++; kfileptr -= ((curbits>>3)+1); break; - case 0xd9: break; - default: kfileptr += leng; break; - } - } - while (kfileptr-(uint8_t const *)kfilebuf < kfilength); - - if (!dctbuf) return 0; - - lnumcomponents = gnumcomponents; - for (i=0; i>lshy[c])*dctx[c] + ((x+xx)>>lshx[c]))<<6]; - quanptr = &quantab[gcompquantab[c]][0]; - for (z=0; z<64; z++) dc[z] = ((int32_t)dcs[zigit[z]])*quanptr[z]; - invdct8x8(dc,0xff); - } - kplib_yrbrend_func(x,y,&dct[0][0]); - } - - Xfree(dctbuf); return 0; -} - -//============================== KPEGILIB ends ============================== -//================================ GIF begins ================================ - -static uint8_t suffix[4100], filbuffer[768], tempstack[4096]; -static int32_t prefix[4100]; - -static int32_t kgifrend(const char *kfilebuf, int32_t kfilelength, - intptr_t dakpframeplace, int32_t dakpbytesperline, int32_t daxres, int32_t dayres) -{ - int32_t i, x, y, xsiz, ysiz, yinc, xend, xspan, yspan, currstr, numbitgoal; - int32_t lzcols, dat, blocklen, bitcnt, xoff, transcol; - intptr_t yoff; - char numbits, startnumbits, chunkind, ilacefirst; - const uint8_t *ptr, *cptr = NULL; - int32_t daglobxoffs = 0, daglobyoffs = 0; - - UNREFERENCED_PARAMETER(kfilelength); - - kcoltype = 3; bitdepth = 8; //For PNGOUT - - if ((kfilebuf[0] != 'G') || (kfilebuf[1] != 'I') || (kfilebuf[2] != 'F')) return -1; - paleng = (1<<((kfilebuf[10]&7)+1)); - ptr = (uint8_t const *)&kfilebuf[13]; - if (kfilebuf[10]&128) { cptr = ptr; ptr += paleng*3; } - transcol = -1; - while ((chunkind = *ptr++) == '!') - { - //! 0xf9 leng flags ? ? transcol - if (ptr[0] == 0xf9) { if (ptr[2]&1) transcol = (int32_t)(((uint8_t)ptr[5])); } - ptr++; - do { i = *ptr++; ptr += i; } - while (i); - } - if (chunkind != ',') return -1; - - xoff = B_LITTLE16(B_UNBUF16(&ptr[0])); - yoff = B_LITTLE16(B_UNBUF16(&ptr[2])); - xspan = B_LITTLE16(B_UNBUF16(&ptr[4])); - yspan = B_LITTLE16(B_UNBUF16(&ptr[6])); ptr += 9; - if (ptr[-1]&64) { yinc = 8; ilacefirst = 1; } - else { yinc = 1; ilacefirst = 0; } - if (ptr[-1]&128) - { - paleng = (1<<((ptr[-1]&7)+1)); - cptr = ptr; ptr += paleng*3; - } - - for (i=0; i= 0) palcol[transcol] &= B_LITTLE32(~0xff000000); - - //Handle GIF files with different logical&image sizes or non-0 offsets (added 05/15/2004) - xsiz = B_LITTLE16(B_UNBUF16(&kfilebuf[6])); - ysiz = B_LITTLE16(B_UNBUF16(&kfilebuf[8])); - if ((xoff != 0) || (yoff != 0) || (xsiz != xspan) || (ysiz != yspan)) - { - daglobxoffs += xoff; //Offset bitmap image by extra amount - daglobyoffs += yoff; - } - - xspan += daglobxoffs; - yspan += daglobyoffs; //UGLY HACK - y = daglobyoffs; - if ((uint32_t)y < (uint32_t)dayres) - { yoff = y*dakpbytesperline+dakpframeplace; x = daglobxoffs; xend = xspan; } - else - { x = daglobxoffs+0x80000000; xend = xspan+0x80000000; } - - lzcols = (1<<(*ptr)); startnumbits = (uint8_t)((*ptr)+1); ptr++; - for (i=lzcols-1; i>=0; i--) { suffix[i] = (uint8_t)(prefix[i] = i); } - currstr = lzcols+2; numbits = startnumbits; numbitgoal = (lzcols<<1); - blocklen = *ptr++; - Bmemcpy(filbuffer,ptr,blocklen); ptr += blocklen; - bitcnt = 0; - while (1) - { - dat = (B_LITTLE32(B_UNBUF32(&filbuffer[bitcnt>>3]))>>(bitcnt&7)) & (numbitgoal-1); - bitcnt += numbits; - if ((bitcnt>>3) > blocklen-3) - { - B_BUF16(filbuffer, B_UNBUF16(&filbuffer[bitcnt>>3])); - i = blocklen-(bitcnt>>3); - blocklen = (int32_t)*ptr++; - Bmemcpy(&filbuffer[i],ptr,blocklen); ptr += blocklen; - bitcnt &= 7; blocklen += i; - } - if (dat == lzcols) - { - currstr = lzcols+2; numbits = startnumbits; numbitgoal = (lzcols<<1); - continue; - } - if ((currstr == numbitgoal) && (numbits < 12)) - { numbits++; numbitgoal <<= 1; } - - prefix[currstr] = dat; - for (i=0; dat>=lzcols; dat=prefix[dat]) tempstack[i++] = suffix[dat]; - tempstack[i] = (uint8_t)prefix[dat]; - suffix[currstr-1] = suffix[currstr] = (uint8_t)dat; - - for (; i>=0; i--) - { - if ((uint32_t)x < (uint32_t)daxres) - B_BUF32((void *) (yoff+(x<<2)), palcol[(int32_t)tempstack[i]]); - x++; - if (x == xend) - { - y += yinc; - if (y >= yspan) - switch (yinc) - { - case 8: if (!ilacefirst) { y = daglobyoffs+2; yinc = 4; break; } - ilacefirst = 0; y = daglobyoffs+4; yinc = 8; break; - case 4: y = daglobyoffs+1; yinc = 2; break; - case 2: case 1: return 0; - } - if ((uint32_t)y < (uint32_t)dayres) - { yoff = y*dakpbytesperline+dakpframeplace; x = daglobxoffs; xend = xspan; } - else - { x = daglobxoffs+0x80000000; xend = xspan+0x80000000; } - } - } - currstr++; - } -} - -//=============================== GIF ends ================================== -//============================== CEL begins ================================= - -// //old .CEL format: -//int16_t id = 0x9119, xdim, ydim, xoff, yoff, id = 0x0008; -//int32_t imagebytes, filler[4]; -//char pal6bit[256][3], image[ydim][xdim]; -#ifdef KPCEL -static int32_t kcelrend(const char *buf, int32_t fleng, - intptr_t dakpframeplace, int32_t dakpbytesperline, int32_t daxres, int32_t dayres) -{ - int32_t i, x, y, x0, x1, y0, y1, xsiz, ysiz; - const char *cptr; - - UNREFERENCED_PARAMETER(fleng); - - kcoltype = 3; bitdepth = 8; paleng = 256; //For PNGOUT - - xsiz = (int32_t)B_LITTLE16(B_UNBUF16(&buf[2])); if (xsiz <= 0) return -1; - ysiz = (int32_t)B_LITTLE16(B_UNBUF16(&buf[4])); if (ysiz <= 0) return -1; - - cptr = &buf[32]; - for (i=0; i<256; i++) - { - palcol[i] = (((int32_t)cptr[0])<<18) + - (((int32_t)cptr[1])<<10) + - (((int32_t)cptr[2])<< 2) + B_LITTLE32(0xff000000); - cptr += 3; - } - - x0 = 0; x1 = xsiz; - y0 = 0; y1 = ysiz; - for (y=y0; y= 12) || (!((1< 32)) return -1; - if (header[17]&0xc0) return -1; - - fptr = (uint8_t const *)&header[header[0]+18]; - xsiz = (int32_t)B_LITTLE16(B_UNBUF16(&header[12])); if (xsiz <= 0) return -1; - ysiz = (int32_t)B_LITTLE16(B_UNBUF16(&header[14])); if (ysiz <= 0) return -1; - colbyte = ((((int32_t)header[16])+7)>>3); - - if (header[1] == 1) - { - pixbyte = ((((int32_t)header[7])+7)>>3); - cptr = &fptr[-B_LITTLE16(B_UNBUF16(&header[3]))*pixbyte]; - fptr += B_LITTLE16(B_UNBUF16(&header[5]))*pixbyte; - } - else pixbyte = colbyte; - - switch (pixbyte) // For PNGOUT - { - case 1: - kcoltype = 0; - bitdepth = 8; - palcol[0] = B_LITTLE32(0xff000000); - for (i = 1; i < 256; i++) palcol[i] = palcol[i - 1] + B_LITTLE32(0x10101); - break; - case 2: - case 3: kcoltype = 2; break; - case 4: kcoltype = 6; break; - } - - if (!(header[17]&16)) { x0 = 0; x1 = xsiz; xi = 1; } - else { x0 = xsiz-1; x1 = -1; xi =-1; } - if (header[17]&32) { y0 = 0; y1 = ysiz; yi = 1; pi = dakpbytesperline; } - else { y0 = ysiz-1; y1 = -1; yi =-1; pi =-dakpbytesperline; } - if (header[2] < 8) rlestat = -2; else rlestat = -1; - - p = y0*dakpbytesperline+dakpframeplace; - for (y=y0; y!=y1; y+=yi,p+=pi) - for (x=x0; x!=x1; x+=xi) - { - if (rlestat < 128) - { - if ((rlestat&127) == 127) { rlestat = (int32_t)fptr[0]; fptr++; } - if (header[1] == 1) - { - if (colbyte == 1) i = fptr[0]; - else i = (int32_t)B_LITTLE16(B_UNBUF16(&fptr[0])); - nptr = &cptr[i*pixbyte]; - } - else nptr = fptr; - - switch (pixbyte) - { - case 1: i = palcol[(int32_t)nptr[0]]; break; - case 2: i = (int32_t)B_LITTLE16(B_UNBUF16(&nptr[0])); - i = B_LITTLE32(((i&0x7c00)<<9) + ((i&0x03e0)<<6) + ((i&0x001f)<<3) + 0xff000000); - break; - case 3: i = (B_UNBUF32(&nptr[0])) | B_LITTLE32(0xff000000); break; - case 4: i = (B_UNBUF32(&nptr[0])); break; - } - fptr += colbyte; - } - if (rlestat >= 0) rlestat--; - - if (((uint32_t)x < (uint32_t)daxres) && ((uint32_t)y < (uint32_t)dayres)) - B_BUF32((void *) (x*4+p), i); - } - return 0; -} - -//============================== TARGA ends ================================= -//============================== BMP begins ================================= -//TODO: handle BI_RLE8 and BI_RLE4 (compression types 1&2 respectively) -// +---------------+ -// | 0(2): "BM" | -// +---------------------+| 10(4): rastoff| +------------------+ -// |headsiz=12 (OS/2 1.x)|| 14(4): headsiz| | All new formats: | -//++---------------------++-------------+-+-+------------------+-----------------------+ -//| 18(2): xsiz | 18(4): xsiz | -//| 20(2): ysiz | 22(4): ysiz | -//| 22(2): planes (always 1) | 26(2): planes (always 1) | -//| 24(2): cdim (1,4,8,24) | 28(2): cdim (1,4,8,16,24,32) | -//| if (cdim < 16) | 30(4): compression (0,1,2,3!?,4) | -//| 26(rastoff-14-headsiz): pal(bgr) | 34(4): (bitmap data size+3)&3 | -//| | 46(4): N colors (0=2^cdim) | -//| | if (cdim < 16) | -//| | 14+headsiz(rastoff-14-headsiz): pal(bgr0) | -//+---------------------+---------------+---------+------------------------------------+ -// | rastoff(?): bitmap data | -// +-------------------------+ -static int32_t kbmprend(const char *buf, int32_t fleng, - intptr_t dakpframeplace, int32_t dakpbytesperline, int32_t daxres, int32_t dayres) -{ - int32_t i, j, x, y, x0, x1, y0, y1, rastoff, headsiz, xsiz, ysiz, cdim, comp, cptrinc, *lptr; - const char *cptr; - - UNREFERENCED_PARAMETER(fleng); - - headsiz = B_UNBUF32(&buf[14]); - if (headsiz == (int32_t)B_LITTLE32(12)) //OS/2 1.x (old format) - { - if (B_UNBUF16(&buf[22]) != B_LITTLE16(1)) return -1; - xsiz = (int32_t)B_LITTLE16(B_UNBUF16(&buf[18])); - ysiz = (int32_t)B_LITTLE16(B_UNBUF16(&buf[20])); - cdim = (int32_t)B_LITTLE16(B_UNBUF16(&buf[24])); - comp = 0; - } - else //All newer formats... - { - if (B_UNBUF16(&buf[26]) != B_LITTLE16(1)) return -1; - xsiz = B_LITTLE32(B_UNBUF32(&buf[18])); - ysiz = B_LITTLE32(B_UNBUF32(&buf[22])); - cdim = (int32_t)B_LITTLE16(B_UNBUF16(&buf[28])); - comp = B_LITTLE32(B_UNBUF32(&buf[30])); - } - if ((xsiz <= 0) || (!ysiz)) return -1; - //cdim must be: (1,4,8,16,24,32) - if (((uint32_t)(cdim-1) >= (uint32_t)32) || (!((1<>1); - } - for (palcol[i+3]=0; palcol[i+3]<32; palcol[i+3]++) - { - if (!(j&1)) break; - j = (((uint32_t)j)>>1); - } - } - } - palcol[0] = 24-(palcol[0]+palcol[3]); - palcol[1] = 16-(palcol[1]+palcol[4]); - palcol[2] = 8-(palcol[2]+palcol[5]); - palcol[3] = (-(1<<(24-palcol[3]))&0x00ff0000); - palcol[4] = (-(1<<(16-palcol[4]))&0x0000ff00); - palcol[5] = (-(1<<(8-palcol[5]))&0x000000ff); - } - - cptrinc = (((xsiz*cdim+31)>>3)&~3); cptr = &buf[rastoff]; - if (ysiz < 0) { ysiz = -ysiz; } - else { cptr = &cptr[(ysiz-1)*cptrinc]; cptrinc = -cptrinc; } - - x0 = 0; x1 = xsiz; - y0 = 0; y1 = ysiz; - if (x1 > daxres) x1 = daxres; - for (y=y0; y= (uint32_t)dayres) continue; - lptr = (int32_t *)(y*dakpbytesperline+dakpframeplace); - switch (cdim) - { - case 1: for (x=x0; x>3]>>((x&7)^7))&1)]; break; - case 4: for (x=x0; x>1]>>(((x&1)^1)<<2))&15)]; break; - case 8: for (x=x0; x xsiz) { daxres = min(daxres,x1); x1 += bpl-xsiz; } - - j = nplanes-1; daxres <<= 2; x0 <<= 2; x1 <<= 2; x <<= 2; x += j; - if (nplanes == 1) //8-bit PCX - { - do - { - c = *cptr++; if (c < 192) i = 1; else { i = (c&63); c = *cptr++; } - j = palcol[(int32_t)c]; - for (; i; i--) - { - if ((uint32_t)y < (uint32_t)dayres) - if ((uint32_t)x < (uint32_t)daxres) B_BUF32((void *) (x+p), j); - x += 4; if (x >= x1) { x = x0; y++; p += dakpbytesperline; } - } - } - while (y < y1); - } - else if (nplanes == 3) //24-bit PCX - { - do - { - c = *cptr++; if (c < 192) i = 1; else { i = (c&63); c = *cptr++; } - for (; i; i--) - { - if ((uint32_t)y < (uint32_t)dayres) - if ((uint32_t)x < (uint32_t)daxres) *(char *)(x+p) = c; - x += 4; if (x >= x1) { j--; if (j < 0) { j = 3-1; y++; p += dakpbytesperline; } x = x0+j; } - } - } - while (y < y1); - } - - return 0; -} - -//=============================== PCX ends ================================== -//============================== DDS begins ================================= - -//Note:currently supports: DXT1,DXT2,DXT3,DXT4,DXT5,A8R8G8B8 - -#ifdef KPDDS -static int32_t kddsrend(const char *buf, int32_t leng, - intptr_t frameptr, int32_t bpl, int32_t xdim, int32_t ydim, int32_t xoff, int32_t yoff) -{ - int32_t x, y, z = 0, xx, yy, xsiz, ysiz, dxt, al[2], ai, k, v, c0, c1, stride; - intptr_t j; - uint32_t lut[256], r[4], g[4], b[4], a[8], rr, gg, bb; - uint8_t *uptr, *wptr; - - UNREFERENCED_PARAMETER(leng); - - xsiz = B_LITTLE32(B_UNBUF32(&buf[16])); - ysiz = B_LITTLE32(B_UNBUF32(&buf[12])); - if ((B_UNBUF32(&buf[80]))&B_LITTLE32(64)) //Uncompressed supports only A8R8G8B8 for now - { - if ((B_UNBUF32(&buf[88])) != B_LITTLE32(32)) return -1; - if ((B_UNBUF32(&buf[92])) != B_LITTLE32(0x00ff0000)) return -1; - if ((B_UNBUF32(&buf[96])) != B_LITTLE32(0x0000ff00)) return -1; - if ((B_UNBUF32(&buf[100])) != B_LITTLE32(0x000000ff)) return -1; - if ((B_UNBUF32(&buf[104])) != B_LITTLE32(0xff000000)) return -1; - buf += 128; - - j = yoff*bpl + (xoff<<2) + frameptr; xx = (xsiz<<2); - if (xoff < 0) { j -= (xoff<<2); buf -= (xoff<<2); xsiz += xoff; } - xsiz = (min(xsiz,xdim-xoff)<<2); ysiz = min(ysiz,ydim); - for (y=0; y= (uint32_t)ydim) continue; - Bmemcpy((void *)j,(void *)buf,xsiz); - } - return 0; - } - if (!((B_UNBUF32(&buf[80]))&B_LITTLE32(4))) return -1; //FOURCC invalid - dxt = buf[87]-'0'; - if ((buf[84] != 'D') || (buf[85] != 'X') || (buf[86] != 'T') || (dxt < 1) || (dxt > 5)) return -1; - buf += 128; - - if (!(dxt&1)) - { - for (z=256-1; z>0; z--) lut[z] = tabledivide32_noinline(255<<16, z); - lut[0] = (1<<16); - } - if (dxt == 1) stride = (xsiz<<1); else stride = (xsiz<<2); - - for (y=0; y>8)&0xf8); g[0] = ((c0>>3)&0xfc); b[0] = ((c0<<3)&0xfc); a[0] = 255; - c1 = B_LITTLE16(B_UNBUF16(&uptr[2])); - r[1] = ((c1>>8)&0xf8); g[1] = ((c1>>3)&0xfc); b[1] = ((c1<<3)&0xfc); a[1] = 255; - if ((c0 > c1) || (dxt != 1)) - { - r[2] = (((r[0]*2 + r[1] + 1)*(65536/3))>>16); - g[2] = (((g[0]*2 + g[1] + 1)*(65536/3))>>16); - b[2] = (((b[0]*2 + b[1] + 1)*(65536/3))>>16); a[2] = 255; - r[3] = (((r[0] + r[1]*2 + 1)*(65536/3))>>16); - g[3] = (((g[0] + g[1]*2 + 1)*(65536/3))>>16); - b[3] = (((b[0] + b[1]*2 + 1)*(65536/3))>>16); a[3] = 255; - } - else - { - r[2] = (r[0] + r[1])>>1; - g[2] = (g[0] + g[1])>>1; - b[2] = (b[0] + b[1])>>1; a[2] = 255; - r[3] = g[3] = b[3] = a[3] = 0; //Transparent - } - v = B_LITTLE32(B_UNBUF32(&uptr[4])); - if (dxt >= 4) - { - a[0] = uptr[-8]; a[1] = uptr[-7]; k = a[1]-a[0]; - if (k < 0) - { - z = a[0]*6 + a[1] + 3; - for (j=2; j<8; j++) { a[j] = ((z*(65536/7))>>16); z += k; } - } - else - { - z = a[0]*4 + a[1] + 2; - for (j=2; j<6; j++) { a[j] = ((z*(65536/5))>>16); z += k; } - a[6] = 0; a[7] = 255; - } - al[0] = B_LITTLE32(B_UNBUF32(&uptr[-6])); - al[1] = B_LITTLE32(B_UNBUF32(&uptr[-3])); - } - wptr = (uint8_t *)((y+yoff)*bpl + ((x+xoff)<<2) + frameptr); - ai = 0; - for (yy=0; yy<4; yy++,wptr+=bpl) - { - if ((uint32_t)(y+yy+yoff) >= (uint32_t)ydim) { ai += 4; continue; } - for (xx=0; xx<4; xx++,ai++) - { - if ((uint32_t)(x+xx+xoff) >= (uint32_t)xdim) continue; - - j = ((v>>(ai<<1))&3); - switch (dxt) - { - case 1: z = a[j]; break; - case 2: case 3: z = ((uptr[(ai>>1)-8] >> ((xx&1)<<2))&15)*17; break; - case 4: case 5: z = a[(al[yy>>1] >> ((ai&7)*3))&7]; break; - } - rr = r[j]; gg = g[j]; bb = b[j]; - if (!(dxt&1)) - { - bb = min((bb*lut[z])>>16,255); - gg = min((gg*lut[z])>>16,255); - rr = min((rr*lut[z])>>16,255); - } - wptr[(xx<<2)+0] = (uint8_t)bb; - wptr[(xx<<2)+1] = (uint8_t)gg; - wptr[(xx<<2)+2] = (uint8_t)rr; - wptr[(xx<<2)+3] = (uint8_t)z; - } - } - } - return 0; -} -#endif -//=============================== DDS ends ================================== -//=================== External picture interface begins ====================== - -static int32_t istarga(const uint8_t *buf, int32_t leng) -{ - return ((leng >= 19) && (!(buf[1]&0xfe)) && (buf[2] < 12) && ((1<