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))
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);
264 WICPixelFormatGUID guid;
265 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
267 if (!This->initialized || !This->width || !This->height)
268 return WINCODEC_ERR_WRONGSTATE;
272 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
273 if (FAILED(hr)) return hr;
274 hr = BmpFrameEncode_SetPixelFormat(iface, &guid);
275 if (FAILED(hr)) return hr;
278 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
279 if (FAILED(hr)) return hr;
280 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
282 /* should use WICConvertBitmapSource to convert, but that's unimplemented */
283 ERR("format %s unsupported\n", debugstr_guid(&guid));
287 if (This->xres == 0.0 || This->yres == 0.0)
290 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
291 if (FAILED(hr)) return hr;
292 hr = BmpFrameEncode_SetResolution(iface, xres, yres);
293 if (FAILED(hr)) return hr;
299 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
300 if (FAILED(hr)) return hr;
308 if (prc->Width != This->width) return E_INVALIDARG;
310 hr = BmpFrameEncode_AllocateBits(This);
311 if (FAILED(hr)) return hr;
313 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, This->stride,
314 This->stride*(This->height-This->lineswritten),
315 This->bits + This->stride*This->lineswritten);
317 This->lineswritten += rc.Height;
322 static HRESULT WINAPI BmpFrameEncode_Commit(IWICBitmapFrameEncode *iface)
324 BmpFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
325 BITMAPFILEHEADER bfh;
332 TRACE("(%p)\n", iface);
334 if (!This->bits || This->committed || This->height != This->lineswritten)
335 return WINCODEC_ERR_WRONGSTATE;
337 bfh.bfType = 0x4d42; /* "BM" */
341 bih.bV5Size = info_size = sizeof(BITMAPINFOHEADER);
342 bih.bV5Width = This->width;
343 bih.bV5Height = -This->height; /* top-down bitmap */
345 bih.bV5BitCount = This->format->bpp;
346 bih.bV5Compression = This->format->compression;
347 bih.bV5SizeImage = This->stride*This->height;
348 bih.bV5XPelsPerMeter = (This->xres+0.0127) / 0.0254;
349 bih.bV5YPelsPerMeter = (This->yres+0.0127) / 0.0254;
351 bih.bV5ClrImportant = 0;
353 if (This->format->compression == BI_BITFIELDS)
355 if (This->format->alphamask)
356 bih.bV5Size = info_size = sizeof(BITMAPV4HEADER);
358 info_size = sizeof(BITMAPINFOHEADER)+12;
359 bih.bV5RedMask = This->format->redmask;
360 bih.bV5GreenMask = This->format->greenmask;
361 bih.bV5BlueMask = This->format->bluemask;
362 bih.bV5AlphaMask = This->format->alphamask;
363 bih.bV5AlphaMask = LCS_DEVICE_RGB;
366 bfh.bfSize = sizeof(BITMAPFILEHEADER) + info_size + bih.bV5SizeImage;
367 bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + info_size;
370 hr = IStream_Seek(This->stream, pos, STREAM_SEEK_SET, NULL);
371 if (FAILED(hr)) return hr;
373 hr = IStream_Write(This->stream, &bfh, sizeof(BITMAPFILEHEADER), &byteswritten);
374 if (FAILED(hr)) return hr;
375 if (byteswritten != sizeof(BITMAPFILEHEADER)) return E_FAIL;
377 hr = IStream_Write(This->stream, &bih, info_size, &byteswritten);
378 if (FAILED(hr)) return hr;
379 if (byteswritten != info_size) return E_FAIL;
381 hr = IStream_Write(This->stream, This->bits, bih.bV5SizeImage, &byteswritten);
382 if (FAILED(hr)) return hr;
383 if (byteswritten != bih.bV5SizeImage) return E_FAIL;
385 This->committed = TRUE;
390 static HRESULT WINAPI BmpFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
391 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
393 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
397 static const IWICBitmapFrameEncodeVtbl BmpFrameEncode_Vtbl = {
398 BmpFrameEncode_QueryInterface,
399 BmpFrameEncode_AddRef,
400 BmpFrameEncode_Release,
401 BmpFrameEncode_Initialize,
402 BmpFrameEncode_SetSize,
403 BmpFrameEncode_SetResolution,
404 BmpFrameEncode_SetPixelFormat,
405 BmpFrameEncode_SetColorContexts,
406 BmpFrameEncode_SetPalette,
407 BmpFrameEncode_SetThumbnail,
408 BmpFrameEncode_WritePixels,
409 BmpFrameEncode_WriteSource,
410 BmpFrameEncode_Commit,
411 BmpFrameEncode_GetMetadataQueryWriter
414 typedef struct BmpEncoder {
415 const IWICBitmapEncoderVtbl *lpVtbl;
418 BmpFrameEncode *frame;
421 static HRESULT WINAPI BmpEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
424 BmpEncoder *This = (BmpEncoder*)iface;
425 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
427 if (!ppv) return E_INVALIDARG;
429 if (IsEqualIID(&IID_IUnknown, iid) ||
430 IsEqualIID(&IID_IWICBitmapEncoder, iid))
437 return E_NOINTERFACE;
440 IUnknown_AddRef((IUnknown*)*ppv);
444 static ULONG WINAPI BmpEncoder_AddRef(IWICBitmapEncoder *iface)
446 BmpEncoder *This = (BmpEncoder*)iface;
447 ULONG ref = InterlockedIncrement(&This->ref);
449 TRACE("(%p) refcount=%u\n", iface, ref);
454 static ULONG WINAPI BmpEncoder_Release(IWICBitmapEncoder *iface)
456 BmpEncoder *This = (BmpEncoder*)iface;
457 ULONG ref = InterlockedDecrement(&This->ref);
459 TRACE("(%p) refcount=%u\n", iface, ref);
463 if (This->stream) IStream_Release(This->stream);
464 if (This->frame) IWICBitmapFrameEncode_Release(&This->frame->IWICBitmapFrameEncode_iface);
465 HeapFree(GetProcessHeap(), 0, This);
471 static HRESULT WINAPI BmpEncoder_Initialize(IWICBitmapEncoder *iface,
472 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
474 BmpEncoder *This = (BmpEncoder*)iface;
476 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
478 IStream_AddRef(pIStream);
479 This->stream = pIStream;
484 static HRESULT WINAPI BmpEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
485 GUID *pguidContainerFormat)
487 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
491 static HRESULT WINAPI BmpEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
492 IWICBitmapEncoderInfo **ppIEncoderInfo)
494 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
498 static HRESULT WINAPI BmpEncoder_SetColorContexts(IWICBitmapEncoder *iface,
499 UINT cCount, IWICColorContext **ppIColorContext)
501 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
505 static HRESULT WINAPI BmpEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
507 TRACE("(%p,%p)\n", iface, pIPalette);
508 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
511 static HRESULT WINAPI BmpEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
513 TRACE("(%p,%p)\n", iface, pIThumbnail);
514 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
517 static HRESULT WINAPI BmpEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
519 TRACE("(%p,%p)\n", iface, pIPreview);
520 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
523 static HRESULT WINAPI BmpEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
524 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
526 BmpEncoder *This = (BmpEncoder*)iface;
527 BmpFrameEncode *encode;
530 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
532 if (This->frame) return WINCODEC_ERR_UNSUPPORTEDOPERATION;
534 if (!This->stream) return WINCODEC_ERR_NOTINITIALIZED;
536 hr = CreatePropertyBag2(ppIEncoderOptions);
537 if (FAILED(hr)) return hr;
539 encode = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameEncode));
542 IPropertyBag2_Release(*ppIEncoderOptions);
543 *ppIEncoderOptions = NULL;
544 return E_OUTOFMEMORY;
546 encode->IWICBitmapFrameEncode_iface.lpVtbl = &BmpFrameEncode_Vtbl;
548 IStream_AddRef(This->stream);
549 encode->stream = This->stream;
550 encode->initialized = FALSE;
554 encode->format = NULL;
557 encode->lineswritten = 0;
558 encode->committed = FALSE;
560 *ppIFrameEncode = &encode->IWICBitmapFrameEncode_iface;
561 This->frame = encode;
566 static HRESULT WINAPI BmpEncoder_Commit(IWICBitmapEncoder *iface)
568 BmpEncoder *This = (BmpEncoder*)iface;
569 TRACE("(%p)\n", iface);
571 if (!This->frame || !This->frame->committed) return WINCODEC_ERR_WRONGSTATE;
576 static HRESULT WINAPI BmpEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
577 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
579 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
583 static const IWICBitmapEncoderVtbl BmpEncoder_Vtbl = {
584 BmpEncoder_QueryInterface,
587 BmpEncoder_Initialize,
588 BmpEncoder_GetContainerFormat,
589 BmpEncoder_GetEncoderInfo,
590 BmpEncoder_SetColorContexts,
591 BmpEncoder_SetPalette,
592 BmpEncoder_SetThumbnail,
593 BmpEncoder_SetPreview,
594 BmpEncoder_CreateNewFrame,
596 BmpEncoder_GetMetadataQueryWriter
599 HRESULT BmpEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
604 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
608 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
610 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpEncoder));
611 if (!This) return E_OUTOFMEMORY;
613 This->lpVtbl = &BmpEncoder_Vtbl;
618 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
619 IUnknown_Release((IUnknown*)This);