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