VideoDLL.dll - An image capture library
  Download it here 



Overview :


VideoDLL is a dynamic linking library written by Ammar Qammaz for image capture purposes :) .
The library contains basic error checking and sticks to Selecting a Capture Device -> Setting its size (resolution) ->   Grabbing frames (  also possible to save them as a bmp if you want ) and finally filtering them , with as little extra ( useless ) code as possible...  
The project , in order to achieve its goals , is composed of two parts. The first one is a Sample Grabber implementation using Direct X which opens a capture device and grabs frames whenever the program asks it and the second a picture filter collection that aplies direct filtering ( using pointer arithmetic ) to the grabbed samples in order for a fast performance , with as little data shifting around as possible..

In order to use it , you will need a PC with Windows 9x/XP/Vista (  ? Linux with Wine ? ) and Direct X 8+
The DLL can be copied to the system folder ( C:\WINDOWS\System32\ ) or in the target executable directory , with it there are 3 files  ( mfc71d.dll , msvcp71d.dll , msvcr71d.dll ) , these are Microsoft MFC support dlls

VideoDLL.dll was compiled using Visual Studio 2003

Exports :


//BASIC FUNCTIONALITY
void _stdcall InitCamera() // Initializes a Camera selected with SetDeviceNumber , using the resolution defined by SetDesiredResolution , if you don`t call SetDeviceNumber or SetDesiredResolution
void _stdcall SetDeviceNumber(unsigned int thecam) // Selects a camera from the connected ones
void _stdcall SetDesiredResolution(unsigned int x,unsigned int y)  // Sets the desired resolution for the camera to be initialized
void _stdcall UnInitCamera() // Closes down cameras

//TAKING A SNAPSHOT 
void _stdcall SnapShot() // Takes a snapshot and stores it in memory and in file cap.bmp
void _stdcall Snap() // Takes a snapshot and stores it in memory

//TAKING A  FAST SNAPSHOT
// The idea is call InitForSnap one time then call FastSnap / SaveSnap as many times as you want , then end by calling UninitForSnap..
void _stdcall InitForSnap() // Sets up initialized device for picture grabbing
void _stdcall FastSnap() //Snaps Data from the device to mem
void _stdcall SaveSnap() // Saves Snapped Data to the file cap.bmp
void _stdcall UnInitForSnap() // Stops picture grabbing

void _stdcall CopyDC(HDC hDC,unsigned int pt_x,unsigned int pt_y) // Copies data to the hdc , starting at pt_x, pt_y
int _stdcall GetVideoXY(int x,int y) // Retrieves Pixel Color For pixel x,y
void _stdcall SetVideoXY(int x,int y,unsigned int color) // Sets Pixel Color For pixel x,y ( color is RGB(r,g,b) windows.h)

int _stdcall VideoSizeX() // Gets Actual x size
int _stdcall VideoSizeY() // Gets Actual y size

//FILTERS
void  _stdcall  PassVideo2Filter() // Passes Snap Data to filtering
void _stdcall Filter_MonochromeSobel() // Applies Monochrome and then Sobel Filter
void _stdcall Filter_KillPixelsBelow(int threshold) // Kills pixels with R , G or B values less than threshold
void _stdcall Filter_GaussianSmooth(bool monochrome) // Smooths the image using Gaussian Filter
void _stdcall Filter_Sobel() // Sobel Filter
void _stdcall Filter_FastEdge(int i,int z) // "Fast" Edge detection :P , under construction
 unsigned int _stdcall GetFilterXY(int x,int y) //Gets Filtered Image Color at Pixel x,y
void _stdcall SetFilterXY(int x,int y,unsigned int color)
//Sets Filtered Image Color at Pixel x,y



Code Sample #1 (Dev-C++) :


//THIS CODE WILL START A  CONSOLE WINDOW , LOAD 3 DLL FUNCTIONS , OPEN THE FIRST CAMERA CONNECTED (
InitCamera)
// TAKE A SNAPSHOT ( create a file cap.bmp with the picture)
// UNINIT THE CAMERAS

#include <windows.h> 
#include <cstdlib>
#include <iostream>  

using namespace std;
 
 
typedef void (*InitCamera)();
typedef void (*UnInitCamera)();
typedef void (*SnapShot)();
 
 
int main(int argc, char *argv[])
{
    InitCamera _InitCamera;
    UnInitCamera _UnInitCamera;
    SnapShot _SnapShot;
    HINSTANCE hInstLibrary = LoadLibrary("VideoDLL.dll");
      if (hInstLibrary)
     {
       _InitCamera = (InitCamera)GetProcAddress(hInstLibrary, "InitCamera");
       _UnInitCamera = (UnInitCamera)GetProcAddress(hInstLibrary, "UnInitCamera");
       _SnapShot = (SnapShot)GetProcAddress(hInstLibrary, "SnapShot");
     
   
       if (_InitCamera) {} else printf("%s","Something went wrong with InitCamera\n");
       if (_UnInitCamera) {} else printf("%s","Something went wrong with UnInitCamera\n");
       if (_SnapShot) {} else printf("%s","Something went wrong with SnapShot\n");
      
          _InitCamera();
         
          _SnapShot();
         
          _UnInitCamera();

        FreeLibrary(hInstLibrary);
     }  else
    printf("%s","Could not load DLL Functions , check if VideoDLL.dll is in the same folder\n");
     
    system("PAUSE");
    return 0;
}



Code Sample #2 (Dev-C++) :

//THIS CODE WILL START A  WINDOW , LOAD THE DLL FUNCTIONS AND OVERLAY THE VIDEO FEED USING COPYDC
#include <windows.h>
#include <cstdlib>
#include <iostream>  

using namespace std;
 
 
typedef void (*InitCamera)();
typedef void (*UnInitCamera)();
typedef void (*SnapShot)();

typedef void (*InitForSnap)();
typedef void (*FastSnap)();
typedef void (*UnInitForSnap)();

typedef void (*CopyDC)(HDC,unsigned int,unsigned int);

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "VideoTest";
    InitCamera _InitCamera;
    UnInitCamera _UnInitCamera;
    SnapShot _SnapShot;
    CopyDC _CopyDC;
    InitForSnap _InitForSnap;
    FastSnap _FastSnap;
    UnInitForSnap _UnInitForSnap;
    HDC dc2;
   
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    FreeConsole();    
    HINSTANCE hInstLibrary = LoadLibrary("VideoDLL.dll");
      if (!hInstLibrary) { MessageBox(0,"Could not find VideoDLL.dll","",0); return 0 ; }
     
     
       _InitCamera = (InitCamera)GetProcAddress(hInstLibrary, "InitCamera");
       _UnInitCamera = (UnInitCamera)GetProcAddress(hInstLibrary, "UnInitCamera");
       _SnapShot = (SnapShot)GetProcAddress(hInstLibrary, "SnapShot");
       _CopyDC = (CopyDC)GetProcAddress(hInstLibrary, "CopyDC");
       _InitForSnap = (InitForSnap)GetProcAddress(hInstLibrary, "InitForSnap");
       _FastSnap = (FastSnap)GetProcAddress(hInstLibrary, "FastSnap");
       _UnInitForSnap = (UnInitForSnap)GetProcAddress(hInstLibrary, "UnInitForSnap");
      
   
       if (_InitCamera) {} else MessageBox(0,"Something went wrong with InitCamera\n","",0);
       if (_UnInitCamera) {} else MessageBox(0,"Something went wrong with UnInitCamera\n","",0);
       if (_SnapShot) {} else MessageBox(0,"Something went wrong with SnapShot\n","",0);
      
      _InitCamera();
      _InitForSnap();   
                         
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "VideoTest",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    const int ID_TIMER = 1;
    unsigned int ret = SetTimer(hwnd, ID_TIMER, 70, NULL);
   
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }
    
     _UnInitForSnap();
     _UnInitCamera();
        
    KillTimer(hwnd, ID_TIMER);     
    FreeLibrary(hInstLibrary);
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


void DrawCamera2(HWND dctodraw)
{
     PAINTSTRUCT ps;
     _FastSnap();
     Sleep(5);
     HDC hdc = BeginPaint(dctodraw, &ps);
     _CopyDC(hdc,3,3); // Copies data to the hdc , starting at pt_x, pt_y
     EndPaint(dctodraw, &ps);
}


void DrawCamera(HWND dctodraw)
{
     _FastSnap(); 
     HDC hdc = GetDC(dctodraw);
     _CopyDC(hdc,3,3); // Copies data to the hdc , starting at pt_x, pt_y
     ReleaseDC(dctodraw, hdc);
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
   

    Sleep(5);
    
    switch (message)                  /* handle the messages */
    {
        case WM_TIMER:
        { DrawCamera(hwnd); }
         break;
        case WM_PAINT:
           {  DrawCamera(hwnd); }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}