Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler Json serialization. #1129

Merged
merged 4 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 92 additions & 51 deletions apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ struct convert_params {
int height = 0;
};

// Json IO
void serialize_value(json_mode mode, json_value& json, convert_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(mode, json, value.image, "image", "Input image.", true);
serialize_property(mode, json, value.output, "output", "Output image.");
serialize_property(
mode, json, value.exposure, "exposure", "Tonemap exposure.");
serialize_property(mode, json, value.filmic, "filmic", "Tonemap filmic.");
serialize_property(mode, json, value.width, "width", "Resize width.");
serialize_property(mode, json, value.height, "height", "Resize height.");
serialize_property(mode, json, value.logo, "logo", "Add logo.");
serialize_clipositionals(mode, json, {"image"});
serialize_clialternates(mode, json,
{{"output", "o"}, {"exposure", "e"}, {"filmic", "f"}, {"width", "w"},
{"height", "h"}, {"logo", "L"}});
}

// convert images
int run_convert(const convert_params& params) {
// load
Expand Down Expand Up @@ -330,6 +348,16 @@ struct view_params {
bool logo = false;
};

// Json IO
void serialize_value(json_mode mode, json_value& json, view_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(mode, json, value.images, "images", "Input images.", true);
serialize_property(mode, json, value.output, "output", "Output image.");
serialize_clipositionals(mode, json, {"images"});
serialize_clialternates(mode, json, {{"output", "o"}});
}

#ifndef YOCTO_OPENGL

// convert images
Expand Down Expand Up @@ -392,6 +420,23 @@ struct diff_params {
float threshold = 0;
};

// Json IO
void serialize_value(json_mode mode, json_value& json, diff_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(
mode, json, value.image1, "image1", "Input image 1.", true);
serialize_property(
mode, json, value.image2, "image2", "Input image 2.", true);
serialize_property(mode, json, value.output, "output", "Output image.");
serialize_property(mode, json, value.signal, "signal", "Error on diff.");
serialize_property(
mode, json, value.threshold, "threshold", "Diff threshold.");
serialize_property(mode, json, value.logo, "logo", "Add logo.");
serialize_clipositionals(mode, json, {"image1", "image2"});
serialize_clialternates(mode, json, {{"output", "o"}});
}

// resize images
int run_diff(const diff_params& params) {
// load
Expand Down Expand Up @@ -450,6 +495,22 @@ struct setalpha_params {
bool to_color = false;
};

// Json IO
void serialize_value(json_mode mode, json_value& json, setalpha_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(mode, json, value.image, "image", "Input image.", true);
serialize_property(mode, json, value.alpha, "alpha", "Alpha image.", true);
serialize_property(mode, json, value.output, "output", "Output image.");
serialize_property(
mode, json, value.from_color, "from-color", "Alpha from color.");
serialize_property(
mode, json, value.to_color, "to-color", "Color from alpha.");
serialize_property(mode, json, value.logo, "logo", "Add logo.");
serialize_clipositionals(mode, json, {"image", "alpha"});
serialize_clialternates(mode, json, {{"output", "o"}});
}

// setalpha images
int run_setalpha(const setalpha_params& params) {
// load
Expand Down Expand Up @@ -504,61 +565,41 @@ int run_setalpha(const setalpha_params& params) {
return 0;
}

struct app_params {
string command = "convert";
convert_params convert = {};
view_params view = {};
diff_params diff = {};
setalpha_params setalpha = {};
};

// Json IO
void serialize_value(json_mode mode, json_value& json, app_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_command(mode, json, value.command, "command", "Command.");
serialize_property(mode, json, value.convert, "convert", "Convert images.");
serialize_property(mode, json, value.view, "view", "View images.");
serialize_property(mode, json, value.diff, "diff", "Diff two images.");
serialize_property(
mode, json, value.setalpha, "setalpha", "Set alpha in images.");
}

int main(int argc, const char* argv[]) {
// command line parameters
auto convert = convert_params{};
auto view = view_params{};
auto diff = diff_params{};
auto setalpha = setalpha_params{};

// parse command line
auto cli = make_cli("yimage", "Transform images");

auto cli_convert = add_command(cli, "convert", "Convert images");
add_optional(cli_convert, "output", convert.output, "Output image", "o");
add_optional(cli_convert, "exposure", convert.exposure, "Exposure", "e");
add_optional(cli_convert, "filmic", convert.filmic, "Filmic curve", "f");
add_optional(cli_convert, "width", convert.width, "resize width", "w");
add_optional(cli_convert, "height", convert.height, "resize height", "h");
add_optional(cli_convert, "logo", convert.logo, "Add logo", "L");
add_positional(cli_convert, "image", convert.image, "Input image");

auto cli_view = add_command(cli, "view", "View images");
add_optional(cli_view, "output", view.output, "Output image", "o");
add_positional(cli_view, "images", view.images, "Input images");

auto cli_diff = add_command(cli, "diff", "Diff two images");
add_optional(cli_diff, "signal", diff.signal, "Signal a diff as error");
add_optional(cli_diff, "threshold,", diff.threshold, "Diff threshold");
add_optional(cli_diff, "logo", diff.logo, "Add logo", "o");
add_optional(cli_diff, "output", diff.output, "Output image");
add_positional(cli_diff, "image1", diff.image1, "Input image");
add_positional(cli_diff, "image2", diff.image2, "Input image");

auto cli_setalpha = add_command(cli, "setalpha", "Set alpha in images");
add_optional(
cli_setalpha, "from-color", setalpha.from_color, "Alpha from color");
add_optional(cli_setalpha, "to-color", setalpha.to_color, "Color from alpha");
add_optional(cli_setalpha, "logo", setalpha.logo, "Add logo");
add_optional(cli_setalpha, "output", setalpha.output, "Output image", "o");
add_positional(cli_setalpha, "image", setalpha.image, "Input image");
add_positional(cli_setalpha, "alpha", setalpha.alpha, "Alpha filename");

// parse cli
parse_cli(cli, argc, argv);
auto params = app_params{};
parse_cli(params, "Process and view images", argc, argv);

// dispatch commands
auto command = get_command(cli);
auto error = string{};
if (command == "convert") {
return run_convert(convert);
} else if (command == "view") {
return run_view(view);
} else if (command == "diff") {
return run_diff(diff);
} else if (command == "setalpha") {
return run_setalpha(setalpha);
if (params.command == "convert") {
return run_convert(params.convert);
} else if (params.command == "view") {
return run_view(params.view);
} else if (params.command == "diff") {
return run_diff(params.diff);
} else if (params.command == "setalpha") {
return run_setalpha(params.setalpha);
} else {
return print_fatal("unknown command " + command);
return print_fatal("unknown command " + params.command);
}
}
11 changes: 11 additions & 0 deletions apps/ymeshtest/ymeshtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ void to_json(json_value& js, const mesh_point& value) {
json_value{json_array{json_value{value.uv.x}, json_value{value.uv.y}}});
}

void from_json(const json_value& js, mesh_point& value) {
value.face = js.at(0).get<int>();
value.uv.x = js.at(0).at(0).get<float>();
value.uv.y = js.at(0).at(1).get<float>();
}

void to_schema(
json_value& schema, const mesh_point& value, const string& description) {
example_to_schema(schema, to_json(value), description);
}

} // namespace yocto

// Save a path
Expand Down
118 changes: 30 additions & 88 deletions apps/ytrace/ytrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,18 @@ struct render_params : trace_params {
};

// Json IO
void to_json(json_value& json, const render_params& value) {
json["scene"] = value.scene;
json["output"] = value.output;
json["camera"] = value.camera;
json["addsky"] = value.addsky;
json["savebatch"] = value.savebatch;
to_json(json, (const trace_params&)value);
}
void from_json(const json_value& json, render_params& value) {
auto default_ = render_params{};
value.scene = json.value("scene", default_.scene);
value.output = json.value("output", default_.output);
value.camera = json.value("camera", default_.camera);
value.addsky = json.value("addsky", default_.addsky);
value.savebatch = json.value("savebatch", default_.savebatch);
from_json(json, (trace_params&)value);
}
void to_schema(
json_value& schema, const render_params& value, const string& descr) {
schema = to_schema_object(descr);
auto& properties = get_schema_properties(schema);
properties["scene"] = to_schema(value.scene, "Scene filename.");
properties["output"] = to_schema(value.output, "Output filename.");
properties["camera"] = to_schema(value.camera, "Camera name.");
properties["addsky"] = to_schema(value.addsky, "Add sky.");
properties["savebatch"] = to_schema(value.savebatch, "Save batch.");
properties.update(
get_schema_properties(to_schema((const trace_params&)value, "")));
get_schema_required(schema).push_back("scene");
get_schema_positional(schema).push_back("scene");
get_schema_alternate(schema)["samples"] = "s";
get_schema_alternate(schema)["bounces"] = "b";
get_schema_alternate(schema)["output"] = "o";
get_schema_alternate(schema)["tracer"] = "t";
void serialize_value(json_mode mode, json_value& json, render_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(mode, json, value.scene, "scene", "Scene filename.", true);
serialize_property(mode, json, value.output, "output", "Output filename.");
serialize_property(mode, json, value.camera, "camera", "Camera name.");
serialize_property(mode, json, value.addsky, "addsky", "Add sky.");
serialize_property(mode, json, value.savebatch, "savebatch", "Save batch.");
serialize_value(mode, json, (trace_params&)value, description);
serialize_clipositionals(mode, json, {"scene"});
serialize_clialternates(mode, json,
{{"samples", "s"}, {"bounces", "b"}, {"output", "o"}, {"tracer", "t"}});
}

// convert images
Expand Down Expand Up @@ -281,40 +259,18 @@ struct view_params : trace_params {
};

// Json IO
void to_json(json_value& json, const view_params& value) {
json["scene"] = value.scene;
json["output"] = value.output;
json["camera"] = value.camera;
json["addsky"] = value.addsky;
json["savebatch"] = value.savebatch;
to_json(json, (const trace_params&)value);
}
void from_json(const json_value& json, view_params& value) {
auto default_ = view_params{};
value.scene = json.value("scene", default_.scene);
value.output = json.value("output", default_.output);
value.camera = json.value("camera", default_.camera);
value.addsky = json.value("addsky", default_.addsky);
value.savebatch = json.value("savebatch", default_.savebatch);
from_json(json, (trace_params&)value);
}
void to_schema(
json_value& schema, const view_params& value, const string& descr) {
schema = to_schema_object(descr);
auto& properties = get_schema_properties(schema);
properties["scene"] = to_schema(value.scene, "Scene filename.");
properties["output"] = to_schema(value.output, "Output filename.");
properties["camera"] = to_schema(value.camera, "Camera name.");
properties["addsky"] = to_schema(value.addsky, "Add sky.");
properties["savebatch"] = to_schema(value.savebatch, "Save batch.");
properties.update(
get_schema_properties(to_schema((const trace_params&)value, "")));
get_schema_required(schema).push_back("scene");
get_schema_positional(schema).push_back("scene");
get_schema_alternate(schema)["samples"] = "s";
get_schema_alternate(schema)["bounces"] = "b";
get_schema_alternate(schema)["output"] = "o";
get_schema_alternate(schema)["tracer"] = "t";
void serialize_value(json_mode mode, json_value& json, view_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_property(mode, json, value.scene, "scene", "Scene filename.", true);
serialize_property(mode, json, value.output, "output", "Output filename.");
serialize_property(mode, json, value.camera, "camera", "Camera name.");
serialize_property(mode, json, value.addsky, "addsky", "Add sky.");
serialize_property(mode, json, value.savebatch, "savebatch", "Save batch.");
serialize_value(mode, json, (trace_params&)value, description);
serialize_clipositionals(mode, json, {"scene"});
serialize_clialternates(mode, json,
{{"samples", "s"}, {"bounces", "b"}, {"output", "o"}, {"tracer", "t"}});
}

#ifndef YOCTO_OPENGL
Expand Down Expand Up @@ -423,26 +379,12 @@ struct app_params {
};

// Json IO
void to_json(json_value& json, const app_params& value) {
json["command"] = value.command;
json["render"] = value.render;
json["view"] = value.view;
}
void from_json(const json_value& json, app_params& value) {
auto default_ = app_params{};
value.command = json.value("command", default_.command);
value.render = json.value("render", default_.render);
value.view = json.value("view", default_.view);
}
void to_schema(
json_value& schema, const app_params& value, const string& descr) {
schema = to_schema_object(descr);
auto& properties = get_schema_properties(schema);
properties["command"] = to_schema(value.command, "Command.");
properties["render"] = to_schema(value.render, "Render final images.");
properties["view"] = to_schema(value.view, "Render interactively.");
get_schema_required(schema).push_back("command");
get_schema_command(schema) = "command";
void serialize_value(json_mode mode, json_value& json, app_params& value,
const string& description) {
serialize_object(mode, json, value, description);
serialize_command(mode, json, value.command, "command", "Command.");
serialize_property(mode, json, value.render, "render", "Render offline.");
serialize_property(mode, json, value.view, "view", "Render interactively.");
}

int main(int argc, const char* argv[]) {
Expand Down
6 changes: 4 additions & 2 deletions libs/yocto/yocto_commonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,10 @@ inline void add_optional(const cli_command& cmd, const string& name, T& value,
property["type"] = cli_gettype<T>();
property["description"] = usage;
property["default"] = value;
for (auto choice : choices) {
property["enum"].push_back(choice);
if constexpr (!std::is_same_v<T, bool>) {
for (auto& choice : choices) {
property["enum"].push_back(choice);
}
}
setter[name].setter = [&value](
const json_value& js, json_error& error) -> bool {
Expand Down
Loading