/*
  Copyright (C) 2011-2012 Van Cuong KIEU, Nicholas JOURNET, van-cuong.kieu@labri.fr
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "ThreeDDistortionModel.hpp"
#include <QDebug>
#include <QtGui/QApplication>
#include "window.h"
#include "QDir"
/**
  * \mainpage
This degradation model aims at mimicking some challenging distortions for staff removal
algorithms, such as skews, curvatures and rotations. Differing from the 2D model used for GREC 2011, our new 3D model generates much more realistic images containing dents, small folds, torns. . . This 3D degradation model can distort the
staff lines, making their detection and removal more difficult. It is based on 3D meshes and texture coordinate generation. The
main idea is that we get multiple 3D meshes of old document pages using real ancient documents and a 3D scanner. Then,
we wrap any 2D image on these meshes using some wrapping functions which are specifically adapted to document images.
  *
  */
using namespace std;

ThreeDDistortionModel::~ThreeDDistortionModel(){

}

QImage ThreeDDistortionModel::degradate(const QImage &image, const QVariantMap &parameters){    
    QVariant v_imgIndex = parameters["Image index"];
    QVariant v_meshIndex = parameters["Mesh index"];
    int imgIndex = v_imgIndex.toInt() -1 ;
    int meshIndex = v_meshIndex.toInt() - 1;

    QDir imgFolder("/home/kvcuong/WorkingSpace/doqubookcpp/Plugins/DegradationModels/3DdistortionModel/image/");
    QStringList imgFilters;
    imgFilters << "*.png";
    imgFolder.setNameFilters(imgFilters);
    QStringList listImages = imgFolder.entryList();


    QDir meshFolder("/home/kvcuong/WorkingSpace/doqubookcpp/Plugins/DegradationModels/3DdistortionModel/mesh/");
    QStringList meshFilters;
    meshFilters << "*.obj";
    meshFolder.setNameFilters(meshFilters);
    QStringList listMeshes = meshFolder.entryList();


    QString imgPath = imgFolder.absoluteFilePath(listImages.at(imgIndex));
    QString meshPath = meshFolder.absoluteFilePath(listMeshes.at(meshIndex));

    QStringList lstMeshPath = meshPath.split("/");
    QString meshName = lstMeshPath.at(lstMeshPath.size()-1);
    QStringList lstMeshName = meshName.split("_");
    QString pageName = lstMeshName.at(0); pageName.replace("livre","");
    QString pageSideName = lstMeshName.at(2); pageSideName.replace("p","");

    int pageId = pageName.toInt();
    int page_l_r = pageSideName.toInt();

    Window *window = new Window(pageId,page_l_r,imgPath,meshPath,image);
    window->show();

    return QImage(imgPath);
}

DQB_EXPORT_PLUGIN(ThreeDDistortionModel)
