#!/usr/bin/perl # @(#) Usage : xbmp2bmp.pl input output # @(#) Example : xbmp2bmp.pl file.xbm file.bmp # @(#) Copyright (C) 2001, Web Sailor Production, All Rights Reserved. # @(#) 23Jul01 - FSNG228, Initial creation. # @(#) # @(#) This script converts a xbm bitmap file to windows bitmap file. open(FPTR,$ARGV[0]) || die("Error opening file $ARGV[0]"); @lines = ; close(FPTR); @data = (); foreach $ix (@lines) { $ix =~ s/^\s+|\s+$//g; if ($ix =~ m/\#define\s+.*?width\s+([0-9]+)/) { $bitmap_width = $1; } @arrays = $ix =~ m/0x([0-9a-fA-F]{2})/g; push(@data,@arrays); } $bitmap_data = ""; map($_ = hex($_),@data); foreach $ix (@data) { # Mirror the byte. $value = 0; foreach (0 .. 7) { $value <<= 1; $value |= $ix & 0x01; $ix >>= 1; } $bitmap_data .= sprintf("%02x",$value); } $char_num = 2 * $bitmap_width / 8; @bitmaps_lines = $bitmap_data =~ m/(.{$char_num})/g; &OutputBitmapFile($ARGV[1],$bitmap_width,@bitmaps_lines); sub OutputBitmapFile() { # @bitmap_lines contains the bitmap data for every line. # $bitmap_width is the width of the bitmap. my ($output_file,$bitmap_width,@bitmaps_lines) = @_; # Local variables. my ($bitmap_height,$bytes_per_line,$image_size,$ix,$iy,@bmp_hdr,@tmp) = (); $bitmap_height = scalar(@bitmaps_lines); @bitmaps_lines = reverse(@bitmaps_lines); printf("width = %d, height = %d\n",$bitmap_width,$bitmap_height); @bmp_hdr = ( 0x42, 0x4D, 0x8E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00 ); $bytes_per_line = int($bitmap_width / 8); $bytes_per_line++ if (($bitmap_width % 8) > 0); $bytes_per_line += 4 - ($bytes_per_line % 4) if (($bytes_per_line % 4) > 0); printf("bytes per line = $bytes_per_line\n"); $bmp_hdr[21] = ($bitmap_width >> 24) & 0xff; $bmp_hdr[20] = ($bitmap_width >> 16) & 0xff; $bmp_hdr[19] = ($bitmap_width >> 8) & 0xff; $bmp_hdr[18] = ($bitmap_width) & 0xff; $bmp_hdr[25] = ($bitmap_height >> 24) & 0xff; $bmp_hdr[24] = ($bitmap_height >> 16) & 0xff; $bmp_hdr[23] = ($bitmap_height >> 8) & 0xff; $bmp_hdr[22] = ($bitmap_height) & 0xff; # Image size. $image_size = $bytes_per_line * $bitmap_height; printf("Image size = %d, = %x\n",$image_size,$image_size); $bmp_hdr[37] = ($image_size >> 24) & 0xff; $bmp_hdr[36] = ($image_size >> 16) & 0xff; $bmp_hdr[35] = ($image_size >> 8) & 0xff; $bmp_hdr[34] = ($image_size >> 0) & 0xff; open(FOUT,">$output_file") ||die("Error opening file $output_file"); binmode(FOUT); foreach $ix (@bmp_hdr) { printf FOUT ("%c",$ix); } foreach $ix (@bitmaps_lines) { @tmp = $ix =~ m/(..)/g; foreach $iy (@tmp) { printf FOUT ("%c",(hex($iy)^0xff)); } for($iy=scalar(@tmp); $iy<$bytes_per_line; $iy++) { printf FOUT ("%c",0); } } close(FOUT); }