Christian Walther
13 years ago
4 changed files with 495 additions and 495 deletions
@ -1,136 +1,136 @@ |
|||||||
/*==LICENSE==*
|
/*==LICENSE==*
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
CyanWorlds.com Engine - MMOG client, server and tools |
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
Copyright (C) 2011 Cyan Worlds, Inc. |
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
This program is free software: you can redistribute it and/or modify |
||||||
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
||||||
the Free Software Foundation, either version 3 of the License, or |
the Free Software Foundation, either version 3 of the License, or |
||||||
(at your option) any later version. |
(at your option) any later version. |
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
GNU General Public License for more details. |
GNU General Public License for more details. |
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||||
or by snail mail at: |
or by snail mail at: |
||||||
Cyan Worlds, Inc. |
Cyan Worlds, Inc. |
||||||
14617 N Newport Hwy |
14617 N Newport Hwy |
||||||
Mead, WA 99021 |
Mead, WA 99021 |
||||||
|
|
||||||
*==LICENSE==*/ |
*==LICENSE==*/ |
||||||
|
|
||||||
#include "hsTypes.h" |
#include "hsTypes.h" |
||||||
#include "hsUtils.h" |
#include "hsUtils.h" |
||||||
#include "hsStream.h" |
#include "hsStream.h" |
||||||
#include "hsResMgr.h" |
#include "hsResMgr.h" |
||||||
#include "plJPEG/plJPEG.h" |
#include "plJPEG/plJPEG.h" |
||||||
#include "plGImage/plPNG.h" |
#include "plGImage/plPNG.h" |
||||||
#include "plGImage/plMipmap.h" |
#include "plGImage/plMipmap.h" |
||||||
|
|
||||||
#include "plClientResMgr.h" |
#include "plClientResMgr.h" |
||||||
|
|
||||||
|
|
||||||
//// Singleton Instance ///////////////////////////////////////////////////////
|
//// Singleton Instance ///////////////////////////////////////////////////////
|
||||||
|
|
||||||
plClientResMgr& plClientResMgr::Instance(void) |
plClientResMgr& plClientResMgr::Instance(void) |
||||||
{ |
{ |
||||||
static plClientResMgr theInstance; |
static plClientResMgr theInstance; |
||||||
return theInstance; |
return theInstance; |
||||||
} |
} |
||||||
|
|
||||||
plClientResMgr::plClientResMgr() |
plClientResMgr::plClientResMgr() |
||||||
{ |
{ |
||||||
this->ClientResources = TRACKED_NEW std::map<std::string, plMipmap*>; |
this->ClientResources = TRACKED_NEW std::map<std::string, plMipmap*>; |
||||||
} |
} |
||||||
|
|
||||||
plClientResMgr::~plClientResMgr() |
plClientResMgr::~plClientResMgr() |
||||||
{ |
{ |
||||||
if (this->ClientResources) { |
if (this->ClientResources) { |
||||||
std::map<std::string, plMipmap*>::iterator it; |
std::map<std::string, plMipmap*>::iterator it; |
||||||
|
|
||||||
for (it = this->ClientResources->begin(); it != this->ClientResources->end(); ++it) { |
for (it = this->ClientResources->begin(); it != this->ClientResources->end(); ++it) { |
||||||
if (it->second) |
if (it->second) |
||||||
it->second->UnRef(); |
it->second->UnRef(); |
||||||
} |
} |
||||||
|
|
||||||
delete this->ClientResources; |
delete this->ClientResources; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
void plClientResMgr::ILoadResources(const char* resfile) |
void plClientResMgr::ILoadResources(const char* resfile) |
||||||
{ |
{ |
||||||
if (!resfile) { |
if (!resfile) { |
||||||
return; |
return; |
||||||
} |
} |
||||||
|
|
||||||
wchar* wFilename = hsStringToWString(resfile); |
wchar* wFilename = hsStringToWString(resfile); |
||||||
hsUNIXStream in; |
hsUNIXStream in; |
||||||
|
|
||||||
if (in.Open(wFilename, L"rb")) { |
if (in.Open(wFilename, L"rb")) { |
||||||
UInt32 header = in.ReadSwap32(); |
UInt32 header = in.ReadSwap32(); |
||||||
UInt32 version = in.ReadSwap32(); |
UInt32 version = in.ReadSwap32(); |
||||||
UInt32 num_resources = 0; |
UInt32 num_resources = 0; |
||||||
|
|
||||||
switch (version) { |
switch (version) { |
||||||
case 1: |
case 1: |
||||||
num_resources = in.ReadSwap32(); |
num_resources = in.ReadSwap32(); |
||||||
|
|
||||||
for (int i = 0; i < num_resources; i++) { |
for (int i = 0; i < num_resources; i++) { |
||||||
plMipmap* res_data = NULL; |
plMipmap* res_data = NULL; |
||||||
UInt32 res_size = 0; |
UInt32 res_size = 0; |
||||||
char* tmp_name = in.ReadSafeStringLong(); |
char* tmp_name = in.ReadSafeStringLong(); |
||||||
std::string res_name = std::string(tmp_name); |
std::string res_name = std::string(tmp_name); |
||||||
std::string res_type = res_name.substr(res_name.length() - 4, 4); |
std::string res_type = res_name.substr(res_name.length() - 4, 4); |
||||||
delete tmp_name; |
delete tmp_name; |
||||||
|
|
||||||
// Version 1 doesn't encode format, so we'll try some simple
|
// Version 1 doesn't encode format, so we'll try some simple
|
||||||
// extension sniffing
|
// extension sniffing
|
||||||
if (res_type == ".png") { |
if (res_type == ".png") { |
||||||
// Read resource stream size, but the PNG has that info in the header
|
// Read resource stream size, but the PNG has that info in the header
|
||||||
// so it's not needed
|
// so it's not needed
|
||||||
res_size = in.ReadSwap32(); |
res_size = in.ReadSwap32(); |
||||||
res_data = plPNG::Instance().ReadFromStream(&in); |
res_data = plPNG::Instance().ReadFromStream(&in); |
||||||
} else if (res_type == ".jpg") { |
} else if (res_type == ".jpg") { |
||||||
// Don't read resource stream size, as plJPEG's reader will need it
|
// Don't read resource stream size, as plJPEG's reader will need it
|
||||||
res_data = plJPEG::Instance().ReadFromStream(&in); |
res_data = plJPEG::Instance().ReadFromStream(&in); |
||||||
} else { |
} else { |
||||||
// Original Myst5 format only is known to support Targa,
|
// Original Myst5 format only is known to support Targa,
|
||||||
// so default fallback is targa
|
// so default fallback is targa
|
||||||
// TODO - Add plTarga::ReadFromStream()
|
// TODO - Add plTarga::ReadFromStream()
|
||||||
// for now, just skip the unknown resource and put NULL into the map
|
// for now, just skip the unknown resource and put NULL into the map
|
||||||
res_size = in.ReadSwap32(); |
res_size = in.ReadSwap32(); |
||||||
in.Skip(res_size); |
in.Skip(res_size); |
||||||
} |
} |
||||||
|
|
||||||
(*this->ClientResources)[res_name] = res_data; |
(*this->ClientResources)[res_name] = res_data; |
||||||
} |
} |
||||||
|
|
||||||
break; |
break; |
||||||
default: |
default: |
||||||
break; |
break; |
||||||
} |
} |
||||||
|
|
||||||
in.Close(); |
in.Close(); |
||||||
} |
} |
||||||
|
|
||||||
delete wFilename; |
delete wFilename; |
||||||
} |
} |
||||||
|
|
||||||
plMipmap* plClientResMgr::getResource(const char* resname) |
plMipmap* plClientResMgr::getResource(const char* resname) |
||||||
{ |
{ |
||||||
plMipmap* resmipmap = NULL; |
plMipmap* resmipmap = NULL; |
||||||
std::map<std::string, plMipmap*>::iterator it = this->ClientResources->find(resname); |
std::map<std::string, plMipmap*>::iterator it = this->ClientResources->find(resname); |
||||||
|
|
||||||
if (it != this->ClientResources->end()) { |
if (it != this->ClientResources->end()) { |
||||||
resmipmap = it->second; |
resmipmap = it->second; |
||||||
} else { |
} else { |
||||||
hsAssert(resmipmap, "Unknown client resource requested."); |
hsAssert(resmipmap, "Unknown client resource requested."); |
||||||
} |
} |
||||||
|
|
||||||
return resmipmap; |
return resmipmap; |
||||||
} |
} |
||||||
|
@ -1,50 +1,50 @@ |
|||||||
/*==LICENSE==*
|
/*==LICENSE==*
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
CyanWorlds.com Engine - MMOG client, server and tools |
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
Copyright (C) 2011 Cyan Worlds, Inc. |
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
This program is free software: you can redistribute it and/or modify |
||||||
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
||||||
the Free Software Foundation, either version 3 of the License, or |
the Free Software Foundation, either version 3 of the License, or |
||||||
(at your option) any later version. |
(at your option) any later version. |
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
GNU General Public License for more details. |
GNU General Public License for more details. |
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||||
or by snail mail at: |
or by snail mail at: |
||||||
Cyan Worlds, Inc. |
Cyan Worlds, Inc. |
||||||
14617 N Newport Hwy |
14617 N Newport Hwy |
||||||
Mead, WA 99021 |
Mead, WA 99021 |
||||||
|
|
||||||
*==LICENSE==*/ |
*==LICENSE==*/ |
||||||
|
|
||||||
#ifndef _plClientResMgr_h |
#ifndef _plClientResMgr_h |
||||||
#define _plClientResMgr_h |
#define _plClientResMgr_h |
||||||
|
|
||||||
#include <map> |
#include <map> |
||||||
#include <string> |
#include <string> |
||||||
|
|
||||||
class plMipmap; |
class plMipmap; |
||||||
|
|
||||||
class plClientResMgr { |
class plClientResMgr { |
||||||
protected: |
protected: |
||||||
std::map<std::string, plMipmap*>* ClientResources; |
std::map<std::string, plMipmap*>* ClientResources; |
||||||
|
|
||||||
public: |
public: |
||||||
plClientResMgr(); |
plClientResMgr(); |
||||||
~plClientResMgr(); |
~plClientResMgr(); |
||||||
|
|
||||||
void ILoadResources(const char* resfile); |
void ILoadResources(const char* resfile); |
||||||
|
|
||||||
plMipmap* getResource(const char* resname); |
plMipmap* getResource(const char* resname); |
||||||
|
|
||||||
static plClientResMgr& Instance(void); |
static plClientResMgr& Instance(void); |
||||||
}; |
}; |
||||||
|
|
||||||
#endif // _plClientResMgr_
|
#endif // _plClientResMgr_
|
||||||
|
@ -1,254 +1,254 @@ |
|||||||
/*==LICENSE==*
|
/*==LICENSE==*
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
CyanWorlds.com Engine - MMOG client, server and tools |
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
Copyright (C) 2011 Cyan Worlds, Inc. |
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
This program is free software: you can redistribute it and/or modify |
||||||
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
||||||
the Free Software Foundation, either version 3 of the License, or |
the Free Software Foundation, either version 3 of the License, or |
||||||
(at your option) any later version. |
(at your option) any later version. |
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
GNU General Public License for more details. |
GNU General Public License for more details. |
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||||
or by snail mail at: |
or by snail mail at: |
||||||
Cyan Worlds, Inc. |
Cyan Worlds, Inc. |
||||||
14617 N Newport Hwy |
14617 N Newport Hwy |
||||||
Mead, WA 99021 |
Mead, WA 99021 |
||||||
|
|
||||||
*==LICENSE==*/ |
*==LICENSE==*/ |
||||||
|
|
||||||
#include "hsTypes.h" |
#include "hsTypes.h" |
||||||
#include "hsStream.h" |
#include "hsStream.h" |
||||||
#include "hsExceptions.h" |
#include "hsExceptions.h" |
||||||
#include "hsUtils.h" |
#include "hsUtils.h" |
||||||
#include "plPNG.h" |
#include "plPNG.h" |
||||||
#include "../plGImage/plMipmap.h" |
#include "../plGImage/plMipmap.h" |
||||||
|
|
||||||
#include <png.h> |
#include <png.h> |
||||||
#define PNGSIGSIZE 8 |
#define PNGSIGSIZE 8 |
||||||
|
|
||||||
// Custom functions to read and write data from or to an hsStream
|
// Custom functions to read and write data from or to an hsStream
|
||||||
// used by libPNG's respective functions
|
// used by libPNG's respective functions
|
||||||
void pngReadDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
void pngReadDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
||||||
{ |
{ |
||||||
hsStream* inStream = (hsStream*)png_get_io_ptr(png_ptr); |
hsStream* inStream = (hsStream*)png_get_io_ptr(png_ptr); |
||||||
inStream->Read(length, (UInt8*)png_data); |
inStream->Read(length, (UInt8*)png_data); |
||||||
} |
} |
||||||
|
|
||||||
void pngWriteDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
void pngWriteDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
||||||
{ |
{ |
||||||
hsStream* outStream = (hsStream*)png_get_io_ptr(png_ptr); |
hsStream* outStream = (hsStream*)png_get_io_ptr(png_ptr); |
||||||
outStream->Write(length, (UInt8*)png_data); |
outStream->Write(length, (UInt8*)png_data); |
||||||
} |
} |
||||||
|
|
||||||
//// Singleton Instance ///////////////////////////////////////////////////////
|
//// Singleton Instance ///////////////////////////////////////////////////////
|
||||||
|
|
||||||
plPNG& plPNG::Instance(void) |
plPNG& plPNG::Instance(void) |
||||||
{ |
{ |
||||||
static plPNG theInstance; |
static plPNG theInstance; |
||||||
return theInstance; |
return theInstance; |
||||||
} |
} |
||||||
|
|
||||||
//// IRead ////////////////////////////////////////////////////////////////////
|
//// IRead ////////////////////////////////////////////////////////////////////
|
||||||
// Given an open hsStream, reads the PNG data off of the
|
// Given an open hsStream, reads the PNG data off of the
|
||||||
// stream and decodes it into a new plMipmap. The mipmap's buffer ends up
|
// stream and decodes it into a new plMipmap. The mipmap's buffer ends up
|
||||||
// being a packed RGBA buffer.
|
// being a packed RGBA buffer.
|
||||||
// Returns a pointer to the new mipmap if successful, NULL otherwise.
|
// Returns a pointer to the new mipmap if successful, NULL otherwise.
|
||||||
|
|
||||||
plMipmap* plPNG::IRead(hsStream* inStream) |
plMipmap* plPNG::IRead(hsStream* inStream) |
||||||
{ |
{ |
||||||
plMipmap* newMipmap = NULL; |
plMipmap* newMipmap = NULL; |
||||||
png_structp png_ptr; |
png_structp png_ptr; |
||||||
png_infop info_ptr; |
png_infop info_ptr; |
||||||
png_infop end_info; |
png_infop end_info; |
||||||
|
|
||||||
try { |
try { |
||||||
// Check PNG Signature
|
// Check PNG Signature
|
||||||
png_byte sig[PNGSIGSIZE]; |
png_byte sig[PNGSIGSIZE]; |
||||||
inStream->Read8Bytes((char*) sig); |
inStream->Read8Bytes((char*) sig); |
||||||
|
|
||||||
if (!png_sig_cmp(sig, 0, PNGSIGSIZE)) { |
if (!png_sig_cmp(sig, 0, PNGSIGSIZE)) { |
||||||
// Allocate required structs
|
// Allocate required structs
|
||||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
||||||
|
|
||||||
if (!png_ptr) { |
if (!png_ptr) { |
||||||
throw(false); |
throw(false); |
||||||
} |
} |
||||||
|
|
||||||
info_ptr = png_create_info_struct(png_ptr); |
info_ptr = png_create_info_struct(png_ptr); |
||||||
|
|
||||||
if (!info_ptr) { |
if (!info_ptr) { |
||||||
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
||||||
throw(false); |
throw(false); |
||||||
} |
} |
||||||
|
|
||||||
end_info = png_create_info_struct(png_ptr); |
end_info = png_create_info_struct(png_ptr); |
||||||
|
|
||||||
if (!end_info) { |
if (!end_info) { |
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
||||||
throw(false); |
throw(false); |
||||||
} |
} |
||||||
|
|
||||||
// Assign delegate function for reading from hsStream
|
// Assign delegate function for reading from hsStream
|
||||||
png_set_read_fn(png_ptr, (png_voidp)inStream, pngReadDelegate); |
png_set_read_fn(png_ptr, (png_voidp)inStream, pngReadDelegate); |
||||||
// Get PNG Header information
|
// Get PNG Header information
|
||||||
png_set_sig_bytes(png_ptr, PNGSIGSIZE); |
png_set_sig_bytes(png_ptr, PNGSIGSIZE); |
||||||
png_read_info(png_ptr, info_ptr); |
png_read_info(png_ptr, info_ptr); |
||||||
png_uint_32 imgWidth = png_get_image_width(png_ptr, info_ptr); |
png_uint_32 imgWidth = png_get_image_width(png_ptr, info_ptr); |
||||||
png_uint_32 imgHeight = png_get_image_height(png_ptr, info_ptr); |
png_uint_32 imgHeight = png_get_image_height(png_ptr, info_ptr); |
||||||
png_uint_32 bitdepth = png_get_bit_depth(png_ptr, info_ptr); |
png_uint_32 bitdepth = png_get_bit_depth(png_ptr, info_ptr); |
||||||
png_uint_32 channels = png_get_channels(png_ptr, info_ptr); |
png_uint_32 channels = png_get_channels(png_ptr, info_ptr); |
||||||
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr); |
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr); |
||||||
|
|
||||||
// Convert images to RGB color space
|
// Convert images to RGB color space
|
||||||
switch (color_type) { |
switch (color_type) { |
||||||
case PNG_COLOR_TYPE_PALETTE: |
case PNG_COLOR_TYPE_PALETTE: |
||||||
png_set_palette_to_rgb(png_ptr); |
png_set_palette_to_rgb(png_ptr); |
||||||
channels = 3; |
channels = 3; |
||||||
break; |
break; |
||||||
case PNG_COLOR_TYPE_GRAY: |
case PNG_COLOR_TYPE_GRAY: |
||||||
|
|
||||||
if (bitdepth < 8) { |
if (bitdepth < 8) { |
||||||
png_set_expand_gray_1_2_4_to_8(png_ptr); |
png_set_expand_gray_1_2_4_to_8(png_ptr); |
||||||
} |
} |
||||||
|
|
||||||
bitdepth = 8; |
bitdepth = 8; |
||||||
break; |
break; |
||||||
} |
} |
||||||
|
|
||||||
// Convert transparency (if needed) to a full alpha channel
|
// Convert transparency (if needed) to a full alpha channel
|
||||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
||||||
png_set_tRNS_to_alpha(png_ptr); |
png_set_tRNS_to_alpha(png_ptr); |
||||||
channels += 1; |
channels += 1; |
||||||
} else if (channels == 3) { |
} else if (channels == 3) { |
||||||
// Add an opaque alpha channel if still none exists
|
// Add an opaque alpha channel if still none exists
|
||||||
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
||||||
channels = 4; |
channels = 4; |
||||||
} |
} |
||||||
|
|
||||||
// Invert color byte-order as used by plMipmap for DirectX
|
// Invert color byte-order as used by plMipmap for DirectX
|
||||||
png_set_bgr(png_ptr); |
png_set_bgr(png_ptr); |
||||||
/// Construct a new mipmap to hold everything
|
/// Construct a new mipmap to hold everything
|
||||||
newMipmap = TRACKED_NEW plMipmap(imgWidth, imgHeight, plMipmap::kARGB32Config, 1, plMipmap::kUncompressed); |
newMipmap = TRACKED_NEW plMipmap(imgWidth, imgHeight, plMipmap::kARGB32Config, 1, plMipmap::kUncompressed); |
||||||
char* destp = (char*)newMipmap->GetImage(); |
char* destp = (char*)newMipmap->GetImage(); |
||||||
png_bytep* row_ptrs = TRACKED_NEW png_bytep[imgHeight]; |
png_bytep* row_ptrs = TRACKED_NEW png_bytep[imgHeight]; |
||||||
const unsigned int stride = imgWidth * bitdepth * channels / 8; |
const unsigned int stride = imgWidth * bitdepth * channels / 8; |
||||||
|
|
||||||
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
||||||
for (size_t i = 0; i < imgHeight; i++) { |
for (size_t i = 0; i < imgHeight; i++) { |
||||||
row_ptrs[i] = (png_bytep)destp + (i * stride); |
row_ptrs[i] = (png_bytep)destp + (i * stride); |
||||||
} |
} |
||||||
|
|
||||||
png_read_image(png_ptr, row_ptrs); |
png_read_image(png_ptr, row_ptrs); |
||||||
png_read_end(png_ptr, end_info); |
png_read_end(png_ptr, end_info); |
||||||
// Clean up allocated structs
|
// Clean up allocated structs
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
||||||
delete [] row_ptrs; |
delete [] row_ptrs; |
||||||
} |
} |
||||||
} catch (...) { |
} catch (...) { |
||||||
if (newMipmap != NULL) { |
if (newMipmap != NULL) { |
||||||
delete newMipmap; |
delete newMipmap; |
||||||
newMipmap = NULL; |
newMipmap = NULL; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
return newMipmap; |
return newMipmap; |
||||||
} |
} |
||||||
|
|
||||||
plMipmap* plPNG::ReadFromFile(const char* fileName) |
plMipmap* plPNG::ReadFromFile(const char* fileName) |
||||||
{ |
{ |
||||||
wchar* wFilename = hsStringToWString(fileName); |
wchar* wFilename = hsStringToWString(fileName); |
||||||
plMipmap* retVal = ReadFromFile(wFilename); |
plMipmap* retVal = ReadFromFile(wFilename); |
||||||
delete [] wFilename; |
delete [] wFilename; |
||||||
return retVal; |
return retVal; |
||||||
} |
} |
||||||
|
|
||||||
plMipmap* plPNG::ReadFromFile(const wchar* fileName) |
plMipmap* plPNG::ReadFromFile(const wchar* fileName) |
||||||
{ |
{ |
||||||
hsUNIXStream in; |
hsUNIXStream in; |
||||||
|
|
||||||
if (!in.Open(fileName, L"rb")) { |
if (!in.Open(fileName, L"rb")) { |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
|
|
||||||
plMipmap* ret = IRead(&in); |
plMipmap* ret = IRead(&in); |
||||||
in.Close(); |
in.Close(); |
||||||
return ret; |
return ret; |
||||||
} |
} |
||||||
|
|
||||||
hsBool plPNG::IWrite(plMipmap* source, hsStream* outStream) |
hsBool plPNG::IWrite(plMipmap* source, hsStream* outStream) |
||||||
{ |
{ |
||||||
hsBool result = true; |
hsBool result = true; |
||||||
|
|
||||||
try { |
try { |
||||||
// Allocate required structs
|
// Allocate required structs
|
||||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
||||||
|
|
||||||
if (!png_ptr) { |
if (!png_ptr) { |
||||||
throw(false); |
throw(false); |
||||||
} |
} |
||||||
|
|
||||||
png_infop info_ptr = png_create_info_struct(png_ptr); |
png_infop info_ptr = png_create_info_struct(png_ptr); |
||||||
|
|
||||||
if (!info_ptr) { |
if (!info_ptr) { |
||||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
||||||
throw(false); |
throw(false); |
||||||
} |
} |
||||||
|
|
||||||
// Assign delegate function for writing to hsStream
|
// Assign delegate function for writing to hsStream
|
||||||
png_set_write_fn(png_ptr, (png_voidp)outStream, pngWriteDelegate, NULL); |
png_set_write_fn(png_ptr, (png_voidp)outStream, pngWriteDelegate, NULL); |
||||||
UInt8 psize = source->GetPixelSize(); |
UInt8 psize = source->GetPixelSize(); |
||||||
png_set_IHDR(png_ptr, info_ptr, source->GetWidth(), source->GetHeight(), 8, PNG_COLOR_TYPE_RGB_ALPHA, |
png_set_IHDR(png_ptr, info_ptr, source->GetWidth(), source->GetHeight(), 8, PNG_COLOR_TYPE_RGB_ALPHA, |
||||||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
||||||
// Invert color byte-order as used by plMipmap for DirectX
|
// Invert color byte-order as used by plMipmap for DirectX
|
||||||
png_set_bgr(png_ptr); |
png_set_bgr(png_ptr); |
||||||
// Write out the image metadata
|
// Write out the image metadata
|
||||||
png_write_info(png_ptr, info_ptr); |
png_write_info(png_ptr, info_ptr); |
||||||
char* srcp = (char*)source->GetImage(); |
char* srcp = (char*)source->GetImage(); |
||||||
png_bytep* row_ptrs = TRACKED_NEW png_bytep[source->GetHeight()]; |
png_bytep* row_ptrs = TRACKED_NEW png_bytep[source->GetHeight()]; |
||||||
const unsigned int stride = source->GetWidth() * source->GetPixelSize() / 8; |
const unsigned int stride = source->GetWidth() * source->GetPixelSize() / 8; |
||||||
|
|
||||||
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
||||||
for (size_t i = 0; i < source->GetHeight(); i++) { |
for (size_t i = 0; i < source->GetHeight(); i++) { |
||||||
row_ptrs[i] = (png_bytep)srcp + (i * stride); |
row_ptrs[i] = (png_bytep)srcp + (i * stride); |
||||||
} |
} |
||||||
|
|
||||||
png_write_image(png_ptr, row_ptrs); |
png_write_image(png_ptr, row_ptrs); |
||||||
png_write_end(png_ptr, info_ptr); |
png_write_end(png_ptr, info_ptr); |
||||||
// Clean up allocated structs
|
// Clean up allocated structs
|
||||||
png_destroy_write_struct(&png_ptr, &info_ptr); |
png_destroy_write_struct(&png_ptr, &info_ptr); |
||||||
delete [] row_ptrs; |
delete [] row_ptrs; |
||||||
} catch (...) { |
} catch (...) { |
||||||
result = false; |
result = false; |
||||||
} |
} |
||||||
|
|
||||||
return result; |
return result; |
||||||
} |
} |
||||||
|
|
||||||
hsBool plPNG::WriteToFile(const char* fileName, plMipmap* sourceData) |
hsBool plPNG::WriteToFile(const char* fileName, plMipmap* sourceData) |
||||||
{ |
{ |
||||||
wchar* wFilename = hsStringToWString(fileName); |
wchar* wFilename = hsStringToWString(fileName); |
||||||
hsBool retVal = WriteToFile(wFilename, sourceData); |
hsBool retVal = WriteToFile(wFilename, sourceData); |
||||||
delete [] wFilename; |
delete [] wFilename; |
||||||
return retVal; |
return retVal; |
||||||
} |
} |
||||||
|
|
||||||
hsBool plPNG::WriteToFile(const wchar* fileName, plMipmap* sourceData) |
hsBool plPNG::WriteToFile(const wchar* fileName, plMipmap* sourceData) |
||||||
{ |
{ |
||||||
hsUNIXStream out; |
hsUNIXStream out; |
||||||
|
|
||||||
if (!out.Open(fileName, L"wb")) { |
if (!out.Open(fileName, L"wb")) { |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
|
|
||||||
hsBool ret = IWrite(sourceData, &out); |
hsBool ret = IWrite(sourceData, &out); |
||||||
out.Close(); |
out.Close(); |
||||||
return ret; |
return ret; |
||||||
} |
} |
||||||
|
@ -1,55 +1,55 @@ |
|||||||
/*==LICENSE==*
|
/*==LICENSE==*
|
||||||
|
|
||||||
CyanWorlds.com Engine - MMOG client, server and tools |
CyanWorlds.com Engine - MMOG client, server and tools |
||||||
Copyright (C) 2011 Cyan Worlds, Inc. |
Copyright (C) 2011 Cyan Worlds, Inc. |
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
This program is free software: you can redistribute it and/or modify |
||||||
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
||||||
the Free Software Foundation, either version 3 of the License, or |
the Free Software Foundation, either version 3 of the License, or |
||||||
(at your option) any later version. |
(at your option) any later version. |
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
GNU General Public License for more details. |
GNU General Public License for more details. |
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||||
or by snail mail at: |
or by snail mail at: |
||||||
Cyan Worlds, Inc. |
Cyan Worlds, Inc. |
||||||
14617 N Newport Hwy |
14617 N Newport Hwy |
||||||
Mead, WA 99021 |
Mead, WA 99021 |
||||||
|
|
||||||
*==LICENSE==*/ |
*==LICENSE==*/ |
||||||
|
|
||||||
#ifndef _plPNG_h |
#ifndef _plPNG_h |
||||||
#define _plPNG_h |
#define _plPNG_h |
||||||
|
|
||||||
|
|
||||||
//// Class Definition /////////////////////////////////////////////////////////
|
//// Class Definition /////////////////////////////////////////////////////////
|
||||||
|
|
||||||
class plMipmap; |
class plMipmap; |
||||||
class hsStream; |
class hsStream; |
||||||
|
|
||||||
class plPNG { |
class plPNG { |
||||||
protected: |
protected: |
||||||
|
|
||||||
plMipmap* IRead(hsStream* inStream); |
plMipmap* IRead(hsStream* inStream); |
||||||
hsBool IWrite(plMipmap* source, hsStream* outStream); |
hsBool IWrite(plMipmap* source, hsStream* outStream); |
||||||
|
|
||||||
public: |
public: |
||||||
|
|
||||||
plMipmap* ReadFromStream(hsStream* inStream) { return IRead(inStream); } |
plMipmap* ReadFromStream(hsStream* inStream) { return IRead(inStream); } |
||||||
plMipmap* ReadFromFile(const char* fileName); |
plMipmap* ReadFromFile(const char* fileName); |
||||||
plMipmap* ReadFromFile(const wchar* fileName); |
plMipmap* ReadFromFile(const wchar* fileName); |
||||||
|
|
||||||
hsBool WriteToStream(hsStream* outStream, plMipmap* sourceData) { return IWrite(sourceData, outStream); } |
hsBool WriteToStream(hsStream* outStream, plMipmap* sourceData) { return IWrite(sourceData, outStream); } |
||||||
hsBool WriteToFile(const char* fileName, plMipmap* sourceData); |
hsBool WriteToFile(const char* fileName, plMipmap* sourceData); |
||||||
hsBool WriteToFile(const wchar* fileName, plMipmap* sourceData); |
hsBool WriteToFile(const wchar* fileName, plMipmap* sourceData); |
||||||
|
|
||||||
static plPNG& Instance(void); |
static plPNG& Instance(void); |
||||||
}; |
}; |
||||||
|
|
||||||
#endif // _plPNG_h
|
#endif // _plPNG_h
|
||||||
|
Loading…
Reference in new issue