fractal_frame.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 25 January 2018
  5. # https://github.com/trizen
  6. # Adds a Mandelbrot-like fractal frame around the edges of an image.
  7. use 5.020;
  8. use strict;
  9. use warnings;
  10. use feature qw(lexical_subs);
  11. use experimental qw(signatures);
  12. use Imager;
  13. use Math::GComplex qw(cplx);
  14. sub complex_transform ($file) {
  15. my $img = Imager->new(file => $file);
  16. my $black = Imager::Color->new('#000000');
  17. my $width = $img->getwidth;
  18. my $height = $img->getheight;
  19. my sub mandelbrot ($x, $y) {
  20. my $z = cplx(
  21. (2 * $x - $width) / $width,
  22. (2 * $y - $height) / $height,
  23. );
  24. my $c = $z;
  25. my $i = 10;
  26. while (abs($z) < 2 and --$i) {
  27. $z = $z->pown(5) + $c;
  28. }
  29. return $i;
  30. }
  31. foreach my $y (0 .. $height - 1) {
  32. foreach my $x (0 .. $width - 1) {
  33. next if (mandelbrot($x, $y) == 0);
  34. $img->setpixel(
  35. x => $x,
  36. y => $y,
  37. color => $black,
  38. );
  39. }
  40. }
  41. return $img;
  42. }
  43. sub usage {
  44. die "usage: $0 [input image] [output image]\n";
  45. }
  46. my $input = shift(@ARGV) // usage();
  47. my $output = shift(@ARGV) // 'fractal_frame.png';
  48. complex_transform($input)->write(file => $output);