-
Notifications
You must be signed in to change notification settings - Fork 8
/
meta.go
70 lines (59 loc) · 1.44 KB
/
meta.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
package imageutil
import (
"image"
"image/color"
"os"
"github.com/grokify/mogo/image/colors"
)
func ImageAspect(img image.Image) float64 {
return Aspect(img.Bounds().Dx(), img.Bounds().Dy())
}
func Aspect(width, height int) float64 {
return float64(width) / float64(height)
}
func NegativeOffset(width, height, offset uint) image.Point {
return image.Pt(int(width-offset), int(height-offset))
}
func IsNilOrEmpty(img image.Image) bool {
if img == nil || img.Bounds().Dx() == 0 || img.Bounds().Dy() == 0 {
return true
}
return false
}
type ImageMeta struct {
File *os.File
FormatName string
Image image.Image
}
func NewImageMeta(img image.Image) ImageMeta {
return ImageMeta{Image: img}
}
func (meta *ImageMeta) ColorAverage() color.Color {
return colors.ColorAverageImage(meta.Image)
}
func RectanglePixelCount(r image.Rectangle) int {
w := r.Dx()
h := r.Dy()
if w <= 0 || h <= 0 {
return 0
}
return w * h
}
// ImageColors returns colors for an image covering the pixels
// described in `image.Rectangle`: https://pkg.go.dev/image#Rectangle
func ImageColors(img image.Image) [][]color.Color {
rows := [][]color.Color{}
minPt := img.Bounds().Min
maxPt := img.Bounds().Max
if minPt.X == maxPt.X || minPt.Y == maxPt.Y {
return rows
}
for y := minPt.Y; y < maxPt.Y; y++ {
row := []color.Color{}
for x := minPt.X; x < maxPt.X; x++ {
row = append(row, img.At(x, y))
}
rows = append(rows, row)
}
return rows
}