/*
#
# File : greycstoration.cpp
# ( C++ source file )
#
# Description : GREYCstoration - A tool to denoise, inpaint and resize images.
# This file is a part of the CImg Library project.
# ( http://cimg.sourceforge.net )
# See also the GREYCstoration web page
# ( http://www.greyc.ensicaen.fr/~dtschump/greycstoration )
#
# The GREYCstoration algorithm is an implementation of tensor-directed and
# patch-based diffusion PDE's for image regularization and interpolation,
# published in
#
# "Defining Some Variational Methods on the Space of Patches :
# Application to Multi-Valued Image Denoising and Registration"
# (D. Tschumperle, L. Brun)
# Rapport de recherche : Les cahiers du GREYC No 08-01, Mars 2008.
#
# "Fast Anisotropic Smoothing of Multi-Valued Images
# using Curvature-Preserving PDE's"
# (D. Tschumperle)
# International Journal of Computer Vision, May 2006.
#
# "Vector-Valued Image Regularization with PDE's : A Common Framework
# for Different Applications"
# (D. Tschumperle, R. Deriche).
# IEEE Transactions on Pattern Analysis and Machine Intelligence,
# Vol 27, No 4, pp 506-517, April 2005.
#
# Copyright : David Tschumperle
# ( http://www.greyc.ensicaen.fr/~dtschump/ )
#
# License : CeCILL v2.0
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
#
*/
#define cimg_plugin "plugins/greycstoration.h"
#ifndef cimg_debug
#define cimg_debug 1
#endif
#include "../CImg.h"
#if cimg_OS!=2
#include <pthread.h>
#endif
#define gprintf if (verbose) std::fprintf
using namespace cimg_library;
// The lines below are necessary when using a non-standard compiler as visualcpp6.
#ifdef cimg_use_visualcpp6
#define std
#endif
#ifdef min
#undef min
#undef max
#endif
//-----------
// get_geom() : read geometry from a string (for instance '320x256' or '200%x200%').
//-----------
void get_geom(const char *geom, int &geom_w, int &geom_h) {
char tmp[16];
std::sscanf(geom,"%d%7[^0-9]%d%7[^0-9]",&geom_w,tmp,&geom_h,tmp+1);
if (tmp[0]=='%') geom_w=-geom_w;
if (tmp[1]=='%') geom_h=-geom_h;
}
//--------------------------
// GREYCstoration main code
//--------------------------
template<typename T> void greycstoration(int argc, char **argv, T pixel_type) {
pixel_type = (T)0;
cimg_usage(" Open Source Algorithms for Image Denoising and Interpolation.");
cimg_help("-------------------------------------------------------------------------\n"
" GREYCstoration v2.9, by David Tschumperle. \n"
" ------------------------------------------------------------------------\n"
" This program allows to denoise, inpaint and resize 2D color images. \n"
" It is the result of the research work done in the IMAGE group of the \n"
" GREYC Lab (CNRS, UMR 6072) (http://www.greyc.ensicaen.fr/EquipeImage/) \n"
" by David Tschumperle (http://www.greyc.ensicaen.fr/~dtschump/). This \n"
" program has been primarily released to help people processing image data\n"
" and to allow comparisons between regularization algorithms. This is an \n"
" open source software, distributed within the CImg Library package \n"
" (http://cimg.sourceforge.net), and submitted to the CeCILL License. If \n"
" you are interested to distribute it in a closed-source product, please \n"
" read the licence file carefully. If you are using 'GREYCstored' images \n"
" in your own publications, be kind to reference the GREYCstoration web \n"
" site or the related scientific papers. More informations available at : \n"
" ** http://www.greyc.ensicaen.fr/~dtschump/greycstoration/ ** \n"
"-------------------------------------------------------------------------\n");
// Read Global parameters
//------------------------
cimg_help(" + Global parameters\n -----------------------");
const char *restore_mode = cimg_option("-restore",(char*)0,"Restore image specified after '-restore'");
const char *inpaint_mode = cimg_option("-inpaint",(char*)0,"Inpaint image specified after '-inpaint'");
const char *resize_mode = cimg_option("-resize",(char*)0,"Resize image specified after '-resize'");
const char *clean_mode = cimg_option("-clean",(char*)0,"Clean image specified after '-clean'");
const char *reference_image = cimg_option("-ref",(char*)0,"Reference image to compare with");
const char nb_bits = cimg_option("-bits",8,"Define input value type (8='uchar', 16='ushort', 32='float')");
const unsigned int value_factor = cimg_option("-fact",0,"Define input value factor (0=auto)");
const float noise_g = cimg_option("-ng",0.0f,"Add Gaussian noise before denoising");
const float noise_u = cimg_option("-nu",0.0f,"Add Uniform noise before denoising");
const float noise_s = cimg_option("-ns",0.0f,"Add Salt&Pepper noise before denoising");
const unsigned int color_base = cimg_option("-cbase",0,"Processed color base (0=RGB, 1=YCbCr)");
const char *channel_range = cimg_option("-crange",(char*)0,"Processed channels (ex: '0-2')");
const unsigned int saving_step = cimg_option("-save",0,"Iteration saving step (>=0)");
const bool visu = cimg_option("-visu",cimg_display_type?true:false,"Enable/Disable visualization windows (0 or 1)");
const char *file_o = cimg_option("-o",(char*)0,"Filename of output image");
const bool append_result = cimg_option("-append",false,"Append images in output file");
const bool verbose = cimg_option("-verbose",true,"Verbose mode");
const unsigned int jpg_quality = cimg_option("-quality",100,"Output compression quality (if JPEG format)");
unsigned int nb_iterations = cimg_option("-iter",(restore_mode||clean_mode)?1:(inpaint_mode?1000:3),
"Number of iterations (>0)");
const float sdt = cimg_option("-sharp",(restore_mode||clean_mode)?0.0f:(inpaint_mode?0.0f:10.0f),
"Sharpening strength (activate sharpening filter, >=0)");