]>
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"
32 #include <wx/msw/dibutils.h>
34 #if defined(__WIN32__)
35 #include <memory.h> // for _fmemcpy()
38 #define hmemcpy memcpy
42 #define BFT_ICON 0x4349 /* 'IC' */
43 #define BFT_BITMAP 0x4d42 /* 'BM' */
44 #define BFT_CURSOR 0x5450 /* 'PT' */
46 /* flags for _lseek */
53 * Clear the System Palette so that we can ensure an identity palette
54 * mapping for fast performance.
57 void ClearSystemPalette(void)
59 //*** A dummy palette setup
64 PALETTEENTRY aEntries
[256];
71 HPALETTE ScreenPalette
= 0;
78 //*** Reset everything in the system palette to black
79 for(Counter
= 0; Counter
< 256; Counter
++)
81 Palette
.aEntries
[Counter
].peRed
= 0;
82 Palette
.aEntries
[Counter
].peGreen
= 0;
83 Palette
.aEntries
[Counter
].peBlue
= 0;
84 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
87 //*** Create, select, realize, deselect, and delete the palette
88 ScreenDC
= GetDC(NULL
);
89 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
93 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
94 nMapped
= RealizePalette(ScreenDC
);
95 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
96 bOK
= DeleteObject(ScreenPalette
);
99 nOK
= ReleaseDC(NULL
, ScreenDC
);
106 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
113 int DibWriteFile(LPSTR szFile
, LPBITMAPINFOHEADER lpbi
)
118 fh
= OpenFile(szFile
, &of
, OF_WRITE
| OF_CREATE
);
121 // printf("la regamos0");
125 long size
= DibSize(lpbi
);
128 BITMAPFILEHEADER bmf
;
130 bmf
.bfSize
= sizeof(bmf
) + size
;
133 bmf
.bfOffBits
= sizeof(bmf
) + (char far
*)(DibPtr(lpbi
)) - (char far
*)lpbi
;
134 #if defined( __WATCOMC__) || defined(_MSC_VER)
135 if (_hwrite(fh
, (LPCSTR
)(&bmf
), sizeof(bmf
))<0 ||
136 _hwrite(fh
, (LPCSTR
)lpbi
, size
)<0) {
138 // printf("la regamos1");
142 if (_hwrite(fh
, (LPBYTE
)(&bmf
), sizeof(bmf
))<0 ||
143 _hwrite(fh
, (LPBYTE
)lpbi
, size
)<0) {
145 // printf("la regamos1");
154 PDIB
DibOpenFile(LPSTR szFile
)
163 #if defined(WIN32) || defined(_WIN32)
164 #define GetCurrentInstance() GetModuleHandle(NULL)
166 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
169 fh
= OpenFile(szFile
, &of
, OF_READ
);
175 // TODO: Unicode version
177 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
179 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
182 #if defined(__WIN32__)
183 //!!! can we call GlobalFree() on this? is it the right format.
184 //!!! can we write to this resource?
186 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
189 fh
= AccessResource(GetCurrentInstance(), h
);
196 pdib
= DibReadBitmapInfo(fh
);
201 /* How much memory do we need to hold the DIB */
203 dwBits
= pdib
->biSizeImage
;
204 dwLen
= pdib
->biSize
+ DibPaletteSize(pdib
) + dwBits
;
206 /* Can we get more memory? */
208 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
222 /* read in the bits */
223 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ DibPaletteSize(pdib
), dwBits
);
233 * ReadDibBitmapInfo()
235 * Will read a file in DIB format and return a global HANDLE to its
236 * BITMAPINFO. This function will work with both "old" and "new"
237 * bitmap formats, but will always return a "new" BITMAPINFO.
240 PDIB
DibReadBitmapInfo(HFILE fh
)
257 off
= _llseek(fh
,0L,SEEK_CUR
);
259 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
263 * do we have a RC HEADER?
265 if (bf
.bfType
!= BFT_BITMAP
)
268 _llseek(fh
,off
,SEEK_SET
);
271 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
275 * what type of bitmap info is this?
277 switch (size
= (int)bi
.biSize
)
280 case sizeof(BITMAPINFOHEADER
):
283 case sizeof(BITMAPCOREHEADER
):
284 bc
= *(BITMAPCOREHEADER
*)&bi
;
285 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
286 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
287 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
288 bi
.biPlanes
= (UINT
)bc
.bcPlanes
;
289 bi
.biBitCount
= (UINT
)bc
.bcBitCount
;
290 bi
.biCompression
= BI_RGB
;
292 bi
.biXPelsPerMeter
= 0;
293 bi
.biYPelsPerMeter
= 0;
295 bi
.biClrImportant
= 0;
297 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
302 nNumColors
= DibNumColors(&bi
);
305 if (bi
.biSizeImage
== 0)
306 bi
.biSizeImage
= DibSizeImage(&bi
);
308 if (bi
.biClrUsed
== 0)
309 bi
.biClrUsed
= DibNumColors(&bi
);
314 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
321 pRgb
= DibColors(pdib
);
325 if (size
== sizeof(BITMAPCOREHEADER
))
328 * convert a old color table (3 byte entries) to a new
329 * color table (4 byte entries)
331 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
333 for (i
=nNumColors
-1; i
>=0; i
--)
337 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
338 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
339 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
340 rgb
.rgbReserved
= (BYTE
)0;
347 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
351 if (bf
.bfOffBits
!= 0L)
352 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
358 * DibSetUsage(hdib,hpal,wUsage)
360 * Modifies the color table of the passed DIB for use with the wUsage
361 * parameter specifed.
363 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
364 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
365 * in the passed palette
368 BOOL
DibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
370 PALETTEENTRY ape
[256];
377 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
382 nColors
= DibNumColors(pdib
);
384 if (nColors
== 3 && DibCompression(pdib
) == BI_BITFIELDS
)
389 pRgb
= DibColors(pdib
);
394 // Set the DIB color table to palette indexes
397 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
402 // Set the DIB color table to RGBQUADS
406 nColors
= (nColors
< 256) ? nColors
: 256;
408 GetPaletteEntries(hpal
,0,nColors
,ape
);
410 for (n
=0; n
<nColors
; n
++)
412 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
413 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
414 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
415 pRgb
[n
].rgbReserved
= 0;
424 * DibCreate(bits, dx, dy)
426 * Creates a new packed DIB with the given dimensions and the
427 * given number of bits per pixel
430 PDIB
DibCreate(int bits
, int dx
, int dy
)
432 LPBITMAPINFOHEADER lpbi
;
437 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
439 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
444 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
448 lpbi
->biBitCount
= bits
;
449 lpbi
->biCompression
= BI_RGB
;
450 lpbi
->biSizeImage
= dwSizeImage
;
451 lpbi
->biXPelsPerMeter
= 0 ;
452 lpbi
->biYPelsPerMeter
= 0 ;
453 lpbi
->biClrUsed
= 0 ;
454 lpbi
->biClrImportant
= 0 ;
457 lpbi
->biClrUsed
= 16;
460 lpbi
->biClrUsed
= 256;
462 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
464 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
466 *pdw
++ = 0x00000000; // 0000 black
467 *pdw
++ = 0x00800000; // 0001 dark red
468 *pdw
++ = 0x00008000; // 0010 dark green
469 *pdw
++ = 0x00808000; // 0011 mustard
470 *pdw
++ = 0x00000080; // 0100 dark blue
471 *pdw
++ = 0x00800080; // 0101 purple
472 *pdw
++ = 0x00008080; // 0110 dark turquoise
473 *pdw
++ = 0x00C0C0C0; // 1000 gray
474 *pdw
++ = 0x00808080; // 0111 dark gray
475 *pdw
++ = 0x00FF0000; // 1001 red
476 *pdw
++ = 0x0000FF00; // 1010 green
477 *pdw
++ = 0x00FFFF00; // 1011 yellow
478 *pdw
++ = 0x000000FF; // 1100 blue
479 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
480 *pdw
++ = 0x0000FFFF; // 1110 cyan
481 *pdw
++ = 0x00FFFFFF; // 1111 white
487 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
492 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
494 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
499 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
504 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
506 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
508 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
516 static void xlatRle8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
520 BYTE _huge
*prle
= pb
;
527 if (cnt
== RLE_ESCAPE
)
546 for (b
=0; b
<cnt
; b
++,prle
++)
562 static void xlatRle4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
566 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
576 * DibMapToPalette(pdib, hpal)
578 * Map the colors of the DIB, using GetNearestPaletteIndex, to
579 * the colors of the given palette.
582 BOOL
DibMapToPalette(PDIB pdib
, HPALETTE hpal
)
584 LPBITMAPINFOHEADER lpbi
;
597 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
598 lpRgb
= DibColors(pdib
);
600 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
601 nDibColors
= DibNumColors(pdib
);
603 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
604 SizeImage
= DibSizeImage(lpbi
);
607 // build a xlat table. from the current DIB colors to the given
610 for (n
=0; n
<nDibColors
; n
++)
611 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
613 lpBits
= (LPBYTE
)DibPtr(lpbi
);
614 lpbi
->biClrUsed
= nPalColors
;
619 if (nPalColors
> nDibColors
)
621 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
622 hmemmove((BYTE _huge
*)DibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
623 lpBits
= (LPBYTE
)DibPtr(lpbi
);
625 else if (nPalColors
< nDibColors
)
627 hmemcpy(DibPtr(lpbi
), lpBits
, SizeImage
);
628 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
629 lpBits
= (LPBYTE
)DibPtr(lpbi
);
633 // translate the DIB bits
635 switch (lpbi
->biCompression
)
638 xlatRle8(lpBits
, SizeImage
, xlat
);
642 xlatRle4(lpBits
, SizeImage
, xlat
);
646 if (lpbi
->biBitCount
== 8)
647 xlatClut8(lpBits
, SizeImage
, xlat
);
649 xlatClut4(lpBits
, SizeImage
, xlat
);
654 // Now copy the RGBs in the logical palette to the dib color table
656 for (n
=0; n
<nPalColors
; n
++)
658 GetPaletteEntries(hpal
,n
,1,&pe
);
660 lpRgb
[n
].rgbRed
= pe
.peRed
;
661 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
662 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
663 lpRgb
[n
].rgbReserved
= (BYTE
)0;
670 HPALETTE
MakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
673 const RGBQUAD far
* rgb
= Info
->bmiColors
;
675 WORD nColors
= Info
->bmiHeader
.biClrUsed
;
677 LOGPALETTE
* logPal
= (LOGPALETTE
*)
678 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
680 logPal
->palVersion
= 0x300; // Windows 3.0 version
681 logPal
->palNumEntries
= nColors
;
682 for (short n
= 0; n
< nColors
; n
++) {
683 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
684 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
685 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
686 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
688 hPalette
= ::CreatePalette(logPal
);