-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
240 lines (215 loc) · 6.73 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2021, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package svg
import (
"errors"
"image"
"log"
"goki.dev/girl/paint"
"goki.dev/grows/images"
"goki.dev/mat32/v2"
"golang.org/x/image/draw"
"golang.org/x/image/math/f64"
)
// Image is an SVG image (bitmap)
type Image struct {
NodeBase
// position of the top-left of the image
Pos mat32.Vec2 `xml:"{x,y}"`
// rendered size of the image (imposes a scaling on image when it is rendered)
Size mat32.Vec2 `xml:"{width,height}"`
// directs resize operations to preserve aspect ratio
PreserveAspectRatio bool `xml:"preserveAspectRatio"`
// file name of image loaded -- set by OpenImage
Filename string
// the image pixels
Pixels *image.RGBA `copy:"-" xml:"-" json:"-" view:"-"`
}
func (g *Image) SVGName() string { return "image" }
func (g *Image) CopyFieldsFrom(frm any) {
fr := frm.(*Image)
g.NodeBase.CopyFieldsFrom(&fr.NodeBase)
g.Pos = fr.Pos
g.Size = fr.Size
g.PreserveAspectRatio = fr.PreserveAspectRatio
g.Filename = fr.Filename
g.Pixels = fr.Pixels
}
// SetImageSize sets size of the bitmap image.
// This does not resize any existing image, just makes a new image
// if the size is different
func (g *Image) SetImageSize(nwsz image.Point) {
if nwsz.X == 0 || nwsz.Y == 0 {
return
}
if g.Pixels != nil && g.Pixels.Bounds().Size() == nwsz {
return
}
g.Pixels = image.NewRGBA(image.Rectangle{Max: nwsz})
}
// SetImage sets an image for the bitmap, and resizes to the size of the image
// or the specified size -- pass 0 for width and/or height to use the actual image size
// for that dimension. Copies from given image into internal image for this bitmap.
func (g *Image) SetImage(img image.Image, width, height float32) {
sz := img.Bounds().Size()
if width <= 0 && height <= 0 {
g.SetImageSize(sz)
draw.Draw(g.Pixels, g.Pixels.Bounds(), img, image.Point{}, draw.Src)
if g.Size.X == 0 && g.Size.Y == 0 {
g.Size = mat32.V2FromPoint(sz)
}
} else {
tsz := sz
transformer := draw.BiLinear
scx := float32(1)
scy := float32(1)
if width > 0 {
scx = width / float32(sz.X)
tsz.X = int(width)
}
if height > 0 {
scy = height / float32(sz.Y)
tsz.Y = int(height)
}
g.SetImageSize(tsz)
m := mat32.Scale2D(scx, scy)
s2d := f64.Aff3{float64(m.XX), float64(m.XY), float64(m.X0), float64(m.YX), float64(m.YY), float64(m.Y0)}
transformer.Transform(g.Pixels, s2d, img, img.Bounds(), draw.Over, nil)
if g.Size.X == 0 && g.Size.Y == 0 {
g.Size = mat32.V2FromPoint(tsz)
}
}
}
func (g *Image) DrawImage(sv *SVG) {
if g.Pixels == nil {
return
}
pc := &paint.Context{&sv.RenderState, &g.Paint}
pc.DrawImageScaled(g.Pixels, g.Pos.X, g.Pos.Y, g.Size.X, g.Size.Y)
}
func (g *Image) NodeBBox(sv *SVG) image.Rectangle {
rs := &sv.RenderState
pos := rs.CurTransform.MulVec2AsPt(g.Pos)
max := rs.CurTransform.MulVec2AsPt(g.Pos.Add(g.Size))
posi := pos.ToPointCeil()
maxi := max.ToPointCeil()
return image.Rectangle{posi, maxi}.Canon()
}
func (g *Image) LocalBBox() mat32.Box2 {
bb := mat32.Box2{}
bb.Min = g.Pos
bb.Max = g.Pos.Add(g.Size)
return bb
}
func (g *Image) Render(sv *SVG) {
vis, rs := g.PushTransform(sv)
if !vis {
return
}
rs.Lock()
g.DrawImage(sv)
rs.Unlock()
g.BBoxes(sv)
g.RenderChildren(sv)
rs.PopTransformLock()
}
// ApplyTransform applies the given 2D transform to the geometry of this node
// each node must define this for itself
func (g *Image) ApplyTransform(sv *SVG, xf mat32.Mat2) {
rot := xf.ExtractRot()
if rot != 0 || !g.Paint.Transform.IsIdentity() {
g.Paint.Transform = g.Paint.Transform.Mul(xf)
g.SetProp("transform", g.Paint.Transform.String())
} else {
g.Pos = xf.MulVec2AsPt(g.Pos)
g.Size = xf.MulVec2AsVec(g.Size)
}
}
// ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node
// relative to given point. Trans translation and point are in top-level coordinates,
// so must be transformed into local coords first.
// Point is upper left corner of selection box that anchors the translation and scaling,
// and for rotation it is the center point around which to rotate
func (g *Image) ApplyDeltaTransform(sv *SVG, trans mat32.Vec2, scale mat32.Vec2, rot float32, pt mat32.Vec2) {
crot := g.Paint.Transform.ExtractRot()
if rot != 0 || crot != 0 {
xf, lpt := g.DeltaTransform(trans, scale, rot, pt, false) // exclude self
mat := g.Paint.Transform.MulCtr(xf, lpt)
g.Paint.Transform = mat
g.SetProp("transform", g.Paint.Transform.String())
} else {
xf, lpt := g.DeltaTransform(trans, scale, rot, pt, true) // include self
g.Pos = xf.MulVec2AsPtCtr(g.Pos, lpt)
g.Size = xf.MulVec2AsVec(g.Size)
}
}
// WriteGeom writes the geometry of the node to a slice of floating point numbers
// the length and ordering of which is specific to each node type.
// Slice must be passed and will be resized if not the correct length.
func (g *Image) WriteGeom(sv *SVG, dat *[]float32) {
SetFloat32SliceLen(dat, 4+6)
(*dat)[0] = g.Pos.X
(*dat)[1] = g.Pos.Y
(*dat)[2] = g.Size.X
(*dat)[3] = g.Size.Y
g.WriteTransform(*dat, 4)
}
// ReadGeom reads the geometry of the node from a slice of floating point numbers
// the length and ordering of which is specific to each node type.
func (g *Image) ReadGeom(sv *SVG, dat []float32) {
g.Pos.X = dat[0]
g.Pos.Y = dat[1]
g.Size.X = dat[2]
g.Size.Y = dat[3]
g.ReadTransform(dat, 4)
}
/*
// ImageProps define the Toolbar for images
var ImageProps = ki.Props{
"Toolbar": ki.PropSlice{
{"OpenImage", ki.Props{
"desc": "Open image file for this image node, rescaling to given size -- use 0, 0 to use native image size.",
"icon": icons.Open,
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".png,.jpg,.jpeg",
}},
{"Width", ki.Props{}},
{"Height", ki.Props{}},
},
}},
{"SaveImage", ki.Props{
"desc": "Save image to a file.",
"icon": icons.SaveAs,
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".png,.jpg,.jpeg",
}},
},
}},
},
}
*/
// OpenImage opens an image for the bitmap, and resizes to the size of the image
// or the specified size -- pass 0 for width and/or height to use the actual image size
// for that dimension
func (g *Image) OpenImage(filename string, width, height float32) error {
img, _, err := images.Open(filename)
if err != nil {
log.Printf("svg.OpenImage -- could not open file: %v, err: %v\n", filename, err)
return err
}
g.Filename = filename
g.SetImage(img, width, height)
return nil
}
// SaveImage saves current image to a file
func (g *Image) SaveImage(filename string) error {
if g.Pixels == nil {
return errors.New("svg.SaveImage Pixels is nil")
}
return images.Save(g.Pixels, filename)
}