Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (C) 2013 by Brendan G Bohannon
- Email: cr88192@gmail.com
- Copying: http://pastebin.com/iJxtZHm6
- */
- void BGBBTJ_BCn_EncodeBlockDXT1F(byte *block,
- byte *rgba, int xstride, int ystride, int nclr)
- {
- byte clr0[4], clr1[4];
- byte pxy[16];
- int p0, p1, p2, p3;
- int mcr, mcg, mcb, mcy;
- int ncr, ncg, ncb, ncy;
- int acr, acg, acb;
- int cr, cg, cb, cy, acy;
- int i, j, k;
- mcr=255; mcg=255; mcb=255; mcy=255;
- ncr= 0; ncg= 0; ncb= 0; ncy= 0;
- acr= 0; acg= 0; acb= 0;
- for(i=0; i<4; i++)
- for(j=0; j<4; j++)
- {
- k=(i*ystride)+(j*xstride);
- cr=rgba[k+0];
- cg=rgba[k+1];
- cb=rgba[k+2];
- cy=(cr+2*cg+cb)>>2;
- pxy[i*4+j]=cy;
- if(cy<mcy) { mcr=cr; mcg=cg; mcb=cb; mcy=cy; }
- if(cy>ncy) { ncr=cr; ncg=cg; ncb=cb; ncy=cy; }
- acr+=cr;
- acg+=cg;
- acb+=cb;
- }
- acr=acr>>4;
- acg=acg>>4;
- acb=acb>>4;
- clr0[0]=(mcr*11+acr*5)>>4;
- clr0[1]=(mcg*11+acg*5)>>4;
- clr0[2]=(mcb*11+acb*5)>>4;
- clr1[0]=(ncr*11+acr*5)>>4;
- clr1[1]=(ncg*11+acg*5)>>4;
- clr1[2]=(ncb*11+acb*5)>>4;
- block[0]=((clr0[1]<<3)&0xE0) | (clr0[2]>>3);
- block[1]=(clr0[0]&0xF8) | (clr0[1]>>5);
- block[2]=((clr1[1]<<3)&0xE0) | (clr1[2]>>3);
- block[3]=(clr1[0]&0xF8) | (clr1[1]>>5);
- clr0[3]=(clr0[0]+2*clr0[1]+clr0[2])>>2;
- clr1[3]=(clr1[0]+2*clr1[1]+clr1[2])>>2;
- acy=(clr0[3]+clr1[3])>>1;
- p0=-((acy-pxy[ 0])>>16);
- p1=-((acy-pxy[ 1])>>16);
- p2=-((acy-pxy[ 2])>>16);
- p3=-((acy-pxy[ 3])>>16);
- block[4]=(p3<<6)|(p2<<4)|(p1<<2)|p0;
- p0=-((acy-pxy[ 4])>>16);
- p1=-((acy-pxy[ 5])>>16);
- p2=-((acy-pxy[ 6])>>16);
- p3=-((acy-pxy[ 7])>>16);
- block[5]=(p3<<6)|(p2<<4)|(p1<<2)|p0;
- p0=-((acy-pxy[ 8])>>16);
- p1=-((acy-pxy[ 9])>>16);
- p2=-((acy-pxy[10])>>16);
- p3=-((acy-pxy[11])>>16);
- block[6]=(p3<<6)|(p2<<4)|(p1<<2)|p0;
- p0=-((acy-pxy[12])>>16);
- p1=-((acy-pxy[13])>>16);
- p2=-((acy-pxy[14])>>16);
- p3=-((acy-pxy[15])>>16);
- block[7]=(p3<<6)|(p2<<4)|(p1<<2)|p0;
- }
- BGBBTJ_API void BGBBTJ_BCn_EncodeImageDXT1F(byte *block,
- byte *rgba, int xs, int ys, int stride)
- {
- int xs1, ys1;
- int i, j;
- xs1=xs>>2; ys1=ys>>2;
- for(i=0; i<ys1; i++)
- for(j=0; j<xs1; j++)
- {
- BGBBTJ_BCn_EncodeBlockDXT1F(
- block+(i*xs1+j)*8,
- rgba+(i*4*xs+j*4)*stride,
- stride, xs*stride, 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement