Skip to content

Commit

Permalink
BasisImageConverter: Replace image loading sources with empty stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
pezcode committed Sep 14, 2024
1 parent 96807d3 commit 20c8fa8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 7 deletions.
13 changes: 6 additions & 7 deletions modules/FindBasisUniversal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,19 @@ foreach(_component ${BasisUniversal_FIND_COMPONENTS})
${BasisUniversalEncoder_DIR}/basisu_resampler.cpp
${BasisUniversalEncoder_DIR}/basisu_resample_filters.cpp
${BasisUniversalEncoder_DIR}/basisu_ssim.cpp
${BasisUniversalEncoder_DIR}/basisu_uastc_enc.cpp
${BasisUniversalEncoder_DIR}/jpgd.cpp)
${BasisUniversalEncoder_DIR}/basisu_uastc_enc.cpp)

# Files not present in all supported basis versions, treat them
# as optional and do nothing if not found.
foreach(_file
# Removed in 1.16
apg_bmp.c
basisu_astc_decomp.cpp
basisu_global_selector_palette_helpers.cpp
lodepng.cpp
# Added in 1.16
basisu_opencl.cpp
pvpngreader.cpp
# Added in 1.50
basisu_astc_hdr_enc.cpp
3rdparty/android_astc_decomp.cpp
3rdparty/tinyexr.cpp)
3rdparty/android_astc_decomp.cpp)
# Disable the find root path here, it overrides the
# CMAKE_FIND_ROOT_PATH_MODE_INCLUDE setting potentially set in
# toolchains.
Expand All @@ -204,6 +199,10 @@ foreach(_component ${BasisUniversal_FIND_COMPONENTS})
endif()
endforeach()

# Source files for image file loading are not added here.
# BasisImageConverter provides stubs for all used functions.
# See src/external/basis-image-loading-stubs for more info.

foreach(_file ${BasisUniversalEncoder_SOURCES})
_basis_setup_source_file(${_file})
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions src/MagnumPlugins/BasisImageConverter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ target_include_directories(BasisImageConverter PUBLIC
# Turns #include "../zstd/zstd.h" into an actual external zstd.h include.
# See the README in that directory for details.
${PROJECT_SOURCE_DIR}/src/external/basis-zstd-include-uncrapifier/put-this-on-include-path)
# Adds empty definitions for unused image loading library functions.
# See the README in that directory for details.
target_sources(BasisImageConverter PRIVATE
${PROJECT_SOURCE_DIR}/src/external/basis-image-loading-stubs/stubs.cpp)
target_link_libraries(BasisImageConverter
PUBLIC Magnum::Trade
PRIVATE BasisUniversal::Encoder)
Expand Down
14 changes: 14 additions & 0 deletions src/external/basis-image-loading-stubs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
The Basis encoder unconditionally includes code for loading image files from
disk (.png, .exr, etc.), but BasisImageConverter always supplies image data
in memory. So the code path using the disk loading is never run, wasting size
and increasing compile time needlessly.

There's no way to remove it through a preprocessor define so instead all
required functions are provided as empty stubs in stubs.cpp.

The following library sources are stubbed:
jpgd.cpp
apg_bmp.c // Removed in 1.16
lodepng.cpp // Removed in 1.16
pvpngreader.cpp // Added in 1.16
3rdparty/tinyexr.cpp // Added in 1.50
122 changes: 122 additions & 0 deletions src/external/basis-image-loading-stubs/stubs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <Corrade/Utility/Assert.h>

#include <vector>
#include <string>

/* jpgd, present in all supported versions */

namespace jpgd {

unsigned char* decompress_jpeg_image_from_file(const char*, int*, int*, int*, int, uint32_t) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

}

/* apg_bmp, removed in 1.16 */

extern "C" {

unsigned char* apg_bmp_read(const char*, int*, int*, unsigned int*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

void apg_bmp_free(unsigned char*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

}

/* lodepng, removed in 1.16 */

/* Can't forward-declare since it has no explicit underlying type */
typedef enum LodePNGColorType {
LCT_GREY = 0,
LCT_RGB = 2,
LCT_PALETTE = 3,
LCT_GREY_ALPHA = 4,
LCT_RGBA = 6
} LodePNGColorType;

typedef struct LodePNGState LodePNGState;

namespace lodepng {

class State {
public:
State();
virtual ~State();
};

State::State() {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

State::~State() {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

unsigned decode(std::vector<unsigned char>&, unsigned&, unsigned&, const unsigned char*, size_t, LodePNGColorType, unsigned) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

unsigned encode(std::vector<unsigned char>&, const unsigned char*, unsigned, unsigned, LodePNGColorType, unsigned) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

unsigned load_file(std::vector<unsigned char>&, const std::string&) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

unsigned save_file(const std::vector<unsigned char>&, const std::string&) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

}

unsigned lodepng_inspect(unsigned*, unsigned*, LodePNGState*, const unsigned char*, size_t) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

/* pvpng, added in 1.16 */

namespace pv_png {

void* load_png(const void*, size_t, uint32_t, uint32_t&, uint32_t&, uint32_t&) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

}

/* tinyexr, added in 1.50 */

extern "C" {

int LoadEXRWithLayer(float **, int*, int*, const char*, const char*, const char**, int*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

typedef struct TEXRHeader EXRHeader;
typedef struct TEXRImage EXRImage;

void InitEXRHeader(EXRHeader*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

void InitEXRImage(EXRImage*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

void FreeEXRErrorMessage(const char*) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

int SaveEXRImageToFile(const EXRImage*, const EXRHeader*, const char*, const char**) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

int LoadEXRFromMemory(float**, int*, int*, const unsigned char*, size_t, const char**) {
CORRADE_INTERNAL_ASSERT_UNREACHABLE();
}

}

0 comments on commit 20c8fa8

Please sign in to comment.