Imager

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


Imager 1.014 - 28 Apr 2022
============

Bug fixes:

 - fix failed to build on non-threaded perls.
   https://github.com/tonycoz/imager/issues/472

 - when writing a paletted image with an alpha channel to PNG only
   set the tRNS chunk if the image has transparent colours.  With
   older libpng this could add an erroneous tRNS chunk (or possibly
   undefined behaviour if I understand the libpng code).
   https://github.com/tonycoz/imager/issues/459

Features:

 - Imager::Color and Imager::Color::Float objects can now be created
   from CSS style rgb(...) strings.
   https://github.com/tonycoz/imager/issues/463

Changes.old  view on Meta::CPAN

        - Win32 support for libpng
        - fixed set_internal creating 2 refs to one object
        - Win32 support for jpeg
        - Win32 support for tiff
        - base level error reporting code added, I still need to modify
          older code to use it
        - fix translate=>'giflib' handling of single-colour images.  
          Previously a single colour image would cause an error in 
          MakeMapObject().
        - fix t/t10formats.t tests against giflib3
        - added test for transparent gifs
        - minor doc fixes for transparent gifs
        - make it less error-prone to write transparent gifs
        - documented the options for reading raw images
        - Changes switched to use spaces for formatting
        - switch gif.c to new error reporting with related changes to
          Imager.xs, Imager.pm
        - each of the image formats now have their own test file,
          extracted from t10formats.t, usually with som extra tests
        - Added flip() and docs to Imager.pm and i_flipxy to image.c.
          Added testcases too.
        - Fixed an overflow bug in png.c
        - added the colors parameter to Imager::read() which receives 

FT2/t/t10ft2.t  view on Meta::CPAN


    # UTF8 encoded \x{2010}
    my $dash = pack("C*", 0xE2, 0x80, 0x90);
    diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
		       font => $font, color => '#FFFFFF', utf8 => 1);
    diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
		       font => $font, channel => 1, utf8 => 1);
  }

  { # RT 11972
    # when rendering to a transparent image the coverage should be
    # expressed in terms of the alpha channel rather than the color
    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
    my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
    ok($im->string(string => "AB", size => 20, aa => 1, color => '#F00',
		   x => 0, y => 15, font => $font),
       "draw to transparent image");
    my $im_noalpha = $im->convert(preset => 'noalpha');
    my $im_pal = $im->to_paletted(make_colors => 'mediancut');
    my @colors = $im_pal->getcolors;
    is(@colors, 2, "should be only 2 colors");
    @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
    is_color3($colors[0], 0, 0, 0, "check we got black");
    is_color3($colors[1], 255, 0, 0, "and red");
  }

  { # RT 27546

GIF/imgif.c  view on Meta::CPAN

=item gif_background

The index in the global colormap of the logical screen's background
color.  This is only set if the current image uses the global
colormap.

=item gif_trans_index

The index of the color in the colormap used for transparency.  If the
image has a transparency then it is returned as a 4 channel image with
the alpha set to zero in this palette entry. ("Transparent Color Index")

=item gif_delay

The delay until the next frame is displayed, in 1/100 of a second. 
("Delay Time").

=item gif_user_input

whether or not a user input is expected before continuing (view dependent) 
("User Input Flag").

ICO/t/t10icon.t  view on Meta::CPAN

  ok($im->write(data => \$data,
		type => "ico",
		ico_mask => $mask), "write with dodgy mask");
  my $im2 = Imager->new(data => \$data, filetype => "ico");
  ok($im2, "read it back");
  is_image($im2, $im, "should match original, despite bad mask");
  my $im3 = Imager->new(data => \$data, filetype => "ico", ico_alpha_masked => 1);
  ok($im3, "read it back with ico_alpha_masked => 1");
  my $cmp = $im->copy;
  $cmp->setpixel(x => 0, y => 0, color => [ 255, 0, 0, 0 ]);
  isnt_image($im3, $cmp, "bad mask makes some pixels transparent");
}

Imager.pm  view on Meta::CPAN

L<Imager::Font::Wrap>

text, wrapping text in an area - L<Imager::Font::Wrap>

text, measuring - L<Imager::Font/bounding_box()>, L<Imager::Font::BBox>

threads - L<Imager::Threads>

tiles, color - L<< Imager::Filters/C<mosaic> >>

transparent images - L<Imager::ImageTypes>,
L<Imager::Cookbook/"Transparent PNG">

=for stopwords unsharp

unsharp mask - L<< Imager::Filters/C<unsharpmask> >>

watermark - L<< Imager::Filters/C<watermark> >>

writing an image to a file - L<Imager::Files>

=head1 SUPPORT

PNG/t/10png.t  view on Meta::CPAN

binmode FH;
$IO = Imager::io_new_fd(fileno(FH));
ok(Imager::File::PNG::i_writepng_wiol($timg, $IO), "write tranparent");
close FH;

open FH,"testout/t102_trans.png" 
  or die "cannot open testout/t102_trans.png\n";
binmode(FH);
$IO = Imager::io_new_fd(fileno(FH));
$cmpimg = Imager::File::PNG::i_readpng_wiol($IO);
ok($cmpimg, "read transparent");
close(FH);

print "# png average mean square pixel difference: ",sqrt(i_img_diff($timg,$cmpimg))/150*150,"\n";
is(i_img_diff($timg, $cmpimg), 0, "compare saved and original transparent");

# REGRESSION TEST
# png.c 1.1 would produce an incorrect image when loading images with
# less than 8 bits/pixel with a transparent palette entry
open FH, "< testimg/palette.png"
  or die "cannot open testimg/palette.png: $!\n";
binmode FH;
$IO = Imager::io_new_fd(fileno(FH));
# 1.1 may segfault here (it does with libefence)
my $pimg = Imager::File::PNG::i_readpng_wiol($IO);
ok($pimg, "read transparent paletted image");
close FH;

open FH, "< testimg/palette_out.png"
  or die "cannot open testimg/palette_out.png: $!\n";
binmode FH;
$IO = Imager::io_new_fd(fileno(FH));
my $poimg = Imager::File::PNG::i_readpng_wiol($IO);
ok($poimg, "read palette_out image");
close FH;
if (!is(i_img_diff($pimg, $poimg), 0, "images the same")) {

T1/t/t10type1.t  view on Meta::CPAN

    # UTF8 encoded \xBF
    my $pound = pack("C*", 0xC2, 0xBF);
    diff_text_with_nul("utf8 pound\0pound vs pound", "$pound\0$pound", $pound,
		       font => $font, color => '#FFFFFF', utf8 => 1);
    diff_text_with_nul("utf8 dash\0dash vs dash", "$pound\0$pound", $pound,
		       font => $font, channel => 1, utf8 => 1);

  }

  { # RT 11972
    # when rendering to a transparent image the coverage should be
    # expressed in terms of the alpha channel rather than the color
    my $font = Imager::Font->new(file=>'fontfiles/dcr10.pfb', type=>'t1');
    my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
    ok($im->string(string => "AB", size => 20, aa => 2, color => '#F00',
		   x => 0, y => 15, font => $font),
       "draw to transparent image");
    my $im_noalpha = $im->convert(preset => 'noalpha');
    my $im_pal = $im->to_paletted(make_colors => 'mediancut');
    my @colors = $im_pal->getcolors;
    is(@colors, 2, "should be only 2 colors");
    @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
    is_color3($colors[0], 0, 0, 0, "check we got black");
    is_color3($colors[1], 255, 0, 0, "and red");
  }

 SKIP:

filters.im  view on Meta::CPAN


    myfree(blur);
    myfree(out);
  }
  i_img_destroy(copy);
}

/*
=item i_diff_image(im1, im2, mindist)

Creates a new image that is transparent, except where the pixel in im2
is different from im1, where it is the pixel from im2.

The samples must differ by at least mindiff to be considered different.

=cut
*/

i_img *
i_diff_image(i_img *im1, i_img *im2, double mindist) {
  i_img *out;

imager.h  view on Meta::CPAN

void i_tt_dump_names(TT_Fonthandle *handle);
size_t i_tt_face_name(TT_Fonthandle *handle, char *name_buf, 
                   size_t name_buf_size);
size_t i_tt_glyph_name(TT_Fonthandle *handle, unsigned long ch, char *name_buf,
                    size_t name_buf_size);

#endif  /* End of freetype headers */

extern void i_quant_makemap(i_quantize *quant, i_img **imgs, int count);
extern i_palidx *i_quant_translate(i_quantize *quant, i_img *img);
extern void i_quant_transparent(i_quantize *quant, i_palidx *indices, i_img *img, i_palidx trans_index);

i_img *im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int ch, int maxpal);

extern i_img *i_img_to_pal(i_img *src, i_quantize *quant);
extern i_img *i_img_to_rgb(i_img *src);
extern i_img *i_img_masked_new(i_img *targ, i_img *mask, i_img_dim x, i_img_dim y, 
                               i_img_dim w, i_img_dim h);
extern i_img *im_img_16_new(pIMCTX, i_img_dim x, i_img_dim y, int ch);
extern i_img *i_img_to_rgb16(i_img *im);
extern i_img *im_img_double_new(pIMCTX, i_img_dim x, i_img_dim y, int ch);

imdatatypes.h  view on Meta::CPAN


C<tr_none> - ignore the alpha channel

=item *

C<tr_threshold> - simple transparency thresholding.

=item *

C<tr_errdiff> - use error diffusion to control which pixels are
transparent.

=item *

C<tr_ordered> - use ordered dithering to control which pixels are
transparent.

=back

=cut
*/

/* transparency handling for quantized output */
typedef enum i_transp_tag {
  tr_none, /* ignore any alpha channel */
  tr_threshold, /* threshold the transparency - uses tr_threshold */

imext.h  view on Meta::CPAN

#define i_new_fill_image(im, matrix, xoff, yoff, combine) \
  ((im_extt->f_i_new_fill_image)((im), (matrix), (xoff), (yoff), (combine)))
#define i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, count, segs) \
  ((im_extt->f_i_new_fill_fount)((xa), (ya), (xb), (yb), (type), (repeat), (combine), (super_sample), (ssample_param), (count), (segs)))
#define i_fill_destroy(fill) ((im_extt->f_i_fill_destroy)(fill))

#define i_quant_makemap(quant, imgs, count) \
  ((im_extt->f_i_quant_makemap)((quant), (imgs), (count)))
#define i_quant_translate(quant, img) \
  ((im_extt->f_i_quant_translate)((quant), (img)))
#define i_quant_transparent(quant, indices, img, trans_index) \
  ((im_extt->f_i_quant_transparent)((quant), (indices), (img), (trans_index)))

#define im_clear_error(ctx) ((im_extt->f_im_clear_error)(ctx))
#define im_push_error(ctx, code, msg) ((im_extt->f_im_push_error)((ctx), (code), (msg)))
#define i_push_errorf (im_extt->f_i_push_errorf)
#define im_push_errorvf(ctx, code, fmt, list)		\
  ((im_extt->f_im_push_errorvf)((ctx), (code), (fmt), (list)))

#define i_tags_new(tags) ((im_extt->f_i_tags_new)(tags))
#define i_tags_set(tags, name, data, size) \
  ((im_extt->f_i_tags_set)((tags), (name), (data), (size)))

imexttypes.h  view on Meta::CPAN

                                i_img_dim yoff, int combine);
  i_fill_t *(*f_i_new_fill_fount)(double xa, double ya, double xb, double yb, 
                 i_fountain_type type, i_fountain_repeat repeat, 
                 int combine, int super_sample, double ssample_param, 
                 int count, i_fountain_seg *segs);  

  void (*f_i_fill_destroy)(i_fill_t *fill);

  void (*f_i_quant_makemap)(i_quantize *quant, i_img **imgs, int count);
  i_palidx * (*f_i_quant_translate)(i_quantize *quant, i_img *img);
  void (*f_i_quant_transparent)(i_quantize *quant, i_palidx *indices, 
                                i_img *img, i_palidx trans_index);

  void (*f_i_clear_error)(void); /* SKIP */
  void (*f_i_push_error)(int code, char const *msg); /* SKIP */
  void (*f_i_push_errorf)(int code, char const *fmt, ...) I_FORMAT_ATTR(2,3);
  void (*f_i_push_errorvf)(int code, char const *fmt, va_list); /* SKIP */
  
  void (*f_i_tags_new)(i_img_tags *tags);
  int (*f_i_tags_set)(i_img_tags *tags, char const *name, char const *data, 
                      int size);

lib/Imager/APIRef.pod  view on Meta::CPAN



=for comment
From: File paste.im

=item i_copyto_trans(C<im>, C<src>, C<x1>, C<y1>, C<x2>, C<y2>, C<tx>, C<ty>, C<trans>)


(C<x1>,C<y1>) (C<x2>,C<y2>) specifies the region to copy (in the
source coordinates) (C<tx>,C<ty>) specifies the upper left corner for
the target image.  pass NULL in C<trans> for non transparent i_colors.


=for comment
From: File image.c

=item i_img_info(im, info)


Return image information

lib/Imager/Color/Float.pm  view on Meta::CPAN

for 3 or 4 channel images the color components are red, green, blue,
alpha.

=item *

for 1 or 2 channel images the color components are gray, alpha, with
the other two components ignored.

=back

An alpha value of zero is fully transparent, an alpha value of 1.0 is
fully opaque.

=head1 METHODS

=over 4

=item new

This creates a color object to pass to functions that need a color argument.

lib/Imager/Cookbook.pod  view on Meta::CPAN

say TIFF to JPEG, you'll need multiple output files:

  my @images = Imager->read_multi(file => 'input.tif')
    or die Imager->errstr;
  my $index = 1;
  for my $image (@images) {
    $image->write(file => sprintf('output%02d.jpg', $index++))
      or die $image->errstr;
  }

=head2 Transparent PNG

To save to a transparent PNG (or GIF or TIFF) you need to start with
an image with transparency.

To make a transparent image, create an image object with 2 or 4
channels:

  # RGB with alpha channel
  my $rgba = Imager->new(xsize => $width, ysize => $height, channels => 4);

  # Gray with alpha channel
  my $graya = Imager->new(xsize => $width, ysize => $height, channels => 2);

By default, the created image will be transparent.

Otherwise, if you have an existing image file with transparency,
simply read it, and the transparency will be preserved.

=head1 IMAGE SYNTHESIS

=head2 Creating an image

To create a simple RGB image, supply the image width and height to the
new() method:

lib/Imager/Engines.pod  view on Meta::CPAN

with an overloaded multiplication operator.

WARNING: the matrix you provide in the matrix operator transforms the
co-ordinates within the B<destination> image to the co-ordinates
within the I<source> image.  This can be confusing.

You can also supply a C<back> argument which acts as a background
color for the areas of the image with no samples available (outside
the rectangle of the source image.)  This can be either an
Imager::Color or Imager::Color::Float object.  This is B<not> mixed
transparent pixels in the middle of the source image, it is B<only>
used for pixels where there is no corresponding pixel in the source
image.

=back

=head1 AUTHOR

Tony Cook <tonyc@cpan.org>, Arnar M. Hrafnkelsson

=cut

lib/Imager/Files.pod  view on Meta::CPAN

screen's background color.  This is only set if the current image uses
the global color map.  You can set this on write too, but for it to
choose the color you want, you will need to supply only paletted
images and set the C<gif_eliminate_unused> tag to 0.

=item *

gif_trans_index - The index of the color in the color map used for
transparency.  If the image has a transparency then it is returned as
a 4 channel image with the alpha set to zero in this palette entry.
This value is not used when writing. ("Transparent Color Index")

=item *

gif_trans_color - A reference to an Imager::Color object, which is the
color to use for the palette entry used to represent transparency in
the palette.  You need to set the C<transp> option (see
L<Imager::ImageTypes/"Quantization options">) for this value to be
used.

=item *

lib/Imager/ImageTypes.pod  view on Meta::CPAN


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.

=item *

C<file>, C<fh>, C<fd>, C<callback>, C<readcb>, or C<io> - specify a

t/200-file/310-pnm.t  view on Meta::CPAN

  $im->box(filled => 1, xmin => 8, color => '#FFE0C0');
  $im->box(filled => 1, color => NC(0, 192, 192, 128),
	   ymin => 8, xmax => 7);
  push @files, "t104_alpha.ppm";
  ok($im->write(file=>"testout/t104_alpha.ppm", type=>'pnm'),
     "should succeed writing 4 channel image");
  my $imread = Imager->new;
  ok($imread->read(file => 'testout/t104_alpha.ppm'), "read it back")
    or print "# ", $imread->errstr, "\n";
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 0, 0, 0, 
	    "check transparent became black");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 0, 96, 96,
	    "check translucent came through");
  my $data;
  ok($im->write(data => \$data, type => 'pnm', i_background => '#FF0000'),
     "write with red background");
  ok($imread->read(data => $data, type => 'pnm'),
     "read it back");
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 255, 0, 0, 
	    "check transparent became red");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 127, 96, 96,
	    "check translucent came through");
}

{
  # more RT #30074 - 16 bit images
  my $im = Imager->new(xsize=>16, ysize=>16, channels=>4, bits => 16);
  $im->box(filled => 1, xmin => 8, color => '#FFE0C0');
  $im->box(filled => 1, color => NC(0, 192, 192, 128),
	   ymin => 8, xmax => 7);
  push @files, "t104_alp16.ppm";
  ok($im->write(file=>"testout/t104_alp16.ppm", type=>'pnm', 
		pnm_write_wide_data => 1),
     "should succeed writing 4 channel image");
  my $imread = Imager->new;
  ok($imread->read(file => 'testout/t104_alp16.ppm'), "read it back");
  is($imread->bits, 16, "check we did produce a 16 bit image");
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 0, 0, 0, 
	    "check transparent became black");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 0, 96, 96,
	    "check translucent came through");
  my $data;
  ok($im->write(data => \$data, type => 'pnm', i_background => '#FF0000',
		pnm_write_wide_data => 1),
     "write with red background");
  ok($imread->read(data => $data, type => 'pnm'),
     "read it back");
  is($imread->bits, 16, "check it's 16-bit");
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 255, 0, 0, 
	    "check transparent became red");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 127, 96, 96,
	    "check translucent came through");
}

# various bad input files
print "# check error handling\n";
{
  my $im = Imager->new;

t/200-file/320-bmp.t  view on Meta::CPAN

  my $im = Imager->new(xsize=>16, ysize=>16, channels=>4);
  $im->box(filled => 1, xmin => 8, color => '#FFE0C0');
  $im->box(filled => 1, color => NC(0, 192, 192, 128),
	   ymin => 8, xmax => 7);
  ok($im->write(file=>"testout/t107_alpha.bmp", type=>'bmp'),
     "should succeed writing 4 channel image");
  push @files, "t107_alpha.bmp";
  my $imread = Imager->new;
  ok($imread->read(file => 'testout/t107_alpha.bmp'), "read it back");
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 0, 0, 0, 
	    "check transparent became black");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 0, 96, 96,
	    "check translucent came through");
  my $data;
  ok($im->write(data => \$data, type => 'bmp', i_background => '#FF0000'),
     "write with red background");
  ok($imread->read(data => $data, type => 'bmp'),
     "read it back");
  is_color3($imread->getpixel('x' => 0, 'y' => 0), 255, 0, 0, 
	    "check transparent became red");
  is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
	    "check color came through");
  is_color3($imread->getpixel('x' => 0, 'y' => 15), 127, 96, 96,
	    "check translucent came through");
}

{ # RT 41406
  my $data;
  my $im = test_image();
  ok($im->write(data => \$data, type => 'bmp'), "write using OO");

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 3.122 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-f5108d614456 )