2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
38 struct bmp_pixelformat
{
39 const WICPixelFormatGUID
*guid
;
48 static const struct bmp_pixelformat formats
[] = {
49 {&GUID_WICPixelFormat24bppBGR
, 24, BI_RGB
},
50 {&GUID_WICPixelFormat16bppBGR555
, 16, BI_RGB
},
51 {&GUID_WICPixelFormat16bppBGR565
, 16, BI_BITFIELDS
, 0xf800, 0x7e0, 0x1f, 0},
52 {&GUID_WICPixelFormat32bppBGR
, 32, BI_RGB
},
54 /* Windows doesn't seem to support this one. */
55 {&GUID_WICPixelFormat32bppBGRA
, 32, BI_BITFIELDS
, 0xff0000, 0xff00, 0xff, 0xff000000},
60 typedef struct BmpFrameEncode
{
61 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
67 const struct bmp_pixelformat
*format
;
74 static inline BmpFrameEncode
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
76 return CONTAINING_RECORD(iface
, BmpFrameEncode
, IWICBitmapFrameEncode_iface
);
79 static HRESULT WINAPI
BmpFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
82 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(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_IWICBitmapFrameEncode
, iid
))
90 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
98 IUnknown_AddRef((IUnknown
*)*ppv
);
102 static ULONG WINAPI
BmpFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
104 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
105 ULONG ref
= InterlockedIncrement(&This
->ref
);
107 TRACE("(%p) refcount=%u\n", iface
, ref
);
112 static ULONG WINAPI
BmpFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
114 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
115 ULONG ref
= InterlockedDecrement(&This
->ref
);
117 TRACE("(%p) refcount=%u\n", iface
, ref
);
121 if (This
->stream
) IStream_Release(This
->stream
);
122 HeapFree(GetProcessHeap(), 0, This
->bits
);
123 HeapFree(GetProcessHeap(), 0, This
);
129 static HRESULT WINAPI
BmpFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
130 IPropertyBag2
*pIEncoderOptions
)
132 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
133 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
135 if (This
->initialized
) return WINCODEC_ERR_WRONGSTATE
;
137 This
->initialized
= TRUE
;
142 static HRESULT WINAPI
BmpFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
143 UINT uiWidth
, UINT uiHeight
)
145 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
146 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
148 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
150 This
->width
= uiWidth
;
151 This
->height
= uiHeight
;
156 static HRESULT WINAPI
BmpFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
157 double dpiX
, double dpiY
)
159 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
160 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
162 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
170 static HRESULT WINAPI
BmpFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
171 WICPixelFormatGUID
*pPixelFormat
)
173 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
175 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
177 if (!This
->initialized
|| This
->bits
) return WINCODEC_ERR_WRONGSTATE
;
179 for (i
=0; formats
[i
].guid
; i
++)
181 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
185 if (!formats
[i
].guid
) i
= 0;
187 This
->format
= &formats
[i
];
188 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
193 static HRESULT WINAPI
BmpFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
194 UINT cCount
, IWICColorContext
**ppIColorContext
)
196 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
200 static HRESULT WINAPI
BmpFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
201 IWICPalette
*pIPalette
)
203 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
204 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
207 static HRESULT WINAPI
BmpFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
208 IWICBitmapSource
*pIThumbnail
)
210 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
211 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
214 static HRESULT
BmpFrameEncode_AllocateBits(BmpFrameEncode
*This
)
218 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
219 return WINCODEC_ERR_WRONGSTATE
;
221 This
->stride
= (((This
->width
* This
->format
->bpp
)+31)/32)*4;
222 This
->bits
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->stride
* This
->height
);
223 if (!This
->bits
) return E_OUTOFMEMORY
;
229 static HRESULT WINAPI
BmpFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
230 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
232 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
235 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
237 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
238 return WINCODEC_ERR_WRONGSTATE
;
240 hr
= BmpFrameEncode_AllocateBits(This
);
241 if (FAILED(hr
)) return hr
;
245 rc
.Width
= This
->width
;
246 rc
.Height
= lineCount
;
248 hr
= copy_pixels(This
->format
->bpp
, pbPixels
, This
->width
, lineCount
, cbStride
,
249 &rc
, This
->stride
, This
->stride
*(This
->height
-This
->lineswritten
),
250 This
->bits
+ This
->stride
*This
->lineswritten
);
253 This
->lineswritten
+= lineCount
;
258 static HRESULT WINAPI
BmpFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
259 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
261 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
263 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
265 if (!This
->initialized
)
266 return WINCODEC_ERR_WRONGSTATE
;
268 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
269 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
270 This
->xres
, This
->yres
);
274 hr
= write_source(iface
, pIBitmapSource
, prc
,
275 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
281 static HRESULT WINAPI
BmpFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
283 BmpFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
284 BITMAPFILEHEADER bfh
;
291 TRACE("(%p)\n", iface
);
293 if (!This
->bits
|| This
->committed
|| This
->height
!= This
->lineswritten
)
294 return WINCODEC_ERR_WRONGSTATE
;
296 bfh
.bfType
= 0x4d42; /* "BM" */
300 bih
.bV5Size
= info_size
= sizeof(BITMAPINFOHEADER
);
301 bih
.bV5Width
= This
->width
;
302 bih
.bV5Height
= -This
->height
; /* top-down bitmap */
304 bih
.bV5BitCount
= This
->format
->bpp
;
305 bih
.bV5Compression
= This
->format
->compression
;
306 bih
.bV5SizeImage
= This
->stride
*This
->height
;
307 bih
.bV5XPelsPerMeter
= (This
->xres
+0.0127) / 0.0254;
308 bih
.bV5YPelsPerMeter
= (This
->yres
+0.0127) / 0.0254;
310 bih
.bV5ClrImportant
= 0;
312 if (This
->format
->compression
== BI_BITFIELDS
)
314 if (This
->format
->alphamask
)
315 bih
.bV5Size
= info_size
= sizeof(BITMAPV4HEADER
);
317 info_size
= sizeof(BITMAPINFOHEADER
)+12;
318 bih
.bV5RedMask
= This
->format
->redmask
;
319 bih
.bV5GreenMask
= This
->format
->greenmask
;
320 bih
.bV5BlueMask
= This
->format
->bluemask
;
321 bih
.bV5AlphaMask
= This
->format
->alphamask
;
322 bih
.bV5CSType
= LCS_DEVICE_RGB
;
325 bfh
.bfSize
= sizeof(BITMAPFILEHEADER
) + info_size
+ bih
.bV5SizeImage
;
326 bfh
.bfOffBits
= sizeof(BITMAPFILEHEADER
) + info_size
;
329 hr
= IStream_Seek(This
->stream
, pos
, STREAM_SEEK_SET
, NULL
);
330 if (FAILED(hr
)) return hr
;
332 hr
= IStream_Write(This
->stream
, &bfh
, sizeof(BITMAPFILEHEADER
), &byteswritten
);
333 if (FAILED(hr
)) return hr
;
334 if (byteswritten
!= sizeof(BITMAPFILEHEADER
)) return E_FAIL
;
336 hr
= IStream_Write(This
->stream
, &bih
, info_size
, &byteswritten
);
337 if (FAILED(hr
)) return hr
;
338 if (byteswritten
!= info_size
) return E_FAIL
;
340 hr
= IStream_Write(This
->stream
, This
->bits
, bih
.bV5SizeImage
, &byteswritten
);
341 if (FAILED(hr
)) return hr
;
342 if (byteswritten
!= bih
.bV5SizeImage
) return E_FAIL
;
344 This
->committed
= TRUE
;
349 static HRESULT WINAPI
BmpFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
350 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
352 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
356 static const IWICBitmapFrameEncodeVtbl BmpFrameEncode_Vtbl
= {
357 BmpFrameEncode_QueryInterface
,
358 BmpFrameEncode_AddRef
,
359 BmpFrameEncode_Release
,
360 BmpFrameEncode_Initialize
,
361 BmpFrameEncode_SetSize
,
362 BmpFrameEncode_SetResolution
,
363 BmpFrameEncode_SetPixelFormat
,
364 BmpFrameEncode_SetColorContexts
,
365 BmpFrameEncode_SetPalette
,
366 BmpFrameEncode_SetThumbnail
,
367 BmpFrameEncode_WritePixels
,
368 BmpFrameEncode_WriteSource
,
369 BmpFrameEncode_Commit
,
370 BmpFrameEncode_GetMetadataQueryWriter
373 typedef struct BmpEncoder
{
374 IWICBitmapEncoder IWICBitmapEncoder_iface
;
377 BmpFrameEncode
*frame
;
380 static inline BmpEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
382 return CONTAINING_RECORD(iface
, BmpEncoder
, IWICBitmapEncoder_iface
);
385 static HRESULT WINAPI
BmpEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
388 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
389 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
391 if (!ppv
) return E_INVALIDARG
;
393 if (IsEqualIID(&IID_IUnknown
, iid
) ||
394 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
396 *ppv
= &This
->IWICBitmapEncoder_iface
;
401 return E_NOINTERFACE
;
404 IUnknown_AddRef((IUnknown
*)*ppv
);
408 static ULONG WINAPI
BmpEncoder_AddRef(IWICBitmapEncoder
*iface
)
410 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
411 ULONG ref
= InterlockedIncrement(&This
->ref
);
413 TRACE("(%p) refcount=%u\n", iface
, ref
);
418 static ULONG WINAPI
BmpEncoder_Release(IWICBitmapEncoder
*iface
)
420 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
421 ULONG ref
= InterlockedDecrement(&This
->ref
);
423 TRACE("(%p) refcount=%u\n", iface
, ref
);
427 if (This
->stream
) IStream_Release(This
->stream
);
428 if (This
->frame
) IWICBitmapFrameEncode_Release(&This
->frame
->IWICBitmapFrameEncode_iface
);
429 HeapFree(GetProcessHeap(), 0, This
);
435 static HRESULT WINAPI
BmpEncoder_Initialize(IWICBitmapEncoder
*iface
,
436 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
438 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
440 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
442 IStream_AddRef(pIStream
);
443 This
->stream
= pIStream
;
448 static HRESULT WINAPI
BmpEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
449 GUID
*pguidContainerFormat
)
451 memcpy(pguidContainerFormat
, &GUID_ContainerFormatBmp
, sizeof(GUID
));
455 static HRESULT WINAPI
BmpEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
456 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
458 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
462 static HRESULT WINAPI
BmpEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
463 UINT cCount
, IWICColorContext
**ppIColorContext
)
465 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
469 static HRESULT WINAPI
BmpEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
471 TRACE("(%p,%p)\n", iface
, pIPalette
);
472 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
475 static HRESULT WINAPI
BmpEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
477 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
478 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
481 static HRESULT WINAPI
BmpEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
483 TRACE("(%p,%p)\n", iface
, pIPreview
);
484 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
487 static HRESULT WINAPI
BmpEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
488 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
490 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
491 BmpFrameEncode
*encode
;
494 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
496 if (This
->frame
) return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
498 if (!This
->stream
) return WINCODEC_ERR_NOTINITIALIZED
;
500 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
501 if (FAILED(hr
)) return hr
;
503 encode
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameEncode
));
506 IPropertyBag2_Release(*ppIEncoderOptions
);
507 *ppIEncoderOptions
= NULL
;
508 return E_OUTOFMEMORY
;
510 encode
->IWICBitmapFrameEncode_iface
.lpVtbl
= &BmpFrameEncode_Vtbl
;
512 IStream_AddRef(This
->stream
);
513 encode
->stream
= This
->stream
;
514 encode
->initialized
= FALSE
;
518 encode
->format
= NULL
;
521 encode
->lineswritten
= 0;
522 encode
->committed
= FALSE
;
524 *ppIFrameEncode
= &encode
->IWICBitmapFrameEncode_iface
;
525 This
->frame
= encode
;
530 static HRESULT WINAPI
BmpEncoder_Commit(IWICBitmapEncoder
*iface
)
532 BmpEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
533 TRACE("(%p)\n", iface
);
535 if (!This
->frame
|| !This
->frame
->committed
) return WINCODEC_ERR_WRONGSTATE
;
540 static HRESULT WINAPI
BmpEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
541 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
543 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
547 static const IWICBitmapEncoderVtbl BmpEncoder_Vtbl
= {
548 BmpEncoder_QueryInterface
,
551 BmpEncoder_Initialize
,
552 BmpEncoder_GetContainerFormat
,
553 BmpEncoder_GetEncoderInfo
,
554 BmpEncoder_SetColorContexts
,
555 BmpEncoder_SetPalette
,
556 BmpEncoder_SetThumbnail
,
557 BmpEncoder_SetPreview
,
558 BmpEncoder_CreateNewFrame
,
560 BmpEncoder_GetMetadataQueryWriter
563 HRESULT
BmpEncoder_CreateInstance(REFIID iid
, void** ppv
)
568 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
572 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(BmpEncoder
));
573 if (!This
) return E_OUTOFMEMORY
;
575 This
->IWICBitmapEncoder_iface
.lpVtbl
= &BmpEncoder_Vtbl
;
580 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
581 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);