-
Notifications
You must be signed in to change notification settings - Fork 34
/
fractal_frame_transparent.pl
executable file
·79 lines (55 loc) · 1.58 KB
/
fractal_frame_transparent.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 27 January 2018
# https://github.com/trizen
# Adds a transparent Mandelbrot-like fractal frame around the edges of an image.
use 5.020;
use strict;
use warnings;
use feature qw(lexical_subs);
use experimental qw(signatures);
use Imager;
use Math::GComplex qw(cplx);
sub complex_transform ($file) {
my $img = Imager->new(file => $file);
my $width = $img->getwidth;
my $height = $img->getheight;
my $max_iter = 10;
my sub mandelbrot ($x, $y) {
my $z = cplx(
(2 * $x - $width) / $width,
(2 * $y - $height) / $height,
);
my $c = $z;
my $i = $max_iter;
while (abs($z) < 2 and --$i) {
$z = $z->pown(5) + $c;
}
($max_iter - $i) / $max_iter;
}
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my $i = mandelbrot($x, $y);
my $pixel = $img->getpixel(x => $x, y => $y);
my ($red, $green, $blue, $alpha) = $pixel->rgba();
$red *= $i;
$green *= $i;
$blue *= $i;
$alpha *= $i;
$pixel->set($red, $green, $blue, $alpha);
$img->setpixel(
x => $x,
y => $y,
color => $pixel,
);
}
}
return $img;
}
sub usage {
die "usage: $0 [input image] [output image]\n";
}
my $input = shift(@ARGV) // usage();
my $output = shift(@ARGV) // 'fractal_frame.png';
complex_transform($input)->write(file => $output);