]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dib.cpp
all delete functions now send delete notification event
[wxWidgets.git] / src / msw / dib.cpp
1 /*******************************************************************************
2 * *
3 * MODULE : DIB.CC *
4 * *
5 * DESCRIPTION : Routines for dealing with Device Independent Bitmaps. *
6 * *
7 * FUNCTIONS : *
8 * *
9 * ReadDIB() - Reads a DIB *
10 * *
11 * WriteDIB() - Writes a global handle in CF_DIB format*
12 * to a file. *
13 * *
14 * PaletteSize() - Calculates the palette size in bytes *
15 * of given DIB *
16 * *
17 * DibNumColors() - Determines the number of colors in DIB *
18 * *
19 * DibFromBitmap() - Creates a DIB repr. the DDB passed in. *
20 * *
21 * *
22 * lread() - Private routine to read more than 64k *
23 * *
24 * lwrite() - Private routine to write more than 64k *
25 * *
26 *******************************************************************************/
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #if defined(__BORLANDC__)
32 #pragma hdrstop
33 #endif
34
35 #ifndef WX_PRECOMP
36 #include "wx/setup.h"
37 #include "wx/defs.h"
38 #endif
39
40 #include <windows.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <memory.h>
44
45 #include "wx/msw/dib.h"
46
47 #ifdef __GNUWIN32__
48 #include "wx/msw/gnuwin32/extra.h"
49 #endif
50
51 #ifndef SEEK_CUR
52 /* flags for _lseek */
53 #define SEEK_CUR 1
54 #define SEEK_END 2
55 #define SEEK_SET 0
56 #endif
57
58 #define MAXREAD 32768 /* Number of bytes to be read during */
59 /* each read operation. */
60
61 /* Header signatutes for various resources */
62 #define BFT_ICON 0x4349 /* 'IC' */
63 #define BFT_BITMAP 0x4d42 /* 'BM' */
64 #define BFT_CURSOR 0x5450 /* 'PT' */
65
66 /* macro to determine if resource is a DIB */
67 #define ISDIB(bft) ((bft) == BFT_BITMAP)
68
69 /* Macro to align given value to the closest DWORD (unsigned long ) */
70 #define ALIGNULONG(i) ((i+3)/4*4)
71
72 /* Macro to determine to round off the given value to the closest byte */
73 #define WIDTHBYTES(i) ((i+31)/32*4)
74
75 #define PALVERSION 0x300
76 #define MAXPALETTE 256 /* max. # supported palette entries */
77
78 DWORD PASCAL lread(int fh, VOID FAR *pv, DWORD ul);
79 DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul);
80
81 BOOL WriteDIB (LPSTR szFile,HANDLE hdib);
82 WORD PaletteSize (VOID FAR * pv);
83 WORD DibNumColors (VOID FAR * pv);
84 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
85 BOOL PASCAL MakeBitmapAndPalette(HDC,HANDLE,HPALETTE *,HBITMAP *);
86 HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER);
87 BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette);
88
89 /****************************************************************************
90 * *
91 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
92 * *
93 * PURPOSE : Write a global handle in CF_DIB format to a file. *
94 * *
95 * RETURNS : TRUE - if successful. *
96 * FALSE - otherwise *
97 * *
98 ****************************************************************************/
99
100 BOOL WriteDIB(LPSTR szFile, HANDLE hdib)
101 {
102 BITMAPFILEHEADER hdr;
103 LPBITMAPINFOHEADER lpbi;
104 int fh;
105 OFSTRUCT of;
106
107 if (!hdib)
108 return FALSE;
109
110 fh = OpenFile(szFile, &of, OF_CREATE | OF_READWRITE);
111 if (fh == -1)
112 return FALSE;
113
114 #ifdef __WINDOWS_386__
115 lpbi = (LPBITMAPINFOHEADER) MK_FP32(GlobalLock(hdib));
116 #else
117 lpbi = (LPBITMAPINFOHEADER) GlobalLock(hdib);
118 #endif
119 /* Fill in the fields of the file header */
120 hdr.bfType = BFT_BITMAP;
121 hdr.bfSize = GlobalSize(hdib) + sizeof(BITMAPFILEHEADER);
122 hdr.bfReserved1 = 0;
123 hdr.bfReserved2 = 0;
124 hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + lpbi->biSize +
125 PaletteSize(lpbi);
126
127 /* Write the file header */
128 _lwrite(fh, (LPSTR) &hdr, sizeof(BITMAPFILEHEADER));
129
130 /* Write the DIB header and the bits */
131 lwrite(fh, (LPSTR) lpbi, GlobalSize(hdib));
132
133 GlobalUnlock(hdib);
134 _lclose(fh);
135 return TRUE;
136 }
137
138 /****************************************************************************
139 * *
140 * FUNCTION : PaletteSize(VOID FAR * pv) *
141 * *
142 * PURPOSE : Calculates the palette size in bytes. If the info. block *
143 * is of the BITMAPCOREHEADER type, the number of colors is *
144 * multiplied by 3 to give the palette size, otherwise the *
145 * number of colors is multiplied by 4. *
146 * *
147 * RETURNS : Palette size in number of bytes. *
148 * *
149 ****************************************************************************/
150
151 WORD PaletteSize(VOID FAR * pv)
152 {
153 LPBITMAPINFOHEADER lpbi;
154 WORD NumColors;
155
156 lpbi = (LPBITMAPINFOHEADER) pv;
157 NumColors = DibNumColors(lpbi);
158
159 if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
160 return NumColors * sizeof(RGBTRIPLE);
161 else
162 return NumColors * sizeof(RGBQUAD);
163 }
164
165 /****************************************************************************
166 * *
167 * FUNCTION : DibNumColors(VOID FAR * pv) *
168 * *
169 * PURPOSE : Determines the number of colors in the DIB by looking at *
170 * the BitCount filed in the info block. *
171 * *
172 * RETURNS : The number of colors in the DIB. *
173 * *
174 ****************************************************************************/
175
176 WORD DibNumColors(VOID FAR *pv)
177 {
178 int bits;
179 BITMAPINFOHEADER *lpbi;
180 BITMAPCOREHEADER *lpbc;
181
182 lpbi = ((BITMAPINFOHEADER*) pv);
183 lpbc = ((BITMAPCOREHEADER*) pv);
184
185 /* With the BITMAPINFO format headers, the size of the palette
186 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
187 * is dependent on the bits per pixel ( = 2 raised to the power of
188 * bits/pixel).
189 */
190 if (lpbi->biSize != sizeof(BITMAPCOREHEADER)) {
191 if (lpbi->biClrUsed != 0)
192 return (WORD) lpbi->biClrUsed;
193 bits = lpbi->biBitCount;
194 }
195 else
196 bits = lpbc->bcBitCount;
197
198 switch (bits) {
199 case 1:
200 return 2;
201 case 4:
202 return 16;
203 case 8:
204 return 256;
205 default:
206 /* A 24 bitcount DIB has no color table */
207 return 0;
208 }
209 }
210
211 /****************************************************************************
212 * *
213 * FUNCTION : DibFromBitmap() *
214 * *
215 * PURPOSE : Will create a global memory block in DIB format that *
216 * represents the Device-dependent bitmap (DDB) passed in. *
217 * *
218 * RETURNS : A handle to the DIB *
219 * *
220 ****************************************************************************/
221
222 #if NOTHING
223 HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal)
224 {
225 BITMAP bm;
226 BITMAPINFOHEADER bi;
227 BITMAPINFOHEADER FAR *lpbi;
228 DWORD dwLen;
229 HANDLE hdib;
230 HANDLE h;
231 HDC hdc;
232
233 if (!hbm)
234 return NULL;
235
236 if (hpal == NULL)
237 hpal = GetStockObject(DEFAULT_PALETTE);
238
239 GetObject(hbm, sizeof (bm), (LPSTR) &bm);
240
241 if (biBits == 0)
242 biBits = bm.bmPlanes * bm.bmBitsPixel;
243
244 bi.biSize = sizeof(BITMAPINFOHEADER);
245 bi.biWidth = bm.bmWidth;
246 bi.biHeight = bm.bmHeight;
247 bi.biPlanes = 1;
248 bi.biBitCount = biBits;
249 bi.biCompression = biStyle;
250 bi.biSizeImage = 0;
251 bi.biXPelsPerMeter = 0;
252 bi.biYPelsPerMeter = 0;
253 bi.biClrUsed = 0;
254 bi.biClrImportant = 0;
255
256 dwLen = bi.biSize + PaletteSize(&bi);
257
258 hdc = GetDC(NULL);
259 hpal = SelectPalette(hdc, hpal, FALSE);
260 RealizePalette(hdc);
261
262 hdib = GlobalAlloc(GHND, dwLen);
263
264 if (!hdib) {
265 SelectPalette(hdc, hpal, FALSE);
266 ReleaseDC(NULL, hdc);
267 return NULL;
268 }
269
270 #ifdef __WINDOWS_386__
271 lpbi = (BITMAPINFOHEADER FAR *) MK_FP32(GlobalLock(hdib));
272 #else
273 lpbi = (BITMAPINFOHEADER FAR *) GlobalLock(hdib);
274 #endif
275
276 *lpbi = bi;
277
278 /* call GetDIBits with a NULL lpBits param, so it will calculate the
279 * biSizeImage field for us
280 */
281 GetDIBits(hdc, hbm, 0, (WORD) bi.biHeight,
282 NULL, (LPBITMAPINFO) lpbi, DIB_RGB_COLORS);
283
284 bi = *lpbi;
285 GlobalUnlock(hdib);
286
287 /* If the driver did not fill in the biSizeImage field, make one up */
288 if (bi.biSizeImage == 0) {
289 bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;
290
291 if (biStyle != BI_RGB)
292 bi.biSizeImage = (bi.biSizeImage * 3) / 2;
293 }
294
295 /* realloc the buffer big enough to hold all the bits */
296 dwLen = bi.biSize + PaletteSize(&bi) + bi.biSizeImage;
297 if (h = GlobalReAlloc(hdib, dwLen, 0))
298 hdib = h;
299 else {
300 GlobalFree(hdib);
301 hdib = NULL;
302
303 SelectPalette(hdc, hpal, FALSE);
304 ReleaseDC(NULL, hdc);
305 return hdib;
306 }
307
308 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
309 * bits this time
310 */
311 #ifdef __WINDOWS_386__
312 lpbi = (BITMAPINFOHEADER FAR *) MK_FP32(GlobalLock(hdib));
313 #else
314 lpbi = (BITMAPINFOHEADER FAR *) GlobalLock(hdib);
315 #endif
316
317 if (GetDIBits(hdc,
318 hbm,
319 0,
320 (WORD) bi.biHeight,
321 (LPSTR) lpbi + (WORD) lpbi->biSize + PaletteSize(lpbi),
322 (LPBITMAPINFO) lpbi, DIB_RGB_COLORS) == 0) {
323 GlobalUnlock(hdib);
324 hdib = NULL;
325 SelectPalette(hdc, hpal, FALSE);
326 ReleaseDC(NULL, hdc);
327 return NULL;
328 }
329
330 bi = *lpbi;
331 GlobalUnlock(hdib);
332
333 SelectPalette(hdc, hpal, FALSE);
334 ReleaseDC(NULL, hdc);
335 return hdib;
336 }
337 #endif
338
339 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
340 /****************************************************************************
341 * *
342 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
343 * *
344 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
345 * *
346 * RETURNS : 0 - If read did not proceed correctly. *
347 * number of bytes read otherwise. *
348 * *
349 ****************************************************************************/
350
351 DWORD PASCAL lread(int fh, void far *pv, DWORD ul)
352 {
353 DWORD ulT = ul;
354 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
355 BYTE *hp = (BYTE *) pv;
356 #else
357 BYTE huge *hp = (BYTE huge *) pv;
358 #endif
359 while (ul > (DWORD) MAXREAD) {
360 if (_lread(fh, (LPSTR) hp, (WORD) MAXREAD) != MAXREAD)
361 return 0;
362 ul -= MAXREAD;
363 hp += MAXREAD;
364 }
365 if (_lread(fh, (LPSTR) hp, (WORD) ul) != (WORD) ul)
366 return 0;
367 return ulT;
368 }
369
370 /****************************************************************************
371 * *
372 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
373 * *
374 * PURPOSE : Writes data in steps of 32k till all the data is written. *
375 * *
376 * RETURNS : 0 - If write did not proceed correctly. *
377 * number of bytes written otherwise. *
378 * *
379 ****************************************************************************/
380
381 DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul)
382 {
383 DWORD ulT = ul;
384 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
385 BYTE *hp = (BYTE *) pv;
386 #else
387 BYTE huge *hp = (BYTE huge *) pv;
388 #endif
389 while (ul > MAXREAD) {
390 if (_lwrite(fh, (LPSTR) hp, (WORD) MAXREAD) != MAXREAD)
391 return 0;
392 ul -= MAXREAD;
393 hp += MAXREAD;
394 }
395 if (_lwrite(fh, (LPSTR) hp, (WORD) ul) != (WORD) ul)
396 return 0;
397 return ulT;
398 }
399
400 /****************************************************************************
401 *
402 * FUNCTION : ReadDIB(hWnd)
403 *
404 * PURPOSE : Reads a DIB from a file, obtains a handle to its
405 * BITMAPINFO struct. and loads the DIB. Once the DIB
406 * is loaded, the function also creates a bitmap and
407 * palette out of the DIB for a device-dependent form.
408 *
409 * RETURNS : TRUE - DIB loaded and bitmap/palette created
410 * The DIBINIT structure pointed to by pInfo is
411 * filled with the appropriate handles.
412 * FALSE - otherwise
413 *
414 ****************************************************************************/
415 BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette)
416 {
417 int fh;
418 LPBITMAPINFOHEADER lpbi;
419 OFSTRUCT of;
420 BITMAPFILEHEADER bf;
421 WORD nNumColors;
422 BOOL result = FALSE;
423 char str[128];
424 WORD offBits;
425 HDC hDC;
426 BOOL bCoreHead = FALSE;
427 HANDLE hDIB = 0;
428
429 /* Open the file and get a handle to it's BITMAPINFO */
430
431 fh = OpenFile (lpFileName, &of, OF_READ);
432 if (fh == -1) {
433 wsprintf(str,"Can't open file '%ls'", (LPSTR)lpFileName);
434 MessageBox(NULL, str, "Error", MB_ICONSTOP | MB_OK);
435 return (0);
436 }
437
438 hDIB = GlobalAlloc(GHND, (DWORD)(sizeof(BITMAPINFOHEADER) +
439 256 * sizeof(RGBQUAD)));
440 if (!hDIB)
441 return(0);
442
443 #ifdef __WINDOWS_386__
444 lpbi = (LPBITMAPINFOHEADER)MK_FP32(GlobalLock(hDIB));
445 #else
446 lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
447 #endif
448
449 /* read the BITMAPFILEHEADER */
450 if (sizeof (bf) != _lread (fh, (LPSTR)&bf, sizeof (bf)))
451 goto ErrExit;
452
453 if (bf.bfType != 0x4d42) /* 'BM' */
454 goto ErrExit;
455
456 if (sizeof(BITMAPCOREHEADER) != _lread (fh, (LPSTR)lpbi, sizeof(BITMAPCOREHEADER)))
457 goto ErrExit;
458
459 if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
460 {
461 lpbi->biSize = sizeof(BITMAPINFOHEADER);
462 lpbi->biBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
463 lpbi->biPlanes = ((LPBITMAPCOREHEADER)lpbi)->bcPlanes;
464 lpbi->biHeight = ((LPBITMAPCOREHEADER)lpbi)->bcHeight;
465 lpbi->biWidth = ((LPBITMAPCOREHEADER)lpbi)->bcWidth;
466 bCoreHead = TRUE;
467 }
468 else
469 {
470 // get to the start of the header and read INFOHEADER
471 _llseek(fh,sizeof(BITMAPFILEHEADER),SEEK_SET);
472 if (sizeof(BITMAPINFOHEADER) != _lread (fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)))
473 goto ErrExit;
474 }
475
476 nNumColors = (WORD)lpbi->biClrUsed;
477 if ( nNumColors == 0 )
478 {
479 /* no color table for 24-bit, default size otherwise */
480 if (lpbi->biBitCount != 24)
481 nNumColors = 1 << lpbi->biBitCount; /* standard size table */
482 }
483
484 /* fill in some default values if they are zero */
485 if (lpbi->biClrUsed == 0)
486 lpbi->biClrUsed = nNumColors;
487
488 if (lpbi->biSizeImage == 0)
489 {
490 lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) + 31) & ~31) >> 3)
491 * lpbi->biHeight;
492 }
493
494 /* get a proper-sized buffer for header, color table and bits */
495 GlobalUnlock(hDIB);
496 hDIB = GlobalReAlloc(hDIB, lpbi->biSize +
497 nNumColors * sizeof(RGBQUAD) +
498 lpbi->biSizeImage, 0);
499 if (!hDIB) /* can't resize buffer for loading */
500 goto ErrExit2;
501
502 #ifdef __WINDOWS_386__
503 lpbi = (LPBITMAPINFOHEADER)MK_FP32(GlobalLock(hDIB));
504 #else
505 lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
506 #endif
507
508 /* read the color table */
509 if (!bCoreHead)
510 _lread(fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBQUAD));
511 else
512 {
513 signed int i;
514 RGBQUAD FAR *pQuad;
515 RGBTRIPLE FAR *pTriple;
516
517 _lread(fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBTRIPLE));
518
519 pQuad = (RGBQUAD FAR *)((LPSTR)lpbi + lpbi->biSize);
520 pTriple = (RGBTRIPLE FAR *) pQuad;
521 for (i = nNumColors - 1; i >= 0; i--)
522 {
523 pQuad[i].rgbRed = pTriple[i].rgbtRed;
524 pQuad[i].rgbBlue = pTriple[i].rgbtBlue;
525 pQuad[i].rgbGreen = pTriple[i].rgbtGreen;
526 pQuad[i].rgbReserved = 0;
527 }
528 }
529
530 /* offset to the bits from start of DIB header */
531 offBits = (WORD)lpbi->biSize + nNumColors * sizeof(RGBQUAD);
532
533 if (bf.bfOffBits != 0L)
534 {
535 _llseek(fh,bf.bfOffBits,SEEK_SET);
536 }
537
538 if (lpbi->biSizeImage == lread(fh, (LPSTR)lpbi + offBits, lpbi->biSizeImage))
539 {
540 GlobalUnlock(hDIB);
541
542 hDC = GetDC(NULL);
543 if (!MakeBitmapAndPalette(hDC, hDIB, palette,
544 bitmap))
545 {
546 ReleaseDC(NULL,hDC);
547 goto ErrExit2;
548 }
549 else
550 {
551 ReleaseDC(NULL,hDC);
552 GlobalFree(hDIB);
553 result = TRUE;
554 }
555 }
556 else
557 {
558 ErrExit:
559 GlobalUnlock(hDIB);
560 ErrExit2:
561 GlobalFree(hDIB);
562 }
563
564 _lclose(fh);
565 return(result);
566 }
567
568 /****************************************************************************
569 *
570 * FUNCTION : MakeBitmapAndPalette
571 *
572 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
573 * to be used for a device-dependent representation of
574 * of the image.
575 *
576 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
577 * appropriate handles. Caller is responsible
578 * for freeing objects.
579 * FALSE --> unable to create objects. both pointer are
580 * not valid
581 *
582 ****************************************************************************/
583 BOOL PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB,
584 HPALETTE * phPal, HBITMAP * phBitmap)
585 {
586 LPBITMAPINFOHEADER lpInfo;
587 BOOL result = FALSE;
588 HBITMAP hBitmap;
589 HPALETTE hPalette, hOldPal;
590 LPSTR lpBits;
591
592 #ifdef __WINDOWS_386__
593 lpInfo = (LPBITMAPINFOHEADER) MK_FP32(GlobalLock(hDIB));
594 #else
595 lpInfo = (LPBITMAPINFOHEADER) GlobalLock(hDIB);
596 #endif
597
598 hPalette = MakeDIBPalette(lpInfo);
599 if ( hPalette )
600 {
601 // Need to realize palette for converting DIB to bitmap.
602 hOldPal = SelectPalette(hDC, hPalette, TRUE);
603 RealizePalette(hDC);
604
605 lpBits = (LPSTR)lpInfo + (WORD)lpInfo->biSize +
606 (WORD)lpInfo->biClrUsed * sizeof(RGBQUAD);
607 hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits,
608 (LPBITMAPINFO)lpInfo, DIB_RGB_COLORS);
609
610 SelectPalette(hDC, hOldPal, TRUE);
611 RealizePalette(hDC);
612
613 if (!hBitmap)
614 DeleteObject(hPalette);
615 else
616 {
617 *phBitmap = hBitmap;
618 *phPal = hPalette;
619 result = TRUE;
620 }
621 }
622 return(result);
623 }
624
625 /****************************************************************************
626 * *
627 * FUNCTION : MakeDIBPalette(lpInfo) *
628 * *
629 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
630 * the color table.
631 *
632 * *
633 * RETURNS : non-zero - handle of a corresponding palette
634 * zero - unable to create palette
635 * *
636 ****************************************************************************/
637 HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER lpInfo)
638 {
639 NPLOGPALETTE npPal;
640 RGBQUAD far *lpRGB;
641 HPALETTE hLogPal;
642 WORD i;
643
644 /* since biClrUsed field was filled during the loading of the DIB,
645 ** we know it contains the number of colors in the color table.
646 */
647 if (lpInfo->biClrUsed)
648 {
649 /*
650 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
651 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
652 */
653 npPal = (NPLOGPALETTE)malloc(sizeof(LOGPALETTE) +
654 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
655
656 if (!npPal)
657 return(FALSE);
658
659 npPal->palVersion = 0x300;
660 npPal->palNumEntries = (WORD)lpInfo->biClrUsed;
661
662 /* get pointer to the color table */
663 lpRGB = (RGBQUAD FAR *)((LPSTR)lpInfo + lpInfo->biSize);
664
665 /* copy colors from the color table to the LogPalette structure */
666 for (i = 0; i < lpInfo->biClrUsed; i++, lpRGB++)
667 {
668 npPal->palPalEntry[i].peRed = lpRGB->rgbRed;
669 npPal->palPalEntry[i].peGreen = lpRGB->rgbGreen;
670 npPal->palPalEntry[i].peBlue = lpRGB->rgbBlue;
671 npPal->palPalEntry[i].peFlags = 0;
672 }
673
674 hLogPal = CreatePalette((LPLOGPALETTE)npPal);
675 // LocalFree((HANDLE)npPal);
676 free(npPal);
677
678 return(hLogPal);
679 }
680
681 /* 24-bit DIB with no color table. return default palette. Another
682 ** option would be to create a 256 color "rainbow" palette to provide
683 ** some good color choices.
684 */
685 else
686 return((HPALETTE) GetStockObject(DEFAULT_PALETTE));
687 }
688
689 bool wxLoadIntoBitmap(char *filename, wxBitmap *bitmap, wxColourMap **pal)
690 {
691 HBITMAP hBitmap;
692 HPALETTE hPalette;
693
694 bool success = (ReadDIB(filename, &hBitmap, &hPalette) != 0);
695
696 if (!success)
697 {
698 DeleteObject(hPalette);
699 return FALSE;
700 }
701
702 if (hPalette)
703 {
704 if (pal)
705 {
706 *pal = new wxColourMap;
707 (*pal)->SetHPALETTE((WXHPALETTE) hPalette);
708 }
709 else
710 DeleteObject(hPalette);
711 }
712 else if (pal)
713 *pal = NULL;
714
715 if (hBitmap)
716 {
717 BITMAP bm;
718 GetObject(hBitmap, sizeof(bm), (LPSTR)&bm);
719
720 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
721 bitmap->SetWidth(bm.bmWidth);
722 bitmap->SetHeight(bm.bmHeight);
723 bitmap->SetDepth(bm.bmPlanes * bm.bmBitsPixel);
724 bitmap->SetOk(TRUE);
725 return TRUE;
726 }
727 else return FALSE;
728 }
729
730 wxBitmap *wxLoadBitmap(char *filename, wxColourMap **pal)
731 {
732 wxBitmap *bitmap = new wxBitmap;
733 if (wxLoadIntoBitmap(filename, bitmap, pal))
734 return bitmap;
735 else
736 {
737 delete bitmap;
738 return NULL;
739 }
740 }
741
742 //---------------------------------------------------------------------
743 //
744 // Function: InitBitmapInfoHeader
745 //
746 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
747 // given the Width, Height, and Bits per Pixel for the
748 // DIB.
749 //
750 // By standard, I mean that all the relevant fields are set
751 // to the specified values. biSizeImage is computed, the
752 // biCompression field is set to "no compression," and all
753 // other fields are 0.
754 //
755 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
756 // 24. This routine makes sure that one of these values is
757 // used (whichever is most appropriate for the specified
758 // nBPP).
759 //
760 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
761 // to be filled in.
762 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
763 // word MUST be 0).
764 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
765 // word MUST be 0).
766 // nBPP == Bits per Pixel for the DIB.
767 //
768 // History: Date Reason
769 // 11/07/91 Created
770 //
771 //---------------------------------------------------------------------
772
773 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr,
774 DWORD dwWidth,
775 DWORD dwHeight,
776 int nBPP)
777 {
778 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
779 memset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
780
781 lpBmInfoHdr->biSize = sizeof (BITMAPINFOHEADER);
782 lpBmInfoHdr->biWidth = dwWidth;
783 lpBmInfoHdr->biHeight = dwHeight;
784 lpBmInfoHdr->biPlanes = 1;
785
786 if (nBPP <= 1)
787 nBPP = 1;
788 else if (nBPP <= 4)
789 nBPP = 4;
790 else if (nBPP <= 8)
791 nBPP = 8;
792 /* Doesn't work
793 else if (nBPP <= 16)
794 nBPP = 16;
795 */
796 else
797 nBPP = 24;
798
799 lpBmInfoHdr->biBitCount = nBPP;
800 lpBmInfoHdr->biSizeImage = WIDTHBYTES (dwWidth * nBPP) * dwHeight;
801 }
802
803
804
805
806 LPSTR FindDIBBits (LPSTR lpbi)
807 {
808 return (lpbi + *(LPDWORD)lpbi + PaletteSize (lpbi));
809 }
810
811 //---------------------------------------------------------------------
812 //
813 // Function: BitmapToDIB
814 //
815 // Purpose: Given a device dependent bitmap and a palette, returns
816 // a handle to global memory with a DIB spec in it. The
817 // DIB is rendered using the colors of the palette passed in.
818 //
819 // Stolen almost verbatim from ShowDIB.
820 //
821 // Parms: hBitmap == Handle to device dependent bitmap compatible
822 // with default screen display device.
823 // hPal == Palette to render the DDB with. If it's NULL,
824 // use the default palette.
825 //
826 // History: Date Reason
827 // 6/01/91 Created
828 //
829 //---------------------------------------------------------------------
830
831 HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal)
832 {
833 BITMAP Bitmap;
834 BITMAPINFOHEADER bmInfoHdr;
835 LPBITMAPINFOHEADER lpbmInfoHdr;
836 LPSTR lpBits;
837 HDC hMemDC;
838 HANDLE hDIB;
839 HPALETTE hOldPal = NULL;
840
841 // Do some setup -- make sure the Bitmap passed in is valid,
842 // get info on the bitmap (like its height, width, etc.),
843 // then setup a BITMAPINFOHEADER.
844
845 if (!hBitmap)
846 return NULL;
847
848 if (!GetObject (hBitmap, sizeof (Bitmap), (LPSTR) &Bitmap))
849 return NULL;
850
851 InitBitmapInfoHeader (&bmInfoHdr,
852 Bitmap.bmWidth,
853 Bitmap.bmHeight,
854 Bitmap.bmPlanes * Bitmap.bmBitsPixel);
855
856
857 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
858 // into this memory, and find out where the bitmap bits go.
859
860 hDIB = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER) +
861 PaletteSize ((LPSTR) &bmInfoHdr) + bmInfoHdr.biSizeImage);
862
863 if (!hDIB)
864 return NULL;
865
866 #ifdef __WINDOWS_386__
867 lpbmInfoHdr = (LPBITMAPINFOHEADER) MK_FP32(GlobalLock (hDIB));
868 #else
869 lpbmInfoHdr = (LPBITMAPINFOHEADER) GlobalLock (hDIB);
870 #endif
871
872 *lpbmInfoHdr = bmInfoHdr;
873 lpBits = FindDIBBits ((LPSTR) lpbmInfoHdr);
874
875
876 // Now, we need a DC to hold our bitmap. If the app passed us
877 // a palette, it should be selected into the DC.
878
879 hMemDC = GetDC (NULL);
880
881 if (hPal)
882 {
883 hOldPal = SelectPalette (hMemDC, hPal, FALSE);
884 RealizePalette (hMemDC);
885 }
886
887
888
889 // We're finally ready to get the DIB. Call the driver and let
890 // it party on our bitmap. It will fill in the color table,
891 // and bitmap bits of our global memory block.
892
893 if (!GetDIBits (hMemDC,
894 hBitmap,
895 0,
896 Bitmap.bmHeight,
897 lpBits,
898 (LPBITMAPINFO) lpbmInfoHdr,
899 DIB_RGB_COLORS))
900 {
901 GlobalUnlock (hDIB);
902 GlobalFree (hDIB);
903 hDIB = NULL;
904 }
905 else
906 GlobalUnlock (hDIB);
907
908
909 // Finally, clean up and return.
910
911 if (hOldPal)
912 SelectPalette (hMemDC, hOldPal, FALSE);
913
914 ReleaseDC (NULL, hMemDC);
915
916 return hDIB;
917 }
918
919 bool wxSaveBitmap(char *filename, wxBitmap *bitmap, wxColourMap *colourmap)
920 {
921 HPALETTE hPalette = 0;
922 if (colourmap)
923 hPalette = (HPALETTE) colourmap->GetHPALETTE();
924
925 HANDLE dibHandle = BitmapToDIB((HBITMAP) bitmap->GetHBITMAP(), hPalette);
926 if (dibHandle)
927 {
928 bool success = (WriteDIB(filename, dibHandle) != 0);
929 GlobalFree(dibHandle);
930 return success;
931 }
932 else return FALSE;
933 }
934
935