include: Assorted spelling fixes.
[wine] / dlls / windowscodecs / palette.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  * Copyright 2012 Dmitry Timoshkov
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "objbase.h"
30 #include "wincodec.h"
31
32 #include "wincodecs_private.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37
38 typedef struct {
39     IWICPalette IWICPalette_iface;
40     LONG ref;
41     UINT count;
42     WICColor *colors;
43     WICBitmapPaletteType type;
44     CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
45 } PaletteImpl;
46
47 static inline PaletteImpl *impl_from_IWICPalette(IWICPalette *iface)
48 {
49     return CONTAINING_RECORD(iface, PaletteImpl, IWICPalette_iface);
50 }
51
52 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
53     void **ppv)
54 {
55     PaletteImpl *This = impl_from_IWICPalette(iface);
56     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
57
58     if (!ppv) return E_INVALIDARG;
59
60     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
61     {
62         *ppv = &This->IWICPalette_iface;
63     }
64     else
65     {
66         *ppv = NULL;
67         return E_NOINTERFACE;
68     }
69
70     IUnknown_AddRef((IUnknown*)*ppv);
71     return S_OK;
72 }
73
74 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
75 {
76     PaletteImpl *This = impl_from_IWICPalette(iface);
77     ULONG ref = InterlockedIncrement(&This->ref);
78
79     TRACE("(%p) refcount=%u\n", iface, ref);
80
81     return ref;
82 }
83
84 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
85 {
86     PaletteImpl *This = impl_from_IWICPalette(iface);
87     ULONG ref = InterlockedDecrement(&This->ref);
88
89     TRACE("(%p) refcount=%u\n", iface, ref);
90
91     if (ref == 0)
92     {
93         This->lock.DebugInfo->Spare[0] = 0;
94         DeleteCriticalSection(&This->lock);
95         HeapFree(GetProcessHeap(), 0, This->colors);
96         HeapFree(GetProcessHeap(), 0, This);
97     }
98
99     return ref;
100 }
101
102 static WICColor *generate_gray16_palette(UINT *count)
103 {
104     WICColor *entries;
105     UINT i;
106
107     *count = 16;
108     entries = HeapAlloc(GetProcessHeap(), 0, 16 * sizeof(WICColor));
109     if (!entries) return NULL;
110
111     for (i = 0; i < 16; i++)
112     {
113         entries[i] = 0xff000000;
114         entries[i] |= (i<<20) | (i<<16) | (i<<12) | (i<<8) | (i<<4) | i;
115     }
116     return entries;
117 }
118
119 static WICColor *generate_gray256_palette(UINT *count)
120 {
121     WICColor *entries;
122     UINT i;
123
124     *count = 256;
125     entries = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(WICColor));
126     if (!entries) return NULL;
127
128     for (i = 0; i < 256; i++)
129     {
130         entries[i] = 0xff000000;
131         entries[i] |= (i<<16) | (i<<8) | i;
132     }
133     return entries;
134 }
135
136 static WICColor *generate_halftone8_palette(UINT *count, BOOL add_transparent)
137 {
138     WICColor *entries;
139     UINT i;
140
141     *count = add_transparent ? 17 : 16;
142     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
143     if (!entries) return NULL;
144
145     for (i = 0; i < 8; i++)
146     {
147         entries[i] = 0xff000000;
148         if (i & 1) entries[i] |= 0xff;
149         if (i & 2) entries[i] |= 0xff00;
150         if (i & 4) entries[i] |= 0xff0000;
151     }
152
153     for (i = 8; i < 16; i++)
154     {
155         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
156                                            0x000080, 0x808000, 0x800080, 0x008080 };
157         entries[i] = 0xff000000;
158         entries[i] |= halftone[i-8];
159     }
160
161     if (add_transparent)
162         entries[i] = 0;
163
164     return entries;
165 }
166
167 static WICColor *generate_halftone27_palette(UINT *count, BOOL add_transparent)
168 {
169     WICColor *entries;
170     UINT i;
171
172     *count = add_transparent ? 29 : 28;
173     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
174     if (!entries) return NULL;
175
176     for (i = 0; i < 27; i++)
177     {
178         static const BYTE halftone_values[4] = { 0x00,0x80,0xff };
179         entries[i] = 0xff000000;
180         entries[i] |= halftone_values[i%3];
181         entries[i] |= halftone_values[(i/3)%3] << 8;
182         entries[i] |= halftone_values[(i/9)%3] << 16;
183     }
184
185     entries[i++] = 0xffc0c0c0;
186     if (add_transparent)
187         entries[i] = 0;
188
189     return entries;
190 }
191
192 static WICColor *generate_halftone64_palette(UINT *count, BOOL add_transparent)
193 {
194     WICColor *entries;
195     UINT i;
196
197     *count = add_transparent ? 73 : 72;
198     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
199     if (!entries) return NULL;
200
201     for (i = 0; i < 64; i++)
202     {
203         static const BYTE halftone_values[4] = { 0x00,0x55,0xaa,0xff };
204         entries[i] = 0xff000000;
205         entries[i] |= halftone_values[i%4];
206         entries[i] |= halftone_values[(i/4)%4] << 8;
207         entries[i] |= halftone_values[(i/16)%4] << 16;
208     }
209
210     for (i = 64; i < 72; i++)
211     {
212         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
213                                            0x000080, 0x808000, 0x800080, 0x008080 };
214         entries[i] = 0xff000000;
215         entries[i] |= halftone[i-64];
216     }
217
218     if (add_transparent)
219         entries[i] = 0;
220
221     return entries;
222 }
223
224 static WICColor *generate_halftone125_palette(UINT *count, BOOL add_transparent)
225 {
226     WICColor *entries;
227     UINT i;
228
229     *count = add_transparent ? 127 : 126;
230     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
231     if (!entries) return NULL;
232
233     for (i = 0; i < 125; i++)
234     {
235         static const BYTE halftone_values[5] = { 0x00,0x40,0x80,0xbf,0xff };
236         entries[i] = 0xff000000;
237         entries[i] |= halftone_values[i%5];
238         entries[i] |= halftone_values[(i/5)%5] << 8;
239         entries[i] |= halftone_values[(i/25)%5] << 16;
240     }
241
242     entries[i++] = 0xffc0c0c0;
243     if (add_transparent)
244         entries[i] = 0;
245
246     return entries;
247 }
248
249 static WICColor *generate_halftone216_palette(UINT *count, BOOL add_transparent)
250 {
251     WICColor *entries;
252     UINT i;
253
254     *count = add_transparent ? 225 : 224;
255     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
256     if (!entries) return NULL;
257
258     for (i = 0; i < 216; i++)
259     {
260         static const BYTE halftone_values[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
261         entries[i] = 0xff000000;
262         entries[i] |= halftone_values[i%6];
263         entries[i] |= halftone_values[(i/6)%6] << 8;
264         entries[i] |= halftone_values[(i/36)%6] << 16;
265     }
266
267     for (i = 216; i < 224; i++)
268     {
269         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
270                                            0x000080, 0x808000, 0x800080, 0x008080 };
271         entries[i] = 0xff000000;
272         entries[i] |= halftone[i-216];
273     }
274
275     if (add_transparent)
276         entries[i] = 0;
277
278     return entries;
279 }
280
281 static WICColor *generate_halftone252_palette(UINT *count, BOOL add_transparent)
282 {
283     WICColor *entries;
284     UINT i;
285
286     *count = add_transparent ? 253 : 252;
287     entries = HeapAlloc(GetProcessHeap(), 0, *count * sizeof(WICColor));
288     if (!entries) return NULL;
289
290     for (i = 0; i < 252; i++)
291     {
292         static const BYTE halftone_values_rb[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
293         static const BYTE halftone_values_g[7] = { 0x00,0x2b,0x55,0x80,0xaa,0xd5,0xff };
294         entries[i] = 0xff000000;
295         entries[i] |= halftone_values_rb[i%6];
296         entries[i] |= halftone_values_g[(i/6)%7] << 8;
297         entries[i] |= halftone_values_rb[(i/42)%6] << 16;
298     }
299
300     if (add_transparent)
301         entries[i] = 0;
302
303     return entries;
304 }
305
306 static WICColor *generate_halftone256_palette(UINT *count, BOOL add_transparent)
307 {
308     WICColor *entries;
309     UINT i;
310
311     *count = 256;
312     entries = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(WICColor));
313     if (!entries) return NULL;
314
315     for (i = 0; i < 256; i++)
316     {
317         static const BYTE halftone_values_b[4] = { 0x00,0x55,0xaa,0xff };
318         static const BYTE halftone_values_gr[8] = { 0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff };
319         entries[i] = 0xff000000;
320         entries[i] |= halftone_values_b[i%4];
321         entries[i] |= halftone_values_gr[(i/4)%8] << 8;
322         entries[i] |= halftone_values_gr[(i/32)%8] << 16;
323     }
324
325     if (add_transparent)
326         entries[255] = 0;
327
328     return entries;
329 }
330
331 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
332     WICBitmapPaletteType type, BOOL add_transparent)
333 {
334     PaletteImpl *This = impl_from_IWICPalette(iface);
335     WICColor *colors;
336     UINT count;
337
338     TRACE("(%p,%u,%d)\n", iface, type, add_transparent);
339
340     switch (type)
341     {
342     case WICBitmapPaletteTypeFixedBW:
343         count = 2;
344         colors = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WICColor));
345         if (!colors) return E_OUTOFMEMORY;
346         colors[0] = 0xff000000;
347         colors[1] = 0xffffffff;
348         break;
349
350     case WICBitmapPaletteTypeFixedGray4:
351         count = 4;
352         colors = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WICColor));
353         if (!colors) return E_OUTOFMEMORY;
354         colors[0] = 0xff000000;
355         colors[1] = 0xff555555;
356         colors[2] = 0xffaaaaaa;
357         colors[3] = 0xffffffff;
358         break;
359
360     case WICBitmapPaletteTypeFixedGray16:
361         colors = generate_gray16_palette(&count);
362         if (!colors) return E_OUTOFMEMORY;
363         break;
364
365     case WICBitmapPaletteTypeFixedGray256:
366         colors = generate_gray256_palette(&count);
367         if (!colors) return E_OUTOFMEMORY;
368         break;
369
370     case WICBitmapPaletteTypeFixedHalftone8:
371         colors = generate_halftone8_palette(&count, add_transparent);
372         if (!colors) return E_OUTOFMEMORY;
373         break;
374
375     case WICBitmapPaletteTypeFixedHalftone27:
376         colors = generate_halftone27_palette(&count, add_transparent);
377         if (!colors) return E_OUTOFMEMORY;
378         break;
379
380     case WICBitmapPaletteTypeFixedHalftone64:
381         colors = generate_halftone64_palette(&count, add_transparent);
382         if (!colors) return E_OUTOFMEMORY;
383         break;
384
385     case WICBitmapPaletteTypeFixedHalftone125:
386         colors = generate_halftone125_palette(&count, add_transparent);
387         if (!colors) return E_OUTOFMEMORY;
388         break;
389
390     case WICBitmapPaletteTypeFixedHalftone216:
391         colors = generate_halftone216_palette(&count, add_transparent);
392         if (!colors) return E_OUTOFMEMORY;
393         break;
394
395     case WICBitmapPaletteTypeFixedHalftone252:
396         colors = generate_halftone252_palette(&count, add_transparent);
397         if (!colors) return E_OUTOFMEMORY;
398         break;
399
400     case WICBitmapPaletteTypeFixedHalftone256:
401         colors = generate_halftone256_palette(&count, add_transparent);
402         if (!colors) return E_OUTOFMEMORY;
403         break;
404
405     default:
406         WARN("invalid palette type %u\n", type);
407         return E_INVALIDARG;
408     }
409
410     EnterCriticalSection(&This->lock);
411     HeapFree(GetProcessHeap(), 0, This->colors);
412     This->colors = colors;
413     This->count = count;
414     This->type = type;
415     LeaveCriticalSection(&This->lock);
416
417     return S_OK;
418 }
419
420 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
421     WICColor *pColors, UINT colorCount)
422 {
423     PaletteImpl *This = impl_from_IWICPalette(iface);
424     WICColor *new_colors;
425
426     TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
427
428     if (colorCount == 0)
429     {
430         new_colors = NULL;
431     }
432     else
433     {
434         if (!pColors) return E_INVALIDARG;
435         new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
436         if (!new_colors) return E_OUTOFMEMORY;
437         memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
438     }
439
440     EnterCriticalSection(&This->lock);
441     HeapFree(GetProcessHeap(), 0, This->colors);
442     This->colors = new_colors;
443     This->count = colorCount;
444     This->type = WICBitmapPaletteTypeCustom;
445     LeaveCriticalSection(&This->lock);
446
447     return S_OK;
448 }
449
450 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
451     IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
452 {
453     FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
454     return E_NOTIMPL;
455 }
456
457 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
458     IWICPalette *source)
459 {
460     PaletteImpl *This = impl_from_IWICPalette(iface);
461     UINT count;
462     WICColor *colors = NULL;
463     WICBitmapPaletteType type;
464     HRESULT hr;
465
466     TRACE("(%p,%p)\n", iface, source);
467
468     if (!source) return E_INVALIDARG;
469
470     hr = IWICPalette_GetType(source, &type);
471     if (hr != S_OK) return hr;
472     hr = IWICPalette_GetColorCount(source, &count);
473     if (hr != S_OK) return hr;
474     if (count)
475     {
476         colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
477         if (!colors) return E_OUTOFMEMORY;
478         hr = IWICPalette_GetColors(source, count, colors, &count);
479         if (hr != S_OK)
480         {
481             HeapFree(GetProcessHeap(), 0, colors);
482             return hr;
483         }
484     }
485
486     EnterCriticalSection(&This->lock);
487     HeapFree(GetProcessHeap(), 0, This->colors);
488     This->colors = colors;
489     This->count = count;
490     This->type = type;
491     LeaveCriticalSection(&This->lock);
492
493     return S_OK;
494 }
495
496 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
497     WICBitmapPaletteType *pePaletteType)
498 {
499     PaletteImpl *This = impl_from_IWICPalette(iface);
500
501     TRACE("(%p,%p)\n", iface, pePaletteType);
502
503     if (!pePaletteType) return E_INVALIDARG;
504
505     EnterCriticalSection(&This->lock);
506     *pePaletteType = This->type;
507     LeaveCriticalSection(&This->lock);
508
509     return S_OK;
510 }
511
512 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
513 {
514     PaletteImpl *This = impl_from_IWICPalette(iface);
515
516     TRACE("(%p,%p)\n", iface, pcCount);
517
518     if (!pcCount) return E_INVALIDARG;
519
520     EnterCriticalSection(&This->lock);
521     *pcCount = This->count;
522     LeaveCriticalSection(&This->lock);
523
524     return S_OK;
525 }
526
527 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
528     WICColor *pColors, UINT *pcActualColors)
529 {
530     PaletteImpl *This = impl_from_IWICPalette(iface);
531
532     TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
533
534     if (!pColors || !pcActualColors) return E_INVALIDARG;
535
536     EnterCriticalSection(&This->lock);
537
538     if (This->count < colorCount) colorCount = This->count;
539
540     memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
541
542     *pcActualColors = colorCount;
543
544     LeaveCriticalSection(&This->lock);
545
546     return S_OK;
547 }
548
549 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
550 {
551     PaletteImpl *This = impl_from_IWICPalette(iface);
552
553     TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
554
555     if (!pfIsBlackWhite) return E_INVALIDARG;
556
557     EnterCriticalSection(&This->lock);
558     if (This->type == WICBitmapPaletteTypeFixedBW)
559         *pfIsBlackWhite = TRUE;
560     else
561         *pfIsBlackWhite = FALSE;
562     LeaveCriticalSection(&This->lock);
563
564     return S_OK;
565 }
566
567 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
568 {
569     PaletteImpl *This = impl_from_IWICPalette(iface);
570
571     TRACE("(%p,%p)\n", iface, pfIsGrayscale);
572
573     if (!pfIsGrayscale) return E_INVALIDARG;
574
575     EnterCriticalSection(&This->lock);
576     switch(This->type)
577     {
578         case WICBitmapPaletteTypeFixedBW:
579         case WICBitmapPaletteTypeFixedGray4:
580         case WICBitmapPaletteTypeFixedGray16:
581         case WICBitmapPaletteTypeFixedGray256:
582             *pfIsGrayscale = TRUE;
583             break;
584         default:
585             *pfIsGrayscale = FALSE;
586     }
587     LeaveCriticalSection(&This->lock);
588
589     return S_OK;
590 }
591
592 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
593 {
594     PaletteImpl *This = impl_from_IWICPalette(iface);
595     int i;
596
597     TRACE("(%p,%p)\n", iface, pfHasAlpha);
598
599     if (!pfHasAlpha) return E_INVALIDARG;
600
601     *pfHasAlpha = FALSE;
602
603     EnterCriticalSection(&This->lock);
604     for (i=0; i<This->count; i++)
605         if ((This->colors[i]&0xff000000) != 0xff000000)
606         {
607             *pfHasAlpha = TRUE;
608             break;
609         }
610     LeaveCriticalSection(&This->lock);
611
612     return S_OK;
613 }
614
615 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
616     PaletteImpl_QueryInterface,
617     PaletteImpl_AddRef,
618     PaletteImpl_Release,
619     PaletteImpl_InitializePredefined,
620     PaletteImpl_InitializeCustom,
621     PaletteImpl_InitializeFromBitmap,
622     PaletteImpl_InitializeFromPalette,
623     PaletteImpl_GetType,
624     PaletteImpl_GetColorCount,
625     PaletteImpl_GetColors,
626     PaletteImpl_IsBlackWhite,
627     PaletteImpl_IsGrayscale,
628     PaletteImpl_HasAlpha
629 };
630
631 HRESULT PaletteImpl_Create(IWICPalette **palette)
632 {
633     PaletteImpl *This;
634
635     This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
636     if (!This) return E_OUTOFMEMORY;
637
638     This->IWICPalette_iface.lpVtbl = &PaletteImpl_Vtbl;
639     This->ref = 1;
640     This->count = 0;
641     This->colors = NULL;
642     This->type = WICBitmapPaletteTypeCustom;
643     InitializeCriticalSection(&This->lock);
644     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
645
646     *palette = &This->IWICPalette_iface;
647
648     return S_OK;
649 }