… | |
… | |
99 | its result becomes the argument to the C<scale> function. |
99 | its result becomes the argument to the C<scale> function. |
100 | |
100 | |
101 | Many operators also allow some parameters preceding the input image |
101 | Many operators also allow some parameters preceding the input image |
102 | that modify its behaviour. For example, C<scale> without any additional |
102 | that modify its behaviour. For example, C<scale> without any additional |
103 | arguments scales the image to size of the terminal window. If you specify |
103 | arguments scales the image to size of the terminal window. If you specify |
104 | an additional argument, it uses it as a percentage: |
104 | an additional argument, it uses it as a scale factor (multiply by 100 to |
|
|
105 | get a percentage): |
105 | |
106 | |
106 | scale 200, load "$HOME/mypic.png" |
107 | scale 2, load "$HOME/mypic.png" |
107 | |
108 | |
108 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
109 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
109 | has now two arguments, the C<200> and the C<load> expression, while |
110 | has now two arguments, the C<200> and the C<load> expression, while |
110 | C<load> only has one argument. Arguments are separated from each other by |
111 | C<load> only has one argument. Arguments are separated from each other by |
111 | commas. |
112 | commas. |
112 | |
113 | |
113 | Scale also accepts two arguments, which are then separate factors for both |
114 | Scale also accepts two arguments, which are then separate factors for both |
114 | horizontal and vertical dimensions. For example, this halves the image |
115 | horizontal and vertical dimensions. For example, this halves the image |
115 | width and doubles the image height: |
116 | width and doubles the image height: |
116 | |
117 | |
117 | scale 50, 200, load "$HOME/mypic.png" |
118 | scale 0.5, 2, load "$HOME/mypic.png" |
118 | |
119 | |
119 | Other effects than scalign are also readily available, for exmaple, you can |
120 | Other effects than scalign are also readily available, for exmaple, you can |
120 | tile the image to fill the whole window, instead of resizing it: |
121 | tile the image to fill the whole window, instead of resizing it: |
121 | |
122 | |
122 | tile load "$HOME/mypic.png" |
123 | tile load "$HOME/mypic.png" |
… | |
… | |
202 | our $MIN_INTERVAL = 1/100; |
203 | our $MIN_INTERVAL = 1/100; |
203 | |
204 | |
204 | { |
205 | { |
205 | package urxvt::bgdsl; # background language |
206 | package urxvt::bgdsl; # background language |
206 | |
207 | |
|
|
208 | use List::Util qw(min max sum shuffle); |
|
|
209 | |
207 | =head2 PROVIDERS/GENERATORS |
210 | =head2 PROVIDERS/GENERATORS |
208 | |
211 | |
209 | These functions provide an image, by loading it from disk, grabbing it |
212 | These functions provide an image, by loading it from disk, grabbing it |
210 | from the root screen or by simply generating it. They are used as starting |
213 | from the root screen or by simply generating it. They are used as starting |
211 | points to get an image you can play with. |
214 | points to get an image you can play with. |
… | |
… | |
260 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
263 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
261 | $img->fill ($colour); |
264 | $img->fill ($colour); |
262 | $img |
265 | $img |
263 | } |
266 | } |
264 | |
267 | |
|
|
268 | =item clone $img |
|
|
269 | |
|
|
270 | Returns an exact copy of the image. This is useful if you want to have |
|
|
271 | multiple copies of the same image to apply different effects to. |
|
|
272 | |
|
|
273 | =cut |
|
|
274 | |
|
|
275 | sub clone($) { |
|
|
276 | $_[0]->clone |
|
|
277 | } |
|
|
278 | |
265 | =back |
279 | =back |
266 | |
280 | |
|
|
281 | =head2 TILING MODES |
|
|
282 | |
|
|
283 | The following operators modify the tiling mode of an image, that is, the |
|
|
284 | way that pixels outside the image area are painted when the image is used. |
|
|
285 | |
|
|
286 | =over 4 |
|
|
287 | |
|
|
288 | =item tile $img |
|
|
289 | |
|
|
290 | Tiles the whole plane with the image and returns this new image - or in |
|
|
291 | other words, it returns a copy of the image in plane tiling mode. |
|
|
292 | |
|
|
293 | Example: load an image and tile it over the background, without |
|
|
294 | resizing. The C<tile> call is superfluous because C<load> already defaults |
|
|
295 | to tiling mode. |
|
|
296 | |
|
|
297 | tile load "mybg.png" |
|
|
298 | |
|
|
299 | =item mirror $img |
|
|
300 | |
|
|
301 | Similar to tile, but reflects the image each time it uses a new copy, so |
|
|
302 | that top edges always touch top edges, right edges always touch right |
|
|
303 | edges and so on (with normal tiling, left edges always touch right edges |
|
|
304 | and top always touch bottom edges). |
|
|
305 | |
|
|
306 | Example: load an image and mirror it over the background, avoiding sharp |
|
|
307 | edges at the image borders at the expense of mirroring the image itself |
|
|
308 | |
|
|
309 | mirror load "mybg.png" |
|
|
310 | |
|
|
311 | =item pad $img |
|
|
312 | |
|
|
313 | Takes an image and modifies it so that all pixels outside the image area |
|
|
314 | become transparent. This mode is most useful when you want to place an |
|
|
315 | image over another image or the background colour while leaving all |
|
|
316 | background pixels outside the image unchanged. |
|
|
317 | |
|
|
318 | Example: load an image and display it in the upper left corner. The rest |
|
|
319 | of the space is left "empty" (transparent or wahtever your compisotr does |
|
|
320 | in alpha mode, else background colour). |
|
|
321 | |
|
|
322 | pad load "mybg.png" |
|
|
323 | |
|
|
324 | =item extend $img |
|
|
325 | |
|
|
326 | Extends the image over the whole plane, using the closest pixel in the |
|
|
327 | area outside the image. This mode is mostly useful when you more complex |
|
|
328 | filtering operations and want the pixels outside the image to have the |
|
|
329 | same values as the pixels near the edge. |
|
|
330 | |
|
|
331 | Example: just for curiosity, how does this pixel extension stuff work? |
|
|
332 | |
|
|
333 | extend move 50, 50, load "mybg.png" |
|
|
334 | |
|
|
335 | =cut |
|
|
336 | |
|
|
337 | sub pad($) { |
|
|
338 | my $img = $_[0]->clone; |
|
|
339 | $img->repeat_mode (urxvt::RepeatNone); |
|
|
340 | $img |
|
|
341 | } |
|
|
342 | |
|
|
343 | sub tile($) { |
|
|
344 | my $img = $_[0]->clone; |
|
|
345 | $img->repeat_mode (urxvt::RepeatNormal); |
|
|
346 | $img |
|
|
347 | } |
|
|
348 | |
|
|
349 | sub mirror($) { |
|
|
350 | my $img = $_[0]->clone; |
|
|
351 | $img->repeat_mode (urxvt::RepeatReflect); |
|
|
352 | $img |
|
|
353 | } |
|
|
354 | |
|
|
355 | sub extend($) { |
|
|
356 | my $img = $_[0]->clone; |
|
|
357 | $img->repeat_mode (urxvt::RepeatPad); |
|
|
358 | $img |
|
|
359 | } |
|
|
360 | |
|
|
361 | =back |
|
|
362 | |
267 | =head2 VARIABLES |
363 | =head2 VARIABLE VALUES |
268 | |
364 | |
269 | The following functions provide variable data such as the terminal window |
365 | The following functions provide variable data such as the terminal window |
270 | dimensions. They are not (Perl-) variables, they jsut return stuff that |
366 | dimensions. They are not (Perl-) variables, they just return stuff that |
271 | varies. Most of them make your expression sensitive to some events, for |
367 | varies. Most of them make your expression sensitive to some events, for |
272 | example using C<TW> (terminal width) means your expression is evaluated |
368 | example using C<TW> (terminal width) means your expression is evaluated |
273 | again when the terminal is resized. |
369 | again when the terminal is resized. |
274 | |
370 | |
275 | =over 4 |
371 | =over 4 |
… | |
… | |
349 | $self->{counter} + 0 |
445 | $self->{counter} + 0 |
350 | } |
446 | } |
351 | |
447 | |
352 | =back |
448 | =back |
353 | |
449 | |
354 | =head2 TILING MODES |
450 | =head2 SHAPE CHANGING OPERATORS |
355 | |
451 | |
356 | The following operators modify the tiling mode of an image, that is, the |
452 | The following operators modify the shape, size or position of the image. |
357 | way that pixels outside the image area are painted when the image is used. |
|
|
358 | |
453 | |
359 | =over 4 |
454 | =over 4 |
360 | |
|
|
361 | =item tile $img |
|
|
362 | |
|
|
363 | Tiles the whole plane with the image and returns this new image - or in |
|
|
364 | other words, it returns a copy of the image in plane tiling mode. |
|
|
365 | |
|
|
366 | Example: load an image and tile it over the background, without |
|
|
367 | resizing. The C<tile> call is superfluous because C<load> already defaults |
|
|
368 | to tiling mode. |
|
|
369 | |
|
|
370 | tile load "mybg.png" |
|
|
371 | |
|
|
372 | =item mirror $img |
|
|
373 | |
|
|
374 | Similar to tile, but reflects the image each time it uses a new copy, so |
|
|
375 | that top edges always touch top edges, right edges always touch right |
|
|
376 | edges and so on (with normal tiling, left edges always touch right edges |
|
|
377 | and top always touch bottom edges). |
|
|
378 | |
|
|
379 | Example: load an image and mirror it over the background, avoiding sharp |
|
|
380 | edges at the image borders at the expense of mirroring the image itself |
|
|
381 | |
|
|
382 | mirror load "mybg.png" |
|
|
383 | |
|
|
384 | =item pad $img |
|
|
385 | |
|
|
386 | Takes an image and modifies it so that all pixels outside the image area |
|
|
387 | become transparent. This mode is most useful when you want to place an |
|
|
388 | image over another image or the background colour while leaving all |
|
|
389 | background pixels outside the image unchanged. |
|
|
390 | |
|
|
391 | Example: load an image and display it in the upper left corner. The rest |
|
|
392 | of the space is left "empty" (transparent or wahtever your compisotr does |
|
|
393 | in alpha mode, else background colour). |
|
|
394 | |
|
|
395 | pad load "mybg.png" |
|
|
396 | |
|
|
397 | =item extend $img |
|
|
398 | |
|
|
399 | Extends the image over the whole plane, using the closest pixel in the |
|
|
400 | area outside the image. This mode is mostly useful when you more complex |
|
|
401 | filtering operations and want the pixels outside the image to have the |
|
|
402 | same values as the pixels near the edge. |
|
|
403 | |
|
|
404 | Example: just for curiosity, how does this pixel extension stuff work? |
|
|
405 | |
|
|
406 | extend move 50, 50, load "mybg.png" |
|
|
407 | |
|
|
408 | =cut |
|
|
409 | |
|
|
410 | sub pad($) { |
|
|
411 | my $img = $_[0]->clone; |
|
|
412 | $img->repeat_mode (urxvt::RepeatNone); |
|
|
413 | $img |
|
|
414 | } |
|
|
415 | |
|
|
416 | sub tile($) { |
|
|
417 | my $img = $_[0]->clone; |
|
|
418 | $img->repeat_mode (urxvt::RepeatNormal); |
|
|
419 | $img |
|
|
420 | } |
|
|
421 | |
|
|
422 | sub mirror($) { |
|
|
423 | my $img = $_[0]->clone; |
|
|
424 | $img->repeat_mode (urxvt::RepeatReflect); |
|
|
425 | $img |
|
|
426 | } |
|
|
427 | |
|
|
428 | sub extend($) { |
|
|
429 | my $img = $_[0]->clone; |
|
|
430 | $img->repeat_mode (urxvt::RepeatPad); |
|
|
431 | $img |
|
|
432 | } |
|
|
433 | |
|
|
434 | =back |
|
|
435 | |
|
|
436 | =head2 PIXEL OPERATORS |
|
|
437 | |
|
|
438 | The following operators modify the image pixels in various ways. |
|
|
439 | |
|
|
440 | =over 4 |
|
|
441 | |
|
|
442 | =item clone $img |
|
|
443 | |
|
|
444 | Returns an exact copy of the image. |
|
|
445 | |
|
|
446 | =cut |
|
|
447 | |
|
|
448 | sub clone($) { |
|
|
449 | $_[0]->clone |
|
|
450 | } |
|
|
451 | |
455 | |
452 | =item clip $img |
456 | =item clip $img |
453 | |
457 | |
454 | =item clip $width, $height, $img |
458 | =item clip $width, $height, $img |
455 | |
459 | |
… | |
… | |
479 | $img->sub_rect ($_[0], $_[1], $w, $h) |
483 | $img->sub_rect ($_[0], $_[1], $w, $h) |
480 | } |
484 | } |
481 | |
485 | |
482 | =item scale $img |
486 | =item scale $img |
483 | |
487 | |
484 | =item scale $size_percent, $img |
488 | =item scale $size_factor, $img |
485 | |
489 | |
486 | =item scale $width_percent, $height_percent, $img |
490 | =item scale $width_factor, $height_factor, $img |
487 | |
491 | |
488 | Scales the image by the given percentages in horizontal |
492 | Scales the image by the given factors in horizontal |
489 | (C<$width_percent>) and vertical (C<$height_percent>) direction. |
493 | (C<$width>) and vertical (C<$height>) direction. |
490 | |
494 | |
491 | If only one percentage is give, it is used for both directions. |
495 | If only one factor is give, it is used for both directions. |
492 | |
496 | |
493 | If no percentages are given, scales the image to the window size without |
497 | If no factors are given, scales the image to the window size without |
494 | keeping aspect. |
498 | keeping aspect. |
495 | |
499 | |
496 | =item resize $width, $height, $img |
500 | =item resize $width, $height, $img |
497 | |
501 | |
498 | Resizes the image to exactly C<$width> times C<$height> pixels. |
502 | Resizes the image to exactly C<$width> times C<$height> pixels. |
499 | |
503 | |
500 | =cut |
504 | =item fit $img |
501 | |
505 | |
502 | #TODO: maximise, maximise_fill? |
506 | =item fit $width, $height, $img |
|
|
507 | |
|
|
508 | Fits the image into the given C<$width> and C<$height> without changing |
|
|
509 | aspect, or the terminal size. That means it will be shrunk or grown until |
|
|
510 | the whole image fits into the given area, possibly leaving borders. |
|
|
511 | |
|
|
512 | =item cover $img |
|
|
513 | |
|
|
514 | =item cover $width, $height, $img |
|
|
515 | |
|
|
516 | Similar to C<fit>, but shrinks or grows until all of the area is covered |
|
|
517 | by the image, so instead of potentially leaving borders, it will cut off |
|
|
518 | image data that doesn't fit. |
|
|
519 | |
|
|
520 | =cut |
503 | |
521 | |
504 | sub scale($;$;$) { |
522 | sub scale($;$;$) { |
505 | my $img = pop; |
523 | my $img = pop; |
506 | |
524 | |
507 | @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) |
525 | @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h) |
508 | : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) |
526 | : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h) |
509 | : $img->scale (TW, TH) |
527 | : $img->scale (TW, TH) |
510 | } |
528 | } |
511 | |
529 | |
512 | sub resize($$$) { |
530 | sub resize($$$) { |
513 | my $img = pop; |
531 | my $img = pop; |
514 | $img->scale ($_[0], $_[1]) |
532 | $img->scale ($_[0], $_[1]) |
|
|
533 | } |
|
|
534 | |
|
|
535 | sub fit($;$$) { |
|
|
536 | my $img = pop; |
|
|
537 | my $w = ($_[0] || TW) / $img->w; |
|
|
538 | my $h = ($_[1] || TH) / $img->h; |
|
|
539 | scale +(min $w, $h), $img |
|
|
540 | } |
|
|
541 | |
|
|
542 | sub cover($;$$) { |
|
|
543 | my $img = pop; |
|
|
544 | my $w = ($_[0] || TW) / $img->w; |
|
|
545 | my $h = ($_[1] || TH) / $img->h; |
|
|
546 | scale +(max $w, $h), $img |
515 | } |
547 | } |
516 | |
548 | |
517 | =item move $dx, $dy, $img |
549 | =item move $dx, $dy, $img |
518 | |
550 | |
519 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
551 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
520 | the vertical. |
552 | the vertical. |
521 | |
553 | |
522 | Example: move the image right by 20 pixels and down by 30. |
554 | Example: move the image right by 20 pixels and down by 30. |
523 | |
555 | |
524 | move 20, 30, ... |
556 | move 20, 30, ... |
|
|
557 | |
|
|
558 | =item center $img |
|
|
559 | |
|
|
560 | =item center $width, $height, $img |
|
|
561 | |
|
|
562 | Centers the image, i.e. the center of the image is moved to the center of |
|
|
563 | the terminal window (or the box specified by C<$width> and C<$height> if |
|
|
564 | given). |
525 | |
565 | |
526 | =item rootalign $img |
566 | =item rootalign $img |
527 | |
567 | |
528 | Moves the image so that it appears glued to the screen as opposed to the |
568 | Moves the image so that it appears glued to the screen as opposed to the |
529 | window. This gives the illusion of a larger area behind the window. It is |
569 | window. This gives the illusion of a larger area behind the window. It is |
… | |
… | |
545 | my $img = pop->clone; |
585 | my $img = pop->clone; |
546 | $img->move ($_[0], $_[1]); |
586 | $img->move ($_[0], $_[1]); |
547 | $img |
587 | $img |
548 | } |
588 | } |
549 | |
589 | |
|
|
590 | sub center($;$$) { |
|
|
591 | my $img = pop; |
|
|
592 | my $w = $_[0] || TW; |
|
|
593 | my $h = $_[0] || TH; |
|
|
594 | |
|
|
595 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
|
|
596 | } |
|
|
597 | |
550 | sub rootalign($) { |
598 | sub rootalign($) { |
551 | move -TX, -TY, $_[0] |
599 | move -TX, -TY, $_[0] |
552 | } |
600 | } |
553 | |
601 | |
|
|
602 | =back |
|
|
603 | |
|
|
604 | =head2 COLOUR MODIFICATIONS |
|
|
605 | |
|
|
606 | The following operators change the pixels of the image. |
|
|
607 | |
|
|
608 | =over 4 |
|
|
609 | |
554 | =item contrast $factor, $img |
610 | =item contrast $factor, $img |
555 | |
611 | |
556 | =item contrast $r, $g, $b, $img |
612 | =item contrast $r, $g, $b, $img |
557 | |
613 | |
558 | =item contrast $r, $g, $b, $a, $img |
614 | =item contrast $r, $g, $b, $a, $img |
559 | |
615 | |
560 | Adjusts the I<contrast> of an image. |
616 | Adjusts the I<contrast> of an image. |
561 | |
617 | |
562 | #TODO# |
618 | The first form applies a single C<$factor> to red, green and blue, the |
|
|
619 | second form applies separate factors to each colour channel, and the last |
|
|
620 | form includes the alpha channel. |
563 | |
621 | |
|
|
622 | Values from 0 to 1 lower the contrast, values higher than 1 increase the |
|
|
623 | contrast. |
|
|
624 | |
|
|
625 | Due to limitations in the underlying XRender extension, lowering contrast |
|
|
626 | also reduces brightness, while increasing contrast currently also |
|
|
627 | increases brightness. |
|
|
628 | |
564 | =item brightness $factor, $img |
629 | =item brightness $bias, $img |
565 | |
630 | |
566 | =item brightness $r, $g, $b, $img |
631 | =item brightness $r, $g, $b, $img |
567 | |
632 | |
568 | =item brightness $r, $g, $b, $a, $img |
633 | =item brightness $r, $g, $b, $a, $img |
569 | |
634 | |
570 | Adjusts the brightness of an image. |
635 | Adjusts the brightness of an image. |
|
|
636 | |
|
|
637 | The first form applies a single C<$bias> to red, green and blue, the |
|
|
638 | second form applies separate biases to each colour channel, and the last |
|
|
639 | form includes the alpha channel. |
|
|
640 | |
|
|
641 | Values less than 0 reduce brightness, while values larger than 0 increase |
|
|
642 | it. Useful range is from -1 to 1 - the former results in a black, the |
|
|
643 | latter in a white picture. |
|
|
644 | |
|
|
645 | Due to idiosynchrasies in the underlying XRender extension, biases less |
|
|
646 | than zero can be I<very> slow. |
571 | |
647 | |
572 | =cut |
648 | =cut |
573 | |
649 | |
574 | sub contrast($$;$$;$) { |
650 | sub contrast($$;$$;$) { |
575 | my $img = pop; |
651 | my $img = pop; |
… | |
… | |
615 | } |
691 | } |
616 | |
692 | |
617 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
693 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
618 | |
694 | |
619 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
695 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
620 | pointer at C<$center_x> and C<$center_y> (specified as percentage of image |
696 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
621 | width/height), generating a new image with width C<$new_width> and height |
697 | width/height), generating a new image with width C<$new_width> and height |
622 | C<$new_height>. |
698 | C<$new_height>. |
623 | |
699 | |
624 | #TODO# new width, height, maybe more operators? |
700 | #TODO# new width, height, maybe more operators? |
625 | |
701 | |
… | |
… | |
630 | sub rotate($$$$$$) { |
706 | sub rotate($$$$$$) { |
631 | my $img = pop; |
707 | my $img = pop; |
632 | $img->rotate ( |
708 | $img->rotate ( |
633 | $_[0], |
709 | $_[0], |
634 | $_[1], |
710 | $_[1], |
635 | $_[2] * $img->w * .01, |
711 | $_[2] * $img->w, |
636 | $_[3] * $img->h * .01, |
712 | $_[3] * $img->h, |
637 | $_[4] * (3.14159265 / 180), |
713 | $_[4] * (3.14159265 / 180), |
638 | ) |
714 | ) |
639 | } |
715 | } |
640 | |
716 | |
641 | =back |
717 | =back |