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 
39*45fd3b9aSArmin Le Grand bool PICTtoPNG( com::sun::star::uno::Sequence<sal_Int8>& rPictData,
40*45fd3b9aSArmin Le Grand 			com::sun::star::uno::Sequence<sal_Int8>& rPngData)
41cdf0e10cSrcweir {
42*45fd3b9aSArmin Le Grand 	ComponentInstance pngExporter = NULL;
43*45fd3b9aSArmin Le Grand 	if( OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypePNG, &pngExporter) != noErr)
44*45fd3b9aSArmin Le Grand 		return false;
45*45fd3b9aSArmin Le Grand 
46*45fd3b9aSArmin Le Grand 	Handle hPict = NULL;
47*45fd3b9aSArmin Le Grand 	if( PtrToHand( rPictData.getArray(), &hPict, rPictData.getLength()) != noErr)
48*45fd3b9aSArmin Le Grand 		hPict = NULL;
49*45fd3b9aSArmin Le Grand 
50*45fd3b9aSArmin Le Grand 	Handle hPng = NULL;
51*45fd3b9aSArmin Le Grand 	if( hPict && GraphicsExportSetInputPicture( pngExporter, (PicHandle)hPict) == noErr)
52*45fd3b9aSArmin Le Grand 		hPng = NewHandleClear(0);
53*45fd3b9aSArmin Le Grand 
54*45fd3b9aSArmin Le Grand 	size_t nPngSize = 0;
55*45fd3b9aSArmin Le Grand 	if( hPng
56*45fd3b9aSArmin Le Grand 	&& (GraphicsExportSetOutputHandle( pngExporter, hPng) == noErr)
57*45fd3b9aSArmin Le Grand 	&& (GraphicsExportDoExport( pngExporter, NULL) == noErr))
58cdf0e10cSrcweir 	{
59*45fd3b9aSArmin Le Grand 		nPngSize = GetHandleSize( hPng);
60*45fd3b9aSArmin Le Grand 		rPngData.realloc( nPngSize);
61cdf0e10cSrcweir 
62*45fd3b9aSArmin Le Grand 		HLock( hPng);
63*45fd3b9aSArmin Le Grand 		rtl_copyMemory( rPngData.getArray(), ((sal_Int8*)*hPng), nPngSize);
64*45fd3b9aSArmin Le Grand 		HUnlock( hPng);
65cdf0e10cSrcweir 	}
66cdf0e10cSrcweir 
67*45fd3b9aSArmin Le Grand 	if( hPict)
68*45fd3b9aSArmin Le Grand 		DisposeHandle( hPict);
69*45fd3b9aSArmin Le Grand 	if( hPng)
70*45fd3b9aSArmin Le Grand 		DisposeHandle( hPng);
71*45fd3b9aSArmin Le Grand 	if( pngExporter)
72*45fd3b9aSArmin Le Grand 		CloseComponent( pngExporter);
73cdf0e10cSrcweir 
74*45fd3b9aSArmin Le Grand 	return (nPngSize > 0);
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78*45fd3b9aSArmin Le Grand bool PNGtoPICT( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
79*45fd3b9aSArmin Le Grand 			   com::sun::star::uno::Sequence<sal_Int8>& rPictData)
80*45fd3b9aSArmin Le Grand {
81*45fd3b9aSArmin Le Grand 	ComponentInstance pictExporter;
82*45fd3b9aSArmin Le Grand 	if( OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypePNG, &pictExporter) != noErr)
83*45fd3b9aSArmin Le Grand 		return false;
84cdf0e10cSrcweir 
85*45fd3b9aSArmin Le Grand 	Handle hPng = NULL;
86*45fd3b9aSArmin Le Grand 	if( PtrToHand( rPngData.getArray(), &hPng, rPngData.getLength()) != noErr)
87*45fd3b9aSArmin Le Grand 		hPng = NULL;
88cdf0e10cSrcweir 
89*45fd3b9aSArmin Le Grand 	size_t nPictSize = 0;
90*45fd3b9aSArmin Le Grand 	PicHandle hPict = NULL;
91*45fd3b9aSArmin Le Grand 	if( hPng
92*45fd3b9aSArmin Le Grand 	&& (GraphicsImportSetDataHandle( pictExporter, hPng) == noErr)
93*45fd3b9aSArmin Le Grand 	&& (GraphicsImportGetAsPicture( pictExporter, &hPict) == noErr))
94cdf0e10cSrcweir 	{
95*45fd3b9aSArmin Le Grand 		nPictSize = GetHandleSize( (Handle)hPict);
96*45fd3b9aSArmin Le Grand 		rPictData.realloc( nPictSize);
97cdf0e10cSrcweir 
98*45fd3b9aSArmin Le Grand 		HLock( (Handle)hPict);
99*45fd3b9aSArmin Le Grand 		rtl_copyMemory( rPictData.getArray(), ((sal_Int8*)*hPict), nPictSize);
100*45fd3b9aSArmin Le Grand 		HUnlock( (Handle)hPict);
101cdf0e10cSrcweir 
102*45fd3b9aSArmin Le Grand 		// Release the data associated with the picture
103*45fd3b9aSArmin Le Grand 		// Note: This function is deprecated in Mac OSX 10.4
104*45fd3b9aSArmin Le Grand 		KillPicture( hPict);
105cdf0e10cSrcweir 	}
106cdf0e10cSrcweir 
107*45fd3b9aSArmin Le Grand 	if( hPng)
108*45fd3b9aSArmin Le Grand 		DisposeHandle( hPng);
109*45fd3b9aSArmin Le Grand 	if( pictExporter)
110*45fd3b9aSArmin Le Grand 		CloseComponent( pictExporter);
111cdf0e10cSrcweir 
112*45fd3b9aSArmin Le Grand 	return (nPictSize > 512);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115*45fd3b9aSArmin Le Grand bool ImageToPNG( com::sun::star::uno::Sequence<sal_Int8>& rImgData,
116*45fd3b9aSArmin Le Grand 			     com::sun::star::uno::Sequence<sal_Int8>& rPngData,
117cdf0e10cSrcweir 			     NSBitmapImageFileType eInFormat)
118cdf0e10cSrcweir {
119*45fd3b9aSArmin Le Grand 	if( eInFormat == PICTImageFileType)
120*45fd3b9aSArmin Le Grand 		return PICTtoPNG( rImgData, rPngData);
121*45fd3b9aSArmin Le Grand 
122*45fd3b9aSArmin Le Grand 	NSData* pData = [NSData dataWithBytesNoCopy: (void*)rImgData.getConstArray() length: rImgData.getLength() freeWhenDone: 0];
123*45fd3b9aSArmin Le Grand 	if( !pData)
124*45fd3b9aSArmin Le Grand 		return false;
125*45fd3b9aSArmin Le Grand 
126*45fd3b9aSArmin Le Grand 	NSBitmapImageRep* pRep =[NSBitmapImageRep imageRepWithData: pData];
127*45fd3b9aSArmin Le Grand         if( !pRep)
128*45fd3b9aSArmin Le Grand 		return false;
129*45fd3b9aSArmin Le Grand 
130*45fd3b9aSArmin Le Grand 	NSData* pOut = [pRep representationUsingType: NSPNGFileType properties: nil];
131*45fd3b9aSArmin Le Grand 	if( !pOut)
132*45fd3b9aSArmin Le Grand 		return false;
133*45fd3b9aSArmin Le Grand 
134*45fd3b9aSArmin Le Grand 	const size_t nPngSize = [pOut length];
135*45fd3b9aSArmin Le Grand 	rPngData.realloc( nPngSize);
136*45fd3b9aSArmin Le Grand 	[pOut getBytes: rPngData.getArray() length: nPngSize];
137*45fd3b9aSArmin Le Grand 	return (nPngSize > 0);
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140*45fd3b9aSArmin Le Grand bool PNGToImage( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
141*45fd3b9aSArmin Le Grand 			     com::sun::star::uno::Sequence<sal_Int8>& rImgData,
142cdf0e10cSrcweir 			     NSBitmapImageFileType eOutFormat
143cdf0e10cSrcweir 			    )
144cdf0e10cSrcweir {
145*45fd3b9aSArmin Le Grand 	if( eOutFormat == PICTImageFileType)
146*45fd3b9aSArmin Le Grand 		return PNGtoPICT( rPngData, rImgData);
147cdf0e10cSrcweir 
148*45fd3b9aSArmin Le Grand 	NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(rPngData.getConstArray()) length: rPngData.getLength() freeWhenDone: 0];
149*45fd3b9aSArmin Le Grand 	if( !pData)
150*45fd3b9aSArmin Le Grand 		return false;
151*45fd3b9aSArmin Le Grand 
152cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
153*45fd3b9aSArmin Le Grand         if( !pRep)
154*45fd3b9aSArmin Le Grand 		return false;
155*45fd3b9aSArmin Le Grand 
156*45fd3b9aSArmin Le Grand 	NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
157*45fd3b9aSArmin Le Grand 	if( !pOut)
158*45fd3b9aSArmin Le Grand 		return false;
159*45fd3b9aSArmin Le Grand 
160*45fd3b9aSArmin Le Grand 	const size_t nImgSize = [pOut length];
161*45fd3b9aSArmin Le Grand 	rImgData.realloc( nImgSize);
162*45fd3b9aSArmin Le Grand 	[pOut getBytes: rImgData.getArray() length: nImgSize];
163*45fd3b9aSArmin Le Grand 	return (nImgSize > 0);
164cdf0e10cSrcweir }
165*45fd3b9aSArmin Le Grand 
166