Bilinear interpolation: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|F#|F sharp}}: Regularize header markup to recommended on category page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13: | Line 13: | ||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
{{libheader|Action! Real Math}} |
{{libheader|Action! Real Math}} |
||
< |
<syntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT" |
||
INCLUDE "H6:LOADPPM5.ACT" |
INCLUDE "H6:LOADPPM5.ACT" |
||
Line 120: | Line 120: | ||
DO UNTIL CH#$FF OD |
DO UNTIL CH#$FF OD |
||
CH=$FF |
CH=$FF |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bilinear_interpolation.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bilinear_interpolation.png Screenshot from Atari 8-bit computer] |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang=c>#include <stdint.h> |
||
typedef struct { |
typedef struct { |
||
uint32_t *pixels; |
uint32_t *pixels; |
||
Line 172: | Line 172: | ||
putpixel(dst,x, y, result); |
putpixel(dst,x, y, result); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
Seems to have some artifacting in the output, but the image is at least recognizable. |
Seems to have some artifacting in the output, but the image is at least recognizable. |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
using System.Drawing; |
using System.Drawing; |
||
Line 227: | Line 227: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
This uses the module from the Grayscale Image task. |
This uses the module from the Grayscale Image task. |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=d>import grayscale_image; |
||
/// Currently this accepts only a Grayscale image, for simplicity. |
/// Currently this accepts only a Grayscale image, for simplicity. |
||
Line 280: | Line 280: | ||
im.rescaleGray(0.3, 0.1).savePGM("lena_smaller.pgm"); |
im.rescaleGray(0.3, 0.1).savePGM("lena_smaller.pgm"); |
||
im.rescaleGray(1.3, 1.8).savePGM("lena_larger.pgm"); |
im.rescaleGray(1.3, 1.8).savePGM("lena_larger.pgm"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|F sharp|F#}}== |
=={{header|F sharp|F#}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang=fsharp>open System |
||
open System.Drawing |
open System.Drawing |
||
Line 330: | Line 330: | ||
result.Save("Lenna100_larger.jpg") |
result.Save("Lenna100_larger.jpg") |
||
0 // return an integer exit code</ |
0 // return an integer exit code</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 337: | Line 337: | ||
<code>[https://godoc.org/golang.org/x/image/draw#BiLinear draw.BiLinear]</code> |
<code>[https://godoc.org/golang.org/x/image/draw#BiLinear draw.BiLinear]</code> |
||
from the <code>golang.org/x/image/draw</code> pacakge). |
from the <code>golang.org/x/image/draw</code> pacakge). |
||
< |
<syntaxhighlight lang=Go>package main |
||
import ( |
import ( |
||
Line 429: | Line 429: | ||
} |
} |
||
return err |
return err |
||
}</ |
}</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang=J> |
|||
<lang J> |
|||
Note 'FEA' |
Note 'FEA' |
||
Here we develop a general method to generate isoparametric interpolants. |
Here we develop a general method to generate isoparametric interpolants. |
||
Line 494: | Line 494: | ||
shape_functions =: COEFFICIENTS mp~ shape_function |
shape_functions =: COEFFICIENTS mp~ shape_function |
||
interpolate =: mp shape_functions |
interpolate =: mp shape_functions |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Note 'demonstrate the interpolant with a saddle' |
Note 'demonstrate the interpolant with a saddle' |
||
Line 513: | Line 513: | ||
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 |
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> |
|||
<lang J> |
|||
Note 'Some elemental information' |
Note 'Some elemental information' |
||
Line 609: | Line 609: | ||
i38q =: mp (C38r mp~ n38r) |
i38q =: mp (C38r mp~ n38r) |
||
i38r =: mp (C38r mp~ n38r) |
i38r =: mp (C38r mp~ n38r) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=Java>import javax.imageio.ImageIO; |
||
import java.awt.image.BufferedImage; |
import java.awt.image.BufferedImage; |
||
import java.io.File; |
import java.io.File; |
||
Line 668: | Line 668: | ||
ImageIO.write(image2, "jpg", lenna2); |
ImageIO.write(image2, "jpg", lenna2); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang=julia>using Images, FileIO, Interpolations |
||
function enlarge(A::Matrix, factor::AbstractFloat) |
function enlarge(A::Matrix, factor::AbstractFloat) |
||
Line 684: | Line 684: | ||
Alarge = enlarge(A, 1.6); |
Alarge = enlarge(A, 1.6); |
||
save("data/lennaenlarged.jpg", Alarge) |
save("data/lennaenlarged.jpg", Alarge) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=scala>// version 1.2.21 |
||
import java.io.File |
import java.io.File |
||
Line 737: | Line 737: | ||
val lenna2 = File("Lenna100_larger.jpg") |
val lenna2 = File("Lenna100_larger.jpg") |
||
ImageIO.write(image2, "jpg", lenna2) |
ImageIO.write(image2, "jpg", lenna2) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>ImageResize[Import["http://www.rosettacode.org/mw/title.png"], Scaled[1.6], Resampling -> "Linear"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Shows a downloaded image that is 60% enlarged. |
Shows a downloaded image that is 60% enlarged. |
||
Line 747: | Line 747: | ||
{{trans|F#}} |
{{trans|F#}} |
||
{{libheader|imageman}} |
{{libheader|imageman}} |
||
< |
<syntaxhighlight lang=Nim>import imageman |
||
func lerp(s, e, t: float): float = |
func lerp(s, e, t: float): float = |
||
Line 780: | Line 780: | ||
let image = loadImage[ColorRGBU]("Lenna100.jpg") |
let image = loadImage[ColorRGBU]("Lenna100.jpg") |
||
let newImage = image.scale(1.6, 1.6) |
let newImage = image.scale(1.6, 1.6) |
||
newImage.saveJPEG("Lenna100_bilinear.jpg")</ |
newImage.saveJPEG("Lenna100_bilinear.jpg")</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang=perl>use strict; |
||
use warnings; |
use warnings; |
||
Line 793: | Line 793: | ||
my $image2 = $image->copyScaleInterpolated( 1.6*$width, 1.6*$height ); |
my $image2 = $image->copyScaleInterpolated( 1.6*$width, 1.6*$height ); |
||
$image2->_file('color_wheel_interpolated.png');</ |
$image2->_file('color_wheel_interpolated.png');</syntaxhighlight> |
||
Compare offsite images: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/color_wheel.png color_wheel.png] vs. |
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] |
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/color_wheel_interpolated.png color_wheel_interpolated.png] |
||
Line 800: | Line 800: | ||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
Gui app with slider for between 2 and 200% scaling. Various bits of this code scavenged from C#/Go/Kotlin/Wikipedia. |
Gui app with slider for between 2 and 200% scaling. Various bits of this code scavenged from C#/Go/Kotlin/Wikipedia. |
||
< |
<syntaxhighlight lang=Phix>-- demo\rosetta\Bilinear_interpolation.exw |
||
include pGUI.e |
include pGUI.e |
||
Line 923: | Line 923: | ||
IupMainLoop() |
IupMainLoop() |
||
IupClose()</ |
IupClose()</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 929: | Line 929: | ||
Of course, it is much faster to use PIL, Pillow or SciPy to resize an image than to rely on this code. |
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 |
import numpy as np |
||
from scipy.misc import imread, imshow |
from scipy.misc import imread, imshow |
||
Line 975: | Line 975: | ||
imshow(enlargedImg) |
imshow(enlargedImg) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
This mimics the Wikipedia example. |
This mimics the Wikipedia example. |
||
< |
<syntaxhighlight lang=racket>#lang racket |
||
(require images/flomap) |
(require images/flomap) |
||
Line 1,000: | Line 1,000: | ||
(λ (k x y) |
(λ (k x y) |
||
(flomap-bilinear-ref |
(flomap-bilinear-ref |
||
fm k (+ 1/2 (/ x 250)) (+ 1/2 (/ y 250))))))</ |
fm k (+ 1/2 (/ x 250)) (+ 1/2 (/ y 250))))))</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang=raku line>#!/usr/bin/env perl6 |
||
use v6; |
use v6; |
||
Line 1,034: | Line 1,034: | ||
fclose($fh1); |
fclose($fh1); |
||
fclose($fh2); |
fclose($fh2); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,043: | Line 1,043: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Imperative solution=== |
===Imperative solution=== |
||
< |
<syntaxhighlight lang=Scala>import java.awt.image.BufferedImage |
||
import java.io.{File, IOException} |
import java.io.{File, IOException} |
||
Line 1,101: | Line 1,101: | ||
private def lerp(s: Float, e: Float, t: Float) = s + (e - s) * t |
private def lerp(s: Float, e: Float, t: Float) = s + (e - s) * t |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=ruby>require('Imager') |
||
func scale(img, scaleX, scaleY) { |
func scale(img, scaleX, scaleY) { |
||
Line 1,145: | Line 1,145: | ||
var img = %O<Imager>.new(file => "input.png") |
var img = %O<Imager>.new(file => "input.png") |
||
var out = scale(img, 1.6, 1.6) |
var out = scale(img, 1.6, 1.6) |
||
out.write(file => "output.png")</ |
out.write(file => "output.png")</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 1,153: | Line 1,153: | ||
The script below will show the computed image in a GUI frame, and present a button to save it. |
The script below will show the computed image in a GUI frame, and present a button to save it. |
||
< |
<syntaxhighlight lang=Tcl> |
||
package require Tk |
package require Tk |
||
Line 1,198: | Line 1,198: | ||
pack [button .b -text "save" -command [list save $im]] |
pack [button .b -text "save" -command [list save $im]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang=vbnet>Imports System.Drawing |
||
Module Module1 |
Module Module1 |
||
Line 1,253: | Line 1,253: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 1,259: | Line 1,259: | ||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
Note that currently DOME's ImageData class can only save files to disk in .png format. |
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 "graphics" for Canvas, Color, ImageData |
||
import "math" for Math |
import "math" for Math |
||
Line 1,324: | Line 1,324: | ||
} |
} |
||
var Game = BilinearInterpolation.new("Lenna100.jpg", "Lenna100_larger.png", 1.6, 1.6)</ |
var Game = BilinearInterpolation.new("Lenna100.jpg", "Lenna100_larger.png", 1.6, 1.6)</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Bilinear_interpolation |
||
// Adapted from Nim to Yabasic by Galileo, 01/2022 |
// Adapted from Nim to Yabasic by Galileo, 01/2022 |
||
Line 1,379: | Line 1,379: | ||
dot x, y |
dot x, y |
||
next |
next |
||
next</ |
next</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 1,386: | Line 1,386: | ||
Not fast enough to be called slow. |
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 blerp(c00,c10,c01,c11, tx,ty){ lerp(lerp(c00,c10,tx), lerp(c01,c11,tx),ty) } |
||
fcn scale(src, scaleX,scaleY){ |
fcn scale(src, scaleX,scaleY){ |
||
Line 1,406: | Line 1,406: | ||
} |
} |
||
dst |
dst |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>img:=PPM.readPPMFile("lena.ppm"); |
||
img2:=scale(img,1.5,1.5); |
img2:=scale(img,1.5,1.5); |
||
img2.write(File("lena1.5.ppm","wb")); |
img2.write(File("lena1.5.ppm","wb")); |
||
scale(img,0.5,0.5).write(File("lena.5.ppm","wb"));</ |
scale(img,0.5,0.5).write(File("lena.5.ppm","wb"));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
http://www.zenkinetic.com/Images/RosettaCode/3Lenas.jpg |
http://www.zenkinetic.com/Images/RosettaCode/3Lenas.jpg |