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 
33 #define FORMAT_BMP "bmp"
34 #define FORMAT_GIF "gif"
35 #define FORMAT_JPG "jpg"
36 #define FORMAT_PNG "png"
37 
38 using namespace com::sun::star;
39 
41 
42 Animation XOutBitmap::MirrorAnimation( const Animation& rAnimation, bool bHMirr, bool bVMirr )
43 {
44  Animation aNewAnim( rAnimation );
45 
46  if( bHMirr || bVMirr )
47  {
48  const Size& rGlobalSize = aNewAnim.GetDisplaySizePixel();
49  BmpMirrorFlags nMirrorFlags = BmpMirrorFlags::NONE;
50 
51  if( bHMirr )
52  nMirrorFlags |= BmpMirrorFlags::Horizontal;
53 
54  if( bVMirr )
55  nMirrorFlags |= BmpMirrorFlags::Vertical;
56 
57  for( sal_uInt16 i = 0, nCount = aNewAnim.Count(); i < nCount; i++ )
58  {
59  AnimationBitmap aAnimationBitmap( aNewAnim.Get( i ) );
60 
61  // mirror the BitmapEx
62  aAnimationBitmap.maBitmapEx.Mirror( nMirrorFlags );
63 
64  // Adjust the positions inside the whole bitmap
65  if( bHMirr )
66  aAnimationBitmap.maPositionPixel.setX(rGlobalSize.Width() - aAnimationBitmap.maPositionPixel.X() -
67  aAnimationBitmap.maSizePixel.Width());
68 
69  if( bVMirr )
70  aAnimationBitmap.maPositionPixel.setY(rGlobalSize.Height() - aAnimationBitmap.maPositionPixel.Y() -
71  aAnimationBitmap.maSizePixel.Height());
72 
73  aNewAnim.Replace(aAnimationBitmap, i);
74  }
75  }
76 
77  return aNewAnim;
78 }
79 
80 Graphic XOutBitmap::MirrorGraphic( const Graphic& rGraphic, const BmpMirrorFlags nMirrorFlags )
81 {
82  Graphic aRetGraphic;
83 
84  if( nMirrorFlags != BmpMirrorFlags::NONE )
85  {
86  if( rGraphic.IsAnimated() )
87  {
88  aRetGraphic = MirrorAnimation( rGraphic.GetAnimation(),
89  bool( nMirrorFlags & BmpMirrorFlags::Horizontal ),
90  bool( nMirrorFlags & BmpMirrorFlags::Vertical ) );
91  }
92  else
93  {
94  BitmapEx aBmp( rGraphic.GetBitmapEx() );
95  aBmp.Mirror( nMirrorFlags );
96  aRetGraphic = aBmp;
97  }
98  }
99  else
100  aRetGraphic = rGraphic;
101 
102  return aRetGraphic;
103 }
104 
105 ErrCode XOutBitmap::WriteGraphic( const Graphic& rGraphic, OUString& rFileName,
106  const OUString& rFilterName, const XOutFlags nFlags,
107  const Size* pMtfSize_100TH_MM,
108  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
109 {
110  if( rGraphic.GetType() != GraphicType::NONE )
111  {
112  INetURLObject aURL( rFileName );
113  Graphic aGraphic;
114  OUString aExt;
117  sal_uInt16 nFilter = GRFILTER_FORMAT_NOTFOUND;
118  bool bTransparent = rGraphic.IsTransparent(), bAnimated = rGraphic.IsAnimated();
119 
120  DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::WriteGraphic(...): invalid URL" );
121 
122  // calculate correct file name
123  if( !( nFlags & XOutFlags::DontExpandFilename ) )
124  {
125  OUString aStr( OUString::number( rGraphic.GetChecksum(), 16 ) );
126  if ( aStr[0] == '-' )
127  aStr = "m" + aStr.copy(1);
128  OUString aName = aURL.getBase() + "_" + aURL.getExtension() + "_" + aStr;
129  aURL.setBase( aName );
130  }
131 
132  // #i121128# use shortcut to write Vector Graphic Data data in original form (if possible)
133  auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
134 
135  if (rVectorGraphicDataPtr && rVectorGraphicDataPtr->getVectorGraphicDataArrayLength())
136  {
137  const bool bIsSvg(rFilterName.equalsIgnoreAsciiCase("svg") && VectorGraphicDataType::Svg == rVectorGraphicDataPtr->getVectorGraphicDataType());
138  const bool bIsWmf(rFilterName.equalsIgnoreAsciiCase("wmf") && VectorGraphicDataType::Wmf == rVectorGraphicDataPtr->getVectorGraphicDataType());
139  const bool bIsEmf(rFilterName.equalsIgnoreAsciiCase("emf") && VectorGraphicDataType::Emf == rVectorGraphicDataPtr->getVectorGraphicDataType());
140  const bool bIsPdf(rFilterName.equalsIgnoreAsciiCase("pdf") && VectorGraphicDataType::Pdf == rVectorGraphicDataPtr->getVectorGraphicDataType());
141 
142  if (bIsSvg || bIsWmf || bIsEmf || bIsPdf)
143  {
144  if (!(nFlags & XOutFlags::DontAddExtension))
145  {
146  aURL.setExtension(rFilterName);
147  }
148 
150  SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
151  SvStream* pOStm = aMedium.GetOutStream();
152 
153  if (pOStm)
154  {
155  pOStm->WriteBytes(rVectorGraphicDataPtr->getVectorGraphicDataArray().getConstArray(), rVectorGraphicDataPtr->getVectorGraphicDataArrayLength());
156  aMedium.Commit();
157 
158  if (!aMedium.GetError())
159  {
160  nErr = ERRCODE_NONE;
161  }
162  }
163  }
164  }
165 
166  if( ERRCODE_NONE != nErr )
167  {
168  if( ( nFlags & XOutFlags::UseNativeIfPossible ) &&
169  !( nFlags & XOutFlags::MirrorHorz ) &&
170  !( nFlags & XOutFlags::MirrorVert ) &&
171  ( rGraphic.GetType() != GraphicType::GdiMetafile ) && rGraphic.IsGfxLink() )
172  {
173  // try to write native link
174  const GfxLink aGfxLink( rGraphic.GetGfxLink() );
175 
176  switch( aGfxLink.GetType() )
177  {
178  case GfxLinkType::NativeGif: aExt = FORMAT_GIF; break;
179 
180  // #i15508# added BMP type for better exports (no call/trigger found, prob used in HTML export)
181  case GfxLinkType::NativeBmp: aExt = FORMAT_BMP; break;
182 
183  case GfxLinkType::NativeJpg: aExt = FORMAT_JPG; break;
184  case GfxLinkType::NativePng: aExt = FORMAT_PNG; break;
185 
186  default:
187  break;
188  }
189 
190  if( !aExt.isEmpty() )
191  {
192  if( !(nFlags & XOutFlags::DontAddExtension) )
193  aURL.setExtension( aExt );
195 
196  SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
197  SvStream* pOStm = aMedium.GetOutStream();
198 
199  if( pOStm && aGfxLink.GetDataSize() && aGfxLink.GetData() )
200  {
201  pOStm->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
202  aMedium.Commit();
203 
204  if( !aMedium.GetError() )
205  nErr = ERRCODE_NONE;
206  }
207  }
208  }
209  }
210 
211  if( ERRCODE_NONE != nErr )
212  {
213  OUString aFilter( rFilterName );
214  bool bWriteTransGrf = ( aFilter.equalsIgnoreAsciiCase( "transgrf" ) ) ||
215  ( aFilter.equalsIgnoreAsciiCase( "gif" ) ) ||
216  ( nFlags & XOutFlags::UseGifIfPossible ) ||
217  ( ( nFlags & XOutFlags::UseGifIfSensible ) && ( bAnimated || bTransparent ) );
218 
219  // get filter and extension
220  if( bWriteTransGrf )
221  aFilter = FORMAT_GIF;
222 
223  nFilter = rFilter.GetExportFormatNumberForShortName( aFilter );
224 
225  if( GRFILTER_FORMAT_NOTFOUND == nFilter )
226  {
227  nFilter = rFilter.GetExportFormatNumberForShortName( FORMAT_PNG );
228 
229  if( GRFILTER_FORMAT_NOTFOUND == nFilter )
230  nFilter = rFilter.GetExportFormatNumberForShortName( FORMAT_BMP );
231  }
232 
233  if( GRFILTER_FORMAT_NOTFOUND != nFilter )
234  {
235  aExt = rFilter.GetExportFormatShortName( nFilter ).toAsciiLowerCase();
236 
237  if( bWriteTransGrf )
238  {
239  if( bAnimated )
240  aGraphic = rGraphic;
241  else
242  {
243  if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GraphicType::Bitmap ) )
244  {
246  const Size aSize(pVDev->LogicToPixel(*pMtfSize_100TH_MM, MapMode(MapUnit::Map100thMM)));
247 
248  if( pVDev->SetOutputSizePixel( aSize ) )
249  {
250  const Wallpaper aWallpaper( pVDev->GetBackground() );
251  const Point aPt;
252 
253  pVDev->SetBackground( Wallpaper( COL_BLACK ) );
254  pVDev->Erase();
255  rGraphic.Draw( pVDev.get(), aPt, aSize );
256 
257  const Bitmap aBitmap( pVDev->GetBitmap( aPt, aSize ) );
258 
259  pVDev->SetBackground( aWallpaper );
260  pVDev->Erase();
261  rGraphic.Draw( pVDev.get(), aPt, aSize );
262 
263  pVDev->SetRasterOp( RasterOp::Xor );
264  pVDev->DrawBitmap( aPt, aSize, aBitmap );
265  aGraphic = BitmapEx( aBitmap, pVDev->GetBitmap( aPt, aSize ) );
266  }
267  else
268  aGraphic = rGraphic.GetBitmapEx();
269  }
270  else
271  aGraphic = rGraphic.GetBitmapEx();
272  }
273  }
274  else
275  {
276  if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GraphicType::Bitmap ) )
277  {
279  const Size aSize(pVDev->LogicToPixel(*pMtfSize_100TH_MM, MapMode(MapUnit::Map100thMM)));
280 
281  if( pVDev->SetOutputSizePixel( aSize ) )
282  {
283  rGraphic.Draw( pVDev.get(), Point(), aSize );
284  aGraphic = pVDev->GetBitmap( Point(), aSize );
285  }
286  else
287  aGraphic = rGraphic.GetBitmapEx();
288  }
289  else
290  aGraphic = rGraphic.GetBitmapEx();
291  }
292 
293  // mirror?
294  if( ( nFlags & XOutFlags::MirrorHorz ) || ( nFlags & XOutFlags::MirrorVert ) )
295  {
296  BmpMirrorFlags nBmpMirrorFlags = BmpMirrorFlags::NONE;
297  if( nFlags & XOutFlags::MirrorHorz )
298  nBmpMirrorFlags |= BmpMirrorFlags::Horizontal;
299  if( nFlags & XOutFlags::MirrorVert )
300  nBmpMirrorFlags |= BmpMirrorFlags::Vertical;
301  aGraphic = MirrorGraphic( aGraphic, nBmpMirrorFlags );
302  }
303 
304  if (aGraphic.GetType() != GraphicType::NONE)
305  {
306  if( !(nFlags & XOutFlags::DontAddExtension) )
307  aURL.setExtension( aExt );
309  nErr = ExportGraphic( aGraphic, aURL, rFilter, nFilter, pFilterData );
310  }
311  }
312  }
313 
314  return nErr;
315  }
316  else
317  {
318  return ERRCODE_NONE;
319  }
320 }
321 
322 bool XOutBitmap::GraphicToBase64(const Graphic& rGraphic, OUString& rOUString, bool bAddPrefix,
323  ConvertDataFormat aTargetFormat)
324 {
325  SvMemoryStream aOStm;
326  GfxLink aLink = rGraphic.GetGfxLink();
327 
328  if (aTargetFormat == ConvertDataFormat::Unknown)
329  {
330  switch (aLink.GetType())
331  {
332  case GfxLinkType::NativeJpg:
333  aTargetFormat = ConvertDataFormat::JPG;
334  break;
335  case GfxLinkType::NativePng:
336  aTargetFormat = ConvertDataFormat::PNG;
337  break;
338  case GfxLinkType::NativeSvg:
339  aTargetFormat = ConvertDataFormat::SVG;
340  break;
341  default:
342  // save everything else (including gif) into png
343  aTargetFormat = ConvertDataFormat::PNG;
344  break;
345  }
346  }
347 
348  ErrCode nErr = GraphicConverter::Export(aOStm,rGraphic,aTargetFormat);
349  if ( nErr )
350  {
351  SAL_WARN("svx", "XOutBitmap::GraphicToBase64() invalid Graphic? error: " << nErr );
352  return false;
353  }
354  css::uno::Sequence<sal_Int8> aOStmSeq( static_cast<sal_Int8 const *>(aOStm.GetData()),aOStm.TellEnd() );
355  OUStringBuffer aStrBuffer;
356  ::comphelper::Base64::encode(aStrBuffer,aOStmSeq);
357  rOUString = aStrBuffer.makeStringAndClear();
358 
359  if (bAddPrefix)
360  {
361  OUString aMimeType
363  rOUString = aMimeType + ";base64," + rOUString;
364  }
365 
366  return true;
367 }
368 
370  GraphicFilter& rFilter, const sal_uInt16 nFormat,
371  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
372 {
373  DBG_ASSERT( rURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::ExportGraphic(...): invalid URL" );
374 
375  SfxMedium aMedium( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC );
376  SvStream* pOStm = aMedium.GetOutStream();
378 
379  if( pOStm )
380  {
381  pGrfFilter = &rFilter;
382 
383  nRet = rFilter.ExportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pOStm, nFormat, pFilterData );
384 
385  pGrfFilter = nullptr;
386  aMedium.Commit();
387 
388  if( aMedium.GetError() && ( ERRCODE_NONE == nRet ) )
390  }
391 
392  return nRet;
393 }
394 
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define FORMAT_PNG
Definition: _xoutbmp.cxx:36
long Width() const
static OUString GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
URL aURL
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
static bool GraphicToBase64(const Graphic &rGraphic, OUString &rOUString, bool bAddPrefix=true, ConvertDataFormat aTargetFormat=ConvertDataFormat::Unknown)
Definition: _xoutbmp.cxx:322
long Height() const
BitmapChecksum GetChecksum() const
OUString getBase(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
#define FORMAT_BMP
Definition: _xoutbmp.cxx:33
bool IsTransparent() const
BitmapEx maBitmapEx
#define ERRCODE_GRFILTER_IOERROR
bool IsGfxLink() const
virtual sal_uInt64 TellEnd() override
bool setExtension(OUString const &rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
int nCount
sal_uInt16 GetExportFormatNumberForShortName(const OUString &rShortName)
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:105
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
bool IsAnimated() const
Animation GetAnimation() const
bool Mirror(BmpMirrorFlags nMirrorFlags)
void Draw(OutputDevice *pOutDev, const Point &rDestPt) const
#define DBG_ASSERT(sCon, aError)
void Replace(const AnimationBitmap &rNewAnimationBmp, sal_uInt16 nAnimation)
int i
GraphicType GetType() const
ConvertDataFormat
const char *const aMimeType[]
std::size_t WriteBytes(const void *pData, std::size_t nSize)
static Graphic MirrorGraphic(const Graphic &rGraphic, const BmpMirrorFlags nMirrorFlags)
Definition: _xoutbmp.cxx:80
BmpMirrorFlags
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
const Size & GetDisplaySizePixel() const
GfxLink GetGfxLink() const
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:369
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
const AnimationBitmap & Get(sal_uInt16 nAnimation) const
bool setBase(OUString const &rTheBase, sal_Int32 nIndex=LAST_SEGMENT, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetExportFormatShortName(sal_uInt16 nFormat)
#define ERRCODE_GRFILTER_FILTERERROR
static ErrCode Export(SvStream &rOStm, const Graphic &rGraphic, ConvertDataFormat nFormat)
XOutFlags
Definition: xoutbmp.hxx:32
#define ERRCODE_NONE
static GraphicFilter * pGrfFilter
Definition: xoutbmp.hxx:57
OUString aName
INetProtocol GetProtocol() const
static Animation MirrorAnimation(const Animation &rAnimation, bool bHMirr, bool bVMirr)
Definition: _xoutbmp.cxx:42
#define FORMAT_GIF
Definition: _xoutbmp.cxx:34
static GraphicFilter & GetGraphicFilter()
size_t Count() const
reference_type * get() const
#define SAL_WARN(area, stream)
#define FORMAT_JPG
Definition: _xoutbmp.cxx:35
const std::shared_ptr< VectorGraphicData > & getVectorGraphicData() const
aStr
OUString getExtension(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
const void * GetData()
#define GRFILTER_FORMAT_NOTFOUND