-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.go
52 lines (43 loc) · 1.11 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
package helios
import (
"errors"
"fmt"
i "image"
"image/png"
"os"
)
var ImageNotFoundError = errors.New("file not found")
var ImageUnsupportedFormatError = errors.New("unsupported format")
var ImageUnknownError = errors.New("unknown error")
type Image struct {
img i.Image
path string
confidenceThreshold float64
}
func (i *Image) GetImage() i.Image {
return i.img
}
func (i *Image) GetPath() string {
return i.path
}
func NewImage(path string, confidenceThreshold float64) (*Image, error) {
file, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("%w: %s", ImageNotFoundError, path)
}
if err != nil {
return nil, fmt.Errorf("%w: %s", ImageUnknownError, err.Error())
}
defer func() { _ = file.Close() }()
// Must specifically use jpeg.Decode() or it
// would encounter unknown format error
image, err := png.Decode(file)
if err != nil {
return nil, fmt.Errorf("%w: %s", ImageUnsupportedFormatError, err.Error())
}
return &Image{
img: image,
path: path,
confidenceThreshold: confidenceThreshold,
}, nil
}