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