Jump to content

Loading order of plugins leads to crash on Pixmap::Save


Go to solution Solved by Josh,

Recommended Posts

Posted

When the KTX2 plugin is loaded before the FreeImage plugin, the Pixmap::Save method throws an exception in the KTX2 plugin when trying to save a jpg or png file:

#include "UltraEngine.h"

using namespace UltraEngine;

#define BUG

int main(int argc, const char* argv[])
{
#ifdef BUG
    auto plg_2 = LoadPlugin("Plugins/KTX2TextureLoader");
    auto plg_1 = LoadPlugin("Plugins/FITextureLoader");
#else
    auto plg_1 = LoadPlugin("Plugins/FITextureLoader");
    auto plg_2 = LoadPlugin("Plugins/KTX2TextureLoader");
#endif

    auto pixmap = CreatePixmap(256, 256);
    pixmap->Save("test.jpg");
    pixmap->Save("test.png");

    return 0;
}

 

  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Posted

Okay, it looks like the plugin doesn't do any pre-checking of the file and the KTX lib isn't set up to account for that:

//Texture load function
void* LoadTexture(Context* context, void* data, uint64_t size, wchar_t* cpath, uint64_t& size_out)
{
	ktx_uint64_t offset = 0;
	ktx_uint8_t* image = NULL;
	ktx_uint32_t level, layer, faceSlice;

	KTX_error_code result = ktxTexture2_CreateFromMemory((const ktx_uint8_t*)data, size, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &context->ktx);
	if (result != KTX_error_code::KTX_SUCCESS) return NULL;

 

Let's build cool stuff and have fun. :)

Posted

See section 3.1 about the file header:
https://registry.khronos.org/KTX/specs/2.0/ktxspec.v2.html

Does this code look right to you?

void* LoadTexture(Context* context, void* data, uint64_t size, wchar_t* cpath, uint64_t& size_out)
{
	//Check file header
	if (size < 12) return NULL;
	char FileIdentifier[12] = { '«', 'K', 'T', 'X', ' ', '2', '0', '»', '\r', '\n', '\x1A', '\n' };
	if (strcmp((char*)data, &FileIdentifier[0]) != 0) return NULL;

 

Let's build cool stuff and have fun. :)

Posted

Yes, this looks right. But I don’t know what this has to do with the save function? Shouldn’t the save function just go by the extension ?

  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Posted

Okay, it just needs this code in the plugin:

void* SaveTexture(Context* context, wchar_t* extension, const int type, const int width, const int height, const int format, void** mipchain, int* sizechain, const int mipcount, const int layers, uint64_t& returnsize, int flags)
{
	std::wstring ext = extension;
	if (ext != L"ktx2") return NULL;

Although I still want to test and make sure KTX saving is working...

  • Like 1

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...