refactor image filtering
This commit is contained in:
@@ -50,6 +50,28 @@ int ImageSelector::getImageRotation(const std::string &fileName)
|
||||
return degrees;
|
||||
}
|
||||
|
||||
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() );
|
||||
@@ -95,19 +117,16 @@ std::string RandomImageSelector::getNextImage()
|
||||
{
|
||||
std:: string filename;
|
||||
try
|
||||
{
|
||||
while (filename.empty())
|
||||
{
|
||||
QStringList images = pathTraverser->getImages();
|
||||
unsigned int selectedImage = selectRandom(images);
|
||||
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)
|
||||
{
|
||||
std::cerr << "Error: " << err << std::endl;
|
||||
@@ -142,6 +161,24 @@ ShuffleImageSelector::~ShuffleImageSelector()
|
||||
}
|
||||
|
||||
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())
|
||||
{
|
||||
@@ -152,32 +189,6 @@ std::string ShuffleImageSelector::getNextImage()
|
||||
std::mt19937 randomizer(rd());
|
||||
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):
|
||||
@@ -204,6 +215,23 @@ bool operator<(const QString& lhs, const QString& rhs) noexcept{
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -213,33 +241,8 @@ std::string SortedImageSelector::getNextImage()
|
||||
{
|
||||
std::cout << "read " << images.size() << " images." << std::endl;
|
||||
for (int i = 0;i <images.size();i++){
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -18,6 +18,7 @@ public:
|
||||
|
||||
protected:
|
||||
int getImageRotation(const std::string& fileName);
|
||||
bool imageMatchesFilter(const std::string& fileName);
|
||||
bool imageValidForAspect(const std::string& fileName);
|
||||
std::unique_ptr<PathTraverser>& pathTraverser;
|
||||
char aspect;
|
||||
@@ -43,6 +44,7 @@ public:
|
||||
virtual std::string getNextImage();
|
||||
|
||||
private:
|
||||
void reloadImagesIfNoneLeft();
|
||||
int current_image_shuffle;
|
||||
QStringList images;
|
||||
};
|
||||
@@ -55,6 +57,7 @@ public:
|
||||
virtual std::string getNextImage();
|
||||
|
||||
private:
|
||||
void reloadImagesIfEmpty();
|
||||
QStringList images;
|
||||
};
|
||||
#endif // IMAGESELECTOR_H
|
||||
|
||||
Reference in New Issue
Block a user