]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gdiimage.cpp
corrections for buffers
[wxWidgets.git] / src / msw / gdiimage.cpp
CommitLineData
0d0512bd
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/gdiimage.cpp
3// Purpose: wxGDIImage implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.11.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6c9a19aa 9// Licence: wxWindows licence
0d0512bd
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
0d0512bd
VZ
21 #pragma implementation "gdiimage.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/string.h"
f94dfb38 33 #include "wx/log.h"
0d0512bd
VZ
34#endif // WX_PRECOMP
35
e42a6c18
VZ
36#include "wx/msw/private.h"
37
0d0512bd
VZ
38#include "wx/app.h"
39
f66f0fd7 40#include "wx/bitmap.h"
474df218 41#include "wx/msw/gdiimage.h"
4676948b
JS
42
43#if wxUSE_WXDIB
474df218 44#include "wx/msw/dib.h"
4676948b
JS
45#endif
46
47#ifdef __WXWINCE__
48#include <winreg.h>
49#include <shellapi.h>
50#endif
f66f0fd7 51
419430a0
JS
52#include "wx/file.h"
53
d162a7ee
VZ
54#include "wx/listimpl.cpp"
55WX_DEFINE_LIST(wxGDIImageHandlerList);
56
419430a0
JS
57// ----------------------------------------------------------------------------
58// auxiliary functions
59// ----------------------------------------------------------------------------
60
61#ifdef __WXWINCE__
62// Used in wxBMPFileHandler::LoadFile
63HBITMAP wxLoadBMP(const wxString& filename) ;
64#endif
65
0d0512bd
VZ
66// ----------------------------------------------------------------------------
67// private classes
68// ----------------------------------------------------------------------------
69
04ef50df
JS
70#ifndef __WXMICROWIN__
71
0d0512bd
VZ
72// all image handlers are declared/defined in this file because the outside
73// world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
74
75class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
76{
77public:
78 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
79 wxBITMAP_TYPE_BMP)
80 {
81 }
82
83 virtual bool LoadFile(wxBitmap *bitmap,
84 const wxString& name, long flags,
85 int desiredWidth, int desiredHeight);
86 virtual bool SaveFile(wxBitmap *bitmap,
87 const wxString& name, int type,
88 const wxPalette *palette = NULL);
89
90private:
91 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
92};
93
94class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
95{
96public:
97 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
98 wxEmptyString,
99 wxBITMAP_TYPE_BMP_RESOURCE)
100 {
101 }
102
103 virtual bool LoadFile(wxBitmap *bitmap,
104 const wxString& name, long flags,
105 int desiredWidth, int desiredHeight);
106
107private:
108 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
109};
110
111class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
112{
113public:
114 wxIconHandler(const wxString& name, const wxString& ext, long type)
115 : wxGDIImageHandler(name, ext, type)
116 {
117 }
118
119 // creating and saving icons is not supported
120 virtual bool Create(wxGDIImage *WXUNUSED(image),
121 void *WXUNUSED(data),
122 long WXUNUSED(flags),
123 int WXUNUSED(width),
124 int WXUNUSED(height),
125 int WXUNUSED(depth) = 1)
126 {
4f8090e0 127 return false;
0d0512bd
VZ
128 }
129
130 virtual bool Save(wxGDIImage *WXUNUSED(image),
131 const wxString& WXUNUSED(name),
132 int WXUNUSED(type))
133 {
4f8090e0 134 return false;
0d0512bd
VZ
135 }
136
137 virtual bool Load(wxGDIImage *image,
138 const wxString& name,
139 long flags,
140 int desiredWidth, int desiredHeight)
141 {
142 wxIcon *icon = wxDynamicCast(image, wxIcon);
4f8090e0 143 wxCHECK_MSG( icon, false, _T("wxIconHandler only works with icons") );
0d0512bd
VZ
144
145 return LoadIcon(icon, name, flags, desiredWidth, desiredHeight);
146 }
147
148protected:
149 virtual bool LoadIcon(wxIcon *icon,
150 const wxString& name, long flags,
151 int desiredWidth = -1, int desiredHeight = -1) = 0;
152};
153
154class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
155{
156public:
157 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
158 _T("ico"),
159 wxBITMAP_TYPE_ICO)
160 {
161 }
162
163 virtual bool LoadIcon(wxIcon *icon,
164 const wxString& name, long flags,
165 int desiredWidth = -1, int desiredHeight = -1);
166
167private:
168 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
169};
170
171class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
172{
173public:
174 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
175 _T("ico"),
176 wxBITMAP_TYPE_ICO_RESOURCE)
177 {
178 }
179
180 virtual bool LoadIcon(wxIcon *icon,
181 const wxString& name, long flags,
182 int desiredWidth = -1, int desiredHeight = -1);
183
184private:
185 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
186};
187
188// ----------------------------------------------------------------------------
189// wxWin macros
190// ----------------------------------------------------------------------------
191
621b3e21
VZ
192IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
193IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
194IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
195IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
0d0512bd
VZ
196
197// ----------------------------------------------------------------------------
198// private functions
199// ----------------------------------------------------------------------------
200
04ef50df
JS
201#endif
202 // __MICROWIN__
0d0512bd
VZ
203
204// ============================================================================
205// implementation
206// ============================================================================
207
d162a7ee 208wxGDIImageHandlerList wxGDIImage::ms_handlers;
0d0512bd
VZ
209
210// ----------------------------------------------------------------------------
211// wxGDIImage functions forwarded to wxGDIImageRefData
212// ----------------------------------------------------------------------------
213
214bool wxGDIImage::FreeResource(bool WXUNUSED(force))
215{
216 if ( !IsNull() )
217 {
218 GetGDIImageData()->Free();
219 GetGDIImageData()->m_handle = 0;
220 }
221
4f8090e0 222 return true;
0d0512bd
VZ
223}
224
2b5f62a0 225WXHANDLE wxGDIImage::GetResourceHandle() const
0d0512bd
VZ
226{
227 return GetHandle();
228}
229
230// ----------------------------------------------------------------------------
231// wxGDIImage handler stuff
232// ----------------------------------------------------------------------------
233
234void wxGDIImage::AddHandler(wxGDIImageHandler *handler)
235{
236 ms_handlers.Append(handler);
237}
238
239void wxGDIImage::InsertHandler(wxGDIImageHandler *handler)
240{
241 ms_handlers.Insert(handler);
242}
243
244bool wxGDIImage::RemoveHandler(const wxString& name)
245{
246 wxGDIImageHandler *handler = FindHandler(name);
247 if ( handler )
248 {
249 ms_handlers.DeleteObject(handler);
4f8090e0 250 return true;
0d0512bd
VZ
251 }
252 else
4f8090e0 253 return false;
0d0512bd
VZ
254}
255
256wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& name)
257{
222ed1d6 258 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
259 while ( node )
260 {
d162a7ee 261 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
262 if ( handler->GetName() == name )
263 return handler;
4f8090e0 264 node = node->GetNext();
0d0512bd
VZ
265 }
266
267 return NULL;
268}
269
270wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& extension,
271 long type)
272{
222ed1d6 273 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
274 while ( node )
275 {
d162a7ee 276 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
277 if ( (handler->GetExtension() = extension) &&
278 (type == -1 || handler->GetType() == type) )
279 {
280 return handler;
281 }
282
4f8090e0 283 node = node->GetNext();
0d0512bd
VZ
284 }
285 return NULL;
286}
287
288wxGDIImageHandler *wxGDIImage::FindHandler(long type)
289{
222ed1d6 290 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
291 while ( node )
292 {
d162a7ee 293 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
294 if ( handler->GetType() == type )
295 return handler;
296
4f8090e0 297 node = node->GetNext();
0d0512bd
VZ
298 }
299
300 return NULL;
301}
302
303void wxGDIImage::CleanUpHandlers()
304{
222ed1d6 305 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
306 while ( node )
307 {
d162a7ee 308 wxGDIImageHandler *handler = node->GetData();
222ed1d6 309 wxGDIImageHandlerList::compatibility_iterator next = node->GetNext();
0d0512bd 310 delete handler;
222ed1d6 311 ms_handlers.Erase( node );
0d0512bd
VZ
312 node = next;
313 }
314}
315
316void wxGDIImage::InitStandardHandlers()
317{
04ef50df 318#ifndef __WXMICROWIN__
0d0512bd
VZ
319 AddHandler(new wxBMPResourceHandler);
320 AddHandler(new wxBMPFileHandler);
0d0512bd
VZ
321 AddHandler(new wxICOResourceHandler);
322 AddHandler(new wxICOFileHandler);
04ef50df 323#endif
0d0512bd
VZ
324}
325
04ef50df
JS
326#ifndef __WXMICROWIN__
327
0d0512bd
VZ
328// ----------------------------------------------------------------------------
329// wxBitmap handlers
330// ----------------------------------------------------------------------------
331
332bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap,
333 const wxString& name, long WXUNUSED(flags),
334 int WXUNUSED(desiredWidth),
335 int WXUNUSED(desiredHeight))
336{
337 // TODO: load colourmap.
338 bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name));
339
8bbbae21 340 if ( !bitmap->Ok() )
0d0512bd
VZ
341 {
342 // it's probably not found
343 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
344 name.c_str());
8bbbae21 345
4f8090e0 346 return false;
8bbbae21
VZ
347 }
348
349 BITMAP bm;
350 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) )
351 {
352 wxLogLastError(wxT("GetObject(HBITMAP)"));
0d0512bd
VZ
353 }
354
8bbbae21
VZ
355 bitmap->SetWidth(bm.bmWidth);
356 bitmap->SetHeight(bm.bmHeight);
357 bitmap->SetDepth(bm.bmBitsPixel);
358
9542f0a1
VZ
359 // use 0xc0c0c0 as transparent colour by default
360 bitmap->SetMask(new wxMask(*bitmap, *wxLIGHT_GREY));
361
4f8090e0 362 return true;
0d0512bd
VZ
363}
364
365bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap,
366 const wxString& name, long WXUNUSED(flags),
367 int WXUNUSED(desiredWidth),
368 int WXUNUSED(desiredHeight))
369{
4676948b 370#if wxUSE_WXDIB
474df218 371 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in LoadFile") );
d275c7eb 372
474df218 373 wxDIB dib(name);
0d0512bd 374
999836aa
VZ
375 bool ok = dib.IsOk() && bitmap->CopyFromDIB(dib);
376 return ok;
4676948b 377#else
419430a0
JS
378 WXHBITMAP hBitmap = (WXHBITMAP)wxLoadBMP(name);
379 if(hBitmap) {
380 bitmap->SetHBITMAP(hBitmap);
381 return TRUE;
382 }
4676948b
JS
383 return FALSE;
384#endif
0d0512bd
VZ
385}
386
387bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap,
388 const wxString& name,
389 int WXUNUSED(type),
2b254edf 390 const wxPalette * WXUNUSED(pal))
0d0512bd 391{
4676948b 392#if wxUSE_WXDIB
2b254edf
VZ
393 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in SaveFile") );
394
395 wxDIB dib(*bitmap);
396
397 return dib.Save(name);
4676948b
JS
398#else
399 return FALSE;
400#endif
0d0512bd
VZ
401}
402
403// ----------------------------------------------------------------------------
404// wxIcon handlers
405// ----------------------------------------------------------------------------
406
407bool wxICOFileHandler::LoadIcon(wxIcon *icon,
408 const wxString& name,
33ac7e6f 409 long WXUNUSED(flags),
0d0512bd
VZ
410 int desiredWidth, int desiredHeight)
411{
0d0512bd
VZ
412 icon->UnRef();
413
414 // actual size
415 wxSize size;
416
a4352768
VZ
417 HICON hicon = NULL;
418
2dace059
VZ
419 // Parse the filename: it may be of the form "filename;n" in order to
420 // specify the nth icon in the file.
421 //
422 // For the moment, ignore the issue of possible semicolons in the
423 // filename.
ff9aab3c 424 int iconIndex = 0;
2dace059 425 wxString nameReal(name);
ff9aab3c 426 wxString strIconIndex = name.AfterLast(wxT(';'));
b642d457 427 if (strIconIndex != name)
ff9aab3c
JS
428 {
429 iconIndex = wxAtoi(strIconIndex);
2dace059 430 nameReal = name.BeforeLast(wxT(';'));
ff9aab3c
JS
431 }
432
2b5f62a0
VZ
433#if 0
434 // If we don't know what size icon we're looking for,
435 // try to find out what's there.
436 // Unfortunately this doesn't work, because ExtractIconEx
437 // will scale the icon to the 'desired' size, even if that
438 // size of icon isn't explicitly stored. So we would have
439 // to parse the icon file outselves.
440 if ( desiredWidth == -1 &&
441 desiredHeight == -1)
442 {
443 // Try loading a large icon first
444 if ( ::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) == 1)
445 {
446 }
447 // Then try loading a small icon
448 else if ( ::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) == 1)
449 {
450 }
451 }
452 else
453#endif
4676948b 454 // were we asked for a large icon?
a4352768
VZ
455 if ( desiredWidth == ::GetSystemMetrics(SM_CXICON) &&
456 desiredHeight == ::GetSystemMetrics(SM_CYICON) )
457 {
ff9aab3c 458 // get the specified large icon from file
2dace059 459 if ( !::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) )
a4352768
VZ
460 {
461 // it is not an error, but it might still be useful to be informed
462 // about it optionally
463 wxLogTrace(_T("iconload"),
464 _T("No large icons found in the file '%s'."),
465 name.c_str());
466 }
467 }
468 else if ( desiredWidth == ::GetSystemMetrics(SM_CXSMICON) &&
469 desiredHeight == ::GetSystemMetrics(SM_CYSMICON) )
470 {
ff9aab3c 471 // get the specified small icon from file
2dace059 472 if ( !::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) )
a4352768
VZ
473 {
474 wxLogTrace(_T("iconload"),
475 _T("No small icons found in the file '%s'."),
476 name.c_str());
477 }
478 }
479 //else: not standard size, load below
480
4676948b 481#ifndef __WXWINCE__
a4352768
VZ
482 if ( !hicon )
483 {
d8f3f983
RD
484 // take any size icon from the file by index
485 hicon = ::ExtractIcon(wxGetInstance(), nameReal, iconIndex);
a4352768 486 }
4676948b 487#endif
a4352768 488
0d0512bd
VZ
489 if ( !hicon )
490 {
491 wxLogSysError(_T("Failed to load icon from the file '%s'"),
492 name.c_str());
493
4f8090e0 494 return false;
0d0512bd
VZ
495 }
496
6bad4c32 497 size = wxGetHiconSize(hicon);
0d0512bd
VZ
498
499 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
500 (desiredHeight != -1 && desiredHeight != size.y) )
501 {
a4352768 502 wxLogTrace(_T("iconload"),
4f8090e0 503 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
a4352768
VZ
504 size.x, size.y,
505 desiredWidth, desiredHeight);
0d0512bd
VZ
506
507 ::DestroyIcon(hicon);
508
4f8090e0 509 return false;
0d0512bd
VZ
510 }
511
512 icon->SetHICON((WXHICON)hicon);
513 icon->SetSize(size.x, size.y);
514
515 return icon->Ok();
0d0512bd
VZ
516}
517
518bool wxICOResourceHandler::LoadIcon(wxIcon *icon,
519 const wxString& name,
33ac7e6f 520 long WXUNUSED(flags),
0d0512bd
VZ
521 int desiredWidth, int desiredHeight)
522{
523 HICON hicon;
524
9cb9fdb3
VZ
525 // do we need the icon of the specific size or would any icon do?
526 bool hasSize = desiredWidth != -1 || desiredHeight != -1;
527
528 wxASSERT_MSG( !hasSize || (desiredWidth != -1 && desiredHeight != -1),
529 _T("width and height should be either both -1 or not") );
530
2dace059
VZ
531 // try to load the icon from this program first to allow overriding the
532 // standard icons (although why one would want to do it considering that
533 // we already have wxApp::GetStdIcon() is unclear)
9990cb50
VZ
534
535 // note that we can't just always call LoadImage() because it seems to do
536 // some icon rescaling internally which results in very ugly 16x16 icons
9990cb50 537 if ( hasSize )
0d0512bd 538 {
9990cb50
VZ
539 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
540 desiredWidth, desiredHeight,
541 LR_DEFAULTCOLOR);
0d0512bd 542 }
9990cb50 543 else
9990cb50
VZ
544 {
545 hicon = ::LoadIcon(wxGetInstance(), name);
546 }
fea2b62e 547
2dace059 548 // next check if it's not a standard icon
4676948b 549#ifndef __WXWINCE__
9cb9fdb3 550 if ( !hicon && !hasSize )
2dace059
VZ
551 {
552 static const struct
553 {
554 const wxChar *name;
555 LPTSTR id;
556 } stdIcons[] =
557 {
558 { wxT("wxICON_QUESTION"), IDI_QUESTION },
559 { wxT("wxICON_WARNING"), IDI_EXCLAMATION },
560 { wxT("wxICON_ERROR"), IDI_HAND },
c2edb18a 561 { wxT("wxICON_INFORMATION"), IDI_ASTERISK },
2dace059
VZ
562 };
563
564 for ( size_t nIcon = 0; !hicon && nIcon < WXSIZEOF(stdIcons); nIcon++ )
565 {
566 if ( name == stdIcons[nIcon].name )
567 {
9cb9fdb3 568 hicon = ::LoadIcon((HINSTANCE)NULL, stdIcons[nIcon].id);
2dace059
VZ
569 }
570 }
571 }
4676948b 572#endif
2dace059 573
6bad4c32 574 wxSize size = wxGetHiconSize(hicon);
0d0512bd
VZ
575 icon->SetSize(size.x, size.y);
576
0d0512bd
VZ
577 icon->SetHICON((WXHICON)hicon);
578
579 return icon->Ok();
580}
581
582// ----------------------------------------------------------------------------
583// private functions
584// ----------------------------------------------------------------------------
585
6bad4c32 586wxSize wxGetHiconSize(HICON hicon)
0d0512bd
VZ
587{
588 wxSize size(32, 32); // default
4676948b 589#ifndef __WXWINCE__
0d0512bd
VZ
590 if ( hicon && wxGetOsVersion() != wxWIN32S )
591 {
592 ICONINFO info;
593 if ( !::GetIconInfo(hicon, &info) )
594 {
f6bcfd97 595 wxLogLastError(wxT("GetIconInfo"));
0d0512bd
VZ
596 }
597 else
598 {
599 HBITMAP hbmp = info.hbmMask;
600 if ( hbmp )
601 {
602 BITMAP bm;
603 if ( ::GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm) )
604 {
605 size = wxSize(bm.bmWidth, bm.bmHeight);
606 }
607
608 ::DeleteObject(info.hbmMask);
609 }
610 if ( info.hbmColor )
611 ::DeleteObject(info.hbmColor);
612 }
613 }
4676948b 614#endif
0d0512bd
VZ
615 return size;
616}
474df218
VZ
617
618#endif // __WXMICROWIN__
619
419430a0
JS
620#ifdef __WXWINCE__
621// Used in wxBMPFileHandler::LoadFile
622HBITMAP wxLoadBMP(const wxString& filename)
623{
624 wxFile file;
625 if(!file.Open(filename))
626 return 0;
627
628 // The first part of the file contains the file header.
629 // This will tell us if it is a bitmap, how big the header is, and how big
630 // the file is. The header size in the file header includes the color table.
631 BITMAPFILEHEADER BmpFileHdr;
632 BITMAPINFO *pBmpInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+255*sizeof(RGBQUAD));
633 BYTE* pBits = 0;
634 HBITMAP hBitmap = 0;
635
636 if(file.Read(&BmpFileHdr, sizeof(BmpFileHdr))==sizeof(BmpFileHdr)
637 && !strncmp((char*)&BmpFileHdr.bfType,"BM",2)
638 && file.Read(pBmpInfo, sizeof(BITMAPINFOHEADER))==sizeof(BITMAPINFOHEADER)
639 && pBmpInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) {
640
641
642 unsigned int nColors = pBmpInfo->bmiHeader.biClrUsed ?
643 pBmpInfo->bmiHeader.biClrUsed : 1 << pBmpInfo->bmiHeader.biBitCount;
644 if (nColors < 1
645 || file.Read(pBmpInfo->bmiColors, nColors * sizeof(RGBQUAD))
646 == (off_t)(nColors * sizeof(RGBQUAD))) {
647
648 // So how big the bitmap surface is.
649 int nBitsSize = BmpFileHdr.bfSize - BmpFileHdr.bfOffBits;
650
651 // Allocate the memory for the bits and read the bits from the file.
652 pBits = (BYTE*) malloc(nBitsSize*2);
653 if (pBits) {
654 // Seek to the bits in the file.
655 file.Seek(BmpFileHdr.bfOffBits);
656
657 // read the bits
658 if(file.Read(pBits, nBitsSize)==nBitsSize) {
659 // Everything went OK.
660 pBmpInfo->bmiHeader.biSizeImage = nBitsSize;
661
662 //HBITMAP hBitmap=SetBitmap((LPBITMAPINFO)pBmpInfo, pBits);
663 DWORD dwBitmapInfoSize = sizeof(BITMAPINFO) + nColors*sizeof(RGBQUAD);
664
665 // Create a DC which will be used to get DIB, then create DIBsection
666 HDC hDC = ::GetDC(NULL);
667 if (hDC) {
668 LPVOID bits;
669 hBitmap = CreateDIBSection(hDC, (const BITMAPINFO*) pBmpInfo,
670 DIB_RGB_COLORS, &bits, NULL, 0);
671 ReleaseDC(0,hDC);
672
673 if (hBitmap) {
674 DWORD dwImageSize = pBmpInfo->bmiHeader.biSizeImage;
675 if (dwImageSize == 0) {
676 int nBytesPerLine = pBmpInfo->bmiHeader.biWidth * pBmpInfo->bmiHeader.biBitCount;
677 nBytesPerLine = ( (nBytesPerLine + 31) & (~31) ) / 8;
678 dwImageSize = nBytesPerLine * pBmpInfo->bmiHeader.biHeight;
679 }
680 memcpy(bits, pBits, dwImageSize);
681 }
682 }
683 }
684 }
685 }
686 }
687
688 if(pBmpInfo)
689 free(pBmpInfo);
690 if(pBits)
691 free(pBits);
692
693 return hBitmap;
694}
695#endif
696