Skip to content

Commit 98c1ea3

Browse files
committed
CrashFix: Incompatible Pixel Format
1 parent bd7f81f commit 98c1ea3

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

include/ImageTools.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
class MultiplyImageByColorTask : public QRunnable
99
{
1010
public:
11-
MultiplyImageByColorTask(QImage* image, QColor color, int startY, int endY)
12-
: m_image(image), m_color(color), m_startY(startY), m_endY(endY)
11+
MultiplyImageByColorTask(QImage* image, QColor color, int startY, int endY, int debug_width, int debug_height, QImage::Format debug_format)
12+
: m_image(image), m_color(color), m_startY(startY), m_endY(endY), m_expectedWidth(debug_width), m_expectedHeight(debug_height), m_pixelFormat(debug_format)
1313
{
1414
}
1515

@@ -41,13 +41,17 @@ class MultiplyImageByColorTask : public QRunnable
4141
QColor m_color;
4242
int m_startY;
4343
int m_endY;
44+
// Crash Checks
45+
int m_expectedWidth;
46+
int m_expectedHeight;
47+
QImage::Format m_pixelFormat;
4448
};
4549

4650
class MultiplyImageByStrengthTask : public QRunnable
4751
{
4852
public:
49-
MultiplyImageByStrengthTask(QImage* image, double strength, int startY, int endY)
50-
: m_image(image), m_strength(strength), m_startY(startY), m_endY(endY)
53+
MultiplyImageByStrengthTask(QImage* image, double strength, int startY, int endY, int debug_width, int debug_height, QImage::Format debug_format)
54+
: m_image(image), m_strength(strength), m_startY(startY), m_endY(endY), m_expectedWidth(debug_width), m_expectedHeight(debug_height), m_pixelFormat(debug_format)
5155
{
5256
}
5357

@@ -79,6 +83,10 @@ class MultiplyImageByStrengthTask : public QRunnable
7983
double m_strength;
8084
int m_startY;
8185
int m_endY;
86+
// Crash Checks
87+
int m_expectedWidth;
88+
int m_expectedHeight;
89+
QImage::Format m_pixelFormat;
8290
};
8391

8492
void multiplyImageByColorMultithreaded(QImage& image, QColor color);

include/common_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#define COMMON_MAJOR 2023
66
#define COMMON_MINOR 2
77
#define COMMON_REV 1
8-
#define COMMON_BUILD 78
8+
#define COMMON_BUILD 79
99

1010
#define COMMON_VERSION DZ_MAKE_VERSION( COMMON_MAJOR, COMMON_MINOR, COMMON_REV, COMMON_BUILD )

src/DzBridgeAction.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,14 +5692,15 @@ bool DzBridgeAction::multiplyTextureValues(DzMaterial* material)
56925692
if (colorValue != QColor(255, 255, 255) && textureFilename != "")
56935693
{
56945694
QImage image = dzApp->getImageMgr()->loadImage(textureFilename);
5695+
QString tempPath = dzApp->getTempPath().replace("\\", "/");
5696+
QString stem = QFileInfo(textureFilename).fileName();
5697+
QString outfile = tempPath + "/" + stem + QString("_%1.png").arg(colorToHexString(colorValue));
56955698

56965699
// multiply image
5700+
dzApp->log(QString("DazBridge: Baking material strength into image: %1").arg(outfile));
56975701
multiplyImageByColorMultithreaded(image, colorValue);
56985702

56995703
// save out file
5700-
QString tempPath = dzApp->getTempPath().replace("\\", "/");
5701-
QString stem = QFileInfo(textureFilename).fileName();
5702-
QString outfile = tempPath + "/" + stem + QString("_%1.png").arg( colorToHexString(colorValue) );
57035704
dzApp->getImageMgr()->saveImage(outfile, image);
57045705

57055706
// create undo record
@@ -5724,14 +5725,15 @@ bool DzBridgeAction::multiplyTextureValues(DzMaterial* material)
57245725
if (numericValue != 1.0 && textureFilename != "")
57255726
{
57265727
QImage image = dzApp->getImageMgr()->loadImage(textureFilename);
5728+
QString tempPath = dzApp->getTempPath().replace("\\", "/");
5729+
QString stem = QFileInfo(textureFilename).fileName();
5730+
QString outfile = tempPath + "/" + stem + QString("_%1.png").arg(numericValue);
57275731

57285732
// multiply image
5733+
dzApp->log(QString("DazBridge: Baking material strength into image: %1").arg(outfile));
57295734
multiplyImageByStrengthMultithreaded(image, numericValue);
57305735

57315736
// save out file
5732-
QString tempPath = dzApp->getTempPath().replace("\\", "/");
5733-
QString stem = QFileInfo(textureFilename).fileName();
5734-
QString outfile = tempPath + "/" + stem + QString("_%1.png").arg(numericValue);
57355737
dzApp->getImageMgr()->saveImage(outfile, image);
57365738

57375739
// create undo record

src/ImageTools.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
#define USE_DAZ_LOG 1
2+
13
#include "ImageTools.h"
24

5+
#if USE_DAZ_LOG
6+
#include <dzapp.h>
7+
#endif
8+
9+
void log(QString message)
10+
{
11+
#if USE_DAZ_LOG
12+
dzApp->log(message);
13+
#else
14+
printf(message.toLocal8Bit().constData());
15+
#endif
16+
}
17+
318

419
// Bitwise check if number is a power of two
520
bool isPowerOfTwo(int n)
@@ -41,15 +56,26 @@ int nearestPowerOfTwo(int n)
4156

4257
void multiplyImageByColorMultithreaded(QImage& image, QColor color)
4358
{
59+
// Crash Check
60+
int width = image.width();
4461
int height = image.height();
62+
int lineLength = image.bytesPerLine();
63+
QImage::Format pixelFormat = image.format();
64+
if (pixelFormat != QImage::Format_ARGB32 &&
65+
pixelFormat != QImage::Format_RGB32)
66+
{
67+
log(QString("WARNING: multiplyImageByColorMultithreaded(): incompatible pixel format: %1, converting to ARGB32...").arg(pixelFormat));
68+
image = image.convertToFormat(QImage::Format_ARGB32);
69+
}
70+
4571
int numThreads = QThreadPool::globalInstance()->maxThreadCount();
4672
int step = height / numThreads;
4773

4874
for (int i = 0; i < numThreads; ++i)
4975
{
5076
int startY = i * step;
5177
int endY = (i == numThreads - 1) ? height : startY + step;
52-
MultiplyImageByColorTask* task = new MultiplyImageByColorTask(&image, color, startY, endY);
78+
MultiplyImageByColorTask* task = new MultiplyImageByColorTask(&image, color, startY, endY, width, height, pixelFormat);
5379
QThreadPool::globalInstance()->start(task);
5480
}
5581

@@ -58,15 +84,26 @@ void multiplyImageByColorMultithreaded(QImage& image, QColor color)
5884

5985
void multiplyImageByStrengthMultithreaded(QImage& image, double strength)
6086
{
87+
// Crash Check
88+
int width = image.width();
6189
int height = image.height();
90+
int lineLength = image.bytesPerLine();
91+
QImage::Format pixelFormat = image.format();
92+
if (pixelFormat != QImage::Format_ARGB32 &&
93+
pixelFormat != QImage::Format_RGB32)
94+
{
95+
log(QString("WARNING: multiplyImageByStrengthMultithreaded(): incompatible pixel format: %1, converting to ARGB32...").arg(pixelFormat));
96+
image = image.convertToFormat(QImage::Format_ARGB32);
97+
}
98+
6299
int numThreads = QThreadPool::globalInstance()->maxThreadCount();
63100
int step = height / numThreads;
64101

65102
for (int i = 0; i < numThreads; ++i)
66103
{
67104
int startY = i * step;
68105
int endY = (i == numThreads - 1) ? height : startY + step;
69-
MultiplyImageByStrengthTask* task = new MultiplyImageByStrengthTask(&image, strength, startY, endY);
106+
MultiplyImageByStrengthTask* task = new MultiplyImageByStrengthTask(&image, strength, startY, endY, width, height, pixelFormat);
70107
QThreadPool::globalInstance()->start(task);
71108
}
72109

0 commit comments

Comments
 (0)