본문 바로가기
코스웨어/14년 스마트컨트롤러

영상처리

by 알 수 없는 사용자 2014. 10. 8.
728x90
반응형

#include <windows.h>

#include "vfw.h"

#pragma comment(lib, "vfw32.lib")

#define BITMAP_MAXSIZE  ((3 * 1024 * 768) + 100)


LRESULT CALLBACK  WndProc(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK  FramInfo(HWND, LPVIDEOHDR);

HINSTANCE      g_hInst;

HWND        HWndMain;

LPCTSTR        lpszClass=TEXT("WiseCat");

HWND        vfw;

BITMAPINFO      bm;

HBITMAP   hbit;


static BITMAPFILEHEADER * stpBFH; // FILE_HEADER

static BITMAPINFOHEADER * stpBIH; // INFO_HEADER

static HANDLE hFile;        


static unsigned int uiPad;  // Bitmap 4의 배수를 맞추는 변수

static unsigned int uiX;  // Bitmap의 가로사이즈

static unsigned int uiY;  // Bitmap의 세로사이즈


unsigned char * ucPixel;    // RGB code(비트맵그림정보를 담는 변수)

unsigned char * ucBitBuffer;  // Buffer(동적할당 메모리 공간)


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance

           ,LPSTR lpszCmdParam,int nCmdShow)

{

  HWND hWnd;

  MSG Message;

  WNDCLASS WndClass;

  g_hInst = hInstance;


  WndClass.cbClsExtra=0;

  WndClass.cbWndExtra=0;

  WndClass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);

  WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);

  WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

  WndClass.hInstance=hInstance;

  WndClass.lpfnWndProc=WndProc;

  WndClass.lpszClassName=lpszClass;

  WndClass.lpszMenuName=NULL;

  WndClass.style=CS_HREDRAW | CS_VREDRAW;

  RegisterClass(&WndClass);


  hWnd=CreateWindow(  lpszClass

            ,lpszClass

            ,WS_OVERLAPPEDWINDOW

            ,CW_USEDEFAULT

            ,CW_USEDEFAULT

            ,CW_USEDEFAULT,CW_USEDEFAULT

            ,NULL

            ,(HMENU)NULL

            ,hInstance

            ,NULL);        

  ShowWindow(hWnd,nCmdShow);


  while(GetMessage(&Message,NULL,0,0))

  {

    TranslateMessage(&Message);

    DispatchMessage(&Message);

  }


  return (int)Message.wParam;

}


HDC hdc;


LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)

{

  PAINTSTRUCT ps;

  DWORD dwReadSize;


  switch(iMessage)

  {

    case WM_CREATE:

      HWndMain = hWnd;


      ucBitBuffer = (unsigned char *)malloc(BITMAP_MAXSIZE);


      if(ucBitBuffer == 0)

      {

        MessageBox(hWnd, TEXT("동적할당 X"), TEXT("sorry"), MB_OK);

        DestroyWindow(hWnd);

      }


      hFile = CreateFile(TEXT("image.bmp"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);


      if(hFile == INVALID_HANDLE_VALUE)

      {

        MessageBox(hWnd, TEXT("Image X"), TEXT("sorry"), MB_OK);

        DestroyWindow(hWnd);

      }


      ReadFile(hFile, ucBitBuffer, BITMAP_MAXSIZE, &dwReadSize, NULL);

      CloseHandle(hFile);


      stpBFH = (BITMAPFILEHEADER *)ucBitBuffer; // File헤더구간

      stpBIH = (BITMAPINFOHEADER *)(ucBitBuffer + sizeof(BITMAPFILEHEADER)); // Info헤더구간 - Bitmap정보


      uiX = stpBIH -> biWidth;

      uiPad = uiX % 4;


      vfw = capCreateCaptureWindow(  TEXT("CAM")

                      ,WS_CHILD | WS_VISIBLE

                      ,0

                      ,0

                      ,400

                      ,300

                      ,hWnd

                      ,NULL);


      capDriverConnect(vfw,0);

      capGetVideoFormat(vfw,&bm,sizeof(bm));

      bm.bmiHeader.biWidth  = 320;

      bm.bmiHeader.biHeight = 240;


      // 비트맵 포맷정보를 바탕으로 비트맵 핸들을 생성하여 저장

      hbit = CreateCompatibleBitmap(hdc, bm.bmiHeader.biWidth, bm.bmiHeader.biHeight);


      capSetVideoFormat(vfw,&bm,sizeof(bm));

      //capDlgVideoFormat(m_capwnd); 

      capSetCallbackOnFrame(vfw, FramInfo);

      capPreviewRate(vfw, 1);

      capPreview(vfw, FALSE);

      return 0;


    case WM_PAINT:

      hdc = BeginPaint(hWnd, &ps);

      EndPaint(hWnd,&ps);

      return 0;


    case WM_DESTROY:

      PostQuitMessage(0);

      return 0;

  }

  return(DefWindowProc(hWnd,iMessage,wParam,lParam));

}


LRESULT CALLBACK FramInfo(HWND hWnd, LPVIDEOHDR lpData)

{

  static int iCntX;

  static int iCntY;

  static int Jump;

  hdc = GetDC(HWndMain);

  StretchDIBits(hdc  , 0

            , 0

            , bm.bmiHeader.biWidth

            , bm.bmiHeader.biHeight

            , 0

            , 0

            , bm.bmiHeader.biWidth

            , bm.bmiHeader.biHeight

            , lpData->lpData

            , &bm

            , DIB_RGB_COLORS

            , SRCCOPY);

  Jump = 0;


  ucPixel = ucBitBuffer + stpBFH -> bfOffBits;

  for(iCntY = 0; iCntY < bm.bmiHeader.biHeight ; ++iCntY)  

  {        

    for(iCntX = 0; iCntX  < bm.bmiHeader.biWidth  ; ++iCntX, Jump += 3, ucPixel += 3 )

    {

     if(lpData->lpData[Jump + 2] > 100)

      {

        continue;

      }

      if(lpData->lpData[Jump + 1] > 100)

      {

        continue;

      }

      if(lpData->lpData[Jump + 0] > 50)

      {

        lpData->lpData[Jump]    = *(ucPixel + 0);  // Blue

        lpData->lpData[Jump + 2]  = *(ucPixel + 2);  // Red

        lpData->lpData[Jump + 1]  = *(ucPixel + 1);  // Green

      }

    }

    ucPixel = ucPixel + uiPad;

  }



  StretchDIBits(hdc  , bm.bmiHeader.biWidth +20

            , 0

            , bm.bmiHeader.biWidth

            , bm.bmiHeader.biHeight

            , 0

            , 0

            , bm.bmiHeader.biWidth

            , bm.bmiHeader.biHeight

            , lpData->lpData

            , &bm

            , DIB_RGB_COLORS

            , SRCCOPY);


  ReleaseDC(HWndMain, hdc);


  return 0;

}

728x90