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 constexpr OUStringLiteral FORMAT_BMP = u"bmp";
34 constexpr OUStringLiteral FORMAT_GIF = u"gif";
35 constexpr OUStringLiteral FORMAT_JPG = u"jpg";
36 constexpr OUStringLiteral FORMAT_PNG = u"png";
37 
38 using namespace com::sun::star;
39 
40 Animation XOutBitmap::MirrorAnimation( const Animation& rAnimation, bool bHMirr, bool bVMirr )
41 {
42  Animation aNewAnim( rAnimation );
43 
44  if( bHMirr || bVMirr )
45  {
46  const Size& rGlobalSize = aNewAnim.GetDisplaySizePixel();
47  BmpMirrorFlags nMirrorFlags = BmpMirrorFlags::NONE;
48 
49  if( bHMirr )
50  nMirrorFlags |= BmpMirrorFlags::Horizontal;
51 
52  if( bVMirr )
53  nMirrorFlags |= BmpMirrorFlags::Vertical;
54 
55  for( sal_uInt16 i = 0, nCount = aNewAnim.Count(); i < nCount; i++ )
56  {
57  AnimationBitmap aAnimationBitmap( aNewAnim.Get( i ) );
58 
59  // mirror the BitmapEx
60  aAnimationBitmap.maBitmapEx.Mirror( nMirrorFlags );
61 
62  // Adjust the positions inside the whole bitmap
63  if( bHMirr )
64  aAnimationBitmap.maPositionPixel.setX(rGlobalSize.Width() - aAnimationBitmap.maPositionPixel.X() -
65  aAnimationBitmap.maSizePixel.Width());
66 
67  if( bVMirr )
68  aAnimationBitmap.maPositionPixel.setY(rGlobalSize.Height() - aAnimationBitmap.maPositionPixel.Y() -
69  aAnimationBitmap.maSizePixel.Height());
70 
71  aNewAnim.Replace(aAnimationBitmap, i);
72  }
73  }
74 
75  return aNewAnim;
76 }
77 
78 Graphic XOutBitmap::MirrorGraphic( const Graphic& rGraphic, const BmpMirrorFlags nMirrorFlags )
79 {
80  Graphic aRetGraphic;
81 
82  if( nMirrorFlags != BmpMirrorFlags::NONE )
83  {
84  if( rGraphic.IsAnimated() )
85  {
86  aRetGraphic = MirrorAnimation( rGraphic.GetAnimation(),
87  bool( nMirrorFlags & BmpMirrorFlags::Horizontal ),
88  bool( nMirrorFlags & BmpMirrorFlags::Vertical ) );
89  }
90  else
91  {
92  BitmapEx aBmp( rGraphic.GetBitmapEx() );
93  aBmp.Mirror( nMirrorFlags );
94  aRetGraphic = aBmp;
95  }
96  }
97  else
98  aRetGraphic = rGraphic;
99 
100  return aRetGraphic;
101 }
102 
103 ErrCode XOutBitmap::WriteGraphic( const Graphic& rGraphic, OUString& rFileName,
104  const OUString& rFilterName, const XOutFlags nFlags,
105  const Size* pMtfSize_100TH_MM,
106  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
107 {
108  if( rGraphic.GetType() == GraphicType::NONE )
109  return ERRCODE_NONE;
110 
111  INetURLObject aURL( rFileName );
112  Graphic aGraphic;
113  OUString aExt;
116  bool bTransparent = rGraphic.IsTransparent(), bAnimated = rGraphic.IsAnimated();
117 
118  DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::WriteGraphic(...): invalid URL" );
119 
120  // calculate correct file name
121  if( !( nFlags & XOutFlags::DontExpandFilename ) )
122  {
123  OUString aStr( OUString::number( rGraphic.GetChecksum(), 16 ) );
124  if ( aStr[0] == '-' )
125  aStr = OUString::Concat("m") + aStr.subView(1);
126  OUString aName = aURL.getBase() + "_" + aURL.getExtension() + "_" + aStr;
127  aURL.setBase( aName );
128  }
129 
130  // #i121128# use shortcut to write Vector Graphic Data data in original form (if possible)
131  auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
132 
133  if (rVectorGraphicDataPtr && rVectorGraphicDataPtr->getBinaryDataContainer().getSize())
134  {
135  // Does the filter name match the original format?
136  const bool bIsSvg(rFilterName.equalsIgnoreAsciiCase("svg") && VectorGraphicDataType::Svg == rVectorGraphicDataPtr->getType());
137  const bool bIsWmf(rFilterName.equalsIgnoreAsciiCase("wmf") && VectorGraphicDataType::Wmf == rVectorGraphicDataPtr->getType());
138  bool bIsEmf(rFilterName.equalsIgnoreAsciiCase("emf") && VectorGraphicDataType::Emf == rVectorGraphicDataPtr->getType());
139  if (!bIsEmf)
140  {
141  bIsEmf = rFilterName.equalsIgnoreAsciiCase("emf") && rGraphic.GetGfxLink().IsEMF();
142  }
143  const bool bIsPdf(rFilterName.equalsIgnoreAsciiCase("pdf") && VectorGraphicDataType::Pdf == rVectorGraphicDataPtr->getType());
144 
145  if (bIsSvg || bIsWmf || bIsEmf || bIsPdf)
146  {
147  if (!(nFlags & XOutFlags::DontAddExtension))
148  {
149  aURL.setExtension(rFilterName);
150  }
151 
153  SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
154  SvStream* pOStm = aMedium.GetOutStream();
155 
156  if (pOStm)
157  {
158  auto & rDataContainer = rVectorGraphicDataPtr->getBinaryDataContainer();
159  pOStm->WriteBytes(rDataContainer.getData(), rDataContainer.getSize());
160  aMedium.Commit();
161 
162  if (!aMedium.GetError())
163  return ERRCODE_NONE;
164  }
165  }
166  }
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  // tdf#60684: use native format if possible but it must correspond to filter name
191  // or no specific format has been required
192  // without this, you may save for example file with png extension but jpg content
193  if( !aExt.isEmpty() && (aExt == rFilterName || rFilterName.isEmpty()) )
194  {
195  if( !(nFlags & XOutFlags::DontAddExtension) )
196  aURL.setExtension( aExt );
198 
199  SfxMedium aMedium(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC);
200  SvStream* pOStm = aMedium.GetOutStream();
201 
202  if( pOStm && aGfxLink.GetDataSize() && aGfxLink.GetData() )
203  {
204  pOStm->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
205  aMedium.Commit();
206 
207  if( !aMedium.GetError() )
208  return ERRCODE_NONE;
209  }
210  }
211  }
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  sal_uInt16 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, aPt, aSize);
256 
257  const Bitmap aBitmap( pVDev->GetBitmap( aPt, aSize ) );
258 
259  pVDev->SetBackground( aWallpaper );
260  pVDev->Erase();
261  rGraphic.Draw(*pVDev, 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, Point(), aSize);
284  aGraphic = BitmapEx(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  return nErr;
314 }
315 
316 bool XOutBitmap::GraphicToBase64(const Graphic& rGraphic, OUString& rOUString, bool bAddPrefix,
317  ConvertDataFormat aTargetFormat)
318 {
319  SvMemoryStream aOStm;
320  GfxLink aLink = rGraphic.GetGfxLink();
321 
322  if (aTargetFormat == ConvertDataFormat::Unknown)
323  {
324  switch (aLink.GetType())
325  {
326  case GfxLinkType::NativeJpg:
327  aTargetFormat = ConvertDataFormat::JPG;
328  break;
329  case GfxLinkType::NativePng:
330  aTargetFormat = ConvertDataFormat::PNG;
331  break;
332  case GfxLinkType::NativeSvg:
333  aTargetFormat = ConvertDataFormat::SVG;
334  break;
335  default:
336  // save everything else (including gif) into png
337  aTargetFormat = ConvertDataFormat::PNG;
338  break;
339  }
340  }
341 
342  ErrCode nErr = GraphicConverter::Export(aOStm,rGraphic,aTargetFormat);
343  if ( nErr )
344  {
345  SAL_WARN("svx", "XOutBitmap::GraphicToBase64() invalid Graphic? error: " << nErr );
346  return false;
347  }
348  css::uno::Sequence<sal_Int8> aOStmSeq( static_cast<sal_Int8 const *>(aOStm.GetData()),aOStm.TellEnd() );
349  OUStringBuffer aStrBuffer;
350  ::comphelper::Base64::encode(aStrBuffer,aOStmSeq);
351  rOUString = aStrBuffer.makeStringAndClear();
352 
353  if (bAddPrefix)
354  {
355  OUString aMimeType
357  rOUString = aMimeType + ";base64," + rOUString;
358  }
359 
360  return true;
361 }
362 
364  GraphicFilter& rFilter, const sal_uInt16 nFormat,
365  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
366 {
367  DBG_ASSERT( rURL.GetProtocol() != INetProtocol::NotValid, "XOutBitmap::ExportGraphic(...): invalid URL" );
368 
369  SfxMedium aMedium( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::SHARE_DENYNONE | StreamMode::TRUNC );
370  SvStream* pOStm = aMedium.GetOutStream();
372 
373  if( pOStm )
374  {
375  nRet = rFilter.ExportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pOStm, nFormat, pFilterData );
376 
377  aMedium.Commit();
378 
379  if( aMedium.GetError() && ( ERRCODE_NONE == nRet ) )
381  }
382 
383  return nRet;
384 }
385 
386 
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
URL aURL
constexpr OUStringLiteral FORMAT_PNG
Definition: _xoutbmp.cxx:36
static bool GraphicToBase64(const Graphic &rGraphic, OUString &rOUString, bool bAddPrefix=true, ConvertDataFormat aTargetFormat=ConvertDataFormat::Unknown)
Definition: _xoutbmp.cxx:316
BitmapChecksum GetChecksum() const
OUString getBase(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
constexpr OUStringLiteral FORMAT_JPG
Definition: _xoutbmp.cxx:35
void Draw(OutputDevice &rOutDev, const Point &rDestPt) const
bool setExtension(std::u16string_view rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
bool IsTransparent() const
BitmapEx maBitmapEx
#define ERRCODE_GRFILTER_IOERROR
bool IsGfxLink() const
virtual sal_uInt64 TellEnd() override
constexpr tools::Long Width() const
int nCount
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:103
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
bool IsAnimated() const
Animation GetAnimation() const
bool Mirror(BmpMirrorFlags nMirrorFlags)
#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:78
float u
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:363
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(std::u16string_view rTheBase, sal_Int32 nIndex=LAST_SEGMENT, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
constexpr OUStringLiteral FORMAT_GIF
Definition: _xoutbmp.cxx:34
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
constexpr tools::Long Height() const
OUString aName
INetProtocol GetProtocol() const
constexpr OUStringLiteral FORMAT_BMP
Definition: _xoutbmp.cxx:33
static Animation MirrorAnimation(const Animation &rAnimation, bool bHMirr, bool bVMirr)
Definition: _xoutbmp.cxx:40
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
static GraphicFilter & GetGraphicFilter()
size_t Count() const
#define SAL_WARN(area, stream)
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
BmpMirrorFlags