Subversion Repositories svn.mios

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1192 Marxon 1
# $Id: convpix_c.pl 43 2008-09-30 23:30:38Z tk $
2
#!/usr/bin/perl -w
3
#
4
# convpix_c.pl by Thorsten Klose (initial version 8/95, C output version 9/2008)
5
# converts graphic to asm-data for graphic display
6
#
7
 
8
use Getopt::Long;
9
 
10
$debug = 0;
11
$icons_per_line = 0;
12
$height = 8;
13
 
14
$cmd_args = join(" ", @ARGV);
15
 
16
GetOptions (
17
   "debug" => \$debug,
18
   "icons_per_line=s" => \$icons_per_line,
19
   "height=s" => \$height,
20
   );
21
 
22
if( scalar(@ARGV) != 1 )
23
{
24
   die "SYNTAX:   convpix.pl <xpm-file> [<-debug>]\n" .
25
       "EXAMPLE:  convpix.pl font.xpm\n";
26
}
27
 
28
$input_file=$ARGV[0];
29
 
30
if( $height % 8 )
31
{
32
   die "ERROR: height must be multiple of 8!\n";
33
}
34
 
35
$output_file=sprintf("%s.inc",substr($input_file, 0, length($input_file)-4));
36
 
37
open(IN,"<$input_file");
38
 
39
my @graphic = ();
40
my $state = "READ_HEADER";
41
my $y = 0;
42
my $x_max;
43
my $y_max;
44
 
45
while( <IN> )
46
{
47
   $line=$_;
48
   chomp($line);
49
 
50
#   print "[${state}] ${line}\n";
51
 
52
   if( $state eq "READ_HEADER" )
53
   {  
54
      if( $line ne "/* XPM */" )
55
      {
56
         die "ERROR: Expecting .xpm Format!\n";
57
      }
58
      $state = "WAIT_RESOLUTION";
59
   }
60
   elsif( $state eq "WAIT_RESOLUTION" )
61
   {
62
      if( /^[\s]*\"(\d+) *(\d+) *(\d+) *(\d+)\",/ )
63
      {
64
         $x_max = $1;
65
         $y_max = $2;
66
 
67
         if( $y_max % 8 )
68
         {
69
            die "ERROR: Y-Size must be dividable by 8!\n";
70
         }
71
 
72
         if( $x_max % 8 )
73
         {
74
            die "ERROR: X-Size must be dividable by 8!\n";
75
         }
76
 
77
         print "Image Size: $x_max * $y_max\n";
78
 
79
         $state = "WAIT_COLOR_0";
80
      }
81
   }
82
   elsif( $state eq "WAIT_COLOR_0" )
83
   {
84
      if( /^[\s]*\".*\",/ )
85
      {
86
         $state = "WAIT_COLOR_1";
87
      }
88
   }
89
   elsif( $state eq "WAIT_COLOR_1" )
90
   {
91
      if( /^[\s]*\".*\",/ )
92
      {
93
         $state = "READ_PIXELS";
94
      }
95
   }  
96
   elsif( $state eq "READ_PIXELS" )
97
   {
98
      if( /^[\s]*\/\*/ )
99
      {
100
         # do nothing
101
      }
102
      else
103
      {  
104
         if( substr($line, 0, 1) ne "\"" || substr($line, $x_max+1, 1) ne "\"" )
105
         {
106
            die "ERROR: Wrong Pixel Size - expecting x = $x_max!\n";
107
         }
108
 
109
         for($i=1;$i<=$x_max;++$i)
110
         {
111
            push(@graphic,substr($line, $i, 1));
112
         }
113
 
114
         if( ++$y == $y_max )
115
         {
116
            $state = "READ_BOTTOM";
117
         }
118
      }  
119
   }      
120
   elsif( $state eq "READ_BOTTOM" )
121
   {
122
      if( $line ne "};" )
123
      {
124
         die "ERROR: Wrong Pixel Size - expecting y = $x_max!\n";
125
      }
126
   }  
127
   else
128
   {  
129
      die "FATAL: Unknown State: ${state}\n";
130
   }
131
}  
132
close(IN);
133
 
134
my $expected_size = $x_max * $y_max;
135
if( scalar(@graphic) != $expected_size )
136
{
137
    die sprintf("FATAL: Wrong Graphic Size: %d (has to be $expected_size)!\n",scalar(@graphic));
138
}
139
 
140
my $character_offset = 0;
141
if( $icons_per_line )
142
{
143
   $character_offset = $x_max / $icons_per_line;
144
   printf "Calculated Character Offset: %d\n", $character_offset;
145
   printf "Character Height:            %d\n", $height;
146
 
147
   if( $y_max % $height )
148
   {
149
      die "ERROR: pic height doesn't comply with character height\n";
150
   }
151
 
152
   my @new_graphic = ();
153
 
154
   my $x=0;
155
   my $new_x_max = ($x_max * $y_max) / 8;
156
   my $i;
157
   my $j;
158
   my $k;
159
   my $l;
160
   my $m;
161
   for($i=0; $i<$y_max/$height; ++$i)
162
   {
163
      for($j=0; $j<$icons_per_line; ++$j)
164
      {
165
     for($l=0; $l<$height/8; ++$l)
166
     {
167
            for($k=0; $k<$character_offset; ++$k)
168
            {
169
           for($m=0; $m<8; ++$m)
170
           {
171
          my $ix = ($j*$character_offset + $k) + (($i*$height + $l*8 + $m)*$x_max);
172
          my $new_ix = $x + ($m*$new_x_max);
173
          $new_graphic[$new_ix] = $graphic[$ix];
174
#         printf "%2d %2d %2d %2d %2d -> %3d/%3d -> %s [%3d/%3d]\n", $i, $j, $k, $l, $m, $ix % $x_max, $ix / $x_max, $graphic[$ix], $new_ix % $new_x_max, $new_ix / $new_x_max;
175
           }
176
           ++$x;
177
        }
178
     }
179
      }
180
   }
181
   @graphic = @new_graphic;
182
   $x_max = $new_x_max;
183
   $y_max = 8;
184
}
185
 
186
open(OUT,">$output_file");
187
 
188
printf OUT "// converted with 'convpix %s'\n\n", $cmd_args;
189
 
190
if( 0 ) # for debugging
191
{
192
   for($h=0; $h<$y_max; ++$h)
193
   {
194
      for($i=0; $i<$x_max; ++$i)
195
      {
196
         print OUT $graphic[$h*$x_max + $i];
197
      }  
198
      print OUT "\n";
199
   }
200
   close(OUT);
201
   exit;
202
}
203
 
204
my @lines = ();
205
for($h=0; $h<$y_max/8; ++$h)
206
{
207
  my @bytes = ();
208
 
209
  for($i=0; $i<$x_max; ++$i) {
210
    my $bits = "";
211
    my $b = 0;
212
    for($k=0; $k<8; ++$k) {
213
      if( $graphic[(($h*8+$k)*$x_max) + $i] eq "." ) {
214
    $b |= (1 << $k);
215
      }
216
    }
217
    push @bytes, sprintf("0x%02x", $b);
218
  }
219
 
220
  my @line = ();
221
  foreach $b (@bytes) {
222
    push @line, $b;
223
    if( scalar(@line) == 16 ) {
224
      push @lines, join(",", @line);
225
      @line = ();
226
    }
227
  }
228
 
229
  if( scalar(@line) ) {
230
    push @lines, join(",", @line);
231
  }
232
}
233
 
234
printf OUT join(",\n", @lines) . "\n";
235
close(OUT);
236
print "Ok.\n";
237