Skip to content

Commit

Permalink
update common utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Jul 28, 2022
1 parent 3bafb64 commit 40604fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/filesystem_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <dirent.h>
#endif // _WIN32

#if __APPLE__
#include <mach-o/dyld.h>
#endif

#if _WIN32
typedef std::wstring path_t;
#define PATHSTR(X) L##X
Expand Down Expand Up @@ -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];
Expand All @@ -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)
{
Expand Down
22 changes: 22 additions & 0 deletions src/wic_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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))
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 40604fa

Please sign in to comment.