Skip to content

Commit b24d646

Browse files
committed
rtw_stb_image to each project, add src/external
- Clone the `rtw_stb_image.h` header to each project. - Move src/common/external to src/external.
1 parent 0baf3d6 commit b24d646

File tree

6 files changed

+206
-15
lines changed

6 files changed

+206
-15
lines changed

CMakeLists.txt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ set ( CMAKE_CXX_EXTENSIONS OFF )
1515

1616
# Source
1717

18+
set ( EXTERNAL
19+
src/external/stb_image.h
20+
)
21+
1822
set ( SOURCE_ONE_WEEKEND
23+
src/external/stb_image.h
24+
1925
src/InOneWeekend/camera.h
2026
src/InOneWeekend/color.h
2127
src/InOneWeekend/hittable.h
2228
src/InOneWeekend/hittable_list.h
2329
src/InOneWeekend/interval.h
2430
src/InOneWeekend/material.h
2531
src/InOneWeekend/ray.h
32+
src/InOneWeekend/rtw_stb_image.h
2633
src/InOneWeekend/rtweekend.h
2734
src/InOneWeekend/sphere.h
2835
src/InOneWeekend/vec3.h
@@ -31,8 +38,8 @@ set ( SOURCE_ONE_WEEKEND
3138
)
3239

3340
set ( SOURCE_NEXT_WEEK
34-
src/common/external/stb_image.h
35-
src/common/rtw_stb_image.h
41+
src/external/stb_image.h
42+
3643
src/TheNextWeek/aabb.h
3744
src/TheNextWeek/bvh.h
3845
src/TheNextWeek/camera.h
@@ -45,6 +52,7 @@ set ( SOURCE_NEXT_WEEK
4552
src/TheNextWeek/perlin.h
4653
src/TheNextWeek/quad.h
4754
src/TheNextWeek/ray.h
55+
src/TheNextWeek/rtw_stb_image.h
4856
src/TheNextWeek/rtweekend.h
4957
src/TheNextWeek/sphere.h
5058
src/TheNextWeek/texture.h
@@ -54,8 +62,8 @@ set ( SOURCE_NEXT_WEEK
5462
)
5563

5664
set ( SOURCE_REST_OF_YOUR_LIFE
57-
src/common/external/stb_image.h
58-
src/common/rtw_stb_image.h
65+
src/external/stb_image.h
66+
5967
src/TheRestOfYourLife/aabb.h
6068
src/TheRestOfYourLife/camera.h
6169
src/TheRestOfYourLife/color.h
@@ -69,6 +77,7 @@ set ( SOURCE_REST_OF_YOUR_LIFE
6977
src/TheRestOfYourLife/perlin.h
7078
src/TheRestOfYourLife/quad.h
7179
src/TheRestOfYourLife/ray.h
80+
src/TheRestOfYourLife/rtw_stb_image.h
7281
src/TheRestOfYourLife/rtweekend.h
7382
src/TheRestOfYourLife/sphere.h
7483
src/TheRestOfYourLife/texture.h
@@ -77,7 +86,7 @@ set ( SOURCE_REST_OF_YOUR_LIFE
7786
src/TheRestOfYourLife/main.cc
7887
)
7988

80-
include_directories(src/common)
89+
include_directories(src)
8190

8291
# Specific compiler flags below. We're not going to add options for all possible compilers, but if
8392
# you're new to CMake (like we are), the following may be a helpful example if you're using a
@@ -102,13 +111,13 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
102111
endif()
103112

104113
# Executables
105-
add_executable(inOneWeekend ${SOURCE_ONE_WEEKEND})
106-
add_executable(theNextWeek ${SOURCE_NEXT_WEEK})
107-
add_executable(theRestOfYourLife ${SOURCE_REST_OF_YOUR_LIFE})
108-
add_executable(cos_cubed src/TheRestOfYourLife/cos_cubed.cc ${COMMON_ALL})
109-
add_executable(cos_density src/TheRestOfYourLife/cos_density.cc ${COMMON_ALL})
110-
add_executable(integrate_x_sq src/TheRestOfYourLife/integrate_x_sq.cc ${COMMON_ALL})
111-
add_executable(pi src/TheRestOfYourLife/pi.cc ${COMMON_ALL})
112-
add_executable(estimate_halfway src/TheRestOfYourLife/estimate_halfway.cc ${COMMON_ALL})
113-
add_executable(sphere_importance src/TheRestOfYourLife/sphere_importance.cc ${COMMON_ALL})
114-
add_executable(sphere_plot src/TheRestOfYourLife/sphere_plot.cc ${COMMON_ALL})
114+
add_executable(inOneWeekend ${EXTERNAL} ${SOURCE_ONE_WEEKEND})
115+
add_executable(theNextWeek ${EXTERNAL} ${SOURCE_NEXT_WEEK})
116+
add_executable(theRestOfYourLife ${EXTERNAL} ${SOURCE_REST_OF_YOUR_LIFE})
117+
add_executable(cos_cubed src/TheRestOfYourLife/cos_cubed.cc )
118+
add_executable(cos_density src/TheRestOfYourLife/cos_density.cc )
119+
add_executable(integrate_x_sq src/TheRestOfYourLife/integrate_x_sq.cc )
120+
add_executable(pi src/TheRestOfYourLife/pi.cc )
121+
add_executable(estimate_halfway src/TheRestOfYourLife/estimate_halfway.cc )
122+
add_executable(sphere_importance src/TheRestOfYourLife/sphere_importance.cc )
123+
add_executable(sphere_plot src/TheRestOfYourLife/sphere_plot.cc )
File renamed without changes.

src/TheNextWeek/rtw_stb_image.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#ifndef RTW_STB_IMAGE_H
2+
#define RTW_STB_IMAGE_H
3+
4+
5+
// Disable strict warnings for this header from the Microsoft Visual C++ compiler.
6+
#ifdef _MSC_VER
7+
#pragma warning (push, 0)
8+
#endif
9+
10+
#define STB_IMAGE_IMPLEMENTATION
11+
#include "external/stb_image.h"
12+
13+
#include <cstdlib>
14+
#include <iostream>
15+
16+
17+
class rtw_image {
18+
public:
19+
rtw_image() : data(nullptr) {}
20+
21+
rtw_image(const char* image_filename) {
22+
// Loads image data from the specified file. If the RTW_IMAGES environment variable is
23+
// defined, looks only in that directory for the image file. If the image was not found,
24+
// searches for the specified image file first from the current directory, then in the
25+
// images/ subdirectory, then the _parent's_ images/ subdirectory, and then _that_
26+
// parent, on so on, for six levels up. If the image was not loaded successfully,
27+
// width() and height() will return 0.
28+
29+
auto filename = std::string(image_filename);
30+
auto imagedir = getenv("RTW_IMAGES");
31+
32+
// Hunt for the image file in some likely locations.
33+
if (imagedir && load(std::string(imagedir) + "/" + image_filename)) return;
34+
if (load(filename)) return;
35+
if (load("images/" + filename)) return;
36+
if (load("../images/" + filename)) return;
37+
if (load("../../images/" + filename)) return;
38+
if (load("../../../images/" + filename)) return;
39+
if (load("../../../../images/" + filename)) return;
40+
if (load("../../../../../images/" + filename)) return;
41+
if (load("../../../../../../images/" + filename)) return;
42+
43+
std::cerr << "ERROR: Could not load image file '" << image_filename << "'.\n";
44+
}
45+
46+
~rtw_image() { STBI_FREE(data); }
47+
48+
bool load(const std::string filename) {
49+
// Loads image data from the given file name. Returns true if the load succeeded.
50+
auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel
51+
data = stbi_load(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel);
52+
bytes_per_scanline = image_width * bytes_per_pixel;
53+
return data != nullptr;
54+
}
55+
56+
int width() const { return (data == nullptr) ? 0 : image_width; }
57+
int height() const { return (data == nullptr) ? 0 : image_height; }
58+
59+
const unsigned char* pixel_data(int x, int y) const {
60+
// Return the address of the three bytes of the pixel at x,y (or magenta if no data).
61+
static unsigned char magenta[] = { 255, 0, 255 };
62+
if (data == nullptr) return magenta;
63+
64+
x = clamp(x, 0, image_width);
65+
y = clamp(y, 0, image_height);
66+
67+
return data + y*bytes_per_scanline + x*bytes_per_pixel;
68+
}
69+
70+
private:
71+
const int bytes_per_pixel = 3;
72+
unsigned char *data;
73+
int image_width, image_height;
74+
int bytes_per_scanline;
75+
76+
static int clamp(int x, int low, int high) {
77+
// Return the value clamped to the range [low, high).
78+
if (x < low) return low;
79+
if (x < high) return x;
80+
return high - 1;
81+
}
82+
};
83+
84+
85+
// Restore MSVC compiler warnings
86+
#ifdef _MSC_VER
87+
#pragma warning (pop)
88+
#endif
89+
90+
91+
#endif
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#ifndef RTW_STB_IMAGE_H
2+
#define RTW_STB_IMAGE_H
3+
4+
5+
// Disable strict warnings for this header from the Microsoft Visual C++ compiler.
6+
#ifdef _MSC_VER
7+
#pragma warning (push, 0)
8+
#endif
9+
10+
#define STB_IMAGE_IMPLEMENTATION
11+
#include "external/stb_image.h"
12+
13+
#include <cstdlib>
14+
#include <iostream>
15+
16+
17+
class rtw_image {
18+
public:
19+
rtw_image() : data(nullptr) {}
20+
21+
rtw_image(const char* image_filename) {
22+
// Loads image data from the specified file. If the RTW_IMAGES environment variable is
23+
// defined, looks only in that directory for the image file. If the image was not found,
24+
// searches for the specified image file first from the current directory, then in the
25+
// images/ subdirectory, then the _parent's_ images/ subdirectory, and then _that_
26+
// parent, on so on, for six levels up. If the image was not loaded successfully,
27+
// width() and height() will return 0.
28+
29+
auto filename = std::string(image_filename);
30+
auto imagedir = getenv("RTW_IMAGES");
31+
32+
// Hunt for the image file in some likely locations.
33+
if (imagedir && load(std::string(imagedir) + "/" + image_filename)) return;
34+
if (load(filename)) return;
35+
if (load("images/" + filename)) return;
36+
if (load("../images/" + filename)) return;
37+
if (load("../../images/" + filename)) return;
38+
if (load("../../../images/" + filename)) return;
39+
if (load("../../../../images/" + filename)) return;
40+
if (load("../../../../../images/" + filename)) return;
41+
if (load("../../../../../../images/" + filename)) return;
42+
43+
std::cerr << "ERROR: Could not load image file '" << image_filename << "'.\n";
44+
}
45+
46+
~rtw_image() { STBI_FREE(data); }
47+
48+
bool load(const std::string filename) {
49+
// Loads image data from the given file name. Returns true if the load succeeded.
50+
auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel
51+
data = stbi_load(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel);
52+
bytes_per_scanline = image_width * bytes_per_pixel;
53+
return data != nullptr;
54+
}
55+
56+
int width() const { return (data == nullptr) ? 0 : image_width; }
57+
int height() const { return (data == nullptr) ? 0 : image_height; }
58+
59+
const unsigned char* pixel_data(int x, int y) const {
60+
// Return the address of the three bytes of the pixel at x,y (or magenta if no data).
61+
static unsigned char magenta[] = { 255, 0, 255 };
62+
if (data == nullptr) return magenta;
63+
64+
x = clamp(x, 0, image_width);
65+
y = clamp(y, 0, image_height);
66+
67+
return data + y*bytes_per_scanline + x*bytes_per_pixel;
68+
}
69+
70+
private:
71+
const int bytes_per_pixel = 3;
72+
unsigned char *data;
73+
int image_width, image_height;
74+
int bytes_per_scanline;
75+
76+
static int clamp(int x, int low, int high) {
77+
// Return the value clamped to the range [low, high).
78+
if (x < low) return low;
79+
if (x < high) return x;
80+
return high - 1;
81+
}
82+
};
83+
84+
85+
// Restore MSVC compiler warnings
86+
#ifdef _MSC_VER
87+
#pragma warning (pop)
88+
#endif
89+
90+
91+
#endif
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)