1#======================================================================
2# The following patches get_image_size to
3#  1) use imagefile's extension (not $IMAGE_TYPE) to determine type
4#  2) and adds support for jpeg.
5#  3) to return the width & height as the 3rd & 4th values
6sub get_image_size { # clean
7    my ($imagefile, $scale) = @_;
8    $scale = '' if ($scale == 1);
9    my ($imgID,$size) = ('','');
10    my ($img_w,$img_h);
11    $imagefile =~ /\.(\w*)$/;
12    my $type = lc($1);
13    $type = $IMAGE_TYPE unless $type =~ /(gif|png|jpe?g)/;
14    if (open(IMAGE, "<$imagefile")) {
15        my ($buffer,$magic,$dummy,$width,$height) = ('','','',0,0);
16	binmode(IMAGE); # not harmful un UNIX
17#       if ($IMAGE_TYPE =~ /gif/) {
18	if ($type =~ /gif/){
19	  read(IMAGE,$buffer,10);
20	    ($magic,$width,$height) = unpack('a6vv',$buffer);
21                        # is this image sane?
22	    unless($magic =~ /^GIF8[79]a$/ && ($width * $height) > 0) {
23                $width = $height = 0;
24	    }
25        }
26#        elsif ($IMAGE_TYPE =~ /png/) {
27        elsif ($type =~ /png/) {
28            read(IMAGE,$buffer,24);
29	    ($magic,$dummy,$width,$height) = unpack('a4a12NN',$buffer);
30	    unless($magic eq "\x89PNG" && ($width * $height) > 0) {
31                $width = $height = 0;
32            }
33	}
34	elsif ($type =~ /jpe?g/){
35	  ($width,$height)=jpegsize(*IMAGE);
36	}
37	else {
38	  warn "\nUnknown image type for $imagefile"; }
39	close(IMAGE);
40
41	# adjust for non-trivial $scale factor.
42	($img_w,$img_h) = ($width,$height);
43	if ($scale && ($width * $height) > 0) {
44            $img_w = int($width / $scale + .5);
45            $img_h = int($height / $scale + .5);
46	}
47	$size = qq{WIDTH="$img_w" HEIGHT="$img_h"};
48
49	# allow height/width to be stored in the stylesheet
50	my ($img_name,$imgID);
51	if ($SCALABLE_IMAGES && $USING_STYLES) {
52	    if ($imagefile =~ /(^|[$dd$dd])([^$dd$dd]+)\.(\Q$IMAGE_TYPE\E|old)$/o) {
53		$img_name = $2;
54		$imgID = $img_name . ($img_name =~ /img/ ? '' : $IMAGE_TYPE);
55	    }
56	    if ($imgID) {
57		$width = $width/$LATEX_FONT_SIZE/$MATH_SCALE_FACTOR;
58		$height = 1.8 * $height/$LATEX_FONT_SIZE/$MATH_SCALE_FACTOR;
59		# How wide is an em in the most likely browser font ?
60		if ($scale) {
61		# How high is an ex in the most likely browser font ?
62		    $width = $width/$scale; $height = $height/$scale;
63		}
64		$width = int(100*$width + .5)/100;
65		$height = int(100*$height + .5)/100;
66		$img_style{$imgID} = qq(width:${width}em ; height:${height}ex );
67		#join('','width:',$width,'em ; height:',$height,'ex ');
68		$imgID = qq{ CLASS="$imgID"};
69	    }
70	}
71    }
72    ($size, $imgID,$img_w,$img_h);
73}
74
75# This subr grabbed from wwwis (http://www.bloodyeck.com/wwwis/)
76# jpegsize : gets the width and height (in pixels) of a jpeg file
77# Andrew Tong, werdna@ugcs.caltech.edu           February 14, 1995
78# modified slightly by alex@ed.ac.uk
79sub jpegsize {
80  my($JPEG) = @_;
81  my($done)=0;
82  my($c1,$c2,$ch,$s,$length, $dummy)=(0,0,0,0,0,0);
83  my($a,$b,$c,$d);
84
85  if(defined($JPEG)             &&
86     read($JPEG, $c1, 1)        &&
87     read($JPEG, $c2, 1)        &&
88     ord($c1) == 0xFF           &&
89     ord($c2) == 0xD8           ){
90    while (ord($ch) != 0xDA && !$done) {
91      # Find next marker (JPEG markers begin with 0xFF)
92      # This can hang the program!!
93      while (ord($ch) != 0xFF) { return(0,0) unless read($JPEG, $ch, 1); }
94      # JPEG markers can be padded with unlimited 0xFF's
95      while (ord($ch) == 0xFF) { return(0,0) unless read($JPEG, $ch, 1); }
96      # Now, $ch contains the value of the marker.
97      if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
98        return(0,0) unless read ($JPEG, $dummy, 3);
99        return(0,0) unless read($JPEG, $s, 4);
100        ($a,$b,$c,$d)=unpack("C"x4,$s);
101        return ($c<<8|$d, $a<<8|$b );
102      } else {
103        # We **MUST** skip variables, since FF's within variable names are
104        # NOT valid JPEG markers
105        return(0,0) unless read ($JPEG, $s, 2);
106        ($c1, $c2) = unpack("C"x2,$s);
107        $length = $c1<<8|$c2;
108        last if (!defined($length) || $length < 2);
109        read($JPEG, $dummy, $length-2);
110      }
111    }
112  }
113  return (0,0);
114}
115
116#======================================================================
1171;
118