mshtml: Return referenced object in get_node_obj.
[wine] / dlls / windowscodecs / scaler.c
1 /*
2  * Copyright 2010 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 BitmapScaler {
37     IWICBitmapScaler IWICBitmapScaler_iface;
38     LONG ref;
39     IWICBitmapSource *source;
40     UINT width, height;
41     WICBitmapInterpolationMode mode;
42     CRITICAL_SECTION lock; /* must be held when initialized */
43 } BitmapScaler;
44
45 static inline BitmapScaler *impl_from_IWICBitmapScaler(IWICBitmapScaler *iface)
46 {
47     return CONTAINING_RECORD(iface, BitmapScaler, IWICBitmapScaler_iface);
48 }
49
50 static HRESULT WINAPI BitmapScaler_QueryInterface(IWICBitmapScaler *iface, REFIID iid,
51     void **ppv)
52 {
53     BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
54     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
55
56     if (!ppv) return E_INVALIDARG;
57
58     if (IsEqualIID(&IID_IUnknown, iid) ||
59         IsEqualIID(&IID_IWICBitmapSource, iid) ||
60         IsEqualIID(&IID_IWICBitmapScaler, iid))
61     {
62         *ppv = This;
63     }
64     else
65     {
66         *ppv = NULL;
67         return E_NOINTERFACE;
68     }
69
70     IUnknown_AddRef((IUnknown*)*ppv);
71     return S_OK;
72 }
73
74 static ULONG WINAPI BitmapScaler_AddRef(IWICBitmapScaler *iface)
75 {
76     BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
77     ULONG ref = InterlockedIncrement(&This->ref);
78
79     TRACE("(%p) refcount=%u\n", iface, ref);
80
81     return ref;
82 }
83
84 static ULONG WINAPI BitmapScaler_Release(IWICBitmapScaler *iface)
85 {
86     BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
87     ULONG ref = InterlockedDecrement(&This->ref);
88
89     TRACE("(%p) refcount=%u\n", iface, ref);
90
91     if (ref == 0)
92     {
93         This->lock.DebugInfo->Spare[0] = 0;
94         DeleteCriticalSection(&This->lock);
95         if (This->source) IWICBitmapSource_Release(This->source);
96         HeapFree(GetProcessHeap(), 0, This);
97     }
98
99     return ref;
100 }
101
102 static HRESULT WINAPI BitmapScaler_GetSize(IWICBitmapScaler *iface,
103     UINT *puiWidth, UINT *puiHeight)
104 {
105     FIXME("(%p,%p,%p): stub\n", iface, puiWidth, puiHeight);
106
107     return E_NOTIMPL;
108 }
109
110 static HRESULT WINAPI BitmapScaler_GetPixelFormat(IWICBitmapScaler *iface,
111     WICPixelFormatGUID *pPixelFormat)
112 {
113     FIXME("(%p,%p): stub\n", iface, pPixelFormat);
114
115     return E_NOTIMPL;
116 }
117
118 static HRESULT WINAPI BitmapScaler_GetResolution(IWICBitmapScaler *iface,
119     double *pDpiX, double *pDpiY)
120 {
121     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
122
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI BitmapScaler_CopyPalette(IWICBitmapScaler *iface,
127     IWICPalette *pIPalette)
128 {
129     FIXME("(%p,%p): stub\n", iface, pIPalette);
130
131     return E_NOTIMPL;
132 }
133
134 static HRESULT WINAPI BitmapScaler_CopyPixels(IWICBitmapScaler *iface,
135     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
136 {
137     FIXME("(%p,%p,%u,%u,%p): stub\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
138
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI BitmapScaler_Initialize(IWICBitmapScaler *iface,
143     IWICBitmapSource *pISource, UINT uiWidth, UINT uiHeight,
144     WICBitmapInterpolationMode mode)
145 {
146     BitmapScaler *This = impl_from_IWICBitmapScaler(iface);
147     HRESULT hr=S_OK;
148
149     TRACE("(%p,%p,%u,%u,%u)\n", iface, pISource, uiWidth, uiHeight, mode);
150
151     EnterCriticalSection(&This->lock);
152
153     if (This->source)
154     {
155         hr = WINCODEC_ERR_WRONGSTATE;
156         goto end;
157     }
158
159     IWICBitmapSource_AddRef(pISource);
160     This->source = pISource;
161
162     This->width = uiWidth;
163     This->height = uiHeight;
164     This->mode = mode;
165
166 end:
167     LeaveCriticalSection(&This->lock);
168
169     return hr;
170 }
171
172 static const IWICBitmapScalerVtbl BitmapScaler_Vtbl = {
173     BitmapScaler_QueryInterface,
174     BitmapScaler_AddRef,
175     BitmapScaler_Release,
176     BitmapScaler_GetSize,
177     BitmapScaler_GetPixelFormat,
178     BitmapScaler_GetResolution,
179     BitmapScaler_CopyPalette,
180     BitmapScaler_CopyPixels,
181     BitmapScaler_Initialize
182 };
183
184 HRESULT BitmapScaler_Create(IWICBitmapScaler **scaler)
185 {
186     BitmapScaler *This;
187
188     This = HeapAlloc(GetProcessHeap(), 0, sizeof(BitmapScaler));
189     if (!This) return E_OUTOFMEMORY;
190
191     This->IWICBitmapScaler_iface.lpVtbl = &BitmapScaler_Vtbl;
192     This->ref = 1;
193     This->source = NULL;
194     This->width = 0;
195     This->height = 0;
196     This->mode = 0;
197     InitializeCriticalSection(&This->lock);
198     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BitmapScaler.lock");
199
200     *scaler = &This->IWICBitmapScaler_iface;
201
202     return S_OK;
203 }