Expose image import simulation through pano cli

This commit is contained in:
2026-06-02 09:59:42 +02:00
parent 1e4b4cad73
commit 3701fd2a71
4 changed files with 152 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
#include "assets/image_format.h"
#include "assets/image_metadata.h"
#include "assets/image_pixels.h"
#include "assets/ppi_header.h"
#include "document/document.h"
#include "document/ppi_import.h"
@@ -10,6 +11,7 @@
#include "renderer_api/recording_renderer.h"
#include "ui_core/layout_xml.h"
#include <array>
#include <cstdint>
#include <fstream>
#include <iostream>
@@ -58,6 +60,11 @@ struct SimulateDocumentEditsArgs {
std::uint32_t height = 64;
};
struct SimulateImageImportArgs {
std::uint32_t width = 64;
std::uint32_t height = 32;
};
struct SimulateDocumentHistoryArgs {
std::uint32_t width = 64;
std::uint32_t height = 32;
@@ -129,6 +136,7 @@ void print_help()
<< " record-render [--width N] [--height N]\n"
<< " simulate-document-edits [--width N] [--height N]\n"
<< " simulate-document-history [--width N] [--height N] [--history N]\n"
<< " simulate-image-import [--width N] [--height N]\n"
<< " simulate-stroke --x1 N --y1 N --x2 N --y2 N [--spacing N]\n"
<< " simulate-stroke-script --path FILE\n"
<< " --help\n";
@@ -820,6 +828,125 @@ int simulate_document_edits(int argc, char** argv)
return 0;
}
pp::foundation::Status parse_simulate_image_import_args(
int argc,
char** argv,
SimulateImageImportArgs& args)
{
for (int i = 2; i < argc; ++i) {
const std::string_view key(argv[i]);
if (key == "--width" || key == "--height") {
if (i + 1 >= argc) {
return pp::foundation::Status::invalid_argument("missing value for option");
}
const auto value = pp::foundation::parse_u32(argv[++i]);
if (!value) {
return value.status();
}
if (key == "--width") {
args.width = value.value();
} else {
args.height = value.value();
}
} else {
return pp::foundation::Status::invalid_argument("unknown option");
}
}
if (args.width == 0 || args.height == 0) {
return pp::foundation::Status::invalid_argument("width and height must be greater than zero");
}
return pp::foundation::Status::success();
}
int simulate_image_import(int argc, char** argv)
{
static constexpr std::array<std::byte, 68> transparent_png_1x1 {
std::byte { 0x89 }, std::byte { 0x50 }, std::byte { 0x4e }, std::byte { 0x47 },
std::byte { 0x0d }, std::byte { 0x0a }, std::byte { 0x1a }, std::byte { 0x0a },
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x0d },
std::byte { 0x49 }, std::byte { 0x48 }, std::byte { 0x44 }, std::byte { 0x52 },
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
std::byte { 0x08 }, std::byte { 0x06 }, std::byte { 0x00 }, std::byte { 0x00 },
std::byte { 0x00 }, std::byte { 0x1f }, std::byte { 0x15 }, std::byte { 0xc4 },
std::byte { 0x89 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
std::byte { 0x0b }, std::byte { 0x49 }, std::byte { 0x44 }, std::byte { 0x41 },
std::byte { 0x54 }, std::byte { 0x78 }, std::byte { 0x9c }, std::byte { 0x63 },
std::byte { 0x60 }, std::byte { 0x00 }, std::byte { 0x02 }, std::byte { 0x00 },
std::byte { 0x00 }, std::byte { 0x05 }, std::byte { 0x00 }, std::byte { 0x01 },
std::byte { 0x7a }, std::byte { 0x5e }, std::byte { 0xab }, std::byte { 0x3f },
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
std::byte { 0x49 }, std::byte { 0x45 }, std::byte { 0x4e }, std::byte { 0x44 },
std::byte { 0xae }, std::byte { 0x42 }, std::byte { 0x60 }, std::byte { 0x82 },
};
SimulateImageImportArgs args;
const auto status = parse_simulate_image_import_args(argc, argv, args);
if (!status.ok()) {
print_error("simulate-image-import", status.message);
return 2;
}
const auto image_result = pp::assets::decode_png_rgba8(transparent_png_1x1);
if (!image_result) {
print_error("simulate-image-import", image_result.status().message);
return 2;
}
const auto document_result = pp::document::CanvasDocument::create(
pp::document::DocumentConfig {
.width = args.width,
.height = args.height,
.layer_count = 1,
});
if (!document_result) {
print_error("simulate-image-import", document_result.status().message);
return 2;
}
const auto& image = image_result.value();
auto document = document_result.value();
const auto import_status = document.set_layer_frame_face_pixels(
0,
0,
pp::document::LayerFacePixels {
.face_index = 0,
.x = 0,
.y = 0,
.width = image.width,
.height = image.height,
.rgba8 = image.pixels,
});
if (!import_status.ok()) {
print_error("simulate-image-import", import_status.message);
return 2;
}
const auto& payload = document.layers()[0].frames[0].face_pixels[0];
std::cout << "{\"ok\":true,\"command\":\"simulate-image-import\""
<< ",\"image\":{\"format\":\"png\",\"width\":" << image.width
<< ",\"height\":" << image.height
<< ",\"bytes\":" << image.pixels.size()
<< ",\"alpha\":" << static_cast<int>(image.pixels[3])
<< "},\"document\":{\"width\":" << document.width()
<< ",\"height\":" << document.height()
<< ",\"layers\":" << document.layers().size()
<< ",\"frames\":" << document.frames().size()
<< ",\"facePayloads\":" << document.face_pixel_payload_count()
<< "},\"payload\":{\"face\":" << payload.face_index
<< ",\"x\":" << payload.x
<< ",\"y\":" << payload.y
<< ",\"width\":" << payload.width
<< ",\"height\":" << payload.height
<< ",\"bytes\":" << payload.rgba8.size()
<< "}}\n";
return 0;
}
pp::foundation::Status parse_simulate_document_history_args(
int argc,
char** argv,
@@ -1165,6 +1292,10 @@ int main(int argc, char** argv)
return simulate_document_history(argc, argv);
}
if (command == "simulate-image-import") {
return simulate_image_import(argc, argv);
}
if (command == "parse-layout") {
return parse_layout(argc, argv);
}