Commit 0a3fa8fb authored by Halla Rempt's avatar Halla Rempt
Browse files

FastColorTransfer: compute mean and sigma and store those in the config

We used to compute mean and sigma for the reference image in the
process(). This is wasteful, so compute those only once and store the
values in the config object.

BUG:277888
parent 358ad5f3
......@@ -62,7 +62,8 @@ FastColorTransferPlugin::~FastColorTransferPlugin()
KisFilterFastColorTransfer::KisFilterFastColorTransfer() : KisFilter(id(), categoryColors(), i18n("&Color Transfer..."))
{
setColorSpaceIndependence(FULLY_INDEPENDENT);
setSupportsPainting(true);
setSupportsThreading(false);
setSupportsPainting(false);
setSupportsIncrementalPainting(false);
setSupportsAdjustmentLayers(false);
}
......@@ -92,35 +93,6 @@ void KisFilterFastColorTransfer::process(KisPaintDeviceSP device,
Q_ASSERT(device != 0);
dbgPlugins << "Start transferring color";
QVariant value;
QString fileName;
if (config && config->getProperty("filename", value)) {
fileName = value.toString();
} else {
// XXX: Make this a warning message box!
dbgPlugins << "No file name for the reference image was specified.";
return;
}
if (fileName.isEmpty()) return;
KisPaintDeviceSP ref;
dbgPlugins << "Use as reference file : " << fileName;
KisDoc2 d;
KoFilterManager manager(&d);
QByteArray nativeFormat = d.nativeFormatMimeType();
KoFilter::ConversionStatus status;
QString s = manager.importDocument(fileName, QString(), status);
KisImageWSP importedImage = d.image();
if (importedImage) {
ref = importedImage->projection();
}
if (!ref) {
dbgPlugins << "No reference image was specified.";
return;
}
// Convert ref and src to LAB
const KoColorSpace* labCS = KoColorSpaceRegistry::instance()->lab16();
......@@ -128,19 +100,16 @@ void KisFilterFastColorTransfer::process(KisPaintDeviceSP device,
dbgPlugins << "The LAB colorspace is not available.";
return;
}
dbgPlugins << "convert a copy of src to lab";
const KoColorSpace* oldCS = device->colorSpace();
KisPaintDeviceSP srcLAB = new KisPaintDevice(*device.data());
dbgPlugins << "srcLab : " << srcLAB->extent();
KUndo2Command* cmd = srcLAB->convertTo(labCS);
delete cmd;
dbgPlugins << "convert ref to lab";
dbgPlugins << "ref : " << srcLAB->extent();
cmd = ref->convertTo(labCS);
delete cmd;
if (progressUpdater) {
progressUpdater->setRange(0, 2*applyRect.width() * applyRect.height() + importedImage->width() * importedImage->height());
progressUpdater->setRange(0, 2 * applyRect.width() * applyRect.height());
}
int count = 0;
......@@ -148,7 +117,9 @@ void KisFilterFastColorTransfer::process(KisPaintDeviceSP device,
dbgPlugins << "Compute the means and sigmas of src";
double meanL_src = 0., meanA_src = 0., meanB_src = 0.;
double sigmaL_src = 0., sigmaA_src = 0., sigmaB_src = 0.;
KisRectConstIteratorSP srcLABIt = srcLAB->createRectConstIteratorNG(applyRect);
do {
const quint16* data = reinterpret_cast<const quint16*>(srcLABIt->oldRawData());
quint32 L = data[0];
......@@ -162,6 +133,7 @@ void KisFilterFastColorTransfer::process(KisPaintDeviceSP device,
sigmaB_src += B * B;
if (progressUpdater) progressUpdater->setValue(++count);
} while (srcLABIt->nextPixel() && !(progressUpdater && progressUpdater->interrupted()));
double totalSize = 1. / (applyRect.width() * applyRect.height());
meanL_src *= totalSize;
meanA_src *= totalSize;
......@@ -169,34 +141,17 @@ void KisFilterFastColorTransfer::process(KisPaintDeviceSP device,
sigmaL_src *= totalSize;
sigmaA_src *= totalSize;
sigmaB_src *= totalSize;
dbgPlugins << totalSize << "" << meanL_src << "" << meanA_src << "" << meanB_src << "" << sigmaL_src << "" << sigmaA_src << "" << sigmaB_src;
// Compute the means and sigmas of ref
dbgPlugins << "Compute the means and sigmas of ref";
double meanL_ref = 0., meanA_ref = 0., meanB_ref = 0.;
double sigmaL_ref = 0., sigmaA_ref = 0., sigmaB_ref = 0.;
KisRectConstIteratorSP refIt = ref->createRectConstIteratorNG(0, 0, importedImage->width(), importedImage->height());
do {
const quint16* data = reinterpret_cast<const quint16*>(refIt->oldRawData());
quint32 L = data[0];
quint32 A = data[1];
quint32 B = data[2];
meanL_ref += L;
meanA_ref += A;
meanB_ref += B;
sigmaL_ref += L * L;
sigmaA_ref += A * A;
sigmaB_ref += B * B;
if (progressUpdater) progressUpdater->setValue(++count);
} while (refIt->nextPixel() && !(progressUpdater && progressUpdater->interrupted()));
totalSize = 1. / (importedImage->width() * importedImage->height());
meanL_ref *= totalSize;
meanA_ref *= totalSize;
meanB_ref *= totalSize;
sigmaL_ref *= totalSize;
sigmaA_ref *= totalSize;
sigmaB_ref *= totalSize;
dbgPlugins << totalSize << "" << meanL_ref << "" << meanA_ref << "" << meanB_ref << "" << sigmaL_ref << "" << sigmaA_ref << "" << sigmaB_ref;
double meanL_ref = config->getDouble("meanL");
double meanA_ref = config->getDouble("meanA");
double meanB_ref = config->getDouble("meanB");
double sigmaL_ref = config->getDouble("sigmaL");
double sigmaA_ref = config->getDouble("sigmaA");
double sigmaB_ref = config->getDouble("sigmaB");
// Transfer colors
dbgPlugins << "Transfer colors";
{
......
......@@ -20,13 +20,24 @@
#include "kis_wdg_fastcolortransfer.h"
#include <qlayout.h>
#include <kurlrequester.h>
#include "ui_wdgfastcolortransfer.h"
#include <KoFilter.h>
#include <KoFilterManager.h>
#include <filter/kis_filter_configuration.h>
#include <kis_doc2.h>
#include <kis_image.h>
#include <kis_iterators_pixel.h>
#include <kis_iterator_ng.h>
#include <kis_paint_device.h>
#include <kundo2command.h>
#include <KoColorSpaceRegistry.h>
#include "ui_wdgfastcolortransfer.h"
KisWdgFastColorTransfer::KisWdgFastColorTransfer(QWidget * parent) : KisConfigWidget(parent)
{
m_widget = new Ui_WdgFastColorTransfer();
......@@ -52,8 +63,79 @@ void KisWdgFastColorTransfer::setConfiguration(const KisPropertiesConfiguration*
KisPropertiesConfiguration* KisWdgFastColorTransfer::configuration() const
{
KisFilterConfiguration* config = new KisFilterConfiguration("colortransfer", 1);
QString url = this->widget()->fileNameURLRequester->url().url();
dbgPlugins << "Filename : " << this->widget()->fileNameURLRequester->url() << " = " << url;
config->setProperty("filename", url);
QString fileName = this->widget()->fileNameURLRequester->url().url();
if (fileName.isEmpty()) return config;
KisPaintDeviceSP ref;
dbgPlugins << "Use as reference file : " << fileName;
KisDoc2 d;
KoFilterManager manager(&d);
KoFilter::ConversionStatus status;
QString s = manager.importDocument(fileName, QString(), status);
dbgPlugins << "import returned" << s << "and status" << status;
KisImageWSP importedImage = d.image();
if (importedImage) {
ref = importedImage->projection();
}
if (!ref) {
dbgPlugins << "No reference image was specified.";
return config;
}
// Convert ref to LAB
const KoColorSpace* labCS = KoColorSpaceRegistry::instance()->lab16();
if (!labCS) {
dbgPlugins << "The LAB colorspace is not available.";
return config;
}
dbgPlugins << "convert ref to lab";
KUndo2Command* cmd = ref->convertTo(labCS);
delete cmd;
// Compute the means and sigmas of ref
double meanL_ref = 0., meanA_ref = 0., meanB_ref = 0.;
double sigmaL_ref = 0., sigmaA_ref = 0., sigmaB_ref = 0.;
KisRectConstIteratorSP refIt = ref->createRectConstIteratorNG(0, 0, importedImage->width(), importedImage->height());
do {
const quint16* data = reinterpret_cast<const quint16*>(refIt->oldRawData());
quint32 L = data[0];
quint32 A = data[1];
quint32 B = data[2];
meanL_ref += L;
meanA_ref += A;
meanB_ref += B;
sigmaL_ref += L * L;
sigmaA_ref += A * A;
sigmaB_ref += B * B;
} while (refIt->nextPixel());
double totalSize = 1. / (importedImage->width() * importedImage->height());
meanL_ref *= totalSize;
meanA_ref *= totalSize;
meanB_ref *= totalSize;
sigmaL_ref *= totalSize;
sigmaA_ref *= totalSize;
sigmaB_ref *= totalSize;
dbgPlugins << totalSize << "" << meanL_ref << "" << meanA_ref << "" << meanB_ref << "" << sigmaL_ref << "" << sigmaA_ref << "" << sigmaB_ref;
config->setProperty("filename", fileName);
config->setProperty("meanL", meanL_ref);
config->setProperty("meanA", meanA_ref);
config->setProperty("meanB", meanB_ref);
config->setProperty("sigmaL", sigmaL_ref);
config->setProperty("sigmaA", sigmaA_ref);
config->setProperty("sigmaB", sigmaB_ref);
return config;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment