www.gusucode.com > 基于dwt的视频数字水印源代码 > 基于dwt的视频数字水印源代码\code\HaYDWT\Main.cpp

    //Download by http://www.NewXing.com
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "About.h"
#include "WatRecover.h"
#include "VideoConvert.h"
#include "Common.h"
#include "Clipbrd.hpp"
#include "MemUtil.h"
#include "FileServices.h"
#include "EmbedRecoverProcess.h"
#include "EmbedVideo.h"
#include "PSNRVideo.h"
#include "PSNRImage.h"
#include <math.h>
#include "SplashScreen.h"
// Start Random
// For Random number generator
#include "\UnifRamdom\randomc.h"
#include "\UnifRamdom\mersenne.cpp"
// End Random
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzTabs"
#pragma link "RzButton"
#pragma link "RzEdit"
#pragma link "RzLabel"
#pragma link "RzPanel"
#pragma link "RzStatus"
#pragma link "RzBckgnd"
#pragma link "RzRadChk"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::MeasureMenu(TObject *Sender,
      TCanvas *ACanvas, int &Width, int &Height)
{
    Width += 25;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::DrawMenuItem(TObject *Sender,
      TCanvas *ACanvas, TRect &ARect, TOwnerDrawState State)
{
    TCanvas *cTemp = new TCanvas;
    AnsiString sText;
    HWND mWnd;
    TRect rMenu;
    TMenuItem *sss = (TMenuItem *)Sender;
    sText = sss->Caption;
    if (State.Contains(odSelected))
    {
        ACanvas->Brush->Color = (TColor)RGB(110, 131, 184);
        ACanvas->Pen->Color = (TColor)RGB(47, 60, 93);
    }
    else
    {
        ACanvas->Brush->Color = clWindow;
        ACanvas->Pen->Color = clWindow;
    }
    ACanvas->Rectangle(ARect);

    if (CompareStr(sText, "-") == 0)
    {
        // Draw line
        ACanvas->Pen->Color = (TColor)RGB(0, 0, 0);
        ACanvas->MoveTo(ARect.Left, ARect.Top + ((ARect.Bottom - ARect.Top) / 2));
        ACanvas->LineTo(ARect.Right, ARect.Top + ((ARect.Bottom - ARect.Top) / 2));
    }
    else
    {
        ARect.Left += 12;
        ACanvas->TextRect(ARect, 5, 2, sText);
    }
    // Draw menu border
    mWnd = WindowFromDC(ACanvas->Handle);

    if (mWnd != Handle)
    {
        //cTemp = TCanvas.Create();
        cTemp->Handle = GetDC(0);

        GetWindowRect(mWnd, &rMenu);

        cTemp->Brush->Color = (TColor)RGB(120, 120, 120);
        cTemp->FrameRect(rMenu);

        InflateRect(&rMenu, -1, -1);
        cTemp->Brush->Color = clWindow;//RGB(240, 240, 240);
        cTemp->FrameRect(rMenu);

        InflateRect(&rMenu, -1, -1);
        cTemp->FrameRect(rMenu);

        ReleaseDC(0, cTemp->Handle);
        cTemp->Free();
    }
}
//---------------------------------------------------------------------------


void __fastcall TFormMain::mnuAboutClick(TObject *Sender)
{
    Application->CreateForm(__classid(TFormAbout), &FormAbout);

    FormAbout->ShowModal();

    FormAbout->Free();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::mnuEmbedWatermarkClick(TObject *Sender)
{
    MainPageControl->ActivePageIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::mnuRecoverWatermarkClick(TObject *Sender)
{
    MainPageControl->ActivePageIndex = 1;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::FormCreate(TObject *Sender)
{

//    Application->CreateForm(__classid(TFormSplash), &FormSplash);
//    FormSplash->ShowModal();

    InitDiractory = ExtractFilePath(Application->ExeName);
    //OpenDialog->InitialDir = InitDiractory;
    //SaveDialog->InitialDir = InitDiractory;

    sEWInputFile = "";
    sEWOutputFile = "";
    MainPageControl->ActivePageIndex = 0;

    HasInputVideo = false;
    isEWKeyRndGen = false;
    HasRWInputVideo = false;
    wantRWCalcCorrelation = false;

    DWTLevels = 2; //3;
    LevelEmbed = 1; //2;
    GainFactor = 5.0;
    DWTLevelsRW = 2; //3;
    LevelEmbedRW = 1; //2;
    BetaFactorRW = 0.05;

    pEWbmpImage = NULL;
    Gray_Image = NULL;
    Gray_Image_Key = NULL;
    Gray_Image_RW = NULL;
    Gray_Image_RW_Key = NULL;
    EWImageType = Img_NONE;
    EWKImageType = Img_NONE;
    RWImageType = Img_NONE;
    RWKImageType = Img_NONE;

    
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::mnuExitClick(TObject *Sender)
{
        this->Close();
}
//---------------------------------------------------------------------------





void __fastcall TFormMain::ButtonRWRecoverClick(TObject *Sender)
{
    Application->CreateForm(__classid(TFormRecover), &FormRecover);

    FormRecover->ShowModal();

    FormRecover->Free();
}
//---------------------------------------------------------------------------



void __fastcall TFormMain::mnuConvertVideoClick(TObject *Sender)
{

    Application->CreateForm(__classid(TFormVideoConvert), &FormVideoConvert);

    FormVideoConvert->ShowModal();

    FormVideoConvert->Free();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::mnuPSNRVideoClick(TObject *Sender)
{
    Application->CreateForm(__classid(TFormPSNRVideo), &FormPSNRVideo);

    FormPSNRVideo->ShowModal();

    FormPSNRVideo->Free();

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::mnuPSNRImageClick(TObject *Sender)
{

    Application->CreateForm(__classid(TFormPSNRImage), &FormPSNRImage);

    FormPSNRImage->ShowModal();

    FormPSNRImage->Free();
}
//---------------------------------------------------------------------------


void __fastcall TFormMain::ButtonEWInputFileClick(TObject *Sender)
{
        OpenDialog->Filter = FORMAT_9;
        OpenDialog->FileName = "";
        OpenDialog->FilterIndex = 0;
        OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
        if (OpenDialog->Execute())
        {
                sEWInputFile = OpenDialog->FileName;
                EditEWInputFile->Text = sEWInputFile;
                HasInputVideo = true;
                DisplayEmbedWatInfo();
        }
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonEWOutputFileClick(TObject *Sender)
{
        SaveDialog->Filter = FORMAT_9;
        SaveDialog->FileName = "";
        SaveDialog->FilterIndex = 0;
        SaveDialog->DefaultExt = ".avi";
        SaveDialog->Options << ofOverwritePrompt << ofPathMustExist;
        if (SaveDialog->Execute())
        {
                sEWOutputFile = SaveDialog->FileName;
                EditEWOutputFile->Text = sEWOutputFile;
                if (HasInputVideo)
                    DisplayEmbedWatInfo();
        }
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonEWWatermarkClick(TObject *Sender)
{
    Graphics::TBitmap *FormImage;

    OpenDialog->Filter = FORMAT_10;
    OpenDialog->FilterIndex = 0;
    OpenDialog->FileName = "";
    OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
    if (OpenDialog->Execute())
    {
        sEWImageName = OpenDialog->FileName;
        AnsiString stmp;
        AnsiString ss = UpperCase(ExtractFileExt(sEWImageName));
        if ( ss == ".BMP" )
        {
            FormImage = new Graphics::TBitmap();
            try
            {
                FormImage->LoadFromFile(sEWImageName);
                if ( (FormImage->Width < ImageEW->Width) || (FormImage->Height < ImageEW->Height) )
                   ImageEW->Stretch = false;
                else
                   ImageEW->Stretch = true;

                Clipboard()->Assign(FormImage);
                ImageEW->Picture->Assign(Clipboard());
            }
            __finally
            {
                delete FormImage;
            }
            if (pEWbmpImage == NULL)
            {
                pEWbmpImage = new Graphics::TBitmap();
            }
            else
            {
                delete pEWbmpImage;
                pEWbmpImage = new Graphics::TBitmap();
            }
            pEWbmpImage->LoadFromFile(sEWImageName);

            // clear Gray image memory
            if (Gray_Image != NULL)
            {
                Free_2D_UCHAR(Gray_Image, Gray_Image_Height);
                Gray_Image = NULL;
            }
            if (pEWbmpImage->PixelFormat == pf24bit)
            {
                EWImageType = Img_BMPColor24;
                stmp = "24-bit RGB color";
            }
            else if (pEWbmpImage->PixelFormat == pf8bit)
            {
                EWImageType = Img_BMPGray;
                stmp = "8-bit";
            }
            else
            {
                MessageDlg("This BMP format is not supported.\nYou cannot use this image in process.", mtError, TMsgDlgButtons() << mbOK, 0);
                EWImageType = Img_NONE;
                delete pEWbmpImage;
                pEWbmpImage = NULL;
            }
            ImageEW->Hint = "Image Size: " + IntToStr(ImageEW->Picture->Width) + "x" +
                            IntToStr(ImageEW->Picture->Height) + "\n" + "PixelFormat: " + stmp;
        }
        else if ( ss == ".PGM" )
        {
            long width, height, max_color, f_start, i, j;
            TCanvas *tmpCanvas;
            BYTE cImg;
            Application->ProcessMessages();
            // get PGM info
            if (!PGM_Read_Header(sEWImageName.c_str(), &width, &height, &max_color, &f_start))
            {
                return;
            }
            // allocate image memory
            Gray_Image = Alloc_2D_UCHAR(width, height);
            // read pgm data
            PGM_Get_Data(sEWImageName.c_str(), width, height, f_start, Gray_Image); // read data

            if ( (width < ImageEW->Width) || (height < ImageEW->Height) )
               ImageEW->Stretch = false;
            else
               ImageEW->Stretch = true;


            // Put gray image into visible picture area
            FormImage = new Graphics::TBitmap();

            FormImage->PixelFormat = pf24bit;
            FormImage->Width = width;
            FormImage->Height = height;
            tmpCanvas = FormImage->Canvas;
            tmpCanvas->Lock();
	        for(i=0;i<height;i++)
	        {
		        for(j=0;j<width;j++)
		        {
                    cImg = Gray_Image[i][j];
                    tmpCanvas->Pixels[j][i] = (TColor)RGB(cImg, cImg, cImg);
		        }
            }
            tmpCanvas->Unlock();
            ImageEW->Picture->Assign(FormImage);
            // Set global status
            Gray_Image_Width = width;
            Gray_Image_Height = height;
            EWImageType = Img_PGMGray;
            ImageEW->Hint = "Image Size: " + IntToStr(ImageEW->Picture->Width) + "x" +
                            IntToStr(ImageEW->Picture->Height) + "\n" + "PixelFormat: 8-bit Gray";

            delete FormImage;
        }
        else
        {
            MessageDlg("This format is not supported", mtError, TMsgDlgButtons() << mbOK, 0);
        }
        if (HasInputVideo)
            DisplayEmbedWatInfo();
    }

}
//---------------------------------------------------------------------------


void __fastcall TFormMain::ButtonEWKeyClick(TObject *Sender)
{
    Graphics::TBitmap *FormImage;

    OpenDialog->Filter = FORMAT_10;
    OpenDialog->FilterIndex = 0;
    OpenDialog->FileName = "";
    OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
    if (OpenDialog->Execute())
    {
        sEWKeyImageName = OpenDialog->FileName;
        AnsiString stmp;
        AnsiString ss = UpperCase(ExtractFileExt(sEWKeyImageName));
        if ( ss == ".BMP" )
        {
            FormImage = new Graphics::TBitmap();
            try
            {
                FormImage->LoadFromFile(sEWKeyImageName);
                if ( (FormImage->Width < ImageEWKey->Width) || (FormImage->Height < ImageEWKey->Height) )
                   ImageEWKey->Stretch = false;
                else
                   ImageEWKey->Stretch = true;

                Clipboard()->Assign(FormImage);
                ImageEWKey->Picture->Assign(Clipboard());
            }
            __finally
            {
                delete FormImage;
            }
            if (pEWKeybmpImage == NULL)
            {
                pEWKeybmpImage = new Graphics::TBitmap();
            }
            else
            {
                delete pEWKeybmpImage;
                pEWKeybmpImage = new Graphics::TBitmap();
            }
            pEWKeybmpImage->LoadFromFile(sEWKeyImageName);

            ULONG uuul;

            // clear Gray image memory
            if (Gray_Image_Key != NULL)
            {
                Free_2D_UCHAR(Gray_Image_Key, Gray_Image_Key_Height);
                Gray_Image_Key = NULL;
            }
            if (pEWKeybmpImage->PixelFormat == pf24bit)
            {
                EWKImageType = Img_BMPColor24;
                stmp = "24-bit RGB color";
            }
            else if (pEWKeybmpImage->PixelFormat == pf8bit)
            {
                EWKImageType = Img_BMPGray;
                stmp = "8-bit";
            }
            else
            {
                MessageDlg("This BMP format is not supported.\nYou cannot use this image in process.", mtError, TMsgDlgButtons() << mbOK, 0);
                EWKImageType = Img_NONE;
                delete pEWKeybmpImage;
                pEWKeybmpImage = NULL;
            }
            ImageEWKey->Hint = "Image Size: " + IntToStr(ImageEWKey->Picture->Width) + "x" +
                            IntToStr(ImageEWKey->Picture->Height) + "\n" + "PixelFormat: " + stmp;
        }
        else if ( ss == ".PGM" )
        {
            long width, height, max_color, f_start, i, j;
            TCanvas *tmpCanvas;
            BYTE cImg;
            Application->ProcessMessages();
            // get PGM info
            if (!PGM_Read_Header(sEWKeyImageName.c_str(), &width, &height, &max_color, &f_start))
            {
                return;
            }
            // allocate image memory
            Gray_Image_Key = Alloc_2D_UCHAR(width, height);
            // read pgm data
            PGM_Get_Data(sEWKeyImageName.c_str(), width, height, f_start, Gray_Image_Key); // read data

            if ( (width < ImageEWKey->Width ) || (height < ImageEWKey->Height) )
               ImageEWKey->Stretch = false;
            else
               ImageEWKey->Stretch = true;

            // Put gray image into visible picture area
            FormImage = new Graphics::TBitmap();

            FormImage->PixelFormat = pf24bit;
            FormImage->Width = width;
            FormImage->Height = height;
            tmpCanvas = FormImage->Canvas;
            tmpCanvas->Lock();
	        for(i=0;i<height;i++)
	        {
		        for(j=0;j<width;j++)
		        {
                    cImg = Gray_Image_Key[i][j];
                    tmpCanvas->Pixels[j][i] = (TColor)RGB(cImg, cImg, cImg);
		        }
            }
            tmpCanvas->Unlock();
            ImageEWKey->Picture->Assign(FormImage);
            // Set global status
            Gray_Image_Key_Width = width;
            Gray_Image_Key_Height = height;
            EWKImageType = Img_PGMGray;
            ImageEWKey->Hint = "Image Size: " + IntToStr(ImageEWKey->Picture->Width) + "x" +
                            IntToStr(ImageEWKey->Picture->Height) + "\n" + "PixelFormat: 8-bit Gray";

            delete FormImage;
        }
        else
        {
            MessageDlg("This format is not supported", mtError, TMsgDlgButtons() << mbOK, 0);
            return;
        }
        if (HasInputVideo)
            DisplayEmbedWatInfo();
        isEWKeyRndGen = false;    
    }

}
//---------------------------------------------------------------------------


void __fastcall TFormMain::ButtonRWInputFileClick(TObject *Sender)
{
        OpenDialog->Filter = FORMAT_9;
        OpenDialog->FilterIndex = 0;
        OpenDialog->FileName = "";
        OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
        if (OpenDialog->Execute())
        {
                sRWInputFile = OpenDialog->FileName;
                EditRWInputFile->Text = sRWInputFile;
                HasRWInputVideo = true;
                DisplayRecoverWatInfo();
        }

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonRWWatermarkClick(TObject *Sender)
{
    Graphics::TBitmap *FormImage;

    OpenDialog->Filter = FORMAT_10;
    OpenDialog->FilterIndex = 0;
    OpenDialog->FileName = "";
    OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
    if (OpenDialog->Execute())
    {
        sRWImageName = OpenDialog->FileName;
        AnsiString stmp;
        AnsiString ss = UpperCase(ExtractFileExt(sRWImageName));
        if ( ss == ".BMP" )
        {
            FormImage = new Graphics::TBitmap();
            try
            {
                FormImage->LoadFromFile(sRWImageName);
                if ( (FormImage->Width < ImageRW->Width) || (FormImage->Height < ImageRW->Height) )
                   ImageRW->Stretch = false;
                else
                   ImageRW->Stretch = true;
                Clipboard()->Assign(FormImage);
                ImageRW->Picture->Assign(Clipboard());
            }
            __finally
            {
                delete FormImage;
            }
            if (pRWbmpImage == NULL)
            {
                pRWbmpImage = new Graphics::TBitmap();
            }
            else
            {
                delete pRWbmpImage;
                pRWbmpImage = new Graphics::TBitmap();
            }
            pRWbmpImage->LoadFromFile(sRWImageName);

            // clear Gray image memory
            if (Gray_Image_RW != NULL)
            {
                Free_2D_UCHAR(Gray_Image_RW, Gray_Image_RW_Height);
                Gray_Image_RW = NULL;
            }
            if (pRWbmpImage->PixelFormat == pf24bit)
            {
                RWImageType = Img_BMPColor24;
                stmp = "24-bit RGB color";
            }
            else if (pRWbmpImage->PixelFormat == pf8bit)
            {
                RWImageType = Img_BMPGray;
                stmp = "8-bit";
            }
            else
            {
                MessageDlg("This BMP format is not supported.\nYou cannot use this image in process.", mtError, TMsgDlgButtons() << mbOK, 0);
                RWImageType = Img_NONE;
                delete pRWbmpImage;
                pRWbmpImage = NULL;
            }
            ImageRW->Hint = "Image Size: " + IntToStr(ImageRW->Picture->Width) + "x" +
                            IntToStr(ImageRW->Picture->Height) + "\n" + "PixelFormat: " + stmp;
        }
        else if ( ss == ".PGM" )
        {
            long width, height, max_color, f_start, i, j;
            TCanvas *tmpCanvas;
            BYTE cImg;
            Application->ProcessMessages();
            // get PGM info
            if (!PGM_Read_Header(sRWImageName.c_str(), &width, &height, &max_color, &f_start))
            {
                return;
            }
            // allocate image memory
            Gray_Image_RW = Alloc_2D_UCHAR(width, height);
            // read pgm data
            PGM_Get_Data(sRWImageName.c_str(), width, height, f_start, Gray_Image_RW); // read data

            if ( (width < ImageRW->Width) || (height < ImageRW->Height) )
               ImageRW->Stretch = false;
            else
               ImageRW->Stretch = true;

            // Put gray image into visible picture area
            FormImage = new Graphics::TBitmap();

            FormImage->PixelFormat = pf24bit;
            FormImage->Width = width;
            FormImage->Height = height;
            tmpCanvas = FormImage->Canvas;
            tmpCanvas->Lock();
	        for(i=0;i<height;i++)
	        {
		        for(j=0;j<width;j++)
		        {
                    cImg = Gray_Image_RW[i][j];
                    tmpCanvas->Pixels[j][i] = (TColor)RGB(cImg, cImg, cImg);
		        }
            }
            tmpCanvas->Unlock();
            ImageRW->Picture->Assign(FormImage);
            // Set global status
            Gray_Image_RW_Width = width;
            Gray_Image_RW_Height = height;
            RWImageType = Img_PGMGray;
            ImageRW->Hint = "Image Size: " + IntToStr(ImageRW->Picture->Width) + "x" +
                            IntToStr(ImageRW->Picture->Height) + "\n" + "PixelFormat: 8-bit Gray";

            delete FormImage;
        }
        else
        {
            MessageDlg("This format is not supported", mtError, TMsgDlgButtons() << mbOK, 0);
            return;
        }
        wantRWCalcCorrelation = true;
        if (HasRWInputVideo)
            DisplayRecoverWatInfo();
    }

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonRWKeyClick(TObject *Sender)
{
    Graphics::TBitmap *FormImage;

    OpenDialog->Filter = FORMAT_10;
    OpenDialog->FilterIndex = 0;
    OpenDialog->FileName = "";
    OpenDialog->Options <<  ofPathMustExist << ofFileMustExist;
    if (OpenDialog->Execute())
    {
        sRWKeyImageName = OpenDialog->FileName;
        AnsiString stmp;
        AnsiString ss = UpperCase(ExtractFileExt(sRWKeyImageName));
        if ( ss == ".BMP" )
        {
            FormImage = new Graphics::TBitmap();
            try
            {
                FormImage->LoadFromFile(sRWKeyImageName);
                if ( (FormImage->Width < ImageRWKey->Width) || (FormImage->Height < ImageRWKey->Height) )
                   ImageRWKey->Stretch = false;
                else
                   ImageRWKey->Stretch = true;

                Clipboard()->Assign(FormImage);
                ImageRWKey->Picture->Assign(Clipboard());
            }
            __finally
            {
                delete FormImage;
            }
            if (pRWKeybmpImage == NULL)
            {
                pRWKeybmpImage = new Graphics::TBitmap();
            }
            else
            {
                delete pRWKeybmpImage;
                pRWKeybmpImage = new Graphics::TBitmap();
            }
            pRWKeybmpImage->LoadFromFile(sRWKeyImageName);

            // clear Gray image memory
            if (Gray_Image_RW_Key != NULL)
            {
                Free_2D_UCHAR(Gray_Image_RW_Key, Gray_Image_RW_Key_Height);
                Gray_Image_RW_Key = NULL;
            }
            if (pRWKeybmpImage->PixelFormat == pf24bit)
            {
                RWKImageType = Img_BMPColor24;
                stmp = "24-bit RGB color";
            }
            else if (pRWKeybmpImage->PixelFormat == pf8bit)
            {
                RWKImageType = Img_BMPGray;
                stmp = "8-bit";
            }
            else
            {
                MessageDlg("This BMP format is not supported.\nYou cannot use this image in process.", mtError, TMsgDlgButtons() << mbOK, 0);
                RWKImageType = Img_NONE;
                delete pRWKeybmpImage;
                pRWKeybmpImage = NULL;
            }
            ImageRWKey->Hint = "Image Size: " + IntToStr(ImageRWKey->Picture->Width) + "x" +
                            IntToStr(ImageRWKey->Picture->Height) + "\n" + "PixelFormat: " + stmp;
        }
        else if ( ss == ".PGM" )
        {
            long width, height, max_color, f_start, i, j;
            TCanvas *tmpCanvas;
            BYTE cImg;
            Application->ProcessMessages();
            // get PGM info
            if (!PGM_Read_Header(sRWKeyImageName.c_str(), &width, &height, &max_color, &f_start))
            {
                return;
            }
            // allocate image memory
            Gray_Image_RW_Key = Alloc_2D_UCHAR(width, height);
            // read pgm data
            PGM_Get_Data(sRWKeyImageName.c_str(), width, height, f_start, Gray_Image_RW_Key); // read data

            if ( (width < ImageRWKey->Width) || (height < ImageRWKey->Height) )
               ImageRWKey->Stretch = false;
            else
               ImageRWKey->Stretch = true;
            // Put gray image into visible picture area
            FormImage = new Graphics::TBitmap();

            FormImage->PixelFormat = pf24bit;
            FormImage->Width = width;
            FormImage->Height = height;
            tmpCanvas = FormImage->Canvas;
            tmpCanvas->Lock();
	        for(i=0;i<height;i++)
	        {
		        for(j=0;j<width;j++)
		        {
                    cImg = Gray_Image_RW_Key[i][j];
                    tmpCanvas->Pixels[j][i] = (TColor)RGB(cImg, cImg, cImg);
		        }
            }
            tmpCanvas->Unlock();
            ImageRWKey->Picture->Assign(FormImage);
            // Set global status
            Gray_Image_RW_Key_Width = width;
            Gray_Image_RW_Key_Height = height;
            RWKImageType = Img_PGMGray;
            ImageRWKey->Hint = "Image Size: " + IntToStr(ImageRWKey->Picture->Width) + "x" +
                            IntToStr(ImageRWKey->Picture->Height) + "\n" + "PixelFormat: 8-bit Gray";

            delete FormImage;
        }
        else
        {
            MessageDlg("This format is not supported", mtError, TMsgDlgButtons() << mbOK, 0);
            return;
        }
        if (HasRWInputVideo)
            DisplayRecoverWatInfo();
    }

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditGainFactorChange(TObject *Sender)
{
    GainFactor = (double)EditGainFactor->Value;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditDWTLevelsChange(TObject *Sender)
{
    DWTLevels = (int)EditDWTLevels->Value;
    if (HasInputVideo)
        DisplayEmbedWatInfo();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditLevelEmbedChange(TObject *Sender)
{
    LevelEmbed = (int)EditLevelEmbed->Value;
    if (HasInputVideo)
        DisplayEmbedWatInfo();
}
//---------------------------------------------------------------------------

void TFormMain::DisplayEmbedWatInfo()
{
    long width, height;
    long tmpW, tmpH;
	TAviUtil *inAvi;
	HAVI Avi;
    bool canEmbed = false;


    if (!FileExists(sEWInputFile))
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nInput video file doesn't exist.";
        ButtonEWEmbed->Enabled = false;
        return;
    }
	Avi = RDOpenAviFile(sEWInputFile.c_str(), false);
	if (Avi == NULL)
	{
		DispError("ERROR! Cannot open AVI file.");
		return;
	}
	inAvi = (TAviUtil*)Avi;
    width = inAvi->iWidth;
    height = inAvi->iHeight;
    RDCloseAvi(Avi);
    inAvi = NULL;
    if ( DWTLevels < LevelEmbed )
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nDWT Levels cannot be smaller than Embed Levels.";
        ButtonEWEmbed->Enabled = false;
        return;
    }
    // Check the dimensions for compatability.
    // if this is not correct we cannot transform video to wavelet domain
    if (width%(1 << DWTLevels) || height%(1 << DWTLevels))
	{
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nwidth and height of input video must be divisible by 2^" + IntToStr(DWTLevels) + "=" + IntToStr((int)pow(2, DWTLevels));
        ButtonEWEmbed->Enabled = false;
        return;
	}

    if ((DWTLevels == 2) || (DWTLevels == 3))
    {
        if (LevelEmbed == 1)
        {
            tmpW = width >> 1; // division by 2
            tmpH = height >> 1;
        }
        else if (LevelEmbed == 2)
        {
            tmpW = width >> 2; // division by 4
            tmpH = height >> 2;
        }
        else
        {
            LabelDisplayInfo->Font->Color = clRed;
            LabelDisplayInfo->Caption = "ERROR!!!\nThis software supports 2-level and 3-level DWT AND level-1 or level-2 embeding.";
            ButtonEWEmbed->Enabled = false;
            return;
        }
    }
    else
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nThis software supports 2-level and 3-level DWT AND level-1 or level-2 embeding.";
        ButtonEWEmbed->Enabled = false;
        return;
    }
    // Write generated key image hints
    ButtonGenKey->Hint = "Suggested size of the generated key is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
    EditGenKeyWidth->Hint = "Suggested width is " + IntToStr(tmpW);
    EditGenKeyHeight->Hint = "Suggested height is " + IntToStr(tmpH);

    // check the Key image size
    if (EWKImageType == Img_PGMGray)
    {
        if ( (Gray_Image_Key_Width != tmpW) || (Gray_Image_Key_Height != tmpH))
        {
            LabelDisplayInfo->Font->Color = clRed;
            LabelDisplayInfo->Caption = "ERROR!!!\nSize of Key must fit the DWT sub-frequency size of the video frame. Cannot use this Key.\nSuggested Key size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = false;
        }
        else
        {
            LabelDisplayInfo->Font->Color = clHotLight;
            LabelDisplayInfo->Caption = "OK!!!\nKey size matches the input video wavelet sub-frequency settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = true;
        }
    }
    else if ( (EWKImageType == Img_BMPGray) || (EWKImageType == Img_BMPColor24) )
    {
        if ( (pEWKeybmpImage->Width != tmpW) || (pEWKeybmpImage->Height != tmpH))
        {
            LabelDisplayInfo->Font->Color = clRed;
            LabelDisplayInfo->Caption = "ERROR!!!\nSize of Key must fit the DWT sub-frequency size of the video frame. Cannot use this Key.\nSuggested Key size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = false;
        }
        else
        {
            LabelDisplayInfo->Font->Color = clHotLight;
            LabelDisplayInfo->Caption = "OK!!!\nKey size matches the input video wavelet sub-frequency settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = true;
        }
    }
    else if (EWKImageType == Img_NONE)
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nKey image does not exist. Select a key image or generate by pressing 'Generate' button. You should generate a " + IntToStr(tmpW) + "x" + IntToStr(tmpH) + " key image.";
        canEmbed = false;
        return;
    }

    // calculate requered size for watermark image
    if (LevelEmbed == 1)
    {
        tmpW = width >> 2; // division by 4
        tmpH = height >> 2;
    }
    else if (LevelEmbed == 2)
    {
        tmpW = width >> 3; // division by 8
        tmpH = height >> 3;
    }
    // check the watermark image size
    if (EWImageType == Img_PGMGray)
    {
        if ( (Gray_Image_Width != tmpW) || (Gray_Image_Height != tmpH))
        {
            LabelDisplayInfo->Font->Color = clRed;
            LabelDisplayInfo->Caption = "ERROR!!!\nSize of Watermark image doesn't match the input video.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = false;
        }
        else
        {
            LabelDisplayInfo->Font->Color = clHotLight;
            LabelDisplayInfo->Caption = "OK!!!\nSize of the Watermark matches your settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = true;
        }
    }
    else if ( (EWImageType == Img_BMPGray) || (EWImageType == Img_BMPColor24) )
    {
        if ( (pEWbmpImage->Width != tmpW) || (pEWbmpImage->Height != tmpH))
        {
            LabelDisplayInfo->Font->Color = clRed;
            LabelDisplayInfo->Caption = "ERROR!!!\nSize of Watermark image doesn't match the input video.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = false;
        }
        else
        {
            LabelDisplayInfo->Font->Color = clHotLight;
            LabelDisplayInfo->Caption = "OK!!!\nSize of the Watermark matches your settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canEmbed = true;
        }
    }
    else if (EWImageType == Img_NONE)
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nWatermark image does not exist. Select a watermark image.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
        canEmbed = false;
    }
    if ( sEWOutputFile == "")
    {
        LabelDisplayInfo->Font->Color = clRed;
        LabelDisplayInfo->Caption = "ERROR!!!\nSelect an output video file.";
        canEmbed = false;
    }
    if (canEmbed)
        ButtonEWEmbed->Enabled = true;
    else
        ButtonEWEmbed->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditGenKeyWidthChange(TObject *Sender)
{
    if ( (EditGenKeyWidth->Value > 0.0) && (EditGenKeyHeight->Value > 0.0))
    {
        ButtonGenKey->Hint = "This will generate a random image with " + IntToStr((int)EditGenKeyWidth->Value) + "x" + IntToStr((int)EditGenKeyHeight->Value) + " sizes.";
    }
    else
        ButtonGenKey->Hint = "";
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditGenKeyHeightChange(TObject *Sender)
{
    if ( (EditGenKeyWidth->Value > 0.0) && (EditGenKeyHeight->Value > 0.0))
    {
        ButtonGenKey->Hint = "This will generate a random image with " + IntToStr((int)EditGenKeyWidth->Value) + "x" + IntToStr((int)EditGenKeyHeight->Value) + " sizes.";
    }
    else
        ButtonGenKey->Hint = "";

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonGenKeyClick(TObject *Sender)
{

    long width, height, max_color, f_start, i, j;
    TCanvas *tmpCanvas;
    BYTE cImg;
    int32 seed, ir;
    Graphics::TBitmap *FormImage;

    width = (long)EditGenKeyWidth->Value;
    height = (long)EditGenKeyHeight->Value;
    if ( (width < 1) && (height < 1))
    {
        MessageDlg("Firstly, you must set Width and Height values.", mtError, TMsgDlgButtons() << mbOK, 0);
        return;
    }
    // for 2x2 sub matrix
    if (((width%2) != 0) || ((height%2) != 0))
    {
        MessageDlg("Image width and height must be divisible by 2.", mtError, TMsgDlgButtons() << mbOK, 0);
        return;
    }

    seed = time(0);   // random seed
    TRandomMersenne RandomGenerator(seed);

    Application->ProcessMessages();

    if ( (width < ImageEWKey->Width) || (height < ImageEWKey->Height) )
       ImageEWKey->Stretch = false;
    else
       ImageEWKey->Stretch = true;

    // allocate image memory
    Gray_Image_Key = Alloc_2D_UCHAR(width, height);

    // Generate random image and fill Gray_Image_Key and Display Bitmap

    FormImage = new Graphics::TBitmap();

    FormImage->PixelFormat = pf24bit;
    FormImage->Width = width;
    FormImage->Height = height;
    tmpCanvas = FormImage->Canvas;
    tmpCanvas->Lock();
/*
    for(i=0;i<height;i++)
    {
        for(j=0;j<width;j++)
        {
            //ir = RandomGenerator.IRandom(0,255);
            ir = RandomGenerator.IRandom(0,1);  // this means 0 is -1, 1 is 0 , 2 is 1
            Gray_Image_Key[i][j] = (UCHAR)ir;
            tmpCanvas->Pixels[j][i] = (TColor)RGB(ir, ir, ir);
        }
    }
*/
    strcRandArr rndArr[8];
    // There are 8 different 2x2 matrix type that
    // each one's mean value is not 0 or (-)1.  
    rndArr[0].A[0] = 0;
    rndArr[0].A[1] = 1;
    rndArr[0].A[2] = 1;
    rndArr[0].A[3] = 1;
    rndArr[1].A[0] = 1;
    rndArr[1].A[1] = 0;
    rndArr[1].A[2] = 1;
    rndArr[1].A[3] = 1;
    rndArr[2].A[0] = 1;
    rndArr[2].A[1] = 1;
    rndArr[2].A[2] = 1;
    rndArr[2].A[3] = 0;
    rndArr[3].A[0] = 1;
    rndArr[3].A[1] = 1;
    rndArr[3].A[2] = 0;
    rndArr[3].A[3] = 1;
    rndArr[4].A[0] = 1;
    rndArr[4].A[1] = 0;
    rndArr[4].A[2] = 0;
    rndArr[4].A[3] = 0;
    rndArr[5].A[0] = 0;
    rndArr[5].A[1] = 1;
    rndArr[5].A[2] = 0;
    rndArr[5].A[3] = 0;
    rndArr[6].A[0] = 0;
    rndArr[6].A[1] = 0;
    rndArr[6].A[2] = 0;
    rndArr[6].A[3] = 1;
    rndArr[7].A[0] = 0;
    rndArr[7].A[1] = 0;
    rndArr[7].A[2] = 1;
    rndArr[7].A[3] = 0;
    for(i=0;i<height/2;i++)
    {
        for(j=0;j<width/2;j++)
        {
            ir = RandomGenerator.IRandom(0,7);

            Gray_Image_Key[i*2][j*2] = rndArr[ir].A[0];
            Gray_Image_Key[i*2][j*2+1] = rndArr[ir].A[1];
            Gray_Image_Key[i*2+1][j*2] = rndArr[ir].A[2];
            Gray_Image_Key[i*2+1][j*2+1] = rndArr[ir].A[3];

            tmpCanvas->Pixels[j*2][i*2] = (TColor)RGB(rndArr[ir].A[0], rndArr[ir].A[0], rndArr[ir].A[0]);
            tmpCanvas->Pixels[j*2+1][i*2] = (TColor)RGB(rndArr[ir].A[1], rndArr[ir].A[1], rndArr[ir].A[1]);
            tmpCanvas->Pixels[j*2][i*2+1] = (TColor)RGB(rndArr[ir].A[2], rndArr[ir].A[2], rndArr[ir].A[2]);
            tmpCanvas->Pixels[j*2+1][i*2+1] = (TColor)RGB(rndArr[ir].A[3], rndArr[ir].A[3], rndArr[ir].A[3]);
        }
    }
    tmpCanvas->Unlock();
    ImageEWKey->Picture->Assign(FormImage);
    // Set global status
    Gray_Image_Key_Width = width;
    Gray_Image_Key_Height = height;
    EWKImageType = Img_PGMGray;
    ImageEWKey->Hint = "Image Size: " + IntToStr(ImageEWKey->Picture->Width) + "x" +
                    IntToStr(ImageEWKey->Picture->Height) + "\nPixelFormat: 8-bit Gray";
    delete FormImage;

    if (HasInputVideo)
        DisplayEmbedWatInfo();
    ButtonSaveGenKey->Enabled = true;
    isEWKeyRndGen = true;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonEWEmbedClick(TObject *Sender)
{
    Application->CreateForm(__classid(TFormEmbedVideo), &FormEmbedVideo);

    FormEmbedVideo->ShowModal();
    
    FormEmbedVideo->Free();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonSaveGenKeyClick(TObject *Sender)
{
    AnsiString sFileName;
    Graphics::TBitmap *FormImage;
    TCanvas *tmpCanvas;
    UCHAR cImg;
    long i, j;

    // is Random Image Generated?
    if (!isEWKeyRndGen)
    {
        MessageDlg("Random image is not generated.", mtError, TMsgDlgButtons() << mbOK, 0);
        return;
    }
    if (Gray_Image_Key == NULL)
    {
        MessageDlg("Rangom image matrix is empty", mtError, TMsgDlgButtons() << mbOK, 0);
        return;
    }

    SaveDialog->Filter = "PGM Files|*.pgm|BMP Files|*.bmp|All Files|*.*";
    SaveDialog->DefaultExt = ".pgm";
    SaveDialog->FileName = "";
    SaveDialog->FilterIndex = 0;
    SaveDialog->Options << ofOverwritePrompt << ofPathMustExist;
    if (SaveDialog->Execute())
    {
        sFileName = SaveDialog->FileName;
        if (FileExists(sFileName.c_str()))
            DeleteFileA(sFileName.c_str());
        if (UpperCase(ExtractFileExt(sFileName)) == ".PGM")
        {
            PGM_Put_Data(sFileName.c_str(), Gray_Image_Key_Width, Gray_Image_Key_Height,	255, Gray_Image_Key);
        }
        else if (UpperCase(ExtractFileExt(sFileName)) == ".BMP")
        {
            FormImage = new Graphics::TBitmap();

            FormImage->PixelFormat = pf24bit;
            FormImage->Width = Gray_Image_Key_Width;
            FormImage->Height = Gray_Image_Key_Height;
            tmpCanvas = FormImage->Canvas;
            tmpCanvas->Lock();
	        for(i=0;i<Gray_Image_Key_Height;i++)
	        {
		        for(j=0;j<Gray_Image_Key_Width;j++)
		        {
                    cImg = Gray_Image_Key[i][j];
                    tmpCanvas->Pixels[j][i] = (TColor)RGB(cImg, cImg, cImg);
		        }
            }
            tmpCanvas->Unlock();
            FormImage->SaveToFile(sFileName);

            delete FormImage;
        }
        else
        {
            MessageDlg("This format is not supported", mtError, TMsgDlgButtons() << mbOK, 0);
            return;
        }
        ButtonSaveGenKey->Enabled = false;
    }

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditDWTLevelsRWChange(TObject *Sender)
{
    DWTLevelsRW = (int)EditDWTLevelsRW->Value;
    if (HasRWInputVideo)
        DisplayRecoverWatInfo();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditBetaFactorRWChange(TObject *Sender)
{

    BetaFactorRW = (double)StrToFloat(EditBetaFactorRW->Text);    
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditLevelEmbedRWChange(TObject *Sender)
{
    LevelEmbedRW = (int)EditLevelEmbedRW->Value;
    if (HasRWInputVideo)
        DisplayRecoverWatInfo();
}
//---------------------------------------------------------------------------

void TFormMain::DisplayRecoverWatInfo()
{
    long width, height;
    long tmpW, tmpH;
	TAviUtil *inAvi;
	HAVI Avi;
    bool canRecover = false;


    if (!FileExists(sRWInputFile))
    {
        LabelDisplayInfoRW->Font->Color = clRed;
        LabelDisplayInfoRW->Caption = "ERROR!!!\nInput video file doesn't exist.";
        ButtonRWRecover->Enabled = false;
        return;
    }
	Avi = RDOpenAviFile(sRWInputFile.c_str(), false);
	if (Avi == NULL)
	{
		DispError("ERROR! Cannot open AVI file.");
		return;
	}
	inAvi = (TAviUtil*)Avi;
    width = inAvi->iWidth;
    height = inAvi->iHeight;
    RDCloseAvi(Avi);
    inAvi = NULL;

    if ( DWTLevelsRW < LevelEmbedRW )
    {
        LabelDisplayInfoRW->Font->Color = clRed;
        LabelDisplayInfoRW->Caption = "ERROR!!!\nDWT Levels cannot be smaller than Embed Levels.";
        ButtonRWRecover->Enabled = false;
        return;
    }
    // Check the dimensions for compatability.
    // if this is not correct we cannot transform video to wavelet domain
    if (width%(1 << DWTLevelsRW) || height%(1 << DWTLevelsRW))
	{
        LabelDisplayInfoRW->Font->Color = clRed;
        LabelDisplayInfoRW->Caption = "ERROR!!!\nwidth and height of input video must be divisible by 2^" + IntToStr(DWTLevels) + "=" + IntToStr((int)pow(2, DWTLevels));
        ButtonRWRecover->Enabled = false;
        return;
	}

    if ((DWTLevelsRW == 2) || (DWTLevelsRW == 3))
    {
        if (LevelEmbedRW == 1)
        {
            tmpW = width >> 1; // division by 2
            tmpH = height >> 1;
        }
        else if (LevelEmbedRW == 2)
        {
            tmpW = width >> 2; // division by 4
            tmpH = height >> 2;
        }
        else
        {
            LabelDisplayInfoRW->Font->Color = clRed;
            LabelDisplayInfoRW->Caption = "ERROR!!!\nThis software supports 2-level and 3-level DWT AND level-1 or level-2 embeding.";
            ButtonRWRecover->Enabled = false;
            return;
        }
    }
    else
    {
        LabelDisplayInfoRW->Font->Color = clRed;
        LabelDisplayInfoRW->Caption = "ERROR!!!\nThis software supports 2-level and 3-level DWT AND level-1 or level-2 embeding.";
        ButtonRWRecover->Enabled = false;
        return;
    }

    // check the Key image size
    if (RWKImageType == Img_PGMGray)
    {
        if ( (Gray_Image_RW_Key_Width != tmpW) || (Gray_Image_RW_Key_Height != tmpH))
        {
            LabelDisplayInfoRW->Font->Color = clRed;
            LabelDisplayInfoRW->Caption = "ERROR!!!\nSize of Key must fit the DWT sub-frequency size of the video frame. Cannot use this Key.\nSuggested Key size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canRecover = false;
        }
        else
        {
            LabelDisplayInfoRW->Font->Color = clHotLight;
            LabelDisplayInfoRW->Caption = "OK!!!\nKey size matches the input video wavelet sub-frequency settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canRecover = true;
        }
    }
    else if ( (RWKImageType == Img_BMPGray) || (RWKImageType == Img_BMPColor24) )
    {
        if ( (pRWKeybmpImage->Width != tmpW) || (pRWKeybmpImage->Height != tmpH))
        {
            LabelDisplayInfoRW->Font->Color = clRed;
            LabelDisplayInfoRW->Caption = "ERROR!!!\nSize of Key must fit the DWT sub-frequency size of the video frame. Cannot use this Key.\nSuggested Key size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canRecover = false;
        }
        else
        {
            LabelDisplayInfoRW->Font->Color = clHotLight;
            LabelDisplayInfoRW->Caption = "OK!!!\nKey size matches the input video wavelet sub-frequency settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canRecover = true;
        }
    }
    else if (RWKImageType == Img_NONE)
    {
        LabelDisplayInfoRW->Font->Color = clRed;
        LabelDisplayInfoRW->Caption = "ERROR!!!\nKey image does not exist. Load a key image.\nSuggested Key size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
        canRecover = false;
        return;
    }

    if (wantRWCalcCorrelation) // user wants to calculate correlation
    {
        // calculate requered size for watermark image
        if (LevelEmbedRW == 1)
        {
            tmpW = width >> 2; // division by 4
            tmpH = height >> 2;
        }
        else if (LevelEmbedRW == 2)
        {
            tmpW = width >> 3; // division by 8
            tmpH = height >> 3;
        }
        // check the watermark image size
        if (RWImageType == Img_PGMGray)
        {
            if ( (Gray_Image_RW_Width != tmpW) || (Gray_Image_RW_Height != tmpH))
            {
                LabelDisplayInfoRW->Font->Color = clRed;
                LabelDisplayInfoRW->Caption = "ERROR!!!\nSize of Watermark image doesn't match the input video.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
                canRecover = false;
            }
            else
            {
                LabelDisplayInfoRW->Font->Color = clHotLight;
                LabelDisplayInfoRW->Caption = "OK!!!\nSize of the Watermark matches your settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
                canRecover = true;
            }
        }
        else if ( (RWImageType == Img_BMPGray) || (RWImageType == Img_BMPColor24) )
        {
            if ( (pRWbmpImage->Width != tmpW) || (pRWbmpImage->Height != tmpH))
            {
                LabelDisplayInfoRW->Font->Color = clRed;
                LabelDisplayInfoRW->Caption = "ERROR!!!\nSize of Watermark image doesn't match the input video.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
                canRecover = false;
            }
            else
            {
                LabelDisplayInfoRW->Font->Color = clHotLight;
                LabelDisplayInfoRW->Caption = "OK!!!\nSize of the Watermark matches your settings, which is " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
                canRecover = true;
            }
        }
        else if (RWImageType == Img_NONE)
        {
            LabelDisplayInfoRW->Font->Color = clRed;
            LabelDisplayInfoRW->Caption = "ERROR!!!\nWatermark image does not exist. Load a watermark image.\nSuggested watermark size: " + IntToStr(tmpW) + "x" + IntToStr(tmpH);
            canRecover = false;
        }
    }
    if (canRecover)
        ButtonRWRecover->Enabled = true;
    else
        ButtonRWRecover->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::zCheckBoxCorrelationClick(TObject *Sender)
{
    if (zCheckBoxCorrelation->Checked)
    {
        ButtonRWWatermark->Enabled = true;
        wantRWCalcCorrelation = true;
    }
    else
    {
        // free all RW image related sources
        sRWImageName = "";
        if (pRWbmpImage != NULL)
        {
            delete pRWbmpImage;
            pRWbmpImage = NULL;
        }
        if (Gray_Image_RW != NULL)
        {
            Free_2D_UCHAR(Gray_Image_RW, Gray_Image_RW_Height);
            Gray_Image_RW = NULL;
            Gray_Image_RW_Width = 0;
            Gray_Image_RW_Height = 0;
        }
	    RWImageType = Img_NONE;
        ImageRW->Picture = NULL;
        wantRWCalcCorrelation = false;
        ButtonRWWatermark->Enabled = false;
    }
    if (HasRWInputVideo)
        DisplayRecoverWatInfo();

}
//---------------------------------------------------------------------------



void __fastcall TFormMain::Contents1Click(TObject *Sender)
{
    AnsiString HelpFile = InitDiractory + "\HAYDWT_Help.chm";

    ShellExecuteA(this->Handle, "open", HelpFile.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------