1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <vcl/graph.hxx>
25 #include <vcl/gfxlink.hxx>
26 #include <vcl/graphicfilter.hxx>
28 #include <o3tl/hash_combine.hxx>
31 : meType(GfxLinkType::NONE
)
34 , mbPrefMapModeValid(false)
35 , mbPrefSizeValid(false)
39 GfxLink::GfxLink(BinaryDataContainer aDataConainer
, GfxLinkType nType
)
42 , maDataContainer(std::move(aDataConainer
))
44 , mbPrefMapModeValid(false)
45 , mbPrefSizeValid(false)
49 size_t GfxLink::GetHash() const
53 std::size_t seed
= maDataContainer
.calculateHash();
54 o3tl::hash_combine(seed
, meType
);
60 bool GfxLink::operator==( const GfxLink
& rGfxLink
) const
62 if (GetDataSize() != rGfxLink
.GetDataSize()
63 || meType
!= rGfxLink
.meType
64 || GetHash() != rGfxLink
.GetHash())
67 const sal_uInt8
* pSource
= GetData();
68 const sal_uInt8
* pDestination
= rGfxLink
.GetData();
70 if (pSource
== pDestination
)
73 sal_uInt32 nSourceSize
= GetDataSize();
74 sal_uInt32 nDestSize
= rGfxLink
.GetDataSize();
76 if (pSource
&& pDestination
&& (nSourceSize
== nDestSize
))
77 return memcmp(pSource
, pDestination
, nSourceSize
) == 0;
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
108 if (IsNative() && !maDataContainer
.isEmpty())
110 const sal_uInt8
* pData
= GetData();
113 SvMemoryStream
aMemoryStream(const_cast<sal_uInt8
*>(pData
), GetDataSize(), StreamMode::READ
| StreamMode::WRITE
);
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;
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
)
145 bool GfxLink::ExportNative( SvStream
& rOStream
) const
149 auto pData
= GetData();
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))
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */