Advertisement
Guest User

raw reader

a guest
Sep 3rd, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. // rawAviReader.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <Windows.h>
  6. #include <Vfw.h>
  7. #include <assert.h>
  8. #include <opencv2/core/core.hpp>
  9. //#include <opencv2/imgproc/imgproc.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     long aviOpRes = 0;
  15.     if (argc < 2)
  16.     {
  17.         fprintf( stderr, "Filename is needed as an argument\n" );  
  18.         return 1;  
  19.     }
  20.     // Error handling is intentionally leaved out for clarity
  21.     PAVISTREAM ppavi;
  22.     AVIFileInit();
  23.     aviOpRes = AVIStreamOpenFromFile(&ppavi, argv[1], streamtypeVIDEO, 0, OF_READ, NULL);
  24.     switch(aviOpRes)
  25.     {
  26.     case 0: // OK
  27.         break;
  28.     case AVIERR_NODATA:
  29.     case AVIERR_BADFORMAT: // The file couldn't be read
  30.     case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
  31.     case AVIERR_FILEREAD: // A disk error occurred while reading the file.
  32.     case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
  33.     case REGDB_E_CLASSNOTREG:
  34.     default:
  35.         abort();
  36.     }
  37.     AVISTREAMINFO si;
  38.     aviOpRes = AVIStreamInfo( ppavi, &si, sizeof(AVISTREAMINFO) );
  39.     if (aviOpRes !=0 )
  40.     {
  41.         abort();
  42.     }
  43.     long format_length = 0;
  44.     if(AVIStreamReadFormat(ppavi, si.dwStart, NULL, &format_length))
  45.     { // just to get format_length
  46.         abort();
  47.     }
  48.     PBYTE cvhunk = new BYTE[format_length];
  49.     aviOpRes = AVIStreamReadFormat(ppavi, 0, cvhunk, &format_length);
  50.     switch(aviOpRes)
  51.     {
  52.     case 0: // OK
  53.         break;
  54.     case AVIERR_NODATA:
  55.     case AVIERR_BADFORMAT: // The file couldn't be read
  56.     case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
  57.     case AVIERR_FILEREAD: // A disk error occurred while reading the file.
  58.     case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
  59.     case REGDB_E_CLASSNOTREG:
  60.     default:
  61.         abort();
  62.     }
  63.     LPBITMAPINFOHEADER bi = reinterpret_cast<LPBITMAPINFOHEADER>(cvhunk);
  64.     assert(bi->biSizeImage == bi->biWidth * bi->biHeight * bi->biBitCount/8,"Inconsistend image size");
  65.     assert(bi->biBitCount == 8,"Image not raw");
  66.     int delay = si.dwRate/si.dwScale;
  67.     PUCHAR buffer = new UCHAR[si.dwSuggestedBufferSize];
  68.     memset(buffer,0,si.dwSuggestedBufferSize);
  69.     LONG smpS = 0,smpN = 0,bRead = 0; //number of first sample, how many to read
  70.     smpS = AVIStreamFindSample(ppavi,0,FIND_FROM_START|FIND_ANY);
  71.     cv::Mat frame;
  72.     cv::namedWindow("Extracted Frame");
  73.     do
  74.     {
  75.         bRead = 0;
  76.         aviOpRes = AVIStreamRead(ppavi,smpS,1,buffer,si.dwSuggestedBufferSize,&bRead,&smpN);
  77.         smpS += smpN;
  78.         smpN = 0;
  79.         switch(aviOpRes)
  80.         {
  81.         case 0: // OK
  82.             break;
  83.         case AVIERR_NODATA:
  84.         case AVIERR_BADFORMAT: // The file couldn't be read
  85.         case AVIERR_MEMORY: // The file could not be opened because of insufficient memory.
  86.         case AVIERR_FILEREAD: // A disk error occurred while reading the file.
  87.         case AVIERR_FILEOPEN: // A disk error occurred while opening the file.
  88.         case REGDB_E_CLASSNOTREG:
  89.         default:
  90.             abort();
  91.         }
  92.         frame = cv::Mat(bi->biHeight,bi->biWidth,CV_8U,buffer);
  93.         cv::imshow("Extracted Frame",frame);
  94.         if (cv::waitKey(delay) >= 0)
  95.             break;
  96.     } while (smpS < si.dwLength + si.dwStart);
  97.     delete []cvhunk;
  98.     delete []buffer;
  99.     AVIStreamRelease(ppavi);
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement