1c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c82f2877SAndrew Rist  * distributed with this work for additional information
6c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10c82f2877SAndrew Rist  *
11c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12c82f2877SAndrew Rist  *
13c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17c82f2877SAndrew Rist  * specific language governing permissions and limitations
18c82f2877SAndrew Rist  * under the License.
19c82f2877SAndrew Rist  *
20c82f2877SAndrew Rist  *************************************************************/
21c82f2877SAndrew Rist 
22c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /* This is a work-around to prevent 'deprecated' warning for 'KillPicture' API
25cdf0e10cSrcweir    Hopefully we can get rid of this whole code again when the OOo PICT filter
26cdf0e10cSrcweir    are good enough to be used see #i78953 thus this hack would vanish to again.
27cdf0e10cSrcweir  */
28cdf0e10cSrcweir #include <premac.h>
29cdf0e10cSrcweir #include <AvailabilityMacros.h>
30cdf0e10cSrcweir #undef DEPRECATED_ATTRIBUTE
31cdf0e10cSrcweir #define DEPRECATED_ATTRIBUTE
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <Carbon/Carbon.h>
34cdf0e10cSrcweir #include <QuickTime/QuickTime.h>
35cdf0e10cSrcweir #include <postmac.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "PictToBmpFlt.hxx"
38cdf0e10cSrcweir 
3945fd3b9aSArmin Le Grand bool PICTtoPNG( com::sun::star::uno::Sequence<sal_Int8>& rPictData,
4045fd3b9aSArmin Le Grand 			com::sun::star::uno::Sequence<sal_Int8>& rPngData)
41cdf0e10cSrcweir {
42*76e2130fSHerbert Dürr #ifdef MAC_OS_X_VERSION_10_6
43*76e2130fSHerbert Dürr 	return false;
44*76e2130fSHerbert Dürr #else // MAC_OS_X_VERSION_10_6
4545fd3b9aSArmin Le Grand 	ComponentInstance pngExporter = NULL;
4645fd3b9aSArmin Le Grand 	if( OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypePNG, &pngExporter) != noErr)
4745fd3b9aSArmin Le Grand 		return false;
4845fd3b9aSArmin Le Grand 
4945fd3b9aSArmin Le Grand 	Handle hPict = NULL;
5045fd3b9aSArmin Le Grand 	if( PtrToHand( rPictData.getArray(), &hPict, rPictData.getLength()) != noErr)
5145fd3b9aSArmin Le Grand 		hPict = NULL;
5245fd3b9aSArmin Le Grand 
5345fd3b9aSArmin Le Grand 	Handle hPng = NULL;
5445fd3b9aSArmin Le Grand 	if( hPict && GraphicsExportSetInputPicture( pngExporter, (PicHandle)hPict) == noErr)
5545fd3b9aSArmin Le Grand 		hPng = NewHandleClear(0);
5645fd3b9aSArmin Le Grand 
5745fd3b9aSArmin Le Grand 	size_t nPngSize = 0;
5845fd3b9aSArmin Le Grand 	if( hPng
5945fd3b9aSArmin Le Grand 	&& (GraphicsExportSetOutputHandle( pngExporter, hPng) == noErr)
6045fd3b9aSArmin Le Grand 	&& (GraphicsExportDoExport( pngExporter, NULL) == noErr))
61cdf0e10cSrcweir 	{
6245fd3b9aSArmin Le Grand 		nPngSize = GetHandleSize( hPng);
6345fd3b9aSArmin Le Grand 		rPngData.realloc( nPngSize);
64cdf0e10cSrcweir 
6545fd3b9aSArmin Le Grand 		HLock( hPng);
6645fd3b9aSArmin Le Grand 		rtl_copyMemory( rPngData.getArray(), ((sal_Int8*)*hPng), nPngSize);
6745fd3b9aSArmin Le Grand 		HUnlock( hPng);
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir 
7045fd3b9aSArmin Le Grand 	if( hPict)
7145fd3b9aSArmin Le Grand 		DisposeHandle( hPict);
7245fd3b9aSArmin Le Grand 	if( hPng)
7345fd3b9aSArmin Le Grand 		DisposeHandle( hPng);
7445fd3b9aSArmin Le Grand 	if( pngExporter)
7545fd3b9aSArmin Le Grand 		CloseComponent( pngExporter);
76cdf0e10cSrcweir 
7745fd3b9aSArmin Le Grand 	return (nPngSize > 0);
78*76e2130fSHerbert Dürr #endif // MAC_OS_X_VERSION_10_6
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
8245fd3b9aSArmin Le Grand bool PNGtoPICT( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
8345fd3b9aSArmin Le Grand 			   com::sun::star::uno::Sequence<sal_Int8>& rPictData)
8445fd3b9aSArmin Le Grand {
85*76e2130fSHerbert Dürr #ifdef MAC_OS_X_VERSION_10_6
86*76e2130fSHerbert Dürr 	return false;
87*76e2130fSHerbert Dürr #else // MAC_OS_X_VERSION_10_6
8845fd3b9aSArmin Le Grand 	ComponentInstance pictExporter;
8945fd3b9aSArmin Le Grand 	if( OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypePNG, &pictExporter) != noErr)
9045fd3b9aSArmin Le Grand 		return false;
91cdf0e10cSrcweir 
9245fd3b9aSArmin Le Grand 	Handle hPng = NULL;
9345fd3b9aSArmin Le Grand 	if( PtrToHand( rPngData.getArray(), &hPng, rPngData.getLength()) != noErr)
9445fd3b9aSArmin Le Grand 		hPng = NULL;
95cdf0e10cSrcweir 
9645fd3b9aSArmin Le Grand 	size_t nPictSize = 0;
9745fd3b9aSArmin Le Grand 	PicHandle hPict = NULL;
9845fd3b9aSArmin Le Grand 	if( hPng
9945fd3b9aSArmin Le Grand 	&& (GraphicsImportSetDataHandle( pictExporter, hPng) == noErr)
10045fd3b9aSArmin Le Grand 	&& (GraphicsImportGetAsPicture( pictExporter, &hPict) == noErr))
101cdf0e10cSrcweir 	{
10245fd3b9aSArmin Le Grand 		nPictSize = GetHandleSize( (Handle)hPict);
10345fd3b9aSArmin Le Grand 		rPictData.realloc( nPictSize);
104cdf0e10cSrcweir 
10545fd3b9aSArmin Le Grand 		HLock( (Handle)hPict);
10645fd3b9aSArmin Le Grand 		rtl_copyMemory( rPictData.getArray(), ((sal_Int8*)*hPict), nPictSize);
10745fd3b9aSArmin Le Grand 		HUnlock( (Handle)hPict);
108cdf0e10cSrcweir 
109*76e2130fSHerbert Dürr #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
11045fd3b9aSArmin Le Grand 		// Release the data associated with the picture
111*76e2130fSHerbert Dürr 		// Note: This function has been deprecated in OSX 10.4 and removed in OSX 10.7
11245fd3b9aSArmin Le Grand 		KillPicture( hPict);
113*76e2130fSHerbert Dürr #endif
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir 
11645fd3b9aSArmin Le Grand 	if( hPng)
11745fd3b9aSArmin Le Grand 		DisposeHandle( hPng);
11845fd3b9aSArmin Le Grand 	if( pictExporter)
11945fd3b9aSArmin Le Grand 		CloseComponent( pictExporter);
120cdf0e10cSrcweir 
12145fd3b9aSArmin Le Grand 	return (nPictSize > 512);
122*76e2130fSHerbert Dürr #endif // MAC_OS_X_VERSION_10_6
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
12545fd3b9aSArmin Le Grand bool ImageToPNG( com::sun::star::uno::Sequence<sal_Int8>& rImgData,
12645fd3b9aSArmin Le Grand 			     com::sun::star::uno::Sequence<sal_Int8>& rPngData,
127cdf0e10cSrcweir 			     NSBitmapImageFileType eInFormat)
128cdf0e10cSrcweir {
129*76e2130fSHerbert Dürr 	// short circuit for PNG->PNG request
130*76e2130fSHerbert Dürr 	if( eInFormat == NSPNGFileType) {
131*76e2130fSHerbert Dürr 		rPngData = rImgData;
132*76e2130fSHerbert Dürr 		return true;
133*76e2130fSHerbert Dürr 	}
134*76e2130fSHerbert Dürr 
135*76e2130fSHerbert Dürr 	// special handling for old PICT images that are not supported by NSBitmapImage
13645fd3b9aSArmin Le Grand 	if( eInFormat == PICTImageFileType)
13745fd3b9aSArmin Le Grand 		return PICTtoPNG( rImgData, rPngData);
13845fd3b9aSArmin Le Grand 
139*76e2130fSHerbert Dürr 	// let Cocoa's NSBitmapImageRep do the conversion
14045fd3b9aSArmin Le Grand 	NSData* pData = [NSData dataWithBytesNoCopy: (void*)rImgData.getConstArray() length: rImgData.getLength() freeWhenDone: 0];
14145fd3b9aSArmin Le Grand 	if( !pData)
14245fd3b9aSArmin Le Grand 		return false;
14345fd3b9aSArmin Le Grand 
14445fd3b9aSArmin Le Grand 	NSBitmapImageRep* pRep =[NSBitmapImageRep imageRepWithData: pData];
14545fd3b9aSArmin Le Grand         if( !pRep)
14645fd3b9aSArmin Le Grand 		return false;
14745fd3b9aSArmin Le Grand 
14845fd3b9aSArmin Le Grand 	NSData* pOut = [pRep representationUsingType: NSPNGFileType properties: nil];
14945fd3b9aSArmin Le Grand 	if( !pOut)
15045fd3b9aSArmin Le Grand 		return false;
15145fd3b9aSArmin Le Grand 
152*76e2130fSHerbert Dürr 	// get the conversion result
15345fd3b9aSArmin Le Grand 	const size_t nPngSize = [pOut length];
15445fd3b9aSArmin Le Grand 	rPngData.realloc( nPngSize);
15545fd3b9aSArmin Le Grand 	[pOut getBytes: rPngData.getArray() length: nPngSize];
15645fd3b9aSArmin Le Grand 	return (nPngSize > 0);
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
15945fd3b9aSArmin Le Grand bool PNGToImage( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
16045fd3b9aSArmin Le Grand 			     com::sun::star::uno::Sequence<sal_Int8>& rImgData,
161cdf0e10cSrcweir 			     NSBitmapImageFileType eOutFormat
162cdf0e10cSrcweir 			    )
163cdf0e10cSrcweir {
164*76e2130fSHerbert Dürr 	// short circuit for PNG->PNG request
165*76e2130fSHerbert Dürr 	if( eOutFormat == NSPNGFileType) {
166*76e2130fSHerbert Dürr 		rImgData = rPngData;
167*76e2130fSHerbert Dürr 		return true;
168*76e2130fSHerbert Dürr 	}
169*76e2130fSHerbert Dürr 
170*76e2130fSHerbert Dürr 	// special handling for old PICT images that are not supported by NSBitmapImage
17145fd3b9aSArmin Le Grand 	if( eOutFormat == PICTImageFileType)
17245fd3b9aSArmin Le Grand 		return PNGtoPICT( rPngData, rImgData);
173*76e2130fSHerbert Dürr 
174*76e2130fSHerbert Dürr 	// let Cocoa's NSBitmapImageRep do the conversion
17545fd3b9aSArmin Le Grand 	NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(rPngData.getConstArray()) length: rPngData.getLength() freeWhenDone: 0];
17645fd3b9aSArmin Le Grand 	if( !pData)
17745fd3b9aSArmin Le Grand 		return false;
17845fd3b9aSArmin Le Grand 
179cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
18045fd3b9aSArmin Le Grand         if( !pRep)
18145fd3b9aSArmin Le Grand 		return false;
18245fd3b9aSArmin Le Grand 
18345fd3b9aSArmin Le Grand 	NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
18445fd3b9aSArmin Le Grand 	if( !pOut)
18545fd3b9aSArmin Le Grand 		return false;
18645fd3b9aSArmin Le Grand 
187*76e2130fSHerbert Dürr 	// get the conversion result
18845fd3b9aSArmin Le Grand 	const size_t nImgSize = [pOut length];
18945fd3b9aSArmin Le Grand 	rImgData.realloc( nImgSize);
19045fd3b9aSArmin Le Grand 	[pOut getBytes: rImgData.getArray() length: nImgSize];
19145fd3b9aSArmin Le Grand 	return (nImgSize > 0);
192cdf0e10cSrcweir }
19345fd3b9aSArmin Le Grand 
194