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