2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2016 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * 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
);
38 struct bmp_pixelformat
{
39 const WICPixelFormatGUID
*guid
;
41 UINT colors
; /* palette size */
49 static const struct bmp_pixelformat formats
[] = {
50 {&GUID_WICPixelFormat24bppBGR
, 24, 0, BI_RGB
},
51 {&GUID_WICPixelFormatBlackWhite
, 1, 2, BI_RGB
},
52 {&GUID_WICPixelFormat1bppIndexed
, 1, 2, BI_RGB
},
53 {&GUID_WICPixelFormat2bppIndexed
, 2, 4, BI_RGB
},
54 {&GUID_WICPixelFormat4bppIndexed
, 4, 16, BI_RGB
},
55 {&GUID_WICPixelFormat8bppIndexed
, 8, 256, BI_RGB
},
56 {&GUID_WICPixelFormat16bppBGR555
, 16, 0, BI_RGB
},
57 {&GUID_WICPixelFormat16bppBGR565
, 16, 0, BI_BITFIELDS
, 0xf800, 0x7e0, 0x1f, 0},
58 {&GUID_WICPixelFormat32bppBGR
, 32, 0, BI_RGB
},
60 /* Windows doesn't seem to support this one. */
61 {&GUID_WICPixelFormat32bppBGRA
, 32, 0, BI_BITFIELDS
, 0xff0000, 0xff00, 0xff, 0xff000000},
66 typedef struct BmpFrameEncode
{
67 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
73 const struct bmp_pixelformat
*format
;
77 WICColor palette
[256];
82 static const WCHAR wszEnableV5Header32bppBGRA
[] = {'E','n','a','b','l','e','V','5','H','e','a','d','e','r','3','2','b','p','p','B','G','R','A',0};
84 static inline BmpFrameEncode
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
86 return CONTAINING_RECORD(iface
, BmpFrameEncode
, IWICBitmapFrameEncode_iface
);
89 static HRESULT WINAPI
BmpFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
92 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
93 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
95 if (!ppv
) return E_INVALIDARG
;
97 if (IsEqualIID(&IID_IUnknown
, iid
) ||
98 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
100 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
105 return E_NOINTERFACE
;
108 IUnknown_AddRef((IUnknown
*)*ppv
);
112 static ULONG WINAPI
BmpFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
114 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
115 ULONG ref
= InterlockedIncrement(&This
->ref
);
117 TRACE("(%p) refcount=%u\n", iface
, ref
);
122 static ULONG WINAPI
BmpFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
124 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
125 ULONG ref
= InterlockedDecrement(&This
->ref
);
127 TRACE("(%p) refcount=%u\n", iface
, ref
);
131 if (This
->stream
) IStream_Release(This
->stream
);
132 HeapFree(GetProcessHeap(), 0, This
->bits
);
133 HeapFree(GetProcessHeap(), 0, This
);
139 static HRESULT WINAPI
BmpFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
140 IPropertyBag2
*pIEncoderOptions
)
142 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
143 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
145 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
147 if (pIEncoderOptions
)
148 WARN("ignoring encoder options.\n");
150 This
->initialized
= TRUE
;
155 static HRESULT WINAPI
BmpFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
156 UINT uiWidth
, UINT uiHeight
)
158 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
159 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
161 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
163 This
->width
= uiWidth
;
164 This
->height
= uiHeight
;
169 static HRESULT WINAPI
BmpFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
170 double dpiX
, double dpiY
)
172 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
173 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
175 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
183 static HRESULT WINAPI
BmpFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
184 WICPixelFormatGUID
*pPixelFormat
)
186 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
188 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
190 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
192 if (IsEqualGUID(pPixelFormat
, &GUID_WICPixelFormatBlackWhite
))
193 *pPixelFormat
= GUID_WICPixelFormat1bppIndexed
;
194 else if (IsEqualGUID(pPixelFormat
, &GUID_WICPixelFormat2bppIndexed
))
195 *pPixelFormat
= GUID_WICPixelFormat4bppIndexed
;
197 for (i
=0; formats
[i
].guid
; i
++)
199 if (IsEqualGUID(formats
[i
].guid
, pPixelFormat
))
203 if (!formats
[i
].guid
) i
= 0;
205 This
->format
= &formats
[i
];
206 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
211 static HRESULT WINAPI
BmpFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
212 UINT cCount
, IWICColorContext
**ppIColorContext
)
214 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
218 static HRESULT WINAPI
BmpFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
219 IWICPalette
*palette
)
221 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
224 TRACE("(%p,%p)\n", iface
, palette
);
226 if (!palette
) return E_INVALIDARG
;
228 if (!This
->initialized
)
229 return WINCODEC_ERR_NOTINITIALIZED
;
231 hr
= IWICPalette_GetColors(palette
, 256, This
->palette
, &This
->colors
);
235 for (i
= 0; i
< This
->colors
; i
++)
236 This
->palette
[i
] |= 0xff000000; /* BMP palette has no alpha */
241 static HRESULT WINAPI
BmpFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
242 IWICBitmapSource
*pIThumbnail
)
244 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
245 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
248 static HRESULT
BmpFrameEncode_AllocateBits(BmpFrameEncode
*This
)
252 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
253 return WINCODEC_ERR_WRONGSTATE
;
255 This
->stride
= (((This
->width
* This
->format
->bpp
)+31)/32)*4;
256 This
->bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->stride
* This
->height
);
257 if (!This
->bits
) return E_OUTOFMEMORY
;
263 static HRESULT WINAPI
BmpFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
264 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
266 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
267 UINT dstbuffersize
, bytesperrow
, row
;
271 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
273 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
274 return WINCODEC_ERR_WRONGSTATE
;
276 hr
= BmpFrameEncode_AllocateBits(This
);
277 if (FAILED(hr
)) return hr
;
279 bytesperrow
= ((This
->format
->bpp
* This
->width
) + 7) / 8;
281 if (This
->stride
< bytesperrow
)
284 dstbuffersize
= This
->stride
* (This
->height
- This
->lineswritten
);
285 if ((This
->stride
* (lineCount
- 1)) + bytesperrow
> dstbuffersize
)
289 dst
= This
->bits
+ This
->stride
* (This
->height
- This
->lineswritten
- 1);
290 for (row
= 0; row
< lineCount
; row
++)
292 memcpy(dst
, src
, bytesperrow
);
297 This
->lineswritten
+= lineCount
;
302 static HRESULT WINAPI
BmpFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
303 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
305 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
307 TRACE("(%p,%p,%s)\n", iface
, pIBitmapSource
, debug_wic_rect(prc
));
309 if (!This
->initialized
)
310 return WINCODEC_ERR_WRONGSTATE
;
312 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
313 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
314 This
->xres
, This
->yres
);
318 hr
= write_source(iface
, pIBitmapSource
, prc
,
319 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
325 static HRESULT WINAPI
BmpFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
327 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
328 BITMAPFILEHEADER bfh
;
335 TRACE("(%p)\n", iface
);
337 if (!This
->bits
|| This
->committed
|| This
->height
!= This
->lineswritten
)
338 return WINCODEC_ERR_WRONGSTATE
;
340 bfh
.bfType
= 0x4d42; /* "BM" */
344 bih
.bV5Size
= info_size
= sizeof(BITMAPINFOHEADER
);
345 bih
.bV5Width
= This
->width
;
346 bih
.bV5Height
= This
->height
;
348 bih
.bV5BitCount
= This
->format
->bpp
;
349 bih
.bV5Compression
= This
->format
->compression
;
350 bih
.bV5SizeImage
= This
->stride
*This
->height
;
351 bih
.bV5XPelsPerMeter
= (This
->xres
+0.0127) / 0.0254;
352 bih
.bV5YPelsPerMeter
= (This
->yres
+0.0127) / 0.0254;
353 bih
.bV5ClrUsed
= (This
->format
->bpp
<= 8) ? This
->colors
: 0;
354 bih
.bV5ClrImportant
= bih
.bV5ClrUsed
;
356 if (This
->format
->compression
== BI_BITFIELDS
)
358 if (This
->format
->alphamask
)
359 bih
.bV5Size
= info_size
= sizeof(BITMAPV4HEADER
);
361 info_size
= sizeof(BITMAPINFOHEADER
)+12;
362 bih
.bV5RedMask
= This
->format
->redmask
;
363 bih
.bV5GreenMask
= This
->format
->greenmask
;
364 bih
.bV5BlueMask
= This
->format
->bluemask
;
365 bih
.bV5AlphaMask
= This
->format
->alphamask
;
366 bih
.bV5CSType
= LCS_DEVICE_RGB
;
369 bfh
.bfSize
= sizeof(BITMAPFILEHEADER
) + info_size
+ bih
.bV5SizeImage
;
370 bfh
.bfOffBits
= sizeof(BITMAPFILEHEADER
) + info_size
;
371 bfh
.bfOffBits
+= bih
.bV5ClrUsed
* sizeof(WICColor
);
374 hr
= IStream_Seek(This
->stream
, pos
, STREAM_SEEK_SET
, NULL
);
375 if (FAILED(hr
)) return hr
;
377 hr
= IStream_Write(This
->stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &byteswritten
);
378 if (FAILED(hr
)) return hr
;
379 if (byteswritten
!= sizeof(BITMAPFILEHEADER
)) return E_FAIL
;
381 hr
= IStream_Write(This
->stream
, &bih
, info_size
, &byteswritten
);
382 if (FAILED(hr
)) return hr
;
383 if (byteswritten
!= info_size
) return E_FAIL
;
385 /* write the palette */
386 if (This
->format
->colors
)
388 hr
= IStream_Write(This
->stream
, This
->palette
, This
->colors
* sizeof(WICColor
), &byteswritten
);
389 if (FAILED(hr
)) return hr
;
390 if (byteswritten
!= This
->colors
* sizeof(WICColor
)) return E_FAIL
;
393 hr
= IStream_Write(This
->stream
, This
->bits
, bih
.bV5SizeImage
, &byteswritten
);
394 if (FAILED(hr
)) return hr
;
395 if (byteswritten
!= bih
.bV5SizeImage
) return E_FAIL
;
397 This
->committed
= TRUE
;
402 static HRESULT WINAPI
BmpFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
403 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
405 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
409 static const IWICBitmapFrameEncodeVtbl BmpFrameEncode_Vtbl
= {
410 BmpFrameEncode_QueryInterface
,
411 BmpFrameEncode_AddRef
,
412 BmpFrameEncode_Release
,
413 BmpFrameEncode_Initialize
,
414 BmpFrameEncode_SetSize
,
415 BmpFrameEncode_SetResolution
,
416 BmpFrameEncode_SetPixelFormat
,
417 BmpFrameEncode_SetColorContexts
,
418 BmpFrameEncode_SetPalette
,
419 BmpFrameEncode_SetThumbnail
,
420 BmpFrameEncode_WritePixels
,
421 BmpFrameEncode_WriteSource
,
422 BmpFrameEncode_Commit
,
423 BmpFrameEncode_GetMetadataQueryWriter
426 typedef struct BmpEncoder
{
427 IWICBitmapEncoder IWICBitmapEncoder_iface
;
430 BmpFrameEncode
*frame
;
433 static inline BmpEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
435 return CONTAINING_RECORD(iface
, BmpEncoder
, IWICBitmapEncoder_iface
);
438 static HRESULT WINAPI
BmpEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
441 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
442 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
444 if (!ppv
) return E_INVALIDARG
;
446 if (IsEqualIID(&IID_IUnknown
, iid
) ||
447 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
449 *ppv
= &This
->IWICBitmapEncoder_iface
;
454 return E_NOINTERFACE
;
457 IUnknown_AddRef((IUnknown
*)*ppv
);
461 static ULONG WINAPI
BmpEncoder_AddRef(IWICBitmapEncoder
*iface
)
463 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
464 ULONG ref
= InterlockedIncrement(&This
->ref
);
466 TRACE("(%p) refcount=%u\n", iface
, ref
);
471 static ULONG WINAPI
BmpEncoder_Release(IWICBitmapEncoder
*iface
)
473 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
474 ULONG ref
= InterlockedDecrement(&This
->ref
);
476 TRACE("(%p) refcount=%u\n", iface
, ref
);
480 if (This
->stream
) IStream_Release(This
->stream
);
481 if (This
->frame
) IWICBitmapFrameEncode_Release(&This
->frame
->IWICBitmapFrameEncode_iface
);
482 HeapFree(GetProcessHeap(), 0, This
);
488 static HRESULT WINAPI
BmpEncoder_Initialize(IWICBitmapEncoder
*iface
,
489 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
491 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
493 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
495 IStream_AddRef(pIStream
);
496 This
->stream
= pIStream
;
501 static HRESULT WINAPI
BmpEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
502 GUID
*pguidContainerFormat
)
504 TRACE("(%p,%p)\n", iface
, pguidContainerFormat
);
506 if (!pguidContainerFormat
)
509 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
513 static HRESULT WINAPI
BmpEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
, IWICBitmapEncoderInfo
**info
)
515 IWICComponentInfo
*comp_info
;
518 TRACE("%p,%p\n", iface
, info
);
520 if (!info
) return E_INVALIDARG
;
522 hr
= CreateComponentInfo(&CLSID_WICBmpEncoder
, &comp_info
);
525 hr
= IWICComponentInfo_QueryInterface(comp_info
, &IID_IWICBitmapEncoderInfo
, (void **)info
);
526 IWICComponentInfo_Release(comp_info
);
531 static HRESULT WINAPI
BmpEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
532 UINT cCount
, IWICColorContext
**ppIColorContext
)
534 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
538 static HRESULT WINAPI
BmpEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*palette
)
540 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
542 TRACE("(%p,%p)\n", iface
, palette
);
543 return This
->stream
? WINCODEC_ERR_UNSUPPORTEDOPERATION
: WINCODEC_ERR_NOTINITIALIZED
;
546 static HRESULT WINAPI
BmpEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
548 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
549 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
552 static HRESULT WINAPI
BmpEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
554 TRACE("(%p,%p)\n", iface
, pIPreview
);
555 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
558 static HRESULT WINAPI
BmpEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
559 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
561 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
562 BmpFrameEncode
*encode
;
564 static const PROPBAG2 opts
[1] =
566 { PROPBAG2_TYPE_DATA
, VT_BOOL
, 0, 0, (LPOLESTR
)wszEnableV5Header32bppBGRA
},
569 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
571 if (This
->frame
) return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
573 if (!This
->stream
) return WINCODEC_ERR_NOTINITIALIZED
;
575 if (ppIEncoderOptions
)
577 hr
= CreatePropertyBag2(opts
, ARRAY_SIZE(opts
), ppIEncoderOptions
);
578 if (FAILED(hr
)) return hr
;
581 encode
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameEncode
));
584 IPropertyBag2_Release(*ppIEncoderOptions
);
585 *ppIEncoderOptions
= NULL
;
586 return E_OUTOFMEMORY
;
588 encode
->IWICBitmapFrameEncode_iface
.lpVtbl
= &BmpFrameEncode_Vtbl
;
590 IStream_AddRef(This
->stream
);
591 encode
->stream
= This
->stream
;
592 encode
->initialized
= FALSE
;
596 encode
->format
= NULL
;
599 encode
->lineswritten
= 0;
601 encode
->committed
= FALSE
;
603 *ppIFrameEncode
= &encode
->IWICBitmapFrameEncode_iface
;
604 This
->frame
= encode
;
609 static HRESULT WINAPI
BmpEncoder_Commit(IWICBitmapEncoder
*iface
)
611 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
612 TRACE("(%p)\n", iface
);
614 if (!This
->frame
|| !This
->frame
->committed
) return WINCODEC_ERR_WRONGSTATE
;
619 static HRESULT WINAPI
BmpEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
620 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
622 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
626 static const IWICBitmapEncoderVtbl BmpEncoder_Vtbl
= {
627 BmpEncoder_QueryInterface
,
630 BmpEncoder_Initialize
,
631 BmpEncoder_GetContainerFormat
,
632 BmpEncoder_GetEncoderInfo
,
633 BmpEncoder_SetColorContexts
,
634 BmpEncoder_SetPalette
,
635 BmpEncoder_SetThumbnail
,
636 BmpEncoder_SetPreview
,
637 BmpEncoder_CreateNewFrame
,
639 BmpEncoder_GetMetadataQueryWriter
642 HRESULT
BmpEncoder_CreateInstance(REFIID iid
, void** ppv
)
647 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
651 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpEncoder
));
652 if (!This
) return E_OUTOFMEMORY
;
654 This
->IWICBitmapEncoder_iface
.lpVtbl
= &BmpEncoder_Vtbl
;
659 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
660 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);