jscript: Make String_slice generic.
[wine] / dlls / windowscodecs / bmpdecode.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
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 "winreg.h"
28 #include "wingdi.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     DWORD bc2Size;
40     DWORD bc2Width;
41     DWORD bc2Height;
42     WORD  bc2Planes;
43     WORD  bc2BitCount;
44     DWORD bc2Compression;
45     DWORD bc2SizeImage;
46     DWORD bc2XRes;
47     DWORD bc2YRes;
48     DWORD bc2ClrUsed;
49     DWORD bc2ClrImportant;
50     /* same as BITMAPINFOHEADER until this point */
51     WORD  bc2ResUnit;
52     WORD  bc2Reserved;
53     WORD  bc2Orientation;
54     WORD  bc2Halftoning;
55     DWORD bc2HalftoneSize1;
56     DWORD bc2HalftoneSize2;
57     DWORD bc2ColorSpace;
58     DWORD bc2AppData;
59 } BITMAPCOREHEADER2;
60
61 struct BmpFrameDecode;
62 typedef HRESULT (*ReadDataFunc)(struct BmpFrameDecode* This);
63
64 typedef struct BmpFrameDecode {
65     const IWICBitmapFrameDecodeVtbl *lpVtbl;
66     LONG ref;
67     IStream *stream;
68     BITMAPFILEHEADER bfh;
69     BITMAPV5HEADER bih;
70     const WICPixelFormatGUID *pixelformat;
71     int bitsperpixel;
72     ReadDataFunc read_data_func;
73     INT stride;
74     BYTE *imagedata;
75     BYTE *imagedatastart;
76 } BmpFrameDecode;
77
78 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
79     void **ppv)
80 {
81     BmpFrameDecode *This = (BmpFrameDecode*)iface;
82     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
83
84     if (!ppv) return E_INVALIDARG;
85
86     if (IsEqualIID(&IID_IUnknown, iid) ||
87         IsEqualIID(&IID_IWICBitmapSource, iid) ||
88         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
89     {
90         *ppv = This;
91     }
92     else
93     {
94         *ppv = NULL;
95         return E_NOINTERFACE;
96     }
97
98     IUnknown_AddRef((IUnknown*)*ppv);
99     return S_OK;
100 }
101
102 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
103 {
104     BmpFrameDecode *This = (BmpFrameDecode*)iface;
105     ULONG ref = InterlockedIncrement(&This->ref);
106
107     TRACE("(%p) refcount=%u\n", iface, ref);
108
109     return ref;
110 }
111
112 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
113 {
114     BmpFrameDecode *This = (BmpFrameDecode*)iface;
115     ULONG ref = InterlockedDecrement(&This->ref);
116
117     TRACE("(%p) refcount=%u\n", iface, ref);
118
119     if (ref == 0)
120     {
121         IStream_Release(This->stream);
122         HeapFree(GetProcessHeap(), 0, This->imagedata);
123         HeapFree(GetProcessHeap(), 0, This);
124     }
125
126     return ref;
127 }
128
129 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
130     UINT *puiWidth, UINT *puiHeight)
131 {
132     BmpFrameDecode *This = (BmpFrameDecode*)iface;
133     TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
134
135     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
136     {
137         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
138         *puiWidth = bch->bcWidth;
139         *puiHeight = bch->bcHeight;
140     }
141     else
142     {
143         *puiWidth = This->bih.bV5Width;
144         *puiHeight = abs(This->bih.bV5Height);
145     }
146     return S_OK;
147 }
148
149 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
150     WICPixelFormatGUID *pPixelFormat)
151 {
152     BmpFrameDecode *This = (BmpFrameDecode*)iface;
153     TRACE("(%p,%p)\n", iface, pPixelFormat);
154
155     memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
156
157     return S_OK;
158 }
159
160 static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, double *pDpiY)
161 {
162     switch (bih->bV5Size)
163     {
164     case sizeof(BITMAPCOREHEADER):
165         *pDpiX = 96.0;
166         *pDpiY = 96.0;
167         return S_OK;
168     case sizeof(BITMAPCOREHEADER2):
169     case sizeof(BITMAPINFOHEADER):
170     case sizeof(BITMAPV4HEADER):
171     case sizeof(BITMAPV5HEADER):
172         *pDpiX = bih->bV5XPelsPerMeter * 0.0254;
173         *pDpiY = bih->bV5YPelsPerMeter * 0.0254;
174         return S_OK;
175     default:
176         return E_FAIL;
177     }
178 }
179
180 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
181     double *pDpiX, double *pDpiY)
182 {
183     BmpFrameDecode *This = (BmpFrameDecode*)iface;
184     TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
185
186     return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
187 }
188
189 static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
190     IWICPalette *pIPalette)
191 {
192     HRESULT hr;
193     BmpFrameDecode *This = (BmpFrameDecode*)iface;
194     int count;
195     WICColor *wiccolors=NULL;
196     RGBTRIPLE *bgrcolors=NULL;
197
198     TRACE("(%p,%p)\n", iface, pIPalette);
199
200     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
201     {
202         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
203         if (bch->bcBitCount <= 8)
204         {
205             /* 2**n colors in BGR format after the header */
206             ULONG tablesize, bytesread;
207             LARGE_INTEGER offset;
208             int i;
209
210             count = 1 << bch->bcBitCount;
211             wiccolors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
212             tablesize = sizeof(RGBTRIPLE) * count;
213             bgrcolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
214             if (!wiccolors || !bgrcolors)
215             {
216                 hr = E_OUTOFMEMORY;
217                 goto end;
218             }
219
220             offset.QuadPart = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER);
221             hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
222             if (FAILED(hr)) goto end;
223
224             hr = IStream_Read(This->stream, bgrcolors, tablesize, &bytesread);
225             if (FAILED(hr)) goto end;
226             if (bytesread != tablesize) {
227                 hr = E_FAIL;
228                 goto end;
229             }
230
231             for (i=0; i<count; i++)
232             {
233                 wiccolors[i] = 0xff000000|
234                                (bgrcolors[i].rgbtRed<<16)|
235                                (bgrcolors[i].rgbtGreen<<8)|
236                                bgrcolors[i].rgbtBlue;
237             }
238         }
239         else
240         {
241             return WINCODEC_ERR_PALETTEUNAVAILABLE;
242         }
243     }
244     else
245     {
246         if (This->bih.bV5BitCount <= 8)
247         {
248             ULONG tablesize, bytesread;
249             LARGE_INTEGER offset;
250             int i;
251
252             if (This->bih.bV5ClrUsed == 0)
253                 count = 1 << This->bih.bV5BitCount;
254             else
255                 count = This->bih.bV5ClrUsed;
256
257             tablesize = sizeof(WICColor) * count;
258             wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
259             if (!wiccolors) return E_OUTOFMEMORY;
260
261             offset.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
262             hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
263             if (FAILED(hr)) goto end;
264
265             hr = IStream_Read(This->stream, wiccolors, tablesize, &bytesread);
266             if (FAILED(hr)) goto end;
267             if (bytesread != tablesize) {
268                 hr = E_FAIL;
269                 goto end;
270             }
271
272             /* convert from BGR to BGRA by setting alpha to 100% */
273             for (i=0; i<count; i++)
274                 wiccolors[i] |= 0xff000000;
275         }
276         else
277         {
278             return WINCODEC_ERR_PALETTEUNAVAILABLE;
279         }
280     }
281
282     hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
283
284 end:
285     HeapFree(GetProcessHeap(), 0, wiccolors);
286     HeapFree(GetProcessHeap(), 0, bgrcolors);
287     return hr;
288 }
289
290 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
291     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
292 {
293     BmpFrameDecode *This = (BmpFrameDecode*)iface;
294     HRESULT hr;
295     UINT width, height;
296     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
297
298     if (!This->imagedata)
299     {
300         hr = This->read_data_func(This);
301         if (FAILED(hr)) return hr;
302     }
303
304     hr = BmpFrameDecode_GetSize(iface, &width, &height);
305     if (FAILED(hr)) return hr;
306
307     return copy_pixels(This->bitsperpixel, This->imagedatastart,
308         width, height, This->stride,
309         prc, cbStride, cbBufferSize, pbBuffer);
310 }
311
312 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
313     IWICMetadataQueryReader **ppIMetadataQueryReader)
314 {
315     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
316     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
317 }
318
319 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
320     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
321 {
322     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
323     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
324 }
325
326 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
327     IWICBitmapSource **ppIThumbnail)
328 {
329     TRACE("(%p,%p)\n", iface, ppIThumbnail);
330     return WINCODEC_ERR_CODECNOTHUMBNAIL;
331 }
332
333 static HRESULT BmpFrameDecode_ReadUncompressed(BmpFrameDecode* This)
334 {
335     UINT bytesperrow;
336     UINT width, height;
337     UINT datasize;
338     int bottomup;
339     HRESULT hr;
340     LARGE_INTEGER offbits;
341     ULONG bytesread;
342
343     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
344     {
345         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
346         width = bch->bcWidth;
347         height = bch->bcHeight;
348         bottomup = 1;
349     }
350     else
351     {
352         width = This->bih.bV5Width;
353         height = abs(This->bih.bV5Height);
354         bottomup = (This->bih.bV5Height > 0);
355     }
356
357     /* row sizes in BMP files must be divisible by 4 bytes */
358     bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
359     datasize = bytesperrow * height;
360
361     This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
362     if (!This->imagedata) return E_OUTOFMEMORY;
363
364     offbits.QuadPart = This->bfh.bfOffBits;
365     hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
366     if (FAILED(hr)) goto fail;
367
368     hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
369     if (FAILED(hr) || bytesread != datasize) goto fail;
370
371     if (bottomup)
372     {
373         This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
374         This->stride = -bytesperrow;
375     }
376     else
377     {
378         This->imagedatastart = This->imagedata;
379         This->stride = bytesperrow;
380     }
381     return S_OK;
382
383 fail:
384     HeapFree(GetProcessHeap(), 0, This->imagedata);
385     This->imagedata = NULL;
386     if (SUCCEEDED(hr)) hr = E_FAIL;
387     return hr;
388 }
389
390 static HRESULT BmpFrameDecode_ReadRLE8(BmpFrameDecode* This)
391 {
392     UINT bytesperrow;
393     UINT width, height;
394     BYTE *rledata, *cursor, *rledataend;
395     UINT rlesize, datasize, palettesize;
396     DWORD palette[256];
397     UINT x, y;
398     DWORD *bgrdata;
399     HRESULT hr;
400     LARGE_INTEGER offbits;
401     ULONG bytesread;
402
403     width = This->bih.bV5Width;
404     height = abs(This->bih.bV5Height);
405     bytesperrow = width * 4;
406     datasize = bytesperrow * height;
407     rlesize = This->bih.bV5SizeImage;
408     if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 256)
409         palettesize = 4 * This->bih.bV5ClrUsed;
410     else
411         palettesize = 4 * 256;
412
413     rledata = HeapAlloc(GetProcessHeap(), 0, rlesize);
414     This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
415     if (!This->imagedata || !rledata)
416     {
417         hr = E_OUTOFMEMORY;
418         goto fail;
419     }
420
421     /* read palette */
422     offbits.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
423     hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
424     if (FAILED(hr)) goto fail;
425
426     hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
427     if (FAILED(hr) || bytesread != palettesize) goto fail;
428
429     /* read RLE data */
430     offbits.QuadPart = This->bfh.bfOffBits;
431     hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
432     if (FAILED(hr)) goto fail;
433
434     hr = IStream_Read(This->stream, rledata, rlesize, &bytesread);
435     if (FAILED(hr) || bytesread != rlesize) goto fail;
436
437     /* decode RLE */
438     bgrdata = (DWORD*)This->imagedata;
439     x = 0;
440     y = 0;
441     rledataend = rledata + rlesize;
442     cursor = rledata;
443     while (cursor < rledataend && y < height)
444     {
445         BYTE length = *cursor++;
446         if (length == 0)
447         {
448             /* escape code */
449             BYTE escape = *cursor++;
450             switch(escape)
451             {
452             case 0: /* end of line */
453                 x = 0;
454                 y++;
455                 break;
456             case 1: /* end of bitmap */
457                 goto end;
458             case 2: /* delta */
459                 if (cursor < rledataend)
460                 {
461                     x += *cursor++;
462                     y += *cursor++;
463                 }
464                 break;
465             default: /* absolute mode */
466                 length = escape;
467                 while (cursor < rledataend && length-- && x < width)
468                     bgrdata[y*width + x++] = palette[*cursor++];
469                 if (escape & 1) cursor++; /* skip pad byte */
470             }
471         }
472         else
473         {
474             DWORD color = palette[*cursor++];
475             while (length-- && x < width)
476                 bgrdata[y*width + x++] = color;
477         }
478     }
479
480 end:
481     HeapFree(GetProcessHeap(), 0, rledata);
482
483     This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
484     This->stride = -bytesperrow;
485
486     return S_OK;
487
488 fail:
489     HeapFree(GetProcessHeap(), 0, rledata);
490     HeapFree(GetProcessHeap(), 0, This->imagedata);
491     This->imagedata = NULL;
492     if (SUCCEEDED(hr)) hr = E_FAIL;
493     return hr;
494 }
495
496 static HRESULT BmpFrameDecode_ReadRLE4(BmpFrameDecode* This)
497 {
498     UINT bytesperrow;
499     UINT width, height;
500     BYTE *rledata, *cursor, *rledataend;
501     UINT rlesize, datasize, palettesize;
502     DWORD palette[16];
503     UINT x, y;
504     DWORD *bgrdata;
505     HRESULT hr;
506     LARGE_INTEGER offbits;
507     ULONG bytesread;
508
509     width = This->bih.bV5Width;
510     height = abs(This->bih.bV5Height);
511     bytesperrow = width * 4;
512     datasize = bytesperrow * height;
513     rlesize = This->bih.bV5SizeImage;
514     if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
515         palettesize = 4 * This->bih.bV5ClrUsed;
516     else
517         palettesize = 4 * 16;
518
519     rledata = HeapAlloc(GetProcessHeap(), 0, rlesize);
520     This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
521     if (!This->imagedata || !rledata)
522     {
523         hr = E_OUTOFMEMORY;
524         goto fail;
525     }
526
527     /* read palette */
528     offbits.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
529     hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
530     if (FAILED(hr)) goto fail;
531
532     hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
533     if (FAILED(hr) || bytesread != palettesize) goto fail;
534
535     /* read RLE data */
536     offbits.QuadPart = This->bfh.bfOffBits;
537     hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
538     if (FAILED(hr)) goto fail;
539
540     hr = IStream_Read(This->stream, rledata, rlesize, &bytesread);
541     if (FAILED(hr) || bytesread != rlesize) goto fail;
542
543     /* decode RLE */
544     bgrdata = (DWORD*)This->imagedata;
545     x = 0;
546     y = 0;
547     rledataend = rledata + rlesize;
548     cursor = rledata;
549     while (cursor < rledataend && y < height)
550     {
551         BYTE length = *cursor++;
552         if (length == 0)
553         {
554             /* escape code */
555             BYTE escape = *cursor++;
556             switch(escape)
557             {
558             case 0: /* end of line */
559                 x = 0;
560                 y++;
561                 break;
562             case 1: /* end of bitmap */
563                 goto end;
564             case 2: /* delta */
565                 if (cursor < rledataend)
566                 {
567                     x += *cursor++;
568                     y += *cursor++;
569                 }
570                 break;
571             default: /* absolute mode */
572                 length = escape;
573                 while (cursor < rledataend && length-- && x < width)
574                 {
575                     BYTE colors = *cursor++;
576                     bgrdata[y*width + x++] = palette[colors>>4];
577                     if (length-- && x < width)
578                         bgrdata[y*width + x++] = palette[colors&0xf];
579                     else
580                         break;
581                 }
582                 if ((cursor - rledata) & 1) cursor++; /* skip pad byte */
583             }
584         }
585         else
586         {
587             BYTE colors = *cursor++;
588             DWORD color1 = palette[colors>>4];
589             DWORD color2 = palette[colors&0xf];
590             while (length-- && x < width)
591             {
592                 bgrdata[y*width + x++] = color1;
593                 if (length-- && x < width)
594                     bgrdata[y*width + x++] = color2;
595                 else
596                     break;
597             }
598         }
599     }
600
601 end:
602     HeapFree(GetProcessHeap(), 0, rledata);
603
604     This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
605     This->stride = -bytesperrow;
606
607     return S_OK;
608
609 fail:
610     HeapFree(GetProcessHeap(), 0, rledata);
611     HeapFree(GetProcessHeap(), 0, This->imagedata);
612     This->imagedata = NULL;
613     if (SUCCEEDED(hr)) hr = E_FAIL;
614     return hr;
615 }
616
617 static HRESULT BmpFrameDecode_ReadUnsupported(BmpFrameDecode* This)
618 {
619     return E_FAIL;
620 }
621
622 static const IWICBitmapFrameDecodeVtbl BmpFrameDecode_Vtbl = {
623     BmpFrameDecode_QueryInterface,
624     BmpFrameDecode_AddRef,
625     BmpFrameDecode_Release,
626     BmpFrameDecode_GetSize,
627     BmpFrameDecode_GetPixelFormat,
628     BmpFrameDecode_GetResolution,
629     BmpFrameDecode_CopyPalette,
630     BmpFrameDecode_CopyPixels,
631     BmpFrameDecode_GetMetadataQueryReader,
632     BmpFrameDecode_GetColorContexts,
633     BmpFrameDecode_GetThumbnail
634 };
635
636 typedef struct {
637     const IWICBitmapDecoderVtbl *lpVtbl;
638     LONG ref;
639     BOOL initialized;
640     IStream *stream;
641     BITMAPFILEHEADER bfh;
642     BITMAPV5HEADER bih;
643     BmpFrameDecode *framedecode;
644     const WICPixelFormatGUID *pixelformat;
645     int bitsperpixel;
646     ReadDataFunc read_data_func;
647 } BmpDecoder;
648
649 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
650 {
651     HRESULT hr;
652     ULONG bytestoread, bytesread;
653     LARGE_INTEGER seek;
654
655     if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
656
657     seek.QuadPart = 0;
658     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
659     if (FAILED(hr)) return hr;
660
661     hr = IStream_Read(stream, &This->bfh, sizeof(BITMAPFILEHEADER), &bytesread);
662     if (FAILED(hr)) return hr;
663     if (bytesread != sizeof(BITMAPFILEHEADER) ||
664         This->bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
665
666     hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
667     if (FAILED(hr)) return hr;
668     if (bytesread != sizeof(DWORD) ||
669         (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
670          This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
671          This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
672          This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
673          This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
674
675     bytestoread = This->bih.bV5Size-sizeof(DWORD);
676     hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
677     if (FAILED(hr)) return hr;
678     if (bytestoread != bytesread) return E_FAIL;
679
680     /* decide what kind of bitmap this is and how/if we can read it */
681     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
682     {
683         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
684         TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
685         This->bitsperpixel = bch->bcBitCount;
686         This->read_data_func = BmpFrameDecode_ReadUncompressed;
687         switch(bch->bcBitCount)
688         {
689         case 1:
690             This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
691             break;
692         case 2:
693             This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
694             break;
695         case 4:
696             This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
697             break;
698         case 8:
699             This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
700             break;
701         case 24:
702             This->pixelformat = &GUID_WICPixelFormat24bppBGR;
703             break;
704         default:
705             This->pixelformat = &GUID_WICPixelFormatUndefined;
706             WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
707             break;
708         }
709     }
710     else /* struct is compatible with BITMAPINFOHEADER */
711     {
712         TRACE("bitmap header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
713         switch(This->bih.bV5Compression)
714         {
715         case BI_RGB:
716             This->bitsperpixel = This->bih.bV5BitCount;
717             This->read_data_func = BmpFrameDecode_ReadUncompressed;
718             switch(This->bih.bV5BitCount)
719             {
720             case 1:
721                 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
722                 break;
723             case 2:
724                 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
725                 break;
726             case 4:
727                 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
728                 break;
729             case 8:
730                 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
731                 break;
732             case 16:
733                 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
734                 break;
735             case 24:
736                 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
737                 break;
738             case 32:
739                 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
740                 break;
741             default:
742                 This->pixelformat = &GUID_WICPixelFormatUndefined;
743                 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
744             }
745             break;
746         case BI_RLE8:
747             This->bitsperpixel = 32;
748             This->read_data_func = BmpFrameDecode_ReadRLE8;
749             This->pixelformat = &GUID_WICPixelFormat32bppBGR;
750             break;
751         case BI_RLE4:
752             This->bitsperpixel = 32;
753             This->read_data_func = BmpFrameDecode_ReadRLE4;
754             This->pixelformat = &GUID_WICPixelFormat32bppBGR;
755             break;
756         default:
757             This->bitsperpixel = 0;
758             This->read_data_func = BmpFrameDecode_ReadUnsupported;
759             This->pixelformat = &GUID_WICPixelFormatUndefined;
760             FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
761             break;
762         }
763     }
764
765     This->initialized = TRUE;
766
767     return S_OK;
768 }
769
770 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
771     void **ppv)
772 {
773     BmpDecoder *This = (BmpDecoder*)iface;
774     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
775
776     if (!ppv) return E_INVALIDARG;
777
778     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
779     {
780         *ppv = This;
781     }
782     else
783     {
784         *ppv = NULL;
785         return E_NOINTERFACE;
786     }
787
788     IUnknown_AddRef((IUnknown*)*ppv);
789     return S_OK;
790 }
791
792 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
793 {
794     BmpDecoder *This = (BmpDecoder*)iface;
795     ULONG ref = InterlockedIncrement(&This->ref);
796
797     TRACE("(%p) refcount=%u\n", iface, ref);
798
799     return ref;
800 }
801
802 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
803 {
804     BmpDecoder *This = (BmpDecoder*)iface;
805     ULONG ref = InterlockedDecrement(&This->ref);
806
807     TRACE("(%p) refcount=%u\n", iface, ref);
808
809     if (ref == 0)
810     {
811         if (This->stream) IStream_Release(This->stream);
812         if (This->framedecode) IUnknown_Release((IUnknown*)This->framedecode);
813         HeapFree(GetProcessHeap(), 0, This);
814     }
815
816     return ref;
817 }
818
819 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
820     DWORD *pdwCapability)
821 {
822     HRESULT hr;
823     BmpDecoder *This = (BmpDecoder*)iface;
824
825     hr = BmpDecoder_ReadHeaders(This, pIStream);
826     if (FAILED(hr)) return hr;
827
828     if (This->read_data_func == BmpFrameDecode_ReadUnsupported)
829         *pdwCapability = 0;
830     else
831         *pdwCapability = WICBitmapDecoderCapabilityCanDecodeAllImages;
832
833     return S_OK;
834 }
835
836 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
837     WICDecodeOptions cacheOptions)
838 {
839     HRESULT hr;
840     BmpDecoder *This = (BmpDecoder*)iface;
841
842     hr = BmpDecoder_ReadHeaders(This, pIStream);
843
844     if (SUCCEEDED(hr))
845     {
846         This->stream = pIStream;
847         IStream_AddRef(pIStream);
848     }
849
850     return hr;
851 }
852
853 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
854     GUID *pguidContainerFormat)
855 {
856     memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
857     return S_OK;
858 }
859
860 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
861     IWICBitmapDecoderInfo **ppIDecoderInfo)
862 {
863     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
864     return E_NOTIMPL;
865 }
866
867 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
868     IWICPalette *pIPalette)
869 {
870     TRACE("(%p,%p)\n", iface, pIPalette);
871
872     return WINCODEC_ERR_PALETTEUNAVAILABLE;
873 }
874
875 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
876     IWICMetadataQueryReader **ppIMetadataQueryReader)
877 {
878     TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
879     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
880 }
881
882 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
883     IWICBitmapSource **ppIBitmapSource)
884 {
885     TRACE("(%p,%p)\n", iface, ppIBitmapSource);
886     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
887 }
888
889 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
890     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
891 {
892     TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
893     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
894 }
895
896 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
897     IWICBitmapSource **ppIThumbnail)
898 {
899     TRACE("(%p,%p)\n", iface, ppIThumbnail);
900     return WINCODEC_ERR_CODECNOTHUMBNAIL;
901 }
902
903 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
904     UINT *pCount)
905 {
906     *pCount = 1;
907     return S_OK;
908 }
909
910 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
911     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
912 {
913     BmpDecoder *This = (BmpDecoder*)iface;
914
915     if (index != 0) return E_INVALIDARG;
916
917     if (!This->stream) return WINCODEC_ERR_WRONGSTATE;
918
919     if (!This->framedecode)
920     {
921         This->framedecode = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameDecode));
922         if (!This->framedecode) return E_OUTOFMEMORY;
923
924         This->framedecode->lpVtbl = &BmpFrameDecode_Vtbl;
925         This->framedecode->ref = 1;
926         This->framedecode->stream = This->stream;
927         IStream_AddRef(This->stream);
928         This->framedecode->bfh = This->bfh;
929         This->framedecode->bih = This->bih;
930         This->framedecode->pixelformat = This->pixelformat;
931         This->framedecode->bitsperpixel = This->bitsperpixel;
932         This->framedecode->read_data_func = This->read_data_func;
933         This->framedecode->imagedata = NULL;
934     }
935
936     *ppIBitmapFrame = (IWICBitmapFrameDecode*)This->framedecode;
937     IWICBitmapFrameDecode_AddRef((IWICBitmapFrameDecode*)This->framedecode);
938
939     return S_OK;
940 }
941
942 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
943     BmpDecoder_QueryInterface,
944     BmpDecoder_AddRef,
945     BmpDecoder_Release,
946     BmpDecoder_QueryCapability,
947     BmpDecoder_Initialize,
948     BmpDecoder_GetContainerFormat,
949     BmpDecoder_GetDecoderInfo,
950     BmpDecoder_CopyPalette,
951     BmpDecoder_GetMetadataQueryReader,
952     BmpDecoder_GetPreview,
953     BmpDecoder_GetColorContexts,
954     BmpDecoder_GetThumbnail,
955     BmpDecoder_GetFrameCount,
956     BmpDecoder_GetFrame
957 };
958
959 HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
960 {
961     BmpDecoder *This;
962     HRESULT ret;
963
964     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
965
966     *ppv = NULL;
967
968     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
969
970     This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
971     if (!This) return E_OUTOFMEMORY;
972
973     This->lpVtbl = &BmpDecoder_Vtbl;
974     This->ref = 1;
975     This->initialized = FALSE;
976     This->stream = NULL;
977     This->framedecode = NULL;
978
979     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
980     IUnknown_Release((IUnknown*)This);
981
982     return ret;
983 }