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