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 <QuickTime/QuickTime.h>
35 #include <postmac.h>
36 
37 #include "PictToBmpFlt.hxx"
38 
39 bool PICTtoPNG( com::sun::star::uno::Sequence<sal_Int8>& rPictData,
40 			com::sun::star::uno::Sequence<sal_Int8>& rPngData)
41 {
42 #ifdef MAC_OS_X_VERSION_10_6
43 	return false;
44 #else // MAC_OS_X_VERSION_10_6
45 	ComponentInstance pngExporter = NULL;
46 	if( OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypePNG, &pngExporter) != noErr)
47 		return false;
48 
49 	Handle hPict = NULL;
50 	if( PtrToHand( rPictData.getArray(), &hPict, rPictData.getLength()) != noErr)
51 		hPict = NULL;
52 
53 	Handle hPng = NULL;
54 	if( hPict && GraphicsExportSetInputPicture( pngExporter, (PicHandle)hPict) == noErr)
55 		hPng = NewHandleClear(0);
56 
57 	size_t nPngSize = 0;
58 	if( hPng
59 	&& (GraphicsExportSetOutputHandle( pngExporter, hPng) == noErr)
60 	&& (GraphicsExportDoExport( pngExporter, NULL) == noErr))
61 	{
62 		nPngSize = GetHandleSize( hPng);
63 		rPngData.realloc( nPngSize);
64 
65 		HLock( hPng);
66 		rtl_copyMemory( rPngData.getArray(), ((sal_Int8*)*hPng), nPngSize);
67 		HUnlock( hPng);
68 	}
69 
70 	if( hPict)
71 		DisposeHandle( hPict);
72 	if( hPng)
73 		DisposeHandle( hPng);
74 	if( pngExporter)
75 		CloseComponent( pngExporter);
76 
77 	return (nPngSize > 0);
78 #endif // MAC_OS_X_VERSION_10_6
79 }
80 
81 
82 bool PNGtoPICT( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
83 			   com::sun::star::uno::Sequence<sal_Int8>& rPictData)
84 {
85 #ifdef MAC_OS_X_VERSION_10_6
86 	return false;
87 #else // MAC_OS_X_VERSION_10_6
88 	ComponentInstance pictExporter;
89 	if( OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypePNG, &pictExporter) != noErr)
90 		return false;
91 
92 	Handle hPng = NULL;
93 	if( PtrToHand( rPngData.getArray(), &hPng, rPngData.getLength()) != noErr)
94 		hPng = NULL;
95 
96 	size_t nPictSize = 0;
97 	PicHandle hPict = NULL;
98 	if( hPng
99 	&& (GraphicsImportSetDataHandle( pictExporter, hPng) == noErr)
100 	&& (GraphicsImportGetAsPicture( pictExporter, &hPict) == noErr))
101 	{
102 		nPictSize = GetHandleSize( (Handle)hPict);
103 		rPictData.realloc( nPictSize);
104 
105 		HLock( (Handle)hPict);
106 		rtl_copyMemory( rPictData.getArray(), ((sal_Int8*)*hPict), nPictSize);
107 		HUnlock( (Handle)hPict);
108 
109 #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
110 		// Release the data associated with the picture
111 		// Note: This function has been deprecated in OSX 10.4 and removed in OSX 10.7
112 		KillPicture( hPict);
113 #endif
114 	}
115 
116 	if( hPng)
117 		DisposeHandle( hPng);
118 	if( pictExporter)
119 		CloseComponent( pictExporter);
120 
121 	return (nPictSize > 512);
122 #endif // MAC_OS_X_VERSION_10_6
123 }
124 
125 bool ImageToPNG( com::sun::star::uno::Sequence<sal_Int8>& rImgData,
126 			     com::sun::star::uno::Sequence<sal_Int8>& rPngData,
127 			     NSBitmapImageFileType eInFormat)
128 {
129 	// short circuit for PNG->PNG request
130 	if( eInFormat == NSPNGFileType) {
131 		rPngData = rImgData;
132 		return true;
133 	}
134 
135 	// special handling for old PICT images that are not supported by NSBitmapImage
136 	if( eInFormat == PICTImageFileType)
137 		return PICTtoPNG( rImgData, rPngData);
138 
139 	// let Cocoa's NSBitmapImageRep do the conversion
140 	NSData* pData = [NSData dataWithBytesNoCopy: (void*)rImgData.getConstArray() length: rImgData.getLength() freeWhenDone: 0];
141 	if( !pData)
142 		return false;
143 
144 	NSBitmapImageRep* pRep =[NSBitmapImageRep imageRepWithData: pData];
145         if( !pRep)
146 		return false;
147 
148 	NSData* pOut = [pRep representationUsingType: NSPNGFileType properties: nil];
149 	if( !pOut)
150 		return false;
151 
152 	// get the conversion result
153 	const size_t nPngSize = [pOut length];
154 	rPngData.realloc( nPngSize);
155 	[pOut getBytes: rPngData.getArray() length: nPngSize];
156 	return (nPngSize > 0);
157 }
158 
159 bool PNGToImage( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
160 			     com::sun::star::uno::Sequence<sal_Int8>& rImgData,
161 			     NSBitmapImageFileType eOutFormat
162 			    )
163 {
164 	// short circuit for PNG->PNG request
165 	if( eOutFormat == NSPNGFileType) {
166 		rImgData = rPngData;
167 		return true;
168 	}
169 
170 	// special handling for old PICT images that are not supported by NSBitmapImage
171 	if( eOutFormat == PICTImageFileType)
172 		return PNGtoPICT( rPngData, rImgData);
173 
174 	// let Cocoa's NSBitmapImageRep do the conversion
175 	NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(rPngData.getConstArray()) length: rPngData.getLength() freeWhenDone: 0];
176 	if( !pData)
177 		return false;
178 
179         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
180         if( !pRep)
181 		return false;
182 
183 	NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
184 	if( !pOut)
185 		return false;
186 
187 	// get the conversion result
188 	const size_t nImgSize = [pOut length];
189 	rImgData.realloc( nImgSize);
190 	[pOut getBytes: rImgData.getArray() length: nImgSize];
191 	return (nImgSize > 0);
192 }
193 
194