]>
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
176 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
178 #if defined(WIN32) || defined(_WIN32)
179 //!!! can we call GlobalFree() on this? is it the right format.
180 //!!! can we write to this resource?
182 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
185 fh
= AccessResource(GetCurrentInstance(), h
);
192 pdib
= DibReadBitmapInfo(fh
);
197 /* How much memory do we need to hold the DIB */
199 dwBits
= pdib
->biSizeImage
;
200 dwLen
= pdib
->biSize
+ DibPaletteSize(pdib
) + dwBits
;
202 /* Can we get more memory? */
204 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
218 /* read in the bits */
219 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ DibPaletteSize(pdib
), dwBits
);
229 * ReadDibBitmapInfo()
231 * Will read a file in DIB format and return a global HANDLE to its
232 * BITMAPINFO. This function will work with both "old" and "new"
233 * bitmap formats, but will always return a "new" BITMAPINFO.
236 PDIB
DibReadBitmapInfo(HFILE fh
)
253 off
= _llseek(fh
,0L,SEEK_CUR
);
255 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
259 * do we have a RC HEADER?
261 if (bf
.bfType
!= BFT_BITMAP
)
264 _llseek(fh
,off
,SEEK_SET
);
267 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
271 * what type of bitmap info is this?
273 switch (size
= (int)bi
.biSize
)
276 case sizeof(BITMAPINFOHEADER
):
279 case sizeof(BITMAPCOREHEADER
):
280 bc
= *(BITMAPCOREHEADER
*)&bi
;
281 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
282 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
283 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
284 bi
.biPlanes
= (UINT
)bc
.bcPlanes
;
285 bi
.biBitCount
= (UINT
)bc
.bcBitCount
;
286 bi
.biCompression
= BI_RGB
;
288 bi
.biXPelsPerMeter
= 0;
289 bi
.biYPelsPerMeter
= 0;
291 bi
.biClrImportant
= 0;
293 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
298 nNumColors
= DibNumColors(&bi
);
301 if (bi
.biSizeImage
== 0)
302 bi
.biSizeImage
= DibSizeImage(&bi
);
304 if (bi
.biClrUsed
== 0)
305 bi
.biClrUsed
= DibNumColors(&bi
);
310 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
317 pRgb
= DibColors(pdib
);
321 if (size
== sizeof(BITMAPCOREHEADER
))
324 * convert a old color table (3 byte entries) to a new
325 * color table (4 byte entries)
327 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
329 for (i
=nNumColors
-1; i
>=0; i
--)
333 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
334 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
335 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
336 rgb
.rgbReserved
= (BYTE
)0;
343 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
347 if (bf
.bfOffBits
!= 0L)
348 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
354 * DibSetUsage(hdib,hpal,wUsage)
356 * Modifies the color table of the passed DIB for use with the wUsage
357 * parameter specifed.
359 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
360 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
361 * in the passed palette
364 BOOL
DibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
366 PALETTEENTRY ape
[256];
373 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
378 nColors
= DibNumColors(pdib
);
380 if (nColors
== 3 && DibCompression(pdib
) == BI_BITFIELDS
)
385 pRgb
= DibColors(pdib
);
390 // Set the DIB color table to palette indexes
393 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
398 // Set the DIB color table to RGBQUADS
402 nColors
= (nColors
< 256) ? nColors
: 256;
404 GetPaletteEntries(hpal
,0,nColors
,ape
);
406 for (n
=0; n
<nColors
; n
++)
408 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
409 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
410 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
411 pRgb
[n
].rgbReserved
= 0;
420 * DibCreate(bits, dx, dy)
422 * Creates a new packed DIB with the given dimensions and the
423 * given number of bits per pixel
426 PDIB
DibCreate(int bits
, int dx
, int dy
)
428 LPBITMAPINFOHEADER lpbi
;
433 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
435 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
440 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
444 lpbi
->biBitCount
= bits
;
445 lpbi
->biCompression
= BI_RGB
;
446 lpbi
->biSizeImage
= dwSizeImage
;
447 lpbi
->biXPelsPerMeter
= 0 ;
448 lpbi
->biYPelsPerMeter
= 0 ;
449 lpbi
->biClrUsed
= 0 ;
450 lpbi
->biClrImportant
= 0 ;
453 lpbi
->biClrUsed
= 16;
456 lpbi
->biClrUsed
= 256;
458 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
460 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
462 *pdw
++ = 0x00000000; // 0000 black
463 *pdw
++ = 0x00800000; // 0001 dark red
464 *pdw
++ = 0x00008000; // 0010 dark green
465 *pdw
++ = 0x00808000; // 0011 mustard
466 *pdw
++ = 0x00000080; // 0100 dark blue
467 *pdw
++ = 0x00800080; // 0101 purple
468 *pdw
++ = 0x00008080; // 0110 dark turquoise
469 *pdw
++ = 0x00C0C0C0; // 1000 gray
470 *pdw
++ = 0x00808080; // 0111 dark gray
471 *pdw
++ = 0x00FF0000; // 1001 red
472 *pdw
++ = 0x0000FF00; // 1010 green
473 *pdw
++ = 0x00FFFF00; // 1011 yellow
474 *pdw
++ = 0x000000FF; // 1100 blue
475 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
476 *pdw
++ = 0x0000FFFF; // 1110 cyan
477 *pdw
++ = 0x00FFFFFF; // 1111 white
483 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
488 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
490 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
495 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
500 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
502 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
504 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
512 static void xlatRle8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
516 BYTE _huge
*prle
= pb
;
523 if (cnt
== RLE_ESCAPE
)
542 for (b
=0; b
<cnt
; b
++,prle
++)
558 static void xlatRle4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
562 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
572 * DibMapToPalette(pdib, hpal)
574 * Map the colors of the DIB, using GetNearestPaletteIndex, to
575 * the colors of the given palette.
578 BOOL
DibMapToPalette(PDIB pdib
, HPALETTE hpal
)
580 LPBITMAPINFOHEADER lpbi
;
593 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
594 lpRgb
= DibColors(pdib
);
596 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
597 nDibColors
= DibNumColors(pdib
);
599 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
600 SizeImage
= DibSizeImage(lpbi
);
603 // build a xlat table. from the current DIB colors to the given
606 for (n
=0; n
<nDibColors
; n
++)
607 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
609 lpBits
= (LPBYTE
)DibPtr(lpbi
);
610 lpbi
->biClrUsed
= nPalColors
;
615 if (nPalColors
> nDibColors
)
617 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
618 hmemmove((BYTE _huge
*)DibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
619 lpBits
= (LPBYTE
)DibPtr(lpbi
);
621 else if (nPalColors
< nDibColors
)
623 hmemcpy(DibPtr(lpbi
), lpBits
, SizeImage
);
624 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
625 lpBits
= (LPBYTE
)DibPtr(lpbi
);
629 // translate the DIB bits
631 switch (lpbi
->biCompression
)
634 xlatRle8(lpBits
, SizeImage
, xlat
);
638 xlatRle4(lpBits
, SizeImage
, xlat
);
642 if (lpbi
->biBitCount
== 8)
643 xlatClut8(lpBits
, SizeImage
, xlat
);
645 xlatClut4(lpBits
, SizeImage
, xlat
);
650 // Now copy the RGBs in the logical palette to the dib color table
652 for (n
=0; n
<nPalColors
; n
++)
654 GetPaletteEntries(hpal
,n
,1,&pe
);
656 lpRgb
[n
].rgbRed
= pe
.peRed
;
657 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
658 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
659 lpRgb
[n
].rgbReserved
= (BYTE
)0;
666 HPALETTE
MakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
669 const RGBQUAD far
* rgb
= Info
->bmiColors
;
671 WORD nColors
= Info
->bmiHeader
.biClrUsed
;
673 LOGPALETTE
* logPal
= (LOGPALETTE
*)
674 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
676 logPal
->palVersion
= 0x300; // Windows 3.0 version
677 logPal
->palNumEntries
= nColors
;
678 for (short n
= 0; n
< nColors
; n
++) {
679 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
680 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
681 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
682 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
684 hPalette
= ::CreatePalette(logPal
);