From 9a0bca07af2cce948ac88fd9621796a0c88c5fb7 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 16 Aug 2013 16:47:18 +0200 Subject: Create the transparency mask only when it is really needed. That is, when the border is not opaque. --- mkindex.pl | 58 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 25 deletions(-) (limited to 'mkindex.pl') diff --git a/mkindex.pl b/mkindex.pl index 1c6010c..bfb41dd 100755 --- a/mkindex.pl +++ b/mkindex.pl @@ -505,9 +505,9 @@ sub annotate { undef $ds; die "Error: Couldn't inverse affine transformation\n" unless $ok; - my $img = Image::Magick::->new(); - $img->Read( $filename ); - my ($width, $height) = $img->Get(qw/width height/); + my $raster = Image::Magick::->new(); + $raster->Read( $filename ); + my ($width, $height) = $raster->Get(qw/width height/); my @draw = ( fill => 'none' , strokewidth => $config{border} @@ -517,6 +517,8 @@ sub annotate { # To get the list of supported fonts: `convert -list font`. , font => $config{font} , pointsize => $config{fontsize} + , fill => $config{fontcolor} + , gravity => 'Center' ); # Separate RGB and alpha channels. This complicated business is @@ -525,26 +527,30 @@ sub annotate { # areas (due to the default composition, which multiplies the # values. # - # To get the list of supported colors: `convert -list color`. + # To get the list of known color names: `convert -list color`. my $pix = Image::Magick::->new(); my ($r, $g, $b, $a) = $pix->QueryColor( $config{bordercolor} ); my $max = (2 << ($pix->Get('depth') - 1)) - 1.; undef $pix; - ($r,$g,$b,$a) = map { $_ * 100. / $max } ($r,$g,$b,$a); + ($r,$g,$b,$a) = map { ($_ // 0) * 100. / $max } ($r,$g,$b,$a); my ($b_rgb, $b_alpha) = ("RGB($r%,$g%,$b%)", 'GRAY('.(100-$a).'%)'); my $rgb= Image::Magick::->new( size => "${width}x${height}" ); $rgb->Read( 'xc:none' ); - my $mask = Image::Magick::->new( size => "${width}x${height}" ); - $mask->Read( 'xc:black' ); - # We really want 'Gray', but a non-linear Gray. See - # http://www.imagemagick.org/script/color-management.php - # for the trick: - # convert myimage.png -set colorspace RGB -colorspace gray myRGBimage.png - # (Also, setting the 'colorspace' during the object creation was - # useless.) - $mask->Set( colorspace => 'RGB' ); + my $mask; + if ($a > 0) { + &debug( "Adding a transparency mask (border opacity: $a%)." ); + $mask = Image::Magick::->new( size => "${width}x${height}" ); + $mask->Read( 'xc:black' ); + # We really want 'Gray', but a non-linear Gray. See + # http://www.imagemagick.org/script/color-management.php + # for the trick: + # convert img.png -set colorspace RGB -colorspace gray RGBimg.png + # (Also, setting the 'colorspace' during the object creation was + # useless.) + $mask->Set( colorspace => 'RGB' ); + } foreach my $map (@mapset) { # In case the projection have changed, the map may have been rotated, @@ -558,7 +564,7 @@ sub annotate { # Add the borders. We do that after the text to draw over in # case of intersection. $rgb->Draw( @draw, stroke => $b_rgb, points => $points ); - $mask->Draw(@draw, stroke => $b_alpha, points => $points ); + $mask->Draw(@draw, stroke => $b_alpha, points => $points ) if $mask; # Get the extremes of this polygon. my ($lx,$uy,$rx,$ly); @@ -574,26 +580,28 @@ sub annotate { # Add the caption. ('gravity' is relative to the 'geometry'.) # Everything that's not in the cell's region is trimmed away. - my @ann = ( @ann, geometry => $cell, gravity => 'Center' - , text => $map->{text} ); - $img->MogrifyRegion( $cell, 'Annotate', - @ann, fill => $config{fontcolor} ); + $raster->MogrifyRegion( $cell, 'Annotate' + , @ann + , geometry => $cell + , text => $map->{text} ); } # Change the colorspace (now to non-linear gray), and turn off # the alpha channel. Then copy this (shared) channel back into the # RGB annotation channel. - $mask->Set( colorspace => 'Gray', alpha => 'Off' ); - $rgb->Composite( image => $mask, compose => 'CopyOpacity' ); - undef $mask; + if ($mask) { + $mask->Set( colorspace => 'Gray', alpha => 'Off' ); + $rgb->Composite( image => $mask, compose => 'CopyOpacity' ); + undef $mask; + } # Merge all the annotations into the mosaic. - $img->Composite( image => $rgb, compose => 'Over' ); + $raster->Composite( image => $rgb, compose => 'Over' ); undef $rgb; binmode STDOUT if $index =~ /^(?:[[:alnum:]]*:)?-$/; # Useful for pipes - $img->Write($index); - undef $img; + $raster->Write($index); + undef $raster; } __END__ -- cgit v1.2.3