palette.c 7.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright 2009 Vincent Povirk for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "objbase.h"
#include "wincodec.h"

#include "wincodecs_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);

typedef struct {
38
    IWICPalette IWICPalette_iface;
39
    LONG ref;
40 41 42
    UINT count;
    WICColor *colors;
    WICBitmapPaletteType type;
43
    CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
44 45
} PaletteImpl;

46 47 48 49 50
static inline PaletteImpl *impl_from_IWICPalette(IWICPalette *iface)
{
    return CONTAINING_RECORD(iface, PaletteImpl, IWICPalette_iface);
}

51 52 53
static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
    void **ppv)
{
54
    PaletteImpl *This = impl_from_IWICPalette(iface);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);

    if (!ppv) return E_INVALIDARG;

    if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
    {
        *ppv = This;
    }
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
{
75
    PaletteImpl *This = impl_from_IWICPalette(iface);
76 77 78 79 80 81 82 83 84
    ULONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) refcount=%u\n", iface, ref);

    return ref;
}

static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
{
85
    PaletteImpl *This = impl_from_IWICPalette(iface);
86 87 88 89 90
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) refcount=%u\n", iface, ref);

    if (ref == 0)
91
    {
92 93
        This->lock.DebugInfo->Spare[0] = 0;
        DeleteCriticalSection(&This->lock);
94
        HeapFree(GetProcessHeap(), 0, This->colors);
95
        HeapFree(GetProcessHeap(), 0, This);
96
    }
97 98 99 100 101 102 103 104 105 106 107 108 109 110

    return ref;
}

static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
    WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
{
    FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
    return E_NOTIMPL;
}

static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
    WICColor *pColors, UINT colorCount)
{
111
    PaletteImpl *This = impl_from_IWICPalette(iface);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    WICColor *new_colors;

    TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);

    if (colorCount == 0)
    {
        new_colors = NULL;
    }
    else
    {
        if (!pColors) return E_INVALIDARG;
        new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
        if (!new_colors) return E_OUTOFMEMORY;
        memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
    }

128
    EnterCriticalSection(&This->lock);
129 130 131 132
    HeapFree(GetProcessHeap(), 0, This->colors);
    This->colors = new_colors;
    This->count = colorCount;
    This->type = WICBitmapPaletteTypeCustom;
133
    LeaveCriticalSection(&This->lock);
134 135

    return S_OK;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
    IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
{
    FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
    return E_NOTIMPL;
}

static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
    IWICPalette *pIPalette)
{
    FIXME("(%p,%p): stub\n", iface, pIPalette);
    return E_NOTIMPL;
}

static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
    WICBitmapPaletteType *pePaletteType)
{
155
    PaletteImpl *This = impl_from_IWICPalette(iface);
156 157 158 159 160

    TRACE("(%p,%p)\n", iface, pePaletteType);

    if (!pePaletteType) return E_INVALIDARG;

161
    EnterCriticalSection(&This->lock);
162
    *pePaletteType = This->type;
163
    LeaveCriticalSection(&This->lock);
164 165

    return S_OK;
166 167 168 169
}

static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
{
170
    PaletteImpl *This = impl_from_IWICPalette(iface);
171 172 173 174 175

    TRACE("(%p,%p)\n", iface, pcCount);

    if (!pcCount) return E_INVALIDARG;

176
    EnterCriticalSection(&This->lock);
177
    *pcCount = This->count;
178
    LeaveCriticalSection(&This->lock);
179 180

    return S_OK;
181 182 183 184 185
}

static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
    WICColor *pColors, UINT *pcActualColors)
{
186
    PaletteImpl *This = impl_from_IWICPalette(iface);
187 188 189 190 191

    TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);

    if (!pColors || !pcActualColors) return E_INVALIDARG;

192 193
    EnterCriticalSection(&This->lock);

194 195 196 197 198 199
    if (This->count < colorCount) colorCount = This->count;

    memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);

    *pcActualColors = colorCount;

200 201
    LeaveCriticalSection(&This->lock);

202
    return S_OK;
203 204 205 206
}

static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
{
207
    PaletteImpl *This = impl_from_IWICPalette(iface);
208 209 210 211 212

    TRACE("(%p,%p)\n", iface, pfIsBlackWhite);

    if (!pfIsBlackWhite) return E_INVALIDARG;

213
    EnterCriticalSection(&This->lock);
214 215 216 217
    if (This->type == WICBitmapPaletteTypeFixedBW)
        *pfIsBlackWhite = TRUE;
    else
        *pfIsBlackWhite = FALSE;
218
    LeaveCriticalSection(&This->lock);
219 220

    return S_OK;
221 222 223 224
}

static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
{
225
    PaletteImpl *This = impl_from_IWICPalette(iface);
226 227 228 229 230

    TRACE("(%p,%p)\n", iface, pfIsGrayscale);

    if (!pfIsGrayscale) return E_INVALIDARG;

231
    EnterCriticalSection(&This->lock);
232 233 234 235 236 237 238 239 240 241 242
    switch(This->type)
    {
        case WICBitmapPaletteTypeFixedBW:
        case WICBitmapPaletteTypeFixedGray4:
        case WICBitmapPaletteTypeFixedGray16:
        case WICBitmapPaletteTypeFixedGray256:
            *pfIsGrayscale = TRUE;
            break;
        default:
            *pfIsGrayscale = FALSE;
    }
243
    LeaveCriticalSection(&This->lock);
244 245

    return S_OK;
246 247 248 249
}

static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
{
250
    PaletteImpl *This = impl_from_IWICPalette(iface);
251 252 253 254 255 256 257 258
    int i;

    TRACE("(%p,%p)\n", iface, pfHasAlpha);

    if (!pfHasAlpha) return E_INVALIDARG;

    *pfHasAlpha = FALSE;

259
    EnterCriticalSection(&This->lock);
260 261 262 263 264 265
    for (i=0; i<This->count; i++)
        if ((This->colors[i]&0xff000000) != 0xff000000)
        {
            *pfHasAlpha = TRUE;
            break;
        }
266
    LeaveCriticalSection(&This->lock);
267 268

    return S_OK;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

static const IWICPaletteVtbl PaletteImpl_Vtbl = {
    PaletteImpl_QueryInterface,
    PaletteImpl_AddRef,
    PaletteImpl_Release,
    PaletteImpl_InitializePredefined,
    PaletteImpl_InitializeCustom,
    PaletteImpl_InitializeFromBitmap,
    PaletteImpl_InitializeFromPalette,
    PaletteImpl_GetType,
    PaletteImpl_GetColorCount,
    PaletteImpl_GetColors,
    PaletteImpl_IsBlackWhite,
    PaletteImpl_IsGrayscale,
    PaletteImpl_HasAlpha
};

HRESULT PaletteImpl_Create(IWICPalette **palette)
{
    PaletteImpl *This;

    This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
    if (!This) return E_OUTOFMEMORY;

294
    This->IWICPalette_iface.lpVtbl = &PaletteImpl_Vtbl;
295
    This->ref = 1;
296 297 298
    This->count = 0;
    This->colors = NULL;
    This->type = WICBitmapPaletteTypeCustom;
299 300
    InitializeCriticalSection(&This->lock);
    This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
301 302 303 304 305

    *palette = (IWICPalette*)This;

    return S_OK;
}