
\section{im-hax-27-39}
\filbreak \subsubsection{readme.txt}
.\newline{}
\begin{verbatim}

# Name:
#   readme.txt
#
# Author:
#   Ron Savage <ron@savage.net.au>
#
# Home:
#   http://savage.net.au/ImageMagick.html

Overview
--------
o   ImageMagick Hax are a set of Perl scripts aimed at people beginning to use
    the Perl module Image::Magick. The aim is to get you up and running as
    quickly as possible.

o   You are expected to study the scripts before running them. Hence the
    promise: No surprises.

o   Each hax is deliberately designed to be trivial, so as to help focus on
    one feature of ImageMagick. The name of the hax tells you the point of
    the exercise. Eg: im-hax-crop-1.pl demonstrates Crop(...).
    It's that simple. This helps deliver on the promise: No surprises.

o   *.cgi are CGI scripts, and *.pl are command line scripts. A simple menu
    script, im-hax-menu.cgi, is supplied to help run the CGI scripts.

o   Comments at the start of each hax list the assumed directory structure,
    input files and output files. These are also stored in variables at the
    start of each script, to facilitate installation anywhere.

o   All input files are either shipped with ImageMagick or output from a hax.
    Eg: The output of im-hax-rotate-1.pl is input to im-hax-composite-1.pl.

o   Only the generic TTF file, Generic.ttf is used to annotate images. Nothing
    would be gained here by using a different font file.

Files
-----
o   im-hax-1-13.tgz contains ImageMagick Hax 1 .. 13, as well as the CGI menu,
    licence.txt and this file, readme.txt.

o   im-hax-14-26.tgz contains ImageMagick Hax 14 .. 26, as well as the CGI menu,
    licence.txt and this file, readme.txt.

o   im-hax-27-39.tgz contains ImageMagick Hax 27 .. 39, as well as the CGI menu,
    licence.txt and this file, readme.txt.
\end{verbatim}
\filbreak \subsubsection{im-hax-annotate-2.cgi}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl -T
#
# Name:
#   im-hax-annotate-2.cgi.
#
# Purpose:
#   Demonstrate the Annotate method,
#   using it to write vertically.
#
# V 1.01 20-Jun-2002
# ------------------
# o Change stroke => 'red' to stroke => 'none'
#   to get sharper text.
#
# V 1.00 7-May-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   Generic.ttf.
#
# Output files:
#   /apache/htdocs/im-hax-annotate-2.gif.
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use Image::Magick;

# -----------------------------------------------

my($inner_table_color)  = '#80c0ff';
my($outer_table_color)  = '#80ffff';

# -----------------------------------------------

delete @ENV{'BASH_ENV', 'CDPATH', 'ENV', 'IFS',
    'PATH', 'SHELL'}; # For security.

my($doc_root_name)      = '/apache/htdocs';
my($output_file_name)   = 'im-hax-annotate-2.gif';
my($output_path)        =
    "$doc_root_name/$output_file_name";
my($title)              = 'Test Annotate()';

my($image, $result);

my($q)      = CGI -> new();
my($html)   = [];
$image      =
    Image::Magick -> new(size => '30x160');

push(@$html, $q -> th('Image::Magick') .
    $q -> td("V $Image::Magick::VERSION") );
push(@$html, $q -> th('Image::Magick -> new()') .
    $q -> td('OK') );

$result = $image -> Read('xc:white') || 'OK';
push(@$html, $q -> th("Read(xc:white)") .
    $q -> td($result) );

my($text)       = 'Hello World';
my($x)          = 10;
my($y)          = 0;
my($pointsize)  = 12;
my($y_step)     = $pointsize + 2;

for my $i (1 .. length $text)
{
    $y += $y_step;

    # stroke => 'none', fill => 'red' produces
    # sharper text than stroke => 'red',
    # fill => 'red'.

    $result = $image -> Annotate
    (
        text        => substr($text, ($i-1), 1),
        x           => $x,
        y           => $y,
        stroke      => 'none',
        font        => 'Generic.ttf',
        pointsize   => $pointsize,
        fill        => 'red') || 'OK';
}
push(@$html, $q -> th('Annotate(...)') .
    $q -> td($result) );
push(@$html, $q -> th('$MAGICK_HOME') .
    $q -> td($ENV{'MAGICK_HOME'}) );
$result = $image -> Write($output_path) || 'OK';
push(@$html, $q -> th("Write($output_path)") .
    $q -> td($result) );
push(@$html, $q -> th('Image') .
    $q -> td
    (
        $q -> img
        ({
            src => "/$output_file_name",
        })
    ) );
push(@$html, $q -> th('&nbsp;') .
    $q -> td('&nbsp;') );
push(@$html, $q -> th('Main Menu') .
    $q -> td
    (
        $q -> a
        ({
class   => 'submit',
href    => '/cgi-bin/ImageMagick/im-hax-menu.cgi'
        }, 'im-hax-menu.cgi')
    ) );

print   $q -> header(),
        $q -> start_html($doc_root_name),
        $q -> h1({align => 'center'}, $title),
        $q -> table
        (
            {
                align   => 'center',
                bgColor => $inner_table_color
            },
            $q -> Tr([@$html])
        ),
        $q -> end_html();
\end{verbatim}
\filbreak \subsubsection{im-hax-append-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-append-1.pl.
#
# Purpose:
#   Demonstrate the Append method to combine
#   files into 1 image.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   model.gif
#   smile.gif
#
# Output files:
#   im-hax-append-1.png
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

my($input_file_name_1)  = 'smile.gif';
my($input_file_name_2)  = 'model.gif';
my($output_file_name)   = 'im-hax-append-1.png';
my($image)              = Image::Magick -> new();
my($result)             = $image -> Read
(
    $input_file_name_1,
    $input_file_name_2
);
die $result if $result;

print "\$image contains: ", ($#{$image} + 1),
    " pages (2 expected). \n";
print "Appending the images vertically. \n";

my($gallery) = $image -> Append(stack => 'True');
die $result if $result;

print "Appended images. \n";

for my $page (0 .. $#{$image})
{
    my($file_name) =
        $$image[$page] -> Get('filename');
    print
    "Image: $page. File name: $file_name. \n";
}

$result = $gallery -> Write($output_file_name);
die $result if $result;

print "Wrote: $output_file_name. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-clip-2-bmp-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-clip-2-bmp-1.pl.
#
# Purpose:
#   Determine what's in the clipboard.
#   If it's an image, write it to disk.
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None.
#
# Output files:
#   im-hax-clip-2-bmp-1.bmp.
#
# Note:
#   o This program has nothing to do with the new
#       (as of ImageMagick V 5.4.6)
#       clipboard image format
#   o If you use Alt-PrintScreen, the image is
#       correctly inserted into the clipboard.
#       Hence it can be retrieved reliably.
#       However, I can't get this program to work
#       with graphics programs which offer
#       their own Select and Copy commands,
#       for reasons I cannot imagine.
#       IMDisplay falls into this latter
#       category :-(.
#       So does convert. For instance:
#       shell>convert smile.gif clipboard:
#       puts something in the clipboard, but
#       neither this program, not convert
#       can recover that image, eg with:
#       shell>convert clipboard: smile.png
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use Win32::Clipboard;

# -----------------------------------------------

sub report
{
    my($clipboard)                  = @_;
    $content{'Is Text Available'}   =
        $clipboard -> IsFormatAvailable(CF_TEXT);
    $content{'Is DIB Available'}    =
        $clipboard -> IsFormatAvailable(CF_DIB);
    $content{'Is HDROP Available'}  =
        $clipboard ->IsFormatAvailable(CF_HDROP);
    $content{'Is Bitmap'}           =
        $clipboard -> IsBitmap();
    $content{'Is Files'}            =
        $clipboard -> IsFiles();
    $content{'Is Text'}             =
        $clipboard -> IsText();
    $content{'Text'}                =
        $clipboard -> IsText()
        ? ('<' . $clipboard -> GetText() . '>')
        : 'Clipboard does not contain text';

    print "Clipboard status: \n";
    print map
        {
            "$_ => $content{$_}. \n"
        } sort keys %content;
    print "\n";

}   # End of report.

# -----------------------------------------------

my($dir_name)       = '/temp';
my($im_version)     = $Image::Magick::VERSION;
my($clip_version)   = $Win32::Clipboard::VERSION;

chdir($dir_name)
|| die("Can't chdir($dir_name): $!");

print "Win32::Clipboard V $clip_version. \n";
print "\n";

my($clipboard)  = Win32::Clipboard();
my($debug)      = 0;

report($clipboard) if ($debug);

if ($clipboard -> IsFormatAvailable(CF_DIB))
{
    my($output_file_name)   =
        'im-hax-clip-2-bmp-1.bmp';
    my($image)              =
        Win32::Clipboard::GetBitmap()
    || die("Can't read image from clipboard");
    open(BITMAP, "> $output_file_name");
    binmode BITMAP;
    print BITMAP $image;
    close BITMAP;
    print "Wrote: $output_file_name. \n";
}
else
{
    print "Clipboard deviod of image. \n";
}
\end{verbatim}
\filbreak \subsubsection{im-hax-color-2-config-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-color-2-config-1.pl.
#
# Purpose:
#   Convert ImageMagick doc file color.html into
#   a Perl hash which is then written to a file
#   with Data::Dumper, so that it can be read in
#   and eval-ed into code, so as to provide
#   a 3-way look-up table.
#
#   See im-hax-color-2-hash.pl for the code to
#   read the output back in.
#
#   See im-hax-color-2-config-2.pl for code to
#   read colors.mgk. This program outputs in the
#   same format as im-hax-color-2-config-1.pl.
#
# Note:
#   o Store data all 3 ways per color entry in
#   color.html, ie as:
#       'aliceblue'     =>
#       {
#           hex => '#f0f8ff',
#           rgb => '240,248,255'
#       },
#       '#f0f8ff'       =>
#       {
#           color_name  => 'aliceblue',
#           rgb         => '240,248,255'
#       },
#       '240,248,255'   =>
#       {
#           color_name  => 'aliceblue',
#           hex         => '#f0f8ff'
#       }
#   o Hence:
#       First char of key   Interpretation
#       ----------------------------------
#       a .. z              Color name
#       #                   Hex value
#       0 .. 9              RGB values
#   o Lower case is used everywhere
#   o Spaces are removed everywhere,
#   eg from RGB: '240, 255, 255'
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input file:
#   color.html
#   (http://www.imagemagick.org/www/color.html)
#
# Output file:
#   im-hax-color-2-config-1.eval.
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Data::Dumper;
use DBD::AnyData;

# -----------------------------------------------

my($dir_name) = '/temp';

chdir($dir_name)
|| die("Can't chdir($dir_name): $!");

my($output_file_name)   =
    'im-hax-color-2-config-1.eval';
my($table_name)         = 'color';
my($table_type)         = 'HTMLtable';
my($table_file)         =
    '/ImageMagick/www/color.html';
my($row_names)          = 'Name,Color,RGB,Hex';
my($dbh)                =
    DBI -> connect('dbi:AnyData(RaiseError=>1):');

$dbh -> func($table_name, $table_type,
    $table_file, {col_names => $row_names},
    'ad_catalog');

my($sql) = "select * from $table_name";
my($sth) = $dbh -> prepare($sql);

$sth -> execute();

my($data, $value, $rgb, %color);

while ($data = $sth -> fetchrow_hashref() )
{
    # Table is effectively double-spaced.
    # So we examine the data, and skip any which
    # is null. Use this opportunity to lower case
    # and zap spaces.

    $value = join('', map
    {
        $$data{$_} = lc $$data{$_};
        $$data{$_} =~ tr/ //d;
        $$data{$_}
    } keys %$data);

    next if (! $value);

    $color{$$data{'Color'} }    =
    {
        hex => $$data{'Hex'},
        rgb => $$data{'RGB'}
    };
    $color{$$data{'Hex'} }      =
    {
        color   => $$data{'Color'},
        rgb     => $$data{'RGB'}
    };
    $color{$$data{'RGB'} }      =
    {
        color   => $$data{'Color'},
        hex     => $$data{'Hex'}
    };

    #print map{"$_ => $$data{$_}. "} keys %$data;
    #print "\n";
}

$dbh -> disconnect();

$Data::Dumper::Indent = 1;
open(OUT, "> $output_file_name")
|| die("Can't open(> $output_file_name): $!");
print OUT Data::Dumper -> Dump([\%color],
        [qw(*color)]);
close(OUT);
print "Wrote: $output_file_name. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-color-2-config.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-color-2-config.pl.
#
# Purpose:
#   Convert ImageMagick doc file color.html into a Perl hash
#   which is then written to a file with Data::Dumper, so that
#   it can be read in and eval-ed into code, so as to provide
#   a 3-way look-up table.
#
#   See im-hax-color-2-hash.pl for the code to read it in.
#
# Note:
#   o Store data all 3 ways per color entry in color.html, ie as:
#       'aliceblue'     => {hex         => '#f0f8ff',   rgb => '240,248,255'},
#       '#f0f8ff'       => {color_name  => 'aliceblue', rgb => '240,248,255'},
#       '240,248,255'   => {color_name  => 'aliceblue', hex => '#f0f8ff'}
#   o Hence:
#       First char of key   Interpretation
#       ----------------------------------
#       a .. z              Color name
#       #                   Hex value
#       0 .. 9              RGB values
#   o Lower case is used everywhere
#   o Spaces are removed everywhere, eg from RGB: '240, 255, 255'
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.24
#   IM: V 5.4.6
#
# Input files:
#   color.html (http://www.imagemagick.org/www/color.html).
#
# Output files:
#   im-hax-color-2-config.eval.
#
# Note:
#   tabs = 4 spaces || die.
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/Perl.html
#
# Licence:
#   Australian Copyright (c) 1999-2002 Ron Savage.
#
#   All Programs of mine are 'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them under the terms of
#   The Artistic License, a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Data::Dumper;
use DBD::AnyData;

# ---------------------------------------------------------

my($dir_name) = '/temp';

chdir($dir_name) || die("Can't chdir($dir_name): $!");

print "ImageMagick V $Image::Magick::VERSION. \n";
print "\n";

my($output_file_name)   = 'im-hax-color-2-config.eval';
my($table_name)         = 'color';
my($table_type)         = 'HTMLtable';
my($table_file)         = 'color.html';
my($row_names)          = 'Name,Color,RGB,Hex';
my($dbh)                = DBI -> connect('dbi:AnyData(RaiseError=>1):');

$dbh -> func($table_name, $table_type, $table_file, {col_names => $row_names}, 'ad_catalog');

my($sql) = "select * from $table_name";
my($sth) = $dbh -> prepare($sql);

$sth -> execute();

my($data, $value, $rgb, %color);

while ($data = $sth -> fetchrow_hashref() )
{
    # Table is effectively double-spaced.
    # So we examine the data, and skip any which is null.
    # Use this opportunity to lower case and zap spaces.

    $value = join('', map{$$data{$_} = lc $$data{$_}; $$data{$_} =~ tr/ //d; $$data{$_} } keys %$data);

    next if (! $value);

    $color{$$data{'Color'} }    = {hex      => $$data{'Hex'},   rgb => $$data{'RGB'} };
    $color{$$data{'Hex'} }      = {color    => $$data{'Color'}, rgb => $$data{'RGB'} };
    $color{$$data{'RGB'} }      = {color    => $$data{'Color'}, hex => $$data{'Hex'} };

    print map{"$_ => $$data{$_}. "} keys %$data;
    print "\n";
}

$dbh -> disconnect();

$Data::Dumper::Indent = 1;
open(OUT, "> $output_file_name") || die("Can't open(> $output_file_name): $!");
print OUT Data::Dumper -> Dump([\%color], [qw(*color)]);
close(OUT);
\end{verbatim}
\filbreak \subsubsection{im-hax-color-2-hash.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-color-2-hash.pl.
#
# Purpose:
#   Read in the file created by
#   im-hax-color-2-config-1.pl,
#   or by im-hax-color-2-config-1.pl,
#   and store the data in a Perl hash.
#   Then write the data to a text file,
#   sorted various ways.
#
#   Thus the output file can be used as a
#   reference doc to convert between color
#   names, hex value and RGB, or the hash can
#   be used in any program to convert similarly.
#
#   See im-hax-color-2-config-1.pl for the code
#   to create the input file.
#   Or
#   See im-hax-color-2-config-2.pl for the code
#   to create the input file.
#
# Note:
#   o Store data all 3 ways per color entry in
#   color.html, ie as:
#       'aliceblue'     =>
#       {
#           hex => '#f0f8ff',
#           rgb => '240,248,255'
#       },
#       '#f0f8ff'       =>
#       {
#           color_name  => 'aliceblue',
#           rgb         => '240,248,255'
#       },
#       '240,248,255'   =>
#       {
#           color_name  => 'aliceblue',
#           hex         => '#f0f8ff'
#       }
#   o Hence:
#       First char of key   Interpretation
#       ----------------------------------
#       a .. z              Color name
#       #                   Hex value
#       0 .. 9              RGB values
#   o Lower case is used everywhere
#   o Spaces are removed everywhere
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input file:
#   im-hax-color-2-config-1.eval
#   (Output by im-hax-color-2-config-1.pl).
#   Or
#   im-hax-color-2-config-2.eval
#   (Output by im-hax-color-2-config-2.pl).
#
# Output files:
#   im-hax-color-2-config.txt.
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

sub by_rgb
{
    my(@a) = split(/,/, $a);
    my(@b) = split(/,/, $b);

    sprintf('%03d%03d%03d', @a)
    cmp
    sprintf('%03d%03d%03d', @b);

}   # End of by_rgb.

# -----------------------------------------------

sub load_config
{
    my($file_name) = @_;

    my(%color, $eval);

    open(INX, $file_name)
    || die("Can't open($file_name): $!");

    {
        local $/;

        $eval = <INX>;
    }

    eval $eval;

    die($@) if ($@);

    return \%color;

}   # End of load_config.

# -----------------------------------------------

my($dir_name)       = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($dir_name)
|| die("Can't chdir($dir_name): $!");

print "ImageMagick V $im_version. \n";
print "\n";

my($input_file_name)    =
    'im-hax-color-2-config-1.eval';
my($output_file_name)   =
    'im-hax-color-2-config-1.txt';
my($color)              =
    load_config($input_file_name);

open(OUT, "> $output_file_name")
|| die("Can't open(> $output_file_name): $!");

print OUT "ImageMagick V $im_version. ",
        "Color/Hex/RGB list. \n";
print OUT "\n";

# Sort by color name.

print OUT "Table 1: Sorted by color name: \n";
print OUT "\n";

for (sort grep{/^[a-z]/} keys %$color)
{
    print OUT "Color: $_. Hex: ",
        "$$color{$_}{'hex'}. ",
        "RGB: $$color{$_}{'rgb'}. \n";
}

print OUT "\n";

# Sort by hex value.

print OUT "Table 2: Sorted by hex value: \n";
print OUT "\n";

for (sort grep{/^#/} keys %$color)
{
    print OUT "Hex: $_. Color: ",
        "$$color{$_}{'color'}. ",
        "RGB: $$color{$_}{'rgb'}. \n";
}

print OUT "\n";

# Sort by RGB value.

print OUT "Table 3: Sorted by RGB value: \n";
print OUT "\n";

for (sort by_rgb grep{/^\d/} keys %$color)
{
    print OUT "RGB: $_. Color: ",
        "$$color{$_}{'color'}. ",
        "Hex: $$color{$_}{'hex'}. \n";
}

close(OUT);

print "Wrote: $output_file_name. \n";
print "\n";
print "Testing... \n";

my($color_name) = 'darkslategrey';
my($hex)        = 'Unknown';
my($rgb)        = 'Unknown';

if ($$color{$color_name})
{
    $hex    = $$color{$color_name}{'hex'};
    $rgb    = $$color{$color_name}{'rgb'};
}

print "Color: $color_name. Hex: $hex. ",
    "RGB: $rgb. \n";

$hex        = '#32cd32';
$color_name = 'Unknown';
$rgb        = 'Unknown';

if ($$color{$hex})
{
    $color_name = $$color{$hex}{'color'};
    $rgb        = $$color{$hex}{'rgb'};
}

print "Hex: $hex. Color: $color_name. ",
    "RGB: $rgb. \n";

$rgb        = '72,209,204';
$color_name = 'Unknown';
$hex        = 'Unknown';

if ($$color{$rgb})
{
    $color_name = $$color{$rgb}{'color'};
    $hex        = $$color{$rgb}{'hex'};
}

print "RGB: $rgb. Color: $color_name. ",
    "Hex: $hex. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-colorize-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-colorize-1.pl.
#
# Purpose:
#   Demonstrate the Colorize method.
#
# Warning:
#   I could not get this program to work
#   in V 5.4.5.
#
# V 1.00 7-May-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   piddle.gif (any image with some strong colors
#   in it will do).
#
# Output files:
#   im-hax-colorize-1.png.
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
    || die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

my($input_file_name)    = 'piddle.gif';
my($output_file_name)   =
    'im-hax-colorize-1.png';
my($image)              = Image::Magick -> new();
my($result)             =
    $image -> Read($input_file_name);
die $result if $result;

$result = $image -> Colorize
(
    opacity => '80/80/80'
);
die $result if $result;

$result = $image -> Write($output_file_name);
die $result if $result;
print "Wrote: $output_file_name. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-composite-3.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-composite-3.pl.
#
# Purpose:
#   Demonstrate the Composite method.
#   4 images are created, and then
#   composed into 1.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None
#
# Output files:
#   im-hax-composite-3-one.png
#   im-hax-composite-3-two.png
#   im-hax-composite-3-three.png
#   im-hax-composite-3-four.png
#   im-hax-composite-3.png
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------
# Input parameters:
#   $output_name    A file name
#   $bkg_color      A color
#   $param          An array ref:
#                    0: A stroke and fill color
#                    1: A string of coordinates

sub make_image
{
    my($output_name, $bkg_color, $param) = @_;
    my($image)  =
        Image::Magick -> new(size => '56x56');
    my($result) =
        $image -> Read("xc:$bkg_color");
    die $result if $result;

    for my $p (@$param)
    {
        $result = $image -> Draw
        (
            primitive   => 'polygon',
            stroke      => $$p[0],
            fill        => $$p[0],
            strokewidth => 1,
            points      => $$p[1]
        );
        die $result if $result;
    }

    $result = $image -> Write($output_name);
    die $result if $result;
    print "Wrote: $output_name. \n";

}   # End of make_image.

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

# Any old images will do.

make_image('im-hax-composite-3-one.png',
    'maroon', [ ['white',  '1,6 54,54 6, 1'] ]);
make_image('im-hax-composite-3-two.png',
    'white',  [ ['green',  '1,6 54, 9 3, 4'],
    ['blue',  ' 6,1  4,3 9,54'] ]);
make_image('im-hax-composite-3-three.png',
    'red',    [ ['green',  '9,1 54, 6 1,54'] ]);
make_image('im-hax-composite-3-four.png',
    'silver', [ ['maroon', '9,1 51, 3 1, 9'],
    ['olive', '52,3 54,5 5,52'] ]);

print "\n";

my($output_file_name)   =
    'im-hax-composite-3.png';
my(%image)              =
(
    upper_left =>
    {
        file_name   =>
            'im-hax-composite-3-one.png',
        image       => '',
        width       => 0,
        height      => 0,
        x           => 0,
        y           => 0,
    },
    upper_right =>
    {
        file_name   =>
            'im-hax-composite-3-two.png',
        image       => '',
        width       => 0,
        height      => 0,
        x           => 0,
        y           => 0,
    },
    lower_left =>
    {
        file_name   =>
            'im-hax-composite-3-three.png',
        image       => '',
        width       => 0,
        height      => 0,
        x           => 0,
        y           => 0,
    },
    lower_right =>
    {
        file_name   =>
            'im-hax-composite-3-four.png',
        image       => '',
        width       => 0,
        height      => 0,
        x           => 0,
        y           => 0,
    },
);

my($image, $file_name, $result);

for $image (keys %image)
{
    $file_name = $image{$image}{'file_name'};

    print "Reading: $file_name. \n";

    $image{$image}{'image'} =
        Image::Magick -> new();
    $result                 =
    $image{$image}{'image'} -> Read($file_name);

    die $result if $result;

    ($image{$image}{'width'},
    $image{$image}{'height'}) =
    $image{$image}{'image'} ->
    Get('width', 'height');

    print "Size: $image{$image}{'width'} x ",
        "$image{$image}{'height'}. \n";
    print "\n";
}

$image{'upper_right'}{'x'}  =
    $image{'upper_left'}{'width'};
$image{'lower_left'}{'y'}   =
    $image{'upper_left'}{'height'};
$image{'lower_right'}{'x'}  =
    $image{'lower_left'}{'width'};
$image{'lower_right'}{'y'}  =
    $image{'upper_left'}{'height'};
my($width, $height)         =
    ($image{'upper_left'}{'width'} +
    $image{'upper_right'}{'width'},
    $image{'upper_left'}{'height'} +
    $image{'lower_left'}{'height'});
my($i)                      =
    Image::Magick -> new(size => "${width}x$height");
$result                     =
    $i -> Read('xc:white');

die $result if $result;

for $image (keys %image)
{
    $file_name = $image{$image}{'file_name'};

    print "Composing: $file_name. \n";

    $result = $i -> Composite
    (
        compose => 'Over',
        image   => $image{$image}{'image'},
        x       => $image{$image}{'x'},
        y       => $image{$image}{'y'},
    );

    die $result if $result;
}

print "\n";

$result = $i -> Write($output_file_name);

die $result if $result;

print "Wrote: $output_file_name. \n";
print "Size:  $width x $height. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-composite-4.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-composite-4.pl.
#
# Purpose:
#   Demonstrate transparency with composition.
#   Read the comments starting at lines 90 and
#   112 to confuse youself :-(.
#
# V 1.00 9-May-2002
# -----------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None
#
# Output files:
#   im-hax-composite-4-background.(gif|png)
#   im-hax-composite-4-foreground.(gif|png)
#   im-hax-composite-4-composite.(gif|png)
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

sub do_it
{
    my($prefix, $suffix) = @_;

    # Fabricate a red image with a cyan border.

    my($background_image)   =
        Image::Magick -> new(size => '100x100');
    my($result)             =
        $background_image -> Read('xc:red');
    die $result if $result;

    $result = $background_image -> Border
    (
        width   => 2,
        height  => 2,
        fill    => 'cyan',
    );
    die $result if $result;

    $result = $background_image ->
        Write("$prefix-background.$suffix");
    die $result if $result;

    print
        "Wrote: $prefix-background.$suffix. \n";

    # Makee a yellow image with a cyan border.

    my($foreground_image)   =
        Image::Magick -> new(size => '50x50');
    $result                 =
        $foreground_image -> Read('xc:yellow');
    die $result if $result;

    $result = $foreground_image -> Border
    (
        width   => 2,
        height  => 2,
        fill    => 'cyan',
    );
    die $result if $result;

    # Draw a maroon square and a blue triangle
    # on the yellow square.

    $result = $foreground_image -> Draw
    (
        primitive   => 'polygon',
        stroke      => 'maroon',
        fill        => 'none',
        strokewidth => 3,
        points      => "10,10 40,10 40,40 10,40",
    );
    die $result if $result;

    $result = $foreground_image -> Draw
    (
        primitive   => 'path',
        stroke      => 'blue',
        fill        => 'none',
        strokewidth => 3,
        points      => "M10,10 L40,25 L10,40 z",
    );
    die $result if $result;

    # The next line makes the blue triangle
    # transparent for GIFs but not for PNGs.
    # When viewing the foreground image, the
    # blue triangle is white.
    #   This is correct (for GIFs) since there
    # is no other background color.
    # When viewing the composite image, the blue
    # triangle is red.
    #   This is correct (for GIFs) since there
    # is a red background.
    # Note: Comment out line 98 or comment out
    # line 119.

    #$result = $foreground_image ->
    #   Transparent(color => 'blue');
    die $result if $result;

    $result = $foreground_image ->
        Write("$prefix-foreground.$suffix");
    die $result if $result;

    print
        "Wrote: $prefix-foreground.$suffix. \n";

    # Compose the yellow image on the red image.

    $result = $background_image -> Composite
    (
        compose => 'over',
        image   => $foreground_image,
        x       => 25,
        y       => 25,
    );
    die $result if $result;

    # If you comment out line 98, and activate
    # the next line, then the next line works
    # for GIFs but not for PNGs.
    # When viewing the foreground image, the
    # blue triangle is blue.
    #   This is correct (for GIFs) since there
    # is no transparent color.
    # When viewing the composite image, the blue
    # triangle is white.
    #   This is correct (for GIFs) since there is
    # no other background color.
    # Note: Comment out line 98 or comment out
    # line 119.

    $result = $background_image ->
        Transparent(color => 'blue');
    die $result if $result;

    $result = $background_image ->
        Write("$prefix-composite.$suffix");
    die $result if $result;

    print "Wrote: $prefix-composite.$suffix. \n";

}   # End of do_it.

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

my($prefix) = 'im-hax-composite-4';

for my $suffix ('gif', 'png')
{
    do_it($prefix, $suffix);
}
\end{verbatim}
\filbreak \subsubsection{im-hax-conjure-1.msl}
.\newline{}
\begin{verbatim}

<?xml version="1.0" encoding="UTF-8"?>

<!-- Usage: shell>conjure im-hax-conjure-1.msl -->

<image size = '200x200' background = "white" >
    <read filename="label:Hello World" />
    <trim />
    <frame geometry = '4x4' fill = 'cyan' />
    <get width="base-width" height="base-height" />
    <print output="Name: im-hax-conjure-1.png. Size: %[base-width]x%[base-height].\n" />
    <write filename="im-hax-conjure-1.png" />
</image>
\end{verbatim}
\filbreak \subsubsection{im-hax-get-set-pixel-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-get-set-pixel-1.pl.
#
# Purpose:
#   Demonstrate GetPixel and SetPixel methods.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None
#
# Output files:
#   None
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# --------------------------------------------

my($im_version) = $Image::Magick::VERSION;

print "Image::Magick V $im_version. \n";
print "\n";

my($image)  =
    Image::Magick -> new(size => '10x10');
my($result) = $image -> Read('xc:white');
die $result if $result;

$result = $image -> Draw
(
    primitive   => 'path',
    stroke      => 'red',
    points      => 'M0,0 L9,9',
);
die $result if $result;

my(@pixel)  =  # Should be red.
    split(/,/, $image -> Get('Pixel[0,0]') );
my(@color)  = ('Red', 'Green', 'Blue', 'Alpha');

print 'Pixel [0,0]: ',
    (map{"$color[$_]: $pixel[$_]. "} 0 .. 3),
    "Red = 65535, 0, 0. \n";
@pixel  = # Should be white.
    split(/,/, $image -> Get('Pixel[9,0]') );
print 'Pixel [9,0]: ',
    (map{"$color[$_]: $pixel[$_]. "} 0 .. 3),
    "White = 65535, 65535, 65535. \n";
\end{verbatim}
\filbreak \subsubsection{im-hax-make-label-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-make-label-1.pl.
#
# Purpose:
#   Demonstrate how to make an image which
#   contains a string of text.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None.
#
# Output files:
#   im-hax-make-label-1.png
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

my($file_name)  = 'im-hax-make-label-1.png';
my($i)          = Image::Magick -> new();

my($result);

$result = $i -> Set
(
    font        => 'Papyrus',
    pointsize   => 40,
);
die $result if $result;

$result = $i -> Read('label:Hello World');
die $result if $result;

$result = $i -> Border
(
    geometry    => '1x1',
    fill        => 'red',
);
die $result if $result;

my($w, $h)  = $i -> Get('width', 'height');
print "test.png: $w x $h\n";

$result = $i -> Write($file_name);
die $result if $result;
print "Wrote: $file_name\n";
\end{verbatim}
\filbreak \subsubsection{im-hax-menu.cgi}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl -T
#
# Name:
#   im-hax-menu.cgi.
#
# Purpose:
#   Provide a simple menu system for the hax.
#
# V 1.01 20-Feb-2002
# ------------------
# o Restrict menu to *.cgi files
#
# V 1.00 17-Jan-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   None.
#
# Output files:
#   None.
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use CGI qw/nobr/;
use CGI::Carp qw/fatalsToBrowser/;

# -----------------------------------------------

my($inner_table_color)  = '#80c0ff';
my($outer_table_color)  = '#80ffff';

# -----------------------------------------------

delete @ENV{'BASH_ENV', 'CDPATH', 'ENV', 'IFS',
    'PATH', 'SHELL'}; # For security.

my($q)          = CGI -> new();
my($html)       = [];
my($title)      = 'ImageMagick Hax Menu';
my($dir_name)   = '/apache/cgi-bin/ImageMagick';

opendir(INX, $dir_name) ||
    die("Can't opendir($dir_name): $!");
my(@menu) = sort grep
                {
                    /^im-hax.+\.cgi$/ &&
                    ! /im-hax-menu/
                } readdir(INX);
closedir(INX);

# Put an even # of items into @menu.

if ( ($#menu % 2) == 0)
{
    my($script) = $q -> url();
    $script     =~ s/.+\///;

    push(@menu, $script);
}

print   $q -> header(),
        $q -> start_html({title => $title}),
        $q -> table
        (
            {
                align   => 'center',
                bgColor => $inner_table_color,
            },
            $q -> caption($title),
            $q -> Tr
            ([
                map
                {
                    my($i) = 2 * $_;

                    $q -> td
                    (
                        $q -> a
                        ({
                            href    => $menu[$i],
                        }, $menu[$i])
                    ) .
                    $q -> td
                    (
                        $q -> a
                        ({
                            href => $menu[$i+1],
                        }, $menu[$i + 1])
                    ),
                    $q -> td('&nbsp;')
                } 0 .. int($#menu / 2)
            ])
        ),
        $q -> end_html();
\end{verbatim}
\filbreak \subsubsection{im-hax-pdf-2-png-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-pdf-2-png-1.pl.
#
# Purpose:
#   Demonstrate conversion from PDF to PNG.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   jasper.pdf (ImageMagick/jp2/doc/jasper.pdf).
#   Any small(!) PDF file will do.
#
# Output files:
#   im-hax-pdf-2-png-1-0.png ..
#   im-hax-pdf-2-png-1-20.png,
#   if input is jasper.png
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

# /ImageMagick/jp2/doc/jasper.pdf
my($input_file_name)    = 'jasper.pdf';
my($output_file_type)   = 'png';
my($output_file_prefix) =
    "im-hax-pdf-2-$output_file_type-1";
my($image)              = Image::Magick -> new();
my($result)             =
    $image -> Read($input_file_name);
die $result if $result;

print "Input file: $input_file_name contains: ",
    ($#{$image} + 1), " pages (21 expected). \n";
print "Writing each page to a separate ",
    "$output_file_type image: \n";

my($output_file_name);

for my $page (0 .. $#{$image})
{
    $output_file_name   =
"$output_file_prefix-$page.$output_file_type";
    $result             =
    $$image[$page] -> Write($output_file_name);
    die $result if $result;

    print "Wrote: $output_file_name. \n";
}
\end{verbatim}
\filbreak \subsubsection{im-hax-resize-1.pl}
.\newline{}
\begin{verbatim}

#!/usr/bin/perl
#
# Name:
#   im-hax-resize-1.pl.
#
# Purpose:
#   Demonstrate the Resize method.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#   OS: WinNT 4 SP 6a
#   Web server: Apache V 1.3.26
#   IM: V 5.4.7
#
# Input files:
#   smile.gif
#
# Output files:
#   96x96.png
#   144x240.png
#
# Note:
#   o tab = 4 spaces || die.
#   o Text reformatted to max 50 chars wide
#       so source can be converted to PDF
#
# Author:
#   Ron Savage <ron@savage.net.au>
#   http://savage.net.au/ImageMagick.html
#
# Licence:
#   Copyright (c) 1999-2002 Ron Savage
#
#   All Programs of mine are
#   'OSI Certified Open Source Software';
#   you can redistribute them and/or modify them
#   under the terms of the Artistic License,
#   a copy of which is available at:
#   http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)  = '/temp';
my($im_version)     = $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($$work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

my($image_name)     = 'smile.gif';
my($image)          = Image::Magick -> new();
my($result)         =
    $image -> Read($image_name);
die $result if $result;
my($x, $y)          =
    $image -> Get('width', 'height');
print "Image: $image_name. Size: $x x $y. \n";
# 0 .. $image_count = 2 images.
my($image_count)    = 1;
my(@x)              = (2 * $x, 3 * $x);
my(@y)              = (2 * $y, 5 * $y);
# This code keeps all images in memory at once.
#my(@image)         =
#   map{$image -> Clone()} 0 .. $image_count;
#@image             =
#   map{$image[$_] -> Resize
#   (
#       width   => $x[$_],
#       height  => $y[$_]
#   ) $image[$_]} 0 .. $image_count;
#my(@name)          =
#   map
#   {
#       "png:$x[$_]x$y[$_].png"
#   } 0 .. $image_count;
#
#for (0 .. $image_count)
#{
#   print "Saving image: $name[$_]. \n";
#   $result = $image[$_] -> Write($name[$_]);
#   die $result if $result;
#}

my($new_image, $new_name);

for (0 .. $image_count)
{
    $new_name   = "$x[$_]x$y[$_].png";
    $new_image  = $image -> Clone();
    $result     = $new_image -> Resize
    (
        width   => $x[$_],
        height  => $y[$_]
    );
    die $result if $result;
    print "Saving image: $new_name. \n";
    $result = $new_image -> Write($new_name);
    die $result if $result;
}
\end{verbatim}


