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 typedef HRESULT (*ReadDataFunc
)(BmpDecoder
* This
);
64 IWICBitmapDecoder IWICBitmapDecoder_iface
;
65 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
72 const WICPixelFormatGUID
*pixelformat
;
74 ReadDataFunc read_data_func
;
78 CRITICAL_SECTION lock
; /* must be held when initialized/imagedata is set or stream is accessed */
79 int packed
; /* If TRUE, don't look for a file header and assume a packed DIB. */
80 int icoframe
; /* If TRUE, this is a frame of a .ico file. */
83 static inline BmpDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
85 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapDecoder_iface
);
88 static inline BmpDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
90 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapFrameDecode_iface
);
93 static HRESULT WINAPI
BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
96 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
98 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
100 if (!ppv
) return E_INVALIDARG
;
102 if (IsEqualIID(&IID_IUnknown
, iid
) ||
103 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
104 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
106 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
111 return E_NOINTERFACE
;
114 IUnknown_AddRef((IUnknown
*)*ppv
);
118 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
120 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
122 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
125 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
127 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
129 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
132 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
133 UINT
*puiWidth
, UINT
*puiHeight
)
135 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
136 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
138 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
140 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
141 *puiWidth
= bch
->bcWidth
;
142 *puiHeight
= bch
->bcHeight
;
146 *puiWidth
= This
->bih
.bV5Width
;
147 *puiHeight
= abs(This
->bih
.bV5Height
);
152 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
153 WICPixelFormatGUID
*pPixelFormat
)
155 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
156 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
158 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
163 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
165 LONG resx
= 0, resy
= 0;
167 switch (bih
->bV5Size
)
170 case sizeof(BITMAPCOREHEADER
):
173 case sizeof(BITMAPCOREHEADER2
):
174 case sizeof(BITMAPINFOHEADER
):
175 case sizeof(BITMAPV4HEADER
):
176 case sizeof(BITMAPV5HEADER
):
177 resx
= bih
->bV5XPelsPerMeter
;
178 resy
= bih
->bV5YPelsPerMeter
;
189 *pDpiX
= resx
* 0.0254;
190 *pDpiY
= resy
* 0.0254;
196 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
197 double *pDpiX
, double *pDpiY
)
199 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
200 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
202 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
205 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
206 IWICPalette
*pIPalette
)
209 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
211 WICColor
*wiccolors
=NULL
;
212 RGBTRIPLE
*bgrcolors
=NULL
;
214 TRACE("(%p,%p)\n", iface
, pIPalette
);
216 EnterCriticalSection(&This
->lock
);
218 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
220 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
221 if (bch
->bcBitCount
<= 8)
223 /* 2**n colors in BGR format after the header */
224 ULONG tablesize
, bytesread
;
225 LARGE_INTEGER offset
;
228 count
= 1 << bch
->bcBitCount
;
229 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
230 tablesize
= sizeof(RGBTRIPLE
) * count
;
231 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
232 if (!wiccolors
|| !bgrcolors
)
238 offset
.QuadPart
= This
->palette_offset
;
239 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
240 if (FAILED(hr
)) goto end
;
242 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
243 if (FAILED(hr
)) goto end
;
244 if (bytesread
!= tablesize
) {
249 for (i
=0; i
<count
; i
++)
251 wiccolors
[i
] = 0xff000000|
252 (bgrcolors
[i
].rgbtRed
<<16)|
253 (bgrcolors
[i
].rgbtGreen
<<8)|
254 bgrcolors
[i
].rgbtBlue
;
259 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
265 if (This
->bih
.bV5BitCount
<= 8)
267 ULONG tablesize
, bytesread
;
268 LARGE_INTEGER offset
;
271 if (This
->bih
.bV5ClrUsed
== 0)
272 count
= 1 << This
->bih
.bV5BitCount
;
274 count
= This
->bih
.bV5ClrUsed
;
276 tablesize
= sizeof(WICColor
) * count
;
277 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
284 offset
.QuadPart
= This
->palette_offset
;
285 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
286 if (FAILED(hr
)) goto end
;
288 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
289 if (FAILED(hr
)) goto end
;
290 if (bytesread
!= tablesize
) {
295 /* convert from BGR to BGRA by setting alpha to 100% */
296 for (i
=0; i
<count
; i
++)
297 wiccolors
[i
] |= 0xff000000;
301 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
308 LeaveCriticalSection(&This
->lock
);
311 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
313 HeapFree(GetProcessHeap(), 0, wiccolors
);
314 HeapFree(GetProcessHeap(), 0, bgrcolors
);
318 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
319 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
321 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
324 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
326 EnterCriticalSection(&This
->lock
);
327 if (!This
->imagedata
)
329 hr
= This
->read_data_func(This
);
331 LeaveCriticalSection(&This
->lock
);
332 if (FAILED(hr
)) return hr
;
334 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
335 if (FAILED(hr
)) return hr
;
337 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
338 width
, height
, This
->stride
,
339 prc
, cbStride
, cbBufferSize
, pbBuffer
);
342 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
343 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
345 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
346 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
349 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
350 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
352 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
353 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
356 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
357 IWICBitmapSource
**ppIThumbnail
)
359 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
360 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
363 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
370 LARGE_INTEGER offbits
;
373 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
375 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
376 width
= bch
->bcWidth
;
377 height
= bch
->bcHeight
;
382 width
= This
->bih
.bV5Width
;
383 height
= abs(This
->bih
.bV5Height
);
384 bottomup
= (This
->bih
.bV5Height
> 0);
387 /* row sizes in BMP files must be divisible by 4 bytes */
388 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
389 datasize
= bytesperrow
* height
;
391 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
392 if (!This
->imagedata
) return E_OUTOFMEMORY
;
394 offbits
.QuadPart
= This
->image_offset
;
395 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
396 if (FAILED(hr
)) goto fail
;
398 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
399 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
403 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
404 This
->stride
= -bytesperrow
;
408 This
->imagedatastart
= This
->imagedata
;
409 This
->stride
= bytesperrow
;
414 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
415 This
->imagedata
= NULL
;
416 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
420 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
425 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
429 hr
= BmpFrameDecode_ReadUncompressed(This
);
434 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
435 width
, height
, This
->stride
);
441 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
442 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
446 if (*bytesread
== 0 || *cursor
== *bytesread
)
448 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
454 if (*cursor
< *bytesread
)
455 *result
= buffer
[(*cursor
)++];
463 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
468 UINT datasize
, palettesize
;
473 LARGE_INTEGER offbits
;
474 ULONG cursor
=0, bytesread
=0;
476 width
= This
->bih
.bV5Width
;
477 height
= abs(This
->bih
.bV5Height
);
478 bytesperrow
= width
* 4;
479 datasize
= bytesperrow
* height
;
480 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
481 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
483 palettesize
= 4 * 256;
485 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
486 if (!This
->imagedata
)
493 offbits
.QuadPart
= This
->palette_offset
;
494 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
495 if (FAILED(hr
)) goto fail
;
497 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
498 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
501 offbits
.QuadPart
= This
->image_offset
;
502 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
503 if (FAILED(hr
)) goto fail
;
506 bgrdata
= (DWORD
*)This
->imagedata
;
514 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
518 else if (length
== 0)
522 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
527 case 0: /* end of line */
531 case 1: /* end of bitmap */
536 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
538 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
545 default: /* absolute mode */
547 while (length
-- && x
< width
)
550 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
553 bgrdata
[y
*width
+ x
++] = palette
[index
];
556 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
565 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
568 color
= palette
[index
];
569 while (length
-- && x
< width
)
570 bgrdata
[y
*width
+ x
++] = color
;
575 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
576 This
->stride
= -bytesperrow
;
581 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
582 This
->imagedata
= NULL
;
583 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
587 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
592 UINT datasize
, palettesize
;
597 LARGE_INTEGER offbits
;
598 ULONG cursor
=0, bytesread
=0;
600 width
= This
->bih
.bV5Width
;
601 height
= abs(This
->bih
.bV5Height
);
602 bytesperrow
= width
* 4;
603 datasize
= bytesperrow
* height
;
604 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
605 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
607 palettesize
= 4 * 16;
609 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
610 if (!This
->imagedata
)
617 offbits
.QuadPart
= This
->palette_offset
;
618 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
619 if (FAILED(hr
)) goto fail
;
621 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
622 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
625 offbits
.QuadPart
= This
->image_offset
;
626 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
627 if (FAILED(hr
)) goto fail
;
630 bgrdata
= (DWORD
*)This
->imagedata
;
638 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
642 else if (length
== 0)
646 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
651 case 0: /* end of line */
655 case 1: /* end of bitmap */
660 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
662 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
669 default: /* absolute mode */
673 while (length
-- && x
< width
)
676 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
680 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
681 if (length
-- && x
< width
)
682 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
687 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
698 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
701 color1
= palette
[colors
>>4];
702 color2
= palette
[colors
&0xf];
703 while (length
-- && x
< width
)
705 bgrdata
[y
*width
+ x
++] = color1
;
706 if (length
-- && x
< width
)
707 bgrdata
[y
*width
+ x
++] = color2
;
715 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
716 This
->stride
= -bytesperrow
;
721 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
722 This
->imagedata
= NULL
;
723 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
727 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
732 struct bitfields_format
{
733 WORD bitcount
; /* 0 for end of list */
738 const WICPixelFormatGUID
*pixelformat
;
739 ReadDataFunc read_data_func
;
742 static const struct bitfields_format bitfields_formats
[] = {
743 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
744 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
745 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
746 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
747 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
751 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
752 BmpFrameDecode_QueryInterface
,
753 BmpFrameDecode_AddRef
,
754 BmpFrameDecode_Release
,
755 BmpFrameDecode_GetSize
,
756 BmpFrameDecode_GetPixelFormat
,
757 BmpFrameDecode_GetResolution
,
758 BmpFrameDecode_CopyPalette
,
759 BmpFrameDecode_CopyPixels
,
760 BmpFrameDecode_GetMetadataQueryReader
,
761 BmpFrameDecode_GetColorContexts
,
762 BmpFrameDecode_GetThumbnail
765 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
768 ULONG bytestoread
, bytesread
;
771 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
774 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
775 if (FAILED(hr
)) return hr
;
779 BITMAPFILEHEADER bfh
;
780 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
781 if (FAILED(hr
)) return hr
;
782 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
783 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
784 This
->image_offset
= bfh
.bfOffBits
;
787 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
788 if (FAILED(hr
)) return hr
;
789 if (bytesread
!= sizeof(DWORD
) ||
790 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
791 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
792 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
793 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
794 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
796 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
797 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
798 if (FAILED(hr
)) return hr
;
799 if (bytestoread
!= bytesread
) return E_FAIL
;
802 This
->palette_offset
= This
->bih
.bV5Size
;
804 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
808 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
810 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
815 This
->bih
.bV5Height
/= 2;
819 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
820 read the extra fields */
821 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
822 This
->bih
.bV5Compression
== BI_BITFIELDS
)
824 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
825 if (FAILED(hr
)) return hr
;
826 if (bytesread
!= 12) return E_FAIL
;
827 This
->bih
.bV5AlphaMask
= 0;
828 This
->palette_offset
+= 12;
831 /* decide what kind of bitmap this is and how/if we can read it */
832 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
834 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
835 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
836 This
->bitsperpixel
= bch
->bcBitCount
;
837 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
838 switch(bch
->bcBitCount
)
841 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
844 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
847 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
850 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
853 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
856 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
857 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
861 else /* struct is compatible with BITMAPINFOHEADER */
863 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
864 switch(This
->bih
.bV5Compression
)
867 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
868 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
869 switch(This
->bih
.bV5BitCount
)
872 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
875 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
878 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
881 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
884 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
887 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
890 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
893 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
894 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
898 This
->bitsperpixel
= 32;
899 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
900 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
903 This
->bitsperpixel
= 32;
904 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
905 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
909 const struct bitfields_format
*format
;
910 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
912 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
913 This
->bitsperpixel
= 0;
914 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
915 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
916 FIXME("Huffman 1D compression is unsupported\n");
919 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
920 for (format
= bitfields_formats
; format
->bitcount
; format
++)
922 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
923 (format
->redmask
== This
->bih
.bV5RedMask
) &&
924 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
925 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
926 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
928 This
->read_data_func
= format
->read_data_func
;
929 This
->pixelformat
= format
->pixelformat
;
933 if (!format
->bitcount
)
935 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
936 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
937 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
938 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
943 This
->bitsperpixel
= 0;
944 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
945 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
946 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
953 /* In a packed DIB, the image follows the palette. */
954 ULONG palette_count
, palette_size
;
955 if (This
->bih
.bV5ClrUsed
)
956 palette_count
= This
->bih
.bV5ClrUsed
;
957 else if (This
->bih
.bV5BitCount
<= 8)
958 palette_count
= 1 << This
->bih
.bV5BitCount
;
961 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
962 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
964 palette_size
= sizeof(RGBQUAD
) * palette_count
;
965 This
->image_offset
= This
->palette_offset
+ palette_size
;
968 This
->initialized
= TRUE
;
973 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
976 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
977 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
979 if (!ppv
) return E_INVALIDARG
;
981 if (IsEqualIID(&IID_IUnknown
, iid
) ||
982 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
984 *ppv
= &This
->IWICBitmapDecoder_iface
;
989 return E_NOINTERFACE
;
992 IUnknown_AddRef((IUnknown
*)*ppv
);
996 static ULONG WINAPI
BmpDecoder_AddRef(IWICBitmapDecoder
*iface
)
998 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
999 ULONG ref
= InterlockedIncrement(&This
->ref
);
1001 TRACE("(%p) refcount=%u\n", iface
, ref
);
1006 static ULONG WINAPI
BmpDecoder_Release(IWICBitmapDecoder
*iface
)
1008 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1009 ULONG ref
= InterlockedDecrement(&This
->ref
);
1011 TRACE("(%p) refcount=%u\n", iface
, ref
);
1015 if (This
->stream
) IStream_Release(This
->stream
);
1016 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
1017 This
->lock
.DebugInfo
->Spare
[0] = 0;
1018 DeleteCriticalSection(&This
->lock
);
1019 HeapFree(GetProcessHeap(), 0, This
);
1025 static HRESULT WINAPI
BmpDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
1029 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1031 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
1033 if (!stream
|| !capability
) return E_INVALIDARG
;
1035 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
1036 if (hr
!= S_OK
) return hr
;
1038 *capability
= This
->read_data_func
== BmpFrameDecode_ReadUnsupported
? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages
;
1042 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1043 WICDecodeOptions cacheOptions
)
1046 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1048 EnterCriticalSection(&This
->lock
);
1049 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1053 This
->stream
= pIStream
;
1054 IStream_AddRef(pIStream
);
1056 LeaveCriticalSection(&This
->lock
);
1061 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1062 GUID
*pguidContainerFormat
)
1064 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1068 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1069 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1072 IWICComponentInfo
*compinfo
;
1074 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1076 hr
= CreateComponentInfo(&CLSID_WICBmpDecoder
, &compinfo
);
1077 if (FAILED(hr
)) return hr
;
1079 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1080 (void**)ppIDecoderInfo
);
1082 IWICComponentInfo_Release(compinfo
);
1087 static HRESULT WINAPI
BmpDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1088 IWICPalette
*pIPalette
)
1090 TRACE("(%p,%p)\n", iface
, pIPalette
);
1092 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1095 static HRESULT WINAPI
BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1096 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1098 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1099 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1102 static HRESULT WINAPI
BmpDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1103 IWICBitmapSource
**ppIBitmapSource
)
1105 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1106 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1109 static HRESULT WINAPI
BmpDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1110 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1112 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1113 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1116 static HRESULT WINAPI
BmpDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1117 IWICBitmapSource
**ppIThumbnail
)
1119 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1120 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1123 static HRESULT WINAPI
BmpDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1126 if (!pCount
) return E_INVALIDARG
;
1132 static HRESULT WINAPI
BmpDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1133 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1135 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1137 if (index
!= 0) return E_INVALIDARG
;
1139 if (!This
->stream
) return WINCODEC_ERR_FRAMEMISSING
;
1141 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
1142 IWICBitmapDecoder_AddRef(iface
);
1147 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl
= {
1148 BmpDecoder_QueryInterface
,
1151 BmpDecoder_QueryCapability
,
1152 BmpDecoder_Initialize
,
1153 BmpDecoder_GetContainerFormat
,
1154 BmpDecoder_GetDecoderInfo
,
1155 BmpDecoder_CopyPalette
,
1156 BmpDecoder_GetMetadataQueryReader
,
1157 BmpDecoder_GetPreview
,
1158 BmpDecoder_GetColorContexts
,
1159 BmpDecoder_GetThumbnail
,
1160 BmpDecoder_GetFrameCount
,
1164 static HRESULT
BmpDecoder_Create(int packed
, int icoframe
, BmpDecoder
**ppDecoder
)
1168 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder
));
1169 if (!This
) return E_OUTOFMEMORY
;
1171 This
->IWICBitmapDecoder_iface
.lpVtbl
= &BmpDecoder_Vtbl
;
1172 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &BmpDecoder_FrameVtbl
;
1174 This
->initialized
= FALSE
;
1175 This
->stream
= NULL
;
1176 This
->imagedata
= NULL
;
1177 InitializeCriticalSection(&This
->lock
);
1178 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BmpDecoder.lock");
1179 This
->packed
= packed
;
1180 This
->icoframe
= icoframe
;
1187 static HRESULT
BmpDecoder_Construct(int packed
, int icoframe
, REFIID iid
, void** ppv
)
1192 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1196 ret
= BmpDecoder_Create(packed
, icoframe
, &This
);
1197 if (FAILED(ret
)) return ret
;
1199 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1200 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1205 HRESULT
BmpDecoder_CreateInstance(REFIID iid
, void** ppv
)
1207 return BmpDecoder_Construct(FALSE
, FALSE
, iid
, ppv
);
1210 HRESULT
DibDecoder_CreateInstance(REFIID iid
, void** ppv
)
1212 return BmpDecoder_Construct(TRUE
, FALSE
, iid
, ppv
);
1215 HRESULT
IcoDibDecoder_CreateInstance(BmpDecoder
**ppDecoder
)
1217 return BmpDecoder_Create(TRUE
, TRUE
, ppDecoder
);
1220 void BmpDecoder_GetWICDecoder(BmpDecoder
*This
, IWICBitmapDecoder
**ppDecoder
)
1222 *ppDecoder
= &This
->IWICBitmapDecoder_iface
;
1225 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1226 void BmpDecoder_FindIconMask(BmpDecoder
*This
, ULONG
*mask_offset
, int *topdown
)
1228 assert(This
->stream
!= NULL
);
1230 if (This
->read_data_func
== BmpFrameDecode_ReadUncompressed
)
1232 /* RGB or BITFIELDS data */
1233 ULONG width
, height
, bytesperrow
, datasize
;
1234 IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
1235 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
1236 datasize
= bytesperrow
* height
;
1237 *mask_offset
= This
->image_offset
+ datasize
;
1242 *topdown
= This
->stride
> 0;