# Path: imgProcessor/camera/DarkCurrentMap.py
# def averageSameExpTimes(imgs_path):
#     '''
#     average background images with same exposure time
#     '''
#     firsts = imgs_path[:2]
#     imgs = imgs_path[2:]
#     for n, i in enumerate(firsts):
#         firsts[n] = np.asfarray(imread(i))
#     d = DarkCurrentMap(firsts)
#     for i in imgs:
#         i = imread(i)
#         d.addImg(i)
#     return d.map()
# 
# Path: imgProcessor/imgIO.py
# def imread(img, color=None, dtype=None):
#     '''
#     dtype = 'noUint', uint8, float, 'float', ...
#     '''
#     COLOR2CV = {'gray': cv2.IMREAD_GRAYSCALE,
#                 'all': cv2.IMREAD_COLOR,
#                 None: cv2.IMREAD_ANYCOLOR
#                 }
#     c = COLOR2CV[color]
#     if callable(img):
#         img = img()
#     elif isinstance(img, string_types):
#         #         from_file = True
#         #         try:
#         #             ftype = img[img.find('.'):]
#         #             img = READERS[ftype](img)[0]
#         #         except KeyError:
#         # open with openCV
#         # grey - 8 bit
#         if dtype in (None, "noUint") or np.dtype(dtype) != np.uint8:
#             c |= cv2.IMREAD_ANYDEPTH
#         img2 = cv2.imread(img, c)
#         if img2 is None:
#             raise IOError("image '%s' is not existing" % img)
#         img = img2
# 
#     elif color == 'gray' and img.ndim == 3:  # multi channel img like rgb
#         # cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cannot handle float64
#         img = toGray(img)
#     # transform array to uint8 array due to openCV restriction
#     if dtype is not None:
#         if isinstance(img, np.ndarray):
#             img = _changeArrayDType(img, dtype, cutHigh=False)
# 
#     return img


from imgProcessor.camera.DarkCurrentMap import averageSameExpTimes
from imgProcessor.imgIO import imread
import numpy as np

# coding=utf-8



def getBackground(bgImages, **kwargs):
    # AVERAGE BACKGROUND IMAGES IF MULTIPLE ARE GIVEN:
    if bgImages is not None:
        if (type(bgImages) in (tuple, list)
                or isinstance(bgImages, np.ndarray) and bgImages.ndim == 3):
            if len(bgImages) > 1:
                return averageSameExpTimes(bgImages)
            else:
                return imread(bgImages[0], **kwargs)
        else:

# Path: imgProcessor/camera/DarkCurrentMap.py
# def averageSameExpTimes(imgs_path):
#     '''
#     average background images with same exposure time
#     '''
#     firsts = imgs_path[:2]
#     imgs = imgs_path[2:]
#     for n, i in enumerate(firsts):
#         firsts[n] = np.asfarray(imread(i))
#     d = DarkCurrentMap(firsts)
#     for i in imgs:
#         i = imread(i)
#         d.addImg(i)
#     return d.map()
# 
# Path: imgProcessor/imgIO.py
# def imread(img, color=None, dtype=None):
#     '''
#     dtype = 'noUint', uint8, float, 'float', ...
#     '''
#     COLOR2CV = {'gray': cv2.IMREAD_GRAYSCALE,
#                 'all': cv2.IMREAD_COLOR,
#                 None: cv2.IMREAD_ANYCOLOR
#                 }
#     c = COLOR2CV[color]
#     if callable(img):
#         img = img()
#     elif isinstance(img, string_types):
#         #         from_file = True
#         #         try:
#         #             ftype = img[img.find('.'):]
#         #             img = READERS[ftype](img)[0]
#         #         except KeyError:
#         # open with openCV
#         # grey - 8 bit
#         if dtype in (None, "noUint") or np.dtype(dtype) != np.uint8:
#             c |= cv2.IMREAD_ANYDEPTH
#         img2 = cv2.imread(img, c)
#         if img2 is None:
#             raise IOError("image '%s' is not existing" % img)
#         img = img2
# 
#     elif color == 'gray' and img.ndim == 3:  # multi channel img like rgb
#         # cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cannot handle float64
#         img = toGray(img)
#     # transform array to uint8 array due to openCV restriction
#     if dtype is not None:
#         if isinstance(img, np.ndarray):
#             img = _changeArrayDType(img, dtype, cutHigh=False)
# 
#     return img


# Path: imgProcessor/utils/getBackground.py
from imgProcessor.camera.DarkCurrentMap import averageSameExpTimes
from imgProcessor.imgIO import imread
import numpy as np

# coding=utf-8



def getBackground(bgImages, **kwargs):
    # AVERAGE BACKGROUND IMAGES IF MULTIPLE ARE GIVEN:
    if bgImages is not None:
        if (type(bgImages) in (tuple, list)
                or isinstance(bgImages, np.ndarray) and bgImages.ndim == 3):
            if len(bgImages) > 1:
                return averageSameExpTimes(bgImages)
            else:
                return imread(bgImages[0], **kwargs)
        else:

            return imread(bgImages, **kwargs)
