#include "../tools/tools.h"

#include <EGrabber.h>

using namespace Euresys;

static void process(ConvertedBuffer &buffer, uint64_t idx) {
    Tools::log("Processing " + Tools::toString(buffer.getWidth())
            + "x" + Tools::toString(buffer.getHeight())
            + " @ " + Tools::toString(buffer.getPixels()));
    buffer.saveToDisk(Tools::getEnv("sample-output-path") + "/frame.NNN.jpeg", idx);
}

static void sample() {
    EGenTL genTL; // load GenTL producer
    EGrabber<CallbackOnDemand> grabber(genTL); // create grabber

    grabber.reallocBuffers(5); // prepare 5 buffer
    static const uint64_t COUNT = 20;
    grabber.start(COUNT); // grab COUNT buffer

    for (uint64_t i = 0; i < COUNT; ++i) {
        ScopedBuffer buffer(grabber); // wait and get a buffer
        ConvertedBuffer mono8 = buffer.convert("Mono8");
        process(mono8, i);
    }
}

static Tools::Sample addSample(__FILE__, sample, "Buffer format conversions using ConvertedBuffer");
