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)
192 FIXME("(%p,%p): stub\n", iface, pIPalette);
196 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
197 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
199 BmpFrameDecode *This = (BmpFrameDecode*)iface;
202 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
204 if (!This->imagedata)
206 hr = This->read_data_func(This);
207 if (FAILED(hr)) return hr;
210 hr = BmpFrameDecode_GetSize(iface, &width, &height);
211 if (FAILED(hr)) return hr;
213 return copy_pixels(This->bitsperpixel, This->imagedatastart,
214 width, height, This->stride,
215 prc, cbStride, cbBufferSize, pbBuffer);
218 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
219 IWICMetadataQueryReader **ppIMetadataQueryReader)
221 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
225 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
226 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
228 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
232 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
233 IWICBitmapSource **ppIThumbnail)
235 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
239 static HRESULT BmpFrameDecode_ReadUncompressed(BmpFrameDecode* This)
246 LARGE_INTEGER offbits;
249 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
251 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
252 width = bch->bcWidth;
253 height = bch->bcHeight;
258 width = This->bih.bV5Width;
259 height = abs(This->bih.bV5Height);
260 bottomup = (This->bih.bV5Height > 0);
263 /* row sizes in BMP files must be divisible by 4 bytes */
264 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
265 datasize = bytesperrow * height;
267 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
268 if (!This->imagedata) return E_OUTOFMEMORY;
270 offbits.QuadPart = This->bfh.bfOffBits;
271 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
272 if (FAILED(hr)) goto fail;
274 hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
275 if (FAILED(hr) || bytesread != datasize) goto fail;
279 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
280 This->stride = -bytesperrow;
284 This->imagedatastart = This->imagedata;
285 This->stride = bytesperrow;
290 HeapFree(GetProcessHeap(), 0, This->imagedata);
291 This->imagedata = NULL;
292 if (SUCCEEDED(hr)) hr = E_FAIL;
296 static HRESULT BmpFrameDecode_ReadUnsupported(BmpFrameDecode* This)
301 static const IWICBitmapFrameDecodeVtbl BmpFrameDecode_Vtbl = {
302 BmpFrameDecode_QueryInterface,
303 BmpFrameDecode_AddRef,
304 BmpFrameDecode_Release,
305 BmpFrameDecode_GetSize,
306 BmpFrameDecode_GetPixelFormat,
307 BmpFrameDecode_GetResolution,
308 BmpFrameDecode_CopyPalette,
309 BmpFrameDecode_CopyPixels,
310 BmpFrameDecode_GetMetadataQueryReader,
311 BmpFrameDecode_GetColorContexts,
312 BmpFrameDecode_GetThumbnail
316 const IWICBitmapDecoderVtbl *lpVtbl;
320 BITMAPFILEHEADER bfh;
322 BmpFrameDecode *framedecode;
323 const WICPixelFormatGUID *pixelformat;
325 ReadDataFunc read_data_func;
328 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
331 ULONG bytestoread, bytesread;
334 if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
337 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
338 if (FAILED(hr)) return hr;
340 hr = IStream_Read(stream, &This->bfh, sizeof(BITMAPFILEHEADER), &bytesread);
341 if (FAILED(hr)) return hr;
342 if (bytesread != sizeof(BITMAPFILEHEADER) ||
343 This->bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
345 hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
346 if (FAILED(hr)) return hr;
347 if (bytesread != sizeof(DWORD) ||
348 (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
349 This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
350 This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
351 This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
352 This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
354 bytestoread = This->bih.bV5Size-sizeof(DWORD);
355 hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
356 if (FAILED(hr)) return hr;
357 if (bytestoread != bytesread) return E_FAIL;
359 /* decide what kind of bitmap this is and how/if we can read it */
360 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
362 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
363 TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
364 This->bitsperpixel = bch->bcBitCount;
365 This->read_data_func = BmpFrameDecode_ReadUncompressed;
366 switch(bch->bcBitCount)
369 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
372 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
375 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
378 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
381 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
384 This->pixelformat = &GUID_WICPixelFormatUndefined;
385 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
389 else /* struct is compatible with BITMAPINFOHEADER */
391 TRACE("bitmap header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
392 switch(This->bih.bV5Compression)
395 This->bitsperpixel = This->bih.bV5BitCount;
396 This->read_data_func = BmpFrameDecode_ReadUncompressed;
397 switch(This->bih.bV5BitCount)
400 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
403 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
406 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
409 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
412 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
415 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
418 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
421 This->pixelformat = &GUID_WICPixelFormatUndefined;
422 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
426 This->bitsperpixel = 0;
427 This->read_data_func = BmpFrameDecode_ReadUnsupported;
428 This->pixelformat = &GUID_WICPixelFormatUndefined;
429 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
434 This->initialized = TRUE;
439 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
442 BmpDecoder *This = (BmpDecoder*)iface;
443 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
445 if (!ppv) return E_INVALIDARG;
447 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
454 return E_NOINTERFACE;
457 IUnknown_AddRef((IUnknown*)*ppv);
461 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
463 BmpDecoder *This = (BmpDecoder*)iface;
464 ULONG ref = InterlockedIncrement(&This->ref);
466 TRACE("(%p) refcount=%u\n", iface, ref);
471 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
473 BmpDecoder *This = (BmpDecoder*)iface;
474 ULONG ref = InterlockedDecrement(&This->ref);
476 TRACE("(%p) refcount=%u\n", iface, ref);
480 if (This->stream) IStream_Release(This->stream);
481 if (This->framedecode) IUnknown_Release((IUnknown*)This->framedecode);
482 HeapFree(GetProcessHeap(), 0, This);
488 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
489 DWORD *pdwCapability)
492 BmpDecoder *This = (BmpDecoder*)iface;
494 hr = BmpDecoder_ReadHeaders(This, pIStream);
495 if (FAILED(hr)) return hr;
497 if (This->read_data_func == BmpFrameDecode_ReadUnsupported)
500 *pdwCapability = WICBitmapDecoderCapabilityCanDecodeAllImages;
505 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
506 WICDecodeOptions cacheOptions)
509 BmpDecoder *This = (BmpDecoder*)iface;
511 hr = BmpDecoder_ReadHeaders(This, pIStream);
515 This->stream = pIStream;
516 IStream_AddRef(pIStream);
522 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
523 GUID *pguidContainerFormat)
525 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
529 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
530 IWICBitmapDecoderInfo **ppIDecoderInfo)
532 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
536 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
537 IWICPalette *pIPalette)
539 FIXME("(%p,%p): stub\n", iface, pIPalette);
543 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
544 IWICMetadataQueryReader **ppIMetadataQueryReader)
546 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
550 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
551 IWICBitmapSource **ppIBitmapSource)
553 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
557 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
558 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
560 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
564 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
565 IWICBitmapSource **ppIThumbnail)
567 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
571 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
578 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
579 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
581 BmpDecoder *This = (BmpDecoder*)iface;
583 if (index != 0) return E_INVALIDARG;
585 if (!This->stream) return WINCODEC_ERR_WRONGSTATE;
587 if (!This->framedecode)
589 This->framedecode = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameDecode));
590 if (!This->framedecode) return E_OUTOFMEMORY;
592 This->framedecode->lpVtbl = &BmpFrameDecode_Vtbl;
593 This->framedecode->ref = 1;
594 This->framedecode->stream = This->stream;
595 IStream_AddRef(This->stream);
596 This->framedecode->bfh = This->bfh;
597 This->framedecode->bih = This->bih;
598 This->framedecode->pixelformat = This->pixelformat;
599 This->framedecode->bitsperpixel = This->bitsperpixel;
600 This->framedecode->read_data_func = This->read_data_func;
601 This->framedecode->imagedata = NULL;
604 *ppIBitmapFrame = (IWICBitmapFrameDecode*)This->framedecode;
605 IWICBitmapFrameDecode_AddRef((IWICBitmapFrameDecode*)This->framedecode);
610 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
611 BmpDecoder_QueryInterface,
614 BmpDecoder_QueryCapability,
615 BmpDecoder_Initialize,
616 BmpDecoder_GetContainerFormat,
617 BmpDecoder_GetDecoderInfo,
618 BmpDecoder_CopyPalette,
619 BmpDecoder_GetMetadataQueryReader,
620 BmpDecoder_GetPreview,
621 BmpDecoder_GetColorContexts,
622 BmpDecoder_GetThumbnail,
623 BmpDecoder_GetFrameCount,
627 HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
632 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
636 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
638 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
639 if (!This) return E_OUTOFMEMORY;
641 This->lpVtbl = &BmpDecoder_Vtbl;
643 This->initialized = FALSE;
645 This->framedecode = NULL;
647 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
648 IUnknown_Release((IUnknown*)This);