Skip to content

Commit 1cdfd78

Browse files
committed
Add colorspace extension to texture options. Fixes #184
1 parent 803b65b commit 1cdfd78

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

models/colorspace-issue-184.mtl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
newmtl default
2+
Ka 0 0 0
3+
Kd 0 0 0
4+
Ks 0 0 0
5+
Kt 0.1 0.2 0.3
6+
map_Kd -colorspace sRGB -o 0.1 diffuse.jpg
7+
map_Ks -s 0.1 0.2 specular.jpg
8+
map_bump -colorspace linear -bm 3 bumpmap.jpg
9+

models/colorspace-issue-184.obj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mtllib colorspace-issue-184.mtl
2+
o Test
3+
v 1.864151 -1.219172 -5.532511
4+
v 0.575869 -0.666304 5.896140
5+
v 0.940448 1.000000 -1.971128
6+
usemtl default
7+
f 1 2 3

tests/tester.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,25 @@ TEST_CASE("multiple-group-names", "[group]") {
825825
REQUIRE(0 == shapes[1].name.compare("back cube")); // multiple whitespaces are aggregated as single white space.
826826
}
827827

828+
TEST_CASE("colorspace", "[Issue184]") {
829+
tinyobj::attrib_t attrib;
830+
std::vector<tinyobj::shape_t> shapes;
831+
std::vector<tinyobj::material_t> materials;
832+
833+
std::string err;
834+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/colorspace-issue-184.obj", gMtlBasePath);
835+
836+
if (!err.empty()) {
837+
std::cerr << err << std::endl;
838+
}
839+
REQUIRE(true == ret);
840+
REQUIRE(1 == shapes.size());
841+
REQUIRE(1 == materials.size());
842+
REQUIRE(0 == materials[0].diffuse_texopt.colorspace.compare("sRGB"));
843+
REQUIRE(0 == materials[0].specular_texopt.colorspace.size());
844+
REQUIRE(0 == materials[0].bump_texopt.colorspace.compare("linear"));
845+
}
846+
828847
// Fuzzer test.
829848
// Just check if it does not crash.
830849
// Disable by default since Windows filesystem can't create filename of afl testdata

tiny_obj_loader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ THE SOFTWARE.
2323
*/
2424

2525
//
26+
// version 1.2.3 : Added color space extension('-colorspace') to tex opts.
2627
// version 1.2.2 : Parse multiple group names.
2728
// version 1.2.1 : Added initial support for line('l') primitive(PR #178)
2829
// version 1.2.0 : Hardened implementation(#175)
@@ -113,6 +114,11 @@ namespace tinyobj {
113114
// cube_front | cube_back | # side of the cube is specified
114115
// separately
115116
// cube_left | cube_right
117+
//
118+
// TinyObjLoader extension.
119+
//
120+
// -colorspace SPACE # Color space of the texture. e.g. 'sRGB` or 'linear'
121+
//
116122

117123
#ifdef TINYOBJLOADER_USE_DOUBLE
118124
//#pragma message "using double"
@@ -147,6 +153,9 @@ typedef struct {
147153
bool blendu; // -blendu (default on)
148154
bool blendv; // -blendv (default on)
149155
real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
156+
157+
// extension
158+
std::string colorspace; // Explicitly specify color space of stored value. Usually `sRGB` or `linear` (default empty).
150159
} texture_option_t;
151160

152161
typedef struct {
@@ -950,6 +959,9 @@ static bool ParseTextureNameAndOption(std::string *texname,
950959
} else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
951960
token += 4;
952961
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
962+
} else if ((0 == strncmp(token, "-colorspace", 11)) && IS_SPACE((token[11]))) {
963+
token += 12;
964+
texopt->colorspace = parseString(&token);
953965
} else {
954966
// Assume texture filename
955967
#if 0

0 commit comments

Comments
 (0)