Move the sources to trunk
[opencv] / apps / VMDemo / VirtualImage.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/// VirtualImage.cpp : implementation file
41 //
42
43 #include "stdafx.h"
44 #include "VMDemo.h"
45 #include "VirtualImage.h"
46 #include "raster.h"
47 #include <math.h>
48
49 #ifdef _DEBUG
50 #define new DEBUG_NEW
51 #undef THIS_FILE
52 static char THIS_FILE[] = __FILE__;
53 #endif
54
55 /////////////////////////////////////////////////////////////////////////////
56 // CVirtualImage
57
58 CVirtualImage::CVirtualImage()
59 //      : CDialog(CVirtualImage::IDD, NULL)
60 {
61     HoughLines = 0;
62     numHoughLines = 0;
63         rasterImage = 0;
64     showScanlines = false;
65 }
66
67 /*======================================================================================*/
68
69 CVirtualImage::~CVirtualImage()
70 {
71 }
72
73 /*======================================================================================*/
74
75 BEGIN_MESSAGE_MAP(CVirtualImage, CWnd)
76     //{{AFX_MSG_MAP(CVirtualImage)
77     ON_WM_PAINT()
78     ON_WM_SIZE()
79     //}}AFX_MSG_MAP
80 END_MESSAGE_MAP()
81
82
83 /////////////////////////////////////////////////////////////////////////////
84 // CVirtualImage message handlers
85
86 void CVirtualImage::OnPaint() 
87 {
88     CPaintDC dc(this); // device context for painting
89
90     RECT rect;
91
92     GetClientRect(&rect);
93
94     SIZE    size;
95     int     n;
96     int     x1,y1,x2,y2;
97     size.cx = rect.right    - rect.left;
98     size.cy = rect.bottom   - rect.top;
99
100     if (rasterImage != 0 ) {
101         if (rasterImage->GetImage() != 0)
102             rasterImage->Draw(&dc,size);
103         else {
104             dc.Rectangle(&rect);
105         }
106     }
107     else {
108         dc.Rectangle(&rect);
109     }
110
111     if (*numScanlines>0 && showScanlines ) {
112
113         float wf = (float)(rasterImage->GetWidth()-1);
114         float hf = (float)(rasterImage->GetHeight()-1);
115
116         for (n = 0; n < *numScanlines; n +=10) {
117             int k = n/10;
118             int r,g,b;
119             b = (((k%7)+1)&1)*255;
120             g = ((((k%7)+1)&2)>>1)*255;
121             r = ((((k%7)+1)&4)>>2)*255;
122             
123
124             CPen pen(PS_SOLID,1,RGB(r,g,b));
125             dc.SelectObject(&pen);
126
127             x1 = (*Scanlines)[n * 4    ];
128             y1 = (*Scanlines)[n * 4 + 1];
129             x2 = (*Scanlines)[n * 4 + 2];
130             y2 = (*Scanlines)[n * 4 + 3];
131
132             x1 = (int)(x1*size.cx/wf);
133             y1 = (int)(y1*size.cy/hf);
134             x2 = (int)(x2*size.cx/wf);
135             y2 = (int)(y2*size.cy/hf);
136
137             dc.MoveTo(x1,y1);
138             dc.LineTo(x2,y2);
139         }
140     }
141
142     if (HoughLines!=0) {
143         int n;
144         int w,h;
145         int xp[2],yp[2];
146         float wf,hf;
147         float a,b,c;
148         float x,y;
149         int currPoint;
150         currPoint = 0;
151         for(n = 0; n < *numHoughLines; n++) {
152
153             currPoint = 0;
154             w = rasterImage->GetWidth();
155             h = rasterImage->GetHeight();
156
157             a = (float)(cos((*HoughLines)[n*2+1]));
158             b = (float)(sin((*HoughLines)[n*2+1]));
159             c = (float)((*HoughLines)[n*2]);
160
161
162             wf = (float)(w-1);
163             hf = (float)(h-1);
164             
165             /* 1,3 */
166             if (b != 0) {
167
168                 /* 1 */
169                 x = 0;
170                 y = (c/b);
171                 if (y>=0 && y<=wf) {
172                     xp[currPoint] = (int)x;
173                     yp[currPoint] = (int)y;
174                     currPoint++;
175                 }
176
177                 /* 3 */
178                 x = wf;
179                 y=(c-a*wf)/b;
180                 if (y>=0 && y<=wf) {
181                     if (currPoint==0) {
182                         xp[currPoint] = (int)x;
183                         yp[currPoint] = (int)y;
184                         currPoint++;
185                     } else {
186                         if (x!=xp[0] || y != yp[0] ) {
187                             xp[currPoint] = (int)x;
188                             yp[currPoint] = (int)y;
189                             currPoint++;
190                         }
191                     }
192                 }
193             }
194
195             /* 2,4 */
196             if (a != 0) {
197
198                 /* 2 */
199                 y = 0;
200                 x = (c/a);
201                 if (x>=0 && x<=hf && currPoint < 2) {
202                     if (currPoint==0) {
203                         xp[currPoint] = (int)x;
204                         yp[currPoint] = (int)y;
205                         currPoint++;
206                     } else {
207                         if (x!=xp[0] || y != yp[0] ) {
208                             xp[currPoint] = (int)x;
209                             yp[currPoint] = (int)y;
210                             currPoint++;
211                         }
212                     }
213                 }
214
215                 /* 4 */
216                 y = hf;
217                 x=(c-b*hf)/a;
218                 if (x>=0 && x<=hf && currPoint < 2) {
219                     if (currPoint==0) {
220                         xp[currPoint] = (int)x;
221                         yp[currPoint] = (int)y;
222                         currPoint++;
223                     } else {
224                         if (x!=xp[0] || y != yp[0] ) {
225                             xp[currPoint] = (int)x;
226                             yp[currPoint] = (int)y;
227                             currPoint++;
228                         }
229                     }
230                 }
231             }
232
233             if (currPoint == 2) {
234                 x1 = (int)(xp[0]*size.cx/wf);
235                 y1 = (int)(yp[0]*size.cy/hf);
236                 x2 = (int)(xp[1]*size.cx/wf);
237                 y2 = (int)(yp[1]*size.cy/hf);
238                 dc.MoveTo(x1,y1);
239                 dc.LineTo(x2,y2);
240
241             }
242         }
243     }
244     return;
245
246 }
247
248
249 /*======================================================================================*/
250
251 void CVirtualImage::OnSize(UINT nType, int cx, int cy) 
252 {
253     CWnd::OnSize(nType, cx, cy);
254
255     Invalidate();
256 }
257
258
259 /*======================================================================================*/
260
261 int CVirtualImage::SetRasterImage(CRaster *newRasterImage)
262 {
263     rasterImage = newRasterImage;
264     Invalidate();
265     return 0;
266 }
267
268 /*======================================================================================*/
269
270 void CVirtualImage::SetScanlines(int **scan,int *numScan)
271 {
272     Scanlines       = scan;
273     numScanlines    = numScan;
274 }