Skip to content

Commit

Permalink
simplify webpDecodeGray
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Feb 11, 2016
1 parent f350dc0 commit 8e31b54
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 60 deletions.
21 changes: 0 additions & 21 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@ func BenchmarkDecodeGray(b *testing.B) {
b.StopTimer()
}

func BenchmarkDecodeGrayEx(b *testing.B) {
data, err := ioutil.ReadFile("./testdata/1_webp_ll.webp")
if err != nil {
b.Fatal(err)
}
cbuf := NewCBuffer(len(data))
copy(cbuf.CData(), data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m, pix, err := DecodeGrayEx(cbuf.CData(), cbuf)
if err != nil {
b.Fatal(err)
}
_ = m
b.StopTimer()
pix.Close()
b.StartTimer()
}
b.StopTimer()
}

func BenchmarkDecodeRGB(b *testing.B) {
data, err := ioutil.ReadFile("./testdata/1_webp_ll.webp")
if err != nil {
Expand Down
27 changes: 7 additions & 20 deletions capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ package webp
#include <stdlib.h>
#include <string.h>
struct cgoWebpDecodeGrayReturn {
int ok;
int width;
int height;
uint8_t* ptr;
} cgoWebpDecodeGray(const uint8_t* data, size_t data_size) {
struct cgoWebpDecodeGrayReturn t;
t.ptr = webpDecodeGray(data, data_size, &t.width, &t.height);
t.ok = (t.ptr != NULL)? 1: 0;
return t;
}
struct cgoWebpDecodeRGBReturn {
int ok;
int width;
Expand Down Expand Up @@ -247,23 +235,22 @@ func webpGetInfo(data []byte) (width, height int, hasAlpha bool, err error) {
return
}

func webpDecodeGray(data []byte, cbuf CBuffer) (pix CBuffer, width, height int, err error) {
func webpDecodeGray(data []byte) (pix []byte, width, height int, err error) {
if len(data) == 0 {
err = errors.New("webpDecodeGray: bad arguments")
return
}
isCBuf := cbuf.Own(data)
cData := cgoSafePtr(data, isCBuf)
defer cgoFreePtr(cData, isCBuf)

rv := C.cgoWebpDecodeGray((*C.uint8_t)(cData), C.size_t(len(data)))
if rv.ok != 1 {
var cw, ch C.int
var cptr = C.webpDecodeGray((*C.uint8_t)(unsafe.Pointer(&data[0])), C.size_t(len(data)), &cw, &ch)
if cptr == nil {
err = errors.New("webpDecodeGray: failed")
return
}
defer C.free(unsafe.Pointer(cptr))

width, height = int(rv.width), int(rv.height)
pix = newCBufferFrom(unsafe.Pointer(rv.ptr), width*height*1)
pix = append([]byte{}, ((*[1 << 30]byte)(unsafe.Pointer(cptr)))[0:len(pix):len(pix)]...)
width, height = int(cw), int(ch)
return
}

Expand Down
21 changes: 2 additions & 19 deletions webp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,12 @@ func GetInfo(data []byte) (width, height int, hasAlpha bool, err error) {
}

func DecodeGray(data []byte) (m *image.Gray, err error) {
pix, w, h, err := webpDecodeGray(data, nilCBuffer)
if err != nil {
return
}
defer pix.Close()
m = &image.Gray{
Pix: append([]byte{}, pix.CData()...),
Stride: 1 * w,
Rect: image.Rect(0, 0, w, h),
}
return
}

func DecodeGrayEx(data []byte, cbuf CBuffer) (m *image.Gray, pix CBuffer, err error) {
if cbuf == nil {
cbuf = nilCBuffer
}
pix, w, h, err := webpDecodeGray(data, cbuf)
pix, w, h, err := webpDecodeGray(data)
if err != nil {
return
}
m = &image.Gray{
Pix: pix.CData(),
Pix: pix,
Stride: 1 * w,
Rect: image.Rect(0, 0, w, h),
}
Expand Down

0 comments on commit 8e31b54

Please sign in to comment.