]>
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"
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' */
53 /* flags for _lseek */
60 * Clear the System Palette so that we can ensure an identity palette
61 * mapping for fast performance.
64 void ClearSystemPalette(void)
66 //*** A dummy palette setup
71 PALETTEENTRY aEntries
[256];
78 HPALETTE ScreenPalette
= 0;
85 // *** Reset everything in the system palette to black
86 for(Counter
= 0; Counter
< 256; Counter
++)
88 Palette
.aEntries
[Counter
].peRed
= 0;
89 Palette
.aEntries
[Counter
].peGreen
= 0;
90 Palette
.aEntries
[Counter
].peBlue
= 0;
91 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
94 // *** Create, select, realize, deselect, and delete the palette
96 ScreenDC
= GetDC((HWND
)NULL
);
98 ScreenDC
= GetDC(NULL
);
100 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
104 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
105 nMapped
= RealizePalette(ScreenDC
);
106 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
107 bOK
= DeleteObject(ScreenPalette
);
111 nOK
= ReleaseDC((HWND
)NULL
, ScreenDC
);
113 nOK
= ReleaseDC(NULL
, ScreenDC
);
121 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
128 int DibWriteFile(LPSTR szFile
, LPBITMAPINFOHEADER lpbi
)
133 fh
= OpenFile(szFile
, &of
, OF_WRITE
| OF_CREATE
);
136 // printf("la regamos0");
140 long size
= DibSize(lpbi
);
143 BITMAPFILEHEADER bmf
;
145 bmf
.bfSize
= sizeof(bmf
) + size
;
148 bmf
.bfOffBits
= sizeof(bmf
) + (char far
*)(DibPtr(lpbi
)) - (char far
*)lpbi
;
149 #if defined( __WATCOMC__) || defined(__VISUALC__) || defined(__SC__) || defined(__SALFORDC__) || defined(__MWERKS__)
150 if (_hwrite(fh
, (LPCSTR
)(&bmf
), sizeof(bmf
))<0 ||
151 _hwrite(fh
, (LPCSTR
)lpbi
, size
)<0) {
153 // printf("la regamos1");
157 if (_hwrite(fh
, (LPBYTE
)(&bmf
), sizeof(bmf
))<0 ||
158 _hwrite(fh
, (LPBYTE
)lpbi
, size
)<0) {
160 // printf("la regamos1");
169 PDIB
DibOpenFile(LPSTR szFile
)
178 #if defined(WIN32) || defined(_WIN32)
179 #define GetCurrentInstance() GetModuleHandle(NULL)
181 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
184 fh
= OpenFile(szFile
, &of
, OF_READ
);
190 // TODO: Unicode version
192 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
194 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
197 #if defined(__WIN32__)
198 //!!! can we call GlobalFree() on this? is it the right format.
199 //!!! can we write to this resource?
201 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
204 fh
= AccessResource(GetCurrentInstance(), h
);
211 pdib
= DibReadBitmapInfo(fh
);
216 /* How much memory do we need to hold the DIB */
218 dwBits
= pdib
->biSizeImage
;
219 dwLen
= pdib
->biSize
+ DibPaletteSize(pdib
) + dwBits
;
221 /* Can we get more memory? */
223 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
237 /* read in the bits */
238 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ DibPaletteSize(pdib
), dwBits
);
248 * ReadDibBitmapInfo()
250 * Will read a file in DIB format and return a global HANDLE to its
251 * BITMAPINFO. This function will work with both "old" and "new"
252 * bitmap formats, but will always return a "new" BITMAPINFO.
255 PDIB
DibReadBitmapInfo(HFILE fh
)
271 off
= _llseek(fh
,0L,SEEK_CUR
);
273 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
277 * do we have a RC HEADER?
279 if (bf
.bfType
!= BFT_BITMAP
)
282 _llseek(fh
,off
,SEEK_SET
);
285 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
289 * what type of bitmap info is this?
291 switch (size
= (int)bi
.biSize
)
294 case sizeof(BITMAPINFOHEADER
):
297 case sizeof(BITMAPCOREHEADER
):
298 bc
= *(BITMAPCOREHEADER
*)&bi
;
299 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
300 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
301 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
302 bi
.biPlanes
= (UINT
)bc
.bcPlanes
;
303 bi
.biBitCount
= (UINT
)bc
.bcBitCount
;
304 bi
.biCompression
= BI_RGB
;
306 bi
.biXPelsPerMeter
= 0;
307 bi
.biYPelsPerMeter
= 0;
309 bi
.biClrImportant
= 0;
311 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
316 nNumColors
= DibNumColors(&bi
);
319 if (bi
.biSizeImage
== 0)
320 bi
.biSizeImage
= DibSizeImage(&bi
);
322 if (bi
.biClrUsed
== 0)
323 bi
.biClrUsed
= DibNumColors(&bi
);
328 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
335 pRgb
= DibColors(pdib
);
339 if (size
== sizeof(BITMAPCOREHEADER
))
342 * convert a old color table (3 byte entries) to a new
343 * color table (4 byte entries)
345 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
347 for (i
=nNumColors
-1; i
>=0; i
--)
351 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
352 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
353 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
354 rgb
.rgbReserved
= (BYTE
)0;
361 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
365 if (bf
.bfOffBits
!= 0L)
366 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
372 * DibSetUsage(hdib,hpal,wUsage)
374 * Modifies the color table of the passed DIB for use with the wUsage
375 * parameter specifed.
377 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
378 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
379 * in the passed palette
382 BOOL
DibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
384 PALETTEENTRY ape
[256];
391 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
396 nColors
= DibNumColors(pdib
);
398 if (nColors
== 3 && DibCompression(pdib
) == BI_BITFIELDS
)
403 pRgb
= DibColors(pdib
);
408 // Set the DIB color table to palette indexes
411 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
416 // Set the DIB color table to RGBQUADS
420 nColors
= (nColors
< 256) ? nColors
: 256;
422 GetPaletteEntries(hpal
,0,nColors
,ape
);
424 for (n
=0; n
<nColors
; n
++)
426 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
427 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
428 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
429 pRgb
[n
].rgbReserved
= 0;
438 * DibCreate(bits, dx, dy)
440 * Creates a new packed DIB with the given dimensions and the
441 * given number of bits per pixel
444 PDIB
DibCreate(int bits
, int dx
, int dy
)
446 LPBITMAPINFOHEADER lpbi
;
451 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
453 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
458 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
462 lpbi
->biBitCount
= bits
;
463 lpbi
->biCompression
= BI_RGB
;
464 lpbi
->biSizeImage
= dwSizeImage
;
465 lpbi
->biXPelsPerMeter
= 0 ;
466 lpbi
->biYPelsPerMeter
= 0 ;
467 lpbi
->biClrUsed
= 0 ;
468 lpbi
->biClrImportant
= 0 ;
471 lpbi
->biClrUsed
= 16;
474 lpbi
->biClrUsed
= 256;
476 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
478 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
480 *pdw
++ = 0x00000000; // 0000 black
481 *pdw
++ = 0x00800000; // 0001 dark red
482 *pdw
++ = 0x00008000; // 0010 dark green
483 *pdw
++ = 0x00808000; // 0011 mustard
484 *pdw
++ = 0x00000080; // 0100 dark blue
485 *pdw
++ = 0x00800080; // 0101 purple
486 *pdw
++ = 0x00008080; // 0110 dark turquoise
487 *pdw
++ = 0x00C0C0C0; // 1000 gray
488 *pdw
++ = 0x00808080; // 0111 dark gray
489 *pdw
++ = 0x00FF0000; // 1001 red
490 *pdw
++ = 0x0000FF00; // 1010 green
491 *pdw
++ = 0x00FFFF00; // 1011 yellow
492 *pdw
++ = 0x000000FF; // 1100 blue
493 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
494 *pdw
++ = 0x0000FFFF; // 1110 cyan
495 *pdw
++ = 0x00FFFFFF; // 1111 white
501 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
506 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
508 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
513 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
518 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
520 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
522 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
530 static void xlatRle8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
534 BYTE _huge
*prle
= pb
;
541 if (cnt
== RLE_ESCAPE
)
560 for (b
=0; b
<cnt
; b
++,prle
++)
576 static void xlatRle4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
580 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
590 * DibMapToPalette(pdib, hpal)
592 * Map the colors of the DIB, using GetNearestPaletteIndex, to
593 * the colors of the given palette.
596 BOOL
DibMapToPalette(PDIB pdib
, HPALETTE hpal
)
598 LPBITMAPINFOHEADER lpbi
;
611 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
612 lpRgb
= DibColors(pdib
);
614 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
615 nDibColors
= DibNumColors(pdib
);
617 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
618 SizeImage
= DibSizeImage(lpbi
);
621 // build a xlat table. from the current DIB colors to the given
624 for (n
=0; n
<nDibColors
; n
++)
625 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
627 lpBits
= (LPBYTE
)DibPtr(lpbi
);
628 lpbi
->biClrUsed
= nPalColors
;
633 if (nPalColors
> nDibColors
)
635 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
636 hmemmove((BYTE _huge
*)DibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
637 lpBits
= (LPBYTE
)DibPtr(lpbi
);
639 else if (nPalColors
< nDibColors
)
641 hmemcpy(DibPtr(lpbi
), lpBits
, SizeImage
);
642 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
643 lpBits
= (LPBYTE
)DibPtr(lpbi
);
647 // translate the DIB bits
649 switch (lpbi
->biCompression
)
652 xlatRle8(lpBits
, SizeImage
, xlat
);
656 xlatRle4(lpBits
, SizeImage
, xlat
);
660 if (lpbi
->biBitCount
== 8)
661 xlatClut8(lpBits
, SizeImage
, xlat
);
663 xlatClut4(lpBits
, SizeImage
, xlat
);
668 // Now copy the RGBs in the logical palette to the dib color table
670 for (n
=0; n
<nPalColors
; n
++)
672 GetPaletteEntries(hpal
,n
,1,&pe
);
674 lpRgb
[n
].rgbRed
= pe
.peRed
;
675 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
676 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
677 lpRgb
[n
].rgbReserved
= (BYTE
)0;
684 HPALETTE
MakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
687 const RGBQUAD far
* rgb
= Info
->bmiColors
;
689 WORD nColors
= Info
->bmiHeader
.biClrUsed
;
691 LOGPALETTE
* logPal
= (LOGPALETTE
*)
692 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
694 logPal
->palVersion
= 0x300; // Windows 3.0 version
695 logPal
->palNumEntries
= nColors
;
696 for (short n
= 0; n
< nColors
; n
++) {
697 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
698 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
699 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
700 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
702 hPalette
= ::CreatePalette(logPal
);