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