From dda9528dac88c15025cc989028f4f6639ec84a3b Mon Sep 17 00:00:00 2001 From: sanikoyes Date: Sat, 19 Mar 2016 09:48:41 +0800 Subject: [PATCH] Add etc1(pkm) texture loading support --- drivers/etc1/SCsub | 3 +- drivers/etc1/texture_loader_pkm.cpp | 84 +++++++++++++++++++++++++++++ drivers/etc1/texture_loader_pkm.h | 18 +++++++ drivers/register_driver_types.cpp | 14 +++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 drivers/etc1/texture_loader_pkm.cpp create mode 100644 drivers/etc1/texture_loader_pkm.h diff --git a/drivers/etc1/SCsub b/drivers/etc1/SCsub index 4ce921ad9fd..2b9dc1b31c7 100644 --- a/drivers/etc1/SCsub +++ b/drivers/etc1/SCsub @@ -3,7 +3,8 @@ Import('env') etc_sources = [ "etc1/image_etc.cpp", - "etc1/rg_etc1.cpp" + "etc1/rg_etc1.cpp", + "etc1/texture_loader_pkm.cpp" ] if (env["etc1"] != "no"): diff --git a/drivers/etc1/texture_loader_pkm.cpp b/drivers/etc1/texture_loader_pkm.cpp new file mode 100644 index 00000000000..275afc1fd61 --- /dev/null +++ b/drivers/etc1/texture_loader_pkm.cpp @@ -0,0 +1,84 @@ +#include "texture_loader_pkm.h" +#include "os/file_access.h" +#include + +struct ETC1Header { + char tag[6]; // "PKM 10" + uint16_t format; // Format == number of mips (== zero) + uint16_t texWidth; // Texture dimensions, multiple of 4 (big-endian) + uint16_t texHeight; + uint16_t origWidth; // Original dimensions (big-endian) + uint16_t origHeight; +}; + +RES ResourceFormatPKM::load(const String &p_path, const String& p_original_path, Error *r_error) { + + if (r_error) + *r_error=ERR_CANT_OPEN; + + Error err; + FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err); + if (!f) + return RES(); + + FileAccessRef fref(f); + if (r_error) + *r_error=ERR_FILE_CORRUPT; + + ERR_EXPLAIN("Unable to open PKM texture file: "+p_path); + ERR_FAIL_COND_V(err!=OK,RES()); + + // big endian + f->set_endian_swap(true); + + ETC1Header h; + ERR_EXPLAIN("Invalid or Unsupported PKM texture file: "+p_path); + f->get_buffer((uint8_t *) &h.tag, sizeof(h.tag)); + if(strncmp(h.tag, "PKM 10", sizeof(h.tag))) + ERR_FAIL_V(RES()); + + h.format = f->get_16(); + h.texWidth = f->get_16(); + h.texHeight = f->get_16(); + h.origWidth = f->get_16(); + h.origHeight = f->get_16(); + + DVector src_data; + + uint32_t size = h.texWidth * h.texHeight / 2; + src_data.resize(size); + DVector::Write wb = src_data.write(); + f->get_buffer(wb.ptr(),size); + wb=DVector::Write(); + + int mipmaps = h.format; + int width = h.origWidth; + int height = h.origHeight; + + Image img(width,height,mipmaps,Image::FORMAT_ETC,src_data); + + Ref texture = memnew( ImageTexture ); + texture->create_from_image(img); + + if (r_error) + *r_error=OK; + + return texture; +} + +void ResourceFormatPKM::get_recognized_extensions(List *p_extensions) const { + + p_extensions->push_back("pkm"); +} + +bool ResourceFormatPKM::handles_type(const String& p_type) const { + + return ObjectTypeDB::is_type(p_type,"Texture"); +} + +String ResourceFormatPKM::get_resource_type(const String &p_path) const { + + if (p_path.extension().to_lower()=="pkm") + return "ImageTexture"; + return ""; +} diff --git a/drivers/etc1/texture_loader_pkm.h b/drivers/etc1/texture_loader_pkm.h new file mode 100644 index 00000000000..5788716d9ff --- /dev/null +++ b/drivers/etc1/texture_loader_pkm.h @@ -0,0 +1,18 @@ +#ifndef TEXTURE_LOADER_PKM_H +#define TEXTURE_LOADER_PKM_H + +#include "scene/resources/texture.h" +#include "io/resource_loader.h" + +class ResourceFormatPKM : public ResourceFormatLoader{ +public: + + virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL); + virtual void get_recognized_extensions(List *p_extensions) const; + virtual bool handles_type(const String& p_type) const; + virtual String get_resource_type(const String &p_path) const; + + virtual ~ResourceFormatPKM() {} +}; + +#endif // TEXTURE_LOADER_PKM_H diff --git a/drivers/register_driver_types.cpp b/drivers/register_driver_types.cpp index 235438f1267..969ea1a7315 100644 --- a/drivers/register_driver_types.cpp +++ b/drivers/register_driver_types.cpp @@ -16,6 +16,7 @@ #include "png/resource_saver_png.h" #include "jpegd/image_loader_jpegd.h" #include "dds/texture_loader_dds.h" +#include "etc1/texture_loader_pkm.h" #include "pvr/texture_loader_pvr.h" #include "etc1/image_etc.h" #include "chibi/event_stream_chibi.h" @@ -79,6 +80,10 @@ static ImageLoaderJPG *image_loader_jpg=NULL; static ResourceFormatDDS *resource_loader_dds=NULL; #endif +#ifdef ETC1_ENABLED +static ResourceFormatPKM *resource_loader_pkm=NULL; +#endif + #ifdef PVR_ENABLED static ResourceFormatPVR *resource_loader_pvr=NULL; @@ -197,6 +202,11 @@ void register_driver_types() { ResourceLoader::add_resource_format_loader(resource_loader_dds ); #endif +#ifdef ETC1_ENABLED + resource_loader_pkm = memnew( ResourceFormatPKM ); + ResourceLoader::add_resource_format_loader(resource_loader_pkm); +#endif + #ifdef PVR_ENABLED resource_loader_pvr = memnew( ResourceFormatPVR ); ResourceLoader::add_resource_format_loader(resource_loader_pvr ); @@ -280,6 +290,10 @@ void unregister_driver_types() { memdelete(resource_loader_dds); #endif +#ifdef ETC1_ENABLED + memdelete(resource_loader_pkm); +#endif + #ifdef PVR_ENABLED memdelete(resource_loader_pvr); #endif