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

영상처리 - 이경진

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

#include <windows.h>
#include "vfw.h"
#pragma comment(lib, "vfw32.lib")

#define BITMAP_MAXSIZE (1024*780*3)+10

BITMAPFILEHEADER * stpBFH;
BITMAPINFOHEADER * stpBIH;
HDC hdc;
unsigned char * BMbuf;
unsigned int uiX;
unsigned int uiPad;
unsigned int uiY;
  PAINTSTRUCT ps;
  HANDLE hFile;
  DWORD dwRead;unsigned char * ucpPixel;



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;

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;
}


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



  switch(iMessage)
  {
    case WM_CREATE:
      HWndMain = hWnd;
    
    //==========================Bitmap 영역 불러오기_START=============================//
    hFile=CreateFile(TEXT("smart.bmp"),GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    BMbuf=(unsigned char *)malloc(sizeof(BITMAP_MAXSIZE) );
    ReadFile(hFile, BMbuf, BITMAP_MAXSIZE, &dwRead, NULL);
   
    if(hFile == INVALID_HANDLE_VALUE)
      {
         MessageBox(hWnd,TEXT("smart.bmp 를 찾을수 없습니다"),TEXT("ERROR"),MB_OK);
         DestroyWindow(hWnd);
      }
    
     BMbuf = (unsigned char *)malloc(BITMAP_MAXSIZE);   
     if(BMbuf == 0)
      {
         MessageBox(hWnd,TEXT("동적할당 생성 실패"),TEXT("ERROR"),MB_OK);
         DestroyWindow(hWnd);
      }
     ReadFile(hFile,BMbuf,BITMAP_MAXSIZE,&dwRead,NULL);
     stpBFH = (BITMAPFILEHEADER *)BMbuf;
     stpBIH = ((BITMAPINFOHEADER *)((char *)BMbuf + sizeof(BITMAPFILEHEADER)) );
     CloseHandle(hFile);
    uiPad = uiX%4;
  
    //==========================Bitmap 영역 불러오기_END=============================//  
    //==========================Video 영역 불러오기_START=============================//
    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;
      capSetVideoFormat(vfw,&Bm,sizeof(Bm));
      capSetCallbackOnFrame(vfw, FramInfo);
      capPreviewRate(vfw, 1);
      capPreview(vfw, FALSE);
    return 0;
//==========================Video 영역 불러오기_END=============================//
    
    


    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);
//==========================Video 영역출력_START=============================//
// #1.
  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;
  //이미지 변환
    ucpPixel = BMbuf + stpBFH->bfOffBits;
  for(iCntY = 0; iCntY < Bm.bmiHeader.biHeight ; ++iCntY)  
  {        
    for(iCntX = 0; iCntX  < Bm.bmiHeader.biWidth  ; ++iCntX, Jump += 3 ,ucpPixel+=3)
    {
      if(lpData->lpData[Jump + 2> 100)
      {
        continue;
      }
      if(lpData->lpData[Jump + 1> 100)
      {
        continue;
      }
      if(lpData->lpData[Jump + 0> 50)
      {  
        lpData->lpData[Jump + 2]  = *(ucpPixel+2);  // Red
        lpData->lpData[Jump + 1]  = *(ucpPixel+1);// Green
        lpData->lpData[Jump + 0]  = *(ucpPixel+0); // Blue    
        
      }//END_if*3
      
    }//END_X_FOR
    ucpPixel = ucpPixel + uiPad;

  }//END_Y_FOR

  
//#2
  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