1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| // Fichier AviFile.cpp: Classe CAviFile.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AviFile.h"
#include "stdafx.h"
#include <vfw.h>
#include <windowsx.h>
#include <memory.h>
#include <mmsystem.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/*********************************************************************
** Prends un bitmap et le transforme en un DIB (Device Independant
** Bitmap)
*********************************************************************/
static HANDLE MakeDib( HBITMAP hbitmap, UINT bits )
{
HANDLE hdib ;
HDC hdc ;
BITMAP bitmap ;
UINT wLineLen ;
DWORD dwSize ;
DWORD wColSize ;
LPBITMAPINFOHEADER lpbi ;
LPBYTE lpBits ;
GetObject(hbitmap,sizeof(BITMAP),&bitmap) ;
// DWORD align the width of the DIB
// Figure out the size of the colour table
// Calculate the size of the DIB
wLineLen = (bitmap.bmWidth*bits+31)/32 * 4;
wColSize = sizeof(RGBQUAD)*((bits <= 8) ? 1<<bits : 0);
dwSize = sizeof(BITMAPINFOHEADER) + wColSize + (DWORD)(UINT)wLineLen*(DWORD)(UINT)bitmap.bmHeight;
// Allocate room for a DIB and set the LPBI fields
hdib = GlobalAlloc(GHND,dwSize);
if (!hdib)
return hdib ;
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib) ;
lpbi->biSize = sizeof(BITMAPINFOHEADER) ;
lpbi->biWidth = bitmap.bmWidth ;
lpbi->biHeight = bitmap.bmHeight ;
lpbi->biPlanes = 1 ;
lpbi->biBitCount = (WORD) bits ;
lpbi->biCompression = BI_RGB ;
lpbi->biSizeImage = dwSize - sizeof(BITMAPINFOHEADER) - wColSize ;
lpbi->biXPelsPerMeter = 0 ;
lpbi->biYPelsPerMeter = 0 ;
lpbi->biClrUsed = (bits <= 8) ? 1<<bits : 0;
lpbi->biClrImportant = 0 ;
// Get the bits from the bitmap and stuff them after the LPBI
lpBits = (LPBYTE)(lpbi+1)+wColSize ;
hdc = CreateCompatibleDC(NULL) ;
GetDIBits(hdc,hbitmap,0,bitmap.bmHeight,lpBits,(LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
// Fix this if GetDIBits messed it up....
lpbi->biClrUsed = (bits <= 8) ? 1<<bits : 0;
DeleteDC(hdc) ;
GlobalUnlock(hdib);
return hdib ;
}
/*********************************************************************
** Construction
** Destruction
*********************************************************************/
CAVIFile::CAVIFile(LPCTSTR lpszFileName, int xdim, int ydim)
{
AVIFileName = lpszFileName;
xDim = xdim;
yDim = ydim;
nFrames = 0;
AVIFramePerSecond = 15; // Valeur par défaut
pfile = NULL;
ps = NULL;
psCompressed = NULL;
psText = NULL;
aopts[0] = &opts;
// Vérification de la version de VFW
WORD wVer = HIWORD(VideoForWindowsVersion());
if (wVer < 0x010A)
{
// Vielle version
bOK = false;
}
else
{
// Initialise les fonctions de AVI
AVIFileInit();
}
}
CAVIFile::~CAVIFile()
{
if (ps)
AVIStreamClose(ps);
if (psCompressed)
AVIStreamClose(psCompressed);
if (psText)
AVIStreamClose(psText);
if (pfile)
AVIFileClose(pfile);
// Vérification de la version de VFW
WORD wVer = HIWORD(VideoForWindowsVersion());
if (wVer >= 0x010A)
{
// Libération des fonctions initialisée
AVIFileExit();
}
}
/*********************************************************************
** Ajout des Frames
** Passage d'un bitmap
*********************************************************************/
bool CAVIFile::AddFrame(CBitmap& bmp)
{
HRESULT hr;
//char szMessage[BUFSIZE];
if (!bOK)
return false;
LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 24));
if (alpbi == NULL)
return false;
if (xDim>=0 && xDim != alpbi->biWidth)
{
GlobalFreePtr(alpbi);
return false;
}
if (yDim>=0 && yDim != alpbi->biHeight)
{
GlobalFreePtr(alpbi);
return false;
}
xDim = alpbi->biWidth;
yDim = alpbi->biHeight;
if (nFrames == 0)
{
hr = AVIFileOpen(&pfile, // returned file pointer
AVIFileName, // file name
OF_WRITE | OF_CREATE, // mode to open file with
NULL); // use handler determined
// from file extension....
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = AVIFramePerSecond; // 15 fps
strhdr.dwSuggestedBufferSize = alpbi->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
(int) alpbi->biWidth,
(int) alpbi->biHeight);
// And create the stream;
hr = AVIFileCreateStream(pfile, // file pointer
&ps, // returned stream pointer
&strhdr); // stream header
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
_fmemset(&opts, 0, sizeof(opts));
if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
hr = AVIStreamSetFormat(psCompressed, 0,
alpbi, // stream format
alpbi->biSize + // format size
alpbi->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
}
hr = AVIStreamWrite(psCompressed, // stream pointer
nFrames,// * 10, // time of this frame
1, // number to write
(LPBYTE) alpbi + // pointer to data
alpbi->biSize +
alpbi->biClrUsed * sizeof(RGBQUAD),
alpbi->biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
NULL,
NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false;
return false;
}
GlobalFreePtr(alpbi);
nFrames++;
return true;
}
/*********************************************************************
** Retour des paramètres
** AVIFileName & IsOK & AVIFramePerSecond
*********************************************************************/
CString CAVIFile::GetAviFileName()
{
return AVIFileName;
};
bool CAVIFile::IsOK()
{
return bOK;
};
void CAVIFile::SetAviFramePerSecond(int fps)
{
AVIFramePerSecond = fps;
}
int CAVIFile::GetAviFramePerSecond()
{
return AVIFramePerSecond;
} |
Partager