# Uses $img_params
sub extract_image {
    local($page_num,$name) = @_;
    local($scale, $external, $thumbnail, $map, $psimage, $global_num, $new_num);
    # $global_num identifies this image in the original source file
    # $new_num identifies this image in images.tex
    ($global_num, $new_num) = split("#", $page_num);
    $name =~ s/\*//;
    local($env,$basename,$img) = ($name,"img$global_num");
    $env =~ s/\d+$//;
    $psname = sprintf("%s%.3d", "$$\_image", $new_num);
system("echo HELLO");
    if ( $EXTERNAL_IMAGES && $PS_IMAGES ) {
	$img =  "$basename.ps"; 
	system("cp $psname.ps $img")}
    else {
	$img =  "$basename.gif";
	($scale, $external, $thumbnail, $map, $psimage) = split('#', $img_params{$name});
	if ($NO_IMAGES) {
	    symlink("image.gif", $img);
	    if ($thumbnail) {
		symlink("image.gif", "T$img");
		$thumbnail = "T$img";
	    }
	}
	else {
	    if ( ($name =~ /figure/) || $psimage
		|| $scale || $thumbnail) {
		$scale = $FIGURE_SCALE_FACTOR unless (defined $scale);
		&convert_image("$psname.ps", $img, $scale, "");
		if ($thumbnail) {	# $thumbnail contains the reduction factor
		&convert_image("$psname.ps", "T$img", $thumbnail, "");
		$thumbnail = "T$img"}}
	    else {
		&convert_image("$psname.ps", $img, $MATH_SCALE_FACTOR , 8)}
	    if ($name =~ /(equation|eqnarray|displaymath)/) {
		&right_justify($basename, $name)}
	    if ($name =~ /inline/ && $depth{$name} != 0) {
	        &top_justify($basename, $name)}
	    if ($TRANSPARENT_FIGURES || (! $env =~/figure/o)) {
		&make_transparent($img); 
		&make_transparent("T$img") if $thumbnail; }
	    $global{'warnings'} .= "\nFailed to convert image $psname.ps" if ! -r $img;
	}  
    }
    &embed_image($img, $name, $external, $thumbnail, $map);
}

$PSTOGIFX = "/usr/local/bin/convert";

$PSTOGIFX = "echo";


sub convert_image {
    local($in_img, $out_img, $scale, $depth) = @_;
    local($paperopt) = ( ($ENV{'PSTOPPM'} =~ /pstoppm\.ps/) ?
			" -papersize $PAPERSIZE" : undef);
    unlink $out_img;
    system( "$PSTOGIF " .
	   # do {" -depth $depth " if $depth} .
	   # This will work not work with pstoppm3.ps
	   $paperopt .
	   do {" -scale $scale " if ($scale > 0)} .
	   " -out $out_img $in_img")
	&& print "Error while converting image: $!\n";
}
