-
Notifications
You must be signed in to change notification settings - Fork 8
/
image_set.go
114 lines (103 loc) · 2.04 KB
/
image_set.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
package imageutil
import (
"image"
_ "image/jpeg"
_ "image/png"
"os"
)
func NewImageSetFiles(filenames []string) (ImageMetaSet, error) {
imSet := ImageMetaSet{ImageMetas: []ImageMeta{}}
for _, filename := range filenames {
file, err := os.Open(filename)
if err != nil {
return imSet, err
}
img, formatName, err := image.Decode(file)
if err != nil {
return imSet, err
}
imSet.ImageMetas = append(imSet.ImageMetas, ImageMeta{
File: file,
Image: img,
FormatName: formatName,
})
}
err := imSet.CloseFilesAll()
return imSet, err
}
type ImageMetaSet struct {
ImageMetas []ImageMeta
}
func (ims *ImageMetaSet) CloseFilesAll() error {
for _, im := range ims.ImageMetas {
if im.File != nil {
err := im.File.Close()
if err != nil {
return err
}
}
}
return nil
}
func (ims *ImageMetaSet) Images() []image.Image {
imgs := []image.Image{}
for _, img := range ims.ImageMetas {
imgs = append(imgs, img.Image)
}
return imgs
}
func (ims *ImageMetaSet) MaxX() int {
maxX := 0
for _, im := range ims.ImageMetas {
if im.Image.Bounds().Dx() > maxX {
maxX = im.Image.Bounds().Dx()
}
}
return maxX
}
func (ims *ImageMetaSet) MaxY() int {
maxY := 0
for _, im := range ims.ImageMetas {
if im.Image.Bounds().Dy() > maxY {
maxY = im.Image.Bounds().Dy()
}
}
return maxY
}
func (ims *ImageMetaSet) SumX(maxIndexInclusive int) int {
sumX := 0
for i, im := range ims.ImageMetas {
if maxIndexInclusive >= 0 && i > maxIndexInclusive {
break
}
sumX += im.Image.Bounds().Dx()
}
return sumX
}
func (ims *ImageMetaSet) SumY(maxIndexInclusive int) int {
sumY := 0
for i, im := range ims.ImageMetas {
if maxIndexInclusive >= 0 && i > maxIndexInclusive {
break
}
sumY += im.Image.Bounds().Dy()
}
return sumY
}
func (ims *ImageMetaSet) Stats() ImagesStats {
return ImagesStats{
DxMax: ims.MaxX(),
DyMax: ims.MaxY(),
DxSum: ims.SumX(-1),
DySum: ims.SumY(-1)}
}
type ImagesStats struct {
Dxs []int
DxMax int
DxMin int
DxSum int
Dys []int
DyMax int
DyMin int
DySum int
}