HTML and CSS Reference
In-Depth Information
var DDS_HEADER_PF_FLAGS = 20;
var DDS_HEADER_PF_FOURCC = 21;
// FourCC format identifiers.
var FOURCC_DXT1 = fourCCToInt32("DXT1");
var FOURCC_DXT3 = fourCCToInt32("DXT3");
var FOURCC_DXT5 = fourCCToInt32("DXT5");
Next, you'll add the function that actually parses the DDS file and extracts the necessary information from it. This
function will take an array buffer containing the file contents (which I'll cover how to get in a moment) and two callbacks,
one which is called when the file is parsed correctly and one that's called in the case of an error. The parsed callback will
accept six arguments: the raw DXT texture data as a TypedArray , the width and height of the highest texture level, the
number of levels the DXT data contains, the internal format of the data (one of the COMPRESSED_* codes defined in
Listing 21-2), and finally, the number of bytes per block for this format, to make size calculations easier. If that sounds
like a lot of information, don't stress! The DDS file provides all of that information for you!
Listing 21-3. Parsing a DDS File
// Parse a DDS file and provide information about the raw DXT data it contains to the given
callback.
function parseDDS(arrayBuffer, callback, errorCallback) {
// Callbacks must be provided.
if (!callback || !errorCallback) { return; }
// Get a view of the arrayBuffer that represents the DDS header.
var header = new Int32Array(arrayBuffer, 0, DDS_HEADER_LENGTH);
// Do some sanity checks to make sure this is a valid DDS file.
if(header[DDS_HEADER_MAGIC] != DDS_MAGIC) {
errorCallback("Invalid magic number in DDS header");
return 0;
}
if(!header[DDS_HEADER_PF_FLAGS] & DDPF_FOURCC) {
errorCallback("Unsupported format, must contain a FourCC code");
return 0;
}
// Determine what type of compressed data the file contains.
var fourCC = header[DDS_HEADER_PF_FOURCC];
var bytesPerBlock, internalFormat;
switch(fourCC) {
case FOURCC_DXT1:
bytesPerBlock = 8;
internalFormat = COMPRESSED_RGB_S3TC_DXT1_EXT;
break;
case FOURCC_DXT3:
bytesPerBlock = 16;
internalFormat = COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
Search WWH ::




Custom Search