3
votes

Modern applications are centered around graphics. Graphics that display information to the user or provide a creative, artsy touch to that otherwise grey application. Most of the time, it makes sense to load graphics at runtime: in image editing programs, when setting custom profile pictures etc.

But what about graphics that are meant to be invariable? I have an app for example, that is intended to have a backdrop for the login screen. This backdrop will never change within versions of the app, and therefore should not be in a separate file that need be shipped with the executable. A separate file could also potentially be modified by other programs and users. Xcode ships with a clever asset system which allows you to add assets to an assets.xcassets file and then use methods like imageNamed:@"nameOfImageInAssets.png" to create an NSImage where the name represents an image in the assets package that Xcode will automatically copy to the resources folder of the app.

This is not portable, and neither are Windows resource files. I have read many articles, most of which suggest using xxd or the gnu linker to turn this image:

SaltBae

into a header file using the command: xxd -i Users/username/Pictures/SaltBae.png SaltBae.h

SaltBae.h

unsigned char _Users_username_Pictures_SaltBae_png[] = {

0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x14,
0x08, 0x06, 0x00, 0x00, 0x00, 0x8d, 0x89, 0x1d, 0x0d, 0x00, 0x00, 0x00,
0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61,
0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00, 0x7a,
0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00, 0x80,
0xe8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00, 0x3a,
0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00, 0x00,
0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b,
0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x01, 0xd5, 0x69, 0x54,
0x58, 0x74, 0x58, 0x4d, 0x4c, 0x3a, 0x63, 0x6f, 0x6d, 0x2e, 0x61, 0x64,
0x6f, 0x62, 0x65, 0x2e, 0x78, 0x6d, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3c, 0x78, 0x3a, 0x78, 0x6d, 0x70, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x78,
0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x78, 0x3d, 0x22, 0x61, 0x64, 0x6f, 0x62,
0x65, 0x3a, 0x6e, 0x73, 0x3a, 0x6d, 0x65, 0x74, 0x61, 0x2f, 0x22, 0x20,
0x78, 0x3a, 0x78, 0x6d, 0x70, 0x74, 0x6b, 0x3d, 0x22, 0x58, 0x4d, 0x50,
0x20, 0x43, 0x6f, 0x72, 0x65, 0x20, 0x35, 0x2e, 0x34, 0x2e, 0x30, 0x22,
0x3e, 0x0a, 0x20, 0x20, 0x20, 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x52, 0x44,
0x46, 0x20, 0x78, 0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x72, 0x64, 0x66, 0x3d,
0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e,
0x77, 0x33, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x31, 0x39, 0x39, 0x39, 0x2f,
0x30, 0x32, 0x2f, 0x32, 0x32, 0x2d, 0x72, 0x64, 0x66, 0x2d, 0x73, 0x79,
0x6e, 0x74, 0x61, 0x78, 0x2d, 0x6e, 0x73, 0x23, 0x22, 0x3e, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x44, 0x65,
0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x72, 0x64,
0x66, 0x3a, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x3d, 0x22, 0x22, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x78,
0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x74, 0x69, 0x66, 0x66, 0x3d, 0x22, 0x68,
0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, 0x64, 0x6f,
0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x69, 0x66, 0x66, 0x2f,
0x31, 0x2e, 0x30, 0x2f, 0x22, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x43, 0x6f,
0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x3e, 0x31, 0x3c,
0x2f, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x65,
0x73, 0x73, 0x69, 0x6f, 0x6e, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x4f, 0x72,
0x69, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3e, 0x31, 0x3c,
0x2f, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x4f, 0x72, 0x69, 0x65, 0x6e, 0x74,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x50, 0x68,
0x6f, 0x74, 0x6f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x49, 0x6e, 0x74,
0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3e,
0x32, 0x3c, 0x2f, 0x74, 0x69, 0x66, 0x66, 0x3a, 0x50, 0x68, 0x6f, 0x74,
0x6f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x49, 0x6e, 0x74, 0x65, 0x72,
0x70, 0x72, 0x65, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3e, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x72, 0x64, 0x66, 0x3a, 0x44,
0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x3e, 0x0a,
0x20, 0x20, 0x20, 0x3c, 0x2f, 0x72, 0x64, 0x66, 0x3a, 0x52, 0x44, 0x46,
0x3e, 0x0a, 0x3c, 0x2f, 0x78, 0x3a, 0x78, 0x6d, 0x70, 0x6d, 0x65, 0x74,
0x61, 0x3e, 0x0a, 0x02, 0xd8, 0x80, 0x05, 0x00, 0x00, 0x04, 0xdc, 0x49,
0x44, 0x41, 0x54, 0x38, 0x11, 0x1d, 0x94, 0x49, 0x6c, 0x1b, 0x65, 0x18,
0x86, 0x9f, 0x99, 0xf9, 0x67, 0xc6, 0x6b, 0xbc, 0x26, 0xce, 0xda, 0xa4,
0x25, 0x69, 0x0b, 0x2d, 0x28, 0x34, 0x2c, 0x95, 0x00, 0x89, 0x45, 0x08,
0x5a, 0x95, 0x03, 0x08, 0x09, 0x21, 0xe0, 0x80, 0x38, 0xc3, 0x85, 0x03,
0xe2, 0x00, 0x47, 0xc4, 0x1d, 0x38, 0x70, 0xe3, 0xc6, 0x01, 0x01, 0x42,
0x20, 0x54, 0x7a, 0x2a, 0x6b, 0x0b, 0x94, 0xd2, 0xd2, 0x25, 0x69, 0x9b,
0xa4, 0x0d, 0x2d, 0xa9, 0xb3, 0x78, 0x89, 0x9d, 0xf1, 0x2c, 0x9e, 0x85,
0x2f, 0xb5, 0x35, 0xb6, 0x35, 0x96, 0xde, 0x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e2, 0xaa, 0x9c, 0xb1, 0x59, 0xdb, 0x12, 0x25, 0xc1, 0x32,
0x92, 0xea, 0xc9, 0xaf, 0x3b, 0x97, 0xca, 0xca, 0xfe, 0x5b, 0xfe, 0xe5,
0x33, 0x29, 0xeb, 0x16, 0x95, 0xd2, 0x24, 0xeb, 0xda, 0x30, 0xeb, 0x95,
0x1a, 0xd3, 0xf7, 0x0f, 0x51, 0x1c, 0xd9, 0x0b, 0x99, 0x12, 0x7a, 0x4a,
0xd0, 0xd3, 0x25, 0x9a, 0x88, 0x45, 0xb1, 0x04, 0x33, 0x2c, 0x8a, 0x99,
0x34, 0x6b, 0x75, 0x19, 0x91, 0x9d, 0x92, 0x29, 0x89, 0xa0, 0x2c, 0x8b,
0x9d, 0xd8, 0x7a, 0x5e, 0x04, 0x07, 0x87, 0x66, 0x28, 0x56, 0x67, 0xb9,
0xd6, 0xd2, 0x39, 0xd9, 0xec, 0x33, 0x30, 0xb2, 0x8b, 0xea, 0xae, 0x83,
0x18, 0xb9, 0x31, 0x34, 0xbb, 0x42, 0x22, 0x0b, 0x21, 0x96, 0x3c, 0x61,
0xac, 0xcb, 0x95, 0x60, 0x2a, 0xe9, 0x68, 0x79, 0x08, 0x36, 0x56, 0x65,
0x27, 0x4a, 0xd9, 0x83, 0x00, 0xcf, 0x0b, 0xf1, 0xfc, 0x10, 0x15, 0x0a,
0x6a, 0x75, 0x77, 0x8b, 0x86, 0xdc, 0x58, 0x57, 0x45, 0x52, 0xe9, 0x84,
0x81, 0x7c, 0x91, 0x28, 0x55, 0x23, 0x96, 0x13, 0xd7, 0x24, 0xbe, 0xac,
0x17, 0xfa, 0xf2, 0x78, 0x63, 0xc7, 0x82, 0x08, 0xda, 0xa6, 0xc5, 0x50,
0x55, 0x04, 0xe5, 0x65, 0x5b, 0x06, 0xde, 0xce, 0xf0, 0x24, 0xf3, 0x4e,
0x70, 0xb5, 0x15, 0x6a, 0x34, 0x7b, 0x11, 0x9d, 0xbe, 0x10, 0x53, 0xd0,
0xa8, 0x86, 0x2e, 0x76, 0xb6, 0x2a, 0x9d, 0x2c, 0x48, 0x3c, 0x5b, 0xa2,
0xc8, 0x3a, 0x37, 0xd4, 0x9d, 0xed, 0x6c, 0x4a, 0xab, 0x95, 0x6e, 0x08,
0x66, 0x3d, 0x5a, 0xad, 0x4d, 0x18, 0xc8, 0xca, 0xfa, 0xd5, 0x85, 0x6f,
0xf9, 0x5f, 0xde, 0x02, 0x30, 0xff, 0x03, 0x8c, 0x47, 0x35, 0xad, 0xbc,
0xbf, 0x26, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
0x42, 0x60, 0x82

};

unsigned int _Users_username_Pictures_SaltBae_png_len = 1863;

That I'm sure was quite a pain to read and it just as painful to store on disk. The file above is only for a 20*20 version of the image your see. As you can imagine, the file sizes for HD images become catastrophically large when stored this way. Raw include files simply won't do.

A workaround someone suggested was to use precompiled headers, but from what I understand that is at the cost of portability.

Ignoring the size issue for a minute I have run into another problem: I can't seem to create an NSImage from the unsigned char array in the file.

I ended up with the following code:

auto test = (unsigned char * _Nullable *)&_Users_username_Pictures_SaltBae_png[0];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                              initWithBitmapDataPlanes:nil
                              pixelsWide:20
                              pixelsHigh:20
                              bitsPerSample:16
                              samplesPerPixel:3
                              hasAlpha:NO
                              isPlanar:NO
                              colorSpaceName:NSDeviceRGBColorSpace
                              bytesPerRow:(3*2*20)
                              bitsPerPixel:48];
NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(20, 20)];
[theImage addRepresentation:rep];

Then I proceed to name the image and set the NSView's content to the image. theImage.name = @"customImage"; [view setBackgroundImage:@"customImage"];

Depending on what parameters I pass into the Bitmap, it will either be blanc or throw an access error. The image works fine when set with the view's image to something in the app's resource folder: [view setBackgroundImage:@"imageInResourceFolder"]; So my question is thus: how can I properly, portably embed data (images, audio, etc.) into an executable file using Xcode, and how can I properly access the data in my header to define an NSImage that can be used as a background for my panel?

UPDATE:

Daij's answer solved my troubles accessing the data, and I was able to produce the following window from embedded data: enter image description here

2

2 Answers

2
votes

seems you have it mostly... just grab the byte* make it into NSData and make that into an image

NSData *imageData = [NSData dataWithBytes:byteArray length:arrayLength];
NSImage *image = [[NSImage alloc] initWithData:imageData];

this assumes the pointer you have leads to valid image data ;)


you can just link into with ld (search for my github sample DDEmbeddedDataReader but any process works.. #define the array or put it in a variable in a .c file... whatever you like :)

... I wrote DDEmbeddedDataReader to see if this is worth it but I decided to just use external assets. why? you DONT want to increase binary size for resources...

1
votes

I work with the GBA / NDS - an embedded system with ROM. One of the biggest problems is getting images into the game, as there is nothing to about 1mB of flash memory. Because of this, one solution is what you did - divvy up the bytes into an array for a pointer to the blob of pixels. There has been a lot of turmoil about how to represent such data and the conclusion is in a compiled file so you don't have recompile each time like you would with headers (not to mention it thrashes your ram).

However, there was one benefit of storing it in the compiled object file: you could relocate the data where you needed to. In fact, you don't even need a symbol for the data itself and the whole methodology can be scrapped for a binutils solution. There is a common application called bin2o that is floating somewhere on the internet that automates the process of reading a file and outputting an object file with 3 symbols: data length, data pointer, data end. This way you can just compile and link the images without having to convert the data into a messy array.

I am not expecting you to find and use bin2o (its not in binutils), however, it does come standard with some embedded compilers. Some of them translate the code into c, which is eww. I am just going to explain here how to do it yourself using binutils. Although, I am a C programmer with light knowledge over obj-c, however, as I researched it should be the same with objects. Translation is necessary, forgive me.

Need-to-know's:

  • Target Format (mingw64 x86_64-w64-mingw32)
  • Input Format (probably binary)
  • Output Architecture (mingw64 i386:x86-64:intel)

You would make a call to objcopy from binutils where -I is input format, -O is output format, and -B is the binary architecture. Say I have a image called cow.bmp: I would run on my intel mingw64 x64 windows pc objcopy -I binary -O x86_64-w64-mingw32 -B i386:x86-64:intel cow.bmp cow.o which will create cow.o with 3 symbols.

  • _binary_cow_bmp_bin_start
  • _binary_cow_bmp_bin_end
  • _binary_cow_bmp_bin_size

You would end up linking whatever needs cow.o together. Here is a MWE in C:

#include <stdio.h> // just some standard headers
#include <stdlib.h>
#include <string.h>

extern void* _binary_cow_bmp_bin_start; // points to nothing
extern void* _binary_cow_bmp_bin_end; // however the address points to the data
extern void* _binary_cow_bmp_bin_size; // use &_binary... to get the data
// To access the values pointing to above is undefined behavior,
// but not the address of the undefined values are defined

int main(int argc, char** argv)
{

    char* data = (char*) &_binary_cow_bmp_bin_start; // character/byte array
    size_t size = (size_t)&_binary_cow_bmp_bin_size; // length of data

    printf("Size:`%Iu`\nData:`%9.9s`\n", size, data); // pretty output

    return EXIT_SUCCESS;
}

Output:

Size:`196662`
Data:` BM6`

(Note it looks skewed but its 6 spaces as per 9.9 requirement)