3 Imager::Engines - Programmable transformation operations
14 my $newimg = $img->transform(
16 yexpr=>'y+10*sin((x+y)/10)')
19 my $newimg = Imager::transform2(\%opts, @imgs)
20 or die "transform2 failed: $Imager::ERRSTR";
22 my $newimg = $img->matrix_transform(
23 matrix=>[ -1, 0, $img->getwidth-1,
32 The C<transform()> function can be used to generate spatial warps and
33 rotations and such effects. It only operates on a single image and
34 its only function is to displace pixels.
36 It can be given the operations in postfix notation or the module
37 Affix::Infix2Postfix can be used to generate postfix code from infix
38 code. Look in the test case t/t55trans.t for an example.
40 C<transform()> needs expressions (or opcodes) that determine the
41 source pixel for each target pixel. Source expressions are infix
42 expressions using any of the +, -, *, / or ** binary operators, the -
43 unary operator, ( and ) for grouping and the C<sin()> and C<cos()>
44 functions. The target pixel is input as the variables x and y.
46 You specify the x and y expressions as C<xexpr> and C<yexpr> respectively.
47 You can also specify opcodes directly, but that's magic deep enough
48 that you can look at the source code.
50 Note: You can still use the transform() function, but the transform2()
51 function is just as fast and is more likely to be enhanced and
54 $new_img=$img->transform(xexpr=>'x',yexpr=>'y+10*sin((x+y)/10)')
56 $new_img=$img->transform(xexpr=>'x+0.1*y+5*sin(y/10.0+1.57)',
57 yexpr=>'y+10*sin((x+y-0.785)/10)')
61 Imager also supports a C<transform2()> class method which allows you
62 perform a more general set of operations, rather than just specifying
63 a spatial transformation as with the transform() method, you can also
64 perform color transformations, image synthesis and image
65 combinations from multiple source images.
67 C<transform2()> takes an reference to an options hash, and a list of
68 images to operate one (this list may be empty):
73 my $img = Imager::transform2(\%opts, @imgs)
74 or die "transform2 failed: $Imager::ERRSTR";
76 The options hash may define a transformation function, and optionally:
82 width - the width of the image in pixels. If this isn't supplied the
83 width of the first input image is used. If there are no input images
88 height - the height of the image in pixels. If this isn't supplied
89 the height of the first input image is used. If there are no input
90 images an error occurs.
94 constants - a reference to hash of constants to define for the
95 expression engine. Some extra constants are defined by Imager
99 channels - the number of channels in the output image. If this isn't
100 supplied a 3 channel image will be created.
104 The transformation function is specified using either the C<expr> or
105 C<rpnexpr> member of the options.
107 =head3 Infix expressions
109 You can supply infix expressions to transform 2 with the C<expr> keyword.
111 $opts{expr} = 'return getp1(w-x, h-y)'
113 The 'expression' supplied follows this general grammar:
115 ( identifier '=' expr ';' )* 'return' expr
117 This allows you to simplify your expressions using variables.
119 A more complex example might be:
121 $opts{expr} = 'pix = getp1(x,y); return if(value(pix)>0.8,pix*0.8,pix)'
123 Currently to use infix expressions you must have the L<Parse::RecDescent>
124 module installed (available from CPAN). There is also what might be a
125 significant delay the first time you run the infix expression parser
126 due to the compilation of the expression grammar.
128 =head3 Postfix expressions
130 You can supply postfix or reverse-polish notation expressions to
131 transform2() through the C<rpnexpr> keyword.
133 The parser for C<rpnexpr> emulates a stack machine, so operators will
134 expect to see their parameters on top of the stack. A stack machine
135 isn't actually used during the image transformation itself.
137 You can store the value at the top of the stack in a variable called
138 C<foo> using C<!foo> and retrieve that value again using @foo. The !foo
139 notation will pop the value from the stack.
141 An example equivalent to the infix expression above:
143 $opts{rpnexpr} = 'x y getp1 !pix @pix value 0.8 gt @pix 0.8 * @pix ifp'
145 At the end of the expression there should be a single pixel value left
146 on the stack, which is used as the output pixel.
150 transform2() has a fairly rich range of operators.
152 Each entry below includes the usage with C<rpnexpr>, formatted as:
156 I<operand> I<operand> ... B<I<operator>> -- I<result>
160 If the operand or result begins with "N" it is a numeric value, if it
161 begins with "C" it is a color or pixel value.
165 =item +, *, -, /, %, **
167 multiplication, addition, subtraction, division, remainder and
168 exponentiation. Multiplication, addition and subtraction can be used
169 on color values too - though you need to be careful - adding 2 white
170 values together and multiplying by 0.5 will give you gray, not white.
172 Division by zero (or a small number) just results in a large number.
173 Modulo zero (or a small number) results in zero. % is implemented
174 using fmod() so you can use this to take a value mod a floating point
177 =for stopwords N1 N2 N uminus
183 I<N1> I<N2> B<+> -- I<N>
185 I<N1> I<N2> B<*> -- I<N>
187 I<N1> I<N2> B<-> -- I<N>
189 I<N1> I<N2> B</> -- I<N>
191 I<N1> I<N2> B<**> -- I<N>
193 I<N1> B<uminus> -- I<N>
197 =item sin(N), cos(N), atan2(y,x)
199 Some basic trig functions. They work in radians, so you can't just
202 =for stopwords Ny Nx atan2
212 I<Ny> I<Nx> B<atan2> -- I<N>
216 =item distance(x1, y1, x2, y2)
218 Find the distance between two points. This is handy (along with
219 atan2()) for producing circular effects.
221 =for stopwords Nx1 Ny1 Nx2 Ny2
227 I<Nx1> I<Ny1> I<Nx2> I<Ny2> B<distance> -- I<N>
233 Find the square root. I haven't had much use for this since adding
234 the distance() function.
246 Find the absolute value.
256 =item getp1(x,y), getp2(x,y), getp3(x, y)
258 Get the pixel at position (x,y) from the first, second or third image
259 respectively. I may add a getpn() function at some point, but this
260 prevents static checking of the instructions against the number of
261 images actually passed in.
263 =for stopwords getp1 getp2 getp3
269 I<Nx> I<Ny> B<getp1> -- I<C>
271 I<Nx> I<Ny> B<getp2> -- I<C>
273 I<Nx> I<Ny> B<getp3> -- I<C>
277 =item value(c), hue(c), sat(c), hsv(h,s,v), hsva(h,s,v,alpha)
279 Separates a color value into it's value (brightness), hue (color)
280 and saturation elements. Use hsv() to put them back together (after
281 suitable manipulation), or hsva() to include a transparency value.
283 =for stopwords Nh Ns Nv hsv hsva Nr Ng Nb rgb rgba
289 I<C> B<value> -- I<N>
295 I<Nh> I<Ns> I<Nv> B<hsv> -- I<C>
297 I<Nh> I<Ns> I<Nv> I<Na> B<hsva> -- I<C>
301 =item red(c), green(c), blue(c), rgb(r,g,b), rgba(r,g,b,a)
303 Separates a color value into it's red, green and blue colors. Use
304 rgb(r,g,b) to put it back together, or rgba() to include a
313 I<C> B<green> -- I<N>
317 I<Nr> I<Ng> I<Nb> B<rgb> -- I<C>
319 I<Nr> I<Ng> I<Nb> I<Na> B<rgba> -- I<C>
325 Retrieve the alpha value from a color.
331 I<C> B<alpha> -- I<N>
337 Convert a value to an integer. Uses a C int cast, so it may break on
348 =item if(cond,ntrue,nfalse), if(cond,ctrue,cfalse)
350 A simple (and inefficient) if function.
352 =for stopwords Ncond ifp
358 I<Ncond> I<N-true-result> I<N-false-result> B<if> -- I<N>
360 I<Ncond> I<C-true-result> I<C-false-result> B<if> -- I<C>
362 I<Ncond> I<C-true-result> I<C-false-result> B<ifp> -- I<C>
366 =item <=,<,==,>=,>,!=
368 Relational operators (typically used with if()). Since we're working
369 with floating point values the equalities are 'near equalities' - an
370 epsilon value is used.
374 I<N1> I<N2> B<< <= >> -- I<N>
376 I<N1> I<N2> B<< < >> -- I<N>
378 I<N1> I<N2> B<< >= >> -- I<N>
380 I<N1> I<N2> B<< > >> -- I<N>
382 I<N1> I<N2> B<< == >> -- I<N>
384 I<N1> I<N2> B<< != >> -- I<N>
390 Basic logical operators.
396 I<N1> I<N2> B<and> -- I<N>
398 I<N1> I<N2> B<or> -- I<N>
406 Natural logarithm and exponential.
418 =item det(a, b, c, d)
420 Calculate the determinant of the 2 x 2 matrix;
425 =for stopwords Na Nv Nc Nd det
431 I<Na> I<Nb> I<Nc> I<Nd> B<det> -- I<N>
439 transform2() defines the following constants:
445 The classical constant.
451 The width and height of the output image.
457 The center of the output image.
459 =item C<w>I<image number>
461 =item C<h>I<image number>
463 The width and height of each of the input images, C<w1> is the width
464 of the first input image and so on.
466 =item C<cx>I<image number>
468 =item C<cy>I<image number>
470 The center of each of the input images, (C<cx1>, C<cy1>) is the center
471 of the first input image and so on.
479 rpnexpr=>'x 25 % 15 * y 35 % 10 * getp1 !pat x y getp1 !pix @pix sat 0.7 gt @pat @pix ifp'
481 tiles a smaller version of the input image over itself where the
482 color has a saturation over 0.7.
484 rpnexpr=>'x 25 % 15 * y 35 % 10 * getp1 !pat y 360 / !rat x y getp1 1 @rat - pmult @pat @rat pmult padd'
486 tiles the input image over itself so that at the top of the image the
487 full-size image is at full strength and at the bottom the tiling is
490 rpnexpr=>'x y getp1 !pix @pix value 0.96 gt @pix sat 0.1 lt and 128 128 255 rgb @pix ifp'
492 replace pixels that are white or almost white with a palish blue
494 rpnexpr=>'x 35 % 10 * y 45 % 8 * getp1 !pat x y getp1 !pix @pix sat 0.2 lt @pix value 0.9 gt and @pix @pat @pix value 2 / 0.5 + pmult ifp'
496 Tiles the input image over it self where the image isn't white or almost
499 rpnexpr=>'x y 160 180 distance !d y 180 - x 160 - atan2 !a @d 10 / @a + 3.1416 2 * % !a2 @a2 180 * 3.1416 / 1 @a2 sin 1 + 2 / hsv'
503 rpnexpr=>'x y 160 180 distance !d y 180 - x 160 - atan2 !a @d 10 / @a + 3.1416 2 * % !a2 @a 180 * 3.1416 / 1 @a2 sin 1 + 2 / hsv'
505 A spiral built on top of a color wheel.
509 For details on expression parsing see L<Imager::Expr>. For details on
510 the virtual machine used to transform the images, see
513 # generate a colorful spiral
514 # requires that Parse::RecDescent be installed
515 my $newimg = Imager::transform2({
516 width => 160, height=>160,
518 dist = distance(x, y, w/2, h/2);
519 angle = atan2(y-h/2, x-w/2);
520 angle2 = (dist / 10 + angle) % ( 2 * pi );
521 return hsv(angle*180/pi, 1, (sin(angle2)+1)/2);
525 # replace green portions of an image with another image
526 my $newimg = Imager::transform2({
528 x y getp2 !pat # used to replace green portions
529 x y getp1 !pix # source with "green screen"
530 @pix red 10 lt @pix blue 10 lt && # low blue and red
531 @pix green 254 gt && # and high green
534 }, $source, $background);
536 =head2 Matrix Transformations
540 =item matrix_transform()
542 Rather than having to write code in a little language, you can use a
543 matrix to perform affine transformations, using the matrix_transform()
546 my $newimg = $img->matrix_transform(matrix=>[ -1, 0, $img->getwidth-1,
550 By default the output image will be the same size as the input image,
551 but you can supply the C<xsize> and C<ysize> parameters to change the
554 Rather than building matrices by hand you can use the Imager::Matrix2d
555 module to build the matrices. This class has methods to allow you to
556 scale, shear, rotate, translate and reflect, and you can combine these
557 with an overloaded multiplication operator.
559 WARNING: the matrix you provide in the matrix operator transforms the
560 co-ordinates within the B<destination> image to the co-ordinates
561 within the I<source> image. This can be confusing.
563 You can also supply a C<back> argument which acts as a background
564 color for the areas of the image with no samples available (outside
565 the rectangle of the source image.) This can be either an
566 Imager::Color or Imager::Color::Float object. This is B<not> mixed
567 transparent pixels in the middle of the source image, it is B<only>
568 used for pixels where there is no corresponding pixel in the source
575 Tony Cook <tonyc@cpan.org>, Arnar M. Hrafnkelsson