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

#include <EGrabber.h>

using namespace Euresys;

static void sample() {
    EGenTL genTL; // load GenTL producer
    EGrabberDiscovery egrabberDiscovery(genTL);
    egrabberDiscovery.discover();
    EGrabber<CallbackOnDemand> grabber(egrabberDiscovery.cameras(0));

    int64_t widthMax = grabber.getInteger<RemoteModule>("WidthMax");

    if (grabber.getInteger<StreamModule>(query::available("BufferHeight"))) {

        grabber.setInteger<RemoteModule>("Width", widthMax / 2);
        grabber.setInteger<StreamModule>("BufferHeight", widthMax / 2);
        grabber.setInteger<StreamModule>("ScanLength", widthMax / 2);

        grabber.reallocBuffers(20); // prepare 20 buffers

        grabber.start(20); // grab 20 buffers
        for (size_t frame = 0; frame < 20; ++frame) {
            ScopedBuffer buffer(grabber); // wait and get a buffer
            // Note: ScopedBuffer pushes the buffer back to the input queue automatically
            ConvertedBuffer bgr = buffer.convert("BGR8");
            // output the converted buffer
            bgr.saveToDisk(Tools::getEnv("sample-output-path") + "/frame.NNN.jpeg", frame);
        }

    }
}

static Tools::Sample addSample(__FILE__, sample, "Set image size and Grab N frames (line-scan)");
