2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "wincodecs_private.h"
34 DWORD bc2ClrImportant
;
35 /* same as BITMAPINFOHEADER until this point */
40 DWORD bc2HalftoneSize1
;
41 DWORD bc2HalftoneSize2
;
46 typedef HRESULT (*ReadDataFunc
)(BmpDecoder
* This
);
49 IWICBitmapDecoder IWICBitmapDecoder_iface
;
50 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
57 const WICPixelFormatGUID
*pixelformat
;
59 ReadDataFunc read_data_func
;
63 CRITICAL_SECTION lock
; /* must be held when initialized/imagedata is set or stream is accessed */
64 int packed
; /* If TRUE, don't look for a file header and assume a packed DIB. */
65 int icoframe
; /* If TRUE, this is a frame of a .ico file. */
68 static inline BmpDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
70 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapDecoder_iface
);
73 static inline BmpDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
75 return CONTAINING_RECORD(iface
, BmpDecoder
, IWICBitmapFrameDecode_iface
);
78 static HRESULT WINAPI
BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
81 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
83 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
85 if (!ppv
) return E_INVALIDARG
;
87 if (IsEqualIID(&IID_IUnknown
, iid
) ||
88 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
89 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
91 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
99 IUnknown_AddRef((IUnknown
*)*ppv
);
103 static ULONG WINAPI
BmpFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
105 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
107 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
110 static ULONG WINAPI
BmpFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
112 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
114 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
117 static HRESULT WINAPI
BmpFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
118 UINT
*puiWidth
, UINT
*puiHeight
)
120 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
121 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
123 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
125 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
126 *puiWidth
= bch
->bcWidth
;
127 *puiHeight
= bch
->bcHeight
;
131 *puiWidth
= This
->bih
.bV5Width
;
132 *puiHeight
= abs(This
->bih
.bV5Height
);
137 static HRESULT WINAPI
BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
138 WICPixelFormatGUID
*pPixelFormat
)
140 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
141 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
143 memcpy(pPixelFormat
, This
->pixelformat
, sizeof(GUID
));
148 static HRESULT
BmpHeader_GetResolution(BITMAPV5HEADER
*bih
, double *pDpiX
, double *pDpiY
)
150 LONG resx
= 0, resy
= 0;
152 switch (bih
->bV5Size
)
155 case sizeof(BITMAPCOREHEADER
):
158 case sizeof(BITMAPCOREHEADER2
):
159 case sizeof(BITMAPINFOHEADER
):
160 case sizeof(BITMAPV4HEADER
):
161 case sizeof(BITMAPV5HEADER
):
162 resx
= bih
->bV5XPelsPerMeter
;
163 resy
= bih
->bV5YPelsPerMeter
;
174 *pDpiX
= resx
* 0.0254;
175 *pDpiY
= resy
* 0.0254;
181 static HRESULT WINAPI
BmpFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
182 double *pDpiX
, double *pDpiY
)
184 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
185 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
187 return BmpHeader_GetResolution(&This
->bih
, pDpiX
, pDpiY
);
190 static HRESULT WINAPI
BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
191 IWICPalette
*pIPalette
)
194 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
196 WICColor
*wiccolors
=NULL
;
197 RGBTRIPLE
*bgrcolors
=NULL
;
199 TRACE("(%p,%p)\n", iface
, pIPalette
);
201 EnterCriticalSection(&This
->lock
);
203 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
205 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
206 if (bch
->bcBitCount
<= 8)
208 /* 2**n colors in BGR format after the header */
209 ULONG tablesize
, bytesread
;
210 LARGE_INTEGER offset
;
213 count
= 1 << bch
->bcBitCount
;
214 wiccolors
= HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor
) * count
);
215 tablesize
= sizeof(RGBTRIPLE
) * count
;
216 bgrcolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
217 if (!wiccolors
|| !bgrcolors
)
223 offset
.QuadPart
= This
->palette_offset
;
224 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
225 if (FAILED(hr
)) goto end
;
227 hr
= IStream_Read(This
->stream
, bgrcolors
, tablesize
, &bytesread
);
228 if (FAILED(hr
)) goto end
;
229 if (bytesread
!= tablesize
) {
234 for (i
=0; i
<count
; i
++)
236 wiccolors
[i
] = 0xff000000|
237 (bgrcolors
[i
].rgbtRed
<<16)|
238 (bgrcolors
[i
].rgbtGreen
<<8)|
239 bgrcolors
[i
].rgbtBlue
;
244 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
250 if (This
->bih
.bV5BitCount
<= 8)
252 ULONG tablesize
, bytesread
;
253 LARGE_INTEGER offset
;
256 if (This
->bih
.bV5ClrUsed
== 0)
257 count
= 1 << This
->bih
.bV5BitCount
;
259 count
= This
->bih
.bV5ClrUsed
;
261 tablesize
= sizeof(WICColor
) * count
;
262 wiccolors
= HeapAlloc(GetProcessHeap(), 0, tablesize
);
269 offset
.QuadPart
= This
->palette_offset
;
270 hr
= IStream_Seek(This
->stream
, offset
, STREAM_SEEK_SET
, NULL
);
271 if (FAILED(hr
)) goto end
;
273 hr
= IStream_Read(This
->stream
, wiccolors
, tablesize
, &bytesread
);
274 if (FAILED(hr
)) goto end
;
275 if (bytesread
!= tablesize
) {
280 /* convert from BGR to BGRA by setting alpha to 100% */
281 for (i
=0; i
<count
; i
++)
282 wiccolors
[i
] |= 0xff000000;
286 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
293 LeaveCriticalSection(&This
->lock
);
296 hr
= IWICPalette_InitializeCustom(pIPalette
, wiccolors
, count
);
298 HeapFree(GetProcessHeap(), 0, wiccolors
);
299 HeapFree(GetProcessHeap(), 0, bgrcolors
);
303 static HRESULT WINAPI
BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
304 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
306 BmpDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
309 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
311 EnterCriticalSection(&This
->lock
);
312 if (!This
->imagedata
)
314 hr
= This
->read_data_func(This
);
316 LeaveCriticalSection(&This
->lock
);
317 if (FAILED(hr
)) return hr
;
319 hr
= BmpFrameDecode_GetSize(iface
, &width
, &height
);
320 if (FAILED(hr
)) return hr
;
322 return copy_pixels(This
->bitsperpixel
, This
->imagedatastart
,
323 width
, height
, This
->stride
,
324 prc
, cbStride
, cbBufferSize
, pbBuffer
);
327 static HRESULT WINAPI
BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
328 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
330 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
331 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
334 static HRESULT WINAPI
BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
335 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
337 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
338 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
341 static HRESULT WINAPI
BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
342 IWICBitmapSource
**ppIThumbnail
)
344 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
345 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
348 static HRESULT
BmpFrameDecode_ReadUncompressed(BmpDecoder
* This
)
355 LARGE_INTEGER offbits
;
358 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
360 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
361 width
= bch
->bcWidth
;
362 height
= bch
->bcHeight
;
367 width
= This
->bih
.bV5Width
;
368 height
= abs(This
->bih
.bV5Height
);
369 bottomup
= (This
->bih
.bV5Height
> 0);
372 /* row sizes in BMP files must be divisible by 4 bytes */
373 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
374 datasize
= bytesperrow
* height
;
376 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
377 if (!This
->imagedata
) return E_OUTOFMEMORY
;
379 offbits
.QuadPart
= This
->image_offset
;
380 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
381 if (FAILED(hr
)) goto fail
;
383 hr
= IStream_Read(This
->stream
, This
->imagedata
, datasize
, &bytesread
);
384 if (FAILED(hr
) || bytesread
!= datasize
) goto fail
;
388 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
389 This
->stride
= -bytesperrow
;
393 This
->imagedatastart
= This
->imagedata
;
394 This
->stride
= bytesperrow
;
399 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
400 This
->imagedata
= NULL
;
401 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
405 static HRESULT
BmpFrameDecode_ReadRGB8(BmpDecoder
* This
)
410 hr
= IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
414 hr
= BmpFrameDecode_ReadUncompressed(This
);
419 reverse_bgr8(This
->bitsperpixel
/8, This
->imagedatastart
,
420 width
, height
, This
->stride
);
426 static HRESULT
ReadByte(IStream
*stream
, BYTE
*buffer
, ULONG buffer_size
,
427 ULONG
*cursor
, ULONG
*bytesread
, BYTE
*result
)
431 if (*bytesread
== 0 || *cursor
== *bytesread
)
433 hr
= IStream_Read(stream
, buffer
, buffer_size
, bytesread
);
439 if (*cursor
< *bytesread
)
440 *result
= buffer
[(*cursor
)++];
448 static HRESULT
BmpFrameDecode_ReadRLE8(BmpDecoder
* This
)
453 UINT datasize
, palettesize
;
458 LARGE_INTEGER offbits
;
459 ULONG cursor
=0, bytesread
=0;
461 width
= This
->bih
.bV5Width
;
462 height
= abs(This
->bih
.bV5Height
);
463 bytesperrow
= width
* 4;
464 datasize
= bytesperrow
* height
;
465 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 256)
466 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
468 palettesize
= 4 * 256;
470 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
471 if (!This
->imagedata
)
478 offbits
.QuadPart
= This
->palette_offset
;
479 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
480 if (FAILED(hr
)) goto fail
;
482 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
483 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
486 offbits
.QuadPart
= This
->image_offset
;
487 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
488 if (FAILED(hr
)) goto fail
;
491 bgrdata
= (DWORD
*)This
->imagedata
;
499 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
503 else if (length
== 0)
507 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
512 case 0: /* end of line */
516 case 1: /* end of bitmap */
521 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
523 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
530 default: /* absolute mode */
532 while (length
-- && x
< width
)
535 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
538 bgrdata
[y
*width
+ x
++] = palette
[index
];
541 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
550 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &index
);
553 color
= palette
[index
];
554 while (length
-- && x
< width
)
555 bgrdata
[y
*width
+ x
++] = color
;
560 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
561 This
->stride
= -bytesperrow
;
566 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
567 This
->imagedata
= NULL
;
568 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
572 static HRESULT
BmpFrameDecode_ReadRLE4(BmpDecoder
* This
)
577 UINT datasize
, palettesize
;
582 LARGE_INTEGER offbits
;
583 ULONG cursor
=0, bytesread
=0;
585 width
= This
->bih
.bV5Width
;
586 height
= abs(This
->bih
.bV5Height
);
587 bytesperrow
= width
* 4;
588 datasize
= bytesperrow
* height
;
589 if (This
->bih
.bV5ClrUsed
&& This
->bih
.bV5ClrUsed
< 16)
590 palettesize
= 4 * This
->bih
.bV5ClrUsed
;
592 palettesize
= 4 * 16;
594 This
->imagedata
= HeapAlloc(GetProcessHeap(), 0, datasize
);
595 if (!This
->imagedata
)
602 offbits
.QuadPart
= This
->palette_offset
;
603 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
604 if (FAILED(hr
)) goto fail
;
606 hr
= IStream_Read(This
->stream
, palette
, palettesize
, &bytesread
);
607 if (FAILED(hr
) || bytesread
!= palettesize
) goto fail
;
610 offbits
.QuadPart
= This
->image_offset
;
611 hr
= IStream_Seek(This
->stream
, offbits
, STREAM_SEEK_SET
, NULL
);
612 if (FAILED(hr
)) goto fail
;
615 bgrdata
= (DWORD
*)This
->imagedata
;
623 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
);
627 else if (length
== 0)
631 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &escape
);
636 case 0: /* end of line */
640 case 1: /* end of bitmap */
645 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dx
);
647 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &dy
);
654 default: /* absolute mode */
658 while (length
-- && x
< width
)
661 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
665 bgrdata
[y
*width
+ x
++] = palette
[colors
>>4];
666 if (length
-- && x
< width
)
667 bgrdata
[y
*width
+ x
++] = palette
[colors
&0xf];
672 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &length
); /* skip pad byte */
683 hr
= ReadByte(This
->stream
, rledata
, 4096, &cursor
, &bytesread
, &colors
);
686 color1
= palette
[colors
>>4];
687 color2
= palette
[colors
&0xf];
688 while (length
-- && x
< width
)
690 bgrdata
[y
*width
+ x
++] = color1
;
691 if (length
-- && x
< width
)
692 bgrdata
[y
*width
+ x
++] = color2
;
700 This
->imagedatastart
= This
->imagedata
+ (height
-1) * bytesperrow
;
701 This
->stride
= -bytesperrow
;
706 HeapFree(GetProcessHeap(), 0, This
->imagedata
);
707 This
->imagedata
= NULL
;
708 if (SUCCEEDED(hr
)) hr
= E_FAIL
;
712 static HRESULT
BmpFrameDecode_ReadUnsupported(BmpDecoder
* This
)
717 struct bitfields_format
{
718 WORD bitcount
; /* 0 for end of list */
723 const WICPixelFormatGUID
*pixelformat
;
724 ReadDataFunc read_data_func
;
727 static const struct bitfields_format bitfields_formats
[] = {
728 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555
,BmpFrameDecode_ReadUncompressed
},
729 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565
,BmpFrameDecode_ReadUncompressed
},
730 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadUncompressed
},
731 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA
,BmpFrameDecode_ReadUncompressed
},
732 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR
,BmpFrameDecode_ReadRGB8
},
736 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl
= {
737 BmpFrameDecode_QueryInterface
,
738 BmpFrameDecode_AddRef
,
739 BmpFrameDecode_Release
,
740 BmpFrameDecode_GetSize
,
741 BmpFrameDecode_GetPixelFormat
,
742 BmpFrameDecode_GetResolution
,
743 BmpFrameDecode_CopyPalette
,
744 BmpFrameDecode_CopyPixels
,
745 BmpFrameDecode_GetMetadataQueryReader
,
746 BmpFrameDecode_GetColorContexts
,
747 BmpFrameDecode_GetThumbnail
750 static HRESULT
BmpDecoder_ReadHeaders(BmpDecoder
* This
, IStream
*stream
)
753 ULONG bytestoread
, bytesread
;
756 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
759 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
760 if (FAILED(hr
)) return hr
;
764 BITMAPFILEHEADER bfh
;
765 hr
= IStream_Read(stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &bytesread
);
766 if (FAILED(hr
)) return hr
;
767 if (bytesread
!= sizeof(BITMAPFILEHEADER
) ||
768 bfh
.bfType
!= 0x4d42 /* "BM" */) return E_FAIL
;
769 This
->image_offset
= bfh
.bfOffBits
;
772 hr
= IStream_Read(stream
, &This
->bih
.bV5Size
, sizeof(DWORD
), &bytesread
);
773 if (FAILED(hr
)) return hr
;
774 if (bytesread
!= sizeof(DWORD
) ||
775 (This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER
) &&
776 This
->bih
.bV5Size
!= sizeof(BITMAPCOREHEADER2
) &&
777 This
->bih
.bV5Size
!= sizeof(BITMAPINFOHEADER
) &&
778 This
->bih
.bV5Size
!= sizeof(BITMAPV4HEADER
) &&
779 This
->bih
.bV5Size
!= sizeof(BITMAPV5HEADER
))) return E_FAIL
;
781 bytestoread
= This
->bih
.bV5Size
-sizeof(DWORD
);
782 hr
= IStream_Read(stream
, &This
->bih
.bV5Width
, bytestoread
, &bytesread
);
783 if (FAILED(hr
)) return hr
;
784 if (bytestoread
!= bytesread
) return E_FAIL
;
787 This
->palette_offset
= This
->bih
.bV5Size
;
789 This
->palette_offset
= sizeof(BITMAPFILEHEADER
) + This
->bih
.bV5Size
;
793 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
795 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
800 This
->bih
.bV5Height
/= 2;
804 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
805 read the extra fields */
806 if (This
->bih
.bV5Size
== sizeof(BITMAPINFOHEADER
) &&
807 This
->bih
.bV5Compression
== BI_BITFIELDS
)
809 hr
= IStream_Read(stream
, &This
->bih
.bV5RedMask
, 12, &bytesread
);
810 if (FAILED(hr
)) return hr
;
811 if (bytesread
!= 12) return E_FAIL
;
812 This
->bih
.bV5AlphaMask
= 0;
813 This
->palette_offset
+= 12;
816 /* decide what kind of bitmap this is and how/if we can read it */
817 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
819 BITMAPCOREHEADER
*bch
= (BITMAPCOREHEADER
*)&This
->bih
;
820 TRACE("BITMAPCOREHEADER with depth=%i\n", bch
->bcBitCount
);
821 This
->bitsperpixel
= bch
->bcBitCount
;
822 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
823 switch(bch
->bcBitCount
)
826 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
829 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
832 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
835 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
838 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
841 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
842 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch
->bcBitCount
);
846 else /* struct is compatible with BITMAPINFOHEADER */
848 TRACE("bitmap header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
849 switch(This
->bih
.bV5Compression
)
852 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
853 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
854 switch(This
->bih
.bV5BitCount
)
857 This
->pixelformat
= &GUID_WICPixelFormat1bppIndexed
;
860 This
->pixelformat
= &GUID_WICPixelFormat2bppIndexed
;
863 This
->pixelformat
= &GUID_WICPixelFormat4bppIndexed
;
866 This
->pixelformat
= &GUID_WICPixelFormat8bppIndexed
;
869 This
->pixelformat
= &GUID_WICPixelFormat16bppBGR555
;
872 This
->pixelformat
= &GUID_WICPixelFormat24bppBGR
;
875 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
878 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
879 FIXME("unsupported bit depth %i for uncompressed RGB\n", This
->bih
.bV5BitCount
);
883 This
->bitsperpixel
= 32;
884 This
->read_data_func
= BmpFrameDecode_ReadRLE8
;
885 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
888 This
->bitsperpixel
= 32;
889 This
->read_data_func
= BmpFrameDecode_ReadRLE4
;
890 This
->pixelformat
= &GUID_WICPixelFormat32bppBGR
;
894 const struct bitfields_format
*format
;
895 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER2
))
897 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
898 This
->bitsperpixel
= 0;
899 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
900 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
901 FIXME("Huffman 1D compression is unsupported\n");
904 This
->bitsperpixel
= This
->bih
.bV5BitCount
;
905 for (format
= bitfields_formats
; format
->bitcount
; format
++)
907 if ((format
->bitcount
== This
->bih
.bV5BitCount
) &&
908 (format
->redmask
== This
->bih
.bV5RedMask
) &&
909 (format
->greenmask
== This
->bih
.bV5GreenMask
) &&
910 (format
->bluemask
== This
->bih
.bV5BlueMask
) &&
911 (format
->alphamask
== This
->bih
.bV5AlphaMask
))
913 This
->read_data_func
= format
->read_data_func
;
914 This
->pixelformat
= format
->pixelformat
;
918 if (!format
->bitcount
)
920 This
->read_data_func
= BmpFrameDecode_ReadUncompressed
;
921 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
922 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
923 This
->bih
.bV5BitCount
, This
->bih
.bV5RedMask
, This
->bih
.bV5GreenMask
, This
->bih
.bV5BlueMask
, This
->bih
.bV5AlphaMask
);
928 This
->bitsperpixel
= 0;
929 This
->read_data_func
= BmpFrameDecode_ReadUnsupported
;
930 This
->pixelformat
= &GUID_WICPixelFormatUndefined
;
931 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This
->bih
.bV5Size
, This
->bih
.bV5Compression
, This
->bih
.bV5BitCount
);
938 /* In a packed DIB, the image follows the palette. */
939 ULONG palette_count
, palette_size
;
940 if (This
->bih
.bV5ClrUsed
)
941 palette_count
= This
->bih
.bV5ClrUsed
;
942 else if (This
->bih
.bV5BitCount
<= 8)
943 palette_count
= 1 << This
->bih
.bV5BitCount
;
946 if (This
->bih
.bV5Size
== sizeof(BITMAPCOREHEADER
))
947 palette_size
= sizeof(RGBTRIPLE
) * palette_count
;
949 palette_size
= sizeof(RGBQUAD
) * palette_count
;
950 This
->image_offset
= This
->palette_offset
+ palette_size
;
953 This
->initialized
= TRUE
;
958 static HRESULT WINAPI
BmpDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
961 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
962 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
964 if (!ppv
) return E_INVALIDARG
;
966 if (IsEqualIID(&IID_IUnknown
, iid
) ||
967 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
969 *ppv
= &This
->IWICBitmapDecoder_iface
;
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
*stream
,
1014 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1016 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
1018 if (!stream
|| !capability
) return E_INVALIDARG
;
1020 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
1021 if (hr
!= S_OK
) return hr
;
1023 *capability
= This
->read_data_func
== BmpFrameDecode_ReadUnsupported
? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages
;
1027 static HRESULT WINAPI
BmpDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
1028 WICDecodeOptions cacheOptions
)
1031 BmpDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1033 EnterCriticalSection(&This
->lock
);
1034 hr
= BmpDecoder_ReadHeaders(This
, pIStream
);
1038 This
->stream
= pIStream
;
1039 IStream_AddRef(pIStream
);
1041 LeaveCriticalSection(&This
->lock
);
1046 static HRESULT WINAPI
BmpDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1047 GUID
*pguidContainerFormat
)
1049 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
1053 static HRESULT WINAPI
BmpDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1054 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1057 IWICComponentInfo
*compinfo
;
1059 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1061 hr
= CreateComponentInfo(&CLSID_WICBmpDecoder
, &compinfo
);
1062 if (FAILED(hr
)) return hr
;
1064 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1065 (void**)ppIDecoderInfo
);
1067 IWICComponentInfo_Release(compinfo
);
1072 static HRESULT WINAPI
BmpDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1073 IWICPalette
*pIPalette
)
1075 TRACE("(%p,%p)\n", iface
, pIPalette
);
1077 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1080 static HRESULT WINAPI
BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1081 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1083 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1084 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1087 static HRESULT WINAPI
BmpDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1088 IWICBitmapSource
**ppIBitmapSource
)
1090 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1091 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1094 static HRESULT WINAPI
BmpDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1095 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1097 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1098 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1101 static HRESULT WINAPI
BmpDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1102 IWICBitmapSource
**ppIThumbnail
)
1104 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1105 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1108 static HRESULT WINAPI
BmpDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1111 if (!pCount
) return E_INVALIDARG
;
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_FRAMEMISSING
;
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
, REFIID iid
, void** ppv
)
1177 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1181 ret
= BmpDecoder_Create(packed
, icoframe
, &This
);
1182 if (FAILED(ret
)) return ret
;
1184 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1185 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1190 HRESULT
BmpDecoder_CreateInstance(REFIID iid
, void** ppv
)
1192 return BmpDecoder_Construct(FALSE
, FALSE
, iid
, ppv
);
1195 HRESULT
DibDecoder_CreateInstance(REFIID iid
, void** ppv
)
1197 return BmpDecoder_Construct(TRUE
, FALSE
, iid
, ppv
);
1200 HRESULT
IcoDibDecoder_CreateInstance(BmpDecoder
**ppDecoder
)
1202 return BmpDecoder_Create(TRUE
, TRUE
, ppDecoder
);
1205 void BmpDecoder_GetWICDecoder(BmpDecoder
*This
, IWICBitmapDecoder
**ppDecoder
)
1207 *ppDecoder
= &This
->IWICBitmapDecoder_iface
;
1210 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1211 void BmpDecoder_FindIconMask(BmpDecoder
*This
, ULONG
*mask_offset
, int *topdown
)
1213 assert(This
->stream
!= NULL
);
1215 if (This
->read_data_func
== BmpFrameDecode_ReadUncompressed
)
1217 /* RGB or BITFIELDS data */
1218 ULONG width
, height
, bytesperrow
, datasize
;
1219 IWICBitmapFrameDecode_GetSize(&This
->IWICBitmapFrameDecode_iface
, &width
, &height
);
1220 bytesperrow
= (((width
* This
->bitsperpixel
)+31)/32)*4;
1221 datasize
= bytesperrow
* height
;
1222 *mask_offset
= This
->image_offset
+ datasize
;
1227 *topdown
= This
->stride
> 0;