Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,6 @@

~rtw_image() {
delete[] bdata;
STBI_FREE(fdata);
}

bool load(const std::string& filename) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -1772,7 +1777,7 @@
return static_cast<unsigned char>(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.

Expand Down
21 changes: 13 additions & 8 deletions src/TheNextWeek/rtw_stb_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class rtw_image {

~rtw_image() {
delete[] bdata;
STBI_FREE(fdata);
}

bool load(const std::string& filename) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -110,7 +115,7 @@ class rtw_image {
return static_cast<unsigned char>(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.

Expand Down
21 changes: 13 additions & 8 deletions src/TheRestOfYourLife/rtw_stb_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class rtw_image {

~rtw_image() {
delete[] bdata;
STBI_FREE(fdata);
}

bool load(const std::string& filename) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -110,7 +115,7 @@ class rtw_image {
return static_cast<unsigned char>(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.

Expand Down