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

2014.10.02 업무일지 [출석번호 21 이재우]

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

 

6 C 동적할당 레퍼런스 변수 동적할당 구조체 클래스 객체.pdf

 

 

 

 

 

 

 

 

 

6 교시 일기예보 카메라 영상 기법 소스 해석 및 분석

소스원본 저자: 김대희 군의 소스를 가져 와서 분석했네요.    나름 가져와서 삽질 하느라 ㅠ..ㅠ 다른 모자라는 부분은 다른 분께 패스여.

[대칭화면 해석]20141007-main.c

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

#define BITMAP_MAXSIZE   (1024*768*3+10
)
#define XOFFSET   
280
#define
 X_WIDTH_SIZE   
100

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

HINSTANCE g_hInst;
HWND hWndMain;
HWND hVFW;
HWND Hwndmain;
HBITMAP hBit;
BITMAPINFO Bm;
LPCTSTR lpszClass
=TEXT("VFW"
);

HBITMAP hBit2;

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

   hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW |
 WS_VSCROLL,
      100,40,800,660
,
      NULL,(HMENU)NULL,hInstance,NULL);
   ShowWindow(hWnd,nCmdShow);

   
while (GetMessage(&Message,NULL,0,0
)) {
      TranslateMessage(&
Message);
      DispatchMessage(&
Message);
   }
   
return (int
)Message.wParam;
}

HDC MemDC;
PAINTSTRUCT ps;
HANDLE hFile;
static unsigned char
 * BMbuf;
static
 BITMAPFILEHEADER * stpBFH;
static
 BITMAPINFOHEADER * stpBIH;
static unsigned int
 uiX;
static unsigned int
 uiY;
static unsigned int
 uiPad;
static unsigned int
 uiXcount;
static unsigned int
 uiYcount;
static unsigned char
 * ucpPixel;
static
 DWORD dwRead;
HBITMAP OldBitmap;

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

   
switch
(iMessage)
   {
   
case
 WM_CREATE:

      Hwndmain =
 hWnd;
      Hdc  =
 GetDC(hWnd);
    
//[1] File Open
      hFile = CreateFile(TEXT("image1.bmp"
),
         GENERIC_READ, 0
, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
//[2] 동적할당
      BMbuf = (unsigned char
 *)malloc(BITMAP_MAXSIZE);
      if(0 == BMbuf)
//[3] 동적할당 실패시 에러 처리
      {
         MessageBox(hWnd, TEXT(
"동적할당을 받을 수 없습니다."), TEXT("오류"
), MB_OK);
         DestroyWindow(hWnd);
      }

      
if(INVALID_HANDLE_VALUE != hFile)
//[3]-1 File Open
      {
         ReadFile(hFile, BMbuf, BITMAP_MAXSIZE, 
&
dwRead, NULL);
         CloseHandle(hFile);
         
     
//[4] 저장된 데이터 비트맵 파일 헤더 포인터 설정 
     stpBFH =
 (BITMAPFILEHEADER *)BMbuf;
         stpBIH = (BITMAPINFOHEADER *)(BMbuf + sizeof
(BITMAPFILEHEADER));
     
     
//[5] 윈도우 창 정보 셋팅 가로 세로 크기 지정
     uiX = (unsigned int)(stpBIH->
biWidth);
         uiPad = uiX%4

         uiY = (unsigned int)(stpBIH->
biHeight);
     
     
//[6] MemDC 생성 및 백업  
         MemDC = CreateCompatibleDC(Hdc); 
//메모리에서 예비 출력할 영역 설정
     hBit2 = CreateCompatibleBitmap(Hdc, uiX, uiY * 2);
//비트맵 창 생성
         OldBitmap = (HBITMAP)SelectObject(MemDC, hBit2); 
//이전 창은 백업 후 새 창 선택

         
//[7] Bitmap 그리기 //시작---------------------------------------------
         ucpPixel = BMbuf + stpBFH->
bfOffBits;
         for(uiYcount = uiY; 0 <
 uiYcount; --uiYcount)
         {
            
for(uiXcount = uiX; 0 <
 uiXcount; --uiXcount)
            {
               SetPixel(MemDC, uiXcount-
1, uiYcount-1
,
                  RGB(*(ucpPixel+2), *(ucpPixel+1
), *ucpPixel));
               ucpPixel = ucpPixel + 3
;
            }
            ucpPixel 
=
 ucpPixel + uiPad;
         }

         SelectObject(MemDC, OldBitmap);
//이전 창 다시 복귀
         DeleteDC(MemDC);
         
      }
      
else  
//[3]-2 File Open  실패시 에러 처리
      {
         MessageBox(hWnd, TEXT(
"파일을 열 수 없습니다."), TEXT("오류"
), MB_OK);
         DestroyWindow(hWnd);
      }
    
//[8] 영상 윈도우 화면 생성
      hVFW = capCreateCaptureWindow(TEXT("VFW"
),
         WS_CHILD | WS_VISIBLE,0,0,320,240,hWnd,0
);
      capDriverConnect(hVFW, 0); 
//[9] 드라이버 연결
      capPreviewRate(hVFW, 1);  
//[10] 프레임 속도 설정(윈도우 핸들,갱신시간(ms))
      capPreview(hVFW, TRUE);
      capGetVideoFormat(hVFW, 
&Bm, sizeof(Bm));
//[11]기존의 설정값을 읽어옮

      capSetVideoFormat(hVFW, &Bm, sizeof(Bm));
//[12]바꾼 값을 다시 설정
      
    hBit 
=
 CreateCompatibleBitmap( Hdc, Bm.bmiHeader.biWidth, Bm.bmiHeader.biHeight);

      if (capSetCallbackOnFrame(hVFW, FramInfo) == FALSE)   
//CALLBACK 등록 OS에서 Call함.
      {
         
return
 FALSE;
      }

      ReleaseDC(hWnd, Hdc);
      
return 0
;
/*
   case WM_PAINT:
      Hdc 
= BeginPaint(hWnd, &
ps);
      MemDC =
 CreateCompatibleDC(Hdc);
      OldBitmap =
 (HBITMAP)SelectObject(MemDC, hBit2);

      BitBlt(Hdc, 0, 320, uiX, uiY, MemDC, 0, 0, SRCCOPY);

      SelectObject(MemDC, OldBitmap);
      DeleteDC(MemDC);
      EndPaint(hWnd, 
&
ps);
      return 0;
*/

   case WM_DESTROY:
      free(BMbuf);
      PostQuitMessage(
0
);
      return 0
;
   }
   
return
 (DefWindowProc(hWnd, iMessage, wParam, lParam));
}



LRESULT CALLBACK FramInfo(HWND hVFW, LPVIDEOHDR VideoHdr)
{
   HDC Hdc;
   HDC hMemDC;
   HBITMAP OldBitmap;
   
int
 xCnt, yCnt; 
   int Jump = 0
;

   Hdc =
 GetDC(Hwndmain);
   hMemDC =
 CreateCompatibleDC(Hdc);
   OldBitmap =
 (HBITMAP)SelectObject(hMemDC, hBit);

   ucpPixel = BMbuf + stpBFH->
bfOffBits;
   for(yCnt = Bm.bmiHeader.biHeight ;yCnt > 0
;--yCnt)
   {
      
//for(xCnt = 0;xCnt < Bm.bmiHeader.biWidth;++xCnt)
      for(xCnt = Bm.bmiHeader.biWidth-1;xCnt >= 0
 ;--xCnt)
      {
         
if(100-1 > VideoHdr->lpData[Jump+2] )    
//R  
         {
            
if(100-1 > VideoHdr->lpData[Jump+1])  
//G  
            {
               
if(100-1 > VideoHdr->lpData[Jump+0])  
//B  
               {
                  VideoHdr-
>lpData[Jump+2]=*(ucpPixel+2
); 
                  VideoHdr->lpData[Jump+1]=*(ucpPixel+1
); 
                  VideoHdr->lpData[Jump+0]=*(ucpPixel+0
);    
          
//회색에 가까운 색 변환(일기 예보등에 많이 사용)
               }
//B
            }
//G
         }
//R
         SetPixel(hMemDC,xCnt ,yCnt, RGB(VideoHdr->lpData[Jump+2],VideoHdr->lpData[Jump+1],VideoHdr->
lpData[Jump]) );
//     SetPixel(hMemDC,xCnt ,yCnt, RGB(VideoHdr->lpData[Jump+2],0,0) );// R(적색)
//     SetPixel(hMemDC,xCnt ,yCnt, RGB(0,VideoHdr->lpData[Jump+1],0) );// G(녹색)
//     SetPixel(hMemDC,xCnt ,yCnt, RGB(0,0,VideoHdr->lpData[Jump+0]) );// B(청색)
         Jump = Jump + 3
;
         ucpPixel = ucpPixel + 3
;
      }
      ucpPixel 
=
 ucpPixel + uiPad;
   }
   
   BitBlt( Hdc , 
320 , 0, Bm.bmiHeader.biWidth, Bm.bmiHeader.biHeight, hMemDC, 00
, SRCCOPY);
   SelectObject(hMemDC,OldBitmap);
   DeleteDC(hMemDC);
   ReleaseDC(Hwndmain, Hdc);
   
//MessageBox(NULL, TEXT("Fuck"), TEXT("u"), MB_OK);
   
//Sleep(1000);
   return 0
;
}

 

728x90