wsdapi: Fix use-after-free when checking for unique prefix (Valgrind).
[wine.git] / dlls / windowscodecs / palette.c
blob89ec9ead11bf4f558221688cb5d5aeb62e71d2c2
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "objbase.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 typedef struct {
38 IWICPalette IWICPalette_iface;
39 LONG ref;
40 UINT count;
41 WICColor *colors;
42 WICBitmapPaletteType type;
43 CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
44 } PaletteImpl;
46 static inline PaletteImpl *impl_from_IWICPalette(IWICPalette *iface)
48 return CONTAINING_RECORD(iface, PaletteImpl, IWICPalette_iface);
51 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
52 void **ppv)
54 PaletteImpl *This = impl_from_IWICPalette(iface);
55 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
57 if (!ppv) return E_INVALIDARG;
59 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
61 *ppv = &This->IWICPalette_iface;
63 else
65 *ppv = NULL;
66 return E_NOINTERFACE;
69 IUnknown_AddRef((IUnknown*)*ppv);
70 return S_OK;
73 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
75 PaletteImpl *This = impl_from_IWICPalette(iface);
76 ULONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) refcount=%u\n", iface, ref);
80 return ref;
83 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
85 PaletteImpl *This = impl_from_IWICPalette(iface);
86 ULONG ref = InterlockedDecrement(&This->ref);
88 TRACE("(%p) refcount=%u\n", iface, ref);
90 if (ref == 0)
92 This->lock.DebugInfo->Spare[0] = 0;
93 DeleteCriticalSection(&This->lock);
94 HeapFree(GetProcessHeap(), 0, This->colors);
95 HeapFree(GetProcessHeap(), 0, This);
98 return ref;
101 static WICColor *generate_gray16_palette(UINT *count)
103 WICColor *entries;
104 UINT i;
106 *count = 16;
107 entries = HeapAlloc(GetProcessHeap(), 0, 16 * sizeof(WICColor));
108 if (!entries) return NULL;
110 for (i = 0; i < 16; i++)
112 entries[i] = 0xff000000;
113 entries[i] |= (i<<20) | (i<<16) | (i<<12) | (i<<8) | (i<<4) | i;
115 return entries;
118 static WICColor *generate_gray256_palette(UINT *count)
120 WICColor *entries;
121 UINT i;
123 *count = 256;
124 entries = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(WICColor));
125 if (!entries) return NULL;
127 for (i = 0; i < 256; i++)
129 entries[i] = 0xff000000;
130 entries[i] |= (i<<16) | (i<<8) | i;
132 return entries;
135 static WICColor *generate_halftone8_palette(UINT *count, BOOL add_transparent)
137 WICColor *entries;
138 UINT i;
140 *count = add_transparent ? 17 : 16;
141 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
142 if (!entries) return NULL;
144 for (i = 0; i < 8; i++)
146 entries[i] = 0xff000000;
147 if (i & 1) entries[i] |= 0xff;
148 if (i & 2) entries[i] |= 0xff00;
149 if (i & 4) entries[i] |= 0xff0000;
152 for (i = 8; i < 16; i++)
154 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
155 0x000080, 0x808000, 0x800080, 0x008080 };
156 entries[i] = 0xff000000;
157 entries[i] |= halftone[i-8];
160 if (add_transparent)
161 entries[i] = 0;
163 return entries;
166 static WICColor *generate_halftone27_palette(UINT *count, BOOL add_transparent)
168 WICColor *entries;
169 UINT i;
171 *count = add_transparent ? 29 : 28;
172 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
173 if (!entries) return NULL;
175 for (i = 0; i < 27; i++)
177 static const BYTE halftone_values[4] = { 0x00,0x80,0xff };
178 entries[i] = 0xff000000;
179 entries[i] |= halftone_values[i%3];
180 entries[i] |= halftone_values[(i/3)%3] << 8;
181 entries[i] |= halftone_values[(i/9)%3] << 16;
184 entries[i++] = 0xffc0c0c0;
185 if (add_transparent)
186 entries[i] = 0;
188 return entries;
191 static WICColor *generate_halftone64_palette(UINT *count, BOOL add_transparent)
193 WICColor *entries;
194 UINT i;
196 *count = add_transparent ? 73 : 72;
197 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
198 if (!entries) return NULL;
200 for (i = 0; i < 64; i++)
202 static const BYTE halftone_values[4] = { 0x00,0x55,0xaa,0xff };
203 entries[i] = 0xff000000;
204 entries[i] |= halftone_values[i%4];
205 entries[i] |= halftone_values[(i/4)%4] << 8;
206 entries[i] |= halftone_values[(i/16)%4] << 16;
209 for (i = 64; i < 72; i++)
211 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
212 0x000080, 0x808000, 0x800080, 0x008080 };
213 entries[i] = 0xff000000;
214 entries[i] |= halftone[i-64];
217 if (add_transparent)
218 entries[i] = 0;
220 return entries;
223 static WICColor *generate_halftone125_palette(UINT *count, BOOL add_transparent)
225 WICColor *entries;
226 UINT i;
228 *count = add_transparent ? 127 : 126;
229 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
230 if (!entries) return NULL;
232 for (i = 0; i < 125; i++)
234 static const BYTE halftone_values[5] = { 0x00,0x40,0x80,0xbf,0xff };
235 entries[i] = 0xff000000;
236 entries[i] |= halftone_values[i%5];
237 entries[i] |= halftone_values[(i/5)%5] << 8;
238 entries[i] |= halftone_values[(i/25)%5] << 16;
241 entries[i++] = 0xffc0c0c0;
242 if (add_transparent)
243 entries[i] = 0;
245 return entries;
248 static WICColor *generate_halftone216_palette(UINT *count, BOOL add_transparent)
250 WICColor *entries;
251 UINT i;
253 *count = add_transparent ? 225 : 224;
254 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
255 if (!entries) return NULL;
257 for (i = 0; i < 216; i++)
259 static const BYTE halftone_values[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
260 entries[i] = 0xff000000;
261 entries[i] |= halftone_values[i%6];
262 entries[i] |= halftone_values[(i/6)%6] << 8;
263 entries[i] |= halftone_values[(i/36)%6] << 16;
266 for (i = 216; i < 224; i++)
268 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
269 0x000080, 0x808000, 0x800080, 0x008080 };
270 entries[i] = 0xff000000;
271 entries[i] |= halftone[i-216];
274 if (add_transparent)
275 entries[i] = 0;
277 return entries;
280 static WICColor *generate_halftone252_palette(UINT *count, BOOL add_transparent)
282 WICColor *entries;
283 UINT i;
285 *count = add_transparent ? 253 : 252;
286 entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
287 if (!entries) return NULL;
289 for (i = 0; i < 252; i++)
291 static const BYTE halftone_values_rb[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
292 static const BYTE halftone_values_g[7] = { 0x00,0x2b,0x55,0x80,0xaa,0xd5,0xff };
293 entries[i] = 0xff000000;
294 entries[i] |= halftone_values_rb[i%6];
295 entries[i] |= halftone_values_g[(i/6)%7] << 8;
296 entries[i] |= halftone_values_rb[(i/42)%6] << 16;
299 if (add_transparent)
300 entries[i] = 0;
302 return entries;
305 static WICColor *generate_halftone256_palette(UINT *count, BOOL add_transparent)
307 WICColor *entries;
308 UINT i;
310 *count = 256;
311 entries = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(WICColor));
312 if (!entries) return NULL;
314 for (i = 0; i < 256; i++)
316 static const BYTE halftone_values_b[4] = { 0x00,0x55,0xaa,0xff };
317 static const BYTE halftone_values_gr[8] = { 0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff };
318 entries[i] = 0xff000000;
319 entries[i] |= halftone_values_b[i%4];
320 entries[i] |= halftone_values_gr[(i/4)%8] << 8;
321 entries[i] |= halftone_values_gr[(i/32)%8] << 16;
324 if (add_transparent)
325 entries[255] = 0;
327 return entries;
330 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
331 WICBitmapPaletteType type, BOOL add_transparent)
333 PaletteImpl *This = impl_from_IWICPalette(iface);
334 WICColor *colors;
335 UINT count;
337 TRACE("(%p,%u,%d)\n", iface, type, add_transparent);
339 switch (type)
341 case WICBitmapPaletteTypeFixedBW:
342 count = 2;
343 colors = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WICColor));
344 if (!colors) return E_OUTOFMEMORY;
345 colors[0] = 0xff000000;
346 colors[1] = 0xffffffff;
347 break;
349 case WICBitmapPaletteTypeFixedGray4:
350 count = 4;
351 colors = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WICColor));
352 if (!colors) return E_OUTOFMEMORY;
353 colors[0] = 0xff000000;
354 colors[1] = 0xff555555;
355 colors[2] = 0xffaaaaaa;
356 colors[3] = 0xffffffff;
357 break;
359 case WICBitmapPaletteTypeFixedGray16:
360 colors = generate_gray16_palette(&count);
361 if (!colors) return E_OUTOFMEMORY;
362 break;
364 case WICBitmapPaletteTypeFixedGray256:
365 colors = generate_gray256_palette(&count);
366 if (!colors) return E_OUTOFMEMORY;
367 break;
369 case WICBitmapPaletteTypeFixedHalftone8:
370 colors = generate_halftone8_palette(&count, add_transparent);
371 if (!colors) return E_OUTOFMEMORY;
372 break;
374 case WICBitmapPaletteTypeFixedHalftone27:
375 colors = generate_halftone27_palette(&count, add_transparent);
376 if (!colors) return E_OUTOFMEMORY;
377 break;
379 case WICBitmapPaletteTypeFixedHalftone64:
380 colors = generate_halftone64_palette(&count, add_transparent);
381 if (!colors) return E_OUTOFMEMORY;
382 break;
384 case WICBitmapPaletteTypeFixedHalftone125:
385 colors = generate_halftone125_palette(&count, add_transparent);
386 if (!colors) return E_OUTOFMEMORY;
387 break;
389 case WICBitmapPaletteTypeFixedHalftone216:
390 colors = generate_halftone216_palette(&count, add_transparent);
391 if (!colors) return E_OUTOFMEMORY;
392 break;
394 case WICBitmapPaletteTypeFixedHalftone252:
395 colors = generate_halftone252_palette(&count, add_transparent);
396 if (!colors) return E_OUTOFMEMORY;
397 break;
399 case WICBitmapPaletteTypeFixedHalftone256:
400 colors = generate_halftone256_palette(&count, add_transparent);
401 if (!colors) return E_OUTOFMEMORY;
402 break;
404 default:
405 WARN("invalid palette type %u\n", type);
406 return E_INVALIDARG;
409 EnterCriticalSection(&This->lock);
410 HeapFree(GetProcessHeap(), 0, This->colors);
411 This->colors = colors;
412 This->count = count;
413 This->type = type;
414 LeaveCriticalSection(&This->lock);
416 return S_OK;
419 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
420 WICColor *pColors, UINT colorCount)
422 PaletteImpl *This = impl_from_IWICPalette(iface);
423 WICColor *new_colors;
425 TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
427 if (colorCount == 0)
429 new_colors = NULL;
431 else
433 if (!pColors) return E_INVALIDARG;
434 new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
435 if (!new_colors) return E_OUTOFMEMORY;
436 memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
439 EnterCriticalSection(&This->lock);
440 HeapFree(GetProcessHeap(), 0, This->colors);
441 This->colors = new_colors;
442 This->count = colorCount;
443 This->type = WICBitmapPaletteTypeCustom;
444 LeaveCriticalSection(&This->lock);
446 return S_OK;
449 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
450 IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
452 FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
453 return E_NOTIMPL;
456 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
457 IWICPalette *source)
459 PaletteImpl *This = impl_from_IWICPalette(iface);
460 UINT count;
461 WICColor *colors = NULL;
462 WICBitmapPaletteType type;
463 HRESULT hr;
465 TRACE("(%p,%p)\n", iface, source);
467 if (!source) return E_INVALIDARG;
469 hr = IWICPalette_GetType(source, &type);
470 if (hr != S_OK) return hr;
471 hr = IWICPalette_GetColorCount(source, &count);
472 if (hr != S_OK) return hr;
473 if (count)
475 colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
476 if (!colors) return E_OUTOFMEMORY;
477 hr = IWICPalette_GetColors(source, count, colors, &count);
478 if (hr != S_OK)
480 HeapFree(GetProcessHeap(), 0, colors);
481 return hr;
485 EnterCriticalSection(&This->lock);
486 HeapFree(GetProcessHeap(), 0, This->colors);
487 This->colors = colors;
488 This->count = count;
489 This->type = type;
490 LeaveCriticalSection(&This->lock);
492 return S_OK;
495 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
496 WICBitmapPaletteType *pePaletteType)
498 PaletteImpl *This = impl_from_IWICPalette(iface);
500 TRACE("(%p,%p)\n", iface, pePaletteType);
502 if (!pePaletteType) return E_INVALIDARG;
504 EnterCriticalSection(&This->lock);
505 *pePaletteType = This->type;
506 LeaveCriticalSection(&This->lock);
508 return S_OK;
511 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
513 PaletteImpl *This = impl_from_IWICPalette(iface);
515 TRACE("(%p,%p)\n", iface, pcCount);
517 if (!pcCount) return E_INVALIDARG;
519 EnterCriticalSection(&This->lock);
520 *pcCount = This->count;
521 LeaveCriticalSection(&This->lock);
523 return S_OK;
526 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
527 WICColor *pColors, UINT *pcActualColors)
529 PaletteImpl *This = impl_from_IWICPalette(iface);
531 TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
533 if (!pColors || !pcActualColors) return E_INVALIDARG;
535 EnterCriticalSection(&This->lock);
537 if (This->count < colorCount) colorCount = This->count;
539 memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
541 *pcActualColors = colorCount;
543 LeaveCriticalSection(&This->lock);
545 return S_OK;
548 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
550 PaletteImpl *This = impl_from_IWICPalette(iface);
552 TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
554 if (!pfIsBlackWhite) return E_INVALIDARG;
556 EnterCriticalSection(&This->lock);
557 if (This->type == WICBitmapPaletteTypeFixedBW)
558 *pfIsBlackWhite = TRUE;
559 else
560 *pfIsBlackWhite = FALSE;
561 LeaveCriticalSection(&This->lock);
563 return S_OK;
566 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
568 PaletteImpl *This = impl_from_IWICPalette(iface);
570 TRACE("(%p,%p)\n", iface, pfIsGrayscale);
572 if (!pfIsGrayscale) return E_INVALIDARG;
574 EnterCriticalSection(&This->lock);
575 switch(This->type)
577 case WICBitmapPaletteTypeFixedBW:
578 case WICBitmapPaletteTypeFixedGray4:
579 case WICBitmapPaletteTypeFixedGray16:
580 case WICBitmapPaletteTypeFixedGray256:
581 *pfIsGrayscale = TRUE;
582 break;
583 default:
584 *pfIsGrayscale = FALSE;
586 LeaveCriticalSection(&This->lock);
588 return S_OK;
591 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
593 PaletteImpl *This = impl_from_IWICPalette(iface);
594 UINT i;
596 TRACE("(%p,%p)\n", iface, pfHasAlpha);
598 if (!pfHasAlpha) return E_INVALIDARG;
600 *pfHasAlpha = FALSE;
602 EnterCriticalSection(&This->lock);
603 for (i=0; i<This->count; i++)
604 if ((This->colors[i]&0xff000000) != 0xff000000)
606 *pfHasAlpha = TRUE;
607 break;
609 LeaveCriticalSection(&This->lock);
611 return S_OK;
614 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
615 PaletteImpl_QueryInterface,
616 PaletteImpl_AddRef,
617 PaletteImpl_Release,
618 PaletteImpl_InitializePredefined,
619 PaletteImpl_InitializeCustom,
620 PaletteImpl_InitializeFromBitmap,
621 PaletteImpl_InitializeFromPalette,
622 PaletteImpl_GetType,
623 PaletteImpl_GetColorCount,
624 PaletteImpl_GetColors,
625 PaletteImpl_IsBlackWhite,
626 PaletteImpl_IsGrayscale,
627 PaletteImpl_HasAlpha
630 HRESULT PaletteImpl_Create(IWICPalette **palette)
632 PaletteImpl *This;
634 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
635 if (!This) return E_OUTOFMEMORY;
637 This->IWICPalette_iface.lpVtbl = &PaletteImpl_Vtbl;
638 This->ref = 1;
639 This->count = 0;
640 This->colors = NULL;
641 This->type = WICBitmapPaletteTypeCustom;
642 InitializeCriticalSection(&This->lock);
643 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
645 *palette = &This->IWICPalette_iface;
647 return S_OK;