Update git submodules
[LibreOffice.git] / vcl / source / gdi / gfxlink.cxx
blobc6ca4678b007180a3fcfc8ec20dd5e241a4a8f74
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 <utility>
24 #include <vcl/graph.hxx>
25 #include <vcl/gfxlink.hxx>
26 #include <vcl/graphicfilter.hxx>
27 #include <memory>
28 #include <o3tl/hash_combine.hxx>
30 GfxLink::GfxLink()
31 : meType(GfxLinkType::NONE)
32 , mnUserId(0)
33 , maHash(0)
34 , mbPrefMapModeValid(false)
35 , mbPrefSizeValid(false)
39 GfxLink::GfxLink(BinaryDataContainer aDataConainer, GfxLinkType nType)
40 : meType(nType)
41 , mnUserId(0)
42 , maDataContainer(std::move(aDataConainer))
43 , maHash(0)
44 , mbPrefMapModeValid(false)
45 , mbPrefSizeValid(false)
49 size_t GfxLink::GetHash() const
51 if (!maHash)
53 std::size_t seed = maDataContainer.calculateHash();
54 o3tl::hash_combine(seed, meType);
55 maHash = seed;
57 return maHash;
60 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
62 if (GetDataSize() != rGfxLink.GetDataSize()
63 || meType != rGfxLink.meType
64 || GetHash() != rGfxLink.GetHash())
65 return false;
67 const sal_uInt8* pSource = GetData();
68 const sal_uInt8* pDestination = rGfxLink.GetData();
70 if (pSource == pDestination)
71 return true;
73 sal_uInt32 nSourceSize = GetDataSize();
74 sal_uInt32 nDestSize = rGfxLink.GetDataSize();
76 if (pSource && pDestination && (nSourceSize == nDestSize))
77 return memcmp(pSource, pDestination, nSourceSize) == 0;
79 return false;
82 bool GfxLink::IsNative() const
84 return meType >= GfxLinkType::NativeFirst && meType <= GfxLinkType::NativeLast;
87 const sal_uInt8* GfxLink::GetData() const
89 return maDataContainer.getData();
92 void GfxLink::SetPrefSize( const Size& rPrefSize )
94 maPrefSize = rPrefSize;
95 mbPrefSizeValid = true;
98 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
100 maPrefMapMode = rPrefMapMode;
101 mbPrefMapModeValid = true;
104 bool GfxLink::LoadNative( Graphic& rGraphic ) const
106 bool bRet = false;
108 if (IsNative() && !maDataContainer.isEmpty())
110 const sal_uInt8* pData = GetData();
111 if (pData)
113 SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), GetDataSize(), StreamMode::READ | StreamMode::WRITE);
114 OUString aShortName;
116 switch (meType)
118 case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
119 case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
120 case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
121 case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
122 case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
123 case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
124 case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
125 case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
126 case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
127 case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
128 case GfxLinkType::NativeWebp: aShortName = WEBP_SHORTNAME; break;
129 default: break;
131 if (!aShortName.isEmpty())
133 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
134 sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
135 ErrCode nResult = rFilter.ImportGraphic(rGraphic, u"", aMemoryStream, nFormat);
136 if (nResult == ERRCODE_NONE)
137 bRet = true;
142 return bRet;
145 bool GfxLink::ExportNative( SvStream& rOStream ) const
147 if( GetDataSize() )
149 auto pData = GetData();
150 if (pData)
151 rOStream.WriteBytes(pData, GetDataSize());
154 return ( rOStream.GetError() == ERRCODE_NONE );
157 bool GfxLink::IsEMF() const
159 const sal_uInt8* pGraphicAry = GetData();
160 if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
162 // check the magic number
163 if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
164 && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
166 //emf detected
167 return true;
170 return false;
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */