Skip to content

Commit

Permalink
Removed miniz from tinyexr header and added it as an external depende…
Browse files Browse the repository at this point in the history
…ncy.
  • Loading branch information
BlockoS committed Jul 5, 2021
1 parent 3c33352 commit 44b20fa
Show file tree
Hide file tree
Showing 16 changed files with 10,530 additions and 6,480 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(${BUILD_TARGET} CXX)

# options
option(TINYEXR_BUILD_SAMPLE "Build a sample" ON)
option(TINYEXR_USE_MINIZ "Use miniz" ON)

# cmake modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
Expand All @@ -20,10 +21,20 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

set(TINYEXR_SOURCES
${PROJECT_SOURCE_DIR}/tinyexr.cc
)

if(TINYEXR_USE_MINIZ)
enable_language(C)
add_library(miniz STATIC deps/miniz/miniz.c)
target_include_directories(miniz PUBLIC deps/miniz)
set_target_properties(miniz PROPERTIES FOLDER "deps")
list(APPEND TINYEXR_EXT_LIBRARIES miniz)
endif()

add_library(${BUILD_TARGET} ${TINYEXR_SOURCES} ${TINYEXR_DEP_SOURCES})
add_sanitizers(${BUILD_TARGET})

Expand All @@ -42,6 +53,7 @@ if (TINYEXR_BUILD_SAMPLE)
)

add_executable(${SAMPLE_TARGET} ${TINYEXR_SAMPLE_SOURCES})
target_link_libraries(${SAMPLE_TARGET} ${TINYEXR_EXT_LIBRARIES})
add_sanitizers(${SAMPLE_TARGET})
if (WIN32)
target_compile_definitions(${SAMPLE_TARGET} PUBLIC UNICODE)
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
#CXXFLAGS += -DTINYEXR_USE_ZFP=1 -I./deps/ZFP/inc
#LDFLAGS += -L./deps/ZFP/lib -lzfp

CXXFLAGS += -I./deps/miniz

miniz.o:
$(CXX) $(CXXFLAGS) -c miniz.c

all:
$(CXX) $(CXXFLAGS) -o test_tinyexr test_tinyexr.cc $(LDFLAGS)
$(CXX) $(CXXFLAGS) -o test_tinyexr test_tinyexr.cc miniz.o $(LDFLAGS)

test:
./test_tinyexr asakusa.exr
Expand Down
8 changes: 7 additions & 1 deletion Makefile.llvm-mingw
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ CXX=$(HOME)/local/llvm-mingw-20200325-ubuntu-18.04/bin/x86_64-w64-mingw32-clang+
#CXXFLAGS += -DTINYEXR_USE_ZFP=1 -I./deps/ZFP/inc
#LDFLAGS += -L./deps/ZFP/lib -lzfp

# miniz
CXXFLAGS += -DTINYEXR_USE_MINIZ=1 -I./deps/miniz
miniz.o:
$(CXX) $(CXXFLAGS) -c ./deps/miniz.c

#
all:
$(CXX) $(CXXFLAGS) -o test_tinyexr test_tinyexr.cc $(LDFLAGS)
$(CXX) $(CXXFLAGS) -o test_tinyexr test_tinyexr.cc miniz.o $(LDFLAGS)

test:
./test_tinyexr asakusa.exr
Expand Down
196 changes: 196 additions & 0 deletions deps/miniz/ChangeLog.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions deps/miniz/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright 2013-2014 RAD Game Tools and Valve Software
Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
105 changes: 105 additions & 0 deletions deps/miniz/examples/example1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// example1.c - Demonstrates miniz.c's compress() and uncompress() functions (same as zlib's).
// Public domain, May 15 2011, Rich Geldreich, [email protected]. See "unlicense" statement at the end of tinfl.c.
#include <stdio.h>
#include "miniz.h"
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;

// The string to compress.
static const char *s_pStr = "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
"Good morning Dr. Chandra. This is Hal. I am ready for my first lesson.";

int main(int argc, char *argv[])
{
uint step = 0;
int cmp_status;
uLong src_len = (uLong)strlen(s_pStr);
uLong cmp_len = compressBound(src_len);
uLong uncomp_len = src_len;
uint8 *pCmp, *pUncomp;
uint total_succeeded = 0;
(void)argc, (void)argv;

printf("miniz.c version: %s\n", MZ_VERSION);

do
{
// Allocate buffers to hold compressed and uncompressed data.
pCmp = (mz_uint8 *)malloc((size_t)cmp_len);
pUncomp = (mz_uint8 *)malloc((size_t)src_len);
if ((!pCmp) || (!pUncomp))
{
printf("Out of memory!\n");
return EXIT_FAILURE;
}

// Compress the string.
cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)s_pStr, src_len);
if (cmp_status != Z_OK)
{
printf("compress() failed!\n");
free(pCmp);
free(pUncomp);
return EXIT_FAILURE;
}

printf("Compressed from %u to %u bytes\n", (mz_uint32)src_len, (mz_uint32)cmp_len);

if (step)
{
// Purposely corrupt the compressed data if fuzzy testing (this is a very crude fuzzy test).
uint n = 1 + (rand() % 3);
while (n--)
{
uint i = rand() % cmp_len;
pCmp[i] ^= (rand() & 0xFF);
}
}

// Decompress.
cmp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);
total_succeeded += (cmp_status == Z_OK);

if (step)
{
printf("Simple fuzzy test: step %u total_succeeded: %u\n", step, total_succeeded);
}
else
{
if (cmp_status != Z_OK)
{
printf("uncompress failed!\n");
free(pCmp);
free(pUncomp);
return EXIT_FAILURE;
}

printf("Decompressed from %u to %u bytes\n", (mz_uint32)cmp_len, (mz_uint32)uncomp_len);

// Ensure uncompress() returned the expected data.
if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, (size_t)src_len)))
{
printf("Decompression failed!\n");
free(pCmp);
free(pUncomp);
return EXIT_FAILURE;
}
}

free(pCmp);
free(pUncomp);

step++;

// Keep on fuzzy testing if there's a non-empty command line.
} while (argc >= 2);

printf("Success.\n");
return EXIT_SUCCESS;
}
164 changes: 164 additions & 0 deletions deps/miniz/examples/example2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// example2.c - Simple demonstration of miniz.c's ZIP archive API's.
// Note this test deletes the test archive file "__mz_example2_test__.zip" in the current directory, then creates a new one with test data.
// Public domain, May 15 2011, Rich Geldreich, [email protected]. See "unlicense" statement at the end of tinfl.c.

#if defined(__GNUC__)
// Ensure we get the 64-bit variants of the CRT's file I/O calls
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE 1
#endif
#endif

#include <stdio.h>
#include "miniz.h"

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;

// The string to compress.
static const char *s_pTest_str =
"MISSION CONTROL I wouldn't worry too much about the computer. First of all, there is still a chance that he is right, despite your tests, and" \
"if it should happen again, we suggest eliminating this possibility by allowing the unit to remain in place and seeing whether or not it" \
"actually fails. If the computer should turn out to be wrong, the situation is still not alarming. The type of obsessional error he may be" \
"guilty of is not unknown among the latest generation of HAL 9000 computers. It has almost always revolved around a single detail, such as" \
"the one you have described, and it has never interfered with the integrity or reliability of the computer's performance in other areas." \
"No one is certain of the cause of this kind of malfunctioning. It may be over-programming, but it could also be any number of reasons. In any" \
"event, it is somewhat analogous to human neurotic behavior. Does this answer your query? Zero-five-three-Zero, MC, transmission concluded.";

static const char *s_pComment = "This is a comment";

int main(int argc, char *argv[])
{
int i, sort_iter;
mz_bool status;
size_t uncomp_size;
mz_zip_archive zip_archive;
void *p;
const int N = 50;
char data[2048];
char archive_filename[64];
static const char *s_Test_archive_filename = "__mz_example2_test__.zip";

assert((strlen(s_pTest_str) + 64) < sizeof(data));

printf("miniz.c version: %s\n", MZ_VERSION);

(void)argc, (void)argv;

// Delete the test archive, so it doesn't keep growing as we run this test
remove(s_Test_archive_filename);

// Append a bunch of text files to the test archive
for (i = (N - 1); i >= 0; --i)
{
sprintf(archive_filename, "%u.txt", i);
sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);

// Add a new file to the archive. Note this is an IN-PLACE operation, so if it fails your archive is probably hosed (its central directory may not be complete) but it should be recoverable using zip -F or -FF. So use caution with this guy.
// A more robust way to add a file to an archive would be to read it into memory, perform the operation, then write a new archive out to a temp file and then delete/rename the files.
// Or, write a new archive to disk to a temp file, then delete/rename the files. For this test this API is fine.
status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, archive_filename, data, strlen(data) + 1, s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION);
if (!status)
{
printf("mz_zip_add_mem_to_archive_file_in_place failed!\n");
return EXIT_FAILURE;
}
}

// Add a directory entry for testing
status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, "directory/", NULL, 0, "no comment", (uint16)strlen("no comment"), MZ_BEST_COMPRESSION);
if (!status)
{
printf("mz_zip_add_mem_to_archive_file_in_place failed!\n");
return EXIT_FAILURE;
}

// Now try to open the archive.
memset(&zip_archive, 0, sizeof(zip_archive));

status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, 0);
if (!status)
{
printf("mz_zip_reader_init_file() failed!\n");
return EXIT_FAILURE;
}

// Get and print information about each file in the archive.
for (i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++)
{
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat))
{
printf("mz_zip_reader_file_stat() failed!\n");
mz_zip_reader_end(&zip_archive);
return EXIT_FAILURE;
}

printf("Filename: \"%s\", Comment: \"%s\", Uncompressed size: %u, Compressed size: %u, Is Dir: %u\n", file_stat.m_filename, file_stat.m_comment, (uint)file_stat.m_uncomp_size, (uint)file_stat.m_comp_size, mz_zip_reader_is_file_a_directory(&zip_archive, i));

if (!strcmp(file_stat.m_filename, "directory/"))
{
if (!mz_zip_reader_is_file_a_directory(&zip_archive, i))
{
printf("mz_zip_reader_is_file_a_directory() didn't return the expected results!\n");
mz_zip_reader_end(&zip_archive);
return EXIT_FAILURE;
}
}
}

// Close the archive, freeing any resources it was using
mz_zip_reader_end(&zip_archive);

// Now verify the compressed data
for (sort_iter = 0; sort_iter < 2; sort_iter++)
{
memset(&zip_archive, 0, sizeof(zip_archive));
status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0);
if (!status)
{
printf("mz_zip_reader_init_file() failed!\n");
return EXIT_FAILURE;
}

for (i = 0; i < N; i++)
{
sprintf(archive_filename, "%u.txt", i);
sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);

// Try to extract all the files to the heap.
p = mz_zip_reader_extract_file_to_heap(&zip_archive, archive_filename, &uncomp_size, 0);
if (!p)
{
printf("mz_zip_reader_extract_file_to_heap() failed!\n");
mz_zip_reader_end(&zip_archive);
return EXIT_FAILURE;
}

// Make sure the extraction really succeeded.
if ((uncomp_size != (strlen(data) + 1)) || (memcmp(p, data, strlen(data))))
{
printf("mz_zip_reader_extract_file_to_heap() failed to extract the proper data\n");
mz_free(p);
mz_zip_reader_end(&zip_archive);
return EXIT_FAILURE;
}

printf("Successfully extracted file \"%s\", size %u\n", archive_filename, (uint)uncomp_size);
printf("File data: \"%s\"\n", (const char *)p);

// We're done.
mz_free(p);
}

// Close the archive, freeing any resources it was using
mz_zip_reader_end(&zip_archive);
}

printf("Success.\n");
return EXIT_SUCCESS;
}
Loading

0 comments on commit 44b20fa

Please sign in to comment.