refactor image filtering

This commit is contained in:
Manuel Dewald
2021-08-02 10:49:54 +02:00
parent 854b498a67
commit e3435c5153
2 changed files with 69 additions and 63 deletions

View File

@@ -19,7 +19,7 @@ ImageSelector::ImageSelector(std::unique_ptr<PathTraverser>& pathTraverser, char
ImageSelector::~ImageSelector(){} ImageSelector::~ImageSelector(){}
int ImageSelector::getImageRotation(const std::string &fileName) int ImageSelector::getImageRotation(const std::string& fileName)
{ {
int orientation = 0; int orientation = 0;
ExifData *exifData = exif_data_new_from_file(fileName.c_str()); ExifData *exifData = exif_data_new_from_file(fileName.c_str());
@@ -50,7 +50,29 @@ int ImageSelector::getImageRotation(const std::string &fileName)
return degrees; return degrees;
} }
bool ImageSelector::imageValidForAspect(const std::string &fileName) bool ImageSelector::imageMatchesFilter(const std::string& fileName)
{
if(!QFileInfo::exists(QString(fileName.c_str())))
{
if(debugMode)
{
std::cout << "file not found: " << fileName << std::endl;
}
return false;
}
if(!imageValidForAspect(fileName)) {
if(debugMode)
{
std::cout << "image aspect ratio doesn't match filter '" << aspect << "' : " << fileName << std::endl;
}
return false;
}
return true;
}
bool ImageSelector::imageValidForAspect(const std::string& fileName)
{ {
QPixmap p( fileName.c_str() ); QPixmap p( fileName.c_str() );
int imageWidth = p.width(); int imageWidth = p.width();
@@ -95,19 +117,16 @@ std::string RandomImageSelector::getNextImage()
{ {
std:: string filename; std:: string filename;
try try
{
while (filename.empty())
{ {
QStringList images = pathTraverser->getImages(); QStringList images = pathTraverser->getImages();
unsigned int selectedImage = selectRandom(images); unsigned int selectedImage = selectRandom(images);
filename = pathTraverser->getImagePath(images.at(selectedImage).toStdString()); filename = pathTraverser->getImagePath(images.at(selectedImage).toStdString());
if (!imageValidForAspect(filename)) while(!imageMatchesFilter(filename))
{ {
filename.clear(); unsigned int selectedImage = selectRandom(images);
filename = pathTraverser->getImagePath(images.at(selectedImage).toStdString());
} }
} }
}
catch(const std::string& err) catch(const std::string& err)
{ {
std::cerr << "Error: " << err << std::endl; std::cerr << "Error: " << err << std::endl;
@@ -142,6 +161,24 @@ ShuffleImageSelector::~ShuffleImageSelector()
} }
std::string ShuffleImageSelector::getNextImage() std::string ShuffleImageSelector::getNextImage()
{
reloadImagesIfNoneLeft();
if (images.size() == 0)
{
return "";
}
std::string filename = pathTraverser->getImagePath(images.at(current_image_shuffle).toStdString());
current_image_shuffle = current_image_shuffle + 1; // ignore and move to next image
while(!imageMatchesFilter(filename)) {
reloadImagesIfNoneLeft();
std::string filename = pathTraverser->getImagePath(images.at(current_image_shuffle).toStdString());
current_image_shuffle = current_image_shuffle + 1; // ignore and move to next image
}
std::cout << "updating image: " << filename << std::endl;
return filename;
}
void ShuffleImageSelector::reloadImagesIfNoneLeft()
{ {
if (images.size() == 0 || current_image_shuffle >= images.size()) if (images.size() == 0 || current_image_shuffle >= images.size())
{ {
@@ -152,32 +189,6 @@ std::string ShuffleImageSelector::getNextImage()
std::mt19937 randomizer(rd()); std::mt19937 randomizer(rd());
std::shuffle(images.begin(), images.end(), randomizer); std::shuffle(images.begin(), images.end(), randomizer);
} }
if (images.size() == 0)
{
return "";
}
std::string filename = pathTraverser->getImagePath(images.at(current_image_shuffle).toStdString());
if(!QFileInfo::exists(QString(filename.c_str())))
{
if(debugMode)
{
std::cout << "file not found: " << filename << std::endl;
}
current_image_shuffle = current_image_shuffle + 1; // ignore and move to next image
return getNextImage();
}
if (!imageValidForAspect(filename))
{
if(debugMode)
{
std::cout << "image has invalid aspect: " << filename << "(images left:" << (images.size()-current_image_shuffle) << ")" << std::endl;
}
current_image_shuffle = current_image_shuffle + 1; // ignore and move to next image
return getNextImage();
}
std::cout << "updating image: " << filename << std::endl;
current_image_shuffle = current_image_shuffle + 1;
return filename;
} }
SortedImageSelector::SortedImageSelector(std::unique_ptr<PathTraverser>& pathTraverser, char aspect): SortedImageSelector::SortedImageSelector(std::unique_ptr<PathTraverser>& pathTraverser, char aspect):
@@ -204,6 +215,23 @@ bool operator<(const QString& lhs, const QString& rhs) noexcept{
} }
std::string SortedImageSelector::getNextImage() std::string SortedImageSelector::getNextImage()
{
reloadImagesIfEmpty();
if (images.size() == 0)
{
return "";
}
std::string filename = pathTraverser->getImagePath(images.takeFirst().toStdString());
while(!imageMatchesFilter(filename)) {
reloadImagesIfEmpty();
filename = pathTraverser->getImagePath(images.takeFirst().toStdString());
}
std::cout << "updating image: " << filename << std::endl;
return filename;
}
void SortedImageSelector::reloadImagesIfEmpty()
{ {
if (images.size() == 0) if (images.size() == 0)
{ {
@@ -213,33 +241,8 @@ std::string SortedImageSelector::getNextImage()
{ {
std::cout << "read " << images.size() << " images." << std::endl; std::cout << "read " << images.size() << " images." << std::endl;
for (int i = 0;i <images.size();i++){ for (int i = 0;i <images.size();i++){
std::cout << images[i].toStdString() << std::endl; std::cout << images[i].toStdString() << std::endl;
} }
} }
} }
if (images.size() == 0)
{
return "";
}
std::string filename = pathTraverser->getImagePath(images.takeFirst().toStdString());
if(!QFileInfo::exists(QString(filename.c_str())))
{
if(debugMode)
{
std::cout << "file not found: " << filename << std::endl;
}
return getNextImage();
}
if (!imageValidForAspect(filename))
{
if(debugMode)
{
std::cout << "image has invalid aspect: " << filename << std::endl;
}
return getNextImage();
}
std::cout << "updating image: " << filename << std::endl;
return filename;
} }

View File

@@ -17,8 +17,9 @@ public:
void setDebugMode(bool debugModeIn) { debugMode = debugModeIn;} void setDebugMode(bool debugModeIn) { debugMode = debugModeIn;}
protected: protected:
int getImageRotation(const std::string &fileName); int getImageRotation(const std::string& fileName);
bool imageValidForAspect(const std::string &fileName); bool imageMatchesFilter(const std::string& fileName);
bool imageValidForAspect(const std::string& fileName);
std::unique_ptr<PathTraverser>& pathTraverser; std::unique_ptr<PathTraverser>& pathTraverser;
char aspect; char aspect;
bool debugMode = false; bool debugMode = false;
@@ -43,6 +44,7 @@ public:
virtual std::string getNextImage(); virtual std::string getNextImage();
private: private:
void reloadImagesIfNoneLeft();
int current_image_shuffle; int current_image_shuffle;
QStringList images; QStringList images;
}; };
@@ -55,6 +57,7 @@ public:
virtual std::string getNextImage(); virtual std::string getNextImage();
private: private:
void reloadImagesIfEmpty();
QStringList images; QStringList images;
}; };
#endif // IMAGESELECTOR_H #endif // IMAGESELECTOR_H