diff --git a/include/ImageTools.h b/include/ImageTools.h index 1b4faff..1cad73c 100644 --- a/include/ImageTools.h +++ b/include/ImageTools.h @@ -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) { } @@ -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) { } @@ -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); diff --git a/include/common_version.h b/include/common_version.h index 468cd43..2a6fb27 100644 --- a/include/common_version.h +++ b/include/common_version.h @@ -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 ) diff --git a/src/DzBridgeAction.cpp b/src/DzBridgeAction.cpp index d3d21e6..79d4589 100644 --- a/src/DzBridgeAction.cpp +++ b/src/DzBridgeAction.cpp @@ -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 @@ -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 diff --git a/src/ImageTools.cpp b/src/ImageTools.cpp index e073efe..ca1de73 100644 --- a/src/ImageTools.cpp +++ b/src/ImageTools.cpp @@ -1,5 +1,20 @@ +#define USE_DAZ_LOG 1 + #include "ImageTools.h" +#if USE_DAZ_LOG +#include +#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) @@ -41,7 +56,18 @@ 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; @@ -49,7 +75,7 @@ void multiplyImageByColorMultithreaded(QImage& image, QColor color) { 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); } @@ -58,7 +84,18 @@ 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; @@ -66,7 +103,7 @@ void multiplyImageByStrengthMultithreaded(QImage& image, double strength) { 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); }