-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
90 lines (72 loc) · 2.06 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
// Copyright 2017 Whit Marbut. All rights reserved.
// License information may be found in the LICENSE file.
package epdfuse
import (
"github.com/fogleman/gg"
"github.com/nfnt/resize"
"image"
)
type ScalePlan byte
const (
SCALE_NO ScalePlan = '0'
SCALE_UP ScalePlan = '1'
SCALE_DOWN ScalePlan = '2'
SCALE_PLACE_ONLY ScalePlan = '3'
)
type Axis byte
const (
AXIS_X Axis = '0'
AXIS_Y Axis = '1'
)
func (epd *EpdFuse) scaleAndPlaceImage(img image.Image) image.Image {
plan := epd.detectScalePlacePlan(img)
if plan == SCALE_DOWN || plan == SCALE_UP {
img = epd.scale(img)
img = epd.placeImage(img)
} else if plan == SCALE_PLACE_ONLY {
img = epd.placeImage(img)
}
return img
}
func (epd *EpdFuse) detectScalePlacePlan(img image.Image) ScalePlan {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
if width > epd.Width || height > epd.Height {
return SCALE_DOWN
} else if width < epd.Width && height < epd.Height {
return SCALE_UP
} else if width < epd.Width || height < epd.Height {
return SCALE_PLACE_ONLY
} else {
return SCALE_NO
}
}
func (epd *EpdFuse) placeImage(img image.Image) image.Image {
context := gg.NewContext(epd.Width, epd.Height)
mW := int(epd.Width / 2)
mH := int(epd.Height / 2)
context.DrawImageAnchored(img, mW, mH, 0.5, 0.5)
return context.Image()
}
func (epd *EpdFuse) scale(img image.Image) image.Image {
cstrAxis := epd.constrainingAxis(img)
sFactor := epd.scaleFactor(img, cstrAxis)
nW := uint(float64(img.Bounds().Dx()) * sFactor)
nH := uint(float64(img.Bounds().Dy()) * sFactor)
img = resize.Resize(nW, nH, img, resize.Lanczos2)
return img
}
func (epd *EpdFuse) constrainingAxis(img image.Image) Axis {
origRatio := float64(epd.Width) / float64(epd.Height)
ratio := float64(img.Bounds().Dx()) / float64(img.Bounds().Dy())
if ratio > origRatio {
return AXIS_X
}
return AXIS_Y
}
func (epd *EpdFuse) scaleFactor(img image.Image, axis Axis) float64 {
if axis == AXIS_X {
return float64(epd.Width) / float64(img.Bounds().Dx())
}
return float64(epd.Height) / float64(img.Bounds().Dy())
}