Skip to content

Commit

Permalink
Lots of changes. Added support for Native images, TGA, Crunched DXT.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Jones committed Apr 29, 2012
1 parent 60a2a00 commit 17a0bf9
Show file tree
Hide file tree
Showing 33 changed files with 19,819 additions and 226 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -15,4 +15,8 @@ Simplest usage is as follows:
});

loadDDSTexture returns a new WebGL texture and populates it with the DDS image data when it has loaded. For more advanced
loading see `uploadDDSLevels` in the source file.
loading see `uploadDDSLevels` in the source file.

Credits
--------------------
Thank you to [Rich Geldreich](https://plus.google.com/106462556644344774154) for developing the [Crunch texture compression library](http://code.google.com/p/crunch/) and [Evan Parker](https://plus.google.com/104261567553968048744) for porting it to Javscript via Emscripten
3 changes: 3 additions & 0 deletions build.sh
@@ -0,0 +1,3 @@
#!/bin/bash

node requirejs/r.js -o name=requirejs/almond.js include=texture-util/export out=build/texture-util.js baseUrl=. wrap=true
1 change: 1 addition & 0 deletions build/texture-util.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions crunch/README.md
@@ -0,0 +1,7 @@
Crunch
=========

crn_decomp.h and crlib.h are part of the Crunch texture compression library by Rich Geldreich, (http://www-cs-students.stanford.edu/~eparker/files/crunch/decode_test.html)

crn.cpp is a modification of the original emscripten wrapper code by Evan Parker, (http://www-cs-students.stanford.edu/~eparker/files/crunch/decode_test.html)

4 changes: 4 additions & 0 deletions crunch/a.out
@@ -0,0 +1,4 @@
#!/bin/sh
lli=${LLVMINTERP-lli}
exec $lli \
/var/folders/34/y7wmqjvx1zv89hn8sr16s1xc0000gn/T/tmp0Cf1nk/crn_decomp.bc ${1+"$@"}
10 changes: 10 additions & 0 deletions crunch/build.sh
@@ -0,0 +1,10 @@
#!/bin/bash

rm ../texture-util/crn_decomp.js

emcc \
-s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_crn_get_width', '_crn_get_height', '_crn_get_levels', '_crn_get_dxt_format', '_crn_get_uncompressed_size', '_crn_decompress']" \
-O2 \
-o ../texture-util/crn_decomp.js \
crn.cpp && \
chmod -R a+rX .
68 changes: 68 additions & 0 deletions crunch/crn.cpp
@@ -0,0 +1,68 @@
#define PLATFORM_NACL

#include <stddef.h> // For NULL, size_t
#include <cstring> // 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 *src, unsigned int src_size, unsigned int level_index);
void crn_decompress(void *src, unsigned int src_size, void *dst, unsigned int dst_size, unsigned int level_index);
}

unsigned int crn_get_width(void *src, unsigned int src_size) {
crnd::crn_texture_info tex_info;
crnd::crnd_get_texture_info(static_cast<crn_uint8*>(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<crn_uint8*>(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<crn_uint8*>(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<crn_uint8*>(src), src_size, &tex_info);
return tex_info.m_format;
}

unsigned int crn_get_uncompressed_size(void *src, unsigned int src_size, unsigned int level_index) {
crnd::crn_texture_info tex_info;
crnd::crnd_get_texture_info(static_cast<crn_uint8*>(src), src_size, &tex_info);
const crn_uint32 width = tex_info.m_width >> level_index;
const crn_uint32 height = tex_info.m_height >> level_index;
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, unsigned int level_index) {
crnd::crn_texture_info tex_info;
crnd::crnd_get_texture_info(static_cast<crn_uint8*>(src), src_size, &tex_info);
const crn_uint32 width = tex_info.m_width >> level_index;
const crn_uint32 height = tex_info.m_height >> level_index;
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<crn_uint8*>(src), src_size);
void *pDecomp_images[1];
pDecomp_images[0] = dst;
crnd::crnd_unpack_level(pContext, pDecomp_images, dst_size, row_pitch, level_index);
crnd::crnd_unpack_end(pContext);
}

0 comments on commit 17a0bf9

Please sign in to comment.