forked from rai-project/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoders.go
166 lines (151 loc) · 4.65 KB
/
decoders.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
package image
import (
"image"
"image/color"
"io"
"github.com/pkg/errors"
"github.com/c3sr/image/types"
)
func decodeReader(decoder func(io.Reader) (image.Image, error), reader io.Reader, options *Options) (types.Image, error) {
img, err := decoder(reader)
if err != nil {
return nil, errors.Wrap(err, "unable decode reader as image")
}
if res, ok := img.(*types.RGBImage); ok && options.mode == types.RGBMode {
return res, nil
}
if res, ok := img.(*types.BGRImage); ok && options.mode == types.BGRMode {
return res, nil
}
model := img.ColorModel()
switch model {
case types.RGBModel:
// pp.Println("RGB Image")
if rgbImage, ok := img.(*types.RGBImage); ok {
return fromRGB(rgbImage, options)
}
return nil, errors.New("unable to cast from an rgb image")
case types.BGRModel:
if bgrImage, ok := img.(*types.BGRImage); ok {
return fromBGR(bgrImage, options)
}
return nil, errors.New("unable to cast from a bgr image")
case color.RGBAModel:
// pp.Println("RGBA Image")
if rgbaImage, ok := img.(*image.RGBA); ok {
return fromRGBA(rgbaImage, options)
}
return nil, errors.New("unable to cast from an rgba image")
case color.NRGBAModel:
// pp.Println("NRGBAModel Image")
if nrgbaImage, ok := img.(*image.NRGBA); ok {
return fromNRGBA(nrgbaImage, options)
}
return nil, errors.New("unable to cast from an nrgba image")
case color.GrayModel:
if grayImage, ok := img.(*image.Gray); ok {
return fromGray(grayImage, options)
}
return nil, errors.New("unable to cast from an nrgba image")
case color.YCbCrModel:
if ycbcrImage, ok := img.(*image.YCbCr); ok {
return fromYCBCR(ycbcrImage, options)
}
return nil, errors.New("unable to cast from an ycbcr image")
}
return nil, errors.New("expecting image to be in RGBA or NRGBA fromat")
}
func fromGray(grayImage *image.Gray, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
rgbImage := types.NewRGBImage(grayImage.Bounds())
if err := rgbImage.FillFromGrayImage(grayImage); err != nil {
return nil, err
}
return rgbImage, nil
case types.BGRMode:
bgrImage := types.NewBGRImage(grayImage.Bounds())
if err := bgrImage.FillFromGrayImage(grayImage); err != nil {
return nil, err
}
return bgrImage, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}
func fromNRGBA(nrgbaImage *image.NRGBA, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
rgbImage := types.NewRGBImage(nrgbaImage.Bounds())
if err := rgbImage.FillFromNRGBAImage(nrgbaImage); err != nil {
return nil, err
}
return rgbImage, nil
case types.BGRMode:
bgrImage := types.NewBGRImage(nrgbaImage.Bounds())
if err := bgrImage.FillFromNRGBAImage(nrgbaImage); err != nil {
return nil, err
}
return bgrImage, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}
func fromYCBCR(ycbcrImage *image.YCbCr, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
rgbImage := types.NewRGBImage(ycbcrImage.Bounds())
if err := rgbImage.FillFromYCBCRImage(ycbcrImage); err != nil {
return nil, err
}
return rgbImage, nil
case types.BGRMode:
bgrImage := types.NewBGRImage(ycbcrImage.Bounds())
if err := bgrImage.FillFromYCBCRImage(ycbcrImage); err != nil {
return nil, err
}
return bgrImage, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}
func fromRGB(rgbImage *types.RGBImage, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
return rgbImage, nil
case types.BGRMode:
res := types.NewBGRImage(rgbImage.Bounds())
if err := res.FillFromRGBImage(rgbImage); err != nil {
return nil, err
}
return res, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}
func fromBGR(bgrImage *types.BGRImage, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
res := types.NewRGBImage(bgrImage.Bounds())
if err := res.FillFromBGRImage(bgrImage); err != nil {
return nil, err
}
return res, nil
case types.BGRMode:
return bgrImage, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}
func fromRGBA(rgbaImage *image.RGBA, options *Options) (types.Image, error) {
switch options.mode {
case types.RGBMode:
rgbImage := types.NewRGBImage(rgbaImage.Bounds())
if err := rgbImage.FillFromRGBAImage(rgbaImage); err != nil {
return nil, err
}
return rgbImage, nil
case types.BGRMode:
bgrImage := types.NewBGRImage(rgbaImage.Bounds())
if err := bgrImage.FillFromRGBAImage(rgbaImage); err != nil {
return nil, err
}
return bgrImage, nil
}
return nil, errors.Errorf("invalid image mode %v", options.mode)
}