#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_export.h" #include "document/ppi_import.h" #include "foundation/parse.h" #include "foundation/result.h" #include "paint/stroke.h" #include "paint/stroke_script.h" #include "renderer_api/recording_renderer.h" #include "ui_core/layout_xml.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { struct DocumentArgs { std::uint32_t width = 0; std::uint32_t height = 0; std::uint32_t layers = 1; std::uint32_t frames = 1; std::uint32_t frame_duration_ms = 100; }; struct SaveProjectArgs { std::string path; std::uint32_t width = 0; std::uint32_t height = 0; std::string layer_name = "Ink"; float layer_opacity = 1.0F; std::uint32_t blend_mode = 0; bool alpha_locked = false; bool visible = true; std::uint32_t layers = 1; std::uint32_t frames = 1; std::uint32_t frame_duration_ms = 100; bool include_test_face_payload = false; std::uint32_t payload_layer = 0; std::uint32_t payload_frame = 0; }; struct SaveDocumentProjectArgs { std::string path; std::uint32_t width = 64; std::uint32_t height = 32; }; struct InspectImageArgs { std::string path; }; struct ImportImageArgs { std::string path; std::uint32_t document_width = 0; std::uint32_t document_height = 0; std::uint32_t face = 0; std::uint32_t x = 0; std::uint32_t y = 0; }; struct ExportImageArgs { std::string path; std::uint32_t width = 2; std::uint32_t height = 2; }; struct ParseLayoutArgs { std::string path; }; struct InspectProjectArgs { std::string path; }; struct SimulateStrokeArgs { std::uint32_t x1 = 0; std::uint32_t y1 = 0; std::uint32_t x2 = 0; std::uint32_t y2 = 0; std::uint32_t spacing = 1; }; 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; }; struct SimulateImageImportArgs { std::uint32_t width = 64; std::uint32_t height = 32; }; struct SimulateDocumentExportArgs { std::uint32_t width = 64; std::uint32_t height = 32; }; struct SimulateDocumentHistoryArgs { std::uint32_t width = 64; std::uint32_t height = 32; std::uint32_t history_entries = 4; }; struct RecordRenderArgs { std::uint32_t width = 64; std::uint32_t height = 32; }; void print_error(std::string_view command, std::string_view message) { std::cout << "{\"ok\":false,\"command\":\"" << command << "\",\"error\":\"" << message << "\"}\n"; } std::span transparent_png_1x1_bytes() noexcept { static constexpr std::array 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 }, }; return transparent_png_1x1; } std::string json_escape(std::string_view value) { constexpr char hex[] = "0123456789abcdef"; std::string escaped; escaped.reserve(value.size()); for (const unsigned char ch : value) { switch (ch) { case '"': escaped += "\\\""; break; case '\\': escaped += "\\\\"; break; case '\b': escaped += "\\b"; break; case '\f': escaped += "\\f"; break; case '\n': escaped += "\\n"; break; case '\r': escaped += "\\r"; break; case '\t': escaped += "\\t"; break; default: if (ch < 0x20U) { escaped += "\\u00"; escaped.push_back(hex[(ch >> 4U) & 0x0fU]); escaped.push_back(hex[ch & 0x0fU]); } else { escaped.push_back(static_cast(ch)); } break; } } return escaped; } pp::foundation::Result parse_float_arg(std::string_view text) { float value = 0.0F; const auto* begin = text.data(); const auto* end = begin + text.size(); const auto [ptr, ec] = std::from_chars(begin, end, value); if (ec != std::errc {} || ptr != end) { return pp::foundation::Result::failure( pp::foundation::Status::invalid_argument("invalid floating-point value")); } return pp::foundation::Result::success(value); } 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" << " import-image --path FILE [--document-width N] [--document-height N] [--face N] [--x N] [--y N]\n" << " inspect-project --path FILE\n" << " load-project --path FILE\n" << " parse-layout --path FILE\n" << " record-render [--width N] [--height N]\n" << " save-document-project --path FILE [--width N] [--height N]\n" << " save-project --path FILE --width N --height N [--layer-name NAME] [--layer-opacity N] [--blend-mode N] [--alpha-locked] [--hidden] [--layers N] [--frames N] [--frame-duration-ms N] [--include-test-face-payload] [--payload-layer N] [--payload-frame N]\n" << " simulate-document-edits [--width N] [--height N]\n" << " simulate-document-export [--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"; } pp::foundation::Status parse_document_args(int argc, char** argv, DocumentArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--width" || key == "--height" || key == "--layers" || key == "--frames" || key == "--frame-duration-ms") { 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 if (key == "--height") { args.height = value.value(); } else if (key == "--layers") { args.layers = value.value(); } else if (key == "--frames") { args.frames = value.value(); } else { args.frame_duration_ms = 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"); } if (args.layers == 0) { return pp::foundation::Status::invalid_argument("layer count must be greater than zero"); } if (args.frames == 0) { return pp::foundation::Status::invalid_argument("frame count must be greater than zero"); } if (args.frame_duration_ms == 0) { return pp::foundation::Status::invalid_argument("frame duration must be greater than zero"); } return pp::foundation::Status::success(); } int create_document(int argc, char** argv) { DocumentArgs args; const auto status = parse_document_args(argc, argv, args); if (!status.ok()) { print_error("create-document", status.message); return 2; } const auto document_result = pp::document::CanvasDocument::create( pp::document::DocumentConfig { .width = args.width, .height = args.height, .layer_count = args.layers, }); if (!document_result) { print_error("create-document", document_result.status().message); return 2; } auto document = document_result.value(); const auto duration_status = document.set_frame_duration(0, args.frame_duration_ms); if (!duration_status.ok()) { print_error("create-document", duration_status.message); return 2; } for (std::uint32_t i = 1; i < args.frames; ++i) { const auto added_frame = document.add_frame(args.frame_duration_ms); if (!added_frame) { print_error("create-document", added_frame.status().message); return 2; } } std::cout << "{\"ok\":true,\"command\":\"create-document\",\"document\":{" << "\"width\":" << document.width() << ",\"height\":" << document.height() << ",\"layers\":" << document.layers().size() << ",\"activeLayer\":" << document.active_layer_index() << ",\"frames\":" << document.frames().size() << ",\"activeFrame\":" << document.active_frame_index() << ",\"animationDurationMs\":" << document.animation_duration_ms() << "}}\n"; return 0; } pp::foundation::Result create_export_sample_document( std::uint32_t width, std::uint32_t height) { const std::vector red_pixel { 255, 0, 0, 255 }; const std::vector blue_pixel { 0, 0, 255, 128 }; const pp::document::AnimationFrame root_frames[] { { .duration_ms = 100, .face_pixels = {} }, { .duration_ms = 250, .face_pixels = {} }, }; const pp::document::AnimationFrame base_frames[] { { .duration_ms = 100, .face_pixels = { pp::document::LayerFacePixels { .face_index = 0, .x = 2, .y = 3, .width = 1, .height = 1, .rgba8 = red_pixel, }, }, }, { .duration_ms = 250, .face_pixels = {} }, }; const pp::document::AnimationFrame paint_frames[] { { .duration_ms = 333, .face_pixels = { pp::document::LayerFacePixels { .face_index = 5, .x = 4, .y = 5, .width = 1, .height = 1, .rgba8 = blue_pixel, }, }, }, }; const pp::document::DocumentLayerConfig layers[] { { .name = "Base", .visible = true, .alpha_locked = false, .opacity = 1.0F, .blend_mode = pp::paint::BlendMode::normal, .frames = std::span(base_frames, 2), }, { .name = "Paint", .visible = false, .alpha_locked = true, .opacity = 0.5F, .blend_mode = pp::paint::BlendMode::overlay, .frames = std::span(paint_frames, 1), }, }; return pp::document::CanvasDocument::create_from_snapshot( pp::document::DocumentSnapshotConfig { .width = width, .height = height, .layers = std::span(layers, 2), .frames = std::span(root_frames, 2), .selection_masks = {}, }); } pp::foundation::Status parse_save_document_project_args( int argc, char** argv, SaveDocumentProjectArgs& 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 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()) { return pp::foundation::Status::invalid_argument("path must not be empty"); } if (args.width < 8 || args.height < 8) { return pp::foundation::Status::out_of_range("width and height must be at least 8 for document export"); } return pp::foundation::Status::success(); } int save_document_project(int argc, char** argv) { SaveDocumentProjectArgs args; const auto status = parse_save_document_project_args(argc, argv, args); if (!status.ok()) { print_error("save-document-project", status.message); return 2; } const auto document = create_export_sample_document(args.width, args.height); if (!document) { print_error("save-document-project", document.status().message); return 2; } const auto exported = pp::document::export_ppi_project_document(document.value()); if (!exported) { print_error("save-document-project", exported.status().message); return 2; } std::ofstream stream(args.path, std::ios::binary); if (!stream) { print_error("save-document-project", "project file could not be opened for writing"); return 2; } const auto& bytes = exported.value(); stream.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); if (!stream) { print_error("save-document-project", "project file could not be written"); return 2; } const auto decoded = pp::assets::decode_ppi_project_images(bytes); if (!decoded) { print_error("save-document-project", decoded.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"save-document-project\"" << ",\"path\":\"" << json_escape(args.path) << "\"" << ",\"format\":\"ppi\"" << ",\"bytes\":" << bytes.size() << ",\"document\":{\"width\":" << document.value().width() << ",\"height\":" << document.value().height() << ",\"layers\":" << document.value().layers().size() << ",\"frames\":" << document.value().frames().size() << ",\"facePayloads\":" << document.value().face_pixel_payload_count() << "},\"export\":{\"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_save_project_args(int argc, char** argv, SaveProjectArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--path" || key == "--layer-name") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } if (key == "--path") { args.path = argv[++i]; } else { args.layer_name = argv[++i]; } } else if (key == "--layer-opacity") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } const auto value = parse_float_arg(argv[++i]); if (!value) { return value.status(); } args.layer_opacity = value.value(); } else if (key == "--alpha-locked") { args.alpha_locked = true; } else if (key == "--hidden") { args.visible = false; } else if (key == "--width" || key == "--height" || key == "--layers" || key == "--frames" || key == "--blend-mode" || key == "--frame-duration-ms" || key == "--payload-layer" || key == "--payload-frame") { 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 if (key == "--height") { args.height = value.value(); } else if (key == "--layers") { args.layers = value.value(); } else if (key == "--frames") { args.frames = value.value(); } else if (key == "--blend-mode") { args.blend_mode = value.value(); } else if (key == "--payload-layer") { args.payload_layer = value.value(); } else if (key == "--payload-frame") { args.payload_frame = value.value(); } else { args.frame_duration_ms = value.value(); } } else if (key == "--include-test-face-payload") { args.include_test_face_payload = true; } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.path.empty()) { return pp::foundation::Status::invalid_argument("path must not be empty"); } if (args.width == 0 || args.height == 0) { return pp::foundation::Status::invalid_argument("width and height must be greater than zero"); } if (args.layer_name.empty()) { return pp::foundation::Status::invalid_argument("layer name must not be empty"); } if (args.layer_opacity < 0.0F || args.layer_opacity > 1.0F) { return pp::foundation::Status::out_of_range("layer opacity must be in the range [0, 1]"); } if (args.blend_mode > 4U) { return pp::foundation::Status::out_of_range("blend mode must be in the range [0, 4]"); } if (args.layers == 0) { return pp::foundation::Status::invalid_argument("layer count must be greater than zero"); } if (args.frames == 0) { return pp::foundation::Status::invalid_argument("frame count must be greater than zero"); } if (args.frame_duration_ms == 0) { return pp::foundation::Status::invalid_argument("frame duration must be greater than zero"); } if (args.include_test_face_payload && args.payload_layer >= args.layers) { return pp::foundation::Status::out_of_range("payload layer must be inside the generated layer list"); } if (args.include_test_face_payload && args.payload_frame >= args.frames) { return pp::foundation::Status::out_of_range("payload frame must be inside the generated frame list"); } if (!args.include_test_face_payload && (args.payload_layer != 0 || args.payload_frame != 0)) { return pp::foundation::Status::invalid_argument( "payload layer/frame options require --include-test-face-payload"); } return pp::foundation::Status::success(); } int save_project(int argc, char** argv) { SaveProjectArgs args; const auto status = parse_save_project_args(argc, argv, args); if (!status.ok()) { print_error("save-project", status.message); return 2; } const auto test_payload = transparent_png_1x1_bytes(); const pp::assets::PpiDirtyFacePayloadConfig dirty_faces[] { { .layer_index = args.payload_layer, .frame_index = args.payload_frame, .face_index = 0, .x = 0, .y = 0, .width = 1, .height = 1, .png_rgba8 = test_payload, }, }; const auto project = pp::assets::create_minimal_ppi_project(pp::assets::PpiMinimalProjectConfig { .width = args.width, .height = args.height, .layer_name = args.layer_name, .layer_metadata = pp::assets::PpiLayerMetadataConfig { .opacity = args.layer_opacity, .blend_mode = args.blend_mode, .alpha_locked = args.alpha_locked, .visible = args.visible, }, .layer_count = args.layers, .frame_count = args.frames, .frame_duration_ms = args.frame_duration_ms, .dirty_faces = args.include_test_face_payload ? std::span(dirty_faces, 1) : std::span(), }); if (!project) { print_error("save-project", project.status().message); return 2; } std::ofstream stream(args.path, std::ios::binary); if (!stream) { print_error("save-project", "project file could not be opened for writing"); return 2; } const auto& bytes = project.value(); stream.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); if (!stream) { print_error("save-project", "project file could not be written"); return 2; } std::cout << "{\"ok\":true,\"command\":\"save-project\"" << ",\"path\":\"" << json_escape(args.path) << "\"" << ",\"format\":\"ppi\"" << ",\"bytes\":" << bytes.size() << ",\"document\":{\"width\":" << args.width << ",\"height\":" << args.height << ",\"layers\":" << args.layers << ",\"frames\":" << args.frames << ",\"layerName\":\"" << json_escape(args.layer_name) << "\"" << ",\"layerOpacity\":" << args.layer_opacity << ",\"blendMode\":" << args.blend_mode << ",\"alphaLocked\":" << (args.alpha_locked ? "true" : "false") << ",\"visible\":" << (args.visible ? "true" : "false") << ",\"frameDurationMs\":" << args.frame_duration_ms << ",\"facePayloads\":" << (args.include_test_face_payload ? 1 : 0) << ",\"payloadLayer\":" << args.payload_layer << ",\"payloadFrame\":" << args.payload_frame << "}}\n"; 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 chars { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto* data = reinterpret_cast(chars.data()); const auto format = pp::assets::detect_image_format(std::span(data, chars.size())); if (!format) { print_error("inspect-image", format.status().message); return 2; } pp::foundation::Result metadata = pp::foundation::Result::failure( pp::foundation::Status::invalid_argument("image metadata is unavailable")); if (format.value() == pp::assets::ImageFormat::png) { metadata = pp::assets::parse_png_metadata(std::span(data, chars.size())); if (!metadata) { print_error("inspect-image", metadata.status().message); return 2; } } std::cout << "{\"ok\":true,\"command\":\"inspect-image\",\"format\":\"" << pp::assets::image_format_name(format.value()) << "\",\"bytes\":" << chars.size(); if (metadata) { std::cout << ",\"metadata\":{\"width\":" << metadata.value().width << ",\"height\":" << metadata.value().height << ",\"bitDepth\":" << static_cast(metadata.value().bit_depth) << ",\"components\":" << static_cast(metadata.value().components) << ",\"colorType\":\"" << pp::assets::image_color_type_name(metadata.value().color_type) << "\"}"; } else { std::cout << ",\"metadata\":null"; } std::cout << "}\n"; return 0; } pp::foundation::Status parse_import_image_args(int argc, char** argv, ImportImageArgs& 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 if (key == "--document-width" || key == "--document-height" || key == "--face" || key == "--x" || key == "--y") { 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 == "--document-width") { args.document_width = value.value(); } else if (key == "--document-height") { args.document_height = value.value(); } else if (key == "--face") { args.face = value.value(); } else if (key == "--x") { args.x = value.value(); } else { args.y = value.value(); } } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.path.empty()) { return pp::foundation::Status::invalid_argument("path must not be empty"); } if ((args.document_width == 0) != (args.document_height == 0)) { return pp::foundation::Status::invalid_argument( "document width and height must both be omitted or both be greater than zero"); } return pp::foundation::Status::success(); } int import_image(int argc, char** argv) { ImportImageArgs args; const auto status = parse_import_image_args(argc, argv, args); if (!status.ok()) { print_error("import-image", status.message); return 2; } std::ifstream stream(args.path, std::ios::binary); if (!stream) { print_error("import-image", "image file could not be opened"); return 2; } const std::vector chars { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto* data = reinterpret_cast(chars.data()); const auto image_result = pp::assets::decode_png_rgba8(std::span(data, chars.size())); if (!image_result) { print_error("import-image", image_result.status().message); return 2; } const auto& image = image_result.value(); const std::uint32_t document_width = args.document_width == 0 ? image.width : args.document_width; const std::uint32_t document_height = args.document_height == 0 ? image.height : args.document_height; const auto document_result = pp::document::CanvasDocument::create( pp::document::DocumentConfig { .width = document_width, .height = document_height, .layer_count = 1, }); if (!document_result) { print_error("import-image", document_result.status().message); return 2; } auto document = document_result.value(); const auto import_status = document.set_layer_frame_face_pixels( 0, 0, pp::document::LayerFacePixels { .face_index = args.face, .x = args.x, .y = args.y, .width = image.width, .height = image.height, .rgba8 = image.pixels, }); if (!import_status.ok()) { print_error("import-image", import_status.message); return 2; } std::cout << "{\"ok\":true,\"command\":\"import-image\"" << ",\"source\":\"" << json_escape(args.path) << "\"" << ",\"image\":{\"format\":\"png\",\"width\":" << image.width << ",\"height\":" << image.height << ",\"bytes\":" << image.pixels.size() << "},\"document\":{\"width\":" << document.width() << ",\"height\":" << document.height() << ",\"layers\":" << document.layers().size() << ",\"frames\":" << document.frames().size() << ",\"facePayloads\":" << document.face_pixel_payload_count() << "},\"payload\":{\"face\":" << args.face << ",\"x\":" << args.x << ",\"y\":" << args.y << ",\"width\":" << image.width << ",\"height\":" << image.height << ",\"bytes\":" << image.pixels.size() << "}}\n"; return 0; } pp::foundation::Status parse_export_image_args(int argc, char** argv, ExportImageArgs& 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 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()) { return pp::foundation::Status::invalid_argument("path must not be empty"); } if (args.width == 0 || args.height == 0) { return pp::foundation::Status::invalid_argument("width and height must be greater than zero"); } constexpr std::uint64_t max_cli_export_bytes = 64ULL * 1024ULL * 1024ULL; if (static_cast(args.width) > max_cli_export_bytes / 4ULL / static_cast(args.height)) { return pp::foundation::Status::out_of_range("export image exceeds the CLI automation size limit"); } return pp::foundation::Status::success(); } int export_image(int argc, char** argv) { ExportImageArgs args; const auto status = parse_export_image_args(argc, argv, args); if (!status.ok()) { print_error("export-image", status.message); return 2; } std::vector pixels( static_cast(args.width) * static_cast(args.height) * 4U); for (std::uint32_t y = 0; y < args.height; ++y) { for (std::uint32_t x = 0; x < args.width; ++x) { const auto offset = (static_cast(y) * args.width + x) * 4U; pixels[offset + 0U] = static_cast((x * 37U) & 0xffU); pixels[offset + 1U] = static_cast((y * 53U) & 0xffU); pixels[offset + 2U] = static_cast(((x + y) * 29U) & 0xffU); pixels[offset + 3U] = 255U; } } const auto encoded = pp::assets::encode_png_rgba8(args.width, args.height, pixels); if (!encoded) { print_error("export-image", encoded.status().message); return 2; } std::ofstream stream(args.path, std::ios::binary); if (!stream) { print_error("export-image", "image file could not be opened for writing"); return 2; } const auto& bytes = encoded.value(); stream.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); if (!stream) { print_error("export-image", "image file could not be written"); return 2; } std::cout << "{\"ok\":true,\"command\":\"export-image\"" << ",\"path\":\"" << json_escape(args.path) << "\"" << ",\"image\":{\"format\":\"png\",\"width\":" << args.width << ",\"height\":" << args.height << ",\"bytes\":" << pixels.size() << "},\"file\":{\"bytes\":" << bytes.size() << "}}\n"; return 0; } pp::foundation::Status parse_inspect_project_args(int argc, char** argv, InspectProjectArgs& 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_project(int argc, char** argv) { InspectProjectArgs args; const auto status = parse_inspect_project_args(argc, argv, args); if (!status.ok()) { print_error("inspect-project", status.message); return 2; } std::ifstream stream(args.path, std::ios::binary); if (!stream) { print_error("inspect-project", "project file could not be opened"); return 2; } const std::vector chars { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto* data = reinterpret_cast(chars.data()); const auto project = pp::assets::parse_ppi_project_index(std::span(data, chars.size())); if (!project) { print_error("inspect-project", project.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"inspect-project\"" << ",\"documentVersion\":\"" << project.value().layout.header.document_version.major << "." << project.value().layout.header.document_version.minor << "\"" << ",\"softwareVersion\":\"" << project.value().layout.header.software_version.major << "." << project.value().layout.header.software_version.minor << "." << project.value().layout.header.software_version.fix << "." << project.value().layout.header.software_version.build << "\"" << ",\"thumbnail\":{\"width\":" << project.value().layout.header.thumbnail.width << ",\"height\":" << project.value().layout.header.thumbnail.height << ",\"components\":" << project.value().layout.header.thumbnail.components << ",\"bytes\":" << project.value().layout.thumbnail_bytes << "},\"body\":{\"offset\":" << project.value().layout.body_offset << ",\"bytes\":" << project.value().layout.body_bytes << ",\"width\":" << project.value().body.summary.width << ",\"height\":" << project.value().body.summary.height << ",\"layers\":" << project.value().body.summary.layer_count << ",\"frames\":" << project.value().body.summary.declared_frame_count << ",\"dirtyFaces\":" << project.value().body.summary.dirty_face_count << ",\"rgbaFacePayloads\":" << project.value().body.summary.rgba_face_payload_count << ",\"compressedBytes\":" << project.value().body.summary.compressed_face_bytes << ",\"infoBytes\":" << project.value().body.summary.info_bytes << "},\"layers\":["; for (std::size_t layer_index = 0; layer_index < project.value().body.layers.size(); ++layer_index) { const auto& layer = project.value().body.layers[layer_index]; if (layer_index != 0U) { std::cout << ","; } std::cout << "{\"index\":" << layer_index << ",\"storedOrder\":" << layer.stored_order << ",\"name\":\"" << json_escape(layer.name) << "\"" << ",\"opacity\":" << layer.opacity << ",\"blendMode\":" << layer.blend_mode << ",\"alphaLocked\":" << (layer.alpha_locked ? "true" : "false") << ",\"visible\":" << (layer.visible ? "true" : "false") << ",\"frames\":["; for (std::size_t frame_index = 0; frame_index < layer.frames.size(); ++frame_index) { const auto& frame = layer.frames[frame_index]; if (frame_index != 0U) { std::cout << ","; } std::cout << "{\"index\":" << frame_index << ",\"durationMs\":" << frame.duration_ms << ",\"dirtyFaces\":["; bool first_face = true; for (std::size_t face_index = 0; face_index < frame.faces.size(); ++face_index) { const auto& face = frame.faces[face_index]; if (!face.has_data) { continue; } if (!first_face) { std::cout << ","; } first_face = false; std::cout << "{\"face\":" << face_index << ",\"box\":[" << face.x0 << "," << face.y0 << "," << face.x1 << "," << face.y1 << "]" << ",\"bodyPayloadOffset\":" << face.body_payload_offset << ",\"bytes\":" << face.payload_bytes << ",\"png\":{\"width\":" << face.png_width << ",\"height\":" << face.png_height << "}}"; } std::cout << "]}"; } std::cout << "]}"; } std::cout << "]}\n"; return 0; } int load_project(int argc, char** argv) { InspectProjectArgs args; const auto status = parse_inspect_project_args(argc, argv, args); if (!status.ok()) { print_error("load-project", status.message); return 2; } std::ifstream stream(args.path, std::ios::binary); if (!stream) { print_error("load-project", "project file could not be opened"); return 2; } const std::string chars { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto* data = reinterpret_cast(chars.data()); const auto project = pp::assets::decode_ppi_project_images(std::span(data, chars.size())); if (!project) { print_error("load-project", project.status().message); return 2; } const auto document_result = pp::document::import_ppi_project_document(project.value()); if (!document_result) { print_error("load-project", document_result.status().message); return 2; } const auto& document = document_result.value(); std::cout << "{\"ok\":true,\"command\":\"load-project\"" << ",\"source\":\"ppi\"" << ",\"pixelDataLoaded\":" << (document.face_pixel_payload_count() > 0U ? "true" : "false") << ",\"facePayloads\":" << document.face_pixel_payload_count() << ",\"document\":{\"width\":" << document.width() << ",\"height\":" << document.height() << ",\"layers\":" << document.layers().size() << ",\"frames\":" << document.frames().size() << ",\"animationDurationMs\":" << document.animation_duration_ms() << ",\"layerNames\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << "\"" << json_escape(document.layers()[layer_index].name) << "\""; } std::cout << "],\"layerFrameCounts\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << document.layers()[layer_index].frames.size(); } std::cout << "],\"layerDurationsMs\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } const auto duration = document.layer_animation_duration_ms(layer_index); std::cout << (duration ? duration.value() : 0U); } std::cout << "],\"layerOpacities\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << document.layers()[layer_index].opacity; } std::cout << "],\"layerBlendModes\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << "\"" << pp::paint::blend_mode_name(document.layers()[layer_index].blend_mode) << "\""; } std::cout << "],\"layerAlphaLocked\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << (document.layers()[layer_index].alpha_locked ? "true" : "false"); } std::cout << "],\"layerVisible\":["; for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << (document.layers()[layer_index].visible ? "true" : "false"); } std::cout << "]}}\n"; return 0; } pp::foundation::Status parse_simulate_stroke_args(int argc, char** argv, SimulateStrokeArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--x1" || key == "--y1" || key == "--x2" || key == "--y2" || key == "--spacing") { 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 == "--x1") { args.x1 = value.value(); } else if (key == "--y1") { args.y1 = value.value(); } else if (key == "--x2") { args.x2 = value.value(); } else if (key == "--y2") { args.y2 = value.value(); } else { args.spacing = value.value(); } } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.spacing == 0) { return pp::foundation::Status::invalid_argument("stroke spacing must be greater than zero"); } return pp::foundation::Status::success(); } int simulate_stroke(int argc, char** argv) { SimulateStrokeArgs args; const auto status = parse_simulate_stroke_args(argc, argv, args); if (!status.ok()) { print_error("simulate-stroke", status.message); return 2; } const pp::paint::StrokePoint points[] { pp::paint::StrokePoint { .x = static_cast(args.x1), .y = static_cast(args.y1), .pressure = 1.0F, }, pp::paint::StrokePoint { .x = static_cast(args.x2), .y = static_cast(args.y2), .pressure = 1.0F, }, }; const auto samples = pp::paint::sample_stroke( points, pp::paint::StrokeSamplingConfig { .spacing = static_cast(args.spacing), }); if (!samples) { print_error("simulate-stroke", samples.status().message); return 2; } const auto& first = samples.value().front(); const auto& last = samples.value().back(); std::cout << "{\"ok\":true,\"command\":\"simulate-stroke\"" << ",\"samples\":" << samples.value().size() << ",\"first\":{\"x\":" << first.x << ",\"y\":" << first.y << "}" << ",\"last\":{\"x\":" << last.x << ",\"y\":" << last.y << ",\"distance\":" << last.distance << "}}\n"; return 0; } pp::foundation::Status parse_simulate_stroke_script_args(int argc, char** argv, SimulateStrokeScriptArgs& 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 simulate_stroke_script(int argc, char** argv) { SimulateStrokeScriptArgs args; const auto status = parse_simulate_stroke_script_args(argc, argv, args); if (!status.ok()) { print_error("simulate-stroke-script", status.message); return 2; } std::ifstream stream(args.path, std::ios::binary); if (!stream) { print_error("simulate-stroke-script", "stroke script file could not be opened"); return 2; } const std::string text { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto script = pp::paint::parse_stroke_script(text); if (!script) { print_error("simulate-stroke-script", script.status().message); return 2; } std::size_t total_samples = 0; float total_distance = 0.0F; for (const auto& stroke : script.value().strokes) { 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("simulate-stroke-script", samples.status().message); return 2; } total_samples += samples.value().size(); total_distance += samples.value().back().distance; } std::cout << "{\"ok\":true,\"command\":\"simulate-stroke-script\"" << ",\"strokes\":" << script.value().strokes.size() << ",\"samples\":" << total_samples << ",\"distance\":" << total_distance << "}\n"; 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(std::lround(normalized * 255.0F)); return static_cast(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(std::lround(value)); const auto clamped = std::clamp(rounded, 0, static_cast(limit - 1U)); return static_cast(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(script_stream), std::istreambuf_iterator() }; 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 applied_points; std::vector 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(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 rgba8( static_cast(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(local_y) * payload_width + local_x) * pp::document::rgba8_components; rgba8[pixel_index + 0U] = static_cast(64U + ((point.stroke_index * 53U) % 192U)); rgba8[pixel_index + 1U] = static_cast((point_index * 31U) % 256U); rgba8[pixel_index + 2U] = static_cast(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(bytes.data()), static_cast(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, SimulateDocumentEditsArgs& 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_document_edits(int argc, char** argv) { SimulateDocumentEditsArgs args; const auto status = parse_simulate_document_edits_args(argc, argv, args); if (!status.ok()) { print_error("simulate-document-edits", 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-document-edits", document_result.status().message); return 2; } auto document = document_result.value(); auto edit_status = document.set_frame_duration(0, 100); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } const auto layer_result = document.add_layer("Paint"); if (!layer_result) { print_error("simulate-document-edits", layer_result.status().message); return 2; } const std::size_t paint_layer = layer_result.value(); edit_status = document.rename_layer(paint_layer, "Ink"); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_layer_visible(paint_layer, false); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_layer_alpha_locked(paint_layer, true); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_layer_opacity(paint_layer, 0.625F); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_layer_blend_mode(paint_layer, pp::paint::BlendMode::overlay); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_active_layer(paint_layer); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } const auto duplicated_frame = document.duplicate_frame(0); if (!duplicated_frame) { print_error("simulate-document-edits", duplicated_frame.status().message); return 2; } edit_status = document.set_frame_duration(duplicated_frame.value(), 333); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } const auto added_frame = document.add_frame(250); if (!added_frame) { print_error("simulate-document-edits", added_frame.status().message); return 2; } edit_status = document.move_frame(added_frame.value(), 0); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.move_layer(paint_layer, 0); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_layer_frame_face_pixels( 0, 0, pp::document::LayerFacePixels { .face_index = 2, .x = 1, .y = 2, .width = 1, .height = 1, .rgba8 = { 255, 0, 0, 255 }, }); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } edit_status = document.set_selection_mask(pp::document::SelectionMask { .face_index = 4, .x = 3, .y = 5, .width = 2, .height = 2, .alpha8 = { 0, 64, 128, 255 }, }); if (!edit_status.ok()) { print_error("simulate-document-edits", edit_status.message); return 2; } const auto& active_layer = document.layers()[document.active_layer_index()]; const auto& selection_mask = document.selection_masks()[0]; std::cout << "{\"ok\":true,\"command\":\"simulate-document-edits\"" << ",\"document\":{\"width\":" << document.width() << ",\"height\":" << document.height() << ",\"layers\":" << document.layers().size() << ",\"frames\":" << document.frames().size() << ",\"activeLayer\":" << document.active_layer_index() << ",\"activeFrame\":" << document.active_frame_index() << ",\"animationDurationMs\":" << document.animation_duration_ms() << ",\"facePayloads\":" << document.face_pixel_payload_count() << ",\"selectionMasks\":" << document.selection_mask_payload_count() << "},\"activeLayer\":{\"name\":\"" << json_escape(active_layer.name) << "\",\"visible\":" << (active_layer.visible ? "true" : "false") << ",\"alphaLocked\":" << (active_layer.alpha_locked ? "true" : "false") << ",\"opacity\":" << active_layer.opacity << ",\"blendMode\":\"" << pp::paint::blend_mode_name(active_layer.blend_mode) << "\",\"frames\":" << active_layer.frames.size() << "},\"frames\":["; for (std::size_t frame_index = 0; frame_index < document.frames().size(); ++frame_index) { if (frame_index != 0U) { std::cout << ","; } std::cout << document.frames()[frame_index].duration_ms; } std::cout << "],\"activeLayerFrameDurations\":["; for (std::size_t frame_index = 0; frame_index < active_layer.frames.size(); ++frame_index) { if (frame_index != 0U) { std::cout << ","; } std::cout << active_layer.frames[frame_index].duration_ms; } std::cout << "],\"selectionMask\":{\"face\":" << selection_mask.face_index << ",\"x\":" << selection_mask.x << ",\"y\":" << selection_mask.y << ",\"width\":" << selection_mask.width << ",\"height\":" << selection_mask.height << ",\"bytes\":" << selection_mask.alpha8.size() << ",\"maxAlpha\":" << static_cast(selection_mask.alpha8.back()) << "}}\n"; 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) { 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_bytes()); 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(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_export_args( int argc, char** argv, SimulateDocumentExportArgs& 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"); } if (args.width < 8 || args.height < 8) { return pp::foundation::Status::out_of_range("width and height must be at least 8 for export simulation"); } return pp::foundation::Status::success(); } int simulate_document_export(int argc, char** argv) { SimulateDocumentExportArgs args; const auto status = parse_simulate_document_export_args(argc, argv, args); if (!status.ok()) { print_error("simulate-document-export", status.message); return 2; } const auto document_result = create_export_sample_document(args.width, args.height); if (!document_result) { print_error("simulate-document-export", document_result.status().message); return 2; } const auto exported = pp::document::export_ppi_project_document(document_result.value()); if (!exported) { print_error("simulate-document-export", exported.status().message); return 2; } const auto decoded = pp::assets::decode_ppi_project_images(exported.value()); if (!decoded) { print_error("simulate-document-export", decoded.status().message); return 2; } const auto imported = pp::document::import_ppi_project_document(decoded.value()); if (!imported) { print_error("simulate-document-export", imported.status().message); return 2; } const auto& source = document_result.value(); const auto& roundtrip = imported.value(); std::cout << "{\"ok\":true,\"command\":\"simulate-document-export\"" << ",\"source\":{\"width\":" << source.width() << ",\"height\":" << source.height() << ",\"layers\":" << source.layers().size() << ",\"frames\":" << source.frames().size() << ",\"facePayloads\":" << source.face_pixel_payload_count() << "},\"export\":{\"bytes\":" << exported.value().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 << "},\"roundtrip\":{\"layers\":" << roundtrip.layers().size() << ",\"frames\":" << roundtrip.frames().size() << ",\"facePayloads\":" << roundtrip.face_pixel_payload_count() << ",\"layerNames\":["; for (std::size_t layer_index = 0; layer_index < roundtrip.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << "\"" << json_escape(roundtrip.layers()[layer_index].name) << "\""; } std::cout << "],\"layerFrameCounts\":["; for (std::size_t layer_index = 0; layer_index < roundtrip.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } std::cout << roundtrip.layers()[layer_index].frames.size(); } std::cout << "],\"layerDurationsMs\":["; for (std::size_t layer_index = 0; layer_index < roundtrip.layers().size(); ++layer_index) { if (layer_index != 0U) { std::cout << ","; } const auto duration = roundtrip.layer_animation_duration_ms(layer_index); std::cout << (duration ? duration.value() : 0U); } std::cout << "]},\"payloads\":["; for (std::size_t payload_index = 0; payload_index < decoded.value().faces.size(); ++payload_index) { const auto& payload = decoded.value().faces[payload_index]; if (payload_index != 0U) { std::cout << ","; } std::cout << "{\"layer\":" << payload.layer_index << ",\"frame\":" << payload.frame_index << ",\"face\":" << payload.face_index << ",\"x\":" << payload.descriptor.x0 << ",\"y\":" << payload.descriptor.y0 << ",\"bytes\":" << payload.image.pixels.size() << ",\"alpha\":" << static_cast(payload.image.pixels[3]) << "}"; } std::cout << "]}\n"; return 0; } pp::foundation::Status parse_simulate_document_history_args( int argc, char** argv, SimulateDocumentHistoryArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--width" || key == "--height" || key == "--history") { 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 if (key == "--height") { args.height = value.value(); } else { args.history_entries = 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_document_history(int argc, char** argv) { SimulateDocumentHistoryArgs args; const auto status = parse_simulate_document_history_args(argc, argv, args); if (!status.ok()) { print_error("simulate-document-history", 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-document-history", document_result.status().message); return 2; } auto history_result = pp::document::DocumentHistory::create( document_result.value(), static_cast(args.history_entries)); if (!history_result) { print_error("simulate-document-history", history_result.status().message); return 2; } auto history = history_result.value(); auto with_layer = history.current(); const auto layer_result = with_layer.add_layer("Paint"); if (!layer_result) { print_error("simulate-document-history", layer_result.status().message); return 2; } auto apply_status = history.apply(with_layer); if (!apply_status.ok()) { print_error("simulate-document-history", apply_status.message); return 2; } auto with_frame = history.current(); const auto frame_result = with_frame.add_frame(250); if (!frame_result) { print_error("simulate-document-history", frame_result.status().message); return 2; } apply_status = history.apply(with_frame); if (!apply_status.ok()) { print_error("simulate-document-history", apply_status.message); return 2; } const auto undo_status = history.undo(); if (!undo_status.ok()) { print_error("simulate-document-history", undo_status.message); return 2; } const auto undo_layers = history.current().layers().size(); const auto undo_frames = history.current().frames().size(); const auto undo_can_redo = history.can_redo(); const auto undo_index = history.current_index(); const auto redo_status = history.redo(); if (!redo_status.ok()) { print_error("simulate-document-history", redo_status.message); return 2; } const auto& current = history.current(); std::cout << "{\"ok\":true,\"command\":\"simulate-document-history\"" << ",\"history\":{\"size\":" << history.size() << ",\"currentIndex\":" << history.current_index() << ",\"canUndo\":" << (history.can_undo() ? "true" : "false") << ",\"canRedo\":" << (history.can_redo() ? "true" : "false") << "},\"undo\":{\"layers\":" << undo_layers << ",\"frames\":" << undo_frames << ",\"currentIndex\":" << undo_index << ",\"canRedo\":" << (undo_can_redo ? "true" : "false") << "},\"current\":{\"width\":" << current.width() << ",\"height\":" << current.height() << ",\"layers\":" << current.layers().size() << ",\"frames\":" << current.frames().size() << ",\"activeLayer\":" << current.active_layer_index() << ",\"activeFrame\":" << current.active_frame_index() << ",\"animationDurationMs\":" << current.animation_duration_ms() << "}}\n"; return 0; } pp::foundation::Status parse_record_render_args(int argc, char** argv, RecordRenderArgs& 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 record_render(int argc, char** argv) { RecordRenderArgs args; const auto status = parse_record_render_args(argc, argv, args); if (!status.ok()) { print_error("record-render", status.message); return 2; } pp::renderer::RecordingRenderDevice device; pp::renderer::RecordingRenderTarget target(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = args.width, .height = args.height }, .format = pp::renderer::TextureFormat::rgba8, .render_target = true, }); pp::renderer::RecordingShaderProgram shader("pano-cli-record-render"); pp::renderer::RecordingMesh mesh(pp::renderer::MeshDesc { .vertex_count = 3, .index_count = 0, .topology = pp::renderer::PrimitiveTopology::triangles, }); device.trace()->marker("renderer", "pano_cli_record_render"); auto& context = device.immediate_context(); const auto begin_status = context.begin_render_pass( target, pp::renderer::ClearColor { .r = 0.0F, .g = 0.0F, .b = 0.0F, .a = 1.0F }); if (!begin_status.ok()) { print_error("record-render", begin_status.message); return 2; } const auto viewport_status = context.set_viewport( pp::renderer::Viewport { .x = 0, .y = 0, .width = args.width, .height = args.height }); if (!viewport_status.ok()) { print_error("record-render", viewport_status.message); return 2; } const auto shader_status = context.bind_shader(shader); const auto mesh_status = context.bind_mesh(mesh); const auto draw_status = context.draw(); context.end_render_pass(); if (!shader_status.ok()) { print_error("record-render", shader_status.message); return 2; } if (!mesh_status.ok()) { print_error("record-render", mesh_status.message); return 2; } if (!draw_status.ok()) { print_error("record-render", draw_status.message); return 2; } std::size_t draw_commands = 0; std::size_t trace_markers = 0; const auto commands = device.commands(); for (const auto& command : commands) { if (command.kind == pp::renderer::RecordedRenderCommandKind::draw) { ++draw_commands; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::trace_marker) { ++trace_markers; } } std::cout << "{\"ok\":true,\"command\":\"record-render\"" << ",\"backend\":\"" << device.backend_name() << "\"" << ",\"target\":{\"width\":" << args.width << ",\"height\":" << args.height << ",\"format\":\"rgba8\"}" << ",\"commands\":" << commands.size() << ",\"drawCommands\":" << draw_commands << ",\"traceMarkers\":" << trace_markers << ",\"first\":\"" << pp::renderer::recorded_render_command_kind_name(commands.front().kind) << "\",\"last\":\"" << pp::renderer::recorded_render_command_kind_name(commands.back().kind) << "\"}\n"; return 0; } pp::foundation::Status parse_layout_args(int argc, char** argv, ParseLayoutArgs& 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 parse_layout(int argc, char** argv) { ParseLayoutArgs args; const auto status = parse_layout_args(argc, argv, args); if (!status.ok()) { print_error("parse-layout", status.message); return 2; } std::ifstream stream(args.path, std::ios::binary); if (!stream) { print_error("parse-layout", "layout file could not be opened"); return 2; } const std::string xml { std::istreambuf_iterator(stream), std::istreambuf_iterator() }; const auto summary = pp::ui::parse_layout_xml(xml); if (!summary) { print_error("parse-layout", summary.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"parse-layout\"" << ",\"nodes\":" << summary.value().node_count << ",\"lengthAttributes\":" << summary.value().length_attribute_count << "}\n"; return 0; } } int main(int argc, char** argv) { if (argc < 2) { print_help(); return 1; } const std::string_view command(argv[1]); if (command == "--help" || command == "-h") { print_help(); return 0; } if (command == "create-document") { return create_document(argc, argv); } if (command == "inspect-image") { return inspect_image(argc, argv); } if (command == "export-image") { return export_image(argc, argv); } if (command == "import-image") { return import_image(argc, argv); } if (command == "inspect-project") { return inspect_project(argc, argv); } if (command == "load-project") { return load_project(argc, argv); } if (command == "simulate-stroke") { return simulate_stroke(argc, argv); } if (command == "simulate-stroke-script") { 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); } if (command == "simulate-document-export") { return simulate_document_export(argc, argv); } if (command == "simulate-document-history") { 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); } if (command == "record-render") { return record_render(argc, argv); } if (command == "save-document-project") { return save_document_project(argc, argv); } if (command == "save-project") { return save_project(argc, argv); } print_error(command, "unknown command"); return 2; }