53 lines
2.4 KiB
C++
53 lines
2.4 KiB
C++
|
|
#include "camerasnapshotdecoder.h"
|
||
|
|
#include <QtCore/QBuffer>
|
||
|
|
#include <QCoreApplication>
|
||
|
|
#include <iostream>
|
||
|
|
#include <stdexcept>
|
||
|
|
|
||
|
|
static void require(bool ok, const char *message)
|
||
|
|
{
|
||
|
|
if (!ok) throw std::runtime_error(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(int argc, char **argv)
|
||
|
|
{
|
||
|
|
QCoreApplication app(argc, argv);
|
||
|
|
try {
|
||
|
|
cmvr::api::FrameData frame;
|
||
|
|
frame.set_codec("none");
|
||
|
|
frame.set_type(cmvr::api::FrameData::U8C3);
|
||
|
|
frame.set_width(1); frame.set_height(2); // Unaligned, tightly packed rows.
|
||
|
|
const char bgr[] = {0, 0, char(255), 0, char(255), 0};
|
||
|
|
frame.set_data(bgr, sizeof bgr);
|
||
|
|
QString error;
|
||
|
|
QImage image = decodeCameraSnapshot(frame, error);
|
||
|
|
require(error.isEmpty() && image.pixelColor(0, 0) == QColor(Qt::red)
|
||
|
|
&& image.pixelColor(0, 1) == QColor(Qt::green), "BGR8 rows/colors");
|
||
|
|
frame.clear_data();
|
||
|
|
require(image.pixelColor(0, 0) == QColor(Qt::red), "decoded image must own bytes");
|
||
|
|
require(decodeCameraSnapshot(frame, error).isNull() && !error.isEmpty(), "truncated raw data");
|
||
|
|
frame.set_type(cmvr::api::FrameData::U8C1);
|
||
|
|
frame.set_data(std::string("\x10\xf0", 2));
|
||
|
|
image = decodeCameraSnapshot(frame, error);
|
||
|
|
require(image.pixelColor(0, 0).red() == 16 && image.pixelColor(0, 1).red() == 240, "gray8");
|
||
|
|
frame.set_height(1); frame.set_type(cmvr::api::FrameData::U16C3);
|
||
|
|
const quint16 values[] = {0, 32768, 65535};
|
||
|
|
frame.set_data(values, sizeof values);
|
||
|
|
image = decodeCameraSnapshot(frame, error);
|
||
|
|
require(image.pixelColor(0, 0) == QColor(255, 128, 0), "BGR16 full-range display");
|
||
|
|
frame.set_width(std::numeric_limits<int>::max());
|
||
|
|
frame.set_height(std::numeric_limits<int>::max());
|
||
|
|
require(decodeCameraSnapshot(frame, error).isNull(), "invalid dimensions");
|
||
|
|
QByteArray png;
|
||
|
|
QBuffer buffer(&png); buffer.open(QIODevice::WriteOnly);
|
||
|
|
require(image.save(&buffer, "PNG"), "PNG encoding fixture");
|
||
|
|
frame.set_codec("png"); frame.set_data(png.constData(), png.size());
|
||
|
|
image = decodeCameraSnapshot(frame, error);
|
||
|
|
require(!image.isNull() && image.pixelColor(0, 0) == QColor(255, 128, 0), "encoded snapshot");
|
||
|
|
std::cout << "Camera snapshot tests passed: BGR8, gray8, BGR16, PNG, ownership, invalid input\n";
|
||
|
|
return 0;
|
||
|
|
} catch (const std::exception &e) {
|
||
|
|
std::cerr << e.what() << '\n'; return 1;
|
||
|
|
}
|
||
|
|
}
|