d3drm: Use an iface instead of a vtbl pointer in IDirect3DRMMeshBuilderImpl.
[wine] / dlls / windowscodecs / fliprotate.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 FlipRotator {
37     const IWICBitmapFlipRotatorVtbl *lpVtbl;
38     LONG ref;
39     IWICBitmapSource *source;
40     int flip_x;
41     int flip_y;
42     int swap_xy;
43     CRITICAL_SECTION lock; /* must be held when initialized */
44 } FlipRotator;
45
46 static HRESULT WINAPI FlipRotator_QueryInterface(IWICBitmapFlipRotator *iface, REFIID iid,
47     void **ppv)
48 {
49     FlipRotator *This = (FlipRotator*)iface;
50     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
51
52     if (!ppv) return E_INVALIDARG;
53
54     if (IsEqualIID(&IID_IUnknown, iid) ||
55         IsEqualIID(&IID_IWICBitmapSource, iid) ||
56         IsEqualIID(&IID_IWICBitmapFlipRotator, iid))
57     {
58         *ppv = This;
59     }
60     else
61     {
62         *ppv = NULL;
63         return E_NOINTERFACE;
64     }
65
66     IUnknown_AddRef((IUnknown*)*ppv);
67     return S_OK;
68 }
69
70 static ULONG WINAPI FlipRotator_AddRef(IWICBitmapFlipRotator *iface)
71 {
72     FlipRotator *This = (FlipRotator*)iface;
73     ULONG ref = InterlockedIncrement(&This->ref);
74
75     TRACE("(%p) refcount=%u\n", iface, ref);
76
77     return ref;
78 }
79
80 static ULONG WINAPI FlipRotator_Release(IWICBitmapFlipRotator *iface)
81 {
82     FlipRotator *This = (FlipRotator*)iface;
83     ULONG ref = InterlockedDecrement(&This->ref);
84
85     TRACE("(%p) refcount=%u\n", iface, ref);
86
87     if (ref == 0)
88     {
89         This->lock.DebugInfo->Spare[0] = 0;
90         DeleteCriticalSection(&This->lock);
91         if (This->source) IWICBitmapSource_Release(This->source);
92         HeapFree(GetProcessHeap(), 0, This);
93     }
94
95     return ref;
96 }
97
98 static HRESULT WINAPI FlipRotator_GetSize(IWICBitmapFlipRotator *iface,
99     UINT *puiWidth, UINT *puiHeight)
100 {
101     FlipRotator *This = (FlipRotator*)iface;
102     TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
103
104     if (!This->source)
105         return WINCODEC_ERR_WRONGSTATE;
106     else if (This->swap_xy)
107         return IWICBitmapSource_GetSize(This->source, puiHeight, puiWidth);
108     else
109         return IWICBitmapSource_GetSize(This->source, puiWidth, puiHeight);
110 }
111
112 static HRESULT WINAPI FlipRotator_GetPixelFormat(IWICBitmapFlipRotator *iface,
113     WICPixelFormatGUID *pPixelFormat)
114 {
115     FIXME("(%p,%p): stub\n", iface, pPixelFormat);
116
117     return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI FlipRotator_GetResolution(IWICBitmapFlipRotator *iface,
121     double *pDpiX, double *pDpiY)
122 {
123     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
124
125     return E_NOTIMPL;
126 }
127
128 static HRESULT WINAPI FlipRotator_CopyPalette(IWICBitmapFlipRotator *iface,
129     IWICPalette *pIPalette)
130 {
131     FIXME("(%p,%p): stub\n", iface, pIPalette);
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI FlipRotator_CopyPixels(IWICBitmapFlipRotator *iface,
136     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
137 {
138     FlipRotator *This = (FlipRotator*)iface;
139     HRESULT hr;
140     UINT y;
141     UINT srcy, srcwidth, srcheight;
142     WICRect rc;
143     WICRect rect;
144
145     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
146
147     if (!This->source) return WINCODEC_ERR_WRONGSTATE;
148
149     if (This->swap_xy || This->flip_x)
150     {
151         /* This requires knowledge of the pixel format. */
152         FIXME("flipping x and rotating are not implemented\n");
153         return E_NOTIMPL;
154     }
155
156     hr = IWICBitmapSource_GetSize(This->source, &srcwidth, &srcheight);
157     if (FAILED(hr)) return hr;
158
159     if (!prc)
160     {
161         UINT width, height;
162         hr = IWICBitmapSource_GetSize(iface, &width, &height);
163         if (FAILED(hr)) return hr;
164         rect.X = 0;
165         rect.Y = 0;
166         rect.Width = width;
167         rect.Height = height;
168         prc = &rect;
169     }
170
171     for (y=prc->Y; y - prc->Y < prc->Height; y++)
172     {
173         if (This->flip_y)
174             srcy = srcheight - 1 - y;
175         else
176             srcy = y;
177
178         rc.X = prc->X;
179         rc.Y = srcy;
180         rc.Width = prc->Width;
181         rc.Height = 1;
182
183         hr = IWICBitmapSource_CopyPixels(This->source, &rc, cbStride, cbStride,
184             pbBuffer);
185
186         if (FAILED(hr)) break;
187
188         pbBuffer += cbStride;
189     }
190
191     return hr;
192 }
193
194 static HRESULT WINAPI FlipRotator_Initialize(IWICBitmapFlipRotator *iface,
195     IWICBitmapSource *pISource, WICBitmapTransformOptions options)
196 {
197     FlipRotator *This = (FlipRotator*)iface;
198     HRESULT hr=S_OK;
199
200     TRACE("(%p,%p,%u)\n", iface, pISource, options);
201
202     EnterCriticalSection(&This->lock);
203
204     if (This->source)
205     {
206         hr = WINCODEC_ERR_WRONGSTATE;
207         goto end;
208     }
209
210     if (options&WICBitmapTransformRotate90)
211     {
212         This->swap_xy = 1;
213         This->flip_x = !This->flip_x;
214     }
215
216     if (options&WICBitmapTransformRotate180)
217     {
218         This->flip_x = !This->flip_x;
219         This->flip_y = !This->flip_y;
220     }
221
222     if (options&WICBitmapTransformFlipHorizontal)
223         This->flip_x = !This->flip_x;
224
225     if (options&WICBitmapTransformFlipVertical)
226         This->flip_y = !This->flip_y;
227
228     IWICBitmapSource_AddRef(pISource);
229     This->source = pISource;
230
231 end:
232     LeaveCriticalSection(&This->lock);
233
234     return hr;
235 }
236
237 static const IWICBitmapFlipRotatorVtbl FlipRotator_Vtbl = {
238     FlipRotator_QueryInterface,
239     FlipRotator_AddRef,
240     FlipRotator_Release,
241     FlipRotator_GetSize,
242     FlipRotator_GetPixelFormat,
243     FlipRotator_GetResolution,
244     FlipRotator_CopyPalette,
245     FlipRotator_CopyPixels,
246     FlipRotator_Initialize
247 };
248
249 HRESULT FlipRotator_Create(IWICBitmapFlipRotator **fliprotator)
250 {
251     FlipRotator *This;
252
253     This = HeapAlloc(GetProcessHeap(), 0, sizeof(FlipRotator));
254     if (!This) return E_OUTOFMEMORY;
255
256     This->lpVtbl = &FlipRotator_Vtbl;
257     This->ref = 1;
258     This->source = NULL;
259     This->flip_x = 0;
260     This->flip_y = 0;
261     This->swap_xy = 0;
262     InitializeCriticalSection(&This->lock);
263     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FlipRotator.lock");
264
265     *fliprotator = (IWICBitmapFlipRotator*)This;
266
267     return S_OK;
268 }