Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkMovieGeneratorWin32.h"
00019 #include <GL/gl.h>
00020
00021
00022 mitk::MovieGeneratorWin32::MovieGeneratorWin32()
00023 {
00024 }
00025
00026
00027 void mitk::MovieGeneratorWin32::SetFileName( const char *fileName )
00028 {
00029 m_sFile = _T( fileName );
00030 if( _tcsstr( (char*)m_sFile, _T("avi") ) == NULL ) m_sFile += _T( ".avi" );
00031 }
00032
00033
00034 void mitk::MovieGeneratorWin32::InitBitmapHeader()
00035 {
00036 m_width = m_renderer->GetRenderWindow()->GetSize()[0];
00037 m_height = m_renderer->GetRenderWindow()->GetSize()[1];
00038
00039 m_width -= 10;
00040 m_height -= 10;
00041
00042 m_width -= m_width % 4;
00043 m_height -= m_height % 4;
00044
00045 BITMAPINFOHEADER bih;
00046 bih.biSize = sizeof( BITMAPINFOHEADER );
00047 bih.biWidth = m_width;
00048 bih.biHeight = m_height;
00049 bih.biPlanes = 1;
00050 int imgSize = 3 * bih.biWidth* bih.biHeight;
00051 bih.biBitCount = 24;
00052 bih.biCompression = BI_RGB;
00053 bih.biSizeImage = imgSize;
00054 bih.biClrUsed = 0;
00055 bih.biClrImportant = 0;
00056
00057
00058
00059
00060
00061 memcpy( &m_bih, &bih, sizeof( BITMAPINFOHEADER ) );
00062 }
00063
00064
00065 bool mitk::MovieGeneratorWin32::InitGenerator()
00066 {
00067 InitBitmapHeader();
00068
00069 AVISTREAMINFO strHdr;
00070 AVICOMPRESSOPTIONS opts;
00071 AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
00072
00073 TCHAR szBuffer[1024];
00074 HRESULT hr;
00075
00076 m_sError=_T("Ok");
00077
00078
00079 DWORD wVer = HIWORD(VideoForWindowsVersion());
00080 if (wVer < 0x010a) {
00081
00082 m_sError=_T("Version of Video for Windows too old. Come on, join the 21th century!");
00083 return false;
00084 }
00085
00086
00087 AVIFileInit();
00088
00089
00090 hr = AVIFileOpen(&m_pAVIFile,
00091 (LPCTSTR)m_sFile,
00092 OF_WRITE | OF_CREATE,
00093 NULL);
00094
00095
00096 if (hr != AVIERR_OK) {
00097 _tprintf(szBuffer,_T("AVI Engine failed to initialize. Check filename %s."),m_sFile);
00098 m_sError=szBuffer;
00099
00100 switch(hr) {
00101 case AVIERR_BADFORMAT:
00102 m_sError+=_T("The file couldn't be read, indicating a corrupt file or an unrecognized format.");
00103 break;
00104 case AVIERR_MEMORY:
00105 m_sError+=_T("The file could not be opened because of insufficient memory.");
00106 break;
00107 case AVIERR_FILEREAD:
00108 m_sError+=_T("A disk error occurred while reading the file.");
00109 break;
00110 case AVIERR_FILEOPEN:
00111 m_sError+=_T("A disk error occurred while opening the file.");
00112 break;
00113 case REGDB_E_CLASSNOTREG:
00114 m_sError+=_T("According to the registry, the type of file specified in AVIFileOpen does not have a handler to process it");
00115 break;
00116 }
00117
00118 return false;
00119 }
00120
00121
00122 memset(&strHdr, 0, sizeof(strHdr));
00123 strHdr.fccType = streamtypeVIDEO;
00124 strHdr.fccHandler = 0;
00125 strHdr.dwScale = 1;
00126 strHdr.dwRate = static_cast<DWORD>(m_FrameRate);
00127 strHdr.dwSuggestedBufferSize = m_bih.biSizeImage;
00128 SetRect(&strHdr.rcFrame, 0, 0,
00129 (int) m_bih.biWidth,
00130 (int) m_bih.biHeight);
00131
00132
00133 hr = AVIFileCreateStream(m_pAVIFile,
00134 &m_pStream,
00135 &strHdr);
00136
00137
00138 if (hr != AVIERR_OK) {
00139 m_sError=_T("AVI Stream creation failed. Check Bitmap info.");
00140 if (hr==AVIERR_READONLY) {
00141 m_sError+=_T(" Read only file.");
00142 }
00143 return false;
00144 }
00145
00146
00147 memset(&opts, 0, sizeof(opts));
00148
00149
00150 opts.fccType = streamtypeVIDEO;
00151
00152 opts.fccHandler = mmioFOURCC('M','S','V','C');
00153 opts.dwQuality = 90000;
00154
00155
00156
00157
00158
00159
00160 #if ! (_MSC_VER >= 1400)
00161
00162 if (!AVISaveOptions(NULL, 0, 1, &m_pStream, (LPAVICOMPRESSOPTIONS FAR *) &aopts)) {
00163 AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts);
00164
00165 }
00166
00167 #endif
00168
00169
00170 hr = AVIMakeCompressedStream(&m_pStreamCompressed,
00171 m_pStream,
00172 &opts,
00173 NULL);
00174
00175 if (hr != AVIERR_OK)
00176 {
00177 m_sError=_T("AVI Compressed Stream creation failed.");
00178
00179 switch(hr)
00180 {
00181 case AVIERR_NOCOMPRESSOR:
00182 m_sError+=_T(" A suitable compressor cannot be found.");
00183 break;
00184 case AVIERR_MEMORY:
00185 m_sError+=_T(" There is not enough memory to complete the operation.");
00186 break;
00187 case AVIERR_UNSUPPORTED:
00188 m_sError+=_T("Compression is not supported for this type of data. This error might be returned if you try to compress data that is not audio or video.");
00189 break;
00190 }
00191
00192 return false;
00193 }
00194
00195
00196 hr=AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts);
00197 if (hr!=AVIERR_OK) {
00198 m_sError=_T("Error releasing memory");
00199 return false;
00200 }
00201
00202
00203 hr = AVIStreamSetFormat(m_pStreamCompressed,
00204 0,
00205 &m_bih,
00206 m_bih.biSize +
00207 m_bih.biClrUsed * sizeof(RGBQUAD));
00208
00209 if (hr != AVIERR_OK) {
00210 m_sError=_T("AVI Compressed Stream format setting failed.");
00211 return false;
00212 }
00213
00214
00215 m_lFrame=0;
00216
00217 return true;
00218 }
00219
00220
00221 bool mitk::MovieGeneratorWin32::AddFrame( void *data )
00222 {
00223 HRESULT hr = AVIStreamWrite(m_pStreamCompressed,
00224 m_lFrame,
00225 1,
00226 (BYTE*)data,
00227 m_bih.biSizeImage,
00228 AVIIF_KEYFRAME,
00229 NULL,
00230 NULL );
00231
00232 m_lFrame++;
00233
00234 if (hr==AVIERR_OK) return true;
00235 else return false;
00236 }
00237
00238
00239 bool mitk::MovieGeneratorWin32::TerminateGenerator()
00240 {
00241 if (m_pStream) {
00242 AVIStreamRelease(m_pStream);
00243 m_pStream=NULL;
00244 }
00245 if (m_pStreamCompressed) {
00246 AVIStreamRelease(m_pStreamCompressed);
00247 m_pStreamCompressed=NULL;
00248 }
00249 if (m_pAVIFile) {
00250 AVIFileRelease(m_pAVIFile);
00251 m_pAVIFile=NULL;
00252 }
00253
00254 AVIFileExit();
00255 return true;
00256 }
00257