If you haven't already read
http://golang.org/doc/articles/image_package.html, please do so. It
describes how an Image's bounds do not necessarily contain the origin
(0, 0).
On 12 July 2012 22:08, Zippoxer <
zipp...@gmail.com> wrote:
> for i := 0; i <= len(rgba.Pix)-rgba2.Stride; i += 4 {
I think that this can give you a false positive if rgba's Stride is
larger than rgba.Rect's width. You may be searching in the Pix slice
outside of an image bounds. Suppose that your rgba variable is the
blue image in
http://golang.org/doc/articles/image-package-05.png
which is found in the article at
http://golang.org/doc/articles/image_package.html
> for row := 0; row < rgba2.Rect.Max.Y; row++ {
I think that the upper bound is wrong if rgab2's Rect.Min is not the
origin. It should be rgba2.Rect.Max.Y - rgba2.Rect.Min.Y.
> for j := 0; j <= rgba2.Stride-4; j += 4 {
> if rgba.Pix[n+j] != rgba2.Pix[n2+j] {
> goto next
> }
> }
I think you're only examining the red values of each RGBA pixel, and
ignoring the blue, green and alpha.
I haven't tested this, but try something like
// contains returns whether the image m0 contains m1. If it returns true, it
// also returns the point in m0 at which m1 appears.
//
// TODO: Boyer-Moore search could be much faster.
func contains(m0, m1 *image.RGBA) (image.Point, bool) {
s1 := m1.Rect.Size()
s0 := m0.Rect.Size().Sub(s1)
for y0 := 0; y0 < s0.Y; y0++ {
loopx0:
for x0 := 0; x0 < s0.X; x0++ {
i0 := y0*m0.Stride + x0*4
j0 := i0 + s1.X*4
i1 := 0
j1 := s1.X * 4
for y1 := 0; y1 < s1.Y; y1++ {
if !bytes.Equal(m0.Pix[i0:j0], m1.Pix[i1:j1]) {
continue loopx0
}
i0 += m0.Stride
j0 += m0.Stride
i1 += m1.Stride
j1 += m1.Stride
}
return m0.Rect.Min.Add(image.Pt(x0, y0)), true
}
}
return image.Point{}, false
}