wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / windowscodecs / bitmap.c
blob85d0076abf6566683d3879e8e071ce69ca0ccb9c
1 /*
2 * Copyright 2012 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 "objbase.h"
28 #include "wincodec.h"
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
36 /* WARNING: .NET Media Integration Layer (MIL) directly dereferences
37 * BitmapImpl members and depends on its exact layout.
39 typedef struct BitmapImpl {
40 IMILUnknown1 IMILUnknown1_iface;
41 LONG ref;
42 IMILBitmapSource IMILBitmapSource_iface;
43 IWICBitmap IWICBitmap_iface;
44 IMILUnknown2 IMILUnknown2_iface;
45 IWICPalette *palette;
46 int palette_set;
47 LONG lock; /* 0 if not locked, -1 if locked for writing, count if locked for reading */
48 BYTE *data;
49 UINT width, height;
50 UINT stride;
51 UINT bpp;
52 WICPixelFormatGUID pixelformat;
53 double dpix, dpiy;
54 CRITICAL_SECTION cs;
55 } BitmapImpl;
57 typedef struct BitmapLockImpl {
58 IWICBitmapLock IWICBitmapLock_iface;
59 LONG ref;
60 BitmapImpl *parent;
61 UINT width, height;
62 BYTE *data;
63 } BitmapLockImpl;
65 static inline BitmapImpl *impl_from_IWICBitmap(IWICBitmap *iface)
67 return CONTAINING_RECORD(iface, BitmapImpl, IWICBitmap_iface);
70 static inline BitmapImpl *impl_from_IMILBitmapSource(IMILBitmapSource *iface)
72 return CONTAINING_RECORD(iface, BitmapImpl, IMILBitmapSource_iface);
75 static inline BitmapImpl *impl_from_IMILUnknown1(IMILUnknown1 *iface)
77 return CONTAINING_RECORD(iface, BitmapImpl, IMILUnknown1_iface);
80 static inline BitmapImpl *impl_from_IMILUnknown2(IMILUnknown2 *iface)
82 return CONTAINING_RECORD(iface, BitmapImpl, IMILUnknown2_iface);
85 static inline BitmapLockImpl *impl_from_IWICBitmapLock(IWICBitmapLock *iface)
87 return CONTAINING_RECORD(iface, BitmapLockImpl, IWICBitmapLock_iface);
90 static BOOL BitmapImpl_AcquireLock(BitmapImpl *This, int write)
92 if (write)
94 return 0 == InterlockedCompareExchange(&This->lock, -1, 0);
96 else
98 while (1)
100 LONG prev_val = This->lock;
101 if (prev_val == -1)
102 return FALSE;
103 if (prev_val == InterlockedCompareExchange(&This->lock, prev_val+1, prev_val))
104 return TRUE;
109 static void BitmapImpl_ReleaseLock(BitmapImpl *This)
111 while (1)
113 LONG prev_val = This->lock, new_val;
114 if (prev_val == -1)
115 new_val = 0;
116 else
117 new_val = prev_val - 1;
118 if (prev_val == InterlockedCompareExchange(&This->lock, new_val, prev_val))
119 break;
124 static HRESULT WINAPI BitmapLockImpl_QueryInterface(IWICBitmapLock *iface, REFIID iid,
125 void **ppv)
127 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
128 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
130 if (!ppv) return E_INVALIDARG;
132 if (IsEqualIID(&IID_IUnknown, iid) ||
133 IsEqualIID(&IID_IWICBitmapLock, iid))
135 *ppv = &This->IWICBitmapLock_iface;
137 else
139 *ppv = NULL;
140 return E_NOINTERFACE;
143 IUnknown_AddRef((IUnknown*)*ppv);
144 return S_OK;
147 static ULONG WINAPI BitmapLockImpl_AddRef(IWICBitmapLock *iface)
149 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
150 ULONG ref = InterlockedIncrement(&This->ref);
152 TRACE("(%p) refcount=%u\n", iface, ref);
154 return ref;
157 static ULONG WINAPI BitmapLockImpl_Release(IWICBitmapLock *iface)
159 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
160 ULONG ref = InterlockedDecrement(&This->ref);
162 TRACE("(%p) refcount=%u\n", iface, ref);
164 if (ref == 0)
166 BitmapImpl_ReleaseLock(This->parent);
167 IWICBitmap_Release(&This->parent->IWICBitmap_iface);
168 HeapFree(GetProcessHeap(), 0, This);
171 return ref;
174 static HRESULT WINAPI BitmapLockImpl_GetSize(IWICBitmapLock *iface,
175 UINT *puiWidth, UINT *puiHeight)
177 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
178 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
180 if (!puiWidth || !puiHeight)
181 return E_INVALIDARG;
183 *puiWidth = This->width;
184 *puiHeight = This->height;
186 return S_OK;
189 static HRESULT WINAPI BitmapLockImpl_GetStride(IWICBitmapLock *iface,
190 UINT *pcbStride)
192 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
193 TRACE("(%p,%p)\n", iface, pcbStride);
195 if (!pcbStride)
196 return E_INVALIDARG;
198 *pcbStride = This->parent->stride;
200 return S_OK;
203 static HRESULT WINAPI BitmapLockImpl_GetDataPointer(IWICBitmapLock *iface,
204 UINT *pcbBufferSize, BYTE **ppbData)
206 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
207 TRACE("(%p,%p,%p)\n", iface, pcbBufferSize, ppbData);
209 if (!pcbBufferSize || !ppbData)
210 return E_INVALIDARG;
212 *pcbBufferSize = This->parent->stride * (This->height - 1) +
213 ((This->parent->bpp * This->width) + 7)/8;
214 *ppbData = This->data;
216 return S_OK;
219 static HRESULT WINAPI BitmapLockImpl_GetPixelFormat(IWICBitmapLock *iface,
220 WICPixelFormatGUID *pPixelFormat)
222 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
223 TRACE("(%p,%p)\n", iface, pPixelFormat);
225 return IWICBitmap_GetPixelFormat(&This->parent->IWICBitmap_iface, pPixelFormat);
228 static const IWICBitmapLockVtbl BitmapLockImpl_Vtbl = {
229 BitmapLockImpl_QueryInterface,
230 BitmapLockImpl_AddRef,
231 BitmapLockImpl_Release,
232 BitmapLockImpl_GetSize,
233 BitmapLockImpl_GetStride,
234 BitmapLockImpl_GetDataPointer,
235 BitmapLockImpl_GetPixelFormat
238 static HRESULT WINAPI BitmapImpl_QueryInterface(IWICBitmap *iface, REFIID iid,
239 void **ppv)
241 BitmapImpl *This = impl_from_IWICBitmap(iface);
242 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
244 if (!ppv) return E_INVALIDARG;
246 if (IsEqualIID(&IID_IUnknown, iid) ||
247 IsEqualIID(&IID_IWICBitmapSource, iid) ||
248 IsEqualIID(&IID_IWICBitmap, iid))
250 *ppv = &This->IWICBitmap_iface;
252 else if (IsEqualIID(&IID_IMILBitmapSource, iid))
254 *ppv = &This->IMILBitmapSource_iface;
256 else
258 *ppv = NULL;
259 return E_NOINTERFACE;
262 IUnknown_AddRef((IUnknown*)*ppv);
263 return S_OK;
266 static ULONG WINAPI BitmapImpl_AddRef(IWICBitmap *iface)
268 BitmapImpl *This = impl_from_IWICBitmap(iface);
269 ULONG ref = InterlockedIncrement(&This->ref);
271 TRACE("(%p) refcount=%u\n", iface, ref);
273 return ref;
276 static ULONG WINAPI BitmapImpl_Release(IWICBitmap *iface)
278 BitmapImpl *This = impl_from_IWICBitmap(iface);
279 ULONG ref = InterlockedDecrement(&This->ref);
281 TRACE("(%p) refcount=%u\n", iface, ref);
283 if (ref == 0)
285 if (This->palette) IWICPalette_Release(This->palette);
286 This->cs.DebugInfo->Spare[0] = 0;
287 DeleteCriticalSection(&This->cs);
288 HeapFree(GetProcessHeap(), 0, This->data);
289 HeapFree(GetProcessHeap(), 0, This);
292 return ref;
295 static HRESULT WINAPI BitmapImpl_GetSize(IWICBitmap *iface,
296 UINT *puiWidth, UINT *puiHeight)
298 BitmapImpl *This = impl_from_IWICBitmap(iface);
299 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
301 if (!puiWidth || !puiHeight)
302 return E_INVALIDARG;
304 *puiWidth = This->width;
305 *puiHeight = This->height;
307 return S_OK;
310 static HRESULT WINAPI BitmapImpl_GetPixelFormat(IWICBitmap *iface,
311 WICPixelFormatGUID *pPixelFormat)
313 BitmapImpl *This = impl_from_IWICBitmap(iface);
314 TRACE("(%p,%p)\n", iface, pPixelFormat);
316 if (!pPixelFormat)
317 return E_INVALIDARG;
319 memcpy(pPixelFormat, &This->pixelformat, sizeof(GUID));
321 return S_OK;
324 static HRESULT WINAPI BitmapImpl_GetResolution(IWICBitmap *iface,
325 double *pDpiX, double *pDpiY)
327 BitmapImpl *This = impl_from_IWICBitmap(iface);
328 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
330 if (!pDpiX || !pDpiY)
331 return E_INVALIDARG;
333 EnterCriticalSection(&This->cs);
334 *pDpiX = This->dpix;
335 *pDpiY = This->dpiy;
336 LeaveCriticalSection(&This->cs);
338 return S_OK;
341 static HRESULT WINAPI BitmapImpl_CopyPalette(IWICBitmap *iface,
342 IWICPalette *pIPalette)
344 BitmapImpl *This = impl_from_IWICBitmap(iface);
345 TRACE("(%p,%p)\n", iface, pIPalette);
347 if (!This->palette_set)
348 return WINCODEC_ERR_PALETTEUNAVAILABLE;
350 return IWICPalette_InitializeFromPalette(pIPalette, This->palette);
353 static HRESULT WINAPI BitmapImpl_CopyPixels(IWICBitmap *iface,
354 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
356 BitmapImpl *This = impl_from_IWICBitmap(iface);
357 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
359 return copy_pixels(This->bpp, This->data, This->width, This->height,
360 This->stride, prc, cbStride, cbBufferSize, pbBuffer);
363 static HRESULT WINAPI BitmapImpl_Lock(IWICBitmap *iface, const WICRect *prcLock,
364 DWORD flags, IWICBitmapLock **ppILock)
366 BitmapImpl *This = impl_from_IWICBitmap(iface);
367 BitmapLockImpl *result;
368 WICRect rc;
370 TRACE("(%p,%p,%x,%p)\n", iface, prcLock, flags, ppILock);
372 if (!(flags & (WICBitmapLockRead|WICBitmapLockWrite)) || !ppILock)
373 return E_INVALIDARG;
375 if (!prcLock)
377 rc.X = rc.Y = 0;
378 rc.Width = This->width;
379 rc.Height = This->height;
380 prcLock = &rc;
382 else if (prcLock->X >= This->width || prcLock->Y >= This->height ||
383 prcLock->X + prcLock->Width > This->width ||
384 prcLock->Y + prcLock->Height > This->height ||
385 prcLock->Width <= 0 || prcLock->Height <= 0)
386 return E_INVALIDARG;
387 else if (((prcLock->X * This->bpp) % 8) != 0)
389 FIXME("Cannot lock at an X coordinate not at a full byte\n");
390 return E_FAIL;
393 result = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapLockImpl));
394 if (!result)
395 return E_OUTOFMEMORY;
397 if (!BitmapImpl_AcquireLock(This, flags & WICBitmapLockWrite))
399 HeapFree(GetProcessHeap(), 0, result);
400 return WINCODEC_ERR_ALREADYLOCKED;
403 result->IWICBitmapLock_iface.lpVtbl = &BitmapLockImpl_Vtbl;
404 result->ref = 1;
405 result->parent = This;
406 result->width = prcLock->Width;
407 result->height = prcLock->Height;
408 result->data = This->data + This->stride * prcLock->Y +
409 (This->bpp * prcLock->X)/8;
411 IWICBitmap_AddRef(&This->IWICBitmap_iface);
412 *ppILock = &result->IWICBitmapLock_iface;
414 return S_OK;
417 static HRESULT WINAPI BitmapImpl_SetPalette(IWICBitmap *iface, IWICPalette *pIPalette)
419 BitmapImpl *This = impl_from_IWICBitmap(iface);
420 HRESULT hr;
422 TRACE("(%p,%p)\n", iface, pIPalette);
424 if (!This->palette)
426 IWICPalette *new_palette;
427 hr = PaletteImpl_Create(&new_palette);
429 if (FAILED(hr)) return hr;
431 if (InterlockedCompareExchangePointer((void**)&This->palette, new_palette, NULL))
433 /* someone beat us to it */
434 IWICPalette_Release(new_palette);
438 hr = IWICPalette_InitializeFromPalette(This->palette, pIPalette);
440 if (SUCCEEDED(hr))
441 This->palette_set = 1;
443 return S_OK;
446 static HRESULT WINAPI BitmapImpl_SetResolution(IWICBitmap *iface,
447 double dpiX, double dpiY)
449 BitmapImpl *This = impl_from_IWICBitmap(iface);
450 TRACE("(%p,%f,%f)\n", iface, dpiX, dpiY);
452 EnterCriticalSection(&This->cs);
453 This->dpix = dpiX;
454 This->dpiy = dpiY;
455 LeaveCriticalSection(&This->cs);
457 return S_OK;
460 static const IWICBitmapVtbl BitmapImpl_Vtbl = {
461 BitmapImpl_QueryInterface,
462 BitmapImpl_AddRef,
463 BitmapImpl_Release,
464 BitmapImpl_GetSize,
465 BitmapImpl_GetPixelFormat,
466 BitmapImpl_GetResolution,
467 BitmapImpl_CopyPalette,
468 BitmapImpl_CopyPixels,
469 BitmapImpl_Lock,
470 BitmapImpl_SetPalette,
471 BitmapImpl_SetResolution
474 static HRESULT WINAPI IMILBitmapImpl_QueryInterface(IMILBitmapSource *iface, REFIID iid,
475 void **ppv)
477 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
478 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
480 if (!ppv) return E_INVALIDARG;
482 if (IsEqualIID(&IID_IUnknown, iid) ||
483 IsEqualIID(&IID_IMILBitmapSource, iid))
485 IUnknown_AddRef(&This->IMILBitmapSource_iface);
486 *ppv = &This->IMILBitmapSource_iface;
487 return S_OK;
490 *ppv = NULL;
491 return E_NOINTERFACE;
494 static ULONG WINAPI IMILBitmapImpl_AddRef(IMILBitmapSource *iface)
496 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
497 return IWICBitmap_AddRef(&This->IWICBitmap_iface);
500 static ULONG WINAPI IMILBitmapImpl_Release(IMILBitmapSource *iface)
502 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
503 return IWICBitmap_Release(&This->IWICBitmap_iface);
506 static HRESULT WINAPI IMILBitmapImpl_GetSize(IMILBitmapSource *iface,
507 UINT *width, UINT *height)
509 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
510 return IWICBitmap_GetSize(&This->IWICBitmap_iface, width, height);
513 static const struct
515 const GUID *WIC_format;
516 int enum_format;
517 } pixel_fmt_map[] =
519 { &GUID_WICPixelFormatDontCare, 0 },
520 { &GUID_WICPixelFormat1bppIndexed, 1 },
521 { &GUID_WICPixelFormat2bppIndexed, 2 },
522 { &GUID_WICPixelFormat4bppIndexed, 3 },
523 { &GUID_WICPixelFormat8bppIndexed, 4 },
524 { &GUID_WICPixelFormatBlackWhite, 5 },
525 { &GUID_WICPixelFormat2bppGray, 6 },
526 { &GUID_WICPixelFormat4bppGray, 7 },
527 { &GUID_WICPixelFormat8bppGray, 8 },
528 { &GUID_WICPixelFormat16bppBGR555, 9 },
529 { &GUID_WICPixelFormat16bppBGR565, 0x0a },
530 { &GUID_WICPixelFormat16bppGray, 0x0b },
531 { &GUID_WICPixelFormat24bppBGR, 0x0c },
532 { &GUID_WICPixelFormat24bppRGB, 0x0d },
533 { &GUID_WICPixelFormat32bppBGR, 0x0e },
534 { &GUID_WICPixelFormat32bppBGRA, 0x0f },
535 { &GUID_WICPixelFormat32bppPBGRA, 0x10 },
536 { &GUID_WICPixelFormat48bppRGB, 0x15 },
537 { &GUID_WICPixelFormat64bppRGBA, 0x16 },
538 { &GUID_WICPixelFormat64bppPRGBA, 0x17 },
539 { &GUID_WICPixelFormat32bppCMYK, 0x1c }
542 static HRESULT WINAPI IMILBitmapImpl_GetPixelFormat(IMILBitmapSource *iface,
543 int *format)
545 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
546 int i;
548 TRACE("(%p,%p)\n", iface, format);
550 if (!format) return E_INVALIDARG;
552 *format = 0;
554 for (i = 0; i < sizeof(pixel_fmt_map)/sizeof(pixel_fmt_map[0]); i++)
556 if (IsEqualGUID(pixel_fmt_map[i].WIC_format, &This->pixelformat))
558 *format = pixel_fmt_map[i].enum_format;
559 break;
563 return S_OK;
566 static HRESULT WINAPI IMILBitmapImpl_GetResolution(IMILBitmapSource *iface,
567 double *dpix, double *dpiy)
569 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
570 return IWICBitmap_GetResolution(&This->IWICBitmap_iface, dpix, dpiy);
573 static HRESULT WINAPI IMILBitmapImpl_CopyPalette(IMILBitmapSource *iface,
574 IWICPalette *palette)
576 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
577 return IWICBitmap_CopyPalette(&This->IWICBitmap_iface, palette);
580 static HRESULT WINAPI IMILBitmapImpl_CopyPixels(IMILBitmapSource *iface,
581 const WICRect *rc, UINT stride, UINT size, BYTE *buffer)
583 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
584 return IWICBitmap_CopyPixels(&This->IWICBitmap_iface, rc, stride, size, buffer);
587 static HRESULT WINAPI IMILBitmapImpl_UnknownMethod1(IMILBitmapSource *iface, void **ppv)
589 BitmapImpl *This = impl_from_IMILBitmapSource(iface);
591 TRACE("(%p,%p)\n", iface, ppv);
593 if (!ppv) return E_INVALIDARG;
595 IUnknown_AddRef(&This->IMILUnknown1_iface);
596 *ppv = &This->IMILUnknown1_iface;
598 return S_OK;
601 static const IMILBitmapSourceVtbl IMILBitmapImpl_Vtbl =
603 IMILBitmapImpl_QueryInterface,
604 IMILBitmapImpl_AddRef,
605 IMILBitmapImpl_Release,
606 IMILBitmapImpl_GetSize,
607 IMILBitmapImpl_GetPixelFormat,
608 IMILBitmapImpl_GetResolution,
609 IMILBitmapImpl_CopyPalette,
610 IMILBitmapImpl_CopyPixels,
611 IMILBitmapImpl_UnknownMethod1,
614 static HRESULT WINAPI IMILUnknown1Impl_QueryInterface(IMILUnknown1 *iface, REFIID iid,
615 void **ppv)
617 BitmapImpl *This = impl_from_IMILUnknown1(iface);
619 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
621 if (!ppv) return E_INVALIDARG;
623 if (IsEqualIID(&IID_IUnknown, iid))
625 IUnknown_AddRef(&This->IMILUnknown1_iface);
626 *ppv = iface;
627 return S_OK;
630 return IWICBitmap_QueryInterface(&This->IWICBitmap_iface, iid, ppv);
633 static ULONG WINAPI IMILUnknown1Impl_AddRef(IMILUnknown1 *iface)
635 BitmapImpl *This = impl_from_IMILUnknown1(iface);
636 return IWICBitmap_AddRef(&This->IWICBitmap_iface);
639 static ULONG WINAPI IMILUnknown1Impl_Release(IMILUnknown1 *iface)
641 BitmapImpl *This = impl_from_IMILUnknown1(iface);
642 return IWICBitmap_Release(&This->IWICBitmap_iface);
645 static const IMILUnknown1Vtbl IMILUnknown1Impl_Vtbl =
647 IMILUnknown1Impl_QueryInterface,
648 IMILUnknown1Impl_AddRef,
649 IMILUnknown1Impl_Release,
652 static HRESULT WINAPI IMILUnknown2Impl_QueryInterface(IMILUnknown2 *iface, REFIID iid,
653 void **ppv)
655 BitmapImpl *This = impl_from_IMILUnknown2(iface);
657 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
659 if (!ppv) return E_INVALIDARG;
661 if (IsEqualIID(&IID_IUnknown, iid))
663 IUnknown_AddRef(&This->IMILUnknown2_iface);
664 *ppv = iface;
665 return S_OK;
668 return IWICBitmap_QueryInterface(&This->IWICBitmap_iface, iid, ppv);
671 static ULONG WINAPI IMILUnknown2Impl_AddRef(IMILUnknown2 *iface)
673 BitmapImpl *This = impl_from_IMILUnknown2(iface);
674 return IWICBitmap_AddRef(&This->IWICBitmap_iface);
677 static ULONG WINAPI IMILUnknown2Impl_Release(IMILUnknown2 *iface)
679 BitmapImpl *This = impl_from_IMILUnknown2(iface);
680 return IWICBitmap_Release(&This->IWICBitmap_iface);
683 static HRESULT WINAPI IMILUnknown2Impl_UnknownMethod1(IMILUnknown2 *iface, void *arg1, void *arg2)
685 FIXME("(%p,%p,%p): stub\n", iface, arg1, arg2);
686 return E_NOTIMPL;
689 static const IMILUnknown2Vtbl IMILUnknown2Impl_Vtbl =
691 IMILUnknown2Impl_QueryInterface,
692 IMILUnknown2Impl_AddRef,
693 IMILUnknown2Impl_Release,
694 IMILUnknown2Impl_UnknownMethod1,
697 HRESULT BitmapImpl_Create(UINT uiWidth, UINT uiHeight,
698 UINT stride, UINT datasize, BYTE *bits,
699 REFWICPixelFormatGUID pixelFormat, WICBitmapCreateCacheOption option,
700 IWICBitmap **ppIBitmap)
702 HRESULT hr;
703 BitmapImpl *This;
704 BYTE *data;
705 UINT bpp;
707 hr = get_pixelformat_bpp(pixelFormat, &bpp);
708 if (FAILED(hr)) return hr;
710 if (!stride) stride = (((bpp*uiWidth)+31)/32)*4;
711 if (!datasize) datasize = stride * uiHeight;
713 if (datasize < stride * uiHeight) return WINCODEC_ERR_INSUFFICIENTBUFFER;
714 if (stride < ((bpp*uiWidth)+7)/8) return E_INVALIDARG;
716 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapImpl));
717 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, datasize);
718 if (!This || !data)
720 HeapFree(GetProcessHeap(), 0, This);
721 HeapFree(GetProcessHeap(), 0, data);
722 return E_OUTOFMEMORY;
724 if (bits) memcpy(data, bits, datasize);
726 This->IWICBitmap_iface.lpVtbl = &BitmapImpl_Vtbl;
727 This->IMILBitmapSource_iface.lpVtbl = &IMILBitmapImpl_Vtbl;
728 This->IMILUnknown1_iface.lpVtbl = &IMILUnknown1Impl_Vtbl;
729 This->IMILUnknown2_iface.lpVtbl = &IMILUnknown2Impl_Vtbl;
730 This->ref = 1;
731 This->palette = NULL;
732 This->palette_set = 0;
733 This->lock = 0;
734 This->data = data;
735 This->width = uiWidth;
736 This->height = uiHeight;
737 This->stride = stride;
738 This->bpp = bpp;
739 memcpy(&This->pixelformat, pixelFormat, sizeof(GUID));
740 This->dpix = This->dpiy = 0.0;
741 InitializeCriticalSection(&This->cs);
742 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BitmapImpl.lock");
744 *ppIBitmap = &This->IWICBitmap_iface;
746 return S_OK;