diff --git a/CHANGELOG.md b/CHANGELOG.md index c7bb82d5..30ec8338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Change Log / Ray Tracing in One Weekend # v4.0.3 (in progress) ### Common + - Change -- Improve lifetime of float image data `fdata` in `rtw_image` class (#1723) ### In One Weekend @@ -19,7 +20,7 @@ Change Log / Ray Tracing in One Weekend ### Common - Fix -- Fixed some dangling references to `random_in_unit_sphere()` (#1637) - Fix -- Clarify `uniform_real_distribution` usage for `random_double()` (#1680) - - Update -- CMake minimum required version max now at 4.0.0. + - Change -- CMake minimum required version max now at 4.0.0. ### In One Weekend - Fix -- Fix equation for refracted rays of non-unit length (#1644) diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index cda2e6e8..47e49334 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -1715,7 +1715,6 @@ ~rtw_image() { delete[] bdata; - STBI_FREE(fdata); } bool load(const std::string& filename) { @@ -1726,16 +1725,23 @@ // below, for the full height of the image. auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel - fdata = stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); - if (fdata == nullptr) return false; + float *fdata = + stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); + + if (fdata == nullptr) { + image_width = image_height = 0; + return false; + } bytes_per_scanline = image_width * bytes_per_pixel; - convert_to_bytes(); + convert_to_bytes(fdata); + + STBI_FREE(fdata); return true; } - int width() const { return (fdata == nullptr) ? 0 : image_width; } - int height() const { return (fdata == nullptr) ? 0 : image_height; } + int width() const { return image_width; } + int height() const { return image_height; } const unsigned char* pixel_data(int x, int y) const { // Return the address of the three RGB bytes of the pixel at x,y. If there is no image @@ -1751,7 +1757,6 @@ private: const int bytes_per_pixel = 3; - float *fdata = nullptr; // Linear floating point pixel data unsigned char *bdata = nullptr; // Linear 8-bit pixel data int image_width = 0; // Loaded image width int image_height = 0; // Loaded image height @@ -1772,7 +1777,7 @@ return static_cast(256.0 * value); } - void convert_to_bytes() { + void convert_to_bytes(float *fdata) { // Convert the linear floating point pixel data to bytes, storing the resulting byte // data in the `bdata` member. diff --git a/src/TheNextWeek/rtw_stb_image.h b/src/TheNextWeek/rtw_stb_image.h index 06fa19b2..f356d319 100644 --- a/src/TheNextWeek/rtw_stb_image.h +++ b/src/TheNextWeek/rtw_stb_image.h @@ -53,7 +53,6 @@ class rtw_image { ~rtw_image() { delete[] bdata; - STBI_FREE(fdata); } bool load(const std::string& filename) { @@ -64,16 +63,23 @@ class rtw_image { // below, for the full height of the image. auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel - fdata = stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); - if (fdata == nullptr) return false; + float *fdata = + stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); + + if (fdata == nullptr) { + image_width = image_height = 0; + return false; + } bytes_per_scanline = image_width * bytes_per_pixel; - convert_to_bytes(); + convert_to_bytes(fdata); + + STBI_FREE(fdata); return true; } - int width() const { return (fdata == nullptr) ? 0 : image_width; } - int height() const { return (fdata == nullptr) ? 0 : image_height; } + int width() const { return image_width; } + int height() const { return image_height; } const unsigned char* pixel_data(int x, int y) const { // Return the address of the three RGB bytes of the pixel at x,y. If there is no image @@ -89,7 +95,6 @@ class rtw_image { private: const int bytes_per_pixel = 3; - float *fdata = nullptr; // Linear floating point pixel data unsigned char *bdata = nullptr; // Linear 8-bit pixel data int image_width = 0; // Loaded image width int image_height = 0; // Loaded image height @@ -110,7 +115,7 @@ class rtw_image { return static_cast(256.0 * value); } - void convert_to_bytes() { + void convert_to_bytes(float *fdata) { // Convert the linear floating point pixel data to bytes, storing the resulting byte // data in the `bdata` member. diff --git a/src/TheRestOfYourLife/rtw_stb_image.h b/src/TheRestOfYourLife/rtw_stb_image.h index 06fa19b2..f356d319 100644 --- a/src/TheRestOfYourLife/rtw_stb_image.h +++ b/src/TheRestOfYourLife/rtw_stb_image.h @@ -53,7 +53,6 @@ class rtw_image { ~rtw_image() { delete[] bdata; - STBI_FREE(fdata); } bool load(const std::string& filename) { @@ -64,16 +63,23 @@ class rtw_image { // below, for the full height of the image. auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel - fdata = stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); - if (fdata == nullptr) return false; + float *fdata = + stbi_loadf(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel); + + if (fdata == nullptr) { + image_width = image_height = 0; + return false; + } bytes_per_scanline = image_width * bytes_per_pixel; - convert_to_bytes(); + convert_to_bytes(fdata); + + STBI_FREE(fdata); return true; } - int width() const { return (fdata == nullptr) ? 0 : image_width; } - int height() const { return (fdata == nullptr) ? 0 : image_height; } + int width() const { return image_width; } + int height() const { return image_height; } const unsigned char* pixel_data(int x, int y) const { // Return the address of the three RGB bytes of the pixel at x,y. If there is no image @@ -89,7 +95,6 @@ class rtw_image { private: const int bytes_per_pixel = 3; - float *fdata = nullptr; // Linear floating point pixel data unsigned char *bdata = nullptr; // Linear 8-bit pixel data int image_width = 0; // Loaded image width int image_height = 0; // Loaded image height @@ -110,7 +115,7 @@ class rtw_image { return static_cast(256.0 * value); } - void convert_to_bytes() { + void convert_to_bytes(float *fdata) { // Convert the linear floating point pixel data to bytes, storing the resulting byte // data in the `bdata` member.