xref: /aoo41x/main/vcl/source/gdi/cvtgrf.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <vcl/metaact.hxx>
32 #include <vcl/cvtgrf.hxx>
33 
34 #include <salinst.hxx>
35 #include <svdata.hxx>
36 
37 // --------------
38 // - Callback	-
39 // --------------
40 
41 // --------------------
42 // - GraphicConverter -
43 // --------------------
44 
45 GraphicConverter::GraphicConverter() :
46 	mpConvertData( NULL )
47 {
48 }
49 
50 // ------------------------------------------------------------------------
51 
52 GraphicConverter::~GraphicConverter()
53 {
54 }
55 
56 // ------------------------------------------------------------------------
57 
58 sal_uLong GraphicConverter::ImplConvert( sal_uLong nInFormat, void* pInBuffer, sal_uLong nInBufSize,
59 									 void** ppOutBuffer, sal_uLong nOutFormat )
60 {
61 	sal_uLong nRetBufSize = 0UL;
62 
63 	if( ( nInFormat != nOutFormat ) && pInBuffer )
64 	{
65 		if( ( nInFormat == CVT_SVM ) || ( nInFormat == CVT_BMP ) )
66 		{
67 			SvMemoryStream	aIStm;
68 			Graphic			aGraphic;
69 
70 			aIStm.SetBuffer( (char*) pInBuffer, nInBufSize, sal_False, nInBufSize );
71 			aIStm >> aGraphic;
72 
73 			if( !aIStm.GetError() )
74 			{
75 				SvMemoryStream aOStm( 64535, 64535 );
76 
77 				mpConvertData = new ConvertData( aGraphic, aOStm, nOutFormat );
78 
79 				if( maFilterHdl.IsSet() && maFilterHdl.Call( mpConvertData ) )
80 				{
81 					nRetBufSize = aOStm.Seek( STREAM_SEEK_TO_END );
82 					*ppOutBuffer = (void*) aOStm.GetData();
83 					aOStm.ObjectOwnsMemory( sal_False );
84 				}
85 
86 				delete mpConvertData;
87 				mpConvertData = NULL;
88 			}
89 		}
90 		else if( ( nOutFormat == CVT_SVM ) || ( nOutFormat == CVT_BMP ) )
91 		{
92 			SvMemoryStream	aIStm;
93 
94 			aIStm.SetBuffer( (char*) pInBuffer, nInBufSize, sal_False, nInBufSize );
95 			mpConvertData = new ConvertData( Graphic(), aIStm, nInFormat );
96 
97 			if( maFilterHdl.IsSet() && maFilterHdl.Call( mpConvertData ) )
98 			{
99 				SvMemoryStream	aOStm( 645535, 64535 );
100 				Graphic&		rGraphic = mpConvertData->maGraphic;
101 
102 				if( ( rGraphic.GetType() == GRAPHIC_BITMAP ) && ( CVT_SVM == nOutFormat ) )
103 				{
104 					GDIMetaFile aMtf;
105 
106 					aMtf.SetPrefSize( rGraphic.GetPrefSize() );
107 					aMtf.SetPrefMapMode( rGraphic.GetPrefMapMode() );
108 					aMtf.AddAction( new MetaBmpExScaleAction( Point(), aMtf.GetPrefSize(), rGraphic.GetBitmapEx() ) );
109 					rGraphic = aMtf;
110 				}
111 				else if( ( rGraphic.GetType() == GRAPHIC_GDIMETAFILE ) && ( CVT_BMP == nOutFormat ) )
112 					rGraphic = rGraphic.GetBitmapEx();
113 
114 				aOStm << rGraphic;
115 
116 				if( !aOStm.GetError() )
117 				{
118 					nRetBufSize = aOStm.Seek( STREAM_SEEK_TO_END );
119 					*ppOutBuffer = (void*) aOStm.GetData();
120 					aOStm.ObjectOwnsMemory( sal_False );
121 				}
122 			}
123 
124 			delete mpConvertData;
125 			mpConvertData = NULL;
126 		}
127 	}
128 
129 	return nRetBufSize;
130 }
131 
132 // ------------------------------------------------------------------------
133 
134 sal_uLong GraphicConverter::Import( SvStream& rIStm, Graphic& rGraphic, sal_uLong nFormat )
135 {
136 	GraphicConverter*	pCvt = ImplGetSVData()->maGDIData.mpGrfConverter;
137 	sal_uLong				nRet = ERRCODE_IO_GENERAL;
138 
139 	if( pCvt && pCvt->GetFilterHdl().IsSet() )
140 	{
141 		ConvertData	aData( rGraphic, rIStm, nFormat );
142 
143 		if( pCvt->GetFilterHdl().Call( &aData ) )
144 		{
145 			rGraphic = aData.maGraphic;
146 			nRet = ERRCODE_NONE;
147 		}
148 		else if( rIStm.GetError() )
149 			nRet = rIStm.GetError();
150 	}
151 
152 	return nRet;
153 }
154 
155 // ------------------------------------------------------------------------
156 
157 sal_uLong GraphicConverter::Export( SvStream& rOStm, const Graphic& rGraphic, sal_uLong nFormat )
158 {
159 	GraphicConverter*	pCvt = ImplGetSVData()->maGDIData.mpGrfConverter;
160 	sal_uLong				nRet = ERRCODE_IO_GENERAL;
161 
162 	if( pCvt && pCvt->GetFilterHdl().IsSet() )
163 	{
164 		ConvertData	aData( rGraphic, rOStm, nFormat );
165 
166 		if( pCvt->GetFilterHdl().Call( &aData ) )
167 			nRet = ERRCODE_NONE;
168 		else if( rOStm.GetError() )
169 			nRet = rOStm.GetError();
170 	}
171 
172 	return nRet;
173 }
174