draw2d/draw2dimg/rgba_interpolation.go

152 lines
5.1 KiB
Go
Raw Normal View History

2012-04-17 09:03:56 +00:00
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 21/11/2010 by Laurent Le Goff
2011-04-27 08:06:14 +00:00
// see http://pippin.gimp.org/image_processing/chap_resampling.html
2012-04-17 09:03:56 +00:00
package draw2dimg
2011-04-27 08:06:14 +00:00
import (
"image"
2012-01-13 09:14:12 +00:00
"image/color"
"image/draw"
2011-04-27 08:06:14 +00:00
"math"
2015-07-09 16:06:14 +00:00
"github.com/llgcode/draw2d"
2011-04-27 08:06:14 +00:00
)
2015-07-09 16:06:14 +00:00
// ImageFilter defines the type of filter to use
2011-04-27 08:06:14 +00:00
type ImageFilter int
const (
2015-07-09 16:06:14 +00:00
// LinearFilter defines a linear filter
2011-04-27 08:06:14 +00:00
LinearFilter ImageFilter = iota
2015-07-09 16:06:14 +00:00
// BilinearFilter defines a bilinear filter
2011-04-27 08:06:14 +00:00
BilinearFilter
2015-07-09 16:06:14 +00:00
// BicubicFilter defines a bicubic filter
2011-04-27 08:06:14 +00:00
BicubicFilter
2015-07-09 16:06:14 +00:00
// M is the maximum value for a rgb component
M = 1<<16 - 1
2011-04-27 08:06:14 +00:00
)
//see http://pippin.gimp.org/image_processing/chap_resampling.html
2012-01-13 09:14:12 +00:00
func getColorLinear(img image.Image, x, y float64) color.Color {
2011-04-27 08:06:14 +00:00
return img.At(int(x), int(y))
}
2012-01-13 09:14:12 +00:00
func getColorBilinear(img image.Image, x, y float64) color.Color {
2011-04-27 08:06:14 +00:00
x0 := math.Floor(x)
y0 := math.Floor(y)
dx := x - x0
dy := y - y0
rt, gt, bt, at := img.At(int(x0), int(y0)).RGBA()
r0, g0, b0, a0 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = img.At(int(x0+1), int(y0)).RGBA()
r1, g1, b1, a1 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = img.At(int(x0+1), int(y0+1)).RGBA()
r2, g2, b2, a2 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = img.At(int(x0), int(y0+1)).RGBA()
r3, g3, b3, a3 := float64(rt), float64(gt), float64(bt), float64(at)
r := int(lerp(lerp(r0, r1, dx), lerp(r3, r2, dx), dy))
g := int(lerp(lerp(g0, g1, dx), lerp(g3, g2, dx), dy))
b := int(lerp(lerp(b0, b1, dx), lerp(b3, b2, dx), dy))
a := int(lerp(lerp(a0, a1, dx), lerp(a3, a2, dx), dy))
2012-01-13 09:14:12 +00:00
return color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
2011-04-27 08:06:14 +00:00
}
2012-01-13 09:14:12 +00:00
2015-07-09 16:06:14 +00:00
// lerp is a linear interpolation bertween 2 points
2011-04-27 08:06:14 +00:00
func lerp(v1, v2, ratio float64) float64 {
return v1*(1-ratio) + v2*ratio
}
2012-01-13 09:14:12 +00:00
func getColorCubicRow(img image.Image, x, y, offset float64) color.Color {
2011-04-27 08:06:14 +00:00
c0 := img.At(int(x), int(y))
c1 := img.At(int(x+1), int(y))
c2 := img.At(int(x+2), int(y))
c3 := img.At(int(x+3), int(y))
rt, gt, bt, at := c0.RGBA()
r0, g0, b0, a0 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c1.RGBA()
r1, g1, b1, a1 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c2.RGBA()
r2, g2, b2, a2 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c3.RGBA()
r3, g3, b3, a3 := float64(rt), float64(gt), float64(bt), float64(at)
r, g, b, a := cubic(offset, r0, r1, r2, r3), cubic(offset, g0, g1, g2, g3), cubic(offset, b0, b1, b2, b3), cubic(offset, a0, a1, a2, a3)
2012-01-13 09:14:12 +00:00
return color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
2011-04-27 08:06:14 +00:00
}
2012-01-13 09:14:12 +00:00
func getColorBicubic(img image.Image, x, y float64) color.Color {
2011-04-27 08:06:14 +00:00
x0 := math.Floor(x)
y0 := math.Floor(y)
dx := x - x0
dy := y - y0
c0 := getColorCubicRow(img, x0-1, y0-1, dx)
c1 := getColorCubicRow(img, x0-1, y0, dx)
c2 := getColorCubicRow(img, x0-1, y0+1, dx)
c3 := getColorCubicRow(img, x0-1, y0+2, dx)
rt, gt, bt, at := c0.RGBA()
r0, g0, b0, a0 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c1.RGBA()
r1, g1, b1, a1 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c2.RGBA()
r2, g2, b2, a2 := float64(rt), float64(gt), float64(bt), float64(at)
rt, gt, bt, at = c3.RGBA()
r3, g3, b3, a3 := float64(rt), float64(gt), float64(bt), float64(at)
r, g, b, a := cubic(dy, r0, r1, r2, r3), cubic(dy, g0, g1, g2, g3), cubic(dy, b0, b1, b2, b3), cubic(dy, a0, a1, a2, a3)
2012-01-13 09:14:12 +00:00
return color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
2011-04-27 08:06:14 +00:00
}
func cubic(offset, v0, v1, v2, v3 float64) uint32 {
// offset is the offset of the sampled value between v1 and v2
return uint32(((((-7*v0+21*v1-21*v2+7*v3)*offset+
(15*v0-36*v1+27*v2-6*v3))*offset+
(-9*v0+9*v2))*offset + (v0 + 16*v1 + v2)) / 18.0)
}
2015-07-09 16:06:14 +00:00
// DrawImage draws an image into dest using an affine transformation matrix, an op and a filter
2015-04-30 12:11:23 +00:00
func DrawImage(src image.Image, dest draw.Image, tr draw2d.Matrix, op draw.Op, filter ImageFilter) {
2011-04-27 08:06:14 +00:00
bounds := src.Bounds()
2015-04-29 14:19:49 +00:00
x0, y0, x1, y1 := tr.TransformRectangle(float64(bounds.Min.X), float64(bounds.Min.Y), float64(bounds.Max.X), float64(bounds.Max.Y))
2011-04-27 08:06:14 +00:00
var x, y, u, v float64
2012-01-13 09:14:12 +00:00
var c1, c2, cr color.Color
2011-04-27 08:06:14 +00:00
var r, g, b, a, ia, r1, g1, b1, a1, r2, g2, b2, a2 uint32
2012-01-13 09:14:12 +00:00
var color color.RGBA
2011-04-27 08:06:14 +00:00
for x = x0; x < x1; x++ {
for y = y0; y < y1; y++ {
u = x
v = y
2015-04-29 14:19:49 +00:00
u, v = tr.InverseTransformPoint(u, v)
2012-05-28 07:52:49 +00:00
if bounds.Min.X <= int(u) && bounds.Max.X > int(u) && bounds.Min.Y <= int(v) && bounds.Max.Y > int(v) {
2012-04-24 08:28:31 +00:00
c1 = dest.At(int(x), int(y))
switch filter {
case LinearFilter:
c2 = src.At(int(u), int(v))
case BilinearFilter:
c2 = getColorBilinear(src, u, v)
case BicubicFilter:
c2 = getColorBicubic(src, u, v)
}
switch op {
case draw.Over:
r1, g1, b1, a1 = c1.RGBA()
r2, g2, b2, a2 = c2.RGBA()
ia = M - a2
r = ((r1 * ia) / M) + r2
g = ((g1 * ia) / M) + g2
b = ((b1 * ia) / M) + b2
a = ((a1 * ia) / M) + a2
color.R = uint8(r >> 8)
color.G = uint8(g >> 8)
color.B = uint8(b >> 8)
color.A = uint8(a >> 8)
cr = color
default:
cr = c2
}
dest.Set(int(x), int(y), cr)
2011-04-27 08:06:14 +00:00
}
}
}
}