# Path: imgProcessor/equations/vignetting.py
# def vignetting(xy, f=100, alpha=0, rot=0, tilt=0, cx=50, cy=50):
#     '''
#     Vignetting equation using the KANG-WEISS-MODEL
#     see http://research.microsoft.com/en-us/um/people/sbkang/publications/eccv00.pdf   
# 
#     f - focal length
#     alpha - coefficient in the geometric vignetting factor
#     tilt - tilt angle of a planar scene
#     rot - rotation angle of a planar scene
#     cx - image center, x
#     cy - image center, y
#     '''
#     x, y = xy
#     # distance to image center:
#     dist = ((x - cx)**2 + (y - cy)**2)**0.5
# 
#     # OFF_AXIS ILLUMINATION FACTOR:
#     A = 1.0 / (1 + (dist / f)**2)**2
#     # GEOMETRIC FACTOR:
#     if alpha != 0:
#         G = (1 - alpha * dist)
#     else:
#         G = 1
#     # TILT FACTOR:
#     if tilt != 0:
#         T = tiltFactor((x, y), f, tilt, rot, (cy, cx))
#     else:
#         T = 1
#     return A * G * T
# 
# def guessVignettingParam(shape):
#     return (shape[0] * 0.7, 0, 0, 0, shape[0] / 2, shape[1] / 2)


import numpy as np
from fancytools.fit.fit2dArrayToFn import fit2dArrayToFn
from imgProcessor.equations.vignetting import vignetting, guessVignettingParam

# coding=utf-8
from __future__ import division



def function(img, mask=None, replace_all=False, outgrid=None, fn=vignetting,
             guess=None, orthogonal=True, **kwargs):
    """
    Functional approach (Kang-Weiss equation by default)
    for post processing of measured flat field images.
    """
    if outgrid is not None:
        replace_all = True
    if mask is None:
        mask = np.zeros_like(img, dtype=bool)
        replace_all = True
    down_scale_factor = 1
    if fn == vignetting and guess is None:
        s0, s1 = img.shape
        if ~mask.sum() > 1000:
            down_scale_factor = 0.3
        guess = guessVignettingParam(
            (s0 * down_scale_factor, s1 * down_scale_factor))
        if orthogonal:

# Path: imgProcessor/equations/vignetting.py
# def vignetting(xy, f=100, alpha=0, rot=0, tilt=0, cx=50, cy=50):
#     '''
#     Vignetting equation using the KANG-WEISS-MODEL
#     see http://research.microsoft.com/en-us/um/people/sbkang/publications/eccv00.pdf   
# 
#     f - focal length
#     alpha - coefficient in the geometric vignetting factor
#     tilt - tilt angle of a planar scene
#     rot - rotation angle of a planar scene
#     cx - image center, x
#     cy - image center, y
#     '''
#     x, y = xy
#     # distance to image center:
#     dist = ((x - cx)**2 + (y - cy)**2)**0.5
# 
#     # OFF_AXIS ILLUMINATION FACTOR:
#     A = 1.0 / (1 + (dist / f)**2)**2
#     # GEOMETRIC FACTOR:
#     if alpha != 0:
#         G = (1 - alpha * dist)
#     else:
#         G = 1
#     # TILT FACTOR:
#     if tilt != 0:
#         T = tiltFactor((x, y), f, tilt, rot, (cy, cx))
#     else:
#         T = 1
#     return A * G * T
# 
# def guessVignettingParam(shape):
#     return (shape[0] * 0.7, 0, 0, 0, shape[0] / 2, shape[1] / 2)


# Path: imgProcessor/camera/flatField/postprocessing/function.py
import numpy as np
from fancytools.fit.fit2dArrayToFn import fit2dArrayToFn
from imgProcessor.equations.vignetting import vignetting, guessVignettingParam

# coding=utf-8
from __future__ import division



def function(img, mask=None, replace_all=False, outgrid=None, fn=vignetting,
             guess=None, orthogonal=True, **kwargs):
    """
    Functional approach (Kang-Weiss equation by default)
    for post processing of measured flat field images.
    """
    if outgrid is not None:
        replace_all = True
    if mask is None:
        mask = np.zeros_like(img, dtype=bool)
        replace_all = True
    down_scale_factor = 1
    if fn == vignetting and guess is None:
        s0, s1 = img.shape
        if ~mask.sum() > 1000:
            down_scale_factor = 0.3
        guess = guessVignettingParam(
            (s0 * down_scale_factor, s1 * down_scale_factor))
        if orthogonal:

            def fn(xy, f, alpha, cx, cy): return vignetting(xy, f=f,
