usp10: Avoid leaking "open_stack" and "stack_index" in case of 0 "pair_count" in...
[wine.git] / dlls / windowscodecs / scaler.c
blobebcc790f75d45bc9bb9463d26cba8857226a0e7d
1 /*
2 * Copyright 2010 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"
29 #include "wincodecs_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35 typedef struct BitmapScaler {
36 IWICBitmapScaler IWICBitmapScaler_iface;
37 LONG ref;
38 IWICBitmapSource *source;
39 UINT width, height;
40 UINT src_width, src_height;
41 WICBitmapInterpolationMode mode;
42 UINT bpp;
43 void (*fn_get_required_source_rect)(struct BitmapScaler*,UINT,UINT,WICRect*);
44 void (*fn_copy_scanline)(struct BitmapScaler*,UINT,UINT,UINT,BYTE**,UINT,UINT,BYTE*);
45 CRITICAL_SECTION lock; /* must be held when initialized */
46 } BitmapScaler;
48 static inline BitmapScaler *impl_from_IWICBitmapScaler(IWICBitmapScaler *iface)
50 return CONTAINING_RECORD(iface, BitmapScaler, IWICBitmapScaler_iface);
53 static HRESULT WINAPI BitmapScaler_QueryInterface(IWICBitmapScaler *iface, REFIID iid,
54 void **ppv)
56 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
57 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
59 if (!ppv) return E_INVALIDARG;
61 if (IsEqualIID(&IID_IUnknown, iid) ||
62 IsEqualIID(&IID_IWICBitmapSource, iid) ||
63 IsEqualIID(&IID_IWICBitmapScaler, iid))
65 *ppv = &This->IWICBitmapScaler_iface;
67 else
69 *ppv = NULL;
70 return E_NOINTERFACE;
73 IUnknown_AddRef((IUnknown*)*ppv);
74 return S_OK;
77 static ULONG WINAPI BitmapScaler_AddRef(IWICBitmapScaler *iface)
79 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
80 ULONG ref = InterlockedIncrement(&This->ref);
82 TRACE("(%p) refcount=%u\n", iface, ref);
84 return ref;
87 static ULONG WINAPI BitmapScaler_Release(IWICBitmapScaler *iface)
89 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
90 ULONG ref = InterlockedDecrement(&This->ref);
92 TRACE("(%p) refcount=%u\n", iface, ref);
94 if (ref == 0)
96 This->lock.DebugInfo->Spare[0] = 0;
97 DeleteCriticalSection(&This->lock);
98 if (This->source) IWICBitmapSource_Release(This->source);
99 HeapFree(GetProcessHeap(), 0, This);
102 return ref;
105 static HRESULT WINAPI BitmapScaler_GetSize(IWICBitmapScaler *iface,
106 UINT *puiWidth, UINT *puiHeight)
108 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
109 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
111 if (!puiWidth || !puiHeight)
112 return E_INVALIDARG;
114 if (!This->source)
115 return WINCODEC_ERR_WRONGSTATE;
117 *puiWidth = This->width;
118 *puiHeight = This->height;
120 return S_OK;
123 static HRESULT WINAPI BitmapScaler_GetPixelFormat(IWICBitmapScaler *iface,
124 WICPixelFormatGUID *pPixelFormat)
126 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
127 TRACE("(%p,%p)\n", iface, pPixelFormat);
129 if (!pPixelFormat)
130 return E_INVALIDARG;
132 if (!This->source)
133 return WINCODEC_ERR_WRONGSTATE;
135 return IWICBitmapSource_GetPixelFormat(This->source, pPixelFormat);
138 static HRESULT WINAPI BitmapScaler_GetResolution(IWICBitmapScaler *iface,
139 double *pDpiX, double *pDpiY)
141 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
142 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
144 if (!pDpiX || !pDpiY)
145 return E_INVALIDARG;
147 if (!This->source)
148 return WINCODEC_ERR_WRONGSTATE;
150 return IWICBitmapSource_GetResolution(This->source, pDpiX, pDpiY);
153 static HRESULT WINAPI BitmapScaler_CopyPalette(IWICBitmapScaler *iface,
154 IWICPalette *pIPalette)
156 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
157 TRACE("(%p,%p)\n", iface, pIPalette);
159 if (!pIPalette)
160 return E_INVALIDARG;
162 if (!This->source)
163 return WINCODEC_ERR_WRONGSTATE;
165 return IWICBitmapSource_CopyPalette(This->source, pIPalette);
168 static void NearestNeighbor_GetRequiredSourceRect(BitmapScaler *This,
169 UINT x, UINT y, WICRect *src_rect)
171 src_rect->X = x * This->src_width / This->width;
172 src_rect->Y = y * This->src_height / This->height;
173 src_rect->Width = src_rect->Height = 1;
176 static void NearestNeighbor_CopyScanline(BitmapScaler *This,
177 UINT dst_x, UINT dst_y, UINT dst_width,
178 BYTE **src_data, UINT src_data_x, UINT src_data_y, BYTE *pbBuffer)
180 UINT i;
181 UINT bytesperpixel = This->bpp/8;
182 UINT src_x, src_y;
184 src_y = dst_y * This->src_height / This->height - src_data_y;
186 for (i=0; i<dst_width; i++)
188 src_x = (dst_x + i) * This->src_width / This->width - src_data_x;
189 memcpy(pbBuffer + bytesperpixel * i, src_data[src_y] + bytesperpixel * src_x, bytesperpixel);
193 static HRESULT WINAPI BitmapScaler_CopyPixels(IWICBitmapScaler *iface,
194 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
196 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
197 HRESULT hr;
198 WICRect dest_rect;
199 WICRect src_rect_ul, src_rect_br, src_rect;
200 BYTE **src_rows;
201 BYTE *src_bits;
202 ULONG bytesperrow;
203 ULONG src_bytesperrow;
204 ULONG buffer_size;
205 UINT y;
207 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
209 EnterCriticalSection(&This->lock);
211 if (!This->source)
213 hr = WINCODEC_ERR_WRONGSTATE;
214 goto end;
217 if (prc)
218 dest_rect = *prc;
219 else
221 dest_rect.X = dest_rect.Y = 0;
222 dest_rect.Width = This->width;
223 dest_rect.Height = This->height;
226 if (dest_rect.X < 0 || dest_rect.Y < 0 ||
227 dest_rect.X+dest_rect.Width > This->width|| dest_rect.Y+dest_rect.Height > This->height)
229 hr = E_INVALIDARG;
230 goto end;
233 bytesperrow = ((This->bpp * dest_rect.Width)+7)/8;
235 if (cbStride < bytesperrow)
237 hr = E_INVALIDARG;
238 goto end;
241 if ((cbStride * dest_rect.Height) > cbBufferSize)
243 hr = E_INVALIDARG;
244 goto end;
247 /* MSDN recommends calling CopyPixels once for each scanline from top to
248 * bottom, and claims codecs optimize for this. Ideally, when called in this
249 * way, we should avoid requesting a scanline from the source more than
250 * once, by saving the data that will be useful for the next scanline after
251 * the call returns. The GetRequiredSourceRect/CopyScanline functions are
252 * designed to make it possible to do this in a generic way, but for now we
253 * just grab all the data we need in each call. */
255 This->fn_get_required_source_rect(This, dest_rect.X, dest_rect.Y, &src_rect_ul);
256 This->fn_get_required_source_rect(This, dest_rect.X+dest_rect.Width-1,
257 dest_rect.Y+dest_rect.Height-1, &src_rect_br);
259 src_rect.X = src_rect_ul.X;
260 src_rect.Y = src_rect_ul.Y;
261 src_rect.Width = src_rect_br.Width + src_rect_br.X - src_rect_ul.X;
262 src_rect.Height = src_rect_br.Height + src_rect_br.Y - src_rect_ul.Y;
264 src_bytesperrow = (src_rect.Width * This->bpp + 7)/8;
265 buffer_size = src_bytesperrow * src_rect.Height;
267 src_rows = HeapAlloc(GetProcessHeap(), 0, sizeof(BYTE*) * src_rect.Height);
268 src_bits = HeapAlloc(GetProcessHeap(), 0, buffer_size);
270 if (!src_rows || !src_bits)
272 HeapFree(GetProcessHeap(), 0, src_rows);
273 HeapFree(GetProcessHeap(), 0, src_bits);
274 hr = E_OUTOFMEMORY;
275 goto end;
278 for (y=0; y<src_rect.Height; y++)
279 src_rows[y] = src_bits + y * src_bytesperrow;
281 hr = IWICBitmapSource_CopyPixels(This->source, &src_rect, src_bytesperrow,
282 buffer_size, src_bits);
284 if (SUCCEEDED(hr))
286 for (y=0; y < dest_rect.Height; y++)
288 This->fn_copy_scanline(This, dest_rect.X, dest_rect.Y+y, dest_rect.Width,
289 src_rows, src_rect.X, src_rect.Y, pbBuffer + cbStride * y);
293 HeapFree(GetProcessHeap(), 0, src_rows);
294 HeapFree(GetProcessHeap(), 0, src_bits);
296 end:
297 LeaveCriticalSection(&This->lock);
299 return hr;
302 static HRESULT WINAPI BitmapScaler_Initialize(IWICBitmapScaler *iface,
303 IWICBitmapSource *pISource, UINT uiWidth, UINT uiHeight,
304 WICBitmapInterpolationMode mode)
306 BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
307 HRESULT hr;
308 GUID src_pixelformat;
310 TRACE("(%p,%p,%u,%u,%u)\n", iface, pISource, uiWidth, uiHeight, mode);
312 EnterCriticalSection(&This->lock);
314 if (This->source)
316 hr = WINCODEC_ERR_WRONGSTATE;
317 goto end;
320 This->width = uiWidth;
321 This->height = uiHeight;
322 This->mode = mode;
324 hr = IWICBitmapSource_GetSize(pISource, &This->src_width, &This->src_height);
326 if (SUCCEEDED(hr))
327 hr = IWICBitmapSource_GetPixelFormat(pISource, &src_pixelformat);
329 if (SUCCEEDED(hr))
331 hr = get_pixelformat_bpp(&src_pixelformat, &This->bpp);
334 if (SUCCEEDED(hr))
336 switch (mode)
338 default:
339 FIXME("unsupported mode %i\n", mode);
340 /* fall-through */
341 case WICBitmapInterpolationModeNearestNeighbor:
342 if ((This->bpp % 8) == 0)
344 IWICBitmapSource_AddRef(pISource);
345 This->source = pISource;
347 else
349 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA,
350 pISource, &This->source);
351 This->bpp = 32;
353 This->fn_get_required_source_rect = NearestNeighbor_GetRequiredSourceRect;
354 This->fn_copy_scanline = NearestNeighbor_CopyScanline;
355 break;
359 end:
360 LeaveCriticalSection(&This->lock);
362 return hr;
365 static const IWICBitmapScalerVtbl BitmapScaler_Vtbl = {
366 BitmapScaler_QueryInterface,
367 BitmapScaler_AddRef,
368 BitmapScaler_Release,
369 BitmapScaler_GetSize,
370 BitmapScaler_GetPixelFormat,
371 BitmapScaler_GetResolution,
372 BitmapScaler_CopyPalette,
373 BitmapScaler_CopyPixels,
374 BitmapScaler_Initialize
377 HRESULT BitmapScaler_Create(IWICBitmapScaler **scaler)
379 BitmapScaler *This;
381 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapScaler));
382 if (!This) return E_OUTOFMEMORY;
384 This->IWICBitmapScaler_iface.lpVtbl = &BitmapScaler_Vtbl;
385 This->ref = 1;
386 This->source = NULL;
387 This->width = 0;
388 This->height = 0;
389 This->src_width = 0;
390 This->src_height = 0;
391 This->mode = 0;
392 This->bpp = 0;
393 InitializeCriticalSection(&This->lock);
394 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BitmapScaler.lock");
396 *scaler = &This->IWICBitmapScaler_iface;
398 return S_OK;