Skip to content

Commit

Permalink
Unexported frameDataFramer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Jan 31, 2024
1 parent 1910f27 commit a13f0e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
54 changes: 27 additions & 27 deletions frame_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type FrameData struct {
}

type frameDataFramer interface {
Height() int
ImageBufferSize(align int) (int, error)
ImageCopyToBuffer(b []byte, align int) (int, error)
Linesize(i int) int
PixelFormat() PixelFormat
PlaneBytes(i int) []byte
Width() int
height() int
imageBufferSize(align int) (int, error)
imageCopyToBuffer(b []byte, align int) (int, error)
linesize(i int) int
pixelFormat() PixelFormat
planeBytes(i int) []byte
width() int
}

func newFrameData(f frameDataFramer) *FrameData {
Expand All @@ -30,9 +30,9 @@ func newFrameData(f frameDataFramer) *FrameData {
func (d *FrameData) Bytes(align int) ([]byte, error) {
switch {
// Video
case d.f.Height() > 0 && d.f.Width() > 0:
case d.f.height() > 0 && d.f.width() > 0:
// Get buffer size
s, err := d.f.ImageBufferSize(align)
s, err := d.f.imageBufferSize(align)
if err != nil {
return nil, fmt.Errorf("astiav: getting image buffer size failed: %w", err)
}
Expand All @@ -46,7 +46,7 @@ func (d *FrameData) Bytes(align int) ([]byte, error) {
b := make([]byte, s)

// Copy image to buffer
if _, err = d.f.ImageCopyToBuffer(b, align); err != nil {
if _, err = d.f.imageCopyToBuffer(b, align); err != nil {
return nil, fmt.Errorf("astiav: copying image to buffer failed: %w", err)
}
return b, nil
Expand All @@ -57,7 +57,7 @@ func (d *FrameData) Bytes(align int) ([]byte, error) {
// Always returns non-premultiplied formats when dealing with alpha channels, however this might not
// always be accurate. In this case, use your own format in .ToImage()
func (d *FrameData) GuessImageFormat() (image.Image, error) {
switch d.f.PixelFormat() {
switch d.f.pixelFormat() {
case PixelFormatGray8:
return &image.Gray{}, nil
case PixelFormatGray16Be:
Expand All @@ -80,11 +80,11 @@ func (d *FrameData) GuessImageFormat() (image.Image, error) {
PixelFormatYuv444P, PixelFormatYuvj444P:
return &image.YCbCr{}, nil
}
return nil, fmt.Errorf("astiav: pixel format %s not handled by Go", d.f.PixelFormat())
return nil, fmt.Errorf("astiav: pixel format %s not handled by Go", d.f.pixelFormat())
}

func (d *FrameData) imageYCbCrSubsampleRatio() image.YCbCrSubsampleRatio {
name := d.f.PixelFormat().Name()
name := d.f.pixelFormat().Name()
for s, r := range map[string]image.YCbCrSubsampleRatio{
"410": image.YCbCrSubsampleRatio410,
"411": image.YCbCrSubsampleRatio411,
Expand All @@ -101,7 +101,7 @@ func (d *FrameData) imageYCbCrSubsampleRatio() image.YCbCrSubsampleRatio {
}

func (d *FrameData) copyPlaneBytes(i int, s *[]uint8) {
b := d.f.PlaneBytes(i)
b := d.f.planeBytes(i)
if len(b) > cap(*s) {
*s = make([]uint8, len(b))
}
Expand All @@ -110,10 +110,10 @@ func (d *FrameData) copyPlaneBytes(i int, s *[]uint8) {

func (d *FrameData) toImagePix(pix *[]uint8, stride *int, rect *image.Rectangle) {
d.copyPlaneBytes(0, pix)
if v := d.f.Linesize(0); *stride != v {
if v := d.f.linesize(0); *stride != v {
*stride = v
}
if w, h := d.f.Width(), d.f.Height(); rect.Dy() != w || rect.Dx() != h {
if w, h := d.f.width(), d.f.height(); rect.Dy() != w || rect.Dx() != h {
*rect = image.Rect(0, 0, w, h)
}
}
Expand All @@ -122,24 +122,24 @@ func (d *FrameData) toImageYCbCr(y, cb, cr *[]uint8, yStride, cStride *int, subs
d.copyPlaneBytes(0, y)
d.copyPlaneBytes(1, cb)
d.copyPlaneBytes(2, cr)
if v := d.f.Linesize(0); *yStride != v {
if v := d.f.linesize(0); *yStride != v {
*yStride = v
}
if v := d.f.Linesize(1); *cStride != v {
if v := d.f.linesize(1); *cStride != v {
*cStride = v
}
if v := d.imageYCbCrSubsampleRatio(); *subsampleRatio != v {
*subsampleRatio = v
}
if w, h := d.f.Width(), d.f.Height(); rect.Dy() != w || rect.Dx() != h {
if w, h := d.f.width(), d.f.height(); rect.Dy() != w || rect.Dx() != h {
*rect = image.Rect(0, 0, w, h)
}
}

func (d *FrameData) toImageYCbCrA(y, cb, cr, a *[]uint8, yStride, cStride, aStride *int, subsampleRatio *image.YCbCrSubsampleRatio, rect *image.Rectangle) {
d.toImageYCbCr(y, cb, cr, yStride, cStride, subsampleRatio, rect)
d.copyPlaneBytes(3, a)
if v := d.f.Linesize(3); *aStride != v {
if v := d.f.linesize(3); *aStride != v {
*aStride = v
}
}
Expand Down Expand Up @@ -183,33 +183,33 @@ func newFrameDataFrame(f *Frame) *frameDataFrame {
return &frameDataFrame{f: f}
}

func (f *frameDataFrame) Height() int {
func (f *frameDataFrame) height() int {
return f.f.Height()
}

func (f *frameDataFrame) ImageBufferSize(align int) (int, error) {
func (f *frameDataFrame) imageBufferSize(align int) (int, error) {
return f.f.ImageBufferSize(align)
}

func (f *frameDataFrame) ImageCopyToBuffer(b []byte, align int) (int, error) {
func (f *frameDataFrame) imageCopyToBuffer(b []byte, align int) (int, error) {
return f.f.ImageCopyToBuffer(b, align)
}

func (f *frameDataFrame) Linesize(i int) int {
func (f *frameDataFrame) linesize(i int) int {
return f.f.Linesize()[i]
}

func (f *frameDataFrame) PixelFormat() PixelFormat {
func (f *frameDataFrame) pixelFormat() PixelFormat {
return f.f.PixelFormat()
}

func (f *frameDataFrame) PlaneBytes(i int) []byte {
func (f *frameDataFrame) planeBytes(i int) []byte {
return bytesFromC(func(size *cUlong) *C.uint8_t {
*size = cUlong(int(f.f.c.linesize[i]) * f.f.Height())
return f.f.c.data[i]
})
}

func (f *frameDataFrame) Width() int {
func (f *frameDataFrame) width() int {
return f.f.Width()
}
34 changes: 17 additions & 17 deletions frame_data_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ import (
)

type mockedFrameDataFrame struct {
height int
h int
imageBytes []byte
linesizes []int
pixelFormat PixelFormat
pf PixelFormat
planesBytes [][]byte
width int
w int
}

var _ frameDataFramer = (*mockedFrameDataFrame)(nil)

func (f *mockedFrameDataFrame) Height() int {
return f.height
func (f *mockedFrameDataFrame) height() int {
return f.h
}

func (f *mockedFrameDataFrame) ImageBufferSize(align int) (int, error) {
func (f *mockedFrameDataFrame) imageBufferSize(align int) (int, error) {
return len(f.imageBytes), nil
}

func (f *mockedFrameDataFrame) ImageCopyToBuffer(b []byte, align int) (int, error) {
func (f *mockedFrameDataFrame) imageCopyToBuffer(b []byte, align int) (int, error) {
copy(b, f.imageBytes)
return len(f.imageBytes), nil
}

func (f *mockedFrameDataFrame) Linesize(i int) int {
func (f *mockedFrameDataFrame) linesize(i int) int {
return f.linesizes[i]
}

func (f *mockedFrameDataFrame) PixelFormat() PixelFormat {
return f.pixelFormat
func (f *mockedFrameDataFrame) pixelFormat() PixelFormat {
return f.pf
}

func (f *mockedFrameDataFrame) PlaneBytes(i int) []byte {
func (f *mockedFrameDataFrame) planeBytes(i int) []byte {
return f.planesBytes[i]
}

func (f *mockedFrameDataFrame) Width() int {
return f.width
func (f *mockedFrameDataFrame) width() int {
return f.w
}

func TestFrameDataInternal(t *testing.T) {
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestFrameDataInternal(t *testing.T) {
},
} {
for _, pf := range v.pfs {
fdf.pixelFormat = pf
fdf.pf = pf
i, err := fd.GuessImageFormat()
if v.err {
require.Error(t, err)
Expand All @@ -124,8 +124,8 @@ func TestFrameDataInternal(t *testing.T) {
fdf.imageBytes = []byte{0, 1, 2, 3}
_, err := fd.Bytes(0)
require.Error(t, err)
fdf.height = 1
fdf.width = 2
fdf.h = 1
fdf.w = 2
b, err := fd.Bytes(0)
require.NoError(t, err)
require.Equal(t, fdf.imageBytes, b)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestFrameDataInternal(t *testing.T) {
},
} {
fdf.linesizes = v.linesizes
fdf.pixelFormat = v.pixelFormat
fdf.pf = v.pixelFormat
fdf.planesBytes = v.planesBytes
err = fd.ToImage(v.i)
if v.err {
Expand Down

0 comments on commit a13f0e5

Please sign in to comment.