wshom.ocx: Implement get_Item() for IWshEnvironment.
[wine.git] / dlls / windowscodecs / bmpencode.c
bloba570884a77b72c1ea267dca25d3d8234afa655fe
1 /*
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 "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "wingdi.h"
29 #include "objbase.h"
30 #include "wincodec.h"
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 struct bmp_pixelformat {
39 const WICPixelFormatGUID *guid;
40 UINT bpp;
41 DWORD compression;
42 DWORD redmask;
43 DWORD greenmask;
44 DWORD bluemask;
45 DWORD alphamask;
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},
53 #if 0
54 /* Windows doesn't seem to support this one. */
55 {&GUID_WICPixelFormat32bppBGRA, 32, BI_BITFIELDS, 0xff0000, 0xff00, 0xff, 0xff000000},
56 #endif
57 {NULL}
60 typedef struct BmpFrameEncode {
61 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
62 LONG ref;
63 IStream *stream;
64 BOOL initialized;
65 UINT width, height;
66 BYTE *bits;
67 const struct bmp_pixelformat *format;
68 double xres, yres;
69 UINT lineswritten;
70 UINT stride;
71 BOOL committed;
72 } BmpFrameEncode;
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,
80 void **ppv)
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;
92 else
94 *ppv = NULL;
95 return E_NOINTERFACE;
98 IUnknown_AddRef((IUnknown*)*ppv);
99 return S_OK;
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);
109 return 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);
119 if (ref == 0)
121 if (This->stream) IStream_Release(This->stream);
122 HeapFree(GetProcessHeap(), 0, This->bits);
123 HeapFree(GetProcessHeap(), 0, This);
126 return ref;
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;
139 return S_OK;
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;
153 return S_OK;
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;
164 This->xres = dpiX;
165 This->yres = dpiY;
167 return S_OK;
170 static HRESULT WINAPI BmpFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
171 WICPixelFormatGUID *pPixelFormat)
173 BmpFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
174 int i;
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)
182 break;
185 if (!formats[i].guid) i = 0;
187 This->format = &formats[i];
188 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
190 return S_OK;
193 static HRESULT WINAPI BmpFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
194 UINT cCount, IWICColorContext **ppIColorContext)
196 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
197 return E_NOTIMPL;
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)
216 if (!This->bits)
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;
226 return S_OK;
229 static HRESULT WINAPI BmpFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
230 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
232 BmpFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
233 HRESULT hr;
234 WICRect rc;
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;
243 rc.X = 0;
244 rc.Y = 0;
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);
252 if (SUCCEEDED(hr))
253 This->lineswritten += lineCount;
255 return hr;
258 static HRESULT WINAPI BmpFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
259 IWICBitmapSource *pIBitmapSource, WICRect *prc)
261 BmpFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
262 HRESULT hr;
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);
272 if (SUCCEEDED(hr))
274 hr = write_source(iface, pIBitmapSource, prc,
275 This->format->guid, This->format->bpp, This->width, This->height);
278 return hr;
281 static HRESULT WINAPI BmpFrameEncode_Commit(IWICBitmapFrameEncode *iface)
283 BmpFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
284 BITMAPFILEHEADER bfh;
285 BITMAPV5HEADER bih;
286 UINT info_size;
287 LARGE_INTEGER pos;
288 ULONG byteswritten;
289 HRESULT hr;
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" */
297 bfh.bfReserved1 = 0;
298 bfh.bfReserved2 = 0;
300 bih.bV5Size = info_size = sizeof(BITMAPINFOHEADER);
301 bih.bV5Width = This->width;
302 bih.bV5Height = -This->height; /* top-down bitmap */
303 bih.bV5Planes = 1;
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;
309 bih.bV5ClrUsed = 0;
310 bih.bV5ClrImportant = 0;
312 if (This->format->compression == BI_BITFIELDS)
314 if (This->format->alphamask)
315 bih.bV5Size = info_size = sizeof(BITMAPV4HEADER);
316 else
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;
328 pos.QuadPart = 0;
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;
346 return S_OK;
349 static HRESULT WINAPI BmpFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
350 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
352 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
353 return E_NOTIMPL;
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;
375 LONG ref;
376 IStream *stream;
377 BmpFrameEncode *frame;
378 } BmpEncoder;
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,
386 void **ppv)
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;
398 else
400 *ppv = NULL;
401 return E_NOINTERFACE;
404 IUnknown_AddRef((IUnknown*)*ppv);
405 return S_OK;
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);
415 return 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);
425 if (ref == 0)
427 if (This->stream) IStream_Release(This->stream);
428 if (This->frame) IWICBitmapFrameEncode_Release(&This->frame->IWICBitmapFrameEncode_iface);
429 HeapFree(GetProcessHeap(), 0, This);
432 return ref;
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;
445 return S_OK;
448 static HRESULT WINAPI BmpEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
449 GUID *pguidContainerFormat)
451 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
452 return S_OK;
455 static HRESULT WINAPI BmpEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
456 IWICBitmapEncoderInfo **ppIEncoderInfo)
458 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
459 return E_NOTIMPL;
462 static HRESULT WINAPI BmpEncoder_SetColorContexts(IWICBitmapEncoder *iface,
463 UINT cCount, IWICColorContext **ppIColorContext)
465 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
466 return E_NOTIMPL;
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;
492 HRESULT hr;
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));
504 if (!encode)
506 IPropertyBag2_Release(*ppIEncoderOptions);
507 *ppIEncoderOptions = NULL;
508 return E_OUTOFMEMORY;
510 encode->IWICBitmapFrameEncode_iface.lpVtbl = &BmpFrameEncode_Vtbl;
511 encode->ref = 2;
512 IStream_AddRef(This->stream);
513 encode->stream = This->stream;
514 encode->initialized = FALSE;
515 encode->width = 0;
516 encode->height = 0;
517 encode->bits = NULL;
518 encode->format = NULL;
519 encode->xres = 0.0;
520 encode->yres = 0.0;
521 encode->lineswritten = 0;
522 encode->committed = FALSE;
524 *ppIFrameEncode = &encode->IWICBitmapFrameEncode_iface;
525 This->frame = encode;
527 return S_OK;
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;
537 return S_OK;
540 static HRESULT WINAPI BmpEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
541 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
543 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
544 return E_NOTIMPL;
547 static const IWICBitmapEncoderVtbl BmpEncoder_Vtbl = {
548 BmpEncoder_QueryInterface,
549 BmpEncoder_AddRef,
550 BmpEncoder_Release,
551 BmpEncoder_Initialize,
552 BmpEncoder_GetContainerFormat,
553 BmpEncoder_GetEncoderInfo,
554 BmpEncoder_SetColorContexts,
555 BmpEncoder_SetPalette,
556 BmpEncoder_SetThumbnail,
557 BmpEncoder_SetPreview,
558 BmpEncoder_CreateNewFrame,
559 BmpEncoder_Commit,
560 BmpEncoder_GetMetadataQueryWriter
563 HRESULT BmpEncoder_CreateInstance(REFIID iid, void** ppv)
565 BmpEncoder *This;
566 HRESULT ret;
568 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
570 *ppv = NULL;
572 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpEncoder));
573 if (!This) return E_OUTOFMEMORY;
575 This->IWICBitmapEncoder_iface.lpVtbl = &BmpEncoder_Vtbl;
576 This->ref = 1;
577 This->stream = NULL;
578 This->frame = NULL;
580 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
581 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
583 return ret;