//---------------------------------------------------------------------------

#ifndef StereoGH
#define StereoGH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <ExtDlgs.hpp>
#include "PERFGRAP.h"
#include <cv.h>
#include <cxcore.h>
#include <cvaux.h>
#include <highgui.h>
#include <Grids.hpp>
#include <ComCtrls.hpp>


//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TOpenPictureDialog *OpenPictureDialog1;
        TOpenPictureDialog *OpenPictureDialog2;
        TPanel *Panel1;
        TPanel *Panel2;
        TButton *LoadLeftBtn;
        TButton *LoadRightBtn;
        TButton *NextCoupleBtn;
        TButton *ExitBtn;
        TListBox *PointList1;
        TLabel *PointListLabel;
        TListBox *PointList2;
        TButton *Generate;
        TStringGrid *fund_mat;
        TLabel *LobrLabel;
        TLabel *RobrLabel;
        TLabel *FundMatLabel;
        TComboBox *MethodBox;
        TLabel *Popis1;
        TLabel *Popis2;
        TButton *FindCorBtn;
        TTrackBar *Panorama;
        TLabel *PanCapR;
        TLabel *PanCapL;
        TButton *InView;
        TLabel *AlphaValue;
        TButton *SaveBtn;
        TButton *FCor;
        TButton *TestCor;
        void __fastcall LoadLeftBtnClick(TObject *Sender);
        void __fastcall LoadRightBtnClick(TObject *Sender);
        void __fastcall NextCoupleBtnClick(TObject *Sender);
        void __fastcall ExitBtnClick(TObject *Sender);
        void __fastcall GenerateClick(TObject *Sender);
        void __fastcall FormActivate(TObject *Sender);
        void __fastcall FindCorBtnClick(TObject *Sender);
        void __fastcall PanoramaChange(TObject *Sender);
        void __fastcall InViewClick(TObject *Sender);
        void __fastcall SaveBtnClick(TObject *Sender);
        void __fastcall FCorClick(TObject *Sender);
        void __fastcall TestCorClick(TObject *Sender);
      
       
   




private:	// User declarations

public:		// User declarations
        __fastcall TForm1(TComponent* Owner);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
///---------------------------------------

void cvShowManyImages(char* title, int nArgs, ...) {

    // img - Used for getting the arguments
    IplImage *img;

    // DispImage - the image in which input images are to be copied
    IplImage *DispImage;

    int size;
    int i;
    int m, n;
    int x, y;

    // w - Maximum number of images in a row 
    // h - Maximum number of images in a column 
    int w, h;

    // scale - How much we have to resize the image
    float scale;
    int max;

    // If the number of arguments is lesser than 0 or greater than 12
    // return without displaying 
    if(nArgs <= 0) {
        printf("Number of arguments too small....\n");
        return;
    }
    else if(nArgs > 12) {
        printf("Number of arguments too large....\n");
        return;
    }
    // Determine the size of the image, 
    // and the number of rows/cols 
    // from number of arguments 
    else if (nArgs == 1) {
        w = h = 1;
        size = 300;
    }
    else if (nArgs == 2) {
        w = 2; h = 1;
        size = 300;
    }
    else if (nArgs == 3 || nArgs == 4) {
        w = 2; h = 2;
        size = 300;
    }
    else if (nArgs == 5 || nArgs == 6) {
        w = 3; h = 2;
        size = 200;
    }
    else if (nArgs == 7 || nArgs == 8) {
        w = 4; h = 2;
        size = 200;
    }
    else {
        w = 4; h = 3;
        size = 150;
    }
// Create a new 3 channel image
    DispImage = cvCreateImage( cvSize(100 + size*w, 60 + size*h), 8, 3 );

    // Used to get the arguments passed
    va_list args;
    va_start(args, nArgs);

    // Loop for nArgs number of arguments
    for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size)) {

        // Get the Pointer to the IplImage
        img = va_arg(args, IplImage*);

        // Check whether it is NULL or not
        // If it is NULL, release the image, and return
        if(img == 0) {
            printf("Invalid arguments");
            cvReleaseImage(&DispImage);
            return;
        }

        // Find the width and height of the image
        x = img->width;
        y = img->height;

        // Find whether height or width is greater in order to resize the image
        max = (x > y)? x: y;

        // Find the scaling factor to resize the image
        scale = (float) ( (float) max / size );

        // Used to Align the images
        if( i % w == 0 && m!= 20) {
            m = 20;
            n+= 20 + size;
        }

        // Set the image ROI to display the current image
        cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));

        // Resize the input image and copy the it to the Single Big Image
        cvResize(img, DispImage);

        // Reset the ROI in order to display the next image
        cvResetImageROI(DispImage);
    }

    // Create a new window, and show the Single Big Image
    cvNamedWindow( title, 1 );
    cvShowImage( title, DispImage);

    cvWaitKey();
    cvDestroyWindow(title);

    // End the number of arguments
    va_end(args);

    // Release the Image Memory
    cvReleaseImage(&DispImage);
}

