]>
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"
36 /* Why module.h? No longer finds this header.
41 #if defined(__WIN32__)
42 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
43 #include <memory.h> // for _fmemcpy()
47 #define hmemcpy memcpy
51 #define BFT_ICON 0x4349 /* 'IC' */
52 #define BFT_BITMAP 0x4d42 /* 'BM' */
53 #define BFT_CURSOR 0x5450 /* 'PT(' */
56 /* flags for _lseek */
62 /* Copied from PNGhandler for coompilation with MingW32, RR */
64 #ifndef GlobalAllocPtr
65 #define GlobalPtrHandle(lp) \
66 ((HGLOBAL)GlobalHandle(lp))
68 #define GlobalLockPtr(lp) \
69 ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
70 #define GlobalUnlockPtr(lp) \
71 GlobalUnlock(GlobalPtrHandle(lp))
73 #define GlobalAllocPtr(flags, cb) \
74 (GlobalLock(GlobalAlloc((flags), (cb))))
75 #define GlobalReAllocPtr(lp, cbNew, flags) \
76 (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
77 #define GlobalFreePtr(lp) \
78 (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp)))
83 * Clear the System Palette so that we can ensure an identity palette
84 * mapping for fast performance.
87 void wxClearSystemPalette(void)
89 //*** A dummy palette setup
94 PALETTEENTRY aEntries
[256];
101 HPALETTE ScreenPalette
= 0;
108 // *** Reset everything in the system palette to black
109 for(Counter
= 0; Counter
< 256; Counter
++)
111 Palette
.aEntries
[Counter
].peRed
= 0;
112 Palette
.aEntries
[Counter
].peGreen
= 0;
113 Palette
.aEntries
[Counter
].peBlue
= 0;
114 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
117 // *** Create, select, realize, deselect, and delete the palette
119 ScreenDC
= GetDC((HWND
)NULL
);
121 ScreenDC
= GetDC(NULL
);
123 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
127 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
128 nMapped
= RealizePalette(ScreenDC
);
129 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
130 bOK
= DeleteObject(ScreenPalette
);
134 nOK
= ReleaseDC((HWND
)NULL
, ScreenDC
);
136 nOK
= ReleaseDC(NULL
, ScreenDC
);
144 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
151 int wxDibWriteFile(LPTSTR szFile
, LPBITMAPINFOHEADER lpbi
)
156 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_WRITE
| OF_CREATE
);
159 // printf("la regamos0");
163 long size
= wxDibSize(lpbi
);
166 BITMAPFILEHEADER bmf
;
167 bmf
.bfType
= BFT_BITMAP
;
168 bmf
.bfSize
= sizeof(bmf
) + size
;
171 bmf
.bfOffBits
= sizeof(bmf
) + (char FAR
*)(wxDibPtr(lpbi
)) - (char FAR
*)lpbi
;
172 #if 1 // defined( __WATCOMC__) || defined(__VISUALC__) || defined(__SC__) || defined(__SALFORDC__) || defined(__MWERKS__) || wxUSE_NORLANDER_HEADERS
173 #define HWRITE_2ND_ARG_TYPE LPCSTR
174 #else // don't know who needs this...
175 #define HWRITE_2ND_ARG_TYPE LPBYTE
178 if ( _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)(&bmf
), sizeof(bmf
)) < 0 ||
179 _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)lpbi
, size
) < 0 )
185 #undef HWRITE_2ND_ARG_TYPE
191 PDIB
wxDibOpenFile(LPTSTR szFile
)
200 #if defined(WIN32) || defined(_WIN32)
201 #define GetCurrentInstance() GetModuleHandle(NULL)
203 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
206 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_READ
);
212 // TODO: Unicode version
214 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
216 h
= FindResourceW(GetCurrentInstance(), szFile
, RT_BITMAP
);
218 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
221 #if defined(__WIN32__)
222 //!!! can we call GlobalFree() on this? is it the right format.
223 //!!! can we write to this resource?
225 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
228 fh
= AccessResource(GetCurrentInstance(), h
);
235 pdib
= wxDibReadBitmapInfo(fh
);
240 /* How much memory do we need to hold the DIB */
242 dwBits
= pdib
->biSizeImage
;
243 dwLen
= pdib
->biSize
+ wxDibPaletteSize(pdib
) + dwBits
;
245 /* Can we get more memory? */
247 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
261 /* read in the bits */
262 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ wxDibPaletteSize(pdib
), dwBits
);
272 * ReadDibBitmapInfo()
274 * Will read a file in DIB format and return a global HANDLE to its
275 * BITMAPINFO. This function will work with both "old" and "new"
276 * bitmap formats, but will always return a "new" BITMAPINFO.
279 PDIB
wxDibReadBitmapInfo(HFILE fh
)
295 off
= _llseek(fh
,0L,SEEK_CUR
);
297 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
301 * do we have a RC HEADER?
303 if (bf
.bfType
!= BFT_BITMAP
)
306 _llseek(fh
,off
,SEEK_SET
);
309 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
313 * what type of bitmap info is this?
315 switch (size
= (int)bi
.biSize
)
318 case sizeof(BITMAPINFOHEADER
):
321 case sizeof(BITMAPCOREHEADER
):
322 bc
= *(BITMAPCOREHEADER
*)&bi
;
323 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
324 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
325 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
326 bi
.biPlanes
= (WORD
)bc
.bcPlanes
;
327 bi
.biBitCount
= (WORD
)bc
.bcBitCount
;
328 bi
.biCompression
= BI_RGB
;
330 bi
.biXPelsPerMeter
= 0;
331 bi
.biYPelsPerMeter
= 0;
333 bi
.biClrImportant
= 0;
335 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
340 nNumColors
= wxDibNumColors(&bi
);
343 if (bi
.biSizeImage
== 0)
344 bi
.biSizeImage
= DibSizeImage(&bi
);
346 if (bi
.biClrUsed
== 0)
347 bi
.biClrUsed
= wxDibNumColors(&bi
);
349 wxFixBitmapInfo(&bi
);
352 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
359 pRgb
= wxDibColors(pdib
);
363 if (size
== sizeof(BITMAPCOREHEADER
))
366 * convert a old color table (3 byte entries) to a new
367 * color table (4 byte entries)
369 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
371 for (i
=nNumColors
-1; i
>=0; i
--)
375 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
376 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
377 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
378 rgb
.rgbReserved
= (BYTE
)0;
385 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
389 if (bf
.bfOffBits
!= 0L)
390 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
396 * DibSetUsage(hdib,hpal,wUsage)
398 * Modifies the color table of the passed DIB for use with the wUsage
399 * parameter specifed.
401 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
402 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
403 * in the passed palette
406 BOOL
wxDibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
408 PALETTEENTRY ape
[256];
415 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
420 nColors
= wxDibNumColors(pdib
);
422 if (nColors
== 3 && wxDibCompression(pdib
) == BI_BITFIELDS
)
427 pRgb
= wxDibColors(pdib
);
432 // Set the DIB color table to palette indexes
435 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
440 // Set the DIB color table to RGBQUADS
444 nColors
= (nColors
< 256) ? nColors
: 256;
446 GetPaletteEntries(hpal
,0,nColors
,ape
);
448 for (n
=0; n
<nColors
; n
++)
450 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
451 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
452 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
453 pRgb
[n
].rgbReserved
= 0;
462 * DibCreate(bits, dx, dy)
464 * Creates a new packed DIB with the given dimensions and the
465 * given number of bits per pixel
468 PDIB
wxDibCreate(int bits
, int dx
, int dy
)
470 LPBITMAPINFOHEADER lpbi
;
475 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
477 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
482 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
486 lpbi
->biBitCount
= bits
;
487 lpbi
->biCompression
= BI_RGB
;
488 lpbi
->biSizeImage
= dwSizeImage
;
489 lpbi
->biXPelsPerMeter
= 0 ;
490 lpbi
->biYPelsPerMeter
= 0 ;
491 lpbi
->biClrUsed
= 0 ;
492 lpbi
->biClrImportant
= 0 ;
495 lpbi
->biClrUsed
= 16;
498 lpbi
->biClrUsed
= 256;
500 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
502 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
504 *pdw
++ = 0x00000000; // 0000 black
505 *pdw
++ = 0x00800000; // 0001 dark red
506 *pdw
++ = 0x00008000; // 0010 dark green
507 *pdw
++ = 0x00808000; // 0011 mustard
508 *pdw
++ = 0x00000080; // 0100 dark blue
509 *pdw
++ = 0x00800080; // 0101 purple
510 *pdw
++ = 0x00008080; // 0110 dark turquoise
511 *pdw
++ = 0x00C0C0C0; // 1000 gray
512 *pdw
++ = 0x00808080; // 0111 dark gray
513 *pdw
++ = 0x00FF0000; // 1001 red
514 *pdw
++ = 0x0000FF00; // 1010 green
515 *pdw
++ = 0x00FFFF00; // 1011 yellow
516 *pdw
++ = 0x000000FF; // 1100 blue
517 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
518 *pdw
++ = 0x0000FFFF; // 1110 cyan
519 *pdw
++ = 0x00FFFFFF; // 1111 white
525 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
530 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
532 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
537 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
542 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
544 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
546 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
554 static void xlatRle8(BYTE FAR
*pb
, DWORD
WXUNUSED(dwSize
), BYTE FAR
*xlat
)
558 BYTE _huge
*prle
= pb
;
565 if (cnt
== RLE_ESCAPE
)
584 for (b
=0; b
<cnt
; b
++,prle
++)
600 static void xlatRle4(BYTE FAR
*WXUNUSED(pb
), DWORD
WXUNUSED(dwSize
), BYTE FAR
*WXUNUSED(xlat
))
604 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
614 * DibMapToPalette(pdib, hpal)
616 * Map the colors of the DIB, using GetNearestPaletteIndex, to
617 * the colors of the given palette.
620 BOOL
wxDibMapToPalette(PDIB pdib
, HPALETTE hpal
)
622 LPBITMAPINFOHEADER lpbi
;
635 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
636 lpRgb
= wxDibColors(pdib
);
638 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
639 nDibColors
= wxDibNumColors(pdib
);
641 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
642 SizeImage
= wxDibSizeImage(lpbi
);
645 // build a xlat table. from the current DIB colors to the given
648 for (n
=0; n
<nDibColors
; n
++)
649 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
651 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
652 lpbi
->biClrUsed
= nPalColors
;
657 if (nPalColors
> nDibColors
)
659 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
660 hmemmove((BYTE _huge
*)wxDibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
661 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
663 else if (nPalColors
< nDibColors
)
665 hmemcpy(wxDibPtr(lpbi
), lpBits
, SizeImage
);
666 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
667 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
671 // translate the DIB bits
673 switch (lpbi
->biCompression
)
676 xlatRle8(lpBits
, SizeImage
, xlat
);
680 xlatRle4(lpBits
, SizeImage
, xlat
);
684 if (lpbi
->biBitCount
== 8)
685 xlatClut8(lpBits
, SizeImage
, xlat
);
687 xlatClut4(lpBits
, SizeImage
, xlat
);
692 // Now copy the RGBs in the logical palette to the dib color table
694 for (n
=0; n
<nPalColors
; n
++)
696 GetPaletteEntries(hpal
,n
,1,&pe
);
698 lpRgb
[n
].rgbRed
= pe
.peRed
;
699 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
700 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
701 lpRgb
[n
].rgbReserved
= (BYTE
)0;
708 HPALETTE
wxMakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
711 const RGBQUAD FAR
* rgb
= Info
->bmiColors
;
713 WORD nColors
= (WORD
)Info
->bmiHeader
.biClrUsed
;
715 LOGPALETTE
* logPal
= (LOGPALETTE
*)
716 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
718 logPal
->palVersion
= 0x300; // Windows 3.0 version
719 logPal
->palNumEntries
= nColors
;
720 for (WORD n
= 0; n
< nColors
; n
++) {
721 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
722 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
723 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
724 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
726 hPalette
= ::CreatePalette(logPal
);