Home > OS >  Embed PNG (Or Bitmap with alpha channel) in executable
Embed PNG (Or Bitmap with alpha channel) in executable

Time:10-28

TL;DR: I want to create a standalone application that has image resources (with alpha channel) embedded. What is the modern/standard way of doing this and extracting the data?

I am currently writing an application in C that uses OpenGL as the graphics engine (I chose it for the faster development time compared to Vulkan, even though I really want to learn Vulkan as well) and in this app I use Signed Distance Fields to render my text. This requires a Font Atlas with an alpha channel to draw the characters from.

I want to be able to embed this Font Atlas (currently it is a .PNG file) in the executable. There are other image resources as well, but I am mentioning this scenario to show why I really need the alpha channel.


I used the Win32 API to do the following:

Get current module.

HMODULE getCurrentModule()
{
    HMODULE hModule = NULL;
    GetModuleHandleEx(
        GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
        (LPCTSTR)getCurrentModule,
        &hModule);
    return hModule;
}

Embed Text files for the shaders.

std::string loadShaderFromResource(int shaderID) 
{
    // Load resource from executable.
    HRSRC shaderResource = FindResource(getCurrentModule(), MAKEINTRESOURCE(shaderID), MAKEINTRESOURCE(TEXTFILE));
    HGLOBAL resourceData = LoadResource(getCurrentModule(), shaderResource);
    DWORD resourceSize = SizeofResource(getCurrentModule(), shaderResource);
    char* resourceFinal = (char*)LockResource(resourceData);
    std::string shaderSource;
    shaderSource.assign(resourceFinal, resourceSize);
    return shaderSource;
}

My resource.h file.

It is a bit messy, but I am including it to provide as much info as possible.

/*=======================================================================================================================================*/
/* Setup by Visual Studio.                                                                                                               */
/*=======================================================================================================================================*/

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        116
#define _APS_NEXT_COMMAND_VALUE         40003
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

/*=======================================================================================================================================*/
/* Resources.                                                                                                                            */
/*=======================================================================================================================================*/

//-------------------------------------------------------------------------------------------------
// Resource Types
#define TEXTFILE 255
#define PNG 254
//-------------------------------------------------------------------------------------------------
// Shaders
#define BASIC_SHADER 253
#define STATIC_SHADER 252
#define TEXTURE_SHADER 251
//-------------------------------------------------------------------------------------------------
// ImGui images.
#define COMPONENT_PNG 250               
#define DRAW_CIRCUIT_BUCKETS 249
#define DRAW_MCC_PNG 248                
//-------------------------------------------------------------------------------------------------
// OpenGL Textures.
#define CIRCUIT_TREE_PNG 247
//-------------------------------------------------------------------------------------------------
// OpenGL Fonts. 
#define ARIAL_SDF_FNT 246       
#define ARIAL_SDF_PNG 245       
#define ARIAL_SDF_BMP 244
#define ARIAL_SDF_MIN_FNT 243
#define ARIAL_SDF_MIN_PNG 242   
//-------------------------------------------------------------------------------------------------
// Application icon.
#define IDI_ICON1 116           // Exe icon.
#define ICON_BMP 115            // GLFW icon.
#define PANDA 114
//-------------------------------------------------------------------------------------------------

/*=======================================================================================================================================*/
/* Includes.                                                                                                                             */
/*=======================================================================================================================================*/

// Windows api for the exe.
#include <Windows.h>
#include <string>

/*=======================================================================================================================================*/
/* Data.                                                                                                                                 */
/*=======================================================================================================================================*/

// Struct that stores the bitmap data.
struct Bitmap {
    void* pixelData;
    int width = 0;
    int height = 0;
};

/*=======================================================================================================================================*/
/* Functions.                                                                                                                            */
/*=======================================================================================================================================*/

// Load the current module.
HMODULE getCurrentModule();

// Load shader from resource.
std::string loadShaderFromResource(int shaderID);

// Loading bitmaps.
Bitmap loadBitmapFromResource(int bitmapID);
unsigned int loadBitmapToGL(Bitmap bitmap);


/*=======================================================================================================================================*/
/* EOF.                                                                                                                                  */
/*=======================================================================================================================================*/

My resource.rc file.

// Microsoft Visual C   generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif    // APSTUDIO_INVOKED

/*=======================================================================================================================================*/
/* Resources.                                                                                                                            */
/*=======================================================================================================================================*/

//-------------------------------------------------------------------------------------------------
// Resource ID                  Resource Type               Resource Path
//-------------------------------------------------------------------------------------------------
// Application icon.
IDI_ICON1                       ICON                        "Icons\\circuitIcon128.ico"
ICON_BMP                        BITMAP                      "Icons\\circuitIcon.bmp"
PANDA                           BITMAP                      "Icons\\panda.bmp"
//-------------------------------------------------------------------------------------------------
// Shaders.
BASIC_SHADER                    TEXTFILE                    "Shaders\\basicShader.shader"
STATIC_SHADER                   TEXTFILE                    "Shaders\\staticShader.shader"
TEXTURE_SHADER                  TEXTFILE                    "Shaders\\textureShader.shader"
//-------------------------------------------------------------------------------------------------
// ImGui images.
COMPONENT_PNG                   PNG                         "Icons\\component.png"
DRAW_CIRCUIT_BUCKETS_PNG        PNG                         "Icons\\Draw_Circuit_Buckets.png"
DRAW_MCC_PNG                    PNG                         "Icons\\Draw_MCC.png"
//-------------------------------------------------------------------------------------------------
// OpenGL Textures.
CIRCUIT_TREE_PNG                PNG                         "Textures\\circuitTree.png"
//-------------------------------------------------------------------------------------------------
// OpenGL Fonts. 
ARIAL_SDF_FNT                   TEXTFILE                    "Fonts\\Arial_SDF.fnt"
ARIAL_SDF_PNG                   PNG                         "Fonts\\Arial_SDF_PNG.png"
ARIAL_SDF_BMP                   BITMAP                      "Fonts\\Arial_SDF_BMP_32.bmp"
ARIAL_SDF_MIN_FNT               TEXTFILE                    "Fonts\\Arial_SDF_Min.fnt"
ARIAL_SDF_MIN_PNG               PNG                         "Fonts\\Arial_SDF_Min.png"
//-------------------------------------------------------------------------------------------------

#endif    // English (United States) resources

//=================================================================================================

#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED
 /*=======================================================================================================================================*/
/* EOF.                                                                                                                                  */
/*=======================================================================================================================================*/

I noticed that most of the forums regarding the Win32 API and this type of question is very, very old. Is it still used? Is there a more modern way of doing it? I saw some people using CMake.


Edit 1

I tried adding the following, as suggested by @Barmak with some edits:

HBITMAP loadImageFromResource(int resourceID)
{
    HBITMAP hbitmap = NULL;
    ULONG_PTR token;
    Gdiplus::GdiplusStartupInput tmp;
    Gdiplus::GdiplusStartup(&token, &tmp, NULL);
    if (auto hres = FindResource(getCurrentModule(), MAKEINTRESOURCE(resourceID), RT_RCDATA))
        if (auto size = SizeofResource(getCurrentModule(), hres))
            if (auto data = LockResource(LoadResource(getCurrentModule(), hres)))
                if (auto stream =  SHCreateMemStream((BYTE*)data, size))
                {
                    Gdiplus::Bitmap bmp(stream);
                    stream->Release();
                    bmp.GetHBITMAP(Gdiplus::Color::Transparent, &hbitmap);
                }
    Gdiplus::GdiplusShutdown(token);
    return hbitmap;
}

However, when viewing hbitmap in debug mode it shows 0xffffffffa00513d7 {unused=???} and does not work.

resource.h:

#define ICON_PNG 240            // GLFW icon.

resource.rc:

ICON_PNG                        RCDATA                      "Icons\\circuitImage.png"

Edit 2

I made a simple mistake. I added these lines to the function:

BITMAP bitmap;
GetObject(hbitmap, sizeof(BITMAP), &bitmap);

And now it works by returing a BITMAP instead of HBITMAP. However, it is upside down and some of the coloring seems off when using it with glfwSetWindowIcon(). It works perfectly in other places!

BITMAP loadImageFromResource(int resourceID)
{
    HBITMAP hbitmap = NULL;
    ULONG_PTR token;
    Gdiplus::GdiplusStartupInput tmp;
    Gdiplus::GdiplusStartup(&token, &tmp, NULL);
    if (auto hres = FindResource(getCurrentModule(), MAKEINTRESOURCE(resourceID), RT_RCDATA))
        if (auto size = SizeofResource(getCurrentModule(), hres))
            if (auto data = LockResource(LoadResource(getCurrentModule(), hres)))
                if (auto stream =  SHCreateMemStream((BYTE*)data, size))
                {
                    Gdiplus::Bitmap bmp(stream);
                    stream->Release();
                    bmp.GetHBITMAP(Gdiplus::Color::Transparent, &hbitmap);
                }
    Gdiplus::GdiplusShutdown(token);
    BITMAP bitmap;
    GetObject(hbitmap, sizeof(BITMAP), &bitmap);
    return bitmap;
}

CodePudding user response:

Credit: Barmak Shemirani

First, store the .PNG file by adding these lines to:

resource.h

#define ICON_PNG 240            // GLFW icon.

resource.rc

ICON_PNG                        RCDATA                      "Icons\\circuitImage.png"

Then load the .PNG as a bitmap like this:

BITMAP loadImageFromResource(int resourceID)
{
    HBITMAP hbitmap = NULL;
    ULONG_PTR token;
    Gdiplus::GdiplusStartupInput tmp;
    Gdiplus::GdiplusStartup(&token, &tmp, NULL);
    if (auto hres = FindResource(getCurrentModule(), MAKEINTRESOURCE(resourceID), RT_RCDATA))
        if (auto size = SizeofResource(getCurrentModule(), hres))
            if (auto data = LockResource(LoadResource(getCurrentModule(), hres)))
                if (auto stream =  SHCreateMemStream((BYTE*)data, size))
                {
                    Gdiplus::Bitmap bmp(stream);
                    stream->Release();
                    bmp.GetHBITMAP(Gdiplus::Color::Transparent, &hbitmap);
                }
    Gdiplus::GdiplusShutdown(token);
    BITMAP bitmap;
    if(!hbitmap) return NULL;
    GetObject(hbitmap, sizeof(BITMAP), &bitmap);
    return bitmap;
}

By calling it like this:

BITMAP image = loadImageFromResource(ICON_PNG);

Edit 1

I used this function to load the bitmap into OpenGL:

unsigned int loadBitmapToGL(BITMAP bitmap) 
{
    unsigned int textureID = 0;
    glCreateTextures(GL_TEXTURE_2D, 1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap.bmWidth, bitmap.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)bitmap.bmBits);
    return textureID;
}

Also added if(!hbitmap) return NULL; as suggested by Barmak.

  • Related