FramGrab.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FramGrab.cpp: implementation of the FrameGrabClass class.
  19. //
  20. //////////////////////////////////////////////////////////////////////
  21. #include "framgrab.h"
  22. #include <stdio.h>
  23. #include <io.h>
  24. //#include <errno.h>
  25. //////////////////////////////////////////////////////////////////////
  26. // Construction/Destruction
  27. //////////////////////////////////////////////////////////////////////
  28. FrameGrabClass::FrameGrabClass(const char *filename, MODE mode, int width, int height, int bitcount, float framerate)
  29. {
  30. HRESULT hr;
  31. Mode = mode;
  32. Filename = filename;
  33. FrameRate = framerate;
  34. Counter = 0;
  35. Stream = 0;
  36. AVIFile = 0;
  37. if(Mode != AVI) return;
  38. AVIFileInit(); // opens AVIFile library
  39. // find the first free file with this prefix
  40. int counter = 0;
  41. int result;
  42. char file[256];
  43. do {
  44. sprintf(file, "%s%d.AVI", filename, counter++);
  45. result = _access(file, 0);
  46. } while(result != -1);
  47. // Create new AVI file using AVIFileOpen.
  48. hr = AVIFileOpen(&AVIFile, file, OF_WRITE | OF_CREATE, NULL);
  49. if (hr != 0) {
  50. char buf[256];
  51. sprintf(buf, "Unable to open %s\n", Filename);
  52. OutputDebugString(buf);
  53. CleanupAVI();
  54. return;
  55. }
  56. // Create a stream using AVIFileCreateStream.
  57. AVIStreamInfo.fccType = streamtypeVIDEO;
  58. AVIStreamInfo.fccHandler = mmioFOURCC('M','S','V','C');
  59. AVIStreamInfo.dwFlags = 0;
  60. AVIStreamInfo.dwCaps = 0;
  61. AVIStreamInfo.wPriority = 0;
  62. AVIStreamInfo.wLanguage = 0;
  63. AVIStreamInfo.dwScale = 1;
  64. AVIStreamInfo.dwRate = (int)FrameRate;
  65. AVIStreamInfo.dwStart = 0;
  66. AVIStreamInfo.dwLength = 0;
  67. AVIStreamInfo.dwInitialFrames = 0;
  68. AVIStreamInfo.dwSuggestedBufferSize = 0;
  69. AVIStreamInfo.dwQuality = 0;
  70. AVIStreamInfo.dwSampleSize = 0;
  71. SetRect(&AVIStreamInfo.rcFrame, 0, 0, width, height);
  72. AVIStreamInfo.dwEditCount = 0;
  73. AVIStreamInfo.dwFormatChangeCount = 0;
  74. sprintf(AVIStreamInfo.szName,"G");
  75. hr = AVIFileCreateStream(AVIFile, &Stream, &AVIStreamInfo);
  76. if (hr != 0) {
  77. CleanupAVI();
  78. return;
  79. }
  80. // Set format of new stream
  81. BitmapInfoHeader.biWidth = width;
  82. BitmapInfoHeader.biHeight = height;
  83. BitmapInfoHeader.biBitCount = (unsigned short)bitcount;
  84. BitmapInfoHeader.biSizeImage = ((((UINT)BitmapInfoHeader.biBitCount * BitmapInfoHeader.biWidth + 31) & ~31) / 8) * BitmapInfoHeader.biHeight;
  85. BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER); // size of structure
  86. BitmapInfoHeader.biPlanes = 1; // must be set to 1
  87. BitmapInfoHeader.biCompression = BI_RGB; // uncompressed
  88. BitmapInfoHeader.biXPelsPerMeter = 1; // not used
  89. BitmapInfoHeader.biYPelsPerMeter = 1; // not used
  90. BitmapInfoHeader.biClrUsed = 0; // all colors are used
  91. BitmapInfoHeader.biClrImportant = 0; // all colors are important
  92. hr = AVIStreamSetFormat(Stream, 0, &BitmapInfoHeader, sizeof(BitmapInfoHeader));
  93. if (hr != 0) {
  94. CleanupAVI();
  95. return;
  96. }
  97. Bitmap = (long *) GlobalAllocPtr(GMEM_MOVEABLE, BitmapInfoHeader.biSizeImage);
  98. }
  99. FrameGrabClass::~FrameGrabClass()
  100. {
  101. if(Mode == AVI) {
  102. CleanupAVI();
  103. }
  104. }
  105. void FrameGrabClass::CleanupAVI() {
  106. if(Bitmap != 0) { GlobalFreePtr(Bitmap); Bitmap = 0; }
  107. if(Stream != 0) { AVIStreamRelease(Stream); Stream = 0; }
  108. if(AVIFile != 0) { AVIFileRelease(AVIFile); AVIFile = 0; }
  109. AVIFileExit();
  110. Mode = RAW;
  111. }
  112. void FrameGrabClass::GrabAVI(void *BitmapPointer)
  113. {
  114. // CompressDIB(&bi, lpOld, &biNew, lpNew);
  115. // Save the compressed data using AVIStreamWrite.
  116. HRESULT hr = AVIStreamWrite(Stream, Counter++, 1, BitmapPointer, BitmapInfoHeader.biSizeImage, AVIIF_KEYFRAME, NULL, NULL);
  117. if(hr != 0) {
  118. char buf[256];
  119. sprintf(buf, "avi write error %x/%d\n", hr, hr);
  120. OutputDebugString(buf);
  121. }
  122. }
  123. void FrameGrabClass::GrabRawFrame(void * /*BitmapPointer*/)
  124. {
  125. }
  126. void FrameGrabClass::ConvertGrab(void *BitmapPointer)
  127. {
  128. ConvertFrame(BitmapPointer);
  129. Grab( Bitmap );
  130. }
  131. void FrameGrabClass::Grab(void *BitmapPointer)
  132. {
  133. if(Mode == AVI)
  134. GrabAVI(BitmapPointer);
  135. else
  136. GrabRawFrame(BitmapPointer);
  137. }
  138. void FrameGrabClass::ConvertFrame(void *BitmapPointer)
  139. {
  140. int width = BitmapInfoHeader.biWidth;
  141. int height = BitmapInfoHeader.biHeight;
  142. long *image = (long *) BitmapPointer;
  143. // copy the data, doing a vertical flip & byte re-ordering of the pixel longwords
  144. int y = height;
  145. while(y--) {
  146. int x = width;
  147. int yoffset = y * width;
  148. int yoffset2 = (height - y) * width;
  149. while(x--) {
  150. long *source = &image[yoffset + x];
  151. long *dest = &Bitmap[yoffset2 + x];
  152. *dest = *source;
  153. unsigned char *c = (unsigned char *) dest;
  154. c[3] = c[0];
  155. c[0] = c[2];
  156. c[2] = c[3];
  157. c[3] = 0;
  158. }
  159. }
  160. }