=head1 NAME

Imager::ImageTypes - image models for Imager

=head1 SYNOPSIS

  use Imager;

  $img = Imager->new(); #  Empty image (size is 0 by 0)
  $img->open(file=>'lena.png',type=>'png'); # Read image from file

  $img = Imager->new(xsize=>400, ysize=>300); # RGB data

  $img = Imager->new(xsize=>400, ysize=>300,  # Grayscale
                     channels=>1);            #

  $img = Imager->new(xsize=>400, ysize=>300,  # RGB with alpha
                     channels=>4);            #
					      
  $img = Imager->new(xsize=>200, ysize=>200,  
                     type=>'paletted');       # paletted image
					      
  $img = Imager->new(xsize=>200, ysize=>200,  
                     bits=>16);               # 16 bits/channel rgb
					      
  $img = Imager->new(xsize=>200, ysize=>200,  
                     bits=>'double');         # 'double' floating point
                                              #  per channel

  $img->img_set(xsize=>500, ysize=>500,       # reset the image object
                channels=>4);


  # Example getting information about an Imager object

  print "Image information:\n";
  print "Width:        ", $img->getwidth(),    "\n";
  print "Height:       ", $img->getheight(),   "\n";
  print "Channels:     ", $img->getchannels(), "\n";
  print "Bits/Channel: ", $img->bits(),        "\n";
  print "Virtual:      ", $img->virtual() ? "Yes" : "No", "\n";
  my $colorcount = $img->getcolorcount(maxcolors=>512);
        print "Actual number of colors in image: ";
  print defined($colorcount) ? $colorcount : ">512", "\n";
  print "Type:         ", $img->type(),        "\n";

  if ($img->type() eq 'direct') {
    print "Modifiable Channels: ";
    print join " ", map {
      ($img->getmask() & 1<<$_) ? $_ : ()
    } 0..$img->getchannels();
    print "\n";
  
  } else {
    # palette info
    my $count = $img->colorcount;  
    @colors = $img->getcolors();
    print "Palette size: $count\n";
    my $mx = @colors > 4 ? 4 : 0+@colors;
    print "First $mx entries:\n";
    for (@colors[0..$mx-1]) {
      my @res = $_->rgba();
      print "(", join(", ", @res[0..$img->getchannels()-1]), ")\n";
    }
  }
  
  my @tags = $img->tags();
  if (@tags) {
    print "Tags:\n";
    for(@tags) {
      print shift @$_, ": ", join " ", @$_, "\n";
    }
  } else {
    print "No tags in image\n";
  }
  
=head1 DESCRIPTION

Imager supports two basic models of image:

=over

=item *

direct color - all samples are stored for every pixel.  eg. for an
8-bit/sample RGB image, 24 bits are stored for each pixel.

=item *

paletted - an index into a table of colors is stored for each pixel.

=back

Direct color or paletted images can have 1 to 4 samples per color
stored.  Imager treats these as follows:

=over

=item *

1 sample per color - grayscale image.

=item *

2 samples per color - grayscale image with alpha channel.

=item *

3 samples per color - RGB image.

=item *

4 samples per color - RGB image with alpha channel.

=back

Direct color images can have sample sizes of 8-bits per sample,
16-bits per sample or a double precision floating point number per
sample (64-bits on many systems).

Paletted images are always 8-bits/sample.

To query an existing image about it's parameters see the C<bits()>,
C<type()>, C<getwidth()>, C<getheight()>, C<getchannels()> and
C<virtual()> methods.

The coordinate system in Imager has the origin in the upper left
corner, see L<Imager::Draw> for details.

The alpha channel when one is present is considered unassociated -
ie. the color data has not been scaled by the alpha channel.  Note
that not all code follows this (recent) rule, but will over time.

=head2 Creating Imager Objects

=over

=item new

  $img = Imager->new();
  $img->read(file=>"alligator.ppm") or die $img->errstr;

Here C<new()> creates an empty image with width and height of zero.
It's only useful for creating an Imager object to call the read()
method on later.

  %opts = (xsize=>300, ysize=>200);
  $img = Imager->new(%opts); # create direct mode RGBA image
  $img = Imager->new(%opts, channels=>4); # create direct mode RGBA image

The parameters for new are:

=over

=item *

C<xsize>, C<ysize> - Defines the width and height in pixels of the
image.  These must be positive.

If not supplied then only placeholder object is created, which can be
supplied to the C<read()> or C<img_set()> methods.

=item *

C<channels> - The number of channels for the image.  Default 3.  Valid
values are from 1 to 4.

=item *

C<bits> - The storage type for samples in the image.  Default: 8.
Valid values are:

=over

=item *

C<8> - One byte per sample.  256 discrete values.

=item *

C<16> - 16-bits per sample, 65536 discrete values.

=item *

C<double> - one C double per sample.

=back

Note: you can use any Imager function on any sample size image.

Paletted images always use 8 bits/sample.

=item *

C<type> - either C<'direct'> or C<'paletted'>.  Default: C<'direct'>.

Direct images store color values for each pixel.  

Paletted images keep a table of up to 256 colors called the palette,
each pixel is represented as an index into that table.

In most cases when working with Imager you will want to use the
C<direct> image type.

If you draw on a C<paletted> image with a color not in the image's
palette then Imager will transparently convert it to a C<direct>
image.

=item *

C<maxcolors> - the maximum number of colors in a paletted image.
Default: 256.  This must be in the range 1 through 256.

=back

In the simplest case just supply the width and height of the image:

  # 8 bit/sample, RGB image
  my $img = Imager->new(xsize => $width, ysize => $height);

or if you want an alpha channel:

  # 8 bits/sample, RGBA image
  my $img = Imager->new(xsize => $width, ysize => $height, channels=>4);

Note that it I<is> possible for image creation to fail, for example if
channels is out of range, or if the image would take too much memory.

To create paletted images, set the 'type' parameter to 'paletted':

  $img = Imager->new(xsize=>200, ysize=>200, type=>'paletted');

which creates an image with a maxiumum of 256 colors, which you can
change by supplying the C<maxcolors> parameter.

For improved color precision you can use the bits parameter to specify
16 bit per channel:

  $img = Imager->new(xsize=>200, ysize=>200,
                     channels=>3, bits=>16);

or for even more precision:

  $img = Imager->new(xsize=>200, ysize=>200,
                     channels=>3, bits=>'double');

to get an image that uses a double for each channel.

Note that as of this writing all functions should work on images with
more than 8-bits/channel, but many will only work at only
8-bit/channel precision.

If you want an empty Imager object to call the read() method on, just
call new() with no parameters:

  my $img = Imager->new;
  $img->read(file=>$filename)
    or die $img->errstr;

=item img_set

img_set destroys the image data in the object and creates a new one
with the given dimensions and channels.  For a way to convert image
data between formats see the C<convert()> method.

  $img->img_set(xsize=>500, ysize=>500, channels=>4);

This takes exactly the same parameters as the new() method.

=back

=head2 Getting Information About an Imager Object

=over

=item getwidth

  print "Image width: ", $img->getwidth(), "\n";

The C<getwidth()> method returns the width of the image.  This value
comes either from C<new()> with xsize,ysize parameters or from reading
data from a file with C<read()>.  If called on an image that has no
valid data in it like C<Imager-E<gt>new()> returns, the return value
of C<getwidth()> is undef.

=item getheight

  print "Image height: ", $img->getheight(), "\n";

Same details apply as for L<getwidth>.

=item getchannels

  print "Image has ",$img->getchannels(), " channels\n";

To get the number of channels in an image C<getchannels()> is used.


=item getcolorcount

It is possible to have Imager find the number of colors in an image by
with the C<getcolorcount()> method. It requires memory proportionally
to the number of colors in the image so it is possible to have it stop
sooner if you only need to know if there are more than a certain
number of colors in the image.  If there are more colors than asked
for the function return undef.  Examples:

  if (defined($img->getcolorcount(maxcolors=>512)) {
    print "Less than 512 colors in image\n";
  }


=item bits

The bits() method retrieves the number of bits used to represent each
channel in a pixel, 8 for a normal image, 16 for 16-bit image and
'double' for a double/channel image.

  if ($img->bits eq 8) {
    # fast but limited to 8-bits/sample
  }
  else {
    # slower but more precise
  }

=item type

The type() method returns either 'direct' for truecolor images or
'paletted' for paletted images.

  if ($img->type eq 'paletted') {
    # print the palette
    for my $color ($img->getcolors) {
      print join(",", $color->rgba), "\n";
    }
  }

=item virtual

The virtual() method returns non-zero if the image contains no actual
pixels, for example masked images.

This may also be used for non-native Imager images in the future, for
example, for an Imager object that draws on an SDL surface.

=back

=head2 Direct Type Images

Direct images store the color value directly for each pixel in the
image.

=over

=item getmask

  @rgbanames = qw( red green blue alpha );
  my $mask = $img->getmask();
  print "Modifiable channels:\n";
  for (0..$img->getchannels()-1) {
    print $rgbanames[$_],"\n" if $mask & 1<<$_;
  }

C<getmask()> is used to fetch the current channel mask.  The mask
determines what channels are currently modifiable in the image.  The
channel mask is an integer value, if the i-th lsb is set the i-th
channel is modifiable.  eg. a channel mask of 0x5 means only channels
0 and 2 are writable.

=item setmask

  $mask = $img->getmask();
  $img->setmask(mask=>8);     # modify alpha only

    ...

  $img->setmask(mask=>$mask); # restore previous mask

C<setmask()> is used to set the channel mask of the image.  See
L<getmask> for details.

=back

=head2 Palette Type Images

Paletted images keep an array of up to 256 colors, and each pixel is
stored as an index into that array.

In general you can work with paletted images in the same way as RGB
images, except that if you attempt to draw to a paletted image with a
color that is not in the image's palette, the image will be converted
to an RGB image.  This means that drawing on a paletted image with
anti-aliasing enabled will almost certainly convert the image to RGB.

Palette management takes place through C<addcolors()>, C<setcolors()>,
C<getcolors()> and C<findcolor()>:

=over

=item addcolors

You can add colors to a paletted image with the addcolors() method:

   my @colors = ( Imager::Color->new(255, 0, 0), 
                  Imager::Color->new(0, 255, 0) );
   my $index = $img->addcolors(colors=>\@colors);

The return value is the index of the first color added, or undef if
adding the colors would overflow the palette.

The only parameter is C<colors> which must be a reference to an array
of Imager::Color objects.

=item setcolors

  $img->setcolors(start=>$start, colors=>\@colors);

Once you have colors in the palette you can overwrite them with the
C<setcolors()> method:  C<setcolors()> returns true on success.

Parameters:

=over

=item *

start - the first index to be set.  Default: 0

=item *

colors - reference to an array of Imager::Color objects.

=back

=item getcolors

To retrieve existing colors from the palette use the getcolors() method:

  # get the whole palette
  my @colors = $img->getcolors();
  # get a single color
  my $color = $img->getcolors(start=>$index);
  # get a range of colors
  my @colors = $img->getcolors(start=>$index, count=>$count);

=item findcolor

To quickly find a color in the palette use findcolor():

  my $index = $img->findcolor(color=>$color);

which returns undef on failure, or the index of the color.

Parameter:

=over

=item *

color - an Imager::Color object.

=back

=item colorcount

Returns the number of colors in the image's palette:

  my $count = $img->colorcount;

=item maxcolors

Returns the maximum size of the image's palette.

  my $maxcount = $img->maxcolors;

=back

=head2 Conversion Between Image Types

Warning: if you draw on a paletted image with colors that aren't in
the palette, the image will be internally converted to a normal image.

=over

=item to_paletted

You can create a new paletted image from an existing image using the
to_paletted() method:

 $palimg = $img->to_paletted(\%opts)

where %opts contains the options specified under L<Quantization options>.

  # convert to a paletted image using the web palette
  # use the closest color to each pixel
  my $webimg = $img->to_paletted({ make_colors => 'webmap' });

  # convert to a paletted image using a fairly optimal palette
  # use an error diffusion dither to try to reduce the average error
  my $optimag = $img->to_paletted({ make_colors => 'mediancut',
                                    translate => 'errdiff' });

=item to_rgb8

You can convert a paletted image (or any image) to an 8-bit/channel
RGB image with:

  $rgbimg = $img->to_rgb8;

No parameters.

=item to_rgb16

You can convert a paletted image (or any image) to an 16-bit/channel
RGB image with:

  $rgbimg = $img->to_rgb16;

No parameters.

=item masked

Creates a masked image.  A masked image lets you create an image proxy
object that protects parts of the underlying target image.

In the discussion below there are 3 image objects involved:

=over

=item *

the masked image - the return value of the masked() method.  Any
writes to this image are written to the target image, assuming the
mask image allows it.

=item *

the mask image - the image that protects writes to the target image.
Supplied as the C<mask> parameter to the masked() method.

=item *

the target image - the image you called the masked() method on.  Any
writes to the masked image end up on this image.

=back

Parameters:

=over

=item *

mask - the mask image.  If not supplied then all pixels in the target
image are writable.  On each write to the masked image, only pixels
that have non-zero in chennel 0 of the mask image will be written to
the original image.  Default: none, if not supplied then no masking is
done, but the other parameters are still honored.

=item *

left, top - the offset of writes to the target image.  eg. if you
attempt to set pixel (x,y) in the masked image, then pixel (x+left,
y+top) will be written to in the original image.

=item *

bottom, right - the bottom right of the area in the target available
from the masked image.

=back

Masked images let you control which pixels are modified in an
underlying image.  Where the first channel is completely black in the
mask image, writes to the underlying image are ignored.

For example, given a base image called $img:

  my $mask = Imager->new(xsize=>$img->getwidth, ysize=>$img->getheight,
                         channels=>1);
  # ... draw something on the mask
  my $maskedimg = $img->masked(mask=>$mask);

  # now draw on $maskedimg and it will only draw on areas of $img 
  # where $mask is non-zero in channel 0.

You can specifiy the region of the underlying image that is masked
using the left, top, right and bottom options.

If you just want a subset of the image, without masking, just specify
the region without specifying a mask.  For example:

  # just work with a 100x100 region of $img
  my $maskedimg = $img->masked(left => 100, top=>100,
                               right=>200, bottom=>200);

=back

=head2 Tags

Image tags contain meta-data about the image, ie. information not
stored as pixels of the image.

At the perl level each tag has a name or code and a value, which is an
integer or an arbitrary string.  An image can contain more than one
tag with the same name or code, but having more than one tag with the
same name is discouraged.

You can retrieve tags from an image using the tags() method, you can
get all of the tags in an image, as a list of array references, with
the code or name of the tag followed by the value of the tag.

=over

=item tags

Retrieve tags from the image.

With no parameters, retrieves a list array references, each containing
a name and value: all tags in the image:

  # get a list of ( [ name1 => value1 ], [ name2 => value2 ] ... )
  my @alltags = $img->tags;
  print $_->[0], ":", $_->[1], "\n" for @all_tags;

  # or put it in a hash, but this will lose duplicates
  my %alltags = map @$_, $img->tags;

in scalar context this returns the number of tags:

  my $num_tags = $img->tags;

or you can get all tags values for the given name:

  my @namedtags = $img->tags(name => $name);

in scalar context this returns the first tag of that name:

  my $firstnamed = $img->tags(name => $name);

or a given code:

  my @tags = $img->tags(code=>$code);

=item addtag

You can add tags using the addtag() method, either by name:

  my $index = $img->addtag(name=>$name, value=>$value);

or by code:

  my $index = $img->addtag(code=>$code, value=>$value);

=item deltag

You can remove tags with the deltag() method, either by index:

  $img->deltag(index=>$index);

or by name:

  $img->deltag(name=>$name);

or by code:

  $img->deltag(code=>$code);

In each case deltag() returns the number of tags deleted.

=item settag

settag() replaces any existing tags with a new tag.  This is
equivalent to calling deltag() then addtag().

=back

=head2 Common Tags

Many tags are only meaningful for one format.  GIF looping information
is pretty useless for JPEG for example.  Thus, many tags are set by
only a single reader or used by a single writer.  For a complete list
of format specific tags see L<Imager::Files>.

Since tags are a relatively new addition their use is not wide spread
but eventually we hope to have all the readers for various formats set
some standard information.

=over

=item *

X<i_xres tag>X<i_yres tag>X<tags, i_xres>X<tags, i_yres>i_xres, i_yres
- The spatial resolution of the image in pixels per inch.  If the
image format uses a different scale, eg. pixels per meter, then this
value is converted.  A floating point number stored as a string.

  # our image was generated as a 300 dpi image
  $img->settag(name => 'i_xres', value => 300);
  $img->settag(name => 'i_yres', value => 300);

  # 100 pixel/cm for a TIFF image
  $img->settag(name => 'tiff_resolutionunit', value => 3); # RESUNIT_CENTIMETER
  # convert to pixels per inch, Imager will convert it back
  $img->settag(name => 'i_xres', value => 100 * 2.54);
  $img->settag(name => 'i_yres', value => 100 * 2.54);

=item *

X<i_aspect_only tag>X<tags, i_aspect_only>i_aspect_only - If this is
non-zero then the values in i_xres and i_yres are treated as a ratio
only.  If the image format does not support aspect ratios then this is
scaled so the smaller value is 72dpi.

=item *

X<i_incomplete tag>X<tags, i_incomplete>i_incomplete - If this tag is
present then the whole image could not be read.  This isn't
implemented for all images yet, and may not be.

=item *

X<i_lines_read tag>X<tags, i_lines_read>i_lines_read - If
C<i_incomplete> is set then this tag may be set to the number of
scanlines successfully read from the file.  This can be used to decide
whether an image is worth processing.

=item *

X<i_format tag>X<tags, i_format>i_format - The file format this file
was read from.

=back

=head2 Quantization options

These options can be specified when calling
L<Imager::ImageTypes/to_paletted>, write_multi() for gif files, when
writing a single image with the gifquant option set to 'gen', or for
direct calls to i_writegif_gen and i_writegif_callback.

=over

=item colors

A arrayref of colors that are fixed.  Note that some color generators
will ignore this.

=item transp

The type of transparency processing to perform for images with an
alpha channel where the output format does not have a proper alpha
channel (eg. gif).  This can be any of:

=over

=item none

No transparency processing is done. (default)

=item threshold

Pixels more transparent that tr_threshold are rendered as transparent.

=item errdiff

An error diffusion dither is done on the alpha channel.  Note that
this is independent of the translation performed on the colour
channels, so some combinations may cause undesired artifacts.

=item ordered

The ordered dither specified by tr_orddith is performed on the alpha
channel.

=back

This will only be used if the image has an alpha channel, and if there
is space in the palette for a transparency colour.

=item tr_threshold

The highest alpha value at which a pixel will be made transparent when
transp is 'threshold'. (0-255, default 127)

=item tr_errdiff

The type of error diffusion to perform on the alpha channel when
transp is 'errdiff'.  This can be any defined error diffusion type
except for custom (see errdiff below).

=item tr_orddith

The type of ordered dither to perform on the alpha channel when transp
is 'ordered'.  Possible values are:

=over

=item random

A semi-random map is used.  The map is the same each time.

=item dot8

8x8 dot dither.

=item dot4

4x4 dot dither

=item hline

horizontal line dither.

=item vline

vertical line dither.

=item "/line"

=item slashline

diagonal line dither

=item '\line'

=item backline

diagonal line dither

=item tiny

dot matrix dither (currently the default).  This is probably the best
for displays (like web pages).

=item custom

A custom dither matrix is used - see tr_map

=back

=item tr_map

When tr_orddith is custom this defines an 8 x 8 matrix of integers
representing the transparency threshold for pixels corresponding to
each position.  This should be a 64 element array where the first 8
entries correspond to the first row of the matrix.  Values should be
betweern 0 and 255.

=item make_colors

Defines how the quantization engine will build the palette(s).
Currently this is ignored if 'translate' is 'giflib', but that may
change.  Possible values are:

=over

=item *

none - only colors supplied in 'colors' are used.

=item *

webmap - the web color map is used (need url here.)

=item *

addi - The original code for generating the color map (Addi's code) is
used.

=item *

mediancut - Uses a mediancut algorithm, faster than 'addi', but not as good a
result.

=item *

mono, monochrome - a fixed black and white palette, suitable for
producing bi-level images (eg. facsimile)

=back

Other methods may be added in the future.

=item colors

A arrayref containing Imager::Color objects, which represents the
starting set of colors to use in translating the images.  webmap will
ignore this.  The final colors used are copied back into this array
(which is expanded if necessary.)

=item max_colors

The maximum number of colors to use in the image.

=item translate

The method used to translate the RGB values in the source image into
the colors selected by make_colors.  Note that make_colors is ignored
whene translate is 'giflib'.

Possible values are:

=over

=item giflib

The giflib native quantization function is used.

=item closest

The closest color available is used.

=item perturb

The pixel color is modified by perturb, and the closest color is chosen.

=item errdiff

An error diffusion dither is performed.

=back

It's possible other transate values will be added.

=item errdiff

The type of error diffusion dither to perform.  These values (except
for custom) can also be used in tr_errdif.

=over

=item floyd

Floyd-Steinberg dither

=item jarvis

Jarvis, Judice and Ninke dither

=item stucki

Stucki dither

=item custom

Custom.  If you use this you must also set errdiff_width,
errdiff_height and errdiff_map.

=back

=item errdiff_width

=item errdiff_height

=item errdiff_orig

=item errdiff_map

When translate is 'errdiff' and errdiff is 'custom' these define a
custom error diffusion map.  errdiff_width and errdiff_height define
the size of the map in the arrayref in errdiff_map.  errdiff_orig is
an integer which indicates the current pixel position in the top row
of the map.

=item perturb

When translate is 'perturb' this is the magnitude of the random bias
applied to each channel of the pixel before it is looked up in the
color table.

=back

=head1 INITIALIZATION

This documents the Imager initialization function, which you will
almost never need to call.

=over

=item init

This is a function, not a method.

This function is a mess, it can take the following named parameters:

=over

=item *

log - name of a log file to log Imager's actions to.  Not all actions
are logged, but the debugging memory allocator does log allocations
here.  Ignored if Imager has been built without logging support.

=item *

loglevel - the maximum level of message to log.  Default: 1.

=item *

warn_obsolete - if this is non-zero then Imager will warn when you
attempt to use obsoleted parameters or functionality.  This currently
only includes the old gif output options instead of tags.

=item *

t1log - if non-zero then T1lib will be configured to produce a log
file.  This will fail if there are any existing T1lib font objects.

=back

Example:

  Imager::init(log => 'trace.log', loglevel => 9);

=back

=head1 REVISION

$Revision: 1208 $

=head1 AUTHORS

Tony Cook, Arnar M. Hrafnkelsson

=head1 SEE ALSO

Imager(3), Imager::Files(3), Imager::Draw(3),
Imager::Color(3), Imager::Fill(3), Imager::Font(3),
Imager::Transformations(3), Imager::Engines(3), Imager::Filters(3),
Imager::Expr(3), Imager::Matrix2d(3), Imager::Fountain(3)

=cut
