Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / gdi / gfxlink.cxx
blob1a383de72cccf97ad45f21eccde81e4301891c0e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/log.hxx>
21 #include <tools/stream.hxx>
22 #include <tools/vcompat.hxx>
23 #include <vcl/graph.hxx>
24 #include <vcl/gfxlink.hxx>
25 #include <vcl/graphicfilter.hxx>
26 #include <memory>
27 #include <TypeSerializer.hxx>
30 GfxLink::GfxLink()
31 : meType(GfxLinkType::NONE)
32 , mnUserId(0)
33 , mnSwapInDataSize(0)
34 , mbPrefMapModeValid(false)
35 , mbPrefSizeValid(false)
39 GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType)
40 : meType(nType)
41 , mnUserId(0)
42 , mpSwapInData(std::shared_ptr<sal_uInt8>(pBuf.release(), pBuf.get_deleter())) // std::move(pBuf) does not compile on Jenkins MacOSX (24 May 2016)
43 , mnSwapInDataSize(nSize)
44 , mbPrefMapModeValid(false)
45 , mbPrefSizeValid(false)
47 SAL_WARN_IF(mpSwapInData == nullptr || mnSwapInDataSize <= 0, "vcl",
48 "GfxLink::GfxLink(): empty/NULL buffer given");
51 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
53 bool bIsEqual = false;
55 if ( ( mnSwapInDataSize == rGfxLink.mnSwapInDataSize ) && ( meType == rGfxLink.meType ) )
57 const sal_uInt8* pSource = GetData();
58 const sal_uInt8* pDest = rGfxLink.GetData();
59 sal_uInt32 nSourceSize = GetDataSize();
60 sal_uInt32 nDestSize = rGfxLink.GetDataSize();
61 if ( pSource && pDest && ( nSourceSize == nDestSize ) )
63 bIsEqual = memcmp( pSource, pDest, nSourceSize ) == 0;
65 else if ( ( pSource == nullptr ) && ( pDest == nullptr ) )
66 bIsEqual = true;
68 return bIsEqual;
72 bool GfxLink::IsNative() const
74 return( meType >= GFX_LINK_FIRST_NATIVE_ID && meType <= GFX_LINK_LAST_NATIVE_ID );
78 const sal_uInt8* GfxLink::GetData() const
80 return mpSwapInData.get();
84 void GfxLink::SetPrefSize( const Size& rPrefSize )
86 maPrefSize = rPrefSize;
87 mbPrefSizeValid = true;
91 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
93 maPrefMapMode = rPrefMapMode;
94 mbPrefMapModeValid = true;
98 bool GfxLink::LoadNative( Graphic& rGraphic )
100 bool bRet = false;
102 if( IsNative() && mnSwapInDataSize )
104 const sal_uInt8* pData = GetData();
105 if (pData)
107 SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), mnSwapInDataSize, StreamMode::READ | StreamMode::WRITE);
108 OUString aShortName;
110 switch (meType)
112 case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
113 case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
114 case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
115 case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
116 case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
117 case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
118 case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
119 case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
120 case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
121 case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
122 default: break;
124 if (!aShortName.isEmpty())
126 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
127 sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
128 ErrCode nResult = rFilter.ImportGraphic(rGraphic, OUString(), aMemoryStream, nFormat);
129 if (nResult == ERRCODE_NONE)
130 bRet = true;
135 return bRet;
138 bool GfxLink::ExportNative( SvStream& rOStream ) const
140 if( GetDataSize() )
142 auto pData = GetSwapInData();
143 if (pData)
144 rOStream.WriteBytes( pData.get(), mnSwapInDataSize );
147 return ( rOStream.GetError() == ERRCODE_NONE );
150 SvStream& WriteGfxLink( SvStream& rOStream, const GfxLink& rGfxLink )
152 std::unique_ptr<VersionCompat> pCompat(new VersionCompat( rOStream, StreamMode::WRITE, 2 ));
153 TypeSerializer aSerializer(rOStream);
155 // Version 1
156 rOStream.WriteUInt16( static_cast<sal_uInt16>(rGfxLink.GetType()) ).WriteUInt32( rGfxLink.GetDataSize() ).WriteUInt32( rGfxLink.GetUserId() );
158 // Version 2
159 aSerializer.writeSize(rGfxLink.GetPrefSize());
160 WriteMapMode( rOStream, rGfxLink.GetPrefMapMode() );
162 pCompat.reset(); // destructor writes stuff into the header
164 if( rGfxLink.GetDataSize() )
166 auto pData = rGfxLink.GetSwapInData();
167 if (pData)
168 rOStream.WriteBytes( pData.get(), rGfxLink.mnSwapInDataSize );
171 return rOStream;
174 SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink)
176 Size aSize;
177 MapMode aMapMode;
178 bool bMapAndSizeValid( false );
179 std::unique_ptr<VersionCompat> pCompat(new VersionCompat( rIStream, StreamMode::READ ));
181 TypeSerializer aSerializer(rIStream);
183 // Version 1
184 sal_uInt16 nType(0);
185 sal_uInt32 nSize(0), nUserId(0);
186 rIStream.ReadUInt16(nType).ReadUInt32(nSize).ReadUInt32(nUserId);
188 if( pCompat->GetVersion() >= 2 )
190 aSerializer.readSize(aSize);
191 ReadMapMode( rIStream, aMapMode );
192 bMapAndSizeValid = true;
195 pCompat.reset(); // destructor writes stuff into the header
197 auto nRemainingData = rIStream.remainingSize();
198 if (nSize > nRemainingData)
200 SAL_WARN("vcl", "graphic link stream is smaller than requested size");
201 nSize = nRemainingData;
204 std::unique_ptr<sal_uInt8[]> pBuf(new sal_uInt8[ nSize ]);
205 rIStream.ReadBytes( pBuf.get(), nSize );
207 rGfxLink = GfxLink( std::move(pBuf), nSize, static_cast<GfxLinkType>(nType) );
208 rGfxLink.SetUserId( nUserId );
210 if( bMapAndSizeValid )
212 rGfxLink.SetPrefSize( aSize );
213 rGfxLink.SetPrefMapMode( aMapMode );
216 return rIStream;
219 std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const
221 return mpSwapInData;
224 bool GfxLink::IsEMF() const
226 const sal_uInt8* pGraphicAry = GetData();
227 if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
229 // check the magic number
230 if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
231 && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
233 //emf detected
234 return true;
237 return false;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */