tex-decoder
v1.0.17
Published
Javascript texture data decoders without the need for WebGL
Downloads
23
Readme
tex-decoder
A pure typescript texture decoder for most image compression types without the use of WebGL. Includes a color profile converter, unswizzler, image flipper and resizer plus a TGA and PNG file maker. Great for Node or web browsers.
Fully supports the following formats:
- ETC - ETC1, ETC2 and EAC decoding. Includes options for alpha in ETC1.
- DXTn - DTX1 (aka BC1), DXT2 (aka BC2), DXT3 (aka BC2), DXT4 (aka BC3) and DXT5 (aka BC3).
- BCn - BC1 (aka DTX1), BC2 (aka DXT3), BC3 (aka DXT5), BC4 (aka ATI1), BC5 (AKA ATI2), BC6 (signed and unsigned) and BC7.
- ATI - ATI1 and ATI2.
- ATC - ATC 4 and 8.
- PVRTC - PVRTC in 2 bit or 4 bit mode.
- ASTC - ASTC 4x4 to 12x12.
- CRN - Crunch data supporting all DXT.
Also includes:
- Color Profile Converter - Convert an image color profile.
- Unswizzler - Unswizzle, untile or mortonize image data.
- Image Flipper - Flips a RGB or RGBA image.
- Image Cropper - Crops a RGB or RGBA image.
- TGA Maker - Create a TGA file from a RGB or RGBA image.
- PNG Maker - Create a PNG file from a RGB or RGBA image. Built on pngjs.
- zlib algo - Simple compression / decompresson zlib port of pako in typescript.
Installation
npm install tex-decoder
Provides CommonJS and ES modules.
Example
Decoding ETC data with supplied format selector and create a TGA file.
import {
decodeETC,
ETC_FORMAT,
makeTGA
} from 'tex-decoder';
//read file data
const data = fs.readFileSync(__dirname + '/ETC2_RGBA8_image.bin');
//decode compressed data
const decodedData = decodeETC(data,512,512,ETC_FORMAT.ETC2_RGBA8);
//or use preset format function decodeETC2RGBA(data,512,512);
//write raw RGBA output
fs.writeFileSync(__dirname + '/ETC2_RGBA8.dat',decodedData);
//or create a TGA file
const tga = makeTGA(decodedData,512,512,true);
//write .tga file
fs.writeFileSync(__dirname + '/ETC2_RGBA8.tga',tga);
ETC
Decodes compressed ETC and EAC data. Source must be Uint8Array or Buffer. Returns the same type.
Use provided ETC_FORMAT
.Format to specify format unless using presets. Use ETC_PROFILE
.RGB/RGBA to force a profile different than packed.
DTX
Decodes compressed DXT data. Source must be Uint8Array or Buffer. Returns the same type.
BC
Decodes compressed BC1-7 data. Source must be Uint8Array or Buffer. Returns the same type.
ATI
Decodes compressed ATI1 or ATI2 data. Source must be Uint8Array or Buffer. Returns the same type.
ATC
Decodes compressed ATC4 or ATC8 data. Source must be Uint8Array or Buffer. Returns the same type.
PVRTC
Decodes compressed PVRTC data in 2 or 4 bit mode. Source must be Uint8Array or Buffer. Returns the same type.
ASTC
Decodes compressed ASTC data in 4x4 to 12x12 bit mode. Source must be Uint8Array or Buffer. Returns the same type.
CRN
Decodes crunched data in DXT format and can return decoded or coded data. Source must be Uint8Array or Buffer. Returns the same type.
Data must be a vaild .crn file format.
Color Profile Converter
Converts the data's color profile bit order and data type. You can use supplied COLOR_PROFILE
profiles or create your own. You can use BYTE_VALUE
to help create your own. Read how below. Source must be Uint8Array or Buffer. Returns the same type.
Unswizzler
Unswizzle, untile or mortonize data. Source must be Uint8Array or Buffer. Returns the same type.
Image Flipper
Flips image data of 24 or 32 bit profiles (needed for some types of image files). Source must be Uint8Array or Buffer. Returns the same type.
Image Cropper
Crops image data. Bits per pixel must be supplied of source data. Source must be Uint8Array or Buffer. Returns the same type.
TGA Maker
Simple TGA file maker. Must be RGB8 or RGBA8 profile. Source must be Uint8Array or Buffer. Returns the same type.
PNG Maker
Simple PNG file maker (uses pngjs). Must be RGB8 or RGBA8 profile. Source must be Uint8Array or Buffer. Returns the same type.
zlib
A Typescript port of pako. Functions inflate
, Inflate
, deflate
, Deflate
, deflateRaw
, inflateRaw
, gzip
, ungzip
See documentation for how functions work.
Acknowledgements
This project was born from the desire to have a single library that could convert any image format. Having been using tools like Noesis and PVRTool in the past, I wanted something I could translate quickly to a Node app and then use in a web site without having to redo work. Sources for all code can be found in comments.
I'm happy to connect and grow this library if others find it useful. Pull requests or bug reports are welcome!
Disclaimer
All libraries are presented as is, I take no responsibility for outside use.
If you plan to implement these libraries for anything other than personal or educational use, please be sure you have the appropriate permissions from the original owners.