Skip to content

image/draw: Possible precision loss for NRGBA images #51893

Closed
@nvinuesa

Description

@nvinuesa

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

$ go version
go version go1.18 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/nicolas/.cache/go-build"
GOENV="/home/nicolas/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/nicolas/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nicolas/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2624768179=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I simply created an NRGBA image with a single pixel (with a small value color (1 in uint8) and an alpha less than 0.5 (127 in uint8)), and tried to copy it using it as source of draw.Draw():

package main

import (
	"image"
	"image/color"
	"image/draw"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestImgDrawNRGBA(t *testing.T) {
	pixel := color.NRGBA{1, 0, 0, 127}
	expected := image.NewNRGBA(image.Rect(0, 0, 1, 1))
	expected.SetNRGBA(0, 0, pixel)

	actual := image.NewNRGBA(expected.Rect)
	draw.Draw(actual, expected.Rect, expected, image.Point{0, 0}, draw.Src)
	assert.Equal(t, expected, actual)
}

Go playground link: https://go.dev/play/p/IYelc0epkM7

What did you expect to see?

I expected the test to pass because this test passes using go-1.17.8 (Go playground link: https://go.dev/play/p/IYelc0epkM7?v=goprev).

What did you see instead?

Instead I saw a difference between the original image (expected) and the actual one (drawn using draw.Draw()):

Diff:
--- Expected
+++ Actual
@@ -2,3 +2,3 @@
	Pix: ([]uint8) (len=4) {
-  00000000  01 00 00 7f                                       |....|
+  00000000  00 00 00 7f                                       |....|
	},

Even though this is a non-alpha-premultiplied image, it looks like a possible precision loss due to the value of the alpha channel: if I use an alpha higher or equal than 0.5 (128 in uint8) the test passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions