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