From 606ea6b94efb9603ce6ec7522f3fb0476d695f04 Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 22:21:07 +0200 Subject: [PATCH 1/9] cleanup --- libs/yocto/yocto_modelio.cpp | 8 -------- libs/yocto/yocto_modelio.h | 2 -- 2 files changed, 10 deletions(-) diff --git a/libs/yocto/yocto_modelio.cpp b/libs/yocto/yocto_modelio.cpp index 656208319..522cfea38 100644 --- a/libs/yocto/yocto_modelio.cpp +++ b/libs/yocto/yocto_modelio.cpp @@ -261,14 +261,6 @@ ply_model::~ply_model() { for (auto element : elements) delete element; } -// Make ply -inline ply_element* add_property(ply_model* ply) { - return ply->elements.emplace_back(new ply_element{}); -} -inline ply_property* add_property(ply_element* element) { - return element->properties.emplace_back(new ply_property{}); -} - // Load ply bool load_ply(const string& filename, ply_model* ply, string& error) { // ply type names diff --git a/libs/yocto/yocto_modelio.h b/libs/yocto/yocto_modelio.h index e2c6a8ae6..2276a836c 100644 --- a/libs/yocto/yocto_modelio.h +++ b/libs/yocto/yocto_modelio.h @@ -120,8 +120,6 @@ bool save_ply(const string& filename, ply_model* ply, string& error); // Get ply properties bool has_property( ply_model* ply, const string& element, const string& property); -ply_property* get_property( - ply_model* ply, const string& element, const string& property); bool get_value(ply_model* ply, const string& element, const string& property, vector& values); From 9ee74c1ad9fbdce59912d30593af7f97e906a9df Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 22:31:25 +0200 Subject: [PATCH 2/9] updated --- libs/yocto/yocto_image.cpp | 67 ++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index 437eb76f4..d13b63302 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1452,50 +1452,50 @@ static vector split_string(const string& str) { } // Pfm load -static float* load_pfm(const char* filename, int* w, int* h, int* nc, int req) { +static vector load_pfm( + const char* filename, int& w, int& h, int& nc, int req) { auto fs = open_file(filename, "rb"); - if (!fs) return nullptr; + if (!fs) return {}; // buffer auto buffer = array{}; auto toks = vector(); // read magic - if (!read_line(fs, buffer)) return nullptr; + if (!read_line(fs, buffer)) return {}; toks = split_string(buffer.data()); - if (toks[0] == "Pf") - *nc = 1; - else if (toks[0] == "PF") - *nc = 3; - else - return nullptr; + if (toks[0] == "Pf") { + nc = 1; + } else if (toks[0] == "PF") { + nc = 3; + } else { + return {}; + } // read w, h - if (!read_line(fs, buffer)) return nullptr; + if (!read_line(fs, buffer)) return {}; toks = split_string(buffer.data()); - *w = atoi(toks[0].c_str()); - *h = atoi(toks[1].c_str()); + w = atoi(toks[0].c_str()); + h = atoi(toks[1].c_str()); // read scale - if (!read_line(fs, buffer)) return nullptr; + if (!read_line(fs, buffer)) return {}; toks = split_string(buffer.data()); auto s = atof(toks[0].c_str()); // read the data (flip y) - auto npixels = (size_t)(*w) * (size_t)(*h); - auto nvalues = npixels * (size_t)(*nc); - auto nrow = (size_t)(*w) * (size_t)(*nc); - auto pixels = std::unique_ptr(new float[nvalues]); - for (auto j = *h - 1; j >= 0; j--) { - if (!read_values(fs, pixels.get() + j * nrow, nrow)) return nullptr; + auto npixels = (size_t)(w) * (size_t)(h); + auto nvalues = npixels * (size_t)(nc); + auto nrow = (size_t)(w) * (size_t)(nc); + auto pixels = vector(nvalues); + for (auto j = h - 1; j >= 0; j--) { + if (!read_values(fs, pixels.data() + j * nrow, nrow)) return {}; } // endian conversion if (s > 0) { for (auto i = 0; i < nvalues; ++i) { - auto dta = (uint8_t*)(pixels.get() + i); - std::swap(dta[0], dta[3]); - std::swap(dta[1], dta[2]); + pixels[i] = swap_endian(pixels[i]); } } @@ -1506,17 +1506,15 @@ static float* load_pfm(const char* filename, int* w, int* h, int* nc, int req) { } // proper number of channels - if (req == 0 || *nc == req) return pixels.release(); + if (req == 0 || nc == req) return pixels; // pack into channels - if (req < 0 || req > 4) { - return nullptr; - } - auto cpixels = std::unique_ptr(new float[req * npixels]); + if (req < 0 || req > 4) return {}; + auto cpixels = vector(req * npixels); for (auto i = 0ull; i < npixels; i++) { - auto vp = pixels.get() + i * (*nc); - auto cp = cpixels.get() + i * req; - if (*nc == 1) { + auto vp = pixels.data() + i * (nc); + auto cp = cpixels.data() + i * req; + if (nc == 1) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1556,7 +1554,7 @@ static float* load_pfm(const char* filename, int* w, int* h, int* nc, int req) { } } } - return cpixels.release(); + return cpixels; } // save pfm @@ -1618,10 +1616,9 @@ bool is_hdr_filename(const string& filename) { return true; } else if (ext == ".pfm" || ext == ".PFM") { auto width = 0, height = 0, ncomp = 0; - auto pixels = load_pfm(filename.c_str(), &width, &height, &ncomp, 4); - if (pixels == nullptr) return read_error(); - img = image{{width, height}, (const vec4f*)pixels}; - delete[] pixels; + auto pixels = load_pfm(filename.c_str(), width, height, ncomp, 4); + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else if (ext == ".hdr" || ext == ".HDR") { auto width = 0, height = 0, ncomp = 0; From 762c0c5ccb16b2c52b4f8084b6bdb04bbfc4baf2 Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 22:36:23 +0200 Subject: [PATCH 3/9] updated --- libs/yocto/yocto_image.cpp | 57 ++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index d13b63302..bbf35e847 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1802,46 +1802,44 @@ bool save_image(const string& filename, const image& imgf, namespace yocto { // Volume load -static float* load_yvol( - const char* filename, int* w, int* h, int* d, int* nc, int req) { +static vector load_yvol( + const char* filename, int& w, int& h, int& d, int& nc, int req) { auto fs = open_file(filename, "rb"); - if (!fs) return nullptr; + if (!fs) return {}; // buffer auto buffer = array{}; auto toks = vector(); // read magic - if (!read_line(fs, buffer)) return nullptr; + if (!read_line(fs, buffer)) return {}; toks = split_string(buffer.data()); - if (toks[0] != "YVOL") return nullptr; + if (toks[0] != "YVOL") return {}; // read w, h - if (!read_line(fs, buffer)) return nullptr; + if (!read_line(fs, buffer)) return {}; toks = split_string(buffer.data()); - *w = atoi(toks[0].c_str()); - *h = atoi(toks[1].c_str()); - *d = atoi(toks[2].c_str()); - *nc = atoi(toks[3].c_str()); + w = atoi(toks[0].c_str()); + h = atoi(toks[1].c_str()); + d = atoi(toks[2].c_str()); + nc = atoi(toks[3].c_str()); // read data - auto nvoxels = (size_t)(*w) * (size_t)(*h) * (size_t)(*d); - auto nvalues = nvoxels * (size_t)(*nc); - auto voxels = std::unique_ptr(new float[nvalues]); - if (!read_values(fs, voxels.get(), nvalues)) return nullptr; + auto nvoxels = (size_t)(w) * (size_t)(h) * (size_t)(d); + auto nvalues = nvoxels * (size_t)(nc); + auto voxels = vector(nvalues); + if (!read_values(fs, voxels.data(), nvalues)) return {}; // proper number of channels - if (req == 0 || *nc == req) return voxels.release(); + if (req == 0 || nc == req) return voxels; // pack into channels - if (req < 0 || req > 4) { - return nullptr; - } - auto cvoxels = std::unique_ptr(new float[req * nvoxels]); + if (req < 0 || req > 4) return {}; + auto cvoxels = vector(req * nvoxels); for (auto i = 0; i < nvoxels; i++) { - auto vp = voxels.get() + i * (*nc); - auto cp = cvoxels.get() + i * req; - if (*nc == 1) { + auto vp = voxels.data() + i * nc; + auto cp = cvoxels.data() + i * req; + if (nc == 1) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1860,7 +1858,7 @@ static float* load_yvol( cp[3] = 1; break; } - } else if (*nc == 2) { + } else if (nc == 2) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1876,7 +1874,7 @@ static float* load_yvol( cp[1] = vp[1]; break; } - } else if (*nc == 3) { + } else if (nc == 3) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1895,7 +1893,7 @@ static float* load_yvol( cp[3] = 1; break; } - } else if (*nc == 4) { + } else if (nc == 4) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1916,7 +1914,7 @@ static float* load_yvol( } } } - return cvoxels.release(); + return cvoxels; } // save pfm @@ -1941,10 +1939,9 @@ bool load_volume(const string& filename, volume& vol, string& error) { return false; }; auto width = 0, height = 0, depth = 0, ncomp = 0; - auto voxels = load_yvol(filename.c_str(), &width, &height, &depth, &ncomp, 1); - if (!voxels) return read_error(); - vol = volume{{width, height, depth}, (const float*)voxels}; - delete[] voxels; + auto voxels = load_yvol(filename.c_str(), width, height, depth, ncomp, 1); + if (voxels.empty()) return read_error(); + vol = volume{{width, height, depth}, (const float*)voxels.data()}; return true; } From a03444e4487360595eb1d2924aad0ea74d49f868 Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 23:06:46 +0200 Subject: [PATCH 4/9] updated --- libs/yocto/yocto_image.cpp | 83 +++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index bbf35e847..7ff93cdf7 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1452,34 +1452,48 @@ static vector split_string(const string& str) { } // Pfm load -static vector load_pfm( - const char* filename, int& w, int& h, int& nc, int req) { +static bool load_pfm(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + auto parse_error = [filename, &error]() { + error = filename + ": parse error"; + return false; + }; + auto read_error = [filename, &error]() { + error = filename + ": read error"; + return false; + }; + auto fs = open_file(filename, "rb"); - if (!fs) return {}; + if (!fs) return open_error(); // buffer auto buffer = array{}; auto toks = vector(); // read magic - if (!read_line(fs, buffer)) return {}; + if (!read_line(fs, buffer)) return read_error(); toks = split_string(buffer.data()); if (toks[0] == "Pf") { nc = 1; } else if (toks[0] == "PF") { nc = 3; } else { - return {}; + return parse_error(); } // read w, h - if (!read_line(fs, buffer)) return {}; + if (!read_line(fs, buffer)) return read_error(); toks = split_string(buffer.data()); w = atoi(toks[0].c_str()); h = atoi(toks[1].c_str()); // read scale - if (!read_line(fs, buffer)) return {}; + if (!read_line(fs, buffer)) return read_error(); toks = split_string(buffer.data()); auto s = atof(toks[0].c_str()); @@ -1487,7 +1501,7 @@ static vector load_pfm( auto npixels = (size_t)(w) * (size_t)(h); auto nvalues = npixels * (size_t)(nc); auto nrow = (size_t)(w) * (size_t)(nc); - auto pixels = vector(nvalues); + pixels = vector(nvalues); for (auto j = h - 1; j >= 0; j--) { if (!read_values(fs, pixels.data() + j * nrow, nrow)) return {}; } @@ -1506,7 +1520,7 @@ static vector load_pfm( } // proper number of channels - if (req == 0 || nc == req) return pixels; + if (req == 0 || nc == req) return true; // pack into channels if (req < 0 || req > 4) return {}; @@ -1554,31 +1568,43 @@ static vector load_pfm( } } } - return cpixels; + + swap(pixels, cpixels); + return true; } // save pfm -static bool save_pfm( - const char* filename, int w, int h, int nc, const float* pixels) { +static bool save_pfm(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + auto fs = open_file(filename, "wb"); - if (!fs) return false; + if (!fs) return open_error(); - if (!write_text(fs, (nc == 1) ? "Pf\n"s : "PF\n"s)) return false; + if (!write_text(fs, (nc == 1) ? "Pf\n"s : "PF\n"s)) return write_error(); if (!write_text(fs, std::to_string(w) + " " + std::to_string(h) + "\n")) return false; - if (!write_text(fs, "-1\n")) return false; + if (!write_text(fs, "-1\n")) return write_error(); if (nc == 1 || nc == 3) { - if (!write_values(fs, pixels, w * h * nc)) return false; + if (!write_values(fs, pixels.data(), pixels.size())) return write_error(); } else { for (auto i = 0; i < w * h; i++) { auto vz = 0.0f; - auto v = pixels + i * nc; - if (!write_value(fs, v + 0)) return false; - if (!write_value(fs, v + 1)) return false; + auto v = pixels.data() + i * nc; + if (!write_value(fs, v + 0)) return write_error(); + if (!write_value(fs, v + 1)) return write_error(); if (nc == 2) { - if (!write_value(fs, &vz)) return false; + if (!write_value(fs, &vz)) return write_error(); } else { - if (!write_value(fs, v + 2)) return false; + if (!write_value(fs, v + 2)) return write_error(); } } } @@ -1616,7 +1642,9 @@ bool is_hdr_filename(const string& filename) { return true; } else if (ext == ".pfm" || ext == ".PFM") { auto width = 0, height = 0, ncomp = 0; - auto pixels = load_pfm(filename.c_str(), width, height, ncomp, 4); + auto pixels = vector{}; + if (!load_pfm(filename, width, height, ncomp, pixels, error, 4)) + return false; if (pixels.empty()) return read_error(); img = image{{width, height}, (const vec4f*)pixels.data()}; return true; @@ -1651,15 +1679,14 @@ bool is_hdr_filename(const string& filename) { auto ext = path_extension(filename); if (ext == ".hdr" || ext == ".HDR") { - if (!stbi_write_hdr( - filename.c_str(), img.width(), img.height(), 4, (float*)img.data())) + if (!stbi_write_hdr(filename.c_str(), img.width(), img.height(), 4, + (const float*)img.data())) return write_error(); return true; } else if (ext == ".pfm" || ext == ".PFM") { - if (!save_pfm( - filename.c_str(), img.width(), img.height(), 4, (float*)img.data())) - return write_error(); - return true; + return save_pfm(filename, img.width(), img.height(), 4, + {(const float*)img.data(), (const float*)img.data() + img.count() * 4}, + error); } else if (ext == ".exr" || ext == ".EXR") { if (SaveEXR((float*)img.data(), img.width(), img.height(), 4, 1, filename.c_str(), nullptr) < 0) From 636501834da0f872e389e7ad239fe1121998996a Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 23:33:01 +0200 Subject: [PATCH 5/9] updated --- libs/yocto/yocto_image.cpp | 291 ++++++++++++++++++++++++++++++++----- 1 file changed, 252 insertions(+), 39 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index 7ff93cdf7..e3c32b7b5 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1612,6 +1612,198 @@ static bool save_pfm(const string& filename, int w, int h, int nc, return true; } +// Png load +static bool load_png(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + if (!pixels_ptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save png +static bool save_png(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (!stbi_write_png(filename.c_str(), w, h, nc, pixels.data(), w * nc)) + return write_error(); + return true; +} + +// jpg load +static bool load_jpg(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + if (!pixels_ptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save jpg +static bool save_jpg(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (!stbi_write_jpg(filename.c_str(), w, h, nc, pixels.data(), 75)) + return write_error(); + return true; +} + +// tga load +static bool load_tga(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + if (!pixels_ptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save tga +static bool save_tga(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (!stbi_write_tga(filename.c_str(), w, h, nc, pixels.data())) + return write_error(); + return true; +} + +// jpg load +static bool load_bmp(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + if (!pixels_ptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save jpg +static bool save_bmp(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (!stbi_write_bmp(filename.c_str(), w, h, nc, pixels.data())) + return write_error(); + return true; +} + +// hdr load +static bool load_hdr(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + auto pixels_ptr = stbi_loadf(filename.c_str(), &w, &h, &nc, req); + if (!pixels_ptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save hdr +static bool save_hdr(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (!stbi_write_hdr(filename.c_str(), w, h, nc, pixels.data())) + return write_error(); + return true; +} + +// exr load +static bool load_exr(const string& filename, int& w, int& h, int& nc, + vector& pixels, string& error, int req) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + + if (req != 4) throw std::invalid_argument{"not supported yet"}; + + auto pixels_ptr = (float*)nullptr; + if (LoadEXR(&pixels_ptr, &w, &h, filename.c_str(), nullptr) != 0) + return open_error(); + if (pixels_ptr == nullptr) return open_error(); + pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * + (req == 0 ? (size_t)nc : (size_t)req)}; + free(pixels_ptr); + return true; +} + +// save exr +static bool save_exr(const string& filename, int w, int h, int nc, + const vector& pixels, string& error) { + // error helpers + auto write_error = [filename, &error]() { + error = filename + ": write error"; + return false; + }; + + if (nc != 4) throw std::invalid_argument{"not supported yet"}; + + if (SaveEXR(pixels.data(), w, h, nc, 1, filename.c_str(), nullptr) < 0) + return write_error(); + return true; +} + // Check if an image is HDR based on filename. bool is_hdr_filename(const string& filename) { auto ext = path_extension(filename); @@ -1650,15 +1842,19 @@ bool is_hdr_filename(const string& filename) { return true; } else if (ext == ".hdr" || ext == ".HDR") { auto width = 0, height = 0, ncomp = 0; - auto pixels = stbi_loadf(filename.c_str(), &width, &height, &ncomp, 4); - if (pixels == nullptr) return read_error(); - img = image{{width, height}, (const vec4f*)pixels}; - free(pixels); + auto pixels = vector{}; + if (!load_hdr(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else if (!is_hdr_filename(filename)) { - auto imgb = image{}; - if (!load_image(filename, imgb, error)) return false; - img = srgb_to_rgb(imgb); + auto width = 0, height = 0, ncomp = 0; + auto pixels = vector{}; + if (!load_exr(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else { return format_error(); @@ -1679,19 +1875,17 @@ bool is_hdr_filename(const string& filename) { auto ext = path_extension(filename); if (ext == ".hdr" || ext == ".HDR") { - if (!stbi_write_hdr(filename.c_str(), img.width(), img.height(), 4, - (const float*)img.data())) - return write_error(); - return true; + return save_hdr(filename, img.width(), img.height(), 4, + {(const float*)img.data(), (const float*)img.data() + img.count() * 4}, + error); } else if (ext == ".pfm" || ext == ".PFM") { return save_pfm(filename, img.width(), img.height(), 4, {(const float*)img.data(), (const float*)img.data() + img.count() * 4}, error); } else if (ext == ".exr" || ext == ".EXR") { - if (SaveEXR((float*)img.data(), img.width(), img.height(), 4, 1, - filename.c_str(), nullptr) < 0) - return write_error(); - return true; + return save_exr(filename, img.width(), img.height(), 4, + {(const float*)img.data(), (const float*)img.data() + img.count() * 4}, + error); } else if (!is_hdr_filename(filename)) { return save_image(filename, rgb_to_srgbb(img), error); } else { @@ -1712,13 +1906,37 @@ bool is_hdr_filename(const string& filename) { }; auto ext = path_extension(filename); - if (ext == ".png" || ext == ".PNG" || ext == ".jpg" || ext == ".JPG" || - ext == ".tga" || ext == ".TGA" || ext == ".bmp" || ext == ".BMP") { + if (ext == ".png" || ext == ".PNG") { auto width = 0, height = 0, ncomp = 0; - auto pixels = stbi_load(filename.c_str(), &width, &height, &ncomp, 4); - if (!pixels) return read_error(); - img = image{{width, height}, (const vec4b*)pixels}; - free(pixels); + auto pixels = vector{}; + if (!load_png(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4b*)pixels.data()}; + return true; + } else if (ext == ".jpg" || ext == ".JPG") { + auto width = 0, height = 0, ncomp = 0; + auto pixels = vector{}; + if (!load_jpg(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4b*)pixels.data()}; + return true; + } else if (ext == ".tga" || ext == ".TGA") { + auto width = 0, height = 0, ncomp = 0; + auto pixels = vector{}; + if (!load_tga(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4b*)pixels.data()}; + return true; + } else if (ext == ".bmp" || ext == ".BMP") { + auto width = 0, height = 0, ncomp = 0; + auto pixels = vector{}; + if (!load_bmp(filename, width, height, ncomp, pixels, error, 4)) + return false; + if (pixels.empty()) return read_error(); + img = image{{width, height}, (const vec4b*)pixels.data()}; return true; } else if (is_hdr_filename(filename)) { auto imgf = image{}; @@ -1744,25 +1962,21 @@ bool is_hdr_filename(const string& filename) { auto ext = path_extension(filename); if (ext == ".png" || ext == ".PNG") { - if (!stbi_write_png(filename.c_str(), img.width(), img.height(), 4, - img.data(), img.width() * 4)) - return write_error(); - return true; + return save_png(filename, img.width(), img.height(), 4, + {(const byte*)img.data(), (const byte*)img.data() + img.count() * 4}, + error); } else if (ext == ".jpg" || ext == ".JPG") { - if (!stbi_write_jpg( - filename.c_str(), img.width(), img.height(), 4, img.data(), 75)) - return write_error(); - return true; + return save_jpg(filename, img.width(), img.height(), 4, + {(const byte*)img.data(), (const byte*)img.data() + img.count() * 4}, + error); } else if (ext == ".tga" || ext == ".TGA") { - if (!stbi_write_tga( - filename.c_str(), img.width(), img.height(), 4, img.data())) - return write_error(); - return true; + return save_tga(filename, img.width(), img.height(), 4, + {(const byte*)img.data(), (const byte*)img.data() + img.count() * 4}, + error); } else if (ext == ".bmp" || ext == ".BMP") { - if (!stbi_write_bmp( - filename.c_str(), img.width(), img.height(), 4, img.data())) - return write_error(); - return true; + return save_bmp(filename, img.width(), img.height(), 4, + {(const byte*)img.data(), (const byte*)img.data() + img.count() * 4}, + error); } else if (is_hdr_filename(filename)) { return save_image(filename, srgb_to_rgb(img), error); } else { @@ -1783,8 +1997,7 @@ bool is_hdr_filename(const string& filename) { }; auto ext = path_extension(filename); - if (ext == ".png" || ext == ".PNG" || ext == ".jpg" || ext == ".JPG" || - ext == ".tga" || ext == ".TGA" || ext == ".bmp" || ext == ".BMP") { + if (ext == ".png" || ext == ".PNG") { auto width = 0, height = 0, ncomp = 0; auto pixels = stbi_load_16(filename.c_str(), &width, &height, &ncomp, 4); if (pixels == nullptr) return read_error(); From a4ab6b667379a253a71f95ea1d98c4182de9ff3c Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 25 Aug 2020 23:40:25 +0200 Subject: [PATCH 6/9] updated --- libs/yocto/yocto_image.cpp | 207 ++++++++++++++++++++----------------- 1 file changed, 114 insertions(+), 93 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index e3c32b7b5..4ef975d86 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1452,8 +1452,8 @@ static vector split_string(const string& str) { } // Pfm load -static bool load_pfm(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_pfm(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1479,18 +1479,18 @@ static bool load_pfm(const string& filename, int& w, int& h, int& nc, if (!read_line(fs, buffer)) return read_error(); toks = split_string(buffer.data()); if (toks[0] == "Pf") { - nc = 1; + components = 1; } else if (toks[0] == "PF") { - nc = 3; + components = 3; } else { return parse_error(); } - // read w, h + // read width, height if (!read_line(fs, buffer)) return read_error(); - toks = split_string(buffer.data()); - w = atoi(toks[0].c_str()); - h = atoi(toks[1].c_str()); + toks = split_string(buffer.data()); + width = atoi(toks[0].c_str()); + height = atoi(toks[1].c_str()); // read scale if (!read_line(fs, buffer)) return read_error(); @@ -1498,11 +1498,11 @@ static bool load_pfm(const string& filename, int& w, int& h, int& nc, auto s = atof(toks[0].c_str()); // read the data (flip y) - auto npixels = (size_t)(w) * (size_t)(h); - auto nvalues = npixels * (size_t)(nc); - auto nrow = (size_t)(w) * (size_t)(nc); + auto npixels = (size_t)width * (size_t)height; + auto nvalues = npixels * (size_t)components; + auto nrow = (size_t)width * (size_t)components; pixels = vector(nvalues); - for (auto j = h - 1; j >= 0; j--) { + for (auto j = height - 1; j >= 0; j--) { if (!read_values(fs, pixels.data() + j * nrow, nrow)) return {}; } @@ -1520,15 +1520,15 @@ static bool load_pfm(const string& filename, int& w, int& h, int& nc, } // proper number of channels - if (req == 0 || nc == req) return true; + if (req == 0 || components == req) return true; // pack into channels if (req < 0 || req > 4) return {}; auto cpixels = vector(req * npixels); for (auto i = 0ull; i < npixels; i++) { - auto vp = pixels.data() + i * (nc); + auto vp = pixels.data() + i * components; auto cp = cpixels.data() + i * req; - if (nc == 1) { + if (components == 1) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -1574,8 +1574,8 @@ static bool load_pfm(const string& filename, int& w, int& h, int& nc, } // save pfm -static bool save_pfm(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_pfm(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1589,19 +1589,21 @@ static bool save_pfm(const string& filename, int w, int h, int nc, auto fs = open_file(filename, "wb"); if (!fs) return open_error(); - if (!write_text(fs, (nc == 1) ? "Pf\n"s : "PF\n"s)) return write_error(); - if (!write_text(fs, std::to_string(w) + " " + std::to_string(h) + "\n")) + if (!write_text(fs, (components == 1) ? "Pf\n"s : "PF\n"s)) + return write_error(); + if (!write_text( + fs, std::to_string(width) + " " + std::to_string(height) + "\n")) return false; if (!write_text(fs, "-1\n")) return write_error(); - if (nc == 1 || nc == 3) { + if (components == 1 || components == 3) { if (!write_values(fs, pixels.data(), pixels.size())) return write_error(); } else { - for (auto i = 0; i < w * h; i++) { + for (auto i = 0; i < width * height; i++) { auto vz = 0.0f; - auto v = pixels.data() + i * nc; + auto v = pixels.data() + i * components; if (!write_value(fs, v + 0)) return write_error(); if (!write_value(fs, v + 1)) return write_error(); - if (nc == 2) { + if (components == 2) { if (!write_value(fs, &vz)) return write_error(); } else { if (!write_value(fs, v + 2)) return write_error(); @@ -1613,163 +1615,178 @@ static bool save_pfm(const string& filename, int w, int h, int nc, } // Png load -static bool load_png(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_png(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + auto pixels_ptr = stbi_load( + filename.c_str(), &width, &height, &components, req); if (!pixels_ptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save png -static bool save_png(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_png(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (!stbi_write_png(filename.c_str(), w, h, nc, pixels.data(), w * nc)) + if (!stbi_write_png(filename.c_str(), width, height, components, + pixels.data(), width * components)) return write_error(); return true; } // jpg load -static bool load_jpg(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_jpg(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + auto pixels_ptr = stbi_load( + filename.c_str(), &width, &height, &components, req); if (!pixels_ptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save jpg -static bool save_jpg(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_jpg(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (!stbi_write_jpg(filename.c_str(), w, h, nc, pixels.data(), 75)) + if (!stbi_write_jpg( + filename.c_str(), width, height, components, pixels.data(), 75)) return write_error(); return true; } // tga load -static bool load_tga(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_tga(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + auto pixels_ptr = stbi_load( + filename.c_str(), &width, &height, &components, req); if (!pixels_ptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save tga -static bool save_tga(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_tga(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (!stbi_write_tga(filename.c_str(), w, h, nc, pixels.data())) + if (!stbi_write_tga( + filename.c_str(), width, height, components, pixels.data())) return write_error(); return true; } // jpg load -static bool load_bmp(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_bmp(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - auto pixels_ptr = stbi_load(filename.c_str(), &w, &h, &nc, req); + auto pixels_ptr = stbi_load( + filename.c_str(), &width, &height, &components, req); if (!pixels_ptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save jpg -static bool save_bmp(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_bmp(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (!stbi_write_bmp(filename.c_str(), w, h, nc, pixels.data())) + if (!stbi_write_bmp( + filename.c_str(), width, height, components, pixels.data())) return write_error(); return true; } // hdr load -static bool load_hdr(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_hdr(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - auto pixels_ptr = stbi_loadf(filename.c_str(), &w, &h, &nc, req); + auto pixels_ptr = stbi_loadf( + filename.c_str(), &width, &height, &components, req); if (!pixels_ptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save hdr -static bool save_hdr(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_hdr(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (!stbi_write_hdr(filename.c_str(), w, h, nc, pixels.data())) + if (!stbi_write_hdr( + filename.c_str(), width, height, components, pixels.data())) return write_error(); return true; } // exr load -static bool load_exr(const string& filename, int& w, int& h, int& nc, - vector& pixels, string& error, int req) { +static bool load_exr(const string& filename, int& width, int& height, + int& components, vector& pixels, string& error, int req) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1779,27 +1796,29 @@ static bool load_exr(const string& filename, int& w, int& h, int& nc, if (req != 4) throw std::invalid_argument{"not supported yet"}; auto pixels_ptr = (float*)nullptr; - if (LoadEXR(&pixels_ptr, &w, &h, filename.c_str(), nullptr) != 0) + if (LoadEXR(&pixels_ptr, &width, &height, filename.c_str(), nullptr) != 0) return open_error(); if (pixels_ptr == nullptr) return open_error(); - pixels = {pixels_ptr, pixels_ptr + (size_t)w * (size_t)h * - (req == 0 ? (size_t)nc : (size_t)req)}; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * + (req == 0 ? (size_t)components : (size_t)req)}; free(pixels_ptr); return true; } // save exr -static bool save_exr(const string& filename, int w, int h, int nc, - const vector& pixels, string& error) { +static bool save_exr(const string& filename, int width, int height, + int components, const vector& pixels, string& error) { // error helpers auto write_error = [filename, &error]() { error = filename + ": write error"; return false; }; - if (nc != 4) throw std::invalid_argument{"not supported yet"}; + if (components != 4) throw std::invalid_argument{"not supported yet"}; - if (SaveEXR(pixels.data(), w, h, nc, 1, filename.c_str(), nullptr) < 0) + if (SaveEXR(pixels.data(), width, height, components, 1, filename.c_str(), + nullptr) < 0) return write_error(); return true; } @@ -2042,8 +2061,8 @@ bool save_image(const string& filename, const image& imgf, namespace yocto { // Volume load -static vector load_yvol( - const char* filename, int& w, int& h, int& d, int& nc, int req) { +static vector load_yvol(const char* filename, int& width, int& height, + int& depth, int& components, int req) { auto fs = open_file(filename, "rb"); if (!fs) return {}; @@ -2056,30 +2075,30 @@ static vector load_yvol( toks = split_string(buffer.data()); if (toks[0] != "YVOL") return {}; - // read w, h + // read width, height if (!read_line(fs, buffer)) return {}; - toks = split_string(buffer.data()); - w = atoi(toks[0].c_str()); - h = atoi(toks[1].c_str()); - d = atoi(toks[2].c_str()); - nc = atoi(toks[3].c_str()); + toks = split_string(buffer.data()); + width = atoi(toks[0].c_str()); + height = atoi(toks[1].c_str()); + depth = atoi(toks[2].c_str()); + components = atoi(toks[3].c_str()); // read data - auto nvoxels = (size_t)(w) * (size_t)(h) * (size_t)(d); - auto nvalues = nvoxels * (size_t)(nc); + auto nvoxels = (size_t)width * (size_t)height * (size_t)depth; + auto nvalues = nvoxels * (size_t)components; auto voxels = vector(nvalues); if (!read_values(fs, voxels.data(), nvalues)) return {}; // proper number of channels - if (req == 0 || nc == req) return voxels; + if (req == 0 || components == req) return voxels; // pack into channels if (req < 0 || req > 4) return {}; auto cvoxels = vector(req * nvoxels); for (auto i = 0; i < nvoxels; i++) { - auto vp = voxels.data() + i * nc; + auto vp = voxels.data() + i * components; auto cp = cvoxels.data() + i * req; - if (nc == 1) { + if (components == 1) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -2098,7 +2117,7 @@ static vector load_yvol( cp[3] = 1; break; } - } else if (nc == 2) { + } else if (components == 2) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -2114,7 +2133,7 @@ static vector load_yvol( cp[1] = vp[1]; break; } - } else if (nc == 3) { + } else if (components == 3) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -2133,7 +2152,7 @@ static vector load_yvol( cp[3] = 1; break; } - } else if (nc == 4) { + } else if (components == 4) { switch (req) { case 1: cp[0] = vp[0]; break; case 2: @@ -2158,16 +2177,18 @@ static vector load_yvol( } // save pfm -static bool save_yvol( - const char* filename, int w, int h, int d, int nc, const float* voxels) { +static bool save_yvol(const char* filename, int width, int height, int depth, + int components, const float* voxels) { auto fs = open_file(filename, "wb"); if (!fs) return false; if (!write_text(fs, "YVOL\n")) return false; - if (!write_text(fs, std::to_string(w) + " " + std::to_string(h) + " " + - std::to_string(d) + " " + std::to_string(nc) + "\n")) + if (!write_text(fs, std::to_string(width) + " " + std::to_string(height) + + " " + std::to_string(depth) + " " + + std::to_string(components) + "\n")) return false; - auto nvalues = (size_t)w * (size_t)h * (size_t)d * (size_t)nc; + auto nvalues = (size_t)width * (size_t)height * (size_t)depth * + (size_t)components; if (!write_values(fs, voxels, nvalues)) return false; return true; } From e6f22582b2348a94da896da949f99dee02a23a6d Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Wed, 26 Aug 2020 00:07:19 +0200 Subject: [PATCH 7/9] updated --- libs/yocto/yocto_image.cpp | 262 ++++++++++++------------------------- 1 file changed, 81 insertions(+), 181 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index 4ef975d86..76b1eea18 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1451,9 +1451,49 @@ static vector split_string(const string& str) { return ret; } +// Convert numbert of components +static vector convert_components( + const vector& pixels, int components, int components_to) { + if (components <= 0 || components > 4 || components_to <= 0 || + components_to > 4) + throw std::invalid_argument{"components not supported"}; + if (components == components_to) return pixels; + + auto cpixels = vector((size_t)components_to * pixels.size()); + for (auto i = 0ull; i < pixels.size(); i++) { + auto vp = pixels.data() + i * components; + auto cp = cpixels.data() + i * components_to; + if (components_to > 0) cp[0] = (components > 0) ? vp[0] : 0; + if (components_to > 1) cp[1] = (components > 1) ? vp[1] : 0; + if (components_to > 2) cp[2] = (components > 2) ? vp[2] : 0; + if (components_to > 3) cp[3] = (components > 3) ? vp[3] : 1; + } + return cpixels; +} + +// Convert numbert of components +static vector convert_components( + const vector& pixels, int components, int components_to) { + if (components <= 0 || components > 4 || components_to <= 0 || + components_to > 4) + throw std::invalid_argument{"components not supported"}; + if (components == components_to) return pixels; + + auto cpixels = vector((size_t)components_to * pixels.size()); + for (auto i = 0ull; i < pixels.size(); i++) { + auto vp = pixels.data() + i * components; + auto cp = cpixels.data() + i * components_to; + if (components_to > 0) cp[0] = (components > 0) ? vp[0] : 0; + if (components_to > 1) cp[1] = (components > 1) ? vp[1] : 0; + if (components_to > 2) cp[2] = (components > 2) ? vp[2] : 0; + if (components_to > 3) cp[3] = (components > 3) ? vp[3] : 255; + } + return cpixels; +} + // Pfm load static bool load_pfm(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1519,57 +1559,7 @@ static bool load_pfm(const string& filename, int& width, int& height, for (auto i = 0; i < nvalues; i++) pixels[i] *= scl; } - // proper number of channels - if (req == 0 || components == req) return true; - - // pack into channels - if (req < 0 || req > 4) return {}; - auto cpixels = vector(req * npixels); - for (auto i = 0ull; i < npixels; i++) { - auto vp = pixels.data() + i * components; - auto cp = cpixels.data() + i * req; - if (components == 1) { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[0]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[0]; - cp[2] = vp[0]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[0]; - cp[2] = vp[0]; - cp[3] = 1; - break; - } - } else { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - cp[3] = 1; - break; - } - } - } - - swap(pixels, cpixels); + // done return true; } @@ -1616,7 +1606,7 @@ static bool save_pfm(const string& filename, int width, int height, // Png load static bool load_png(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1624,11 +1614,10 @@ static bool load_png(const string& filename, int& width, int& height, }; auto pixels_ptr = stbi_load( - filename.c_str(), &width, &height, &components, req); + filename.c_str(), &width, &height, &components, 0); if (!pixels_ptr) return open_error(); pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1650,7 +1639,7 @@ static bool save_png(const string& filename, int width, int height, // jpg load static bool load_jpg(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1658,11 +1647,10 @@ static bool load_jpg(const string& filename, int& width, int& height, }; auto pixels_ptr = stbi_load( - filename.c_str(), &width, &height, &components, req); + filename.c_str(), &width, &height, &components, 0); if (!pixels_ptr) return open_error(); pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1684,7 +1672,7 @@ static bool save_jpg(const string& filename, int width, int height, // tga load static bool load_tga(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1692,11 +1680,10 @@ static bool load_tga(const string& filename, int& width, int& height, }; auto pixels_ptr = stbi_load( - filename.c_str(), &width, &height, &components, req); + filename.c_str(), &width, &height, &components, 0); if (!pixels_ptr) return open_error(); pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1718,7 +1705,7 @@ static bool save_tga(const string& filename, int width, int height, // jpg load static bool load_bmp(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1726,11 +1713,10 @@ static bool load_bmp(const string& filename, int& width, int& height, }; auto pixels_ptr = stbi_load( - filename.c_str(), &width, &height, &components, req); + filename.c_str(), &width, &height, &components, 0); if (!pixels_ptr) return open_error(); pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1752,7 +1738,7 @@ static bool save_bmp(const string& filename, int width, int height, // hdr load static bool load_hdr(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1760,11 +1746,10 @@ static bool load_hdr(const string& filename, int& width, int& height, }; auto pixels_ptr = stbi_loadf( - filename.c_str(), &width, &height, &components, req); + filename.c_str(), &width, &height, &components, 0); if (!pixels_ptr) return open_error(); pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1786,22 +1771,20 @@ static bool save_hdr(const string& filename, int width, int height, // exr load static bool load_exr(const string& filename, int& width, int& height, - int& components, vector& pixels, string& error, int req) { + int& components, vector& pixels, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; return false; }; - if (req != 4) throw std::invalid_argument{"not supported yet"}; - auto pixels_ptr = (float*)nullptr; if (LoadEXR(&pixels_ptr, &width, &height, filename.c_str(), nullptr) != 0) return open_error(); if (pixels_ptr == nullptr) return open_error(); - pixels = {pixels_ptr, - pixels_ptr + (size_t)width * (size_t)height * - (req == 0 ? (size_t)components : (size_t)req)}; + components = 4; + pixels = {pixels_ptr, + pixels_ptr + (size_t)width * (size_t)height * (size_t)components}; free(pixels_ptr); return true; } @@ -1854,25 +1837,25 @@ bool is_hdr_filename(const string& filename) { } else if (ext == ".pfm" || ext == ".PFM") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_pfm(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_pfm(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else if (ext == ".hdr" || ext == ".HDR") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_hdr(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_hdr(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else if (!is_hdr_filename(filename)) { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_exr(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_exr(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4f*)pixels.data()}; return true; } else { @@ -1928,33 +1911,33 @@ bool is_hdr_filename(const string& filename) { if (ext == ".png" || ext == ".PNG") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_png(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_png(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4b*)pixels.data()}; return true; } else if (ext == ".jpg" || ext == ".JPG") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_jpg(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_jpg(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4b*)pixels.data()}; return true; } else if (ext == ".tga" || ext == ".TGA") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_tga(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_tga(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4b*)pixels.data()}; return true; } else if (ext == ".bmp" || ext == ".BMP") { auto width = 0, height = 0, ncomp = 0; auto pixels = vector{}; - if (!load_bmp(filename, width, height, ncomp, pixels, error, 4)) - return false; + if (!load_bmp(filename, width, height, ncomp, pixels, error)) return false; if (pixels.empty()) return read_error(); + if (ncomp != 4) pixels = convert_components(pixels, ncomp, 4); img = image{{width, height}, (const vec4b*)pixels.data()}; return true; } else if (is_hdr_filename(filename)) { @@ -2062,7 +2045,7 @@ namespace yocto { // Volume load static vector load_yvol(const char* filename, int& width, int& height, - int& depth, int& components, int req) { + int& depth, int& components) { auto fs = open_file(filename, "rb"); if (!fs) return {}; @@ -2089,91 +2072,8 @@ static vector load_yvol(const char* filename, int& width, int& height, auto voxels = vector(nvalues); if (!read_values(fs, voxels.data(), nvalues)) return {}; - // proper number of channels - if (req == 0 || components == req) return voxels; - - // pack into channels - if (req < 0 || req > 4) return {}; - auto cvoxels = vector(req * nvoxels); - for (auto i = 0; i < nvoxels; i++) { - auto vp = voxels.data() + i * components; - auto cp = cvoxels.data() + i * req; - if (components == 1) { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[0]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[0]; - cp[2] = vp[0]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[0]; - cp[2] = vp[0]; - cp[3] = 1; - break; - } - } else if (components == 2) { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - } - } else if (components == 3) { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - cp[3] = 1; - break; - } - } else if (components == 4) { - switch (req) { - case 1: cp[0] = vp[0]; break; - case 2: - cp[0] = vp[0]; - cp[1] = vp[1]; - break; - case 3: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - break; - case 4: - cp[0] = vp[0]; - cp[1] = vp[1]; - cp[2] = vp[2]; - cp[3] = vp[3]; - break; - } - } - } - return cvoxels; + // done + return voxels; } // save pfm @@ -2189,8 +2089,7 @@ static bool save_yvol(const char* filename, int width, int height, int depth, return false; auto nvalues = (size_t)width * (size_t)height * (size_t)depth * (size_t)components; - if (!write_values(fs, voxels, nvalues)) return false; - return true; + return write_values(fs, voxels, nvalues); } // Loads volume data from binary format. @@ -2200,8 +2099,9 @@ bool load_volume(const string& filename, volume& vol, string& error) { return false; }; auto width = 0, height = 0, depth = 0, ncomp = 0; - auto voxels = load_yvol(filename.c_str(), width, height, depth, ncomp, 1); + auto voxels = load_yvol(filename.c_str(), width, height, depth, ncomp); if (voxels.empty()) return read_error(); + if (ncomp != 1) voxels = convert_components(voxels, ncomp, 1); vol = volume{{width, height, depth}, (const float*)voxels.data()}; return true; } From 02f2ce6aedd13394db95a43dc7e2c3b1f36224ae Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Wed, 26 Aug 2020 00:13:56 +0200 Subject: [PATCH 8/9] updated --- libs/yocto/yocto_image.cpp | 70 ++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index 76b1eea18..ba63b9d7a 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -2044,22 +2044,36 @@ bool save_image(const string& filename, const image& imgf, namespace yocto { // Volume load -static vector load_yvol(const char* filename, int& width, int& height, - int& depth, int& components) { +static bool load_yvol(const string& filename, int& width, int& height, + int& depth, int& components, vector& voxels, string& error) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + auto parse_error = [filename, &error]() { + error = filename + ": parse error"; + return false; + }; + auto read_error = [filename, &error]() { + error = filename + ": read error"; + return false; + }; + auto fs = open_file(filename, "rb"); - if (!fs) return {}; + if (!fs) return open_error(); // buffer auto buffer = array{}; auto toks = vector(); // read magic - if (!read_line(fs, buffer)) return {}; + if (!read_line(fs, buffer)) return parse_error(); toks = split_string(buffer.data()); - if (toks[0] != "YVOL") return {}; + if (toks[0] != "YVOL") return parse_error(); // read width, height - if (!read_line(fs, buffer)) return {}; + if (!read_line(fs, buffer)) return parse_error(); toks = split_string(buffer.data()); width = atoi(toks[0].c_str()); height = atoi(toks[1].c_str()); @@ -2069,27 +2083,38 @@ static vector load_yvol(const char* filename, int& width, int& height, // read data auto nvoxels = (size_t)width * (size_t)height * (size_t)depth; auto nvalues = nvoxels * (size_t)components; - auto voxels = vector(nvalues); - if (!read_values(fs, voxels.data(), nvalues)) return {}; + voxels = vector(nvalues); + if (!read_values(fs, voxels.data(), nvalues)) return read_error(); // done - return voxels; + return true; } // save pfm -static bool save_yvol(const char* filename, int width, int height, int depth, - int components, const float* voxels) { +static bool save_yvol(const string& filename, int width, int height, int depth, + int components, const vector& voxels, string& error) { + // error helpers + auto open_error = [filename, &error]() { + error = filename + ": file not found"; + return false; + }; + auto write_error = [filename, &error]() { + error = filename + ": read error"; + return false; + }; + auto fs = open_file(filename, "wb"); - if (!fs) return false; + if (!fs) return open_error(); - if (!write_text(fs, "YVOL\n")) return false; + if (!write_text(fs, "YVOL\n")) return write_error(); if (!write_text(fs, std::to_string(width) + " " + std::to_string(height) + " " + std::to_string(depth) + " " + std::to_string(components) + "\n")) - return false; + return write_error(); auto nvalues = (size_t)width * (size_t)height * (size_t)depth * (size_t)components; - return write_values(fs, voxels, nvalues); + if (!write_values(fs, voxels.data(), nvalues)) return write_error(); + return true; } // Loads volume data from binary format. @@ -2099,8 +2124,9 @@ bool load_volume(const string& filename, volume& vol, string& error) { return false; }; auto width = 0, height = 0, depth = 0, ncomp = 0; - auto voxels = load_yvol(filename.c_str(), width, height, depth, ncomp); - if (voxels.empty()) return read_error(); + auto voxels = vector{}; + if (!load_yvol(filename, width, height, depth, ncomp, voxels, error)) + return false; if (ncomp != 1) voxels = convert_components(voxels, ncomp, 1); vol = volume{{width, height, depth}, (const float*)voxels.data()}; return true; @@ -2109,14 +2135,8 @@ bool load_volume(const string& filename, volume& vol, string& error) { // Saves volume data in binary format. bool save_volume( const string& filename, const volume& vol, string& error) { - auto write_error = [filename, &error]() { - error = filename + ": write error"; - return false; - }; - if (!save_yvol(filename.c_str(), vol.width(), vol.height(), vol.depth(), 1, - vol.data())) - return write_error(); - return true; + return save_yvol(filename, vol.width(), vol.height(), vol.depth(), 1, + {vol.data(), vol.data() + vol.count()}, error); } } // namespace yocto From 28244d07995d0481688ddf63c0c84e947261302f Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Wed, 26 Aug 2020 00:15:24 +0200 Subject: [PATCH 9/9] updated --- libs/yocto/yocto_image.cpp | 13 ++-- libs/yocto/yocto_mesh.cpp | 29 ++++----- libs/yocto/yocto_mesh.h | 22 +++---- libs/yocto/yocto_modelio.cpp | 117 +++++++++++++++-------------------- libs/yocto/yocto_shape.cpp | 4 +- 5 files changed, 81 insertions(+), 104 deletions(-) diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index ba63b9d7a..f3feccb5a 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -1813,8 +1813,7 @@ bool is_hdr_filename(const string& filename) { } // Loads an hdr image. -[[nodiscard]] bool load_image( - const string& filename, image& img, string& error) { +bool load_image(const string& filename, image& img, string& error) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; return false; @@ -1864,7 +1863,7 @@ bool is_hdr_filename(const string& filename) { } // Saves an hdr image. -[[nodiscard]] bool save_image( +bool save_image( const string& filename, const image& img, string& error) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; @@ -1896,8 +1895,7 @@ bool is_hdr_filename(const string& filename) { } // Loads an ldr image. -[[nodiscard]] bool load_image( - const string& filename, image& img, string& error) { +bool load_image(const string& filename, image& img, string& error) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; return false; @@ -1951,7 +1949,7 @@ bool is_hdr_filename(const string& filename) { } // Saves an ldr image. -[[nodiscard]] bool save_image( +bool save_image( const string& filename, const image& img, string& error) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; @@ -1987,8 +1985,7 @@ bool is_hdr_filename(const string& filename) { } // Loads a 16 bit image. -[[nodiscard]] bool load_image( - const string& filename, image& img, string& error) { +bool load_image(const string& filename, image& img, string& error) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; return false; diff --git a/libs/yocto/yocto_mesh.cpp b/libs/yocto/yocto_mesh.cpp index 59070856e..879b1eb8d 100644 --- a/libs/yocto/yocto_mesh.cpp +++ b/libs/yocto/yocto_mesh.cpp @@ -1777,8 +1777,7 @@ vector strip_ascending_distance_field(const geodesic_solver& solver, return result; } -[[nodiscard]] bool check_strip( - const vector& adjacencies, const vector& strip) { +bool check_strip(const vector& adjacencies, const vector& strip) { auto faces = std::unordered_set{}; faces.insert(strip[0]); for (auto i = 1; i < strip.size(); ++i) { @@ -2212,7 +2211,7 @@ static float intersect_segments(const vec2f& start1, const vec2f& end1, return (a.x * d.y - a.y * d.x) / det; } -[[nodiscard]] bool path_check_strip( +bool path_check_strip( const vector& adjacencies, const vector& strip) { auto faces = unordered_set{}; faces.insert(strip[0]); @@ -2358,7 +2357,7 @@ static vector funnel( return lerps; } -[[nodiscard]] bool check_point(const mesh_point& point) { +bool check_point(const mesh_point& point) { assert(point.face != -1); assert(point.uv.x >= 0); assert(point.uv.y >= 0); @@ -2533,7 +2532,7 @@ mesh_point eval_path_point(const geodesic_path& path, namespace yocto { // Load ply mesh -[[nodiscard]] bool load_mesh(const string& filename, vector& triangles, +bool load_mesh(const string& filename, vector& triangles, vector& positions, vector& normals, vector& texcoords, vector& colors, string& error, bool flip_texcoord) { auto format_error = [filename, &error]() { @@ -2600,11 +2599,10 @@ namespace yocto { } // Save ply mesh -[[nodiscard]] bool save_mesh(const string& filename, - const vector& triangles, const vector& positions, - const vector& normals, const vector& texcoords, - const vector& colors, string& error, bool ascii, - bool flip_texcoord) { +bool save_mesh(const string& filename, const vector& triangles, + const vector& positions, const vector& normals, + const vector& texcoords, const vector& colors, string& error, + bool ascii, bool flip_texcoord) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; return false; @@ -2645,7 +2643,7 @@ namespace yocto { } // Load ply mesh -[[nodiscard]] bool load_lines(const string& filename, vector& lines, +bool load_lines(const string& filename, vector& lines, vector& positions, vector& normals, vector& texcoords, vector& colors, string& error, bool flip_texcoord) { auto format_error = [filename, &error]() { @@ -2712,11 +2710,10 @@ namespace yocto { } // Save ply mesh -[[nodiscard]] bool save_lines(const string& filename, - const vector& lines, const vector& positions, - const vector& normals, const vector& texcoords, - const vector& colors, string& error, bool ascii, - bool flip_texcoord) { +bool save_lines(const string& filename, const vector& lines, + const vector& positions, const vector& normals, + const vector& texcoords, const vector& colors, string& error, + bool ascii, bool flip_texcoord) { auto format_error = [filename, &error]() { error = filename + ": unknown format"; return false; diff --git a/libs/yocto/yocto_mesh.h b/libs/yocto/yocto_mesh.h index eba0e2844..3bdcafd60 100644 --- a/libs/yocto/yocto_mesh.h +++ b/libs/yocto/yocto_mesh.h @@ -354,24 +354,22 @@ void meandering_triangles(const vector& field, float isoline, namespace yocto { // Load/save a shape as indexed meshes -[[nodiscard]] bool load_mesh(const string& filename, vector& triangles, +bool load_mesh(const string& filename, vector& triangles, vector& positions, vector& normals, vector& texcoords, vector& colors, string& error, bool flip_texcoords = true); -[[nodiscard]] bool save_mesh(const string& filename, - const vector& triangles, const vector& positions, - const vector& normals, const vector& texcoords, - const vector& colors, string& error, bool ascii = false, - bool flip_texcoords = true); +bool save_mesh(const string& filename, const vector& triangles, + const vector& positions, const vector& normals, + const vector& texcoords, const vector& colors, string& error, + bool ascii = false, bool flip_texcoords = true); // Load/save a set of lines -[[nodiscard]] bool load_lines(const string& filename, vector& lines, +bool load_lines(const string& filename, vector& lines, vector& positions, vector& normals, vector& texcoords, vector& colors, string& error, bool flip_texcoords = true); -[[nodiscard]] bool save_lines(const string& filename, - const vector& lines, const vector& positions, - const vector& normals, const vector& texcoords, - const vector& colors, string& error, bool ascii = false, - bool flip_texcoords = true); +bool save_lines(const string& filename, const vector& lines, + const vector& positions, const vector& normals, + const vector& texcoords, const vector& colors, string& error, + bool ascii = false, bool flip_texcoords = true); } // namespace yocto diff --git a/libs/yocto/yocto_modelio.cpp b/libs/yocto/yocto_modelio.cpp index 522cfea38..b216770ed 100644 --- a/libs/yocto/yocto_modelio.cpp +++ b/libs/yocto/yocto_modelio.cpp @@ -106,7 +106,7 @@ inline void remove_comment(string_view& str, char comment_char = '#') { } // Parse values from a string -[[nodiscard]] inline bool parse_value(string_view& str, string_view& value) { +inline bool parse_value(string_view& str, string_view& value) { skip_whitespace(str); if (str.empty()) return false; if (str.front() != '"') { @@ -129,76 +129,76 @@ inline void remove_comment(string_view& str, char comment_char = '#') { } return true; } -[[nodiscard]] inline bool parse_value(string_view& str, string& value) { +inline bool parse_value(string_view& str, string& value) { auto valuev = string_view{}; if (!parse_value(str, valuev)) return false; value = string{valuev}; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, int8_t& value) { +inline bool parse_value(string_view& str, int8_t& value) { char* end = nullptr; value = (int8_t)strtol(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, int16_t& value) { +inline bool parse_value(string_view& str, int16_t& value) { char* end = nullptr; value = (int16_t)strtol(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, int32_t& value) { +inline bool parse_value(string_view& str, int32_t& value) { char* end = nullptr; value = (int32_t)strtol(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, int64_t& value) { +inline bool parse_value(string_view& str, int64_t& value) { char* end = nullptr; value = (int64_t)strtoll(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, uint8_t& value) { +inline bool parse_value(string_view& str, uint8_t& value) { char* end = nullptr; value = (uint8_t)strtoul(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, uint16_t& value) { +inline bool parse_value(string_view& str, uint16_t& value) { char* end = nullptr; value = (uint16_t)strtoul(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, uint32_t& value) { +inline bool parse_value(string_view& str, uint32_t& value) { char* end = nullptr; value = (uint32_t)strtoul(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, uint64_t& value) { +inline bool parse_value(string_view& str, uint64_t& value) { char* end = nullptr; value = (uint64_t)strtoull(str.data(), &end, 10); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, float& value) { +inline bool parse_value(string_view& str, float& value) { char* end = nullptr; value = strtof(str.data(), &end); if (str.data() == end) return false; str.remove_prefix(end - str.data()); return true; } -[[nodiscard]] inline bool parse_value(string_view& str, double& value) { +inline bool parse_value(string_view& str, double& value) { char* end = nullptr; value = strtod(str.data(), &end); if (str.data() == end) return false; @@ -206,7 +206,7 @@ inline void remove_comment(string_view& str, char comment_char = '#') { return true; } #ifdef __APPLE__ -[[nodiscard]] inline bool parse_value(string_view& str, size_t& value) { +inline bool parse_value(string_view& str, size_t& value) { char* end = nullptr; value = (size_t)strtoull(str.data(), &end, 10); if (str.data() == end) return false; @@ -214,34 +214,34 @@ inline void remove_comment(string_view& str, char comment_char = '#') { return true; } #endif -[[nodiscard]] inline bool parse_value(string_view& str, bool& value) { +inline bool parse_value(string_view& str, bool& value) { auto valuei = 0; if (!parse_value(str, valuei)) return false; value = (bool)valuei; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, vec2f& value) { +inline bool parse_value(string_view& str, vec2f& value) { for (auto i = 0; i < 2; i++) if (!parse_value(str, value[i])) return false; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, vec3f& value) { +inline bool parse_value(string_view& str, vec3f& value) { for (auto i = 0; i < 3; i++) if (!parse_value(str, value[i])) return false; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, vec4f& value) { +inline bool parse_value(string_view& str, vec4f& value) { for (auto i = 0; i < 4; i++) if (!parse_value(str, value[i])) return false; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, mat4f& value) { +inline bool parse_value(string_view& str, mat4f& value) { for (auto i = 0; i < 4; i++) if (!parse_value(str, value[i])) return false; return true; } -[[nodiscard]] inline bool parse_value(string_view& str, frame3f& value) { +inline bool parse_value(string_view& str, frame3f& value) { for (auto i = 0; i < 4; i++) if (!parse_value(str, value[i])) return false; return true; @@ -1157,7 +1157,7 @@ bool add_points(ply_model* ply, const vector& values) { // ----------------------------------------------------------------------------- namespace yocto { -[[nodiscard]] inline bool parse_value(string_view& str, obj_vertex& value) { +inline bool parse_value(string_view& str, obj_vertex& value) { value = obj_vertex{0, 0, 0}; if (!parse_value(str, value.position)) return false; if (!str.empty() && str.front() == '/') { @@ -1177,7 +1177,7 @@ namespace yocto { } // Input for OBJ textures -[[nodiscard]] inline bool parse_value(string_view& str, obj_texture& info) { +inline bool parse_value(string_view& str, obj_texture& info) { // initialize info = obj_texture(); @@ -1208,8 +1208,7 @@ namespace yocto { } // Read obj -[[nodiscard]] inline bool load_mtl( - const string& filename, obj_scene* obj, string& error) { +inline bool load_mtl(const string& filename, obj_scene* obj, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1435,8 +1434,7 @@ namespace yocto { } // Read obj -[[nodiscard]] inline bool load_objx( - const string& filename, obj_scene* obj, string& error) { +inline bool load_objx(const string& filename, obj_scene* obj, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -1772,8 +1770,7 @@ inline void format_value(string& str, const obj_vertex& value) { } // Save obj -[[nodiscard]] inline bool save_mtl( - const string& filename, obj_scene* obj, string& error) { +inline bool save_mtl(const string& filename, obj_scene* obj, string& error) { // throw helpers // error helpers auto open_error = [filename, &error]() { @@ -1948,8 +1945,7 @@ inline void format_value(string& str, const obj_vertex& value) { } // Save obj -[[nodiscard]] inline bool save_objx( - const string& filename, obj_scene* obj, string& error) { +inline bool save_objx(const string& filename, obj_scene* obj, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -2007,8 +2003,7 @@ inline void format_value(string& str, const obj_vertex& value) { } // Save obj -[[nodiscard]] bool save_obj( - const string& filename, obj_scene* obj, string& error) { +bool save_obj(const string& filename, obj_scene* obj, string& error) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; @@ -2559,7 +2554,7 @@ struct pbrt_command { }; // get pbrt value -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, string& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, string& val) { if (pbrt.type == pbrt_type::string || pbrt.type == pbrt_type::texture) { val = pbrt.value1s; return true; @@ -2567,7 +2562,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, bool& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, bool& val) { if (pbrt.type == pbrt_type::boolean) { val = pbrt.value1b; return true; @@ -2575,7 +2570,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, int& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, int& val) { if (pbrt.type == pbrt_type::integer) { val = pbrt.value1i; return true; @@ -2583,7 +2578,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, float& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, float& val) { if (pbrt.type == pbrt_type::real) { val = pbrt.value1f; return true; @@ -2591,7 +2586,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, vec2f& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vec2f& val) { if (pbrt.type == pbrt_type::point2 || pbrt.type == pbrt_type::vector2) { val = pbrt.value2f; return true; @@ -2599,7 +2594,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value(const pbrt_value& pbrt, vec3f& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vec3f& val) { if (pbrt.type == pbrt_type::point || pbrt.type == pbrt_type::vector || pbrt.type == pbrt_type::normal || pbrt.type == pbrt_type::color) { val = pbrt.value3f; @@ -2611,8 +2606,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vector& val) { if (pbrt.type == pbrt_type::real) { if (!pbrt.vector1f.empty()) { val = pbrt.vector1f; @@ -2624,8 +2618,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vector& val) { if (pbrt.type == pbrt_type::point2 || pbrt.type == pbrt_type::vector2) { if (!pbrt.vector2f.empty()) { val = pbrt.vector2f; @@ -2644,8 +2637,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vector& val) { if (pbrt.type == pbrt_type::point || pbrt.type == pbrt_type::vector || pbrt.type == pbrt_type::normal || pbrt.type == pbrt_type::color) { if (!pbrt.vector3f.empty()) { @@ -2667,8 +2659,7 @@ struct pbrt_command { } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vector& val) { if (pbrt.type == pbrt_type::integer) { if (!pbrt.vector1i.empty()) { val = pbrt.vector1i; @@ -2680,8 +2671,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, vector& val) { if (pbrt.type == pbrt_type::integer) { if (pbrt.vector1i.empty() || (pbrt.vector1i.size() % 3) != 0) throw std::invalid_argument{"expected int3 array"}; @@ -2694,8 +2684,7 @@ struct pbrt_command { return false; } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, pair& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, pair& val) { if (pbrt.type == pbrt_type::string || pbrt.type == pbrt_type::texture) { val.first = 0; return get_pbrt_value(pbrt, val.second); @@ -2704,8 +2693,7 @@ struct pbrt_command { return get_pbrt_value(pbrt, val.first); } } -[[nodiscard]] inline bool get_pbrt_value( - const pbrt_value& pbrt, pair& val) { +inline bool get_pbrt_value(const pbrt_value& pbrt, pair& val) { if (pbrt.type == pbrt_type::string || pbrt.type == pbrt_type::texture) { val.first = zero3f; return get_pbrt_value(pbrt, val.second); @@ -2715,7 +2703,7 @@ struct pbrt_command { } } template -[[nodiscard]] inline bool get_pbrt_value( +inline bool get_pbrt_value( const vector& pbrt, const string& name, T& val) { for (auto& p : pbrt) { if (p.name == name) { @@ -2812,7 +2800,7 @@ inline void remove_pbrt_comment(string_view& str, char comment_char = '#') { } // Read a pbrt command from file -[[nodiscard]] inline bool read_pbrt_cmdline(file_stream& fs, string& cmd) { +inline bool read_pbrt_cmdline(file_stream& fs, string& cmd) { auto buffer = array{}; cmd.clear(); auto found = false; @@ -2845,7 +2833,7 @@ inline void remove_pbrt_comment(string_view& str, char comment_char = '#') { } // parse a quoted string -[[nodiscard]] inline bool parse_command(string_view& str, string& value) { +inline bool parse_command(string_view& str, string& value) { skip_whitespace(str); if (!isalpha((int)str.front())) return false; auto pos = str.find_first_not_of( @@ -2862,7 +2850,7 @@ inline void remove_pbrt_comment(string_view& str, char comment_char = '#') { // parse pbrt value with optional parens template -[[nodiscard]] inline bool parse_param(string_view& str, T& value) { +inline bool parse_param(string_view& str, T& value) { skip_whitespace(str); auto parens = !str.empty() && str.front() == '['; if (parens) str.remove_prefix(1); @@ -2877,8 +2865,7 @@ template } // parse a quoted string -[[nodiscard]] inline bool parse_nametype( - string_view& str_, string& name, string& type) { +inline bool parse_nametype(string_view& str_, string& name, string& type) { auto value = ""s; if (!parse_value(str_, value)) return false; if (str_.empty()) return false; @@ -3071,8 +3058,7 @@ inline pair get_subsurface(const string& name) { return params.at(name); } -[[nodiscard]] inline bool parse_params( - string_view& str, vector& values) { +inline bool parse_params(string_view& str, vector& values) { auto parse_pvalues = [](string_view& str, auto& value, auto& values) -> bool { values.clear(); skip_whitespace(str); @@ -4046,13 +4032,12 @@ struct pbrt_context { }; // load pbrt -[[nodiscard]] inline bool load_pbrt(const string& filename, pbrt_scene* pbrt, - string& error, pbrt_context& ctx, - unordered_map& material_map, - unordered_map& named_materials, - unordered_map& named_textures, - unordered_map& named_mediums, - const string& ply_dirname) { +inline bool load_pbrt(const string& filename, pbrt_scene* pbrt, string& error, + pbrt_context& ctx, unordered_map& material_map, + unordered_map& named_materials, + unordered_map& named_textures, + unordered_map& named_mediums, + const string& ply_dirname) { // error helpers auto open_error = [filename, &error]() { error = filename + ": file not found"; diff --git a/libs/yocto/yocto_shape.cpp b/libs/yocto/yocto_shape.cpp index 30e80116c..7ee5a4153 100644 --- a/libs/yocto/yocto_shape.cpp +++ b/libs/yocto/yocto_shape.cpp @@ -3499,7 +3499,7 @@ void make_heightfield(vector& quads, vector& positions, namespace yocto { // Load ply mesh -[[nodiscard]] bool load_shape(const string& filename, vector& points, +bool load_shape(const string& filename, vector& points, vector& lines, vector& triangles, vector& quads, vector& quadspos, vector& quadsnorm, vector& quadstexcoord, vector& positions, @@ -3611,7 +3611,7 @@ namespace yocto { } // Save ply mesh -[[nodiscard]] bool save_shape(const string& filename, const vector& points, +bool save_shape(const string& filename, const vector& points, const vector& lines, const vector& triangles, const vector& quads, const vector& quadspos, const vector& quadsnorm, const vector& quadstexcoord,