embedding xpm images in your perl scripts



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.

Attachment: xpm2perl
Description: Text Data



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