ws2_32: Avoid accessing uninitialized memory in GetAddrInfoW.
[wine] / dlls / wined3d / palette.c
1 /*              DirectDraw - IDirectPalette base interface
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 2000-2001 TransGaming Technologies Inc.
5  * Copyright 2006 Stefan Dösinger for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "config.h"
22 #include "winerror.h"
23 #include "wine/debug.h"
24
25 #include <string.h>
26
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 #define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
32
33 static HRESULT  WINAPI IWineD3DPaletteImpl_QueryInterface(IWineD3DPalette *iface, REFIID refiid, void **obj) {
34     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
36
37     if (IsEqualGUID(refiid, &IID_IUnknown)
38         || IsEqualGUID(refiid, &IID_IWineD3DPalette)) {
39         *obj = iface;
40         IWineD3DPalette_AddRef(iface);
41         return S_OK;
42     }
43     else {
44         *obj = NULL;
45         return E_NOINTERFACE;
46     }
47 }
48
49 static ULONG  WINAPI IWineD3DPaletteImpl_AddRef(IWineD3DPalette *iface) {
50     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
51     ULONG ref = InterlockedIncrement(&This->ref);
52
53     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
54
55     return ref;
56 }
57
58 static ULONG  WINAPI IWineD3DPaletteImpl_Release(IWineD3DPalette *iface) {
59     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
60     ULONG ref = InterlockedDecrement(&This->ref);
61
62     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
63
64     if (!ref) {
65         DeleteObject(This->hpal);
66         HeapFree(GetProcessHeap(), 0, This);
67         return 0;
68     }
69
70     return ref;
71 }
72
73 /* Not called from the vtable */
74 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags) {
75     switch (dwFlags & SIZE_BITS) {
76         case WINEDDPCAPS_1BIT: return 2;
77         case WINEDDPCAPS_2BIT: return 4;
78         case WINEDDPCAPS_4BIT: return 16;
79         case WINEDDPCAPS_8BIT: return 256;
80         default:
81             FIXME("Unhandled size bits %#x.\n", dwFlags & SIZE_BITS);
82             return 256;
83     }
84 }
85
86 static HRESULT  WINAPI IWineD3DPaletteImpl_GetEntries(IWineD3DPalette *iface, DWORD Flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt) {
87     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
88
89     TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
90
91     if (Flags != 0) return WINED3DERR_INVALIDCALL; /* unchecked */
92     if (Start + Count > IWineD3DPaletteImpl_Size(This->Flags))
93         return WINED3DERR_INVALIDCALL;
94
95     if (This->Flags & WINEDDPCAPS_8BITENTRIES)
96     {
97         unsigned int i;
98         LPBYTE entry = (LPBYTE)PalEnt;
99
100         for (i=Start; i < Count+Start; i++)
101             *entry++ = This->palents[i].peRed;
102     }
103     else
104         memcpy(PalEnt, This->palents+Start, Count * sizeof(PALETTEENTRY));
105
106     return WINED3D_OK;
107 }
108
109 static HRESULT  WINAPI IWineD3DPaletteImpl_SetEntries(IWineD3DPalette *iface,
110         DWORD Flags, DWORD Start, DWORD Count, const PALETTEENTRY *PalEnt)
111 {
112     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
113     IWineD3DResourceImpl *res;
114
115     TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
116     TRACE("Palette flags: %#x\n", This->Flags);
117
118     if (This->Flags & WINEDDPCAPS_8BITENTRIES) {
119         unsigned int i;
120         const BYTE* entry = (const BYTE*)PalEnt;
121
122         for (i=Start; i < Count+Start; i++)
123             This->palents[i].peRed = *entry++;
124     }
125     else {
126         memcpy(This->palents+Start, PalEnt, Count * sizeof(PALETTEENTRY));
127
128         /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
129         if(!(This->Flags & WINEDDPCAPS_ALLOW256))
130         {
131             TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
132             This->palents[0].peRed = 0;
133             This->palents[0].peGreen = 0;
134             This->palents[0].peBlue = 0;
135
136             This->palents[255].peRed = 255;
137             This->palents[255].peGreen = 255;
138             This->palents[255].peBlue = 255;
139         }
140
141         if (This->hpal)
142             SetPaletteEntries(This->hpal, Start, Count, This->palents+Start);
143     }
144
145 #if 0
146     /* Now, if we are in 'depth conversion mode', update the screen palette */
147     /* FIXME: we need to update the image or we won't get palette fading. */
148     if (This->ddraw->d->palette_convert != NULL)
149         This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
150 #endif
151
152     /* If the palette is attached to the render target, update all render targets */
153
154     LIST_FOR_EACH_ENTRY(res, &This->wineD3DDevice->resources, IWineD3DResourceImpl, resource.resource_list_entry) {
155         if(IWineD3DResource_GetType((IWineD3DResource *) res) == WINED3DRTYPE_SURFACE) {
156             IWineD3DSurfaceImpl *impl = (IWineD3DSurfaceImpl *) res;
157             if(impl->palette == This)
158                 IWineD3DSurface_RealizePalette((IWineD3DSurface *) res);
159         }
160     }
161
162     return WINED3D_OK;
163 }
164
165 static HRESULT  WINAPI IWineD3DPaletteImpl_GetCaps(IWineD3DPalette *iface, DWORD *Caps) {
166     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
167     TRACE("(%p)->(%p)\n", This, Caps);
168
169     *Caps = This->Flags;
170     return WINED3D_OK;
171 }
172
173 static HRESULT  WINAPI IWineD3DPaletteImpl_GetParent(IWineD3DPalette *iface, IUnknown **Parent) {
174     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
175     TRACE("(%p)->(%p)\n", This, Parent);
176
177     *Parent = This->parent;
178     IUnknown_AddRef(This->parent);
179     return WINED3D_OK;
180 }
181
182 const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl =
183 {
184     /*** IUnknown ***/
185     IWineD3DPaletteImpl_QueryInterface,
186     IWineD3DPaletteImpl_AddRef,
187     IWineD3DPaletteImpl_Release,
188     /*** IWineD3DPalette ***/
189     IWineD3DPaletteImpl_GetParent,
190     IWineD3DPaletteImpl_GetEntries,
191     IWineD3DPaletteImpl_GetCaps,
192     IWineD3DPaletteImpl_SetEntries
193 };