- added comment support the postfix transform2() expression
[imager.git] / lib / Imager / Engines.pod
CommitLineData
3e1be2c1
AMH
1=head1 NAME
2
3Imager::Engines - Programmable transformation operations
4
5=head1 SYNOPSIS
6
7 use Imager;
8
9 my %opts;
10 my @imgs;
11 my $img;
12 ...
13
14 my $newimg = $img->transform(
15 xexpr=>'x',
16 yexpr=>'y+10*sin((x+y)/10)')
17 or die $img->errstr;
18
19 my $newimg = Imager::transform2(\%opts, @imgs)
20 or die "transform2 failed: $Imager::ERRSTR";
21
22 my $newimg = $img->matrix_transform(
23 matrix=>[ -1, 0, $img->getwidth-1,
24 0, 1, 0,
25 0, 0, 1 ]);
26
27
28=head1 DESCRIPTION
29
30=head2 transform
31
32The C<transform()> function can be used to generate spatial warps and
33rotations and such effects. It only operates on a single image and
34its only function is to displace pixels.
35
36It can be given the operations in postfix notation or the module
37Affix::Infix2Postfix can be used to generate postfix code from infix
38code. Look in the test case t/t55trans.t for an example.
39
40C<transform()> needs expressions (or opcodes) that determine the
41source pixel for each target pixel. Source expressions are infix
42expressions using any of the +, -, *, / or ** binary operators, the -
43unary operator, ( and ) for grouping and the sin() and cos()
44functions. The target pixel is input as the variables x and y.
45
46You specify the x and y expressions as xexpr and yexpr respectively.
47You can also specify opcodes directly, but that's magic deep enough
48that you can look at the source code.
49
50Note: You can still use the transform() function, but the transform2()
51function is just as fast and is more likely to be enhanced and
52maintained.
53
54
55
56=head2 transform2
57
58Imager also supports a C<transform2()> class method which allows you
59perform a more general set of operations, rather than just specifying
60a spatial transformation as with the transform() method, you can also
61perform colour transformations, image synthesis and image
62combinations from multiple source images.
63
64C<transform2()> takes an reference to an options hash, and a list of
65images to operate one (this list may be empty):
66
67 my %opts;
68 my @imgs;
69 ...
70 my $img = Imager::transform2(\%opts, @imgs)
71 or die "transform2 failed: $Imager::ERRSTR";
72
73The options hash may define a transformation function, and optionally:
74
75=over
76
77=item *
78
79width - the width of the image in pixels. If this isn't supplied the
80width of the first input image is used. If there are no input images
81an error occurs.
82
83=item *
84
85height - the height of the image in pixels. If this isn't supplied
86the height of the first input image is used. If there are no input
87images an error occurs.
88
89=item *
90
91constants - a reference to hash of constants to define for the
92expression engine. Some extra constants are defined by Imager
93
e5744e01
TC
94=item *
95
96channels - the number of channels in the output image. If this isn't
97supplied a 3 channel image will be created.
98
3e1be2c1
AMH
99=back
100
101The tranformation function is specified using either the expr or
102rpnexpr member of the options.
103
104=over
105
106=item Infix expressions
107
108You can supply infix expressions to transform 2 with the expr keyword.
109
110$opts{expr} = 'return getp1(w-x, h-y)'
111
112The 'expression' supplied follows this general grammar:
113
114 ( identifier '=' expr ';' )* 'return' expr
115
116This allows you to simplify your expressions using variables.
117
118A more complex example might be:
119
120$opts{expr} = 'pix = getp1(x,y); return if(value(pix)>0.8,pix*0.8,pix)'
121
122Currently to use infix expressions you must have the Parse::RecDescent
123module installed (available from CPAN). There is also what might be a
124significant delay the first time you run the infix expression parser
125due to the compilation of the expression grammar.
126
127=item Postfix expressions
128
129You can supply postfix or reverse-polish notation expressions to
130transform2() through the rpnexpr keyword.
131
132The parser for rpnexpr emulates a stack machine, so operators will
133expect to see their parameters on top of the stack. A stack machine
134isn't actually used during the image transformation itself.
135
136You can store the value at the top of the stack in a variable called
137foo using !foo and retrieve that value again using @foo. The !foo
138notation will pop the value from the stack.
139
140An example equivalent to the infix expression above:
141
142 $opts{rpnexpr} = 'x y getp1 !pix @pix value 0.8 gt @pix 0.8 * @pix ifp'
143
144=back
145
146transform2() has a fairly rich range of operators.
147
148=over
149
150=item +, *, -, /, %, **
151
152multiplication, addition, subtraction, division, remainder and
153exponentiation. Multiplication, addition and subtraction can be used
154on colour values too - though you need to be careful - adding 2 white
155values together and multiplying by 0.5 will give you grey, not white.
156
157Division by zero (or a small number) just results in a large number.
3799c4d1
TC
158Modulo zero (or a small number) results in zero. % is implemented
159using fmod() so you can use this to take a value mod a floating point
160value.
3e1be2c1
AMH
161
162=item sin(N), cos(N), atan2(y,x)
163
164Some basic trig functions. They work in radians, so you can't just
165use the hue values.
166
167=item distance(x1, y1, x2, y2)
168
169Find the distance between two points. This is handy (along with
170atan2()) for producing circular effects.
171
172=item sqrt(n)
173
174Find the square root. I haven't had much use for this since adding
175the distance() function.
176
177=item abs(n)
178
179Find the absolute value.
180
181=item getp1(x,y), getp2(x,y), getp3(x, y)
182
183Get the pixel at position (x,y) from the first, second or third image
184respectively. I may add a getpn() function at some point, but this
185prevents static checking of the instructions against the number of
186images actually passed in.
187
e5744e01 188=item value(c), hue(c), sat(c), hsv(h,s,v), hsva(h,s,v,alpha)
3e1be2c1
AMH
189
190Separates a colour value into it's value (brightness), hue (colour)
191and saturation elements. Use hsv() to put them back together (after
e5744e01 192suitable manipulation), or hsva() to include a tranparency value.
3e1be2c1
AMH
193
194=item red(c), green(c), blue(c), rgb(r,g,b)
195
196Separates a colour value into it's red, green and blue colours. Use
e5744e01
TC
197rgb(r,g,b) to put it back together, or rgba() to include a
198transparency value.
199
200=item alpha(c)
201
202Retrieve the alpha value from a colour.
3e1be2c1
AMH
203
204=item int(n)
205
206Convert a value to an integer. Uses a C int cast, so it may break on
207large values.
208
209=item if(cond,ntrue,nfalse), if(cond,ctrue,cfalse)
210
211A simple (and inefficient) if function.
212
213=item <=,<,==,>=,>,!=
214
215Relational operators (typically used with if()). Since we're working
216with floating point values the equalities are 'near equalities' - an
217epsilon value is used.
218
219=item &&, ||, not(n)
220
221Basic logical operators.
222
223=back
224
225A few examples:
226
227=over
228
229=item rpnexpr=>'x 25 % 15 * y 35 % 10 * getp1 !pat x y getp1 !pix @pix sat 0.7 gt @pat @pix ifp'
230
231tiles a smaller version of the input image over itself where the
232colour has a saturation over 0.7.
233
234=item rpnexpr=>'x 25 % 15 * y 35 % 10 * getp1 !pat y 360 / !rat x y getp1 1 @rat - pmult @pat @rat pmult padd'
235
236tiles the input image over itself so that at the top of the image the
237full-size image is at full strength and at the bottom the tiling is
238most visible.
239
240=item rpnexpr=>'x y getp1 !pix @pix value 0.96 gt @pix sat 0.1 lt and 128 128 255 rgb @pix ifp'
241
242replace pixels that are white or almost white with a palish blue
243
244=item 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'
245
246Tiles the input image overitself where the image isn't white or almost
247white.
248
249=item 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'
250
251Produces a spiral.
252
253=item 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'
254
255A spiral built on top of a colour wheel.
256
257=back
258
259For details on expression parsing see L<Imager::Expr>. For details on
260the virtual machine used to transform the images, see
261L<Imager::regmach.pod>.
262
3799c4d1
TC
263 # generate a colorful spiral
264 # requires that Parse::RecDescent be installed
265 my $newimg = Imager::transform2({
266 width => 160, height=>160,
267 expr => <<EOS
268 dist = distance(x, y, w/2, h/2);
269 angle = atan2(y-h/2, x-w/2);
270 angle2 = (dist / 10 + angle) % ( 2 * pi );
271 return hsv(angle*180/pi, 1, (sin(angle2)+1)/2);
272 EOS
273 });
3e1be2c1 274
e5744e01
TC
275 # replace green portions of an image with another image
276 my $newimg = Imager::transform2({
277 rpnexpr => <<EOS
278 x y getp2 !pat # used to replace green portions
279 x y getp1 !pix # source with "green screen"
280 @pix red 10 lt @pix blue 10 lt && # low blue and red
281 @pix green 254 gt && # and high green
282 @pat @pix ifp
283 EOS
284 }, $source, $background);
285
3e1be2c1
AMH
286=head2 Matrix Transformations
287
288Rather than having to write code in a little language, you can use a
289matrix to perform affine transformations, using the matrix_transform()
290method:
291
292 my $newimg = $img->matrix_transform(matrix=>[ -1, 0, $img->getwidth-1,
293 0, 1, 0,
294 0, 0, 1 ]);
295
296By default the output image will be the same size as the input image,
297but you can supply the xsize and ysize parameters to change the size.
298
299Rather than building matrices by hand you can use the Imager::Matrix2d
300module to build the matrices. This class has methods to allow you to
301scale, shear, rotate, translate and reflect, and you can combine these
302with an overloaded multiplication operator.
303
304WARNING: the matrix you provide in the matrix operator transforms the
305co-ordinates within the B<destination> image to the co-ordinates
306within the I<source> image. This can be confusing.
307
308Since Imager has 3 different fairly general ways of transforming an
309image spatially, this method also has a yatf() alias. Yet Another
310Transformation Function.
311
312=cut