Add stroke script document automation

This commit is contained in:
2026-06-02 11:31:08 +02:00
parent 0eded78c4c
commit 2b50c2157f
6 changed files with 394 additions and 5 deletions

View File

@@ -12,8 +12,10 @@
#include "renderer_api/recording_renderer.h"
#include "ui_core/layout_xml.h"
#include <algorithm>
#include <array>
#include <charconv>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iostream>
@@ -96,6 +98,13 @@ struct SimulateStrokeScriptArgs {
std::string path;
};
struct ApplyStrokeScriptArgs {
std::string path;
std::string output_path;
std::uint32_t width = 64;
std::uint32_t height = 32;
};
struct SimulateDocumentEditsArgs {
std::uint32_t width = 128;
std::uint32_t height = 64;
@@ -212,6 +221,7 @@ void print_help()
{
std::cout
<< "pano_cli commands:\n"
<< " apply-stroke-script --path FILE --output FILE [--width N] [--height N]\n"
<< " create-document --width N --height N [--layers N] [--frames N] [--frame-duration-ms N]\n"
<< " export-image --path FILE [--width N] [--height N]\n"
<< " inspect-image --path FILE\n"
@@ -1350,6 +1360,254 @@ int simulate_stroke_script(int argc, char** argv)
return 0;
}
pp::foundation::Status parse_apply_stroke_script_args(int argc, char** argv, ApplyStrokeScriptArgs& args)
{
for (int i = 2; i < argc; ++i) {
const std::string_view key(argv[i]);
if (key == "--path" || key == "--output") {
if (i + 1 >= argc) {
return pp::foundation::Status::invalid_argument("missing value for option");
}
if (key == "--path") {
args.path = argv[++i];
} else {
args.output_path = argv[++i];
}
} else 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.path.empty() || args.output_path.empty()) {
return pp::foundation::Status::invalid_argument("path and output must not be empty");
}
if (args.width < 8 || args.height < 8 || args.width > 4096 || args.height > 4096) {
return pp::foundation::Status::out_of_range("width and height must be between 8 and 4096");
}
return pp::foundation::Status::success();
}
std::uint8_t pressure_to_alpha(float pressure) noexcept
{
const auto normalized = std::clamp(pressure, 0.0F, 1.0F);
const auto alpha = static_cast<int>(std::lround(normalized * 255.0F));
return static_cast<std::uint8_t>(std::clamp(alpha, 1, 255));
}
std::uint32_t clamp_sample_coordinate(float value, std::uint32_t limit) noexcept
{
if (limit == 0U) {
return 0;
}
const auto rounded = static_cast<int>(std::lround(value));
const auto clamped = std::clamp(rounded, 0, static_cast<int>(limit - 1U));
return static_cast<std::uint32_t>(clamped);
}
int apply_stroke_script(int argc, char** argv)
{
ApplyStrokeScriptArgs args;
const auto status = parse_apply_stroke_script_args(argc, argv, args);
if (!status.ok()) {
print_error("apply-stroke-script", status.message);
return 2;
}
std::ifstream script_stream(args.path, std::ios::binary);
if (!script_stream) {
print_error("apply-stroke-script", "stroke script file could not be opened");
return 2;
}
const std::string text {
std::istreambuf_iterator<char>(script_stream),
std::istreambuf_iterator<char>()
};
const auto script = pp::paint::parse_stroke_script(text);
if (!script) {
print_error("apply-stroke-script", script.status().message);
return 2;
}
struct AppliedPoint {
std::uint32_t x = 0;
std::uint32_t y = 0;
std::uint32_t stroke_index = 0;
float pressure = 1.0F;
};
std::vector<AppliedPoint> applied_points;
std::vector<std::size_t> samples_per_stroke;
std::size_t total_samples = 0;
float total_distance = 0.0F;
std::uint32_t x0 = args.width;
std::uint32_t y0 = args.height;
std::uint32_t x1 = 0;
std::uint32_t y1 = 0;
for (std::size_t stroke_index = 0; stroke_index < script.value().strokes.size(); ++stroke_index) {
const auto& stroke = script.value().strokes[stroke_index];
const pp::paint::StrokePoint points[] { stroke.start, stroke.end };
const auto samples = pp::paint::sample_stroke(
points,
pp::paint::StrokeSamplingConfig {
.spacing = stroke.spacing,
});
if (!samples) {
print_error("apply-stroke-script", samples.status().message);
return 2;
}
samples_per_stroke.push_back(samples.value().size());
total_samples += samples.value().size();
total_distance += samples.value().back().distance;
for (const auto& sample : samples.value()) {
const auto x = clamp_sample_coordinate(sample.x, args.width);
const auto y = clamp_sample_coordinate(sample.y, args.height);
x0 = std::min(x0, x);
y0 = std::min(y0, y);
x1 = std::max(x1, x);
y1 = std::max(y1, y);
applied_points.push_back(AppliedPoint {
.x = x,
.y = y,
.stroke_index = static_cast<std::uint32_t>(stroke_index),
.pressure = sample.pressure,
});
}
}
if (applied_points.empty()) {
print_error("apply-stroke-script", "stroke script produced no samples");
return 2;
}
const auto payload_width = x1 - x0 + 1U;
const auto payload_height = y1 - y0 + 1U;
std::vector<std::uint8_t> rgba8(
static_cast<std::size_t>(payload_width) * payload_height * pp::document::rgba8_components,
0);
for (std::size_t point_index = 0; point_index < applied_points.size(); ++point_index) {
const auto& point = applied_points[point_index];
const auto local_x = point.x - x0;
const auto local_y = point.y - y0;
const auto pixel_index = (static_cast<std::size_t>(local_y) * payload_width + local_x)
* pp::document::rgba8_components;
rgba8[pixel_index + 0U] = static_cast<std::uint8_t>(64U + ((point.stroke_index * 53U) % 192U));
rgba8[pixel_index + 1U] = static_cast<std::uint8_t>((point_index * 31U) % 256U);
rgba8[pixel_index + 2U] = static_cast<std::uint8_t>(255U - ((point.stroke_index * 29U) % 192U));
rgba8[pixel_index + 3U] = pressure_to_alpha(point.pressure);
}
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("apply-stroke-script", document_result.status().message);
return 2;
}
auto document = document_result.value();
auto edit_status = document.rename_layer(0, "Stroke Script");
if (!edit_status.ok()) {
print_error("apply-stroke-script", edit_status.message);
return 2;
}
edit_status = document.set_layer_frame_face_pixels(
0,
0,
pp::document::LayerFacePixels {
.face_index = 0,
.x = x0,
.y = y0,
.width = payload_width,
.height = payload_height,
.rgba8 = std::move(rgba8),
});
if (!edit_status.ok()) {
print_error("apply-stroke-script", edit_status.message);
return 2;
}
const auto exported = pp::document::export_ppi_project_document(document);
if (!exported) {
print_error("apply-stroke-script", exported.status().message);
return 2;
}
std::ofstream output_stream(args.output_path, std::ios::binary);
if (!output_stream) {
print_error("apply-stroke-script", "project file could not be opened for writing");
return 2;
}
const auto& bytes = exported.value();
output_stream.write(reinterpret_cast<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
if (!output_stream) {
print_error("apply-stroke-script", "project file could not be written");
return 2;
}
const auto decoded = pp::assets::decode_ppi_project_images(bytes);
if (!decoded) {
print_error("apply-stroke-script", decoded.status().message);
return 2;
}
std::cout << "{\"ok\":true,\"command\":\"apply-stroke-script\""
<< ",\"path\":\"" << json_escape(args.path) << "\""
<< ",\"output\":\"" << json_escape(args.output_path) << "\""
<< ",\"strokes\":" << script.value().strokes.size()
<< ",\"samples\":" << total_samples
<< ",\"distance\":" << total_distance
<< ",\"samplesPerStroke\":[";
for (std::size_t stroke_index = 0; stroke_index < samples_per_stroke.size(); ++stroke_index) {
if (stroke_index != 0U) {
std::cout << ",";
}
std::cout << samples_per_stroke[stroke_index];
}
std::cout << "],\"document\":{\"width\":" << document.width()
<< ",\"height\":" << document.height()
<< ",\"layers\":" << document.layers().size()
<< ",\"frames\":" << document.frames().size()
<< ",\"facePayloads\":" << document.face_pixel_payload_count()
<< "},\"payload\":{\"face\":0"
<< ",\"box\":[" << x0 << "," << y0 << "," << (x1 + 1U) << "," << (y1 + 1U) << "]"
<< ",\"width\":" << payload_width
<< ",\"height\":" << payload_height
<< ",\"bytes\":" << document.layers()[0].frames[0].face_pixels[0].rgba8.size()
<< "},\"export\":{\"bytes\":" << bytes.size()
<< ",\"dirtyFaces\":" << decoded.value().project.body.summary.dirty_face_count
<< ",\"rgbaFacePayloads\":" << decoded.value().project.body.summary.rgba_face_payload_count
<< ",\"compressedBytes\":" << decoded.value().project.body.summary.compressed_face_bytes
<< "}}\n";
return 0;
}
pp::foundation::Status parse_simulate_document_edits_args(
int argc,
char** argv,
@@ -2119,6 +2377,10 @@ int main(int argc, char** argv)
return simulate_stroke_script(argc, argv);
}
if (command == "apply-stroke-script") {
return apply_stroke_script(argc, argv);
}
if (command == "simulate-document-edits") {
return simulate_document_edits(argc, argv);
}