1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /* This is a work-around to prevent 'deprecated' warning for 'KillPicture' API
25    Hopefully we can get rid of this whole code again when the OOo PICT filter
26    are good enough to be used see #i78953 thus this hack would vanish to again.
27  */
28 #include <premac.h>
29 #include <AvailabilityMacros.h>
30 #undef DEPRECATED_ATTRIBUTE
31 #define DEPRECATED_ATTRIBUTE
32 
33 #include <Carbon/Carbon.h>
34 #include <postmac.h>
35 
36 #include "PictToBmpFlt.hxx"
37 
PICTtoPNG(com::sun::star::uno::Sequence<sal_Int8> & rPictData,com::sun::star::uno::Sequence<sal_Int8> & rPngData)38 bool PICTtoPNG( com::sun::star::uno::Sequence<sal_Int8>& rPictData,
39 			com::sun::star::uno::Sequence<sal_Int8>& rPngData)
40 {
41 #ifdef MAC_OS_X_VERSION_10_6
42 	return false;
43 #else // MAC_OS_X_VERSION_10_6
44 	ComponentInstance pngExporter = NULL;
45 	if( OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypePNG, &pngExporter) != noErr)
46 		return false;
47 
48 	Handle hPict = NULL;
49 	if( PtrToHand( rPictData.getArray(), &hPict, rPictData.getLength()) != noErr)
50 		hPict = NULL;
51 
52 	Handle hPng = NULL;
53 	if( hPict && GraphicsExportSetInputPicture( pngExporter, (PicHandle)hPict) == noErr)
54 		hPng = NewHandleClear(0);
55 
56 	size_t nPngSize = 0;
57 	if( hPng
58 	&& (GraphicsExportSetOutputHandle( pngExporter, hPng) == noErr)
59 	&& (GraphicsExportDoExport( pngExporter, NULL) == noErr))
60 	{
61 		nPngSize = GetHandleSize( hPng);
62 		rPngData.realloc( nPngSize);
63 
64 		HLock( hPng);
65 		rtl_copyMemory( rPngData.getArray(), ((sal_Int8*)*hPng), nPngSize);
66 		HUnlock( hPng);
67 	}
68 
69 	if( hPict)
70 		DisposeHandle( hPict);
71 	if( hPng)
72 		DisposeHandle( hPng);
73 	if( pngExporter)
74 		CloseComponent( pngExporter);
75 
76 	return (nPngSize > 0);
77 #endif // MAC_OS_X_VERSION_10_6
78 }
79 
80 
PNGtoPICT(com::sun::star::uno::Sequence<sal_Int8> & rPngData,com::sun::star::uno::Sequence<sal_Int8> & rPictData)81 bool PNGtoPICT( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
82 			   com::sun::star::uno::Sequence<sal_Int8>& rPictData)
83 {
84 #ifdef MAC_OS_X_VERSION_10_6
85 	return false;
86 #else // MAC_OS_X_VERSION_10_6
87 	ComponentInstance pictExporter;
88 	if( OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypePNG, &pictExporter) != noErr)
89 		return false;
90 
91 	Handle hPng = NULL;
92 	if( PtrToHand( rPngData.getArray(), &hPng, rPngData.getLength()) != noErr)
93 		hPng = NULL;
94 
95 	size_t nPictSize = 0;
96 	PicHandle hPict = NULL;
97 	if( hPng
98 	&& (GraphicsImportSetDataHandle( pictExporter, hPng) == noErr)
99 	&& (GraphicsImportGetAsPicture( pictExporter, &hPict) == noErr))
100 	{
101 		nPictSize = GetHandleSize( (Handle)hPict);
102 		rPictData.realloc( nPictSize);
103 
104 		HLock( (Handle)hPict);
105 		rtl_copyMemory( rPictData.getArray(), ((sal_Int8*)*hPict), nPictSize);
106 		HUnlock( (Handle)hPict);
107 
108 #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
109 		// Release the data associated with the picture
110 		// Note: This function has been deprecated in OSX 10.4 and removed in OSX 10.7
111 		KillPicture( hPict);
112 #endif
113 	}
114 
115 	if( hPng)
116 		DisposeHandle( hPng);
117 	if( pictExporter)
118 		CloseComponent( pictExporter);
119 
120 	return (nPictSize > 512);
121 #endif // MAC_OS_X_VERSION_10_6
122 }
123 
ImageToPNG(com::sun::star::uno::Sequence<sal_Int8> & rImgData,com::sun::star::uno::Sequence<sal_Int8> & rPngData,NSBitmapImageFileType eInFormat)124 bool ImageToPNG( com::sun::star::uno::Sequence<sal_Int8>& rImgData,
125 			     com::sun::star::uno::Sequence<sal_Int8>& rPngData,
126 			     NSBitmapImageFileType eInFormat)
127 {
128 	// short circuit for PNG->PNG request
129 	if( eInFormat == NSPNGFileType) {
130 		rPngData = rImgData;
131 		return true;
132 	}
133 
134 	// special handling for old PICT images that are not supported by NSBitmapImage
135 	if( eInFormat == PICTImageFileType)
136 		return PICTtoPNG( rImgData, rPngData);
137 
138 	// let Cocoa's NSBitmapImageRep do the conversion
139 	NSData* pData = [NSData dataWithBytesNoCopy: (void*)rImgData.getConstArray() length: rImgData.getLength() freeWhenDone: 0];
140 	if( !pData)
141 		return false;
142 
143 	NSBitmapImageRep* pRep =[NSBitmapImageRep imageRepWithData: pData];
144         if( !pRep)
145 		return false;
146 
147 	NSData* pOut = [pRep representationUsingType: NSPNGFileType properties: nil];
148 	if( !pOut)
149 		return false;
150 
151 	// get the conversion result
152 	const size_t nPngSize = [pOut length];
153 	rPngData.realloc( nPngSize);
154 	[pOut getBytes: rPngData.getArray() length: nPngSize];
155 	return (nPngSize > 0);
156 }
157 
PNGToImage(com::sun::star::uno::Sequence<sal_Int8> & rPngData,com::sun::star::uno::Sequence<sal_Int8> & rImgData,NSBitmapImageFileType eOutFormat)158 bool PNGToImage( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
159 			     com::sun::star::uno::Sequence<sal_Int8>& rImgData,
160 			     NSBitmapImageFileType eOutFormat
161 			    )
162 {
163 	// short circuit for PNG->PNG request
164 	if( eOutFormat == NSPNGFileType) {
165 		rImgData = rPngData;
166 		return true;
167 	}
168 
169 	// special handling for old PICT images that are not supported by NSBitmapImage
170 	if( eOutFormat == PICTImageFileType)
171 		return PNGtoPICT( rPngData, rImgData);
172 
173 	// let Cocoa's NSBitmapImageRep do the conversion
174 	NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(rPngData.getConstArray()) length: rPngData.getLength() freeWhenDone: 0];
175 	if( !pData)
176 		return false;
177 
178         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
179         if( !pRep)
180 		return false;
181 
182 	NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
183 	if( !pOut)
184 		return false;
185 
186 	// get the conversion result
187 	const size_t nImgSize = [pOut length];
188 	rImgData.realloc( nImgSize);
189 	[pOut getBytes: rImgData.getArray() length: nImgSize];
190 	return (nImgSize > 0);
191 }
192 
193