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 >
C/C++ Source or Header  |  1996-07-16  |  9KB  |  374 lines

  1. #include <owl\owlpch.h>
  2. #pragma hdrstop
  3. #include <stdio.h>
  4. #include <sys\stat.h>
  5. #include <vfw.h>
  6. #include "tch2vapp.h"
  7. #include "tshwwndw.h"
  8. #include "tch.h"
  9.  
  10. extern TApplication *app;
  11. extern int InTrans;
  12.  
  13. static int frames;
  14. static TShowWindow *client;
  15. static char *tchfile;
  16. static char *avifile;
  17. static AVICOMPRESSOPTIONS *opts;
  18. static HRESULT hr;
  19. static PAVIFILE        pfile = NULL;
  20. static AVISTREAMINFO strhdr;
  21. static BITMAPINFOHEADER *lpbi = NULL;
  22. static PAVISTREAM        psAll = NULL, psVideo = NULL;
  23. extern TDib *dib;
  24. extern char mess[256];
  25. static int rate;
  26. static int waitflag;
  27. static int removeflag;
  28. static int errorstatus;
  29. static char errorfuncname[32];
  30. static int lastwait = 1;
  31. int sleeptime = 0;
  32.  
  33. static TDib *OpenDIB(char *fname)
  34. {
  35.     TClientDC dc(*client);
  36.     char str[120];
  37.     if (waitflag) {
  38.         struct stat statbuf;
  39.         if (stat(fname, &statbuf) != 0 || statbuf.st_size == 0) {
  40.             char *p;
  41.             if ((p = strrchr(fname, '\\')) == NULL) {
  42.                 p = fname;
  43.             } else {
  44.                 p++;
  45.             }
  46.             wsprintf(mess, "待機: %s                         ", p);
  47.             dc.TextOut(0,0,mess);
  48.             dib = NULL;
  49. #if 0
  50.             while (stat(fname, &statbuf) != 0 || statbuf.st_size == 0) {
  51. #else
  52.             OFSTRUCT openbuf;
  53.             while (stat(fname, &statbuf) != 0 || statbuf.st_size == 0
  54.                 || OpenFile(fname, &openbuf, OF_EXIST|OF_SHARE_EXCLUSIVE) == HFILE_ERROR) {
  55. #endif
  56.                 if (sleeptime > 0) {
  57.                     DWORD last,now;
  58.                     last = GetTickCount();
  59.                     do {
  60.                         if (InTrans == FALSE) {
  61.                             return NULL;
  62.                         }
  63.                         app->PumpWaitingMessages();
  64.                         now = GetTickCount();
  65.                     } while (now < last + sleeptime * 1000 && now > last);
  66.                 }
  67.                 if (InTrans == FALSE) {
  68.                     return NULL;
  69.                 }
  70.                 app->PumpWaitingMessages();
  71.             }
  72. #if 0
  73.             if (fp) fclose(fp);
  74. #endif
  75.             app->PumpWaitingMessages();
  76.         }
  77.         app->PumpWaitingMessages();
  78.     }
  79.     mess[0] = '\0';
  80.     TDib *dib;
  81.     if ((dib = LoadDib(fname)) == NULL) {
  82.         wsprintf(str, "ビットマップファイル(%s)を開けません", fname);
  83.         client->MessageBox(str, "Tch2Avi", MB_OK);
  84.         dib = NULL;
  85.         return NULL;
  86.     }
  87.     if (removeflag) {
  88.         unlink(fname);
  89.     }
  90. #if 0
  91.     strcpy(mess, fname);
  92. #else
  93.     char *p;
  94.     if ((p = strrchr(fname, '\\')) == NULL) {
  95.         p = fname;
  96.     } else {
  97.         p++;
  98.     }
  99.     wsprintf(mess, "%s                              ", p);
  100. #endif
  101.     TRect clientRect(0,0,dib->Width(), dib->Height());
  102.     dc.SetDIBitsToDevice(clientRect, TPoint(0,0), *dib);
  103.     dc.TextOut(0,0,mess);
  104. //    client->EvPaint();
  105. //    client->Redraw();
  106.     return dib;
  107. }
  108.  
  109. static int AviOpen(void)
  110. {
  111.     AVIFileInit();
  112.  
  113.     hr = AVIFileOpen(&pfile,                // returned file pointer
  114.                avifile,             // file name
  115.                OF_WRITE | OF_CREATE,        // mode to open file with
  116.                NULL);                // use handler determined
  117.     if (hr != AVIERR_OK) {
  118.         strcpy(errorfuncname, "AVIFileOpen");
  119.         goto error;
  120.     }
  121.  
  122.     memset(&strhdr, 0, sizeof(strhdr));
  123.     strhdr.fccType                = streamtypeVIDEO;// stream type
  124.     strhdr.fccHandler             = 0;
  125.     strhdr.dwScale                = 1;
  126.     strhdr.dwRate                 = rate;            // 15 fps
  127.     strhdr.dwSuggestedBufferSize  = lpbi->biSizeImage;
  128.     SetRect(&strhdr.rcFrame, 0, 0,            // rectangle for stream
  129.             dib->Width(), dib->Height());
  130.  
  131.     hr = AVIFileCreateStream(pfile,            // file pointer
  132.                    &psAll,            // returned stream pointer
  133.                    &strhdr);        // stream header
  134.     if (hr != AVIERR_OK) {
  135.         strcpy(errorfuncname, "AVIFileCreateStream");
  136.         goto error;
  137.     }
  138. #if 0
  139.     if (opts->fccHandler == 0) {
  140.         if (AVISaveOptions(NULL,
  141.                     ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE,
  142.                     1, &psAll, &opts) == FALSE) {
  143.             goto error;
  144.         }
  145.     }
  146. #endif
  147.     hr = AVIMakeCompressedStream(&psVideo, psAll, opts, NULL);
  148.     if (hr != AVIERR_OK) {
  149.         strcpy(errorfuncname, "AVIMakeCompressedStream");
  150.         goto error;
  151.     }
  152.  
  153.     hr = AVIStreamSetFormat(psVideo, 0,
  154.                    lpbi,        // stream format
  155.                    lpbi->biSize +   // format size
  156.                    lpbi->biClrUsed * sizeof(RGBQUAD));
  157.     if (hr != AVIERR_OK) {
  158.         strcpy(errorfuncname, "AVIStreamSetFormat");
  159.         goto error;
  160.     }
  161.  
  162.     return TRUE;
  163.     error:
  164.     return FALSE;
  165. }
  166.  
  167. static void AviClose(void)
  168. {
  169. //    if (dib)    delete dib;
  170.  
  171.     if (psAll)        AVIStreamClose(psAll);
  172.  
  173.     if (psVideo)    AVIStreamClose(psVideo);
  174.  
  175.     if (pfile)        AVIFileClose(pfile);
  176.  
  177.     AVIFileExit();
  178. }
  179.  
  180. static void pic(char* name, int wait, int speed)
  181. {
  182.     app->PumpWaitingMessages();
  183.     if (InTrans == FALSE) {
  184.         return;
  185.     }
  186.     if (errorstatus) {
  187.         return;
  188.     }
  189.     if (dib) {
  190.         delete dib;
  191.         dib = NULL;
  192.     }
  193.     if ((dib = OpenDIB(name)) == NULL) {
  194.         errorstatus = TRUE;
  195.         strcpy(errorfuncname, "OpenDIB");
  196.         return;
  197.     }
  198.  
  199.     if (lastwait < 0) {
  200.         TFrameWindow *main = TYPESAFE_DOWNCAST(app->GetMainWindow(), TFrameWindow);     // フレーム用のクライアントウィンドウ
  201.         TRect rect, crect;
  202.         main->GetWindowRect(rect);
  203.         main->GetClientWindow()->GetWindowRect(crect);
  204.         int wx = (rect.right-rect.left) - (crect.right-crect.left);
  205.         int wy = (rect.bottom-rect.top) - (crect.bottom-crect.top);
  206.         main->MoveWindow(rect.left, rect.top,
  207.                     dib->Width() + wx, dib->Height() + wy, TRUE);
  208.  
  209.     }
  210.     lastwait = wait;
  211.     lpbi = dib->GetInfoHeader();
  212.     if (frames == 0) {
  213.         rate = 60 / speed;
  214.         if (AviOpen() == FALSE) {
  215.             errorstatus = TRUE;
  216. //            strcpy(errorfuncname, "AviOpen");
  217.             return;
  218.         }
  219.     }
  220. //    for (; wait > 0; --wait) {
  221.         hr = AVIStreamWrite(psVideo,        // stream pointer
  222.                           frames,            // time of this frame
  223.                           1,            // number to write
  224.                             (LPBYTE)dib->GetBits(),
  225.                           lpbi->biSizeImage,// size of this frame
  226.                           AVIIF_KEYFRAME,        // flags....
  227.                           NULL, NULL);
  228.     if (hr != AVIERR_OK) {
  229.         errorstatus = TRUE;
  230.         strcpy(errorfuncname, "AVIStreamWrite");
  231.     }
  232. //    }
  233.     frames += wait;
  234. }
  235.  
  236. void pcm(char */*name*/, int /*rate*/)
  237. {
  238. }
  239.  
  240. int tch2avi(TShowWindow *_client, char *_tchfile, char *_avifile,
  241.              AVICOMPRESSOPTIONS *_opts, int _waitflag, int _removeflag)
  242. {
  243.     InTrans = TRUE;
  244.  
  245.     client = _client;
  246.     tchfile = _tchfile;
  247.     avifile = _avifile;
  248.     opts = _opts;
  249.     waitflag = _waitflag;
  250.     removeflag = _removeflag;
  251.     lastwait = -1;
  252.  
  253.     errorstatus = 0;
  254.     errorfuncname[0] = '\0';
  255.  
  256.     pfile = NULL;
  257.     lpbi = NULL;
  258.     psAll = NULL;
  259.     psVideo = NULL;
  260.  
  261.     frames = 0;
  262.  
  263.     scantimechart(pic, pcm);
  264.  
  265.     if (lastwait > 1) {
  266.         hr = AVIStreamWrite(psVideo,        // stream pointer
  267.                           frames-1,            // time of this frame
  268.                           1,            // number to write
  269.                             (LPBYTE)dib->GetBits(),
  270.                           lpbi->biSizeImage,// size of this frame
  271.                           AVIIF_KEYFRAME,        // flags....
  272.                           NULL, NULL);
  273.     }
  274.  
  275.     AviClose();
  276.     if (InTrans == FALSE) {
  277.         return FALSE;
  278.     } else if (errorstatus) {
  279.         char str[120];
  280.         if (errorfuncname[0]) {
  281.             wsprintf(str, "%s: ", errorfuncname);
  282.         } else {
  283.             str[0] = '\0';
  284.         }
  285.         strcat(str, "コンバートに失敗しました。");
  286.         if (hr == AVIERR_OK) {
  287.         } else if (hr == (HRESULT)AVIERR_UNSUPPORTED) {
  288.             strcat(str, "(UNSUPPORTED)");
  289.         } else if (hr == (HRESULT)AVIERR_BADFORMAT) {
  290.             strcat(str, "(BADFORMAT)");
  291.         } else if (hr == (HRESULT)AVIERR_MEMORY) {
  292.             strcat(str, "(MEMORY)");
  293.         } else if (hr == (HRESULT)AVIERR_INTERNAL) {
  294.             strcat(str, "(INTERNAL)");
  295.         } else if (hr == (HRESULT)AVIERR_BADFLAGS) {
  296.             strcat(str, "(BADCLASS)");
  297.         } else if (hr == (HRESULT)AVIERR_BADPARAM) {
  298.             strcat(str, "(BADPARAM)");
  299.         } else if (hr == (HRESULT)AVIERR_BADSIZE) {
  300.             strcat(str, "(BADSIZE)");
  301.         } else if (hr == (HRESULT)AVIERR_BADHANDLE) {
  302.             strcat(str, "(BADHANDLE)");
  303.         } else if (hr == (HRESULT)AVIERR_FILEREAD) {
  304.             strcat(str, "(FILEREAD)");
  305.         } else if (hr == (HRESULT)AVIERR_FILEWRITE) {
  306.             strcat(str, "(FILEWRITED)");
  307.         } else if (hr == (HRESULT)AVIERR_FILEOPEN) {
  308.             strcat(str, "(FILEOPEN)");
  309.         } else if (hr == (HRESULT)AVIERR_COMPRESSOR) {
  310.             strcat(str, "(COMPRESSOR)");
  311.         } else if (hr == (HRESULT)AVIERR_NOCOMPRESSOR) {
  312.             strcat(str, "(NOCOMPRESS)");
  313.         } else if (hr == (HRESULT)AVIERR_READONLY) {
  314.             strcat(str, "(READONLY)");
  315.         } else if (hr == (HRESULT)AVIERR_NODATA) {
  316.             strcat(str, "(NODATA)");
  317.         } else if (hr == (HRESULT)AVIERR_BUFFERTOOSMALL) {
  318.             strcat(str, "(BUFFERTOOSMALL)");
  319.         } else if (hr == (HRESULT)AVIERR_CANTCOMPRESS) {
  320.             strcat(str, "(CANTCOMPRESS)");
  321.         } else if (hr == (HRESULT)AVIERR_USERABORT) {
  322.             strcat(str, "(USERABORT)");
  323.         } else if (hr == (HRESULT)AVIERR_ERROR) {
  324.             strcat(str, "(ERROR)");
  325.         }
  326.         client->MessageBox(str, "Tch2Avi", MB_OK);
  327.         InTrans = FALSE;
  328.         return FALSE;
  329.     } else {
  330. #if 0
  331.         char str[120];
  332.         wsprintf(str, "コンバートに成功しました。");
  333.         client->MessageBox(str, "Tch2Avi", MB_OK);
  334. #endif
  335.         InTrans = FALSE;
  336.         return TRUE;
  337.     }
  338. }
  339.  
  340.  
  341. FILE *logfp = NULL;
  342.  
  343.  
  344. static void piccheck(char* name, int /*wait*/, int /*speed*/)
  345. {
  346.     app->PumpWaitingMessages();
  347.     struct stat statbuf;
  348.     if (stat(name, &statbuf) != 0) {
  349.         if (errorstatus < 16) {
  350.             char str[256];
  351.             wsprintf(str, "ビットマップファイル(%s)を開けません", name);
  352.             client->MessageBox(str, "Tch2Avi", MB_OK);
  353.         }
  354.         if (logfp == NULL) {
  355.             logfp = fopen("tch2avi.log", "a");
  356.         }
  357.         if (logfp) {
  358.             fprintf(logfp, "ビットマップファイル(%s)を開けません\n", name);
  359.         }
  360.         errorstatus++;
  361.     }
  362. }
  363.  
  364. int tchcheck(TShowWindow *_client)
  365. {
  366.     client = _client;
  367.     errorstatus = 0;
  368.     scantimechart(piccheck, pcm);
  369.     if (logfp) fclose(logfp);
  370.     logfp = NULL;
  371.     return (errorstatus == 0);
  372. }
  373.  
  374.