Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rawAviReader.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <tchar.h>
- #include <Windows.h>
- #include <Vfw.h>
- #include <assert.h>
- #include <opencv2/core/core.hpp>
- //#include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/highgui/highgui.hpp>
- int _tmain(int argc, _TCHAR* argv[])
- {
- long aviOpRes = 0;
- if (argc < 2)
- {
- fprintf( stderr, "Filename is needed as an argument\n" );
- return 1;
- }
- // Error handling is intentionally leaved out for clarity
- PAVISTREAM ppavi;
- AVIFileInit();
- aviOpRes = AVIStreamOpenFromFile(&ppavi, argv[1], streamtypeVIDEO, 0, OF_READ, NULL);
- switch(aviOpRes)
- {
- case 0: // OK
- break;
- case AVIERR_NODATA:
- case AVIERR_BADFORMAT: // The file couldn't be read
- case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
- case AVIERR_FILEREAD: // A disk error occurred while reading the file.
- case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
- case REGDB_E_CLASSNOTREG:
- default:
- abort();
- }
- AVISTREAMINFO si;
- aviOpRes = AVIStreamInfo( ppavi, &si, sizeof(AVISTREAMINFO) );
- if (aviOpRes !=0 )
- {
- abort();
- }
- long format_length = 0;
- if(AVIStreamReadFormat(ppavi, si.dwStart, NULL, &format_length))
- { // just to get format_length
- abort();
- }
- PBYTE cvhunk = new BYTE[format_length];
- aviOpRes = AVIStreamReadFormat(ppavi, 0, cvhunk, &format_length);
- switch(aviOpRes)
- {
- case 0: // OK
- break;
- case AVIERR_NODATA:
- case AVIERR_BADFORMAT: // The file couldn't be read
- case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
- case AVIERR_FILEREAD: // A disk error occurred while reading the file.
- case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
- case REGDB_E_CLASSNOTREG:
- default:
- abort();
- }
- LPBITMAPINFOHEADER bi = reinterpret_cast<LPBITMAPINFOHEADER>(cvhunk);
- assert(bi->biSizeImage == bi->biWidth * bi->biHeight * bi->biBitCount/8,"Inconsistend image size");
- assert(bi->biBitCount == 8,"Image not raw");
- int delay = si.dwRate/si.dwScale;
- PUCHAR buffer = new UCHAR[si.dwSuggestedBufferSize];
- memset(buffer,0,si.dwSuggestedBufferSize);
- LONG smpS = 0,smpN = 0,bRead = 0; //number of first sample, how many to read
- smpS = AVIStreamFindSample(ppavi,0,FIND_FROM_START|FIND_ANY);
- cv::Mat frame;
- cv::namedWindow("Extracted Frame");
- do
- {
- bRead = 0;
- aviOpRes = AVIStreamRead(ppavi,smpS,1,buffer,si.dwSuggestedBufferSize,&bRead,&smpN);
- smpS += smpN;
- smpN = 0;
- switch(aviOpRes)
- {
- case 0: // OK
- break;
- case AVIERR_NODATA:
- case AVIERR_BADFORMAT: // The file couldn't be read
- case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
- case AVIERR_FILEREAD: // A disk error occurred while reading the file.
- case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
- case REGDB_E_CLASSNOTREG:
- default:
- abort();
- }
- frame = cv::Mat(bi->biHeight,bi->biWidth,CV_8U,buffer);
- cv::imshow("Extracted Frame",frame);
- if (cv::waitKey(delay) >= 0)
- break;
- } while (smpS < si.dwLength + si.dwStart);
- delete []cvhunk;
- delete []buffer;
- AVIStreamRelease(ppavi);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement