DXT5 data to rgba image converter

Hello all.

I am currently trying to create a "data" to rgba image viewer. My DXT5 data is in the form of a pak file. I am trying to upload a pak file into my program, and then call function BlockDecompressImageDXT5, run the pak through it and get an image, which I'm guessing I will probably have to save to a file (png) in some way.

I need help figuring out how I should:
1. Upload my pak file
2. Call my function (i'm confused bc there are pointers involved)
3. I need a lib for saving a image (png) to a file.

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
/**
 * unsigned long PackRGBA(): Helper method that packs RGBA channels into a single 4 byte pixel.
 *
 * unsigned char r: red channel.
 * unsigned char g: green channel.
 * unsigned char b: blue channel.
 * unsigned char a: alpha channel.
 */
unsigned long PackRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
	return ((r << 24) | (g << 16) | (b << 8) | a);
}


/**
 * void DecompressBlockDXT5(): Decompresses one block of a DXT5 texture and stores the resulting pixels at the appropriate offset in 'image'.
 *
 * unsigned long x:						x-coordinate of the first pixel in the block.
 * unsigned long y:						y-coordinate of the first pixel in the block.
 * unsigned long width: 				width of the texture being decompressed.
 * unsigned long height:				height of the texture being decompressed.
 * const unsigned char *blockStorage:	pointer to the block to decompress.
 * unsigned long *image:				pointer to image where the decompressed pixel data should be stored.
 */
void DecompressBlockDXT5(unsigned long x, unsigned long y, unsigned long width, const unsigned char *blockStorage, unsigned long *image) {
	unsigned char alpha0 = *reinterpret_cast<const unsigned char *>(blockStorage);
	unsigned char alpha1 = *reinterpret_cast<const unsigned char *>(blockStorage + 1);
	
	const unsigned char *bits = blockStorage + 2;
	unsigned long alphaCode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24);
	unsigned short alphaCode2 = bits[0] | (bits[1] << 8);
	
	unsigned short color0 = *reinterpret_cast<const unsigned short *>(blockStorage + 8);
	unsigned short color1 = *reinterpret_cast<const unsigned short *>(blockStorage + 10);	
	
	unsigned long temp;
	
	temp = (color0 >> 11) * 255 + 16;
	unsigned char r0 = (unsigned char)((temp/32 + temp)/32);
	temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
	unsigned char g0 = (unsigned char)((temp/64 + temp)/64);
	temp = (color0 & 0x001F) * 255 + 16;
	unsigned char b0 = (unsigned char)((temp/32 + temp)/32);
	
	temp = (color1 >> 11) * 255 + 16;
	unsigned char r1 = (unsigned char)((temp/32 + temp)/32);
	temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
	unsigned char g1 = (unsigned char)((temp/64 + temp)/64);
	temp = (color1 & 0x001F) * 255 + 16;
	unsigned char b1 = (unsigned char)((temp/32 + temp)/32);
	
	unsigned long code = *reinterpret_cast<const unsigned long *>(blockStorage + 12);
	
	for (int j=0; j < 4; j++) {
		for (int i=0; i < 4; i++) {
			int alphaCodeIndex = 3*(4*j+i);
			int alphaCode;
			
			if (alphaCodeIndex <= 12) {
				alphaCode = (alphaCode2 >> alphaCodeIndex) & 0x07;
			} else if (alphaCodeIndex == 15) {
				alphaCode = (alphaCode2 >> 15) | ((alphaCode1 << 1) & 0x06);
			} else { // alphaCodeIndex >= 18 && alphaCodeIndex <= 45
				alphaCode = (alphaCode1 >> (alphaCodeIndex - 16)) & 0x07;
			}
			
			unsigned char finalAlpha;
			if (alphaCode == 0) {
				finalAlpha = alpha0;
			} else if (alphaCode == 1) {
				finalAlpha = alpha1;
			} else {
				if (alpha0 > alpha1) {
					finalAlpha = ((8-alphaCode)*alpha0 + (alphaCode-1)*alpha1)/7;
				} else {
					if (alphaCode == 6) {
						finalAlpha = 0;
					} else if (alphaCode == 7) {
						finalAlpha = 255;
					} else {
						finalAlpha = ((6-alphaCode)*alpha0 + (alphaCode-1)*alpha1)/5;
					}
				}
			}
			
			unsigned char colorCode = (code >> 2*(4*j+i)) & 0x03;
			
			unsigned long finalColor;
			switch (colorCode) {
				case 0:
					finalColor = PackRGBA(r0, g0, b0, finalAlpha);
					break;
				case 1:
					finalColor = PackRGBA(r1, g1, b1, finalAlpha);
					break;
				case 2:
					finalColor = PackRGBA((2*r0+r1)/3, (2*g0+g1)/3, (2*b0+b1)/3, finalAlpha);
					break;
				case 3:
					finalColor = PackRGBA((r0+2*r1)/3, (g0+2*g1)/3, (b0+2*b1)/3, finalAlpha);
					break;
			}
			
			if (x + i < width) image[(y + j)*width + (x + i)] = finalColor;
		}
	}
}

/**
 * void BlockDecompressImageDXT5(): Decompresses all the blocks of a DXT5 compressed texture and stores the resulting pixels in 'image'.
 *
 * unsigned long width:               Texture width.
 * unsigned long height:              Texture height.
 * const unsigned char *blockStorage: pointer to compressed DXT5 blocks.
 * unsigned long *image:              pointer to the image where the decompressed pixels will be stored.
 */

void BlockDecompressImageDXT5(unsigned long width, unsigned long height, const unsigned char *blockStorage, unsigned long *image)
{
	unsigned long blockCountX = (width + 3) / 4;
	unsigned long blockCountY = (height + 3) / 4;
	unsigned long blockWidth = (width < 4) ? width : 4;
	unsigned long blockHeight = (height < 4) ? height : 4;
 
	for (unsigned long j = 0; j < blockCountY; j++) {
		for (unsigned long i = 0; i < blockCountX; i++) DecompressBlockDXT5(i*4, j*4, width, blockStorage + i * 16, image);
		blockStorage += blockCountX * 16;
	}
}


Thank you, I have been working on this project for months in multiple coding languages and I think I am finally getting close to a working tool.
Last edited on
It's hard to help when your link to the code doesn't work.
Linked worked for me, but I just put the code at the bottom. I'm trying to call BlockDecompressImageDXT5
Last edited on
1. Upload my pak file
So what are you doing for this?

> BlockDecompressImageDXT5(unsigned long width, unsigned long height, const unsigned char *blockStorage, unsigned long *image)
Somewhere in the process of extracting your pak file, you figure out what the image width and height is.
From that, you can allocate the space for your image pointer.

And blockStorage points to your extracted data.


3. I need a lib for saving a image (png) to a file.
libpng is the thing you want here.

These are great tips on my steps, I am going to start developing it. I'm gonna have the hardest time with the pointer, but hopefully I can figure it out. If I run into a problem, I will keep this thread alive.


Thanks
Ok, I think I have figured out a couple things, but I need some help with making my uploaded pak file equal blockStorage.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {

    FILE* pFile;
    pFile = fopen("C:\Users\erast\Desktop\encrypt.pak", "r");
    
    unsigned long x = 0;
    unsigned long y = 0;
    unsigned long width = 512;
    unsigned long height = 512;
    const unsigned char* blockStorage = reinterpret_cast<const unsigned char*>(pFile.data());
    unsigned long* image = new unsigned long[width * height];

    
    BlockDecompressImageDXT5(width, height, blockStorage, image);
      


- I'm trying to upload my file (it's in a pak format)
- Take that file, make it blockStorage
- Call BlockDecompressImageDXT5 and run the file through that


I'm not sure if I am uploading my file correctly, and I am confused if I am making the file into blockStorage correctly. I think I should figure out these things first, and make sure that my program is calling the function and inputting the pak file correctly before I try to save the output as an image.

Last edited on
Ok, I have figured this out, all I need help with now is png.h. I need to write a image from the image data I got, and I'm really confused on how to do this, also the png.h documentation is really bad
https://www.file-extensions.org/pak-file-extension
The first thing to establish is whether your 'pak' file is really just a renamed zip file, or whether it's something completely novel.

Make a copy of it, rename it to 'test.zip' and see if your regular unzip program can make sense of it.

If that's OK, then this is worth a read.
https://stackoverflow.com/questions/11611950/unzip-a-zip-file-using-zlib

If your unzip barfs on it, then you need to find out more information (like talk to the person who gave you the pak file).

> pFile = fopen("C:\Users\erast\Desktop\encrypt.pak", "r");
1. All your \ should be written as \\
2. Opening a data file on Windows should always be done using the "rb" mode (and "wb" when writing).

That it's called 'encrypt.pak' has me wondering - is there something else going on as well.
Registered users can post here. Sign in or register to post.