1 /* DirectDraw - IDirectPalette base interface
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 ULONG CDECL
wined3d_palette_incref(struct wined3d_palette
*palette
)
29 ULONG refcount
= InterlockedIncrement(&palette
->ref
);
31 TRACE("%p increasing refcount to %u.\n", palette
, refcount
);
36 static void wined3d_palette_destroy_object(void *object
)
38 TRACE("object %p.\n", object
);
43 ULONG CDECL
wined3d_palette_decref(struct wined3d_palette
*palette
)
45 ULONG refcount
= InterlockedDecrement(&palette
->ref
);
47 TRACE("%p decreasing refcount to %u.\n", palette
, refcount
);
50 wined3d_cs_destroy_object(palette
->device
->cs
, wined3d_palette_destroy_object
, palette
);
55 HRESULT CDECL
wined3d_palette_get_entries(const struct wined3d_palette
*palette
,
56 DWORD flags
, DWORD start
, DWORD count
, PALETTEENTRY
*entries
)
59 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
60 palette
, flags
, start
, count
, entries
);
63 return WINED3DERR_INVALIDCALL
; /* unchecked */
64 if (!wined3d_bound_range(start
, count
, palette
->size
))
65 return WINED3DERR_INVALIDCALL
;
67 if (palette
->flags
& WINED3D_PALETTE_8BIT_ENTRIES
)
69 BYTE
*entry
= (BYTE
*)entries
;
71 for (i
= start
; i
< count
+ start
; ++i
)
72 *entry
++ = palette
->colors
[i
].rgbRed
;
76 for (i
= 0; i
< count
; ++i
)
78 entries
[i
].peRed
= palette
->colors
[i
+ start
].rgbRed
;
79 entries
[i
].peGreen
= palette
->colors
[i
+ start
].rgbGreen
;
80 entries
[i
].peBlue
= palette
->colors
[i
+ start
].rgbBlue
;
81 entries
[i
].peFlags
= palette
->colors
[i
+ start
].rgbReserved
;
88 void CDECL
wined3d_palette_apply_to_dc(const struct wined3d_palette
*palette
, HDC dc
)
90 if (SetDIBColorTable(dc
, 0, 256, palette
->colors
) != 256)
91 ERR("Failed to set DIB color table.\n");
94 HRESULT CDECL
wined3d_palette_set_entries(struct wined3d_palette
*palette
,
95 DWORD flags
, DWORD start
, DWORD count
, const PALETTEENTRY
*entries
)
99 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
100 palette
, flags
, start
, count
, entries
);
101 TRACE("Palette flags: %#x.\n", palette
->flags
);
103 wined3d_cs_finish(palette
->device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
105 if (palette
->flags
& WINED3D_PALETTE_8BIT_ENTRIES
)
107 const BYTE
*entry
= (const BYTE
*)entries
;
109 for (i
= start
; i
< count
+ start
; ++i
)
110 palette
->colors
[i
].rgbRed
= *entry
++;
114 for (i
= 0; i
< count
; ++i
)
116 palette
->colors
[i
+ start
].rgbRed
= entries
[i
].peRed
;
117 palette
->colors
[i
+ start
].rgbGreen
= entries
[i
].peGreen
;
118 palette
->colors
[i
+ start
].rgbBlue
= entries
[i
].peBlue
;
119 palette
->colors
[i
+ start
].rgbReserved
= entries
[i
].peFlags
;
122 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
123 if (!(palette
->flags
& WINED3D_PALETTE_ALLOW_256
))
125 TRACE("WINED3D_PALETTE_ALLOW_256 not set, overriding palette entry 0 with black and 255 with white.\n");
126 palette
->colors
[0].rgbRed
= 0;
127 palette
->colors
[0].rgbGreen
= 0;
128 palette
->colors
[0].rgbBlue
= 0;
130 palette
->colors
[255].rgbRed
= 255;
131 palette
->colors
[255].rgbGreen
= 255;
132 palette
->colors
[255].rgbBlue
= 255;
139 static HRESULT
wined3d_palette_init(struct wined3d_palette
*palette
, struct wined3d_device
*device
,
140 DWORD flags
, unsigned int entry_count
, const PALETTEENTRY
*entries
)
145 palette
->device
= device
;
146 palette
->flags
= flags
;
147 palette
->size
= entry_count
;
149 if (FAILED(hr
= wined3d_palette_set_entries(palette
, 0, 0, entry_count
, entries
)))
151 WARN("Failed to set palette entries, hr %#x.\n", hr
);
158 HRESULT CDECL
wined3d_palette_create(struct wined3d_device
*device
, DWORD flags
,
159 unsigned int entry_count
, const PALETTEENTRY
*entries
, struct wined3d_palette
**palette
)
161 struct wined3d_palette
*object
;
164 TRACE("device %p, flags %#x, entry_count %u, entries %p, palette %p.\n",
165 device
, flags
, entry_count
, entries
, palette
);
167 if (!(object
= heap_alloc_zero(sizeof(*object
))))
168 return E_OUTOFMEMORY
;
170 if (FAILED(hr
= wined3d_palette_init(object
, device
, flags
, entry_count
, entries
)))
172 WARN("Failed to initialize palette, hr %#x.\n", hr
);
177 TRACE("Created palette %p.\n", object
);