2 * Copyright 2010 Damjan Jovanovic
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
20 #include "wine/port.h"
24 #ifdef HAVE_APPLICATIONSERVICES_APPLICATIONSERVICES_H
25 #define GetCurrentProcess GetCurrentProcess_Mac
26 #define GetCurrentThread GetCurrentThread_Mac
27 #define LoadResource LoadResource_Mac
28 #define AnimatePalette AnimatePalette_Mac
29 #define EqualRgn EqualRgn_Mac
30 #define FillRgn FillRgn_Mac
31 #define FrameRgn FrameRgn_Mac
32 #define GetPixel GetPixel_Mac
33 #define InvertRgn InvertRgn_Mac
34 #define LineTo LineTo_Mac
35 #define OffsetRgn OffsetRgn_Mac
36 #define PaintRgn PaintRgn_Mac
37 #define Polygon Polygon_Mac
38 #define ResizePalette ResizePalette_Mac
39 #define SetRectRgn SetRectRgn_Mac
40 #define EqualRect EqualRect_Mac
41 #define FillRect FillRect_Mac
42 #define FrameRect FrameRect_Mac
43 #define GetCursor GetCursor_Mac
44 #define InvertRect InvertRect_Mac
45 #define OffsetRect OffsetRect_Mac
46 #define PtInRect PtInRect_Mac
47 #define SetCursor SetCursor_Mac
48 #define SetRect SetRect_Mac
49 #define ShowCursor ShowCursor_Mac
50 #define UnionRect UnionRect_Mac
51 #include <ApplicationServices/ApplicationServices.h>
52 #undef GetCurrentProcess
53 #undef GetCurrentThread
88 #include "wincodecs_private.h"
90 #include "wine/debug.h"
91 #include "wine/library.h"
93 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
95 #if defined(HAVE_APPLICATIONSERVICES_APPLICATIONSERVICES_H) && \
96 MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4
98 typedef struct IcnsEncoder
{
99 IWICBitmapEncoder IWICBitmapEncoder_iface
;
102 IconFamilyHandle icns_family
;
103 BOOL any_frame_committed
;
104 int outstanding_commits
;
106 CRITICAL_SECTION lock
;
109 static inline IcnsEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
111 return CONTAINING_RECORD(iface
, IcnsEncoder
, IWICBitmapEncoder_iface
);
114 typedef struct IcnsFrameEncode
{
115 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
116 IcnsEncoder
*encoder
;
126 static inline IcnsFrameEncode
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
128 return CONTAINING_RECORD(iface
, IcnsFrameEncode
, IWICBitmapFrameEncode_iface
);
131 static HRESULT WINAPI
IcnsFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
134 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
135 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
137 if (!ppv
) return E_INVALIDARG
;
139 if (IsEqualIID(&IID_IUnknown
, iid
) ||
140 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
142 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
147 return E_NOINTERFACE
;
150 IUnknown_AddRef((IUnknown
*)*ppv
);
154 static ULONG WINAPI
IcnsFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
156 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
157 ULONG ref
= InterlockedIncrement(&This
->ref
);
159 TRACE("(%p) refcount=%u\n", iface
, ref
);
164 static ULONG WINAPI
IcnsFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
166 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
167 ULONG ref
= InterlockedDecrement(&This
->ref
);
169 TRACE("(%p) refcount=%u\n", iface
, ref
);
173 if (!This
->committed
)
175 EnterCriticalSection(&This
->encoder
->lock
);
176 This
->encoder
->outstanding_commits
--;
177 LeaveCriticalSection(&This
->encoder
->lock
);
179 if (This
->icns_image
!= NULL
)
180 HeapFree(GetProcessHeap(), 0, This
->icns_image
);
182 IWICBitmapEncoder_Release(&This
->encoder
->IWICBitmapEncoder_iface
);
183 HeapFree(GetProcessHeap(), 0, This
);
189 static HRESULT WINAPI
IcnsFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
190 IPropertyBag2
*pIEncoderOptions
)
192 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
195 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
197 EnterCriticalSection(&This
->encoder
->lock
);
199 if (This
->initialized
)
201 hr
= WINCODEC_ERR_WRONGSTATE
;
204 This
->initialized
= TRUE
;
207 LeaveCriticalSection(&This
->encoder
->lock
);
211 static HRESULT WINAPI
IcnsFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
212 UINT uiWidth
, UINT uiHeight
)
214 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
217 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
219 EnterCriticalSection(&This
->encoder
->lock
);
221 if (!This
->initialized
|| This
->icns_image
)
223 hr
= WINCODEC_ERR_WRONGSTATE
;
227 if (uiWidth
!= uiHeight
)
229 WARN("cannot generate ICNS icon from %dx%d image\n", uiWidth
, uiHeight
);
244 WARN("cannot generate ICNS icon from %dx%d image\n", This
->size
, This
->size
);
249 This
->size
= uiWidth
;
252 LeaveCriticalSection(&This
->encoder
->lock
);
256 static HRESULT WINAPI
IcnsFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
257 double dpiX
, double dpiY
)
259 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
262 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
264 EnterCriticalSection(&This
->encoder
->lock
);
266 if (!This
->initialized
|| This
->icns_image
)
268 hr
= WINCODEC_ERR_WRONGSTATE
;
273 LeaveCriticalSection(&This
->encoder
->lock
);
277 static HRESULT WINAPI
IcnsFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
278 WICPixelFormatGUID
*pPixelFormat
)
280 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
283 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
285 EnterCriticalSection(&This
->encoder
->lock
);
287 if (!This
->initialized
|| This
->icns_image
)
289 hr
= WINCODEC_ERR_WRONGSTATE
;
293 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGRA
, sizeof(GUID
));
296 LeaveCriticalSection(&This
->encoder
->lock
);
300 static HRESULT WINAPI
IcnsFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
301 UINT cCount
, IWICColorContext
**ppIColorContext
)
303 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
307 static HRESULT WINAPI
IcnsFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
308 IWICPalette
*pIPalette
)
310 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
311 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
314 static HRESULT WINAPI
IcnsFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
315 IWICBitmapSource
*pIThumbnail
)
317 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
318 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
321 static HRESULT WINAPI
IcnsFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
322 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
324 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
328 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
330 EnterCriticalSection(&This
->encoder
->lock
);
332 if (!This
->initialized
|| !This
->size
)
334 hr
= WINCODEC_ERR_WRONGSTATE
;
337 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->size
)
343 if (!This
->icns_image
)
347 case 16: This
->icns_type
= kIconServices16PixelDataARGB
; break;
348 case 32: This
->icns_type
= kIconServices32PixelDataARGB
; break;
349 case 48: This
->icns_type
= kIconServices48PixelDataARGB
; break;
350 case 128: This
->icns_type
= kIconServices128PixelDataARGB
; break;
351 case 256: This
->icns_type
= kIconServices256PixelDataARGB
; break;
352 case 512: This
->icns_type
= kIconServices512PixelDataARGB
; break;
354 WARN("cannot generate ICNS icon from %dx%d image\n", This
->size
, This
->size
);
358 This
->icns_image
= HeapAlloc(GetProcessHeap(), 0, This
->size
* This
->size
* 4);
359 if (!This
->icns_image
)
361 WARN("failed to allocate image buffer\n");
367 for (i
= 0; i
< lineCount
; i
++)
369 BYTE
*src_row
, *dst_row
;
371 src_row
= pbPixels
+ cbStride
* i
;
372 dst_row
= This
->icns_image
+ (This
->lines_written
+ i
)*(This
->size
*4);
373 /* swap bgr -> rgb */
374 for (j
= 0; j
< This
->size
*4; j
+= 4)
376 dst_row
[j
] = src_row
[j
+3];
377 dst_row
[j
+1] = src_row
[j
+2];
378 dst_row
[j
+2] = src_row
[j
+1];
379 dst_row
[j
+3] = src_row
[j
];
382 This
->lines_written
+= lineCount
;
385 LeaveCriticalSection(&This
->encoder
->lock
);
389 static HRESULT WINAPI
IcnsFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
390 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
392 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
395 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
397 if (!This
->initialized
)
398 return WINCODEC_ERR_WRONGSTATE
;
400 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
401 &GUID_WICPixelFormat32bppBGRA
, This
->size
, This
->size
,
406 hr
= write_source(iface
, pIBitmapSource
, prc
,
407 &GUID_WICPixelFormat32bppBGRA
, 32, This
->size
, This
->size
);
413 static HRESULT WINAPI
IcnsFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
415 IcnsFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
420 TRACE("(%p): stub\n", iface
);
422 EnterCriticalSection(&This
->encoder
->lock
);
424 if (!This
->icns_image
|| This
->lines_written
!= This
->size
|| This
->committed
)
426 hr
= WINCODEC_ERR_WRONGSTATE
;
430 ret
= PtrToHand(This
->icns_image
, &handle
, This
->size
* This
->size
* 4);
431 if (ret
!= noErr
|| !handle
)
433 WARN("PtrToHand failed with error %d\n", ret
);
438 ret
= SetIconFamilyData(This
->encoder
->icns_family
, This
->icns_type
, handle
);
439 DisposeHandle(handle
);
443 WARN("SetIconFamilyData failed for image with error %d\n", ret
);
448 This
->committed
= TRUE
;
449 This
->encoder
->any_frame_committed
= TRUE
;
450 This
->encoder
->outstanding_commits
--;
453 LeaveCriticalSection(&This
->encoder
->lock
);
457 static HRESULT WINAPI
IcnsFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
458 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
460 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
464 static const IWICBitmapFrameEncodeVtbl IcnsEncoder_FrameVtbl
= {
465 IcnsFrameEncode_QueryInterface
,
466 IcnsFrameEncode_AddRef
,
467 IcnsFrameEncode_Release
,
468 IcnsFrameEncode_Initialize
,
469 IcnsFrameEncode_SetSize
,
470 IcnsFrameEncode_SetResolution
,
471 IcnsFrameEncode_SetPixelFormat
,
472 IcnsFrameEncode_SetColorContexts
,
473 IcnsFrameEncode_SetPalette
,
474 IcnsFrameEncode_SetThumbnail
,
475 IcnsFrameEncode_WritePixels
,
476 IcnsFrameEncode_WriteSource
,
477 IcnsFrameEncode_Commit
,
478 IcnsFrameEncode_GetMetadataQueryWriter
481 static HRESULT WINAPI
IcnsEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
484 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
485 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
487 if (!ppv
) return E_INVALIDARG
;
489 if (IsEqualIID(&IID_IUnknown
, iid
) ||
490 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
492 *ppv
= &This
->IWICBitmapEncoder_iface
;
497 return E_NOINTERFACE
;
500 IUnknown_AddRef((IUnknown
*)*ppv
);
504 static ULONG WINAPI
IcnsEncoder_AddRef(IWICBitmapEncoder
*iface
)
506 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
507 ULONG ref
= InterlockedIncrement(&This
->ref
);
509 TRACE("(%p) refcount=%u\n", iface
, ref
);
514 static ULONG WINAPI
IcnsEncoder_Release(IWICBitmapEncoder
*iface
)
516 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
517 ULONG ref
= InterlockedDecrement(&This
->ref
);
519 TRACE("(%p) refcount=%u\n", iface
, ref
);
523 This
->lock
.DebugInfo
->Spare
[0] = 0;
524 DeleteCriticalSection(&This
->lock
);
525 if (This
->icns_family
)
526 DisposeHandle((Handle
)This
->icns_family
);
528 IStream_Release(This
->stream
);
529 HeapFree(GetProcessHeap(), 0, This
);
535 static HRESULT WINAPI
IcnsEncoder_Initialize(IWICBitmapEncoder
*iface
,
536 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
538 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
541 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
543 EnterCriticalSection(&This
->lock
);
545 if (This
->icns_family
)
547 hr
= WINCODEC_ERR_WRONGSTATE
;
550 This
->icns_family
= (IconFamilyHandle
)NewHandle(0);
551 if (!This
->icns_family
)
553 WARN("error creating icns family\n");
557 IStream_AddRef(pIStream
);
558 This
->stream
= pIStream
;
561 LeaveCriticalSection(&This
->lock
);
566 static HRESULT WINAPI
IcnsEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
567 GUID
*pguidContainerFormat
)
569 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
573 static HRESULT WINAPI
IcnsEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
574 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
576 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
580 static HRESULT WINAPI
IcnsEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
581 UINT cCount
, IWICColorContext
**ppIColorContext
)
583 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
587 static HRESULT WINAPI
IcnsEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
589 TRACE("(%p,%p)\n", iface
, pIPalette
);
590 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
593 static HRESULT WINAPI
IcnsEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
595 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
596 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
599 static HRESULT WINAPI
IcnsEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
601 TRACE("(%p,%p)\n", iface
, pIPreview
);
602 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
605 static HRESULT WINAPI
IcnsEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
606 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
608 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
610 IcnsFrameEncode
*frameEncode
= NULL
;
612 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
614 EnterCriticalSection(&This
->lock
);
616 if (!This
->icns_family
)
618 hr
= WINCODEC_ERR_NOTINITIALIZED
;
622 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
626 frameEncode
= HeapAlloc(GetProcessHeap(), 0, sizeof(IcnsFrameEncode
));
627 if (frameEncode
== NULL
)
632 frameEncode
->IWICBitmapFrameEncode_iface
.lpVtbl
= &IcnsEncoder_FrameVtbl
;
633 frameEncode
->encoder
= This
;
634 frameEncode
->ref
= 1;
635 frameEncode
->initialized
= FALSE
;
636 frameEncode
->size
= 0;
637 frameEncode
->icns_image
= NULL
;
638 frameEncode
->lines_written
= 0;
639 frameEncode
->committed
= FALSE
;
640 *ppIFrameEncode
= &frameEncode
->IWICBitmapFrameEncode_iface
;
641 This
->outstanding_commits
++;
642 IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
645 LeaveCriticalSection(&This
->lock
);
650 static HRESULT WINAPI
IcnsEncoder_Commit(IWICBitmapEncoder
*iface
)
652 IcnsEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
657 TRACE("(%p)\n", iface
);
659 EnterCriticalSection(&This
->lock
);
661 if (!This
->any_frame_committed
|| This
->outstanding_commits
> 0 || This
->committed
)
663 hr
= WINCODEC_ERR_WRONGSTATE
;
667 buffer_size
= GetHandleSize((Handle
)This
->icns_family
);
668 hr
= IStream_Write(This
->stream
, *This
->icns_family
, buffer_size
, &byteswritten
);
669 if (FAILED(hr
) || byteswritten
!= buffer_size
)
671 WARN("writing file failed, hr = 0x%08X\n", hr
);
676 This
->committed
= TRUE
;
679 LeaveCriticalSection(&This
->lock
);
683 static HRESULT WINAPI
IcnsEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
684 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
686 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
690 static const IWICBitmapEncoderVtbl IcnsEncoder_Vtbl
= {
691 IcnsEncoder_QueryInterface
,
694 IcnsEncoder_Initialize
,
695 IcnsEncoder_GetContainerFormat
,
696 IcnsEncoder_GetEncoderInfo
,
697 IcnsEncoder_SetColorContexts
,
698 IcnsEncoder_SetPalette
,
699 IcnsEncoder_SetThumbnail
,
700 IcnsEncoder_SetPreview
,
701 IcnsEncoder_CreateNewFrame
,
703 IcnsEncoder_GetMetadataQueryWriter
706 HRESULT
IcnsEncoder_CreateInstance(REFIID iid
, void** ppv
)
711 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
715 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(IcnsEncoder
));
716 if (!This
) return E_OUTOFMEMORY
;
718 This
->IWICBitmapEncoder_iface
.lpVtbl
= &IcnsEncoder_Vtbl
;
721 This
->icns_family
= NULL
;
722 This
->any_frame_committed
= FALSE
;
723 This
->outstanding_commits
= 0;
724 This
->committed
= FALSE
;
725 InitializeCriticalSection(&This
->lock
);
726 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": IcnsEncoder.lock");
728 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
729 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
734 #else /* !defined(HAVE_APPLICATIONSERVICES_APPLICATIONSERVICES_H) ||
735 MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 */
737 HRESULT
IcnsEncoder_CreateInstance(REFIID iid
, void** ppv
)
739 ERR("Trying to save ICNS picture, but ICNS support is not compiled in.\n");