]> git.imager.perl.org - imager.git/blob - lib/Imager/Filters.pod
don't supply a default for a missing channel list
[imager.git] / lib / Imager / Filters.pod
1 =head1 NAME
2
3 Imager::Filters - Entire Image Filtering Operations
4
5 =head1 SYNOPSIS
6
7   use Imager;
8
9   $img = ...;
10
11   $img->filter(type=>'autolevels');
12   $img->filter(type=>'autolevels', lsat=>0.2);
13   $img->filter(type=>'turbnoise')
14
15   # and lots of others
16
17   load_plugin("dynfilt/dyntest.so")
18     or die "unable to load plugin\n";
19
20   $img->filter(type=>'lin_stretch', a=>35, b=>200);
21
22   unload_plugin("dynfilt/dyntest.so")
23     or die "unable to load plugin\n";
24
25   $out = $img->difference(other=>$other_img);
26
27 =head1 DESCRIPTION
28
29 Filters are operations that have similar calling interface.
30
31 =over
32
33 =item filter()
34
35 Parameters:
36
37 =over
38
39 =item *
40
41 type - the type of filter, see L</Types of Filters>.
42
43 =item *
44
45 many other possible parameters, see L</Types of Filters> below.
46
47 =back
48
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
51 the failure.
52
53   $self->filter(type => $type, ...)
54     or die $self->errstr;
55
56 =back
57
58 =head2 Types of Filters
59
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.
63
64   Filter          Arguments   Default value
65   autolevels      lsat        0.1
66                   usat        0.1
67                   skew        0
68
69   bumpmap         bump lightx lighty
70                   elevation   0
71                   st          2
72
73   bumpmap_complex bump
74                   channel     0
75                   tx          0
76                   ty          0
77                   Lx          0.2
78                   Ly          0.4
79                   Lz          -1 
80                   cd          1.0 
81                   cs          40.0
82                   n           1.3
83                   Ia          (0 0 0)
84                   Il          (255 255 255)
85                   Is          (255 255 255)
86
87   contrast        intensity
88
89   conv            coef
90
91   fountain        xa ya xb yb
92                   ftype        linear
93                   repeat       none
94                   combine      none
95                   super_sample none
96                   ssample_param 4
97                   segments(see below)
98
99   gaussian        stddev
100
101   gradgen         xo yo colors 
102                   dist         0
103
104   hardinvert
105
106   hardinvertall
107
108   mosaic          size         20
109
110   noise           amount       3
111                   subtype      0
112
113   postlevels      levels       10
114
115   radnoise        xo           100
116                   yo           100
117                   ascale       17.0
118                   rscale       0.02
119
120   turbnoise       xo           0.0
121                   yo           0.0
122                   scale        10.0
123
124   unsharpmask     stddev       2.0
125                   scale        1.0
126
127   watermark       wmark
128                   pixdiff      10
129                   tx           0
130                   ty           0
131
132 All parameters must have some value but if a parameter has a default
133 value it may be omitted when calling the filter function.
134
135 Every one of these filters modifies the image in place.
136
137 If none of the filters here do what you need, the
138 L<Imager::Engines/transform()> or L<Imager::Engines/transform2()>
139 function may be useful.
140
141 =for stopwords
142 autolevels bumpmap bumpmap_complex conv gaussian hardinvert hardinvertall
143 radnoise turbnoise unsharpmask gradgen postlevels
144
145 A reference of the filters follows:
146
147 =over
148
149 =item autolevels
150
151 scales the value of each channel so that the values in the image will
152 cover the whole possible range for the channel.  C<lsat> and C<usat>
153 truncate the range by the specified fraction at the top and bottom of
154 the range respectively.
155
156   # increase contrast per channel, losing little detail
157   $img->filter(type=>"autolevels")
158     or die $img->errstr;
159
160   # increase contrast, losing 20% of highlight at top and bottom range
161   $img->filter(type=>"autolevels", lsat=>0.2, usat=>0.2)
162     or die $img->errstr;
163
164 =item bumpmap
165
166 uses the channel C<elevation> image C<bump> as a bump map on your
167 image, with the light at (C<lightx>, C<lightty>), with a shadow length
168 of C<st>.
169
170   $img->filter(type=>"bumpmap", bump=>$bumpmap_img,
171                lightx=>10, lighty=>10, st=>5)
172     or die $img->errstr;
173
174 =item bumpmap_complex
175
176 uses the channel C<channel> image C<bump> as a bump map on your image.
177 If C<< Lz < 0 >> the three L parameters are considered to be the
178 direction of the light.  If C<< Lz > 0 >> the L parameters are
179 considered to be the light position.  C<Ia> is the ambient color,
180 C<Il> is the light color, C<Is> is the color of specular highlights.
181 C<cd> is the diffuse coefficient and C<cs> is the specular
182 coefficient.  C<n> is the shininess of the surface.
183
184   $img->filter(type=>"bumpmap_complex", bump=>$bumpmap_img)
185     or die $img->errstr;
186
187 =item contrast
188
189 scales each channel by C<intensity>.  Values of C<intensity> < 1.0
190 will reduce the contrast.
191
192   # higher contrast
193   $img->filter(type=>"contrast", intensity=>1.3)
194     or die $img->errstr;
195
196   # lower contrast
197   $img->filter(type=>"contrast", intensity=>0.8)
198     or die $img->errstr;
199
200 =item conv
201
202 performs 2 1-dimensional convolutions on the image using the values
203 from C<coef>.  C<coef> should be have an odd length and the sum of the
204 coefficients must be non-zero.
205
206   # sharper
207   $img->filter(type=>"conv", coef=>[-0.5, 2, -0.5 ])
208     or die $img->errstr;
209
210   # blur
211   $img->filter(type=>"conv", coef=>[ 1, 2, 1 ])
212     or die $img->errstr;
213
214   # error
215   $img->filter(type=>"conv", coef=>[ -0.5, 1, -0.5 ])
216     or die $img->errstr;
217
218 =item fountain
219
220 renders a fountain fill, similar to the gradient tool in most paint
221 software.  The default fill is a linear fill from opaque black to
222 opaque white.  The points C<A(Cxa, ya)> and C<B(xb, yb)> control the
223 way the fill is performed, depending on the C<ftype> parameter:
224
225 =for stopwords ramping
226
227 =over
228
229 =item C<linear>
230
231 the fill ramps from A through to B.
232
233 =item C<bilinear>
234
235 the fill ramps in both directions from A, where AB defines the length
236 of the gradient.
237
238 =item C<radial>
239
240 A is the center of a circle, and B is a point on it's circumference.
241 The fill ramps from the center out to the circumference.
242
243 =item C<radial_square>
244
245 A is the center of a square and B is the center of one of it's sides.
246 This can be used to rotate the square.  The fill ramps out to the
247 edges of the square.
248
249 =item C<revolution>
250
251 A is the center of a circle and B is a point on its circumference.  B
252 marks the 0 and 360 point on the circle, with the fill ramping
253 clockwise.
254
255 =item C<conical>
256
257 A is the center of a circle and B is a point on it's circumference.  B
258 marks the 0 and point on the circle, with the fill ramping in both
259 directions to meet opposite.
260
261 =back
262
263 The C<repeat> option controls how the fill is repeated for some
264 C<ftype>s after it leaves the AB range:
265
266 =over
267
268 =item C<none>
269
270 no repeats, points outside of each range are treated as if they were
271 on the extreme end of that range.
272
273 =item C<sawtooth>
274
275 the fill simply repeats in the positive direction
276
277 =item C<triangle>
278
279 the fill repeats in reverse and then forward and so on, in the
280 positive direction
281
282 =item C<saw_both>
283
284 the fill repeats in both the positive and negative directions (only
285 meaningful for a linear fill).
286
287 =item C<tri_both>
288
289 as for triangle, but in the negative direction too (only meaningful
290 for a linear fill).
291
292 =back
293
294 By default the fill simply overwrites the whole image (unless you have
295 parts of the range 0 through 1 that aren't covered by a segment), if
296 any segments of your fill have any transparency, you can set the
297 I<combine> option to 'normal' to have the fill combined with the
298 existing pixels.  See the description of I<combine> in L<Imager::Fill>.
299
300 If your fill has sharp edges, for example between steps if you use
301 repeat set to 'triangle', you may see some aliased or ragged edges.
302 You can enable super-sampling which will take extra samples within the
303 pixel in an attempt anti-alias the fill.
304
305 The possible values for the super_sample option are:
306
307 =over
308
309 =item none
310
311 no super-sampling is done
312
313 =item grid
314
315 a square grid of points are sampled.  The number of points sampled is
316 the square of ceil(0.5 + sqrt(ssample_param)).
317
318 =item random
319
320 a random set of points within the pixel are sampled.  This looks
321 pretty bad for low ssample_param values.
322
323 =item circle
324
325 the points on the radius of a circle within the pixel are sampled.
326 This seems to produce the best results, but is fairly slow (for now).
327
328 =back
329
330 You can control the level of sampling by setting the ssample_param
331 option.  This is roughly the number of points sampled, but depends on
332 the type of sampling.
333
334 The segments option is an arrayref of segments.  You really should use
335 the L<Imager::Fountain> class to build your fountain fill.  Each
336 segment is an array ref containing:
337
338 =over
339
340 =item start
341
342 a floating point number between 0 and 1, the start of the range of
343 fill parameters covered by this segment.
344
345 =item middle
346
347 a floating point number between start and end which can be used to
348 push the color range towards one end of the segment.
349
350 =item end
351
352 a floating point number between 0 and 1, the end of the range of fill
353 parameters covered by this segment.  This should be greater than
354 start.
355
356 =item c0
357
358 =item c1
359
360 The colors at each end of the segment.  These can be either
361 Imager::Color or Imager::Color::Float objects.
362
363 =item segment type
364
365 The type of segment, this controls the way the fill parameter varies
366 over the segment. 0 for linear, 1 for curved (unimplemented), 2 for
367 sine, 3 for sphere increasing, 4 for sphere decreasing.
368
369 =item color type
370
371 The way the color varies within the segment, 0 for simple RGB, 1 for
372 hue increasing and 2 for hue decreasing.
373
374 =back
375
376 Don't forget to use Imager::Fountain instead of building your own.
377 Really.  It even loads GIMP gradient files.
378
379   # build the gradient the hard way - linear from black to white,
380   # then back again
381   my @simple =
382    (
383      [   0, 0.25, 0.5, 'black', 'white', 0, 0 ],
384      [ 0.5. 0.75, 1.0, 'white', 'black', 0, 0 ],
385    );
386   # across
387   my $linear = $img->copy;
388   $linear->filter(type     => "fountain",
389                   ftype    => 'linear',
390                   repeat   => 'sawtooth',
391                   segments => \@simple,
392                   xa       => 0,
393                   ya       => $linear->getheight / 2,
394                   xb       => $linear->getwidth - 1,
395                   yb       => $linear->getheight / 2)
396     or die $linear->errstr;
397   # around
398   my $revolution = $img->copy;
399   $revolution->filter(type     => "fountain",
400                       ftype    => 'revolution',
401                       segments => \@simple,
402                       xa       => $revolution->getwidth / 2,
403                       ya       => $revolution->getheight / 2,
404                       xb       => $revolution->getwidth / 2,
405                       yb       => 0)
406     or die $revolution->errstr;
407   # out from the middle
408   my $radial = $img->copy;
409   $radial->filter(type     => "fountain",
410                   ftype    => 'radial',
411                   segments => \@simple,
412                   xa       => $im->getwidth / 2,
413                   ya       => $im->getheight / 2,
414                   xb       => $im->getwidth / 2,
415                   yb       => 0)
416     or die $radial->errstr;
417
418 =for stopwords Gaussian
419                            
420 =item gaussian
421
422 performs a Gaussian blur of the image, using C<stddev> as the standard
423 deviation of the curve used to combine pixels, larger values give
424 bigger blurs.  For a definition of Gaussian Blur, see:
425
426   http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
427
428 Values of C<stddev> around 0.5 provide a barely noticeable blur,
429 values around 5 provide a very strong blur.
430
431   # only slightly blurred
432   $img->filter(type=>"gaussian", stddev=>0.5)
433     or die $img->errstr;
434
435   # more strongly blurred
436   $img->filter(type=>"gaussian", stddev=>5)
437     or die $img->errstr;
438
439 =item gradgen
440
441 renders a gradient, with the given I<colors> at the corresponding
442 points (x,y) in C<xo> and C<yo>.  You can specify the way distance is
443 measured for color blending by setting C<dist> to 0 for Euclidean, 1
444 for Euclidean squared, and 2 for Manhattan distance.
445
446   $img->filter(type="gradgen", 
447                xo=>[ 10, 50, 10 ], 
448                yo=>[ 10, 50, 50 ],
449                colors=>[ qw(red blue green) ]);
450
451 =item hardinvert
452 X<filters, hardinvert>X<hardinvert>
453
454 inverts the image, black to white, white to black.  All color channels
455 are inverted, excluding the alpha channel if any.
456
457   $img->filter(type=>"hardinvert")
458     or die $img->errstr;
459
460 =item hardinvertall
461 X<filters, hardinvertall>X<hardinvertall>
462
463 inverts the image, black to white, white to black.  All channels are
464 inverted, including the alpha channel if any.
465
466   $img->filter(type=>"hardinvertall")
467     or die $img->errstr;
468
469 =item mosaic
470
471 produces averaged tiles of the given C<size>.
472
473   $img->filter(type=>"mosaic", size=>5)
474     or die $img->errstr;
475
476 =item noise
477
478 adds noise of the given C<amount> to the image.  If C<subtype> is
479 zero, the noise is even to each channel, otherwise noise is added to
480 each channel independently.
481
482   # monochrome noise
483   $img->filter(type=>"noise", amount=>20, subtype=>0)
484     or die $img->errstr;
485
486   # color noise
487   $img->filter(type=>"noise", amount=>20, subtype=>1)
488     or die $img->errstr;
489
490 =for stopwords Perlin
491
492 =item radnoise
493
494 renders radiant Perlin turbulent noise.  The center of the noise is at
495 (C<xo>, C<yo>), C<ascale> controls the angular scale of the noise ,
496 and C<rscale> the radial scale, higher numbers give more detail.
497
498   $img->filter(type=>"radnoise", xo=>50, yo=>50,
499                ascale=>1, rscale=>0.02)
500     or die $img->errstr;
501
502 =item postlevels
503
504 alters the image to have only C<levels> distinct level in each
505 channel.
506
507   $img->filter(type=>"postlevels", levels=>10)
508     or die $img->errstr;
509
510 =item turbnoise
511
512 renders Perlin turbulent noise.  (C<xo>, C<yo>) controls the origin of
513 the noise, and C<scale> the scale of the noise, with lower numbers
514 giving more detail.
515
516   $img->filter(type=>"turbnoise", xo=>10, yo=>10, scale=>10)
517     or die $img->errstr;
518
519 =for stopwords unsharp
520
521 =item unsharpmask
522
523 performs an unsharp mask on the image.  This increases the contrast of
524 edges in the image.
525
526 This is the result of subtracting a Gaussian blurred version of the
527 image from the original.  C<stddev> controls the C<stddev> parameter
528 of the Gaussian blur.  Each output pixel is:
529
530   in + scale * (in - blurred)
531
532 eg.
533
534   $img->filter(type=>"unsharpmask", stddev=>1, scale=>0.5)
535     or die $img->errstr;
536
537 C<unsharpmark> has the following parameters:
538
539 =for stopwords GIMP GIMP's
540
541 =over
542
543 =item *
544
545 C<stddev> - this is equivalent to the C<Radius> value in the GIMP's
546 unsharp mask filter.  This controls the size of the contrast increase
547 around edges, larger values will remove fine detail. You should
548 probably experiment on the types of images you plan to work with.
549 Default: 2.0.
550
551 =item *
552
553 C<scale> - controls the strength of the edge enhancement, equivalent
554 to I<Amount> in the GIMP's unsharp mask filter.  Default: 1.0.
555
556 =back
557
558 =item watermark
559
560 applies C<wmark> as a watermark on the image with strength C<pixdiff>,
561 with an origin at (C<tx>, C<ty>)
562
563   $img->filter(type=>"watermark", tx=>10, ty=>50, 
564                wmark=>$wmark_image, pixdiff=>50)
565     or die $img->errstr;
566
567 =back
568
569 A demonstration of most of the filters can be found at:
570
571   http://www.develop-help.com/imager/filters.html
572
573 =head2 External Filters
574
575 As of Imager 0.48 you can create perl or XS based filters and hook
576 them into Imager's filter() method:
577
578 =over
579
580 =item register_filter()
581
582 Registers a filter so it is visible via Imager's filter() method.
583
584   Imager->register_filter(type => 'your_filter',
585                           defaults => { parm1 => 'default1' },
586                           callseq => [ qw/image parm1/ ],
587                           callsub => \&your_filter);
588   $img->filter(type=>'your_filter', parm1 => 'something');
589
590 The following parameters are needed:
591
592 =over
593
594 =item *
595
596 C<type> - the type value that will be supplied to filter() to use your
597 filter.
598
599 =item *
600
601 C<defaults> - a hash of defaults for the filter's parameters
602
603 =item *
604
605 C<callseq> - a reference to an array of required parameter names.
606
607 =item *
608
609 C<callsub> - a code reference called to execute your filter.  The
610 parameters passed to filter() are supplied as a list of parameter
611 name, value ... which can be assigned to a hash.
612
613 The special parameters C<image> and C<imager> are supplied as the low
614 level image object from $self and $self itself respectively.
615
616 The function you supply must modify the image in place.
617
618 To indicate an error, die with an error message followed by a
619 newline. C<filter()> will store the error message as the C<errstr()>
620 for the invocant and return false to indicate failure.
621
622   sub my_filter {
623     my %opts = @_;
624     _is_valid($opts{myparam})
625       or die "myparam invalid!\n";
626
627     # actually do the filtering...
628   }
629
630 =back
631
632 See L<Imager::Filter::Mandelbrot> for an example.
633
634 =back
635
636 =for stopwords DSOs
637
638 =head2 Plug-ins
639
640 The plug in interface is deprecated.  Please use the Imager API, see
641 L<Imager::API> and L</External Filters> for details
642
643 It is possible to add filters to the module without recompiling Imager
644 itself.  This is done by using DSOs (Dynamic shared object) available
645 on most systems.  This way you can maintain your own filters and not
646 have to have it added to Imager, or worse patch every new version of
647 Imager.  Modules can be loaded AND UNLOADED at run time.  This means
648 that you can have a server/daemon thingy that can do something like:
649
650   load_plugin("dynfilt/dyntest.so")
651     or die "unable to load plugin\n";
652
653   $img->filter(type=>'lin_stretch', a=>35, b=>200);
654
655   unload_plugin("dynfilt/dyntest.so")
656     or die "unable to load plugin\n";
657
658 Someone decides that the filter is not working as it should -
659 F<dyntest.c> can be modified and recompiled, and then reloaded:
660
661   load_plugin("dynfilt/dyntest.so")
662     or die "unable to load plugin\n";
663
664   $img->filter(%hsh);
665
666 =for stopwords Linux Solaris HPUX OpenBSD FreeBSD TRU64 OSF1 AIX Win32 OS X
667
668 Note: This has been tested successfully on the following systems:
669 Linux, Solaris, HPUX, OpenBSD, FreeBSD, TRU64/OSF1, AIX, Win32, OS X.
670
671 =over
672
673 =item load_plugin()
674
675 This is a function, not a method, exported by default.  You should
676 import this function explicitly for future compatibility if you need
677 it.
678
679 Accepts a single parameter, the name of a shared library file to load.
680
681 Returns true on success.  Check Imager->errstr on failure.
682
683 =item unload_plugin()
684
685 This is a function, not a method, which is exported by default.  You
686 should import this function explicitly for future compatibility if you
687 need it.
688
689 Accepts a single parameter, the name of a shared library to unload.
690 This library must have been previously loaded by load_plugin().
691
692 Returns true on success.  Check Imager->errstr on failure.
693
694 =back
695
696 A few example plug-ins are included and built (but not installed):
697
698 =over
699
700 =item *
701
702 F<plugins/dyntest.c> - provides the C<null> (no action) filter, and
703 C<lin_stretch> filters.  C<lin_stretch> stretches sample values
704 between C<a> and C<b> out to the full sample range.
705
706 =item *
707
708 F<plugins/dt2.c> - provides the C<html_art> filter that writes the
709 image to the HTML fragment file supplied in C<fname> as a HTML table.
710
711 =item *
712
713 F<plugins/flines.c> - provides the C<flines> filter that dims
714 alternate lines to emulate an old CRT display.
715 L<Imager::Filter::Flines> provides the same functionality.
716
717 =item *
718
719 F<plugins/mandelbrot.c> - provides the C<mandelbrot> filter that
720 renders the Mandelbrot set within the given range of x [-2, 0.5) and y
721 [-1.25, 1,25).  L<Imager::Filter::Mandelbrot> provides a more flexible
722 Mandelbrot set renderer.
723
724 =back
725
726 =head2 Image Difference
727
728 =over
729
730 =item difference()
731
732 You can create a new image that is the difference between 2 other images.
733
734   my $diff = $img->difference(other=>$other_img);
735
736 For each pixel in $img that is different to the pixel in $other_img,
737 the pixel from $other_img is given, otherwise the pixel is transparent
738 black.
739
740 This can be used for debugging image differences ("Where are they
741 different?"), and for optimizing animated GIFs.
742
743 Note that $img and $other_img must have the same number of channels.
744 The width and height of $diff will be the minimum of each of the width
745 and height of $img and $other_img.
746
747 Parameters:
748
749 =over
750
751 =item *
752
753 C<other> - the other image object to compare against
754
755 =item *
756
757 C<mindist> - the difference between corresponding samples must be
758 greater than C<mindist> for the pixel to be considered different.  So
759 a value of zero returns all different pixels, not all pixels.  Range:
760 0 to 255 inclusive.  Default: 0.
761
762 For large sample images this is scaled down to the range 0 .. 1.
763
764 =back
765
766 =back
767
768 =head1 AUTHOR
769
770 Arnar M. Hrafnkelsson, Tony Cook <tonyc@cpan.org>.
771
772 =head1 SEE ALSO
773
774 Imager, Imager::Filter::Flines, Imager::Filter::Mandelbrot
775
776 =head1 REVISION
777
778 $Revision$
779
780 =cut