1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2008 by Sun Microsystems, Inc.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * $RCSfile: OSXTransferable.hxx,v $
10*cdf0e10cSrcweir  * $Revision: 1.4 $
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir  *
18*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir  *
24*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir  *
29*cdf0e10cSrcweir  ************************************************************************/
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /* This is a work-around to prevent 'deprecated' warning for 'KillPicture' API
32*cdf0e10cSrcweir    Hopefully we can get rid of this whole code again when the OOo PICT filter
33*cdf0e10cSrcweir    are good enough to be used see #i78953 thus this hack would vanish to again.
34*cdf0e10cSrcweir  */
35*cdf0e10cSrcweir #include <premac.h>
36*cdf0e10cSrcweir #include <AvailabilityMacros.h>
37*cdf0e10cSrcweir #undef DEPRECATED_ATTRIBUTE
38*cdf0e10cSrcweir #define DEPRECATED_ATTRIBUTE
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <Carbon/Carbon.h>
41*cdf0e10cSrcweir #include <QuickTime/QuickTime.h>
42*cdf0e10cSrcweir #include <postmac.h>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include "PictToBmpFlt.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir bool PICTtoBMP(com::sun::star::uno::Sequence<sal_Int8>& aPict,
47*cdf0e10cSrcweir 			   com::sun::star::uno::Sequence<sal_Int8>& aBmp)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir   bool result = false;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir   ComponentInstance bmpExporter;
53*cdf0e10cSrcweir   if (OpenADefaultComponent(GraphicsExporterComponentType,
54*cdf0e10cSrcweir 							kQTFileTypeBMP,
55*cdf0e10cSrcweir 							&bmpExporter) != noErr)
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 	  return result;
58*cdf0e10cSrcweir 	}
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir   Handle hPict;
61*cdf0e10cSrcweir   if (PtrToHand(aPict.getArray(), &hPict, aPict.getLength()) != noErr)
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir 	  return result;
64*cdf0e10cSrcweir 	}
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir   Handle hBmp;
67*cdf0e10cSrcweir   if ((GraphicsExportSetInputPicture(bmpExporter, (PicHandle)hPict) != noErr) ||
68*cdf0e10cSrcweir 	  ((hBmp = NewHandleClear(0)) == NULL))
69*cdf0e10cSrcweir 	{
70*cdf0e10cSrcweir 	  CloseComponent(bmpExporter);
71*cdf0e10cSrcweir 	  DisposeHandle(hPict);
72*cdf0e10cSrcweir 	  return result;
73*cdf0e10cSrcweir 	}
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir   if ((GraphicsExportSetOutputHandle(bmpExporter, hBmp) == noErr) &&
76*cdf0e10cSrcweir 	  (GraphicsExportDoExport(bmpExporter, NULL) == noErr))
77*cdf0e10cSrcweir 	{
78*cdf0e10cSrcweir 	  size_t sz = GetHandleSize(hBmp);
79*cdf0e10cSrcweir 	  aBmp.realloc(sz);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	  HLock(hBmp);
82*cdf0e10cSrcweir 	  rtl_copyMemory(aBmp.getArray(), ((sal_Int8*)*hBmp), sz);
83*cdf0e10cSrcweir 	  HUnlock(hBmp);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	  result = true;
86*cdf0e10cSrcweir 	}
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir   DisposeHandle(hPict);
89*cdf0e10cSrcweir   DisposeHandle(hBmp);
90*cdf0e10cSrcweir   CloseComponent(bmpExporter);
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir   return result;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir bool BMPtoPICT(com::sun::star::uno::Sequence<sal_Int8>& aBmp,
96*cdf0e10cSrcweir 			   com::sun::star::uno::Sequence<sal_Int8>& aPict)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir   bool result = false;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir   Handle hBmp;
101*cdf0e10cSrcweir   ComponentInstance pictExporter;
102*cdf0e10cSrcweir   if ((PtrToHand(aBmp.getArray(), &hBmp, aBmp.getLength()) != noErr))
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 	  return result;
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir   if (OpenADefaultComponent(GraphicsImporterComponentType,
108*cdf0e10cSrcweir 							kQTFileTypeBMP,
109*cdf0e10cSrcweir 							&pictExporter) != noErr)
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 	  DisposeHandle(hBmp);
112*cdf0e10cSrcweir 	  return result;
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir   if (GraphicsImportSetDataHandle(pictExporter, hBmp) != noErr)
116*cdf0e10cSrcweir 	{
117*cdf0e10cSrcweir 	  DisposeHandle(hBmp);
118*cdf0e10cSrcweir 	  CloseComponent(pictExporter);
119*cdf0e10cSrcweir 	  return result;
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir   PicHandle hPict;
123*cdf0e10cSrcweir   if (GraphicsImportGetAsPicture(pictExporter, &hPict) == noErr)
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 	  size_t sz = GetHandleSize((Handle)hPict);
126*cdf0e10cSrcweir 	  aPict.realloc(sz);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	  HLock((Handle)hPict);
129*cdf0e10cSrcweir 	  rtl_copyMemory(aPict.getArray(), ((sal_Int8*)*hPict), sz);
130*cdf0e10cSrcweir 	  HUnlock((Handle)hPict);
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	  // Release the data associated with the picture
133*cdf0e10cSrcweir 	  // Note: This function is deprecated in Mac OS X
134*cdf0e10cSrcweir 	  // 10.4.
135*cdf0e10cSrcweir 	  KillPicture(hPict);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	  result = true;
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir   DisposeHandle(hBmp);
141*cdf0e10cSrcweir   CloseComponent(pictExporter);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir   return result;
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir bool ImageToBMP( com::sun::star::uno::Sequence<sal_Int8>& aPict,
147*cdf0e10cSrcweir 			     com::sun::star::uno::Sequence<sal_Int8>& aBmp,
148*cdf0e10cSrcweir 			     NSBitmapImageFileType eInFormat)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir     if( eInFormat == PICTImageFileType )
151*cdf0e10cSrcweir         return PICTtoBMP( aPict, aBmp );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     bool bResult = false;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     NSData* pData = [NSData dataWithBytesNoCopy: (void*)aPict.getConstArray() length: aPict.getLength() freeWhenDone: 0];
156*cdf0e10cSrcweir     if( pData )
157*cdf0e10cSrcweir     {
158*cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
159*cdf0e10cSrcweir         if( pRep )
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             NSData* pOut = [pRep representationUsingType: NSBMPFileType properties: nil];
162*cdf0e10cSrcweir             if( pOut )
163*cdf0e10cSrcweir             {
164*cdf0e10cSrcweir                 aBmp.realloc( [pOut length] );
165*cdf0e10cSrcweir                 [pOut getBytes: aBmp.getArray() length: aBmp.getLength()];
166*cdf0e10cSrcweir                 bResult = (aBmp.getLength() != 0);
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     return bResult;
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir bool BMPToImage( com::sun::star::uno::Sequence<sal_Int8>& aBmp,
175*cdf0e10cSrcweir 			     com::sun::star::uno::Sequence<sal_Int8>& aPict,
176*cdf0e10cSrcweir 			     NSBitmapImageFileType eOutFormat
177*cdf0e10cSrcweir 			    )
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     if( eOutFormat == PICTImageFileType )
180*cdf0e10cSrcweir         return BMPtoPICT( aBmp, aPict );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     bool bResult = false;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(aBmp.getConstArray()) length: aBmp.getLength() freeWhenDone: 0];
185*cdf0e10cSrcweir     if( pData )
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
188*cdf0e10cSrcweir         if( pRep )
189*cdf0e10cSrcweir         {
190*cdf0e10cSrcweir             NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
191*cdf0e10cSrcweir             if( pOut )
192*cdf0e10cSrcweir             {
193*cdf0e10cSrcweir                 aPict.realloc( [pOut length] );
194*cdf0e10cSrcweir                 [pOut getBytes: aPict.getArray() length: aPict.getLength()];
195*cdf0e10cSrcweir                 bResult = (aPict.getLength() != 0);
196*cdf0e10cSrcweir             }
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     return bResult;
201*cdf0e10cSrcweir }
202