LibreOffice Module svx (master) 1
_xoutbmp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21#include <sal/log.hxx>
22
23#include <comphelper/base64.hxx>
25#include <tools/debug.hxx>
26#include <vcl/virdev.hxx>
27#include <sfx2/docfile.hxx>
28#include <svx/xoutbmp.hxx>
29#include <vcl/graphicfilter.hxx>
30#include <vcl/cvtgrf.hxx>
31#include <memory>
32
33constexpr OUStringLiteral FORMAT_BMP = u"bmp";
34constexpr OUStringLiteral FORMAT_GIF = u"gif";
35constexpr OUStringLiteral FORMAT_JPG = u"jpg";
36constexpr OUStringLiteral FORMAT_PNG = u"png";
37constexpr OUStringLiteral FORMAT_WEBP = u"webp";
38
39using namespace com::sun::star;
40
41Animation XOutBitmap::MirrorAnimation( const Animation& rAnimation, bool bHMirr, bool bVMirr )
42{
43 Animation aNewAnim( rAnimation );
44
45 if( bHMirr || bVMirr )
46 {
47 const Size& rGlobalSize = aNewAnim.GetDisplaySizePixel();
48 BmpMirrorFlags nMirrorFlags = BmpMirrorFlags::NONE;
49
50 if( bHMirr )
51 nMirrorFlags |= BmpMirrorFlags::Horizontal;
52
53 if( bVMirr )
54 nMirrorFlags |= BmpMirrorFlags::Vertical;
55
56 for( sal_uInt16 i = 0, nCount = aNewAnim.Count(); i < nCount; i++ )
57 {
58 AnimationBitmap aAnimationBitmap( aNewAnim.Get( i ) );
59
60 // mirror the BitmapEx
61 aAnimationBitmap.maBitmapEx.Mirror( nMirrorFlags );
62
63 // Adjust the positions inside the whole bitmap
64 if( bHMirr )
65 aAnimationBitmap.maPositionPixel.setX(rGlobalSize.Width() - aAnimationBitmap.maPositionPixel.X() -
66 aAnimationBitmap.maSizePixel.Width());
67
68 if( bVMirr )
69 aAnimationBitmap.maPositionPixel.setY(rGlobalSize.Height() - aAnimationBitmap.maPositionPixel.Y() -
70 aAnimationBitmap.maSizePixel.Height());
71
72 aNewAnim.Replace(aAnimationBitmap, i);
73 }
74 }
75
76 return aNewAnim;
77}
78
79Graphic XOutBitmap::MirrorGraphic( const Graphic& rGraphic, const BmpMirrorFlags nMirrorFlags )
80{
81 Graphic aRetGraphic;
82
83 if( nMirrorFlags != BmpMirrorFlags::NONE )
84 {
85 if( rGraphic.IsAnimated() )
86 {
87 aRetGraphic = MirrorAnimation( rGraphic.GetAnimation(),
88 bool( nMirrorFlags & BmpMirrorFlags::Horizontal ),
89 bool( nMirrorFlags & BmpMirrorFlags::Vertical ) );
90 }
91 else
92 {
93 BitmapEx aBmp( rGraphic.GetBitmapEx() );
94 aBmp.Mirror( nMirrorFlags );
95 aRetGraphic = aBmp;
96 }
97 }
98 else
99 aRetGraphic = rGraphic;
100
101 return aRetGraphic;
102}
103
104ErrCode XOutBitmap::WriteGraphic( const Graphic& rGraphic, OUString& rFileName,
105 const OUString& rFilterName, const XOutFlags nFlags,
106 const Size* pMtfSize_100TH_MM,
107 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
108{
109 if( rGraphic.GetType() == GraphicType::NONE )
110 return ERRCODE_NONE;
111
112 INetURLObject aURL( rFileName );
113 Graphic aGraphic;
114 OUString aExt;
117 bool bTransparent = rGraphic.IsTransparent(), bAnimated = rGraphic.IsAnimated();
118
119 DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::WriteGraphic(...): invalid URL" );
120
121 // calculate correct file name
122 if( !( nFlags & XOutFlags::DontExpandFilename ) )
123 {
124 OUString aStr( OUString::number( rGraphic.GetChecksum(), 16 ) );
125 if ( aStr[0] == '-' )
126 aStr = OUString::Concat("m") + aStr.subView(1);
127 OUString aName = aURL.getBase() + "_" + aURL.getExtension() + "_" + aStr;
128 aURL.setBase( aName );
129 }
130
131 // #i121128# use shortcut to write Vector Graphic Data data in original form (if possible)
132 auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
133
134 if (rVectorGraphicDataPtr && rVectorGraphicDataPtr->getBinaryDataContainer().getSize())
135 {
136 // Does the filter name match the original format?
137 const bool bIsSvg(rFilterName.equalsIgnoreAsciiCase("svg") && VectorGraphicDataType::Svg == rVectorGraphicDataPtr->getType());
138 const bool bIsWmf(rFilterName.equalsIgnoreAsciiCase("wmf") && VectorGraphicDataType::Wmf == rVectorGraphicDataPtr->getType());
139 bool bIsEmf(rFilterName.equalsIgnoreAsciiCase("emf") && VectorGraphicDataType::Emf == rVectorGraphicDataPtr->getType());
140 if (!bIsEmf)
141 {
142 bIsEmf = rFilterName.equalsIgnoreAsciiCase("emf") && rGraphic.GetGfxLink().IsEMF();
143 }
144 const bool bIsPdf(rFilterName.equalsIgnoreAsciiCase("pdf") && VectorGraphicDataType::Pdf == rVectorGraphicDataPtr->getType());
145
146 if (bIsSvg || bIsWmf || bIsEmf || bIsPdf)
147 {
148 if (!(nFlags & XOutFlags::DontAddExtension))
149 {
150 aURL.setExtension(rFilterName);
151 }
152
153 rFileName = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
154 SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
155 SvStream* pOStm = aMedium.GetOutStream();
156
157 if (pOStm)
158 {
159 auto & rDataContainer = rVectorGraphicDataPtr->getBinaryDataContainer();
160 pOStm->WriteBytes(rDataContainer.getData(), rDataContainer.getSize());
161 aMedium.Commit();
162
163 if (!aMedium.GetError())
164 return ERRCODE_NONE;
165 }
166 }
167 }
168
169 if( ( nFlags & XOutFlags::UseNativeIfPossible ) &&
170 !( nFlags & XOutFlags::MirrorHorz ) &&
171 !( nFlags & XOutFlags::MirrorVert ) &&
172 ( rGraphic.GetType() != GraphicType::GdiMetafile ) && rGraphic.IsGfxLink() )
173 {
174 // try to write native link
175 const GfxLink aGfxLink( rGraphic.GetGfxLink() );
176
177 switch( aGfxLink.GetType() )
178 {
179 case GfxLinkType::NativeGif: aExt = FORMAT_GIF; break;
180
181 // #i15508# added BMP type for better exports (no call/trigger found, prob used in HTML export)
182 case GfxLinkType::NativeBmp: aExt = FORMAT_BMP; break;
183
184 case GfxLinkType::NativeJpg: aExt = FORMAT_JPG; break;
185 case GfxLinkType::NativePng: aExt = FORMAT_PNG; break;
186 case GfxLinkType::NativeWebp: aExt = FORMAT_WEBP; break;
187
188 default:
189 break;
190 }
191
192 // tdf#60684: use native format if possible but it must correspond to filter name
193 // or no specific format has been required
194 // without this, you may save for example file with png extension but jpg content
195 if( !aExt.isEmpty() && (aExt == rFilterName || rFilterName.isEmpty()) )
196 {
197 if( !(nFlags & XOutFlags::DontAddExtension) )
198 aURL.setExtension( aExt );
199 rFileName = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
200
201 SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
202 SvStream* pOStm = aMedium.GetOutStream();
203
204 if( pOStm && aGfxLink.GetDataSize() && aGfxLink.GetData() )
205 {
206 pOStm->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
207 aMedium.Commit();
208
209 if( !aMedium.GetError() )
210 return ERRCODE_NONE;
211 }
212 }
213 }
214
215 OUString aFilter( rFilterName );
216 bool bWriteTransGrf = ( aFilter.equalsIgnoreAsciiCase( "transgrf" ) ) ||
217 ( aFilter.equalsIgnoreAsciiCase( "gif" ) ) ||
218 ( nFlags & XOutFlags::UseGifIfPossible ) ||
219 ( ( nFlags & XOutFlags::UseGifIfSensible ) && ( bAnimated || bTransparent ) );
220
221 // get filter and extension
222 if( bWriteTransGrf )
223 aFilter = FORMAT_GIF;
224
225 sal_uInt16 nFilter = rFilter.GetExportFormatNumberForShortName( aFilter );
226
227 if( GRFILTER_FORMAT_NOTFOUND == nFilter )
228 {
230
231 if( GRFILTER_FORMAT_NOTFOUND == nFilter )
233 }
234
235 if( GRFILTER_FORMAT_NOTFOUND != nFilter )
236 {
237 aExt = rFilter.GetExportFormatShortName( nFilter ).toAsciiLowerCase();
238
239 if( bWriteTransGrf )
240 {
241 if( bAnimated )
242 aGraphic = rGraphic;
243 else
244 {
245 if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GraphicType::Bitmap ) )
246 {
248 const Size aSize(pVDev->LogicToPixel(*pMtfSize_100TH_MM, MapMode(MapUnit::Map100thMM)));
249
250 if( pVDev->SetOutputSizePixel( aSize ) )
251 {
252 const Wallpaper aWallpaper( pVDev->GetBackground() );
253 const Point aPt;
254
255 pVDev->SetBackground( Wallpaper( COL_BLACK ) );
256 pVDev->Erase();
257 rGraphic.Draw(*pVDev, aPt, aSize);
258
259 const Bitmap aBitmap( pVDev->GetBitmap( aPt, aSize ) );
260
261 pVDev->SetBackground( aWallpaper );
262 pVDev->Erase();
263 rGraphic.Draw(*pVDev, aPt, aSize);
264
265 pVDev->SetRasterOp( RasterOp::Xor );
266 pVDev->DrawBitmap( aPt, aSize, aBitmap );
267 aGraphic = BitmapEx( aBitmap, pVDev->GetBitmap( aPt, aSize ) );
268 }
269 else
270 aGraphic = rGraphic.GetBitmapEx();
271 }
272 else
273 aGraphic = rGraphic.GetBitmapEx();
274 }
275 }
276 else
277 {
278 if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GraphicType::Bitmap ) )
279 {
281 const Size aSize(pVDev->LogicToPixel(*pMtfSize_100TH_MM, MapMode(MapUnit::Map100thMM)));
282
283 if( pVDev->SetOutputSizePixel( aSize ) )
284 {
285 rGraphic.Draw(*pVDev, Point(), aSize);
286 aGraphic = BitmapEx(pVDev->GetBitmap(Point(), aSize));
287 }
288 else
289 aGraphic = rGraphic.GetBitmapEx();
290 }
291 else
292 aGraphic = rGraphic.GetBitmapEx();
293 }
294
295 // mirror?
296 if( ( nFlags & XOutFlags::MirrorHorz ) || ( nFlags & XOutFlags::MirrorVert ) )
297 {
298 BmpMirrorFlags nBmpMirrorFlags = BmpMirrorFlags::NONE;
299 if( nFlags & XOutFlags::MirrorHorz )
300 nBmpMirrorFlags |= BmpMirrorFlags::Horizontal;
301 if( nFlags & XOutFlags::MirrorVert )
302 nBmpMirrorFlags |= BmpMirrorFlags::Vertical;
303 aGraphic = MirrorGraphic( aGraphic, nBmpMirrorFlags );
304 }
305
306 if (aGraphic.GetType() != GraphicType::NONE)
307 {
308 if( !(nFlags & XOutFlags::DontAddExtension) )
309 aURL.setExtension( aExt );
310 rFileName = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
311 nErr = ExportGraphic( aGraphic, aURL, rFilter, nFilter, pFilterData );
312 }
313 }
314
315 return nErr;
316}
317
318bool XOutBitmap::GraphicToBase64(const Graphic& rGraphic, OUString& rOUString, bool bAddPrefix,
319 ConvertDataFormat aTargetFormat)
320{
321 SvMemoryStream aOStm;
322 GfxLink aLink = rGraphic.GetGfxLink();
323
324 if (aTargetFormat == ConvertDataFormat::Unknown)
325 {
326 switch (aLink.GetType())
327 {
328 case GfxLinkType::NativeJpg:
329 aTargetFormat = ConvertDataFormat::JPG;
330 break;
331 case GfxLinkType::NativePng:
332 aTargetFormat = ConvertDataFormat::PNG;
333 break;
334 case GfxLinkType::NativeSvg:
335 aTargetFormat = ConvertDataFormat::SVG;
336 break;
337 default:
338 // save everything else (including gif) into png
339 aTargetFormat = ConvertDataFormat::PNG;
340 break;
341 }
342 }
343
344 ErrCode nErr = GraphicConverter::Export(aOStm,rGraphic,aTargetFormat);
345 if ( nErr )
346 {
347 SAL_WARN("svx", "XOutBitmap::GraphicToBase64() invalid Graphic? error: " << nErr );
348 return false;
349 }
350 css::uno::Sequence<sal_Int8> aOStmSeq( static_cast<sal_Int8 const *>(aOStm.GetData()),aOStm.TellEnd() );
351 OUStringBuffer aStrBuffer;
352 ::comphelper::Base64::encode(aStrBuffer,aOStmSeq);
353 rOUString = aStrBuffer.makeStringAndClear();
354
355 if (bAddPrefix)
356 {
357 OUString aMimeType
359 rOUString = aMimeType + ";base64," + rOUString;
360 }
361
362 return true;
363}
364
366 GraphicFilter& rFilter, const sal_uInt16 nFormat,
367 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
368{
369 DBG_ASSERT( rURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::ExportGraphic(...): invalid URL" );
370
371 SfxMedium aMedium( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC );
372 SvStream* pOStm = aMedium.GetOutStream();
374
375 if( pOStm )
376 {
377 nRet = rFilter.ExportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pOStm, nFormat, pFilterData );
378
379 aMedium.Commit();
380
381 if( aMedium.GetError() && ( ERRCODE_NONE == nRet ) )
383 }
384
385 return nRet;
386}
387
388
389/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral FORMAT_BMP
Definition: _xoutbmp.cxx:33
constexpr OUStringLiteral FORMAT_PNG
Definition: _xoutbmp.cxx:36
constexpr OUStringLiteral FORMAT_GIF
Definition: _xoutbmp.cxx:34
constexpr OUStringLiteral FORMAT_WEBP
Definition: _xoutbmp.cxx:37
constexpr OUStringLiteral FORMAT_JPG
Definition: _xoutbmp.cxx:35
size_t Count() const
const Size & GetDisplaySizePixel() const
const AnimationBitmap & Get(sal_uInt16 nAnimation) const
void Replace(const AnimationBitmap &rNewAnimationBmp, sal_uInt16 nAnimation)
bool Mirror(BmpMirrorFlags nMirrorFlags)
static ErrCode Export(SvStream &rOStm, const Graphic &rGraphic, ConvertDataFormat nFormat)
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
static GraphicFilter & GetGraphicFilter()
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
OUString GetExportFormatShortName(sal_uInt16 nFormat)
Animation GetAnimation() const
GraphicType GetType() const
GfxLink GetGfxLink() const
bool IsAnimated() const
BitmapChecksum GetChecksum() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
void Draw(OutputDevice &rOutDev, const Point &rDestPt) const
bool IsGfxLink() const
const std::shared_ptr< VectorGraphicData > & getVectorGraphicData() const
bool IsTransparent() const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INetProtocol GetProtocol() const
ErrCode GetError() const
SvStream * GetOutStream()
bool Commit()
constexpr tools::Long Height() const
constexpr tools::Long Width() const
const void * GetData()
virtual sal_uInt64 TellEnd() override
std::size_t WriteBytes(const void *pData, std::size_t nSize)
static ErrCode WriteGraphic(const Graphic &rGraphic, OUString &rFileName, const OUString &rFilterName, const XOutFlags nFlags, const Size *pMtfSize_100TH_MM=nullptr, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
Definition: _xoutbmp.cxx:104
static bool GraphicToBase64(const Graphic &rGraphic, OUString &rOUString, bool bAddPrefix=true, ConvertDataFormat aTargetFormat=ConvertDataFormat::Unknown)
Definition: _xoutbmp.cxx:318
static Animation MirrorAnimation(const Animation &rAnimation, bool bHMirr, bool bVMirr)
Definition: _xoutbmp.cxx:41
static Graphic MirrorGraphic(const Graphic &rGraphic, const BmpMirrorFlags nMirrorFlags)
Definition: _xoutbmp.cxx:79
static ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rURL, GraphicFilter &rFilter, const sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData)
Definition: _xoutbmp.cxx:365
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
static OUString GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
int nCount
#define DBG_ASSERT(sCon, aError)
URL aURL
float u
#define ERRCODE_NONE
#define GRFILTER_FORMAT_NOTFOUND
#define ERRCODE_GRFILTER_FILTERERROR
#define ERRCODE_GRFILTER_IOERROR
BmpMirrorFlags
OUString aName
#define SAL_WARN(area, stream)
aStr
int i
ConvertDataFormat
const sal_Unicode *const aMimeType[]
BitmapEx maBitmapEx
signed char sal_Int8
XOutFlags
Definition: xoutbmp.hxx:32
@ DontAddExtension
@ DontExpandFilename
@ UseNativeIfPossible
@ UseGifIfSensible
@ UseGifIfPossible