mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
- added a new CompressedData type to love.image - added love.image.isCompressed(file or data) - love.graphics.newImage automatically tests for compression when loading a file - added love.graphics.isSupported strings for DXT, BC5, and BC7 Compressed textures meant to be used on the GPU offer huge performance gains when - loading the texture from a file - loading the texture from RAM to VRAM - loading mipmaps - rendering --HG-- branch : image-CompressedData
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* Simple DDS data parser for compressed 2D textures.
|
||||
*
|
||||
* Copyright (c) 2013 Alexander Szpakowski.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*
|
||||
* Enums and structs copied from Microsoft.
|
||||
* http://msdn.microsoft.com/en-us/library/bb943991.aspx
|
||||
**/
|
||||
|
||||
#ifndef DDS_INFO_H
|
||||
#define DDS_INFO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace dds
|
||||
{
|
||||
namespace dxinfo
|
||||
{
|
||||
|
||||
enum DDPF
|
||||
{
|
||||
DDPF_ALPHAPIXELS = 0x000001,
|
||||
DDPF_ALPHA = 0x000002,
|
||||
DDPF_FOURCC = 0x000004,
|
||||
DDPF_RGB = 0x000040,
|
||||
DDPF_YUV = 0x000200,
|
||||
DDPF_LUMINANCE = 0x020000
|
||||
};
|
||||
|
||||
enum D3D10ResourceDimension
|
||||
{
|
||||
D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
|
||||
D3D10_RESOURCE_DIMENSION_BUFFER = 1,
|
||||
D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
|
||||
D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
|
||||
D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
|
||||
};
|
||||
|
||||
enum DXGIFormat
|
||||
{
|
||||
DXGI_FORMAT_UNKNOWN = 0,
|
||||
|
||||
DXGI_FORMAT_R32G32B32A32_TYPELESS = 1,
|
||||
DXGI_FORMAT_R32G32B32A32_FLOAT = 2,
|
||||
DXGI_FORMAT_R32G32B32A32_UINT = 3,
|
||||
DXGI_FORMAT_R32G32B32A32_SINT = 4,
|
||||
|
||||
DXGI_FORMAT_R32G32B32_TYPELESS = 5,
|
||||
DXGI_FORMAT_R32G32B32_FLOAT = 6,
|
||||
DXGI_FORMAT_R32G32B32_UINT = 7,
|
||||
DXGI_FORMAT_R32G32B32_SINT = 8,
|
||||
|
||||
DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
|
||||
DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
|
||||
DXGI_FORMAT_R16G16B16A16_UNORM = 11,
|
||||
DXGI_FORMAT_R16G16B16A16_UINT = 12,
|
||||
DXGI_FORMAT_R16G16B16A16_SNORM = 13,
|
||||
DXGI_FORMAT_R16G16B16A16_SINT = 14,
|
||||
|
||||
DXGI_FORMAT_R32G32_TYPELESS = 15,
|
||||
DXGI_FORMAT_R32G32_FLOAT = 16,
|
||||
DXGI_FORMAT_R32G32_UINT = 17,
|
||||
DXGI_FORMAT_R32G32_SINT = 18,
|
||||
|
||||
DXGI_FORMAT_R32G8X24_TYPELESS = 19,
|
||||
DXGI_FORMAT_D32_FLOAT_S8X24_UINT = 20,
|
||||
DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS = 21,
|
||||
DXGI_FORMAT_X32_TYPELESS_G8X24_UINT = 22,
|
||||
|
||||
DXGI_FORMAT_R10G10B10A2_TYPELESS = 23,
|
||||
DXGI_FORMAT_R10G10B10A2_UNORM = 24,
|
||||
DXGI_FORMAT_R10G10B10A2_UINT = 25,
|
||||
|
||||
DXGI_FORMAT_R11G11B10_FLOAT = 26,
|
||||
|
||||
DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
|
||||
DXGI_FORMAT_R8G8B8A8_UNORM = 28,
|
||||
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
|
||||
DXGI_FORMAT_R8G8B8A8_UINT = 30,
|
||||
DXGI_FORMAT_R8G8B8A8_SNORM = 31,
|
||||
DXGI_FORMAT_R8G8B8A8_SINT = 32,
|
||||
|
||||
DXGI_FORMAT_R16G16_TYPELESS = 33,
|
||||
DXGI_FORMAT_R16G16_FLOAT = 34,
|
||||
DXGI_FORMAT_R16G16_UNORM = 35,
|
||||
DXGI_FORMAT_R16G16_UINT = 36,
|
||||
DXGI_FORMAT_R16G16_SNORM = 37,
|
||||
DXGI_FORMAT_R16G16_SINT = 38,
|
||||
|
||||
DXGI_FORMAT_R32_TYPELESS = 39,
|
||||
DXGI_FORMAT_D32_FLOAT = 40,
|
||||
DXGI_FORMAT_R32_FLOAT = 41,
|
||||
DXGI_FORMAT_R32_UINT = 42,
|
||||
DXGI_FORMAT_R32_SINT = 43,
|
||||
|
||||
DXGI_FORMAT_R24G8_TYPELESS = 44,
|
||||
DXGI_FORMAT_D24_UNORM_S8_UINT = 45,
|
||||
DXGI_FORMAT_R24_UNORM_X8_TYPELESS = 46,
|
||||
DXGI_FORMAT_X24_TYPELESS_G8_UINT = 47,
|
||||
|
||||
DXGI_FORMAT_R8G8_TYPELESS = 48,
|
||||
DXGI_FORMAT_R8G8_UNORM = 49,
|
||||
DXGI_FORMAT_R8G8_UINT = 50,
|
||||
DXGI_FORMAT_R8G8_SNORM = 51,
|
||||
DXGI_FORMAT_R8G8_SINT = 52,
|
||||
|
||||
DXGI_FORMAT_R16_TYPELESS = 53,
|
||||
DXGI_FORMAT_R16_FLOAT = 54,
|
||||
DXGI_FORMAT_D16_UNORM = 55,
|
||||
DXGI_FORMAT_R16_UNORM = 56,
|
||||
DXGI_FORMAT_R16_UINT = 57,
|
||||
DXGI_FORMAT_R16_SNORM = 58,
|
||||
DXGI_FORMAT_R16_SINT = 59,
|
||||
|
||||
DXGI_FORMAT_R8_TYPELESS = 60,
|
||||
DXGI_FORMAT_R8_UNORM = 61,
|
||||
DXGI_FORMAT_R8_UINT = 62,
|
||||
DXGI_FORMAT_R8_SNORM = 63,
|
||||
DXGI_FORMAT_R8_SINT = 64,
|
||||
DXGI_FORMAT_A8_UNORM = 65,
|
||||
|
||||
DXGI_FORMAT_R1_UNORM = 66,
|
||||
|
||||
DXGI_FORMAT_R9G9B9E5_SHAREDEXP = 67,
|
||||
|
||||
DXGI_FORMAT_R8G8_B8G8_UNORM = 68,
|
||||
DXGI_FORMAT_G8R8_G8B8_UNORM = 69,
|
||||
|
||||
DXGI_FORMAT_BC1_TYPELESS = 70,
|
||||
DXGI_FORMAT_BC1_UNORM = 71,
|
||||
DXGI_FORMAT_BC1_UNORM_SRGB = 72,
|
||||
|
||||
DXGI_FORMAT_BC2_TYPELESS = 73,
|
||||
DXGI_FORMAT_BC2_UNORM = 74,
|
||||
DXGI_FORMAT_BC2_UNORM_SRGB = 75,
|
||||
|
||||
DXGI_FORMAT_BC3_TYPELESS = 76,
|
||||
DXGI_FORMAT_BC3_UNORM = 77,
|
||||
DXGI_FORMAT_BC3_UNORM_SRGB = 78,
|
||||
|
||||
DXGI_FORMAT_BC4_TYPELESS = 79,
|
||||
DXGI_FORMAT_BC4_UNORM = 80,
|
||||
DXGI_FORMAT_BC4_SNORM = 81,
|
||||
|
||||
DXGI_FORMAT_BC5_TYPELESS = 82,
|
||||
DXGI_FORMAT_BC5_UNORM = 83,
|
||||
DXGI_FORMAT_BC5_SNORM = 84,
|
||||
|
||||
DXGI_FORMAT_B5G6R5_UNORM = 85,
|
||||
DXGI_FORMAT_B5G5R5A1_UNORM = 86,
|
||||
DXGI_FORMAT_B8G8R8A8_UNORM = 87,
|
||||
DXGI_FORMAT_B8G8R8X8_UNORM = 88,
|
||||
|
||||
DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM = 89,
|
||||
DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
|
||||
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
|
||||
DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
|
||||
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
|
||||
|
||||
DXGI_FORMAT_BC6H_TYPELESS = 94,
|
||||
DXGI_FORMAT_BC6H_UF16 = 95,
|
||||
DXGI_FORMAT_BC6H_SF16 = 96,
|
||||
|
||||
DXGI_FORMAT_BC7_TYPELESS = 97,
|
||||
DXGI_FORMAT_BC7_UNORM = 98,
|
||||
DXGI_FORMAT_BC7_UNORM_SRGB = 99,
|
||||
|
||||
DXGI_FORMAT_FORCE_UINT = 0xffffffffUL
|
||||
};
|
||||
|
||||
struct DDSPixelFormat
|
||||
{
|
||||
uint32_t size;
|
||||
uint32_t flags;
|
||||
uint32_t fourCC;
|
||||
uint32_t rgbBitCount;
|
||||
uint32_t rBitMask;
|
||||
uint32_t gBitMask;
|
||||
uint32_t bBitMask;
|
||||
uint32_t aBitMask;
|
||||
};
|
||||
|
||||
struct DDSHeader
|
||||
{
|
||||
uint32_t size;
|
||||
uint32_t flags;
|
||||
uint32_t height;
|
||||
uint32_t width;
|
||||
uint32_t pitchOrLinearSize;
|
||||
uint32_t depth;
|
||||
uint32_t mipMapCount;
|
||||
uint32_t reserved[11];
|
||||
|
||||
DDSPixelFormat format;
|
||||
|
||||
uint32_t caps1;
|
||||
uint32_t caps2;
|
||||
uint32_t caps3;
|
||||
uint32_t caps4;
|
||||
uint32_t reserved2;
|
||||
};
|
||||
|
||||
struct DDSHeader10
|
||||
{
|
||||
DXGIFormat dxgiFormat;
|
||||
D3D10ResourceDimension resourceDimension;
|
||||
|
||||
uint32_t miscFlag;
|
||||
uint32_t arraySize;
|
||||
uint32_t reserved;
|
||||
};
|
||||
|
||||
} // dxinfo
|
||||
} // dds
|
||||
|
||||
#endif // DDS_INFO_H
|
||||
@@ -0,0 +1,368 @@
|
||||
/**
|
||||
* Simple DDS data parser for compressed 2D textures.
|
||||
*
|
||||
* Copyright (c) 2013 Alexander Szpakowski.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
**/
|
||||
|
||||
#include "ddsparse.h"
|
||||
#include "ddsinfo.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace dds
|
||||
{
|
||||
|
||||
using namespace dds::dxinfo;
|
||||
|
||||
// Creates a packed uint representation of a FourCC code.
|
||||
static inline uint32_t FourCC(char a, char b, char c, char d)
|
||||
{
|
||||
uint32_t fcc = ((uint32_t) a)
|
||||
| (((uint32_t) b) << 8)
|
||||
| (((uint32_t) c) << 16)
|
||||
| (((uint32_t) d) << 24);
|
||||
|
||||
return fcc;
|
||||
}
|
||||
|
||||
Parser::Image::Image()
|
||||
: width(0)
|
||||
, height(0)
|
||||
, dataSize(0)
|
||||
, data(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool Parser::isDDS(const void *data, size_t dataSize)
|
||||
{
|
||||
const uint8_t *readData = (const uint8_t *) data;
|
||||
ptrdiff_t offset = 0;
|
||||
|
||||
// Is the data large enough to hold the DDS header?
|
||||
if(dataSize < sizeof(uint32_t) + sizeof(DDSHeader))
|
||||
return false;
|
||||
|
||||
// All DDS files start with "DDS ".
|
||||
if((*(uint32_t *) readData) != FourCC('D','D','S',' '))
|
||||
return false;
|
||||
|
||||
offset += sizeof(uint32_t);
|
||||
|
||||
DDSHeader *header = (DDSHeader *) &readData[offset];
|
||||
|
||||
|
||||
// Verify header to validate DDS data.
|
||||
if (header->size != sizeof(DDSHeader) || header->format.size != sizeof(DDSPixelFormat))
|
||||
return false;
|
||||
|
||||
offset += sizeof(DDSHeader);
|
||||
|
||||
// Check for DX10 extension.
|
||||
if ((header->format.flags & DDPF_FOURCC) && (header->format.fourCC == FourCC('D','X','1','0')))
|
||||
{
|
||||
// Data must be big enough for both headers plus the magic value.
|
||||
if (dataSize < (sizeof(uint32_t) + sizeof(DDSHeader) + sizeof(DDSHeader10)))
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Parser::Parser(const void *data, size_t dataSize, Options opts)
|
||||
: format(FORMAT_UNKNOWN)
|
||||
, options(opts)
|
||||
{
|
||||
parseData(data, dataSize);
|
||||
}
|
||||
|
||||
Parser::Parser(const Parser &other)
|
||||
: format(other.format)
|
||||
, options(other.options)
|
||||
{
|
||||
std::vector<Image>::const_iterator it;
|
||||
for (it = other.texData.begin(); it != other.texData.end(); ++it)
|
||||
{
|
||||
Image img = *it;
|
||||
|
||||
if ((options & OPTIONS_COPY_DATA) && img.dataSize > 0)
|
||||
{
|
||||
uint8_t *data = 0;
|
||||
|
||||
try
|
||||
{
|
||||
data = new uint8_t[img.dataSize];
|
||||
}
|
||||
catch (std::exception &)
|
||||
{
|
||||
clearData();
|
||||
throw;
|
||||
}
|
||||
|
||||
img.data = data;
|
||||
}
|
||||
|
||||
texData.push_back(img);
|
||||
}
|
||||
}
|
||||
|
||||
Parser::~Parser()
|
||||
{
|
||||
// Delete any data we created.
|
||||
clearData();
|
||||
}
|
||||
|
||||
Format Parser::getFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
const Parser::Image *Parser::getImageData(size_t miplevel) const
|
||||
{
|
||||
if (miplevel >= texData.size())
|
||||
return 0;
|
||||
|
||||
return &texData[miplevel];
|
||||
}
|
||||
|
||||
size_t Parser::getNumMipmaps() const
|
||||
{
|
||||
return texData.size();
|
||||
}
|
||||
|
||||
bool Parser::ownsData() const
|
||||
{
|
||||
return (options & OPTIONS_COPY_DATA) != 0;
|
||||
}
|
||||
|
||||
Format Parser::parseDDSFormat(const DDSPixelFormat &fmt) const
|
||||
{
|
||||
if (fmt.flags & DDPF_FOURCC)
|
||||
{
|
||||
if (fmt.fourCC == FourCC('D','X','T','1'))
|
||||
return FORMAT_DXT1;
|
||||
else if (fmt.fourCC == FourCC('D','X','T','3'))
|
||||
return FORMAT_DXT3;
|
||||
else if (fmt.fourCC == FourCC('D','X','T','5'))
|
||||
return FORMAT_DXT5;
|
||||
else if (fmt.fourCC == FourCC('A','T','I','2'))
|
||||
return FORMAT_BC5u;
|
||||
}
|
||||
|
||||
return FORMAT_UNKNOWN;
|
||||
}
|
||||
|
||||
Format Parser::parseDX10Format(DXGIFormat fmt) const
|
||||
{
|
||||
Format f = FORMAT_UNKNOWN;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case DXGI_FORMAT_BC1_TYPELESS:
|
||||
case DXGI_FORMAT_BC1_UNORM:
|
||||
case DXGI_FORMAT_BC1_UNORM_SRGB:
|
||||
f = FORMAT_DXT1;
|
||||
break;
|
||||
case DXGI_FORMAT_BC2_TYPELESS:
|
||||
case DXGI_FORMAT_BC2_UNORM:
|
||||
case DXGI_FORMAT_BC2_UNORM_SRGB:
|
||||
f = FORMAT_DXT3;
|
||||
break;
|
||||
case DXGI_FORMAT_BC3_TYPELESS:
|
||||
case DXGI_FORMAT_BC3_UNORM:
|
||||
case DXGI_FORMAT_BC3_UNORM_SRGB:
|
||||
f = FORMAT_DXT5;
|
||||
break;
|
||||
case DXGI_FORMAT_BC5_SNORM:
|
||||
f = FORMAT_BC5s;
|
||||
break;
|
||||
case DXGI_FORMAT_BC5_TYPELESS:
|
||||
case DXGI_FORMAT_BC5_UNORM:
|
||||
f = FORMAT_BC5u;
|
||||
break;
|
||||
case DXGI_FORMAT_BC7_TYPELESS:
|
||||
case DXGI_FORMAT_BC7_UNORM:
|
||||
f = FORMAT_BC7;
|
||||
break;
|
||||
case DXGI_FORMAT_BC7_UNORM_SRGB:
|
||||
f = FORMAT_BC7srgb;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
size_t Parser::parseImageSize(Format fmt, int width, int height) const
|
||||
{
|
||||
size_t size = 0;
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case FORMAT_DXT1:
|
||||
case FORMAT_DXT3:
|
||||
case FORMAT_DXT5:
|
||||
case FORMAT_BC5s:
|
||||
case FORMAT_BC5u:
|
||||
case FORMAT_BC7:
|
||||
case FORMAT_BC7srgb:
|
||||
{
|
||||
int numBlocksWide = 0;
|
||||
if (width > 0)
|
||||
numBlocksWide = std::max(1, (width + 3) / 4);
|
||||
|
||||
int numBlocksHigh = 0;
|
||||
if (height > 0)
|
||||
numBlocksHigh = std::max(1, (height + 3) / 4);
|
||||
|
||||
int numBytesPerBlock = (fmt == FORMAT_DXT1 ? 8 : 16);
|
||||
|
||||
size = numBlocksWide * numBytesPerBlock * numBlocksHigh;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
bool Parser::parseTexData(const uint8_t *data, size_t dataSize, Format fmt, int w, int h, int mips)
|
||||
{
|
||||
size_t offset = 0;
|
||||
|
||||
for (int i = 0; i < mips; i++)
|
||||
{
|
||||
Image img;
|
||||
img.width = w;
|
||||
img.height = h;
|
||||
|
||||
img.dataSize = parseImageSize(fmt, img.width, img.height);
|
||||
|
||||
// Make sure the data size is valid.
|
||||
if (img.dataSize == 0 || (offset + img.dataSize) > dataSize)
|
||||
{
|
||||
// Clean up any data we allocated previously.
|
||||
clearData();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create our own copy of the data, if requested.
|
||||
if (options & OPTIONS_COPY_DATA)
|
||||
{
|
||||
uint8_t *newData = 0;
|
||||
|
||||
try
|
||||
{
|
||||
newData = new uint8_t[img.dataSize];
|
||||
}
|
||||
catch (std::exception &)
|
||||
{
|
||||
// Clean up before throwing.
|
||||
clearData();
|
||||
throw;
|
||||
}
|
||||
|
||||
memcpy(newData, &data[offset], img.dataSize);
|
||||
img.data = newData;
|
||||
}
|
||||
else
|
||||
img.data = &data[offset];
|
||||
|
||||
texData.push_back(img);
|
||||
|
||||
// Move to the next mip level.
|
||||
offset += img.dataSize;
|
||||
|
||||
w = std::max(w / 2, 1);
|
||||
h = std::max(h / 2, 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Parser::parseData(const void *data, size_t dataSize)
|
||||
{
|
||||
if (!isDDS(data, dataSize))
|
||||
return false;
|
||||
|
||||
const uint8_t *readData = (const uint8_t *) data;
|
||||
ptrdiff_t offset = sizeof(uint32_t);
|
||||
|
||||
DDSHeader *header = (DDSHeader *) &readData[offset];
|
||||
offset += sizeof(DDSHeader);
|
||||
|
||||
|
||||
// Check for DX10 extension.
|
||||
if ((header->format.flags & DDPF_FOURCC) && (header->format.fourCC == FourCC('D','X','1','0')))
|
||||
{
|
||||
DDSHeader10 *header10 = (DDSHeader10 *) &readData[offset];
|
||||
offset += sizeof(DDSHeader10);
|
||||
|
||||
// We can't deal with 1D/3D textures.
|
||||
switch (header10->resourceDimension)
|
||||
{
|
||||
case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
|
||||
case D3D10_RESOURCE_DIMENSION_UNKNOWN:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// We also can't deal with texture arrays and cubemaps.
|
||||
if (header10->arraySize > 1)
|
||||
return false;
|
||||
|
||||
format = parseDX10Format(header10->dxgiFormat);
|
||||
}
|
||||
else
|
||||
format = parseDDSFormat(header->format);
|
||||
|
||||
|
||||
if (format == FORMAT_UNKNOWN)
|
||||
return false;
|
||||
|
||||
|
||||
int w = header->width;
|
||||
int h = header->height;
|
||||
|
||||
int mips = std::max((int) header->mipMapCount, 1);
|
||||
|
||||
return parseTexData(&readData[offset], dataSize - offset, format, w, h, mips);
|
||||
}
|
||||
|
||||
void Parser::clearData()
|
||||
{
|
||||
if (options & OPTIONS_COPY_DATA)
|
||||
{
|
||||
// Delete any data we created.
|
||||
std::vector<Image>::iterator it;
|
||||
for (it = texData.begin(); it != texData.end(); ++it)
|
||||
{
|
||||
delete[] it->data;
|
||||
it->data = 0;
|
||||
}
|
||||
}
|
||||
|
||||
texData.clear();
|
||||
}
|
||||
|
||||
} // dds
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Simple DDS data parser for compressed 2D textures.
|
||||
*
|
||||
* Copyright (c) 2013 Alexander Szpakowski.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
**/
|
||||
|
||||
#ifndef DDS_PARSE_H
|
||||
#define DDS_PARSE_H
|
||||
|
||||
#include "ddsinfo.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
|
||||
namespace dds
|
||||
{
|
||||
|
||||
// Supported DDS formats.
|
||||
enum Format
|
||||
{
|
||||
FORMAT_DXT1,
|
||||
FORMAT_DXT3,
|
||||
FORMAT_DXT5,
|
||||
FORMAT_BC5s, // Signed.
|
||||
FORMAT_BC5u, // Unsigned.
|
||||
FORMAT_BC7,
|
||||
FORMAT_BC7srgb, // sRGB color space.
|
||||
FORMAT_UNKNOWN
|
||||
};
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
|
||||
enum Options
|
||||
{
|
||||
OPTIONS_NONE = 0x00,
|
||||
OPTIONS_COPY_DATA = 0x01 // Copy texture data internally when parsing.
|
||||
};
|
||||
|
||||
// Represents a single mipmap level of a texture.
|
||||
struct Image
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
size_t dataSize;
|
||||
const uint8_t *data;
|
||||
|
||||
Image();
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines whether the input byte data is a valid DDS representation.
|
||||
**/
|
||||
static bool isDDS(const void *data, size_t dataSize);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Attempts to parse byte data as DDS. May throw std::bad_alloc if out of
|
||||
* memory.
|
||||
* @param data The byte data to parse.
|
||||
* @param dataSize The size in bytes of the data.
|
||||
* @param options Any optional settings (see above.)
|
||||
**/
|
||||
Parser(const void *data, size_t dataSize, Options opts = OPTIONS_NONE);
|
||||
Parser(const Parser &other);
|
||||
|
||||
~Parser();
|
||||
|
||||
/**
|
||||
* Gets the format of this texture.
|
||||
**/
|
||||
Format getFormat() const;
|
||||
|
||||
/**
|
||||
* Gets the data of this texture at a mipmap level. Mipmap level 0
|
||||
* represents the base image.
|
||||
* @param miplevel The mipmap level to get the data of.
|
||||
* @return Pointer to the image data, or 0 if miplevel is not within the
|
||||
* range of [0, numMipmaps).
|
||||
**/
|
||||
const Image *getImageData(size_t miplevel = 0) const;
|
||||
|
||||
/**
|
||||
* Gets the number of mipmap levels in this texture.
|
||||
* Includes the base mip level.
|
||||
**/
|
||||
size_t getNumMipmaps() const;
|
||||
|
||||
/**
|
||||
* Gets whether this Parser is using an internal copy of the texture data.
|
||||
* Accessing texture data from a Parser which hasn't internally copied the
|
||||
* DDS data will result in undefined behaviour if the original data is deleted.
|
||||
**/
|
||||
bool ownsData() const;
|
||||
|
||||
private:
|
||||
|
||||
Format parseDDSFormat(const dxinfo::DDSPixelFormat &fmt) const;
|
||||
Format parseDX10Format(dxinfo::DXGIFormat fmt) const;
|
||||
size_t parseImageSize(Format fmt, int width, int height) const;
|
||||
|
||||
bool parseTexData(const uint8_t *data, size_t dataSize, Format fmt, int w, int h, int mips);
|
||||
|
||||
bool parseData(const void *data, size_t dataSize);
|
||||
|
||||
// Delete any heap data created by this object.
|
||||
void clearData();
|
||||
|
||||
std::vector<Image> texData;
|
||||
Format format;
|
||||
Options options;
|
||||
|
||||
}; // Parser
|
||||
|
||||
} // dds
|
||||
|
||||
#endif // DDS_PARSE_H
|
||||
Reference in New Issue
Block a user