2 * DIB Engine color Primitives
4 * Copyright 2009 Massimo Del Fedele
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dibdrv
);
28 /* ------------------------------------------------------------*/
29 /* BITFIELD HELPERS */
30 static inline DWORD
PutField32(DWORD field
, int shift
, int len
)
32 shift
= shift
- (8 - len
);
34 field
&= (((1 << len
) - 1) << (8 - len
));
42 static inline WORD
PutField16(WORD field
, int shift
, int len
)
44 shift
= shift
- (8 - len
);
46 field
&= (((1 << len
) - 1) << (8 - len
));
54 /* ------------------------------------------------------------*/
56 DWORD
_DIBDRV_ColorToPixel32_RGB(const DIBDRVBITMAP
*dib
, COLORREF color
)
58 return ( ((color
>> 16) & 0xff) | (color
& 0xff00) | ((color
<< 16) & 0xff0000) );
61 DWORD
_DIBDRV_ColorToPixel32_BITFIELDS(const DIBDRVBITMAP
*dib
, COLORREF color
)
69 return PutField32(r
, dib
->redShift
, dib
->redLen
) |
70 PutField32(g
, dib
->greenShift
, dib
->greenLen
) |
71 PutField32(b
, dib
->blueShift
, dib
->blueLen
);
74 DWORD
_DIBDRV_ColorToPixel24(const DIBDRVBITMAP
*dib
, COLORREF color
)
76 return ( ((color
>> 16) & 0xff) | (color
& 0xff00) | ((color
<< 16) & 0xff0000) );
79 DWORD
_DIBDRV_ColorToPixel16_RGB(const DIBDRVBITMAP
*dib
, COLORREF color
)
81 return ( ((color
>> 19) & 0x001f) | ((color
>> 6) & 0x03e0) | ((color
<< 7) & 0x7c00) );
84 DWORD
_DIBDRV_ColorToPixel16_BITFIELDS(const DIBDRVBITMAP
*dib
, COLORREF color
)
92 return PutField16(r
, dib
->redShift
, dib
->redLen
) |
93 PutField16(g
, dib
->greenShift
, dib
->greenLen
) |
94 PutField16(b
, dib
->blueShift
, dib
->blueLen
);
97 DWORD
_DIBDRV_ColorToPixelColortable(const DIBDRVBITMAP
*dib
, COLORREF color
)
99 int i
, best_index
= 0;
101 DWORD diff
, best_diff
= 0xffffffff;
103 r
= GetRValue(color
);
104 g
= GetGValue(color
);
105 b
= GetBValue(color
);
107 /* just in case it's being called without color table
108 properly initialized */
109 if(!dib
->colorTableGrabbed
)
112 /* for monochrome bitmaps, color is background
113 if not matching foreground */
114 if(dib
->colorTableSize
== 2)
116 RGBQUAD
*fore
= dib
->colorTable
+ 1;
117 if(r
== fore
->rgbRed
&& g
== fore
->rgbGreen
&& b
== fore
->rgbBlue
)
122 for(i
= 0; i
< dib
->colorTableSize
; i
++)
124 RGBQUAD
*cur
= dib
->colorTable
+ i
;
125 diff
= (r
- cur
->rgbRed
) * (r
- cur
->rgbRed
)
126 + (g
- cur
->rgbGreen
) * (g
- cur
->rgbGreen
)
127 + (b
- cur
->rgbBlue
) * (b
- cur
->rgbBlue
);