1#!/usr/local/bin/perl
2#
3# gif2xface -- converts a 48x48 GIF file to an X-Face mail header
4#
5# Author:  Ricardo Mones <ricardo@mones.org>
6#
7#   This is a hack over the original xbm2face script. The xbm files generated
8#   by some graphic tools (notably The Gimp version 1.2.1) aren't suitable to
9#   feed the compface program. Starting with a GIF and using some filters does
10#   the trick. A little help screen also added.
11#   This requieres giftopnm and pbmtoxbm (both in libgr-progs package).
12#
13#   The original xbm2face author's comment follows:
14#
15# xbm2xface -- converts a 48x48 xbm file to an X-Face mail header
16#
17# Author:  Jonathan Stigelman <Stig@hackvan.com>
18#
19# URL:     http://hackvan.com/pub/stig/src/linux/
20# FTP:	   hackvan.com:/pub/stig/src/linux/
21#
22#   This is a Perl script that I wrote to convert 48x48 xbm (X bitmap) files
23#   into X-Face: headers suitable for inclusion in RFC822 internet
24#   electronic mail.  A 48x48 bitmap is awfully small, but X-Faces are still
25#   good enough for other people to visually establish your identity in
26#   email without having to carefully read your name.
27#
28#   Basically, it gets you noticed...either as the person with the cool
29#   X-Face or as that jerk with modem noise in all of his email messages.
30#
31#   People will start looking over your shoulder and say "Hey Cool!  How'd
32#   you do that?"  When they do, you just send 'em to my URL for this
33#   utility and tell 'em to upgrade to a real mail reader: XEmacs and VM.
34#
35# It also requires the 'compface' utility.
36#
37
38sub check_for_help {
39  local($param) = @_;
40
41  # is a filter, no args must be present
42  if (defined($param))
43  {
44    print "\n", 'gif2xface -- A filter for converting an 48x48 gif into a xface string', "\n\n" ;
45    print 'Usage:  gif2xface < input.gif > output.xface', "\n\n";
46    exit;
47  }
48}
49
50sub reverse_byte {
51    local($byte) = @_;
52    local($n, $b);
53    for ( $b= $n= 0; $b<8; ++$b) {
54	$n |= (($byte & 1) << (7-$b));
55	$byte >>= 1;
56    }
57    return($n);
58}
59
60
61&check_for_help($ARGV[0]);
62
63# printf "0x%02x\n", &reverse_byte(0xF0);
64
65$ra = rand;
66$tf = "/tmp/gif2xface.$ra";
67open(GP,"|giftopnm|pbmtoxbm>$tf");
68
69while (<>) {
70  print GP $_;
71}
72close(GP);
73open(GP,"<$tf");
74<GP>;
75m/^#define \w+_width (\d+)/ && ($width=$1);
76<GP>;
77m/^#define \w+_height (\d+)/ && ($height=$1);
78<GP>;
79m/^static.* = \{/ && (( $width == 48 && $height == 48 )
80		      || die $0, ": sorry, xfaces must be 48x48 pixels" );
81
82$| = 1; print "X-Face:";
83
84open(CF,"|compface");
85
86while (<GP>) {
87    $st="";
88    while (s/(0x..)(,|\};)\s*//) {
89	$st .= sprintf("0x%02x, ", &reverse_byte(eval($1)));
90    }
91    $_=$st;
92    s/(0x..), 0x(..)/\1\2/g;
93    s/\s*(0x...., 0x...., 0x....)(,|\};)\s/\1,\n/g;
94    print CF $_;
95}
96close (CF);
97close (GP);
98unlink $tf;
99