Update the changelog
[opencv] / apps / VMDemo / Raster.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*/// RasterDoc.cpp : implementation file
41 //
42
43 #include "stdafx.h"
44 #include "Raster.h"
45
46  
47 /////////////////////////////////////////////////////////////////////////////
48 // CRaster
49
50 /*======================================================================================*/
51
52 CRaster::CRaster()
53 {
54     imgDataSave     = NULL;
55     imgData         = NULL;
56     imgInfo         = NULL;
57     pal             = NULL;
58
59     XDest           = 0;
60     YDest           = 0;
61     DestWidth       = 0;
62     DestHeight      = 0;
63     XSrc            = 0;
64     YSrc            = 0;
65     SrcWidth        = 0;
66     SrcHeight       = 0;
67
68     fileName        = "Default.bmp";
69     flag            = 0;
70 }/* CRaster::CRaster */
71
72
73 /*======================================================================================*/
74
75 CRaster::~CRaster()
76 {
77     DeleteAllData();
78 }/* CRaster::~CRaster */
79
80
81 /*======================================================================================*/
82
83 int CRaster::CreateRaster(DWORD width, DWORD height, int bitPerPixel)
84 {
85     DWORD               numColor;
86     LOGPALETTE          *logPal;
87     switch(bitPerPixel) {
88     case 1:
89         shiftPixel  = (width + 7) / 8;
90         break;
91     case 4:
92         shiftPixel  = (width + 1) / 2;
93         break;
94     case 8:
95         shiftPixel  = width;
96         break;
97     case 24:
98         shiftPixel  = width * 3;
99         break;
100     default:
101         return -1;
102     }/* switch */
103
104     if(flag) {
105         DeleteAllData();
106     }/* if */
107
108     numColor = 1 << bitPerPixel;
109
110     if( bitPerPixel == 1 ||
111         bitPerPixel == 4 ||
112         bitPerPixel == 8) { // type of bitmap
113
114         imgInfo = (BITMAPINFO*)malloc(  sizeof(BITMAPINFO) 
115                                         + sizeof(RGBQUAD) * numColor);
116     // Create Palette for 1<<bitCount colors
117         pal     = new CPalette();
118         logPal  = (LOGPALETTE*)calloc(  1, sizeof(LOGPALETTE) 
119                                         + sizeof(PALETTEENTRY) * numColor);
120         logPal->palVersion      = 0x300;
121         logPal->palNumEntries   = (WORD)numColor;
122
123         for(DWORD Index = 0; Index < numColor; Index++) {
124             logPal->palPalEntry[Index].peRed    = (BYTE)Index;
125             logPal->palPalEntry[Index].peGreen  = (BYTE)Index;
126             logPal->palPalEntry[Index].peBlue   = (BYTE)Index;
127         }/* for */
128
129         pal->CreatePalette(logPal);
130         free(logPal);
131     } else
132         if( bitPerPixel == 24 ) {
133             imgInfo = (BITMAPINFO*)malloc(  sizeof(BITMAPINFO) 
134                                             + sizeof(RGBQUAD));
135     }/* if */
136
137     sizeString  = (shiftPixel + 3) / 4 * 4; // Attention !!! divided 4 !!!;
138
139     imgInfo = (BITMAPINFO*)calloc(  sizeof(BITMAPINFO) + sizeof(RGBQUAD), 1);
140     imgInfo->bmiHeader.biSize           = sizeof(BITMAPINFOHEADER);
141     imgInfo->bmiHeader.biWidth          = width;
142     imgInfo->bmiHeader.biHeight         = -(int)(height);
143     imgInfo->bmiHeader.biPlanes         = 1;
144     imgInfo->bmiHeader.biBitCount       = WORD(bitPerPixel);
145     imgInfo->bmiHeader.biCompression    = BI_RGB;
146     imgInfo->bmiHeader.biSizeImage      = sizeString*height;
147
148     imgDataSave     = new BYTE[sizeString*height];
149     imgData         = new BYTE[sizeString*height];
150
151     return 1;
152 }/* CRaster::CreateRaster */
153
154
155 /*======================================================================================*/
156
157 int CRaster::UpdateImage(BYTE* imgSrc)
158 {
159     BYTE* imgDst = imgData;
160     if(!imgSrc){
161         imgSrc = imgDataSave;
162     }/* if */
163
164     for(DWORD index = 0; index < (DWORD)abs(imgInfo->bmiHeader.biHeight); index++){
165         memcpy( imgDst, imgSrc, shiftPixel);
166         imgDst  +=  sizeString;
167         imgSrc  +=  sizeString;
168     }/* for */
169     return 1;
170 }/* CRaster::UpdateImage */
171
172
173 /*======================================================================================*/
174
175 int CRaster::LoadBMP(const char* cfileName)
176 {
177     CFile   file;
178     BITMAPFILEHEADER    fileHeader;
179     BITMAPINFOHEADER    infoHeader;
180     DWORD               width, height;
181
182     LOGPALETTE          *logPal;
183
184     DWORD               Index;
185     WORD                bitCount;
186     BYTE*               tempImage;
187
188     if(flag) {
189         DeleteAllData();
190     }/* if */
191
192     if(file.Open(cfileName, CFile::modeRead, NULL) == 0)
193         return -1;
194
195     fileName    = cfileName; 
196
197     file.Read(&fileHeader, sizeof(BITMAPFILEHEADER));
198     if(fileHeader.bfType != 0x4d42) {
199         file.Close();
200         return -1;
201     } /* if... it's not BMP */
202
203 // Load bitmap info
204     file.Read(&infoHeader, sizeof(BITMAPINFOHEADER));
205     bitCount    = infoHeader.biBitCount;
206     if(infoHeader.biCompression != BI_RGB ) {
207         file.Close();
208         return -2;
209     }/* if */
210     if( bitCount == 1 ||
211         bitCount == 4 ||
212         bitCount == 8) { // type of bitmap
213
214         DWORD numColor = infoHeader.biClrUsed;
215         if(numColor == 0) {
216             numColor = 1<<bitCount;
217         }
218
219         imgInfo = (BITMAPINFO*)malloc(  sizeof(BITMAPINFO) 
220                                         + sizeof(RGBQUAD) * (1<<bitCount));
221         file.Read(imgInfo->bmiColors, sizeof(RGBQUAD) * numColor);
222
223     // Create Palette for 1<<bitCount colors
224         pal     = new CPalette();
225         logPal  = (LOGPALETTE*)
226                     calloc( 1,  sizeof(LOGPALETTE) 
227                                     + sizeof(PALETTEENTRY) * (1<<bitCount));
228
229         logPal->palVersion      = 0x300;
230         logPal->palNumEntries   = BYTE(1<<bitCount);
231
232         for(Index = 0; Index < numColor; Index++) {
233
234             logPal->palPalEntry[Index].peRed    =
235                 imgInfo->bmiColors[Index].rgbRed;
236
237             logPal->palPalEntry[Index].peGreen  =
238                 imgInfo->bmiColors[Index].rgbGreen;
239
240             logPal->palPalEntry[Index].peBlue   =
241                 imgInfo->bmiColors[Index].rgbBlue;
242         }/* for */
243
244         pal->CreatePalette(logPal);
245         free(logPal);
246     } else if(  bitCount == 24 ) {
247         imgInfo = (BITMAPINFO*)malloc(  sizeof(BITMAPINFO) 
248                                         + sizeof(RGBQUAD));
249     } else {
250         file.Close();
251         return -2;
252     } /* if... not support BMP */
253
254     imgInfo->bmiHeader  = infoHeader;
255
256 // Load image data
257     width   = imgInfo->bmiHeader.biWidth;
258     height  = abs(imgInfo->bmiHeader.biHeight);
259
260     switch(bitCount) {
261     case 1:
262         shiftPixel  = (width + 7) / 8;
263         break;
264     case 4:
265         shiftPixel  = (width + 1) / 2;
266         break;
267     case 8:
268         shiftPixel  = width;
269         break;
270     case 24:
271         shiftPixel  = width * 3;
272         break;
273     }/* switch */
274
275     DestWidth   = width;
276     DestHeight  = height;
277     SrcWidth    = width;
278     SrcHeight   = height;
279
280     sizeString  = (shiftPixel + 3) / 4 * 4;     // Attention !!! divided 4 !!!;
281
282     tempImage       = new BYTE[sizeString*height];
283     imgData         = new BYTE[sizeString*height];
284     imgDataSave     = tempImage;
285
286     for(Index = 0; Index < height; Index++) {
287         file.Read(tempImage, sizeString);
288         tempImage+=sizeString;
289     }/* for */
290
291     imgInfo->bmiHeader.biHeight = -abs(imgInfo->bmiHeader.biHeight);
292
293     UpdateImage(0);
294     flag    = 1;
295     file.Close();
296     return 1;
297 }/* CRaster::LoadBMP */
298
299
300 /*======================================================================================*/
301
302 int CRaster::Draw(CDC* pDC) 
303 {
304     HDC         hDC;
305     CPalette*   oldPal;
306
307     oldPal  = 0;
308     hDC     =   pDC->m_hDC;
309     if(pal) {
310         oldPal  =   pDC->SelectPalette(pal, TRUE);
311     }/* if */
312
313     pDC->SetStretchBltMode(COLORONCOLOR);
314
315     StretchDIBits(  hDC, 
316                     XDest,          YDest, 
317                     DestWidth,      DestHeight,
318                     XSrc,           YSrc,
319                     SrcWidth,       SrcHeight,
320                     imgData,        imgInfo,
321                     DIB_RGB_COLORS, SRCCOPY);
322
323     if(pal) {
324         pDC->SelectPalette(oldPal, TRUE);
325     }/* if */
326
327     return 1;
328 }/* CRaster::Draw */
329
330
331 /*======================================================================================*/
332 int CRaster::Draw(CDC* pDC, RECT , RECT) 
333 {
334     HDC         hDC;
335     CPalette*   oldPal;
336
337     oldPal  = 0;
338
339     hDC     =   pDC->m_hDC;
340     if(pal) {
341         oldPal  =   pDC->SelectPalette(pal, TRUE);
342     }/* if */
343
344     pDC->SetStretchBltMode(COLORONCOLOR);
345
346     StretchDIBits(  hDC, 
347                     XDest,          YDest, 
348                     DestWidth,      DestHeight,
349                     XSrc,           YSrc,
350                     SrcWidth,       SrcHeight,
351                     imgData,        imgInfo,
352                     DIB_RGB_COLORS, SRCCOPY);
353
354     if(pal) {
355         pDC->SelectPalette(oldPal, TRUE);
356     }/* if */
357
358     return 1;
359 }/* CRaster::Draw */
360
361
362 /*======================================================================================*/
363
364 int CRaster::Draw(CDC* pDC, SIZE dstSize) 
365 {
366     HDC         hDC;
367     CPalette*   oldPal;
368
369     oldPal  = 0;
370
371     hDC     =   pDC->m_hDC;
372     if(pal) {
373         oldPal  =   pDC->SelectPalette(pal, TRUE);
374     }/* if */
375
376     pDC->SetStretchBltMode(COLORONCOLOR);
377
378     ::StretchDIBits(  hDC, 
379                     0,          0, 
380                     dstSize.cx,     dstSize.cy,
381                     0,          0,
382                     GetWidth(),     GetHeight(),
383                     imgData,        imgInfo,
384                     DIB_RGB_COLORS, SRCCOPY);
385
386     if(pal) {
387         pDC->SelectPalette(oldPal, TRUE);
388     }/* if */
389
390     return 1;
391 }/* CRaster::Draw */
392
393
394 /*======================================================================================*/
395
396 int CRaster::Draw(CDC* pDC, POINT point,SIZE size) 
397 {
398     HDC         hDC;
399     CPalette*   oldPal;
400
401     oldPal  = 0;
402
403     hDC     =   pDC->m_hDC;
404     if(pal) {
405         oldPal  =   pDC->SelectPalette(pal, TRUE);
406     }/* if */
407
408     pDC->SetStretchBltMode(COLORONCOLOR);
409
410     StretchDIBits(  hDC, 
411                     point.x,        point.y, 
412                     size.cx,        size.cy,
413                     XSrc,           YSrc,
414                     SrcWidth,       SrcHeight,
415                     imgData,        imgInfo,
416                     DIB_RGB_COLORS, SRCCOPY);
417
418     if(pal) {
419         pDC->SelectPalette(oldPal, TRUE);
420     }/* if */
421
422     return 1;
423 }/* CRaster::Draw */
424
425
426 /*======================================================================================*/
427
428 void CRaster::DeleteAllData()
429 {
430     if(imgData)
431         delete imgData;
432
433     if(imgDataSave)
434         delete imgDataSave;
435
436     if(imgInfo)
437         free(imgInfo);
438
439     if(pal)
440         delete pal;
441
442     imgData         = NULL;
443     imgDataSave     = NULL;
444     imgInfo         = NULL;
445     pal             = NULL;
446     flag            = 0;
447 }/* CRaster::DeleteAllData */
448
449
450 /*======================================================================================*/
451
452 unsigned long CRaster::GetWidth()
453 {
454     return imgInfo->bmiHeader.biWidth;
455 }/* CRaster::GetWidth */
456
457
458 /*======================================================================================*/
459
460 unsigned long CRaster::GetHeight()
461 {
462     return abs(imgInfo->bmiHeader.biHeight);
463 }/* CRaster::GetHeight */
464
465
466 /*======================================================================================*/
467
468 unsigned long CRaster::GetSizeString()
469 {
470     return sizeString;
471 }/* CRaster::GetSizeString */
472
473
474 /*======================================================================================*/
475
476 BYTE* CRaster::GetImage()
477 {
478     return imgData;
479 }/* CRaster::GetImage */
480
481
482 /*======================================================================================*/
483
484 int CRaster::GetNumBitPerPixel()
485 {
486     return imgInfo->bmiHeader.biBitCount;
487 }/* CRaster::GetNumBitPerPixel */
488
489
490 /*======================================================================================*/
491
492 BITMAPINFO* CRaster::GetBitmapInfo()
493 {
494     return imgInfo;
495 }/* CRaster::GetBitmapInfo */
496
497
498 /*======================================================================================*/