Skip to content

Commit

Permalink
CrashFix: Incompatible Pixel Format
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbui78 committed Oct 17, 2023
1 parent bd7f81f commit 98c1ea3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
16 changes: 12 additions & 4 deletions include/ImageTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class MultiplyImageByColorTask : public QRunnable
{
public:
MultiplyImageByColorTask(QImage* image, QColor color, int startY, int endY)
: m_image(image), m_color(color), m_startY(startY), m_endY(endY)
MultiplyImageByColorTask(QImage* image, QColor color, int startY, int endY, int debug_width, int debug_height, QImage::Format debug_format)
: m_image(image), m_color(color), m_startY(startY), m_endY(endY), m_expectedWidth(debug_width), m_expectedHeight(debug_height), m_pixelFormat(debug_format)
{
}

Expand Down Expand Up @@ -41,13 +41,17 @@ class MultiplyImageByColorTask : public QRunnable
QColor m_color;
int m_startY;
int m_endY;
// Crash Checks
int m_expectedWidth;
int m_expectedHeight;
QImage::Format m_pixelFormat;
};

class MultiplyImageByStrengthTask : public QRunnable
{
public:
MultiplyImageByStrengthTask(QImage* image, double strength, int startY, int endY)
: m_image(image), m_strength(strength), m_startY(startY), m_endY(endY)
MultiplyImageByStrengthTask(QImage* image, double strength, int startY, int endY, int debug_width, int debug_height, QImage::Format debug_format)
: m_image(image), m_strength(strength), m_startY(startY), m_endY(endY), m_expectedWidth(debug_width), m_expectedHeight(debug_height), m_pixelFormat(debug_format)
{
}

Expand Down Expand Up @@ -79,6 +83,10 @@ class MultiplyImageByStrengthTask : public QRunnable
double m_strength;
int m_startY;
int m_endY;
// Crash Checks
int m_expectedWidth;
int m_expectedHeight;
QImage::Format m_pixelFormat;
};

void multiplyImageByColorMultithreaded(QImage& image, QColor color);
Expand Down
2 changes: 1 addition & 1 deletion include/common_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#define COMMON_MAJOR 2023
#define COMMON_MINOR 2
#define COMMON_REV 1
#define COMMON_BUILD 78
#define COMMON_BUILD 79

#define COMMON_VERSION DZ_MAKE_VERSION( COMMON_MAJOR, COMMON_MINOR, COMMON_REV, COMMON_BUILD )
14 changes: 8 additions & 6 deletions src/DzBridgeAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5692,14 +5692,15 @@ bool DzBridgeAction::multiplyTextureValues(DzMaterial* material)
if (colorValue != QColor(255, 255, 255) && textureFilename != "")
{
QImage image = dzApp->getImageMgr()->loadImage(textureFilename);
QString tempPath = dzApp->getTempPath().replace("\\", "/");
QString stem = QFileInfo(textureFilename).fileName();
QString outfile = tempPath + "/" + stem + QString("_%1.png").arg(colorToHexString(colorValue));

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

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

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

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

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

// create undo record
Expand Down
41 changes: 39 additions & 2 deletions src/ImageTools.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#define USE_DAZ_LOG 1

#include "ImageTools.h"

#if USE_DAZ_LOG
#include <dzapp.h>
#endif

void log(QString message)
{
#if USE_DAZ_LOG
dzApp->log(message);
#else
printf(message.toLocal8Bit().constData());
#endif
}


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

void multiplyImageByColorMultithreaded(QImage& image, QColor color)
{
// Crash Check
int width = image.width();
int height = image.height();
int lineLength = image.bytesPerLine();
QImage::Format pixelFormat = image.format();
if (pixelFormat != QImage::Format_ARGB32 &&
pixelFormat != QImage::Format_RGB32)
{
log(QString("WARNING: multiplyImageByColorMultithreaded(): incompatible pixel format: %1, converting to ARGB32...").arg(pixelFormat));
image = image.convertToFormat(QImage::Format_ARGB32);
}

int numThreads = QThreadPool::globalInstance()->maxThreadCount();
int step = height / numThreads;

for (int i = 0; i < numThreads; ++i)
{
int startY = i * step;
int endY = (i == numThreads - 1) ? height : startY + step;
MultiplyImageByColorTask* task = new MultiplyImageByColorTask(&image, color, startY, endY);
MultiplyImageByColorTask* task = new MultiplyImageByColorTask(&image, color, startY, endY, width, height, pixelFormat);
QThreadPool::globalInstance()->start(task);
}

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

void multiplyImageByStrengthMultithreaded(QImage& image, double strength)
{
// Crash Check
int width = image.width();
int height = image.height();
int lineLength = image.bytesPerLine();
QImage::Format pixelFormat = image.format();
if (pixelFormat != QImage::Format_ARGB32 &&
pixelFormat != QImage::Format_RGB32)
{
log(QString("WARNING: multiplyImageByStrengthMultithreaded(): incompatible pixel format: %1, converting to ARGB32...").arg(pixelFormat));
image = image.convertToFormat(QImage::Format_ARGB32);
}

int numThreads = QThreadPool::globalInstance()->maxThreadCount();
int step = height / numThreads;

for (int i = 0; i < numThreads; ++i)
{
int startY = i * step;
int endY = (i == numThreads - 1) ? height : startY + step;
MultiplyImageByStrengthTask* task = new MultiplyImageByStrengthTask(&image, strength, startY, endY);
MultiplyImageByStrengthTask* task = new MultiplyImageByStrengthTask(&image, strength, startY, endY, width, height, pixelFormat);
QThreadPool::globalInstance()->start(task);
}

Expand Down

0 comments on commit 98c1ea3

Please sign in to comment.