#include "qtmain.h"


#include <QApplication>

#include <QPainter>
//#include <stdafx.h>
//#include <stdlib.h>

#include "Texture.h"
//#include <vector>
#include <fstream>
#include <time.h>
#include <iostream>
//#include <perspectivedistortionrender.h>
#include <QImage>
#include <QDebug>
#include <cv.h>
#include <highgui.h>
#include <QMap>
#include <QDir>
static float ypoz = 0, zpoz = 0, xpoz = 0;
GLMmodel* pmodel2 = NULL;
float theta = 30;
// You need an integer value to store the texture index that OpenGL retures when you load a texture
// Using this index you can later refer to a specific texture
// Note that GLuint is actualy an int.
GLuint	texture;
Texture	treeTexture;
GLint gl_width=200, gl_height =200;
int src_width = 0, src_height = 0;
int img_plaquage_width =0, img_plaquage_height =0;
// Luminere

GLfloat y_trans =-0.0, x_trans =0.0;

QMap<int,QString> idxPage;
bool IS_ROTATION90=false;
bool IS_LIGHTING=false;
bool IS_BINARY = false;

QString img_input_fullPath = "";
QString img_input_name = "";
QString img_out_fullPath = "/home/kvcuong/WorkingSpace/doqubookcpp/Plugins/DegradationModels/3DdistortionModel/out/";
QString mesh_path="";
int index_of_page = 1;
int level = 0;
float x_axis_scale = 1;
float y_axis_scale = 0;
bool isLight0 = false;
bool isLight1 = false;
bool isLight2 = true;
bool isLight3 = true;

Qtmain::Qtmain(int pageId, int page_L_R, QString imgPath,QString meshPath, QImage srcInput, QWidget *parent, QGLWidget *shareWidget)
    : QGLWidget(parent, shareWidget)
{
    _pageId = pageId;
    _page_L_R = page_L_R;
    _srcInput = srcInput;
    mesh_path = meshPath;

    QStringList lstStr = imgPath.split("/");
    QString imgName = lstStr.at(lstStr.size()-1);

    img_out_fullPath.append(imgName.mid(0,imgName.lastIndexOf(".")-1));

    img_out_fullPath.append("_p");
    img_out_fullPath.append(QString::number(pageId));
    if(_page_L_R==1)img_out_fullPath.append("_r.png");
    if(_page_L_R==2)img_out_fullPath.append("_l.png");
    this->setFocusPolicy(Qt::StrongFocus);
    // For KHOI only
    /*  _listImageName << "H01_H_DIBCO2009.png" << "H01_H_DIBCO2010.png" <<"H02_H_DIBCO2009.png" <<
                      "H02_H_DIBCO2010.png" << "H03_H_DIBCO2009.png" << "H03_H_DIBCO2010.png" <<
                      "H04_H_DIBCO2009.png" << "H04_H_DIBCO2010.png" << "H05_H_DIBCO2009.png" <<
                      "H05_H_DIBCO2010.png" << "H06_H_DIBCO2010.png" << "H07_H_DIBCO2010.png" <<
                      "H08_H_DIBCO2010.png" << "H09_H_DIBCO2010.png" << "H10_H_DIBCO2010.png" <<
                      "HW1_H_DIBCO2011.png" << "HW2_H_DIBCO2011.png" << "HW3_H_DIBCO2011.png" <<
                      "HW4_H_DIBCO2011.png" << "HW5_H_DIBCO2011.png" << "HW6_H_DIBCO2011.png" <<
                      "HW7_H_DIBCO2011.png" << "HW8_H_DIBCO2011.png" << "P01_DIBCO2009.png" <<
                      "P02_DIBCO2009.png" << "P03_DIBCO2009.png" << "P04_DIBCO2009.png" <<
                      "P05_DIBCO2009.png" << "PR1_DIBCO2011.png" << "PR2_DIBCO2011.png" <<
                      "PR3_DIBCO2011.png" << "PR4_DIBCO2011.png" << "PR5_DIBCO2011.png" <<
                      "PR6_DIBCO2011.png" << "PR7_DIBCO2011.png" << "PR8_DIBCO2011.png" ;

    _pageId = pageId;
    _page_L_R = page_L_R;
    _imgId = _listImageName.indexOf(imgName);
    _srcInput = srcInput;
    img_out_fullPath = imgName.split(".").at(0);
    img_out_fullPath = "/media/My Passport/KHOI/Semi-synthetic/Distorsion/classe_graphiques_png/";

    if(pageId == 1 && page_L_R==1){
        img_out_fullPath.append("M1/");
    }
    if(pageId == 1 && page_L_R==2){
        img_out_fullPath.append("M2/");
    }
    if(pageId == 4 && page_L_R==1){
        img_out_fullPath.append("M3/");
    }

    img_out_fullPath.append(imgName);
    img_out_fullPath.replace(".png","_p");
    img_out_fullPath.append(QString::number(pageId));
    if(_page_L_R==1)img_out_fullPath.append("_r.png");
    if(_page_L_R==2)img_out_fullPath.append("_l.png");
    this->setFocusPolicy(Qt::StrongFocus);
    */
}

Qtmain::~Qtmain()
{
}

void Qtmain::initializeGL(){
    init();
}
void Qtmain::resizeGL(int w, int h){
    reshape(w,h);
}

void Qtmain::paintGL(){
    //    qDebug() << " display";
    display();
    //    qDebug() << " update";
    display();
    //    qDebug() << " saving";

    QImage img = grabFrameBuffer();
    if(!IS_ROTATION90){
        QImage out = img.scaled(_srcInput.width(),_srcInput.height());
        out.save(img_out_fullPath);
    }else{
        QImage out = img.scaled(_srcInput.height(),_srcInput.width());
        out.save(img_out_fullPath);
    }

    emit displayed();
}
void Qtmain::keyPressEvent(QKeyEvent *event){

    unsigned char key = event->key();
    keyboard(key,0,1);
}

void Qtmain::changePerspective()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(theta,1.0,0.1,40.0);
    glMatrixMode(GL_MODELVIEW);

}

bool Qtmain::LoadTreeTextures()
{
    if (LoadPNG(&treeTexture, "tmp_image.png"))
        //    if (LoadPNG(&treeTexture, "syn_image_degraded_1.png"))
    {
        if(treeTexture.imageData==NULL) return false;
        // This tells opengl to create 1 texture and put it's ID in the given integer variable
        // OpenGL keeps a track of loaded textures by numbering them: the first one you load is 1, second is 2, ...and so on.
        glGenTextures(1, &treeTexture.texID);
        // Binding the texture to GL_TEXTURE_2D is like telling OpenGL that the texture with this ID is now the current 2D texture in use
        // If you draw anything the used texture will be the last binded texture
        glBindTexture(GL_TEXTURE_2D, treeTexture.texID);
        // This call will actualy load the image data into OpenGL and your video card's memory. The texture is allways loaded into the current texture
        // you have selected with the last glBindTexture call
        // It asks for the width, height, type of image (determins the format of the data you are giveing to it) and the pointer to the actual data
        //                glTexImage2D(GL_TEXTURE_2D, 0, treeTexture.bpp / 8, treeTexture.width, treeTexture.height, 0, treeTexture.type, GL_UNSIGNED_BYTE, treeTexture.imageData);

        //glTexImage2D(GL_TEXTURE_2D, 0, treeTexture.bpp/8, treeTexture.width, treeTexture.height, 0, treeTexture.type, GL_UNSIGNED_BYTE, treeTexture.imageData);
        glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, treeTexture.width, treeTexture.height, 0, treeTexture.type, GL_UNSIGNED_BYTE, treeTexture.imageData);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glEnable(GL_TEXTURE_2D);
        if (treeTexture.imageData)
        {
            // You can now free the image data that was allocated by LoadTGA
            // You don't want to keep a few Mb of worthless data on heap. It's worthless because OpenGL stores the image someware else after
            // you call glTexImage2D (usualy in you video card)
            free(treeTexture.imageData);					// Free The Texture Image Memory ( CHANGE )
        }
        return true;
    }													// Return The Status
    else return false;
}

void Qtmain::init()
{
    // A Hack ??
    qApp->arguments();
    int argc;   char** argv = NULL;
    glutInit(&argc, argv);

    initialize(_pageId,_page_L_R,_srcInput);

    GLfloat luminere1[] = {0,250,100,10};
    GLfloat luminere2[] = {0,415,100,10};
    GLfloat luminere3[] = {85,415,100,10};
    GLfloat luminere4[] = {85,250,100,10};

    GLfloat ambient1[] ={1.0,1.0,1.0,1};
    GLfloat diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};//0.4
    GLfloat highlights1[] = {2.0f, 2.0f, 2.0f, 1.0f};

    GLfloat ambient2[] ={0.5,0.5,0.5,1};
    GLfloat diffuse2[] = {1.0f, 1.0f, 1.0f, 1.0f};//0.4
    GLfloat highlights2[] = {1.2f, 1.2f, 1.2f, 1.0f};

    GLfloat ambient3[] ={0.1,0.1,0.1,1};
    GLfloat diffuse3[] = {0.4f, 0.4f, 0.4f, 1.0f};//0.4
    GLfloat highlights3[] = {0.2f, 0.2f, 0.2f, 1.0f};

    GLfloat ambient4[] ={0.1,0.1,0.1,1};
    GLfloat diffuse4[] = {1.0f, 1.0f, 1.0f, 1.0f};//0.4
    GLfloat highlights4[] = {2.2f, 2.2f, 2.2f, 1.0f};

    glClearColor (0.0, 0.0, 0.0, 0.0);
    LoadTreeTextures();
    glEnable(GL_DEPTH_TEST);
    glShadeModel (GL_SMOOTH);

    /** Lighting **/
    if(IS_LIGHTING){
        glEnable(GL_NORMALIZE);
        glEnable(GL_LIGHTING);

        glLightfv(GL_LIGHT0,GL_POSITION,luminere1);
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient1);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);
        glLightfv(GL_LIGHT0, GL_SPECULAR, highlights1);
        glEnable(GL_LIGHT0);


        glLightfv(GL_LIGHT1,GL_POSITION,luminere2);
        glLightfv(GL_LIGHT1, GL_AMBIENT, ambient2);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2);
        glLightfv(GL_LIGHT1, GL_SPECULAR, highlights2);
        glEnable(GL_LIGHT1);


        glLightfv(GL_LIGHT2,GL_POSITION,luminere3);
        glLightfv(GL_LIGHT2, GL_AMBIENT, ambient3);
        glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse3);
        glLightfv(GL_LIGHT2, GL_SPECULAR, highlights3);
        //    glEnable(GL_LIGHT2);
        glDisable(GL_LIGHT2);



        glLightfv(GL_LIGHT3,GL_POSITION,luminere4);
        glLightfv(GL_LIGHT3, GL_AMBIENT, ambient4);
        glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuse4);
        glLightfv(GL_LIGHT3, GL_SPECULAR, highlights4);
        //    glEnable(GL_LIGHT3);
        glDisable(GL_LIGHT3);

        glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
        glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR);
    }
}
void Qtmain::drawmodel(void)
{
    // Load the model just as I have shown you in the previous tutorial
    // You could replace this part with your own loader
    if (!pmodel2) {
        pmodel2 = glmReadOBJ(mesh_path.toStdString().c_str());
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
    }
    // before you draw anything you should bind the right texture
    // If you have more then one texture you will have to make sure you bind the right one
    glBindTexture(GL_TEXTURE_2D, treeTexture.texID);
    // You can replace this with your drawing code
    // This calls GLM to draw the loaded model with GLM_TEXTURE on to tell it to also render texture coordinates
    // without those coordinates OpenGL will have no ideea how to map your texture to the object
    glmDraw(pmodel2, GLM_SMOOTH | GLM_TEXTURE );
    //    std::cout << "Drawing model...........END" << std::endl;
}

void Qtmain::display(void)
{

    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity ();

    glTranslatef(0,0,-2);

    // Lets do some rotations here so you can see the textures
    glRotatef(xpoz,1,0,0);
    glRotatef(ypoz,0,1,0);
    glRotatef(zpoz,0,0,1);
    if(IS_ROTATION90) glRotatef(90,0,0,1);
    glTranslatef(0,y_trans,0);
    glTranslatef(x_trans,0,0);
    drawmodel();

    // zooming
    changePerspective();
    // translation

    updateOverlayGL();
    update();
    updateGeometry();

    swapBuffers();

}

void Qtmain::reshape (int w, int h)
{
    gl_width = w;
    gl_height =h;
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();

    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
    glMatrixMode (GL_MODELVIEW);
}

void Qtmain::keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27:
        exit(0);
        break;
        // These will allow you to force some extra rotation by pressing the 'y' and 'z' key.
    case 79://o

        x_trans=x_trans+0.01;
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case 80://p
        x_trans=x_trans-0.01;
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case 85://u
        y_trans=y_trans+0.01;
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case 73://i
        y_trans=y_trans-0.01;
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case 89://y
        ypoz=ypoz+5;
        if (ypoz>360) ypoz=0;
        repaint();
        updateGL();
        break;
    case 84://t
        ypoz=ypoz-5;
        if (ypoz<-360) ypoz=0;
        repaint();
        updateGL();
        break;
    case 90://z
        zpoz = zpoz+5;
        if (zpoz>360) zpoz=0;
        repaint();
        updateGL();
        break;
    case 69://e
        zpoz = zpoz-2;
        if (zpoz<-360) zpoz=0;
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta << "zpoz = " << zpoz;
        break;
    case 88://x
        xpoz = xpoz+5;
        if (xpoz>360) xpoz=0;
        repaint();
        updateGL();
        break;
    case 67://c
        xpoz = xpoz-5;
        if (xpoz<-360) xpoz=0;
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta << " xpoz = " << xpoz;
        repaint();
        updateGL();
        break;
    case 65://a
        theta+=0.5;
        if (theta>180)
            theta=180;
        changePerspective();
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case 81://q
        theta-=0.5;
        if (theta<0)
            theta=0;
        changePerspective();
        repaint();
        updateGL();
        qDebug() << "x=" << x_trans << " y = " << y_trans << " theta = " << theta;
        break;
    case '1':

        if(isLight0) glEnable(GL_LIGHT0);
        else glDisable(GL_LIGHT0);
        isLight0=!isLight0;
        qDebug() << isLight0 << " " << isLight1 << " " << isLight2 << " " << isLight3;
        repaint();
        updateGL();
        break;
    case '2':

        if(isLight1) glEnable(GL_LIGHT1);
        else glDisable(GL_LIGHT1);
        isLight1=!isLight1;
        qDebug() << isLight0 << " " << isLight1 << " " << isLight2 << " " << isLight3;
        repaint();
        updateGL();
        break;
    case '3':

        if(isLight2) glEnable(GL_LIGHT2);
        else glDisable(GL_LIGHT2);
        isLight2=!isLight2;
        qDebug() << isLight0 << " " << isLight1 << " " << isLight2 << " " << isLight3;
        repaint();
        updateGL();
        break;
    case '4':

        if(isLight3) glEnable(GL_LIGHT3);
        else glDisable(GL_LIGHT3);
        isLight3=!isLight3;
        qDebug() << isLight0 << " " << isLight1 << " " << isLight2 << " " << isLight3;
        repaint();
        updateGL();
        break;

    default:
        break;
    }
}

void Qtmain::initialize(int pageId, int page_L_R, QImage srcInput){
    IS_ROTATION90 = false;
    IS_LIGHTING = true;
    IS_BINARY = !IS_LIGHTING;

    int h_scale = srcInput.height();
    int w_scale = srcInput.width();
    int delta_w = 0;
    int delta_h = 0;
    int color_mode = 2;

    QImage src1(w_scale,h_scale,QImage::Format_RGB32);

    for(int x =0;x<srcInput.width();x++)
        for(int y=0;y<h_scale;y++){
            if(y<delta_h || y>=(delta_h +srcInput.height()) || x>=(w_scale-delta_w)) {
                if(color_mode==2) src1.setPixel(x,y,qRgb(255,255,255));
                else src1.setPixel(x,y,qRgb(0,0,0));
            }
            else{
                if((x+delta_w) < srcInput.width() && (y-delta_h)>=0 && (y-delta_h) < srcInput.height())
                    src1.setPixel(x,y,srcInput.pixel(x+delta_w,y-delta_h));
            }
        }

    src_width = srcInput.width();
    src_height = srcInput.height();

    float r = src_width/(float)src_height;

    QImage src2;
    if((r-0.5)<0.25){// 0.5
        src2 = src1.scaled(1500,3000);
    }
    if((r-0.5)>=0.25 && (r-0.5)<=0.75){// 1
        src2 = src1.scaled(1500,1500);
    }
    if((r-0.5)>=0.75 && (r-0.5)<=1.25){// 1.5
        src2 = src1.scaled(1500,1000);
    }
    if((r-0.5)>=1.25){// 2
        src2 = src1.scaled(1500,750);
    }

    int src_width_scaled = src2.width();
    int src_height_scaled = src2.height();

    QImage src = src2.mirrored();

    int w = src.width();
    int h = src.width();
    int ex_h = 40;
    int ex_w = 60;
    w = w + ex_w ;
    qDebug() << src_width_scaled << " " << src_height_scaled;
    if(src_height_scaled ==3000){
        IS_ROTATION90 = true;

        //mesh 1
        if(pageId == 1 && page_L_R==1){
            h = w  + 4550;
            x_trans= 0.02; y_trans = -0.495; theta =  29; zpoz = -1;
        }
        if(pageId == 1 && page_L_R==2){
            h = w  + 5000;
            x_trans= -0.01; y_trans = -0.535; theta =  27.5; zpoz = -1;
        }
        // mesh 2
        if(pageId == 2 && page_L_R==1){
            h = w  + 2650;
            x_trans= 0.01; y_trans = -0.265; theta =  40; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==2){
            h = w  + 2900;
            x_trans= 0.01; y_trans = -0.315; theta =  38.5; zpoz = -1;
        }
        // mesh 3
        if(pageId == 3 && page_L_R==1){
            h = w  + 2650;
            x_trans= -0.01; y_trans = -0.265; theta =  40; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==2){
            h = w  + 2700;
            x_trans= 0.01; y_trans = -0.275; theta =  40.5; zpoz = -1;
        }
        //
        if(pageId == 4 && page_L_R==1){
            h = w  + 2800;
            x_trans= 0.02; y_trans = -0.295; theta = 38; zpoz = -2;
        }
        if(pageId == 4 && page_L_R==2){
            h = w  + 2800;
            x_trans= -0.05; y_trans = -0.305; theta = 39.5; zpoz = -0;
        }
        if(pageId == 5 && page_L_R==1){
            h = w  + 2850;
            x_trans= 0.01; y_trans = -0.29; theta = 38.5; zpoz = -0;
        }
        if(pageId == 6 && page_L_R==1){
            h = w  + 2850;
            x_trans= 0.0; y_trans = -0.29; theta = 38.5; zpoz = -1.5;
        }
        if(pageId == 7 && page_L_R==1){
            h = w  + 2750;
            x_trans= -0.04; y_trans = -0.28; theta = 40; zpoz = -0.5;xpoz=-5;
        }
        if(pageId == 7 && page_L_R==2){
            h = w  + 2700;
            x_trans= -0.01; y_trans = -0.3; theta = 40; zpoz = -0.5;xpoz=-5;
        }

    }
    if(src_height_scaled ==1500){

        if(pageId == 1 && page_L_R==1){
            h = w  + 1520;
            x_trans= 0.02; y_trans = -0.487; theta =  28.5; zpoz = -1;
        }
        if(pageId == 1 && page_L_R==2){
            h = w  + 1820;
            x_trans= -0.01; y_trans = -0.535; theta =  27.5; zpoz = -1;        
        }

        if(pageId == 2 && page_L_R==1){
            h = w  + 620;
            x_trans= 0.0; y_trans = -0.27; theta =  39; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==2){
            h = w  + 750;
            x_trans= 0.015; y_trans = -0.33; theta =  38; zpoz = -1;
        }

        if(pageId == 3 && page_L_R==1){
            h = w  + 620;
            x_trans= -0.01; y_trans = -0.27; theta =  39; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==2){
            h = w  + 650;
            x_trans= 0.015; y_trans = -0.29; theta =  40; zpoz = -1;
        }
        if(pageId == 4 && page_L_R==1){
            h = w  + 700;
            x_trans= 0.02; y_trans = -0.305; theta = 37.5; zpoz = -2;
        }
        if(pageId == 4 && page_L_R==2){
            h = w  + 700;
            x_trans= -0.02; y_trans = -0.305; theta = 39; zpoz = -2;
        }
        if(pageId == 5 && page_L_R==1){
            h = w  + 700;
            x_trans= 0.0; y_trans = -0.285; theta = 38; zpoz = 1;
        }
        if(pageId == 6 && page_L_R==1){
            h = w  + 700;
            x_trans= 0.0; y_trans = -0.295; theta = 38; zpoz = -1;
        }

        if(pageId == 7 && page_L_R==1){
            h = w  + 700;
            x_trans= -0.05; y_trans = -0.295; theta = 39; zpoz = 1;
        }
        if(pageId == 7 && page_L_R==2){
            h = w  + 700;
            x_trans= -0.03; y_trans = -0.315; theta = 39; zpoz = -2;
        }
    }
    if(src_height_scaled ==1000){
        if(pageId == 1 && page_L_R==1){
            h = w  + 540;
            x_trans= 0.02; y_trans = -0.485; theta =  28.5; zpoz = -1;
        }
        if(pageId == 1 && page_L_R==2){
            h = w  + 650;
            x_trans= -0.01; y_trans = -0.515; theta =  27.5; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==1){
            h = w  - 100;
            x_trans= -0.0; y_trans = -0.255; theta =  39; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==2){
            h = w  - 50;
            x_trans= 0.01; y_trans = -0.285; theta =  38.5; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==1){
            h = w  - 120;
            x_trans= -0.01; y_trans = -0.245; theta =  39.3; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==2){
            h = w  - 120;
            x_trans= 0.01; y_trans = -0.245; theta =  40.3; zpoz = -3;
        }
        if(pageId == 4 && page_L_R==1){
            h = w  - 100;
            x_trans= 0.02; y_trans = -0.265; theta = 38; zpoz = -2;
        }
        if(pageId == 4 && page_L_R==2){
            h = w  - 100;
            x_trans= -0.02; y_trans = -0.265; theta = 39.5; zpoz = -2;
        }
        if(pageId == 5 && page_L_R==1){
            h = w  - 70;
            x_trans= 0.01; y_trans = -0.265; theta = 38.5; zpoz = -1;
        }
        if(pageId == 6 && page_L_R==1){
            h = w  - 70;
            x_trans= -0.01; y_trans = -0.265; theta = 38; zpoz = -1;
        }
        if(pageId == 7 && page_L_R==1){
            h = w  - 70;
            x_trans= -0.055; y_trans = -0.265; theta = 39; zpoz = 0;
        }
        if(pageId == 7 && page_L_R==2){
            h = w  - 70;
            x_trans= -0.04; y_trans = -0.285; theta = 39.5; zpoz = -3;
        }
    }
    if(src_height_scaled ==750){
        if(pageId == 1 && page_L_R==1){
            h = w  ;
            x_trans= 0.02; y_trans = -0.465; theta =  28.5; zpoz = -1;
        }
        if(pageId == 1 && page_L_R==2){
            h = w  + 100;
            x_trans= -0.01; y_trans = -0.505; theta =  27.5; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==1){
            h = w -450 ;
            x_trans= 0.0; y_trans = -0.245; theta =  39; zpoz = -1;
        }
        if(pageId == 2 && page_L_R==2){
            h = w -400 ;
            x_trans= 0.015; y_trans = -0.295; theta =  38; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==1){
            h = w -450 ;
            x_trans= 0.005; y_trans = -0.245; theta =  39; zpoz = -1;
        }
        if(pageId == 3 && page_L_R==2){
            h = w -470 ;
            x_trans= 0.015; y_trans = -0.245; theta =  40; zpoz = -2;
        }
        if(pageId == 4 && page_L_R==1){
            h = w  - 450;
            x_trans= 0.02; y_trans = -0.265; theta = 38; zpoz = -2;
        }
        if(pageId == 4 && page_L_R==2){
            h = w  - 450;
            x_trans= -0.02; y_trans = -0.255; theta = 39.5; zpoz = -2;
        }
        if(pageId == 5 && page_L_R==1){
            h = w  - 430;
            x_trans= 0.01; y_trans = -0.255; theta = 38; zpoz = -0;
        }
        if(pageId == 6 && page_L_R==1){
            h = w  - 430;
            x_trans= 0.005; y_trans = -0.255; theta = 38; zpoz = -1;
        }
        if(pageId == 7 && page_L_R==1){
            h = w  - 430;
            x_trans= 0.055; y_trans = -0.255; theta = 39; zpoz = 0;
        }
        if(pageId == 7 && page_L_R==2){
            h = w  - 430;
            x_trans= 0.035; y_trans = -0.285; theta = 39; zpoz = 0;
        }
    }
    img_plaquage_width = w;
    img_plaquage_height = h;

    QImage* out = new QImage(w,h,QImage::Format_RGB32);

    for(int x =0;x<w;x++)
        for(int y=0;y<h;y++) out->setPixel(x,y,qRgb(0,0,0));

    for(int x =0;x<src.width();x++)
        for(int y=(h-src.height()- ex_h);y<(h-ex_h);y++){
            if(y>=0 && y <h && (y + src.height() - h + ex_h) >=0 && (y + src.height() - h + ex_h) < src.height())
                out->setPixel(x+ ex_w/2,y,src.pixel(x,(y + src.height() - h + ex_h)));
        }

    out->save("tmp_image.png");
}

