forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_file_test.go
152 lines (131 loc) · 5.74 KB
/
image_file_test.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
package rimage
import (
"bytes"
"context"
"image"
"image/png"
"os"
"testing"
libjpeg "github.com/viam-labs/go-libjpeg/jpeg"
"go.viam.com/test"
"go.viam.com/utils/artifact"
"go.viam.com/rdk/utils"
)
func TestPngEncodings(t *testing.T) {
t.Parallel()
openBytes, err := os.ReadFile(artifact.MustPath("rimage/opencv_encoded_image.png"))
test.That(t, err, test.ShouldBeNil)
openGray16, err := png.Decode(bytes.NewReader(openBytes))
test.That(t, err, test.ShouldBeNil)
goBytes, err := os.ReadFile(artifact.MustPath("rimage/go_encoded_image.png"))
test.That(t, err, test.ShouldBeNil)
goGray16, err := png.Decode(bytes.NewReader(goBytes))
test.That(t, err, test.ShouldBeNil)
test.That(t, openBytes, test.ShouldNotResemble, goBytes) // go and openCV encode PNGs differently
test.That(t, openGray16, test.ShouldResemble, goGray16) // but they decode to the same image
var goEncodedOpenCVBytes bytes.Buffer
err = png.Encode(&goEncodedOpenCVBytes, openGray16)
test.That(t, err, test.ShouldBeNil)
test.That(t, goEncodedOpenCVBytes.Bytes(), test.ShouldResemble, goBytes)
}
func TestDecodeImage(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 4, 8))
img.Set(3, 3, Red)
var buf bytes.Buffer
test.That(t, png.Encode(&buf, img), test.ShouldBeNil)
t.Run("lazy", func(t *testing.T) {
decoded, err := DecodeImage(context.Background(), buf.Bytes(), utils.WithLazyMIMEType(utils.MimeTypePNG))
test.That(t, err, test.ShouldBeNil)
test.That(t, decoded, test.ShouldHaveSameTypeAs, &LazyEncodedImage{})
decodedLazy := decoded.(*LazyEncodedImage)
test.That(t, decodedLazy.RawData(), test.ShouldResemble, buf.Bytes())
test.That(t, decodedLazy.Bounds(), test.ShouldResemble, img.Bounds())
})
}
func TestEncodeImage(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 4, 8))
img.Set(3, 3, Red)
var buf bytes.Buffer
test.That(t, png.Encode(&buf, img), test.ShouldBeNil)
var bufJPEG bytes.Buffer
test.That(t, libjpeg.Encode(&bufJPEG, img, jpegEncoderOptions), test.ShouldBeNil)
t.Run("lazy", func(t *testing.T) {
// fast
lazyImg := NewLazyEncodedImage(buf.Bytes(), "hehe")
encoded, err := EncodeImage(context.Background(), lazyImg, "hehe")
test.That(t, err, test.ShouldBeNil)
test.That(t, encoded, test.ShouldResemble, buf.Bytes())
lazyImg = NewLazyEncodedImage(buf.Bytes(), "hehe")
encoded, err = EncodeImage(context.Background(), lazyImg, utils.WithLazyMIMEType("hehe"))
test.That(t, err, test.ShouldBeNil)
test.That(t, encoded, test.ShouldResemble, buf.Bytes())
})
t.Run("jpeg from png", func(t *testing.T) {
lazyImg := NewLazyEncodedImage(buf.Bytes(), utils.MimeTypePNG)
encoded, err := EncodeImage(context.Background(), lazyImg, utils.MimeTypeJPEG)
test.That(t, err, test.ShouldBeNil)
test.That(t, encoded, test.ShouldResemble, bufJPEG.Bytes())
})
}
func TestRawRGBAEncodingDecoding(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 4, 8))
img.Set(3, 3, Red)
encodedImgBytes, err := EncodeImage(context.Background(), img, utils.MimeTypeRawRGBA)
test.That(t, err, test.ShouldBeNil)
reader := bytes.NewReader(encodedImgBytes)
conf, header, err := image.DecodeConfig(reader)
test.That(t, err, test.ShouldBeNil)
test.That(t, header, test.ShouldEqual, "vnd.viam.rgba")
test.That(t, conf.Width, test.ShouldEqual, img.Bounds().Dx())
test.That(t, conf.Height, test.ShouldEqual, img.Bounds().Dy())
// decode with image package
reader = bytes.NewReader(encodedImgBytes)
imgDecoded, header, err := image.Decode(reader)
test.That(t, err, test.ShouldBeNil)
test.That(t, header, test.ShouldEqual, "vnd.viam.rgba")
test.That(t, imgDecoded.Bounds(), test.ShouldResemble, img.Bounds())
test.That(t, imgDecoded.At(3, 6), test.ShouldResemble, img.At(3, 6))
test.That(t, imgDecoded.At(1, 3), test.ShouldResemble, img.At(1, 3))
decodedImg, err := DecodeImage(context.Background(), encodedImgBytes, utils.MimeTypeRawRGBA)
test.That(t, err, test.ShouldBeNil)
test.That(t, decodedImg.Bounds(), test.ShouldResemble, img.Bounds())
imgR, imgG, imgB, imgA := img.At(3, 3).RGBA()
decodedImgR, decodedImgG, decodedImgB, decodedImgA := decodedImg.At(3, 3).RGBA()
test.That(t, imgR, test.ShouldResemble, decodedImgR)
test.That(t, imgG, test.ShouldResemble, decodedImgG)
test.That(t, imgB, test.ShouldResemble, decodedImgB)
test.That(t, imgA, test.ShouldResemble, decodedImgA)
}
func TestRawDepthEncodingDecoding(t *testing.T) {
img := NewEmptyDepthMap(4, 8)
for x := 0; x < 4; x++ {
for y := 0; y < 8; y++ {
img.Set(x, y, Depth(x*y))
}
}
encodedImgBytes, err := EncodeImage(context.Background(), img, utils.MimeTypeRawDepth)
test.That(t, err, test.ShouldBeNil)
reader := bytes.NewReader(encodedImgBytes)
// decode Header
conf, header, err := image.DecodeConfig(reader)
test.That(t, err, test.ShouldBeNil)
test.That(t, header, test.ShouldEqual, "vnd.viam.dep")
test.That(t, conf.Width, test.ShouldEqual, img.Bounds().Dx())
test.That(t, conf.Height, test.ShouldEqual, img.Bounds().Dy())
// decode with image package
reader = bytes.NewReader(encodedImgBytes)
imgDecoded, header, err := image.Decode(reader)
test.That(t, err, test.ShouldBeNil)
test.That(t, header, test.ShouldEqual, "vnd.viam.dep")
test.That(t, imgDecoded.Bounds(), test.ShouldResemble, img.Bounds())
test.That(t, imgDecoded.At(2, 3), test.ShouldResemble, img.At(2, 3))
test.That(t, imgDecoded.At(1, 0), test.ShouldResemble, img.At(1, 0))
// decode with rimage package
decodedImg, err := DecodeImage(context.Background(), encodedImgBytes, utils.MimeTypeRawDepth)
test.That(t, err, test.ShouldBeNil)
test.That(t, decodedImg.Bounds(), test.ShouldResemble, img.Bounds())
decodedDm, ok := decodedImg.(*DepthMap)
test.That(t, ok, test.ShouldBeTrue)
test.That(t, decodedDm.GetDepth(2, 3), test.ShouldEqual, img.GetDepth(2, 3))
test.That(t, decodedDm.GetDepth(1, 0), test.ShouldEqual, img.GetDepth(1, 0))
}