vqenc non-square texture compression

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
bogglez
Moderator
Moderator
Posts: 578
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

vqenc non-square texture compression

Post by bogglez »

I've tried to allow non-square textures in vqenc, but I seem to output garbage for some reason.
I run "vqenc file.png", file.png is of dimensions 1024x512. No twiddle, no mipmap, just VQ compression.
My program shows a proper texture with "texconv -c -i image.png -o image.vq -f RGB565" (ignoring the first 16 bytes, they're the DTEX header).
Any obvious mistakes? I didn't grasp the whole program quite yet.

Code: Select all

diff --git a/utils/vqenc/vqenc.c b/utils/vqenc/vqenc.c
index bf07146..a72663e 100644
--- a/utils/vqenc/vqenc.c
+++ b/utils/vqenc/vqenc.c
@@ -741,13 +741,13 @@ static int encode(const char *infile) {
         return -EINVAL;
     } */
 
-    if(image.w != image.h) {
-        fprintf(stderr, "%s is not a square image\n", infile);
+    if((use_twiddle || use_mipmap) && image.w != image.h) {
+        fprintf(stderr, "%s is not a square image, it cannot be twiddled or mipmapped\n", infile);
         destroy_image(&image);
         return -EINVAL;
     }
 
-    if(valid_size(image.w) == 0) {
+    if(valid_size(image.w) == 0 || valid_size(image.h) == 0) {
         fprintf(stderr, "image dimensions for %s are not valid, see manual\n", infile);
         destroy_image(&image);
         return -EINVAL;
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: vqenc non-square texture compression

Post by PH3NOM »

Out of curiosity, yesterday I decided to start writing a VQ compression routine, based on the PVR VQ format.

Basically, there is a 2048 byte codebook, or palette, that stores the pixels, the actual texture is encoded as 8 bit indices into the palette.

The complex part is constructing the codebook.
My algorithm, using plain c and no external libs, stores the source pixels in a sorted array, discarding duplicate pixels.
From the sorted array, the pixels are averaged until they fit into the codebook.

Results are surprisingly good! In almost all cases, my VQ compression algorithm beats the downsampled texture, with better compression!

The Left Texture is uncompressed. Middle Texture is 1/2 resolution. Right Texture is VQ compressed:

Image

Image

Image

Image

Image
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: vqenc non-square texture compression

Post by bogglez »

Hehe, I hope you had fun playing around with that :)
The choice of codebook is indeed "complex", in fact it is NP-hard: http://www.mqasem.net/vectorquantization/vq.html
That said I don't know how much quality can be gained by implementing a more complex converter compared to the simple solution you chose.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Post Reply