InlineXPM

From WxPerl Wiki
Jump to: navigation, search

Problem

Loading a bitmap from a set of data can be very practical. Especially the XPM format allows a lot of manipulation by programming and even editing since its visuals can be even reproduced by the eye. The crucial line

my $bitmap = Wx::Bitmap->newFromXPM( \@xpmfile );

is pretty straightforward (every element of @xpmfile is a line of the file) but the accepted data format has some differences to what you know from a file (see last chapter). It has not the first two lines of C syntax and no quotes, commas nor line end characters. Translated into Perl this means

@xpmfile = map { s/\r|\n|"|,//g; $_} @xpmfile;

Full Program

#!usr/bin/perl
use v5.12;
use warnings;

InlineXPM->new->MainLoop;

package InlineXPM;
use Wx;
use base qw(Wx::App);

sub OnInit {
	my $app   = shift;
	my $frame = Wx::Frame->new( undef, -1, __PACKAGE__, [-1,-1], [1000,800]);
	Wx::InitAllImageHandlers();

	# load bmp data from a file and prepare it for inlining
	my $file = 'proton.xpm';
	open my $FH, '<', $file; 
	binmode($FH);
	my @xpmfile = <$FH>;
	@xpmfile = map { s/\r|\n|"|,//g; $_} @xpmfile;
	shift @xpmfile;
	shift @xpmfile;

	# load bmp data from __DATA__ section
	my @xpmdata = <DATA>;
	@xpmdata = map { s/\r|\n//g; $_} @xpmdata;

	my $xpmhere = <<'EOB';
32 32 5 1
 	c None
.	c #808080
+	c #FFFFFF
@	c #000000
#	c #000080
................................
................................
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++#################++++++++@.
..+++#################++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###################++++++@.
..+++###################++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###############++++++++++@.
..+++###############++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###################++++++@.
..+++###################++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++################+++++++++@.
..+++################+++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++##################+++++++@.
..+++##################+++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.
................................
EOB

	# just use one of three ways or the file directly
	my $bitmap = Wx::Bitmap->newFromXPM( \@xpmfile );
	# my $bitmap = Wx::Bitmap->newFromXPM( \@xpmdata );
	# my $bitmap = Wx::Bitmap->newFromXPM( [split("\r|\n", $xpmhere)] );
	# my $bitmap = Wx::Bitmap->new($file, &Wx::wxBITMAP_TYPE_XPM);

	my $bmp = Wx::StaticBitmap->new($frame, -1, $bitmap); 
	my $ed = Wx::TextCtrl->new($frame, -1,);

	# show data in the editor
	# $ed->AppendText($_) for @xpmdata;
 	$ed->AppendText($xpmhere) ;
 
	my $sizer = Wx::BoxSizer->new(&Wx::wxVERTICAL);
	$sizer->Add($bmp, 0);
	$sizer->Add($ed, 1, &Wx::wxGROW);
	$frame->SetSizer($sizer);
	$frame->Show(1);
	$app->SetTopWindow($frame);
	1;
}

__DATA__
32 32 5 1
 	c None
.	c #808080
+	c #FFFFFF
@	c #000000
#	c #000080
................................
................................
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++#################++++++++@.
..+++#################++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###################++++++@.
..+++###################++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###############++++++++++@.
..+++###############++++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++###################++++++@.
..+++###################++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++################+++++++++@.
..+++################+++++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..+++##################+++++++@.
..+++##################+++++++@.
..++++++++++++++++++++++++++++@.
..++++++++++++++++++++++++++++@.
..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.
................................

proton.xpm

/* XPM */
static char * proton_xpm[] = {
"32 32 5 1",
" 	c None",
".	c #808080",
"+	c #FFFFFF",
"@	c #000000",
"#	c #000080",
"................................",
"................................",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++#################++++++++@.",
"..+++#################++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++###################++++++@.",
"..+++###################++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++###############++++++++++@.",
"..+++###############++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++###################++++++@.",
"..+++###################++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++################+++++++++@.",
"..+++################+++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..+++##################+++++++@.",
"..+++##################+++++++@.",
"..++++++++++++++++++++++++++++@.",
"..++++++++++++++++++++++++++++@.",
"..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.",
"................................"};
Personal tools
Hosting-sponsor

TNX