Start assets component image signature tests

This commit is contained in:
2026-05-31 23:55:20 +02:00
parent ec5ecbdb54
commit 99eda95cee
12 changed files with 315 additions and 10 deletions

View File

@@ -1,9 +1,14 @@
#include "assets/image_format.h"
#include "foundation/parse.h"
#include "foundation/result.h"
#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
namespace {
@@ -13,6 +18,10 @@ struct DocumentArgs {
std::uint32_t layers = 1;
};
struct InspectImageArgs {
std::string path;
};
void print_error(std::string_view command, std::string_view message)
{
std::cout << "{\"ok\":false,\"command\":\"" << command
@@ -24,6 +33,7 @@ void print_help()
std::cout
<< "pano_cli commands:\n"
<< " create-document --width N --height N [--layers N]\n"
<< " inspect-image --path FILE\n"
<< " --help\n";
}
@@ -81,6 +91,60 @@ int create_document(int argc, char** argv)
return 0;
}
pp::foundation::Status parse_inspect_image_args(int argc, char** argv, InspectImageArgs& args)
{
for (int i = 2; i < argc; ++i) {
const std::string_view key(argv[i]);
if (key == "--path") {
if (i + 1 >= argc) {
return pp::foundation::Status::invalid_argument("missing value for option");
}
args.path = argv[++i];
} else {
return pp::foundation::Status::invalid_argument("unknown option");
}
}
if (args.path.empty()) {
return pp::foundation::Status::invalid_argument("path must not be empty");
}
return pp::foundation::Status::success();
}
int inspect_image(int argc, char** argv)
{
InspectImageArgs args;
const auto status = parse_inspect_image_args(argc, argv, args);
if (!status.ok()) {
print_error("inspect-image", status.message);
return 2;
}
std::ifstream stream(args.path, std::ios::binary);
if (!stream) {
print_error("inspect-image", "image file could not be opened");
return 2;
}
const std::vector<char> chars {
std::istreambuf_iterator<char>(stream),
std::istreambuf_iterator<char>()
};
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
const auto format = pp::assets::detect_image_format(std::span<const std::byte>(data, chars.size()));
if (!format) {
print_error("inspect-image", format.status().message);
return 2;
}
std::cout << "{\"ok\":true,\"command\":\"inspect-image\",\"format\":\""
<< pp::assets::image_format_name(format.value())
<< "\",\"bytes\":" << chars.size() << "}\n";
return 0;
}
}
int main(int argc, char** argv)
@@ -100,6 +164,10 @@ int main(int argc, char** argv)
return create_document(argc, argv);
}
if (command == "inspect-image") {
return inspect_image(argc, argv);
}
print_error(command, "unknown command");
return 2;
}