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