Skip to content

Commit 84476c3

Browse files
committed
Added optional buffer for image Read()
1 parent 9f884ae commit 84476c3

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ For the `SetFrameBuffer()` method, the `buffer` parameter must have a length of
136136

137137
## Images
138138

139-
Vidio provides some convenience functions for reading and writing to images using an array of bytes. Currently, only `png` and `jpeg` formats are supported.
139+
Vidio provides some convenience functions for reading and writing to images using an array of bytes. Currently, only `png` and `jpeg` formats are supported. When reading images, an optional `buffer` can be passed in to avoid array reallocation.
140+
141+
```go
142+
Read(filename string, buffer ...[]byte) (int, int, []byte, error)
143+
Write(filename string, width, height int, buffer []byte) error
144+
```
145+
```
140146
141147
```go
142148
w, h, img, err := vidio.Read("input.png")

imageio.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package vidio
22

33
import (
4+
"errors"
5+
"fmt"
46
"image"
57
"os"
68
"strings"
@@ -13,7 +15,7 @@ import (
1315
)
1416

1517
// Reads an image from a file. Currently only supports png and jpeg.
16-
func Read(filename string) (int, int, []byte, error) {
18+
func Read(filename string, buffer ...[]byte) (int, int, []byte, error) {
1719
f, err := os.Open(filename)
1820
if err != nil {
1921
return 0, 0, nil, err
@@ -28,7 +30,16 @@ func Read(filename string) (int, int, []byte, error) {
2830
bounds := image.Bounds().Max
2931
size := bounds.X * bounds.Y * 3
3032

31-
data := make([]byte, size)
33+
var data []byte
34+
if len(buffer) > 0 {
35+
if len(buffer[0]) < size {
36+
errmsg := fmt.Sprintf("Buffer size (%d) is smaller than image size (%d)", len(buffer[0]), size)
37+
return 0, 0, nil, errors.New(errmsg)
38+
}
39+
data = buffer[0]
40+
} else {
41+
data = make([]byte, size, size)
42+
}
3243

3344
index := 0
3445
for h := 0; h < bounds.Y; h++ {
@@ -47,17 +58,18 @@ func Read(filename string) (int, int, []byte, error) {
4758
}
4859

4960
// Writes an image to a file. Currently only supports png and jpeg.
50-
func Write(filename string, width, height int, data []byte) error {
61+
func Write(filename string, width, height int, buffer []byte) error {
5162
f, err := os.Create(filename)
5263
if err != nil {
5364
return err
5465
}
5566
defer f.Close()
67+
5668
image := image.NewRGBA(image.Rect(0, 0, width, height))
5769
index := 0
5870
for h := 0; h < height; h++ {
5971
for w := 0; w < width; w++ {
60-
r, g, b := data[index], data[index+1], data[index+2]
72+
r, g, b := buffer[index], buffer[index+1], buffer[index+2]
6173
image.Set(w, h, color.RGBA{r, g, b, 255})
6274
index += 3
6375
}

test/bananas.jpg

2.31 KB
Loading

vidio_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
)
88

9-
func assertEquals(expected, actual interface{}) {
9+
func assertEquals(actual, expected interface{}) {
1010
if expected != actual {
1111
panic(fmt.Sprintf("Expected %v, got %v", expected, actual))
1212
}
@@ -226,3 +226,42 @@ At least one output file must be specified`,
226226

227227
fmt.Println("Webcam Parsing Test Passed")
228228
}
229+
230+
func TestImageRead(t *testing.T) {
231+
w, h, img, err := Read("test/bananas.jpg")
232+
if err != nil {
233+
panic(err)
234+
}
235+
236+
assertEquals(w, 200)
237+
assertEquals(h, 133)
238+
assertEquals(len(img), 200*133*3)
239+
// [255 221 189 255 221 189 255 222 186 255]
240+
assertEquals(img[0], uint8(255))
241+
assertEquals(img[1], uint8(221))
242+
assertEquals(img[2], uint8(189))
243+
assertEquals(img[3], uint8(255))
244+
assertEquals(img[4], uint8(221))
245+
assertEquals(img[5], uint8(189))
246+
assertEquals(img[6], uint8(255))
247+
assertEquals(img[7], uint8(222))
248+
assertEquals(img[8], uint8(186))
249+
assertEquals(img[9], uint8(255))
250+
251+
fmt.Println("Image Reading Test Passed")
252+
}
253+
254+
func TestImageWrite(t *testing.T) {
255+
w, h, img, err := Read("test/bananas.jpg")
256+
if err != nil {
257+
panic(err)
258+
}
259+
err = Write("test/bananas-out.png", w, h, img)
260+
if err != nil {
261+
panic(err)
262+
}
263+
264+
os.Remove("test/bananas-out.png")
265+
266+
fmt.Println("Image Writing Test Passed")
267+
}

0 commit comments

Comments
 (0)