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