// Snapshot.cpp
//
// This is a sample application which connects to an Artemis camera
// and takes a snapshot, which is then displayed on the screen.
//
// The code is intended to show how the Artemis SDK can be used.
// It is provided 'as is', and can not be guaranteed to be correct
// or suitable for any particular purpose other than as stated above.
//
// You are free to modify this code and include it in your own applications.
//
#include "stdafx.h"
#include <stdio.h>
#include "resource.h"
#include "..\..\ArtemisHSCAPI.h"
#define MAX_LOADSTRING 100
#define ARTEMISDLLNAME "ArtemisHSC.dll"
// Global Variables:
HINSTANCE hInst; // current instance
HWND hWnd; // current window handle
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
ArtemisHandle hCam=NULL;
int ExposureDuration=100; // exposure time in milliseconds
int BlackLevel=0;
int WhiteLevel=20000;
unsigned short* pImgBuf=NULL; // image buffer
int nImgBuf=0; // allocated size of buffer
int imgW=0; // width of image
int imgH=0; // height of image
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void Connect();
void Disconnect();
void Exposure();
void Snapshot();
void DisplayImage(HDC hdc);
bool SaveBitmap(char *szFile);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SNAPSHOT, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SNAPSHOT);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SNAPSHOT);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_SNAPSHOT;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Load the Artemis DLL
// This must be done before calling any of the other Artemis API functions.
if (!ArtemisLoadDLL(ARTEMISDLLNAME))
{
MessageBox(hWnd, "Cannot load " ARTEMISDLLNAME, "Artemis", MB_OK);
return FALSE;
}
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWND, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
hWnd=hWND;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_FILE_SAVE:
SaveBitmap(".\\Snapshot.bmp");
break;
case IDM_CAMERA_CONNECT:
Connect();
break;
case IDM_CAMERA_DISCONNECT:
Disconnect();
break;
case IDM_CAMERA_EXPOSURE:
Exposure();
break;
case IDM_CAMERA_SNAPSHOT:
Snapshot();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
DisplayImage(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
// Unload the Artemis DLL
ArtemisUnLoadDLL();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// Message handler for Exposure dialog.
LRESULT CALLBACK ExposureDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
SetDlgItemInt(hDlg, IDC_EXPOSURE, ExposureDuration, FALSE);
SetDlgItemInt(hDlg, IDC_BLACK, BlackLevel, FALSE);
SetDlgItemInt(hDlg, IDC_WHITE, WhiteLevel, FALSE);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
if (LOWORD(wParam) == IDOK)
{
BOOL bOK=FALSE;
int v;
v=GetDlgItemInt(hDlg, IDC_EXPOSURE, &bOK, FALSE);
if (bOK) ExposureDuration=v;
v=GetDlgItemInt(hDlg, IDC_BLACK, &bOK, FALSE);
if (bOK) BlackLevel=v;
v=GetDlgItemInt(hDlg, IDC_WHITE, &bOK, FALSE);
if (bOK) WhiteLevel=v;
}
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
////////////////////////////////////////////
// Connect to first available Artemis device
void Connect()
{
// Check if we're already connected
if (hCam!=NULL)
{
MessageBox(hWnd, "Already connected!", "Artemis", MB_OK);
return;
}
// Now connect to the first available camera
hCam=ArtemisConnect(-1);
if (hCam==NULL)
{
MessageBox(hWnd, "No camera available", "Artemis", MB_OK);
return;
}
}
////////////////////////////////////////////
// Disconnect from Artemis device
void Disconnect()
{
// Only disconnect if we're currently connected!
if (hCam!=NULL)
{
ArtemisDisconnect(hCam);
hCam=NULL;
}
}
////////////////////////////////////////////
// Set exposure parameters
void Exposure()
{
DialogBox(hInst, (LPCTSTR)IDD_EXPOSURE, hWnd, (DLGPROC)ExposureDlg);
// update display in case black/white levels changed
InvalidateRect(hWnd, NULL, FALSE);
}
////////////////////////////////////////////
// Take a snapshot
void Snapshot()
{
// Ensure we're connected
if (hCam==NULL)
{
MessageBox(hWnd, "You must connect to the camera first.", "Artemis", MB_OK);
return;
}
// Don't switch amplifier off for short exposures
ArtemisSetAmplifierSwitched(hCam, ExposureDuration>2.5f);
// Start the exposure
ArtemisStartExposure(hCam, ExposureDuration*0.001f);
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_WAIT)));
// Wait until it's ready, or ESC is pressed.
// A more polished app would not use this method!
while(!ArtemisImageReady(hCam))
{
if (GetAsyncKeyState(VK_ESCAPE)&0x8000)
{
ArtemisAbortExposure(hCam);
return;
}
Sleep(100);
}
// We'll keep a local copy of the image so we can refresh the display even if
// the original copy is lost.
// Get dimensions of image
int x,y,wid,hgt,binx,biny;
ArtemisGetImageData(hCam, &x, &y, &wid, &hgt, &binx, &biny);
unsigned short* pimg=(unsigned short*)ArtemisImageBuffer(hCam);
if (pimg)
{
// make sure we have enough space to store the image
int imgsize=wid*hgt;
if (nImgBuf<imgsize)
{
delete[] pImgBuf;
nImgBuf=imgsize;
pImgBuf=new unsigned short[nImgBuf];
}
memcpy(pImgBuf, pimg, imgsize*2);
imgW=wid;
imgH=hgt;
InvalidateRect(hWnd, NULL, FALSE);
}
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
////////////////////////////////////////////
// If we have an image available, show it
void DisplayImage(HDC hdc)
{
static BITMAPINFO BmiMain; // data for bitmap used for refreshing display
static HBITMAP hBmpMain=NULL;
static int BufMainW;
static int BufMainH;
static int BufMainPitch;
static unsigned char *pBufMain=NULL;
RECT rt;
static RECT rectBuf={0,0,0,0};
GetClientRect(hWnd, &rt);
if (memcmp(&rt,&rectBuf,sizeof(RECT))!=0 || hBmpMain==NULL)
{
// window has changed size. Dispose of old bitmap and make a new one
if (hBmpMain!=NULL)
DeleteObject(hBmpMain);
hBmpMain=NULL;
rectBuf = rt;
BufMainW=rt.right-rt.left;
BufMainH=rt.bottom-rt.top;
BufMainPitch=(BufMainW*3+3)&~3;
BmiMain.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
BmiMain.bmiHeader.biWidth=BufMainW;
BmiMain.bmiHeader.biHeight=-BufMainH;
BmiMain.bmiHeader.biPlanes=1;
BmiMain.bmiHeader.biBitCount=24;
BmiMain.bmiHeader.biCompression=BI_RGB;
BmiMain.bmiHeader.biSizeImage=BufMainH*BufMainPitch;
BmiMain.bmiHeader.biXPelsPerMeter=0;
BmiMain.bmiHeader.biYPelsPerMeter=0;
BmiMain.bmiHeader.biClrUsed=0;
BmiMain.bmiHeader.biClrImportant=0;
hBmpMain=CreateDIBSection(hdc, &BmiMain, DIB_RGB_COLORS, (void **)&pBufMain, NULL, 0);
}
// First clear the buffer
memset(pBufMain, 0, BufMainH*BufMainPitch);
// clip to screen buffer
int w=imgW;
int h=imgH;
if (w>BufMainW)
w=BufMainW;
if (h>BufMainH)
h=BufMainH;
int offset=BlackLevel;
float scale=0.0f;
if (WhiteLevel!=BlackLevel)
scale=255.0f/(WhiteLevel-BlackLevel);
for (int y=0; y<h; y++)
{
unsigned short *pSrc=pImgBuf+(y*imgW);
unsigned char *pDst=pBufMain+(y*BufMainPitch);
for (int x=0; x<w; x++)
{
// single input channel (greyscale)
int v=*pSrc++;
// adjust according to black/white levels
v=(int)((v-offset)*scale);
if (v<0) v=0;
else if (v>255) v=255;
// three output channels (B,G,R)
*pDst++=v;
*pDst++=v;
*pDst++=v;
}
}
HDC hDIB;
hDIB=CreateCompatibleDC(hdc);
HBITMAP hbmpOld=(HBITMAP)SelectObject(hDIB,hBmpMain);
BitBlt(hdc, 0,0,BufMainW,BufMainH, hDIB, 0, 0, SRCCOPY);
SelectObject(hDIB, hbmpOld);
}
////////////////////////////////////////////////////
// Save currently displayed image as a BMP file.
// This is for info only - BMP is not the ideal format
// for storing images taken using a 16-bit camera!
bool SaveBitmap(char *szFile)
{
BITMAPFILEHEADER hdr;
BITMAPINFOHEADER bmi;
if (!pImgBuf)
return FALSE;
FILE *file = fopen(szFile, "wb");
if (file==NULL)
return FALSE;
int imgPitch=((3*imgW)+3)&~3; // round up to multiple of 4
bmi.biSize=sizeof(BITMAPINFOHEADER);
bmi.biWidth=imgW;
bmi.biHeight=imgH;
bmi.biPlanes=1;
bmi.biBitCount=24;
bmi.biCompression=BI_RGB;
bmi.biSizeImage=imgPitch*imgH;
bmi.biXPelsPerMeter=0;
bmi.biYPelsPerMeter=0;
bmi.biClrUsed=0;
bmi.biClrImportant=0;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = bmi.biSize + bmi.biSizeImage + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof(hdr) + bmi.biSize);
// Write the file header
fwrite(&hdr, sizeof(hdr), 1, file);
// Write the DIB header
fwrite(&bmi, bmi.biSize, 1, file);
// Write image data
int offset=BlackLevel;
float scale=0.0f;
if (WhiteLevel!=BlackLevel)
scale=255.0f/(WhiteLevel-BlackLevel);
char *pix = (char*)malloc(imgPitch);
for (int y=0; y<imgH; y++)
{
unsigned short *pSrc=pImgBuf+((imgH-y-1)*imgW);
char *pDest=pix;
for (int x=0; x<imgW; x++)
{
// single input channel (greyscale)
int v=*pSrc++;
// adjust according to black/white levels
v=(int)((v-offset)*scale);
if (v<0) v=0;
else if (v>255) v=255;
// three output channels (B,G,R)
*pDest++=v;
*pDest++=v;
*pDest++=v;
}
// pad to multiple of 4 bytes
int x = 0;
for (x*=3; x<imgPitch; x++)
*pDest++=0;
fwrite(pix, imgPitch, 1, file);
}
fclose(file);
free(pix);
return TRUE;
}