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