home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
TCH2AVI.LZH
/
TCH2AVI.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-16
|
9KB
|
374 lines
#include <owl\owlpch.h>
#pragma hdrstop
#include <stdio.h>
#include <sys\stat.h>
#include <vfw.h>
#include "tch2vapp.h"
#include "tshwwndw.h"
#include "tch.h"
extern TApplication *app;
extern int InTrans;
static int frames;
static TShowWindow *client;
static char *tchfile;
static char *avifile;
static AVICOMPRESSOPTIONS *opts;
static HRESULT hr;
static PAVIFILE pfile = NULL;
static AVISTREAMINFO strhdr;
static BITMAPINFOHEADER *lpbi = NULL;
static PAVISTREAM psAll = NULL, psVideo = NULL;
extern TDib *dib;
extern char mess[256];
static int rate;
static int waitflag;
static int removeflag;
static int errorstatus;
static char errorfuncname[32];
static int lastwait = 1;
int sleeptime = 0;
static TDib *OpenDIB(char *fname)
{
TClientDC dc(*client);
char str[120];
if (waitflag) {
struct stat statbuf;
if (stat(fname, &statbuf) != 0 || statbuf.st_size == 0) {
char *p;
if ((p = strrchr(fname, '\\')) == NULL) {
p = fname;
} else {
p++;
}
wsprintf(mess, "待機: %s ", p);
dc.TextOut(0,0,mess);
dib = NULL;
#if 0
while (stat(fname, &statbuf) != 0 || statbuf.st_size == 0) {
#else
OFSTRUCT openbuf;
while (stat(fname, &statbuf) != 0 || statbuf.st_size == 0
|| OpenFile(fname, &openbuf, OF_EXIST|OF_SHARE_EXCLUSIVE) == HFILE_ERROR) {
#endif
if (sleeptime > 0) {
DWORD last,now;
last = GetTickCount();
do {
if (InTrans == FALSE) {
return NULL;
}
app->PumpWaitingMessages();
now = GetTickCount();
} while (now < last + sleeptime * 1000 && now > last);
}
if (InTrans == FALSE) {
return NULL;
}
app->PumpWaitingMessages();
}
#if 0
if (fp) fclose(fp);
#endif
app->PumpWaitingMessages();
}
app->PumpWaitingMessages();
}
mess[0] = '\0';
TDib *dib;
if ((dib = LoadDib(fname)) == NULL) {
wsprintf(str, "ビットマップファイル(%s)を開けません", fname);
client->MessageBox(str, "Tch2Avi", MB_OK);
dib = NULL;
return NULL;
}
if (removeflag) {
unlink(fname);
}
#if 0
strcpy(mess, fname);
#else
char *p;
if ((p = strrchr(fname, '\\')) == NULL) {
p = fname;
} else {
p++;
}
wsprintf(mess, "%s ", p);
#endif
TRect clientRect(0,0,dib->Width(), dib->Height());
dc.SetDIBitsToDevice(clientRect, TPoint(0,0), *dib);
dc.TextOut(0,0,mess);
// client->EvPaint();
// client->Redraw();
return dib;
}
static int AviOpen(void)
{
AVIFileInit();
hr = AVIFileOpen(&pfile, // returned file pointer
avifile, // file name
OF_WRITE | OF_CREATE, // mode to open file with
NULL); // use handler determined
if (hr != AVIERR_OK) {
strcpy(errorfuncname, "AVIFileOpen");
goto error;
}
memset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = rate; // 15 fps
strhdr.dwSuggestedBufferSize = lpbi->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
dib->Width(), dib->Height());
hr = AVIFileCreateStream(pfile, // file pointer
&psAll, // returned stream pointer
&strhdr); // stream header
if (hr != AVIERR_OK) {
strcpy(errorfuncname, "AVIFileCreateStream");
goto error;
}
#if 0
if (opts->fccHandler == 0) {
if (AVISaveOptions(NULL,
ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE,
1, &psAll, &opts) == FALSE) {
goto error;
}
}
#endif
hr = AVIMakeCompressedStream(&psVideo, psAll, opts, NULL);
if (hr != AVIERR_OK) {
strcpy(errorfuncname, "AVIMakeCompressedStream");
goto error;
}
hr = AVIStreamSetFormat(psVideo, 0,
lpbi, // stream format
lpbi->biSize + // format size
lpbi->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK) {
strcpy(errorfuncname, "AVIStreamSetFormat");
goto error;
}
return TRUE;
error:
return FALSE;
}
static void AviClose(void)
{
// if (dib) delete dib;
if (psAll) AVIStreamClose(psAll);
if (psVideo) AVIStreamClose(psVideo);
if (pfile) AVIFileClose(pfile);
AVIFileExit();
}
static void pic(char* name, int wait, int speed)
{
app->PumpWaitingMessages();
if (InTrans == FALSE) {
return;
}
if (errorstatus) {
return;
}
if (dib) {
delete dib;
dib = NULL;
}
if ((dib = OpenDIB(name)) == NULL) {
errorstatus = TRUE;
strcpy(errorfuncname, "OpenDIB");
return;
}
if (lastwait < 0) {
TFrameWindow *main = TYPESAFE_DOWNCAST(app->GetMainWindow(), TFrameWindow); // フレーム用のクライアントウィンドウ
TRect rect, crect;
main->GetWindowRect(rect);
main->GetClientWindow()->GetWindowRect(crect);
int wx = (rect.right-rect.left) - (crect.right-crect.left);
int wy = (rect.bottom-rect.top) - (crect.bottom-crect.top);
main->MoveWindow(rect.left, rect.top,
dib->Width() + wx, dib->Height() + wy, TRUE);
}
lastwait = wait;
lpbi = dib->GetInfoHeader();
if (frames == 0) {
rate = 60 / speed;
if (AviOpen() == FALSE) {
errorstatus = TRUE;
// strcpy(errorfuncname, "AviOpen");
return;
}
}
// for (; wait > 0; --wait) {
hr = AVIStreamWrite(psVideo, // stream pointer
frames, // time of this frame
1, // number to write
(LPBYTE)dib->GetBits(),
lpbi->biSizeImage,// size of this frame
AVIIF_KEYFRAME, // flags....
NULL, NULL);
if (hr != AVIERR_OK) {
errorstatus = TRUE;
strcpy(errorfuncname, "AVIStreamWrite");
}
// }
frames += wait;
}
void pcm(char */*name*/, int /*rate*/)
{
}
int tch2avi(TShowWindow *_client, char *_tchfile, char *_avifile,
AVICOMPRESSOPTIONS *_opts, int _waitflag, int _removeflag)
{
InTrans = TRUE;
client = _client;
tchfile = _tchfile;
avifile = _avifile;
opts = _opts;
waitflag = _waitflag;
removeflag = _removeflag;
lastwait = -1;
errorstatus = 0;
errorfuncname[0] = '\0';
pfile = NULL;
lpbi = NULL;
psAll = NULL;
psVideo = NULL;
frames = 0;
scantimechart(pic, pcm);
if (lastwait > 1) {
hr = AVIStreamWrite(psVideo, // stream pointer
frames-1, // time of this frame
1, // number to write
(LPBYTE)dib->GetBits(),
lpbi->biSizeImage,// size of this frame
AVIIF_KEYFRAME, // flags....
NULL, NULL);
}
AviClose();
if (InTrans == FALSE) {
return FALSE;
} else if (errorstatus) {
char str[120];
if (errorfuncname[0]) {
wsprintf(str, "%s: ", errorfuncname);
} else {
str[0] = '\0';
}
strcat(str, "コンバートに失敗しました。");
if (hr == AVIERR_OK) {
} else if (hr == (HRESULT)AVIERR_UNSUPPORTED) {
strcat(str, "(UNSUPPORTED)");
} else if (hr == (HRESULT)AVIERR_BADFORMAT) {
strcat(str, "(BADFORMAT)");
} else if (hr == (HRESULT)AVIERR_MEMORY) {
strcat(str, "(MEMORY)");
} else if (hr == (HRESULT)AVIERR_INTERNAL) {
strcat(str, "(INTERNAL)");
} else if (hr == (HRESULT)AVIERR_BADFLAGS) {
strcat(str, "(BADCLASS)");
} else if (hr == (HRESULT)AVIERR_BADPARAM) {
strcat(str, "(BADPARAM)");
} else if (hr == (HRESULT)AVIERR_BADSIZE) {
strcat(str, "(BADSIZE)");
} else if (hr == (HRESULT)AVIERR_BADHANDLE) {
strcat(str, "(BADHANDLE)");
} else if (hr == (HRESULT)AVIERR_FILEREAD) {
strcat(str, "(FILEREAD)");
} else if (hr == (HRESULT)AVIERR_FILEWRITE) {
strcat(str, "(FILEWRITED)");
} else if (hr == (HRESULT)AVIERR_FILEOPEN) {
strcat(str, "(FILEOPEN)");
} else if (hr == (HRESULT)AVIERR_COMPRESSOR) {
strcat(str, "(COMPRESSOR)");
} else if (hr == (HRESULT)AVIERR_NOCOMPRESSOR) {
strcat(str, "(NOCOMPRESS)");
} else if (hr == (HRESULT)AVIERR_READONLY) {
strcat(str, "(READONLY)");
} else if (hr == (HRESULT)AVIERR_NODATA) {
strcat(str, "(NODATA)");
} else if (hr == (HRESULT)AVIERR_BUFFERTOOSMALL) {
strcat(str, "(BUFFERTOOSMALL)");
} else if (hr == (HRESULT)AVIERR_CANTCOMPRESS) {
strcat(str, "(CANTCOMPRESS)");
} else if (hr == (HRESULT)AVIERR_USERABORT) {
strcat(str, "(USERABORT)");
} else if (hr == (HRESULT)AVIERR_ERROR) {
strcat(str, "(ERROR)");
}
client->MessageBox(str, "Tch2Avi", MB_OK);
InTrans = FALSE;
return FALSE;
} else {
#if 0
char str[120];
wsprintf(str, "コンバートに成功しました。");
client->MessageBox(str, "Tch2Avi", MB_OK);
#endif
InTrans = FALSE;
return TRUE;
}
}
FILE *logfp = NULL;
static void piccheck(char* name, int /*wait*/, int /*speed*/)
{
app->PumpWaitingMessages();
struct stat statbuf;
if (stat(name, &statbuf) != 0) {
if (errorstatus < 16) {
char str[256];
wsprintf(str, "ビットマップファイル(%s)を開けません", name);
client->MessageBox(str, "Tch2Avi", MB_OK);
}
if (logfp == NULL) {
logfp = fopen("tch2avi.log", "a");
}
if (logfp) {
fprintf(logfp, "ビットマップファイル(%s)を開けません\n", name);
}
errorstatus++;
}
}
int tchcheck(TShowWindow *_client)
{
client = _client;
errorstatus = 0;
scantimechart(piccheck, pcm);
if (logfp) fclose(logfp);
logfp = NULL;
return (errorstatus == 0);
}