Skip to content

Commit

Permalink
Io uses exceptions like the standard library. (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Jan 22, 2020
1 parent 594aebf commit b472478
Show file tree
Hide file tree
Showing 20 changed files with 2,035 additions and 4,143 deletions.
52 changes: 25 additions & 27 deletions apps/yimgproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ image<vec4f> filter_bilateral(

} // namespace yocto

int main(int argc, const char* argv[]) {
void run_app(int argc, const char* argv[]) {
// command line parameters
auto tonemap_on = false;
auto tonemap_exposure = 0;
Expand Down Expand Up @@ -142,44 +142,36 @@ int main(int argc, const char* argv[]) {
add_cli_option(cli, "--diff-threshold,", diff_threshold, "diff threshold");
add_cli_option(cli, "--output,-o", output, "output image filename", true);
add_cli_option(cli, "filename", filename, "input image filename", true);
if (!parse_cli(cli, argc, argv)) exit(1);
parse_cli(cli, argc, argv);

// error string buffer
auto error = ""s;

// load
auto img = image<vec4f>();
if (!load_image(filename, img)) {
print_fatal("cannor load " + filename);
}
auto img = load_image(filename);

// set alpha
if (alpha_filename != "") {
auto alpha = image<vec4f>();
if (!load_image(alpha_filename, alpha)) {
print_fatal("cannor load " + alpha_filename);
}
if (img.size() != alpha.size()) print_fatal("bad image size");
auto alpha = load_image(alpha_filename);
if (img.size() != alpha.size()) throw std::runtime_error("bad image size");
for (auto j = 0; j < img.size().y; j++)
for (auto i = 0; i < img.size().x; i++) img[{i, j}].w = alpha[{i, j}].w;
}

// set alpha
if (coloralpha_filename != "") {
auto alpha = image<vec4f>();
if (!load_image(coloralpha_filename, alpha)) {
print_fatal("cannor load " + coloralpha_filename);
}
if (img.size() != alpha.size()) print_fatal("bad image size");
auto alpha = load_image(coloralpha_filename);
if (img.size() != alpha.size()) throw std::runtime_error("bad image size");
for (auto j = 0; j < img.size().y; j++)
for (auto i = 0; i < img.size().x; i++)
img[{i, j}].w = mean(xyz(alpha[{i, j}]));
}

// diff
if (diff_filename != "") {
auto diff = image<vec4f>();
if (!load_image(diff_filename, diff)) {
print_fatal("cannor load " + diff_filename);
}
if (img.size() != diff.size()) print_fatal("image sizes are different");
auto diff = load_image(diff_filename);
if (img.size() != diff.size())
throw std::runtime_error("image sizes are different");
img = image_difference(img, diff, true);
}

Expand All @@ -199,17 +191,23 @@ int main(int argc, const char* argv[]) {
}

// save
if (!save_image(output, logo ? add_logo(img) : img)) {
print_fatal("cannor save " + output);
}
save_image(output, logo ? add_logo(img) : img);

// check diff
if (diff_filename != "" && diff_signal) {
for (auto& c : img) {
if (max(xyz(c)) > diff_threshold) print_fatal("image content differs");
if (max(xyz(c)) > diff_threshold)
throw std::runtime_error("image content differs");
}
}
}

// done
return 0;
int main(int argc, const char* argv[]) {
try {
run_app(argc, argv);
return 0;
} catch (std::exception& e) {
print_fatal(e.what());
return 1;
}
}
48 changes: 23 additions & 25 deletions apps/yimgview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,14 @@ struct app_state {
string error = "";
};

// load state
struct load_state {
string filename = "";
shared_ptr<app_state> app = {};
imageio_status status = {};
};

// app states
struct app_states {
// data
vector<shared_ptr<app_state>> states = {};
int selected = -1;

// loading
deque<future<load_state>> loaders = {};
deque<future<shared_ptr<app_state>>> loaders = {};

// default options
float exposure = 0;
Expand Down Expand Up @@ -149,7 +142,7 @@ void update_display(shared_ptr<app_state> app) {
// add a new image
void load_image_async(shared_ptr<app_states> apps, const string& filename) {
apps->loaders.push_back(
async(launch::async, [apps, filename]() -> load_state {
async(launch::async, [apps, filename]() -> shared_ptr<app_state> {
auto app = make_shared<app_state>();
app->filename = filename;
app->outname = replace_extension(filename, ".display.png");
Expand All @@ -158,9 +151,7 @@ void load_image_async(shared_ptr<app_states> apps, const string& filename) {
app->filmic = apps->filmic;
app->params = apps->params;
apps->selected = (int)apps->states.size() - 1;
if (auto ret = load_image(app->filename, app->source); !ret) {
return {filename, nullptr, ret};
}
load_image(app->filename, app->source);
compute_stats(
app->source_stats, app->source, is_hdr_filename(app->filename));
if (app->colorgrade) {
Expand All @@ -169,7 +160,7 @@ void load_image_async(shared_ptr<app_states> apps, const string& filename) {
app->display = tonemap_image(app->source, app->exposure, app->filmic);
}
compute_stats(app->display_stats, app->display, false);
return {filename, app, {}};
return app;
}));
}

Expand Down Expand Up @@ -306,35 +297,35 @@ void draw(const opengl_window& win, shared_ptr<app_states> apps,
}

void update(const opengl_window& win, shared_ptr<app_states> apps) {
auto is_ready = [](const future<load_state>& result) -> bool {
auto is_ready = [](const future<shared_ptr<app_state>>& result) -> bool {
return result.valid() &&
result.wait_for(chrono::microseconds(0)) == future_status::ready;
};

while (!apps->loaders.empty() && is_ready(apps->loaders.front())) {
auto [filename, app, status] = apps->loaders.front().get();
apps->loaders.pop_front();
if (!status) {
push_glmessage(win, "cannot load image " + filename);
log_glinfo(win, "cannot load image " + filename);
log_glinfo(win, status.error);
} else {
try {
auto app = apps->loaders.front().get();
apps->loaders.pop_front();
apps->states.push_back(app);
update_display(app);
if (apps->selected < 0) apps->selected = (int)apps->states.size() - 1;
} catch (std::exception& e) {
apps->loaders.pop_front();
push_glmessage(win, e.what());
log_glinfo(win, e.what());
}
}
}

int main(int argc, const char* argv[]) {
void run_app(int argc, const char* argv[]) {
// prepare application
auto apps = make_shared<app_states>();
auto filenames = vector<string>{};

// command line options
auto cli = make_cli("yimgview", "view images");
add_cli_option(cli, "images", filenames, "image filenames", true);
if (!parse_cli(cli, argc, argv)) exit(1);
parse_cli(cli, argc, argv);

// loading images
for (auto filename : filenames) load_image_async(apps, filename);
Expand Down Expand Up @@ -380,7 +371,14 @@ int main(int argc, const char* argv[]) {

// cleanup
clear_glwindow(win);
}

// done
return 0;
int main(int argc, const char* argv[]) {
try {
run_app(argc, argv);
return 0;
} catch (std::exception& e) {
print_fatal(e.what());
return 1;
}
}
18 changes: 12 additions & 6 deletions apps/yimgviews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void update_display(shared_ptr<app_state> app) {
});
}

int main(int argc, const char* argv[]) {
void run_app(int argc, const char* argv[]) {
// prepare application
auto app = make_shared<app_state>();
auto filenames = vector<string>{};
Expand All @@ -93,11 +93,10 @@ int main(int argc, const char* argv[]) {
auto cli = make_cli("yimgview", "view images");
add_cli_option(cli, "--output,-o", app->outname, "image output");
add_cli_option(cli, "image", app->filename, "image filename", true);
if (!parse_cli(cli, argc, argv)) exit(1);
parse_cli(cli, argc, argv);

// load image
if (!load_image(app->filename, app->source))
print_fatal("cannot load " + app->filename);
load_image(app->filename, app->source);

// update display
update_display(app);
Expand Down Expand Up @@ -136,7 +135,14 @@ int main(int argc, const char* argv[]) {

// cleanup
clear_glwindow(win);
}

// done
return 0;
int main(int argc, const char* argv[]) {
try {
run_app(argc, argv);
return 0;
} catch (std::exception& e) {
print_fatal(e.what());
return 1;
}
}
14 changes: 12 additions & 2 deletions apps/yimshproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ void my_draw_glwidgets(
}
}

int main(int num_args, const char* args[]) {
void run_app(int argc, const char* argv[]) {
string input_filename = "model.obj";

// Parse command line.
auto cli = make_cli("yimshproc", "interactive viewer for mesh processing");
add_cli_option(cli, "model", input_filename, "model filenames", true);
if (!parse_cli(cli, num_args, args)) exit(1);
parse_cli(cli, argc, argv);

auto data = my_data{};

Expand All @@ -161,3 +161,13 @@ int main(int num_args, const char* args[]) {

yimshproc(input_filename, init, key_callback, click_callback, draw_glwidgets);
}

int main(int argc, const char* argv[]) {
try {
run_app(argc, argv);
return 0;
} catch (std::exception& e) {
print_fatal(e.what());
return 1;
}
}
26 changes: 9 additions & 17 deletions apps/ymshproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int main(int argc, const char** argv) {
add_cli_option(cli, "--info,-i", info, "print mesh info");
add_cli_option(cli, "--output,-o", output, "output mesh", true);
add_cli_option(cli, "mesh", filename, "input mesh", true);
if (!parse_cli(cli, argc, argv)) exit(1);
parse_cli(cli, argc, argv);

// mesh data
auto positions = vector<vec3f>{};
Expand All @@ -104,15 +104,11 @@ int main(int argc, const char** argv) {
// load mesh
auto load_timer = print_timed("loading shape");
if (!facevarying) {
if (auto ret = load_shape(filename, points, lines, triangles, quads,
positions, normals, texcoords, colors, radius);
!ret)
print_fatal(ret.error);
load_shape(filename, points, lines, triangles, quads, positions, normals,
texcoords, colors, radius);
} else {
if (auto ret = load_fvshape(filename, quadspos, quadsnorm, quadstexcoord,
positions, normals, texcoords);
!ret)
print_fatal(ret.error);
load_fvshape(filename, quadspos, quadsnorm, quadstexcoord, positions,
normals, texcoords);
}
print_elapsed(load_timer);

Expand Down Expand Up @@ -266,15 +262,11 @@ int main(int argc, const char** argv) {
// save mesh
auto save_timer = print_timed("saving shape");
if (!quadspos.empty()) {
if (auto ret = save_fvshape(output, quadspos, quadsnorm, quadstexcoord,
positions, normals, texcoords);
!ret)
print_fatal(ret.error);
save_fvshape(output, quadspos, quadsnorm, quadstexcoord, positions, normals,
texcoords);
} else {
if (auto ret = save_shape(output, points, lines, triangles, quads,
positions, normals, texcoords, colors, radius);
!ret)
print_fatal(ret.error);
save_shape(output, points, lines, triangles, quads, positions, normals,
texcoords, colors, radius);
}
print_elapsed(save_timer);

Expand Down
Loading

0 comments on commit b472478

Please sign in to comment.