xref: /AOO41X/main/vcl/source/gdi/cvtgrf.cxx (revision 9f62ea84a806e17e6f2bbff75724a7257a0eb5d9)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <vcl/metaact.hxx>
28 #include <vcl/cvtgrf.hxx>
29 
30 #include <salinst.hxx>
31 #include <svdata.hxx>
32 
33 // --------------
34 // - Callback   -
35 // --------------
36 
37 // --------------------
38 // - GraphicConverter -
39 // --------------------
40 
GraphicConverter()41 GraphicConverter::GraphicConverter() :
42     mpConvertData( NULL )
43 {
44 }
45 
46 // ------------------------------------------------------------------------
47 
~GraphicConverter()48 GraphicConverter::~GraphicConverter()
49 {
50 }
51 
52 // ------------------------------------------------------------------------
53 
ImplConvert(sal_uLong nInFormat,void * pInBuffer,sal_uLong nInBufSize,void ** ppOutBuffer,sal_uLong nOutFormat)54 sal_uLong GraphicConverter::ImplConvert( sal_uLong nInFormat, void* pInBuffer, sal_uLong nInBufSize,
55                                      void** ppOutBuffer, sal_uLong nOutFormat )
56 {
57     sal_uLong nRetBufSize = 0UL;
58 
59     if( ( nInFormat != nOutFormat ) && pInBuffer )
60     {
61         if( ( nInFormat == CVT_SVM ) || ( nInFormat == CVT_BMP ) )
62         {
63             SvMemoryStream  aIStm;
64             Graphic         aGraphic;
65 
66             aIStm.SetBuffer( (char*) pInBuffer, nInBufSize, sal_False, nInBufSize );
67             aIStm >> aGraphic;
68 
69             if( !aIStm.GetError() )
70             {
71                 SvMemoryStream aOStm( 64535, 64535 );
72 
73                 mpConvertData = new ConvertData( aGraphic, aOStm, nOutFormat );
74 
75                 if( maFilterHdl.IsSet() && maFilterHdl.Call( mpConvertData ) )
76                 {
77                     nRetBufSize = aOStm.Seek( STREAM_SEEK_TO_END );
78                     *ppOutBuffer = (void*) aOStm.GetData();
79                     aOStm.ObjectOwnsMemory( sal_False );
80                 }
81 
82                 delete mpConvertData;
83                 mpConvertData = NULL;
84             }
85         }
86         else if( ( nOutFormat == CVT_SVM ) || ( nOutFormat == CVT_BMP ) )
87         {
88             SvMemoryStream  aIStm;
89 
90             aIStm.SetBuffer( (char*) pInBuffer, nInBufSize, sal_False, nInBufSize );
91             mpConvertData = new ConvertData( Graphic(), aIStm, nInFormat );
92 
93             if( maFilterHdl.IsSet() && maFilterHdl.Call( mpConvertData ) )
94             {
95                 SvMemoryStream  aOStm( 645535, 64535 );
96                 Graphic&        rGraphic = mpConvertData->maGraphic;
97 
98                 if( ( rGraphic.GetType() == GRAPHIC_BITMAP ) && ( CVT_SVM == nOutFormat ) )
99                 {
100                     GDIMetaFile aMtf;
101 
102                     aMtf.SetPrefSize( rGraphic.GetPrefSize() );
103                     aMtf.SetPrefMapMode( rGraphic.GetPrefMapMode() );
104                     aMtf.AddAction( new MetaBmpExScaleAction( Point(), aMtf.GetPrefSize(), rGraphic.GetBitmapEx() ) );
105                     rGraphic = aMtf;
106                 }
107                 else if( ( rGraphic.GetType() == GRAPHIC_GDIMETAFILE ) && ( CVT_BMP == nOutFormat ) )
108                     rGraphic = rGraphic.GetBitmapEx();
109 
110                 aOStm << rGraphic;
111 
112                 if( !aOStm.GetError() )
113                 {
114                     nRetBufSize = aOStm.Seek( STREAM_SEEK_TO_END );
115                     *ppOutBuffer = (void*) aOStm.GetData();
116                     aOStm.ObjectOwnsMemory( sal_False );
117                 }
118             }
119 
120             delete mpConvertData;
121             mpConvertData = NULL;
122         }
123     }
124 
125     return nRetBufSize;
126 }
127 
128 // ------------------------------------------------------------------------
129 
Import(SvStream & rIStm,Graphic & rGraphic,sal_uLong nFormat)130 sal_uLong GraphicConverter::Import( SvStream& rIStm, Graphic& rGraphic, sal_uLong nFormat )
131 {
132     GraphicConverter*   pCvt = ImplGetSVData()->maGDIData.mpGrfConverter;
133     sal_uLong               nRet = ERRCODE_IO_GENERAL;
134 
135     if( pCvt && pCvt->GetFilterHdl().IsSet() )
136     {
137         ConvertData aData( rGraphic, rIStm, nFormat );
138 
139         if( pCvt->GetFilterHdl().Call( &aData ) )
140         {
141             rGraphic = aData.maGraphic;
142             nRet = ERRCODE_NONE;
143         }
144         else if( rIStm.GetError() )
145             nRet = rIStm.GetError();
146     }
147 
148     return nRet;
149 }
150 
151 // ------------------------------------------------------------------------
152 
Export(SvStream & rOStm,const Graphic & rGraphic,sal_uLong nFormat)153 sal_uLong GraphicConverter::Export( SvStream& rOStm, const Graphic& rGraphic, sal_uLong nFormat )
154 {
155     GraphicConverter*   pCvt = ImplGetSVData()->maGDIData.mpGrfConverter;
156     sal_uLong               nRet = ERRCODE_IO_GENERAL;
157 
158     if( pCvt && pCvt->GetFilterHdl().IsSet() )
159     {
160         ConvertData aData( rGraphic, rOStm, nFormat );
161 
162         if( pCvt->GetFilterHdl().Call( &aData ) )
163             nRet = ERRCODE_NONE;
164         else if( rOStm.GetError() )
165             nRet = rOStm.GetError();
166     }
167 
168     return nRet;
169 }
170