TNB Library
TnbAviMaker.h
[詳解]
1#pragma once
11#include "TnbBitmapHandle.h"
12#include <vfw.h>
13#pragma comment(lib, "Vfw32.lib")
14
15
16
17//TNB Library
18namespace TNB
19{
20
21
22
46{
47 BITMAPINFOHEADER m_bmpinfo;
48 PAVISTREAM m_pSourceStream;
49 PAVISTREAM m_pCompressedStream;
50 PAVIFILE m_pAviFile;
51 COMPVARS m_compvars;
52 UINT m_index;
53public:
54
56 CAviMaker(void) : m_pSourceStream(NULL), m_pCompressedStream(NULL), m_pAviFile(NULL), m_index(0)
57 {
58 m_compvars.cbSize = 0;
59 }
60
62 virtual ~CAviMaker()
63 {
64 Close();
65 }
66
71 void Close(void)
72 {
73 if ( m_pCompressedStream != NULL )
74 {
75 ::AVIStreamRelease(m_pCompressedStream);
76 m_pCompressedStream = NULL;
77 }
78 if ( m_pSourceStream != NULL )
79 {
80 ::AVIStreamRelease(m_pSourceStream);
81 m_pSourceStream = NULL;
82 }
83 if (m_pAviFile!=NULL) {
84 ::AVIFileRelease(m_pAviFile);
85 m_pAviFile=NULL;
86 }
87 if ( m_compvars.cbSize != 0 )
88 {
89 ::ICCompressorFree(&m_compvars);
90 m_compvars.cbSize = 0;
91 }
92 m_index = 0;
93 ::AVIFileExit();
94 }
95
107 bool Open(const SIZE& size, LPCTSTR fileName, DWORD scale, DWORD rate, bool boIsComp = false)
108 {
109 Close();
110 size_t imageBytes = (((size.cx * 24 + 31)& ~31) / 8) * size.cy;
111 BITMAPINFOHEADER bi =
112 {
113 sizeof(BITMAPINFOHEADER), size.cx, size.cy, 1, 24, BI_RGB, ToDword(imageBytes), 0, 0, 0, 0
114 };
115 m_bmpinfo = bi;
116 AVISTREAMINFO si =
117 {
118 streamtypeVIDEO, comptypeDIB, 0, 0, 0, 0,
119 scale, rate, 0, 0, 0, 0, DWORD_MAX, 0, {0, 0, size.cx, size.cy}, 0, 0, _T("VIDEO")
120 };
121 ::AVIFileInit();
122 if ( ::AVIFileOpen(&m_pAviFile, fileName, OF_CREATE | OF_WRITE | OF_SHARE_DENY_NONE, NULL) != 0 )
123 {
124 Close();
125 return false;
126 }
127 if ( boIsComp )
128 {
129 memset(&m_compvars, 0, sizeof(COMPVARS));
130 m_compvars.cbSize = sizeof(COMPVARS);
131 m_compvars.dwFlags = ICMF_COMPVARS_VALID;
132 m_compvars.fccHandler = comptypeDIB;
133 m_compvars.lQ = ICQUALITY_DEFAULT;
134 if ( ! ::ICCompressorChoose(NULL, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME, &bi, NULL, &m_compvars, NULL) )
135 {
136 Close();
137 return false;
138 }
139 si.fccHandler = m_compvars.fccHandler;
140 AVICOMPRESSOPTIONS opt;
141 opt.fccType = streamtypeVIDEO;
142 opt.fccHandler = m_compvars.fccHandler;
143 opt.dwKeyFrameEvery = m_compvars.lKey;
144 opt.dwQuality = m_compvars.lQ;
145 opt.dwBytesPerSecond = m_compvars.lDataRate;
146 opt.dwFlags = (m_compvars.lDataRate > 0 ? AVICOMPRESSF_DATARATE : 0) | (m_compvars.lKey > 0 ? AVICOMPRESSF_KEYFRAMES : 0);
147 opt.lpFormat = NULL;
148 opt.cbFormat = 0;
149 opt.lpParms = m_compvars.lpState;
150 opt.cbParms = m_compvars.cbState;
151 opt.dwInterleaveEvery = 0;
152 if ( ::AVIFileCreateStream(m_pAviFile, &m_pSourceStream, &si) != 0)
153 {
154 Close();
155 return false;
156 }
157 if ( ::AVIMakeCompressedStream(&m_pCompressedStream, m_pSourceStream, &opt, NULL) != AVIERR_OK )
158 {
159 Close();
160 return false;
161 }
162 }
163 else
164 {
165 if ( ::AVIFileCreateStream(m_pAviFile, &m_pCompressedStream, &si) != 0 )
166 {
167 Close();
168 return false;
169 }
170 }
171 if ( ::AVIStreamSetFormat(m_pCompressedStream, 0, &bi, sizeof(BITMAPINFOHEADER)) != 0 )
172 {
173 Close();
174 return false;
175 }
176 return true;
177 }
178
186 {
187 if ( m_pCompressedStream != NULL )
188 {
189 CWorkMem work(m_bmpinfo.biSizeImage);
190 HDC dc = ::GetDC(NULL);
191 int r = ::GetDIBits(dc, bmp, 0, m_bmpinfo.biHeight - 1,
192 work.Ref(), reinterpret_cast<BITMAPINFO*>(&m_bmpinfo), DIB_RGB_COLORS);
193 ::ReleaseDC(NULL, dc);
194 if ( r == m_bmpinfo.biHeight - 1 )
195 {
196 if ( ::AVIStreamWrite(m_pCompressedStream, m_index++, 1, work.Ref(), down_cast<LONG>(work.GetSize()), AVIIF_KEYFRAME, NULL, NULL) == 0 )
197 {
198 return true;
199 }
200 }
201 }
202 return false;
203 }
204};
205
206
207
208}; // TNB
ビットマップハンドル関係のヘッダ
AVIメーカークラス.
Definition: TnbAviMaker.h:46
bool Add(CBitmapHandle bmp)
[追加] フレーム追加.
Definition: TnbAviMaker.h:185
virtual ~CAviMaker()
デストラクタ
Definition: TnbAviMaker.h:62
void Close(void)
[停止] ストップ.
Definition: TnbAviMaker.h:71
CAviMaker(void)
コンストラクタ
Definition: TnbAviMaker.h:56
bool Open(const SIZE &size, LPCTSTR fileName, DWORD scale, DWORD rate, bool boIsComp=false)
[開始] オープン.
Definition: TnbAviMaker.h:107
HBITMAP型ハンドルハンドル
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
TNB Library
Definition: TnbDoxyTitle.txt:2