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