1*c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c82f2877SAndrew Rist  * distributed with this work for additional information
6*c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9*c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c82f2877SAndrew Rist  *
11*c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c82f2877SAndrew Rist  *
13*c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15*c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c82f2877SAndrew Rist  * specific language governing permissions and limitations
18*c82f2877SAndrew Rist  * under the License.
19*c82f2877SAndrew Rist  *
20*c82f2877SAndrew Rist  *************************************************************/
21*c82f2877SAndrew Rist 
22*c82f2877SAndrew 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 <QuickTime/QuickTime.h>
35cdf0e10cSrcweir #include <postmac.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "PictToBmpFlt.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir bool PICTtoBMP(com::sun::star::uno::Sequence<sal_Int8>& aPict,
40cdf0e10cSrcweir 			   com::sun::star::uno::Sequence<sal_Int8>& aBmp)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir   bool result = false;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir   ComponentInstance bmpExporter;
46cdf0e10cSrcweir   if (OpenADefaultComponent(GraphicsExporterComponentType,
47cdf0e10cSrcweir 							kQTFileTypeBMP,
48cdf0e10cSrcweir 							&bmpExporter) != noErr)
49cdf0e10cSrcweir 	{
50cdf0e10cSrcweir 	  return result;
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
53cdf0e10cSrcweir   Handle hPict;
54cdf0e10cSrcweir   if (PtrToHand(aPict.getArray(), &hPict, aPict.getLength()) != noErr)
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 	  return result;
57cdf0e10cSrcweir 	}
58cdf0e10cSrcweir 
59cdf0e10cSrcweir   Handle hBmp;
60cdf0e10cSrcweir   if ((GraphicsExportSetInputPicture(bmpExporter, (PicHandle)hPict) != noErr) ||
61cdf0e10cSrcweir 	  ((hBmp = NewHandleClear(0)) == NULL))
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 	  CloseComponent(bmpExporter);
64cdf0e10cSrcweir 	  DisposeHandle(hPict);
65cdf0e10cSrcweir 	  return result;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir 
68cdf0e10cSrcweir   if ((GraphicsExportSetOutputHandle(bmpExporter, hBmp) == noErr) &&
69cdf0e10cSrcweir 	  (GraphicsExportDoExport(bmpExporter, NULL) == noErr))
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 	  size_t sz = GetHandleSize(hBmp);
72cdf0e10cSrcweir 	  aBmp.realloc(sz);
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	  HLock(hBmp);
75cdf0e10cSrcweir 	  rtl_copyMemory(aBmp.getArray(), ((sal_Int8*)*hBmp), sz);
76cdf0e10cSrcweir 	  HUnlock(hBmp);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	  result = true;
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir 
81cdf0e10cSrcweir   DisposeHandle(hPict);
82cdf0e10cSrcweir   DisposeHandle(hBmp);
83cdf0e10cSrcweir   CloseComponent(bmpExporter);
84cdf0e10cSrcweir 
85cdf0e10cSrcweir   return result;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir bool BMPtoPICT(com::sun::star::uno::Sequence<sal_Int8>& aBmp,
89cdf0e10cSrcweir 			   com::sun::star::uno::Sequence<sal_Int8>& aPict)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir   bool result = false;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir   Handle hBmp;
94cdf0e10cSrcweir   ComponentInstance pictExporter;
95cdf0e10cSrcweir   if ((PtrToHand(aBmp.getArray(), &hBmp, aBmp.getLength()) != noErr))
96cdf0e10cSrcweir 	{
97cdf0e10cSrcweir 	  return result;
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir 
100cdf0e10cSrcweir   if (OpenADefaultComponent(GraphicsImporterComponentType,
101cdf0e10cSrcweir 							kQTFileTypeBMP,
102cdf0e10cSrcweir 							&pictExporter) != noErr)
103cdf0e10cSrcweir 	{
104cdf0e10cSrcweir 	  DisposeHandle(hBmp);
105cdf0e10cSrcweir 	  return result;
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir   if (GraphicsImportSetDataHandle(pictExporter, hBmp) != noErr)
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 	  DisposeHandle(hBmp);
111cdf0e10cSrcweir 	  CloseComponent(pictExporter);
112cdf0e10cSrcweir 	  return result;
113cdf0e10cSrcweir 	}
114cdf0e10cSrcweir 
115cdf0e10cSrcweir   PicHandle hPict;
116cdf0e10cSrcweir   if (GraphicsImportGetAsPicture(pictExporter, &hPict) == noErr)
117cdf0e10cSrcweir 	{
118cdf0e10cSrcweir 	  size_t sz = GetHandleSize((Handle)hPict);
119cdf0e10cSrcweir 	  aPict.realloc(sz);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	  HLock((Handle)hPict);
122cdf0e10cSrcweir 	  rtl_copyMemory(aPict.getArray(), ((sal_Int8*)*hPict), sz);
123cdf0e10cSrcweir 	  HUnlock((Handle)hPict);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	  // Release the data associated with the picture
126cdf0e10cSrcweir 	  // Note: This function is deprecated in Mac OS X
127cdf0e10cSrcweir 	  // 10.4.
128cdf0e10cSrcweir 	  KillPicture(hPict);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	  result = true;
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir   DisposeHandle(hBmp);
134cdf0e10cSrcweir   CloseComponent(pictExporter);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir   return result;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir bool ImageToBMP( com::sun::star::uno::Sequence<sal_Int8>& aPict,
140cdf0e10cSrcweir 			     com::sun::star::uno::Sequence<sal_Int8>& aBmp,
141cdf0e10cSrcweir 			     NSBitmapImageFileType eInFormat)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     if( eInFormat == PICTImageFileType )
144cdf0e10cSrcweir         return PICTtoBMP( aPict, aBmp );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     bool bResult = false;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     NSData* pData = [NSData dataWithBytesNoCopy: (void*)aPict.getConstArray() length: aPict.getLength() freeWhenDone: 0];
149cdf0e10cSrcweir     if( pData )
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
152cdf0e10cSrcweir         if( pRep )
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             NSData* pOut = [pRep representationUsingType: NSBMPFileType properties: nil];
155cdf0e10cSrcweir             if( pOut )
156cdf0e10cSrcweir             {
157cdf0e10cSrcweir                 aBmp.realloc( [pOut length] );
158cdf0e10cSrcweir                 [pOut getBytes: aBmp.getArray() length: aBmp.getLength()];
159cdf0e10cSrcweir                 bResult = (aBmp.getLength() != 0);
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     return bResult;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir bool BMPToImage( com::sun::star::uno::Sequence<sal_Int8>& aBmp,
168cdf0e10cSrcweir 			     com::sun::star::uno::Sequence<sal_Int8>& aPict,
169cdf0e10cSrcweir 			     NSBitmapImageFileType eOutFormat
170cdf0e10cSrcweir 			    )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     if( eOutFormat == PICTImageFileType )
173cdf0e10cSrcweir         return BMPtoPICT( aBmp, aPict );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     bool bResult = false;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     NSData* pData = [NSData dataWithBytesNoCopy: const_cast<sal_Int8*>(aBmp.getConstArray()) length: aBmp.getLength() freeWhenDone: 0];
178cdf0e10cSrcweir     if( pData )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         NSBitmapImageRep* pRep = [NSBitmapImageRep imageRepWithData: pData];
181cdf0e10cSrcweir         if( pRep )
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             NSData* pOut = [pRep representationUsingType: eOutFormat properties: nil];
184cdf0e10cSrcweir             if( pOut )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 aPict.realloc( [pOut length] );
187cdf0e10cSrcweir                 [pOut getBytes: aPict.getArray() length: aPict.getLength()];
188cdf0e10cSrcweir                 bResult = (aPict.getLength() != 0);
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     return bResult;
194cdf0e10cSrcweir }
195