Sorry, I hit "Send" too soon.
On Mon, Dec 24, 2012 at 3:43 PM, Nigel Tao <
nige...@golang.org> wrote:
> On Mon, Dec 24, 2012 at 1:53 AM, <
ma...@ungerik.net> wrote:
>> On Friday, November 9, 2012 5:31:34 PM UTC+1, Rob Pike wrote:
>>> It's in the same coordinate system because it's the same pixels. The
>>> pixel at (36, 44) is the same pixel, with the same address, in both
>>> the original and the subimage.
>>
>> No, because the sub image is initialized with Pix: p.Pix[i:], where i is the
>> byte offset of the sub images origin.
>> Subsequently subImage.At(0,0) must be valid, because after slicing the sub
>> images data starts at the first byte of its Pix.
>>
>> But according to the spec subImage.At(originX,originY) should be the first
>> byte.
>>
>> The fix to make it work like the spec would be to simply not slice the sub
>> image Pix.
>
> Rob is right: subImage.At(0, 0) is not necessarily valid.
An image's bounds have a top-left corner, called Rect.Min for the
concrete implementations in package image. The first byte of Pix
correspond to this corner but the bounds do not necessarily contain
the origin (0, 0). A sub-image's Pix bytes for (3, 4) have the same
memory address (but not the same Pix offset) as the super-image's Pix
bytes for (3, 4).
http://golang.org/doc/articles/image_package.html has more details
(and some pictures). It also says, "A common mistake is assuming that
an Image's bounds start at (0, 0). For example, an animated GIF
contains a sequence of Images, and each Image after the first
typically only holds pixel data for the area that changed, and that
area doesn't necessarily start at (0, 0). The correct way to iterate
over an Image m's pixels looks like:
b := m.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; y < b.Max.X; x++ {
doStuffWith(m.At(x, y))
}
}