Sync with trunk r58151 to bring the latest changes from Amine and Timo.
[reactos.git] / dll / win32 / windowscodecs / bitmap.c
1 /*
2 * Copyright 2012 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "wincodec.h"
29
30 #include "wincodecs_private.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35
36 typedef struct BitmapImpl {
37 IWICBitmap IWICBitmap_iface;
38 LONG ref;
39 IWICPalette *palette;
40 int palette_set;
41 LONG lock; /* 0 if not locked, -1 if locked for writing, count if locked for reading */
42 BYTE *data;
43 UINT width, height;
44 UINT stride;
45 UINT bpp;
46 WICPixelFormatGUID pixelformat;
47 double dpix, dpiy;
48 CRITICAL_SECTION cs;
49 } BitmapImpl;
50
51 typedef struct BitmapLockImpl {
52 IWICBitmapLock IWICBitmapLock_iface;
53 LONG ref;
54 BitmapImpl *parent;
55 UINT width, height;
56 BYTE *data;
57 } BitmapLockImpl;
58
59 static inline BitmapImpl *impl_from_IWICBitmap(IWICBitmap *iface)
60 {
61 return CONTAINING_RECORD(iface, BitmapImpl, IWICBitmap_iface);
62 }
63
64 static inline BitmapLockImpl *impl_from_IWICBitmapLock(IWICBitmapLock *iface)
65 {
66 return CONTAINING_RECORD(iface, BitmapLockImpl, IWICBitmapLock_iface);
67 }
68
69 static BOOL BitmapImpl_AcquireLock(BitmapImpl *This, int write)
70 {
71 if (write)
72 {
73 return 0 == InterlockedCompareExchange(&This->lock, -1, 0);
74 }
75 else
76 {
77 while (1)
78 {
79 LONG prev_val = This->lock;
80 if (prev_val == -1)
81 return FALSE;
82 if (prev_val == InterlockedCompareExchange(&This->lock, prev_val+1, prev_val))
83 return TRUE;
84 }
85 }
86 }
87
88 static void BitmapImpl_ReleaseLock(BitmapImpl *This)
89 {
90 while (1)
91 {
92 LONG prev_val = This->lock, new_val;
93 if (prev_val == -1)
94 new_val = 0;
95 else
96 new_val = prev_val - 1;
97 if (prev_val == InterlockedCompareExchange(&This->lock, new_val, prev_val))
98 break;
99 }
100 }
101
102
103 static HRESULT WINAPI BitmapLockImpl_QueryInterface(IWICBitmapLock *iface, REFIID iid,
104 void **ppv)
105 {
106 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
107 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
108
109 if (!ppv) return E_INVALIDARG;
110
111 if (IsEqualIID(&IID_IUnknown, iid) ||
112 IsEqualIID(&IID_IWICBitmapLock, iid))
113 {
114 *ppv = &This->IWICBitmapLock_iface;
115 }
116 else
117 {
118 *ppv = NULL;
119 return E_NOINTERFACE;
120 }
121
122 IUnknown_AddRef((IUnknown*)*ppv);
123 return S_OK;
124 }
125
126 static ULONG WINAPI BitmapLockImpl_AddRef(IWICBitmapLock *iface)
127 {
128 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
129 ULONG ref = InterlockedIncrement(&This->ref);
130
131 TRACE("(%p) refcount=%u\n", iface, ref);
132
133 return ref;
134 }
135
136 static ULONG WINAPI BitmapLockImpl_Release(IWICBitmapLock *iface)
137 {
138 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
139 ULONG ref = InterlockedDecrement(&This->ref);
140
141 TRACE("(%p) refcount=%u\n", iface, ref);
142
143 if (ref == 0)
144 {
145 BitmapImpl_ReleaseLock(This->parent);
146 IWICBitmap_Release(&This->parent->IWICBitmap_iface);
147 HeapFree(GetProcessHeap(), 0, This);
148 }
149
150 return ref;
151 }
152
153 static HRESULT WINAPI BitmapLockImpl_GetSize(IWICBitmapLock *iface,
154 UINT *puiWidth, UINT *puiHeight)
155 {
156 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
157 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
158
159 if (!puiWidth || !puiHeight)
160 return E_INVALIDARG;
161
162 *puiWidth = This->width;
163 *puiHeight = This->height;
164
165 return S_OK;
166 }
167
168 static HRESULT WINAPI BitmapLockImpl_GetStride(IWICBitmapLock *iface,
169 UINT *pcbStride)
170 {
171 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
172 TRACE("(%p,%p)\n", iface, pcbStride);
173
174 if (!pcbStride)
175 return E_INVALIDARG;
176
177 *pcbStride = This->parent->stride;
178
179 return S_OK;
180 }
181
182 static HRESULT WINAPI BitmapLockImpl_GetDataPointer(IWICBitmapLock *iface,
183 UINT *pcbBufferSize, BYTE **ppbData)
184 {
185 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
186 TRACE("(%p,%p,%p)\n", iface, pcbBufferSize, ppbData);
187
188 if (!pcbBufferSize || !ppbData)
189 return E_INVALIDARG;
190
191 *pcbBufferSize = This->parent->stride * (This->height - 1) +
192 ((This->parent->bpp * This->width) + 7)/8;
193 *ppbData = This->data;
194
195 return S_OK;
196 }
197
198 static HRESULT WINAPI BitmapLockImpl_GetPixelFormat(IWICBitmapLock *iface,
199 WICPixelFormatGUID *pPixelFormat)
200 {
201 BitmapLockImpl *This = impl_from_IWICBitmapLock(iface);
202 TRACE("(%p,%p)\n", iface, pPixelFormat);
203
204 return IWICBitmap_GetPixelFormat(&This->parent->IWICBitmap_iface, pPixelFormat);
205 }
206
207 static const IWICBitmapLockVtbl BitmapLockImpl_Vtbl = {
208 BitmapLockImpl_QueryInterface,
209 BitmapLockImpl_AddRef,
210 BitmapLockImpl_Release,
211 BitmapLockImpl_GetSize,
212 BitmapLockImpl_GetStride,
213 BitmapLockImpl_GetDataPointer,
214 BitmapLockImpl_GetPixelFormat
215 };
216
217 static HRESULT WINAPI BitmapImpl_QueryInterface(IWICBitmap *iface, REFIID iid,
218 void **ppv)
219 {
220 BitmapImpl *This = impl_from_IWICBitmap(iface);
221 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
222
223 if (!ppv) return E_INVALIDARG;
224
225 if (IsEqualIID(&IID_IUnknown, iid) ||
226 IsEqualIID(&IID_IWICBitmapSource, iid) ||
227 IsEqualIID(&IID_IWICBitmap, iid))
228 {
229 *ppv = &This->IWICBitmap_iface;
230 }
231 else
232 {
233 *ppv = NULL;
234 return E_NOINTERFACE;
235 }
236
237 IUnknown_AddRef((IUnknown*)*ppv);
238 return S_OK;
239 }
240
241 static ULONG WINAPI BitmapImpl_AddRef(IWICBitmap *iface)
242 {
243 BitmapImpl *This = impl_from_IWICBitmap(iface);
244 ULONG ref = InterlockedIncrement(&This->ref);
245
246 TRACE("(%p) refcount=%u\n", iface, ref);
247
248 return ref;
249 }
250
251 static ULONG WINAPI BitmapImpl_Release(IWICBitmap *iface)
252 {
253 BitmapImpl *This = impl_from_IWICBitmap(iface);
254 ULONG ref = InterlockedDecrement(&This->ref);
255
256 TRACE("(%p) refcount=%u\n", iface, ref);
257
258 if (ref == 0)
259 {
260 if (This->palette) IWICPalette_Release(This->palette);
261 This->cs.DebugInfo->Spare[0] = 0;
262 DeleteCriticalSection(&This->cs);
263 HeapFree(GetProcessHeap(), 0, This->data);
264 HeapFree(GetProcessHeap(), 0, This);
265 }
266
267 return ref;
268 }
269
270 static HRESULT WINAPI BitmapImpl_GetSize(IWICBitmap *iface,
271 UINT *puiWidth, UINT *puiHeight)
272 {
273 BitmapImpl *This = impl_from_IWICBitmap(iface);
274 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
275
276 if (!puiWidth || !puiHeight)
277 return E_INVALIDARG;
278
279 *puiWidth = This->width;
280 *puiHeight = This->height;
281
282 return S_OK;
283 }
284
285 static HRESULT WINAPI BitmapImpl_GetPixelFormat(IWICBitmap *iface,
286 WICPixelFormatGUID *pPixelFormat)
287 {
288 BitmapImpl *This = impl_from_IWICBitmap(iface);
289 TRACE("(%p,%p)\n", iface, pPixelFormat);
290
291 if (!pPixelFormat)
292 return E_INVALIDARG;
293
294 memcpy(pPixelFormat, &This->pixelformat, sizeof(GUID));
295
296 return S_OK;
297 }
298
299 static HRESULT WINAPI BitmapImpl_GetResolution(IWICBitmap *iface,
300 double *pDpiX, double *pDpiY)
301 {
302 BitmapImpl *This = impl_from_IWICBitmap(iface);
303 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
304
305 if (!pDpiX || !pDpiY)
306 return E_INVALIDARG;
307
308 EnterCriticalSection(&This->cs);
309 *pDpiX = This->dpix;
310 *pDpiY = This->dpiy;
311 LeaveCriticalSection(&This->cs);
312
313 return S_OK;
314 }
315
316 static HRESULT WINAPI BitmapImpl_CopyPalette(IWICBitmap *iface,
317 IWICPalette *pIPalette)
318 {
319 BitmapImpl *This = impl_from_IWICBitmap(iface);
320 TRACE("(%p,%p)\n", iface, pIPalette);
321
322 if (!This->palette_set)
323 return WINCODEC_ERR_PALETTEUNAVAILABLE;
324
325 return IWICPalette_InitializeFromPalette(pIPalette, This->palette);
326 }
327
328 static HRESULT WINAPI BitmapImpl_CopyPixels(IWICBitmap *iface,
329 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
330 {
331 BitmapImpl *This = impl_from_IWICBitmap(iface);
332 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
333
334 return copy_pixels(This->bpp, This->data, This->width, This->height,
335 This->stride, prc, cbStride, cbBufferSize, pbBuffer);
336 }
337
338 static HRESULT WINAPI BitmapImpl_Lock(IWICBitmap *iface, const WICRect *prcLock,
339 DWORD flags, IWICBitmapLock **ppILock)
340 {
341 BitmapImpl *This = impl_from_IWICBitmap(iface);
342 BitmapLockImpl *result;
343 WICRect rc;
344
345 TRACE("(%p,%p,%x,%p)\n", iface, prcLock, flags, ppILock);
346
347 if (!(flags & (WICBitmapLockRead|WICBitmapLockWrite)) || !ppILock)
348 return E_INVALIDARG;
349
350 if (!prcLock)
351 {
352 rc.X = rc.Y = 0;
353 rc.Width = This->width;
354 rc.Height = This->height;
355 prcLock = &rc;
356 }
357 else if (prcLock->X >= This->width || prcLock->Y >= This->height ||
358 prcLock->X + prcLock->Width > This->width ||
359 prcLock->Y + prcLock->Height > This->height ||
360 prcLock->Width <= 0 || prcLock->Height <= 0)
361 return E_INVALIDARG;
362 else if (((prcLock->X * This->bpp) % 8) != 0)
363 {
364 FIXME("Cannot lock at an X coordinate not at a full byte\n");
365 return E_FAIL;
366 }
367
368 result = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapLockImpl));
369 if (!result)
370 return E_OUTOFMEMORY;
371
372 if (!BitmapImpl_AcquireLock(This, flags & WICBitmapLockWrite))
373 {
374 HeapFree(GetProcessHeap(), 0, result);
375 return WINCODEC_ERR_ALREADYLOCKED;
376 }
377
378 result->IWICBitmapLock_iface.lpVtbl = &BitmapLockImpl_Vtbl;
379 result->ref = 1;
380 result->parent = This;
381 result->width = prcLock->Width;
382 result->height = prcLock->Height;
383 result->data = This->data + This->stride * prcLock->Y +
384 (This->bpp * prcLock->X)/8;
385
386 IWICBitmap_AddRef(&This->IWICBitmap_iface);
387 *ppILock = &result->IWICBitmapLock_iface;
388
389 return S_OK;
390 }
391
392 static HRESULT WINAPI BitmapImpl_SetPalette(IWICBitmap *iface, IWICPalette *pIPalette)
393 {
394 BitmapImpl *This = impl_from_IWICBitmap(iface);
395 HRESULT hr;
396
397 TRACE("(%p,%p)\n", iface, pIPalette);
398
399 if (!This->palette)
400 {
401 IWICPalette *new_palette;
402 hr = PaletteImpl_Create(&new_palette);
403
404 if (FAILED(hr)) return hr;
405
406 if (InterlockedCompareExchangePointer((void**)&This->palette, new_palette, NULL))
407 {
408 /* someone beat us to it */
409 IWICPalette_Release(new_palette);
410 }
411 }
412
413 hr = IWICPalette_InitializeFromPalette(This->palette, pIPalette);
414
415 if (SUCCEEDED(hr))
416 This->palette_set = 1;
417
418 return S_OK;
419 }
420
421 static HRESULT WINAPI BitmapImpl_SetResolution(IWICBitmap *iface,
422 double dpiX, double dpiY)
423 {
424 BitmapImpl *This = impl_from_IWICBitmap(iface);
425 TRACE("(%p,%f,%f)\n", iface, dpiX, dpiY);
426
427 EnterCriticalSection(&This->cs);
428 This->dpix = dpiX;
429 This->dpiy = dpiY;
430 LeaveCriticalSection(&This->cs);
431
432 return S_OK;
433 }
434
435 static const IWICBitmapVtbl BitmapImpl_Vtbl = {
436 BitmapImpl_QueryInterface,
437 BitmapImpl_AddRef,
438 BitmapImpl_Release,
439 BitmapImpl_GetSize,
440 BitmapImpl_GetPixelFormat,
441 BitmapImpl_GetResolution,
442 BitmapImpl_CopyPalette,
443 BitmapImpl_CopyPixels,
444 BitmapImpl_Lock,
445 BitmapImpl_SetPalette,
446 BitmapImpl_SetResolution
447 };
448
449 HRESULT BitmapImpl_Create(UINT uiWidth, UINT uiHeight,
450 REFWICPixelFormatGUID pixelFormat, WICBitmapCreateCacheOption option,
451 IWICBitmap **ppIBitmap)
452 {
453 HRESULT hr;
454 BitmapImpl *This;
455 UINT bpp, stride, datasize;
456 BYTE *data;
457
458 hr = get_pixelformat_bpp(pixelFormat, &bpp);
459 if (FAILED(hr)) return hr;
460
461 stride = (((bpp*uiWidth)+31)/32)*4;
462 datasize = stride * uiHeight;
463
464 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapImpl));
465 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, datasize);
466 if (!This || !data)
467 {
468 HeapFree(GetProcessHeap(), 0, This);
469 HeapFree(GetProcessHeap(), 0, data);
470 return E_OUTOFMEMORY;
471 }
472
473 This->IWICBitmap_iface.lpVtbl = &BitmapImpl_Vtbl;
474 This->ref = 1;
475 This->palette = NULL;
476 This->palette_set = 0;
477 This->lock = 0;
478 This->data = data;
479 This->width = uiWidth;
480 This->height = uiHeight;
481 This->stride = stride;
482 This->bpp = bpp;
483 memcpy(&This->pixelformat, pixelFormat, sizeof(GUID));
484 This->dpix = This->dpiy = 0.0;
485 InitializeCriticalSection(&This->cs);
486 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BitmapImpl.lock");
487
488 *ppIBitmap = &This->IWICBitmap_iface;
489
490 return S_OK;
491 }