-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpm2src.pl
132 lines (104 loc) · 2.75 KB
/
xpm2src.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl
#
# xpm 2 src
#
# quick and dirty perl script to read in an XPM file, and save
# out a source file to be used with loader.bin
#
# Created 2005-08 By Scott Lawrence
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# xpm parsing heuristics:
# ignore anything not in quotes on a line
sub parseit
{
my( $xpmfile, $cfile, $name, $line, $content, $state );
my( $w, $h, $c, $p );
my( $cc, $cj, $cid, $cval );
my( $curcol );
my( $byte, $nper );
$xpmfile = shift;
$name = shift;
$cfile = $name . ".c";
$state = 0; # waiting on parameters
$curcol = 0; # current color = 0;
open OF, ">$cfile";
printf OF "/* Generated by xpm2src.pl */\n";
printf OF "#include \"tools.h\"\n";
printf OF "unsigned char %s_img_data[] = {\n", $name;
open IF, "$xpmfile";
foreach $line (<IF>)
{
chomp $line;
$line =~ s/\/\*.*\*\///g; # zot comments
$line =~ s/.*"(.*)".*/$1/; # look at stuff in quotes only
$line = $1;
next if( 0 == (length $line) );
if( $state == 2 ) # image data
{
printf "%s\n", $line;
my( $x, $i );
for( $x=0 ; $x<(length $line) ; $x++ )
{
$i = substr( $line, $x, 1 );
#printf "%d", $colhash{ $i };
$byte = $byte << 2;
$byte += $colhash{ $i };
$bytec++;
if( $bytec > 3 ) {
printf OF " 0x%02x,", $byte;
$bytec = 0;
$byte = 0;
}
}
printf OF "\n";
}
if( $state == 1 ) # get colors
{
$cc = substr $line, 0, $p;
$cid = substr $line, $p+3;
$cval = 3;
# some rough assumptions...
if( $cid eq "#555555" ) { $cval = 2; }
if( $cid eq "#AAAAAA" ) { $cval = 1; }
if( $cid eq "#FFFFFF" ) { $cval = 0; }
if( $cid eq "gray100" ) { $cval = 0; }
if( $cid eq "white" ) { $cval = 0; }
printf "Color %d is |%s| -> %s %d\n", $curcol, $cc, $cid, $cval;
$colhash{ $cc } = $cval;
$curcol++;
if( $curcol >= $c ) {
$state = 2;
$nper = 0;
$byte = 0;
$bytec = 0;
}
}
if( $state == 0 ) # get dimenstions
{
($w, $h, $c, $p) = split " ", $line;
$state = 1;
printf "Image is %dx%d, %d colors, %d chars per pixel\n", $w, $h, $c, $p;
}
}
close IF;
printf OF "};\n";
printf OF "img %s_hdr = {\n", $name;
printf OF " 0x00, 0x00,\n"; # offx, offy
printf OF " 0x%02x, 0x%02x,\n", $h, $w; # height, width
printf OF " 0x%02x,\n", ($w/4); # data_width
printf OF " 0x02,\n"; # img_type
printf OF " 0x00,\n"; # padding
printf OF " 0x%02x,\n", ($w * $h)/4; # length
printf OF " %s_img_data\n", $name; # data
printf OF "};\n";
close OF;
}
sub main
{
parseit( $ARGV[0], $ARGV[1] );
}
&main;