Re: embedding xpm images in your perl scripts



In one of my good old Tk programs I have the following code:

    sub create_pixmaps {
        $pix{dynamite} = <<__;
    /* XPM */
    static char *dyna[] = {
    /* width height num_colors chars_per_pixel */
    "    29    20        5            1",
    /* colors */
    ". c #000000",
    "# c #bebebe",
    "a c #ffe4c4",
    "b c #ff0000",
    "c c #ffff00",
    /* pixels */
    "aaaaaa..aacaaaaabaaaaaaaaaaaa",
    "aaaaa.aa.aabaaaaaaaaaaaaaaaaa",
    "aaaa.aaaa.aacbaaaaaaaaaaaaaaa",
    "aaaa.aaab.cabaaaaaaaaaaaaaaaa",
    "aaaa.aabca.abaaaaaaaaaaaaaaaa",
    "aaa.aaaabcc.aaabaaaaaaaaaaaaa",
    "aa.aaaaababa.aaaaaaaaaaaaaaaa",
    "a.aaaabaaaaaa.aaaaaaaaaaaaaaa",
    "a.aaaabaaaaaaaaaaaaaaaaaaaaaa",
    "a.aaaaaaaaaaaaaaaaaaaaaaaaaaa",
    "a..........................aa",
    "a.#.bbbb.ca.bbbbbbb.ca.bbbb.a",
    "a.#.bbbb.ac.bbbbbbb.ac.bbbb.a",
    "..........ac.........ac.....a",
    ".#.#.bbbb.ca.bbbbbbb.ca.bbbb.",
    ".#.#.bbbb.ac.bbbbbbb.ac.bbbb.",
    ".........ca.........ac......a",
    "a.#.bbbb.ac.bbbbbbb.ca.bbbb.a",
    "a.#.bbbb.ca.bbbbbbb.ac.bbbb.a",
    "aa.........................aa"
    };
    __
    # more images created here
    :
    }
    :
    $killicon = $w::top->Pixmap(-data=>$pix{dynamite});
    
I.e. it remains the xpm files exactly as they are including the C-syntax.
As far as perl is concerned it is just a string without any specific
structure. Changing it to Perl-syntax would make the data not adher to
the XPM format anymore... Btw, I think we should also be able to do 
inline of png images. These are preferably stored in perl in uuencoded 
format...

Regards,
Dov

On Sat, Nov 29, 2003 at 07:58:32PM -0500, muppet wrote:
as seen in irc (tm)...

XPM images are designed to simple to edit, and have a C syntax file
format to make them easy to #include directly into C programs. 
embedding images this way is handy, because your program doesn't depend
on external files.

using XPM data for embedded images is possible in gtk2-perl via three
functions:

  Gtk2::Gdk::Pixmap->create_from_xpm_d
  Gtk2::Gdk::Pixmap->colormap_create_from_xpm_d
  Gtk2::Gdk::Pixbuf->new_from_xpm_data

the Pixmap functions create server-side resources; the colormap form
allows you to create a pixmap without a window (the window is only
needed to get the visual and colormap, anyway).

the Pixbuf function creates a client-side resource.

The hard part, then, is getting the right input format for those
functions.

as i mentioned, XPM files are C syntax.  C syntax isn't quite Perl
syntax; if you just copy and paste the contents of the XPM into your
program, you'll get errors.  all you need to do is mangle the C
array-of-strings declaration into a Perl list-of-strings declaration. 
since this can be tedious work, especially if you want to automate the
process of updating the images, i've attached an example of how to use
Perl's all-powerful regular expressions to do this for you.


does this look useful enough to distribute and install with Gtk2 (like
gdk-pixbuf-csource, which comes with gtk+)?


other examples of how to create images from inline data are included in
Gtk2/examples/inline-images.pl in the Gtk2 source distribution.


props to cipher for bugging me for this.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]