]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dibutils.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utilities for DIBs
4 // Author: Julian Smart
8 // Copyright: (c) Microsoft, Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dibutils.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/string.h"
33 #include "wx/msw/dibutils.h"
35 #if defined(__WIN32__)
36 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
37 #include <memory.h> // for _fmemcpy()
41 #define hmemcpy memcpy
45 #define BFT_ICON 0x4349 /* 'IC' */
46 #define BFT_BITMAP 0x4d42 /* 'BM' */
47 #define BFT_CURSOR 0x5450 /* 'PT(' */
50 /* flags for _lseek */
56 /* Copied from PNGhandler for coompilation with MingW32, RR */
58 #ifndef GlobalAllocPtr
59 #define GlobalPtrHandle(lp) \
60 ((HGLOBAL)GlobalHandle(lp))
62 #define GlobalLockPtr(lp) \
63 ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
64 #define GlobalUnlockPtr(lp) \
65 GlobalUnlock(GlobalPtrHandle(lp))
67 #define GlobalAllocPtr(flags, cb) \
68 (GlobalLock(GlobalAlloc((flags), (cb))))
69 #define GlobalReAllocPtr(lp, cbNew, flags) \
70 (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
71 #define GlobalFreePtr(lp) \
72 (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp)))
77 * Clear the System Palette so that we can ensure an identity palette
78 * mapping for fast performance.
81 void wxClearSystemPalette(void)
83 //*** A dummy palette setup
88 PALETTEENTRY aEntries
[256];
95 HPALETTE ScreenPalette
= 0;
102 // *** Reset everything in the system palette to black
103 for(Counter
= 0; Counter
< 256; Counter
++)
105 Palette
.aEntries
[Counter
].peRed
= 0;
106 Palette
.aEntries
[Counter
].peGreen
= 0;
107 Palette
.aEntries
[Counter
].peBlue
= 0;
108 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
111 // *** Create, select, realize, deselect, and delete the palette
112 ScreenDC
= GetDC(NULL
);
113 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
117 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
118 nMapped
= RealizePalette(ScreenDC
);
119 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
120 bOK
= DeleteObject(ScreenPalette
);
123 nOK
= ReleaseDC(NULL
, ScreenDC
);
130 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
137 int wxDibWriteFile(LPTSTR szFile
, LPBITMAPINFOHEADER lpbi
)
142 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_WRITE
| OF_CREATE
);
145 // printf("la regamos0");
149 long size
= wxDibSize(lpbi
);
152 BITMAPFILEHEADER bmf
;
153 bmf
.bfType
= BFT_BITMAP
;
154 bmf
.bfSize
= sizeof(bmf
) + size
;
157 bmf
.bfOffBits
= sizeof(bmf
) + (char FAR
*)(wxDibPtr(lpbi
)) - (char FAR
*)lpbi
;
158 #if 1 // defined( __WATCOMC__) || defined(__VISUALC__) || defined(__SC__) || defined(__SALFORDC__) || defined(__MWERKS__) || wxUSE_NORLANDER_HEADERS
159 #define HWRITE_2ND_ARG_TYPE LPCSTR
160 #else // don't know who needs this...
161 #define HWRITE_2ND_ARG_TYPE LPBYTE
164 if ( _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)(&bmf
), sizeof(bmf
)) < 0 ||
165 _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)lpbi
, size
) < 0 )
171 #undef HWRITE_2ND_ARG_TYPE
177 PDIB
wxDibOpenFile(LPTSTR szFile
)
186 #if defined(__WIN32__)
187 #define GetCurrentInstance() GetModuleHandle(NULL)
189 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
192 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_READ
);
198 // TODO: Unicode version
200 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
202 h
= FindResourceW(GetCurrentInstance(), szFile
, RT_BITMAP
);
204 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
207 #if defined(__WIN32__)
208 //!!! can we call GlobalFree() on this? is it the right format.
209 //!!! can we write to this resource?
211 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
214 fh
= AccessResource(GetCurrentInstance(), h
);
221 pdib
= wxDibReadBitmapInfo(fh
);
226 /* How much memory do we need to hold the DIB */
228 dwBits
= pdib
->biSizeImage
;
229 dwLen
= pdib
->biSize
+ wxDibPaletteSize(pdib
) + dwBits
;
231 /* Can we get more memory? */
233 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
247 /* read in the bits */
248 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ wxDibPaletteSize(pdib
), dwBits
);
258 * ReadDibBitmapInfo()
260 * Will read a file in DIB format and return a global HANDLE to its
261 * BITMAPINFO. This function will work with both "old" and "new"
262 * bitmap formats, but will always return a "new" BITMAPINFO.
265 PDIB
wxDibReadBitmapInfo(HFILE fh
)
281 off
= _llseek(fh
,0L,SEEK_CUR
);
283 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
287 * do we have a RC HEADER?
289 if (bf
.bfType
!= BFT_BITMAP
)
292 _llseek(fh
,off
,SEEK_SET
);
295 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
299 * what type of bitmap info is this?
301 switch (size
= (int)bi
.biSize
)
304 case sizeof(BITMAPINFOHEADER
):
307 case sizeof(BITMAPCOREHEADER
):
308 bc
= *(BITMAPCOREHEADER
*)&bi
;
309 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
310 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
311 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
312 bi
.biPlanes
= (WORD
)bc
.bcPlanes
;
313 bi
.biBitCount
= (WORD
)bc
.bcBitCount
;
314 bi
.biCompression
= BI_RGB
;
316 bi
.biXPelsPerMeter
= 0;
317 bi
.biYPelsPerMeter
= 0;
319 bi
.biClrImportant
= 0;
321 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
326 nNumColors
= wxDibNumColors(&bi
);
329 if (bi
.biSizeImage
== 0)
330 bi
.biSizeImage
= DibSizeImage(&bi
);
332 if (bi
.biClrUsed
== 0)
333 bi
.biClrUsed
= wxDibNumColors(&bi
);
335 wxFixBitmapInfo(&bi
);
338 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
345 pRgb
= wxDibColors(pdib
);
349 if (size
== sizeof(BITMAPCOREHEADER
))
352 * convert a old color table (3 byte entries) to a new
353 * color table (4 byte entries)
355 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
357 for (i
=nNumColors
-1; i
>=0; i
--)
361 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
362 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
363 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
364 rgb
.rgbReserved
= (BYTE
)0;
371 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
375 if (bf
.bfOffBits
!= 0L)
376 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
382 * DibSetUsage(hdib,hpal,wUsage)
384 * Modifies the color table of the passed DIB for use with the wUsage
385 * parameter specifed.
387 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
388 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
389 * in the passed palette
392 BOOL
wxDibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
394 PALETTEENTRY ape
[256];
401 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
406 nColors
= wxDibNumColors(pdib
);
408 if (nColors
== 3 && wxDibCompression(pdib
) == BI_BITFIELDS
)
413 pRgb
= wxDibColors(pdib
);
418 // Set the DIB color table to palette indexes
421 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
426 // Set the DIB color table to RGBQUADS
430 nColors
= (nColors
< 256) ? nColors
: 256;
432 GetPaletteEntries(hpal
,0,nColors
,ape
);
434 for (n
=0; n
<nColors
; n
++)
436 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
437 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
438 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
439 pRgb
[n
].rgbReserved
= 0;
448 * DibCreate(bits, dx, dy)
450 * Creates a new packed DIB with the given dimensions and the
451 * given number of bits per pixel
454 PDIB
wxDibCreate(int bits
, int dx
, int dy
)
456 LPBITMAPINFOHEADER lpbi
;
461 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
463 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
468 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
472 lpbi
->biBitCount
= bits
;
473 lpbi
->biCompression
= BI_RGB
;
474 lpbi
->biSizeImage
= dwSizeImage
;
475 lpbi
->biXPelsPerMeter
= 0 ;
476 lpbi
->biYPelsPerMeter
= 0 ;
477 lpbi
->biClrUsed
= 0 ;
478 lpbi
->biClrImportant
= 0 ;
481 lpbi
->biClrUsed
= 16;
484 lpbi
->biClrUsed
= 256;
486 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
488 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
490 *pdw
++ = 0x00000000; // 0000 black
491 *pdw
++ = 0x00800000; // 0001 dark red
492 *pdw
++ = 0x00008000; // 0010 dark green
493 *pdw
++ = 0x00808000; // 0011 mustard
494 *pdw
++ = 0x00000080; // 0100 dark blue
495 *pdw
++ = 0x00800080; // 0101 purple
496 *pdw
++ = 0x00008080; // 0110 dark turquoise
497 *pdw
++ = 0x00C0C0C0; // 1000 gray
498 *pdw
++ = 0x00808080; // 0111 dark gray
499 *pdw
++ = 0x00FF0000; // 1001 red
500 *pdw
++ = 0x0000FF00; // 1010 green
501 *pdw
++ = 0x00FFFF00; // 1011 yellow
502 *pdw
++ = 0x000000FF; // 1100 blue
503 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
504 *pdw
++ = 0x0000FFFF; // 1110 cyan
505 *pdw
++ = 0x00FFFFFF; // 1111 white
511 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
516 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
518 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
523 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
528 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
530 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
532 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
540 static void xlatRle8(BYTE FAR
*pb
, DWORD
WXUNUSED(dwSize
), BYTE FAR
*xlat
)
544 BYTE _huge
*prle
= pb
;
551 if (cnt
== RLE_ESCAPE
)
570 for (b
=0; b
<cnt
; b
++,prle
++)
586 static void xlatRle4(BYTE FAR
*WXUNUSED(pb
), DWORD
WXUNUSED(dwSize
), BYTE FAR
*WXUNUSED(xlat
))
590 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
600 * DibMapToPalette(pdib, hpal)
602 * Map the colors of the DIB, using GetNearestPaletteIndex, to
603 * the colors of the given palette.
606 BOOL
wxDibMapToPalette(PDIB pdib
, HPALETTE hpal
)
608 LPBITMAPINFOHEADER lpbi
;
621 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
622 lpRgb
= wxDibColors(pdib
);
624 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
625 nDibColors
= wxDibNumColors(pdib
);
627 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
628 SizeImage
= wxDibSizeImage(lpbi
);
631 // build a xlat table. from the current DIB colors to the given
634 for (n
=0; n
<nDibColors
; n
++)
635 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
637 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
638 lpbi
->biClrUsed
= nPalColors
;
643 if (nPalColors
> nDibColors
)
645 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
646 hmemmove((BYTE _huge
*)wxDibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
647 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
649 else if (nPalColors
< nDibColors
)
651 hmemcpy(wxDibPtr(lpbi
), lpBits
, SizeImage
);
652 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
653 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
657 // translate the DIB bits
659 switch (lpbi
->biCompression
)
662 xlatRle8(lpBits
, SizeImage
, xlat
);
666 xlatRle4(lpBits
, SizeImage
, xlat
);
670 if (lpbi
->biBitCount
== 8)
671 xlatClut8(lpBits
, SizeImage
, xlat
);
673 xlatClut4(lpBits
, SizeImage
, xlat
);
678 // Now copy the RGBs in the logical palette to the dib color table
680 for (n
=0; n
<nPalColors
; n
++)
682 GetPaletteEntries(hpal
,n
,1,&pe
);
684 lpRgb
[n
].rgbRed
= pe
.peRed
;
685 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
686 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
687 lpRgb
[n
].rgbReserved
= (BYTE
)0;
694 HPALETTE
wxMakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
697 const RGBQUAD FAR
* rgb
= Info
->bmiColors
;
699 WORD nColors
= (WORD
)Info
->bmiHeader
.biClrUsed
;
701 LOGPALETTE
* logPal
= (LOGPALETTE
*)
702 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
704 logPal
->palVersion
= 0x300; // Windows 3.0 version
705 logPal
->palNumEntries
= nColors
;
706 for (WORD n
= 0; n
< nColors
; n
++) {
707 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
708 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
709 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
710 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
712 hPalette
= ::CreatePalette(logPal
);