# Path: MEHI/utils/tool.py
# def exeTime(func):
#     '''
#     Usage:
#      - just put '@exeTime'(with out quotation) before your function
#      - will show the running time
#     '''
#     import time
#     def wraper(*args, **args2):
#         s = time.time()
#         ret = func(*args, **args2)
#         e = time.time()
#         msg = "%3.3fs taken for {%s}" % (e-s, func.__name__)
#         log('time')(msg)
#         return ret
#     return wraper


import numpy as np
import math
    import pywt
from MEHI.utils.tool import exeTime
    from skimage.filters import gaussian_filter

################################
# Author   : septicmk
# Date     : 2015/07/23 19:20:57
# FileName : fusion.py
################################


@exeTime
def content_fusion(rdd, sgm1=44, sgm2=81): 
    '''
    Usage:
     - a fast implementation of content-based fusion
    Args:
     - rdd: the ziped L&R img stack
     - sgm1/sgm2: gaussian smooth size
    '''
    def func(dframe):
        frame1, frame2 = dframe[0], dframe[1]
        tmp1 = frame1 - gaussian_filter(frame1,sgm1)
        tmp1 = gaussian_filter(tmp1*tmp1,sgm2)
        tmp2 = frame2 - gaussian_filter(frame2,sgm1)
        tmp2 = gaussian_filter(tmp2*tmp2,sgm2)
        ret = (tmp1*frame1 + frame1*tmp1)/(tmp1+tmp2)
        ret = ret.astype(frame1.dtype)
        return ret
    rdd = rdd.map(func)
    fused_img = np.squeeze(np.array(rdd.collect()))
    return fused_img


# Path: MEHI/utils/tool.py
# def exeTime(func):
#     '''
#     Usage:
#      - just put '@exeTime'(with out quotation) before your function
#      - will show the running time
#     '''
#     import time
#     def wraper(*args, **args2):
#         s = time.time()
#         ret = func(*args, **args2)
#         e = time.time()
#         msg = "%3.3fs taken for {%s}" % (e-s, func.__name__)
#         log('time')(msg)
#         return ret
#     return wraper


# Path: MEHI/paralleled/fusion.py
import numpy as np
import math
    import pywt
from MEHI.utils.tool import exeTime
    from skimage.filters import gaussian_filter

################################
# Author   : septicmk
# Date     : 2015/07/23 19:20:57
# FileName : fusion.py
################################


@exeTime
def content_fusion(rdd, sgm1=44, sgm2=81): 
    '''
    Usage:
     - a fast implementation of content-based fusion
    Args:
     - rdd: the ziped L&R img stack
     - sgm1/sgm2: gaussian smooth size
    '''
    def func(dframe):
        frame1, frame2 = dframe[0], dframe[1]
        tmp1 = frame1 - gaussian_filter(frame1,sgm1)
        tmp1 = gaussian_filter(tmp1*tmp1,sgm2)
        tmp2 = frame2 - gaussian_filter(frame2,sgm1)
        tmp2 = gaussian_filter(tmp2*tmp2,sgm2)
        ret = (tmp1*frame1 + frame1*tmp1)/(tmp1+tmp2)
        ret = ret.astype(frame1.dtype)
        return ret
    rdd = rdd.map(func)
    fused_img = np.squeeze(np.array(rdd.collect()))
    return fused_img


@exeTime
