From ad7a618222c44a48c866afbc17f1de8e6607bb47 Mon Sep 17 00:00:00 2001 From: castano Date: Tue, 22 Mar 2011 22:52:43 +0000 Subject: [PATCH] Start adding ktx file support. --- src/nvimage/CMakeLists.txt | 3 +- src/nvimage/KtxFile.cpp | 83 ++++++++++++++++++++++++++++++ src/nvimage/KtxFile.h | 102 +++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 src/nvimage/KtxFile.cpp create mode 100644 src/nvimage/KtxFile.h diff --git a/src/nvimage/CMakeLists.txt b/src/nvimage/CMakeLists.txt index bbb75d9..029f962 100644 --- a/src/nvimage/CMakeLists.txt +++ b/src/nvimage/CMakeLists.txt @@ -5,11 +5,12 @@ SET(IMAGE_SRCS BlockDXT.h BlockDXT.cpp ColorBlock.h ColorBlock.cpp DirectDrawSurface.h DirectDrawSurface.cpp + ErrorMetric.h ErrorMetric.cpp Filter.h Filter.cpp FloatImage.h FloatImage.cpp - ErrorMetric.h ErrorMetric.cpp Image.h Image.cpp ImageIO.h ImageIO.cpp + #KtxFile.h KtxFile.cpp NormalMap.h NormalMap.cpp PixelFormat.h PsdFile.h diff --git a/src/nvimage/KtxFile.cpp b/src/nvimage/KtxFile.cpp new file mode 100644 index 0000000..de075bd --- /dev/null +++ b/src/nvimage/KtxFile.cpp @@ -0,0 +1,83 @@ +// This code is in the public domain -- Ignacio Castaņo + +#include "KtxFile.h" + +using namespace nv; + +static const uint8 fileIdentifier[12] = { + 0xAB, 0x4B, 0x54, 0x58, + 0x20, 0x31, 0x31, 0xBB, + 0x0D, 0x0A, 0x1A, 0x0A +}; + + +KtxHeader::KtxHeader() { + memcpy(identifier, fileIdentifier, 12); + + endianness = 0x04030201; + + glType = 0; + glTypeSize = 1; + glFormat = 0; + glInternalFormat = KTX_RGBA; + glBaseInternalFormat = KTX_RGBA; + pixelWidth = 0; + pixelHeight = 0; + pixelDepth = 0; + numberOfArrayElements = 0; + numberOfFaces = 1; + numberOfMipmapLevels = 0; + bytesOfKeyValueData = 0; +} + + +Stream & operator<< (Stream & s, DDSHeader & header) { + s.serialize(header.identifier, 12); + s << header.endiannes << header.glType << header.glTypeSize << header.glFormat << header.glInternalFormat << header.glBaseInternalFormat; + s << header.pixelWidth << header.pixelHeight << header.pixelDepth; + s << header.numberOfArrayElements << header.numberOfFaces << header.numberOfMipmapLevels; + s << header.bytesOfKeyValueData; + return s; +} + + +KtxFile::KtxFile() { +} +KtxFile::~KtxFile() { +} + +void KtxFile::addKeyValue(const char * key, const char * value) { + keyArray.append(key); + valueArray.append(value); + bytesOfKeyValueData += strlen(key) + 1 + strlen(value) + 1; +} + + +Stream & operator<< (Stream & s, KtxFile & file) { + s << header; + + if (s.isSaving()) { + + int keyValueCount = keyArray.count(); + for (int i = 0; i < keyValueCount; i++) { + const String & key = keyArray[i]; + const String & value = valueArray[i]; + uint keySize = key.length() + 1; + uint valueSize = value.length() + 1; + uint keyValueSize = keySize + valueSize; + + s << keyValueSize; + + s.serialize(key.str(), keySize); + s.serialize(value.str(), valueSize); + } + } + else { + // @@ Read key value pairs. + } + + return s; +} + + + diff --git a/src/nvimage/KtxFile.h b/src/nvimage/KtxFile.h new file mode 100644 index 0000000..9f89590 --- /dev/null +++ b/src/nvimage/KtxFile.h @@ -0,0 +1,102 @@ +// This code is in the public domain -- Ignacio Castaņo + +#pragma once +#ifndef NV_IMAGE_KTXFILE_H +#define NV_IMAGE_KTXFILE_H + +#include "nvimage.h" +#include "nvcore/StrLib.h" + +// KTX File format specification: +// http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/#key + +namespace nv +{ + class Stream; + + // GL types (Table 3.2) + const uint KTX_UNSIGNED_BYTE; + const uint KTX_UNSIGNED_SHORT_5_6_5; + // ... + + // GL formats (Table 3.3) + // ... + + // GL internal formats (Table 3.12, 3.13) + // ... + + // GL base internal format. (Table 3.11) + const uint KTX_RGB; + const uint KTX_RGBA; + const uint KTX_ALPHA; + // ... + + + struct KtxHeader { + uint8 identifier[12]; + uint32 endianness; + uint32 glType; + uint32 glTypeSize; + uint32 glFormat; + uint32 glInternalFormat; + uint32 glBaseInternalFormat; + uint32 pixelWidth; + uint32 pixelHeight; + uint32 pixelDepth; + uint32 numberOfArrayElements; + uint32 numberOfFaces; + uint32 numberOfMipmapLevels; + uint32 bytesOfKeyValueData; + + KtxHeader(); + + }; + + NVIMAGE_API Stream & operator<< (Stream & s, DDSHeader & header); + + + struct KtxFile { + KtxFile(); + ~KtxFile(); + + void addKeyValue(const char * key, const char * value); + + private: + KtxHeader header; + + Array keyArray; + Array valueArray; + + }; + + NVIMAGE_API Stream & operator<< (Stream & s, KtxFile & file); + + + /* + for each keyValuePair that fits in bytesOfKeyValueData + UInt32 keyAndValueByteSize + Byte keyAndValue[keyAndValueByteSize] + Byte valuePadding[3 - ((keyAndValueByteSize + 3) % 4)] + end + + for each mipmap_level in numberOfMipmapLevels* + UInt32 imageSize; + for each array_element in numberOfArrayElements* + for each face in numberOfFaces + for each z_slice in pixelDepth* + for each row or row_of_blocks in pixelHeight* + for each pixel or block_of_pixels in pixelWidth + Byte data[format-specific-number-of-bytes]** + end + end + end + Byte cubePadding[0-3] + end + end + Byte mipPadding[3 - ((imageSize + 3) % 4)] + end + */ + +} // nv namespace + +#endif // NV_IMAGE_KTXFILE_H