# Path: imgProcessor/interpolate/polyfit2d.py
# def polyfit2dGrid(arr, mask=None, order=3, replace_all=False,
#                   copy=True, outgrid=None):
#     '''
#     replace all masked values with polynomial fitted ones
#     '''
#     s0,s1 = arr.shape
#     if mask is None:
#         if outgrid is None:
#             y,x = np.mgrid[:float(s0),:float(s1)]
#             p = polyfit2d(x.flatten(),y.flatten(),arr.flatten(),order)
#             return polyval2d(x,y, p, dtype=arr.dtype)
#         mask = np.zeros_like(arr, dtype=bool)
#     elif mask.sum() == 0 and not replace_all and outgrid is None:
# 
#         return arr
#     valid = ~mask
#     y,x = np.where(valid)
#     z = arr[valid]
#     p = polyfit2d(x,y,z,order)
#     if outgrid is not None:
#         yy,xx = outgrid
#     else:
#         if replace_all:
#             yy,xx = np.mgrid[:float(s0),:float(s1)]
#         else:
#             yy,xx = np.where(mask)
#     new = polyval2d(xx,yy, p, dtype=arr.dtype)
#     
#     if outgrid is not None or replace_all:
#         return new
#     
#     if copy:
#         arr = arr.copy()
#     arr[mask] = new
#     return arr
# 
# 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 scipy.ndimage.filters import maximum_filter, laplace
from fancytools.fit.fit2dArrayToFn import fit2dArrayToFn
from imgProcessor.interpolate.polyfit2d import polyfit2dGrid
from imgProcessor.equations.vignetting import vignetting, guessVignettingParam

# coding=utf-8
from __future__ import division
from __future__ import print_function






def function(img, mask=None, replace_all=False, outgrid=None, fn=vignetting,
             guess=None, orthogonal=True, **kwargs):
    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

# Path: imgProcessor/interpolate/polyfit2d.py
# def polyfit2dGrid(arr, mask=None, order=3, replace_all=False,
#                   copy=True, outgrid=None):
#     '''
#     replace all masked values with polynomial fitted ones
#     '''
#     s0,s1 = arr.shape
#     if mask is None:
#         if outgrid is None:
#             y,x = np.mgrid[:float(s0),:float(s1)]
#             p = polyfit2d(x.flatten(),y.flatten(),arr.flatten(),order)
#             return polyval2d(x,y, p, dtype=arr.dtype)
#         mask = np.zeros_like(arr, dtype=bool)
#     elif mask.sum() == 0 and not replace_all and outgrid is None:
# 
#         return arr
#     valid = ~mask
#     y,x = np.where(valid)
#     z = arr[valid]
#     p = polyfit2d(x,y,z,order)
#     if outgrid is not None:
#         yy,xx = outgrid
#     else:
#         if replace_all:
#             yy,xx = np.mgrid[:float(s0),:float(s1)]
#         else:
#             yy,xx = np.where(mask)
#     new = polyval2d(xx,yy, p, dtype=arr.dtype)
#     
#     if outgrid is not None or replace_all:
#         return new
#     
#     if copy:
#         arr = arr.copy()
#     arr[mask] = new
#     return arr
# 
# 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: DUMP/interpolationMethods.py
import numpy as np
from scipy.ndimage.filters import maximum_filter, laplace
from fancytools.fit.fit2dArrayToFn import fit2dArrayToFn
from imgProcessor.interpolate.polyfit2d import polyfit2dGrid
from imgProcessor.equations.vignetting import vignetting, guessVignettingParam

# coding=utf-8
from __future__ import division
from __future__ import print_function






def function(img, mask=None, replace_all=False, outgrid=None, fn=vignetting,
             guess=None, orthogonal=True, **kwargs):
    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:
