Embedding png images – wxwiki infection trichomonas

The last strategy is the best from wxWidgets philosophy of trying to do everything in the most native way possible. Since version 2.9.5, wxWidgets simplifies its implementation by providing wxBITMAP_PNG() macro that will load the bitmap from the corresponding location depending on the platform, i.e. from the resource section under Windows, a file in the resources directory under OS X or from embedded byte array elsewhere.

Files in the XPM format are made of plain C code already, so they could be easily embedded into C++ code by way of using #include. However, when your master image is in a different format (and it often is), one needs to convert to XPM first. In addition, XPM is uncompressed and does not support an alpha channel (only on-off-transparency), which makes it unsuitable for large images, or images that want to be composited.

If you chose to use –wxbitmap, you have to initialize the wx image handlers and call a bin2c function to create said bitmap objects from the raw data. In the initialization function of your program — often this is MyApp::OnInit, where MyApp is your wxApp-derived class — this will look like:

The wxWidgets 2.9.1+ source tree ships the python script in misc/scripts/png2c.py. (It is uncredited, but judging from the wiki page titles, it may have been authored by Sigala/Leoni.) It produces a solitary .h file with character arrays on stdout out of one or more input files, which strictly have to be PNG.

Once you have converted your graphics files converted with one of the utilities, you need to #include them from your source code to use them. If the conversion program has left you with a pair of .c/.h (or .cpp/.hpp) files, the .c(pp) file contains the actual data and is meant to be compiled using your regular build infrastructure, and the .h(pp) file contains the declarations to use the variables in any other source file. It will perhaps be necessary that you call a function to load all the images; check the .h(pp) file you received.

If you have a solitary .h file, then that leaves you with having to include that to make the raw data available in a particular source file. If you need the image available in multiple source files, you either have to repeatedly #include it per source file (possibly growing the final executable), or constructing additional source code so as to make it available across all translation units.

When dealing with variables containing raw byte streams, you may want to convert them to wxBitmap (if the tool did not already offer you this possibility). Since wxWidgets 2.9.5, you can then use the wxBITMAP_PNG_FROM_DATA macro to create a bitmap from a variable holding raw data:

Notice that this macro appends a _png suffix. The macro also requires that myimage_png be an array whose size is determinable (by way of sizeof()). Alternatively, one can directly use the wxMemoryInputStream class, needed if you have non-PNG images:

This technique of embedding an image is also used by the Audacity sound editor (latest CVS only). It has bin2c code built into it and uses wxWidgets code to combine and split PNG images, so that a theme consists of a single large image rather than many small ones.

One thing to be aware of: When you add a PNG resource in Visual Studio 2010, it may define it as PNG type rather than RCDATA, and this method will fail to load them. The easiest way to deal with that is to edit the resource file (.rc) and change the PNG entry for each file added to an RCDATA entry.

Under OS X image files should be put into the Resources subdirectory of the application bundle. This can be done either in the Xcode project or by simply manually copying them there in your makefile. Since wxWidgets 2.9.5 wxBITMAP_PNG macro can be used to load the images from this standard location. With the previous versions you’d need to use wxStandardPaths::GetResourcesDir() manually.