diff --git a/src/filesystem_utils.h b/src/filesystem_utils.h index b8da335..fdf172c 100644 --- a/src/filesystem_utils.h +++ b/src/filesystem_utils.h @@ -16,6 +16,10 @@ #include #endif // _WIN32 +#if __APPLE__ +#include +#endif + #if _WIN32 typedef std::wstring path_t; #define PATHSTR(X) L##X @@ -121,7 +125,19 @@ static path_t get_executable_directory() return path_t(filepath); } -#else // _WIN32 +#elif __APPLE__ +static path_t get_executable_directory() +{ + char filepath[256]; + uint32_t size = sizeof(filepath); + _NSGetExecutablePath(filepath, &size); + + char* slash = strrchr(filepath, '/'); + slash[1] = '\0'; + + return path_t(filepath); +} +#else static path_t get_executable_directory() { char filepath[256]; @@ -132,7 +148,7 @@ static path_t get_executable_directory() return path_t(filepath); } -#endif // _WIN32 +#endif static bool filepath_is_readable(const path_t& path) { diff --git a/src/wic_image.h b/src/wic_image.h index 8e8d908..c7bfe11 100644 --- a/src/wic_image.h +++ b/src/wic_image.h @@ -9,6 +9,9 @@ unsigned char* wic_decode_image(const wchar_t* filepath, int* w, int* h, int* c) IWICImagingFactory* factory = 0; IWICBitmapDecoder* decoder = 0; IWICBitmapFrameDecode* frame = 0; + IWICPalette* palette = 0; + BOOL global_palette_has_alpha = FALSE; + BOOL frame_palette_has_alpha = FALSE; WICPixelFormatGUID pixel_format; IWICFormatConverter* converter = 0; IWICBitmap* bitmap = 0; @@ -28,9 +31,24 @@ unsigned char* wic_decode_image(const wchar_t* filepath, int* w, int* h, int* c) if (factory->CreateDecoderFromFilename(filepath, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &decoder)) goto RETURN; + if (factory->CreatePalette(&palette)) + goto RETURN; + + if (decoder->CopyPalette(palette) == S_OK) + { + if (palette->HasAlpha(&global_palette_has_alpha)) + goto RETURN; + } + if (decoder->GetFrame(0, &frame)) goto RETURN; + if (frame->CopyPalette(palette) == S_OK) + { + if (palette->HasAlpha(&frame_palette_has_alpha)) + goto RETURN; + } + if (factory->CreateFormatConverter(&converter)) goto RETURN; @@ -40,6 +58,9 @@ unsigned char* wic_decode_image(const wchar_t* filepath, int* w, int* h, int* c) if (!IsEqualGUID(pixel_format, GUID_WICPixelFormat32bppBGRA)) pixel_format = GUID_WICPixelFormat24bppBGR; + if (global_palette_has_alpha || frame_palette_has_alpha) + pixel_format = GUID_WICPixelFormat32bppBGRA; + channels = IsEqualGUID(pixel_format, GUID_WICPixelFormat32bppBGRA) ? 4 : 3; if (converter->Initialize(frame, pixel_format, WICBitmapDitherTypeNone, 0, 0.0, WICBitmapPaletteTypeCustom)) @@ -82,6 +103,7 @@ unsigned char* wic_decode_image(const wchar_t* filepath, int* w, int* h, int* c) if (bitmap) bitmap->Release(); if (decoder) decoder->Release(); if (frame) frame->Release(); + if (palette) palette->Release(); if (converter) converter->Release(); if (factory) factory->Release();