From 8fbb07dc57fce35c86372c2124a18efcd84ca77a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Jan 2017 14:13:39 -0500 Subject: [PATCH] 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