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 <postmac.h>
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include "PictToBmpFlt.hxx"
37cdf0e10cSrcweir
PICTtoPNG(com::sun::star::uno::Sequence<sal_Int8> & rPictData,com::sun::star::uno::Sequence<sal_Int8> & rPngData)3845fd3b9aSArmin Le Grand bool PICTtoPNG( com::sun::star::uno::Sequence<sal_Int8>& rPictData,
3945fd3b9aSArmin Le Grand com::sun::star::uno::Sequence<sal_Int8>& rPngData)
40cdf0e10cSrcweir {
41*76e2130fSHerbert Dürr #ifdef MAC_OS_X_VERSION_10_6
42*76e2130fSHerbert Dürr return false;
43*76e2130fSHerbert Dürr #else // MAC_OS_X_VERSION_10_6
4445fd3b9aSArmin Le Grand ComponentInstance pngExporter = NULL;
4545fd3b9aSArmin Le Grand if( OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypePNG, &pngExporter) != noErr)
4645fd3b9aSArmin Le Grand return false;
4745fd3b9aSArmin Le Grand
4845fd3b9aSArmin Le Grand Handle hPict = NULL;
4945fd3b9aSArmin Le Grand if( PtrToHand( rPictData.getArray(), &hPict, rPictData.getLength()) != noErr)
5045fd3b9aSArmin Le Grand hPict = NULL;
5145fd3b9aSArmin Le Grand
5245fd3b9aSArmin Le Grand Handle hPng = NULL;
5345fd3b9aSArmin Le Grand if( hPict && GraphicsExportSetInputPicture( pngExporter, (PicHandle)hPict) == noErr)
5445fd3b9aSArmin Le Grand hPng = NewHandleClear(0);
5545fd3b9aSArmin Le Grand
5645fd3b9aSArmin Le Grand size_t nPngSize = 0;
5745fd3b9aSArmin Le Grand if( hPng
5845fd3b9aSArmin Le Grand && (GraphicsExportSetOutputHandle( pngExporter, hPng) == noErr)
5945fd3b9aSArmin Le Grand && (GraphicsExportDoExport( pngExporter, NULL) == noErr))
60cdf0e10cSrcweir {
6145fd3b9aSArmin Le Grand nPngSize = GetHandleSize( hPng);
6245fd3b9aSArmin Le Grand rPngData.realloc( nPngSize);
63cdf0e10cSrcweir
6445fd3b9aSArmin Le Grand HLock( hPng);
6545fd3b9aSArmin Le Grand rtl_copyMemory( rPngData.getArray(), ((sal_Int8*)*hPng), nPngSize);
6645fd3b9aSArmin Le Grand HUnlock( hPng);
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
6945fd3b9aSArmin Le Grand if( hPict)
7045fd3b9aSArmin Le Grand DisposeHandle( hPict);
7145fd3b9aSArmin Le Grand if( hPng)
7245fd3b9aSArmin Le Grand DisposeHandle( hPng);
7345fd3b9aSArmin Le Grand if( pngExporter)
7445fd3b9aSArmin Le Grand CloseComponent( pngExporter);
75cdf0e10cSrcweir
7645fd3b9aSArmin Le Grand return (nPngSize > 0);
77*76e2130fSHerbert Dürr #endif // MAC_OS_X_VERSION_10_6
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir
PNGtoPICT(com::sun::star::uno::Sequence<sal_Int8> & rPngData,com::sun::star::uno::Sequence<sal_Int8> & rPictData)8145fd3b9aSArmin Le Grand bool PNGtoPICT( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
8245fd3b9aSArmin Le Grand com::sun::star::uno::Sequence<sal_Int8>& rPictData)
8345fd3b9aSArmin Le Grand {
84*76e2130fSHerbert Dürr #ifdef MAC_OS_X_VERSION_10_6
85*76e2130fSHerbert Dürr return false;
86*76e2130fSHerbert Dürr #else // MAC_OS_X_VERSION_10_6
8745fd3b9aSArmin Le Grand ComponentInstance pictExporter;
8845fd3b9aSArmin Le Grand if( OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypePNG, &pictExporter) != noErr)
8945fd3b9aSArmin Le Grand return false;
90cdf0e10cSrcweir
9145fd3b9aSArmin Le Grand Handle hPng = NULL;
9245fd3b9aSArmin Le Grand if( PtrToHand( rPngData.getArray(), &hPng, rPngData.getLength()) != noErr)
9345fd3b9aSArmin Le Grand hPng = NULL;
94cdf0e10cSrcweir
9545fd3b9aSArmin Le Grand size_t nPictSize = 0;
9645fd3b9aSArmin Le Grand PicHandle hPict = NULL;
9745fd3b9aSArmin Le Grand if( hPng
9845fd3b9aSArmin Le Grand && (GraphicsImportSetDataHandle( pictExporter, hPng) == noErr)
9945fd3b9aSArmin Le Grand && (GraphicsImportGetAsPicture( pictExporter, &hPict) == noErr))
100cdf0e10cSrcweir {
10145fd3b9aSArmin Le Grand nPictSize = GetHandleSize( (Handle)hPict);
10245fd3b9aSArmin Le Grand rPictData.realloc( nPictSize);
103cdf0e10cSrcweir
10445fd3b9aSArmin Le Grand HLock( (Handle)hPict);
10545fd3b9aSArmin Le Grand rtl_copyMemory( rPictData.getArray(), ((sal_Int8*)*hPict), nPictSize);
10645fd3b9aSArmin Le Grand HUnlock( (Handle)hPict);
107cdf0e10cSrcweir
108*76e2130fSHerbert Dürr #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
10945fd3b9aSArmin Le Grand // Release the data associated with the picture
110*76e2130fSHerbert Dürr // Note: This function has been deprecated in OSX 10.4 and removed in OSX 10.7
11145fd3b9aSArmin Le Grand KillPicture( hPict);
112*76e2130fSHerbert Dürr #endif
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
11545fd3b9aSArmin Le Grand if( hPng)
11645fd3b9aSArmin Le Grand DisposeHandle( hPng);
11745fd3b9aSArmin Le Grand if( pictExporter)
11845fd3b9aSArmin Le Grand CloseComponent( pictExporter);
119cdf0e10cSrcweir
12045fd3b9aSArmin Le Grand return (nPictSize > 512);
121*76e2130fSHerbert Dürr #endif // MAC_OS_X_VERSION_10_6
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
ImageToPNG(com::sun::star::uno::Sequence<sal_Int8> & rImgData,com::sun::star::uno::Sequence<sal_Int8> & rPngData,NSBitmapImageFileType eInFormat)12445fd3b9aSArmin Le Grand bool ImageToPNG( com::sun::star::uno::Sequence<sal_Int8>& rImgData,
12545fd3b9aSArmin Le Grand com::sun::star::uno::Sequence<sal_Int8>& rPngData,
126cdf0e10cSrcweir NSBitmapImageFileType eInFormat)
127cdf0e10cSrcweir {
128*76e2130fSHerbert Dürr // short circuit for PNG->PNG request
129*76e2130fSHerbert Dürr if( eInFormat == NSPNGFileType) {
130*76e2130fSHerbert Dürr rPngData = rImgData;
131*76e2130fSHerbert Dürr return true;
132*76e2130fSHerbert Dürr }
133*76e2130fSHerbert Dürr
134*76e2130fSHerbert Dürr // special handling for old PICT images that are not supported by NSBitmapImage
13545fd3b9aSArmin Le Grand if( eInFormat == PICTImageFileType)
13645fd3b9aSArmin Le Grand return PICTtoPNG( rImgData, rPngData);
13745fd3b9aSArmin Le Grand
138*76e2130fSHerbert Dürr // let Cocoa's NSBitmapImageRep do the conversion
13945fd3b9aSArmin Le Grand NSData* pData = [NSData dataWithBytesNoCopy: (void*)rImgData.getConstArray() length: rImgData.getLength() freeWhenDone: 0];
14045fd3b9aSArmin Le Grand if( !pData)
14145fd3b9aSArmin Le Grand return false;
14245fd3b9aSArmin Le Grand
14345fd3b9aSArmin Le Grand NSBitmapImageRep* pRep =[NSBitmapImageRep imageRepWithData: pData];
14445fd3b9aSArmin Le Grand if( !pRep)
14545fd3b9aSArmin Le Grand return false;
14645fd3b9aSArmin Le Grand
14745fd3b9aSArmin Le Grand NSData* pOut = [pRep representationUsingType: NSPNGFileType properties: nil];
14845fd3b9aSArmin Le Grand if( !pOut)
14945fd3b9aSArmin Le Grand return false;
15045fd3b9aSArmin Le Grand
151*76e2130fSHerbert Dürr // get the conversion result
15245fd3b9aSArmin Le Grand const size_t nPngSize = [pOut length];
15345fd3b9aSArmin Le Grand rPngData.realloc( nPngSize);
15445fd3b9aSArmin Le Grand [pOut getBytes: rPngData.getArray() length: nPngSize];
15545fd3b9aSArmin Le Grand return (nPngSize > 0);
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
PNGToImage(com::sun::star::uno::Sequence<sal_Int8> & rPngData,com::sun::star::uno::Sequence<sal_Int8> & rImgData,NSBitmapImageFileType eOutFormat)15845fd3b9aSArmin Le Grand bool PNGToImage( com::sun::star::uno::Sequence<sal_Int8>& rPngData,
15945fd3b9aSArmin Le Grand com::sun::star::uno::Sequence<sal_Int8>& rImgData,
160cdf0e10cSrcweir NSBitmapImageFileType eOutFormat
161cdf0e10cSrcweir )
162cdf0e10cSrcweir {
163*76e2130fSHerbert Dürr // short circuit for PNG->PNG request
164*76e2130fSHerbert Dürr if( eOutFormat == NSPNGFileType) {
165*76e2130fSHerbert Dürr rImgData = rPngData;
166*76e2130fSHerbert Dürr return true;
167*76e2130fSHerbert Dürr }
168*76e2130fSHerbert Dürr
169*76e2130fSHerbert Dürr // special handling for old PICT images that are not supported by NSBitmapImage
17045fd3b9aSArmin Le Grand if( eOutFormat == PICTImageFileType)
17145fd3b9aSArmin Le Grand return PNGtoPICT( rPngData, rImgData);
172*76e2130fSHerbert Dürr
173*76e2130fSHerbert Dürr // let Cocoa's NSBitmapImageRep do the conversion
17445fd3b9aSArmin Le Grand NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(rPngData.getConstArray()) length: rPngData.getLength() freeWhenDone: 0];
17545fd3b9aSArmin Le Grand if( !pData)
17645fd3b9aSArmin Le Grand return false;
17745fd3b9aSArmin Le Grand
178cdf0e10cSrcweir NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
17945fd3b9aSArmin Le Grand if( !pRep)
18045fd3b9aSArmin Le Grand return false;
18145fd3b9aSArmin Le Grand
18245fd3b9aSArmin Le Grand NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
18345fd3b9aSArmin Le Grand if( !pOut)
18445fd3b9aSArmin Le Grand return false;
18545fd3b9aSArmin Le Grand
186*76e2130fSHerbert Dürr // get the conversion result
18745fd3b9aSArmin Le Grand const size_t nImgSize = [pOut length];
18845fd3b9aSArmin Le Grand rImgData.realloc( nImgSize);
18945fd3b9aSArmin Le Grand [pOut getBytes: rImgData.getArray() length: nImgSize];
19045fd3b9aSArmin Le Grand return (nImgSize > 0);
191cdf0e10cSrcweir }
19245fd3b9aSArmin Le Grand
193