-
Notifications
You must be signed in to change notification settings - Fork 33
/
nearest_neighbor_interpolation.pl
executable file
·49 lines (36 loc) · 1.36 KB
/
nearest_neighbor_interpolation.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 27 July 2018
# https://github.com/trizen
# A simple implementation of the nearest-neighbor interpolation algorithm for scaling up an image.
# See also:
# https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
use 5.020;
use strict;
use warnings;
use Imager;
use experimental qw(signatures);
sub nearest_neighbor_interpolation ($file, $zoom = 2) {
my $img = Imager->new(file => $file)
or die Imager->errstr();
my $width = $img->getwidth;
my $height = $img->getheight;
my $out_img = Imager->new(xsize => $zoom * $width,
ysize => $zoom * $height);
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my $pixel = $img->getpixel(x => $x, y => $y);
#<<<
# Fill the gaps
$out_img->setpixel(x => $zoom * $x, y => $zoom * $y, color => $pixel);
$out_img->setpixel(x => $zoom * $x + 1, y => $zoom * $y + 1, color => $pixel);
$out_img->setpixel(x => $zoom * $x + 1, y => $zoom * $y, color => $pixel);
$out_img->setpixel(x => $zoom * $x, y => $zoom * $y + 1, color => $pixel);
#>>>
}
}
return $out_img;
}
my $file = shift(@ARGV) // die "usage: $0 [image]\n";
my $img = nearest_neighbor_interpolation($file, 2);
$img->write(file => "output.png");