# Path: _stbt/cv2_compat.py
# def find_contour_boxes(image, mode, method):
# def _fix_pre_3_2_rects(r):
# def find_contour_boxes(image, mode, method):
# FILLED = cv2.FILLED
# LINE_AA = cv2.LINE_AA
# FILLED = cv2.cv.CV_FILLED  # pylint: disable=c-extension-no-member
# LINE_AA = cv2.CV_AA  # pylint: disable=c-extension-no-member
# 
# Path: _stbt/imgutils.py
# def _image_region(image):
#     s = image.shape
#     return Region(0, 0, s[1], s[0])
# 
# Path: _stbt/logging.py
# @contextmanager
# def scoped_debug_level(level):
#     global _debug_level
#     oldlevel = _debug_level
#     _debug_level = level
#     try:
#         yield
#     finally:
#         _debug_level = oldlevel
# 
# Path: _stbt/match.py
# def _merge_regions(regions):
#     """Discard regions that are entirely contained within another region."""
#     regions.sort(key=lambda r: r.width * r.height)
#     i = len(regions) - 1
#     while i > 0:
#         r = regions[i]
#         for j in range(i - 1, -1, -1):
#             if r.contains(regions[j]):
#                 del regions[j]
#                 i -= 1
#         i -= 1
# 
# Path: tests/test_core.py
# def _find_file(path, root=os.path.dirname(os.path.abspath(__file__))):
#     return os.path.join(root, path)
# 
# Path: tests/test_ocr.py
# def requires_tesseract(func):
#     """Decorator for tests that require Tesseract to be installed."""
#     try:
#         _tesseract_version()
#     except:
#         raise SkipTest("tesseract isn't installed")
#     return func


import os
import random
import re
import timeit
import cv2
import numpy
import pytest
import stbt_core as stbt
from _stbt import cv2_compat
from _stbt.imgutils import _image_region
from _stbt.logging import scoped_debug_level
from _stbt.match import _merge_regions
from tests.test_core import _find_file
from tests.test_ocr import requires_tesseract
    from _stbt.match import _build_pyramid
    black_frame = black(1280, 720)
    almost_black_frame = black(1280, 720, value=2)

    sqdiff = mp(match_method=stbt.MatchMethod.SQDIFF)
    sqdiff_normed = mp(match_method=stbt.MatchMethod.SQDIFF_NORMED)

    assert not stbt.match(black_reference, black_frame, sqdiff_normed)
    assert not stbt.match(almost_black_reference, black_frame, sqdiff_normed)
    assert not stbt.match(almost_black_reference, almost_black_frame,
                          sqdiff_normed)
    assert stbt.match(black_reference, black_frame, sqdiff)
    assert stbt.match(almost_black_reference, black_frame, sqdiff)
    assert stbt.match(almost_black_reference, almost_black_frame, sqdiff)


def test_transparent_reference_image_with_sqdiff_normed_raises_valueerror():
    f = stbt.load_image("buttons-on-blue-background.png")
    with pytest.raises(ValueError):
        stbt.match("button-transparent.png", f,
                   match_parameters=mp(stbt.MatchMethod.SQDIFF_NORMED))


def test_that_build_pyramid_relaxes_mask():

    mask = numpy.ones((200, 20, 3), dtype=numpy.uint8) * 255
    mask[5:9, 5:9] = 0  # first 0 is an even row/col, last 0 is an odd row/col
    n = mask.size - numpy.count_nonzero(mask)
    assert n == 4 * 4 * 3
    cv2.imwrite("/tmp/dave1.png", mask)


# Path: _stbt/cv2_compat.py
# def find_contour_boxes(image, mode, method):
# def _fix_pre_3_2_rects(r):
# def find_contour_boxes(image, mode, method):
# FILLED = cv2.FILLED
# LINE_AA = cv2.LINE_AA
# FILLED = cv2.cv.CV_FILLED  # pylint: disable=c-extension-no-member
# LINE_AA = cv2.CV_AA  # pylint: disable=c-extension-no-member
# 
# Path: _stbt/imgutils.py
# def _image_region(image):
#     s = image.shape
#     return Region(0, 0, s[1], s[0])
# 
# Path: _stbt/logging.py
# @contextmanager
# def scoped_debug_level(level):
#     global _debug_level
#     oldlevel = _debug_level
#     _debug_level = level
#     try:
#         yield
#     finally:
#         _debug_level = oldlevel
# 
# Path: _stbt/match.py
# def _merge_regions(regions):
#     """Discard regions that are entirely contained within another region."""
#     regions.sort(key=lambda r: r.width * r.height)
#     i = len(regions) - 1
#     while i > 0:
#         r = regions[i]
#         for j in range(i - 1, -1, -1):
#             if r.contains(regions[j]):
#                 del regions[j]
#                 i -= 1
#         i -= 1
# 
# Path: tests/test_core.py
# def _find_file(path, root=os.path.dirname(os.path.abspath(__file__))):
#     return os.path.join(root, path)
# 
# Path: tests/test_ocr.py
# def requires_tesseract(func):
#     """Decorator for tests that require Tesseract to be installed."""
#     try:
#         _tesseract_version()
#     except:
#         raise SkipTest("tesseract isn't installed")
#     return func


# Path: tests/test_match.py
import os
import random
import re
import timeit
import cv2
import numpy
import pytest
import stbt_core as stbt
from _stbt import cv2_compat
from _stbt.imgutils import _image_region
from _stbt.logging import scoped_debug_level
from _stbt.match import _merge_regions
from tests.test_core import _find_file
from tests.test_ocr import requires_tesseract
    from _stbt.match import _build_pyramid

    black_frame = black(1280, 720)
    almost_black_frame = black(1280, 720, value=2)

    sqdiff = mp(match_method=stbt.MatchMethod.SQDIFF)
    sqdiff_normed = mp(match_method=stbt.MatchMethod.SQDIFF_NORMED)

    assert not stbt.match(black_reference, black_frame, sqdiff_normed)
    assert not stbt.match(almost_black_reference, black_frame, sqdiff_normed)
    assert not stbt.match(almost_black_reference, almost_black_frame,
                          sqdiff_normed)
    assert stbt.match(black_reference, black_frame, sqdiff)
    assert stbt.match(almost_black_reference, black_frame, sqdiff)
    assert stbt.match(almost_black_reference, almost_black_frame, sqdiff)


def test_transparent_reference_image_with_sqdiff_normed_raises_valueerror():
    f = stbt.load_image("buttons-on-blue-background.png")
    with pytest.raises(ValueError):
        stbt.match("button-transparent.png", f,
                   match_parameters=mp(stbt.MatchMethod.SQDIFF_NORMED))


def test_that_build_pyramid_relaxes_mask():

    mask = numpy.ones((200, 20, 3), dtype=numpy.uint8) * 255
    mask[5:9, 5:9] = 0  # first 0 is an even row/col, last 0 is an odd row/col
    n = mask.size - numpy.count_nonzero(mask)
    assert n == 4 * 4 * 3
    cv2.imwrite("/tmp/dave1.png", mask)


    with scoped_debug_level(2):