findTopLeftCorner :: Img -> (Int,Int)
findTopLeftCorner img = trace (show (matchCoord,matchPix)) topLeftCoord
  where
    w = imageWidth img
    h = imageHeight img
    (cx,cy) = (w `div` 2, h `div` 2)
    diagCoords = reverse (zip [cx,cx-1..0] [cy,cy-1..0])
    pixAtCoord = pixelToTuple . uncurry (pixelAt img)
    diagPixels = map pixAtCoord diagCoords
    (match:_) = locateSubstring targetBlock diagPixels
    matchCoord = diagCoords !! (match+2)
    matchPix = pixAtCoord matchCoord
    abovePix = pixAtCoord (above matchCoord)
    leftPix = pixAtCoord (left matchCoord)
    direction = case (abovePix == topLeftEdgePixel,
                      leftPix == topLeftEdgePixel) of
                  (True, True) -> error "!"
                  (False, True) -> left
                  (True, False) -> above
                  (False, False) -> above
    topLeftCoord = last (takeWhile (pixAtIsEdge img) (iterate direction matchCoord))
findTopLeftCorner :: Img -> (Int,Int)
findTopLeftCorner img = trace (show (matchCoord,matchPix)) topLeftCoord
  where
    w = imageWidth img
    h = imageHeight img
    (cx,cy) = (w `div` 2, h `div` 2)
    diagCoords = reverse (zip [cx,cx-1..0] [cy,cy-1..0])
    pixAtCoord = pixelToTuple . uncurry (pixelAt img)
    diagPixels = map pixAtCoord diagCoords
    (match:_) = locateSubstring targetBlock diagPixels
    matchCoord = diagCoords !! (match+2)
    matchPix = pixAtCoord matchCoord
    abovePix = pixAtCoord (above matchCoord)
    leftPix = pixAtCoord (left matchCoord)
    direction = case (abovePix == topLeftEdgePixel,
                      leftPix == topLeftEdgePixel) of
                  (True, True) -> error "!"
                  (False, True) -> left
                  (True, False) -> above
                  (False, False) -> above
    topLeftCoord = last (takeWhile (pixAtIsEdge img) (iterate direction matchCoord))
findTopLeftCorner img = trace (show (matchCoord,matchPix)) topLeftCoord
  where
    w = imageWidth img
    h = imageHeight img
    (cx,cy) = (w `div` 2, h `div` 2)
    diagCoords = reverse (zip [cx,cx-1..0] [cy,cy-1..0])
    pixAtCoord = pixelToTuple . uncurry (pixelAt img)
    diagPixels = map pixAtCoord diagCoords
    (match:_) = locateSubstring targetBlock diagPixels
    matchCoord = diagCoords !! (match+2)
    matchPix = pixAtCoord matchCoord
    abovePix = pixAtCoord (above matchCoord)
    leftPix = pixAtCoord (left matchCoord)
    direction = case (abovePix == topLeftEdgePixel,
                      leftPix == topLeftEdgePixel) of
                  (True, True) -> error "!"
                  (False, True) -> left
                  (True, False) -> above
                  (False, False) -> above
    topLeftCoord = last (takeWhile (pixAtIsEdge img) (iterate direction matchCoord))