2 * Copyright 2009 Vincent Povirk for CodeWeavers
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.
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.
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
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
49 DWORD bc2ClrImportant;
50 /* same as BITMAPINFOHEADER until this point */
55 DWORD bc2HalftoneSize1;
56 DWORD bc2HalftoneSize2;
61 struct BmpFrameDecode;
62 typedef HRESULT (*ReadDataFunc)(struct BmpFrameDecode* This);
64 typedef struct BmpFrameDecode {
65 const IWICBitmapFrameDecodeVtbl *lpVtbl;
70 const WICPixelFormatGUID *pixelformat;
72 ReadDataFunc read_data_func;
78 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
81 BmpFrameDecode *This = (BmpFrameDecode*)iface;
82 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
84 if (!ppv) return E_INVALIDARG;
86 if (IsEqualIID(&IID_IUnknown, iid) ||
87 IsEqualIID(&IID_IWICBitmapSource, iid) ||
88 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
98 IUnknown_AddRef((IUnknown*)*ppv);
102 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
104 BmpFrameDecode *This = (BmpFrameDecode*)iface;
105 ULONG ref = InterlockedIncrement(&This->ref);
107 TRACE("(%p) refcount=%u\n", iface, ref);
112 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
114 BmpFrameDecode *This = (BmpFrameDecode*)iface;
115 ULONG ref = InterlockedDecrement(&This->ref);
117 TRACE("(%p) refcount=%u\n", iface, ref);
121 IStream_Release(This->stream);
122 HeapFree(GetProcessHeap(), 0, This->imagedata);
123 HeapFree(GetProcessHeap(), 0, This);
129 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
130 UINT *puiWidth, UINT *puiHeight)
132 BmpFrameDecode *This = (BmpFrameDecode*)iface;
133 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
135 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
137 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
138 *puiWidth = bch->bcWidth;
139 *puiHeight = bch->bcHeight;
143 *puiWidth = This->bih.bV5Width;
144 *puiHeight = abs(This->bih.bV5Height);
149 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
150 WICPixelFormatGUID *pPixelFormat)
152 BmpFrameDecode *This = (BmpFrameDecode*)iface;
153 TRACE("(%p,%p)\n", iface, pPixelFormat);
155 memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
160 static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, double *pDpiY)
162 switch (bih->bV5Size)
164 case sizeof(BITMAPCOREHEADER):
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;
180 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
181 double *pDpiX, double *pDpiY)
183 BmpFrameDecode *This = (BmpFrameDecode*)iface;
184 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
186 return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
189 static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
190 IWICPalette *pIPalette)
193 BmpFrameDecode *This = (BmpFrameDecode*)iface;
194 TRACE("(%p,%p)\n", iface, pIPalette);
196 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
198 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
199 if (bch->bcBitCount <= 8)
201 /* 2**n colors in BGR format after the header */
202 int count = 1 << bch->bcBitCount;
204 ULONG tablesize, bytesread;
205 RGBTRIPLE *bgrcolors;
206 LARGE_INTEGER offset;
209 wiccolors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
210 tablesize = sizeof(RGBTRIPLE) * count;
211 bgrcolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
212 if (!wiccolors || !bgrcolors)
214 HeapFree(GetProcessHeap(), 0, wiccolors);
215 HeapFree(GetProcessHeap(), 0, bgrcolors);
216 return E_OUTOFMEMORY;
219 offset.QuadPart = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER);
220 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
221 if (FAILED(hr)) return hr;
223 hr = IStream_Read(This->stream, bgrcolors, tablesize, &bytesread);
224 if (FAILED(hr)) return hr;
225 if (bytesread != tablesize) return E_FAIL;
227 for (i=0; i<count; i++)
229 wiccolors[i] = 0xff000000|
230 (bgrcolors[i].rgbtRed<<16)|
231 (bgrcolors[i].rgbtGreen<<8)|
232 bgrcolors[i].rgbtBlue;
235 hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
237 HeapFree(GetProcessHeap(), 0, wiccolors);
238 HeapFree(GetProcessHeap(), 0, bgrcolors);
243 return WINCODEC_ERR_PALETTEUNAVAILABLE;
248 if (This->bih.bV5BitCount <= 8)
252 ULONG tablesize, bytesread;
253 LARGE_INTEGER offset;
256 if (This->bih.bV5ClrUsed == 0)
257 count = 1 << This->bih.bV5BitCount;
259 count = This->bih.bV5ClrUsed;
261 tablesize = sizeof(WICColor) * count;
262 wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
263 if (!wiccolors) return E_OUTOFMEMORY;
265 offset.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
266 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
267 if (FAILED(hr)) return hr;
269 hr = IStream_Read(This->stream, wiccolors, tablesize, &bytesread);
270 if (FAILED(hr)) return hr;
271 if (bytesread != tablesize) return E_FAIL;
273 /* convert from BGR to BGRA by setting alpha to 100% */
274 for (i=0; i<count; i++)
275 wiccolors[i] |= 0xff000000;
277 hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
279 HeapFree(GetProcessHeap(), 0, wiccolors);
284 return WINCODEC_ERR_PALETTEUNAVAILABLE;
289 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
290 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
292 BmpFrameDecode *This = (BmpFrameDecode*)iface;
295 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
297 if (!This->imagedata)
299 hr = This->read_data_func(This);
300 if (FAILED(hr)) return hr;
303 hr = BmpFrameDecode_GetSize(iface, &width, &height);
304 if (FAILED(hr)) return hr;
306 return copy_pixels(This->bitsperpixel, This->imagedatastart,
307 width, height, This->stride,
308 prc, cbStride, cbBufferSize, pbBuffer);
311 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
312 IWICMetadataQueryReader **ppIMetadataQueryReader)
314 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
315 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
318 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
319 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
321 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
322 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
325 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
326 IWICBitmapSource **ppIThumbnail)
328 TRACE("(%p,%p)\n", iface, ppIThumbnail);
329 return WINCODEC_ERR_CODECNOTHUMBNAIL;
332 static HRESULT BmpFrameDecode_ReadUncompressed(BmpFrameDecode* This)
339 LARGE_INTEGER offbits;
342 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
344 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
345 width = bch->bcWidth;
346 height = bch->bcHeight;
351 width = This->bih.bV5Width;
352 height = abs(This->bih.bV5Height);
353 bottomup = (This->bih.bV5Height > 0);
356 /* row sizes in BMP files must be divisible by 4 bytes */
357 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
358 datasize = bytesperrow * height;
360 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
361 if (!This->imagedata) return E_OUTOFMEMORY;
363 offbits.QuadPart = This->bfh.bfOffBits;
364 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
365 if (FAILED(hr)) goto fail;
367 hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
368 if (FAILED(hr) || bytesread != datasize) goto fail;
372 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
373 This->stride = -bytesperrow;
377 This->imagedatastart = This->imagedata;
378 This->stride = bytesperrow;
383 HeapFree(GetProcessHeap(), 0, This->imagedata);
384 This->imagedata = NULL;
385 if (SUCCEEDED(hr)) hr = E_FAIL;
389 static HRESULT BmpFrameDecode_ReadRLE8(BmpFrameDecode* This)
393 BYTE *rledata, *cursor, *rledataend;
394 UINT rlesize, datasize, palettesize;
399 LARGE_INTEGER offbits;
402 width = This->bih.bV5Width;
403 height = abs(This->bih.bV5Height);
404 bytesperrow = width * 4;
405 datasize = bytesperrow * height;
406 rlesize = This->bih.bV5SizeImage;
407 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 256)
408 palettesize = 4 * This->bih.bV5ClrUsed;
410 palettesize = 4 * 256;
412 rledata = HeapAlloc(GetProcessHeap(), 0, rlesize);
413 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
414 if (!This->imagedata || !rledata)
421 offbits.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
422 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
423 if (FAILED(hr)) goto fail;
425 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
426 if (FAILED(hr) || bytesread != palettesize) goto fail;
429 offbits.QuadPart = This->bfh.bfOffBits;
430 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
431 if (FAILED(hr)) goto fail;
433 hr = IStream_Read(This->stream, rledata, rlesize, &bytesread);
434 if (FAILED(hr) || bytesread != rlesize) goto fail;
437 bgrdata = (DWORD*)This->imagedata;
440 rledataend = rledata + rlesize;
442 while (cursor < rledataend && y < height)
444 BYTE length = *cursor++;
448 BYTE escape = *cursor++;
451 case 0: /* end of line */
455 case 1: /* end of bitmap */
458 if (cursor < rledataend)
464 default: /* absolute mode */
466 while (cursor < rledataend && length-- && x < width)
467 bgrdata[y*width + x++] = palette[*cursor++];
468 if (escape & 1) cursor++; /* skip pad byte */
473 DWORD color = palette[*cursor++];
474 while (length-- && x < width)
475 bgrdata[y*width + x++] = color;
480 HeapFree(GetProcessHeap(), 0, rledata);
482 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
483 This->stride = -bytesperrow;
488 HeapFree(GetProcessHeap(), 0, rledata);
489 HeapFree(GetProcessHeap(), 0, This->imagedata);
490 This->imagedata = NULL;
491 if (SUCCEEDED(hr)) hr = E_FAIL;
495 static HRESULT BmpFrameDecode_ReadRLE4(BmpFrameDecode* This)
499 BYTE *rledata, *cursor, *rledataend;
500 UINT rlesize, datasize, palettesize;
505 LARGE_INTEGER offbits;
508 width = This->bih.bV5Width;
509 height = abs(This->bih.bV5Height);
510 bytesperrow = width * 4;
511 datasize = bytesperrow * height;
512 rlesize = This->bih.bV5SizeImage;
513 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
514 palettesize = 4 * This->bih.bV5ClrUsed;
516 palettesize = 4 * 16;
518 rledata = HeapAlloc(GetProcessHeap(), 0, rlesize);
519 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
520 if (!This->imagedata || !rledata)
527 offbits.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
528 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
529 if (FAILED(hr)) goto fail;
531 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
532 if (FAILED(hr) || bytesread != palettesize) goto fail;
535 offbits.QuadPart = This->bfh.bfOffBits;
536 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
537 if (FAILED(hr)) goto fail;
539 hr = IStream_Read(This->stream, rledata, rlesize, &bytesread);
540 if (FAILED(hr) || bytesread != rlesize) goto fail;
543 bgrdata = (DWORD*)This->imagedata;
546 rledataend = rledata + rlesize;
548 while (cursor < rledataend && y < height)
550 BYTE length = *cursor++;
554 BYTE escape = *cursor++;
557 case 0: /* end of line */
561 case 1: /* end of bitmap */
564 if (cursor < rledataend)
570 default: /* absolute mode */
572 while (cursor < rledataend && length-- && x < width)
574 BYTE colors = *cursor++;
575 bgrdata[y*width + x++] = palette[colors>>4];
576 if (length-- && x < width)
577 bgrdata[y*width + x++] = palette[colors&0xf];
581 if ((cursor - rledata) & 1) cursor++; /* skip pad byte */
586 BYTE colors = *cursor++;
587 DWORD color1 = palette[colors>>4];
588 DWORD color2 = palette[colors&0xf];
589 while (length-- && x < width)
591 bgrdata[y*width + x++] = color1;
592 if (length-- && x < width)
593 bgrdata[y*width + x++] = color2;
601 HeapFree(GetProcessHeap(), 0, rledata);
603 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
604 This->stride = -bytesperrow;
609 HeapFree(GetProcessHeap(), 0, rledata);
610 HeapFree(GetProcessHeap(), 0, This->imagedata);
611 This->imagedata = NULL;
612 if (SUCCEEDED(hr)) hr = E_FAIL;
616 static HRESULT BmpFrameDecode_ReadUnsupported(BmpFrameDecode* This)
621 static const IWICBitmapFrameDecodeVtbl BmpFrameDecode_Vtbl = {
622 BmpFrameDecode_QueryInterface,
623 BmpFrameDecode_AddRef,
624 BmpFrameDecode_Release,
625 BmpFrameDecode_GetSize,
626 BmpFrameDecode_GetPixelFormat,
627 BmpFrameDecode_GetResolution,
628 BmpFrameDecode_CopyPalette,
629 BmpFrameDecode_CopyPixels,
630 BmpFrameDecode_GetMetadataQueryReader,
631 BmpFrameDecode_GetColorContexts,
632 BmpFrameDecode_GetThumbnail
636 const IWICBitmapDecoderVtbl *lpVtbl;
640 BITMAPFILEHEADER bfh;
642 BmpFrameDecode *framedecode;
643 const WICPixelFormatGUID *pixelformat;
645 ReadDataFunc read_data_func;
648 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
651 ULONG bytestoread, bytesread;
654 if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
657 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
658 if (FAILED(hr)) return hr;
660 hr = IStream_Read(stream, &This->bfh, sizeof(BITMAPFILEHEADER), &bytesread);
661 if (FAILED(hr)) return hr;
662 if (bytesread != sizeof(BITMAPFILEHEADER) ||
663 This->bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
665 hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
666 if (FAILED(hr)) return hr;
667 if (bytesread != sizeof(DWORD) ||
668 (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
669 This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
670 This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
671 This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
672 This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
674 bytestoread = This->bih.bV5Size-sizeof(DWORD);
675 hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
676 if (FAILED(hr)) return hr;
677 if (bytestoread != bytesread) return E_FAIL;
679 /* decide what kind of bitmap this is and how/if we can read it */
680 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
682 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
683 TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
684 This->bitsperpixel = bch->bcBitCount;
685 This->read_data_func = BmpFrameDecode_ReadUncompressed;
686 switch(bch->bcBitCount)
689 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
692 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
695 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
698 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
701 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
704 This->pixelformat = &GUID_WICPixelFormatUndefined;
705 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
709 else /* struct is compatible with BITMAPINFOHEADER */
711 TRACE("bitmap header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
712 switch(This->bih.bV5Compression)
715 This->bitsperpixel = This->bih.bV5BitCount;
716 This->read_data_func = BmpFrameDecode_ReadUncompressed;
717 switch(This->bih.bV5BitCount)
720 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
723 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
726 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
729 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
732 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
735 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
738 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
741 This->pixelformat = &GUID_WICPixelFormatUndefined;
742 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
746 This->bitsperpixel = 32;
747 This->read_data_func = BmpFrameDecode_ReadRLE8;
748 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
751 This->bitsperpixel = 32;
752 This->read_data_func = BmpFrameDecode_ReadRLE4;
753 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
756 This->bitsperpixel = 0;
757 This->read_data_func = BmpFrameDecode_ReadUnsupported;
758 This->pixelformat = &GUID_WICPixelFormatUndefined;
759 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
764 This->initialized = TRUE;
769 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
772 BmpDecoder *This = (BmpDecoder*)iface;
773 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
775 if (!ppv) return E_INVALIDARG;
777 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
784 return E_NOINTERFACE;
787 IUnknown_AddRef((IUnknown*)*ppv);
791 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
793 BmpDecoder *This = (BmpDecoder*)iface;
794 ULONG ref = InterlockedIncrement(&This->ref);
796 TRACE("(%p) refcount=%u\n", iface, ref);
801 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
803 BmpDecoder *This = (BmpDecoder*)iface;
804 ULONG ref = InterlockedDecrement(&This->ref);
806 TRACE("(%p) refcount=%u\n", iface, ref);
810 if (This->stream) IStream_Release(This->stream);
811 if (This->framedecode) IUnknown_Release((IUnknown*)This->framedecode);
812 HeapFree(GetProcessHeap(), 0, This);
818 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
819 DWORD *pdwCapability)
822 BmpDecoder *This = (BmpDecoder*)iface;
824 hr = BmpDecoder_ReadHeaders(This, pIStream);
825 if (FAILED(hr)) return hr;
827 if (This->read_data_func == BmpFrameDecode_ReadUnsupported)
830 *pdwCapability = WICBitmapDecoderCapabilityCanDecodeAllImages;
835 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
836 WICDecodeOptions cacheOptions)
839 BmpDecoder *This = (BmpDecoder*)iface;
841 hr = BmpDecoder_ReadHeaders(This, pIStream);
845 This->stream = pIStream;
846 IStream_AddRef(pIStream);
852 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
853 GUID *pguidContainerFormat)
855 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
859 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
860 IWICBitmapDecoderInfo **ppIDecoderInfo)
862 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
866 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
867 IWICPalette *pIPalette)
869 TRACE("(%p,%p)\n", iface, pIPalette);
871 return WINCODEC_ERR_PALETTEUNAVAILABLE;
874 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
875 IWICMetadataQueryReader **ppIMetadataQueryReader)
877 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
878 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
881 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
882 IWICBitmapSource **ppIBitmapSource)
884 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
885 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
888 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
889 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
891 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
892 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
895 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
896 IWICBitmapSource **ppIThumbnail)
898 TRACE("(%p,%p)\n", iface, ppIThumbnail);
899 return WINCODEC_ERR_CODECNOTHUMBNAIL;
902 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
909 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
910 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
912 BmpDecoder *This = (BmpDecoder*)iface;
914 if (index != 0) return E_INVALIDARG;
916 if (!This->stream) return WINCODEC_ERR_WRONGSTATE;
918 if (!This->framedecode)
920 This->framedecode = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameDecode));
921 if (!This->framedecode) return E_OUTOFMEMORY;
923 This->framedecode->lpVtbl = &BmpFrameDecode_Vtbl;
924 This->framedecode->ref = 1;
925 This->framedecode->stream = This->stream;
926 IStream_AddRef(This->stream);
927 This->framedecode->bfh = This->bfh;
928 This->framedecode->bih = This->bih;
929 This->framedecode->pixelformat = This->pixelformat;
930 This->framedecode->bitsperpixel = This->bitsperpixel;
931 This->framedecode->read_data_func = This->read_data_func;
932 This->framedecode->imagedata = NULL;
935 *ppIBitmapFrame = (IWICBitmapFrameDecode*)This->framedecode;
936 IWICBitmapFrameDecode_AddRef((IWICBitmapFrameDecode*)This->framedecode);
941 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
942 BmpDecoder_QueryInterface,
945 BmpDecoder_QueryCapability,
946 BmpDecoder_Initialize,
947 BmpDecoder_GetContainerFormat,
948 BmpDecoder_GetDecoderInfo,
949 BmpDecoder_CopyPalette,
950 BmpDecoder_GetMetadataQueryReader,
951 BmpDecoder_GetPreview,
952 BmpDecoder_GetColorContexts,
953 BmpDecoder_GetThumbnail,
954 BmpDecoder_GetFrameCount,
958 HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
963 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
967 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
969 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
970 if (!This) return E_OUTOFMEMORY;
972 This->lpVtbl = &BmpDecoder_Vtbl;
974 This->initialized = FALSE;
976 This->framedecode = NULL;
978 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
979 IUnknown_Release((IUnknown*)This);