windowscodecs: Implement FlipRotator_GetResolution.
[wine.git] / dlls / windowscodecs / fliprotate.c
blob26062cd9dd8bcee448dedf7e95da23e063b1c142
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"
28 #include "wincodec.h"
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
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;
46 static inline FlipRotator *impl_from_IWICBitmapFlipRotator(IWICBitmapFlipRotator *iface)
48 return CONTAINING_RECORD(iface, FlipRotator, IWICBitmapFlipRotator_iface);
51 static HRESULT WINAPI FlipRotator_QueryInterface(IWICBitmapFlipRotator *iface, REFIID iid,
52 void **ppv)
54 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
55 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
57 if (!ppv) return E_INVALIDARG;
59 if (IsEqualIID(&IID_IUnknown, iid) ||
60 IsEqualIID(&IID_IWICBitmapSource, iid) ||
61 IsEqualIID(&IID_IWICBitmapFlipRotator, iid))
63 *ppv = This;
65 else
67 *ppv = NULL;
68 return E_NOINTERFACE;
71 IUnknown_AddRef((IUnknown*)*ppv);
72 return S_OK;
75 static ULONG WINAPI FlipRotator_AddRef(IWICBitmapFlipRotator *iface)
77 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
78 ULONG ref = InterlockedIncrement(&This->ref);
80 TRACE("(%p) refcount=%u\n", iface, ref);
82 return ref;
85 static ULONG WINAPI FlipRotator_Release(IWICBitmapFlipRotator *iface)
87 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
88 ULONG ref = InterlockedDecrement(&This->ref);
90 TRACE("(%p) refcount=%u\n", iface, ref);
92 if (ref == 0)
94 This->lock.DebugInfo->Spare[0] = 0;
95 DeleteCriticalSection(&This->lock);
96 if (This->source) IWICBitmapSource_Release(This->source);
97 HeapFree(GetProcessHeap(), 0, This);
100 return ref;
103 static HRESULT WINAPI FlipRotator_GetSize(IWICBitmapFlipRotator *iface,
104 UINT *puiWidth, UINT *puiHeight)
106 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
107 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
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);
117 static HRESULT WINAPI FlipRotator_GetPixelFormat(IWICBitmapFlipRotator *iface,
118 WICPixelFormatGUID *pPixelFormat)
120 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
121 TRACE("(%p,%p)\n", iface, pPixelFormat);
123 if (!This->source)
124 return WINCODEC_ERR_WRONGSTATE;
125 else
126 return IWICBitmapSource_GetPixelFormat(This->source, pPixelFormat);
129 static HRESULT WINAPI FlipRotator_GetResolution(IWICBitmapFlipRotator *iface,
130 double *pDpiX, double *pDpiY)
132 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
133 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
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);
143 static HRESULT WINAPI FlipRotator_CopyPalette(IWICBitmapFlipRotator *iface,
144 IWICPalette *pIPalette)
146 FIXME("(%p,%p): stub\n", iface, pIPalette);
147 return E_NOTIMPL;
150 static HRESULT WINAPI FlipRotator_CopyPixels(IWICBitmapFlipRotator *iface,
151 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
153 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
154 HRESULT hr;
155 UINT y;
156 UINT srcy, srcwidth, srcheight;
157 WICRect rc;
158 WICRect rect;
160 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
162 if (!This->source) return WINCODEC_ERR_WRONGSTATE;
164 if (This->swap_xy || This->flip_x)
166 /* This requires knowledge of the pixel format. */
167 FIXME("flipping x and rotating are not implemented\n");
168 return E_NOTIMPL;
171 hr = IWICBitmapSource_GetSize(This->source, &srcwidth, &srcheight);
172 if (FAILED(hr)) return hr;
174 if (!prc)
176 UINT width, height;
177 hr = IWICBitmapSource_GetSize(iface, &width, &height);
178 if (FAILED(hr)) return hr;
179 rect.X = 0;
180 rect.Y = 0;
181 rect.Width = width;
182 rect.Height = height;
183 prc = &rect;
186 for (y=prc->Y; y - prc->Y < prc->Height; y++)
188 if (This->flip_y)
189 srcy = srcheight - 1 - y;
190 else
191 srcy = y;
193 rc.X = prc->X;
194 rc.Y = srcy;
195 rc.Width = prc->Width;
196 rc.Height = 1;
198 hr = IWICBitmapSource_CopyPixels(This->source, &rc, cbStride, cbStride,
199 pbBuffer);
201 if (FAILED(hr)) break;
203 pbBuffer += cbStride;
206 return hr;
209 static HRESULT WINAPI FlipRotator_Initialize(IWICBitmapFlipRotator *iface,
210 IWICBitmapSource *pISource, WICBitmapTransformOptions options)
212 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
213 HRESULT hr=S_OK;
215 TRACE("(%p,%p,%u)\n", iface, pISource, options);
217 EnterCriticalSection(&This->lock);
219 if (This->source)
221 hr = WINCODEC_ERR_WRONGSTATE;
222 goto end;
225 if (options&WICBitmapTransformRotate90)
227 This->swap_xy = 1;
228 This->flip_x = !This->flip_x;
231 if (options&WICBitmapTransformRotate180)
233 This->flip_x = !This->flip_x;
234 This->flip_y = !This->flip_y;
237 if (options&WICBitmapTransformFlipHorizontal)
238 This->flip_x = !This->flip_x;
240 if (options&WICBitmapTransformFlipVertical)
241 This->flip_y = !This->flip_y;
243 IWICBitmapSource_AddRef(pISource);
244 This->source = pISource;
246 end:
247 LeaveCriticalSection(&This->lock);
249 return hr;
252 static const IWICBitmapFlipRotatorVtbl FlipRotator_Vtbl = {
253 FlipRotator_QueryInterface,
254 FlipRotator_AddRef,
255 FlipRotator_Release,
256 FlipRotator_GetSize,
257 FlipRotator_GetPixelFormat,
258 FlipRotator_GetResolution,
259 FlipRotator_CopyPalette,
260 FlipRotator_CopyPixels,
261 FlipRotator_Initialize
264 HRESULT FlipRotator_Create(IWICBitmapFlipRotator **fliprotator)
266 FlipRotator *This;
268 This = HeapAlloc(GetProcessHeap(), 0, sizeof(FlipRotator));
269 if (!This) return E_OUTOFMEMORY;
271 This->IWICBitmapFlipRotator_iface.lpVtbl = &FlipRotator_Vtbl;
272 This->ref = 1;
273 This->source = NULL;
274 This->flip_x = 0;
275 This->flip_y = 0;
276 This->swap_xy = 0;
277 InitializeCriticalSection(&This->lock);
278 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FlipRotator.lock");
280 *fliprotator = &This->IWICBitmapFlipRotator_iface;
282 return S_OK;