3 Imager::Filters - Entire Image Filtering Operations
11 $img->filter(type=>'autolevels');
12 $img->filter(type=>'autolevels', lsat=>0.2);
13 $img->filter(type=>'turbnoise')
17 load_plugin("dynfilt/dyntest.so")
18 or die "unable to load plugin\n";
20 $img->filter(type=>'lin_stretch', a=>35, b=>200);
22 unload_plugin("dynfilt/dyntest.so")
23 or die "unable to load plugin\n";
25 $out = $img->difference(other=>$other_img);
29 Filters are operations that have similar calling interface.
41 type - the type of filter, see L</Types of Filters>.
45 many other possible parameters, see L</Types of Filters> below.
49 Returns the invocant (C<$self>) on success, returns a false value on
50 failure. You can call C<< $self->errstr >> to determine the cause of
53 $self->filter(type => $type, ...)
58 =head2 Types of Filters
60 Here is a list of the filters that are always available in Imager.
61 This list can be obtained by running the C<filterlist.perl> script
62 that comes with the module source.
64 Filter Arguments Default value
68 autolevels_skew lsat 0.1
72 bumpmap bump lightx lighty
130 unsharpmask stddev 2.0
138 All parameters must have some value but if a parameter has a default
139 value it may be omitted when calling the filter function.
141 Every one of these filters modifies the image in place.
143 If none of the filters here do what you need, the
144 L<Imager::Engines/transform()> or L<Imager::Engines/transform2()>
145 function may be useful.
148 autolevels bumpmap bumpmap_complex conv gaussian hardinvert hardinvertall
149 radnoise turbnoise unsharpmask gradgen postlevels
151 A reference of the filters follows:
157 Scales the luminance of the image so that the luminance will cover
158 the possible range for the image. C<lsat> and C<usat> truncate the
159 range by the specified fraction at the top and bottom of the range
162 # increase contrast, losing little detail
163 $img->filter(type=>"autolevels")
166 The method used here is typically called L<Histogram
167 Equalization|http://en.wikipedia.org/wiki/Histogram_equalization>.
169 =item C<autolevels_skew>
171 Scales the value of each channel so that the values in the image will
172 cover the whole possible range for the channel. C<lsat> and C<usat>
173 truncate the range by the specified fraction at the top and bottom of
174 the range respectively.
176 # increase contrast per channel, losing little detail
177 $img->filter(type=>"autolevels_skew")
180 # increase contrast, losing 20% of highlight at top and bottom range
181 $img->filter(type=>"autolevels", lsat=>0.2, usat=>0.2)
184 This filter was the original C<autolevels> filter, but it's typically
185 useless due to the significant color skew it can produce.
189 uses the channel C<elevation> image C<bump> as a bump map on your
190 image, with the light at (C<lightx>, C<lightty>), with a shadow length
193 $img->filter(type=>"bumpmap", bump=>$bumpmap_img,
194 lightx=>10, lighty=>10, st=>5)
197 =item C<bumpmap_complex>
199 uses the channel C<channel> image C<bump> as a bump map on your image.
200 If C<< Lz < 0 >> the three L parameters are considered to be the
201 direction of the light. If C<< Lz > 0 >> the L parameters are
202 considered to be the light position. C<Ia> is the ambient color,
203 C<Il> is the light color, C<Is> is the color of specular highlights.
204 C<cd> is the diffuse coefficient and C<cs> is the specular
205 coefficient. C<n> is the shininess of the surface.
207 $img->filter(type=>"bumpmap_complex", bump=>$bumpmap_img)
212 scales each channel by C<intensity>. Values of C<intensity> < 1.0
213 will reduce the contrast.
216 $img->filter(type=>"contrast", intensity=>1.3)
220 $img->filter(type=>"contrast", intensity=>0.8)
225 performs 2 1-dimensional convolutions on the image using the values
226 from C<coef>. C<coef> should be have an odd length and the sum of the
227 coefficients must be non-zero.
230 $img->filter(type=>"conv", coef=>[-0.5, 2, -0.5 ])
234 $img->filter(type=>"conv", coef=>[ 1, 2, 1 ])
238 $img->filter(type=>"conv", coef=>[ -0.5, 1, -0.5 ])
243 renders a fountain fill, similar to the gradient tool in most paint
244 software. The default fill is a linear fill from opaque black to
245 opaque white. The points C<A(Cxa, ya)> and C<B(xb, yb)> control the
246 way the fill is performed, depending on the C<ftype> parameter:
248 =for stopwords ramping
254 the fill ramps from A through to B.
258 the fill ramps in both directions from A, where AB defines the length
263 A is the center of a circle, and B is a point on it's circumference.
264 The fill ramps from the center out to the circumference.
266 =item C<radial_square>
268 A is the center of a square and B is the center of one of it's sides.
269 This can be used to rotate the square. The fill ramps out to the
274 A is the center of a circle and B is a point on its circumference. B
275 marks the 0 and 360 point on the circle, with the fill ramping
280 A is the center of a circle and B is a point on it's circumference. B
281 marks the 0 and point on the circle, with the fill ramping in both
282 directions to meet opposite.
286 The C<repeat> option controls how the fill is repeated for some
287 C<ftype>s after it leaves the AB range:
293 no repeats, points outside of each range are treated as if they were
294 on the extreme end of that range.
298 the fill simply repeats in the positive direction
302 the fill repeats in reverse and then forward and so on, in the
307 the fill repeats in both the positive and negative directions (only
308 meaningful for a linear fill).
312 as for triangle, but in the negative direction too (only meaningful
317 By default the fill simply overwrites the whole image (unless you have
318 parts of the range 0 through 1 that aren't covered by a segment), if
319 any segments of your fill have any transparency, you can set the
320 I<combine> option to 'normal' to have the fill combined with the
321 existing pixels. See the description of I<combine> in L<Imager::Fill>.
323 If your fill has sharp edges, for example between steps if you use
324 repeat set to 'triangle', you may see some aliased or ragged edges.
325 You can enable super-sampling which will take extra samples within the
326 pixel in an attempt anti-alias the fill.
328 The possible values for the super_sample option are:
334 no super-sampling is done
338 a square grid of points are sampled. The number of points sampled is
339 the square of ceil(0.5 + sqrt(ssample_param)).
343 a random set of points within the pixel are sampled. This looks
344 pretty bad for low ssample_param values.
348 the points on the radius of a circle within the pixel are sampled.
349 This seems to produce the best results, but is fairly slow (for now).
353 You can control the level of sampling by setting the ssample_param
354 option. This is roughly the number of points sampled, but depends on
355 the type of sampling.
357 The segments option is an arrayref of segments. You really should use
358 the L<Imager::Fountain> class to build your fountain fill. Each
359 segment is an array ref containing:
365 a floating point number between 0 and 1, the start of the range of
366 fill parameters covered by this segment.
370 a floating point number between start and end which can be used to
371 push the color range towards one end of the segment.
375 a floating point number between 0 and 1, the end of the range of fill
376 parameters covered by this segment. This should be greater than
383 The colors at each end of the segment. These can be either
384 Imager::Color or Imager::Color::Float objects.
388 The type of segment, this controls the way the fill parameter varies
389 over the segment. 0 for linear, 1 for curved (unimplemented), 2 for
390 sine, 3 for sphere increasing, 4 for sphere decreasing.
394 The way the color varies within the segment, 0 for simple RGB, 1 for
395 hue increasing and 2 for hue decreasing.
399 Don't forget to use Imager::Fountain instead of building your own.
400 Really. It even loads GIMP gradient files.
402 # build the gradient the hard way - linear from black to white,
406 [ 0, 0.25, 0.5, 'black', 'white', 0, 0 ],
407 [ 0.5. 0.75, 1.0, 'white', 'black', 0, 0 ],
410 my $linear = $img->copy;
411 $linear->filter(type => "fountain",
413 repeat => 'sawtooth',
414 segments => \@simple,
416 ya => $linear->getheight / 2,
417 xb => $linear->getwidth - 1,
418 yb => $linear->getheight / 2)
419 or die $linear->errstr;
421 my $revolution = $img->copy;
422 $revolution->filter(type => "fountain",
423 ftype => 'revolution',
424 segments => \@simple,
425 xa => $revolution->getwidth / 2,
426 ya => $revolution->getheight / 2,
427 xb => $revolution->getwidth / 2,
429 or die $revolution->errstr;
430 # out from the middle
431 my $radial = $img->copy;
432 $radial->filter(type => "fountain",
434 segments => \@simple,
435 xa => $im->getwidth / 2,
436 ya => $im->getheight / 2,
437 xb => $im->getwidth / 2,
439 or die $radial->errstr;
441 =for stopwords Gaussian
445 performs a Gaussian blur of the image, using C<stddev> as the standard
446 deviation of the curve used to combine pixels, larger values give
447 bigger blurs. For a definition of Gaussian Blur, see:
449 http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
451 Values of C<stddev> around 0.5 provide a barely noticeable blur,
452 values around 5 provide a very strong blur.
454 # only slightly blurred
455 $img->filter(type=>"gaussian", stddev=>0.5)
458 # more strongly blurred
459 $img->filter(type=>"gaussian", stddev=>5)
464 performs a Gaussian blur of the image, using C<stddevX>, C<stddevY> as the
465 standard deviation of the curve used to combine pixels on the X and Y axis,
466 respectively. Larger values give bigger blurs. For a definition of Gaussian
469 http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
471 Values of C<stddevX> or C<stddevY> around 0.5 provide a barely noticeable blur,
472 values around 5 provide a very strong blur.
474 # only slightly blurred
475 $img->filter(type=>"gaussian2", stddevX=>0.5, stddevY=>0.5)
478 # blur an image in the Y axis
479 $img->filter(type=>"gaussian", stddevX=>0, stddevY=>5 )
484 renders a gradient, with the given I<colors> at the corresponding
485 points (x,y) in C<xo> and C<yo>. You can specify the way distance is
486 measured for color blending by setting C<dist> to 0 for Euclidean, 1
487 for Euclidean squared, and 2 for Manhattan distance.
489 $img->filter(type="gradgen",
492 colors=>[ qw(red blue green) ]);
495 X<filters, hardinvert>X<hardinvert>
497 inverts the image, black to white, white to black. All color channels
498 are inverted, excluding the alpha channel if any.
500 $img->filter(type=>"hardinvert")
503 =item C<hardinvertall>
504 X<filters, hardinvertall>X<hardinvertall>
506 inverts the image, black to white, white to black. All channels are
507 inverted, including the alpha channel if any.
509 $img->filter(type=>"hardinvertall")
514 produces averaged tiles of the given C<size>.
516 $img->filter(type=>"mosaic", size=>5)
521 adds noise of the given C<amount> to the image. If C<subtype> is
522 zero, the noise is even to each channel, otherwise noise is added to
523 each channel independently.
526 $img->filter(type=>"noise", amount=>20, subtype=>0)
530 $img->filter(type=>"noise", amount=>20, subtype=>1)
533 =for stopwords Perlin
537 renders radiant Perlin turbulent noise. The center of the noise is at
538 (C<xo>, C<yo>), C<ascale> controls the angular scale of the noise ,
539 and C<rscale> the radial scale, higher numbers give more detail.
541 $img->filter(type=>"radnoise", xo=>50, yo=>50,
542 ascale=>1, rscale=>0.02)
547 alters the image to have only C<levels> distinct level in each
550 $img->filter(type=>"postlevels", levels=>10)
555 renders Perlin turbulent noise. (C<xo>, C<yo>) controls the origin of
556 the noise, and C<scale> the scale of the noise, with lower numbers
559 $img->filter(type=>"turbnoise", xo=>10, yo=>10, scale=>10)
562 =for stopwords unsharp
566 performs an unsharp mask on the image. This increases the contrast of
569 This is the result of subtracting a Gaussian blurred version of the
570 image from the original. C<stddev> controls the C<stddev> parameter
571 of the Gaussian blur. Each output pixel is:
573 in + scale * (in - blurred)
577 $img->filter(type=>"unsharpmask", stddev=>1, scale=>0.5)
580 C<unsharpmark> has the following parameters:
582 =for stopwords GIMP GIMP's
588 C<stddev> - this is equivalent to the C<Radius> value in the GIMP's
589 unsharp mask filter. This controls the size of the contrast increase
590 around edges, larger values will remove fine detail. You should
591 probably experiment on the types of images you plan to work with.
596 C<scale> - controls the strength of the edge enhancement, equivalent
597 to I<Amount> in the GIMP's unsharp mask filter. Default: 1.0.
603 applies C<wmark> as a watermark on the image with strength C<pixdiff>,
604 with an origin at (C<tx>, C<ty>)
606 $img->filter(type=>"watermark", tx=>10, ty=>50,
607 wmark=>$wmark_image, pixdiff=>50)
612 A demonstration of most of the filters can be found at:
614 http://www.develop-help.com/imager/filters.html
616 =head2 External Filters
618 As of Imager 0.48 you can create perl or XS based filters and hook
619 them into Imager's filter() method:
623 =item register_filter()
625 Registers a filter so it is visible via Imager's filter() method.
627 Imager->register_filter(type => 'your_filter',
628 defaults => { parm1 => 'default1' },
629 callseq => [ qw/image parm1/ ],
630 callsub => \&your_filter);
631 $img->filter(type=>'your_filter', parm1 => 'something');
633 The following parameters are needed:
639 C<type> - the type value that will be supplied to filter() to use your
644 C<defaults> - a hash of defaults for the filter's parameters
648 C<callseq> - a reference to an array of required parameter names.
652 C<callsub> - a code reference called to execute your filter. The
653 parameters passed to filter() are supplied as a list of parameter
654 name, value ... which can be assigned to a hash.
656 The special parameters C<image> and C<imager> are supplied as the low
657 level image object from $self and $self itself respectively.
659 The function you supply must modify the image in place.
661 To indicate an error, die with an error message followed by a
662 newline. C<filter()> will store the error message as the C<errstr()>
663 for the invocant and return false to indicate failure.
667 _is_valid($opts{myparam})
668 or die "myparam invalid!\n";
670 # actually do the filtering...
675 See L<Imager::Filter::Mandelbrot> for an example.
683 The plug in interface is deprecated. Please use the Imager API, see
684 L<Imager::API> and L</External Filters> for details
686 It is possible to add filters to the module without recompiling Imager
687 itself. This is done by using DSOs (Dynamic shared object) available
688 on most systems. This way you can maintain your own filters and not
689 have to have it added to Imager, or worse patch every new version of
690 Imager. Modules can be loaded AND UNLOADED at run time. This means
691 that you can have a server/daemon thingy that can do something like:
693 load_plugin("dynfilt/dyntest.so")
694 or die "unable to load plugin\n";
696 $img->filter(type=>'lin_stretch', a=>35, b=>200);
698 unload_plugin("dynfilt/dyntest.so")
699 or die "unable to load plugin\n";
701 Someone decides that the filter is not working as it should -
702 F<dyntest.c> can be modified and recompiled, and then reloaded:
704 load_plugin("dynfilt/dyntest.so")
705 or die "unable to load plugin\n";
709 =for stopwords Linux Solaris HPUX OpenBSD FreeBSD TRU64 OSF1 AIX Win32 OS X
711 Note: This has been tested successfully on the following systems:
712 Linux, Solaris, HPUX, OpenBSD, FreeBSD, TRU64/OSF1, AIX, Win32, OS X.
718 This is a function, not a method, exported by default. You should
719 import this function explicitly for future compatibility if you need
722 Accepts a single parameter, the name of a shared library file to load.
724 Returns true on success. Check Imager->errstr on failure.
726 =item unload_plugin()
728 This is a function, not a method, which is exported by default. You
729 should import this function explicitly for future compatibility if you
732 Accepts a single parameter, the name of a shared library to unload.
733 This library must have been previously loaded by load_plugin().
735 Returns true on success. Check Imager->errstr on failure.
739 A few example plug-ins are included and built (but not installed):
745 F<plugins/dyntest.c> - provides the C<null> (no action) filter, and
746 C<lin_stretch> filters. C<lin_stretch> stretches sample values
747 between C<a> and C<b> out to the full sample range.
751 F<plugins/dt2.c> - provides the C<html_art> filter that writes the
752 image to the HTML fragment file supplied in C<fname> as a HTML table.
756 F<plugins/flines.c> - provides the C<flines> filter that dims
757 alternate lines to emulate an old CRT display.
758 L<Imager::Filter::Flines> provides the same functionality.
762 F<plugins/mandelbrot.c> - provides the C<mandelbrot> filter that
763 renders the Mandelbrot set within the given range of x [-2, 0.5) and y
764 [-1.25, 1,25). L<Imager::Filter::Mandelbrot> provides a more flexible
765 Mandelbrot set renderer.
769 =head2 Image Difference
775 You can create a new image that is the difference between 2 other images.
777 my $diff = $img->difference(other=>$other_img);
779 For each pixel in $img that is different to the pixel in $other_img,
780 the pixel from $other_img is given, otherwise the pixel is transparent
783 This can be used for debugging image differences ("Where are they
784 different?"), and for optimizing animated GIFs.
786 Note that $img and $other_img must have the same number of channels.
787 The width and height of $diff will be the minimum of each of the width
788 and height of $img and $other_img.
796 C<other> - the other image object to compare against
800 C<mindist> - the difference between corresponding samples must be
801 greater than C<mindist> for the pixel to be considered different. So
802 a value of zero returns all different pixels, not all pixels. Range:
803 0 to 255 inclusive. Default: 0.
805 For large sample images this is scaled down to the range 0 .. 1.
813 Arnar M. Hrafnkelsson, Tony Cook <tonyc@cpan.org>.
817 Imager, Imager::Filter::Flines, Imager::Filter::Mandelbrot