www.gusucode.com > 基于dwt的视频数字水印源代码 > 基于dwt的视频数字水印源代码\code\HaYDWT\VideoConvert.cpp
//Download by http://www.NewXing.com //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "VideoConvert.h" #include "Main.h" #include "Common.h" #include "FileServices.h" #include "MemUtil.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "RzButton" #pragma link "RzEdit" #pragma link "RzPrgres" #pragma link "RzLabel" #pragma link "RzPanel" #pragma link "RzRadChk" #pragma resource "*.dfm" TFormVideoConvert *FormVideoConvert; //--------------------------------------------------------------------------- __fastcall TFormVideoConvert::TFormVideoConvert(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::ButtonInputFileClick(TObject *Sender) { OpenDialog->Filter = "Supported Video Files|*.cif;*.qcif;*.cif_g;*.qcif_g|CIF Format 352x288|*.cif|QCIF Format 176x144|*.qcif|GrayCIF Format 352x288|*.cif_g|GrayQCIF Format 176x144|*.qcif_g|All Files|*.*"; // OpenDialog->InitialDir = FormMain->InitDiractory; OpenDialog->FileName = ""; OpenDialog->FilterIndex = 0; OpenDialog->Options << ofPathMustExist << ofFileMustExist; if (OpenDialog->Execute()) { sInputFile = OpenDialog->FileName; EditInputFile->Text = sInputFile; if (UpperCase(ExtractFileExt(sInputFile)) == ".CIF") { int tframes = CIF_Get_Frame_Info(sInputFile.c_str()); LabelNumFrameNs->Caption = "Total frames: " + IntToStr(tframes); EditFrameTo->Value = tframes; } else if (UpperCase(ExtractFileExt(sInputFile)) == ".QCIF") { int tframes = QCIF_Get_Frame_Info(sInputFile.c_str()); LabelNumFrameNs->Caption = "Total frames: " + IntToStr(tframes); EditFrameTo->Value = tframes; } else if (UpperCase(ExtractFileExt(sInputFile)) == ".CIF_G") { int tframes = GrayCIF_Get_Frame_Info(sInputFile.c_str()); LabelNumFrameNs->Caption = "Total frames: " + IntToStr(tframes); EditFrameTo->Value = tframes; } else if (UpperCase(ExtractFileExt(sInputFile)) == ".QCIF_G") { int tframes = GrayQCIF_Get_Frame_Info(sInputFile.c_str()); LabelNumFrameNs->Caption = "Total frames: " + IntToStr(tframes); EditFrameTo->Value = tframes; } } } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::ButtonOutputFileClick(TObject *Sender) { SaveDialog->Filter = FORMAT_9; // SaveDialog->InitialDir = FormMain->InitDiractory; SaveDialog->FileName = ""; SaveDialog->FilterIndex = 0; SaveDialog->DefaultExt = ".avi"; SaveDialog->Options << ofOverwritePrompt << ofPathMustExist; if (SaveDialog->Execute()) { sOutputFile = SaveDialog->FileName; EditOutputFile->Text = sOutputFile; } } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::FormCreate(TObject *Sender) { sInputFile = ""; sOutputFile = ""; CancelConvert = false; ButtonCancel->Enabled = false; ConvertAllFrames = false; } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::ButtonConvertClick(TObject *Sender) { LabelConvert->Caption = "0/0"; BarRecover->Percent = 0; // currently only supports CIF to AVI conversion if ( (UpperCase(ExtractFileExt(sInputFile)) == ".CIF")&& (UpperCase(ExtractFileExt(sOutputFile)) == ".AVI") ) { ConvertCIFtoAVI(sInputFile, sOutputFile); } else if ( (UpperCase(ExtractFileExt(sInputFile)) == ".QCIF")&& (UpperCase(ExtractFileExt(sOutputFile)) == ".AVI") ) { ConvertQCIFtoAVI(sInputFile, sOutputFile); } else if ( (UpperCase(ExtractFileExt(sInputFile)) == ".CIF_G")&& (UpperCase(ExtractFileExt(sOutputFile)) == ".AVI") ) { ConvertCIF_GtoAVI(sInputFile, sOutputFile); } else { MessageDlg("Currenty only supports CIF to AVI conversion.", mtInformation, TMsgDlgButtons() << mbOK, 0); } } //--------------------------------------------------------------------------- int TFormVideoConvert::ConvertCIFtoAVI(AnsiString asIFile, AnsiString asOFile) { UCHAR **Y, **U, **V; UCHAR **Red, **Green, **Blue; long FrameStart, NumFrames, i, xL, yL, width, height; long tY, tU, tV; long tR, tG, tB; double dtR, dtG, dtB; HAVI Avi = NULL; LabelConvert->Caption = "0/0"; BarRecover->Percent = 0; NumFrames = CIF_Get_Frame_Info(asIFile.c_str()); width = 352; height =288; /***********************************/ /* Create AVI file */ if (Avi != NULL) { CloseAvi(Avi); } // erase file if (FileExists(asOFile)) DeleteFileA(asOFile); Avi = CreateAvi(asOFile.c_str(), 40, 1000, NULL); if (MessageBox(this->Handle, "Do you want to compress output AVI?","Question", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES) { HBITMAP hbm; HDC hdcscreen=GetDC(0), hdc=CreateCompatibleDC(hdcscreen); ReleaseDC(0,hdcscreen); BITMAPINFO bmi; ZeroMemory(&bmi,sizeof(bmi)); BITMAPINFOHEADER &bih = bmi.bmiHeader; bih.biSize = sizeof(bih); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = ((bih.biWidth*bih.biBitCount/8+3)&0xFFFFFFFC)*bih.biHeight; bih.biXPelsPerMeter = 1; bih.biYPelsPerMeter = 1; bih.biClrUsed = 0; bih.biClrImportant = 0; void *bits; hbm = CreateDIBSection(hdc,(BITMAPINFO*)&bih,DIB_RGB_COLORS,&bits,NULL,NULL); AVICOMPRESSOPTIONS opts; ZeroMemory(&opts,sizeof(opts)); opts.fccHandler = mmioFOURCC('D','I','B',' '); SetAviVideoCompression(Avi, hbm, &opts, true, this->Handle); DeleteDC(hdc); DeleteObject(hbm); // free(&bmi); } /***********************************/ Y = Alloc_2D_UCHAR(width, height); U = Alloc_2D_UCHAR(width, height); V = Alloc_2D_UCHAR(width, height); Red = Alloc_2D_UCHAR(width, height); Green = Alloc_2D_UCHAR(width, height); Blue = Alloc_2D_UCHAR(width, height); LabelBlink->Visible = true; ButtonCancel->Enabled = true; ButtonConvert->Enabled = false; // Check for frame selection if (ConvertAllFrames) FrameStart = 0; else { if (EditFrameFrom->Value >= 0) FrameStart = EditFrameFrom->Value; else FrameStart = 0; if (EditFrameTo->Value <= NumFrames) NumFrames = EditFrameTo->Value; } for(i=FrameStart;i<NumFrames;i++) { Application->ProcessMessages(); // Read Data CIF_Read_Data(asIFile.c_str(), i, Y, U, V); // Convert YUV format to GRB format for(yL=0;yL<height;yL++) { for(xL=0;xL<width;xL++) { // assign temporary variables tY = Y[yL][xL]; tU = U[yL][xL]; tV = V[yL][xL]; // YUV-->RGB conversion tB = 1.164*(tY - 16) + 2.018*(tU - 128); tG = 1.164*(tY - 16) - 0.813*(tV - 128) - 0.391*(tU - 128); tR = 1.164*(tY - 16) + 1.596*(tV - 128); // check the range if (tR > 255) tR = 255; if (tR < 0) tR = 0; if (tG > 255) tG = 255; if (tG < 0) tG = 0; if (tB > 255) tB = 255; if (tB < 0) tB = 0; // put the colors in the matrices Red[yL][xL] = tR; Green[yL][xL] = tG; Blue[yL][xL] = tB; } } Application->ProcessMessages(); /* Write current Frame */ AddAviFrameMatrix(Avi, width, height, Red, Green, Blue); LabelConvert->Caption = IntToStr(i+1) + "/" + IntToStr(NumFrames) + " frames"; BarRecover->Percent = ((i-FrameStart+1)*100)/(NumFrames-FrameStart); if (CancelConvert == true) { LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); Free_2D_UCHAR(U, height); Free_2D_UCHAR(V, height); Free_2D_UCHAR(Red, height); Free_2D_UCHAR(Green, height); Free_2D_UCHAR(Blue, height); return S_OK; } } LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); Free_2D_UCHAR(U, height); Free_2D_UCHAR(V, height); Free_2D_UCHAR(Red, height); Free_2D_UCHAR(Green, height); Free_2D_UCHAR(Blue, height); return S_OK; } int TFormVideoConvert::ConvertQCIFtoAVI(AnsiString asIFile, AnsiString asOFile) { UCHAR **Y, **U, **V; UCHAR **Red, **Green, **Blue; long FrameStart, NumFrames, i, xL, yL, width, height; long tY, tU, tV; long tR, tG, tB; double dtR, dtG, dtB; HAVI Avi = NULL; LabelConvert->Caption = "0/0"; BarRecover->Percent = 0; NumFrames = QCIF_Get_Frame_Info(asIFile.c_str()); width = 176; height =144; /***********************************/ /* Create AVI file */ if (Avi != NULL) { CloseAvi(Avi); } // erase file if (FileExists(asOFile)) DeleteFileA(asOFile); Avi = CreateAvi(asOFile.c_str(), 40, 1000, NULL); if (MessageBox(this->Handle, "Do you want to compress output AVI?","Question", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES) { HBITMAP hbm; HDC hdcscreen=GetDC(0), hdc=CreateCompatibleDC(hdcscreen); ReleaseDC(0,hdcscreen); BITMAPINFO bmi; ZeroMemory(&bmi,sizeof(bmi)); BITMAPINFOHEADER &bih = bmi.bmiHeader; bih.biSize = sizeof(bih); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = ((bih.biWidth*bih.biBitCount/8+3)&0xFFFFFFFC)*bih.biHeight; bih.biXPelsPerMeter = 1; bih.biYPelsPerMeter = 1; bih.biClrUsed = 0; bih.biClrImportant = 0; void *bits; hbm = CreateDIBSection(hdc,(BITMAPINFO*)&bih,DIB_RGB_COLORS,&bits,NULL,NULL); AVICOMPRESSOPTIONS opts; ZeroMemory(&opts,sizeof(opts)); opts.fccHandler = mmioFOURCC('D','I','B',' '); SetAviVideoCompression(Avi, hbm, &opts, true, this->Handle); DeleteDC(hdc); DeleteObject(hbm); // free(&bmi); } /***********************************/ Y = Alloc_2D_UCHAR(width, height); U = Alloc_2D_UCHAR(width, height); V = Alloc_2D_UCHAR(width, height); Red = Alloc_2D_UCHAR(width, height); Green = Alloc_2D_UCHAR(width, height); Blue = Alloc_2D_UCHAR(width, height); LabelBlink->Visible = true; ButtonCancel->Enabled = true; ButtonConvert->Enabled = false; // Check for frame selection if (ConvertAllFrames) FrameStart = 0; else { if (EditFrameFrom->Value >= 0) FrameStart = EditFrameFrom->Value; else FrameStart = 0; if (EditFrameTo->Value <= NumFrames) NumFrames = EditFrameTo->Value; } for(i=FrameStart;i<NumFrames;i++) { Application->ProcessMessages(); // Read Data QCIF_Read_Data(asIFile.c_str(), i, Y, U, V); // Convert YUV format to GRB format for(yL=0;yL<height;yL++) { for(xL=0;xL<width;xL++) { // assign temporary variables tY = Y[yL][xL]; tU = U[yL][xL]; tV = V[yL][xL]; // YUV-->RGB conversion tB = 1.164*(tY - 16) + 2.018*(tU - 128); tG = 1.164*(tY - 16) - 0.813*(tV - 128) - 0.391*(tU - 128); tR = 1.164*(tY - 16) + 1.596*(tV - 128); // check the range if (tR > 255) tR = 255; if (tR < 0) tR = 0; if (tG > 255) tG = 255; if (tG < 0) tG = 0; if (tB > 255) tB = 255; if (tB < 0) tB = 0; // put the colors in the matrices Red[yL][xL] = tR; Green[yL][xL] = tG; Blue[yL][xL] = tB; } } Application->ProcessMessages(); /* Write current Frame */ AddAviFrameMatrix(Avi, width, height, Red, Green, Blue); LabelConvert->Caption = IntToStr(i+1) + "/" + IntToStr(NumFrames) + " frames"; BarRecover->Percent = ((i-FrameStart+1)*100)/(NumFrames-FrameStart); if (CancelConvert == true) { LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); Free_2D_UCHAR(U, height); Free_2D_UCHAR(V, height); Free_2D_UCHAR(Red, height); Free_2D_UCHAR(Green, height); Free_2D_UCHAR(Blue, height); return S_OK; } } LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); Free_2D_UCHAR(U, height); Free_2D_UCHAR(V, height); Free_2D_UCHAR(Red, height); Free_2D_UCHAR(Green, height); Free_2D_UCHAR(Blue, height); return S_OK; } int TFormVideoConvert::ConvertCIF_GtoAVI(AnsiString asIFile, AnsiString asOFile) { UCHAR **Y; long FrameStart, NumFrames, i, xL, yL, width, height; HAVI Avi = NULL; LabelConvert->Caption = "0/0"; BarRecover->Percent = 0; NumFrames = GrayCIF_Get_Frame_Info(asIFile.c_str()); width = 352; height =288; /***********************************/ /* Create AVI file */ if (Avi != NULL) { CloseAvi(Avi); } // erase file if (FileExists(asOFile)) DeleteFileA(asOFile); Avi = CreateAvi(asOFile.c_str(), 40, 1000, NULL); if (MessageBox(this->Handle, "Do you want to compress output AVI?","Question", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES) { HBITMAP hbm; HDC hdcscreen=GetDC(0), hdc=CreateCompatibleDC(hdcscreen); ReleaseDC(0,hdcscreen); BITMAPINFO bmi; ZeroMemory(&bmi,sizeof(bmi)); BITMAPINFOHEADER &bih = bmi.bmiHeader; bih.biSize = sizeof(bih); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = ((bih.biWidth*bih.biBitCount/8+3)&0xFFFFFFFC)*bih.biHeight; bih.biXPelsPerMeter = 1; bih.biYPelsPerMeter = 1; bih.biClrUsed = 0; bih.biClrImportant = 0; void *bits; hbm = CreateDIBSection(hdc,(BITMAPINFO*)&bih,DIB_RGB_COLORS,&bits,NULL,NULL); AVICOMPRESSOPTIONS opts; ZeroMemory(&opts,sizeof(opts)); opts.fccHandler = mmioFOURCC('D','I','B',' '); SetAviVideoCompression(Avi, hbm, &opts, true, this->Handle); DeleteDC(hdc); DeleteObject(hbm); // free(&bmi); } /***********************************/ Y = Alloc_2D_UCHAR(width, height); LabelBlink->Visible = true; ButtonCancel->Enabled = true; ButtonConvert->Enabled = false; // Check for frame selection if (ConvertAllFrames) FrameStart = 0; else { if (EditFrameFrom->Value >= 0) FrameStart = EditFrameFrom->Value; else FrameStart = 0; if (EditFrameTo->Value <= NumFrames) NumFrames = EditFrameTo->Value; } for(i=FrameStart;i<NumFrames;i++) { Application->ProcessMessages(); // Read Data GrayCIF_Read_Data(asIFile.c_str(), i, Y); Application->ProcessMessages(); /* Write current Frame */ AddAviFrameMatrix(Avi, width, height, Y, Y, Y); LabelConvert->Caption = IntToStr(i+1) + "/" + IntToStr(NumFrames) + " frames"; BarRecover->Percent = ((i-FrameStart+1)*100)/(NumFrames-FrameStart); if (CancelConvert == true) { LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); return S_OK; } } LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); return S_OK; } int TFormVideoConvert::ConvertQCIF_GtoAVI(AnsiString asIFile, AnsiString asOFile) { UCHAR **Y; long FrameStart, NumFrames, i, xL, yL, width, height; HAVI Avi = NULL; LabelConvert->Caption = "0/0"; BarRecover->Percent = 0; NumFrames = GrayQCIF_Get_Frame_Info(asIFile.c_str()); width = 176; height = 144; /***********************************/ /* Create AVI file */ if (Avi != NULL) { CloseAvi(Avi); } // erase file if (FileExists(asOFile)) DeleteFileA(asOFile); Avi = CreateAvi(asOFile.c_str(), 40, 1000, NULL); if (MessageBox(this->Handle, "Do you want to compress output AVI?","Question", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES) { HBITMAP hbm; HDC hdcscreen=GetDC(0), hdc=CreateCompatibleDC(hdcscreen); ReleaseDC(0,hdcscreen); BITMAPINFO bmi; ZeroMemory(&bmi,sizeof(bmi)); BITMAPINFOHEADER &bih = bmi.bmiHeader; bih.biSize = sizeof(bih); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = ((bih.biWidth*bih.biBitCount/8+3)&0xFFFFFFFC)*bih.biHeight; bih.biXPelsPerMeter = 1; bih.biYPelsPerMeter = 1; bih.biClrUsed = 0; bih.biClrImportant = 0; void *bits; hbm = CreateDIBSection(hdc,(BITMAPINFO*)&bih,DIB_RGB_COLORS,&bits,NULL,NULL); AVICOMPRESSOPTIONS opts; ZeroMemory(&opts,sizeof(opts)); opts.fccHandler = mmioFOURCC('D','I','B',' '); SetAviVideoCompression(Avi, hbm, &opts, true, this->Handle); DeleteDC(hdc); DeleteObject(hbm); // free(&bmi); } /***********************************/ Y = Alloc_2D_UCHAR(width, height); LabelBlink->Visible = true; ButtonCancel->Enabled = true; ButtonConvert->Enabled = false; // Check for frame selection if (ConvertAllFrames) FrameStart = 0; else { if (EditFrameFrom->Value >= 0) FrameStart = EditFrameFrom->Value; else FrameStart = 0; if (EditFrameTo->Value <= NumFrames) NumFrames = EditFrameTo->Value; } for(i=FrameStart;i<NumFrames;i++) { Application->ProcessMessages(); // Read Data GrayQCIF_Read_Data(asIFile.c_str(), i, Y); Application->ProcessMessages(); /* Write current Frame */ AddAviFrameMatrix(Avi, width, height, Y, Y, Y); LabelConvert->Caption = IntToStr(i+1) + "/" + IntToStr(NumFrames) + " frames"; BarRecover->Percent = ((i-FrameStart+1)*100)/(NumFrames-FrameStart); if (CancelConvert == true) { LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); return S_OK; } } LabelBlink->Visible = false; ButtonCancel->Enabled = false; ButtonConvert->Enabled = true; CancelConvert = false; CloseAvi(Avi); Free_2D_UCHAR(Y, height); return S_OK; } void __fastcall TFormVideoConvert::ButtonCancelClick(TObject *Sender) { CancelConvert = true; } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::RadioAllFramesClick(TObject *Sender) { if (RadioAllFrames->Checked) ConvertAllFrames = true; } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::RadioFramesFromClick(TObject *Sender) { if (RadioFramesFrom->Checked) ConvertAllFrames = false; } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::EditFrameFromClick(TObject *Sender) { RadioFramesFrom->Checked = true; } //--------------------------------------------------------------------------- void __fastcall TFormVideoConvert::EditFrameToClick(TObject *Sender) { RadioFramesFrom->Checked = true; } //---------------------------------------------------------------------------