Bilinear interpolation: Difference between revisions
Content added Content deleted
(Tcl implementation added) |
|||
Line 306: | Line 306: | ||
(flomap-bilinear-ref |
(flomap-bilinear-ref |
||
fm k (+ 1/2 (/ x 250)) (+ 1/2 (/ y 250))))))</lang> |
fm k (+ 1/2 (/ x 250)) (+ 1/2 (/ y 250))))))</lang> |
||
=={{header|Tcl}}== |
|||
This uses the polynomial expansion described in wikipedia, and draws the same example as illustrated in that page with a different pallette. It's not particularly fast - about 300ms for a 200x200 surface on an arbitrary machine. |
|||
The script below will show the computed image in a GUI frame, and present a button to save it. |
|||
<lang Tcl> |
|||
package require Tk |
|||
proc pixel {f} { |
|||
if {$f < 0} { |
|||
error "why is $f?" |
|||
} |
|||
set i [expr {0xff & entier(0xff*$f)}] |
|||
format #%02x%02x%02x $i [expr {255-$i}] 127 |
|||
} |
|||
proc bilerp {im O X Y XY} { |
|||
set w [image width $im] |
|||
set h [image height $im] |
|||
set dx [expr {1.0/$w}] |
|||
set dy [expr {1.0/$h}] |
|||
set a0 $O |
|||
set a1 [expr {$X - $O}] |
|||
set a2 [expr {$Y - $O}] |
|||
set a3 [expr {$O + $XY - ($X + $Y)}] |
|||
for {set y 0} {$y < $h} {incr y} { |
|||
for {set x 0} {$x < $w} {incr x} { |
|||
set i [expr {$x * $dx}] |
|||
set j [expr {$y * $dy}] |
|||
set xv [expr {$a0 + $a1*$i + $a2*$j + $a3*$i*$j}] |
|||
set y [expr {$h - $y}] ;# invert for screen coords |
|||
$im put [pixel $xv] -to $x $y |
|||
} |
|||
} |
|||
} |
|||
proc save {im} { |
|||
set fn [tk_getSaveFile -defaultextension png] |
|||
if {$fn eq ""} return |
|||
set fd [open $fn wb] |
|||
puts -nonewline $fd [$im data -format png] |
|||
close $fd |
|||
tk_messageBox -message "Saved as $fn!" |
|||
} |
|||
set im [image create photo -width 200 -height 200] |
|||
puts [time {bilerp $im 0 1 1 0.5} 1] |
|||
pack [label .l1 -image $im] |
|||
pack [button .b -text "save" -command [list save $im]] |
|||
</lang> |