Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/gif: bounds offset reversed when encoding #30887

Closed
splace opened this issue Mar 17, 2019 · 3 comments
Closed

image/gif: bounds offset reversed when encoding #30887

splace opened this issue Mar 17, 2019 · 3 comments

Comments

@splace
Copy link

@splace splace commented Mar 17, 2019

go 1.12.1 linux/amd64

What did you expect to see?

image shift not depending on saved format.

What did you see instead?

gif image shifted in the opposite direction to all other formats.

(several other image format generating libs tried and gif the only one in disagreement.)

demo example:

package main
import "image"
import "image/png"
import "image/jpeg"
import "image/gif"
import "os"

type offsetImage struct{
	image.Image
	Rect image.Rectangle	
}

func (i offsetImage) Bounds() image.Rectangle{
	return i.Rect
}

func main() {
	img:=offsetImage{image.Rect(0,0,50,50),image.Rect(0,0,50,50) }
	w,_:= os.Create("1.png")
	png.Encode(w,img)
	w,_= os.Create("1.jpg")
	jpeg.Encode(w,img,nil)
	w,_= os.Create("1.gif")
	gif.Encode(w,img,nil)
	
	img.Rect=image.Rect(30,30,80,80)  // same size just offset

	w,_= os.Create("2.png")
	png.Encode(w,img)
	w,_= os.Create("2.jpg")
	jpeg.Encode(w,img,nil)
	w,_= os.Create("2.gif")
	gif.Encode(w,img,nil)
	
}

output

without shift:all white, all the same as expected

  • 1
  • 1
  • 1

with shift:

  • 2
  • 2
  • 2

(png image above not showing in github md?)

@katiehockman
Copy link
Member

@katiehockman katiehockman commented Mar 19, 2019

I was able to reproduce this, does seem that gif changes after the offset in a way that's different from the other image formats.

/cc @nigeltao for image/gif package

@katiehockman katiehockman added this to the Unplanned milestone Mar 19, 2019
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 21, 2019

Change https://golang.org/cl/168418 mentions this issue: image/gif: don't assume Encode src's origin is (0, 0)

@splace
Copy link
Author

@splace splace commented Mar 21, 2019

confirmed fixed. (with patch) demo now results in matching images and original code now works as expected. nice.

@splace splace closed this Mar 21, 2019
gopherbot pushed a commit that referenced this issue Mar 21, 2019
When gif.Encode is given an "m image.Image" argument that isn't an
*image.Paletted, it creates a temporary *image.Paletted (called pm) that
is intended to be a copy of this image, only with fewer colors.

That creation process, and specifically the opts.Drawer.Draw call that
does the copy, incorrectly assumed that m.Bounds().Min is the zero point
(0, 0). This commit fixes that.

Fixes #30887

Change-Id: Ie03bddec359e2dcc52f18451049452105514e179
Reviewed-on: https://go-review.googlesource.com/c/go/+/168418
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
@golang golang locked and limited conversation to collaborators Mar 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.