/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #pragma once #ifndef READER_HPP #define READER_HPP #include #include #include #include #include #include #include #include #include "task.hpp" using namespace ff; template class Reader : public ff_node_t> { std::vector Images; public: Reader(std::vector images); Task *svc(Task *); void operator()(std::vector images, std::vector *> *> *OutputVector); }; template Task *read_one_image(std::string imagename); template Reader::Reader(std::vector images) : Images(images) {} // svc function for fastflow library, function as an emitter: generates all // tasks from the list of paths template Task *Reader::svc(Task *) { for (std::string s : Images) { Task *t = read_one_image(s); ff_node::ff_send_out(t); } return this->EOS; } // operator for std thread template void Reader::operator()( std::vector images, std::vector *> *> *OutputVector) { assert(images.size() >= (*OutputVector).size() && "Error: wrong length for promise vector [Reader::operator()]"); int count = 0; for (std::promise *> *output : *OutputVector) { output->set_value(read_one_image(images[count])); ++count; } } template Task *read_one_image(std::string image_name) { T *image; int32_t rows; int32_t cols; const std::string &filepath(image_name); std::fstream file{filepath, file.binary | file.in}; if (!file.is_open()) { std::cerr << "Error: Failed to open " << filepath << std::endl; return NULL; } else { file.read(reinterpret_cast(&rows), sizeof(rows)); file.read(reinterpret_cast(&cols), sizeof(cols)); image = new T[rows * cols]; file.read(reinterpret_cast(image), (rows * cols) * sizeof(T)); file.close(); } Task *task; std::vector> *matrix = new std::vector>(rows, std::vector(cols)); for (int x = 0; x < rows; ++x) { for (int y = 0; y < cols; ++y) { (*matrix)[x][y] = image[x * cols + y]; } } task = new Task(matrix, rows, cols, filepath); delete[] image; return task; } #endif /* READER_HPP */