-
Notifications
You must be signed in to change notification settings - Fork 885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The definitions of clamp
functions are inconsistent
#1649
Comments
Thank you @ycshao21. @hollasch he's got a point with regards to the diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html
index deca87df..497bbde9 100644
--- a/books/RayTracingTheNextWeek.html
+++ b/books/RayTracingTheNextWeek.html
@@ -1743,8 +1743,8 @@ header into a folder called `external`. Adjust according to your directory struc
static unsigned char magenta[] = { 255, 0, 255 };
if (bdata == nullptr) return magenta;
- x = clamp(x, 0, image_width);
- y = clamp(y, 0, image_height);
+ x = clamp(x, 0, image_width - 1);
+ y = clamp(y, 0, image_height - 1);
return bdata + y*bytes_per_scanline + x*bytes_per_pixel;
}
@@ -1761,7 +1761,7 @@ header into a folder called `external`. Adjust according to your directory struc
// Return the value clamped to the range [low, high).
if (x < low) return low;
if (x < high) return x;
- return high - 1;
+ return high;
}
static unsigned char float_to_byte(float value) {
diff --git a/src/TheNextWeek/rtw_stb_image.h b/src/TheNextWeek/rtw_stb_image.h
index 06fa19b2..52a4bc56 100644
--- a/src/TheNextWeek/rtw_stb_image.h
+++ b/src/TheNextWeek/rtw_stb_image.h
@@ -81,8 +81,8 @@ class rtw_image {
static unsigned char magenta[] = { 255, 0, 255 };
if (bdata == nullptr) return magenta;
- x = clamp(x, 0, image_width);
- y = clamp(y, 0, image_height);
+ x = clamp(x, 0, image_width - 1);
+ y = clamp(y, 0, image_height - 1);
return bdata + y*bytes_per_scanline + x*bytes_per_pixel;
}
@@ -99,7 +99,7 @@ class rtw_image {
// Return the value clamped to the range [low, high).
if (x < low) return low;
if (x < high) return x;
- return high - 1;
+ return high;
}
static unsigned char float_to_byte(float value) {
diff --git a/src/TheRestOfYourLife/rtw_stb_image.h b/src/TheRestOfYourLife/rtw_stb_image.h
index 06fa19b2..52a4bc56 100644
--- a/src/TheRestOfYourLife/rtw_stb_image.h
+++ b/src/TheRestOfYourLife/rtw_stb_image.h
@@ -81,8 +81,8 @@ class rtw_image {
static unsigned char magenta[] = { 255, 0, 255 };
if (bdata == nullptr) return magenta;
- x = clamp(x, 0, image_width);
- y = clamp(y, 0, image_height);
+ x = clamp(x, 0, image_width - 1);
+ y = clamp(y, 0, image_height - 1);
return bdata + y*bytes_per_scanline + x*bytes_per_pixel;
}
@@ -99,7 +99,7 @@ class rtw_image {
// Return the value clamped to the range [low, high).
if (x < low) return low;
if (x < high) return x;
- return high - 1;
+ return high;
}
static unsigned char float_to_byte(float value) { |
Rats. Agreed on the need to standardize. I think I prefer Blah blah blah, I agree with Dimitry here. |
Hello! Thanks for your tutorials!
I noticed something a bit confusing in the
image_texture::value
function:Since
u
andv
are clamped to [0,1], this suggests thati
andj
would range from [0,image.width()
] and [0,image.height()
] respectively.However, calling
image.pixel_data(i, j)
looks like there is a potential access violation wheni == image.width()
andj == image.height()
.So I checked the definition of
rtw_image::pixel_data
:x
andy
are also clamped here, but it seems that the access violation still exists.Then I checked
rtw_image::clamp
, and I realized that the function excludes the upper bound, so it actually works fine.My point is that the behavior of
rtw_image::clamp
differs fromstd::clamp
, and it is also inconsistent withinterval::clamp
. For readability and maintainability, it might be better to make these clamp functions consistent.So, I suggest adjusting
rtw_image::clamp
to include the upper bound:Then in
rtw_image::pixel_data
, we'd have:Lastly, I want to adjust how
i
andj
are assigned inimage_texture::value
.Since it is widely accepted that the range of uv is [0, 1], so keep it as it is. However, hard clipping on the upper bound may lead to multiple sampling on the edge of the texture. In my opinion, here's the most elegant way of implementation, which gives a smoother transition:
Given that
i
andj
are already valid now, the clamps inrtw_image::pixel_data
can be removed. I keep it here for robustness.It's not a huge issue, but I think making these adjustments will offer less ambiguity and a clearer code to readers.
The text was updated successfully, but these errors were encountered: