Skip to content

Commit

Permalink
LCE mode
Browse files Browse the repository at this point in the history
LCE generation now working for everything except 3D brush (as 3D brush is essentially broken anyway - it works as well as anything does in 3D brush). Also fixed recalc brush to respect locking in the same way that generate does
  • Loading branch information
marksutton committed Nov 9, 2021
1 parent 1853c26 commit f1d818d
Show file tree
Hide file tree
Showing 16 changed files with 541 additions and 54 deletions.
11 changes: 7 additions & 4 deletions SPIERSedit/src/brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "brush.h"
#include "globals.h"
#include "display.h"
#include "stdlib.h"
#include <math.h>
#include "myscene.h" //for the scene
#include <QPen>
Expand Down Expand Up @@ -351,7 +352,7 @@ void Brush_class::resize(int size, int shape, double o)

if (TY.count())
{
qSort(TY); //now ordered
std::sort(TY.begin(), TY.end()); //replaced qSort
bool on = false;
int last = TY[0] - 2;
foreach (int y, TY)
Expand Down Expand Up @@ -388,7 +389,8 @@ void Brush_class::resize(int size, int shape, double o)

if (TX.count())
{
qSort(TX); //now ordered
std::sort(TX.begin(), TX.end());

bool on = false;
int last = TX[0] - 2;
foreach (int x, TX)
Expand Down Expand Up @@ -722,7 +724,7 @@ void Brush_class::brighten(int x, int y, int segment, int effect)
//FilesDirty[CurrentFile]=true;
}

void Brush_class::recalc(int x, int y, int segment)
void Brush_class::recalc(int x, int y, int segment, QVector<uchar> *sample, QByteArray *locks)
{
int n;
uchar *data;
Expand All @@ -748,7 +750,8 @@ void Brush_class::recalc(int x, int y, int segment)
{
pos = ay * fwidth + ax;
int pos4 = ay * fwidth4 + ax;
data[pos4] = GenPixel(ax, ay, segment);
if (locks->at(pos)==0)
data[pos4] = GenPixel(ax, ay, segment, sample, locks);

dirty[pos] = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion SPIERSedit/src/brush.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Brush_class
void mask(int x, int y, int mask);
void Brush_Flag_Restart();
void segment(int x, int y, int effect);
void recalc(int x, int y, int segment);
void recalc(int x, int y, int segment, QVector<uchar> *sample, QByteArray *locks);
//more to follow!
int PixelCount; //applies to all three lists above
private:
Expand Down
74 changes: 67 additions & 7 deletions SPIERSedit/src/copyingimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,32 @@ void CopyingImpl::DeleteSegments(QList <int> list)
* @brief CopyingImpl::GenerateLinear
* @param SliceSelectorList
*/

//New generate for local contrast enhancement
//Base is copy of generateLinear
void CopyingImpl::GenerateLCE(QListWidget *SliceSelectorList)
{
int c = SliceSelectorList->selectedItems().count();
if (c > 1) show(); //show progress dialog if multifile
copying = true; //what does this do? Block GUI?

this->setWindowTitle("Perfoming Local Contrast Enhancement (LCE)...");
WriteAllData(CurrentFile);
if (c > 1) progressBar->setMaximum(c);
int item_count=0;
for (int i = 0; i < Files.count(); i++)
{
//for each file
if ((SliceSelectorList->item(i))->isSelected()) ApplyLCE(CurrentSegment, i, false);
if (c > 1) progressBar->setValue(item_count++);
if (c > 1) qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
//restore setup
LoadAllData(CurrentFile);
copying = false;
if (c > 1) close(); //close dialog
}

void CopyingImpl::GenerateLinear(QListWidget *SliceSelectorList)
{
int c = SliceSelectorList->selectedItems().count();
Expand All @@ -376,11 +402,12 @@ void CopyingImpl::GenerateLinear(QListWidget *SliceSelectorList)
this->setWindowTitle("Generating linear segment files...");
WriteAllData(CurrentFile);
if (c > 1) progressBar->setMaximum(c);
int item_count=0;
for (int i = 0; i < Files.count(); i++)
{

if ((SliceSelectorList->item(i))->isSelected()) MakeLinearGreyScale(CurrentSegment, i, false);
if (c > 1) progressBar->setValue(i);
if (c > 1) progressBar->setValue(item_count++);
if (c > 1) qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
LoadAllData(CurrentFile);
Expand All @@ -400,10 +427,11 @@ void CopyingImpl::GeneratePoly(QListWidget *SliceSelectorList)
this->setWindowTitle("Generating polynomial segment files...");
WriteAllData(CurrentFile);
if (c > 1) progressBar->setMaximum(c);
int item_count=0;
for (int i = 0; i < Files.count(); i++)
{
if ((SliceSelectorList->item(i))->isSelected()) MakePolyGreyScale(CurrentSegment, i, false);
if (c > 1) progressBar->setValue(i);
if (c > 1) progressBar->setValue(item_count++);
if (c > 1) qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
LoadAllData(CurrentFile);
Expand All @@ -423,13 +451,14 @@ void CopyingImpl::GenerateRange(QListWidget *SliceSelectorList)
this->setWindowTitle("Generating range segment files...");
WriteAllData(CurrentFile);
if (c > 1) progressBar->setMaximum(c);
int item_count=0;
for (int i = 0; i < Files.count(); i++)
{
if (RangeSelectedOnly) if ((SliceSelectorList->item(i))->isSelected()) MakeRangeGreyScale(CurrentSegment, i, false);
if (!RangeSelectedOnly) if ((SliceSelectorList->item(i))->isSelected())
for (int j = 0; j < SegmentCount; j++) if (Segments[j]->Activated) MakeRangeGreyScale(j, i, false);

if (c > 1) progressBar->setValue(i);
if (c > 1) progressBar->setValue(item_count++);
if (c > 1) qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
LoadAllData(CurrentFile);
Expand Down Expand Up @@ -516,7 +545,7 @@ void CopyingImpl::ReverseStretches(QList <double> *stretches, int Sstart, int Ss
* @return
*/
bool CopyingImpl::DoIHaveChildren(int parent)
//Yes, I do!
//Yes, I do - MDS
{
for (int i = 0; i < OutputObjectsCount; i++)
if (OutputObjects[i]->IsGroup == false && OutputObjects[i]->Parent == parent && OutputObjects[i]->Show) return true;
Expand Down Expand Up @@ -880,12 +909,43 @@ void CopyingImpl::Apply3DBrush(int button)
else Brush.segment(LastMouseX, LastMouseY, 0);
break;
case 5:
if (tabwidget->currentIndex() < 2 || RangeSelectedOnly)
Brush.recalc(LastMouseX, LastMouseY, CurrentSegment);
if (tabwidget->currentIndex() < 2 || RangeSelectedOnly || tabwidget->currentIndex()>2)
{

QByteArray NewLocks;

if (tabwidget->currentIndex()==3) //LCE mode
{
if (LCE_sample.size()!=fwidth*fheight) LCE_sample.resize(fwidth4 * fheight);
// get data from GA array

memcpy(LCE_sample.data(),GA[CurrentSegment]->bits(),fwidth4*fheight);

NewLocks = DoMaskLocking();

}

Brush.recalc(LastMouseX, LastMouseY, CurrentSegment, &LCE_sample, &NewLocks);
}
else
//all segs
for (int i = 0; i < SegmentCount; i++) if (Segments[i]->Activated) Brush.recalc(LastMouseX, LastMouseY, i);
{
QByteArray NewLocks;

if (tabwidget->currentIndex()==3) //LCE mode
{
if (LCE_sample.size()!=fwidth*fheight) LCE_sample.resize(fwidth4 * fheight);
// get data from GA array

memcpy(LCE_sample.data(),GA[CurrentSegment]->bits(),fwidth4*fheight);

NewLocks = DoMaskLocking();

}
for (int i = 0; i < SegmentCount; i++) if (Segments[i]->Activated) Brush.recalc(LastMouseX, LastMouseY, i, &LCE_sample, &NewLocks);
}
break;

}
progressBar->setValue(count++);
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
Expand Down
2 changes: 2 additions & 0 deletions SPIERSedit/src/copyingimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CopyingImpl : public QDialog, public Ui::Copying
void Copy(QDir source, QDir dest);
void MakeNewSegFiles(int snum);
void DeleteSegments(QList <int> list);
void GenerateLCE(QListWidget *SliceSelectorList);
void GenerateLinear(QListWidget *SliceSelectorList);
void GeneratePoly(QListWidget *SliceSelectorList);
void GenerateRange(QListWidget *SliceSelectorList);
Expand Down Expand Up @@ -71,6 +72,7 @@ class CopyingImpl : public QDialog, public Ui::Copying
void WriteSPVData(int, QByteArray, QVector<double> *TrigArray, int TrigCount, QDataStream *out);
QByteArray ExpandGrid(QByteArray *grid, int awidth, int aheight);
QString CountMessage;
QVector <uchar> LCE_sample;
int Count;

private slots:
Expand Down
87 changes: 84 additions & 3 deletions SPIERSedit/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,35 @@ QByteArray DoMaskLocking()
return newlocks;
}

//Do LCE - based on MakeLinearGreyScale
void ApplyLCE(int seg, int fnum, bool flag = false)
{
//load data for file - can and should assume existing data is safe
if (!flag) LoadAllData(fnum);

if (Segments[seg]->Locked) return;
uchar *data= GA[seg]->bits(); // get data from GA array

//make a copy of underlying data
QVector<uchar> data_original_vector(fwidth4 * fheight);
uchar *data_original = data_original_vector.data();
memcpy(data_original,data,fwidth4*fheight);

QByteArray NewLocks = DoMaskLocking();

//ignore inversion - doesn't make sense for LCE
for (int h = 0; h < fheight; h++)
for (int w = 0; w < fwidth; w++)
{
if (!(NewLocks[(fwidth * h + w)]))
*(data + (fwidth4 * h + w))
= LCEPixel(w, h, data_original, &NewLocks);
}
if (!flag) SaveGreyData(fnum, seg);


}

void MakeLinearGreyScale(int seg, int fnum, bool flag = false)
{
//load data for file - can and should assume existing data is safe
Expand Down Expand Up @@ -821,7 +850,7 @@ void MakePolyGreyScale(int seg, int fnum, bool flag = false)



uchar GenPixel(int x, int y, int s)
uchar GenPixel(int x, int y, int s, QVector<uchar> *sample, QByteArray *locks)
{
CurrentPolyContrast = pow(static_cast<double>(2), Segments[s]->PolyContrast) / Segments[s]->PolyScale;
//generate a pixel using whatever method
Expand Down Expand Up @@ -854,9 +883,59 @@ uchar GenPixel(int x, int y, int s)
return t2;
}

if (tabwidget->currentIndex() == 3)
{
return LCEPixel(x,y,sample->data(),locks);
}

return 0;
}

uchar LCEPixel(int w, int h, uchar *original_data, QByteArray *new_locks)
{

int val = (int)original_data[w + fwidth4*h];


//work out min, max h and w for loop
int minw = w-LCE_Radius;
if (minw<0) minw=0;
int maxw = w+LCE_Radius;
if (maxw>=fwidth) maxw = fwidth-1;
int minh = h-LCE_Radius;
if (minh<0) minh=0;
int maxh = h+LCE_Radius;
if (maxh>=fheight) maxh = fheight-1;

int radius_squared = LCE_Radius*LCE_Radius;
quint64 count=0;
quint64 sum=0;
for (int ypos = minh; ypos<=maxh; ypos++)
for (int xpos = minw; xpos<=maxw; xpos++)
{
//Exclude outside radius
int d0 = ypos-h;
int d1 = xpos-w;
if (d0*d0+d1*d1<=radius_squared)
{
if (!(new_locks->at(ypos*fwidth+xpos))) // exclude lock-excluded pixels
{
sum+=static_cast<quint64>(original_data[ypos*fwidth4+xpos]);
count++;
}
}
}
if (count==0) return static_cast<uchar>(val); //probably impossible?
int mean = static_cast<int>(sum/count);

int diff = static_cast<int>(val) - mean;
int boost = (diff * LCE_Boost) / 5 + LCE_Adjust;
val += boost;
if (val<0) return 0;
if (val>255) return 255;
return static_cast<uchar>(val);
}

uchar GreyScalePixel(int w, int h, int r, int g, int b, int glob)
{
w *= ColMonoScale;
Expand Down Expand Up @@ -886,7 +965,9 @@ uchar GreyScalePixel(int w, int h, int r, int g, int b, int glob)
rtot *= glob;
temp = rtot / (ColMonoScale * ColMonoScale * 10000);
}
if (temp < 0) return 0;
if (temp > 255) return 255;


if (temp < 0) temp= 0;
if (temp > 255) temp= 255;
return static_cast<uchar>(temp);
}
5 changes: 4 additions & 1 deletion SPIERSedit/src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ extern void InitImage(QGraphicsView *gv);
extern void DeleteDisplayObjects();
extern void ClearImages();
extern void MakeLinearGreyScale(int seg, int fnum, bool flag);
extern void ApplyLCE(int seg, int fnum, bool flag);
extern void MakeBlankGreyScale(int seg, int fnum, bool flag);
extern uchar GreyScalePixel(int w, int h, int r, int g, int b, int glob);
extern uchar LCEPixel(int w, int h, uchar *original_data, QByteArray *new_locks);
extern void MakePolyGreyScale(int seg, int fnum, bool flag);
extern void MakeRangeGreyScale(int seg, int fnum, bool flag);
extern uchar PolyPixel(int w, int h, int seg);
extern uchar RangePixel(int w, int h, int bot, int top, double cen, double gra, int seg);
extern uchar GenPixel(int x, int y, int s);
extern uchar GenPixel(int x, int y, int s, QVector<uchar> *sample, QByteArray *locks);
extern double CalcPoly(unsigned char r, unsigned char g, unsigned char b, Segment *seg);
extern void SaveMainImage(QString fname);
extern QByteArray DoMaskLocking();

#endif // __DISPLAY_H__
12 changes: 12 additions & 0 deletions SPIERSedit/src/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ void ApplyDefaultSettings()
BrightUp = 10;
BrightDown = 10;
BrightSoft = 0;
LCE_Boost = 10;
LCE_Adjust = 0;
LCE_Radius = 5;
LastTrans = 0;
ThreshFlag = true;
MasksFlag = false;
Expand Down Expand Up @@ -2188,6 +2191,11 @@ void WriteSettings()
out << pitch;
out << roll;
out << Notes;

out << LCE_Boost;
out << LCE_Radius;
out << LCE_Adjust;

file.close();
}

Expand Down Expand Up @@ -2455,6 +2463,9 @@ void ReadSettings()

if (!in.atEnd()) in >> Notes;

if (!in.atEnd()) in >> LCE_Boost;
if (!in.atEnd()) in >> LCE_Radius;
if (!in.atEnd()) in >> LCE_Adjust;
//now doctor Files array using zsparsity, if necessary
if (zsparsity > 1)
{
Expand All @@ -2476,5 +2487,6 @@ void ReadSettings()
}
else Stretches = FullStretches;
file.close();

}

3 changes: 3 additions & 0 deletions SPIERSedit/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ int Brush_Size;
int BrightUp;
int BrightDown;
int BrightSoft;
int LCE_Boost;
int LCE_Radius;
int LCE_Adjust;
int LastTrans;
bool ThreshFlag;
bool MasksFlag;
Expand Down
3 changes: 3 additions & 0 deletions SPIERSedit/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ extern int Brush_Size;
extern int BrightUp;
extern int BrightDown;
extern int BrightSoft;
extern int LCE_Boost;
extern int LCE_Radius;
extern int LCE_Adjust;
extern int LastTrans;
extern int CurrentSegment, CurrentRSegment;
extern bool ThreshFlag, MasksFlag, SegsFlag;
Expand Down

0 comments on commit f1d818d

Please sign in to comment.