-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp2h.pl
executable file
·79 lines (61 loc) · 1.29 KB
/
bmp2h.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
#!/usr/bin/perl
use strict;
use warnings;
my $negative = 0;
my $order = 1;
my $filename;
my $bitmap = "bitmap";
while ($_ = shift @ARGV){
if (/-n/){
$negative = 1;
next;
} elsif(/-b(.*)/) {
$bitmap = $1 ? $1 : shift @ARGV;
next;
}
$filename = $_;
}
print STDERR "bmpfile: $filename\n";
open my $in, '<', $filename || die("Can't read.\n");
binmode $in;
my $bmpsize = -s $in;
print STDERR "size: $bmpsize\n";
my $bmp;
read($in, $bmp, $bmpsize, 0);
undef $in;
my @bytes = unpack("C*", $bmp);
my ($bfType, $bfSize, $bfR1, $bfR2, $bfoffBits,
$bcSize, $bcWidth, $bcHeight, $bcPlanes, $bcBitCount)
= unpack("vVvvV VVVvv", $bmp);
if ($bcHeight < 0){
$order = 0;
$bcHeight = -$bcHeight;
}
print STDERR "Offset: $bfoffBits\n";
my (@data) = unpack("x$bfoffBits C*", $bmp);
if ($order){
my @d;
for (my $y = $bcHeight - 1; $y > -1; $y --){
for (my $x = 0; $x < ($bcWidth / 8); $x ++){
push @d, $data[$y * ($bcWidth / 8) + $x];
}
}
@data = @d;
}
print "/* filename: $filename */\n";
print "#define ${bitmap}_w $bcWidth\n";
print "#define ${bitmap}_h $bcHeight\n";
print "const uint8_t PROGMEM ${bitmap}[] = {\n\t";
my $cnt = 0;
foreach my $b (@data){
if ($negative){
$b = $b ^ 0xff; # xor
}
printf("%#2X, ", $b);
$cnt ++;
if ($cnt == 7){
print "\n\t";
$cnt = 0;
}
}
print "\n};\n";