⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddslib.cpp

📁 wowmodelview魔兽世界的模型查看工具。下了看看吧
💻 CPP
📖 第 1 页 / 共 2 页
字号:

static void DDSDecodeAlpha3BitLinear( unsigned int *pixel, ddsAlphaBlock3BitLinear_t *alphaBlock, int width, unsigned int alphaZero )
{
	
	int					row, pix;
	unsigned int		stuff;
	unsigned char		bits[ 4 ][ 4 ];
	unsigned short		alphas[ 8 ];
	ddsColor_t			aColors[ 4 ][ 4 ];
	
	
	/* get initial alphas */
	alphas[ 0 ] = alphaBlock->alpha0;
	alphas[ 1 ] = alphaBlock->alpha1;
	
	/* 8-alpha block */
	if( alphas[ 0 ] > alphas[ 1 ] )
	{
		/* 000 = alpha_0, 001 = alpha_1, others are interpolated */
		alphas[ 2 ] = ( 6 * alphas[ 0 ] +     alphas[ 1 ]) / 7;	/* bit code 010 */
		alphas[ 3 ] = ( 5 * alphas[ 0 ] + 2 * alphas[ 1 ]) / 7;	/* bit code 011 */
		alphas[ 4 ] = ( 4 * alphas[ 0 ] + 3 * alphas[ 1 ]) / 7;	/* bit code 100 */
		alphas[ 5 ] = ( 3 * alphas[ 0 ] + 4 * alphas[ 1 ]) / 7;	/* bit code 101 */
		alphas[ 6 ] = ( 2 * alphas[ 0 ] + 5 * alphas[ 1 ]) / 7;	/* bit code 110 */
		alphas[ 7 ] = (     alphas[ 0 ] + 6 * alphas[ 1 ]) / 7;	/* bit code 111 */
	}
	
	/* 6-alpha block */
	else
	{ 
		/* 000 = alpha_0, 001 = alpha_1, others are interpolated */
		alphas[ 2 ] = (4 * alphas[ 0 ] +     alphas[ 1 ]) / 5;	/* bit code 010 */
		alphas[ 3 ] = (3 * alphas[ 0 ] + 2 * alphas[ 1 ]) / 5;	/* bit code 011 */
		alphas[ 4 ] = (2 * alphas[ 0 ] + 3 * alphas[ 1 ]) / 5;	/* bit code 100 */
		alphas[ 5 ] = (    alphas[ 0 ] + 4 * alphas[ 1 ]) / 5;	/* bit code 101 */
		alphas[ 6 ] = 0;										/* bit code 110 */
		alphas[ 7 ] = 255;										/* bit code 111 */
	}
	
	/* decode 3-bit fields into array of 16 bytes with same value */
	
	/* first two rows of 4 pixels each */
	stuff = *((unsigned int*) &(alphaBlock->stuff[ 0 ]));
	
	bits[ 0 ][ 0 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 0 ][ 1 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 0 ][ 2 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 0 ][ 3 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 1 ][ 0 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 1 ][ 1 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 1 ][ 2 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 1 ][ 3 ] = (unsigned char) (stuff & 0x00000007);
	
	/* last two rows */
	stuff = *((unsigned int*) &(alphaBlock->stuff[ 3 ])); /* last 3 bytes */
	
	bits[ 2 ][ 0 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 2 ][ 1 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 2 ][ 2 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 2 ][ 3 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 3 ][ 0 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 3 ][ 1 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 3 ][ 2 ] = (unsigned char) (stuff & 0x00000007);
	stuff >>= 3;
	bits[ 3 ][ 3 ] = (unsigned char) (stuff & 0x00000007);
	
	/* decode the codes into alpha values */
	for( row = 0; row < 4; row++ )
	{
		for( pix=0; pix < 4; pix++ )
		{
			aColors[ row ][ pix ].r = 0;
			aColors[ row ][ pix ].g = 0;
			aColors[ row ][ pix ].b = 0;
			aColors[ row ][ pix ].a = (unsigned char) alphas[ bits[ row ][ pix ] ];
		}
	}
	
	/* write out alpha values to the image bits */
	for( row = 0; row < 4; row++, pixel += width-4 )
	{
		for( pix = 0; pix < 4; pix++ )
		{
			/* zero the alpha bits of image pixel */
			*pixel &= alphaZero;
			
			/* or the bits into the prev. nulled alpha */
			*pixel |= *((unsigned int*) &(aColors[ row ][ pix ]));	
			pixel++;
		}
	}
}



/*
DDSDecompressDXT1()
decompresses a dxt1 format texture
*/

int DDSDecompressDXT1( unsigned char *src, int width, int height, unsigned char *dest )
{
	int				x, y, xBlocks, yBlocks;
	unsigned int	*pixel;
	ddsColorBlock_t	*block;
	ddsColor_t		colors[ 4 ];
	
	
	/* setup */
	xBlocks = width / 4;
	yBlocks = height / 4;
	
	/* walk y */
	for( y = 0; y < yBlocks; y++ )
	{
		/* 8 bytes per block */
		block = (ddsColorBlock_t*) ((unsigned int) src + y * xBlocks * 8);

		/* walk x */
		for( x = 0; x < xBlocks; x++, block++ )
		{
			DDSGetColorBlockColors( block, colors );
			pixel = (unsigned int*) (dest + x * 16 + (y * 4) * width * 4);
			DDSDecodeColorBlock( pixel, block, width, (unsigned int*) colors );
		}
	}
	
	/* return ok */
	return 0;
}



/*
DDSDecompressDXT3()
decompresses a dxt3 format texture
*/

int DDSDecompressDXT3(unsigned char *src, int width, int height, unsigned char *dest )
{
	int						x, y, xBlocks, yBlocks;
	unsigned int			*pixel, alphaZero;
	ddsColorBlock_t			*block;
	ddsAlphaBlockExplicit_t	*alphaBlock;
	ddsColor_t				colors[ 4 ];


	/* setup */
	xBlocks = width / 4;
	yBlocks = height / 4;
	
	/* create zero alpha */
	colors[ 0 ].a = 0;
	colors[ 0 ].r = 0xFF;
	colors[ 0 ].g = 0xFF;
	colors[ 0 ].b = 0xFF;
	alphaZero = *((unsigned int*) &colors[ 0 ]);
	
	/* walk y */
	for( y = 0; y < yBlocks; y++ )
	{
		/* 8 bytes per block, 1 block for alpha, 1 block for color */
		block = (ddsColorBlock_t*) ((unsigned int) src + y * xBlocks * 16);

		/* walk x */
		for( x = 0; x < xBlocks; x++, block++ )
		{
			/* get alpha block */
			alphaBlock = (ddsAlphaBlockExplicit_t*) block;
			
			/* get color block */
			block++;
			DDSGetColorBlockColors( block, colors );
			
			/* decode color block */
			pixel = (unsigned int*) (dest + x * 16 + (y * 4) * width * 4);
			DDSDecodeColorBlock( pixel, block, width, (unsigned int*) colors );
			
			/* overwrite alpha bits with alpha block */
			DDSDecodeAlphaExplicit( pixel, alphaBlock, width, alphaZero );
		}
	}
	
	/* return ok */
	return 0;
}



/*
DDSDecompressDXT5()
decompresses a dxt5 format texture
*/

static int DDSDecompressDXT5( ddsBuffer_t *dds, int width, int height, unsigned char *dest )
{
	int							x, y, xBlocks, yBlocks;
	unsigned int				*pixel, alphaZero;
	ddsColorBlock_t				*block;
	ddsAlphaBlock3BitLinear_t	*alphaBlock;
	ddsColor_t					colors[ 4 ];


	/* setup */
	xBlocks = width / 4;
	yBlocks = height / 4;
	
	/* create zero alpha */
	colors[ 0 ].a = 0;
	colors[ 0 ].r = 0xFF;
	colors[ 0 ].g = 0xFF;
	colors[ 0 ].b = 0xFF;
	alphaZero = *((unsigned int*) &colors[ 0 ]);
	
	/* walk y */
	for( y = 0; y < yBlocks; y++ )
	{
		/* 8 bytes per block, 1 block for alpha, 1 block for color */
		block = (ddsColorBlock_t*) ((unsigned int) dds->data + y * xBlocks * 16);

		/* walk x */
		for( x = 0; x < xBlocks; x++, block++ )
		{
			/* get alpha block */
			alphaBlock = (ddsAlphaBlock3BitLinear_t*) block;
			
			/* get color block */
			block++;
			DDSGetColorBlockColors( block, colors );
			
			/* decode color block */
			pixel = (unsigned int*) (dest + x * 16 + (y * 4) * width * 4);
			DDSDecodeColorBlock( pixel, block, width, (unsigned int*) colors );
			
			/* overwrite alpha bits with alpha block */
			DDSDecodeAlpha3BitLinear( pixel, alphaBlock, width, alphaZero );
		}
	}
	
	/* return ok */
	return 0;
}



/*
DDSDecompressDXT2()
decompresses a dxt2 format texture (fixme: un-premultiply alpha)
*/

/*
static int DDSDecompressDXT2( ddsBuffer_t *dds, int width, int height, unsigned char *pixels )
{
	int		r;
	
	
	// decompress dxt3 first //
	r = DDSDecompressDXT3( dds, width, height, pixels );
	
	// return to sender //
	return r;
}
*/



/*
DDSDecompressDXT4()
decompresses a dxt4 format texture (fixme: un-premultiply alpha)
*/

static int DDSDecompressDXT4( ddsBuffer_t *dds, int width, int height, unsigned char *pixels )
{
	int		r;
	
	/* decompress dxt5 first */
	r = DDSDecompressDXT5( dds, width, height, pixels );
	
	/* return to sender */
	return r;
}



/*
DDSDecompressARGB8888()
decompresses an argb 8888 format texture
*/

static int DDSDecompressARGB8888( ddsBuffer_t *dds, int width, int height, unsigned char *pixels )
{
	int							x, y;
	unsigned char				*in, *out;
	
	
	/* setup */
	in = dds->data;
	out = pixels;
	
	/* walk y */
	for( y = 0; y < height; y++ )
	{
		/* walk x */
		for( x = 0; x < width; x++ )
		{
			*out++ = *in++;
			*out++ = *in++;
			*out++ = *in++;
			*out++ = *in++;
		}
	}
	
	/* return ok */
	return 0;
}



///*
//DDSDecompress()
//decompresses a dds texture into an rgba image buffer, returns 0 on success
//*/
//
//int DDSDecompress( ddsBuffer_t *dds, unsigned char *pixels )
//{
//	int			width, height, r;
//	ddsPF_t		pf;
//	
//	
//	/* get dds info */
//	r = DDSGetInfo( dds, &width, &height, &pf );
//	if( r )
//		return r;
//	
//	/* decompress */
//	switch( pf )
//	{
//		case DDS_PF_ARGB8888:
//			/* fixme: support other [a]rgb formats */
//			r = DDSDecompressARGB8888( dds, width, height, pixels );
//			break;
//		/*
//		case DDS_PF_DXT1:
//			r = DDSDecompressDXT1( dds, width, height, pixels );
//			break;
//		*/
//		/*
//		case DDS_PF_DXT2:
//			r = DDSDecompressDXT2( dds, width, height, pixels );
//			break;
//		*/
//		/*
//		case DDS_PF_DXT3:
//			r = DDSDecompressDXT3( dds, width, height, pixels );	
//			break;
//		*/
//		
//		case DDS_PF_DXT4:
//			r = DDSDecompressDXT4( dds, width, height, pixels );
//			break;
//		
//		case DDS_PF_DXT5:
//			r = DDSDecompressDXT5( dds, width, height, pixels );		
//			break;
//		
//		default:
//		case DDS_PF_UNKNOWN:
//			memset( pixels, 0xFF, width * height * 4 );
//			r = -1;
//			break;
//	}
//	
//	/* return to sender */
//	return r;
//}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -