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