-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
56 lines (47 loc) · 1.25 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
package imagegenerator
import (
"image"
"log"
"github.com/hmmftg/image/draw"
)
type Image struct {
ID string
Target image.Image
Scale float64
X, Y float64
RightAlign bool
}
func DrawImage(img draw.Image, target image.Image, x, y int) {
r := image.Rectangle{
Min: image.Point{X: x, Y: y},
Max: image.Point{X: x + target.Bounds().Dx(), Y: y + target.Bounds().Dy()}}
draw.Draw(img, r, target, image.Point{0, 0}, draw.Over)
}
func (i Image) Draw(tx *PrintTx) int {
if i.Scale == 0 {
i.Scale = 1
}
var ok bool
i.Target, ok = tx.Images[i.ID]
if !ok {
log.Println("image not found(ignored)", i.ID)
return 0
}
scaledRect := image.Rect(
0,
0,
int(float64(i.Target.Bounds().Dx()*int(tx.Dpi/72.))*i.Scale),
int(float64(i.Target.Bounds().Dy()*int(tx.Dpi/72.))*i.Scale))
// log.Println("scaledRect", scaledRect, tx.Dpi/72., i.Scale, math.Ceil((tx.Dpi/72.)*i.Scale))
scaledImage := tx.getNewImage(scaledRect)
draw.NearestNeighbor.Scale(scaledImage, scaledRect, i.Target, i.Target.Bounds(), draw.Over, nil)
x := tx.RelationalX(i.X)
if i.RightAlign {
x = tx.Rgba.Bounds().Max.X - scaledImage.Bounds().Max.X - x
} else {
x = tx.RelationalX(i.X)
}
y := tx.RelationalY(i.Y)
DrawImage(tx.Rgba, scaledImage, x, y)
return x
}