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
33 #include "wincodecs_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
50 DWORD bc2ClrImportant
;
51 /* same as BITMAPINFOHEADER until this point */
56 DWORD bc2HalftoneSize1
;
57 DWORD bc2HalftoneSize2
;
62 typedef HRESULT (*ReadDataFunc
)(BmpDecoder
* This
);
65 IWICBitmapDecoder IWICBitmapDecoder_iface
;
66 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
73 const WICPixelFormatGUID
*pixelformat
;
75 ReadDataFunc read_data_func
;
79 CRITICAL_SECTION lock
; /* must be held when initialized/imagedata is set or stream is accessed */
80 int packed
; /* If TRUE, don't look for a file header and assume a packed DIB. */
81 int icoframe
; /* If TRUE, this is a frame of a .ico file. */
84 static inline BmpDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
86 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapDecoder_iface
);
89 static inline BmpDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
91 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapFrameDecode_iface
);
94 static HRESULT WINAPI
BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
97 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
99 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
101 if (!ppv
) return E_INVALIDARG
;
103 if (IsEqualIID(&IID_IUnknown
, iid
) ||
104 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
105 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
107 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
112 return E_NOINTERFACE
;
115 IUnknown_AddRef((IUnknown
*)*ppv
);
119 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
121 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
123 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
126 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
128 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
130 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
133 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
134 UINT
*puiWidth
, UINT
*puiHeight
)
136 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
137 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
139 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
141 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
142 *puiWidth
= bch
->bcWidth
;
143 *puiHeight
= bch
->bcHeight
;
147 *puiWidth
= This
->bih
.bV5Width
;
148 *puiHeight
= abs(This
->bih
.bV5Height
);
153 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
154 WICPixelFormatGUID
*pPixelFormat
)
156 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
157 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
159 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
164 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
166 LONG resx
= 0, resy
= 0;
168 switch (bih
->bV5Size
)
171 case sizeof(BITMAPCOREHEADER
):
174 case sizeof(BITMAPCOREHEADER2
):
175 case sizeof(BITMAPINFOHEADER
):
176 case sizeof(BITMAPV4HEADER
):
177 case sizeof(BITMAPV5HEADER
):
178 resx
= bih
->bV5XPelsPerMeter
;
179 resy
= bih
->bV5YPelsPerMeter
;
190 *pDpiX
= resx
* 0.0254;
191 *pDpiY
= resy
* 0.0254;
197 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
198 double *pDpiX
, double *pDpiY
)
200 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
201 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
203 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
206 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
207 IWICPalette
*pIPalette
)
210 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
212 WICColor
*wiccolors
=NULL
;
213 RGBTRIPLE
*bgrcolors
=NULL
;
215 TRACE("(%p,%p)\n", iface
, pIPalette
);
217 EnterCriticalSection(&This
->lock
);
219 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
221 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
222 if (bch
->bcBitCount
<= 8)
224 /* 2**n colors in BGR format after the header */
225 ULONG tablesize
, bytesread
;
226 LARGE_INTEGER offset
;
229 count
= 1 << bch
->bcBitCount
;
230 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
231 tablesize
= sizeof(RGBTRIPLE
) * count
;
232 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
233 if (!wiccolors
|| !bgrcolors
)
239 offset
.QuadPart
= This
->palette_offset
;
240 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
241 if (FAILED(hr
)) goto end
;
243 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
244 if (FAILED(hr
)) goto end
;
245 if (bytesread
!= tablesize
) {
250 for (i
=0; i
<count
; i
++)
252 wiccolors
[i
] = 0xff000000|
253 (bgrcolors
[i
].rgbtRed
<<16)|
254 (bgrcolors
[i
].rgbtGreen
<<8)|
255 bgrcolors
[i
].rgbtBlue
;
260 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
266 if (This
->bih
.bV5BitCount
<= 8)
268 ULONG tablesize
, bytesread
;
269 LARGE_INTEGER offset
;
272 if (This
->bih
.bV5ClrUsed
== 0)
273 count
= 1 << This
->bih
.bV5BitCount
;
275 count
= This
->bih
.bV5ClrUsed
;
277 tablesize
= sizeof(WICColor
) * count
;
278 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
285 offset
.QuadPart
= This
->palette_offset
;
286 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
287 if (FAILED(hr
)) goto end
;
289 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
290 if (FAILED(hr
)) goto end
;
291 if (bytesread
!= tablesize
) {
296 /* convert from BGR to BGRA by setting alpha to 100% */
297 for (i
=0; i
<count
; i
++)
298 wiccolors
[i
] |= 0xff000000;
302 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
309 LeaveCriticalSection(&This
->lock
);
312 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
314 HeapFree(GetProcessHeap(), 0, wiccolors
);
315 HeapFree(GetProcessHeap(), 0, bgrcolors
);
319 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
320 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
322 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
325 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
327 EnterCriticalSection(&This
->lock
);
328 if (!This
->imagedata
)
330 hr
= This
->read_data_func(This
);
332 LeaveCriticalSection(&This
->lock
);
333 if (FAILED(hr
)) return hr
;
335 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
336 if (FAILED(hr
)) return hr
;
338 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
339 width
, height
, This
->stride
,
340 prc
, cbStride
, cbBufferSize
, pbBuffer
);
343 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
344 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
346 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
347 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
350 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
351 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
353 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
354 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
357 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
358 IWICBitmapSource
**ppIThumbnail
)
360 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
361 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
364 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
371 LARGE_INTEGER offbits
;
374 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
376 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
377 width
= bch
->bcWidth
;
378 height
= bch
->bcHeight
;
383 width
= This
->bih
.bV5Width
;
384 height
= abs(This
->bih
.bV5Height
);
385 bottomup
= (This
->bih
.bV5Height
> 0);
388 /* row sizes in BMP files must be divisible by 4 bytes */
389 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
390 datasize
= bytesperrow
* height
;
392 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
393 if (!This
->imagedata
) return E_OUTOFMEMORY
;
395 offbits
.QuadPart
= This
->image_offset
;
396 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
397 if (FAILED(hr
)) goto fail
;
399 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
400 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
404 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
405 This
->stride
= -bytesperrow
;
409 This
->imagedatastart
= This
->imagedata
;
410 This
->stride
= bytesperrow
;
415 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
416 This
->imagedata
= NULL
;
417 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
421 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
426 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
430 hr
= BmpFrameDecode_ReadUncompressed(This
);
435 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
436 width
, height
, This
->stride
);
442 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
443 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
447 if (*bytesread
== 0 || *cursor
== *bytesread
)
449 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
455 if (*cursor
< *bytesread
)
456 *result
= buffer
[(*cursor
)++];
464 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
469 UINT datasize
, palettesize
;
474 LARGE_INTEGER offbits
;
475 ULONG cursor
=0, bytesread
=0;
477 width
= This
->bih
.bV5Width
;
478 height
= abs(This
->bih
.bV5Height
);
479 bytesperrow
= width
* 4;
480 datasize
= bytesperrow
* height
;
481 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
482 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
484 palettesize
= 4 * 256;
486 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
487 if (!This
->imagedata
)
494 offbits
.QuadPart
= This
->palette_offset
;
495 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
496 if (FAILED(hr
)) goto fail
;
498 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
499 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
502 offbits
.QuadPart
= This
->image_offset
;
503 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
504 if (FAILED(hr
)) goto fail
;
507 bgrdata
= (DWORD
*)This
->imagedata
;
515 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
519 else if (length
== 0)
523 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
528 case 0: /* end of line */
532 case 1: /* end of bitmap */
537 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
539 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
546 default: /* absolute mode */
548 while (length
-- && x
< width
)
551 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
554 bgrdata
[y
*width
+ x
++] = palette
[index
];
557 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
566 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
569 color
= palette
[index
];
570 while (length
-- && x
< width
)
571 bgrdata
[y
*width
+ x
++] = color
;
576 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
577 This
->stride
= -bytesperrow
;
582 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
583 This
->imagedata
= NULL
;
584 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
588 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
593 UINT datasize
, palettesize
;
598 LARGE_INTEGER offbits
;
599 ULONG cursor
=0, bytesread
=0;
601 width
= This
->bih
.bV5Width
;
602 height
= abs(This
->bih
.bV5Height
);
603 bytesperrow
= width
* 4;
604 datasize
= bytesperrow
* height
;
605 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
606 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
608 palettesize
= 4 * 16;
610 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
611 if (!This
->imagedata
)
618 offbits
.QuadPart
= This
->palette_offset
;
619 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
620 if (FAILED(hr
)) goto fail
;
622 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
623 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
626 offbits
.QuadPart
= This
->image_offset
;
627 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
628 if (FAILED(hr
)) goto fail
;
631 bgrdata
= (DWORD
*)This
->imagedata
;
639 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
643 else if (length
== 0)
647 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
652 case 0: /* end of line */
656 case 1: /* end of bitmap */
661 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
663 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
670 default: /* absolute mode */
674 while (length
-- && x
< width
)
677 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
681 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
682 if (length
-- && x
< width
)
683 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
688 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
699 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
702 color1
= palette
[colors
>>4];
703 color2
= palette
[colors
&0xf];
704 while (length
-- && x
< width
)
706 bgrdata
[y
*width
+ x
++] = color1
;
707 if (length
-- && x
< width
)
708 bgrdata
[y
*width
+ x
++] = color2
;
716 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
717 This
->stride
= -bytesperrow
;
722 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
723 This
->imagedata
= NULL
;
724 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
728 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
733 struct bitfields_format
{
734 WORD bitcount
; /* 0 for end of list */
739 const WICPixelFormatGUID
*pixelformat
;
740 ReadDataFunc read_data_func
;
743 static const struct bitfields_format bitfields_formats
[] = {
744 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
745 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
746 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
747 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
748 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
752 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
753 BmpFrameDecode_QueryInterface
,
754 BmpFrameDecode_AddRef
,
755 BmpFrameDecode_Release
,
756 BmpFrameDecode_GetSize
,
757 BmpFrameDecode_GetPixelFormat
,
758 BmpFrameDecode_GetResolution
,
759 BmpFrameDecode_CopyPalette
,
760 BmpFrameDecode_CopyPixels
,
761 BmpFrameDecode_GetMetadataQueryReader
,
762 BmpFrameDecode_GetColorContexts
,
763 BmpFrameDecode_GetThumbnail
766 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
769 ULONG bytestoread
, bytesread
;
772 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
775 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
776 if (FAILED(hr
)) return hr
;
780 BITMAPFILEHEADER bfh
;
781 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
782 if (FAILED(hr
)) return hr
;
783 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
784 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
785 This
->image_offset
= bfh
.bfOffBits
;
788 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
789 if (FAILED(hr
)) return hr
;
790 if (bytesread
!= sizeof(DWORD
) ||
791 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
792 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
793 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
794 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
795 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
797 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
798 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
799 if (FAILED(hr
)) return hr
;
800 if (bytestoread
!= bytesread
) return E_FAIL
;
803 This
->palette_offset
= This
->bih
.bV5Size
;
805 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
809 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
811 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
816 This
->bih
.bV5Height
/= 2;
820 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
821 read the extra fields */
822 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
823 This
->bih
.bV5Compression
== BI_BITFIELDS
)
825 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
826 if (FAILED(hr
)) return hr
;
827 if (bytesread
!= 12) return E_FAIL
;
828 This
->bih
.bV5AlphaMask
= 0;
829 This
->palette_offset
+= 12;
832 /* decide what kind of bitmap this is and how/if we can read it */
833 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
835 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
836 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
837 This
->bitsperpixel
= bch
->bcBitCount
;
838 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
839 switch(bch
->bcBitCount
)
842 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
845 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
848 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
851 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
854 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
857 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
858 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
862 else /* struct is compatible with BITMAPINFOHEADER */
864 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
865 switch(This
->bih
.bV5Compression
)
868 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
869 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
870 switch(This
->bih
.bV5BitCount
)
873 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
876 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
879 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
882 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
885 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
888 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
891 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
894 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
895 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
899 This
->bitsperpixel
= 32;
900 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
901 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
904 This
->bitsperpixel
= 32;
905 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
906 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
910 const struct bitfields_format
*format
;
911 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
913 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
914 This
->bitsperpixel
= 0;
915 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
916 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
917 FIXME("Huffman 1D compression is unsupported\n");
920 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
921 for (format
= bitfields_formats
; format
->bitcount
; format
++)
923 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
924 (format
->redmask
== This
->bih
.bV5RedMask
) &&
925 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
926 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
927 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
929 This
->read_data_func
= format
->read_data_func
;
930 This
->pixelformat
= format
->pixelformat
;
934 if (!format
->bitcount
)
936 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
937 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
938 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
939 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
944 This
->bitsperpixel
= 0;
945 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
946 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
947 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
954 /* In a packed DIB, the image follows the palette. */
955 ULONG palette_count
, palette_size
;
956 if (This
->bih
.bV5ClrUsed
)
957 palette_count
= This
->bih
.bV5ClrUsed
;
958 else if (This
->bih
.bV5BitCount
<= 8)
959 palette_count
= 1 << This
->bih
.bV5BitCount
;
962 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
963 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
965 palette_size
= sizeof(RGBQUAD
) * palette_count
;
966 This
->image_offset
= This
->palette_offset
+ palette_size
;
969 This
->initialized
= TRUE
;
974 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
977 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
978 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
980 if (!ppv
) return E_INVALIDARG
;
982 if (IsEqualIID(&IID_IUnknown
, iid
) ||
983 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
985 *ppv
= &This
->IWICBitmapDecoder_iface
;
990 return E_NOINTERFACE
;
993 IUnknown_AddRef((IUnknown
*)*ppv
);
997 static ULONG WINAPI
BmpDecoder_AddRef(IWICBitmapDecoder
*iface
)
999 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1000 ULONG ref
= InterlockedIncrement(&This
->ref
);
1002 TRACE("(%p) refcount=%u\n", iface
, ref
);
1007 static ULONG WINAPI
BmpDecoder_Release(IWICBitmapDecoder
*iface
)
1009 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1010 ULONG ref
= InterlockedDecrement(&This
->ref
);
1012 TRACE("(%p) refcount=%u\n", iface
, ref
);
1016 if (This
->stream
) IStream_Release(This
->stream
);
1017 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
1018 This
->lock
.DebugInfo
->Spare
[0] = 0;
1019 DeleteCriticalSection(&This
->lock
);
1020 HeapFree(GetProcessHeap(), 0, This
);
1026 static HRESULT WINAPI
BmpDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
1030 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1032 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
1034 if (!stream
|| !capability
) return E_INVALIDARG
;
1036 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
1037 if (hr
!= S_OK
) return hr
;
1039 *capability
= This
->read_data_func
== BmpFrameDecode_ReadUnsupported
? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages
;
1043 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1044 WICDecodeOptions cacheOptions
)
1047 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1049 EnterCriticalSection(&This
->lock
);
1050 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1054 This
->stream
= pIStream
;
1055 IStream_AddRef(pIStream
);
1057 LeaveCriticalSection(&This
->lock
);
1062 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1063 GUID
*pguidContainerFormat
)
1065 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1069 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1070 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1073 IWICComponentInfo
*compinfo
;
1075 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1077 hr
= CreateComponentInfo(&CLSID_WICBmpDecoder
, &compinfo
);
1078 if (FAILED(hr
)) return hr
;
1080 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1081 (void**)ppIDecoderInfo
);
1083 IWICComponentInfo_Release(compinfo
);
1088 static HRESULT WINAPI
BmpDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1089 IWICPalette
*pIPalette
)
1091 TRACE("(%p,%p)\n", iface
, pIPalette
);
1093 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1096 static HRESULT WINAPI
BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1097 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1099 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1100 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1103 static HRESULT WINAPI
BmpDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1104 IWICBitmapSource
**ppIBitmapSource
)
1106 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1107 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1110 static HRESULT WINAPI
BmpDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1111 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1113 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1114 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1117 static HRESULT WINAPI
BmpDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1118 IWICBitmapSource
**ppIThumbnail
)
1120 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1121 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1124 static HRESULT WINAPI
BmpDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1127 if (!pCount
) return E_INVALIDARG
;
1133 static HRESULT WINAPI
BmpDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1134 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1136 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1138 if (index
!= 0) return E_INVALIDARG
;
1140 if (!This
->stream
) return WINCODEC_ERR_FRAMEMISSING
;
1142 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
1143 IWICBitmapDecoder_AddRef(iface
);
1148 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl
= {
1149 BmpDecoder_QueryInterface
,
1152 BmpDecoder_QueryCapability
,
1153 BmpDecoder_Initialize
,
1154 BmpDecoder_GetContainerFormat
,
1155 BmpDecoder_GetDecoderInfo
,
1156 BmpDecoder_CopyPalette
,
1157 BmpDecoder_GetMetadataQueryReader
,
1158 BmpDecoder_GetPreview
,
1159 BmpDecoder_GetColorContexts
,
1160 BmpDecoder_GetThumbnail
,
1161 BmpDecoder_GetFrameCount
,
1165 static HRESULT
BmpDecoder_Create(int packed
, int icoframe
, BmpDecoder
**ppDecoder
)
1169 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder
));
1170 if (!This
) return E_OUTOFMEMORY
;
1172 This
->IWICBitmapDecoder_iface
.lpVtbl
= &BmpDecoder_Vtbl
;
1173 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &BmpDecoder_FrameVtbl
;
1175 This
->initialized
= FALSE
;
1176 This
->stream
= NULL
;
1177 This
->imagedata
= NULL
;
1178 InitializeCriticalSection(&This
->lock
);
1179 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BmpDecoder.lock");
1180 This
->packed
= packed
;
1181 This
->icoframe
= icoframe
;
1188 static HRESULT
BmpDecoder_Construct(int packed
, int icoframe
, REFIID iid
, void** ppv
)
1193 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1197 ret
= BmpDecoder_Create(packed
, icoframe
, &This
);
1198 if (FAILED(ret
)) return ret
;
1200 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1201 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1206 HRESULT
BmpDecoder_CreateInstance(REFIID iid
, void** ppv
)
1208 return BmpDecoder_Construct(FALSE
, FALSE
, iid
, ppv
);
1211 HRESULT
DibDecoder_CreateInstance(REFIID iid
, void** ppv
)
1213 return BmpDecoder_Construct(TRUE
, FALSE
, iid
, ppv
);
1216 HRESULT
IcoDibDecoder_CreateInstance(BmpDecoder
**ppDecoder
)
1218 return BmpDecoder_Create(TRUE
, TRUE
, ppDecoder
);
1221 void BmpDecoder_GetWICDecoder(BmpDecoder
*This
, IWICBitmapDecoder
**ppDecoder
)
1223 *ppDecoder
= &This
->IWICBitmapDecoder_iface
;
1226 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1227 void BmpDecoder_FindIconMask(BmpDecoder
*This
, ULONG
*mask_offset
, int *topdown
)
1229 assert(This
->stream
!= NULL
);
1231 if (This
->read_data_func
== BmpFrameDecode_ReadUncompressed
)
1233 /* RGB or BITFIELDS data */
1234 ULONG width
, height
, bytesperrow
, datasize
;
1235 IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
1236 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
1237 datasize
= bytesperrow
* height
;
1238 *mask_offset
= This
->image_offset
+ datasize
;
1243 *topdown
= This
->stride
> 0;