Skip to content

image/jpeg: encoding an *image.YCbCr is sub-optimal #18487

Closed
@tbonfort

Description

@tbonfort

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

master

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tbonfort/dev/go"
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

func BenchmarkEncodeYCbCr(b *testing.B) {
	b.StopTimer()
	img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420)
	bo := img.Bounds()
	rnd := rand.New(rand.NewSource(123))
	for y := bo.Min.Y; y < bo.Max.Y; y++ {
		for x := bo.Min.X; x < bo.Max.X; x++ {
			cy := img.YOffset(x, y)
			ci := img.COffset(x, y)
			img.Y[cy] = uint8(rnd.Intn(256))
			img.Cb[ci] = uint8(rnd.Intn(256))
			img.Cr[ci] = uint8(rnd.Intn(256))
		}
	}
	b.SetBytes(640 * 480 * 4)
	b.StartTimer()
	options := &Options{Quality: 90}
	for i := 0; i < b.N; i++ {
		Encode(ioutil.Discard, img, options)
	}
}

What did you expect to see?

jpeg.Encode() of *image.YCbCr should be faster than for *image.RGBA

What did you see instead?

This is slow as jpeg.Encode() uses the generic image.At() which involves unnecessary ycbr->rgba->ycbcr conversions, and more importantly image.color allocations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions