ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 20141002 (C++ scope, namespace, bool, reference, new, delete, 비디오 처리 소스만)
    부산IT학원/스마트컨트롤러 2014. 10. 6. 10:34

    151일차










    ---------

    C++

    ---------




    ------- line

    inline의 단점은 프로그램의 용량이 증가한다.

    간단하고 자주 호출되는 함수에만 사용할 것.





    ------- scope 연산자

    std::cout      여기서 :: 을 scope라고 부른다.


    활용 예)


    include <iostream>


    int num = 100;


    int main()

    {

        int num = 200;


        std::cout << num << std::endl;

        std::cout << ::num << std::endl;


        return 0;

    }


    결과

      200  

      100  



    변수 앞에 :: 을 붙이면 전역 변수에 접근할 수 있다.






    ------- namespace
    지금까지 std::cout 앞에 std를 붙이는게 제법 귀찮다.

    include 밑에 다음과 같은 명령을 추가한다.


    using namespace std;


    using : 사용하겠다

    namespace : 이름 공간

    std : std를


    라는 뜻 정도로 보면 될 것 같다.

    이 명령을 추가하고 나면 이제 cout과 cin,  endl 앞에는 std:: 이 생략 가능하다.


    예)

    include <iostream>


    using namespace std;


    int main()

    {

        int num;


        cout << "숫자 입력 : ";

        cin >> num;


        cout << "입력 하신 숫자는 " << num << "입니다." << endl;


        return 0;

    }






    ------- bool

    C에서는 없던 bool 이라는 데이터형이 하나 추가되었다.

    bool은 false, true 두 가지만 있다.

    false  :  0

    true   :  1

    (winapi에서는 BOOL, TRUE, FALSE 대문자로 주의 할 것.)







    ------- Reference

    C에서 변수 'A'를 가리킬 때 포인터를 사용하였다.


    int a = 10;

    int * p = &a;


    C++에서는 reference가 추가 되었다.


    int a = 10;

    int & r = a;


    포인터는 a(변수)의 주소를 가리킨 반면,

    레퍼런스는 a(변수)의 또 다른 이름이다.


    말 그대로 포인터는 4 byte형 메모리를 더 생성하였고,

    레퍼런스는 a(변수)에 또 다른 이름만 붙었으니 메모리는 그대로다.





    --- 특징

    - 보통 함수 인자로 사용

    - 변수 선언시 초기화를 해줘야만 사용 가능


    --- 따라서 C++에서는 함수 호출 방식이 3가지다.

    - Call by Value                   test(int);

    - Call by Address               test(int *);

    - Call by Reference             test(int &);







    ------- new, delete

    C에서 malloc, free와 같은 기능


    예제)

    #include <iostream>

    #include <string.h>

    using namespace std;


    char * MakeStrAdr(int len)

    {

    // char * str = (char*)malloc(sizeof(char)*len);

    char * str = new char[len];

    return str;

    }


    int main(void)

    {

    char * str = MakeStrAdr(20);

    strcpy(str, "I am so happy~");

    cout << str << endl;

    // free(str);

    delete []str;

    return 0;

    }



    배열은 delete 할때, delete와 변수 사이에 배열이라는 표시로 []를 넣어 줘야한다.











    ----------------------

    비디오 처리

    ----------------------




    #include <windows.h>

    #include <Vfw.h>

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


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

    HINSTANCE g_hInst;

    HWND hWndMain;

    LPCTSTR lpszClass=TEXT("Class");

    //WinMain 시작


    //windows 그리기//시작----------------------------------------------------

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance

          ,LPSTR lpszCmdParam,int nCmdShow)

    {

    HWND hWnd;

    MSG Message;

    WNDCLASS WndClass;

    g_hInst=hInstance;

    //1. 윈도우 속성값 등록

    WndClass.cbClsExtra=0;

    WndClass.cbWndExtra=0;

    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

    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);  //주소에 Write

     

    //2. 윈도우 생성

    hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,

     CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,

     NULL, (HMENU)NULL,hInstance,NULL);

    ShowWindow(hWnd,nCmdShow); //화면에 뿌려줌

     

    //3. 메시지 처리(무한 반복)

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

    {

     TranslateMessage(&Message);

     DispatchMessage(&Message);

    }

    return (int)Message.wParam;

    }

    //windows 그리기//끝------------------------------------------------------


    LRESULT CALLBACK FramInfo(HWND hVFW, LPVIDEOHDR VideoHdr);

    HBITMAP hBit;

    BITMAPINFO Bm;


    //메시지 처리//시작--------------------------------------------------------

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

    {

    HDC hdc;

    HWND hVFW;

    PAINTSTRUCT ps;


    switch(iMessage)

    {

    case WM_CREATE:

     hWndMain=hWnd;

     hdc=GetDC(hWndMain);

     hVFW=capCreateCaptureWindow(TEXT("VFW"),WS_CHILD | WS_VISIBLE , 0,0,320,240,hWnd,0);

     capDriverConnect(hVFW,0);

     capPreviewRate(hVFW,10);

     capGetVideoFormat(hVFW,&Bm, sizeof(Bm));

     Bm.bmiHeader.biWidth = 320;

     Bm.bmiHeader.biHeight = 240;

     capSetVideoFormat(hVFW, &Bm, sizeof(Bm));

     capPreview(hVFW,TRUE);


     hBit = CreateCompatibleBitmap(hdc, Bm.bmiHeader.biWidth,Bm.bmiHeader.biHeight);


     if (capSetCallbackOnFrame(hVFW,FramInfo) == FALSE)  // FramInfo를 Callback 함수 등록

     {

    return FALSE;

     }

     ReleaseDC(hWndMain, hdc);

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

    }

    //메시지 처리//끝----------------------------------------------------------


    //비디오 처리 callback 함수 //시작-------------------------------------------

    LRESULT CALLBACK FramInfo(HWND hVFW, LPVIDEOHDR VideoHdr)

    {

    HDC hdc;

    HDC hMemDC;

    HBITMAP OldBitmap;

    int iCntX;

    int iCntY;

    int Jump;

     

    hdc = GetDC(hWndMain);

    hMemDC = CreateCompatibleDC(hdc);

    OldBitmap = (HBITMAP)SelectObject(hMemDC, hBit);

     

             // VideoHdr에 들어있는 영상 정보 출력 // 시작-----------------------------------------

    Jump=0;

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

    {

     //for (iCntX=0 ; iCntX < Bm.bmiHeader.biWidth ; ++iCntX)

     for (iCntX=Bm.bmiHeader.biWidth-1 ; 0 <= iCntX ; --iCntX)

     {

    // 특정 색 전환//시작----------------------------------------------------------------------

     if(VideoHdr->lpData[Jump+2] < 70 && VideoHdr->lpData[Jump+2] > 40)

     {

      if(VideoHdr->lpData[Jump+1] < 110 && VideoHdr->lpData[Jump+1] > 70)

      {

       if(VideoHdr->lpData[Jump+0] < 110 && VideoHdr->lpData[Jump+0] > 70)

    {

    VideoHdr->lpData[Jump+2] = 0;

    VideoHdr->lpData[Jump+1] = 0;

    VideoHdr->lpData[Jump] = 0;

    }

      }

     }

    // 특정 색 전환//끝------------------------------------------------------------------------


     Jump = Jump + 3;   // 다음 픽셀로

     }

    }

             // VideoHdr에 들어있는 영상 정보 출력 // 끝-------------------------------------------


    BitBlt(hdc, 320,0,Bm.bmiHeader.biWidth,Bm.bmiHeader.biHeight,hMemDC,0,0,SRCCOPY);

    SelectObject(hMemDC,OldBitmap);

    ReleaseDC(hWndMain,hdc);


    return 0;

    }

    //비디오 처리 callback 함수 //끝---------------------------------------------










Designed by Tistory.