Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing DDS images using QImageWriter

Tags:

c++

qt

qimage

The documentation for Qt says that QImageWriter has a setCOmpression method and that this method depends on the chosen image format. For example, for tiff images, 0 means no compression and 1 means LZW compression. By the same token, I am setting my format to dds then call this method as follows;

QImageWriter writer(dir);
writer.setFormat("dds");
writer.setCompression(5);
bool saved = writer.write(image);

image is a QImage variable of course. But there is no compression in my resulting image. I use ImageMagick to manually compress and compressed image has lower size. Qt outputs a dds file but it is not compressed. So how can I set the compression. I chose 5 as a parameter expecting that it would set compression level to DXT5.

like image 749
meguli Avatar asked Apr 15 '26 22:04

meguli


1 Answers

A good option until Qt implements saving compressed DDS textures is to statically link SOIL2 into your project.

https://bitbucket.org/SpartanJ/soil2

You can use its save_image_as_DDS() function with a QImage like the following:

QImage *pTexture = [Your initial image]
AtlasTextureType eTextureType = [Your desired image type]

switch(eTextureType)
{
    case ATLASTEXTYPE_PNG: {
        if(false == pTexture->save("MyTexture.png")) {
            Log("failed to generate a PNG atlas", LOGTYPE_Error);
        }
    } break;

    case ATLASTEXTYPE_DTX5: {
        QImage imgProperlyFormatted = pTexture->convertToFormat(QImage::Format_RGBA8888);
        if(0 == SOIL_save_image_quality("MyTexture.dds",
                                        SOIL_SAVE_TYPE_DDS,
                                        imgProperlyFormatted.width(),
                                        imgProperlyFormatted.height(),
                                        4,
                                        imgProperlyFormatted.bits(),
                                        0))
        {
            Log("failed to generate a DTX5 atlas", LOGTYPE_Error);
        }
    } break;

    case ATLASTEXTYPE_DTX1: {
        QImage imgProperlyFormatted = pTexture->convertToFormat(QImage::Format_RGB888);
        if(0 == SOIL_save_image_quality("MyTexture.dds",
                                        SOIL_SAVE_TYPE_DDS,
                                        imgProperlyFormatted.width(),
                                        imgProperlyFormatted.height(),
                                        3,
                                        imgProperlyFormatted.bits(),
                                        0))
        {
            Log("failed to generate a DTX1 atlas", LOGTYPE_Error);
        }
    } break;
}
like image 193
Game_Overture Avatar answered Apr 18 '26 13:04

Game_Overture