From 8fbb07dc57fce35c86372c2124a18efcd84ca77a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Jan 2017 14:13:39 -0500 Subject: [PATCH 1/4] Add functions to export with emscripten. --- emscripten/crn.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 emscripten/crn.cpp diff --git a/emscripten/crn.cpp b/emscripten/crn.cpp new file mode 100644 index 0000000..3a37761 --- /dev/null +++ b/emscripten/crn.cpp @@ -0,0 +1,68 @@ +#define PLATFORM_NACL + +#include // For NULL, size_t +#include // for malloc etc + +#include "crn_decomp.h" + +extern "C" { + unsigned int crn_get_width(void *src, unsigned int src_size); + unsigned int crn_get_height(void *src, unsigned int src_size); + unsigned int crn_get_levels(void *src, unsigned int src_size); + unsigned int crn_get_dxt_format(void *src, unsigned int src_size); + unsigned int crn_get_uncompressed_size(void *p, unsigned int size); + void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size); +} + +unsigned int crn_get_width(void *src, unsigned int src_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + return tex_info.m_width; +} + +unsigned int crn_get_height(void *src, unsigned int src_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + return tex_info.m_height; +} + +unsigned int crn_get_levels(void *src, unsigned int src_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + return tex_info.m_levels; +} + +unsigned int crn_get_dxt_format(void *src, unsigned int src_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + return tex_info.m_format; +} + +unsigned int crn_get_uncompressed_size(void *src, unsigned int src_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + const crn_uint32 width = tex_info.m_width; + const crn_uint32 height = tex_info.m_height; + const crn_uint32 blocks_x = (width + 3) >> 2; + const crn_uint32 blocks_y = (height + 3) >> 2; + const crn_uint32 row_pitch = blocks_x * crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); + const crn_uint32 total_face_size = row_pitch * blocks_y; + return total_face_size; +} + +void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + const crn_uint32 width = tex_info.m_width; + const crn_uint32 height = tex_info.m_height; + const crn_uint32 blocks_x = (width + 3) >> 2; + const crn_uint32 blocks_y = (height + 3) >> 2; + const crn_uint32 row_pitch = blocks_x * crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); + + crnd::crnd_unpack_context pContext = + crnd::crnd_unpack_begin(static_cast(src), src_size); + void *pDecomp_images[1]; + pDecomp_images[0] = dst; + crnd::crnd_unpack_level(pContext, pDecomp_images, dst_size, row_pitch, 0); + crnd::crnd_unpack_end(pContext); +} \ No newline at end of file From a60ed487c5daa3e01481eb771d6e7c0b588843a2 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Jan 2017 14:17:31 -0500 Subject: [PATCH 2/4] Update crn.cpp license. --- emscripten/crn.cpp | 72 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/emscripten/crn.cpp b/emscripten/crn.cpp index 3a37761..cc66b7d 100644 --- a/emscripten/crn.cpp +++ b/emscripten/crn.cpp @@ -1,4 +1,26 @@ -#define PLATFORM_NACL +/* Copyright (c) 2013, Evan Parker, Brandon Jones. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#define PLATFORM_NACL // This disables use of 64 bit integers, among other things. #include // For NULL, size_t #include // for malloc etc @@ -10,8 +32,9 @@ extern "C" { unsigned int crn_get_height(void *src, unsigned int src_size); unsigned int crn_get_levels(void *src, unsigned int src_size); unsigned int crn_get_dxt_format(void *src, unsigned int src_size); - unsigned int crn_get_uncompressed_size(void *p, unsigned int size); - void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size); + unsigned int crn_get_bytes_per_block(void *src, unsigned int src_size); + unsigned int crn_get_uncompressed_size(void *p, unsigned int size, unsigned int level); + void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size, unsigned int firstLevel, unsigned int levelCount); } unsigned int crn_get_width(void *src, unsigned int src_size) { @@ -38,11 +61,17 @@ unsigned int crn_get_dxt_format(void *src, unsigned int src_size) { return tex_info.m_format; } -unsigned int crn_get_uncompressed_size(void *src, unsigned int src_size) { +unsigned int crn_get_bytes_per_block(void *src, unsigned int src_size) { crnd::crn_texture_info tex_info; crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); - const crn_uint32 width = tex_info.m_width; - const crn_uint32 height = tex_info.m_height; + return crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); +} + +unsigned int crn_get_uncompressed_size(void *src, unsigned int src_size, unsigned int level) { + crnd::crn_texture_info tex_info; + crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); + const crn_uint32 width = tex_info.m_width >> level; + const crn_uint32 height = tex_info.m_height >> level; const crn_uint32 blocks_x = (width + 3) >> 2; const crn_uint32 blocks_y = (height + 3) >> 2; const crn_uint32 row_pitch = blocks_x * crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); @@ -50,19 +79,32 @@ unsigned int crn_get_uncompressed_size(void *src, unsigned int src_size) { return total_face_size; } -void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size) { +void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size, unsigned int firstLevel, unsigned int levelCount) { crnd::crn_texture_info tex_info; crnd::crnd_get_texture_info(static_cast(src), src_size, &tex_info); - const crn_uint32 width = tex_info.m_width; - const crn_uint32 height = tex_info.m_height; - const crn_uint32 blocks_x = (width + 3) >> 2; - const crn_uint32 blocks_y = (height + 3) >> 2; - const crn_uint32 row_pitch = blocks_x * crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); + + crn_uint32 width = tex_info.m_width >> firstLevel; + crn_uint32 height = tex_info.m_height >> firstLevel; + crn_uint32 bytes_per_block = crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format); + + void *pDecomp_images[1]; + pDecomp_images[0] = dst; crnd::crnd_unpack_context pContext = crnd::crnd_unpack_begin(static_cast(src), src_size); - void *pDecomp_images[1]; - pDecomp_images[0] = dst; - crnd::crnd_unpack_level(pContext, pDecomp_images, dst_size, row_pitch, 0); + + for (int i = firstLevel; i < firstLevel + levelCount; ++i) { + crn_uint32 blocks_x = (width + 3) >> 2; + crn_uint32 blocks_y = (height + 3) >> 2; + crn_uint32 row_pitch = blocks_x * bytes_per_block; + crn_uint32 total_level_size = row_pitch * blocks_y; + + crnd::crnd_unpack_level(pContext, pDecomp_images, total_level_size, row_pitch, i); + pDecomp_images[0] = (char*)pDecomp_images[0] + total_level_size; + + width = width >> 1; + height = height >> 1; + } + crnd::crnd_unpack_end(pContext); } \ No newline at end of file From 0c22f98142c2086b92bd9ab9cb44f877fd877f3c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Jan 2017 14:57:52 -0500 Subject: [PATCH 3/4] Rename crn.cpp -> crunch_lib.cpp. --- emscripten/{crn.cpp => crunch_lib.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename emscripten/{crn.cpp => crunch_lib.cpp} (100%) diff --git a/emscripten/crn.cpp b/emscripten/crunch_lib.cpp similarity index 100% rename from emscripten/crn.cpp rename to emscripten/crunch_lib.cpp From 1d3fcb59cde87ccb4ec879bf7100daca95f04117 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Jan 2017 15:04:52 -0500 Subject: [PATCH 4/4] Update README with instructions for using Emscripten. --- readme.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/readme.txt b/readme.txt index 39116ff..237e551 100644 --- a/readme.txt +++ b/readme.txt @@ -308,3 +308,12 @@ hasn't been tuned for max. quality yet. supported when writing to .DDS, not .KTX. Also, only plain block by block compression is supported when writing to ETC1, and .CRN does not support ETC1. +Compile to Javascript with Emscripten +------------------------------------- + +Download and install Emscripten: + http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html + +From the root directory, run: + emcc -O3 emscripten/crn.cpp -I./inc -s EXPORTED_FUNCTIONS="['_malloc', '_free', '_crn_get_width', '_crn_get_height', '_crn_get_levels', '_crn_get_dxt_format', '_crn_get_bytes_per_block', '_crn_get_uncompressed_size', '_crn_decompress']" -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s ALLOW_MEMORY_GROWTH=1 --memory-init-file 0 -o crunch.js +