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
27 #include "wincodecs_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
33 typedef struct FlipRotator
{
34 IWICBitmapFlipRotator IWICBitmapFlipRotator_iface
;
36 IWICBitmapSource
*source
;
40 CRITICAL_SECTION lock
; /* must be held when initialized */
43 static inline FlipRotator
*impl_from_IWICBitmapFlipRotator(IWICBitmapFlipRotator
*iface
)
45 return CONTAINING_RECORD(iface
, FlipRotator
, IWICBitmapFlipRotator_iface
);
48 static HRESULT WINAPI
FlipRotator_QueryInterface(IWICBitmapFlipRotator
*iface
, REFIID iid
,
51 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
52 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
54 if (!ppv
) return E_INVALIDARG
;
56 if (IsEqualIID(&IID_IUnknown
, iid
) ||
57 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
58 IsEqualIID(&IID_IWICBitmapFlipRotator
, iid
))
60 *ppv
= &This
->IWICBitmapFlipRotator_iface
;
68 IUnknown_AddRef((IUnknown
*)*ppv
);
72 static ULONG WINAPI
FlipRotator_AddRef(IWICBitmapFlipRotator
*iface
)
74 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
75 ULONG ref
= InterlockedIncrement(&This
->ref
);
77 TRACE("(%p) refcount=%u\n", iface
, ref
);
82 static ULONG WINAPI
FlipRotator_Release(IWICBitmapFlipRotator
*iface
)
84 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
85 ULONG ref
= InterlockedDecrement(&This
->ref
);
87 TRACE("(%p) refcount=%u\n", iface
, ref
);
91 This
->lock
.DebugInfo
->Spare
[0] = 0;
92 DeleteCriticalSection(&This
->lock
);
93 if (This
->source
) IWICBitmapSource_Release(This
->source
);
94 HeapFree(GetProcessHeap(), 0, This
);
100 static HRESULT WINAPI
FlipRotator_GetSize(IWICBitmapFlipRotator
*iface
,
101 UINT
*puiWidth
, UINT
*puiHeight
)
103 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
104 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
107 return WINCODEC_ERR_WRONGSTATE
;
108 else if (This
->swap_xy
)
109 return IWICBitmapSource_GetSize(This
->source
, puiHeight
, puiWidth
);
111 return IWICBitmapSource_GetSize(This
->source
, puiWidth
, puiHeight
);
114 static HRESULT WINAPI
FlipRotator_GetPixelFormat(IWICBitmapFlipRotator
*iface
,
115 WICPixelFormatGUID
*pPixelFormat
)
117 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
118 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
121 return WINCODEC_ERR_WRONGSTATE
;
123 return IWICBitmapSource_GetPixelFormat(This
->source
, pPixelFormat
);
126 static HRESULT WINAPI
FlipRotator_GetResolution(IWICBitmapFlipRotator
*iface
,
127 double *pDpiX
, double *pDpiY
)
129 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
130 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
133 return WINCODEC_ERR_WRONGSTATE
;
134 else if (This
->swap_xy
)
135 return IWICBitmapSource_GetResolution(This
->source
, pDpiY
, pDpiX
);
137 return IWICBitmapSource_GetResolution(This
->source
, pDpiX
, pDpiY
);
140 static HRESULT WINAPI
FlipRotator_CopyPalette(IWICBitmapFlipRotator
*iface
,
141 IWICPalette
*pIPalette
)
143 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
144 TRACE("(%p,%p)\n", iface
, pIPalette
);
147 return WINCODEC_ERR_WRONGSTATE
;
149 return IWICBitmapSource_CopyPalette(This
->source
, pIPalette
);
152 static HRESULT WINAPI
FlipRotator_CopyPixels(IWICBitmapFlipRotator
*iface
,
153 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
155 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
158 UINT srcy
, srcwidth
, srcheight
;
162 TRACE("(%p,%s,%u,%u,%p)\n", iface
, debug_wic_rect(prc
), cbStride
, cbBufferSize
, pbBuffer
);
164 if (!This
->source
) return WINCODEC_ERR_WRONGSTATE
;
166 if (This
->swap_xy
|| This
->flip_x
)
168 /* This requires knowledge of the pixel format. */
169 FIXME("flipping x and rotating are not implemented\n");
173 hr
= IWICBitmapSource_GetSize(This
->source
, &srcwidth
, &srcheight
);
174 if (FAILED(hr
)) return hr
;
179 hr
= IWICBitmapFlipRotator_GetSize(iface
, &width
, &height
);
180 if (FAILED(hr
)) return hr
;
184 rect
.Height
= height
;
188 for (y
=prc
->Y
; y
- prc
->Y
< prc
->Height
; y
++)
191 srcy
= srcheight
- 1 - y
;
197 rc
.Width
= prc
->Width
;
200 hr
= IWICBitmapSource_CopyPixels(This
->source
, &rc
, cbStride
, cbStride
,
203 if (FAILED(hr
)) break;
205 pbBuffer
+= cbStride
;
211 static HRESULT WINAPI
FlipRotator_Initialize(IWICBitmapFlipRotator
*iface
,
212 IWICBitmapSource
*pISource
, WICBitmapTransformOptions options
)
214 FlipRotator
*This
= impl_from_IWICBitmapFlipRotator(iface
);
217 TRACE("(%p,%p,%u)\n", iface
, pISource
, options
);
219 EnterCriticalSection(&This
->lock
);
223 hr
= WINCODEC_ERR_WRONGSTATE
;
227 if (options
&WICBitmapTransformRotate90
)
230 This
->flip_x
= !This
->flip_x
;
233 if (options
&WICBitmapTransformRotate180
)
235 This
->flip_x
= !This
->flip_x
;
236 This
->flip_y
= !This
->flip_y
;
239 if (options
&WICBitmapTransformFlipHorizontal
)
240 This
->flip_x
= !This
->flip_x
;
242 if (options
&WICBitmapTransformFlipVertical
)
243 This
->flip_y
= !This
->flip_y
;
245 IWICBitmapSource_AddRef(pISource
);
246 This
->source
= pISource
;
249 LeaveCriticalSection(&This
->lock
);
254 static const IWICBitmapFlipRotatorVtbl FlipRotator_Vtbl
= {
255 FlipRotator_QueryInterface
,
259 FlipRotator_GetPixelFormat
,
260 FlipRotator_GetResolution
,
261 FlipRotator_CopyPalette
,
262 FlipRotator_CopyPixels
,
263 FlipRotator_Initialize
266 HRESULT
FlipRotator_Create(IWICBitmapFlipRotator
**fliprotator
)
270 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(FlipRotator
));
271 if (!This
) return E_OUTOFMEMORY
;
273 This
->IWICBitmapFlipRotator_iface
.lpVtbl
= &FlipRotator_Vtbl
;
279 InitializeCriticalSection(&This
->lock
);
280 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FlipRotator.lock");
282 *fliprotator
= &This
->IWICBitmapFlipRotator_iface
;