function J = deconvlucy(varargin)
%DECONVLUCY Deblur image using Lucy-Richardson method.
% J = DECONVLUCY(I,PSF) deconvolves image I using Lucy-
% Richardson algorithm, returning deblurred image J. The assumption is
% that the image I was created by convolving a true image with a
% point-spread function PSF and possibly by adding noise.
%
% I can be an N-Dimensional array.
%
% To improve the restoration, additional parameters can be passed in
% (use [] as a place holder if an intermediate parameter is unknown):
% J = DECONVLUCY(I,PSF,NUMIT)
% J = DECONVLUCY(I,PSF,NUMIT,DAMPAR)
% J = DECONVLUCY(I,PSF,NUMIT,DAMPAR,WEIGHT)
% J = DECONVLUCY(I,PSF,NUMIT,DAMPAR,WEIGHT,READOUT)
% J = DECONVLUCY(I,PSF,NUMIT,DAMPAR,WEIGHT,READOUT,SUBSMPL), where
%
% NUMIT (optional) is the number of iterations (default is 10).
%
% DAMPAR (optional) is an array that specifies the threshold deviation
% of the resulting image from the image I (in terms of the standard
% deviation of Poisson noise) below which the damping occurs. The
% iterations are suppressed for the pixels that deviate within the
% DAMPAR value from their original value. This suppresses the noise
% generation in such pixels, preserving necessary image details
% elsewhere. Default is 0 (no damping).
%
% WEIGHT (optional) is assigned to each pixel to reflect its recording
% quality in the camera. A bad pixel is excluded from the solution by
% assigning it zero weight value. Instead of giving a weight of one for
% good pixels, you can adjust their weight according to the amount of
% flat-field correction. Default is a unit array of the same size as
% input image I.
%
% READOUT (optional) is an array (or a value) corresponding to the
% additive noise (e.g., background, foreground noise) and the variance
% of the read-out camera noise. READOUT has to be in the units of the
% image. Default is 0.
%
% SUBSMPL (optional) denotes subsampling and is used when the PSF is
% given on a grid that is SUBSMPL times finer than the image. Default
% is 1.
%
% Note that the output image J could exhibit ringing introduced by the
% discrete Fourier transform used in the algorithm. To reduce the
% ringing use I = EDGETAPER(I,PSF) prior to calling DECONVLUCY.
%
% Note also that DECONVLUCY allows you to resume deconvolution starting
% from the results of an earlier DECONVLUCY run. To initiate this
% syntax, the input image I has to be passed in as cell array, {I}.
% Then the output J becomes a cell array and can be passed as the input
% array into the next DECONVLUCY call. The input cell array can contain
% one numeric array (on initial call), or four numeric arrays (when it
% is the output from a previous run of DECONVLUCY). The output J
% contains four elements, where J{1}=I, J{2} is the image resulted from
% the last iterat