- Add windowscodecs.dll from Wine 1.1.28
[reactos.git] / reactos / dll / win32 / windowscodecs / converter.c
1 /*
2 * Copyright 2009 Vincent Povirk
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "wincodec.h"
29
30 #include "wincodecs_private.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35
36 struct FormatConverter;
37
38 enum pixelformat {
39 format_1bppIndexed,
40 format_4bppIndexed,
41 format_8bppIndexed,
42 format_16bppBGR555,
43 format_16bppBGR565,
44 format_24bppBGR,
45 format_32bppBGR,
46 format_32bppBGRA
47 };
48
49 typedef HRESULT (*copyfunc)(struct FormatConverter *This, const WICRect *prc,
50 UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format);
51
52 struct pixelformatinfo {
53 enum pixelformat format;
54 const WICPixelFormatGUID *guid;
55 copyfunc copy_function;
56 };
57
58 typedef struct FormatConverter {
59 const IWICFormatConverterVtbl *lpVtbl;
60 LONG ref;
61 IWICBitmapSource *source;
62 const struct pixelformatinfo *dst_format, *src_format;
63 WICBitmapDitherType dither;
64 double alpha_threshold;
65 WICBitmapPaletteType palette_type;
66 } FormatConverter;
67
68 static HRESULT copypixels_to_32bppBGRA(struct FormatConverter *This, const WICRect *prc,
69 UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
70 {
71 switch (source_format)
72 {
73 case format_1bppIndexed:
74 if (prc)
75 {
76 HRESULT res;
77 UINT x, y;
78 BYTE *srcdata;
79 UINT srcstride, srcdatasize;
80 const BYTE *srcrow;
81 const BYTE *srcbyte;
82 BYTE *dstrow;
83 DWORD *dstpixel;
84 WICColor colors[2];
85 IWICPalette *palette;
86 UINT actualcolors;
87
88 res = PaletteImpl_Create(&palette);
89 if (FAILED(res)) return res;
90
91 res = IWICBitmapSource_CopyPalette(This->source, palette);
92 if (SUCCEEDED(res))
93 res = IWICPalette_GetColors(palette, 2, colors, &actualcolors);
94
95 IWICPalette_Release(palette);
96
97 if (FAILED(res)) return res;
98
99 srcstride = (prc->Width+7)/8;
100 srcdatasize = srcstride * prc->Height;
101
102 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
103 if (!srcdata) return E_OUTOFMEMORY;
104
105 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
106
107 if (SUCCEEDED(res))
108 {
109 srcrow = srcdata;
110 dstrow = pbBuffer;
111 for (y=0; y<prc->Height; y++) {
112 srcbyte=(const BYTE*)srcrow;
113 dstpixel=(DWORD*)dstrow;
114 for (x=0; x<prc->Width; x+=8) {
115 BYTE srcval;
116 srcval=*srcbyte++;
117 *dstpixel++ = colors[srcval>>7&1];
118 if (x+1 < prc->Width) *dstpixel++ = colors[srcval>>6&1];
119 if (x+2 < prc->Width) *dstpixel++ = colors[srcval>>5&1];
120 if (x+3 < prc->Width) *dstpixel++ = colors[srcval>>4&1];
121 if (x+4 < prc->Width) *dstpixel++ = colors[srcval>>3&1];
122 if (x+5 < prc->Width) *dstpixel++ = colors[srcval>>2&1];
123 if (x+6 < prc->Width) *dstpixel++ = colors[srcval>>1&1];
124 if (x+7 < prc->Width) *dstpixel++ = colors[srcval&1];
125 }
126 srcrow += srcstride;
127 dstrow += cbStride;
128 }
129 }
130
131 HeapFree(GetProcessHeap(), 0, srcdata);
132
133 return res;
134 }
135 return S_OK;
136 case format_4bppIndexed:
137 if (prc)
138 {
139 HRESULT res;
140 UINT x, y;
141 BYTE *srcdata;
142 UINT srcstride, srcdatasize;
143 const BYTE *srcrow;
144 const BYTE *srcbyte;
145 BYTE *dstrow;
146 DWORD *dstpixel;
147 WICColor colors[16];
148 IWICPalette *palette;
149 UINT actualcolors;
150
151 res = PaletteImpl_Create(&palette);
152 if (FAILED(res)) return res;
153
154 res = IWICBitmapSource_CopyPalette(This->source, palette);
155 if (SUCCEEDED(res))
156 res = IWICPalette_GetColors(palette, 16, colors, &actualcolors);
157
158 IWICPalette_Release(palette);
159
160 if (FAILED(res)) return res;
161
162 srcstride = (prc->Width+1)/2;
163 srcdatasize = srcstride * prc->Height;
164
165 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
166 if (!srcdata) return E_OUTOFMEMORY;
167
168 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
169
170 if (SUCCEEDED(res))
171 {
172 srcrow = srcdata;
173 dstrow = pbBuffer;
174 for (y=0; y<prc->Height; y++) {
175 srcbyte=(const BYTE*)srcrow;
176 dstpixel=(DWORD*)dstrow;
177 for (x=0; x<prc->Width; x+=2) {
178 BYTE srcval;
179 srcval=*srcbyte++;
180 *dstpixel++ = colors[srcval>>4];
181 if (x+1 < prc->Width) *dstpixel++ = colors[srcval&0xf];
182 }
183 srcrow += srcstride;
184 dstrow += cbStride;
185 }
186 }
187
188 HeapFree(GetProcessHeap(), 0, srcdata);
189
190 return res;
191 }
192 return S_OK;
193 case format_8bppIndexed:
194 if (prc)
195 {
196 HRESULT res;
197 UINT x, y;
198 BYTE *srcdata;
199 UINT srcstride, srcdatasize;
200 const BYTE *srcrow;
201 const BYTE *srcbyte;
202 BYTE *dstrow;
203 DWORD *dstpixel;
204 WICColor colors[256];
205 IWICPalette *palette;
206 UINT actualcolors;
207
208 res = PaletteImpl_Create(&palette);
209 if (FAILED(res)) return res;
210
211 res = IWICBitmapSource_CopyPalette(This->source, palette);
212 if (SUCCEEDED(res))
213 res = IWICPalette_GetColors(palette, 256, colors, &actualcolors);
214
215 IWICPalette_Release(palette);
216
217 if (FAILED(res)) return res;
218
219 srcstride = prc->Width;
220 srcdatasize = srcstride * prc->Height;
221
222 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
223 if (!srcdata) return E_OUTOFMEMORY;
224
225 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
226
227 if (SUCCEEDED(res))
228 {
229 srcrow = srcdata;
230 dstrow = pbBuffer;
231 for (y=0; y<prc->Height; y++) {
232 srcbyte=(const BYTE*)srcrow;
233 dstpixel=(DWORD*)dstrow;
234 for (x=0; x<prc->Width; x++)
235 *dstpixel++ = colors[*srcbyte++];
236 srcrow += srcstride;
237 dstrow += cbStride;
238 }
239 }
240
241 HeapFree(GetProcessHeap(), 0, srcdata);
242
243 return res;
244 }
245 return S_OK;
246 case format_16bppBGR555:
247 if (prc)
248 {
249 HRESULT res;
250 UINT x, y;
251 BYTE *srcdata;
252 UINT srcstride, srcdatasize;
253 const BYTE *srcrow;
254 const WORD *srcpixel;
255 BYTE *dstrow;
256 DWORD *dstpixel;
257
258 srcstride = 2 * prc->Width;
259 srcdatasize = srcstride * prc->Height;
260
261 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
262 if (!srcdata) return E_OUTOFMEMORY;
263
264 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
265
266 if (SUCCEEDED(res))
267 {
268 srcrow = srcdata;
269 dstrow = pbBuffer;
270 for (y=0; y<prc->Height; y++) {
271 srcpixel=(const WORD*)srcrow;
272 dstpixel=(DWORD*)dstrow;
273 for (x=0; x<prc->Width; x++) {
274 WORD srcval;
275 srcval=*srcpixel++;
276 *dstpixel++=0xff000000 | /* constant 255 alpha */
277 ((srcval << 9) & 0xf80000) | /* r */
278 ((srcval << 4) & 0x070000) | /* r - 3 bits */
279 ((srcval << 6) & 0x00f800) | /* g */
280 ((srcval << 1) & 0x000700) | /* g - 3 bits */
281 ((srcval << 3) & 0x0000f8) | /* b */
282 ((srcval >> 2) & 0x000007); /* b - 3 bits */
283 }
284 srcrow += srcstride;
285 dstrow += cbStride;
286 }
287 }
288
289 HeapFree(GetProcessHeap(), 0, srcdata);
290
291 return res;
292 }
293 return S_OK;
294 case format_16bppBGR565:
295 if (prc)
296 {
297 HRESULT res;
298 UINT x, y;
299 BYTE *srcdata;
300 UINT srcstride, srcdatasize;
301 const BYTE *srcrow;
302 const WORD *srcpixel;
303 BYTE *dstrow;
304 DWORD *dstpixel;
305
306 srcstride = 2 * prc->Width;
307 srcdatasize = srcstride * prc->Height;
308
309 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
310 if (!srcdata) return E_OUTOFMEMORY;
311
312 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
313
314 if (SUCCEEDED(res))
315 {
316 srcrow = srcdata;
317 dstrow = pbBuffer;
318 for (y=0; y<prc->Height; y++) {
319 srcpixel=(const WORD*)srcrow;
320 dstpixel=(DWORD*)dstrow;
321 for (x=0; x<prc->Width; x++) {
322 WORD srcval;
323 srcval=*srcpixel++;
324 *dstpixel++=0xff000000 | /* constant 255 alpha */
325 ((srcval << 8) & 0xf80000) | /* r */
326 ((srcval << 3) & 0x070000) | /* r - 3 bits */
327 ((srcval << 5) & 0x00fc00) | /* g */
328 ((srcval >> 1) & 0x000300) | /* g - 2 bits */
329 ((srcval << 3) & 0x0000f8) | /* b */
330 ((srcval >> 2) & 0x000007); /* b - 3 bits */
331 }
332 srcrow += srcstride;
333 dstrow += cbStride;
334 }
335 }
336
337 HeapFree(GetProcessHeap(), 0, srcdata);
338
339 return res;
340 }
341 return S_OK;
342 case format_24bppBGR:
343 if (prc)
344 {
345 HRESULT res;
346 UINT x, y;
347 BYTE *srcdata;
348 UINT srcstride, srcdatasize;
349 const BYTE *srcrow;
350 const BYTE *srcpixel;
351 BYTE *dstrow;
352 BYTE *dstpixel;
353
354 srcstride = 3 * prc->Width;
355 srcdatasize = srcstride * prc->Height;
356
357 srcdata = HeapAlloc(GetProcessHeap(), 0, srcdatasize);
358 if (!srcdata) return E_OUTOFMEMORY;
359
360 res = IWICBitmapSource_CopyPixels(This->source, prc, srcstride, srcdatasize, srcdata);
361
362 if (SUCCEEDED(res))
363 {
364 srcrow = srcdata;
365 dstrow = pbBuffer;
366 for (y=0; y<prc->Height; y++) {
367 srcpixel=srcrow;
368 dstpixel=dstrow;
369 for (x=0; x<prc->Width; x++) {
370 *dstpixel++=*srcpixel++; /* blue */
371 *dstpixel++=*srcpixel++; /* green */
372 *dstpixel++=*srcpixel++; /* red */
373 *dstpixel++=255; /* alpha */
374 }
375 srcrow += srcstride;
376 dstrow += cbStride;
377 }
378 }
379
380 HeapFree(GetProcessHeap(), 0, srcdata);
381
382 return res;
383 }
384 return S_OK;
385 case format_32bppBGR:
386 if (prc)
387 {
388 HRESULT res;
389 UINT x, y;
390
391 res = IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
392 if (FAILED(res)) return res;
393
394 /* set all alpha values to 255 */
395 for (y=0; y<prc->Height; y++)
396 for (x=0; x<prc->Width; x++)
397 pbBuffer[cbStride*y+4*x+3] = 0xff;
398 }
399 return S_OK;
400 case format_32bppBGRA:
401 if (prc)
402 return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
403 return S_OK;
404 default:
405 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
406 }
407 }
408
409 static HRESULT copypixels_to_32bppBGR(struct FormatConverter *This, const WICRect *prc,
410 UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer, enum pixelformat source_format)
411 {
412 switch (source_format)
413 {
414 case format_32bppBGR:
415 case format_32bppBGRA:
416 if (prc)
417 return IWICBitmapSource_CopyPixels(This->source, prc, cbStride, cbBufferSize, pbBuffer);
418 return S_OK;
419 default:
420 return copypixels_to_32bppBGRA(This, prc, cbStride, cbBufferSize, pbBuffer, source_format);
421 }
422 }
423
424 static const struct pixelformatinfo supported_formats[] = {
425 {format_1bppIndexed, &GUID_WICPixelFormat1bppIndexed, NULL},
426 {format_4bppIndexed, &GUID_WICPixelFormat4bppIndexed, NULL},
427 {format_8bppIndexed, &GUID_WICPixelFormat8bppIndexed, NULL},
428 {format_16bppBGR555, &GUID_WICPixelFormat16bppBGR555, NULL},
429 {format_16bppBGR565, &GUID_WICPixelFormat16bppBGR565, NULL},
430 {format_24bppBGR, &GUID_WICPixelFormat24bppBGR, NULL},
431 {format_32bppBGR, &GUID_WICPixelFormat32bppBGR, copypixels_to_32bppBGR},
432 {format_32bppBGRA, &GUID_WICPixelFormat32bppBGRA, copypixels_to_32bppBGRA},
433 {0}
434 };
435
436 static const struct pixelformatinfo *get_formatinfo(const WICPixelFormatGUID *format)
437 {
438 UINT i;
439
440 for (i=0; supported_formats[i].guid; i++)
441 if (IsEqualGUID(supported_formats[i].guid, format)) return &supported_formats[i];
442
443 return NULL;
444 }
445
446 static HRESULT WINAPI FormatConverter_QueryInterface(IWICFormatConverter *iface, REFIID iid,
447 void **ppv)
448 {
449 FormatConverter *This = (FormatConverter*)iface;
450 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
451
452 if (!ppv) return E_INVALIDARG;
453
454 if (IsEqualIID(&IID_IUnknown, iid) ||
455 IsEqualIID(&IID_IWICBitmapSource, iid) ||
456 IsEqualIID(&IID_IWICFormatConverter, iid))
457 {
458 *ppv = This;
459 }
460 else
461 {
462 *ppv = NULL;
463 return E_NOINTERFACE;
464 }
465
466 IUnknown_AddRef((IUnknown*)*ppv);
467 return S_OK;
468 }
469
470 static ULONG WINAPI FormatConverter_AddRef(IWICFormatConverter *iface)
471 {
472 FormatConverter *This = (FormatConverter*)iface;
473 ULONG ref = InterlockedIncrement(&This->ref);
474
475 TRACE("(%p) refcount=%u\n", iface, ref);
476
477 return ref;
478 }
479
480 static ULONG WINAPI FormatConverter_Release(IWICFormatConverter *iface)
481 {
482 FormatConverter *This = (FormatConverter*)iface;
483 ULONG ref = InterlockedDecrement(&This->ref);
484
485 TRACE("(%p) refcount=%u\n", iface, ref);
486
487 if (ref == 0)
488 {
489 if (This->source) IWICBitmapSource_Release(This->source);
490 HeapFree(GetProcessHeap(), 0, This);
491 }
492
493 return ref;
494 }
495
496 static HRESULT WINAPI FormatConverter_GetSize(IWICFormatConverter *iface,
497 UINT *puiWidth, UINT *puiHeight)
498 {
499 FormatConverter *This = (FormatConverter*)iface;
500
501 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
502
503 if (This->source)
504 return IWICBitmapSource_GetSize(This->source, puiWidth, puiHeight);
505 else
506 return WINCODEC_ERR_NOTINITIALIZED;
507 }
508
509 static HRESULT WINAPI FormatConverter_GetPixelFormat(IWICFormatConverter *iface,
510 WICPixelFormatGUID *pPixelFormat)
511 {
512 FormatConverter *This = (FormatConverter*)iface;
513
514 TRACE("(%p,%p): stub\n", iface, pPixelFormat);
515
516 if (This->source)
517 memcpy(pPixelFormat, This->dst_format->guid, sizeof(GUID));
518 else
519 return WINCODEC_ERR_NOTINITIALIZED;
520
521 return S_OK;
522 }
523
524 static HRESULT WINAPI FormatConverter_GetResolution(IWICFormatConverter *iface,
525 double *pDpiX, double *pDpiY)
526 {
527 FormatConverter *This = (FormatConverter*)iface;
528
529 TRACE("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
530
531 if (This->source)
532 return IWICBitmapSource_GetResolution(This->source, pDpiX, pDpiY);
533 else
534 return WINCODEC_ERR_NOTINITIALIZED;
535 }
536
537 static HRESULT WINAPI FormatConverter_CopyPalette(IWICFormatConverter *iface,
538 IWICPalette *pIPalette)
539 {
540 FIXME("(%p,%p): stub\n", iface, pIPalette);
541 return E_NOTIMPL;
542 }
543
544 static HRESULT WINAPI FormatConverter_CopyPixels(IWICFormatConverter *iface,
545 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
546 {
547 FormatConverter *This = (FormatConverter*)iface;
548 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
549
550 if (This->source)
551 return This->dst_format->copy_function(This, prc, cbStride, cbBufferSize,
552 pbBuffer, This->src_format->format);
553 else
554 return WINCODEC_ERR_NOTINITIALIZED;
555 }
556
557 static HRESULT WINAPI FormatConverter_Initialize(IWICFormatConverter *iface,
558 IWICBitmapSource *pISource, REFWICPixelFormatGUID dstFormat, WICBitmapDitherType dither,
559 IWICPalette *pIPalette, double alphaThresholdPercent, WICBitmapPaletteType paletteTranslate)
560 {
561 FormatConverter *This = (FormatConverter*)iface;
562 const struct pixelformatinfo *srcinfo, *dstinfo;
563 static INT fixme=0;
564 GUID srcFormat;
565 HRESULT res;
566
567 TRACE("(%p,%p,%s,%u,%p,%0.1f,%u)\n", iface, pISource, debugstr_guid(dstFormat),
568 dither, pIPalette, alphaThresholdPercent, paletteTranslate);
569
570 if (pIPalette && !fixme++) FIXME("ignoring palette\n");
571
572 if (This->source) return WINCODEC_ERR_WRONGSTATE;
573
574 res = IWICBitmapSource_GetPixelFormat(pISource, &srcFormat);
575 if (FAILED(res)) return res;
576
577 srcinfo = get_formatinfo(&srcFormat);
578 if (!srcinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
579
580 dstinfo = get_formatinfo(dstFormat);
581 if (!dstinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
582
583 if (dstinfo->copy_function)
584 {
585 IWICBitmapSource_AddRef(pISource);
586 This->source = pISource;
587 This->src_format = srcinfo;
588 This->dst_format = dstinfo;
589 This->dither = dither;
590 This->alpha_threshold = alphaThresholdPercent;
591 This->palette_type = paletteTranslate;
592 }
593 else
594 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
595
596 return S_OK;
597 }
598
599 static HRESULT WINAPI FormatConverter_CanConvert(IWICFormatConverter *iface,
600 REFWICPixelFormatGUID srcPixelFormat, REFWICPixelFormatGUID dstPixelFormat,
601 BOOL *pfCanConvert)
602 {
603 FormatConverter *This = (FormatConverter*)iface;
604 const struct pixelformatinfo *srcinfo, *dstinfo;
605
606 TRACE("(%p,%s,%s,%p)\n", iface, debugstr_guid(srcPixelFormat),
607 debugstr_guid(dstPixelFormat), pfCanConvert);
608
609 srcinfo = get_formatinfo(srcPixelFormat);
610 if (!srcinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
611
612 dstinfo = get_formatinfo(dstPixelFormat);
613 if (!dstinfo) return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT;
614
615 if (dstinfo->copy_function &&
616 SUCCEEDED(dstinfo->copy_function(This, NULL, 0, 0, NULL, dstinfo->format)))
617 *pfCanConvert = TRUE;
618 else
619 *pfCanConvert = FALSE;
620
621 return S_OK;
622 }
623
624 static const IWICFormatConverterVtbl FormatConverter_Vtbl = {
625 FormatConverter_QueryInterface,
626 FormatConverter_AddRef,
627 FormatConverter_Release,
628 FormatConverter_GetSize,
629 FormatConverter_GetPixelFormat,
630 FormatConverter_GetResolution,
631 FormatConverter_CopyPalette,
632 FormatConverter_CopyPixels,
633 FormatConverter_Initialize,
634 FormatConverter_CanConvert
635 };
636
637 HRESULT FormatConverter_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
638 {
639 FormatConverter *This;
640 HRESULT ret;
641
642 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
643
644 *ppv = NULL;
645
646 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
647
648 This = HeapAlloc(GetProcessHeap(), 0, sizeof(FormatConverter));
649 if (!This) return E_OUTOFMEMORY;
650
651 This->lpVtbl = &FormatConverter_Vtbl;
652 This->ref = 1;
653 This->source = NULL;
654
655 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
656 IUnknown_Release((IUnknown*)This);
657
658 return ret;
659 }