Skip to content

Commit

Permalink
Merge pull request #166 from marlam/stb-zlib
Browse files Browse the repository at this point in the history
Add support for using the stb_image[_write].h ZLIB implementation
  • Loading branch information
syoyo committed May 6, 2022
2 parents b0e6d9e + cfe8a01 commit 0c48a75
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

`tinyexr` is a small, single header-only library to load and save OpenEXR (.exr) images.
`tinyexr` is written in portable C++ (no library dependency except for STL), thus `tinyexr` is good to embed into your application.
To use `tinyexr`, simply copy `tinyexr.h`, `miniz.c` and `miniz.h`(for zlib. You can use system-installed zlib instead of miniz. Controlled with `TINYEXR_USE_MINIZ` compile flag) into your project.
To use `tinyexr`, simply copy `tinyexr.h`, `miniz.c` and `miniz.h`(for zlib. You can use system-installed zlib instead of miniz, or the zlib implementation included in `stb_image[_write].h`. Controlled with `TINYEXR_USE_MINIZ` and `TINYEXR_USE_STB_ZLIB` compile flags) into your project.

# Features

Expand Down Expand Up @@ -138,13 +138,17 @@ Include `tinyexr.h` with `TINYEXR_IMPLEMENTATION` flag (do this only for **one**
//including `tinyexr.h` when you disable `TINYEXR_USE_MINIZ`
//#define TINYEXR_USE_MINIZ 0
//#include "zlib.h"
//Or, if your project uses `stb_image[_write].h`, use their
//zlib implementation:
//#define TINYEXR_USE_STB_ZLIB 1
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"
```

### Compile flags

* `TINYEXR_USE_MINIZ` Use miniz (default = 1). Please include `zlib.h` header before `tinyexr.h` if you disable miniz support(e.g. use system's zlib).
* `TINYEXR_USE_STB_ZLIB` Use zlib from `stb_image[_write].h` instead of miniz or the system's zlib (default = 0).
* `TINYEXR_USE_PIZ` Enable PIZ compression support (default = 1)
* `TINYEXR_USE_ZFP` Enable ZFP compression supoort (TinyEXR extension, default = 0)
* `TINYEXR_USE_THREAD` Enable threaded loading using C++11 thread (Requires C++11 compiler, default = 0)
Expand Down
35 changes: 34 additions & 1 deletion tinyexr.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ extern "C" {
#endif

// Use miniz or not to decode ZIP format pixel. Linking with zlib
// required if this flas is 0.
// required if this flas is 0 and TINYEXR_USE_STB_ZLIB is 0.
#ifndef TINYEXR_USE_MINIZ
#define TINYEXR_USE_MINIZ (1)
#endif

// Use the ZIP implementation of stb_image.h and stb_image_write.h.
#ifndef TINYEXR_USE_STB_ZLIB
#define TINYEXR_USE_STB_ZLIB (0)
#endif

// Disable PIZ comporession when applying cpplint.
#ifndef TINYEXR_USE_PIZ
#define TINYEXR_USE_PIZ (1)
Expand Down Expand Up @@ -619,6 +624,16 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height,
//#include "zlib.h"
#endif

#if TINYEXR_USE_STB_ZLIB
// Since we don't know where a project has stb_image.h and stb_image_write.h
// and whether they are in the include path, we don't include them here, and
// instead declare the two relevant functions manually.
// from stb_image.h:
extern "C" int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
// from stb_image_write.h:
extern "C" unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality);
#endif

#if TINYEXR_USE_ZFP

#ifdef __clang__
Expand Down Expand Up @@ -1284,6 +1299,14 @@ static void CompressZip(unsigned char *dst,
assert(ret == MZ_OK);
(void)ret;

compressedSize = outSize;
#elif TINYEXR_USE_STB_ZLIB
int outSize;
unsigned char* r = stbi_zlib_compress(const_cast<unsigned char*>(&tmpBuf.at(0)), src_size, &outSize, 8);
assert(ret);
memcpy(dst, r, outSize);
free(r);

compressedSize = outSize;
#else
uLong outSize = compressBound(static_cast<uLong>(src_size));
Expand Down Expand Up @@ -1318,6 +1341,12 @@ static bool DecompressZip(unsigned char *dst,
if (MZ_OK != ret) {
return false;
}
#elif TINYEXR_USE_STB_ZLIB
int ret = stbi_zlib_decode_buffer(reinterpret_cast<char*>(&tmpBuf.at(0)),
*uncompressed_size, reinterpret_cast<const char*>(src), src_size);
if (ret < 0) {
return false;
}
#else
int ret = uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size);
if (Z_OK != ret) {
Expand Down Expand Up @@ -6566,6 +6595,10 @@ static bool EncodePixelData(/* out */ std::vector<unsigned char>& out_data,
#if TINYEXR_USE_MINIZ
std::vector<unsigned char> block(mz_compressBound(
static_cast<unsigned long>(buf.size())));
#elif TINYEXR_USE_STB_ZLIB
// there is no compressBound() function, so we use a value that
// is grossly overestimated, but should always work
std::vector<unsigned char> block(256 + 2 * buf.size());
#else
std::vector<unsigned char> block(
compressBound(static_cast<uLong>(buf.size())));
Expand Down

0 comments on commit 0c48a75

Please sign in to comment.