#include "app_core/document_export.h" #include "app_core/document_route.h" #include "app_core/document_session.h" #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/blend.h" #include "paint/stroke.h" #include "paint/stroke_script.h" #include "renderer_api/recording_renderer.h" #ifdef PP_PANO_CLI_ENABLE_OPENGL_PLAN #include "renderer_gl/command_plan.h" #endif #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 ClassifyOpenArgs { std::string path; }; struct PlanDocumentFileArgs { std::string work_directory; std::string name; bool target_exists = false; }; struct PlanExportTargetArgs { std::string kind; std::string work_directory; std::string directory; std::string document_name; std::string extension; std::string suffix; }; struct SimulateAppSessionArgs { bool has_canvas = true; bool new_document = false; bool unsaved = false; bool close_prompt_open = false; pp::app::DocumentSaveIntent save_intent = pp::app::DocumentSaveIntent::save; }; 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; bool exercise_clear = false; }; 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; } const char* json_bool(bool value) noexcept { return value ? "true" : "false"; } const char* document_open_kind_name(pp::app::DocumentOpenKind kind) noexcept { switch (kind) { case pp::app::DocumentOpenKind::import_abr: return "import-abr"; case pp::app::DocumentOpenKind::import_ppbr: return "import-ppbr"; case pp::app::DocumentOpenKind::open_project: return "open-project"; } return "open-project"; } const char* project_open_decision_name(pp::app::ProjectOpenDecision decision) noexcept { switch (decision) { case pp::app::ProjectOpenDecision::open_now: return "open-now"; case pp::app::ProjectOpenDecision::prompt_discard_unsaved: return "prompt-discard-unsaved"; } return "open-now"; } const char* close_request_decision_name(pp::app::CloseRequestDecision decision) noexcept { switch (decision) { case pp::app::CloseRequestDecision::close_now: return "close-now"; case pp::app::CloseRequestDecision::show_unsaved_prompt: return "show-unsaved-prompt"; case pp::app::CloseRequestDecision::wait_for_existing_prompt: return "wait-for-existing-prompt"; } return "close-now"; } const char* document_save_intent_name(pp::app::DocumentSaveIntent intent) noexcept { switch (intent) { case pp::app::DocumentSaveIntent::save: return "save"; case pp::app::DocumentSaveIntent::save_as: return "save-as"; case pp::app::DocumentSaveIntent::save_version: return "save-version"; case pp::app::DocumentSaveIntent::save_dirty_version: return "save-dirty-version"; } return "save"; } const char* document_save_decision_name(pp::app::DocumentSaveDecision decision) noexcept { switch (decision) { case pp::app::DocumentSaveDecision::no_op: return "no-op"; case pp::app::DocumentSaveDecision::show_save_dialog: return "show-save-dialog"; case pp::app::DocumentSaveDecision::save_existing: return "save-existing"; case pp::app::DocumentSaveDecision::save_version: return "save-version"; } return "no-op"; } const char* document_workflow_decision_name(pp::app::DocumentWorkflowDecision decision) noexcept { switch (decision) { case pp::app::DocumentWorkflowDecision::unavailable: return "unavailable"; case pp::app::DocumentWorkflowDecision::continue_now: return "continue-now"; case pp::app::DocumentWorkflowDecision::prompt_save_before_continue: return "prompt-save-before-continue"; } return "unavailable"; } const char* document_file_write_decision_name(pp::app::DocumentFileWriteDecision decision) noexcept { switch (decision) { case pp::app::DocumentFileWriteDecision::save_now: return "save-now"; case pp::app::DocumentFileWriteDecision::prompt_overwrite: return "prompt-overwrite"; } return "save-now"; } 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")); } if (!std::isfinite(value)) { return pp::foundation::Result::failure( pp::foundation::Status::invalid_argument("floating-point value must be finite")); } 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" << " classify-open --path FILE\n" << " plan-document-file --work-dir DIR --name NAME [--target-exists]\n" << " plan-export-target --kind file|collection|stem|name --doc-name NAME [--work-dir DIR] [--directory DIR] [--extension EXT] [--suffix SUFFIX]\n" << " load-project --path FILE\n" << " parse-layout --path FILE\n" << " record-render [--width N] [--height N] [--exercise-clear]\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-app-session [--no-canvas] [--new-document] [--unsaved] [--close-prompt-open] [--save-intent save|save-as|save-version|save-dirty-version]\n" << " simulate-blend\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(); } pp::foundation::Status parse_classify_open_args(int argc, char** argv, ClassifyOpenArgs& 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 classify_open(int argc, char** argv) { ClassifyOpenArgs args; const auto status = parse_classify_open_args(argc, argv, args); if (!status.ok()) { print_error("classify-open", status.message); return 2; } const auto route = pp::app::classify_document_open_path(args.path); if (!route) { print_error("classify-open", route.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"classify-open\"" << ",\"route\":{\"kind\":\"" << document_open_kind_name(route.value().kind) << "\",\"path\":\"" << json_escape(route.value().path) << "\",\"directory\":\"" << json_escape(route.value().directory) << "\",\"name\":\"" << json_escape(route.value().name) << "\",\"extension\":\"" << json_escape(route.value().extension) << "\"}}\n"; return 0; } pp::foundation::Status parse_plan_document_file_args( int argc, char** argv, PlanDocumentFileArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--work-dir") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } args.work_directory = argv[++i]; } else if (key == "--name") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } args.name = argv[++i]; } else if (key == "--target-exists") { args.target_exists = true; } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.work_directory.empty()) { return pp::foundation::Status::invalid_argument("work directory must not be empty"); } return pp::foundation::Status::success(); } int plan_document_file(int argc, char** argv) { PlanDocumentFileArgs args; const auto status = parse_plan_document_file_args(argc, argv, args); if (!status.ok()) { print_error("plan-document-file", status.message); return 2; } const auto target = pp::app::make_document_file_target(args.work_directory, args.name); if (!target) { print_error("plan-document-file", target.status().message); return 2; } const auto write_decision = pp::app::plan_document_file_write(args.target_exists); std::cout << "{\"ok\":true,\"command\":\"plan-document-file\"" << ",\"target\":{\"name\":\"" << json_escape(target.value().name) << "\",\"directory\":\"" << json_escape(target.value().directory) << "\",\"path\":\"" << json_escape(target.value().path) << "\",\"exists\":" << json_bool(args.target_exists) << "},\"decision\":\"" << document_file_write_decision_name(write_decision) << "\"}\n"; return 0; } pp::foundation::Status parse_plan_export_target_args( int argc, char** argv, PlanExportTargetArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); auto read_value = [&](std::string& output) -> pp::foundation::Status { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } output = argv[++i]; return pp::foundation::Status::success(); }; if (key == "--kind") { const auto status = read_value(args.kind); if (!status.ok()) { return status; } } else if (key == "--work-dir") { const auto status = read_value(args.work_directory); if (!status.ok()) { return status; } } else if (key == "--directory") { const auto status = read_value(args.directory); if (!status.ok()) { return status; } } else if (key == "--doc-name") { const auto status = read_value(args.document_name); if (!status.ok()) { return status; } } else if (key == "--extension") { const auto status = read_value(args.extension); if (!status.ok()) { return status; } } else if (key == "--suffix") { const auto status = read_value(args.suffix); if (!status.ok()) { return status; } } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.kind.empty()) { return pp::foundation::Status::invalid_argument("kind must not be empty"); } if (args.document_name.empty()) { return pp::foundation::Status::invalid_argument("document name must not be empty"); } if ((args.kind == "file" || args.kind == "collection") && args.work_directory.empty()) { return pp::foundation::Status::invalid_argument("work directory must not be empty"); } if (args.kind == "stem" && args.directory.empty()) { return pp::foundation::Status::invalid_argument("directory must not be empty"); } if (args.kind == "file" && args.extension.empty()) { return pp::foundation::Status::invalid_argument("extension must not be empty"); } if ((args.kind == "collection" || args.kind == "name") && args.suffix.empty()) { return pp::foundation::Status::invalid_argument("suffix must not be empty"); } if (args.kind != "file" && args.kind != "collection" && args.kind != "stem" && args.kind != "name") { return pp::foundation::Status::invalid_argument("unknown export target kind"); } return pp::foundation::Status::success(); } int plan_export_target(int argc, char** argv) { PlanExportTargetArgs args; const auto status = parse_plan_export_target_args(argc, argv, args); if (!status.ok()) { print_error("plan-export-target", status.message); return 2; } if (args.kind == "file") { const auto target = pp::app::make_document_export_file_target( args.work_directory, args.document_name, args.extension); if (!target) { print_error("plan-export-target", target.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"plan-export-target\"" << ",\"kind\":\"file\",\"target\":{\"path\":\"" << json_escape(target.value().path) << "\",\"suggestedName\":\"" << json_escape(target.value().suggested_name) << "\"}}\n"; return 0; } if (args.kind == "collection") { const auto target = pp::app::make_document_export_collection_target( args.work_directory, args.document_name, args.suffix); if (!target) { print_error("plan-export-target", target.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"plan-export-target\"" << ",\"kind\":\"collection\",\"target\":{\"directory\":\"" << json_escape(target.value().directory) << "\",\"stemPath\":\"" << json_escape(target.value().stem_path) << "\"}}\n"; return 0; } if (args.kind == "stem") { const auto target = pp::app::make_document_export_stem_target(args.directory, args.document_name); if (!target) { print_error("plan-export-target", target.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"plan-export-target\"" << ",\"kind\":\"stem\",\"target\":{\"stemPath\":\"" << json_escape(target.value().stem_path) << "\"}}\n"; return 0; } const auto target = pp::app::make_document_export_suggested_name(args.document_name, args.suffix); if (!target) { print_error("plan-export-target", target.status().message); return 2; } std::cout << "{\"ok\":true,\"command\":\"plan-export-target\"" << ",\"kind\":\"name\",\"target\":{\"suggestedName\":\"" << json_escape(target.value().name) << "\"}}\n"; return 0; } pp::foundation::Status parse_simulate_app_session_args( int argc, char** argv, SimulateAppSessionArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--new-document") { args.new_document = true; } else if (key == "--no-canvas") { args.has_canvas = false; } else if (key == "--unsaved") { args.unsaved = true; } else if (key == "--close-prompt-open") { args.close_prompt_open = true; } else if (key == "--save-intent") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } const std::string_view value(argv[++i]); if (value == "save") { args.save_intent = pp::app::DocumentSaveIntent::save; } else if (value == "save-as") { args.save_intent = pp::app::DocumentSaveIntent::save_as; } else if (value == "save-version") { args.save_intent = pp::app::DocumentSaveIntent::save_version; } else if (value == "save-dirty-version") { args.save_intent = pp::app::DocumentSaveIntent::save_dirty_version; } else { return pp::foundation::Status::invalid_argument("unknown save intent"); } } else { return pp::foundation::Status::invalid_argument("unknown option"); } } return pp::foundation::Status::success(); } int simulate_app_session(int argc, char** argv) { SimulateAppSessionArgs args; const auto status = parse_simulate_app_session_args(argc, argv, args); if (!status.ok()) { print_error("simulate-app-session", status.message); return 2; } const auto open_decision = pp::app::plan_project_open(args.unsaved); const auto close_decision = pp::app::plan_close_request(args.unsaved, args.close_prompt_open); const auto save_decision = pp::app::plan_document_save( args.new_document, args.unsaved, args.save_intent); const auto workflow_decision = pp::app::plan_document_workflow(args.has_canvas, args.unsaved); std::cout << "{\"ok\":true,\"command\":\"simulate-app-session\"" << ",\"state\":{\"hasCanvas\":" << json_bool(args.has_canvas) << ",\"newDocument\":" << json_bool(args.new_document) << ",\"unsaved\":" << json_bool(args.unsaved) << ",\"closePromptOpen\":" << json_bool(args.close_prompt_open) << "},\"decisions\":{\"projectOpen\":\"" << project_open_decision_name(open_decision) << "\",\"closeRequest\":\"" << close_request_decision_name(close_decision) << "\",\"saveIntent\":\"" << document_save_intent_name(args.save_intent) << "\",\"saveRequest\":\"" << document_save_decision_name(save_decision) << "\",\"workflowStart\":\"" << document_workflow_decision_name(workflow_decision) << "\"}}\n"; return 0; } 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; } int simulate_blend(int argc, char**) { if (argc > 2) { print_error("simulate-blend", "unknown option"); return 2; } const auto normal = pp::paint::blend_pixels( pp::paint::Rgba { .r = 0.2F, .g = 0.4F, .b = 0.6F, .a = 0.5F }, pp::paint::Rgba { .r = 0.8F, .g = 0.2F, .b = 0.1F, .a = 0.25F }, pp::paint::BlendMode::normal); const auto multiply = pp::paint::blend_pixels( pp::paint::Rgba { .r = 0.25F, .g = 0.5F, .b = 0.75F, .a = 1.0F }, pp::paint::Rgba { .r = 0.5F, .g = 0.5F, .b = 0.5F, .a = 1.0F }, pp::paint::BlendMode::multiply); const auto stroke_normal = pp::paint::blend_stroke_alpha( 0.2F, 0.8F, 0.25F, pp::paint::StrokeBlendMode::normal); const auto stroke_linear_height = pp::paint::blend_stroke_alpha( 0.5F, 0.25F, 0.16F, pp::paint::StrokeBlendMode::linear_height); const auto stroke_height = pp::paint::blend_stroke_alpha( 0.5F, 0.25F, 0.16F, pp::paint::StrokeBlendMode::height); const auto stroke_unknown = pp::paint::blend_stroke_alpha( 0.2F, 0.2F, 0.5F, static_cast(255)); std::cout << "{\"ok\":true,\"command\":\"simulate-blend\"" << ",\"finalBlendModes\":5" << ",\"strokeBlendModes\":11" << ",\"normal\":{\"r\":" << normal.r << ",\"g\":" << normal.g << ",\"b\":" << normal.b << ",\"a\":" << normal.a << "}" << ",\"multiply\":{\"r\":" << multiply.r << ",\"g\":" << multiply.g << ",\"b\":" << multiply.b << ",\"a\":" << multiply.a << "}" << ",\"stroke\":{\"normal\":" << stroke_normal << ",\"linearHeight\":" << stroke_linear_height << ",\"height\":" << stroke_height << ",\"unknown\":" << stroke_unknown << "}}\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"); } const auto render_target_size = pp::renderer::texture_byte_size(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = args.width, .height = args.height }, .format = pp::renderer::TextureFormat::rgba8, .usage = pp::renderer::TextureUsage::render_target | pp::renderer::TextureUsage::sampled | pp::renderer::TextureUsage::upload_destination | pp::renderer::TextureUsage::readback_source | pp::renderer::TextureUsage::copy_source | pp::renderer::TextureUsage::copy_destination, .debug_name = "record-render-texture", }); if (!render_target_size) { return render_target_size.status(); } 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 == "--exercise-clear") { args.exercise_clear = true; } 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.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; const auto texture = device.create_texture(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = args.width, .height = args.height }, .format = pp::renderer::TextureFormat::rgba8, .usage = pp::renderer::TextureUsage::render_target | pp::renderer::TextureUsage::sampled | pp::renderer::TextureUsage::upload_destination | pp::renderer::TextureUsage::readback_source | pp::renderer::TextureUsage::copy_source | pp::renderer::TextureUsage::copy_destination, .debug_name = "record-render-texture", }); const auto target = device.create_render_target(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = args.width, .height = args.height }, .format = pp::renderer::TextureFormat::rgba8, .usage = pp::renderer::TextureUsage::render_target | pp::renderer::TextureUsage::sampled | pp::renderer::TextureUsage::upload_destination | pp::renderer::TextureUsage::readback_source | pp::renderer::TextureUsage::copy_source | pp::renderer::TextureUsage::copy_destination, .debug_name = "record-render-target", }); const auto blit_target = device.create_render_target(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = args.width, .height = args.height }, .format = pp::renderer::TextureFormat::rgba8, .usage = pp::renderer::TextureUsage::render_target | pp::renderer::TextureUsage::sampled | pp::renderer::TextureUsage::upload_destination | pp::renderer::TextureUsage::readback_source | pp::renderer::TextureUsage::copy_source | pp::renderer::TextureUsage::copy_destination, .debug_name = "record-render-blit-target", }); const auto mip_texture = device.create_texture(pp::renderer::TextureDesc { .extent = pp::renderer::Extent2D { .width = 4, .height = 4 }, .format = pp::renderer::TextureFormat::rgba8, .mip_levels = 3, .usage = pp::renderer::TextureUsage::sampled | pp::renderer::TextureUsage::copy_source | pp::renderer::TextureUsage::copy_destination, .debug_name = "record-render-mip-texture", }); const auto readback_buffer = device.create_readback_buffer( static_cast(args.width) * args.height * 4U); const std::array upload_pixel { std::byte { 0xff }, std::byte { 0x00 }, std::byte { 0xff }, std::byte { 0xff }, }; const std::array uniform_mvp {}; static constexpr char shader_source[] = "void main() {}"; const auto shader = device.create_shader_program(pp::renderer::ShaderProgramDesc { .debug_name = "pano-cli-record-render", .vertex = pp::renderer::ShaderStageSource { .source = shader_source, .source_size = sizeof(shader_source) - 1U, }, .fragment = pp::renderer::ShaderStageSource { .source = shader_source, .source_size = sizeof(shader_source) - 1U, }, }); const auto mesh = device.create_mesh(pp::renderer::MeshDesc { .vertex_count = 3, .index_count = 3, .topology = pp::renderer::PrimitiveTopology::triangles, .debug_name = "record-render-mesh", }); if (!texture.ok()) { print_error("record-render", texture.status().message); return 2; } if (!target.ok()) { print_error("record-render", target.status().message); return 2; } if (!blit_target.ok()) { print_error("record-render", blit_target.status().message); return 2; } if (!mip_texture.ok()) { print_error("record-render", mip_texture.status().message); return 2; } if (!readback_buffer.ok()) { print_error("record-render", readback_buffer.status().message); return 2; } if (!shader.ok()) { print_error("record-render", shader.status().message); return 2; } if (!mesh.ok()) { print_error("record-render", mesh.status().message); return 2; } constexpr std::size_t created_resources = 7; auto* trace = device.trace(); auto& context = device.immediate_context(); bool clear_rejected_orphaned_trace_end = false; bool clear_reused_render_pass = false; if (args.exercise_clear) { const auto interrupted_trace_status = trace->begin_scope("renderer", "interrupted-frame"); if (!interrupted_trace_status.ok()) { print_error("record-render", interrupted_trace_status.message); return 2; } const auto interrupted_begin_status = context.begin_render_pass(*target.value(), pp::renderer::RenderPassDesc {}); if (!interrupted_begin_status.ok()) { print_error("record-render", interrupted_begin_status.message); return 2; } device.clear(); const auto stale_trace_end_status = trace->end_scope(); if (stale_trace_end_status.ok()) { print_error("record-render", "recording clear did not reset trace scope state"); return 2; } clear_rejected_orphaned_trace_end = true; const auto reuse_begin_status = context.begin_render_pass(*target.value(), pp::renderer::RenderPassDesc {}); if (!reuse_begin_status.ok()) { print_error("record-render", reuse_begin_status.message); return 2; } context.end_render_pass(); clear_reused_render_pass = true; device.clear(); } const auto trace_begin_status = trace->begin_scope("renderer", "pano_cli_record_render"); if (!trace_begin_status.ok()) { print_error("record-render", trace_begin_status.message); return 2; } const auto trace_marker_status = trace->marker("renderer", "frame"); if (!trace_marker_status.ok()) { print_error("record-render", trace_marker_status.message); return 2; } const auto trace_end_status = trace->end_scope(); if (!trace_end_status.ok()) { print_error("record-render", trace_end_status.message); return 2; } const auto transition_upload_status = context.transition_texture( *texture.value(), pp::renderer::TextureState::undefined, pp::renderer::TextureState::upload_destination); if (!transition_upload_status.ok()) { print_error("record-render", transition_upload_status.message); return 2; } const auto upload_status = context.upload_texture( *texture.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = 1, .height = 1, }, upload_pixel); if (!upload_status.ok()) { print_error("record-render", upload_status.message); return 2; } const auto transition_shader_read_status = context.transition_texture( *texture.value(), pp::renderer::TextureState::upload_destination, pp::renderer::TextureState::shader_read); if (!transition_shader_read_status.ok()) { print_error("record-render", transition_shader_read_status.message); return 2; } const auto transition_mip_destination_status = context.transition_texture( *mip_texture.value(), pp::renderer::TextureState::undefined, pp::renderer::TextureState::copy_destination); if (!transition_mip_destination_status.ok()) { print_error("record-render", transition_mip_destination_status.message); return 2; } const auto mipmap_status = context.generate_mipmaps(*mip_texture.value()); if (!mipmap_status.ok()) { print_error("record-render", mipmap_status.message); return 2; } const auto transition_mip_shader_read_status = context.transition_texture( *mip_texture.value(), pp::renderer::TextureState::copy_destination, pp::renderer::TextureState::shader_read); if (!transition_mip_shader_read_status.ok()) { print_error("record-render", transition_mip_shader_read_status.message); return 2; } const auto begin_status = context.begin_render_pass( *target.value(), pp::renderer::RenderPassDesc { .clear_color = pp::renderer::ClearColor { .r = 0.0F, .g = 0.0F, .b = 0.0F, .a = 1.0F }, .clear_depth_enabled = true, .clear_depth = 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 scissor_status = context.set_scissor(pp::renderer::ScissorRect { .enabled = true, .x = 0, .y = 0, .width = args.width, .height = args.height, }); if (!scissor_status.ok()) { print_error("record-render", scissor_status.message); return 2; } const auto shader_status = context.bind_shader(*shader.value()); const auto uniform_status = context.set_shader_uniform("mvp", uniform_mvp); const auto blend_status = context.set_blend_state(pp::renderer::BlendState { .enabled = true, .source_color = pp::renderer::BlendFactor::source_alpha, .destination_color = pp::renderer::BlendFactor::one_minus_source_alpha, .source_alpha = pp::renderer::BlendFactor::one, .destination_alpha = pp::renderer::BlendFactor::one_minus_source_alpha, }); const auto depth_status = context.set_depth_state(pp::renderer::DepthState { .test_enabled = true, .write_enabled = true, .compare = pp::renderer::CompareOp::less_or_equal, }); const auto bind_texture_status = context.bind_texture(0, *texture.value()); const auto bind_sampler_status = context.bind_sampler(0, pp::renderer::SamplerDesc { .min_filter = pp::renderer::SamplerFilter::linear, .mag_filter = pp::renderer::SamplerFilter::linear, .mip_filter = pp::renderer::SamplerFilter::linear, .address_u = pp::renderer::SamplerAddressMode::clamp_to_edge, .address_v = pp::renderer::SamplerAddressMode::clamp_to_edge, .address_w = pp::renderer::SamplerAddressMode::clamp_to_edge, }); const auto mesh_status = context.bind_mesh(*mesh.value()); const auto draw_status = context.draw(pp::renderer::DrawDesc { .vertex_count = 3, .index_count = 3, }); context.end_render_pass(); if (!shader_status.ok()) { print_error("record-render", shader_status.message); return 2; } if (!uniform_status.ok()) { print_error("record-render", uniform_status.message); return 2; } if (!blend_status.ok()) { print_error("record-render", blend_status.message); return 2; } if (!depth_status.ok()) { print_error("record-render", depth_status.message); return 2; } if (!bind_texture_status.ok()) { print_error("record-render", bind_texture_status.message); return 2; } if (!bind_sampler_status.ok()) { print_error("record-render", bind_sampler_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; } const auto readback_status = context.read_texture( *texture.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = args.width, .height = args.height, }, *readback_buffer.value()); if (!readback_status.ok()) { print_error("record-render", readback_status.message); return 2; } const auto copy_status = context.copy_texture( *texture.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = args.width, .height = args.height, }, *texture.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = args.width, .height = args.height, }); if (!copy_status.ok()) { print_error("record-render", copy_status.message); return 2; } const auto capture_status = context.capture_frame(*target.value(), *readback_buffer.value()); if (!capture_status.ok()) { print_error("record-render", capture_status.message); return 2; } const auto blit_status = context.blit_render_target( *target.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = args.width, .height = args.height, }, *blit_target.value(), pp::renderer::ReadbackRegion { .x = 0, .y = 0, .width = args.width, .height = args.height, }, pp::renderer::BlitFilter::nearest); if (!blit_status.ok()) { print_error("record-render", blit_status.message); return 2; } std::size_t draw_commands = 0; std::size_t scissor_commands = 0; std::size_t blend_commands = 0; std::size_t depth_commands = 0; std::size_t uniform_commands = 0; std::size_t bind_texture_commands = 0; std::size_t bind_sampler_commands = 0; std::size_t upload_commands = 0; std::size_t mipmap_commands = 0; std::size_t transition_commands = 0; std::size_t transition_to_upload = 0; std::size_t transition_to_shader_read = 0; std::size_t copy_commands = 0; std::size_t readback_commands = 0; std::size_t capture_commands = 0; std::size_t blit_commands = 0; std::size_t trace_markers = 0; std::size_t trace_begin_scopes = 0; std::size_t trace_end_scopes = 0; std::size_t render_passes = 0; std::size_t depth_clears = 0; std::size_t stencil_clears = 0; std::uint64_t draw_vertices = 0; std::uint64_t draw_indices = 0; std::uint64_t uniform_bytes = 0; std::uint64_t upload_bytes = 0; std::uint64_t mipmap_levels = 0; std::uint64_t mipmap_bytes = 0; std::uint64_t copy_source_bytes = 0; std::uint64_t copy_destination_bytes = 0; std::uint64_t bound_texture_bytes = 0; std::uint64_t readback_bytes = 0; std::uint64_t capture_bytes = 0; std::uint64_t blit_source_bytes = 0; std::uint64_t blit_destination_bytes = 0; std::size_t labeled_command_descriptors = 0; const auto count_label = [&labeled_command_descriptors](const char* label) { if (label != nullptr && label[0] != '\0') { ++labeled_command_descriptors; } }; const auto commands = device.commands(); const auto features = device.features(); #ifdef PP_PANO_CLI_ENABLE_OPENGL_PLAN const auto open_gl_plan = pp::renderer::gl::plan_recorded_render_commands(commands); #endif for (const auto& command : commands) { if (command.kind == pp::renderer::RecordedRenderCommandKind::begin_render_pass) { ++render_passes; count_label(command.target_desc.debug_name); if (command.clear_depth_enabled) { ++depth_clears; } if (command.clear_stencil_enabled) { ++stencil_clears; } } else if (command.kind == pp::renderer::RecordedRenderCommandKind::draw) { ++draw_commands; count_label(command.mesh_desc.debug_name); draw_vertices += command.draw_desc.vertex_count; draw_indices += command.draw_desc.index_count; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::set_scissor) { ++scissor_commands; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::set_blend_state) { ++blend_commands; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::set_depth_state) { ++depth_commands; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::set_shader_uniform) { ++uniform_commands; uniform_bytes += command.uniform_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::bind_texture) { ++bind_texture_commands; count_label(command.texture_desc.debug_name); const auto bound_bytes = pp::renderer::texture_byte_size(command.texture_desc); if (bound_bytes.ok()) { bound_texture_bytes += bound_bytes.value(); } } else if (command.kind == pp::renderer::RecordedRenderCommandKind::bind_sampler) { ++bind_sampler_commands; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::bind_mesh) { count_label(command.mesh_desc.debug_name); } else if (command.kind == pp::renderer::RecordedRenderCommandKind::generate_mipmaps) { ++mipmap_commands; count_label(command.texture_desc.debug_name); mipmap_levels += command.generated_mip_levels; mipmap_bytes += command.generated_mip_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::transition_texture) { ++transition_commands; count_label(command.texture_desc.debug_name); if (command.after_state == pp::renderer::TextureState::upload_destination) { ++transition_to_upload; } if (command.after_state == pp::renderer::TextureState::shader_read) { ++transition_to_shader_read; } } else if (command.kind == pp::renderer::RecordedRenderCommandKind::upload_texture) { ++upload_commands; count_label(command.texture_desc.debug_name); upload_bytes += command.upload_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::copy_texture) { ++copy_commands; count_label(command.source_desc.debug_name); count_label(command.destination_desc.debug_name); copy_source_bytes += command.copy_source_bytes; copy_destination_bytes += command.copy_destination_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::read_texture) { ++readback_commands; count_label(command.texture_desc.debug_name); readback_bytes += command.readback_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::capture_frame) { ++capture_commands; count_label(command.target_desc.debug_name); capture_bytes += command.capture_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::blit_render_target) { ++blit_commands; count_label(command.source_desc.debug_name); count_label(command.destination_desc.debug_name); blit_source_bytes += command.blit_source_bytes; blit_destination_bytes += command.blit_destination_bytes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::trace_marker) { ++trace_markers; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::trace_begin_scope) { ++trace_begin_scopes; } else if (command.kind == pp::renderer::RecordedRenderCommandKind::trace_end_scope) { ++trace_end_scopes; } } std::cout << "{\"ok\":true,\"command\":\"record-render\"" << ",\"backend\":\"" << device.backend_name() << "\"" << ",\"features\":{\"framebufferFetch\":" << json_bool(features.framebuffer_fetch) << ",\"explicitTextureTransitions\":" << json_bool(features.explicit_texture_transitions) << ",\"textureCopy\":" << json_bool(features.texture_copy) << ",\"renderTargetBlit\":" << json_bool(features.render_target_blit) << ",\"frameCapture\":" << json_bool(features.frame_capture) << ",\"float16RenderTargets\":" << json_bool(features.float16_render_targets) << ",\"float32RenderTargets\":" << json_bool(features.float32_render_targets) << "}" << ",\"target\":{\"width\":" << args.width << ",\"height\":" << args.height << ",\"format\":\"rgba8\"}" << ",\"createdResources\":" << created_resources << ",\"exercisedClearReset\":" << json_bool(args.exercise_clear) << ",\"clearRejectedOrphanedTraceEnd\":" << json_bool(clear_rejected_orphaned_trace_end) << ",\"clearReusedRenderPass\":" << json_bool(clear_reused_render_pass) << ",\"labeledCommandDescriptors\":" << labeled_command_descriptors << ",\"commands\":" << commands.size() #ifdef PP_PANO_CLI_ENABLE_OPENGL_PLAN << ",\"openGlPlan\":{\"available\":true" << ",\"supported\":" << json_bool(open_gl_plan.supported) << ",\"commands\":" << open_gl_plan.commands.size() << ",\"renderPasses\":" << open_gl_plan.render_pass_count << ",\"drawCommands\":" << open_gl_plan.draw_command_count << ",\"shaderBindCommands\":" << open_gl_plan.shader_bind_command_count << ",\"uniformCommands\":" << open_gl_plan.uniform_command_count << ",\"textureBindCommands\":" << open_gl_plan.texture_bind_command_count << ",\"samplerBindCommands\":" << open_gl_plan.sampler_bind_command_count << ",\"uploadCommands\":" << open_gl_plan.upload_command_count << ",\"mipmapCommands\":" << open_gl_plan.mipmap_command_count << ",\"transitionCommands\":" << open_gl_plan.transition_command_count << ",\"copyCommands\":" << open_gl_plan.copy_command_count << ",\"readbackCommands\":" << open_gl_plan.readback_command_count << ",\"captureCommands\":" << open_gl_plan.capture_command_count << ",\"passthroughCommands\":" << open_gl_plan.passthrough_command_count << ",\"traceCommands\":" << open_gl_plan.trace_command_count << ",\"unsupportedCommands\":" << open_gl_plan.unsupported_command_count << ",\"renderPassOrderErrors\":" << open_gl_plan.render_pass_order_error_count << ",\"dependencyErrors\":" << open_gl_plan.dependency_error_count << ",\"endedInRenderPass\":" << json_bool(open_gl_plan.ended_in_render_pass) << "}" #endif << ",\"renderPasses\":" << render_passes << ",\"depthClears\":" << depth_clears << ",\"stencilClears\":" << stencil_clears << ",\"drawCommands\":" << draw_commands << ",\"drawVertices\":" << draw_vertices << ",\"drawIndices\":" << draw_indices << ",\"scissorCommands\":" << scissor_commands << ",\"blendCommands\":" << blend_commands << ",\"depthCommands\":" << depth_commands << ",\"uniformCommands\":" << uniform_commands << ",\"uniformBytes\":" << uniform_bytes << ",\"bindTextureCommands\":" << bind_texture_commands << ",\"bindSamplerCommands\":" << bind_sampler_commands << ",\"boundTextureBytes\":" << bound_texture_bytes << ",\"uploadCommands\":" << upload_commands << ",\"uploadBytes\":" << upload_bytes << ",\"mipmapCommands\":" << mipmap_commands << ",\"mipmapLevels\":" << mipmap_levels << ",\"mipmapBytes\":" << mipmap_bytes << ",\"transitionCommands\":" << transition_commands << ",\"transitionToUpload\":" << transition_to_upload << ",\"transitionToShaderRead\":" << transition_to_shader_read << ",\"copyCommands\":" << copy_commands << ",\"copySourceBytes\":" << copy_source_bytes << ",\"copyDestinationBytes\":" << copy_destination_bytes << ",\"readbackCommands\":" << readback_commands << ",\"readbackBytes\":" << readback_bytes << ",\"captureCommands\":" << capture_commands << ",\"captureBytes\":" << capture_bytes << ",\"blitCommands\":" << blit_commands << ",\"blitSourceBytes\":" << blit_source_bytes << ",\"blitDestinationBytes\":" << blit_destination_bytes << ",\"traceMarkers\":" << trace_markers << ",\"traceBeginScopes\":" << trace_begin_scopes << ",\"traceEndScopes\":" << trace_end_scopes << ",\"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 == "classify-open") { return classify_open(argc, argv); } if (command == "plan-document-file") { return plan_document_file(argc, argv); } if (command == "plan-export-target") { return plan_export_target(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 == "simulate-blend") { return simulate_blend(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-app-session") { return simulate_app_session(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; }