Commit 066ba35d authored by Gilles Caulier's avatar Gilles Caulier 🗼
Browse files

FishEyes and Caricature effects : add support of multicore CPU based on...

FishEyes and Caricature effects : add support of multicore CPU based on QtConcurrents API. Computation time is divided by 2 with i7 (8 core CPU)
CCBUGS: 289204
parent 788a450b
......@@ -38,6 +38,7 @@
#include <QDateTime>
#include <QSize>
#include <QtConcurrentRun>
#include <qmath.h>
// Local includes
......@@ -172,36 +173,16 @@ void DistortionFXFilter::filterImage()
}
}
/* Function to apply the fisheye effect backported from ImageProcesqSing version 2
*
* data => The image data in RGBA mode.
* Width => Width of image.
* Height => Height of image.
* Coeff => Distortion effect coeff. Positive value render 'Fish Eyes' effect,
* and negative values render 'Caricature' effect.
* Antialias => Smart blurring result.
*
* Theory => This is a great effect if you take employee photos
* Its pure trigonometry. I think if you study hard the code you
* understand very well.
*/
void DistortionFXFilter::fisheye(DImg* orgImage, DImg* destImage, double Coeff, bool AntiAlias)
void DistortionFXFilter::fisheyeMultithreaded(const Args& prm)
{
if (Coeff == 0.0)
{
return;
}
int Width = prm.orgImage->width();
int Height = prm.orgImage->height();
uchar* data = prm.orgImage->bits();
bool sixteenBit = prm.orgImage->sixteenBit();
int bytesDepth = prm.orgImage->bytesDepth();
uchar* pResBits = prm.destImage->bits();
int progress;
int Width = orgImage->width();
int Height = orgImage->height();
uchar* data = orgImage->bits();
bool sixteenBit = orgImage->sixteenBit();
int bytesDepth = orgImage->bytesDepth();
uchar* pResBits = destImage->bits();
double nh, nw, th, tw;
double nh, nw, tw;
DColor color;
int offset;
......@@ -210,7 +191,7 @@ void DistortionFXFilter::fisheye(DImg* orgImage, DImg* destImage, double Coeff,
int nHalfH = Height / 2;
double lfXScale = 1.0;
double lfYScale = 1.0;
double lfCoeffStep = Coeff / 1000.0;
double lfCoeffStep = prm.Coeff / 1000.0;
double lfRadius, lfAngle;
if (Width > Height)
......@@ -224,49 +205,94 @@ void DistortionFXFilter::fisheye(DImg* orgImage, DImg* destImage, double Coeff,
double lfRadMax = (double)qMax(Height, Width) / 2.0;
double lfCoeff = lfRadMax / qLn(qFabs(lfCoeffStep) * lfRadMax + 1.0);
double th = lfYScale * (double)(prm.h - nHalfH);
// main loop
for (int h = 0; runningFlag() && (h < Height); ++h)
for (int w = prm.start; runningFlag() && (w < prm.stop); ++w)
{
th = lfYScale * (double)(h - nHalfH);
tw = lfXScale * (double)(w - nHalfW);
for (int w = 0; runningFlag() && (w < Width); ++w)
{
tw = lfXScale * (double)(w - nHalfW);
// we find the distance from the center
lfRadius = qSqrt(th * th + tw * tw);
// we find the distance from the center
lfRadius = qSqrt(th * th + tw * tw);
if (lfRadius < lfRadMax)
{
lfAngle = qAtan2(th, tw);
if (lfRadius < lfRadMax)
if (prm.Coeff > 0.0)
{
lfAngle = qAtan2(th, tw);
if (Coeff > 0.0)
{
lfRadius = (qExp(lfRadius / lfCoeff) - 1.0) / lfCoeffStep;
}
else
{
lfRadius = lfCoeff * qLn(1.0 + (-1.0 * lfCoeffStep) * lfRadius);
}
nw = (double)nHalfW + (lfRadius / lfXScale) * qCos(lfAngle);
nh = (double)nHalfH + (lfRadius / lfYScale) * qSin(lfAngle);
setPixelFromOther(Width, Height, sixteenBit, bytesDepth, data, pResBits, w, h, nw, nh, AntiAlias);
lfRadius = (qExp(lfRadius / lfCoeff) - 1.0) / lfCoeffStep;
}
else
{
// copy pixel
offset = getOffset(Width, w, h, bytesDepth);
color.setColor(data + offset, sixteenBit);
color.setPixel(pResBits + offset);
lfRadius = lfCoeff * qLn(1.0 + (-1.0 * lfCoeffStep) * lfRadius);
}
nw = (double)nHalfW + (lfRadius / lfXScale) * qCos(lfAngle);
nh = (double)nHalfH + (lfRadius / lfYScale) * qSin(lfAngle);
setPixelFromOther(Width, Height, sixteenBit, bytesDepth, data, pResBits, w, prm.h, nw, nh, prm.AntiAlias);
}
else
{
// copy pixel
offset = getOffset(Width, w, prm.h, bytesDepth);
color.setColor(data + offset, sixteenBit);
color.setPixel(pResBits + offset);
}
}
}
/* Function to apply the fisheye effect backported from ImageProcesqSing version 2
*
* data => The image data in RGBA mode.
* Width => Width of image.
* Height => Height of image.
* Coeff => Distortion effect coeff. Positive value render 'Fish Eyes' effect,
* and negative values render 'Caricature' effect.
* Antialias => Smart blurring result.
*
* Theory => This is a great effect if you take employee photos
* Its pure trigonometry. I think if you study hard the code you
* understand very well.
*/
void DistortionFXFilter::fisheye(DImg* orgImage, DImg* destImage, double Coeff, bool AntiAlias)
{
if (Coeff == 0.0)
{
return;
}
int progress;
QList<int> vals = multithreadedSteps(orgImage->width());
QList <QFuture<void> > tasks;
Args prm;
prm.orgImage = orgImage;
prm.destImage = destImage;
prm.Coeff = Coeff;
prm.AntiAlias = AntiAlias;
// main loop
for (int h = 0; runningFlag() && (h < (int)orgImage->height()); ++h)
{
for (int j = 0 ; runningFlag() && (j < vals.count()-1) ; ++j)
{
prm.start = vals[j];
prm.stop = vals[j+1];
prm.h = h;
tasks.append(QtConcurrent::run(this,
&DistortionFXFilter::fisheyeMultithreaded,
prm
));
}
foreach(QFuture<void> t, tasks)
t.waitForFinished();
// Update the progress bar in dialog.
progress = (int)(((double)(h) * 100.0) / Height);
progress = (int)(((double)(h) * 100.0) / orgImage->height());
if (progress % 5 == 0)
{
......
......@@ -104,8 +104,8 @@ private:
struct Args
{
uint start;
uint stop;
int start;
int stop;
int h;
DImg* orgImage;
DImg* destImage;
......
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