2009年3月23日星期一

XPM图像

X PixMap

X Pixmap (XPM) is an ASCII-text-based image format used by the X Window System.

xpm可以直接用文本表示
vim /usr/share/system-config-network/pixmaps/uk.xpm
static char * uk_xpm[] = {
"21 14 5 1",
" c None",
". c #000080",
"+ c #C0C0C0",
"@ c #FF0000",
"# c #FFFFFF",
"#......#@@@#.....@@++",
"@##....#@@@#...@@##++",
".@@##..#@@@#.@@##..++",
"...@@#.#@@@#@##....++",
"########@@@########++",
"@@@@@@@@@@@@@@@@@@@++",
"@@@@@@@@@@@@@@@@@@@++",
"@@@@@@@@@@@@@@@@@@@++",
"########@@@########++",
"....##@#@@@#.#@@...++",
"..##@@.#@@@#..##@@.++",
"##@@...#@@@#....##@++",
"@@.....#@@@#......#++",
"+++++++++++++++++++++"};

而且是直接表示成c中字符数组的形式,比起其他的图片格式,这种表示方法感觉太简洁强大了。
第一行的意思为:图片宽21为像素,高为14像素,5种颜色,每个数据字母代表一个像素
之后5行为5中颜色的定义,把每种颜色map到一个字母上面
之后便是21×14的像素数据

perl中也可以直接用操作xpm数据。下面是一个gtk2的例子

#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

my @question_xpm = (
'32 32 17 1',
' c None',

'. c #030303',
'+ c #1A1A1A',
'@ c #4A4A4A',
'# c #616161',
'$ c #939393',
'% c #AAAAAA',
'& c #D2D2D2',
'* c #DCDCDC',
'= c #E2E2E2',
'- c #E6E6E6',
'; c #EDEDED',
'> c #343434',
', c #AEAEAE',
'x c #7B7B7B',
') c #C5C5C5',
'! c #F3F3F3',
' ',
' ',
' ',
' )&&&&)), ',
' )=*=--=-=;*& ',
' )*-;;!;;!;;;** ',
' )=;!)x@>+>#)!;!* ',
' &*!;#.>x%%x..>&!!& ',
' =;!@.$!!;;!=+.+;;=& ',
' );!%..;!;--;!$..x!;& ',
' *;;#..&!-&*;!&..>!;& ',
' &=;#..x!=&*;!&..>!;& ',
' )*;*@>&;&)=;!$..@!;& ',
' &*;;;;=&=;!!+..,!;* ',
' &--***=;!*@..#!;= ',
' )&)&=;!$..+x!;=& ',
' %&;!#.@%;!!=& ',
' &&;$.x!!;;-* ',
' &-!#.!!;=&) ',
' )-!@@;;*) ',
' )*;x$;-) ',
' &;!!!;;* ',
' &*!&@@);=& ',
' *=;@..@;=) ',
' *-;@..>!;* ',
' )-;)>>&-*& ',
' )=!!;;=) ',
' &=-;=) ',
' )**) ',
' ',
' ',
' ');

my ($vsize,$hsize) = (51,51);
my $file = '/usr/share/pixmaps/gnomine/mine.svg';
my $window = Gtk2::Window->new();
my $hbox = Gtk2::HBox->new();
my $button = Gtk2::Button->new();
my $button2 = Gtk2::Button->new();
$button->set_size_request($vsize,$hsize);
$button2->set_size_request($vsize-5,$hsize-5);

#my $image = Gtk2::Image->new_from_file($file);
my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,$vsize-20,$hsize-20,1);
my $image = Gtk2::Image->new_from_pixbuf($pixbuf);
$button->set_image($image);
my $pixbuf2 = Gtk2::Gdk::Pixbuf->new_from_xpm_data(@question_xpm);
my $image2 = Gtk2::Image->new_from_pixbuf($pixbuf2);
$button2->set_image($image2);
$hbox->pack_start($button,0,0,0);
$hbox->pack_start($button2,0,0,0);
$window->add($hbox);

$window->show_all;
Gtk2->main;

ScreenShot
上面那个奇怪的xpm内容就是这个?号

没有评论: