Skip to content

Commit

Permalink
added pvrtc support
Browse files Browse the repository at this point in the history
  • Loading branch information
Volcore committed Sep 24, 2009
1 parent e5f6a07 commit 1988aaa
Show file tree
Hide file tree
Showing 3 changed files with 808 additions and 81 deletions.
97 changes: 16 additions & 81 deletions pvr.cc
Expand Up @@ -29,6 +29,18 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
uint32_t PackedData[2];
}AMTC_BLOCK_STRUCT;

extern void Decompress(AMTC_BLOCK_STRUCT *pCompressedData,
const int Do2bitMode,
const int XDim,
const int YDim,
const int AssumeImageTiles,
unsigned char* pResultImage);

/*******************************************************************************
This PVR code is loosely based on Wolfgang Engel's Oolong Engine:
Expand Down Expand Up @@ -76,83 +88,6 @@ PVRTexture::~PVRTexture()
free(this->data);
}

void get5554(uint16_t c, uint8_t& r, uint8_t& g, uint8_t& b, uint8_t& a, bool
first)
{
if(c&0x8000)
{
// opaque mode
r = (c>>10)&0x1f;
g = (c>> 5)&0x1f;
b = (c )&0x1f;
if(first)
b |= b>>4;
a = 0xf;
} else
{
// alpha mode
r = (c>>7)&0x1e;
r |= r>>4;

g = (c>>3)&0x1e;
g |= g>>4;

b = (c&0xf)<<1;

if(first)
b |= b>>3;
else
b |= b>>4;

a = (c>>11)&0xe;
}
}

void decompressPVRTC(uint8_t *target, uint8_t* data, bool highPrecision, int
width, int height)
{
// 4x4 texel block structure
uint64_t *blocks = (uint64_t*)data;

int bwidth = width>>2;
int bheight = height>>2;

for(int j=0; j<bheight; ++j)
for(int i=0; i<bwidth; ++i)
{
int bx = i<<2;
int by = j<<2;

uint64_t block = blocks[i+j*bwidth];
uint32_t colors = block>>32;
uint32_t moddata = block&0xffffffff;

uint16_t A = (colors&0x0000fffe);
uint16_t B = (colors&0xffff0000)>>16;
int modmode = colors&1;

uint8_t Ar, Ag, Ab, Aa;
get5554(A, Ar, Ag, Ab, Aa, true);

uint8_t Br, Bg, Bb, Ba;
get5554(B, Br, Bg, Bb, Ba, false);

for(int oy=0; oy<4; ++oy)
for(int ox=0; ox<4; ++ox)
{
int x = bx+ox;
int y = by+oy;
int baseindex = x+y*width;
int tidx = (baseindex*4);

target[tidx+0] = Ar<<3;
target[tidx+1] = Ag<<3;
target[tidx+2] = Ab<<3;
target[tidx+3] = Aa<<4;
}
}
}

ePVRLoadResult PVRTexture::load(const char *const path)
{
uint8_t *data;
Expand Down Expand Up @@ -383,13 +318,13 @@ ePVRLoadResult PVRTexture::load(const char *const path)
break;
case PVR_TYPE_PVRTC2:
{
decompressPVRTC(this->data, p, false, this->width,
this->height);
Decompress((AMTC_BLOCK_STRUCT*)p, 1, this->width,
this->height, 1, this->data);
} break;
case PVR_TYPE_PVRTC4:
{
decompressPVRTC(this->data, p, false, this->width,
this->height);
Decompress((AMTC_BLOCK_STRUCT*)p, 0, this->width,
this->height, 1, this->data);
} break;
default:
printf("unknown PVR type %i!\n", ptype);
Expand Down

0 comments on commit 1988aaa

Please sign in to comment.