d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / wined3d / palette.c
blob1ebeb11568432f196d81418dfc094764748dccf5
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
21 #include "config.h"
22 #include "wine/port.h"
23 #include "winerror.h"
24 #include "wine/debug.h"
26 #include <string.h>
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 ULONG CDECL wined3d_palette_incref(struct wined3d_palette *palette)
34 ULONG refcount = InterlockedIncrement(&palette->ref);
36 TRACE("%p increasing refcount to %u.\n", palette, refcount);
38 return refcount;
41 ULONG CDECL wined3d_palette_decref(struct wined3d_palette *palette)
43 ULONG refcount = InterlockedDecrement(&palette->ref);
45 TRACE("%p decreasing refcount to %u.\n", palette, refcount);
47 if (!refcount)
48 HeapFree(GetProcessHeap(), 0, palette);
50 return refcount;
53 HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette,
54 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
56 unsigned int i;
57 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
58 palette, flags, start, count, entries);
60 if (flags)
61 return WINED3DERR_INVALIDCALL; /* unchecked */
62 if (start > palette->size || count > palette->size - start)
63 return WINED3DERR_INVALIDCALL;
65 if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES)
67 BYTE *entry = (BYTE *)entries;
69 for (i = start; i < count + start; ++i)
70 *entry++ = palette->colors[i].rgbRed;
72 else
74 for (i = 0; i < count; ++i)
76 entries[i].peRed = palette->colors[i + start].rgbRed;
77 entries[i].peGreen = palette->colors[i + start].rgbGreen;
78 entries[i].peBlue = palette->colors[i + start].rgbBlue;
79 entries[i].peFlags = palette->colors[i + start].rgbReserved;
83 return WINED3D_OK;
86 void CDECL wined3d_palette_apply_to_dc(const struct wined3d_palette *palette, HDC dc)
88 if (SetDIBColorTable(dc, 0, 256, palette->colors) != 256)
89 ERR("Failed to set DIB color table.\n");
92 HRESULT CDECL wined3d_palette_set_entries(struct wined3d_palette *palette,
93 DWORD flags, DWORD start, DWORD count, const PALETTEENTRY *entries)
95 unsigned int i;
97 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
98 palette, flags, start, count, entries);
99 TRACE("Palette flags: %#x.\n", palette->flags);
101 if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES)
103 const BYTE *entry = (const BYTE *)entries;
105 for (i = start; i < count + start; ++i)
106 palette->colors[i].rgbRed = *entry++;
108 else
110 for (i = 0; i < count; ++i)
112 palette->colors[i + start].rgbRed = entries[i].peRed;
113 palette->colors[i + start].rgbGreen = entries[i].peGreen;
114 palette->colors[i + start].rgbBlue = entries[i].peBlue;
115 palette->colors[i + start].rgbReserved = entries[i].peFlags;
118 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
119 if (!(palette->flags & WINED3D_PALETTE_ALLOW_256))
121 TRACE("WINED3D_PALETTE_ALLOW_256 not set, overriding palette entry 0 with black and 255 with white.\n");
122 palette->colors[0].rgbRed = 0;
123 palette->colors[0].rgbGreen = 0;
124 palette->colors[0].rgbBlue = 0;
126 palette->colors[255].rgbRed = 255;
127 palette->colors[255].rgbGreen = 255;
128 palette->colors[255].rgbBlue = 255;
132 return WINED3D_OK;
135 static HRESULT wined3d_palette_init(struct wined3d_palette *palette, struct wined3d_device *device,
136 DWORD flags, unsigned int entry_count, const PALETTEENTRY *entries)
138 HRESULT hr;
140 palette->ref = 1;
141 palette->device = device;
142 palette->flags = flags;
143 palette->size = entry_count;
145 if (FAILED(hr = wined3d_palette_set_entries(palette, 0, 0, entry_count, entries)))
147 WARN("Failed to set palette entries, hr %#x.\n", hr);
148 return hr;
151 return WINED3D_OK;
154 HRESULT CDECL wined3d_palette_create(struct wined3d_device *device, DWORD flags,
155 unsigned int entry_count, const PALETTEENTRY *entries, struct wined3d_palette **palette)
157 struct wined3d_palette *object;
158 HRESULT hr;
160 TRACE("device %p, flags %#x, entries %p, palette %p.\n",
161 device, flags, entries, palette);
163 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
164 if (!object)
165 return E_OUTOFMEMORY;
167 if (FAILED(hr = wined3d_palette_init(object, device, flags, entry_count, entries)))
169 WARN("Failed to initialize palette, hr %#x.\n", hr);
170 HeapFree(GetProcessHeap(), 0, object);
171 return hr;
174 TRACE("Created palette %p.\n", object);
175 *palette = object;
177 return WINED3D_OK;