wined3d: Use the texture dimension helpers in wined3d_texture_update_overlay().
[wine.git] / dlls / windowscodecs / fliprotate.c
blob72d1e8a2878114b7c136a1f88762d53a86fb5756
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 FlipRotator {
36 IWICBitmapFlipRotator IWICBitmapFlipRotator_iface;
37 LONG ref;
38 IWICBitmapSource *source;
39 int flip_x;
40 int flip_y;
41 int swap_xy;
42 CRITICAL_SECTION lock; /* must be held when initialized */
43 } FlipRotator;
45 static inline FlipRotator *impl_from_IWICBitmapFlipRotator(IWICBitmapFlipRotator *iface)
47 return CONTAINING_RECORD(iface, FlipRotator, IWICBitmapFlipRotator_iface);
50 static HRESULT WINAPI FlipRotator_QueryInterface(IWICBitmapFlipRotator *iface, REFIID iid,
51 void **ppv)
53 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
54 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
56 if (!ppv) return E_INVALIDARG;
58 if (IsEqualIID(&IID_IUnknown, iid) ||
59 IsEqualIID(&IID_IWICBitmapSource, iid) ||
60 IsEqualIID(&IID_IWICBitmapFlipRotator, iid))
62 *ppv = &This->IWICBitmapFlipRotator_iface;
64 else
66 *ppv = NULL;
67 return E_NOINTERFACE;
70 IUnknown_AddRef((IUnknown*)*ppv);
71 return S_OK;
74 static ULONG WINAPI FlipRotator_AddRef(IWICBitmapFlipRotator *iface)
76 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
77 ULONG ref = InterlockedIncrement(&This->ref);
79 TRACE("(%p) refcount=%u\n", iface, ref);
81 return ref;
84 static ULONG WINAPI FlipRotator_Release(IWICBitmapFlipRotator *iface)
86 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
87 ULONG ref = InterlockedDecrement(&This->ref);
89 TRACE("(%p) refcount=%u\n", iface, ref);
91 if (ref == 0)
93 This->lock.DebugInfo->Spare[0] = 0;
94 DeleteCriticalSection(&This->lock);
95 if (This->source) IWICBitmapSource_Release(This->source);
96 HeapFree(GetProcessHeap(), 0, This);
99 return ref;
102 static HRESULT WINAPI FlipRotator_GetSize(IWICBitmapFlipRotator *iface,
103 UINT *puiWidth, UINT *puiHeight)
105 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
106 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
108 if (!This->source)
109 return WINCODEC_ERR_WRONGSTATE;
110 else if (This->swap_xy)
111 return IWICBitmapSource_GetSize(This->source, puiHeight, puiWidth);
112 else
113 return IWICBitmapSource_GetSize(This->source, puiWidth, puiHeight);
116 static HRESULT WINAPI FlipRotator_GetPixelFormat(IWICBitmapFlipRotator *iface,
117 WICPixelFormatGUID *pPixelFormat)
119 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
120 TRACE("(%p,%p)\n", iface, pPixelFormat);
122 if (!This->source)
123 return WINCODEC_ERR_WRONGSTATE;
124 else
125 return IWICBitmapSource_GetPixelFormat(This->source, pPixelFormat);
128 static HRESULT WINAPI FlipRotator_GetResolution(IWICBitmapFlipRotator *iface,
129 double *pDpiX, double *pDpiY)
131 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
132 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
134 if (!This->source)
135 return WINCODEC_ERR_WRONGSTATE;
136 else if (This->swap_xy)
137 return IWICBitmapSource_GetResolution(This->source, pDpiY, pDpiX);
138 else
139 return IWICBitmapSource_GetResolution(This->source, pDpiX, pDpiY);
142 static HRESULT WINAPI FlipRotator_CopyPalette(IWICBitmapFlipRotator *iface,
143 IWICPalette *pIPalette)
145 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
146 TRACE("(%p,%p)\n", iface, pIPalette);
148 if (!This->source)
149 return WINCODEC_ERR_WRONGSTATE;
150 else
151 return IWICBitmapSource_CopyPalette(This->source, pIPalette);
154 static HRESULT WINAPI FlipRotator_CopyPixels(IWICBitmapFlipRotator *iface,
155 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
157 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
158 HRESULT hr;
159 UINT y;
160 UINT srcy, srcwidth, srcheight;
161 WICRect rc;
162 WICRect rect;
164 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
166 if (!This->source) return WINCODEC_ERR_WRONGSTATE;
168 if (This->swap_xy || This->flip_x)
170 /* This requires knowledge of the pixel format. */
171 FIXME("flipping x and rotating are not implemented\n");
172 return E_NOTIMPL;
175 hr = IWICBitmapSource_GetSize(This->source, &srcwidth, &srcheight);
176 if (FAILED(hr)) return hr;
178 if (!prc)
180 UINT width, height;
181 hr = IWICBitmapFlipRotator_GetSize(iface, &width, &height);
182 if (FAILED(hr)) return hr;
183 rect.X = 0;
184 rect.Y = 0;
185 rect.Width = width;
186 rect.Height = height;
187 prc = &rect;
190 for (y=prc->Y; y - prc->Y < prc->Height; y++)
192 if (This->flip_y)
193 srcy = srcheight - 1 - y;
194 else
195 srcy = y;
197 rc.X = prc->X;
198 rc.Y = srcy;
199 rc.Width = prc->Width;
200 rc.Height = 1;
202 hr = IWICBitmapSource_CopyPixels(This->source, &rc, cbStride, cbStride,
203 pbBuffer);
205 if (FAILED(hr)) break;
207 pbBuffer += cbStride;
210 return hr;
213 static HRESULT WINAPI FlipRotator_Initialize(IWICBitmapFlipRotator *iface,
214 IWICBitmapSource *pISource, WICBitmapTransformOptions options)
216 FlipRotator *This = impl_from_IWICBitmapFlipRotator(iface);
217 HRESULT hr=S_OK;
219 TRACE("(%p,%p,%u)\n", iface, pISource, options);
221 EnterCriticalSection(&This->lock);
223 if (This->source)
225 hr = WINCODEC_ERR_WRONGSTATE;
226 goto end;
229 if (options&WICBitmapTransformRotate90)
231 This->swap_xy = 1;
232 This->flip_x = !This->flip_x;
235 if (options&WICBitmapTransformRotate180)
237 This->flip_x = !This->flip_x;
238 This->flip_y = !This->flip_y;
241 if (options&WICBitmapTransformFlipHorizontal)
242 This->flip_x = !This->flip_x;
244 if (options&WICBitmapTransformFlipVertical)
245 This->flip_y = !This->flip_y;
247 IWICBitmapSource_AddRef(pISource);
248 This->source = pISource;
250 end:
251 LeaveCriticalSection(&This->lock);
253 return hr;
256 static const IWICBitmapFlipRotatorVtbl FlipRotator_Vtbl = {
257 FlipRotator_QueryInterface,
258 FlipRotator_AddRef,
259 FlipRotator_Release,
260 FlipRotator_GetSize,
261 FlipRotator_GetPixelFormat,
262 FlipRotator_GetResolution,
263 FlipRotator_CopyPalette,
264 FlipRotator_CopyPixels,
265 FlipRotator_Initialize
268 HRESULT FlipRotator_Create(IWICBitmapFlipRotator **fliprotator)
270 FlipRotator *This;
272 This = HeapAlloc(GetProcessHeap(), 0, sizeof(FlipRotator));
273 if (!This) return E_OUTOFMEMORY;
275 This->IWICBitmapFlipRotator_iface.lpVtbl = &FlipRotator_Vtbl;
276 This->ref = 1;
277 This->source = NULL;
278 This->flip_x = 0;
279 This->flip_y = 0;
280 This->swap_xy = 0;
281 InitializeCriticalSection(&This->lock);
282 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FlipRotator.lock");
284 *fliprotator = &This->IWICBitmapFlipRotator_iface;
286 return S_OK;