]>
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"
35 #if defined(__WIN32__)
36 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
37 #include <memory.h> // for _fmemcpy()
41 #define hmemcpy memcpy
45 #define BFT_ICON 0x4349 /* 'IC' */
46 #define BFT_BITMAP 0x4d42 /* 'BM' */
47 #define BFT_CURSOR 0x5450 /* 'PT' */
49 /* flags for _lseek */
56 * Clear the System Palette so that we can ensure an identity palette
57 * mapping for fast performance.
60 void ClearSystemPalette(void)
62 //*** A dummy palette setup
67 PALETTEENTRY aEntries
[256];
74 HPALETTE ScreenPalette
= 0;
81 //*** Reset everything in the system palette to black
82 for(Counter
= 0; Counter
< 256; Counter
++)
84 Palette
.aEntries
[Counter
].peRed
= 0;
85 Palette
.aEntries
[Counter
].peGreen
= 0;
86 Palette
.aEntries
[Counter
].peBlue
= 0;
87 Palette
.aEntries
[Counter
].peFlags
= PC_NOCOLLAPSE
;
90 //*** Create, select, realize, deselect, and delete the palette
91 ScreenDC
= GetDC(NULL
);
92 ScreenPalette
= CreatePalette((LOGPALETTE
*)&Palette
);
96 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
97 nMapped
= RealizePalette(ScreenDC
);
98 ScreenPalette
= SelectPalette(ScreenDC
,ScreenPalette
,FALSE
);
99 bOK
= DeleteObject(ScreenPalette
);
102 nOK
= ReleaseDC(NULL
, ScreenDC
);
109 * Open a DIB file and return a MEMORY DIB, a memory handle containing..
116 int DibWriteFile(LPSTR szFile
, LPBITMAPINFOHEADER lpbi
)
121 fh
= OpenFile(szFile
, &of
, OF_WRITE
| OF_CREATE
);
124 // printf("la regamos0");
128 long size
= DibSize(lpbi
);
131 BITMAPFILEHEADER bmf
;
133 bmf
.bfSize
= sizeof(bmf
) + size
;
136 bmf
.bfOffBits
= sizeof(bmf
) + (char far
*)(DibPtr(lpbi
)) - (char far
*)lpbi
;
137 #if defined( __WATCOMC__) || defined(__VISUALC__) || defined(__SC__) || defined(__SALFORDC__) || defined(__MWERKS__)
138 if (_hwrite(fh
, (LPCSTR
)(&bmf
), sizeof(bmf
))<0 ||
139 _hwrite(fh
, (LPCSTR
)lpbi
, size
)<0) {
141 // printf("la regamos1");
145 if (_hwrite(fh
, (LPBYTE
)(&bmf
), sizeof(bmf
))<0 ||
146 _hwrite(fh
, (LPBYTE
)lpbi
, size
)<0) {
148 // printf("la regamos1");
157 PDIB
DibOpenFile(LPSTR szFile
)
166 #if defined(WIN32) || defined(_WIN32)
167 #define GetCurrentInstance() GetModuleHandle(NULL)
169 #define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
172 fh
= OpenFile(szFile
, &of
, OF_READ
);
178 // TODO: Unicode version
180 h
= FindResource(GetCurrentInstance(), szFile
, RT_BITMAP
);
182 h
= FindResourceA(GetCurrentInstance(), szFile
, RT_BITMAP
);
185 #if defined(__WIN32__)
186 //!!! can we call GlobalFree() on this? is it the right format.
187 //!!! can we write to this resource?
189 return (PDIB
)LockResource(LoadResource(GetCurrentInstance(), h
));
192 fh
= AccessResource(GetCurrentInstance(), h
);
199 pdib
= DibReadBitmapInfo(fh
);
204 /* How much memory do we need to hold the DIB */
206 dwBits
= pdib
->biSizeImage
;
207 dwLen
= pdib
->biSize
+ DibPaletteSize(pdib
) + dwBits
;
209 /* Can we get more memory? */
211 p
= GlobalReAllocPtr(pdib
,dwLen
,0);
225 /* read in the bits */
226 _hread(fh
, (LPBYTE
)pdib
+ (UINT
)pdib
->biSize
+ DibPaletteSize(pdib
), dwBits
);
236 * ReadDibBitmapInfo()
238 * Will read a file in DIB format and return a global HANDLE to its
239 * BITMAPINFO. This function will work with both "old" and "new"
240 * bitmap formats, but will always return a "new" BITMAPINFO.
243 PDIB
DibReadBitmapInfo(HFILE fh
)
260 off
= _llseek(fh
,0L,SEEK_CUR
);
262 if (sizeof(bf
) != _lread(fh
,(LPSTR
)&bf
,sizeof(bf
)))
266 * do we have a RC HEADER?
268 if (bf
.bfType
!= BFT_BITMAP
)
271 _llseek(fh
,off
,SEEK_SET
);
274 if (sizeof(bi
) != _lread(fh
,(LPSTR
)&bi
,sizeof(bi
)))
278 * what type of bitmap info is this?
280 switch (size
= (int)bi
.biSize
)
283 case sizeof(BITMAPINFOHEADER
):
286 case sizeof(BITMAPCOREHEADER
):
287 bc
= *(BITMAPCOREHEADER
*)&bi
;
288 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
289 bi
.biWidth
= (DWORD
)bc
.bcWidth
;
290 bi
.biHeight
= (DWORD
)bc
.bcHeight
;
291 bi
.biPlanes
= (UINT
)bc
.bcPlanes
;
292 bi
.biBitCount
= (UINT
)bc
.bcBitCount
;
293 bi
.biCompression
= BI_RGB
;
295 bi
.biXPelsPerMeter
= 0;
296 bi
.biYPelsPerMeter
= 0;
298 bi
.biClrImportant
= 0;
300 _llseek(fh
,(LONG
)sizeof(BITMAPCOREHEADER
)-sizeof(BITMAPINFOHEADER
),SEEK_CUR
);
305 nNumColors
= DibNumColors(&bi
);
308 if (bi
.biSizeImage
== 0)
309 bi
.biSizeImage
= DibSizeImage(&bi
);
311 if (bi
.biClrUsed
== 0)
312 bi
.biClrUsed
= DibNumColors(&bi
);
317 pdib
= (PDIB
)GlobalAllocPtr(GMEM_MOVEABLE
,(LONG
)bi
.biSize
+ nNumColors
* sizeof(RGBQUAD
));
324 pRgb
= DibColors(pdib
);
328 if (size
== sizeof(BITMAPCOREHEADER
))
331 * convert a old color table (3 byte entries) to a new
332 * color table (4 byte entries)
334 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBTRIPLE
));
336 for (i
=nNumColors
-1; i
>=0; i
--)
340 rgb
.rgbRed
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtRed
;
341 rgb
.rgbBlue
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtBlue
;
342 rgb
.rgbGreen
= ((RGBTRIPLE FAR
*)pRgb
)[i
].rgbtGreen
;
343 rgb
.rgbReserved
= (BYTE
)0;
350 _lread(fh
,(LPVOID
)pRgb
,nNumColors
* sizeof(RGBQUAD
));
354 if (bf
.bfOffBits
!= 0L)
355 _llseek(fh
,off
+ bf
.bfOffBits
,SEEK_SET
);
361 * DibSetUsage(hdib,hpal,wUsage)
363 * Modifies the color table of the passed DIB for use with the wUsage
364 * parameter specifed.
366 * if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
367 * if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
368 * in the passed palette
371 BOOL
DibSetUsage(PDIB pdib
, HPALETTE hpal
,UINT wUsage
)
373 PALETTEENTRY ape
[256];
380 hpal
= (HPALETTE
)GetStockObject(DEFAULT_PALETTE
);
385 nColors
= DibNumColors(pdib
);
387 if (nColors
== 3 && DibCompression(pdib
) == BI_BITFIELDS
)
392 pRgb
= DibColors(pdib
);
397 // Set the DIB color table to palette indexes
400 for (pw
= (WORD FAR
*)pRgb
,n
=0; n
<nColors
; n
++,pw
++)
405 // Set the DIB color table to RGBQUADS
409 nColors
= (nColors
< 256) ? nColors
: 256;
411 GetPaletteEntries(hpal
,0,nColors
,ape
);
413 for (n
=0; n
<nColors
; n
++)
415 pRgb
[n
].rgbRed
= ape
[n
].peRed
;
416 pRgb
[n
].rgbGreen
= ape
[n
].peGreen
;
417 pRgb
[n
].rgbBlue
= ape
[n
].peBlue
;
418 pRgb
[n
].rgbReserved
= 0;
427 * DibCreate(bits, dx, dy)
429 * Creates a new packed DIB with the given dimensions and the
430 * given number of bits per pixel
433 PDIB
DibCreate(int bits
, int dx
, int dy
)
435 LPBITMAPINFOHEADER lpbi
;
440 dwSizeImage
= dy
*(DWORD
)((dx
*bits
/8+3)&~3);
442 lpbi
= (PDIB
)GlobalAllocPtr(GHND
,sizeof(BITMAPINFOHEADER
)+dwSizeImage
+ 1024);
447 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
) ;
451 lpbi
->biBitCount
= bits
;
452 lpbi
->biCompression
= BI_RGB
;
453 lpbi
->biSizeImage
= dwSizeImage
;
454 lpbi
->biXPelsPerMeter
= 0 ;
455 lpbi
->biYPelsPerMeter
= 0 ;
456 lpbi
->biClrUsed
= 0 ;
457 lpbi
->biClrImportant
= 0 ;
460 lpbi
->biClrUsed
= 16;
463 lpbi
->biClrUsed
= 256;
465 pdw
= (DWORD FAR
*)((LPBYTE
)lpbi
+(int)lpbi
->biSize
);
467 for (i
=0; i
<(int)lpbi
->biClrUsed
/16; i
++)
469 *pdw
++ = 0x00000000; // 0000 black
470 *pdw
++ = 0x00800000; // 0001 dark red
471 *pdw
++ = 0x00008000; // 0010 dark green
472 *pdw
++ = 0x00808000; // 0011 mustard
473 *pdw
++ = 0x00000080; // 0100 dark blue
474 *pdw
++ = 0x00800080; // 0101 purple
475 *pdw
++ = 0x00008080; // 0110 dark turquoise
476 *pdw
++ = 0x00C0C0C0; // 1000 gray
477 *pdw
++ = 0x00808080; // 0111 dark gray
478 *pdw
++ = 0x00FF0000; // 1001 red
479 *pdw
++ = 0x0000FF00; // 1010 green
480 *pdw
++ = 0x00FFFF00; // 1011 yellow
481 *pdw
++ = 0x000000FF; // 1100 blue
482 *pdw
++ = 0x00FF00FF; // 1101 pink (magenta)
483 *pdw
++ = 0x0000FFFF; // 1110 cyan
484 *pdw
++ = 0x00FFFFFF; // 1111 white
490 static void xlatClut8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
495 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
497 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
502 static void xlatClut4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
507 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*&)pb
)++)
509 for (dw
= 0; dw
< dwSize
; dw
++, ((BYTE _huge
*)pb
)++)
511 *pb
= (BYTE
)(xlat
[*pb
& 0x0F] | (xlat
[(*pb
>> 4) & 0x0F] << 4));
519 static void xlatRle8(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
523 BYTE _huge
*prle
= pb
;
530 if (cnt
== RLE_ESCAPE
)
549 for (b
=0; b
<cnt
; b
++,prle
++)
565 static void xlatRle4(BYTE FAR
*pb
, DWORD dwSize
, BYTE FAR
*xlat
)
569 static void hmemmove(BYTE _huge
*d
, BYTE _huge
*s
, LONG len
)
579 * DibMapToPalette(pdib, hpal)
581 * Map the colors of the DIB, using GetNearestPaletteIndex, to
582 * the colors of the given palette.
585 BOOL
DibMapToPalette(PDIB pdib
, HPALETTE hpal
)
587 LPBITMAPINFOHEADER lpbi
;
600 lpbi
= (LPBITMAPINFOHEADER
)pdib
;
601 lpRgb
= DibColors(pdib
);
603 GetObject(hpal
,sizeof(int),(LPSTR
)&nPalColors
);
604 nDibColors
= DibNumColors(pdib
);
606 if ((SizeImage
= lpbi
->biSizeImage
) == 0)
607 SizeImage
= DibSizeImage(lpbi
);
610 // build a xlat table. from the current DIB colors to the given
613 for (n
=0; n
<nDibColors
; n
++)
614 xlat
[n
] = (BYTE
)GetNearestPaletteIndex(hpal
,RGB(lpRgb
[n
].rgbRed
,lpRgb
[n
].rgbGreen
,lpRgb
[n
].rgbBlue
));
616 lpBits
= (LPBYTE
)DibPtr(lpbi
);
617 lpbi
->biClrUsed
= nPalColors
;
622 if (nPalColors
> nDibColors
)
624 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
625 hmemmove((BYTE _huge
*)DibPtr(lpbi
), (BYTE _huge
*)lpBits
, SizeImage
);
626 lpBits
= (LPBYTE
)DibPtr(lpbi
);
628 else if (nPalColors
< nDibColors
)
630 hmemcpy(DibPtr(lpbi
), lpBits
, SizeImage
);
631 GlobalReAllocPtr(lpbi
, lpbi
->biSize
+ nPalColors
*sizeof(RGBQUAD
) + SizeImage
, 0);
632 lpBits
= (LPBYTE
)DibPtr(lpbi
);
636 // translate the DIB bits
638 switch (lpbi
->biCompression
)
641 xlatRle8(lpBits
, SizeImage
, xlat
);
645 xlatRle4(lpBits
, SizeImage
, xlat
);
649 if (lpbi
->biBitCount
== 8)
650 xlatClut8(lpBits
, SizeImage
, xlat
);
652 xlatClut4(lpBits
, SizeImage
, xlat
);
657 // Now copy the RGBs in the logical palette to the dib color table
659 for (n
=0; n
<nPalColors
; n
++)
661 GetPaletteEntries(hpal
,n
,1,&pe
);
663 lpRgb
[n
].rgbRed
= pe
.peRed
;
664 lpRgb
[n
].rgbGreen
= pe
.peGreen
;
665 lpRgb
[n
].rgbBlue
= pe
.peBlue
;
666 lpRgb
[n
].rgbReserved
= (BYTE
)0;
673 HPALETTE
MakePalette(const BITMAPINFO FAR
* Info
, UINT flags
)
676 const RGBQUAD far
* rgb
= Info
->bmiColors
;
678 WORD nColors
= Info
->bmiHeader
.biClrUsed
;
680 LOGPALETTE
* logPal
= (LOGPALETTE
*)
681 new BYTE
[sizeof(LOGPALETTE
) + (nColors
-1)*sizeof(PALETTEENTRY
)];
683 logPal
->palVersion
= 0x300; // Windows 3.0 version
684 logPal
->palNumEntries
= nColors
;
685 for (short n
= 0; n
< nColors
; n
++) {
686 logPal
->palPalEntry
[n
].peRed
= rgb
[n
].rgbRed
;
687 logPal
->palPalEntry
[n
].peGreen
= rgb
[n
].rgbGreen
;
688 logPal
->palPalEntry
[n
].peBlue
= rgb
[n
].rgbBlue
;
689 logPal
->palPalEntry
[n
].peFlags
= (BYTE
)flags
;
691 hPalette
= ::CreatePalette(logPal
);