]>
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"
39 #if defined(__WIN32__)
40 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
41 #include <memory.h> // for _fmemcpy()
45 #define hmemcpy memcpy
49 #define BFT_ICON 0x4349 /* 'IC' */
50 #define BFT_BITMAP 0x4d42 /* 'BM' */
51 #define BFT_CURSOR 0x5450 /* 'PT(' */
54 /* flags for _lseek */
60 /* Copied from PNGhandler for coompilation with MingW32, RR */
62 #ifndef GlobalAllocPtr
63 #define GlobalPtrHandle(lp) \
64 ((HGLOBAL)GlobalHandle(lp))
66 #define GlobalLockPtr(lp) \
67 ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
68 #define GlobalUnlockPtr(lp) \
69 GlobalUnlock(GlobalPtrHandle(lp))
71 #define GlobalAllocPtr(flags, cb) \
72 (GlobalLock(GlobalAlloc((flags), (cb))))
73 #define GlobalReAllocPtr(lp, cbNew, flags) \
74 (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
75 #define GlobalFreePtr(lp) \
76 (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp)))
81 * Clear the System Palette so that we can ensure an identity palette
82 * mapping for fast performance.
85 void wxClearSystemPalette(void)
87 //*** A dummy palette setup
92 PALETTEENTRY aEntries
[256];
99 HPALETTE ScreenPalette
= 0;
106 // *** Reset everything in the system palette to black
107 for(Counter
= 0; Counter
< 256; Counter
++)
109 Palette
.aEntries
[Counter
].peRed
= 0;
110 Palette
.aEntries
[Counter
].peGreen
= 0;
111 Palette
.aEntries
[Counter
].peBlue
= 0;
112 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
115 // *** Create, select, realize, deselect, and delete the palette
117 ScreenDC
= GetDC((HWND
)NULL
);
119 ScreenDC
= GetDC(NULL
);
121 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
125 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
126 nMapped
= RealizePalette(ScreenDC
);
127 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
128 bOK
= DeleteObject(ScreenPalette
);
132 nOK
= ReleaseDC((HWND
)NULL
, ScreenDC
);
134 nOK
= ReleaseDC(NULL
, ScreenDC
);
142 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
149 int wxDibWriteFile(LPTSTR szFile
, LPBITMAPINFOHEADER lpbi
)
154 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_WRITE
| OF_CREATE
);
157 // printf("la regamos0");
161 long size
= wxDibSize(lpbi
);
164 BITMAPFILEHEADER bmf
;
165 bmf
.bfType
= BFT_BITMAP
;
166 bmf
.bfSize
= sizeof(bmf
) + size
;
169 bmf
.bfOffBits
= sizeof(bmf
) + (char FAR
*)(wxDibPtr(lpbi
)) - (char FAR
*)lpbi
;
170 #if 1 // defined( __WATCOMC__) || defined(__VISUALC__) || defined(__SC__) || defined(__SALFORDC__) || defined(__MWERKS__) || wxUSE_NORLANDER_HEADERS
171 #define HWRITE_2ND_ARG_TYPE LPCSTR
172 #else // don't know who needs this...
173 #define HWRITE_2ND_ARG_TYPE LPBYTE
176 if ( _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)(&bmf
), sizeof(bmf
)) < 0 ||
177 _hwrite(fh
, (HWRITE_2ND_ARG_TYPE
)lpbi
, size
) < 0 )
183 #undef HWRITE_2ND_ARG_TYPE
189 PDIB
wxDibOpenFile(LPTSTR szFile
)
198 #if defined(WIN32) || defined(_WIN32)
199 #define GetCurrentInstance() GetModuleHandle(NULL)
201 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
204 fh
= OpenFile(wxConvFile
.cWX2MB(szFile
), &of
, OF_READ
);
210 // TODO: Unicode version
212 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
214 h
= FindResourceW(GetCurrentInstance(), szFile
, RT_BITMAP
);
216 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
219 #if defined(__WIN32__)
220 //!!! can we call GlobalFree() on this? is it the right format.
221 //!!! can we write to this resource?
223 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
226 fh
= AccessResource(GetCurrentInstance(), h
);
233 pdib
= wxDibReadBitmapInfo(fh
);
238 /* How much memory do we need to hold the DIB */
240 dwBits
= pdib
->biSizeImage
;
241 dwLen
= pdib
->biSize
+ wxDibPaletteSize(pdib
) + dwBits
;
243 /* Can we get more memory? */
245 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
259 /* read in the bits */
260 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ wxDibPaletteSize(pdib
), dwBits
);
270 * ReadDibBitmapInfo()
272 * Will read a file in DIB format and return a global HANDLE to its
273 * BITMAPINFO. This function will work with both "old" and "new"
274 * bitmap formats, but will always return a "new" BITMAPINFO.
277 PDIB
wxDibReadBitmapInfo(HFILE fh
)
293 off
= _llseek(fh
,0L,SEEK_CUR
);
295 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
299 * do we have a RC HEADER?
301 if (bf
.bfType
!= BFT_BITMAP
)
304 _llseek(fh
,off
,SEEK_SET
);
307 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
311 * what type of bitmap info is this?
313 switch (size
= (int)bi
.biSize
)
316 case sizeof(BITMAPINFOHEADER
):
319 case sizeof(BITMAPCOREHEADER
):
320 bc
= *(BITMAPCOREHEADER
*)&bi
;
321 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
322 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
323 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
324 bi
.biPlanes
= (UINT
)bc
.bcPlanes
;
325 bi
.biBitCount
= (UINT
)bc
.bcBitCount
;
326 bi
.biCompression
= BI_RGB
;
328 bi
.biXPelsPerMeter
= 0;
329 bi
.biYPelsPerMeter
= 0;
331 bi
.biClrImportant
= 0;
333 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
338 nNumColors
= wxDibNumColors(&bi
);
341 if (bi
.biSizeImage
== 0)
342 bi
.biSizeImage
= DibSizeImage(&bi
);
344 if (bi
.biClrUsed
== 0)
345 bi
.biClrUsed
= wxDibNumColors(&bi
);
347 wxFixBitmapInfo(&bi
);
350 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
357 pRgb
= wxDibColors(pdib
);
361 if (size
== sizeof(BITMAPCOREHEADER
))
364 * convert a old color table (3 byte entries) to a new
365 * color table (4 byte entries)
367 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
369 for (i
=nNumColors
-1; i
>=0; i
--)
373 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
374 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
375 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
376 rgb
.rgbReserved
= (BYTE
)0;
383 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
387 if (bf
.bfOffBits
!= 0L)
388 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
394 * DibSetUsage(hdib,hpal,wUsage)
396 * Modifies the color table of the passed DIB for use with the wUsage
397 * parameter specifed.
399 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
400 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
401 * in the passed palette
404 BOOL
wxDibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
406 PALETTEENTRY ape
[256];
413 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
418 nColors
= wxDibNumColors(pdib
);
420 if (nColors
== 3 && wxDibCompression(pdib
) == BI_BITFIELDS
)
425 pRgb
= wxDibColors(pdib
);
430 // Set the DIB color table to palette indexes
433 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
438 // Set the DIB color table to RGBQUADS
442 nColors
= (nColors
< 256) ? nColors
: 256;
444 GetPaletteEntries(hpal
,0,nColors
,ape
);
446 for (n
=0; n
<nColors
; n
++)
448 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
449 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
450 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
451 pRgb
[n
].rgbReserved
= 0;
460 * DibCreate(bits, dx, dy)
462 * Creates a new packed DIB with the given dimensions and the
463 * given number of bits per pixel
466 PDIB
wxDibCreate(int bits
, int dx
, int dy
)
468 LPBITMAPINFOHEADER lpbi
;
473 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
475 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
480 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
484 lpbi
->biBitCount
= bits
;
485 lpbi
->biCompression
= BI_RGB
;
486 lpbi
->biSizeImage
= dwSizeImage
;
487 lpbi
->biXPelsPerMeter
= 0 ;
488 lpbi
->biYPelsPerMeter
= 0 ;
489 lpbi
->biClrUsed
= 0 ;
490 lpbi
->biClrImportant
= 0 ;
493 lpbi
->biClrUsed
= 16;
496 lpbi
->biClrUsed
= 256;
498 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
500 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
502 *pdw
++ = 0x00000000; // 0000 black
503 *pdw
++ = 0x00800000; // 0001 dark red
504 *pdw
++ = 0x00008000; // 0010 dark green
505 *pdw
++ = 0x00808000; // 0011 mustard
506 *pdw
++ = 0x00000080; // 0100 dark blue
507 *pdw
++ = 0x00800080; // 0101 purple
508 *pdw
++ = 0x00008080; // 0110 dark turquoise
509 *pdw
++ = 0x00C0C0C0; // 1000 gray
510 *pdw
++ = 0x00808080; // 0111 dark gray
511 *pdw
++ = 0x00FF0000; // 1001 red
512 *pdw
++ = 0x0000FF00; // 1010 green
513 *pdw
++ = 0x00FFFF00; // 1011 yellow
514 *pdw
++ = 0x000000FF; // 1100 blue
515 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
516 *pdw
++ = 0x0000FFFF; // 1110 cyan
517 *pdw
++ = 0x00FFFFFF; // 1111 white
523 static void xlatClut8(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
)++)
535 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
540 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
542 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
544 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
552 static void xlatRle8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
556 BYTE _huge
*prle
= pb
;
563 if (cnt
== RLE_ESCAPE
)
582 for (b
=0; b
<cnt
; b
++,prle
++)
598 static void xlatRle4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
602 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
612 * DibMapToPalette(pdib, hpal)
614 * Map the colors of the DIB, using GetNearestPaletteIndex, to
615 * the colors of the given palette.
618 BOOL
wxDibMapToPalette(PDIB pdib
, HPALETTE hpal
)
620 LPBITMAPINFOHEADER lpbi
;
633 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
634 lpRgb
= wxDibColors(pdib
);
636 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
637 nDibColors
= wxDibNumColors(pdib
);
639 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
640 SizeImage
= wxDibSizeImage(lpbi
);
643 // build a xlat table. from the current DIB colors to the given
646 for (n
=0; n
<nDibColors
; n
++)
647 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
649 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
650 lpbi
->biClrUsed
= nPalColors
;
655 if (nPalColors
> nDibColors
)
657 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
658 hmemmove((BYTE _huge
*)wxDibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
659 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
661 else if (nPalColors
< nDibColors
)
663 hmemcpy(wxDibPtr(lpbi
), lpBits
, SizeImage
);
664 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
665 lpBits
= (LPBYTE
)wxDibPtr(lpbi
);
669 // translate the DIB bits
671 switch (lpbi
->biCompression
)
674 xlatRle8(lpBits
, SizeImage
, xlat
);
678 xlatRle4(lpBits
, SizeImage
, xlat
);
682 if (lpbi
->biBitCount
== 8)
683 xlatClut8(lpBits
, SizeImage
, xlat
);
685 xlatClut4(lpBits
, SizeImage
, xlat
);
690 // Now copy the RGBs in the logical palette to the dib color table
692 for (n
=0; n
<nPalColors
; n
++)
694 GetPaletteEntries(hpal
,n
,1,&pe
);
696 lpRgb
[n
].rgbRed
= pe
.peRed
;
697 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
698 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
699 lpRgb
[n
].rgbReserved
= (BYTE
)0;
706 HPALETTE
wxMakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
709 const RGBQUAD FAR
* rgb
= Info
->bmiColors
;
711 WORD nColors
= Info
->bmiHeader
.biClrUsed
;
713 LOGPALETTE
* logPal
= (LOGPALETTE
*)
714 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
716 logPal
->palVersion
= 0x300; // Windows 3.0 version
717 logPal
->palNumEntries
= nColors
;
718 for (WORD n
= 0; n
< nColors
; n
++) {
719 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
720 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
721 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
722 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
724 hPalette
= ::CreatePalette(logPal
);