-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage2tiles.go
201 lines (159 loc) · 5.94 KB
/
image2tiles.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
package image2tiles
import (
"errors"
"fmt"
"github.com/disintegration/imaging"
"image"
"image/color"
"math"
"os"
"path"
"strings"
)
var (
resampleFilter = imaging.Lanczos
)
type Converter struct {
Image image.Image
ImageWidth int
ImageHeight int
OriginalImageWidth int
OriginalImageHeight int
MaxZoom int
TileSize [2]int
}
func NewConverter() *Converter {
return &Converter{}
}
// Prepare a large image for tiling
func (converter *Converter) Prepare(imageName string, bgColor string) error {
originalImage, err := LoadImage(imageName)
if err != nil {
return errors.New("open newImage file failed: " + err.Error())
}
originalImageWidth := originalImage.Bounds().Dx()
originalImageHeight := originalImage.Bounds().Dy()
imageWidth := 1
imageHeight := 1
for imageWidth < originalImageWidth || imageHeight < originalImageHeight {
imageWidth *= 2
imageHeight *= 2
}
fillColor := ParseHexColor(bgColor)
newImage := imaging.New(imageWidth, imageHeight, fillColor)
newImage = imaging.Paste(newImage, originalImage, image.Pt((imageWidth-originalImageWidth)/2, (imageHeight-originalImageHeight)/2))
converter.Image = newImage
converter.ImageWidth = imageWidth
converter.ImageHeight = imageHeight
converter.OriginalImageWidth = originalImageWidth
converter.OriginalImageHeight = originalImageHeight
return nil
}
// Tile Extract a single tile from a larger image
func (converter *Converter) Tile(level int, size [2]int, quadrant [2]int, efficient bool) (image.Image, error) {
scale := int(math.Pow(2, float64(level)))
// crop out the area of interest first, then scale and copy it
if efficient {
inverseSize := [2]float64{
float64(converter.ImageHeight) / float64(size[0]*scale),
float64(converter.ImageHeight) / float64(size[1]*scale),
}
topLeft := [2]int{
int(float64(quadrant[0]) * float64(size[0]) * inverseSize[0]),
int(float64(quadrant[1]) * float64(size[1]) * inverseSize[1]),
}
bottomRight := [2]int{
topLeft[0] + int(float64(size[0])*inverseSize[0]),
topLeft[1] + int(float64(size[1])*inverseSize[1]),
}
if inverseSize[0] < 1 || inverseSize[1] < 1 {
return nil, fmt.Errorf("requested zoom level (%d) is too high", level)
}
zoomed := imaging.Crop(converter.Image, image.Rect(topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]))
zoomed = imaging.Resize(zoomed, size[0], size[1], resampleFilter)
return zoomed, nil
}
// copy the whole image, scale it and then crop out the area of interest
newSize := [2]int{size[0] * scale, size[0] * scale}
topLeft := [2]int{quadrant[0] * size[0], quadrant[1] * size[1]}
bottomRight := [2]int{topLeft[0] + size[0], topLeft[1] + size[1]}
if newSize[0] > converter.ImageWidth || newSize[1] > converter.ImageHeight {
return nil, fmt.Errorf("requested zoom level (%d) is too high", level)
}
zoomed := imaging.Clone(converter.Image)
zoomed = imaging.Resize(zoomed, newSize[0], newSize[1], resampleFilter)
zoomed = imaging.Crop(zoomed, image.Rect(topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]))
return zoomed, nil
}
// saveTile Save tile image
func (converter *Converter) saveTile(img image.Image, level int, quadrant [2]int, imageQuality int, output string) (string, error) {
filePath := fmt.Sprintf(output, level, quadrant[0], quadrant[1])
if err := os.MkdirAll(path.Dir(filePath), os.ModePerm); err != nil {
return filePath, fmt.Errorf("create output directory: %v", err)
}
if isPng := strings.ToLower(path.Ext(filePath)); isPng == ".png" {
if err := SavePNG(filePath, img); err != nil {
return filePath, err
}
} else {
if err := SaveJPG(filePath, img, imageQuality); err != nil {
return filePath, err
}
}
return filePath, nil
}
// subdivide Recursively subdivide a large image into small tiles
func (converter *Converter) subdivide(level int, size [2]int, quadrant [2]int, efficient bool, imageQuality int, output string) (image.Image, error) {
if converter.ImageWidth <= size[0]*int(math.Pow(2, float64(level))) {
outImg, err := converter.Tile(level, size, quadrant, efficient)
if err != nil {
return nil, err
}
filePath, err := converter.saveTile(outImg, level, quadrant, imageQuality, output)
if err != nil {
return nil, err
}
if level > converter.MaxZoom {
converter.MaxZoom = level
}
converter.TileSize = size
fmt.Printf("level %d quadrant %v filePath %s \n", level, quadrant, filePath)
return outImg, nil
}
outImg := imaging.New(size[0]*2, size[1]*2, color.NRGBA{})
if img, err := converter.subdivide(level+1, size, [2]int{quadrant[0]*2 + 0, quadrant[1]*2 + 0}, efficient, imageQuality, output); err != nil {
return nil, err
} else {
outImg = imaging.Paste(outImg, img, image.Pt(0, 0))
}
if img, err := converter.subdivide(level+1, size, [2]int{quadrant[0]*2 + 0, quadrant[1]*2 + 1}, efficient, imageQuality, output); err != nil {
return nil, err
} else {
outImg = imaging.Paste(outImg, img, image.Pt(0, size[1]))
}
if img, err := converter.subdivide(level+1, size, [2]int{quadrant[0]*2 + 1, quadrant[1]*2 + 0}, efficient, imageQuality, output); err != nil {
return nil, err
} else {
outImg = imaging.Paste(outImg, img, image.Pt(size[0], 0))
}
if img, err := converter.subdivide(level+1, size, [2]int{quadrant[0]*2 + 1, quadrant[1]*2 + 1}, efficient, imageQuality, output); err != nil {
return nil, err
} else {
outImg = imaging.Paste(outImg, img, image.Pt(size[0], size[1]))
}
outImg = imaging.Resize(outImg, size[0], size[1], resampleFilter)
filePath, err := converter.saveTile(outImg, level, quadrant, imageQuality, output)
if err != nil {
return nil, err
}
fmt.Printf("level %d quadrant %v filePath %s \n", level, quadrant, filePath)
return outImg, nil
}
// Execute a large image into small tiles
func (converter *Converter) Execute(size [2]int, efficient bool, imageQuality int, output string) error {
_, err := converter.subdivide(0, size, [2]int{0, 0}, efficient, imageQuality, output)
if err != nil {
return err
}
return nil
}