1793 lines
62 KiB
C++
1793 lines
62 KiB
C++
#include "assets/image_format.h"
|
|
#include "assets/image_metadata.h"
|
|
#include "assets/image_pixels.h"
|
|
#include "assets/ppi_header.h"
|
|
#include "document/document.h"
|
|
#include "document/ppi_import.h"
|
|
#include "foundation/parse.h"
|
|
#include "foundation/result.h"
|
|
#include "paint/stroke.h"
|
|
#include "paint/stroke_script.h"
|
|
#include "renderer_api/recording_renderer.h"
|
|
#include "ui_core/layout_xml.h"
|
|
|
|
#include <array>
|
|
#include <charconv>
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <system_error>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
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;
|
|
};
|
|
|
|
struct InspectImageArgs {
|
|
std::string path;
|
|
};
|
|
|
|
struct ImportImageArgs {
|
|
std::string path;
|
|
std::uint32_t document_width = 0;
|
|
std::uint32_t document_height = 0;
|
|
std::uint32_t face = 0;
|
|
std::uint32_t x = 0;
|
|
std::uint32_t y = 0;
|
|
};
|
|
|
|
struct ExportImageArgs {
|
|
std::string path;
|
|
std::uint32_t width = 2;
|
|
std::uint32_t height = 2;
|
|
};
|
|
|
|
struct ParseLayoutArgs {
|
|
std::string path;
|
|
};
|
|
|
|
struct InspectProjectArgs {
|
|
std::string path;
|
|
};
|
|
|
|
struct SimulateStrokeArgs {
|
|
std::uint32_t x1 = 0;
|
|
std::uint32_t y1 = 0;
|
|
std::uint32_t x2 = 0;
|
|
std::uint32_t y2 = 0;
|
|
std::uint32_t spacing = 1;
|
|
};
|
|
|
|
struct SimulateStrokeScriptArgs {
|
|
std::string path;
|
|
};
|
|
|
|
struct SimulateDocumentEditsArgs {
|
|
std::uint32_t width = 128;
|
|
std::uint32_t height = 64;
|
|
};
|
|
|
|
struct SimulateImageImportArgs {
|
|
std::uint32_t width = 64;
|
|
std::uint32_t height = 32;
|
|
};
|
|
|
|
struct SimulateDocumentHistoryArgs {
|
|
std::uint32_t width = 64;
|
|
std::uint32_t height = 32;
|
|
std::uint32_t history_entries = 4;
|
|
};
|
|
|
|
struct RecordRenderArgs {
|
|
std::uint32_t width = 64;
|
|
std::uint32_t height = 32;
|
|
};
|
|
|
|
void print_error(std::string_view command, std::string_view message)
|
|
{
|
|
std::cout << "{\"ok\":false,\"command\":\"" << command
|
|
<< "\",\"error\":\"" << message << "\"}\n";
|
|
}
|
|
|
|
std::span<const std::byte> transparent_png_1x1_bytes() noexcept
|
|
{
|
|
static constexpr std::array<std::byte, 68> transparent_png_1x1 {
|
|
std::byte { 0x89 }, std::byte { 0x50 }, std::byte { 0x4e }, std::byte { 0x47 },
|
|
std::byte { 0x0d }, std::byte { 0x0a }, std::byte { 0x1a }, std::byte { 0x0a },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x0d },
|
|
std::byte { 0x49 }, std::byte { 0x48 }, std::byte { 0x44 }, std::byte { 0x52 },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x08 }, std::byte { 0x06 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x00 }, std::byte { 0x1f }, std::byte { 0x15 }, std::byte { 0xc4 },
|
|
std::byte { 0x89 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x0b }, std::byte { 0x49 }, std::byte { 0x44 }, std::byte { 0x41 },
|
|
std::byte { 0x54 }, std::byte { 0x78 }, std::byte { 0x9c }, std::byte { 0x63 },
|
|
std::byte { 0x60 }, std::byte { 0x00 }, std::byte { 0x02 }, std::byte { 0x00 },
|
|
std::byte { 0x00 }, std::byte { 0x05 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x7a }, std::byte { 0x5e }, std::byte { 0xab }, std::byte { 0x3f },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x49 }, std::byte { 0x45 }, std::byte { 0x4e }, std::byte { 0x44 },
|
|
std::byte { 0xae }, std::byte { 0x42 }, std::byte { 0x60 }, std::byte { 0x82 },
|
|
};
|
|
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<char>(ch));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return escaped;
|
|
}
|
|
|
|
pp::foundation::Result<float> 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<float>::failure(
|
|
pp::foundation::Status::invalid_argument("invalid floating-point value"));
|
|
}
|
|
|
|
return pp::foundation::Result<float>::success(value);
|
|
}
|
|
|
|
void print_help()
|
|
{
|
|
std::cout
|
|
<< "pano_cli commands:\n"
|
|
<< " create-document --width N --height N [--layers N] [--frames N] [--frame-duration-ms N]\n"
|
|
<< " export-image --path FILE [--width N] [--height N]\n"
|
|
<< " inspect-image --path FILE\n"
|
|
<< " import-image --path FILE [--document-width N] [--document-height N] [--face N] [--x N] [--y N]\n"
|
|
<< " inspect-project --path FILE\n"
|
|
<< " load-project --path FILE\n"
|
|
<< " parse-layout --path FILE\n"
|
|
<< " record-render [--width N] [--height N]\n"
|
|
<< " save-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]\n"
|
|
<< " simulate-document-edits [--width N] [--height N]\n"
|
|
<< " simulate-document-history [--width N] [--height N] [--history N]\n"
|
|
<< " simulate-image-import [--width N] [--height N]\n"
|
|
<< " simulate-stroke --x1 N --y1 N --x2 N --y2 N [--spacing N]\n"
|
|
<< " simulate-stroke-script --path FILE\n"
|
|
<< " --help\n";
|
|
}
|
|
|
|
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::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") {
|
|
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 {
|
|
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");
|
|
}
|
|
|
|
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[] {
|
|
{
|
|
.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<const pp::assets::PpiDirtyFacePayloadConfig>(dirty_faces, 1)
|
|
: std::span<const pp::assets::PpiDirtyFacePayloadConfig>(),
|
|
});
|
|
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<const char*>(bytes.data()), static_cast<std::streamsize>(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)
|
|
<< "}}\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<char> chars {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
|
|
const auto format = pp::assets::detect_image_format(std::span<const std::byte>(data, chars.size()));
|
|
if (!format) {
|
|
print_error("inspect-image", format.status().message);
|
|
return 2;
|
|
}
|
|
|
|
pp::foundation::Result<pp::assets::ImageMetadata> metadata =
|
|
pp::foundation::Result<pp::assets::ImageMetadata>::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<const std::byte>(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<int>(metadata.value().bit_depth)
|
|
<< ",\"components\":" << static_cast<int>(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<char> chars {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
|
|
const auto image_result = pp::assets::decode_png_rgba8(std::span<const std::byte>(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<std::uint64_t>(args.width) > max_cli_export_bytes / 4ULL / static_cast<std::uint64_t>(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<std::uint8_t> pixels(
|
|
static_cast<std::size_t>(args.width) * static_cast<std::size_t>(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<std::size_t>(y) * args.width + x) * 4U;
|
|
pixels[offset + 0U] = static_cast<std::uint8_t>((x * 37U) & 0xffU);
|
|
pixels[offset + 1U] = static_cast<std::uint8_t>((y * 53U) & 0xffU);
|
|
pixels[offset + 2U] = static_cast<std::uint8_t>(((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<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
|
|
if (!stream) {
|
|
print_error("export-image", "image file could not be written");
|
|
return 2;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"export-image\""
|
|
<< ",\"path\":\"" << json_escape(args.path) << "\""
|
|
<< ",\"image\":{\"format\":\"png\",\"width\":" << args.width
|
|
<< ",\"height\":" << args.height
|
|
<< ",\"bytes\":" << pixels.size()
|
|
<< "},\"file\":{\"bytes\":" << bytes.size()
|
|
<< "}}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_inspect_project_args(int argc, char** argv, InspectProjectArgs& args)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string_view key(argv[i]);
|
|
if (key == "--path") {
|
|
if (i + 1 >= argc) {
|
|
return pp::foundation::Status::invalid_argument("missing value for option");
|
|
}
|
|
|
|
args.path = argv[++i];
|
|
} else {
|
|
return pp::foundation::Status::invalid_argument("unknown option");
|
|
}
|
|
}
|
|
|
|
if (args.path.empty()) {
|
|
return pp::foundation::Status::invalid_argument("path must not be empty");
|
|
}
|
|
|
|
return pp::foundation::Status::success();
|
|
}
|
|
|
|
int inspect_project(int argc, char** argv)
|
|
{
|
|
InspectProjectArgs args;
|
|
const auto status = parse_inspect_project_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("inspect-project", status.message);
|
|
return 2;
|
|
}
|
|
|
|
std::ifstream stream(args.path, std::ios::binary);
|
|
if (!stream) {
|
|
print_error("inspect-project", "project file could not be opened");
|
|
return 2;
|
|
}
|
|
|
|
const std::vector<char> chars {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
|
|
const auto project = pp::assets::parse_ppi_project_index(std::span<const std::byte>(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<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
|
|
const auto project = pp::assets::decode_ppi_project_images(std::span<const std::byte>(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<float>(args.x1),
|
|
.y = static_cast<float>(args.y1),
|
|
.pressure = 1.0F,
|
|
},
|
|
pp::paint::StrokePoint {
|
|
.x = static_cast<float>(args.x2),
|
|
.y = static_cast<float>(args.y2),
|
|
.pressure = 1.0F,
|
|
},
|
|
};
|
|
const auto samples = pp::paint::sample_stroke(
|
|
points,
|
|
pp::paint::StrokeSamplingConfig {
|
|
.spacing = static_cast<float>(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<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto script = pp::paint::parse_stroke_script(text);
|
|
if (!script) {
|
|
print_error("simulate-stroke-script", script.status().message);
|
|
return 2;
|
|
}
|
|
|
|
std::size_t total_samples = 0;
|
|
float total_distance = 0.0F;
|
|
for (const auto& stroke : script.value().strokes) {
|
|
const pp::paint::StrokePoint points[] { stroke.start, stroke.end };
|
|
const auto samples = pp::paint::sample_stroke(
|
|
points,
|
|
pp::paint::StrokeSamplingConfig {
|
|
.spacing = stroke.spacing,
|
|
});
|
|
if (!samples) {
|
|
print_error("simulate-stroke-script", samples.status().message);
|
|
return 2;
|
|
}
|
|
|
|
total_samples += samples.value().size();
|
|
total_distance += samples.value().back().distance;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"simulate-stroke-script\""
|
|
<< ",\"strokes\":" << script.value().strokes.size()
|
|
<< ",\"samples\":" << total_samples
|
|
<< ",\"distance\":" << total_distance << "}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_simulate_document_edits_args(
|
|
int argc,
|
|
char** argv,
|
|
SimulateDocumentEditsArgs& args)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string_view key(argv[i]);
|
|
if (key == "--width" || key == "--height") {
|
|
if (i + 1 >= argc) {
|
|
return pp::foundation::Status::invalid_argument("missing value for option");
|
|
}
|
|
|
|
const auto value = pp::foundation::parse_u32(argv[++i]);
|
|
if (!value) {
|
|
return value.status();
|
|
}
|
|
|
|
if (key == "--width") {
|
|
args.width = value.value();
|
|
} else {
|
|
args.height = value.value();
|
|
}
|
|
} else {
|
|
return pp::foundation::Status::invalid_argument("unknown option");
|
|
}
|
|
}
|
|
|
|
if (args.width == 0 || args.height == 0) {
|
|
return pp::foundation::Status::invalid_argument("width and height must be greater than zero");
|
|
}
|
|
|
|
return pp::foundation::Status::success();
|
|
}
|
|
|
|
int simulate_document_edits(int argc, char** argv)
|
|
{
|
|
SimulateDocumentEditsArgs args;
|
|
const auto status = parse_simulate_document_edits_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("simulate-document-edits", status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto document_result = pp::document::CanvasDocument::create(
|
|
pp::document::DocumentConfig {
|
|
.width = args.width,
|
|
.height = args.height,
|
|
.layer_count = 1,
|
|
});
|
|
if (!document_result) {
|
|
print_error("simulate-document-edits", document_result.status().message);
|
|
return 2;
|
|
}
|
|
|
|
auto document = document_result.value();
|
|
auto edit_status = document.set_frame_duration(0, 100);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto layer_result = document.add_layer("Paint");
|
|
if (!layer_result) {
|
|
print_error("simulate-document-edits", layer_result.status().message);
|
|
return 2;
|
|
}
|
|
|
|
const std::size_t paint_layer = layer_result.value();
|
|
edit_status = document.rename_layer(paint_layer, "Ink");
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_layer_visible(paint_layer, false);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_layer_alpha_locked(paint_layer, true);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_layer_opacity(paint_layer, 0.625F);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_layer_blend_mode(paint_layer, pp::paint::BlendMode::overlay);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_active_layer(paint_layer);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto duplicated_frame = document.duplicate_frame(0);
|
|
if (!duplicated_frame) {
|
|
print_error("simulate-document-edits", duplicated_frame.status().message);
|
|
return 2;
|
|
}
|
|
edit_status = document.set_frame_duration(duplicated_frame.value(), 333);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto added_frame = document.add_frame(250);
|
|
if (!added_frame) {
|
|
print_error("simulate-document-edits", added_frame.status().message);
|
|
return 2;
|
|
}
|
|
edit_status = document.move_frame(added_frame.value(), 0);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
edit_status = document.move_layer(paint_layer, 0);
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
|
|
edit_status = document.set_layer_frame_face_pixels(
|
|
0,
|
|
0,
|
|
pp::document::LayerFacePixels {
|
|
.face_index = 2,
|
|
.x = 1,
|
|
.y = 2,
|
|
.width = 1,
|
|
.height = 1,
|
|
.rgba8 = { 255, 0, 0, 255 },
|
|
});
|
|
if (!edit_status.ok()) {
|
|
print_error("simulate-document-edits", edit_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto& active_layer = document.layers()[document.active_layer_index()];
|
|
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()
|
|
<< "},\"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 << "]}\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<int>(image.pixels[3])
|
|
<< "},\"document\":{\"width\":" << document.width()
|
|
<< ",\"height\":" << document.height()
|
|
<< ",\"layers\":" << document.layers().size()
|
|
<< ",\"frames\":" << document.frames().size()
|
|
<< ",\"facePayloads\":" << document.face_pixel_payload_count()
|
|
<< "},\"payload\":{\"face\":" << payload.face_index
|
|
<< ",\"x\":" << payload.x
|
|
<< ",\"y\":" << payload.y
|
|
<< ",\"width\":" << payload.width
|
|
<< ",\"height\":" << payload.height
|
|
<< ",\"bytes\":" << payload.rgba8.size()
|
|
<< "}}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_simulate_document_history_args(
|
|
int argc,
|
|
char** argv,
|
|
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<std::size_t>(args.history_entries));
|
|
if (!history_result) {
|
|
print_error("simulate-document-history", history_result.status().message);
|
|
return 2;
|
|
}
|
|
|
|
auto history = history_result.value();
|
|
auto with_layer = history.current();
|
|
const auto layer_result = with_layer.add_layer("Paint");
|
|
if (!layer_result) {
|
|
print_error("simulate-document-history", layer_result.status().message);
|
|
return 2;
|
|
}
|
|
|
|
auto apply_status = history.apply(with_layer);
|
|
if (!apply_status.ok()) {
|
|
print_error("simulate-document-history", apply_status.message);
|
|
return 2;
|
|
}
|
|
|
|
auto with_frame = history.current();
|
|
const auto frame_result = with_frame.add_frame(250);
|
|
if (!frame_result) {
|
|
print_error("simulate-document-history", frame_result.status().message);
|
|
return 2;
|
|
}
|
|
|
|
apply_status = history.apply(with_frame);
|
|
if (!apply_status.ok()) {
|
|
print_error("simulate-document-history", apply_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto undo_status = history.undo();
|
|
if (!undo_status.ok()) {
|
|
print_error("simulate-document-history", undo_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto undo_layers = history.current().layers().size();
|
|
const auto undo_frames = history.current().frames().size();
|
|
const auto undo_can_redo = history.can_redo();
|
|
const auto undo_index = history.current_index();
|
|
|
|
const auto redo_status = history.redo();
|
|
if (!redo_status.ok()) {
|
|
print_error("simulate-document-history", redo_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto& current = history.current();
|
|
std::cout << "{\"ok\":true,\"command\":\"simulate-document-history\""
|
|
<< ",\"history\":{\"size\":" << history.size()
|
|
<< ",\"currentIndex\":" << history.current_index()
|
|
<< ",\"canUndo\":" << (history.can_undo() ? "true" : "false")
|
|
<< ",\"canRedo\":" << (history.can_redo() ? "true" : "false")
|
|
<< "},\"undo\":{\"layers\":" << undo_layers
|
|
<< ",\"frames\":" << undo_frames
|
|
<< ",\"currentIndex\":" << undo_index
|
|
<< ",\"canRedo\":" << (undo_can_redo ? "true" : "false")
|
|
<< "},\"current\":{\"width\":" << current.width()
|
|
<< ",\"height\":" << current.height()
|
|
<< ",\"layers\":" << current.layers().size()
|
|
<< ",\"frames\":" << current.frames().size()
|
|
<< ",\"activeLayer\":" << current.active_layer_index()
|
|
<< ",\"activeFrame\":" << current.active_frame_index()
|
|
<< ",\"animationDurationMs\":" << current.animation_duration_ms()
|
|
<< "}}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_record_render_args(int argc, char** argv, RecordRenderArgs& args)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string_view key(argv[i]);
|
|
if (key == "--width" || key == "--height") {
|
|
if (i + 1 >= argc) {
|
|
return pp::foundation::Status::invalid_argument("missing value for option");
|
|
}
|
|
|
|
const auto value = pp::foundation::parse_u32(argv[++i]);
|
|
if (!value) {
|
|
return value.status();
|
|
}
|
|
|
|
if (key == "--width") {
|
|
args.width = value.value();
|
|
} else {
|
|
args.height = value.value();
|
|
}
|
|
} else {
|
|
return pp::foundation::Status::invalid_argument("unknown option");
|
|
}
|
|
}
|
|
|
|
if (args.width == 0 || args.height == 0) {
|
|
return pp::foundation::Status::invalid_argument("width and height must be greater than zero");
|
|
}
|
|
|
|
return pp::foundation::Status::success();
|
|
}
|
|
|
|
int record_render(int argc, char** argv)
|
|
{
|
|
RecordRenderArgs args;
|
|
const auto status = parse_record_render_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("record-render", status.message);
|
|
return 2;
|
|
}
|
|
|
|
pp::renderer::RecordingRenderDevice device;
|
|
pp::renderer::RecordingRenderTarget target(pp::renderer::TextureDesc {
|
|
.extent = pp::renderer::Extent2D { .width = args.width, .height = args.height },
|
|
.format = pp::renderer::TextureFormat::rgba8,
|
|
.render_target = true,
|
|
});
|
|
pp::renderer::RecordingShaderProgram shader("pano-cli-record-render");
|
|
pp::renderer::RecordingMesh mesh(pp::renderer::MeshDesc {
|
|
.vertex_count = 3,
|
|
.index_count = 0,
|
|
.topology = pp::renderer::PrimitiveTopology::triangles,
|
|
});
|
|
|
|
device.trace()->marker("renderer", "pano_cli_record_render");
|
|
auto& context = device.immediate_context();
|
|
const auto begin_status = context.begin_render_pass(
|
|
target,
|
|
pp::renderer::ClearColor { .r = 0.0F, .g = 0.0F, .b = 0.0F, .a = 1.0F });
|
|
if (!begin_status.ok()) {
|
|
print_error("record-render", begin_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto viewport_status = context.set_viewport(
|
|
pp::renderer::Viewport { .x = 0, .y = 0, .width = args.width, .height = args.height });
|
|
if (!viewport_status.ok()) {
|
|
print_error("record-render", viewport_status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto shader_status = context.bind_shader(shader);
|
|
const auto mesh_status = context.bind_mesh(mesh);
|
|
const auto draw_status = context.draw();
|
|
context.end_render_pass();
|
|
|
|
if (!shader_status.ok()) {
|
|
print_error("record-render", shader_status.message);
|
|
return 2;
|
|
}
|
|
if (!mesh_status.ok()) {
|
|
print_error("record-render", mesh_status.message);
|
|
return 2;
|
|
}
|
|
if (!draw_status.ok()) {
|
|
print_error("record-render", draw_status.message);
|
|
return 2;
|
|
}
|
|
|
|
std::size_t draw_commands = 0;
|
|
std::size_t trace_markers = 0;
|
|
const auto commands = device.commands();
|
|
for (const auto& command : commands) {
|
|
if (command.kind == pp::renderer::RecordedRenderCommandKind::draw) {
|
|
++draw_commands;
|
|
} else if (command.kind == pp::renderer::RecordedRenderCommandKind::trace_marker) {
|
|
++trace_markers;
|
|
}
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"record-render\""
|
|
<< ",\"backend\":\"" << device.backend_name() << "\""
|
|
<< ",\"target\":{\"width\":" << args.width
|
|
<< ",\"height\":" << args.height
|
|
<< ",\"format\":\"rgba8\"}"
|
|
<< ",\"commands\":" << commands.size()
|
|
<< ",\"drawCommands\":" << draw_commands
|
|
<< ",\"traceMarkers\":" << trace_markers
|
|
<< ",\"first\":\""
|
|
<< pp::renderer::recorded_render_command_kind_name(commands.front().kind)
|
|
<< "\",\"last\":\""
|
|
<< pp::renderer::recorded_render_command_kind_name(commands.back().kind)
|
|
<< "\"}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_layout_args(int argc, char** argv, ParseLayoutArgs& args)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string_view key(argv[i]);
|
|
if (key == "--path") {
|
|
if (i + 1 >= argc) {
|
|
return pp::foundation::Status::invalid_argument("missing value for option");
|
|
}
|
|
|
|
args.path = argv[++i];
|
|
} else {
|
|
return pp::foundation::Status::invalid_argument("unknown option");
|
|
}
|
|
}
|
|
|
|
if (args.path.empty()) {
|
|
return pp::foundation::Status::invalid_argument("path must not be empty");
|
|
}
|
|
|
|
return pp::foundation::Status::success();
|
|
}
|
|
|
|
int parse_layout(int argc, char** argv)
|
|
{
|
|
ParseLayoutArgs args;
|
|
const auto status = parse_layout_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("parse-layout", status.message);
|
|
return 2;
|
|
}
|
|
|
|
std::ifstream stream(args.path, std::ios::binary);
|
|
if (!stream) {
|
|
print_error("parse-layout", "layout file could not be opened");
|
|
return 2;
|
|
}
|
|
|
|
const std::string xml {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto summary = pp::ui::parse_layout_xml(xml);
|
|
if (!summary) {
|
|
print_error("parse-layout", summary.status().message);
|
|
return 2;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"parse-layout\""
|
|
<< ",\"nodes\":" << summary.value().node_count
|
|
<< ",\"lengthAttributes\":" << summary.value().length_attribute_count
|
|
<< "}\n";
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc < 2) {
|
|
print_help();
|
|
return 1;
|
|
}
|
|
|
|
const std::string_view command(argv[1]);
|
|
if (command == "--help" || command == "-h") {
|
|
print_help();
|
|
return 0;
|
|
}
|
|
|
|
if (command == "create-document") {
|
|
return create_document(argc, argv);
|
|
}
|
|
|
|
if (command == "inspect-image") {
|
|
return inspect_image(argc, argv);
|
|
}
|
|
|
|
if (command == "export-image") {
|
|
return export_image(argc, argv);
|
|
}
|
|
|
|
if (command == "import-image") {
|
|
return import_image(argc, argv);
|
|
}
|
|
|
|
if (command == "inspect-project") {
|
|
return inspect_project(argc, argv);
|
|
}
|
|
|
|
if (command == "load-project") {
|
|
return load_project(argc, argv);
|
|
}
|
|
|
|
if (command == "simulate-stroke") {
|
|
return simulate_stroke(argc, argv);
|
|
}
|
|
|
|
if (command == "simulate-stroke-script") {
|
|
return simulate_stroke_script(argc, argv);
|
|
}
|
|
|
|
if (command == "simulate-document-edits") {
|
|
return simulate_document_edits(argc, argv);
|
|
}
|
|
|
|
if (command == "simulate-document-history") {
|
|
return simulate_document_history(argc, argv);
|
|
}
|
|
|
|
if (command == "simulate-image-import") {
|
|
return simulate_image_import(argc, argv);
|
|
}
|
|
|
|
if (command == "parse-layout") {
|
|
return parse_layout(argc, argv);
|
|
}
|
|
|
|
if (command == "record-render") {
|
|
return record_render(argc, argv);
|
|
}
|
|
|
|
if (command == "save-project") {
|
|
return save_project(argc, argv);
|
|
}
|
|
|
|
print_error(command, "unknown command");
|
|
return 2;
|
|
}
|