Compiling image into executable

This is a discussion on Compiling image into executable within the C Programming forums, part of the General Programming Boards category; I've been using gtk and was wondering how images are put into executables so that it's self contained rather than ...

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Compiling image into executable

    I've been using gtk and was wondering how images are put into executables so that it's self contained rather than referencing an image from a different location.
    For example, how would this icon be compiled into an executable? Currently the code just references it from an image called Ban.jpg in the same directory the executable is in. Any help would be appreciated.

    Code:
    GdkPixbuf *create_pixbuf(const gchar * filename)
    {
       GdkPixbuf *pixbuf;
       GError *error = NULL;
       pixbuf = gdk_pixbuf_new_from_file(filename, &error);
       if(!pixbuf) {
          fprintf(stderr, "%s\n", error->message);
          g_error_free(error);
       }
    
       return pixbuf;
    }
    
    gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("Ban.jpg"));

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This totally depends on the file format of the executable. I don't think there's an easy way (without lots of hacking) to do it with ELF binaries, but the file format used in Mac OS X and Windows for executables allows you to embed resources that you can then use from the code.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redruby147 View Post
    I've been using gtk and was wondering how images are put into executables so that it's self contained rather than referencing an image from a different location.
    You can but it is not very advantageous for a few reasons:

    1) it can drastically increase your executable size.
    2) if you are going to use a compressed format, such as a jpeg, you will need to write some custom routines to both rip the jpeg data into your code, then to deal with it there.

    Have a look at an .xpm file (just open it in a text editor -- xpm images are actually C code). This will give you some idea of what putting image data into your code will involve.

    It is just not a worthwhile thing to do -- it is much more practical to load a file, unless for some reason you want to thoroughly hide the image from the world (which is pointless, it can be gotten with a screenshot).

    Code:
    /* XPM */
    static char *Toaster[] = {
    /* columns rows colors chars-per-pixel */
    "48 48 90 1",
    "  c #020202",
    ". c #0C0C0C",
    "X c #141414",
    "o c #181916",
    "O c #151518",
    "+ c #1B1B1C",
    "@ c #1F201E",
    ad nauseum
    Last edited by MK27; 03-17-2010 at 06:19 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 01:46 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. Compiling data into an executable
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2003, 04:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21