2015-02-20 08:28:54 +01:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var debug = flag.Bool("debug", false, "")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
w := new(bytes.Buffer)
|
|
|
|
w.WriteString("// generated by \"go run gen.go\". DO NOT EDIT.\n\n" +
|
2015-03-05 05:46:57 +01:00
|
|
|
"package draw\n\nimport (\n" +
|
|
|
|
"\"image\"\n" +
|
|
|
|
"\"image/color\"\n" +
|
2015-03-13 07:44:34 +01:00
|
|
|
"\"math\"\n" +
|
2015-03-05 05:46:57 +01:00
|
|
|
"\n" +
|
|
|
|
"\"golang.org/x/image/math/f64\"\n" +
|
|
|
|
")\n")
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
gen(w, "nnInterpolator", codeNNScaleLeaf, codeNNTransformLeaf)
|
|
|
|
gen(w, "ablInterpolator", codeABLScaleLeaf, codeABLTransformLeaf)
|
2015-02-20 08:28:54 +01:00
|
|
|
genKernel(w)
|
|
|
|
|
|
|
|
if *debug {
|
|
|
|
os.Stdout.Write(w.Bytes())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err := format.Source(w.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile("impl.go", out, 0660); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
var (
|
|
|
|
// dsTypes are the (dst image type, src image type) pairs to generate
|
|
|
|
// scale_DType_SType implementations for. The last element in the slice
|
|
|
|
// should be the fallback pair ("Image", "image.Image").
|
|
|
|
//
|
|
|
|
// TODO: add *image.CMYK src type after Go 1.5 is released.
|
2015-04-08 12:47:13 +02:00
|
|
|
// An *image.CMYK is also alwaysOpaque.
|
2015-02-24 05:28:16 +01:00
|
|
|
dsTypes = []struct{ dType, sType string }{
|
2015-02-28 06:05:58 +01:00
|
|
|
{"*image.RGBA", "*image.Gray"},
|
2015-02-24 05:28:16 +01:00
|
|
|
{"*image.RGBA", "*image.NRGBA"},
|
|
|
|
{"*image.RGBA", "*image.RGBA"},
|
|
|
|
{"*image.RGBA", "*image.YCbCr"},
|
|
|
|
{"*image.RGBA", "image.Image"},
|
|
|
|
{"Image", "image.Image"},
|
|
|
|
}
|
2015-03-19 01:04:55 +01:00
|
|
|
dTypes, sTypes []string
|
|
|
|
sTypesForDType = map[string][]string{}
|
|
|
|
subsampleRatios = []string{
|
|
|
|
"444",
|
|
|
|
"422",
|
|
|
|
"420",
|
|
|
|
"440",
|
|
|
|
}
|
2015-04-07 08:22:05 +02:00
|
|
|
ops = []string{"Over", "Src"}
|
2015-04-08 12:47:13 +02:00
|
|
|
// alwaysOpaque are those image.Image implementations that are always
|
|
|
|
// opaque. For these types, Over is equivalent to the faster Src, in the
|
|
|
|
// absence of a source mask.
|
|
|
|
alwaysOpaque = map[string]bool{
|
|
|
|
"*image.Gray": true,
|
|
|
|
"*image.YCbCr": true,
|
|
|
|
}
|
2015-02-24 05:28:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dTypesSeen := map[string]bool{}
|
|
|
|
sTypesSeen := map[string]bool{}
|
|
|
|
for _, t := range dsTypes {
|
|
|
|
if !sTypesSeen[t.sType] {
|
|
|
|
sTypesSeen[t.sType] = true
|
|
|
|
sTypes = append(sTypes, t.sType)
|
|
|
|
}
|
|
|
|
if !dTypesSeen[t.dType] {
|
|
|
|
dTypesSeen[t.dType] = true
|
|
|
|
dTypes = append(dTypes, t.dType)
|
|
|
|
}
|
|
|
|
sTypesForDType[t.dType] = append(sTypesForDType[t.dType], t.sType)
|
|
|
|
}
|
|
|
|
sTypesForDType["anyDType"] = sTypes
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
dType string
|
|
|
|
sType string
|
2015-03-19 01:04:55 +01:00
|
|
|
sratio string
|
2015-02-20 08:28:54 +01:00
|
|
|
receiver string
|
2015-04-07 03:22:25 +02:00
|
|
|
op string
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
func gen(w *bytes.Buffer, receiver string, codes ...string) {
|
2015-02-20 08:28:54 +01:00
|
|
|
expn(w, codeRoot, &data{receiver: receiver})
|
2015-03-13 07:44:34 +01:00
|
|
|
for _, code := range codes {
|
|
|
|
for _, t := range dsTypes {
|
2015-04-07 03:22:25 +02:00
|
|
|
for _, op := range ops {
|
2015-04-08 12:47:13 +02:00
|
|
|
if op == "Over" && alwaysOpaque[t.sType] {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-07 03:22:25 +02:00
|
|
|
expn(w, code, &data{
|
|
|
|
dType: t.dType,
|
|
|
|
sType: t.sType,
|
|
|
|
receiver: receiver,
|
|
|
|
op: op,
|
|
|
|
})
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genKernel(w *bytes.Buffer) {
|
|
|
|
expn(w, codeKernelRoot, &data{})
|
2015-02-24 05:28:16 +01:00
|
|
|
for _, sType := range sTypes {
|
2015-03-05 05:46:57 +01:00
|
|
|
expn(w, codeKernelScaleLeafX, &data{
|
2015-02-24 05:28:16 +01:00
|
|
|
sType: sType,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, dType := range dTypes {
|
2015-04-07 03:22:25 +02:00
|
|
|
for _, op := range ops {
|
|
|
|
expn(w, codeKernelScaleLeafY, &data{
|
|
|
|
dType: dType,
|
|
|
|
op: op,
|
|
|
|
})
|
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
for _, t := range dsTypes {
|
2015-04-07 03:22:25 +02:00
|
|
|
for _, op := range ops {
|
2015-04-08 12:47:13 +02:00
|
|
|
if op == "Over" && alwaysOpaque[t.sType] {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-07 03:22:25 +02:00
|
|
|
expn(w, codeKernelTransformLeaf, &data{
|
|
|
|
dType: t.dType,
|
|
|
|
sType: t.sType,
|
|
|
|
op: op,
|
|
|
|
})
|
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func expn(w *bytes.Buffer, code string, d *data) {
|
2015-03-19 01:04:55 +01:00
|
|
|
if d.sType == "*image.YCbCr" && d.sratio == "" {
|
|
|
|
for _, sratio := range subsampleRatios {
|
|
|
|
e := *d
|
|
|
|
e.sratio = sratio
|
|
|
|
expn(w, code, &e)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
for _, line := range strings.Split(code, "\n") {
|
2015-02-27 06:18:34 +01:00
|
|
|
line = expnLine(line, d)
|
|
|
|
if line == ";" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, line)
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
func expnLine(line string, d *data) string {
|
|
|
|
for {
|
|
|
|
i := strings.IndexByte(line, '$')
|
|
|
|
if i < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
prefix, s := line[:i], line[i+1:]
|
2015-02-20 08:28:54 +01:00
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
i = len(s)
|
|
|
|
for j, c := range s {
|
|
|
|
if !('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z') {
|
|
|
|
i = j
|
|
|
|
break
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 05:28:16 +01:00
|
|
|
dollar, suffix := s[:i], s[i:]
|
|
|
|
|
|
|
|
e := expnDollar(prefix, dollar, suffix, d)
|
|
|
|
if e == "" {
|
|
|
|
log.Fatalf("couldn't expand %q", line)
|
|
|
|
}
|
|
|
|
line = e
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-02-24 05:28:16 +01:00
|
|
|
return line
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 08:46:52 +01:00
|
|
|
// expnDollar expands a "$foo" fragment in a line of generated code. It returns
|
|
|
|
// the empty string if there was a problem. It returns ";" if the generated
|
|
|
|
// code is a no-op.
|
2015-02-24 05:28:16 +01:00
|
|
|
func expnDollar(prefix, dollar, suffix string, d *data) string {
|
2015-02-20 08:28:54 +01:00
|
|
|
switch dollar {
|
|
|
|
case "dType":
|
|
|
|
return prefix + d.dType + suffix
|
|
|
|
case "dTypeRN":
|
|
|
|
return prefix + relName(d.dType) + suffix
|
2015-03-19 01:04:55 +01:00
|
|
|
case "sratio":
|
|
|
|
return prefix + d.sratio + suffix
|
2015-02-20 08:28:54 +01:00
|
|
|
case "sType":
|
|
|
|
return prefix + d.sType + suffix
|
|
|
|
case "sTypeRN":
|
|
|
|
return prefix + relName(d.sType) + suffix
|
|
|
|
case "receiver":
|
|
|
|
return prefix + d.receiver + suffix
|
2015-04-07 03:22:25 +02:00
|
|
|
case "op":
|
|
|
|
return prefix + d.op + suffix
|
2015-02-20 08:28:54 +01:00
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
case "switch":
|
2015-04-07 03:22:25 +02:00
|
|
|
return expnSwitch("", "", true, suffix)
|
2015-02-24 05:28:16 +01:00
|
|
|
case "switchD":
|
2015-04-07 03:22:25 +02:00
|
|
|
return expnSwitch("", "", false, suffix)
|
2015-02-24 05:28:16 +01:00
|
|
|
case "switchS":
|
2015-04-07 03:22:25 +02:00
|
|
|
return expnSwitch("", "anyDType", false, suffix)
|
2015-02-24 05:28:16 +01:00
|
|
|
|
2015-02-27 06:18:34 +01:00
|
|
|
case "preOuter":
|
|
|
|
switch d.dType {
|
|
|
|
default:
|
|
|
|
return ";"
|
|
|
|
case "Image":
|
2015-04-24 06:45:24 +02:00
|
|
|
s := ""
|
|
|
|
if d.sType == "image.Image" {
|
|
|
|
s = "srcMask, smp := opts.SrcMask, opts.SrcMaskP\n"
|
|
|
|
}
|
|
|
|
return s +
|
2015-04-21 06:26:44 +02:00
|
|
|
"dstMask, dmp := opts.DstMask, opts.DstMaskP\n" +
|
2015-02-27 06:18:34 +01:00
|
|
|
"dstColorRGBA64 := &color.RGBA64{}\n" +
|
2015-02-20 08:28:54 +01:00
|
|
|
"dstColor := color.Color(dstColorRGBA64)"
|
|
|
|
}
|
2015-02-27 06:18:34 +01:00
|
|
|
|
|
|
|
case "preInner":
|
|
|
|
switch d.dType {
|
|
|
|
default:
|
|
|
|
return ";"
|
2015-03-19 10:25:07 +01:00
|
|
|
case "*image.RGBA":
|
2015-03-20 06:36:51 +01:00
|
|
|
return "d := " + pixOffset("dst", "dr.Min.X+adr.Min.X", "dr.Min.Y+int(dy)", "*4", "*dst.Stride")
|
2015-02-27 06:18:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-24 06:45:24 +02:00
|
|
|
case "preKernelOuter":
|
|
|
|
switch d.sType {
|
|
|
|
default:
|
|
|
|
return ";"
|
|
|
|
case "image.Image":
|
|
|
|
return "srcMask, smp := opts.SrcMask, opts.SrcMaskP"
|
|
|
|
}
|
|
|
|
|
2015-02-27 06:18:34 +01:00
|
|
|
case "preKernelInner":
|
|
|
|
switch d.dType {
|
|
|
|
default:
|
|
|
|
return ";"
|
2015-03-19 10:25:07 +01:00
|
|
|
case "*image.RGBA":
|
2015-03-20 06:36:51 +01:00
|
|
|
return "d := " + pixOffset("dst", "dr.Min.X+int(dx)", "dr.Min.Y+adr.Min.Y", "*4", "*dst.Stride")
|
2015-02-27 06:18:34 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
|
|
|
|
case "blend":
|
|
|
|
args, _ := splitArgs(suffix)
|
|
|
|
if len(args) != 4 {
|
|
|
|
return ""
|
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
switch d.sType {
|
|
|
|
default:
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
|
|
|
"$3r = $0*$1r + $2*$3r\n"+
|
|
|
|
"$3g = $0*$1g + $2*$3g\n"+
|
|
|
|
"$3b = $0*$1b + $2*$3b\n"+
|
|
|
|
"$3a = $0*$1a + $2*$3a",
|
2015-03-18 04:29:15 +01:00
|
|
|
)
|
|
|
|
case "*image.Gray":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
|
|
|
"$3r = $0*$1r + $2*$3r",
|
2015-03-18 04:29:15 +01:00
|
|
|
)
|
2015-03-20 00:14:03 +01:00
|
|
|
case "*image.YCbCr":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
|
|
|
"$3r = $0*$1r + $2*$3r\n"+
|
|
|
|
"$3g = $0*$1g + $2*$3g\n"+
|
|
|
|
"$3b = $0*$1b + $2*$3b",
|
2015-03-20 00:14:03 +01:00
|
|
|
)
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
|
2015-04-10 05:05:20 +02:00
|
|
|
case "clampToAlpha":
|
|
|
|
if alwaysOpaque[d.sType] {
|
|
|
|
return ";"
|
|
|
|
}
|
|
|
|
// Go uses alpha-premultiplied color. The naive computation can lead to
|
|
|
|
// invalid colors, e.g. red > alpha, when some weights are negative.
|
|
|
|
return `
|
|
|
|
if pr > pa {
|
|
|
|
pr = pa
|
|
|
|
}
|
|
|
|
if pg > pa {
|
|
|
|
pg = pa
|
|
|
|
}
|
|
|
|
if pb > pa {
|
|
|
|
pb = pa
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-04-17 09:57:53 +02:00
|
|
|
case "convFtou":
|
|
|
|
args, _ := splitArgs(suffix)
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch d.sType {
|
|
|
|
default:
|
|
|
|
return argf(args, ""+
|
|
|
|
"$0r := uint32($1r)\n"+
|
|
|
|
"$0g := uint32($1g)\n"+
|
|
|
|
"$0b := uint32($1b)\n"+
|
|
|
|
"$0a := uint32($1a)",
|
|
|
|
)
|
|
|
|
case "*image.Gray":
|
|
|
|
return argf(args, ""+
|
|
|
|
"$0r := uint32($1r)",
|
|
|
|
)
|
|
|
|
case "*image.YCbCr":
|
|
|
|
return argf(args, ""+
|
|
|
|
"$0r := uint32($1r)\n"+
|
|
|
|
"$0g := uint32($1g)\n"+
|
|
|
|
"$0b := uint32($1b)",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
case "outputu":
|
|
|
|
args, _ := splitArgs(suffix)
|
|
|
|
if len(args) != 3 {
|
|
|
|
return ""
|
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
switch d.op {
|
|
|
|
case "Over":
|
|
|
|
switch d.dType {
|
2015-03-18 04:29:15 +01:00
|
|
|
default:
|
2015-04-10 11:58:30 +02:00
|
|
|
log.Fatalf("bad dType %q", d.dType)
|
|
|
|
case "Image":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
|
|
|
"qr, qg, qb, qa := dst.At($0, $1).RGBA()\n"+
|
2015-04-21 06:26:44 +02:00
|
|
|
"if dstMask != nil {\n"+
|
|
|
|
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
|
|
|
|
" $2r = $2r * ma / 0xffff\n"+
|
|
|
|
" $2g = $2g * ma / 0xffff\n"+
|
|
|
|
" $2b = $2b * ma / 0xffff\n"+
|
|
|
|
" $2a = $2a * ma / 0xffff\n"+
|
|
|
|
"}\n"+
|
2015-04-17 09:57:53 +02:00
|
|
|
"$2a1 := 0xffff - $2a\n"+
|
|
|
|
"dstColorRGBA64.R = uint16(qr*$2a1/0xffff + $2r)\n"+
|
|
|
|
"dstColorRGBA64.G = uint16(qg*$2a1/0xffff + $2g)\n"+
|
|
|
|
"dstColorRGBA64.B = uint16(qb*$2a1/0xffff + $2b)\n"+
|
|
|
|
"dstColorRGBA64.A = uint16(qa*$2a1/0xffff + $2a)\n"+
|
2015-04-14 02:26:02 +02:00
|
|
|
"dst.Set($0, $1, dstColor)",
|
2015-03-18 04:29:15 +01:00
|
|
|
)
|
2015-04-10 11:58:30 +02:00
|
|
|
case "*image.RGBA":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
2015-04-17 09:57:53 +02:00
|
|
|
"$2a1 := (0xffff - $2a) * 0x101\n"+
|
|
|
|
"dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*$2a1/0xffff + $2r) >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*$2a1/0xffff + $2g) >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*$2a1/0xffff + $2b) >> 8)\n"+
|
|
|
|
"dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*$2a1/0xffff + $2a) >> 8)",
|
2015-03-20 00:14:03 +01:00
|
|
|
)
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
case "Src":
|
|
|
|
switch d.dType {
|
2015-03-18 04:29:15 +01:00
|
|
|
default:
|
2015-04-10 11:58:30 +02:00
|
|
|
log.Fatalf("bad dType %q", d.dType)
|
|
|
|
case "Image":
|
2015-04-22 06:25:58 +02:00
|
|
|
return argf(args, ""+
|
|
|
|
"if dstMask != nil {\n"+
|
|
|
|
" qr, qg, qb, qa := dst.At($0, $1).RGBA()\n"+
|
|
|
|
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
|
|
|
|
" pr = pr * ma / 0xffff\n"+
|
|
|
|
" pg = pg * ma / 0xffff\n"+
|
|
|
|
" pb = pb * ma / 0xffff\n"+
|
|
|
|
" pa = pa * ma / 0xffff\n"+
|
|
|
|
" $2a1 := 0xffff - ma\n"+ // Note that this is ma, not $2a.
|
|
|
|
" dstColorRGBA64.R = uint16(qr*$2a1/0xffff + $2r)\n"+
|
|
|
|
" dstColorRGBA64.G = uint16(qg*$2a1/0xffff + $2g)\n"+
|
|
|
|
" dstColorRGBA64.B = uint16(qb*$2a1/0xffff + $2b)\n"+
|
|
|
|
" dstColorRGBA64.A = uint16(qa*$2a1/0xffff + $2a)\n"+
|
|
|
|
" dst.Set($0, $1, dstColor)\n"+
|
|
|
|
"} else {\n"+
|
|
|
|
" dstColorRGBA64.R = uint16($2r)\n"+
|
|
|
|
" dstColorRGBA64.G = uint16($2g)\n"+
|
|
|
|
" dstColorRGBA64.B = uint16($2b)\n"+
|
|
|
|
" dstColorRGBA64.A = uint16($2a)\n"+
|
|
|
|
" dst.Set($0, $1, dstColor)\n"+
|
|
|
|
"}",
|
|
|
|
)
|
2015-04-10 11:58:30 +02:00
|
|
|
case "*image.RGBA":
|
|
|
|
switch d.sType {
|
|
|
|
default:
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
2015-04-17 09:57:53 +02:00
|
|
|
"dst.Pix[d+0] = uint8($2r >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8($2g >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8($2b >> 8)\n"+
|
|
|
|
"dst.Pix[d+3] = uint8($2a >> 8)",
|
2015-04-10 11:58:30 +02:00
|
|
|
)
|
|
|
|
case "*image.Gray":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
2015-04-17 09:57:53 +02:00
|
|
|
"out := uint8($2r >> 8)\n"+
|
2015-04-10 11:58:30 +02:00
|
|
|
"dst.Pix[d+0] = out\n"+
|
|
|
|
"dst.Pix[d+1] = out\n"+
|
|
|
|
"dst.Pix[d+2] = out\n"+
|
|
|
|
"dst.Pix[d+3] = 0xff",
|
|
|
|
)
|
|
|
|
case "*image.YCbCr":
|
2015-04-14 02:26:02 +02:00
|
|
|
return argf(args, ""+
|
2015-04-17 09:57:53 +02:00
|
|
|
"dst.Pix[d+0] = uint8($2r >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8($2g >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8($2b >> 8)\n"+
|
2015-04-10 11:58:30 +02:00
|
|
|
"dst.Pix[d+3] = 0xff",
|
|
|
|
)
|
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case "outputf":
|
|
|
|
args, _ := splitArgs(suffix)
|
2015-03-17 08:46:52 +01:00
|
|
|
if len(args) != 5 {
|
2015-02-20 08:28:54 +01:00
|
|
|
return ""
|
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
ret := ""
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
switch d.op {
|
|
|
|
case "Over":
|
|
|
|
switch d.dType {
|
2015-03-18 04:29:15 +01:00
|
|
|
default:
|
2015-04-10 11:58:30 +02:00
|
|
|
log.Fatalf("bad dType %q", d.dType)
|
|
|
|
case "Image":
|
2015-04-14 02:26:02 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"qr, qg, qb, qa := dst.At($0, $1).RGBA()\n"+
|
|
|
|
"$3r0 := uint32($2($3r * $4))\n"+
|
|
|
|
"$3g0 := uint32($2($3g * $4))\n"+
|
|
|
|
"$3b0 := uint32($2($3b * $4))\n"+
|
|
|
|
"$3a0 := uint32($2($3a * $4))\n"+
|
2015-04-21 06:26:44 +02:00
|
|
|
"if dstMask != nil {\n"+
|
|
|
|
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
|
|
|
|
" $3r0 = $3r0 * ma / 0xffff\n"+
|
|
|
|
" $3g0 = $3g0 * ma / 0xffff\n"+
|
|
|
|
" $3b0 = $3b0 * ma / 0xffff\n"+
|
|
|
|
" $3a0 = $3a0 * ma / 0xffff\n"+
|
|
|
|
"}\n"+
|
2015-04-14 02:26:02 +02:00
|
|
|
"$3a1 := 0xffff - $3a0\n"+
|
|
|
|
"dstColorRGBA64.R = uint16(qr*$3a1/0xffff + $3r0)\n"+
|
|
|
|
"dstColorRGBA64.G = uint16(qg*$3a1/0xffff + $3g0)\n"+
|
|
|
|
"dstColorRGBA64.B = uint16(qb*$3a1/0xffff + $3b0)\n"+
|
|
|
|
"dstColorRGBA64.A = uint16(qa*$3a1/0xffff + $3a0)\n"+
|
|
|
|
"dst.Set($0, $1, dstColor)",
|
2015-03-18 04:29:15 +01:00
|
|
|
)
|
2015-04-10 11:58:30 +02:00
|
|
|
case "*image.RGBA":
|
2015-04-14 02:26:02 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"$3r0 := uint32($2($3r * $4))\n"+
|
|
|
|
"$3g0 := uint32($2($3g * $4))\n"+
|
|
|
|
"$3b0 := uint32($2($3b * $4))\n"+
|
|
|
|
"$3a0 := uint32($2($3a * $4))\n"+
|
|
|
|
"$3a1 := (0xffff - uint32($3a0)) * 0x101\n"+
|
|
|
|
"dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*$3a1/0xffff + $3r0) >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*$3a1/0xffff + $3g0) >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*$3a1/0xffff + $3b0) >> 8)\n"+
|
|
|
|
"dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*$3a1/0xffff + $3a0) >> 8)",
|
2015-03-20 00:14:03 +01:00
|
|
|
)
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
case "Src":
|
|
|
|
switch d.dType {
|
2015-03-18 04:29:15 +01:00
|
|
|
default:
|
2015-04-10 11:58:30 +02:00
|
|
|
log.Fatalf("bad dType %q", d.dType)
|
|
|
|
case "Image":
|
2015-04-22 06:25:58 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"if dstMask != nil {\n"+
|
|
|
|
" qr, qg, qb, qa := dst.At($0, $1).RGBA()\n"+
|
|
|
|
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
|
|
|
|
" pr := uint32($2($3r * $4)) * ma / 0xffff\n"+
|
|
|
|
" pg := uint32($2($3g * $4)) * ma / 0xffff\n"+
|
|
|
|
" pb := uint32($2($3b * $4)) * ma / 0xffff\n"+
|
|
|
|
" pa := uint32($2($3a * $4)) * ma / 0xffff\n"+
|
|
|
|
" pa1 := 0xffff - ma\n"+ // Note that this is ma, not pa.
|
|
|
|
" dstColorRGBA64.R = uint16(qr*pa1/0xffff + pr)\n"+
|
|
|
|
" dstColorRGBA64.G = uint16(qg*pa1/0xffff + pg)\n"+
|
|
|
|
" dstColorRGBA64.B = uint16(qb*pa1/0xffff + pb)\n"+
|
|
|
|
" dstColorRGBA64.A = uint16(qa*pa1/0xffff + pa)\n"+
|
|
|
|
" dst.Set($0, $1, dstColor)\n"+
|
|
|
|
"} else {\n"+
|
|
|
|
" dstColorRGBA64.R = $2($3r * $4)\n"+
|
|
|
|
" dstColorRGBA64.G = $2($3g * $4)\n"+
|
|
|
|
" dstColorRGBA64.B = $2($3b * $4)\n"+
|
|
|
|
" dstColorRGBA64.A = $2($3a * $4)\n"+
|
|
|
|
" dst.Set($0, $1, dstColor)\n"+
|
|
|
|
"}",
|
|
|
|
)
|
2015-04-10 11:58:30 +02:00
|
|
|
case "*image.RGBA":
|
|
|
|
switch d.sType {
|
|
|
|
default:
|
2015-04-14 02:26:02 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"dst.Pix[d+0] = uint8($2($3r * $4) >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8($2($3g * $4) >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8($2($3b * $4) >> 8)\n"+
|
|
|
|
"dst.Pix[d+3] = uint8($2($3a * $4) >> 8)",
|
2015-04-10 11:58:30 +02:00
|
|
|
)
|
|
|
|
case "*image.Gray":
|
2015-04-14 02:26:02 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"out := uint8($2($3r * $4) >> 8)\n"+
|
2015-04-10 11:58:30 +02:00
|
|
|
"dst.Pix[d+0] = out\n"+
|
|
|
|
"dst.Pix[d+1] = out\n"+
|
|
|
|
"dst.Pix[d+2] = out\n"+
|
|
|
|
"dst.Pix[d+3] = 0xff",
|
|
|
|
)
|
|
|
|
case "*image.YCbCr":
|
2015-04-14 02:26:02 +02:00
|
|
|
ret = argf(args, ""+
|
|
|
|
"dst.Pix[d+0] = uint8($2($3r * $4) >> 8)\n"+
|
|
|
|
"dst.Pix[d+1] = uint8($2($3g * $4) >> 8)\n"+
|
|
|
|
"dst.Pix[d+2] = uint8($2($3b * $4) >> 8)\n"+
|
2015-04-10 11:58:30 +02:00
|
|
|
"dst.Pix[d+3] = 0xff",
|
|
|
|
)
|
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
|
2015-03-17 08:46:52 +01:00
|
|
|
return strings.Replace(ret, " * 1)", ")", -1)
|
2015-02-20 08:28:54 +01:00
|
|
|
|
|
|
|
case "srcf", "srcu":
|
|
|
|
lhs, eqOp := splitEq(prefix)
|
|
|
|
if lhs == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
args, extra := splitArgs(suffix)
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := ""
|
|
|
|
if dollar == "srcf" {
|
|
|
|
tmp = "u"
|
|
|
|
}
|
|
|
|
|
2015-03-20 00:14:03 +01:00
|
|
|
// TODO: there's no need to multiply by 0x101 in the switch below if
|
|
|
|
// the next thing we're going to do is shift right by 8.
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
switch d.sType {
|
|
|
|
default:
|
|
|
|
log.Fatalf("bad sType %q", d.sType)
|
2015-03-20 12:13:15 +01:00
|
|
|
case "image.Image":
|
2015-03-20 00:14:03 +01:00
|
|
|
fmt.Fprintf(buf, ""+
|
|
|
|
"%sr%s, %sg%s, %sb%s, %sa%s := src.At(%s, %s).RGBA()\n",
|
|
|
|
lhs, tmp, lhs, tmp, lhs, tmp, lhs, tmp, args[0], args[1],
|
2015-02-27 11:26:47 +01:00
|
|
|
)
|
2015-04-24 06:45:24 +02:00
|
|
|
if d.dType == "" || d.dType == "Image" {
|
|
|
|
fmt.Fprintf(buf, ""+
|
|
|
|
"if srcMask != nil {\n"+
|
|
|
|
" _, _, _, ma := srcMask.At(smp.X+%s, smp.Y+%s).RGBA()\n"+
|
|
|
|
" %sr%s = %sr%s * ma / 0xffff\n"+
|
|
|
|
" %sg%s = %sg%s * ma / 0xffff\n"+
|
|
|
|
" %sb%s = %sb%s * ma / 0xffff\n"+
|
|
|
|
" %sa%s = %sa%s * ma / 0xffff\n"+
|
|
|
|
"}\n",
|
|
|
|
args[0], args[1],
|
|
|
|
lhs, tmp, lhs, tmp,
|
|
|
|
lhs, tmp, lhs, tmp,
|
|
|
|
lhs, tmp, lhs, tmp,
|
|
|
|
lhs, tmp, lhs, tmp,
|
|
|
|
)
|
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
case "*image.Gray":
|
2015-03-20 00:14:03 +01:00
|
|
|
fmt.Fprintf(buf, ""+
|
2015-03-20 06:36:51 +01:00
|
|
|
"%si := %s\n"+
|
2015-03-18 04:29:15 +01:00
|
|
|
"%sr%s := uint32(src.Pix[%si]) * 0x101\n",
|
2015-03-20 06:36:51 +01:00
|
|
|
lhs, pixOffset("src", args[0], args[1], "", "*src.Stride"),
|
2015-03-18 04:29:15 +01:00
|
|
|
lhs, tmp, lhs,
|
|
|
|
)
|
2015-03-19 10:25:07 +01:00
|
|
|
case "*image.NRGBA":
|
2015-03-20 00:14:03 +01:00
|
|
|
fmt.Fprintf(buf, ""+
|
2015-03-20 06:36:51 +01:00
|
|
|
"%si := %s\n"+
|
2015-03-19 10:25:07 +01:00
|
|
|
"%sa%s := uint32(src.Pix[%si+3]) * 0x101\n"+
|
|
|
|
"%sr%s := uint32(src.Pix[%si+0]) * %sa%s / 0xff\n"+
|
|
|
|
"%sg%s := uint32(src.Pix[%si+1]) * %sa%s / 0xff\n"+
|
|
|
|
"%sb%s := uint32(src.Pix[%si+2]) * %sa%s / 0xff\n",
|
2015-03-20 06:36:51 +01:00
|
|
|
lhs, pixOffset("src", args[0], args[1], "*4", "*src.Stride"),
|
2015-03-19 10:25:07 +01:00
|
|
|
lhs, tmp, lhs,
|
|
|
|
lhs, tmp, lhs, lhs, tmp,
|
|
|
|
lhs, tmp, lhs, lhs, tmp,
|
|
|
|
lhs, tmp, lhs, lhs, tmp,
|
|
|
|
)
|
2015-02-27 11:26:47 +01:00
|
|
|
case "*image.RGBA":
|
2015-03-20 00:14:03 +01:00
|
|
|
fmt.Fprintf(buf, ""+
|
2015-03-20 06:36:51 +01:00
|
|
|
"%si := %s\n"+
|
2015-02-27 11:26:47 +01:00
|
|
|
"%sr%s := uint32(src.Pix[%si+0]) * 0x101\n"+
|
|
|
|
"%sg%s := uint32(src.Pix[%si+1]) * 0x101\n"+
|
|
|
|
"%sb%s := uint32(src.Pix[%si+2]) * 0x101\n"+
|
|
|
|
"%sa%s := uint32(src.Pix[%si+3]) * 0x101\n",
|
2015-03-20 06:36:51 +01:00
|
|
|
lhs, pixOffset("src", args[0], args[1], "*4", "*src.Stride"),
|
2015-02-27 11:26:47 +01:00
|
|
|
lhs, tmp, lhs,
|
|
|
|
lhs, tmp, lhs,
|
|
|
|
lhs, tmp, lhs,
|
|
|
|
lhs, tmp, lhs,
|
|
|
|
)
|
2015-03-20 00:14:03 +01:00
|
|
|
case "*image.YCbCr":
|
|
|
|
fmt.Fprintf(buf, ""+
|
2015-03-20 06:36:51 +01:00
|
|
|
"%si := %s\n"+
|
|
|
|
"%sj := %s\n"+
|
2015-03-26 09:33:18 +01:00
|
|
|
"%s\n",
|
2015-03-20 06:36:51 +01:00
|
|
|
lhs, pixOffset("src", args[0], args[1], "", "*src.YStride"),
|
|
|
|
lhs, cOffset(args[0], args[1], d.sratio),
|
2015-03-26 09:33:18 +01:00
|
|
|
ycbcrToRGB(lhs, tmp),
|
2015-03-20 00:14:03 +01:00
|
|
|
)
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if dollar == "srcf" {
|
2015-03-18 04:29:15 +01:00
|
|
|
switch d.sType {
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(buf, ""+
|
|
|
|
"%sr %s float64(%sru)%s\n"+
|
|
|
|
"%sg %s float64(%sgu)%s\n"+
|
|
|
|
"%sb %s float64(%sbu)%s\n"+
|
|
|
|
"%sa %s float64(%sau)%s\n",
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
)
|
|
|
|
case "*image.Gray":
|
|
|
|
fmt.Fprintf(buf, ""+
|
|
|
|
"%sr %s float64(%sru)%s\n",
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
)
|
2015-03-20 00:14:03 +01:00
|
|
|
case "*image.YCbCr":
|
|
|
|
fmt.Fprintf(buf, ""+
|
|
|
|
"%sr %s float64(%sru)%s\n"+
|
|
|
|
"%sg %s float64(%sgu)%s\n"+
|
|
|
|
"%sb %s float64(%sbu)%s\n",
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
lhs, eqOp, lhs, extra,
|
|
|
|
)
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(buf.String())
|
2015-02-27 06:18:34 +01:00
|
|
|
|
2015-03-17 08:46:52 +01:00
|
|
|
case "tweakD":
|
|
|
|
if d.dType == "*image.RGBA" {
|
|
|
|
return "d += dst.Stride"
|
|
|
|
}
|
|
|
|
return ";"
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
case "tweakDx":
|
|
|
|
if d.dType == "*image.RGBA" {
|
2015-03-18 07:08:50 +01:00
|
|
|
return strings.Replace(prefix, "dx++", "dx, d = dx+1, d+4", 1)
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
2015-03-18 07:08:50 +01:00
|
|
|
return prefix
|
2015-03-13 07:44:34 +01:00
|
|
|
|
2015-02-27 06:18:34 +01:00
|
|
|
case "tweakDy":
|
|
|
|
if d.dType == "*image.RGBA" {
|
2015-03-18 07:08:50 +01:00
|
|
|
return strings.Replace(prefix, "for dy, s", "for _, s", 1)
|
2015-02-27 06:18:34 +01:00
|
|
|
}
|
2015-03-18 07:08:50 +01:00
|
|
|
return prefix
|
2015-03-18 04:29:15 +01:00
|
|
|
|
|
|
|
case "tweakP":
|
2015-03-20 00:14:03 +01:00
|
|
|
switch d.sType {
|
|
|
|
case "*image.Gray":
|
2015-03-18 07:08:50 +01:00
|
|
|
if strings.HasPrefix(strings.TrimSpace(prefix), "pa * ") {
|
2015-03-18 04:29:15 +01:00
|
|
|
return "1,"
|
|
|
|
}
|
|
|
|
return "pr,"
|
2015-03-20 00:14:03 +01:00
|
|
|
case "*image.YCbCr":
|
|
|
|
if strings.HasPrefix(strings.TrimSpace(prefix), "pa * ") {
|
|
|
|
return "1,"
|
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-03-18 07:08:50 +01:00
|
|
|
return prefix
|
2015-03-18 04:29:15 +01:00
|
|
|
|
|
|
|
case "tweakPr":
|
|
|
|
if d.sType == "*image.Gray" {
|
|
|
|
return "pr *= s.invTotalWeightFFFF"
|
|
|
|
}
|
|
|
|
return ";"
|
|
|
|
|
|
|
|
case "tweakVarP":
|
2015-03-20 00:14:03 +01:00
|
|
|
switch d.sType {
|
|
|
|
case "*image.Gray":
|
2015-03-18 07:08:50 +01:00
|
|
|
return strings.Replace(prefix, "var pr, pg, pb, pa", "var pr", 1)
|
2015-03-20 00:14:03 +01:00
|
|
|
case "*image.YCbCr":
|
|
|
|
return strings.Replace(prefix, "var pr, pg, pb, pa", "var pr, pg, pb", 1)
|
2015-03-18 04:29:15 +01:00
|
|
|
}
|
2015-03-18 07:08:50 +01:00
|
|
|
return prefix
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func expnSwitch(op, dType string, expandBoth bool, template string) string {
|
|
|
|
if op == "" && dType != "anyDType" {
|
2015-04-29 09:05:36 +02:00
|
|
|
lines := []string{"switch op {"}
|
2015-04-07 03:22:25 +02:00
|
|
|
for _, op = range ops {
|
|
|
|
lines = append(lines,
|
|
|
|
fmt.Sprintf("case %s:", op),
|
|
|
|
expnSwitch(op, dType, expandBoth, template),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
lines = append(lines, "}")
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
switchVar := "dst"
|
|
|
|
if dType != "" {
|
|
|
|
switchVar = "src"
|
|
|
|
}
|
|
|
|
lines := []string{fmt.Sprintf("switch %s := %s.(type) {", switchVar, switchVar)}
|
|
|
|
|
|
|
|
fallback, values := "Image", dTypes
|
|
|
|
if dType != "" {
|
|
|
|
fallback, values = "image.Image", sTypesForDType[dType]
|
|
|
|
}
|
|
|
|
for _, v := range values {
|
2015-04-08 12:47:13 +02:00
|
|
|
if dType != "" {
|
|
|
|
// v is the sType. Skip those always-opaque sTypes, where Over is
|
|
|
|
// equivalent to Src.
|
|
|
|
if op == "Over" && alwaysOpaque[v] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 05:28:16 +01:00
|
|
|
if v == fallback {
|
|
|
|
lines = append(lines, "default:")
|
|
|
|
} else {
|
|
|
|
lines = append(lines, fmt.Sprintf("case %s:", v))
|
|
|
|
}
|
|
|
|
|
|
|
|
if dType != "" {
|
2015-03-19 01:04:55 +01:00
|
|
|
if v == "*image.YCbCr" {
|
2015-04-07 03:22:25 +02:00
|
|
|
lines = append(lines, expnSwitchYCbCr(op, dType, template))
|
2015-03-19 01:04:55 +01:00
|
|
|
} else {
|
2015-04-07 03:22:25 +02:00
|
|
|
lines = append(lines, expnLine(template, &data{dType: dType, sType: v, op: op}))
|
2015-03-19 01:04:55 +01:00
|
|
|
}
|
2015-02-24 05:28:16 +01:00
|
|
|
} else if !expandBoth {
|
2015-04-07 03:22:25 +02:00
|
|
|
lines = append(lines, expnLine(template, &data{dType: v, op: op}))
|
2015-02-24 05:28:16 +01:00
|
|
|
} else {
|
2015-04-07 03:22:25 +02:00
|
|
|
lines = append(lines, expnSwitch(op, v, false, template))
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = append(lines, "}")
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func expnSwitchYCbCr(op, dType, template string) string {
|
2015-03-19 01:04:55 +01:00
|
|
|
lines := []string{
|
|
|
|
"switch src.SubsampleRatio {",
|
|
|
|
"default:",
|
2015-04-07 03:22:25 +02:00
|
|
|
expnLine(template, &data{dType: dType, sType: "image.Image", op: op}),
|
2015-03-19 01:04:55 +01:00
|
|
|
}
|
|
|
|
for _, sratio := range subsampleRatios {
|
|
|
|
lines = append(lines,
|
|
|
|
fmt.Sprintf("case image.YCbCrSubsampleRatio%s:", sratio),
|
2015-04-07 03:22:25 +02:00
|
|
|
expnLine(template, &data{dType: dType, sType: "*image.YCbCr", sratio: sratio, op: op}),
|
2015-03-19 01:04:55 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
lines = append(lines, "}")
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2015-04-14 02:26:02 +02:00
|
|
|
func argf(args []string, s string) string {
|
|
|
|
if len(args) > 9 {
|
|
|
|
panic("too many args")
|
|
|
|
}
|
|
|
|
for i, a := range args {
|
|
|
|
old := fmt.Sprintf("$%d", i)
|
|
|
|
s = strings.Replace(s, old, a, -1)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-03-20 06:36:51 +01:00
|
|
|
func pixOffset(m, x, y, xstride, ystride string) string {
|
|
|
|
return fmt.Sprintf("(%s-%s.Rect.Min.Y)%s + (%s-%s.Rect.Min.X)%s", y, m, ystride, x, m, xstride)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cOffset(x, y, sratio string) string {
|
|
|
|
switch sratio {
|
|
|
|
case "444":
|
|
|
|
return fmt.Sprintf("( %s - src.Rect.Min.Y )*src.CStride + ( %s - src.Rect.Min.X )", y, x)
|
|
|
|
case "422":
|
|
|
|
return fmt.Sprintf("( %s - src.Rect.Min.Y )*src.CStride + ((%s)/2 - src.Rect.Min.X/2)", y, x)
|
|
|
|
case "420":
|
|
|
|
return fmt.Sprintf("((%s)/2 - src.Rect.Min.Y/2)*src.CStride + ((%s)/2 - src.Rect.Min.X/2)", y, x)
|
|
|
|
case "440":
|
|
|
|
return fmt.Sprintf("((%s)/2 - src.Rect.Min.Y/2)*src.CStride + ( %s - src.Rect.Min.X )", y, x)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unsupported sratio %q", sratio)
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:33:18 +01:00
|
|
|
func ycbcrToRGB(lhs, tmp string) string {
|
|
|
|
s := `
|
|
|
|
// This is an inline version of image/color/ycbcr.go's YCbCr.RGBA method.
|
2015-07-15 07:03:37 +02:00
|
|
|
$yy1 := int(src.Y[$i]) * 0x10100
|
2015-03-21 06:55:24 +01:00
|
|
|
$cb1 := int(src.Cb[$j]) - 128
|
|
|
|
$cr1 := int(src.Cr[$j]) - 128
|
2015-03-26 09:33:18 +01:00
|
|
|
$r@ := ($yy1 + 91881*$cr1) >> 8
|
|
|
|
$g@ := ($yy1 - 22554*$cb1 - 46802*$cr1) >> 8
|
|
|
|
$b@ := ($yy1 + 116130*$cb1) >> 8
|
|
|
|
if $r@ < 0 {
|
|
|
|
$r@ = 0
|
|
|
|
} else if $r@ > 0xffff {
|
|
|
|
$r@ = 0xffff
|
|
|
|
}
|
|
|
|
if $g@ < 0 {
|
|
|
|
$g@ = 0
|
|
|
|
} else if $g@ > 0xffff {
|
|
|
|
$g@ = 0xffff
|
|
|
|
}
|
|
|
|
if $b@ < 0 {
|
|
|
|
$b@ = 0
|
|
|
|
} else if $b@ > 0xffff {
|
|
|
|
$b@ = 0xffff
|
|
|
|
}
|
|
|
|
`
|
|
|
|
s = strings.Replace(s, "$", lhs, -1)
|
|
|
|
s = strings.Replace(s, "@", tmp, -1)
|
|
|
|
return s
|
2015-03-21 06:55:24 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
func split(s, sep string) (string, string) {
|
|
|
|
if i := strings.Index(s, sep); i >= 0 {
|
|
|
|
return strings.TrimSpace(s[:i]), strings.TrimSpace(s[i+len(sep):])
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitEq(s string) (lhs, eqOp string) {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if lhs, _ = split(s, ":="); lhs != "" {
|
|
|
|
return lhs, ":="
|
|
|
|
}
|
|
|
|
if lhs, _ = split(s, "+="); lhs != "" {
|
|
|
|
return lhs, "+="
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitArgs(s string) (args []string, extra string) {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if s == "" || s[0] != '[' {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
s = s[1:]
|
|
|
|
|
|
|
|
i := strings.IndexByte(s, ']')
|
|
|
|
if i < 0 {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
args, extra = strings.Split(s[:i], ","), s[i+1:]
|
|
|
|
for i := range args {
|
|
|
|
args[i] = strings.TrimSpace(args[i])
|
|
|
|
}
|
|
|
|
return args, extra
|
|
|
|
}
|
|
|
|
|
|
|
|
func relName(s string) string {
|
|
|
|
if i := strings.LastIndex(s, "."); i >= 0 {
|
|
|
|
return s[i+1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
codeRoot = `
|
2015-04-29 09:05:36 +02:00
|
|
|
func (z $receiver) Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
2015-07-31 10:09:14 +02:00
|
|
|
// Try to simplify a Scale to a Copy.
|
|
|
|
if dr.Size() == sr.Size() {
|
|
|
|
Copy(dst, dr.Min, src, sr, op, opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-14 08:23:36 +02:00
|
|
|
var o Options
|
|
|
|
if opts != nil {
|
|
|
|
o = *opts
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:24:07 +02:00
|
|
|
// adr is the affected destination pixels.
|
|
|
|
adr := dst.Bounds().Intersect(dr)
|
|
|
|
adr, o.DstMask = clipAffectedDestRect(adr, o.DstMask, o.DstMaskP)
|
2015-03-03 06:54:53 +01:00
|
|
|
if adr.Empty() || sr.Empty() {
|
2015-02-26 06:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
2015-04-22 07:24:07 +02:00
|
|
|
// Make adr relative to dr.Min.
|
|
|
|
adr = adr.Sub(dr.Min)
|
2015-04-29 09:05:36 +02:00
|
|
|
if op == Over && o.SrcMask == nil && opaque(src) {
|
|
|
|
op = Src
|
2015-04-08 12:47:13 +02:00
|
|
|
}
|
2015-04-14 08:23:36 +02:00
|
|
|
|
2015-02-27 11:26:47 +01:00
|
|
|
// sr is the source pixels. If it extends beyond the src bounds,
|
|
|
|
// we cannot use the type-specific fast paths, as they access
|
|
|
|
// the Pix fields directly without bounds checking.
|
2015-04-14 08:23:36 +02:00
|
|
|
//
|
|
|
|
// Similarly, the fast paths assume that the masks are nil.
|
|
|
|
if o.DstMask != nil || o.SrcMask != nil || !sr.In(src.Bounds()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
switch op {
|
2015-04-07 03:22:25 +02:00
|
|
|
case Over:
|
2015-04-16 08:17:58 +02:00
|
|
|
z.scale_Image_Image_Over(dst, dr, adr, src, sr, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
case Src:
|
2015-04-16 08:17:58 +02:00
|
|
|
z.scale_Image_Image_Src(dst, dr, adr, src, sr, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
}
|
2015-03-20 12:13:15 +01:00
|
|
|
} else if _, ok := src.(*image.Uniform); ok {
|
2015-04-29 09:05:36 +02:00
|
|
|
Draw(dst, dr, src, src.Bounds().Min, op)
|
2015-02-27 11:26:47 +01:00
|
|
|
} else {
|
2015-04-16 08:17:58 +02:00
|
|
|
$switch z.scale_$dTypeRN_$sTypeRN$sratio_$op(dst, dr, adr, src, sr, &o)
|
2015-02-27 11:26:47 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-03-05 05:46:57 +01:00
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
func (z $receiver) Transform(dst Image, s2d f64.Aff3, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
2015-07-31 10:09:14 +02:00
|
|
|
// Try to simplify a Transform to a Copy.
|
|
|
|
if s2d[0] == 1 && s2d[1] == 0 && s2d[3] == 0 && s2d[4] == 1 {
|
|
|
|
dx := int(s2d[2])
|
|
|
|
dy := int(s2d[5])
|
|
|
|
if float64(dx) == s2d[2] && float64(dy) == s2d[5] {
|
|
|
|
Copy(dst, image.Point{X: sr.Min.X + dx, Y: sr.Min.X + dy}, src, sr, op, opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 08:23:36 +02:00
|
|
|
var o Options
|
|
|
|
if opts != nil {
|
|
|
|
o = *opts
|
|
|
|
}
|
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
dr := transformRect(&s2d, &sr)
|
2015-03-23 03:30:31 +01:00
|
|
|
// adr is the affected destination pixels.
|
|
|
|
adr := dst.Bounds().Intersect(dr)
|
2015-04-22 07:24:07 +02:00
|
|
|
adr, o.DstMask = clipAffectedDestRect(adr, o.DstMask, o.DstMaskP)
|
2015-03-13 07:44:34 +01:00
|
|
|
if adr.Empty() || sr.Empty() {
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 09:05:36 +02:00
|
|
|
if op == Over && o.SrcMask == nil && opaque(src) {
|
|
|
|
op = Src
|
2015-04-08 12:47:13 +02:00
|
|
|
}
|
2015-04-14 08:23:36 +02:00
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
d2s := invert(&s2d)
|
2015-04-14 08:23:36 +02:00
|
|
|
// bias is a translation of the mapping from dst coordinates to src
|
|
|
|
// coordinates such that the latter temporarily have non-negative X
|
|
|
|
// and Y coordinates. This allows us to write int(f) instead of
|
|
|
|
// int(math.Floor(f)), since "round to zero" and "round down" are
|
|
|
|
// equivalent when f >= 0, but the former is much cheaper. The X--
|
|
|
|
// and Y-- are because the TransformLeaf methods have a "sx -= 0.5"
|
|
|
|
// adjustment.
|
2015-03-23 03:30:31 +01:00
|
|
|
bias := transformRect(&d2s, &adr).Min
|
|
|
|
bias.X--
|
|
|
|
bias.Y--
|
|
|
|
d2s[2] -= float64(bias.X)
|
|
|
|
d2s[5] -= float64(bias.Y)
|
|
|
|
// Make adr relative to dr.Min.
|
|
|
|
adr = adr.Sub(dr.Min)
|
2015-03-17 08:46:52 +01:00
|
|
|
// sr is the source pixels. If it extends beyond the src bounds,
|
|
|
|
// we cannot use the type-specific fast paths, as they access
|
|
|
|
// the Pix fields directly without bounds checking.
|
2015-04-14 08:23:36 +02:00
|
|
|
//
|
|
|
|
// Similarly, the fast paths assume that the masks are nil.
|
|
|
|
if o.DstMask != nil || o.SrcMask != nil || !sr.In(src.Bounds()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
switch op {
|
2015-04-07 03:22:25 +02:00
|
|
|
case Over:
|
2015-04-16 08:17:58 +02:00
|
|
|
z.transform_Image_Image_Over(dst, dr, adr, &d2s, src, sr, bias, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
case Src:
|
2015-04-16 08:17:58 +02:00
|
|
|
z.transform_Image_Image_Src(dst, dr, adr, &d2s, src, sr, bias, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
}
|
2015-03-20 12:13:15 +01:00
|
|
|
} else if u, ok := src.(*image.Uniform); ok {
|
2015-04-29 09:05:36 +02:00
|
|
|
transform_Uniform(dst, dr, adr, &d2s, u, sr, bias, op)
|
2015-03-17 08:46:52 +01:00
|
|
|
} else {
|
2015-04-16 08:17:58 +02:00
|
|
|
$switch z.transform_$dTypeRN_$sTypeRN$sratio_$op(dst, dr, adr, &d2s, src, sr, bias, &o)
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
2015-03-05 05:46:57 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
`
|
|
|
|
|
2015-03-05 05:46:57 +01:00
|
|
|
codeNNScaleLeaf = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (nnInterpolator) scale_$dTypeRN_$sTypeRN$sratio_$op(dst $dType, dr, adr image.Rectangle, src $sType, sr image.Rectangle, opts *Options) {
|
2015-03-03 06:54:53 +01:00
|
|
|
dw2 := uint64(dr.Dx()) * 2
|
|
|
|
dh2 := uint64(dr.Dy()) * 2
|
|
|
|
sw := uint64(sr.Dx())
|
|
|
|
sh := uint64(sr.Dy())
|
2015-02-27 06:18:34 +01:00
|
|
|
$preOuter
|
2015-03-03 05:47:39 +01:00
|
|
|
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
2015-03-03 06:54:53 +01:00
|
|
|
sy := (2*uint64(dy) + 1) * sh / dh2
|
2015-02-27 06:18:34 +01:00
|
|
|
$preInner
|
2015-03-18 07:08:50 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { $tweakDx
|
2015-03-03 06:54:53 +01:00
|
|
|
sx := (2*uint64(dx) + 1) * sw / dw2
|
2015-03-13 07:44:34 +01:00
|
|
|
p := $srcu[sr.Min.X + int(sx), sr.Min.Y + int(sy)]
|
|
|
|
$outputu[dr.Min.X + int(dx), dr.Min.Y + int(dy), p]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
codeNNTransformLeaf = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (nnInterpolator) transform_$dTypeRN_$sTypeRN$sratio_$op(dst $dType, dr, adr image.Rectangle, d2s *f64.Aff3, src $sType, sr image.Rectangle, bias image.Point, opts *Options) {
|
2015-03-13 07:44:34 +01:00
|
|
|
$preOuter
|
|
|
|
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
|
|
|
dyf := float64(dr.Min.Y + int(dy)) + 0.5
|
|
|
|
$preInner
|
2015-03-18 07:08:50 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { $tweakDx
|
2015-03-13 07:44:34 +01:00
|
|
|
dxf := float64(dr.Min.X + int(dx)) + 0.5
|
2015-03-23 03:30:31 +01:00
|
|
|
sx0 := int(d2s[0]*dxf + d2s[1]*dyf + d2s[2]) + bias.X
|
|
|
|
sy0 := int(d2s[3]*dxf + d2s[4]*dyf + d2s[5]) + bias.Y
|
2015-03-13 07:44:34 +01:00
|
|
|
if !(image.Point{sx0, sy0}).In(sr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p := $srcu[sx0, sy0]
|
|
|
|
$outputu[dr.Min.X + int(dx), dr.Min.Y + int(dy), p]
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-03-05 05:46:57 +01:00
|
|
|
codeABLScaleLeaf = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (ablInterpolator) scale_$dTypeRN_$sTypeRN$sratio_$op(dst $dType, dr, adr image.Rectangle, src $sType, sr image.Rectangle, opts *Options) {
|
2015-03-03 06:54:53 +01:00
|
|
|
sw := int32(sr.Dx())
|
|
|
|
sh := int32(sr.Dy())
|
|
|
|
yscale := float64(sh) / float64(dr.Dy())
|
|
|
|
xscale := float64(sw) / float64(dr.Dx())
|
2015-03-13 07:44:34 +01:00
|
|
|
swMinus1, shMinus1 := sw - 1, sh - 1
|
2015-02-27 06:18:34 +01:00
|
|
|
$preOuter
|
2015-03-13 07:44:34 +01:00
|
|
|
|
2015-03-03 05:47:39 +01:00
|
|
|
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
2015-02-20 08:28:54 +01:00
|
|
|
sy := (float64(dy)+0.5)*yscale - 0.5
|
2015-03-13 07:44:34 +01:00
|
|
|
// If sy < 0, we will clamp sy0 to 0 anyway, so it doesn't matter if
|
|
|
|
// we say int32(sy) instead of int32(math.Floor(sy)). Similarly for
|
|
|
|
// sx, below.
|
2015-02-20 08:28:54 +01:00
|
|
|
sy0 := int32(sy)
|
|
|
|
yFrac0 := sy - float64(sy0)
|
|
|
|
yFrac1 := 1 - yFrac0
|
|
|
|
sy1 := sy0 + 1
|
|
|
|
if sy < 0 {
|
|
|
|
sy0, sy1 = 0, 0
|
|
|
|
yFrac0, yFrac1 = 0, 1
|
2015-03-13 07:44:34 +01:00
|
|
|
} else if sy1 > shMinus1 {
|
|
|
|
sy0, sy1 = shMinus1, shMinus1
|
2015-02-20 08:28:54 +01:00
|
|
|
yFrac0, yFrac1 = 1, 0
|
|
|
|
}
|
2015-02-27 06:18:34 +01:00
|
|
|
$preInner
|
2015-03-13 07:44:34 +01:00
|
|
|
|
2015-03-18 07:08:50 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { $tweakDx
|
2015-02-20 08:28:54 +01:00
|
|
|
sx := (float64(dx)+0.5)*xscale - 0.5
|
|
|
|
sx0 := int32(sx)
|
|
|
|
xFrac0 := sx - float64(sx0)
|
|
|
|
xFrac1 := 1 - xFrac0
|
|
|
|
sx1 := sx0 + 1
|
|
|
|
if sx < 0 {
|
|
|
|
sx0, sx1 = 0, 0
|
|
|
|
xFrac0, xFrac1 = 0, 1
|
2015-03-13 07:44:34 +01:00
|
|
|
} else if sx1 > swMinus1 {
|
|
|
|
sx0, sx1 = swMinus1, swMinus1
|
2015-02-20 08:28:54 +01:00
|
|
|
xFrac0, xFrac1 = 1, 0
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
|
|
|
|
s00 := $srcf[sr.Min.X + int(sx0), sr.Min.Y + int(sy0)]
|
|
|
|
s10 := $srcf[sr.Min.X + int(sx1), sr.Min.Y + int(sy0)]
|
|
|
|
$blend[xFrac1, s00, xFrac0, s10]
|
|
|
|
s01 := $srcf[sr.Min.X + int(sx0), sr.Min.Y + int(sy1)]
|
|
|
|
s11 := $srcf[sr.Min.X + int(sx1), sr.Min.Y + int(sy1)]
|
|
|
|
$blend[xFrac1, s01, xFrac0, s11]
|
|
|
|
$blend[yFrac1, s10, yFrac0, s11]
|
2015-04-17 09:57:53 +02:00
|
|
|
$convFtou[p, s11]
|
|
|
|
$outputu[dr.Min.X + int(dx), dr.Min.Y + int(dy), p]
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
codeABLTransformLeaf = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (ablInterpolator) transform_$dTypeRN_$sTypeRN$sratio_$op(dst $dType, dr, adr image.Rectangle, d2s *f64.Aff3, src $sType, sr image.Rectangle, bias image.Point, opts *Options) {
|
2015-03-13 07:44:34 +01:00
|
|
|
$preOuter
|
|
|
|
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
|
|
|
dyf := float64(dr.Min.Y + int(dy)) + 0.5
|
|
|
|
$preInner
|
2015-03-18 07:08:50 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { $tweakDx
|
2015-03-13 07:44:34 +01:00
|
|
|
dxf := float64(dr.Min.X + int(dx)) + 0.5
|
|
|
|
sx := d2s[0]*dxf + d2s[1]*dyf + d2s[2]
|
|
|
|
sy := d2s[3]*dxf + d2s[4]*dyf + d2s[5]
|
2015-03-23 03:30:31 +01:00
|
|
|
if !(image.Point{int(sx) + bias.X, int(sy) + bias.Y}).In(sr) {
|
2015-03-13 07:44:34 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sx -= 0.5
|
2015-03-23 03:30:31 +01:00
|
|
|
sx0 := int(sx)
|
|
|
|
xFrac0 := sx - float64(sx0)
|
2015-03-13 07:44:34 +01:00
|
|
|
xFrac1 := 1 - xFrac0
|
2015-03-23 03:30:31 +01:00
|
|
|
sx0 += bias.X
|
2015-03-13 07:44:34 +01:00
|
|
|
sx1 := sx0 + 1
|
|
|
|
if sx0 < sr.Min.X {
|
|
|
|
sx0, sx1 = sr.Min.X, sr.Min.X
|
|
|
|
xFrac0, xFrac1 = 0, 1
|
|
|
|
} else if sx1 >= sr.Max.X {
|
|
|
|
sx0, sx1 = sr.Max.X-1, sr.Max.X-1
|
|
|
|
xFrac0, xFrac1 = 1, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
sy -= 0.5
|
2015-03-23 03:30:31 +01:00
|
|
|
sy0 := int(sy)
|
|
|
|
yFrac0 := sy - float64(sy0)
|
2015-03-13 07:44:34 +01:00
|
|
|
yFrac1 := 1 - yFrac0
|
2015-03-23 03:30:31 +01:00
|
|
|
sy0 += bias.Y
|
2015-03-13 07:44:34 +01:00
|
|
|
sy1 := sy0 + 1
|
|
|
|
if sy0 < sr.Min.Y {
|
|
|
|
sy0, sy1 = sr.Min.Y, sr.Min.Y
|
|
|
|
yFrac0, yFrac1 = 0, 1
|
|
|
|
} else if sy1 >= sr.Max.Y {
|
|
|
|
sy0, sy1 = sr.Max.Y-1, sr.Max.Y-1
|
|
|
|
yFrac0, yFrac1 = 1, 0
|
|
|
|
}
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
s00 := $srcf[sx0, sy0]
|
|
|
|
s10 := $srcf[sx1, sy0]
|
|
|
|
$blend[xFrac1, s00, xFrac0, s10]
|
|
|
|
s01 := $srcf[sx0, sy1]
|
|
|
|
s11 := $srcf[sx1, sy1]
|
|
|
|
$blend[xFrac1, s01, xFrac0, s11]
|
|
|
|
$blend[yFrac1, s10, yFrac0, s11]
|
2015-04-17 09:57:53 +02:00
|
|
|
$convFtou[p, s11]
|
|
|
|
$outputu[dr.Min.X + int(dx), dr.Min.Y + int(dy), p]
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
codeKernelRoot = `
|
2015-04-29 09:05:36 +02:00
|
|
|
func (z *kernelScaler) Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
2015-03-03 06:54:53 +01:00
|
|
|
if z.dw != int32(dr.Dx()) || z.dh != int32(dr.Dy()) || z.sw != int32(sr.Dx()) || z.sh != int32(sr.Dy()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
z.kernel.Scale(dst, dr, src, sr, op, opts)
|
2015-02-20 08:28:54 +01:00
|
|
|
return
|
|
|
|
}
|
2015-04-14 08:23:36 +02:00
|
|
|
|
|
|
|
var o Options
|
|
|
|
if opts != nil {
|
|
|
|
o = *opts
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:24:07 +02:00
|
|
|
// adr is the affected destination pixels.
|
|
|
|
adr := dst.Bounds().Intersect(dr)
|
|
|
|
adr, o.DstMask = clipAffectedDestRect(adr, o.DstMask, o.DstMaskP)
|
2015-03-03 06:54:53 +01:00
|
|
|
if adr.Empty() || sr.Empty() {
|
2015-02-26 06:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
2015-04-22 07:24:07 +02:00
|
|
|
// Make adr relative to dr.Min.
|
|
|
|
adr = adr.Sub(dr.Min)
|
2015-04-29 09:05:36 +02:00
|
|
|
if op == Over && o.SrcMask == nil && opaque(src) {
|
|
|
|
op = Src
|
2015-04-08 12:47:13 +02:00
|
|
|
}
|
2015-03-20 12:13:15 +01:00
|
|
|
|
2015-04-14 08:23:36 +02:00
|
|
|
if _, ok := src.(*image.Uniform); ok && o.DstMask == nil && o.SrcMask == nil && sr.In(src.Bounds()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
Draw(dst, dr, src, src.Bounds().Min, op)
|
2015-03-20 12:13:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-20 08:28:54 +01:00
|
|
|
// Create a temporary buffer:
|
|
|
|
// scaleX distributes the source image's columns over the temporary image.
|
|
|
|
// scaleY distributes the temporary image's rows over the destination image.
|
2015-03-27 01:21:16 +01:00
|
|
|
var tmp [][4]float64
|
|
|
|
if z.pool.New != nil {
|
|
|
|
tmpp := z.pool.Get().(*[][4]float64)
|
|
|
|
defer z.pool.Put(tmpp)
|
|
|
|
tmp = *tmpp
|
|
|
|
} else {
|
|
|
|
tmp = z.makeTmpBuf()
|
|
|
|
}
|
2015-02-27 11:26:47 +01:00
|
|
|
|
|
|
|
// sr is the source pixels. If it extends beyond the src bounds,
|
|
|
|
// we cannot use the type-specific fast paths, as they access
|
|
|
|
// the Pix fields directly without bounds checking.
|
2015-04-14 08:23:36 +02:00
|
|
|
//
|
|
|
|
// Similarly, the fast paths assume that the masks are nil.
|
|
|
|
if o.SrcMask != nil || !sr.In(src.Bounds()) {
|
2015-04-16 08:17:58 +02:00
|
|
|
z.scaleX_Image(tmp, src, sr, &o)
|
2015-02-27 11:26:47 +01:00
|
|
|
} else {
|
2015-04-16 08:17:58 +02:00
|
|
|
$switchS z.scaleX_$sTypeRN$sratio(tmp, src, sr, &o)
|
2015-02-27 11:26:47 +01:00
|
|
|
}
|
|
|
|
|
2015-04-21 06:26:44 +02:00
|
|
|
if o.DstMask != nil {
|
2015-04-29 09:05:36 +02:00
|
|
|
switch op {
|
2015-04-21 06:26:44 +02:00
|
|
|
case Over:
|
|
|
|
z.scaleY_Image_Over(dst, dr, adr, tmp, &o)
|
|
|
|
case Src:
|
|
|
|
z.scaleY_Image_Src(dst, dr, adr, tmp, &o)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$switchD z.scaleY_$dTypeRN_$op(dst, dr, adr, tmp, &o)
|
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-03-05 05:46:57 +01:00
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
func (q *Kernel) Transform(dst Image, s2d f64.Aff3, src image.Image, sr image.Rectangle, op Op, opts *Options) {
|
2015-04-14 08:23:36 +02:00
|
|
|
var o Options
|
|
|
|
if opts != nil {
|
|
|
|
o = *opts
|
|
|
|
}
|
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
dr := transformRect(&s2d, &sr)
|
2015-03-23 03:30:31 +01:00
|
|
|
// adr is the affected destination pixels.
|
|
|
|
adr := dst.Bounds().Intersect(dr)
|
2015-04-22 07:24:07 +02:00
|
|
|
adr, o.DstMask = clipAffectedDestRect(adr, o.DstMask, o.DstMaskP)
|
2015-03-17 08:46:52 +01:00
|
|
|
if adr.Empty() || sr.Empty() {
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 09:05:36 +02:00
|
|
|
if op == Over && o.SrcMask == nil && opaque(src) {
|
|
|
|
op = Src
|
2015-04-08 12:47:13 +02:00
|
|
|
}
|
2015-07-27 07:58:18 +02:00
|
|
|
d2s := invert(&s2d)
|
2015-04-14 08:23:36 +02:00
|
|
|
// bias is a translation of the mapping from dst coordinates to src
|
|
|
|
// coordinates such that the latter temporarily have non-negative X
|
|
|
|
// and Y coordinates. This allows us to write int(f) instead of
|
|
|
|
// int(math.Floor(f)), since "round to zero" and "round down" are
|
|
|
|
// equivalent when f >= 0, but the former is much cheaper. The X--
|
|
|
|
// and Y-- are because the TransformLeaf methods have a "sx -= 0.5"
|
|
|
|
// adjustment.
|
2015-03-23 03:30:31 +01:00
|
|
|
bias := transformRect(&d2s, &adr).Min
|
|
|
|
bias.X--
|
|
|
|
bias.Y--
|
|
|
|
d2s[2] -= float64(bias.X)
|
|
|
|
d2s[5] -= float64(bias.Y)
|
|
|
|
// Make adr relative to dr.Min.
|
|
|
|
adr = adr.Sub(dr.Min)
|
2015-03-17 08:46:52 +01:00
|
|
|
|
2015-04-14 08:23:36 +02:00
|
|
|
if u, ok := src.(*image.Uniform); ok && o.DstMask != nil && o.SrcMask != nil && sr.In(src.Bounds()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
transform_Uniform(dst, dr, adr, &d2s, u, sr, bias, op)
|
2015-03-20 12:13:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-17 08:46:52 +01:00
|
|
|
xscale := abs(d2s[0])
|
|
|
|
if s := abs(d2s[1]); xscale < s {
|
|
|
|
xscale = s
|
|
|
|
}
|
|
|
|
yscale := abs(d2s[3])
|
|
|
|
if s := abs(d2s[4]); yscale < s {
|
|
|
|
yscale = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// sr is the source pixels. If it extends beyond the src bounds,
|
|
|
|
// we cannot use the type-specific fast paths, as they access
|
|
|
|
// the Pix fields directly without bounds checking.
|
2015-04-14 08:23:36 +02:00
|
|
|
//
|
|
|
|
// Similarly, the fast paths assume that the masks are nil.
|
|
|
|
if o.DstMask != nil || o.SrcMask != nil || !sr.In(src.Bounds()) {
|
2015-04-29 09:05:36 +02:00
|
|
|
switch op {
|
2015-04-07 03:22:25 +02:00
|
|
|
case Over:
|
2015-04-16 08:17:58 +02:00
|
|
|
q.transform_Image_Image_Over(dst, dr, adr, &d2s, src, sr, bias, xscale, yscale, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
case Src:
|
2015-04-16 08:17:58 +02:00
|
|
|
q.transform_Image_Image_Src(dst, dr, adr, &d2s, src, sr, bias, xscale, yscale, &o)
|
2015-04-07 03:22:25 +02:00
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
} else {
|
2015-04-16 08:17:58 +02:00
|
|
|
$switch q.transform_$dTypeRN_$sTypeRN$sratio_$op(dst, dr, adr, &d2s, src, sr, bias, xscale, yscale, &o)
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
2015-03-05 05:46:57 +01:00
|
|
|
}
|
2015-02-20 08:28:54 +01:00
|
|
|
`
|
|
|
|
|
2015-03-05 05:46:57 +01:00
|
|
|
codeKernelScaleLeafX = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (z *kernelScaler) scaleX_$sTypeRN$sratio(tmp [][4]float64, src $sType, sr image.Rectangle, opts *Options) {
|
2015-02-20 08:28:54 +01:00
|
|
|
t := 0
|
2015-04-24 06:45:24 +02:00
|
|
|
$preKernelOuter
|
2015-02-20 08:28:54 +01:00
|
|
|
for y := int32(0); y < z.sh; y++ {
|
|
|
|
for _, s := range z.horizontal.sources {
|
2015-03-18 07:08:50 +01:00
|
|
|
var pr, pg, pb, pa float64 $tweakVarP
|
2015-02-20 08:28:54 +01:00
|
|
|
for _, c := range z.horizontal.contribs[s.i:s.j] {
|
2015-03-13 07:44:34 +01:00
|
|
|
p += $srcf[sr.Min.X + int(c.coord), sr.Min.Y + int(y)] * c.weight
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
2015-03-18 04:29:15 +01:00
|
|
|
$tweakPr
|
2015-02-20 08:28:54 +01:00
|
|
|
tmp[t] = [4]float64{
|
2015-03-18 07:08:50 +01:00
|
|
|
pr * s.invTotalWeightFFFF, $tweakP
|
|
|
|
pg * s.invTotalWeightFFFF, $tweakP
|
|
|
|
pb * s.invTotalWeightFFFF, $tweakP
|
|
|
|
pa * s.invTotalWeightFFFF, $tweakP
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
t++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-03-05 05:46:57 +01:00
|
|
|
codeKernelScaleLeafY = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (z *kernelScaler) scaleY_$dTypeRN_$op(dst $dType, dr, adr image.Rectangle, tmp [][4]float64, opts *Options) {
|
2015-02-27 06:18:34 +01:00
|
|
|
$preOuter
|
2015-03-03 05:47:39 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ {
|
2015-02-27 06:18:34 +01:00
|
|
|
$preKernelInner
|
2015-03-18 07:08:50 +01:00
|
|
|
for dy, s := range z.vertical.sources[adr.Min.Y:adr.Max.Y] { $tweakDy
|
2015-02-20 08:28:54 +01:00
|
|
|
var pr, pg, pb, pa float64
|
|
|
|
for _, c := range z.vertical.contribs[s.i:s.j] {
|
2015-02-26 06:20:04 +01:00
|
|
|
p := &tmp[c.coord*z.dw+dx]
|
2015-02-20 08:28:54 +01:00
|
|
|
pr += p[0] * c.weight
|
|
|
|
pg += p[1] * c.weight
|
|
|
|
pb += p[2] * c.weight
|
|
|
|
pa += p[3] * c.weight
|
|
|
|
}
|
2015-04-10 05:05:20 +02:00
|
|
|
$clampToAlpha
|
2015-03-17 08:46:52 +01:00
|
|
|
$outputf[dr.Min.X + int(dx), dr.Min.Y + int(adr.Min.Y + dy), ftou, p, s.invTotalWeight]
|
|
|
|
$tweakD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
codeKernelTransformLeaf = `
|
2015-04-16 08:17:58 +02:00
|
|
|
func (q *Kernel) transform_$dTypeRN_$sTypeRN$sratio_$op(dst $dType, dr, adr image.Rectangle, d2s *f64.Aff3, src $sType, sr image.Rectangle, bias image.Point, xscale, yscale float64, opts *Options) {
|
2015-03-17 08:46:52 +01:00
|
|
|
// When shrinking, broaden the effective kernel support so that we still
|
|
|
|
// visit every source pixel.
|
|
|
|
xHalfWidth, xKernelArgScale := q.Support, 1.0
|
|
|
|
if xscale > 1 {
|
|
|
|
xHalfWidth *= xscale
|
|
|
|
xKernelArgScale = 1 / xscale
|
|
|
|
}
|
|
|
|
yHalfWidth, yKernelArgScale := q.Support, 1.0
|
|
|
|
if yscale > 1 {
|
|
|
|
yHalfWidth *= yscale
|
|
|
|
yKernelArgScale = 1 / yscale
|
|
|
|
}
|
|
|
|
|
|
|
|
xWeights := make([]float64, 1 + 2*int(math.Ceil(xHalfWidth)))
|
|
|
|
yWeights := make([]float64, 1 + 2*int(math.Ceil(yHalfWidth)))
|
|
|
|
|
|
|
|
$preOuter
|
|
|
|
for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ {
|
|
|
|
dyf := float64(dr.Min.Y + int(dy)) + 0.5
|
|
|
|
$preInner
|
2015-03-18 07:08:50 +01:00
|
|
|
for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { $tweakDx
|
2015-03-17 08:46:52 +01:00
|
|
|
dxf := float64(dr.Min.X + int(dx)) + 0.5
|
|
|
|
sx := d2s[0]*dxf + d2s[1]*dyf + d2s[2]
|
|
|
|
sy := d2s[3]*dxf + d2s[4]*dyf + d2s[5]
|
2015-03-23 03:30:31 +01:00
|
|
|
if !(image.Point{int(sx) + bias.X, int(sy) + bias.Y}).In(sr) {
|
2015-03-17 08:46:52 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:30:31 +01:00
|
|
|
// TODO: adjust the bias so that we can use int(f) instead
|
|
|
|
// of math.Floor(f) and math.Ceil(f).
|
|
|
|
sx += float64(bias.X)
|
2015-03-17 08:46:52 +01:00
|
|
|
sx -= 0.5
|
|
|
|
ix := int(math.Floor(sx - xHalfWidth))
|
|
|
|
if ix < sr.Min.X {
|
|
|
|
ix = sr.Min.X
|
|
|
|
}
|
|
|
|
jx := int(math.Ceil(sx + xHalfWidth))
|
|
|
|
if jx > sr.Max.X {
|
|
|
|
jx = sr.Max.X
|
|
|
|
}
|
|
|
|
|
|
|
|
totalXWeight := 0.0
|
|
|
|
for kx := ix; kx < jx; kx++ {
|
|
|
|
xWeight := 0.0
|
|
|
|
if t := abs((sx - float64(kx)) * xKernelArgScale); t < q.Support {
|
|
|
|
xWeight = q.At(t)
|
|
|
|
}
|
|
|
|
xWeights[kx - ix] = xWeight
|
|
|
|
totalXWeight += xWeight
|
|
|
|
}
|
|
|
|
for x := range xWeights[:jx-ix] {
|
|
|
|
xWeights[x] /= totalXWeight
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:30:31 +01:00
|
|
|
sy += float64(bias.Y)
|
2015-03-17 08:46:52 +01:00
|
|
|
sy -= 0.5
|
|
|
|
iy := int(math.Floor(sy - yHalfWidth))
|
|
|
|
if iy < sr.Min.Y {
|
|
|
|
iy = sr.Min.Y
|
|
|
|
}
|
|
|
|
jy := int(math.Ceil(sy + yHalfWidth))
|
|
|
|
if jy > sr.Max.Y {
|
|
|
|
jy = sr.Max.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
totalYWeight := 0.0
|
|
|
|
for ky := iy; ky < jy; ky++ {
|
|
|
|
yWeight := 0.0
|
|
|
|
if t := abs((sy - float64(ky)) * yKernelArgScale); t < q.Support {
|
|
|
|
yWeight = q.At(t)
|
|
|
|
}
|
|
|
|
yWeights[ky - iy] = yWeight
|
|
|
|
totalYWeight += yWeight
|
|
|
|
}
|
|
|
|
for y := range yWeights[:jy-iy] {
|
|
|
|
yWeights[y] /= totalYWeight
|
|
|
|
}
|
|
|
|
|
2015-03-18 07:08:50 +01:00
|
|
|
var pr, pg, pb, pa float64 $tweakVarP
|
2015-03-17 08:46:52 +01:00
|
|
|
for ky := iy; ky < jy; ky++ {
|
2015-03-24 10:16:40 +01:00
|
|
|
if yWeight := yWeights[ky - iy]; yWeight != 0 {
|
|
|
|
for kx := ix; kx < jx; kx++ {
|
|
|
|
if w := xWeights[kx - ix] * yWeight; w != 0 {
|
|
|
|
p += $srcf[kx, ky] * w
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 05:05:20 +02:00
|
|
|
$clampToAlpha
|
2015-03-17 08:46:52 +01:00
|
|
|
$outputf[dr.Min.X + int(dx), dr.Min.Y + int(dy), fffftou, p, 1]
|
2015-02-20 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|