forked from MilanMisak/pixlserv
/
transformations.go
287 lines (245 loc) · 8.49 KB
/
transformations.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
// TODO - refactor out a resizing function
import (
"image"
"image/color"
"image/draw"
"log"
"code.google.com/p/freetype-go/freetype"
"code.google.com/p/freetype-go/freetype/truetype"
"github.com/nfnt/resize"
)
// Transformation specifies parameters and a watermark to be used when transforming an image
type Transformation struct {
params *Params
watermark *Watermark
texts []*Text
}
// Watermark specifies a watermark to be applied to an image
type Watermark struct {
imagePath, gravity string
x, y int
}
// Text specifies a text overlay to be applied to an image
type Text struct {
content, gravity string
x, y, size int
font *truetype.Font
color color.Color
}
// FontMetrics defines font metrics for a Text struct as rounded up integers
type FontMetrics struct {
width, height, ascent, descent float64
}
func (t *Text) GetFontMetrics(scale int) FontMetrics {
// Adapted from: https://code.google.com/p/plotinum/
// Converts truetype.FUnit to float64
fUnit2Float64 := float64(t.size) / float64(t.font.FUnitsPerEm())
width := 0
prev, hasPrev := truetype.Index(0), false
for _, rune := range t.content {
index := t.font.Index(rune)
if hasPrev {
width += int(t.font.Kerning(t.font.FUnitsPerEm(), prev, index))
}
width += int(t.font.HMetric(t.font.FUnitsPerEm(), index).AdvanceWidth)
prev, hasPrev = index, true
}
widthFloat := float64(width) * fUnit2Float64 * float64(scale)
bounds := t.font.Bounds(t.font.FUnitsPerEm())
height := float64(bounds.YMax-bounds.YMin) * fUnit2Float64 * float64(scale)
ascent := float64(bounds.YMax) * fUnit2Float64 * float64(scale)
descent := float64(bounds.YMin) * fUnit2Float64 * float64(scale)
return FontMetrics{widthFloat, height, ascent, descent}
}
func transformCropAndResize(img image.Image, transformation *Transformation) (imgNew image.Image) {
parameters := transformation.params
width := parameters.width
height := parameters.height
gravity := parameters.gravity
scale := parameters.scale
imgWidth := img.Bounds().Dx()
imgHeight := img.Bounds().Dy()
// Scaling factor
if parameters.cropping != CroppingModeKeepScale {
width *= scale
height *= scale
}
// Resize and crop
switch parameters.cropping {
case CroppingModeExact:
imgNew = resize.Resize(uint(width), uint(height), img, resize.Bilinear)
case CroppingModeAll:
if float32(width)*(float32(imgHeight)/float32(imgWidth)) > float32(height) {
// Keep height
imgNew = resize.Resize(0, uint(height), img, resize.Bilinear)
} else {
// Keep width
imgNew = resize.Resize(uint(width), 0, img, resize.Bilinear)
}
case CroppingModePart:
// Use the top left part of the image for now
var croppedRect image.Rectangle
if float32(width)*(float32(imgHeight)/float32(imgWidth)) > float32(height) {
// Whole width displayed
newHeight := int((float32(imgWidth) / float32(width)) * float32(height))
croppedRect = image.Rect(0, 0, imgWidth, newHeight)
} else {
// Whole height displayed
newWidth := int((float32(imgHeight) / float32(height)) * float32(width))
croppedRect = image.Rect(0, 0, newWidth, imgHeight)
}
topLeftPoint := calculateTopLeftPointFromGravity(gravity, croppedRect.Dx(), croppedRect.Dy(), imgWidth, imgHeight)
imgDraw := image.NewRGBA(croppedRect)
draw.Draw(imgDraw, croppedRect, img, topLeftPoint, draw.Src)
imgNew = resize.Resize(uint(width), uint(height), imgDraw, resize.Bilinear)
case CroppingModeKeepScale:
// If passed in dimensions are bigger use those of the image
if width > imgWidth {
width = imgWidth
}
if height > imgHeight {
height = imgHeight
}
croppedRect := image.Rect(0, 0, width, height)
topLeftPoint := calculateTopLeftPointFromGravity(gravity, width, height, imgWidth, imgHeight)
imgDraw := image.NewRGBA(croppedRect)
draw.Draw(imgDraw, croppedRect, img, topLeftPoint, draw.Src)
imgNew = imgDraw.SubImage(croppedRect)
}
// Filters
if parameters.filter == FilterGrayScale {
bounds := imgNew.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
gray := image.NewGray(bounds)
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
oldColor := imgNew.At(x, y)
grayColor := color.GrayModel.Convert(oldColor)
gray.Set(x, y, grayColor)
}
}
imgNew = gray
}
if transformation.watermark != nil {
w := transformation.watermark
var watermarkSrcScaled image.Image
var watermarkBounds image.Rectangle
// Try to load a scaled watermark first
if scale > 1 {
scaledPath, err := constructScaledPath(w.imagePath, scale)
if err != nil {
log.Println("Error:", err)
return
}
watermarkSrc, _, err := loadImage(scaledPath)
if err != nil {
log.Println("Error: could not load a watermark", err)
} else {
watermarkBounds = watermarkSrc.Bounds()
watermarkSrcScaled = watermarkSrc
}
}
if watermarkSrcScaled == nil {
watermarkSrc, _, err := loadImage(w.imagePath)
if err != nil {
log.Println("Error: could not load a watermark", err)
return
}
watermarkBounds = image.Rect(0, 0, watermarkSrc.Bounds().Max.X*scale, watermarkSrc.Bounds().Max.Y*scale)
watermarkSrcScaled = resize.Resize(uint(watermarkBounds.Max.X), uint(watermarkBounds.Max.Y), watermarkSrc, resize.Bilinear)
}
bounds := imgNew.Bounds()
// Make sure we have a transparent watermark if possible
watermark := image.NewRGBA(watermarkBounds)
draw.Draw(watermark, watermarkBounds, watermarkSrcScaled, watermarkBounds.Min, draw.Src)
pt := calculateTopLeftPointFromGravity(w.gravity, watermarkBounds.Dx(), watermarkBounds.Dy(), bounds.Dx(), bounds.Dy())
pt = pt.Add(getTranslation(w.gravity, w.x*scale, w.y*scale))
wX := pt.X
wY := pt.Y
watermarkRect := image.Rect(wX, wY, watermarkBounds.Dx()+wX, watermarkBounds.Dy()+wY)
finalImage := image.NewRGBA(bounds)
draw.Draw(finalImage, bounds, imgNew, bounds.Min, draw.Src)
draw.Draw(finalImage, watermarkRect, watermark, watermarkBounds.Min, draw.Over)
imgNew = finalImage.SubImage(bounds)
}
if transformation.texts != nil {
bounds := imgNew.Bounds()
rgba := image.NewRGBA(bounds)
draw.Draw(rgba, bounds, imgNew, image.ZP, draw.Src)
dpi := float64(72) // Multiply this by scale for a baaad time
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
for _, text := range transformation.texts {
size := float64(text.size * scale)
c.SetSrc(image.NewUniform(text.color))
c.SetFont(text.font)
c.SetFontSize(size)
fontMetrics := text.GetFontMetrics(scale)
width := int(c.PointToFix32(fontMetrics.width) >> 8)
height := int(c.PointToFix32(fontMetrics.height) >> 8)
pt := calculateTopLeftPointFromGravity(text.gravity, width, height, bounds.Dx(), bounds.Dy())
pt = pt.Add(getTranslation(text.gravity, text.x*scale, text.y*scale))
x := pt.X
y := pt.Y + int(c.PointToFix32(fontMetrics.ascent)>>8)
_, err := c.DrawString(text.content, freetype.Pt(x, y))
if err != nil {
log.Println("Error adding text:", err)
return
}
}
imgNew = rgba
}
return
}
func calculateTopLeftPointFromGravity(gravity string, width, height, imgWidth, imgHeight int) image.Point {
// Assuming width <= imgWidth && height <= imgHeight
switch gravity {
case GravityNorth:
return image.Point{(imgWidth - width) / 2, 0}
case GravityNorthEast:
return image.Point{imgWidth - width, 0}
case GravityEast:
return image.Point{imgWidth - width, (imgHeight - height) / 2}
case GravitySouthEast:
return image.Point{imgWidth - width, imgHeight - height}
case GravitySouth:
return image.Point{(imgWidth - width) / 2, imgHeight - height}
case GravitySouthWest:
return image.Point{0, imgHeight - height}
case GravityWest:
return image.Point{0, (imgHeight - height) / 2}
case GravityNorthWest:
return image.Point{0, 0}
case GravityCenter:
return image.Point{(imgWidth - width) / 2, (imgHeight - height) / 2}
}
panic("This point should not be reached")
}
// getTranslation returns a point specifying a translation by a given
// horizontal and vertical offset according to gravity
func getTranslation(gravity string, h, v int) image.Point {
switch gravity {
case GravityNorth:
return image.Point{0, v}
case GravityNorthEast:
return image.Point{-h, v}
case GravityEast:
return image.Point{-h, 0}
case GravitySouthEast:
return image.Point{-h, -v}
case GravitySouth:
return image.Point{0, -v}
case GravitySouthWest:
return image.Point{h, -v}
case GravityWest:
return image.Point{h, 0}
case GravityNorthWest:
return image.Point{h, v}
case GravityCenter:
return image.Point{0, 0}
}
panic("This point should not be reached")
}