Bilinear interpolation: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 7:
Open an image file, enlarge it by 60% using bilinear interpolation, then either display the result or save the result to a file.
<br><br>
 
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
Line 13 ⟶ 12:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:REALMATH.ACT"
INCLUDE "H6:LOADPPM5.ACT"
 
Line 123 ⟶ 122:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bilinear_interpolation.png Screenshot from Atari 8-bit computer]
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdint.h>
typedef struct {
uint32_t *pixels;
Line 173 ⟶ 171:
}
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
Seems to have some artifacting in the output, but the image is at least recognizable.
<syntaxhighlight lang="csharp">using System;
using System.Drawing;
 
Line 228 ⟶ 225:
}
}</syntaxhighlight>
 
=={{header|D}}==
This uses the module from the Grayscale Image task.
{{trans|C}}
<syntaxhighlight lang="d">import grayscale_image;
 
/// Currently this accepts only a Grayscale image, for simplicity.
Line 281 ⟶ 277:
im.rescaleGray(1.3, 1.8).savePGM("lena_larger.pgm");
}</syntaxhighlight>
 
=={{header|F sharp|F#}}==
{{trans|C#}}
<syntaxhighlight lang="fsharp">open System
open System.Drawing
 
Line 331 ⟶ 326:
 
0 // return an integer exit code</syntaxhighlight>
 
=={{header|Go}}==
{{trans|C}}
Line 337 ⟶ 331:
<code>[https://godoc.org/golang.org/x/image/draw#BiLinear draw.BiLinear]</code>
from the <code>golang.org/x/image/draw</code> pacakge).
<syntaxhighlight lang=Go"go">package main
 
import (
Line 430 ⟶ 424:
return err
}</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang=J"j">
Note 'FEA'
Here we develop a general method to generate isoparametric interpolants.
Line 513 ⟶ 506:
 
Let n mean shape function, C mean constants, i mean interpolant, and the three digits meaning dimensionality, number of corners, and (in base 36) the number of nodes we construct various linear and quadratic interpolants in 1, 2, and 3 dimensions as
<syntaxhighlight lang=J"j">
Note 'Some elemental information'
 
Line 610 ⟶ 603:
i38r =: mp (C38r mp~ n38r)
</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang=Java"java">import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
Line 669 ⟶ 661:
}
}</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Images, FileIO, Interpolations
function enlarge(A::Matrix, factor::AbstractFloat)
Line 685 ⟶ 676:
save("data/lennaenlarged.jpg", Alarge)
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|C}}
<syntaxhighlight lang="scala">// version 1.2.21
 
import java.io.File
Line 738 ⟶ 728:
ImageIO.write(image2, "jpg", lenna2)
}</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ImageResize[Import["http://www.rosettacode.org/mw/title.png"], Scaled[1.6], Resampling -> "Linear"]</syntaxhighlight>
{{out}}
Shows a downloaded image that is 60% enlarged.
 
=={{header|Nim}}==
{{trans|F#}}
{{libheader|imageman}}
<syntaxhighlight lang=Nim"nim">import imageman
 
func lerp(s, e, t: float): float =
Line 781 ⟶ 769:
let newImage = image.scale(1.6, 1.6)
newImage.saveJPEG("Lenna100_bilinear.jpg")</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use strict;
use warnings;
 
Line 796 ⟶ 783:
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/color_wheel.png color_wheel.png] vs.
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/color_wheel_interpolated.png color_wheel_interpolated.png]
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Gui app with slider for between 2 and 200% scaling. Various bits of this code scavenged from C#/Go/Kotlin/Wikipedia.
<syntaxhighlight lang=Phix"phix">-- demo\rosetta\Bilinear_interpolation.exw
include pGUI.e
 
Line 924 ⟶ 910:
IupMainLoop()
IupClose()</syntaxhighlight>
 
=={{header|Python}}==
 
Of course, it is much faster to use PIL, Pillow or SciPy to resize an image than to rely on this code.
 
<syntaxhighlight lang="python">#!/bin/python
import numpy as np
from scipy.misc import imread, imshow
Line 976 ⟶ 961:
imshow(enlargedImg)
</syntaxhighlight>
 
=={{header|Racket}}==
This mimics the Wikipedia example.
<syntaxhighlight lang="racket">#lang racket
(require images/flomap)
 
Line 1,001 ⟶ 985:
(flomap-bilinear-ref
fm k (+ 1/2 (/ x 250)) (+ 1/2 (/ y 250))))))</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>#!/usr/bin/env perl6
 
use v6;
Line 1,040 ⟶ 1,023:
Lenna100.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, baseline, precision 8, 512x512, frames 3
Lenna100-larger.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 96x96, segment length 16, comment: "CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality", baseline, precision 8, 820x820, frames 3</pre>
 
=={{header|Scala}}==
===Imperative solution===
<syntaxhighlight lang=Scala"scala">import java.awt.image.BufferedImage
import java.io.{File, IOException}
 
Line 1,102 ⟶ 1,084:
private def lerp(s: Float, e: Float, t: Float) = s + (e - s) * t
}</syntaxhighlight>
 
=={{header|Sidef}}==
{{trans|C}}
<syntaxhighlight lang="ruby">require('Imager')
 
func scale(img, scaleX, scaleY) {
Line 1,146 ⟶ 1,127:
var out = scale(img, 1.6, 1.6)
out.write(file => "output.png")</syntaxhighlight>
 
=={{header|Tcl}}==
 
Line 1,153 ⟶ 1,133:
The script below will show the computed image in a GUI frame, and present a button to save it.
 
<syntaxhighlight lang=Tcl"tcl">
package require Tk
 
Line 1,199 ⟶ 1,179:
 
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Drawing
 
Module Module1
Line 1,254 ⟶ 1,233:
 
End Module</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
Note that currently DOME's ImageData class can only save files to disk in .png format.
<syntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color, ImageData
import "math" for Math
Line 1,325 ⟶ 1,303:
 
var Game = BilinearInterpolation.new("Lenna100.jpg", "Lenna100_larger.png", 1.6, 1.6)</syntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|Nim}}
<syntaxhighlight lang=Yabasic"yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Bilinear_interpolation
// Adapted from Nim to Yabasic by Galileo, 01/2022
 
Line 1,380 ⟶ 1,357:
next
next</syntaxhighlight>
 
=={{header|zkl}}==
{{trans|C}}
Line 1,386 ⟶ 1,362:
 
Not fast enough to be called slow.
<syntaxhighlight lang="zkl">fcn lerp(s,e,t){ s + (e-s)*t; }
fcn blerp(c00,c10,c01,c11, tx,ty){ lerp(lerp(c00,c10,tx), lerp(c01,c11,tx),ty) }
fcn scale(src, scaleX,scaleY){
Line 1,407 ⟶ 1,383:
dst
}</syntaxhighlight>
<syntaxhighlight lang="zkl">img:=PPM.readPPMFile("lena.ppm");
img2:=scale(img,1.5,1.5);
img2.write(File("lena1.5.ppm","wb"));
10,327

edits