Skip to content

Commit

Permalink
Added real png test for FrameData
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Jan 26, 2024
1 parent 7c941fe commit 27ef0bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions frame_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package astiav_test

import (
"image"
"image/png"
"os"
"testing"

"github.com/asticode/go-astiav"
Expand Down Expand Up @@ -49,8 +51,8 @@ func (f *frameDataFrame) Width() int {
}

func TestFrameData(t *testing.T) {
f := &frameDataFrame{}
fd := astiav.NewFrameData(f)
fdf := &frameDataFrame{}
fd := astiav.NewFrameData(fdf)

for _, v := range []struct {
err bool
Expand Down Expand Up @@ -112,7 +114,7 @@ func TestFrameData(t *testing.T) {
},
} {
for _, pf := range v.pfs {
f.pixelFormat = pf
fdf.pixelFormat = pf
i, err := fd.GuessImageFormat()
if v.err {
require.Error(t, err)
Expand All @@ -122,14 +124,14 @@ func TestFrameData(t *testing.T) {
}
}

f.imageBytes = []byte{0, 1, 2, 3}
fdf.imageBytes = []byte{0, 1, 2, 3}
_, err := fd.Bytes(0)
require.Error(t, err)
f.height = 1
f.width = 2
fdf.height = 1
fdf.width = 2
b, err := fd.Bytes(0)
require.NoError(t, err)
require.Equal(t, f.imageBytes, b)
require.Equal(t, fdf.imageBytes, b)

for _, v := range []struct {
e image.Image
Expand Down Expand Up @@ -273,14 +275,41 @@ func TestFrameData(t *testing.T) {
planesBytes: [][]byte{{0, 1}, {2, 3}, {4, 5}},
},
} {
f.linesizes = v.linesizes
f.pixelFormat = v.pixelFormat
f.planesBytes = v.planesBytes
fdf.linesizes = v.linesizes
fdf.pixelFormat = v.pixelFormat
fdf.planesBytes = v.planesBytes
err = fd.ToImage(v.i)
if v.err {
require.Error(t, err)
} else {
require.Equal(t, v.e, v.i)
}
}

const (
name = "image-rgba"
ext = "png"
)
f, err := globalHelper.inputLastFrame(name+"."+ext, astiav.MediaTypeVideo)
require.NoError(t, err)
fd = f.Data()

b1, err := fd.Bytes(1)
require.NoError(t, err)

b2, err := os.ReadFile("testdata/" + name + "-bytes")
require.NoError(t, err)
require.Equal(t, b1, b2)

f1, err := os.Open("testdata/" + name + "." + ext)
require.NoError(t, err)
defer f1.Close()

i1, err := fd.GuessImageFormat()
require.NoError(t, err)
require.NoError(t, err)
require.NoError(t, fd.ToImage(i1))
i2, err := png.Decode(f1)
require.NoError(t, err)
require.Equal(t, i1, i2)
}
Binary file added testdata/image-rgba-bytes
Binary file not shown.
Binary file added testdata/image-rgba.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27ef0bb

Please sign in to comment.