Simplified some compressed image parsing code.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-08-30 22:25:26 -03:00
parent 907630b04c
commit a41f78dfe4
26 changed files with 101 additions and 161 deletions
@@ -1,70 +0,0 @@
/**
* Copyright (c) 2006-2017 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#pragma once
// LOVE
#include "common/Object.h"
#include "common/Data.h"
#include "CompressedSlice.h"
namespace love
{
namespace image
{
/**
* Base class for all CompressedImageData parser library interfaces.
* We inherit from love::Object to take advantage of reference counting...
**/
class CompressedFormatHandler : public love::Object
{
public:
CompressedFormatHandler() {}
virtual ~CompressedFormatHandler() {}
/**
* Determines whether a particular FileData can be parsed as
* CompressedImageData by this handler.
* @param data The data to parse.
**/
virtual bool canParse(const Data *data) = 0;
/**
* Parses compressed image filedata into a list of sub-images and returns
* a single block of memory containing all the images.
*
* @param[in] filedata The data to parse.
* @param[out] images The list of sub-images generated. Byte data is a
* pointer to the returned data.
* @param[out] format The format of the Compressed Data.
* @param[out] sRGB Whether the texture is sRGB-encoded.
*
* @return The single block of memory containing the parsed images.
**/
virtual StrongRef<CompressedMemory> parse(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) = 0;
}; // CompressedFormatHandler
} // image
} // love
+5 -5
View File
@@ -27,15 +27,15 @@ namespace image
love::Type CompressedImageData::type("CompressedImageData", &Data::type);
CompressedImageData::CompressedImageData(const std::list<CompressedFormatHandler *> &formats, Data *filedata)
CompressedImageData::CompressedImageData(const std::list<FormatHandler *> &formats, Data *filedata)
: format(PIXELFORMAT_UNKNOWN)
, sRGB(false)
{
CompressedFormatHandler *parser = nullptr;
FormatHandler *parser = nullptr;
for (CompressedFormatHandler *handler : formats)
for (FormatHandler *handler : formats)
{
if (handler->canParse(filedata))
if (handler->canParseCompressed(filedata))
{
parser = handler;
break;
@@ -45,7 +45,7 @@ CompressedImageData::CompressedImageData(const std::list<CompressedFormatHandler
if (parser == nullptr)
throw love::Exception("Could not parse compressed data: Unknown format.");
memory = parser->parse(filedata, dataImages, format, sRGB);
memory = parser->parseCompressed(filedata, dataImages, format, sRGB);
if (memory == nullptr)
throw love::Exception("Could not parse compressed data.");
+2 -2
View File
@@ -26,7 +26,7 @@
#include "common/int.h"
#include "common/pixelformat.h"
#include "CompressedSlice.h"
#include "CompressedFormatHandler.h"
#include "FormatHandler.h"
// STL
#include <vector>
@@ -48,7 +48,7 @@ public:
static love::Type type;
CompressedImageData(const std::list<CompressedFormatHandler *> &formats, Data *filedata);
CompressedImageData(const std::list<FormatHandler *> &formats, Data *filedata);
CompressedImageData(const CompressedImageData &c);
virtual ~CompressedImageData();
+11 -1
View File
@@ -55,7 +55,17 @@ FormatHandler::EncodedImage FormatHandler::encode(const DecodedImage& /*img*/, E
throw love::Exception("Image encoding is not implemented for this format backend.");
}
void FormatHandler::free(unsigned char *mem)
bool FormatHandler::canParseCompressed(Data* /*data*/)
{
return false;
}
StrongRef<CompressedMemory> FormatHandler::parseCompressed(Data* /*filedata*/, std::vector<StrongRef<CompressedSlice>>& /*images*/, PixelFormat& /*format*/, bool& /*sRGB*/)
{
throw love::Exception("Compressed image parsing is not implemented for this format backend.");
}
void FormatHandler::freeRawPixels(unsigned char *mem)
{
delete[] mem;
}
+30 -16
View File
@@ -22,8 +22,9 @@
// LOVE
#include "common/Object.h"
#include "common/pixelformat.h"
#include "common/Data.h"
#include "common/pixelformat.h"
#include "CompressedSlice.h"
namespace love
{
@@ -31,8 +32,8 @@ namespace image
{
/**
* Base class for all ImageData encoder/decoder library interfaces.
* We inherit from love::Object to take advantage of reference counting...
* Base class for all ImageData and CompressedImageData encoder/decoder library
* interfaces. We inherit from love::Object to take advantage of ref coounting.
**/
class FormatHandler : public love::Object
{
@@ -66,41 +67,54 @@ public:
* The default constructor is called when the Image module is initialized.
**/
FormatHandler();
/**
* The destructor is called when the Image module is uninitialized.
**/
virtual ~FormatHandler();
/**
* Whether this format handler can decode a particular FileData.
* Whether this format handler can decode the given Data into raw pixels.
**/
virtual bool canDecode(Data *data);
/**
* Whether this format handler can encode to a particular format.
* Whether this format handler can encode raw pixels to a particular format.
**/
virtual bool canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat);
/**
* Decodes an image from its encoded form into raw pixel data.
* @param data The encoded data to decode.
* @return The decoded pixel data.
**/
virtual DecodedImage decode(Data *data);
/**
* Encodes an image from raw pixel data into a particular format.
* @param img The raw image data to encode.
* @param format The format to encode to.
* @return The encoded image data.
**/
virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format);
/**
* Frees memory allocated by the format handler.
* Whether this format handler can parse the given Data into a
* CompressedImageData object.
**/
virtual void free(unsigned char *mem);
virtual bool canParseCompressed(Data *data);
/**
* Parses compressed image data into a list of sub-images and returns a
* single block of memory containing all the images.
*
* @param[in] filedata The data to parse.
* @param[out] images The list of sub-images generated. Byte data is a
* pointer to the returned data.
* @param[out] format The format of the Compressed Data.
* @param[out] sRGB Whether the texture is sRGB-encoded.
*
* @return The single block of memory containing the parsed images.
**/
virtual StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB);
/**
* Frees raw pixel memory allocated by the format handler.
**/
virtual void freeRawPixels(unsigned char *mem);
}; // FormatHandler
+3 -9
View File
@@ -49,9 +49,6 @@ Image::Image()
new PNGHandler,
new STBHandler,
new EXRHandler,
};
compressedFormatHandlers = {
new DDSHandler,
new PVRHandler,
new KTXHandler,
@@ -66,9 +63,6 @@ Image::~Image()
// release them instead of deleting them completely here.
for (FormatHandler *handler : formatHandlers)
handler->release();
for (CompressedFormatHandler *handler : compressedFormatHandlers)
handler->release();
}
const char *Image::getName() const
@@ -93,14 +87,14 @@ love::image::ImageData *Image::newImageData(int width, int height, PixelFormat f
love::image::CompressedImageData *Image::newCompressedData(Data *data)
{
return new CompressedImageData(compressedFormatHandlers, data);
return new CompressedImageData(formatHandlers, data);
}
bool Image::isCompressed(Data *data)
{
for (CompressedFormatHandler *handler : compressedFormatHandlers)
for (FormatHandler *handler : formatHandlers)
{
if (handler->canParse(data))
if (handler->canParseCompressed(data))
return true;
}
-3
View File
@@ -107,9 +107,6 @@ private:
// Image format handlers we can use for decoding and encoding ImageData.
std::list<FormatHandler *> formatHandlers;
// Compressed image format handers we can use for parsing CompressedImageData.
std::list<CompressedFormatHandler *> compressedFormatHandlers;
}; // Image
} // image
+5 -5
View File
@@ -78,7 +78,7 @@ ImageData::ImageData(const ImageData &c)
ImageData::~ImageData()
{
if (decodeHandler.get())
decodeHandler->free(data);
decodeHandler->freeRawPixels(data);
else
delete[] data;
}
@@ -145,13 +145,13 @@ void ImageData::decode(Data *data)
if (decodedimage.size != decodedimage.width * decodedimage.height * getPixelFormatSize(decodedimage.format))
{
decoder->free(decodedimage.data);
decoder->freeRawPixels(decodedimage.data);
throw love::Exception("Could not convert image!");
}
// Clean up any old data.
if (decodeHandler)
decodeHandler->free(this->data);
decodeHandler->freeRawPixels(this->data);
else
delete[] this->data;
@@ -210,12 +210,12 @@ love::filesystem::FileData *ImageData::encode(FormatHandler::EncodedFormat encod
}
catch (love::Exception &)
{
encoder->free(encodedimage.data);
encoder->freeRawPixels(encodedimage.data);
throw;
}
memcpy(filedata->getData(), encodedimage.data, encodedimage.size);
encoder->free(encodedimage.data);
encoder->freeRawPixels(encodedimage.data);
if (writefile)
{
+3 -3
View File
@@ -87,7 +87,7 @@ static PixelFormat convertFormat(uint32 blockX, uint32 blockY, uint32 blockZ)
} // Anonymous namespace.
bool ASTCHandler::canParse(const Data *data)
bool ASTCHandler::canParseCompressed(Data *data)
{
if (data->getSize() <= sizeof(ASTCHeader))
return false;
@@ -105,9 +105,9 @@ bool ASTCHandler::canParse(const Data *data)
return true;
}
StrongRef<CompressedMemory> ASTCHandler::parse(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
StrongRef<CompressedMemory> ASTCHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
{
if (!canParse(filedata))
if (!canParseCompressed(filedata))
throw love::Exception("Could not decode compressed data (not an .astc file?)");
ASTCHeader header = *(const ASTCHeader *) filedata->getData();
+5 -5
View File
@@ -21,7 +21,7 @@
#pragma once
#include "common/config.h"
#include "image/CompressedFormatHandler.h"
#include "image/FormatHandler.h"
namespace love
{
@@ -34,16 +34,16 @@ namespace magpie
* Handles simple .astc files (generated by ARM's astcenc tool) with compressed
* ASTC data inside.
**/
class ASTCHandler : public CompressedFormatHandler
class ASTCHandler : public FormatHandler
{
public:
virtual ~ASTCHandler() {}
// Implements CompressedFormatHandler.
bool canParse(const Data *data) override;
// Implements FormatHandler.
bool canParseCompressed(Data *data) override;
StrongRef<CompressedMemory> parse(Data *filedata,
StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) override;
+1 -1
View File
@@ -193,7 +193,7 @@ FormatHandler::EncodedImage EXRHandler::encode(const DecodedImage & /*img*/, Enc
throw love::Exception("Invalid format.");
}
void EXRHandler::free(unsigned char *mem)
void EXRHandler::freeRawPixels(unsigned char *mem)
{
delete[] mem;
}
+1 -1
View File
@@ -44,7 +44,7 @@ public:
virtual DecodedImage decode(Data *data);
virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format);
virtual void free(unsigned char *mem);
virtual void freeRawPixels(unsigned char *mem);
}; // EXRHandler
+3 -3
View File
@@ -281,7 +281,7 @@ PixelFormat convertFormat(uint32 glformat, bool &sRGB)
} // Anonymous namespace.
bool KTXHandler::canParse(const Data *data)
bool KTXHandler::canParseCompressed(Data *data)
{
if (data->getSize() < sizeof(KTXHeader))
return false;
@@ -298,9 +298,9 @@ bool KTXHandler::canParse(const Data *data)
return true;
}
StrongRef<CompressedMemory> KTXHandler::parse(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
StrongRef<CompressedMemory> KTXHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
{
if (!canParse(filedata))
if (!canParseCompressed(filedata))
throw love::Exception("Could not decode compressed data (not a KTX file?)");
KTXHeader header = *(KTXHeader *) filedata->getData();
+5 -5
View File
@@ -21,7 +21,7 @@
#pragma once
#include "common/config.h"
#include "image/CompressedFormatHandler.h"
#include "image/FormatHandler.h"
namespace love
{
@@ -33,16 +33,16 @@ namespace magpie
/**
* Handles KTX files with compressed image data inside.
**/
class KTXHandler : public CompressedFormatHandler
class KTXHandler : public FormatHandler
{
public:
virtual ~KTXHandler() {}
// Implements CompressedFormatHandler.
bool canParse(const Data *data) override;
// Implements FormatHandler.
bool canParseCompressed(Data *data) override;
StrongRef<CompressedMemory> parse(Data *filedata,
StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) override;
+3 -3
View File
@@ -97,7 +97,7 @@ static PixelFormat convertFormat(uint16 texformat)
} // Anonymous namespace.
bool PKMHandler::canParse(const Data *data)
bool PKMHandler::canParseCompressed(Data *data)
{
if (data->getSize() <= sizeof(PKMHeader))
return false;
@@ -114,9 +114,9 @@ bool PKMHandler::canParse(const Data *data)
return true;
}
StrongRef<CompressedMemory> PKMHandler::parse(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
StrongRef<CompressedMemory> PKMHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
{
if (!canParse(filedata))
if (!canParseCompressed(filedata))
throw love::Exception("Could not decode compressed data (not a PKM file?)");
PKMHeader header = *(const PKMHeader *) filedata->getData();
+5 -5
View File
@@ -21,7 +21,7 @@
#pragma once
#include "common/config.h"
#include "image/CompressedFormatHandler.h"
#include "image/FormatHandler.h"
namespace love
{
@@ -33,16 +33,16 @@ namespace magpie
/**
* Handles PKM files with compressed ETC data inside.
**/
class PKMHandler : public CompressedFormatHandler
class PKMHandler : public FormatHandler
{
public:
virtual ~PKMHandler() {}
// Implements CompressedFormatHandler.
bool canParse(const Data *data) override;
// Implements FormatHandler.
bool canParseCompressed(Data *data) override;
StrongRef<CompressedMemory> parse(Data *filedata,
StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) override;
+1 -1
View File
@@ -257,7 +257,7 @@ FormatHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, EncodedF
return encimg;
}
void PNGHandler::free(unsigned char *mem)
void PNGHandler::freeRawPixels(unsigned char *mem)
{
// LodePNG uses malloc, realloc, and free.
if (mem)
+1 -1
View File
@@ -45,7 +45,7 @@ public:
virtual DecodedImage decode(Data *data);
virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format);
virtual void free(unsigned char *mem);
virtual void freeRawPixels(unsigned char *mem);
}; // PNGHandler
+3 -3
View File
@@ -454,7 +454,7 @@ size_t getMipLevelSize(const PVRTexHeaderV3 &header, int miplevel)
} // Anonymous namespace.
bool PVRHandler::canParse(const Data *data)
bool PVRHandler::canParseCompressed(Data *data)
{
if (data->getSize() < sizeof(PVRTexHeaderV2) || data->getSize() < sizeof(PVRTexHeaderV3))
return false;
@@ -475,9 +475,9 @@ bool PVRHandler::canParse(const Data *data)
return false;
}
StrongRef<CompressedMemory> PVRHandler::parse(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
StrongRef<CompressedMemory> PVRHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
{
if (!canParse(filedata))
if (!canParseCompressed(filedata))
throw love::Exception("Could not decode compressed data (not a PVR file?)");
PVRTexHeaderV3 header3 = *(PVRTexHeaderV3 *) filedata->getData();
+5 -5
View File
@@ -22,7 +22,7 @@
// LOVE
#include "common/config.h"
#include "image/CompressedFormatHandler.h"
#include "image/FormatHandler.h"
namespace love
{
@@ -31,16 +31,16 @@ namespace image
namespace magpie
{
class PVRHandler : public CompressedFormatHandler
class PVRHandler : public FormatHandler
{
public:
virtual ~PVRHandler() {}
// Implements CompressedFormatHandler.
bool canParse(const Data *data) override;
// Implements FormatHandler.
bool canParseCompressed(Data *data) override;
StrongRef<CompressedMemory> parse(Data *filedata,
StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) override;
+1 -1
View File
@@ -162,7 +162,7 @@ FormatHandler::EncodedImage STBHandler::encode(const DecodedImage &img, EncodedF
return encimg;
}
void STBHandler::free(unsigned char *mem)
void STBHandler::freeRawPixels(unsigned char *mem)
{
// The STB decoder gave memory allocated directly by stb_image to the
// ImageData, so we use stb_image_free to delete it.
+1 -1
View File
@@ -48,7 +48,7 @@ public:
DecodedImage decode(Data *data) override;
EncodedImage encode(const DecodedImage &img, EncodedFormat format) override;
void free(unsigned char *mem) override;
void freeRawPixels(unsigned char *mem) override;
}; // STBHandler
+2 -2
View File
@@ -28,12 +28,12 @@ namespace image
namespace magpie
{
bool DDSHandler::canParse(const Data *data)
bool DDSHandler::canParseCompressed(Data *data)
{
return dds::isCompressedDDS(data->getData(), data->getSize());
}
StrongRef<CompressedMemory> DDSHandler::parse(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
StrongRef<CompressedMemory> DDSHandler::parseCompressed(Data *filedata, std::vector<StrongRef<CompressedSlice>> &images, PixelFormat &format, bool &sRGB)
{
if (!dds::isDDS(filedata->getData(), filedata->getSize()))
throw love::Exception("Could not decode compressed data (not a DDS file?)");
+5 -5
View File
@@ -21,7 +21,7 @@
#pragma once
// LOVE
#include "image/CompressedFormatHandler.h"
#include "image/FormatHandler.h"
// dds parser
#include "ddsparse/ddsparse.h"
@@ -39,16 +39,16 @@ namespace magpie
/**
* Interface between CompressedImageData and the ddsparse library.
**/
class DDSHandler : public CompressedFormatHandler
class DDSHandler : public FormatHandler
{
public:
virtual ~DDSHandler() {}
// Implements CompressedFormatHandler.
bool canParse(const Data *data) override;
// Implements FormatHandler.
bool canParseCompressed(Data *data) override;
StrongRef<CompressedMemory> parse(Data *filedata,
StrongRef<CompressedMemory> parseCompressed(Data *filedata,
std::vector<StrongRef<CompressedSlice>> &images,
PixelFormat &format, bool &sRGB) override;