I found a method:
1) texture loading by means of QtOpenGL;
2) receiving QImage from QGLPixelBuffer;
3) change of an order of channels;
4) image writing in the file means of API for Nvidia Texture Tools 2.
But I had a problem on 1 step - I am not sure that correctly I load a texture because the received image contains only black color aka Malevich's Black Square.
ddstexture.h
#ifndef DDSTEXTURE_H
#define DDSTEXTURE_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QImage>
#include <QDebug>
#include <nvtt/include/nvtt.h>
{
Q_OBJECT
private:
struct DDS_PIXELFORMAT{
unsigned long Size;
unsigned long Flags;
unsigned long FourCC;
unsigned long RGBBitCount;
unsigned long RBitMask;
unsigned long GBitMask;
unsigned long BBitMask;
unsigned long ABitMask;
};
typedef struct{
unsigned long Size;
unsigned long Flags;
unsigned long Height;
unsigned long Width;
unsigned long PitchOrLinearSize;
unsigned long Depth;
unsigned long MipMapCount;
unsigned long Reserved1[11];
DDS_PIXELFORMAT ddspf;
unsigned long Caps;
unsigned long Caps2;
unsigned long Caps3;
unsigned long Caps4;
unsigned long Reserved2;
}DDS_HEADER;
public:
explicit DDSTexture
(QWidget *parent
= 0);
~DDSTexture();
signals:
public slots:
};
#endif // DDSTEXTURE_H
#ifndef DDSTEXTURE_H
#define DDSTEXTURE_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QImage>
#include <QDebug>
#include <nvtt/include/nvtt.h>
class DDSTexture : public QGLWidget
{
Q_OBJECT
private:
struct DDS_PIXELFORMAT{
unsigned long Size;
unsigned long Flags;
unsigned long FourCC;
unsigned long RGBBitCount;
unsigned long RBitMask;
unsigned long GBitMask;
unsigned long BBitMask;
unsigned long ABitMask;
};
typedef struct{
unsigned long Size;
unsigned long Flags;
unsigned long Height;
unsigned long Width;
unsigned long PitchOrLinearSize;
unsigned long Depth;
unsigned long MipMapCount;
unsigned long Reserved1[11];
DDS_PIXELFORMAT ddspf;
unsigned long Caps;
unsigned long Caps2;
unsigned long Caps3;
unsigned long Caps4;
unsigned long Reserved2;
}DDS_HEADER;
QSize p_getDDSSize(const QString &path);
QImage p_getImageFromDDS(const QString &path);
QImage image;
QGLPixelBuffer *pbuffer;
public:
explicit DDSTexture(QWidget *parent = 0);
~DDSTexture();
signals:
public slots:
};
#endif // DDSTEXTURE_H
To copy to clipboard, switch view to plain text mode
ddstexture.cpp
#include "ddstexture.h"
{
DDS_HEADER DDSHeader = {0};
DDSHeader.Size = sizeof(DDS_HEADER);
ddsFile.seek(4);
ddsFile.read((char*)&DDSHeader, sizeof(DDS_HEADER));
}
ddsFile.close();
qDebug() << "height = " << DDSHeader.Height;
qDebug() << "width = " << DDSHeader.Width;
qDebug() << "size = " << DDSHeader.Size;
return QSize(DDSHeader.
Width, DDSHeader.
Height);
}
{
makeCurrent();
pbuffer->bindTexture(path);
QImage image
= pbuffer
->toImage
();
image.save("D:\\Programming\\MyProjects\\ConverterChannelsDDS\\test.png", "PNG");
return pbuffer->toImage();
}
{
this
->p_getImageFromDDS
(QString("D:\\Programming\\MyProjects\\ConverterChannelsDDS\\wind_01_n.dds"));
}
DDSTexture::~DDSTexture()
{
delete pbuffer;
}
#include "ddstexture.h"
QSize DDSTexture::p_getDDSSize(const QString &path)
{
DDS_HEADER DDSHeader = {0};
DDSHeader.Size = sizeof(DDS_HEADER);
QFile ddsFile(path);
if(ddsFile.open(QIODevice::ReadOnly)){
ddsFile.seek(4);
ddsFile.read((char*)&DDSHeader, sizeof(DDS_HEADER));
}
ddsFile.close();
qDebug() << "height = " << DDSHeader.Height;
qDebug() << "width = " << DDSHeader.Width;
qDebug() << "size = " << DDSHeader.Size;
return QSize(DDSHeader.Width, DDSHeader.Height);
}
QImage DDSTexture::p_getImageFromDDS(const QString &path)
{
pbuffer = new QGLPixelBuffer(p_getDDSSize(path), format(), this);
makeCurrent();
pbuffer->bindTexture(path);
QImage image = pbuffer->toImage();
image.save("D:\\Programming\\MyProjects\\ConverterChannelsDDS\\test.png", "PNG");
return pbuffer->toImage();
}
DDSTexture::DDSTexture(QWidget *parent):QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
this->p_getImageFromDDS(QString("D:\\Programming\\MyProjects\\ConverterChannelsDDS\\wind_01_n.dds"));
}
DDSTexture::~DDSTexture()
{
delete pbuffer;
}
To copy to clipboard, switch view to plain text mode
What do I do not correctly?
p.s. I need to process channels only, without an output of the loaded texture somewhere.
Bookmarks