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
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
47 DWORD bc2ClrImportant
;
48 /* same as BITMAPINFOHEADER until this point */
53 DWORD bc2HalftoneSize1
;
54 DWORD bc2HalftoneSize2
;
59 typedef HRESULT (*ReadDataFunc
)(BmpDecoder
* This
);
62 IWICBitmapDecoder IWICBitmapDecoder_iface
;
63 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
70 const WICPixelFormatGUID
*pixelformat
;
72 ReadDataFunc read_data_func
;
76 CRITICAL_SECTION lock
; /* must be held when initialized/imagedata is set or stream is accessed */
77 int packed
; /* If TRUE, don't look for a file header and assume a packed DIB. */
78 int icoframe
; /* If TRUE, this is a frame of a .ico file. */
81 static inline BmpDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
83 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapDecoder_iface
);
86 static inline BmpDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
88 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapFrameDecode_iface
);
91 static HRESULT WINAPI
BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
94 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
96 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
98 if (!ppv
) return E_INVALIDARG
;
100 if (IsEqualIID(&IID_IUnknown
, iid
) ||
101 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
102 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
104 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
109 return E_NOINTERFACE
;
112 IUnknown_AddRef((IUnknown
*)*ppv
);
116 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
118 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
120 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
123 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
125 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
127 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
130 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
131 UINT
*puiWidth
, UINT
*puiHeight
)
133 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
134 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
136 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
138 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
139 *puiWidth
= bch
->bcWidth
;
140 *puiHeight
= bch
->bcHeight
;
144 *puiWidth
= This
->bih
.bV5Width
;
145 *puiHeight
= abs(This
->bih
.bV5Height
);
150 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
151 WICPixelFormatGUID
*pPixelFormat
)
153 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
154 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
156 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
161 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
163 LONG resx
= 0, resy
= 0;
165 switch (bih
->bV5Size
)
168 case sizeof(BITMAPCOREHEADER
):
171 case sizeof(BITMAPCOREHEADER2
):
172 case sizeof(BITMAPINFOHEADER
):
173 case sizeof(BITMAPV4HEADER
):
174 case sizeof(BITMAPV5HEADER
):
175 resx
= bih
->bV5XPelsPerMeter
;
176 resy
= bih
->bV5YPelsPerMeter
;
187 *pDpiX
= resx
* 0.0254;
188 *pDpiY
= resy
* 0.0254;
194 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
195 double *pDpiX
, double *pDpiY
)
197 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
198 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
200 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
203 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
204 IWICPalette
*pIPalette
)
207 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
209 WICColor
*wiccolors
=NULL
;
210 RGBTRIPLE
*bgrcolors
=NULL
;
212 TRACE("(%p,%p)\n", iface
, pIPalette
);
214 EnterCriticalSection(&This
->lock
);
216 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
218 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
219 if (bch
->bcBitCount
<= 8)
221 /* 2**n colors in BGR format after the header */
222 ULONG tablesize
, bytesread
;
223 LARGE_INTEGER offset
;
226 count
= 1 << bch
->bcBitCount
;
227 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
228 tablesize
= sizeof(RGBTRIPLE
) * count
;
229 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
230 if (!wiccolors
|| !bgrcolors
)
236 offset
.QuadPart
= This
->palette_offset
;
237 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
238 if (FAILED(hr
)) goto end
;
240 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
241 if (FAILED(hr
)) goto end
;
242 if (bytesread
!= tablesize
) {
247 for (i
=0; i
<count
; i
++)
249 wiccolors
[i
] = 0xff000000|
250 (bgrcolors
[i
].rgbtRed
<<16)|
251 (bgrcolors
[i
].rgbtGreen
<<8)|
252 bgrcolors
[i
].rgbtBlue
;
257 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
263 if (This
->bih
.bV5BitCount
<= 8)
265 ULONG tablesize
, bytesread
;
266 LARGE_INTEGER offset
;
269 if (This
->bih
.bV5ClrUsed
== 0)
270 count
= 1 << This
->bih
.bV5BitCount
;
272 count
= min(This
->bih
.bV5ClrUsed
, 1 << This
->bih
.bV5BitCount
);
274 tablesize
= sizeof(WICColor
) * count
;
275 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
282 offset
.QuadPart
= This
->palette_offset
;
283 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
284 if (FAILED(hr
)) goto end
;
286 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
287 if (FAILED(hr
)) goto end
;
288 if (bytesread
!= tablesize
) {
293 /* convert from BGR to BGRA by setting alpha to 100% */
294 for (i
=0; i
<count
; i
++)
295 wiccolors
[i
] |= 0xff000000;
299 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
306 LeaveCriticalSection(&This
->lock
);
309 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
311 HeapFree(GetProcessHeap(), 0, wiccolors
);
312 HeapFree(GetProcessHeap(), 0, bgrcolors
);
316 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
317 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
319 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
322 TRACE("(%p,%s,%u,%u,%p)\n", iface
, debug_wic_rect(prc
), cbStride
, cbBufferSize
, pbBuffer
);
324 EnterCriticalSection(&This
->lock
);
325 if (!This
->imagedata
)
327 hr
= This
->read_data_func(This
);
329 LeaveCriticalSection(&This
->lock
);
330 if (FAILED(hr
)) return hr
;
332 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
333 if (FAILED(hr
)) return hr
;
335 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
336 width
, height
, This
->stride
,
337 prc
, cbStride
, cbBufferSize
, pbBuffer
);
340 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
341 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
343 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
344 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
347 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
348 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
350 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
351 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
354 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
355 IWICBitmapSource
**ppIThumbnail
)
357 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
358 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
361 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
368 LARGE_INTEGER offbits
;
371 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
373 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
374 width
= bch
->bcWidth
;
375 height
= bch
->bcHeight
;
380 width
= This
->bih
.bV5Width
;
381 height
= abs(This
->bih
.bV5Height
);
382 bottomup
= (This
->bih
.bV5Height
> 0);
385 /* row sizes in BMP files must be divisible by 4 bytes */
386 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
387 datasize
= bytesperrow
* height
;
389 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
390 if (!This
->imagedata
) return E_OUTOFMEMORY
;
392 offbits
.QuadPart
= This
->image_offset
;
393 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
394 if (FAILED(hr
)) goto fail
;
396 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
397 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
401 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
402 This
->stride
= -bytesperrow
;
406 This
->imagedatastart
= This
->imagedata
;
407 This
->stride
= bytesperrow
;
412 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
413 This
->imagedata
= NULL
;
414 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
418 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
423 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
427 hr
= BmpFrameDecode_ReadUncompressed(This
);
432 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
433 width
, height
, This
->stride
);
439 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
440 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
444 if (*bytesread
== 0 || *cursor
== *bytesread
)
446 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
452 if (*cursor
< *bytesread
)
453 *result
= buffer
[(*cursor
)++];
461 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
466 UINT datasize
, palettesize
;
471 LARGE_INTEGER offbits
;
472 ULONG cursor
=0, bytesread
=0;
474 width
= This
->bih
.bV5Width
;
475 height
= abs(This
->bih
.bV5Height
);
476 bytesperrow
= width
* 4;
477 datasize
= bytesperrow
* height
;
478 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
479 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
481 palettesize
= 4 * 256;
483 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
484 if (!This
->imagedata
)
491 offbits
.QuadPart
= This
->palette_offset
;
492 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
493 if (FAILED(hr
)) goto fail
;
495 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
496 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
499 offbits
.QuadPart
= This
->image_offset
;
500 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
501 if (FAILED(hr
)) goto fail
;
504 bgrdata
= (DWORD
*)This
->imagedata
;
512 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
516 else if (length
== 0)
520 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
525 case 0: /* end of line */
529 case 1: /* end of bitmap */
534 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
536 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
543 default: /* absolute mode */
545 while (length
-- && x
< width
)
548 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
551 bgrdata
[y
*width
+ x
++] = palette
[index
];
554 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
563 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
566 color
= palette
[index
];
567 while (length
-- && x
< width
)
568 bgrdata
[y
*width
+ x
++] = color
;
573 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
574 This
->stride
= -bytesperrow
;
579 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
580 This
->imagedata
= NULL
;
581 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
585 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
590 UINT datasize
, palettesize
;
595 LARGE_INTEGER offbits
;
596 ULONG cursor
=0, bytesread
=0;
598 width
= This
->bih
.bV5Width
;
599 height
= abs(This
->bih
.bV5Height
);
600 bytesperrow
= width
* 4;
601 datasize
= bytesperrow
* height
;
602 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
603 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
605 palettesize
= 4 * 16;
607 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
608 if (!This
->imagedata
)
615 offbits
.QuadPart
= This
->palette_offset
;
616 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
617 if (FAILED(hr
)) goto fail
;
619 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
620 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
623 offbits
.QuadPart
= This
->image_offset
;
624 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
625 if (FAILED(hr
)) goto fail
;
628 bgrdata
= (DWORD
*)This
->imagedata
;
636 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
640 else if (length
== 0)
644 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
649 case 0: /* end of line */
653 case 1: /* end of bitmap */
658 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
660 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
667 default: /* absolute mode */
671 while (length
-- && x
< width
)
674 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
678 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
679 if (length
-- && x
< width
)
680 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
685 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
696 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
699 color1
= palette
[colors
>>4];
700 color2
= palette
[colors
&0xf];
701 while (length
-- && x
< width
)
703 bgrdata
[y
*width
+ x
++] = color1
;
704 if (length
-- && x
< width
)
705 bgrdata
[y
*width
+ x
++] = color2
;
713 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
714 This
->stride
= -bytesperrow
;
719 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
720 This
->imagedata
= NULL
;
721 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
725 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
730 struct bitfields_format
{
731 WORD bitcount
; /* 0 for end of list */
736 const WICPixelFormatGUID
*pixelformat
;
737 ReadDataFunc read_data_func
;
740 static const struct bitfields_format bitfields_formats
[] = {
741 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
742 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
743 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
744 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
745 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
749 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
750 BmpFrameDecode_QueryInterface
,
751 BmpFrameDecode_AddRef
,
752 BmpFrameDecode_Release
,
753 BmpFrameDecode_GetSize
,
754 BmpFrameDecode_GetPixelFormat
,
755 BmpFrameDecode_GetResolution
,
756 BmpFrameDecode_CopyPalette
,
757 BmpFrameDecode_CopyPixels
,
758 BmpFrameDecode_GetMetadataQueryReader
,
759 BmpFrameDecode_GetColorContexts
,
760 BmpFrameDecode_GetThumbnail
763 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
766 ULONG bytestoread
, bytesread
;
769 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
772 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
773 if (FAILED(hr
)) return hr
;
777 BITMAPFILEHEADER bfh
;
778 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
779 if (FAILED(hr
)) return hr
;
780 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
781 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
782 This
->image_offset
= bfh
.bfOffBits
;
785 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
786 if (FAILED(hr
)) return hr
;
787 if (bytesread
!= sizeof(DWORD
) ||
788 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
789 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
790 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
791 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
792 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
794 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
795 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
796 if (FAILED(hr
)) return hr
;
797 if (bytestoread
!= bytesread
) return E_FAIL
;
800 This
->palette_offset
= This
->bih
.bV5Size
;
802 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
806 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
808 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
813 This
->bih
.bV5Height
/= 2;
817 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
818 read the extra fields */
819 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
820 This
->bih
.bV5Compression
== BI_BITFIELDS
)
822 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
823 if (FAILED(hr
)) return hr
;
824 if (bytesread
!= 12) return E_FAIL
;
825 This
->bih
.bV5AlphaMask
= 0;
826 This
->palette_offset
+= 12;
829 /* decide what kind of bitmap this is and how/if we can read it */
830 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
832 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
833 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
834 This
->bitsperpixel
= bch
->bcBitCount
;
835 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
836 switch(bch
->bcBitCount
)
839 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
842 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
845 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
848 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
851 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
854 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
855 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
859 else /* struct is compatible with BITMAPINFOHEADER */
861 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
862 switch(This
->bih
.bV5Compression
)
865 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
866 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
867 switch(This
->bih
.bV5BitCount
)
870 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
873 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
876 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
879 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
882 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
885 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
888 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
891 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
892 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
896 This
->bitsperpixel
= 32;
897 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
898 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
901 This
->bitsperpixel
= 32;
902 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
903 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
907 const struct bitfields_format
*format
;
908 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
910 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
911 This
->bitsperpixel
= 0;
912 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
913 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
914 FIXME("Huffman 1D compression is unsupported\n");
917 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
918 for (format
= bitfields_formats
; format
->bitcount
; format
++)
920 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
921 (format
->redmask
== This
->bih
.bV5RedMask
) &&
922 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
923 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
924 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
926 This
->read_data_func
= format
->read_data_func
;
927 This
->pixelformat
= format
->pixelformat
;
931 if (!format
->bitcount
)
933 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
934 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
935 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
936 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
941 This
->bitsperpixel
= 0;
942 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
943 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
944 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
951 /* In a packed DIB, the image follows the palette. */
952 ULONG palette_count
, palette_size
;
953 if (This
->bih
.bV5ClrUsed
)
954 palette_count
= This
->bih
.bV5ClrUsed
;
955 else if (This
->bih
.bV5BitCount
<= 8)
956 palette_count
= 1 << This
->bih
.bV5BitCount
;
959 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
960 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
962 palette_size
= sizeof(RGBQUAD
) * palette_count
;
963 This
->image_offset
= This
->palette_offset
+ palette_size
;
966 This
->initialized
= TRUE
;
971 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
974 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
975 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
977 if (!ppv
) return E_INVALIDARG
;
979 if (IsEqualIID(&IID_IUnknown
, iid
) ||
980 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
982 *ppv
= &This
->IWICBitmapDecoder_iface
;
987 return E_NOINTERFACE
;
990 IUnknown_AddRef((IUnknown
*)*ppv
);
994 static ULONG WINAPI
BmpDecoder_AddRef(IWICBitmapDecoder
*iface
)
996 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
997 ULONG ref
= InterlockedIncrement(&This
->ref
);
999 TRACE("(%p) refcount=%u\n", iface
, ref
);
1004 static ULONG WINAPI
BmpDecoder_Release(IWICBitmapDecoder
*iface
)
1006 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1007 ULONG ref
= InterlockedDecrement(&This
->ref
);
1009 TRACE("(%p) refcount=%u\n", iface
, ref
);
1013 if (This
->stream
) IStream_Release(This
->stream
);
1014 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
1015 This
->lock
.DebugInfo
->Spare
[0] = 0;
1016 DeleteCriticalSection(&This
->lock
);
1017 HeapFree(GetProcessHeap(), 0, This
);
1023 static HRESULT WINAPI
BmpDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
1027 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1029 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
1031 if (!stream
|| !capability
) return E_INVALIDARG
;
1033 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
1034 if (hr
!= S_OK
) return hr
;
1036 *capability
= This
->read_data_func
== BmpFrameDecode_ReadUnsupported
? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages
;
1040 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1041 WICDecodeOptions cacheOptions
)
1044 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1046 EnterCriticalSection(&This
->lock
);
1047 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1051 This
->stream
= pIStream
;
1052 IStream_AddRef(pIStream
);
1054 LeaveCriticalSection(&This
->lock
);
1059 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1060 GUID
*pguidContainerFormat
)
1062 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1066 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1067 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1069 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1071 return get_decoder_info(&CLSID_WICBmpDecoder
, ppIDecoderInfo
);
1074 static HRESULT WINAPI
BmpDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1075 IWICPalette
*pIPalette
)
1077 TRACE("(%p,%p)\n", iface
, pIPalette
);
1079 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1082 static HRESULT WINAPI
BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1083 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1085 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1086 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1089 static HRESULT WINAPI
BmpDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1090 IWICBitmapSource
**ppIBitmapSource
)
1092 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1093 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1096 static HRESULT WINAPI
BmpDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1097 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1099 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1100 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1103 static HRESULT WINAPI
BmpDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1104 IWICBitmapSource
**ppIThumbnail
)
1106 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1107 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1110 static HRESULT WINAPI
BmpDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1113 if (!pCount
) return E_INVALIDARG
;
1119 static HRESULT WINAPI
BmpDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1120 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1122 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1124 if (index
!= 0) return E_INVALIDARG
;
1126 if (!This
->stream
) return WINCODEC_ERR_FRAMEMISSING
;
1128 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
1129 IWICBitmapDecoder_AddRef(iface
);
1134 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl
= {
1135 BmpDecoder_QueryInterface
,
1138 BmpDecoder_QueryCapability
,
1139 BmpDecoder_Initialize
,
1140 BmpDecoder_GetContainerFormat
,
1141 BmpDecoder_GetDecoderInfo
,
1142 BmpDecoder_CopyPalette
,
1143 BmpDecoder_GetMetadataQueryReader
,
1144 BmpDecoder_GetPreview
,
1145 BmpDecoder_GetColorContexts
,
1146 BmpDecoder_GetThumbnail
,
1147 BmpDecoder_GetFrameCount
,
1151 static HRESULT
BmpDecoder_Create(int packed
, int icoframe
, BmpDecoder
**ppDecoder
)
1155 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder
));
1156 if (!This
) return E_OUTOFMEMORY
;
1158 This
->IWICBitmapDecoder_iface
.lpVtbl
= &BmpDecoder_Vtbl
;
1159 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &BmpDecoder_FrameVtbl
;
1161 This
->initialized
= FALSE
;
1162 This
->stream
= NULL
;
1163 This
->imagedata
= NULL
;
1164 InitializeCriticalSection(&This
->lock
);
1165 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BmpDecoder.lock");
1166 This
->packed
= packed
;
1167 This
->icoframe
= icoframe
;
1174 static HRESULT
BmpDecoder_Construct(int packed
, int icoframe
, REFIID iid
, void** ppv
)
1179 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1183 ret
= BmpDecoder_Create(packed
, icoframe
, &This
);
1184 if (FAILED(ret
)) return ret
;
1186 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1187 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1192 HRESULT
BmpDecoder_CreateInstance(REFIID iid
, void** ppv
)
1194 return BmpDecoder_Construct(FALSE
, FALSE
, iid
, ppv
);
1197 HRESULT
DibDecoder_CreateInstance(REFIID iid
, void** ppv
)
1199 return BmpDecoder_Construct(TRUE
, FALSE
, iid
, ppv
);
1202 HRESULT
IcoDibDecoder_CreateInstance(BmpDecoder
**ppDecoder
)
1204 return BmpDecoder_Create(TRUE
, TRUE
, ppDecoder
);
1207 void BmpDecoder_GetWICDecoder(BmpDecoder
*This
, IWICBitmapDecoder
**ppDecoder
)
1209 *ppDecoder
= &This
->IWICBitmapDecoder_iface
;
1212 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1213 void BmpDecoder_FindIconMask(BmpDecoder
*This
, ULONG
*mask_offset
, int *topdown
)
1215 assert(This
->stream
!= NULL
);
1217 if (This
->read_data_func
== BmpFrameDecode_ReadUncompressed
)
1219 /* RGB or BITFIELDS data */
1220 ULONG width
, height
, bytesperrow
, datasize
;
1221 IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
1222 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
1223 datasize
= bytesperrow
* height
;
1224 *mask_offset
= This
->image_offset
+ datasize
;
1229 *topdown
= This
->stride
> 0;