File: hex2h.pl

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 28,424 kB
  • sloc: cpp: 299,797; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (97 lines) | stat: -rwxr-xr-x 2,509 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

# PCSX2 - PS2 Emulator for PCs
# Copyright (C) 2002-2011  PCSX2 Dev Team
#
# PCSX2 is free software: you can redistribute it and/or modify it under the terms
# of the GNU Lesser General Public License as published by the Free Software Found-
# ation, either version 3 of the License, or (at your option) any later version.
#
# PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with PCSX2.
# If not, see <http://www.gnu.org/licenses/>.

# This basic script convert an jpeg/png image to a .h include file 
# compatible with PCSX2 gui

use File::Basename;
use strict;
use warnings;

sub ascii_to_hex ($)
{
    (my $str = shift) =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg;
    return $str;
}

sub get_wx_ext ($)
{
    my $ext = shift;

    my $result = "BAD_FORMAT";
    if ($ext =~ /png/i) {
        $result = "wxBITMAP_TYPE_PNG";
    } elsif ($ext =~ /jpe?g/i) {
        $result = "wxBITMAP_TYPE_JPEG";
    } else {
        print "ERROR: bad format $ext\n";
    }

    return $result;
}

my $input=$ARGV[0];
my $output=$ARGV[1] . ".h" ;

my($filename, $directories, $suffix) = fileparse($input);
my ($name, $ext) = split(/\./,$filename);

my $wx_img_class = "res_$name";
my $wx_img_extension = get_wx_ext($ext);
my $filesize = -s $input;

### Collect binary data
my $lenght = 1;
my $binary = "\t\t";
my $data;
my $byte;

open(IN,"$input");
binmode IN;
while (($byte = read IN, $data, 1) != 0) {
    my $hex = ascii_to_hex($data);
    $binary .= "0x$hex";
    if ($lenght % 17 == 0 && $lenght > 1) {
        # End of line
        $binary .= ",\n\t\t";
    } elsif ($filesize == $lenght) {
        # End of file
        $binary .= "\n";
    } else {
        $binary .= ",";
    }
    $lenght++;
}
close(IN);

open(OUT,">$output");
### Print the header
print OUT "#pragma once\n\n";
print OUT "#include \"Pcsx2Types.h\"\n";
print OUT "#include <wx/gdicmn.h>\n\n";
print OUT "class $wx_img_class\n{\n";
print OUT "public:\n";
print OUT "\tstatic const uint Length = $filesize;\n";
print OUT "\tstatic const u8 Data[Length];\n";
print OUT "\tstatic wxBitmapType GetFormat() { return $wx_img_extension; }\n};\n\n";
print OUT "const u8 ${wx_img_class}::Data[Length] =\n{\n";

### Print the array
print OUT $binary;
print OUT "};\n";

close(OUT);