I have some code that runs a bunch of threads in parallel. Each thread execute a function in order to copy a "watermark" over an image loaded from a directory, and save the modified image in another directory, specified by the user. In order to achieve the result I have to use the CImg library. I can't figure out why sometimes CImg throws CImgIOException
like CImg<unsigned char>::save_other(): Failed to save file
, or CImg<unsigned char>::save_jpeg(): Failed to save file
. I have imagemagick installed, so there shouldn't be a problem to save and open images using the jpeg/jpg format.
This is my code:
#include <atomic>
#include <chrono>
#include "CImg.h"
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <iostream>
#include <mutex>
#include <optional>
#include <queue>
#include <string>
#include <thread>
#include <typeinfo>
using namespace cimg_library;
std::mutex IMAGES_MUTEX, IO_MUTEX;
std::atomic_int PROCESSED_IMAGES = 0;
void apply_watermark(std::queue<std::string>& images, CImg<unsigned char>& watermark, int workload, \
std::string output_dir, int id) {
std::queue<std::string> to_process;
int counter = 0;
bool stop = false;
CImg<unsigned char> img;
while (!stop) {
IMAGES_MUTEX.lock();
while(counter < workload) {
if (images.empty()) {
std::cout << "Thread " << id << ": founded empty queue" << std::endl;
counter = workload;
stop = true;
} else {
std::cout << "Thread " << id << ": aquiring image" << images.front() << std::endl;
to_process.push(images.front());
images.pop();
counter += 1;
}
}
IMAGES_MUTEX.unlock();
counter = 0;
while(!(to_process.empty())) {
img.assign(to_process.front().c_str());
if (!(img.width() != 1024 || img.height() != 768)) {
cimg_forXY(watermark, x, y) {
int R = (int)watermark(x, y, 0, 0);
if (R != 255) {
img(x, y, 0, 0) = 0;
img(x, y, 0, 1) = 0;
img(x, y, 0, 2) = 0;
}
}
std::string fname = to_process.front().substr(to_process.front().find_last_of('/') + 1);
IO_MUTEX.lock();
std::cout << "Thread " << id << ": saving image " << fname << std::endl;
IO_MUTEX.unlock();
img.save_jpeg(((std::string)output_dir + (std::string)"/" + fname).c_str());
to_process.pop();
PROCESSED_IMAGES += 1;
} else {
to_process.pop();
}
img.clear();
}
}
return;
}
int main(int argc, char const *argv[]) {
auto completion_time_start = std::chrono::high_resolution_clock::now();
if (argc != 5) {
std::cout << "MISSING PARAMETERS!" << std::endl;
return -1;
}
int par_degree = std::atoi(argv[3]);
if (!(std::filesystem::exists(argv[2]))) {
std::cout << "WATERMARK NOT FOUND!" << std::endl;
return -1;
}
CImg<unsigned char> wtrk(argv[2]);
std::queue<std::string> images;
if (!(std::filesystem::exists(argv[1]))) {
std::cout << "IMAGES' DIRECTORY NOT FOUND!" << std::endl;
return -1;
}
for (auto& path : std::filesystem::directory_iterator(argv[1])) {
std::string fname = path.path().string().substr(path.path().string().find_last_of('/') + 1);
if (fname != ".DS_Store") {
images.push(path.path().string());
}
}
if (!(std::filesystem::exists((std::string)argv[4]))) {
std::filesystem::create_directory((std::string)argv[4]);
}
if (par_degree == 1) {
while(!(images.empty())) {
CImg<unsigned char> img(images.front().c_str());
if (!(img.width() != 1024 || img.height() != 768)) {
cimg_forXY(wtrk, x, y) {
int R = (int)wtrk(x, y, 0, 0);
if (R != 255) {
img(x, y, 0, 0) = 0;
img(x, y, 0, 1) = 0;
img(x, y, 0, 2) = 0;
}
}
std::string fname = images.front().substr(images.front().find_last_of('/') + 1);
img.save_jpeg(((std::string)argv[4] + (std::string)"/" + fname).c_str());
images.pop();
PROCESSED_IMAGES += 1;
} else {
images.pop();
}
}
} else {
int workload = (int)images.size() / par_degree;
std::thread workers[par_degree];
for (int i = 0; i < par_degree; i++) {
workers[i] = std::thread(apply_watermark, std::ref(images), std::ref(wtrk), workload, \
(std::string)argv[4], i);
}
for (int i = 0; i < par_degree; i++) {
workers[i].join();
}
}
auto completion_time_end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::ratio<1>> completion_time = completion_time_end - \
completion_time_start;
std::cout << "\nPARALLELISM DEGREE: " << par_degree << std::endl;
std::cout << "COMPLETION TIME: " << completion_time.count() << " SECONDS" << std::endl;
std::cout << "PROCESSED IMAGES: " << PROCESSED_IMAGES << std::endl;
return 0;
}
All the exceptions are throwed during the saving of an image, I'm quite new to the C++ language, and I really have no clue. Running the program with only one thread, the main one, doesn't create any problem.
This is an example of launch command:
./test ../imgs ../watermark.jpg 4 ../output_dir
Any help will be appreciated.