Skip to content

Commit c5ea386

Browse files
committed
Add option to disable libjpeg-turbo.
Instead of always disabling it for Emscripten, when this is really only need when building for Unity.
1 parent 8b86d7f commit c5ea386

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ endif()
3232

3333
option(CESIUM_USE_EZVCPKG "use ezvcpkg helper" ${CESIUM_USE_EZVCPKG_DEFAULT})
3434
option(CESIUM_DISABLE_CURL "Disable cesium-native's use of libcurl" OFF)
35+
option(CESIUM_DISABLE_LIBJPEG_TURBO "Disable cesium-native's use of libjpeg-turbo. JPEG images will be decoded with STB instead." OFF)
3536
option(CESIUM_WASM64 "Enable 64-bit WebAssembly target" OFF)
3637

3738
if (CESIUM_TARGET_WASM)
@@ -126,7 +127,7 @@ set(PACKAGES_PUBLIC asyncplusplus expected-lite fmt glm rapidjson spdlog stb ada
126127
# OpenSSL and abseil are both dependencies of s2geometry
127128
set(PACKAGES_PRIVATE
128129
abseil draco ktx[core] modp-base64 meshoptimizer openssl s2geometry
129-
libjpeg-turbo sqlite3 tinyxml2 libwebp zlib-ng picosha2
130+
sqlite3 tinyxml2 libwebp zlib-ng picosha2
130131
earcut-hpp libmorton zstd
131132
)
132133

@@ -142,6 +143,10 @@ if(NOT CESIUM_DISABLE_CURL)
142143
list(APPEND PACKAGES_PRIVATE curl)
143144
endif()
144145

146+
if(NOT CESIUM_DISABLE_LIBJPEG_TURBO)
147+
list(APPEND PACKAGES_PRIVATE libjpeg-turbo)
148+
endif()
149+
145150
# We use cpp-httplib to host a local web server for OAuth2 authorization. That's not
146151
# going to work at all on the web, and the latest versions of cpp-httplib only support
147152
# 64-bit platforms anyway, so skip it entirely for WebAssembly builds.
@@ -373,7 +378,6 @@ find_package(draco CONFIG REQUIRED)
373378
find_package(expected-lite CONFIG REQUIRED)
374379
find_package(glm CONFIG REQUIRED)
375380
find_package(Ktx CONFIG REQUIRED)
376-
find_package(libjpeg-turbo CONFIG REQUIRED)
377381
find_package(libmorton CONFIG REQUIRED)
378382
find_package(meshoptimizer CONFIG REQUIRED)
379383
find_package(OpenSSL REQUIRED)
@@ -393,6 +397,10 @@ if(NOT CESIUM_DISABLE_CURL)
393397
find_package(CURL REQUIRED)
394398
endif()
395399

400+
if(NOT CESIUM_DISABLE_LIBJPEG_TURBO)
401+
find_package(libjpeg-turbo CONFIG REQUIRED)
402+
endif()
403+
396404
if(NOT CESIUM_TARGET_WASM)
397405
find_package(httplib CONFIG REQUIRED)
398406
endif()

CesiumGltfReader/CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ target_link_libraries(CesiumGltfReader
6262
meshoptimizer::meshoptimizer
6363
modp_b64::modp_b64
6464
KTX::ktx
65-
WebP::webp WebP::webpdecoder
66-
$<IF:$<TARGET_EXISTS:libjpeg-turbo::turbojpeg>,libjpeg-turbo::turbojpeg,libjpeg-turbo::turbojpeg-static>
65+
WebP::webp
66+
WebP::webpdecoder
6767
)
68+
69+
if(CESIUM_DISABLE_LIBJPEG_TURBO)
70+
target_compile_definitions(
71+
CesiumGltfReader
72+
PRIVATE
73+
CESIUM_DISABLE_LIBJPEG_TURBO
74+
)
75+
else()
76+
target_link_libraries(
77+
CesiumGltfReader
78+
PRIVATE
79+
$<IF:$<TARGET_EXISTS:libjpeg-turbo::turbojpeg>,libjpeg-turbo::turbojpeg,libjpeg-turbo::turbojpeg-static>
80+
)
81+
endif()

CesiumGltfReader/src/ImageDecoder.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <CesiumUtility/Tracing.h>
66

77
#include <ktx.h>
8-
#include <turbojpeg.h>
98
#include <webp/decode.h>
109

1110
#include <algorithm>
@@ -28,6 +27,10 @@
2827
#define STB_IMAGE_RESIZE_STATIC
2928
#include <stb_image_resize2.h>
3029

30+
#ifndef CESIUM_DISABLE_LIBJPEG_TURBO
31+
#include <turbojpeg.h>
32+
#endif
33+
3134
namespace CesiumGltfReader {
3235

3336
using namespace CesiumGltf;
@@ -287,7 +290,7 @@ ImageReaderResult ImageDecoder::readImage(
287290
}
288291

289292
{
290-
#ifndef __EMSCRIPTEN__ // Conflict between turbojpeg and Unity
293+
#ifndef CESIUM_DISABLE_LIBJPEG_TURBO
291294
tjhandle tjInstance = tjInitDecompress();
292295
int inSubsamp, inColorspace;
293296
if (!tjDecompressHeader3(
@@ -318,7 +321,7 @@ ImageReaderResult ImageDecoder::readImage(
318321
result.pImage = nullptr;
319322
}
320323
} else
321-
#endif // __EMSCRIPTEN__
324+
#endif // !CESIUM_DISABLE_LIBJPEG_TURBO
322325
{
323326
CESIUM_TRACE("Decode PNG");
324327
image.bytesPerChannel = 1;
@@ -352,9 +355,9 @@ ImageReaderResult ImageDecoder::readImage(
352355
result.errors.emplace_back(stbi_failure_reason());
353356
}
354357
}
355-
#ifndef __EMSCRIPTEN__
358+
#ifndef CESIUM_DISABLE_LIBJPEG_TURBO
356359
tjDestroy(tjInstance);
357-
#endif // __EMSCRIPTEN__
360+
#endif
358361
}
359362
return result;
360363
}

0 commit comments

Comments
 (0)