Skip to content

Commit

Permalink
Eliminate dodgy namespacing of STBIR.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Nov 5, 2024
1 parent 9733179 commit 4c2aebe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
20 changes: 7 additions & 13 deletions CesiumGltfContent/src/ImageManipulation.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#include <CesiumGltf/ImageAsset.h>
#include <CesiumGltfContent/ImageManipulation.h>
#include <CesiumGltfReader/ImageDecoder.h>

#include <cstring>

namespace Cesium {
// Use STB resize in our own namespace to avoid conflicts from other libs
#define STBIRDEF
#include <stb_image_resize2.h>
#undef STBIRDEF
} // namespace Cesium

using namespace Cesium;

#define STB_IMAGE_WRITE_STATIC
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>

using namespace CesiumGltfReader;

namespace CesiumGltfContent {

void ImageManipulation::unsafeBlitImage(
Expand Down Expand Up @@ -108,16 +102,16 @@ bool ImageManipulation::blitImage(
}

// Use STB to do the copy / scale
stbir_resize_uint8_linear(
reinterpret_cast<const unsigned char*>(pSource),
ImageDecoder::unsafeResize(
pSource,
sourcePixels.width,
sourcePixels.height,
int(bytesPerSourceRow),
reinterpret_cast<unsigned char*>(pTarget),
pTarget,
targetPixels.width,
targetPixels.height,
int(bytesPerTargetRow),
static_cast<stbir_pixel_layout>(target.channels));
target.channels);
}

return true;
Expand Down
27 changes: 27 additions & 0 deletions CesiumGltfReader/include/CesiumGltfReader/ImageDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ class ImageDecoder {
*/
static std::optional<std::string>
generateMipMaps(CesiumGltf::ImageAsset& image);

/**
* @brief Resize an image, without validating the provided pointers or ranges.
*
* @param inputPixels The input image.
* @param inputWidth The width of the input image, in pixels.
* @param inputHeight The height of the input image, in pixels.
* @param inputStrideBytes The stride of the input image, in bytes. Stride is
* the number of bytes between successive rows.
* @param outputPixels The buffer into which to write the output image.
* @param outputWidth The width of the output image, in pixels.
* @param outputHeight The height of the otuput image, in pixels.
* @param outputStrideBytes The stride of the output image, in bytes. Stride
* is the number of bytes between successive rows.
* @param channels The number of channels in both the input and output images.
* @return True if the resize succeeded, false if it failed.
*/
static bool unsafeResize(
const std::byte* pInputPixels,
int32_t inputWidth,
int32_t inputHeight,
int32_t inputStrideBytes,
std::byte* pOutputPixels,
int32_t outputWidth,
int32_t outputHeight,
int32_t outputStrideBytes,
int32_t channels);
};

} // namespace CesiumGltfReader
47 changes: 32 additions & 15 deletions CesiumGltfReader/src/ImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,19 @@

#define STBI_FAILURE_USERMSG

namespace Cesium {
// Use STB resize in our own namespace to avoid conflicts from other libs
#define STBIRDEF
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize2.h>
#undef STBIRDEF
}; // namespace Cesium

#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#define STBI_NO_STDIO
#define STBI_ASSERT(x) CESIUM_ASSERT(x)
#include <stb_image.h>
#include <turbojpeg.h>

#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_STATIC
#include <stb_image_resize2.h>

namespace CesiumGltfReader {

using namespace CesiumGltf;
using namespace Cesium;

namespace {

Expand Down Expand Up @@ -436,17 +432,16 @@ std::optional<std::string> ImageDecoder::generateMipMaps(ImageAsset& image) {
image.mipPositions[mipIndex].byteOffset = byteOffset;
image.mipPositions[mipIndex].byteSize = byteSize;

if (!stbir_resize_uint8_linear(
reinterpret_cast<const unsigned char*>(
&image.pixelData[lastByteOffset]),
if (!ImageDecoder::unsafeResize(
&image.pixelData[lastByteOffset],
lastWidth,
lastHeight,
0,
reinterpret_cast<unsigned char*>(&image.pixelData[byteOffset]),
&image.pixelData[byteOffset],
mipWidth,
mipHeight,
0,
static_cast<stbir_pixel_layout>(image.channels))) {
image.channels)) {
// Remove any added mipmaps.
image.mipPositions.clear();
image.pixelData.resize(imageByteSize);
Expand All @@ -457,4 +452,26 @@ std::optional<std::string> ImageDecoder::generateMipMaps(ImageAsset& image) {
return std::nullopt;
}

/*static*/ bool ImageDecoder::unsafeResize(
const std::byte* pInputPixels,
int32_t inputWidth,
int32_t inputHeight,
int32_t inputStrideBytes,
std::byte* pOutputPixels,
int32_t outputWidth,
int32_t outputHeight,
int32_t outputStrideBytes,
int32_t channels) {
return stbir_resize_uint8_linear(
reinterpret_cast<const unsigned char*>(pInputPixels),
inputWidth,
inputHeight,
inputStrideBytes,
reinterpret_cast<unsigned char*>(pOutputPixels),
outputWidth,
outputHeight,
outputStrideBytes,
static_cast<stbir_pixel_layout>(channels)) != nullptr;
}

} // namespace CesiumGltfReader

0 comments on commit 4c2aebe

Please sign in to comment.