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
19 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
37 #include "wincodecs_private.h"
39 #include <wine/debug.h>
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
54 DWORD bc2ClrImportant
;
55 /* same as BITMAPINFOHEADER until this point */
60 DWORD bc2HalftoneSize1
;
61 DWORD bc2HalftoneSize2
;
66 typedef HRESULT (*ReadDataFunc
)(BmpDecoder
* This
);
69 IWICBitmapDecoder IWICBitmapDecoder_iface
;
70 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
77 const WICPixelFormatGUID
*pixelformat
;
79 ReadDataFunc read_data_func
;
83 CRITICAL_SECTION lock
; /* must be held when initialized/imagedata is set or stream is accessed */
84 int packed
; /* If TRUE, don't look for a file header and assume a packed DIB. */
85 int icoframe
; /* If TRUE, this is a frame of a .ico file. */
88 static inline BmpDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
90 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapDecoder_iface
);
93 static inline BmpDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
95 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapFrameDecode_iface
);
98 static HRESULT WINAPI
BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
101 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
103 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
105 if (!ppv
) return E_INVALIDARG
;
107 if (IsEqualIID(&IID_IUnknown
, iid
) ||
108 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
109 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
111 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
116 return E_NOINTERFACE
;
119 IUnknown_AddRef((IUnknown
*)*ppv
);
123 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
125 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
127 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
130 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
132 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
134 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
137 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
138 UINT
*puiWidth
, UINT
*puiHeight
)
140 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
141 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
143 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
145 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
146 *puiWidth
= bch
->bcWidth
;
147 *puiHeight
= bch
->bcHeight
;
151 *puiWidth
= This
->bih
.bV5Width
;
152 *puiHeight
= abs(This
->bih
.bV5Height
);
157 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
158 WICPixelFormatGUID
*pPixelFormat
)
160 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
161 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
163 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
168 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
170 switch (bih
->bV5Size
)
172 case sizeof(BITMAPCOREHEADER
):
176 case sizeof(BITMAPCOREHEADER2
):
177 case sizeof(BITMAPINFOHEADER
):
178 case sizeof(BITMAPV4HEADER
):
179 case sizeof(BITMAPV5HEADER
):
180 *pDpiX
= bih
->bV5XPelsPerMeter
* 0.0254;
181 *pDpiY
= bih
->bV5YPelsPerMeter
* 0.0254;
188 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
189 double *pDpiX
, double *pDpiY
)
191 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
192 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
194 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
197 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
198 IWICPalette
*pIPalette
)
201 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
203 WICColor
*wiccolors
=NULL
;
204 RGBTRIPLE
*bgrcolors
=NULL
;
206 TRACE("(%p,%p)\n", iface
, pIPalette
);
208 EnterCriticalSection(&This
->lock
);
210 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
212 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
213 if (bch
->bcBitCount
<= 8)
215 /* 2**n colors in BGR format after the header */
216 ULONG tablesize
, bytesread
;
217 LARGE_INTEGER offset
;
220 count
= 1 << bch
->bcBitCount
;
221 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
222 tablesize
= sizeof(RGBTRIPLE
) * count
;
223 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
224 if (!wiccolors
|| !bgrcolors
)
230 offset
.QuadPart
= This
->palette_offset
;
231 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
232 if (FAILED(hr
)) goto end
;
234 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
235 if (FAILED(hr
)) goto end
;
236 if (bytesread
!= tablesize
) {
241 for (i
=0; i
<count
; i
++)
243 wiccolors
[i
] = 0xff000000|
244 (bgrcolors
[i
].rgbtRed
<<16)|
245 (bgrcolors
[i
].rgbtGreen
<<8)|
246 bgrcolors
[i
].rgbtBlue
;
251 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
257 if (This
->bih
.bV5BitCount
<= 8)
259 ULONG tablesize
, bytesread
;
260 LARGE_INTEGER offset
;
263 if (This
->bih
.bV5ClrUsed
== 0)
264 count
= 1 << This
->bih
.bV5BitCount
;
266 count
= This
->bih
.bV5ClrUsed
;
268 tablesize
= sizeof(WICColor
) * count
;
269 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
276 offset
.QuadPart
= This
->palette_offset
;
277 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
278 if (FAILED(hr
)) goto end
;
280 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
281 if (FAILED(hr
)) goto end
;
282 if (bytesread
!= tablesize
) {
287 /* convert from BGR to BGRA by setting alpha to 100% */
288 for (i
=0; i
<count
; i
++)
289 wiccolors
[i
] |= 0xff000000;
293 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
300 LeaveCriticalSection(&This
->lock
);
303 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
305 HeapFree(GetProcessHeap(), 0, wiccolors
);
306 HeapFree(GetProcessHeap(), 0, bgrcolors
);
310 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
311 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
313 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
316 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
318 EnterCriticalSection(&This
->lock
);
319 if (!This
->imagedata
)
321 hr
= This
->read_data_func(This
);
323 LeaveCriticalSection(&This
->lock
);
324 if (FAILED(hr
)) return hr
;
326 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
327 if (FAILED(hr
)) return hr
;
329 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
330 width
, height
, This
->stride
,
331 prc
, cbStride
, cbBufferSize
, pbBuffer
);
334 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
335 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
337 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
338 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
341 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
342 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
344 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
345 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
348 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
349 IWICBitmapSource
**ppIThumbnail
)
351 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
352 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
355 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
362 LARGE_INTEGER offbits
;
365 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
367 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
368 width
= bch
->bcWidth
;
369 height
= bch
->bcHeight
;
374 width
= This
->bih
.bV5Width
;
375 height
= abs(This
->bih
.bV5Height
);
376 bottomup
= (This
->bih
.bV5Height
> 0);
379 /* row sizes in BMP files must be divisible by 4 bytes */
380 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
381 datasize
= bytesperrow
* height
;
383 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
384 if (!This
->imagedata
) return E_OUTOFMEMORY
;
386 offbits
.QuadPart
= This
->image_offset
;
387 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
388 if (FAILED(hr
)) goto fail
;
390 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
391 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
395 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
396 This
->stride
= -bytesperrow
;
400 This
->imagedatastart
= This
->imagedata
;
401 This
->stride
= bytesperrow
;
406 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
407 This
->imagedata
= NULL
;
408 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
412 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
417 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
421 hr
= BmpFrameDecode_ReadUncompressed(This
);
426 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
427 width
, height
, This
->stride
);
433 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
434 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
438 if (*bytesread
== 0 || *cursor
== *bytesread
)
440 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
446 if (*cursor
< *bytesread
)
447 *result
= buffer
[(*cursor
)++];
455 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
460 UINT datasize
, palettesize
;
465 LARGE_INTEGER offbits
;
466 ULONG cursor
=0, bytesread
=0;
468 width
= This
->bih
.bV5Width
;
469 height
= abs(This
->bih
.bV5Height
);
470 bytesperrow
= width
* 4;
471 datasize
= bytesperrow
* height
;
472 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
473 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
475 palettesize
= 4 * 256;
477 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
478 if (!This
->imagedata
)
485 offbits
.QuadPart
= This
->palette_offset
;
486 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
487 if (FAILED(hr
)) goto fail
;
489 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
490 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
493 offbits
.QuadPart
= This
->image_offset
;
494 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
495 if (FAILED(hr
)) goto fail
;
498 bgrdata
= (DWORD
*)This
->imagedata
;
506 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
510 else if (length
== 0)
514 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
519 case 0: /* end of line */
523 case 1: /* end of bitmap */
528 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
530 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
537 default: /* absolute mode */
539 while (length
-- && x
< width
)
542 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
545 bgrdata
[y
*width
+ x
++] = palette
[index
];
548 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
557 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
560 color
= palette
[index
];
561 while (length
-- && x
< width
)
562 bgrdata
[y
*width
+ x
++] = color
;
567 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
568 This
->stride
= -bytesperrow
;
573 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
574 This
->imagedata
= NULL
;
575 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
579 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
584 UINT datasize
, palettesize
;
589 LARGE_INTEGER offbits
;
590 ULONG cursor
=0, bytesread
=0;
592 width
= This
->bih
.bV5Width
;
593 height
= abs(This
->bih
.bV5Height
);
594 bytesperrow
= width
* 4;
595 datasize
= bytesperrow
* height
;
596 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
597 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
599 palettesize
= 4 * 16;
601 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
602 if (!This
->imagedata
)
609 offbits
.QuadPart
= This
->palette_offset
;
610 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
611 if (FAILED(hr
)) goto fail
;
613 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
614 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
617 offbits
.QuadPart
= This
->image_offset
;
618 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
619 if (FAILED(hr
)) goto fail
;
622 bgrdata
= (DWORD
*)This
->imagedata
;
630 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
634 else if (length
== 0)
638 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
643 case 0: /* end of line */
647 case 1: /* end of bitmap */
652 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
654 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
661 default: /* absolute mode */
665 while (length
-- && x
< width
)
668 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
672 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
673 if (length
-- && x
< width
)
674 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
679 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
690 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
693 color1
= palette
[colors
>>4];
694 color2
= palette
[colors
&0xf];
695 while (length
-- && x
< width
)
697 bgrdata
[y
*width
+ x
++] = color1
;
698 if (length
-- && x
< width
)
699 bgrdata
[y
*width
+ x
++] = color2
;
707 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
708 This
->stride
= -bytesperrow
;
713 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
714 This
->imagedata
= NULL
;
715 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
719 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
724 struct bitfields_format
{
725 WORD bitcount
; /* 0 for end of list */
730 const WICPixelFormatGUID
*pixelformat
;
731 ReadDataFunc read_data_func
;
734 static const struct bitfields_format bitfields_formats
[] = {
735 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
736 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
737 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
738 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
739 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
743 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
744 BmpFrameDecode_QueryInterface
,
745 BmpFrameDecode_AddRef
,
746 BmpFrameDecode_Release
,
747 BmpFrameDecode_GetSize
,
748 BmpFrameDecode_GetPixelFormat
,
749 BmpFrameDecode_GetResolution
,
750 BmpFrameDecode_CopyPalette
,
751 BmpFrameDecode_CopyPixels
,
752 BmpFrameDecode_GetMetadataQueryReader
,
753 BmpFrameDecode_GetColorContexts
,
754 BmpFrameDecode_GetThumbnail
757 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
760 ULONG bytestoread
, bytesread
;
763 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
766 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
767 if (FAILED(hr
)) return hr
;
771 BITMAPFILEHEADER bfh
;
772 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
773 if (FAILED(hr
)) return hr
;
774 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
775 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
776 This
->image_offset
= bfh
.bfOffBits
;
779 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
780 if (FAILED(hr
)) return hr
;
781 if (bytesread
!= sizeof(DWORD
) ||
782 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
783 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
784 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
785 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
786 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
788 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
789 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
790 if (FAILED(hr
)) return hr
;
791 if (bytestoread
!= bytesread
) return E_FAIL
;
794 This
->palette_offset
= This
->bih
.bV5Size
;
796 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
800 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
802 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
807 This
->bih
.bV5Height
/= 2;
811 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
812 read the extra fields */
813 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
814 This
->bih
.bV5Compression
== BI_BITFIELDS
)
816 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
817 if (FAILED(hr
)) return hr
;
818 if (bytesread
!= 12) return E_FAIL
;
819 This
->bih
.bV5AlphaMask
= 0;
820 This
->palette_offset
+= 12;
823 /* decide what kind of bitmap this is and how/if we can read it */
824 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
826 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
827 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
828 This
->bitsperpixel
= bch
->bcBitCount
;
829 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
830 switch(bch
->bcBitCount
)
833 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
836 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
839 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
842 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
845 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
848 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
849 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
853 else /* struct is compatible with BITMAPINFOHEADER */
855 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
856 switch(This
->bih
.bV5Compression
)
859 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
860 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
861 switch(This
->bih
.bV5BitCount
)
864 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
867 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
870 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
873 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
876 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
879 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
882 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
885 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
886 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
890 This
->bitsperpixel
= 32;
891 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
892 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
895 This
->bitsperpixel
= 32;
896 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
897 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
901 const struct bitfields_format
*format
;
902 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
904 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
905 This
->bitsperpixel
= 0;
906 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
907 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
908 FIXME("Huffman 1D compression is unsupported\n");
911 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
912 for (format
= bitfields_formats
; format
->bitcount
; format
++)
914 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
915 (format
->redmask
== This
->bih
.bV5RedMask
) &&
916 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
917 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
918 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
920 This
->read_data_func
= format
->read_data_func
;
921 This
->pixelformat
= format
->pixelformat
;
925 if (!format
->bitcount
)
927 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
928 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
929 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
930 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
935 This
->bitsperpixel
= 0;
936 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
937 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
938 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
945 /* In a packed DIB, the image follows the palette. */
946 ULONG palette_count
, palette_size
;
947 if (This
->bih
.bV5ClrUsed
)
948 palette_count
= This
->bih
.bV5ClrUsed
;
949 else if (This
->bih
.bV5BitCount
<= 8)
950 palette_count
= 1 << This
->bih
.bV5BitCount
;
953 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
954 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
956 palette_size
= sizeof(RGBQUAD
) * palette_count
;
957 This
->image_offset
= This
->palette_offset
+ palette_size
;
960 This
->initialized
= TRUE
;
965 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
968 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
969 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
971 if (!ppv
) return E_INVALIDARG
;
973 if (IsEqualIID(&IID_IUnknown
, iid
) ||
974 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
976 *ppv
= &This
->IWICBitmapDecoder_iface
;
981 return E_NOINTERFACE
;
984 IUnknown_AddRef((IUnknown
*)*ppv
);
988 static ULONG WINAPI
BmpDecoder_AddRef(IWICBitmapDecoder
*iface
)
990 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
991 ULONG ref
= InterlockedIncrement(&This
->ref
);
993 TRACE("(%p) refcount=%u\n", iface
, ref
);
998 static ULONG WINAPI
BmpDecoder_Release(IWICBitmapDecoder
*iface
)
1000 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1001 ULONG ref
= InterlockedDecrement(&This
->ref
);
1003 TRACE("(%p) refcount=%u\n", iface
, ref
);
1007 if (This
->stream
) IStream_Release(This
->stream
);
1008 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
1009 This
->lock
.DebugInfo
->Spare
[0] = 0;
1010 DeleteCriticalSection(&This
->lock
);
1011 HeapFree(GetProcessHeap(), 0, This
);
1017 static HRESULT WINAPI
BmpDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
1021 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1023 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
1025 if (!stream
|| !capability
) return E_INVALIDARG
;
1027 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
1028 if (hr
!= S_OK
) return hr
;
1030 *capability
= This
->read_data_func
== BmpFrameDecode_ReadUnsupported
? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages
;
1034 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1035 WICDecodeOptions cacheOptions
)
1038 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1040 EnterCriticalSection(&This
->lock
);
1041 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1045 This
->stream
= pIStream
;
1046 IStream_AddRef(pIStream
);
1048 LeaveCriticalSection(&This
->lock
);
1053 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1054 GUID
*pguidContainerFormat
)
1056 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1060 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1061 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1064 IWICComponentInfo
*compinfo
;
1066 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1068 hr
= CreateComponentInfo(&CLSID_WICBmpDecoder
, &compinfo
);
1069 if (FAILED(hr
)) return hr
;
1071 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1072 (void**)ppIDecoderInfo
);
1074 IWICComponentInfo_Release(compinfo
);
1079 static HRESULT WINAPI
BmpDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1080 IWICPalette
*pIPalette
)
1082 TRACE("(%p,%p)\n", iface
, pIPalette
);
1084 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1087 static HRESULT WINAPI
BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1088 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1090 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1091 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1094 static HRESULT WINAPI
BmpDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1095 IWICBitmapSource
**ppIBitmapSource
)
1097 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1098 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1101 static HRESULT WINAPI
BmpDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1102 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1104 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1105 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1108 static HRESULT WINAPI
BmpDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1109 IWICBitmapSource
**ppIThumbnail
)
1111 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1112 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1115 static HRESULT WINAPI
BmpDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1118 if (!pCount
) return E_INVALIDARG
;
1124 static HRESULT WINAPI
BmpDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1125 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1127 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1129 if (index
!= 0) return E_INVALIDARG
;
1131 if (!This
->stream
) return WINCODEC_ERR_FRAMEMISSING
;
1133 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
1134 IWICBitmapDecoder_AddRef(iface
);
1139 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl
= {
1140 BmpDecoder_QueryInterface
,
1143 BmpDecoder_QueryCapability
,
1144 BmpDecoder_Initialize
,
1145 BmpDecoder_GetContainerFormat
,
1146 BmpDecoder_GetDecoderInfo
,
1147 BmpDecoder_CopyPalette
,
1148 BmpDecoder_GetMetadataQueryReader
,
1149 BmpDecoder_GetPreview
,
1150 BmpDecoder_GetColorContexts
,
1151 BmpDecoder_GetThumbnail
,
1152 BmpDecoder_GetFrameCount
,
1156 static HRESULT
BmpDecoder_Create(int packed
, int icoframe
, BmpDecoder
**ppDecoder
)
1160 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder
));
1161 if (!This
) return E_OUTOFMEMORY
;
1163 This
->IWICBitmapDecoder_iface
.lpVtbl
= &BmpDecoder_Vtbl
;
1164 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &BmpDecoder_FrameVtbl
;
1166 This
->initialized
= FALSE
;
1167 This
->stream
= NULL
;
1168 This
->imagedata
= NULL
;
1169 InitializeCriticalSection(&This
->lock
);
1170 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BmpDecoder.lock");
1171 This
->packed
= packed
;
1172 This
->icoframe
= icoframe
;
1179 static HRESULT
BmpDecoder_Construct(int packed
, int icoframe
, IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1184 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1188 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1190 ret
= BmpDecoder_Create(packed
, icoframe
, &This
);
1191 if (FAILED(ret
)) return ret
;
1193 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1194 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1199 HRESULT
BmpDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1201 return BmpDecoder_Construct(FALSE
, FALSE
, pUnkOuter
, iid
, ppv
);
1204 HRESULT
DibDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1206 return BmpDecoder_Construct(TRUE
, FALSE
, pUnkOuter
, iid
, ppv
);
1209 HRESULT
IcoDibDecoder_CreateInstance(BmpDecoder
**ppDecoder
)
1211 return BmpDecoder_Create(TRUE
, TRUE
, ppDecoder
);
1214 void BmpDecoder_GetWICDecoder(BmpDecoder
*This
, IWICBitmapDecoder
**ppDecoder
)
1216 *ppDecoder
= &This
->IWICBitmapDecoder_iface
;
1219 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1220 void BmpDecoder_FindIconMask(BmpDecoder
*This
, ULONG
*mask_offset
, int *topdown
)
1222 assert(This
->stream
!= NULL
);
1224 if (This
->read_data_func
== BmpFrameDecode_ReadUncompressed
)
1226 /* RGB or BITFIELDS data */
1227 ULONG width
, height
, bytesperrow
, datasize
;
1228 IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
1229 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
1230 datasize
= bytesperrow
* height
;
1231 *mask_offset
= This
->image_offset
+ datasize
;
1236 *topdown
= This
->stride
> 0;