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 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
99 if (!ppv
) return E_INVALIDARG
;
101 if (IsEqualIID(&IID_IUnknown
, iid
) ||
102 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
103 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
110 return E_NOINTERFACE
;
113 IUnknown_AddRef((IUnknown
*)*ppv
);
117 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
119 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
121 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
124 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
126 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
128 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
131 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
132 UINT
*puiWidth
, UINT
*puiHeight
)
134 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
135 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
137 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
139 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
140 *puiWidth
= bch
->bcWidth
;
141 *puiHeight
= bch
->bcHeight
;
145 *puiWidth
= This
->bih
.bV5Width
;
146 *puiHeight
= abs(This
->bih
.bV5Height
);
151 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
152 WICPixelFormatGUID
*pPixelFormat
)
154 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
155 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
157 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
162 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
164 switch (bih
->bV5Size
)
166 case sizeof(BITMAPCOREHEADER
):
170 case sizeof(BITMAPCOREHEADER2
):
171 case sizeof(BITMAPINFOHEADER
):
172 case sizeof(BITMAPV4HEADER
):
173 case sizeof(BITMAPV5HEADER
):
174 *pDpiX
= bih
->bV5XPelsPerMeter
* 0.0254;
175 *pDpiY
= bih
->bV5YPelsPerMeter
* 0.0254;
182 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
183 double *pDpiX
, double *pDpiY
)
185 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
186 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
188 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
191 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
192 IWICPalette
*pIPalette
)
195 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
197 WICColor
*wiccolors
=NULL
;
198 RGBTRIPLE
*bgrcolors
=NULL
;
200 TRACE("(%p,%p)\n", iface
, pIPalette
);
202 EnterCriticalSection(&This
->lock
);
204 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
206 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
207 if (bch
->bcBitCount
<= 8)
209 /* 2**n colors in BGR format after the header */
210 ULONG tablesize
, bytesread
;
211 LARGE_INTEGER offset
;
214 count
= 1 << bch
->bcBitCount
;
215 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
216 tablesize
= sizeof(RGBTRIPLE
) * count
;
217 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
218 if (!wiccolors
|| !bgrcolors
)
224 offset
.QuadPart
= This
->palette_offset
;
225 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
226 if (FAILED(hr
)) goto end
;
228 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
229 if (FAILED(hr
)) goto end
;
230 if (bytesread
!= tablesize
) {
235 for (i
=0; i
<count
; i
++)
237 wiccolors
[i
] = 0xff000000|
238 (bgrcolors
[i
].rgbtRed
<<16)|
239 (bgrcolors
[i
].rgbtGreen
<<8)|
240 bgrcolors
[i
].rgbtBlue
;
245 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
251 if (This
->bih
.bV5BitCount
<= 8)
253 ULONG tablesize
, bytesread
;
254 LARGE_INTEGER offset
;
257 if (This
->bih
.bV5ClrUsed
== 0)
258 count
= 1 << This
->bih
.bV5BitCount
;
260 count
= This
->bih
.bV5ClrUsed
;
262 tablesize
= sizeof(WICColor
) * count
;
263 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
270 offset
.QuadPart
= This
->palette_offset
;
271 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
272 if (FAILED(hr
)) goto end
;
274 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
275 if (FAILED(hr
)) goto end
;
276 if (bytesread
!= tablesize
) {
281 /* convert from BGR to BGRA by setting alpha to 100% */
282 for (i
=0; i
<count
; i
++)
283 wiccolors
[i
] |= 0xff000000;
287 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
294 LeaveCriticalSection(&This
->lock
);
297 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
299 HeapFree(GetProcessHeap(), 0, wiccolors
);
300 HeapFree(GetProcessHeap(), 0, bgrcolors
);
304 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
305 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
307 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
310 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
312 EnterCriticalSection(&This
->lock
);
313 if (!This
->imagedata
)
315 hr
= This
->read_data_func(This
);
317 LeaveCriticalSection(&This
->lock
);
318 if (FAILED(hr
)) return hr
;
320 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
321 if (FAILED(hr
)) return hr
;
323 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
324 width
, height
, This
->stride
,
325 prc
, cbStride
, cbBufferSize
, pbBuffer
);
328 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
329 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
331 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
332 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
335 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
336 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
338 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
339 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
342 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
343 IWICBitmapSource
**ppIThumbnail
)
345 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
346 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
349 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
356 LARGE_INTEGER offbits
;
359 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
361 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
362 width
= bch
->bcWidth
;
363 height
= bch
->bcHeight
;
368 width
= This
->bih
.bV5Width
;
369 height
= abs(This
->bih
.bV5Height
);
370 bottomup
= (This
->bih
.bV5Height
> 0);
373 /* row sizes in BMP files must be divisible by 4 bytes */
374 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
375 datasize
= bytesperrow
* height
;
377 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
378 if (!This
->imagedata
) return E_OUTOFMEMORY
;
380 offbits
.QuadPart
= This
->image_offset
;
381 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
382 if (FAILED(hr
)) goto fail
;
384 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
385 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
389 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
390 This
->stride
= -bytesperrow
;
394 This
->imagedatastart
= This
->imagedata
;
395 This
->stride
= bytesperrow
;
400 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
401 This
->imagedata
= NULL
;
402 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
406 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
411 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
415 hr
= BmpFrameDecode_ReadUncompressed(This
);
420 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
421 width
, height
, This
->stride
);
427 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
428 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
432 if (*bytesread
== 0 || *cursor
== *bytesread
)
434 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
440 if (*cursor
< *bytesread
)
441 *result
= buffer
[(*cursor
)++];
449 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
454 UINT datasize
, palettesize
;
459 LARGE_INTEGER offbits
;
460 ULONG cursor
=0, bytesread
=0;
462 width
= This
->bih
.bV5Width
;
463 height
= abs(This
->bih
.bV5Height
);
464 bytesperrow
= width
* 4;
465 datasize
= bytesperrow
* height
;
466 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
467 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
469 palettesize
= 4 * 256;
471 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
472 if (!This
->imagedata
)
479 offbits
.QuadPart
= This
->palette_offset
;
480 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
481 if (FAILED(hr
)) goto fail
;
483 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
484 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
487 offbits
.QuadPart
= This
->image_offset
;
488 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
489 if (FAILED(hr
)) goto fail
;
492 bgrdata
= (DWORD
*)This
->imagedata
;
500 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
504 else if (length
== 0)
508 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
513 case 0: /* end of line */
517 case 1: /* end of bitmap */
522 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
524 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
531 default: /* absolute mode */
533 while (length
-- && x
< width
)
536 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
539 bgrdata
[y
*width
+ x
++] = palette
[index
];
542 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
551 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
554 color
= palette
[index
];
555 while (length
-- && x
< width
)
556 bgrdata
[y
*width
+ x
++] = color
;
561 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
562 This
->stride
= -bytesperrow
;
567 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
568 This
->imagedata
= NULL
;
569 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
573 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
578 UINT datasize
, palettesize
;
583 LARGE_INTEGER offbits
;
584 ULONG cursor
=0, bytesread
=0;
586 width
= This
->bih
.bV5Width
;
587 height
= abs(This
->bih
.bV5Height
);
588 bytesperrow
= width
* 4;
589 datasize
= bytesperrow
* height
;
590 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
591 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
593 palettesize
= 4 * 16;
595 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
596 if (!This
->imagedata
)
603 offbits
.QuadPart
= This
->palette_offset
;
604 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
605 if (FAILED(hr
)) goto fail
;
607 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
608 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
611 offbits
.QuadPart
= This
->image_offset
;
612 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
613 if (FAILED(hr
)) goto fail
;
616 bgrdata
= (DWORD
*)This
->imagedata
;
624 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
628 else if (length
== 0)
632 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
637 case 0: /* end of line */
641 case 1: /* end of bitmap */
646 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
648 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
655 default: /* absolute mode */
659 while (length
-- && x
< width
)
662 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
666 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
667 if (length
-- && x
< width
)
668 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
673 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
684 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
687 color1
= palette
[colors
>>4];
688 color2
= palette
[colors
&0xf];
689 while (length
-- && x
< width
)
691 bgrdata
[y
*width
+ x
++] = color1
;
692 if (length
-- && x
< width
)
693 bgrdata
[y
*width
+ x
++] = color2
;
701 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
702 This
->stride
= -bytesperrow
;
707 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
708 This
->imagedata
= NULL
;
709 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
713 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
718 struct bitfields_format
{
719 WORD bitcount
; /* 0 for end of list */
724 const WICPixelFormatGUID
*pixelformat
;
725 ReadDataFunc read_data_func
;
728 static const struct bitfields_format bitfields_formats
[] = {
729 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
730 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
731 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
732 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
733 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
737 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
738 BmpFrameDecode_QueryInterface
,
739 BmpFrameDecode_AddRef
,
740 BmpFrameDecode_Release
,
741 BmpFrameDecode_GetSize
,
742 BmpFrameDecode_GetPixelFormat
,
743 BmpFrameDecode_GetResolution
,
744 BmpFrameDecode_CopyPalette
,
745 BmpFrameDecode_CopyPixels
,
746 BmpFrameDecode_GetMetadataQueryReader
,
747 BmpFrameDecode_GetColorContexts
,
748 BmpFrameDecode_GetThumbnail
751 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
754 ULONG bytestoread
, bytesread
;
757 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
760 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
761 if (FAILED(hr
)) return hr
;
765 BITMAPFILEHEADER bfh
;
766 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
767 if (FAILED(hr
)) return hr
;
768 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
769 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
770 This
->image_offset
= bfh
.bfOffBits
;
773 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
774 if (FAILED(hr
)) return hr
;
775 if (bytesread
!= sizeof(DWORD
) ||
776 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
777 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
778 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
779 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
780 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
782 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
783 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
784 if (FAILED(hr
)) return hr
;
785 if (bytestoread
!= bytesread
) return E_FAIL
;
788 This
->palette_offset
= This
->bih
.bV5Size
;
790 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
794 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
796 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
801 This
->bih
.bV5Height
/= 2;
805 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
806 read the extra fields */
807 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
808 This
->bih
.bV5Compression
== BI_BITFIELDS
)
810 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
811 if (FAILED(hr
)) return hr
;
812 if (bytesread
!= 12) return E_FAIL
;
813 This
->bih
.bV5AlphaMask
= 0;
814 This
->palette_offset
+= 12;
817 /* decide what kind of bitmap this is and how/if we can read it */
818 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
820 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
821 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
822 This
->bitsperpixel
= bch
->bcBitCount
;
823 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
824 switch(bch
->bcBitCount
)
827 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
830 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
833 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
836 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
839 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
842 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
843 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
847 else /* struct is compatible with BITMAPINFOHEADER */
849 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
850 switch(This
->bih
.bV5Compression
)
853 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
854 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
855 switch(This
->bih
.bV5BitCount
)
858 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
861 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
864 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
867 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
870 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
873 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
876 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
879 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
880 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
884 This
->bitsperpixel
= 32;
885 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
886 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
889 This
->bitsperpixel
= 32;
890 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
891 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
895 const struct bitfields_format
*format
;
896 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
898 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
899 This
->bitsperpixel
= 0;
900 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
901 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
902 FIXME("Huffman 1D compression is unsupported\n");
905 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
906 for (format
= bitfields_formats
; format
->bitcount
; format
++)
908 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
909 (format
->redmask
== This
->bih
.bV5RedMask
) &&
910 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
911 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
912 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
914 This
->read_data_func
= format
->read_data_func
;
915 This
->pixelformat
= format
->pixelformat
;
919 if (!format
->bitcount
)
921 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
922 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
923 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
924 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
929 This
->bitsperpixel
= 0;
930 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
931 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
932 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
939 /* In a packed DIB, the image follows the palette. */
940 ULONG palette_count
, palette_size
;
941 if (This
->bih
.bV5ClrUsed
)
942 palette_count
= This
->bih
.bV5ClrUsed
;
943 else if (This
->bih
.bV5BitCount
<= 8)
944 palette_count
= 1 << This
->bih
.bV5BitCount
;
947 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
948 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
950 palette_size
= sizeof(RGBQUAD
) * palette_count
;
951 This
->image_offset
= This
->palette_offset
+ palette_size
;
954 This
->initialized
= TRUE
;
959 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
962 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
963 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
965 if (!ppv
) return E_INVALIDARG
;
967 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
974 return E_NOINTERFACE
;
977 IUnknown_AddRef((IUnknown
*)*ppv
);
981 static ULONG WINAPI
BmpDecoder_AddRef(IWICBitmapDecoder
*iface
)
983 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
984 ULONG ref
= InterlockedIncrement(&This
->ref
);
986 TRACE("(%p) refcount=%u\n", iface
, ref
);
991 static ULONG WINAPI
BmpDecoder_Release(IWICBitmapDecoder
*iface
)
993 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
994 ULONG ref
= InterlockedDecrement(&This
->ref
);
996 TRACE("(%p) refcount=%u\n", iface
, ref
);
1000 if (This
->stream
) IStream_Release(This
->stream
);
1001 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
1002 This
->lock
.DebugInfo
->Spare
[0] = 0;
1003 DeleteCriticalSection(&This
->lock
);
1004 HeapFree(GetProcessHeap(), 0, This
);
1010 static HRESULT WINAPI
BmpDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1011 DWORD
*pdwCapability
)
1014 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1016 EnterCriticalSection(&This
->lock
);
1017 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1018 LeaveCriticalSection(&This
->lock
);
1019 if (FAILED(hr
)) return hr
;
1021 if (This
->read_data_func
== BmpFrameDecode_ReadUnsupported
)
1024 *pdwCapability
= WICBitmapDecoderCapabilityCanDecodeAllImages
;
1029 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1030 WICDecodeOptions cacheOptions
)
1033 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1035 EnterCriticalSection(&This
->lock
);
1036 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1040 This
->stream
= pIStream
;
1041 IStream_AddRef(pIStream
);
1043 LeaveCriticalSection(&This
->lock
);
1048 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1049 GUID
*pguidContainerFormat
)
1051 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1055 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1056 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1059 IWICComponentInfo
*compinfo
;
1061 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1063 hr
= CreateComponentInfo(&CLSID_WICBmpDecoder
, &compinfo
);
1064 if (FAILED(hr
)) return hr
;
1066 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1067 (void**)ppIDecoderInfo
);
1069 IWICComponentInfo_Release(compinfo
);
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
,
1117 static HRESULT WINAPI
BmpDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1118 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1120 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1122 if (index
!= 0) return E_INVALIDARG
;
1124 if (!This
->stream
) return WINCODEC_ERR_WRONGSTATE
;
1126 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
1127 IWICBitmapDecoder_AddRef(iface
);
1132 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl
= {
1133 BmpDecoder_QueryInterface
,
1136 BmpDecoder_QueryCapability
,
1137 BmpDecoder_Initialize
,
1138 BmpDecoder_GetContainerFormat
,
1139 BmpDecoder_GetDecoderInfo
,
1140 BmpDecoder_CopyPalette
,
1141 BmpDecoder_GetMetadataQueryReader
,
1142 BmpDecoder_GetPreview
,
1143 BmpDecoder_GetColorContexts
,
1144 BmpDecoder_GetThumbnail
,
1145 BmpDecoder_GetFrameCount
,
1149 static HRESULT
BmpDecoder_Create(int packed
, int icoframe
, BmpDecoder
**ppDecoder
)
1153 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder
));
1154 if (!This
) return E_OUTOFMEMORY
;
1156 This
->IWICBitmapDecoder_iface
.lpVtbl
= &BmpDecoder_Vtbl
;
1157 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &BmpDecoder_FrameVtbl
;
1159 This
->initialized
= FALSE
;
1160 This
->stream
= NULL
;
1161 This
->imagedata
= NULL
;
1162 InitializeCriticalSection(&This
->lock
);
1163 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BmpDecoder.lock");
1164 This
->packed
= packed
;
1165 This
->icoframe
= icoframe
;
1172 static HRESULT
BmpDecoder_Construct(int packed
, int icoframe
, IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1177 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1181 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
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(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1194 return BmpDecoder_Construct(FALSE
, FALSE
, pUnkOuter
, iid
, ppv
);
1197 HRESULT
DibDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1199 return BmpDecoder_Construct(TRUE
, FALSE
, pUnkOuter
, 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;