SWORD25: Merged the PNG and thumbnail decoding code into a common class

This commit is contained in:
md5 2011-05-26 12:29:51 +03:00
parent a654115f1a
commit 62c026d3b6
4 changed files with 40 additions and 36 deletions

View File

@ -31,13 +31,13 @@
#include "common/memstream.h"
#include "sword25/gfx/image/image.h"
#include "sword25/gfx/image/pngloader.h"
#include "sword25/gfx/image/imgloader.h"
#include "graphics/pixelformat.h"
#include "graphics/png.h"
namespace Sword25 {
bool PNGLoader::decodeImage(const byte *fileDataPtr, uint fileSize, byte *&uncompressedDataPtr, int &width, int &height, int &pitch) {
bool ImgLoader::decodePNGImage(const byte *fileDataPtr, uint fileSize, byte *&uncompressedDataPtr, int &width, int &height, int &pitch) {
Common::MemoryReadStream *fileStr = new Common::MemoryReadStream(fileDataPtr, fileSize, DisposeAfterUse::NO);
Graphics::PNG *png = new Graphics::PNG();
if (!png->read(fileStr)) // the fileStr pointer, and thus pFileData will be deleted after this is done
@ -59,4 +59,27 @@ bool PNGLoader::decodeImage(const byte *fileDataPtr, uint fileSize, byte *&uncom
return true;
}
bool ImgLoader::decodeThumbnailImage(const byte *pFileData, uint fileSize, byte *&pUncompressedData, int &width, int &height, int &pitch) {
const byte *src = pFileData + 4; // skip header
width = READ_LE_UINT16(src); src += 2;
height = READ_LE_UINT16(src); src += 2;
src++; // version, ignored for now
pitch = width * 4;
uint32 totalSize = pitch * height;
pUncompressedData = new byte[totalSize];
uint32 *dst = (uint32 *)pUncompressedData; // treat as uint32, for pixelformat output
const Graphics::PixelFormat format = Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24);
byte r, g, b;
for (uint32 i = 0; i < totalSize / 4; i++) {
r = *src++;
g = *src++;
b = *src++;
*dst++ = format.RGBToColor(r, g, b);
}
return true;
}
} // End of namespace Sword25

View File

@ -29,8 +29,8 @@
*
*/
#ifndef SWORD25_PNGLOADER2_H
#define SWORD25_PNGLOADER2_H
#ifndef SWORD25_IMGLOADER_H
#define SWORD25_IMGLOADER_H
#include "sword25/kernel/common.h"
#include "sword25/gfx/graphicengine.h"
@ -42,9 +42,9 @@ namespace Sword25 {
*
* Originally written by Malte Thiesen.
*/
class PNGLoader {
class ImgLoader {
protected:
PNGLoader() {} // Protected constructor to prevent instances
ImgLoader() {} // Protected constructor to prevent instances
public:
@ -62,7 +62,12 @@ public:
* @remark This function does not free the image buffer passed to it,
* it is the callers responsibility to do so.
*/
static bool decodeImage(const byte *pFileData, uint fileSize,
static bool decodePNGImage(const byte *pFileData, uint fileSize,
byte *&pUncompressedData,
int &width, int &height,
int &pitch);
static bool decodeThumbnailImage(const byte *pFileData, uint fileSize,
byte *&pUncompressedData,
int &width, int &height,
int &pitch);

View File

@ -35,7 +35,7 @@
#include "common/savefile.h"
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/pngloader.h"
#include "sword25/gfx/image/imgloader.h"
#include "sword25/gfx/image/renderedimage.h"
#include "common/system.h"
@ -93,30 +93,6 @@ static byte *readSavegameThumbnail(const Common::String &filename, uint &fileSiz
return pFileData;
}
// TODO: Move this method into a more generic image loading class, together with the PNG reading code
static bool decodeThumbnail(const byte *pFileData, uint fileSize, byte *&pUncompressedData, int &width, int &height, int &pitch) {
const byte *src = pFileData + 4; // skip header
width = READ_LE_UINT16(src); src += 2;
height = READ_LE_UINT16(src); src += 2;
src++; // version, ignored for now
pitch = width * 4;
uint32 totalSize = pitch * height;
pUncompressedData = new byte[totalSize];
uint32 *dst = (uint32 *)pUncompressedData; // treat as uint32, for pixelformat output
const Graphics::PixelFormat format = Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24);
byte r, g, b;
for (uint32 i = 0; i < totalSize / 4; i++) {
r = *src++;
g = *src++;
b = *src++;
*dst++ = format.RGBToColor(r, g, b);
}
return true;
}
RenderedImage::RenderedImage(const Common::String &filename, bool &result) :
_data(0),
_width(0),
@ -148,9 +124,9 @@ RenderedImage::RenderedImage(const Common::String &filename, bool &result) :
// Uncompress the image
int pitch;
if (isPNG)
result = PNGLoader::decodeImage(pFileData, fileSize, _data, _width, _height, pitch);
result = ImgLoader::decodePNGImage(pFileData, fileSize, _data, _width, _height, pitch);
else
result = decodeThumbnail(pFileData, fileSize, _data, _width, _height, pitch);
result = ImgLoader::decodeThumbnailImage(pFileData, fileSize, _data, _width, _height, pitch);
if (!result) {
error("Could not decode image.");

View File

@ -30,7 +30,7 @@
*/
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/pngloader.h"
#include "sword25/gfx/image/imgloader.h"
#include "sword25/gfx/image/swimage.h"
namespace Sword25 {
@ -56,7 +56,7 @@ SWImage::SWImage(const Common::String &filename, bool &result) :
// Uncompress the image
int pitch;
byte *pUncompressedData;
if (!PNGLoader::decodeImage(pFileData, fileSize, pUncompressedData, _width, _height, pitch)) {
if (!ImgLoader::decodePNGImage(pFileData, fileSize, pUncompressedData, _width, _height, pitch)) {
error("Could not decode image.");
return;
}