3

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.

meguli
  • 1,426
  • 1
  • 18
  • 35

2 Answers2

3

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;
}
Game_Overture
  • 1,578
  • 3
  • 22
  • 34
1

There is no compression because nobody implemented it yet in the Qt version you're using. File a feature request if the feature isn't implemented in most recent Qt, patch your copy of Qt to add this feature (possibly as a backport) and please submit your change for inclusion in Qt if the most recent version doesn't implement it:)

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313