📄 bmpreader.c
字号:
// bmpReader.c - by Robin Hewitt, 2005
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for details.
//
//////////////////////////////////////////////////////////////
// A dll that "captures" frame data from a list of .bmp files
// rather than from framecap. The purpose of this dll is to
// help with testing and postmortem analysis. Given a captured
// frame, or series of captured frames, saved in windows bitmap
// format, this dll allows Mavis to function as if the data were
// arriving from live video feed.
//
// The exported function interfaces, are in fgcomm.h -- same
// header file that the frame-capture client dll uses.
// USAGE:
// This initial implementation is simple. The name of the file that
// contains the list of .bmp files to use is hardcoded as bmpfiles.txt.
// It's assumed to be in the current run directory. It's also assumed
// that the .bmp files are in the current directory.
// Todo: create a generic FrameReader interface for image data.
// Consolidate the API into that interface and modify the existing
// dlls to use the consolidated API.
// special project settings:
// C/C++, category=preprocessor, added ../../include to the additional
// include directories.
//
// added post-build command: copy Debug\bmpReader.dll ..\..\bin
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "fgcomm.h"
__declspec(dllexport) int getWidth(); // get frame width
__declspec(dllexport) int getHeight(); // get frame height
__declspec(dllexport) int getFrameSize(); // get buffer size needed to hold one frame
__declspec(dllexport) int getFrame(BYTE * pBuf, int lBuf); // load a bitmap into the buffer
BOOL init();
static int width = 0;
static int height = 0;
static int frameSize = 0;
static int nBmpFiles = 0;
static int iFile = 0;
static char ** fileList = 0;
static BITMAPINFOHEADER bih;
static fpos_t fpBuf = 0; // file-pointer position for start of buffer data
//set up a shared variable to track number of
//attached instances
#pragma data_seg(".shared")
int instCount = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // DLL module handle
DWORD fdwReason, // reason called
LPVOID lpvReserved // reserved
) {
switch (fdwReason) {
int i;
case DLL_PROCESS_ATTACH:
if(instCount) {
// only allow one client instance to run at a time
return FALSE;
} else {
++instCount;
return init();
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
for(i=0; i<nBmpFiles; i++)
if( fileList[i] ) free(fileList[i]);
if(fileList) free(fileList);
--instCount;
break;
default:
break;
}
return TRUE;
UNREFERENCED_PARAMETER(hinstDLL);
UNREFERENCED_PARAMETER(lpvReserved);
}
BOOL init()
{
BITMAPFILEHEADER bfh = {0};
int bfhSize;
FILE * pListFile = NULL;
FILE * pBmpFile = NULL;
char bmpName[_MAX_PATH];
int i;
bfhSize = sizeof(BITMAPFILEHEADER);
bih.biSize = sizeof(BITMAPINFOHEADER);
fpBuf = bfhSize + bih.biSize;
pListFile = fopen("bmpfiles.txt", "r");
if(!pListFile) return FALSE;
// count the number of files in the list
while( EOF != fscanf(pListFile, "%s", bmpName) )
{
pBmpFile = fopen(bmpName, "rb");
if(!pBmpFile)
{
nBmpFiles = 0;
fclose(pListFile);
return FALSE;
}
fclose(pBmpFile);
++nBmpFiles;
}
if(!nBmpFiles)
{
fclose(pListFile);
return FALSE;
}
// make the file list
fileList = (char **)calloc( nBmpFiles, sizeof(char *) );
memset( fileList, 0, nBmpFiles*sizeof(char *) );
rewind(pListFile);
for(i=0; i<nBmpFiles; i++)
{
fscanf(pListFile, "%s", bmpName);
fileList[i] = (char *)calloc( 1+strlen(bmpName), sizeof(char) );
strcpy(fileList[i], bmpName);
}
fclose(pListFile);
// get bitmap size data
pBmpFile = fopen(fileList[0], "rb");
if(!pBmpFile) return FALSE;
fread(&bfh, bfhSize, 1, pBmpFile);
fread(&bih, bfhSize, 1, pBmpFile);
fclose(pBmpFile);
width = bih.biWidth;
height = bih.biHeight;
frameSize = bih.biHeight * bih.biWidth * 3;
return TRUE;
}
int getWidth() { return width; }
int getHeight() { return height; }
int getFrameSize() { return frameSize; }
int getFrame(BYTE * pBuf, int lBuf) {
FILE * pBmpFile = NULL;
if(iFile == nBmpFiles) iFile = 0;
pBmpFile = fopen(fileList[iFile++], "rb");
fsetpos(pBmpFile, &fpBuf);
fread(pBuf, frameSize, 1, pBmpFile);
fclose(pBmpFile);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
// Mavis License Agreement
//
// Copyright (c) 2004-2005, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage.
///////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -