본문 바로가기
코스웨어/10년 시스템제어

[시스템제어] 6월 9일 업무일지_1 20번 이창민

by 알 수 없는 사용자 2010. 6. 11.
728x90
반응형
 < 영상처리>

 비트맵파일을 읽은 후 출력,  메모리 할당 받은 후 출력 및 180도 회전하여 출력하기.
 - 소스만 먼저 올립니다. -
1  
2 #include<Windows.h>
3 #include"vfw.h"
4 #include <math.h>
5 #include <iostream>
6  
7  
8 #pragma comment(lib, "vfw32.lib"//vfw32 라이브러리 포함
9  
10 /**********  4의 배수로 맞춰주기 위한 변수 선언 **************************************/
11 #define WIDTH4(width) ((width+3)>>2)<<2  
12  
13 #define ID_BCAPTURE 101
14  
15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
16 LRESULT CALLBACK FramInfo(HWND, LPVIDEOHDR); // 콜백 함수
17  
18 TCHAR str[10];    //정수데이터값 확인을 위한 버퍼
19 unsigned char Menuflag;  //인터페이스 선택 
20  
21 HINSTANCE g_hInst;
22 HWND hWndMain;
23 HWND hVFW;
24 HWND Hwndmain;
25 HBITMAP hBit;
26 BITMAPINFO Bm;    //비트맵 정보를 가짐
27  
28 //영상 메모리
29 int Action;
30  
31 LPCTSTR lpszClass = TEXT("VFW 기본 예제");
32  
33 BYTE *imageData;
34 BITMAPFILEHEADER bmfh;
35 BITMAPINFO *binfo;
36 BYTE *cpyData;
37 BYTE *reData;
38  
39  
40 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
41                      ,LPSTR lpszCndParam, int nCmdShow)
42 {
43     HWND hWnd;
44     MSG Message;
45     WNDCLASS WndClass;
46     g_hInst = hInstance;
47     
48     WndClass.cbClsExtra = 0;
49     WndClass.cbWndExtra = 0;
50     WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
51     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
52     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
53     WndClass.hInstance = hInstance;
54     WndClass.lpfnWndProc = WndProc;
55     WndClass.lpszClassName = lpszClass;
56     WndClass.lpszMenuName = NULL;
57     WndClass.style = CS_HREDRAW|CS_VREDRAW;
58     
59     RegisterClass(&WndClass);
60     
61     //영상을 보여줄 윈도우 생성
62     hWnd = CreateWindow(lpszClass, lpszClass,WS_OVERLAPPEDWINDOW|WS_CAPTION,200,200
63         300300, NULL, (HMENU)NULL, hInstance, NULL); 
64     
65     hWndMain = hWnd;
66     ShowWindow(hWnd, SW_SHOW);
67     
68     while(GetMessage(&Message, 0,0,0))
69     {
70         TranslateMessage(&Message);
71         DispatchMessage(&Message);
72     }
73     return (int)Message.wParam;
74     
75 }
76  
77  
78  
79 LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
80 {
81     HDC Hdc;
82     FILE *file;
83     LONG width, height;
84     int count=0;
85   int width4;
86     switch(iMessage)
87     {
88     case WM_CREATE:
89     {
90         
91         Hwndmain = hWnd;
92         
93         Hdc = GetDC(hWnd);
94         
95         file=fopen("5.bmp","rb");
96     
97   
98  
99         if(file==NULL)
100             return 0;
101         
102         
103         //BITMAPINFO 
104         binfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER)+(sizeof(RGBQUAD)*256));
105         //초기화
106         memset(binfo,0,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
107         //이미지 데이터가 보관될 메모리 할당
108         
109         
110         //비트맵 파일 헤더 읽음 
111         fread(&bmfh,sizeof(BITMAPFILEHEADER),1,file);
112         //비트맵 인포 헤더를 읽음
113         fread(&(binfo->bmiHeader),sizeof(BITMAPINFOHEADER),1,file);
114         
115         imageData = (BYTE*)malloc(binfo->bmiHeader.biSizeImage);
116         //이미지 데이터를 읽음
117         fread(imageData,sizeof(BYTE),binfo->bmiHeader.biSizeImage,file);
118         
119         fclose(file);
120     
121         /**************메모리 동적할당 하여 비트맵 복사 ******************************/
122  
123     cpyData = (BYTE*)malloc(binfo->bmiHeader.biSizeImage);   // 메모리 동적할당. 
124     width4=binfo->bmiHeader.biWidth*3;    
125     //width4=WIDTH4(width4);                    //4의 배수로 비트맵 크기를 맞추는 작업.
126     for(height=0; height<binfo->bmiHeader.biHeight;height++)
127     {
128       for(width=0; width<binfo->bmiHeader.biWidth;width++)
129       {
130         cpyData[(height*width4)+width*3]=
131           imageData[(height*width4)+width*3];
132  
133         cpyData[(height*width4)+width*3+1]=
134           imageData[(height*width4)+width*3+1];
135         
136         cpyData[(height*width4)+width*3+2]=
137           imageData[(height*width4)+width*3+2];
138       }
139     }
140     
141     /*********************비트맵 정보 180도 회전하여 복사*********************************/
142  
143     reData = (BYTE*)malloc(binfo->bmiHeader.biSizeImage);    
144     for(height=0; height<binfo->bmiHeader.biHeight;height++)
145     {
146       for(width=0; width<binfo->bmiHeader.biWidth;width++)
147       {
148         reData[(height*width4)+width*3]=
149           imageData[((binfo->bmiHeader.biHeight-height-1)*width4)
150                      +(binfo->bmiHeader.biWidth-width-1)*3];
151         reData[(height*width4)+width*3+1]=
152           imageData[((binfo->bmiHeader.biHeight-height-1)*width4)
153                      +(binfo->bmiHeader.biWidth-width-1)*3+1];
154         reData[(height*width4)+width*3+2]=
155           imageData[((binfo->bmiHeader.biHeight-height-1)*width4)
156                      +(binfo->bmiHeader.biWidth-width-1)*3+2];
157       }
158     }
159     /******************************************************************************************/
160         
161         return 0;
162     }
163     case WM_COMMAND: 
164         return 0;
165     case WM_LBUTTONDOWN:{
166         
167         width = (unsigned int)binfo->bmiHeader.biWidth;
168         height = (unsigned int)binfo->bmiHeader.biHeight;
169         
170         
171         Hdc = GetDC(hWnd);
172         
173         MoveWindow(hWndMain,100,100,width*3,height+35,true);
174         
175         SetDIBitsToDevice(Hdc,0,0,width,height,
176             NULL,NULL,NULL,height,imageData,
177             binfo,DIB_RGB_COLORS);
178         if(cpyData!=NULL){
179             SetDIBitsToDevice(Hdc,width,0,width,height,
180                 NULL,NULL,NULL,height,cpyData,
181                 binfo,DIB_RGB_COLORS);
182         }
183     if(reData!=NULL){
184             SetDIBitsToDevice(Hdc,width*2,0,width,height,
185                 NULL,NULL,NULL,height,reData,
186                 binfo,DIB_RGB_COLORS);
187         }
188         ReleaseDC(hWnd,Hdc);
189         return 0;}
190     case WM_DESTROY:
191     if(cpyData!=NULL){
192       free(cpyData);
193       cpyData=NULL;
194     }  
195     if(imageData!=NULL){
196       free(imageData);
197       imageData =NULL;
198     }
199     free(binfo);
200     free(cpyData);
201     free(reData);
202     PostQuitMessage(0);
203     return 0;
204     }
205     return (DefWindowProc(hWnd,iMessage,wParam,lParam));
206 }
207  

728x90