5#include <QtQuick3DRuntimeRender/private/qssgrenderloadedtexture_p.h>
6#include <QtQuick3DRuntimeRender/private/qssgrendererutil_p.h>
7#include <QtQuick3DRuntimeRender/private/qssgruntimerenderlogging_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrendertexturedata_p.h>
9#include <QtGui/QImageReader>
10#include <QtGui/QColorSpace>
13#include <QtQuick3DUtils/private/qssgutils_p.h>
14#include <QtQuick3DUtils/private/qssgassert_p.h>
16#include <private/qtexturefilereader_p.h>
18#define TINYEXR_IMPLEMENTATION
19#define TINYEXR_USE_MINIZ 0
20#define TINYEXR_USE_THREAD 1
27QSharedPointer<QIODevice> QSSGInputUtil::getStreamForFile(
const QString &inPath,
bool inQuiet, QString *outPath)
29 QFile *file =
nullptr;
30 QString tryPath = inPath.startsWith(QLatin1String(
"qrc:/")) ? inPath.mid(3) : inPath;
31 QFileInfo fi(tryPath);
32 bool found = fi.exists();
33 if (!found && fi.isNativePath()) {
34 tryPath.prepend(QLatin1String(
":/"));
39 QString filePath = fi.canonicalFilePath();
40 file =
new QFile(filePath);
41 if (file->open(QIODevice::ReadOnly)) {
49 if (!file && !inQuiet)
50 qCWarning(WARNING,
"Failed to find file: %s", qPrintable(inPath));
51 return QSharedPointer<QIODevice>(file);
54QSharedPointer<QIODevice> QSSGInputUtil::getStreamForTextureFile(
const QString &inPath,
bool inQuiet,
55 QString *outPath, FileType *outFileType)
57 static const QList<QByteArray> hdrFormats = QList<QByteArray>({
"hdr",
"exr" });
58 static const QList<QByteArray> textureFormats = QTextureFileReader::supportedFileFormats();
59 static const QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();
60 static const QList<QByteArray> allFormats = textureFormats + hdrFormats + imageFormats;
64 QSharedPointer<QIODevice> stream = getStreamForFile(inPath,
true, &filePath);
66 ext = QFileInfo(filePath).suffix().toLatin1().toLower();
68 for (
const QByteArray &format : allFormats) {
69 QString tryName = inPath + QLatin1Char(
'.') + QLatin1String(format);
70 stream = getStreamForFile(tryName,
true, &filePath);
81 FileType type = UnknownFile;
82 if (hdrFormats.contains(ext))
84 else if (textureFormats.contains(ext))
86 else if (imageFormats.contains(ext))
90 }
else if (!inQuiet) {
91 qCWarning(WARNING,
"Failed to find texture file for: %s", qPrintable(inPath));
98 switch (internalFormat) {
100 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R8);
102 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R16);
104 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R16F);
106 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32I);
108 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32UI);
110 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32F);
112 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG8);
114 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8);
116 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8);
118 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8);
120 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8A8);
122 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB565);
124 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Alpha8);
126 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Luminance8);
128 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Luminance16);
130 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::LuminanceAlpha8);
132 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16F);
134 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG16F);
136 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG32F);
138 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32F);
140 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32F);
142 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11G11B10);
144 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB9E5);
146 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB10_A2);
148 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16F);
150 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32UI);
152 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32UI);
154 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16UI);
156 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16UI);
158 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8UI);
160 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8UI);
162 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32I);
164 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32I);
166 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16I);
168 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16I);
170 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8I);
172 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8I);
174 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT1);
176 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB_DXT1);
178 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT3);
180 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT5);
182 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11_EAC_UNorm);
184 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11_EAC_SNorm);
186 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG11_EAC_UNorm);
188 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG11_EAC_SNorm);
190 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8_ETC2);
192 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_ETC2);
194 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8_PunchThrough_Alpha1_ETC2);
196 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_PunchThrough_Alpha1_ETC2);
198 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8_ETC2_EAC);
200 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ETC2_EAC);
202 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_4x4);
204 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_5x4);
206 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_5x5);
208 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_6x5);
210 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_6x6);
212 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x5);
214 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x6);
216 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x8);
218 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x5);
220 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x6);
222 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x8);
224 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x10);
226 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_12x10);
228 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_12x12);
230 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_4x4);
232 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x4);
234 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x5);
236 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x5);
238 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x6);
240 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x5);
242 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x6);
244 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x8);
246 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x5);
248 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x6);
250 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x8);
252 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x10);
254 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x10);
256 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x12);
258 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth16);
260 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth24);
262 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth32);
264 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth24Stencil8);
266 return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Unknown);
272 QImage image(inPath);
275 const QPixelFormat pixFormat = image.pixelFormat();
276 QImage::Format targetFormat = QImage::Format_RGBA8888_Premultiplied;
277 if (image.colorCount())
278 targetFormat = QImage::Format_RGBA8888;
279 else if (pixFormat.channelCount() == 1)
280 targetFormat = QImage::Format_Grayscale8;
281 else if (pixFormat.alphaUsage() == QPixelFormat::IgnoresAlpha)
282 targetFormat = QImage::Format_RGBX8888;
283 else if (pixFormat.premultiplied() == QPixelFormat::NotPremultiplied)
284 targetFormat = QImage::Format_RGBA8888;
286 image.convertTo(targetFormat);
292QSSGLoadedTexture *QSSGLoadedTexture::loadQImage(
const QString &inPath, qint32 flipVertical)
294 QImage image = loadImage(inPath, flipVertical);
297 QSSGLoadedTexture *retval =
new QSSGLoadedTexture;
298 retval->width = image.width();
299 retval->height = image.height();
300 retval->components = image.pixelFormat().channelCount();
301 retval->image = image;
302 retval->data = (
void *)retval->image.bits();
303 retval->dataSizeInBytes = image.sizeInBytes();
304 retval->setFormatFromComponents();
306 retval->isSRGB = image.colorSpace().transferFunction() != QColorSpace::TransferFunction::Linear;
311QSSGLoadedTexture *QSSGLoadedTexture::loadCompressedImage(
const QString &inPath)
313 QSSGLoadedTexture *retval =
nullptr;
316 QFile imageFile(inPath);
317 if (!imageFile.open(QIODevice::ReadOnly)) {
318 qWarning() <<
"Could not open image file: " << inPath;
321 auto reader =
new QTextureFileReader(&imageFile, inPath);
323 if (!reader->canRead()) {
324 qWarning() <<
"Unable to read image file: " << inPath;
328 retval =
new QSSGLoadedTexture;
329 retval->textureFileData = reader->read();
332 retval->width = retval->textureFileData.size().width();
333 retval->height = retval->textureFileData.size().height();
334 auto glFormat = retval->textureFileData.glInternalFormat()
335 ? retval->textureFileData.glInternalFormat()
336 : retval->textureFileData.glFormat();
337 retval->format = fromGLtoTextureFormat(glFormat);
347typedef unsigned char RGBE[4];
354#define MAXELEN 0x7fff
358inline int calculateLine(
int width,
int bitdepth) {
return ((width * bitdepth) + 7) / 8; }
360inline int calculatePitch(
int line) {
return (line + 3) & ~3; }
362float convertComponent(
int exponent,
int val)
364 float v = val / (256.0f);
365 float d = powf(2.0f, (
float)exponent - 128.0f);
369void decrunchScanline(
const char *&p,
const char *pEnd, RGBE *scanline,
int w)
371 scanline[0][
R] = *p++;
372 scanline[0][
G] = *p++;
373 scanline[0][
B] = *p++;
374 scanline[0][
E] = *p++;
376 if (scanline[0][
R] == 2 && scanline[0][
G] == 2 && scanline[0][
B] < 128) {
378 for (
int channel = 0; channel < 4; ++channel) {
379 for (
int x = 0; x < w && p < pEnd; ) {
380 unsigned char c = *p++;
383 int repCount = c & 127;
386 scanline[x++][channel] = c;
389 while (c-- && p < pEnd)
390 scanline[x++][channel] = *p++;
399 while (x < w && pEnd - p >= 4) {
400 scanline[x][
R] = *p++;
401 scanline[x][
G] = *p++;
402 scanline[x][
B] = *p++;
403 scanline[x][
E] = *p++;
405 if (scanline[x][
R] == 1 && scanline[x][
G] == 1 && scanline[x][
B] == 1) {
406 int repCount = scanline[x][3] << bitshift;
408 memcpy(scanline[x], scanline[x - 1], 4);
420void decodeScanlineToTexture(RGBE *scanline,
int width,
void *outBuf, quint32 offset, QSSGRenderTextureFormat inFormat)
422 quint8 *target =
reinterpret_cast<quint8 *>(outBuf);
425 if (inFormat == QSSGRenderTextureFormat::RGBE8) {
426 memcpy(target, scanline, size_t(width) * 4);
429 for (
int i = 0; i < width; ++i) {
430 rgbaF32[
R] = convertComponent(scanline[i][
E], scanline[i][
R]);
431 rgbaF32[
G] = convertComponent(scanline[i][
E], scanline[i][
G]);
432 rgbaF32[
B] = convertComponent(scanline[i][
E], scanline[i][
B]);
435 inFormat.encodeToPixel(rgbaF32, target, i * inFormat.getSizeofFormat());
440QSSGLoadedTexture *loadRadianceHdr(
const QSharedPointer<QIODevice> &source,
const QSSGRenderTextureFormat &format)
442 QSSGLoadedTexture *imageData =
nullptr;
446 source->read(sig, 11);
447 if (!strncmp(sig,
"#?RADIANCE\n", 11)) {
448 QByteArray buf = source->readAll();
449 const char *p = buf.constData();
450 const char *pEnd = p + buf.size();
459 if (line.startsWith(QByteArrayLiteral(
"FORMAT="))) {
460 const QByteArray format = line.mid(7).trimmed();
461 if (format != QByteArrayLiteral(
"32-bit_rle_rgbe")) {
462 qWarning(
"HDR format '%s' is not supported", format.constData());
472 qWarning(
"Malformed HDR image data at property strings");
484 qWarning(
"Malformed HDR image data at resolution string");
492 if (!sscanf_s(line.constData(),
"-Y %d +X %d", &height, &width)) {
494 if (!sscanf(line.constData(),
"-Y %d +X %d", &height, &width)) {
496 qWarning(
"Unsupported HDR resolution string '%s'", line.constData());
499 if (width <= 0 || height <= 0) {
500 qWarning(
"Invalid HDR resolution");
504 const int bytesPerPixel = format.getSizeofFormat();
505 const int bitCount = bytesPerPixel * 8;
506 const int pitch = calculatePitch(calculateLine(width, bitCount));
507 const size_t dataSize = height * pitch;
508 QSSG_CHECK_X(dataSize <= std::numeric_limits<quint32>::max(),
"Requested data size exceeds 4GB limit!");
509 imageData =
new QSSGLoadedTexture;
510 imageData->dataSizeInBytes = quint32(dataSize);
511 imageData->data = ::malloc(dataSize);
512 imageData->width = width;
513 imageData->height = height;
514 imageData->format = format;
515 imageData->components = format.getNumberOfComponent();
516 imageData->isSRGB =
false;
519 RGBE *scanline =
new RGBE[width];
523 for (
int y = 0; y < height; ++y) {
524 quint32 byteOffset = quint32((height - 1 - y) * width * bytesPerPixel);
526 qWarning(
"Unexpected end of HDR data");
530 decrunchScanline(p, pEnd, scanline, width);
531 decodeScanlineToTexture(scanline, width, imageData->data, byteOffset, format);
540QSSGLoadedTexture *loadExr(
const QSharedPointer<QIODevice> &source,
const QSSGRenderTextureFormat format)
542 QSSGLoadedTexture *imageData =
nullptr;
544 char versionBuffer[tinyexr::kEXRVersionSize];
546 auto size = source->read(versionBuffer, tinyexr::kEXRVersionSize);
548 if (size != tinyexr::kEXRVersionSize)
551 EXRVersion exrVersion;
552 if (ParseEXRVersionFromMemory(&exrVersion,
reinterpret_cast<
unsigned char *>(versionBuffer), tinyexr::kEXRVersionSize) != TINYEXR_SUCCESS)
556 if (exrVersion.multipart)
561 QByteArray buf = source->readAll();
562 const char *err =
nullptr;
565 InitEXRHeader(&exrHeader);
566 if (ParseEXRHeaderFromMemory(&exrHeader,
568 reinterpret_cast<
const unsigned char *>(buf.constData()),
570 &err) != TINYEXR_SUCCESS) {
571 qWarning(
"Failed to parse EXR Header with error: '%s'", err);
572 FreeEXRErrorMessage(err);
577 for (
int i = 0; i < exrHeader.num_channels; i++) {
578 if (exrHeader.pixel_types[i] == TINYEXR_PIXELTYPE_HALF)
579 exrHeader.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
585 InitEXRImage(&exrImage);
586 if (LoadEXRImageFromMemory(&exrImage,
588 reinterpret_cast<
const unsigned char *>(buf.constData()),
590 &err) != TINYEXR_SUCCESS) {
591 qWarning(
"Failed to load EXR Image with error: '%s'", err);
592 FreeEXRHeader(&exrHeader);
593 FreeEXRErrorMessage(err);
598 const int bytesPerPixel = format.getSizeofFormat();
599 const int bitCount = bytesPerPixel * 8;
600 const int pitch = calculatePitch(calculateLine(exrImage.width, bitCount));
601 const size_t dataSize = exrImage.height * pitch;
602 QSSG_CHECK_X(dataSize <= std::numeric_limits<quint32>::max(),
"Requested data size exceeds 4GB limit!");
603 imageData =
new QSSGLoadedTexture;
604 imageData->dataSizeInBytes = quint32(dataSize);
605 imageData->data = ::malloc(imageData->dataSizeInBytes);
606 imageData->width = exrImage.width;
607 imageData->height = exrImage.height;
608 imageData->format = format;
609 imageData->components = format.getNumberOfComponent();
610 imageData->isSRGB =
false;
612 quint8 *target =
reinterpret_cast<quint8 *>(imageData->data);
620 for (
int c = 0; c < exrHeader.num_channels; c++) {
621 if (strcmp(exrHeader.channels[c].name,
"R") == 0)
623 else if (strcmp(exrHeader.channels[c].name,
"G") == 0)
625 else if (strcmp(exrHeader.channels[c].name,
"B") == 0)
627 else if (strcmp(exrHeader.channels[c].name,
"A") == 0)
630 const bool isSingleChannel = exrHeader.num_channels == 1;
633 if (exrHeader.tiled) {
634 for (
int it = 0; it < exrImage.num_tiles; it++) {
635 for (
int j = 0; j < exrHeader.tile_size_y; j++)
636 for (
int i = 0; i < exrHeader.tile_size_x; i++) {
638 exrImage.tiles[it].offset_x * exrHeader.tile_size_x + i;
640 exrImage.tiles[it].offset_y * exrHeader.tile_size_y + j;
641 const int inverseJJ = std::abs(jj - (exrImage.height - 1));
642 const int idx = ii + inverseJJ * exrImage.width;
645 if (ii >= exrImage.width) {
648 if (jj >= exrImage.height) {
651 const int srcIdx = i + j * exrHeader.tile_size_x;
652 unsigned char **src = exrImage.tiles[it].images;
653 if (isSingleChannel) {
654 rgbaF32[
R] =
reinterpret_cast<
float **>(src)[0][srcIdx];
655 rgbaF32[
G] = rgbaF32[
R];
656 rgbaF32[
B] = rgbaF32[
R];
657 rgbaF32[3] = rgbaF32[
R];
659 rgbaF32[
R] =
reinterpret_cast<
float **>(src)[idxR][srcIdx];
660 rgbaF32[
G] =
reinterpret_cast<
float **>(src)[idxG][srcIdx];
661 rgbaF32[
B] =
reinterpret_cast<
float **>(src)[idxB][srcIdx];
663 rgbaF32[3] =
reinterpret_cast<
float **>(src)[idxA][srcIdx];
667 format.encodeToPixel(rgbaF32, target, idx * bytesPerPixel);
672 for (
int y = exrImage.height - 1; y >= 0; --y) {
673 for (
int x = 0; x < exrImage.width; x++) {
674 const int i = y * exrImage.width + x;
675 if (isSingleChannel) {
676 rgbaF32[
R] =
reinterpret_cast<
float **>(exrImage.images)[0][i];
677 rgbaF32[
G] = rgbaF32[
R];
678 rgbaF32[
B] = rgbaF32[
R];
679 rgbaF32[3] = rgbaF32[
R];
681 rgbaF32[
R] =
reinterpret_cast<
float **>(exrImage.images)[idxR][i];
682 rgbaF32[
G] =
reinterpret_cast<
float **>(exrImage.images)[idxG][i];
683 rgbaF32[
B] =
reinterpret_cast<
float **>(exrImage.images)[idxB][i];
685 rgbaF32[3] =
reinterpret_cast<
float **>(exrImage.images)[idxA][i];
689 format.encodeToPixel(rgbaF32, target, idx * bytesPerPixel);
696 FreeEXRImage(&exrImage);
697 FreeEXRHeader(&exrHeader);
704QSSGLoadedTexture *QSSGLoadedTexture::loadHdrImage(
const QSharedPointer<QIODevice> &source,
const QSSGRenderTextureFormat &inFormat)
706 QSSGLoadedTexture *imageData =
nullptr;
708 QSSGRenderTextureFormat format = inFormat;
709 if (format.format == QSSGRenderTextureFormat::Unknown) {
714 format = QSSGRenderTextureFormat::RGBA16F;
718 imageData = loadRadianceHdr(source, format);
722 imageData = loadExr(source, format);
727QSSGLoadedTexture *QSSGLoadedTexture::loadTextureData(QSSGRenderTextureData *textureData)
729 QSSGLoadedTexture *imageData =
new QSSGLoadedTexture;
731 if (!textureData->format().isCompressedTextureFormat()) {
732 const int bytesPerPixel = textureData->format().getSizeofFormat();
733 const int bitCount = bytesPerPixel * 8;
734 const int pitch = calculatePitch(calculateLine(textureData->size().width(), bitCount));
735 size_t dataSize = size_t(textureData->size().height()) * pitch;
736 if (textureData->depth() > 0)
737 dataSize *= textureData->depth();
738 QSSG_CHECK_X(dataSize <= std::numeric_limits<quint32>::max(),
"Requested data size exceeds 4GB limit!");
739 imageData->dataSizeInBytes = quint32(dataSize);
741 imageData->data =
const_cast<
void*>(
reinterpret_cast<
const void*>(textureData->textureData().data()));
742 imageData->width = textureData->size().width();
743 imageData->height = textureData->size().height();
744 imageData->depth = textureData->depth();
745 imageData->format = textureData->format();
746 imageData->components = textureData->format().getNumberOfComponent();
750 imageData->data =
const_cast<
void*>(
reinterpret_cast<
const void*>(textureData->textureData().data()));
751 const size_t dataSize = textureData->textureData().size();
752 QSSG_CHECK_X(dataSize <= std::numeric_limits<quint32>::max(),
"Requested data size exceeds 4GB limit!");
753 imageData->dataSizeInBytes = quint32(dataSize);
757 imageData->width = textureData->size().width();
758 imageData->height = textureData->size().height();
759 imageData->format = textureData->format();
766 if (imageData->format == QSSGRenderTextureFormat::RGBE8 ||
767 imageData->format == QSSGRenderTextureFormat::RGBA16F ||
768 imageData->format == QSSGRenderTextureFormat::RGBA32F ||
769 imageData->format == QSSGRenderTextureFormat::BC6H)
770 imageData->isSRGB =
false;
772 imageData->isSRGB =
true;
779bool scanImageForAlpha(
const void *inData, quint32 inWidth, quint32 inHeight, quint32 inPixelSizeInBytes, quint8 inAlphaSizeInBits)
781 const quint8 *rowPtr =
reinterpret_cast<
const quint8 *>(inData);
782 bool hasAlpha =
false;
783 if (inAlphaSizeInBits == 0)
785 if (inPixelSizeInBytes != 2 && inPixelSizeInBytes != 4) {
789 if (inAlphaSizeInBits > 8) {
794 quint32 alphaRightShift = inPixelSizeInBytes * 8 - inAlphaSizeInBits;
795 quint32 maxAlphaValue = (1 << inAlphaSizeInBits) - 1;
797 for (quint32 rowIdx = 0; rowIdx < inHeight && !hasAlpha; ++rowIdx) {
798 for (quint32 idx = 0; idx < inWidth && !hasAlpha; ++idx, rowPtr += inPixelSizeInBytes) {
799 quint32 pixelValue = 0;
800 if (inPixelSizeInBytes == 2)
801 pixelValue = *(
reinterpret_cast<
const quint16 *>(rowPtr));
803 pixelValue = *(
reinterpret_cast<
const quint32 *>(rowPtr));
804 pixelValue = pixelValue >> alphaRightShift;
805 if (pixelValue < maxAlphaValue)
813QSSGLoadedTexture::~QSSGLoadedTexture()
815 if (data && image.sizeInBytes() <= 0 && ownsData)
819bool QSSGLoadedTexture::scanForTransparency()
const
821 switch (format.format) {
822 case QSSGRenderTextureFormat::SRGB8A8:
823 case QSSGRenderTextureFormat::RGBA8:
827 return scanImageForAlpha(data, width, height, 4, 8);
829 case QSSGRenderTextureFormat::SRGB8:
830 case QSSGRenderTextureFormat::RGB8:
831 case QSSGRenderTextureFormat::RGBE8:
833 case QSSGRenderTextureFormat::RGB565:
835 case QSSGRenderTextureFormat::RGBA5551:
839 return scanImageForAlpha(data, width, height, 2, 1);
841 case QSSGRenderTextureFormat::Alpha8:
843 case QSSGRenderTextureFormat::R8:
844 case QSSGRenderTextureFormat::Luminance8:
845 case QSSGRenderTextureFormat::RG8:
847 case QSSGRenderTextureFormat::LuminanceAlpha8:
851 return scanImageForAlpha(data, width, height, 2, 8);
852 case QSSGRenderTextureFormat::RGB_DXT1:
854 case QSSGRenderTextureFormat::RGBA_DXT3:
855 case QSSGRenderTextureFormat::RGBA_DXT1:
856 case QSSGRenderTextureFormat::RGBA_DXT5:
858 case QSSGRenderTextureFormat::RGB9E5:
860 case QSSGRenderTextureFormat::RG32F:
861 case QSSGRenderTextureFormat::RGB32F:
862 case QSSGRenderTextureFormat::RGBA16F:
863 case QSSGRenderTextureFormat::RGBA32F:
877 if (img1.size() != img2.size())
879 if (img1.pixelFormat().channelCount() != img2.pixelFormat().channelCount())
887 QStringList fileNames;
888 if (inPath.contains(QStringLiteral(
"%p"))) {
889 fileNames.reserve(6);
890 const char *faces[6] = {
"posx",
"negx",
"posy",
"negy",
"posz",
"negz" };
891 for (
const auto face : faces) {
892 QString fileName = inPath;
893 fileName.replace(QStringLiteral(
"%p"), QLatin1StringView(face));
894 fileNames << fileName;
897 }
else if (inPath.contains(QStringLiteral(
";"))) {
898 fileNames = inPath.split(QChar(u';'));
900 if (fileNames.size() != 6)
902 std::unique_ptr<QTextureFileData> textureFileData = std::make_unique<QTextureFileData>(QTextureFileData::ImageMode);
903 textureFileData->setNumFaces(6);
904 textureFileData->setNumLevels(1);
905 textureFileData->setLogName(inPath.toUtf8());
907 for (
int i = 0; i < 6; ++i) {
908 QString searchName = fileNames[i];
910 auto stream = QSSGInputUtil::getStreamForFile(searchName,
true, &filePath);
914 QImage face = loadImage(filePath, !flipY);
915 if (face.isNull() || (!prevImage.isNull() && !isCompatible(prevImage, face))) {
918 textureFileData->setData(face, 0, i);
919 textureFileData->setSize(face.size());
923 QSSGLoadedTexture *retval =
new QSSGLoadedTexture;
925 retval->textureFileData = *textureFileData;
927 retval->width = prevImage.width();
928 retval->height = prevImage.height();
929 retval->components = prevImage.pixelFormat().channelCount();
930 retval->image = prevImage;
931 retval->data = (
void *)retval->image.bits();
932 const size_t dataSize = prevImage.sizeInBytes();
933 QSSG_CHECK_X(dataSize <= std::numeric_limits<quint32>::max(),
"Requested data size exceeds 4GB limit!");
934 retval->dataSizeInBytes = quint32(dataSize);
935 retval->setFormatFromComponents();
937 retval->isSRGB = prevImage.colorSpace().transferFunction() != QColorSpace::TransferFunction::Linear;
942QSSGLoadedTexture *QSSGLoadedTexture::load(
const QString &inPath,
943 const QSSGRenderTextureFormat &inFormat,
946 if (inPath.isEmpty())
949 QSSGLoadedTexture *theLoadedImage =
nullptr;
951 QSSGInputUtil::FileType fileType = QSSGInputUtil::UnknownFile;
952 QSharedPointer<QIODevice> theStream =
953 QSSGInputUtil::getStreamForTextureFile(inPath,
true, &fileName, &fileType);
957 case QSSGInputUtil::HdrFile:
960 theLoadedImage = loadHdrImage(theStream, inFormat);
962 case QSSGInputUtil::TextureFile:
963 theLoadedImage = loadCompressedImage(fileName);
966 theLoadedImage = loadQImage(fileName, inFlipY);
971 return loadCubeMap(inPath, inFlipY);
973 return theLoadedImage;
static bool isCompatible(const QImage &img1, const QImage &img2)
static QSSGRenderTextureFormat fromGLtoTextureFormat(quint32 internalFormat)
static QImage loadImage(const QString &inPath, bool flipVertical)
static QSSGLoadedTexture * loadCubeMap(const QString &inPath, bool flipY)