Skip to content

Commit

Permalink
Remove PFM support (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Sep 3, 2021
1 parent 4001b97 commit d8f895c
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 1,110 deletions.
22 changes: 11 additions & 11 deletions apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ void run_diff(const diff_params& params) {

// check sizes
if (image1.width != image2.width || image1.height != image2.height) {
throw io_error::mismatch_error(
params.image1, params.image2, "image different sizes");
throw io_error{
params.image1 + "," + params.image2 + ": image different sizes"};
}

// check types
if (image1.linear != image2.linear) {
throw io_error::mismatch_error(
params.image1, params.image2, "image different types");
throw io_error{
params.image1 + "," + params.image2 + "image different types"};
}

// compute diff
Expand All @@ -191,8 +191,8 @@ void run_diff(const diff_params& params) {
if (params.signal) {
for (auto& c : diff.pixels) {
if (max(xyz(c)) > params.threshold) {
throw io_error::mismatch_error(
params.image1, params.image2, "image content differs");
throw io_error{
params.image1 + "," + params.image2 + "image content differs"};
}
}
}
Expand Down Expand Up @@ -226,14 +226,14 @@ void run_setalpha(const setalpha_params& params) {

// check sizes
if (image.width != alpha.width || image.height != alpha.height) {
throw io_error::mismatch_error(
params.image, params.alpha, "image different size");
throw io_error{
params.image + "," + params.alpha + ": image different size"};
}

// check types
if (image.linear != alpha.linear) {
throw io_error::mismatch_error(
params.image, params.alpha, "image different types");
throw io_error{
params.image + "," + params.alpha + ": image different types"};
}

// edit alpha
Expand Down Expand Up @@ -294,7 +294,7 @@ void run(const vector<string>& args) {
} else if (params.command == "setalpha") {
return run_setalpha(params.setalpha);
} else {
throw io_error::command_error("yimage", params.command);
throw io_error("yimage: unknown command");
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ void run(const vector<string>& args) {
} else if (params.command == "glsculpt") {
return run_glsculpt(params.glsculpt);
} else {
throw io_error::command_error("ymesh", params.command);
throw io_error("ymesh: unknown command");
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/yscene/yscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void run(const vector<string>& args) {
} else if (params.command == "glview") {
return run_glview(params.glview);
} else {
throw io_error::command_error("yscene", params.command);
throw io_error("yscene; unknown command");
}
}

Expand Down
4 changes: 2 additions & 2 deletions apps/yshape/yshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void run_convert(const convert_params& params) {
if (params.toedges) {
// check faces
if (shape.triangles.empty() && shape.quads.empty())
throw io_error::shape_error(params.shape);
throw io_error{params.shape + ": empty shape"};

// convert to edges
auto edges = !shape.triangles.empty() ? get_edges(shape.triangles)
Expand Down Expand Up @@ -507,7 +507,7 @@ void run(const vector<string>& args) {
} else if (params.command == "glview") {
return run_glview(params.glview);
} else {
throw io_error::command_error("yshape", params.command);
throw io_error("yshape: unknown command");
}
}

Expand Down
52 changes: 10 additions & 42 deletions libs/yocto/yocto_commonio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <filesystem>
#include <limits>

#include "ext/fast_float.h"
#include "ext/json.hpp"

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -126,7 +125,7 @@ vector<string> list_directory(const string& dirname) {
}
return entries;
} catch (...) {
throw io_error{dirname, "cannot list directory"};
throw io_error{dirname + ": cannot list directory"};
}
}

Expand All @@ -136,7 +135,7 @@ void make_directory(const string& dirname) {
try {
create_directories(make_path(dirname));
} catch (...) {
throw io_error{dirname, "cannot create directory"};
throw io_error{dirname + ": cannot create directory"};
}
}

Expand Down Expand Up @@ -194,25 +193,25 @@ string load_text(const string& filename) {
void load_text(const string& filename, string& text) {
// https://stackoverflow.com/questions/174531/how-to-read-the-content-of-a-file-to-a-string-in-c
auto fs = fopen_utf8(filename.c_str(), "rb");
if (!fs) throw io_error::open_error(filename);
if (!fs) throw io_error{filename + ": cannot open file"};
fseek(fs, 0, SEEK_END);
auto length = ftell(fs);
fseek(fs, 0, SEEK_SET);
text.resize(length);
if (fread(text.data(), 1, length, fs) != length) {
fclose(fs);
throw io_error::read_error(filename);
throw io_error{filename + ": read error"};
}
fclose(fs);
}

// Save a text file
void save_text(const string& filename, const string& text) {
auto fs = fopen_utf8(filename.c_str(), "wt");
if (!fs) throw io_error::open_error(filename);
if (!fs) throw io_error{filename + ": cannot open file"};
if (fprintf(fs, "%s", text.c_str()) < 0) {
fclose(fs);
throw io_error::write_error(filename);
throw io_error{filename + ": write error"};
}
fclose(fs);
}
Expand All @@ -226,25 +225,25 @@ vector<byte> load_binary(const string& filename) {
void load_binary(const string& filename, vector<byte>& data) {
// https://stackoverflow.com/questions/174531/how-to-read-the-content-of-a-file-to-a-string-in-c
auto fs = fopen_utf8(filename.c_str(), "rb");
if (!fs) throw io_error::open_error(filename);
if (!fs) throw io_error{filename + ": cannot open file"};
fseek(fs, 0, SEEK_END);
auto length = ftell(fs);
fseek(fs, 0, SEEK_SET);
data.resize(length);
if (fread(data.data(), 1, length, fs) != length) {
fclose(fs);
throw io_error::read_error(filename);
throw io_error{filename + ": read error"};
}
fclose(fs);
}

// Save a binary file
void save_binary(const string& filename, const vector<byte>& data) {
auto fs = fopen_utf8(filename.c_str(), "wb");
if (!fs) throw io_error::open_error(filename);
if (!fs) throw io_error{filename + ": cannot open file"};
if (fwrite(data.data(), 1, data.size(), fs) != data.size()) {
fclose(fs);
throw io_error::write_error(filename);
throw io_error{filename + ": write error"};
}
fclose(fs);
}
Expand Down Expand Up @@ -648,34 +647,3 @@ bool format_json(string& text, const json_value& json, string& error) {
}

} // namespace yocto

// -----------------------------------------------------------------------------
// FAST CONVERSIONS FROM/TO CHARS
// -----------------------------------------------------------------------------
namespace yocto {

from_chars_result from_chars(
const char* first, const char* last, float& value) {
auto result = fast_float::from_chars(first, last, value);
return {result.ptr, result.ec};
}
from_chars_result from_chars(
const char* first, const char* last, double& value) {
auto result = fast_float::from_chars(first, last, value);
return {result.ptr, result.ec};
}

to_chars_result to_chars(char* first, char* last, float value) {
if (last - first >= std::numeric_limits<float>::max_digits10)
return {last, std::errc::value_too_large};
auto ptr = ::nlohmann::detail::to_chars(first, last, value);
return {ptr, std::errc()};
}
to_chars_result to_chars(char* first, char* last, double value) {
if (last - first >= std::numeric_limits<double>::max_digits10)
return {last, std::errc::value_too_large};
auto ptr = ::nlohmann::detail::to_chars(first, last, value);
return {ptr, std::errc()};
}

} // namespace yocto
68 changes: 2 additions & 66 deletions libs/yocto/yocto_commonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// Yocto/CommonIO is a collection of utilities used in writing IO functionality,
// including file IO, Json IO, and path manipulation.
// Yocto/CommonIO is implemented in `yocto_commonio.h` and `yocto_commonio.cpp`,
// and depends on `json.hpp` for Json serialization and number printing, and
// `fast_float.h` for number parsing.
// and depends on `json.hpp` for Json serialization and number printing.
//

//
Expand Down Expand Up @@ -69,32 +68,7 @@ namespace yocto {

// Io error used in exception handling
struct io_error : std::runtime_error {
string filename = "", message = "";
io_error(const string& filename, const string& message)
: std::runtime_error(filename + ": " + message)
, filename{filename}
, message{message} {}

// common errors
// clang-format off
static io_error open_error(const string& filename) { return {filename, "file not found"}; }
static io_error read_error(const string& filename) { return {filename, "read error"}; }
static io_error write_error(const string& filename) { return {filename, "write error"}; }
static io_error parse_error(const string& filename) { return {filename, "parse error"}; }
static io_error parse_error(const string& filename, const string& path) { return {filename, "parse error at " + path}; }
static io_error format_error(const string& filename) { return {filename, "unknown format"}; }
static io_error preset_error(const string& filename) { return {filename, "unknown preset"}; }
static io_error shape_error(const string& filename) { return {filename, "empty shape"}; }
static io_error dependent_error(const string& filename, const io_error& error) { return {filename, string{"error in "} + error.what()}; }
static io_error json_error(const string& filename) { return {filename, "json error"}; }
static io_error material_error(const string& filename, const string& name) { return {filename, "missing material " + name}; }
static io_error object_error(const string& filename, const string& name) { return {filename, "missing object " + name}; }
static io_error shape_error(const string& filename, const string& name) { return {filename, "missing shape " + name}; }
static io_error command_error(const string& filename, const string& name) { return {filename, "unknown command " + name}; }
static io_error type_error(const string& filename, const string& name) { return {filename, "unknown type " + name}; }
static io_error mismatch_error(const string& filename1, const string& filename2, const string& error) { return {filename1 + " and " + filename2, error}; }
static io_error not_implemented_error(const string& error) { return {"not implemented", error}; }
// clang-format on
io_error(const string& message) : std::runtime_error(message) {}
};

} // namespace yocto
Expand Down Expand Up @@ -783,42 +757,4 @@ bool format_json(string& text, const json_value& json, string& error);

} // namespace yocto

// -----------------------------------------------------------------------------
// FAST CONVERSIONS FROM/TO CHARS
// -----------------------------------------------------------------------------
namespace yocto {

// from_chars result
struct from_chars_result {
const char* ptr;
std::errc ec;
};

// from_chars
template <typename T>
from_chars_result from_chars(const char* first, const char* last, T& value) {
auto result = std::from_chars(first, last, value);
return {result.ptr, result.ec};
}
from_chars_result from_chars(const char* first, const char* last, float& value);
from_chars_result from_chars(
const char* first, const char* last, double& value);

// to_chars result
struct to_chars_result {
char* ptr;
std::errc ec;
};

// to_chars
template <typename T>
from_chars_result to_chars(const char* first, const char* last, T value) {
auto result = std::to_chars(first, last, value);
return {result.ptr, result.ec};
}
to_chars_result to_chars(char* first, char* last, float value);
to_chars_result to_chars(char* first, char* last, double value);

} // namespace yocto

#endif
Loading

0 comments on commit d8f895c

Please sign in to comment.