Skip to content

Commit 6683378

Browse files
committed
Copy rtw_stb_image.h updates from book 2 to 3
I forgot to make the equivalent fixes to the copy in the book 3 source.
1 parent d4c3c41 commit 6683378

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/TheRestOfYourLife/rtw_stb_image.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ class rtw_image {
102102
return high - 1;
103103
}
104104

105+
static unsigned char float_to_byte(float value) {
106+
if (value <= 0.0)
107+
return 0;
108+
if (1.0 <= value)
109+
return 255;
110+
return static_cast< unsigned char >(256.0 * value);
111+
}
112+
105113
void convert_to_bytes() {
106114
// Convert the linear floating point pixel data to bytes, storing the resulting byte
107115
// data in the `bdata` member.
@@ -115,7 +123,7 @@ class rtw_image {
115123
auto *bptr = bdata;
116124
auto *fptr = fdata;
117125
for (auto i=0; i < total_bytes; i++, fptr++, bptr++)
118-
*bptr = static_cast<unsigned char>(*fptr * 256.0);
126+
*bptr = float_to_byte(*fptr);
119127
}
120128
};
121129

0 commit comments

Comments
 (0)