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 #ifndef INCLUDED_VCL_GRAPHICFILTER_HXX
21 #define INCLUDED_VCL_GRAPHICFILTER_HXX
23 #include <tools/gen.hxx>
24 #include <vcl/dllapi.h>
25 #include <vcl/graph.hxx>
26 #include <comphelper/errcode.hxx>
27 #include <o3tl/typed_flags_set.hxx>
28 #include <vcl/BinaryDataContainer.hxx>
29 #include <vcl/graphic/GraphicMetadata.hxx>
33 namespace com::sun::star::beans
{ struct PropertyValue
; }
34 namespace com::sun::star::uno
{ template <class E
> class Sequence
; }
38 class FilterConfigCache
;
43 #define ERRCODE_GRFILTER_OPENERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 1)
44 #define ERRCODE_GRFILTER_IOERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 2)
45 #define ERRCODE_GRFILTER_FORMATERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 3)
46 #define ERRCODE_GRFILTER_VERSIONERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 4)
47 #define ERRCODE_GRFILTER_FILTERERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 5)
48 #define ERRCODE_GRFILTER_TOOBIG ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 7)
50 #define GRFILTER_OUTHINT_GREY 1
52 #define GRFILTER_FORMAT_NOTFOUND (sal_uInt16(0xFFFF))
53 #define GRFILTER_FORMAT_DONTKNOW (sal_uInt16(0xFFFF))
55 enum class GraphicFilterImportFlags
58 SetLogsizeForJpeg
= 0x001,
59 DontSetLogsizeForJpeg
= 0x002,
60 /// Only create a bitmap, do not read pixel data.
61 OnlyCreateBitmap
= 0x020,
62 /// Read pixel data into an existing bitmap.
63 UseExistingBitmap
= 0x040,
67 template<> struct typed_flags
<GraphicFilterImportFlags
> : is_typed_flags
<GraphicFilterImportFlags
, 0x0063> {};
70 #define IMP_BMP "SVBMP"
71 #define IMP_MOV "SVMOV"
72 #define IMP_SVMETAFILE "SVMETAFILE"
73 #define IMP_WMF "SVWMF"
74 #define IMP_WMZ "SVWMZ"
75 #define IMP_EMF "SVEMF"
76 #define IMP_EMZ "SVEMZ"
77 #define IMP_GIF "SVIGIF"
78 #define IMP_PNG "SVIPNG"
79 #define IMP_JPEG "SVIJPEG"
80 #define IMP_XBM "SVIXBM"
81 #define IMP_XPM "SVIXPM"
82 #define IMP_SVG "SVISVG"
83 #define IMP_SVGZ "SVISVGZ"
84 #define IMP_PDF "SVIPDF"
85 #define IMP_TIFF "SVTIFF"
86 #define IMP_TGA "SVTGA"
87 #define IMP_PICT "SVPICT"
88 #define IMP_MET "SVMET"
89 #define IMP_RAS "SVRAS"
90 #define IMP_PCX "SVPCX"
91 #define IMP_EPS "SVIEPS"
92 #define IMP_PSD "SVPSD"
93 #define IMP_PCD "SVPCD"
94 #define IMP_PBM "SVPBM"
95 #define IMP_DXF "SVDXF"
96 #define IMP_WEBP "SVIWEBP"
98 #define EXP_BMP "SVBMP"
99 #define EXP_SVMETAFILE "SVMETAFILE"
100 #define EXP_WMF "SVWMF"
101 #define EXP_WMZ "SVWMZ"
102 #define EXP_EMF "SVEMF"
103 #define EXP_EMZ "SVEMZ"
104 #define EXP_JPEG "SVEJPEG"
105 #define EXP_SVG "SVESVG"
106 #define EXP_SVGZ "SVESVGZ"
107 #define EXP_PDF "SVEPDF"
108 #define EXP_PNG "SVEPNG"
109 #define EXP_TIFF "SVTIFF"
110 #define EXP_EPS "SVEEPS"
111 #define EXP_GIF "SVEGIF"
112 #define EXP_WEBP "SVEWEBP"
115 inline constexpr OUStringLiteral BMP_SHORTNAME
= u
"BMP";
116 inline constexpr OUStringLiteral GIF_SHORTNAME
= u
"GIF";
117 inline constexpr OUStringLiteral JPG_SHORTNAME
= u
"JPG";
118 inline constexpr OUStringLiteral MET_SHORTNAME
= u
"MET";
119 inline constexpr OUStringLiteral PCT_SHORTNAME
= u
"PCT";
120 inline constexpr OUStringLiteral PNG_SHORTNAME
= u
"PNG";
121 inline constexpr OUStringLiteral SVM_SHORTNAME
= u
"SVM";
122 inline constexpr OUStringLiteral TIF_SHORTNAME
= u
"TIF";
123 inline constexpr OUStringLiteral WMF_SHORTNAME
= u
"WMF";
124 inline constexpr OUStringLiteral EMF_SHORTNAME
= u
"EMF";
125 inline constexpr OUStringLiteral SVG_SHORTNAME
= u
"SVG";
126 inline constexpr OUStringLiteral PDF_SHORTNAME
= u
"PDF";
127 inline constexpr OUStringLiteral WEBP_SHORTNAME
= u
"WEBP";
129 class VCL_DLLPUBLIC GraphicDescriptor final
133 GraphicMetadata aMetadata
;
138 bool ImpDetectBMP( SvStream
& rStm
, bool bExtendedInfo
);
139 bool ImpDetectGIF( SvStream
& rStm
, bool bExtendedInfo
);
140 bool ImpDetectJPG( SvStream
& rStm
, bool bExtendedInfo
);
141 bool ImpDetectPCD( SvStream
& rStm
, bool bExtendedInfo
);
142 bool ImpDetectPCX( SvStream
& rStm
);
143 bool ImpDetectPNG( SvStream
& rStm
, bool bExtendedInfo
);
144 bool ImpDetectTIF( SvStream
& rStm
, bool bExtendedInfo
);
145 bool ImpDetectXBM( SvStream
& rStm
, bool bExtendedInfo
);
146 bool ImpDetectXPM( SvStream
& rStm
, bool bExtendedInfo
);
147 bool ImpDetectPBM( SvStream
& rStm
, bool bExtendedInfo
);
148 bool ImpDetectPGM( SvStream
& rStm
, bool bExtendedInfo
);
149 bool ImpDetectPPM( SvStream
& rStm
, bool bExtendedInfo
);
150 bool ImpDetectRAS( SvStream
& rStm
, bool bExtendedInfo
);
151 bool ImpDetectTGA( SvStream
& rStm
, bool bExtendedInfo
);
152 bool ImpDetectPSD( SvStream
& rStm
, bool bExtendedInfo
);
153 bool ImpDetectEPS( SvStream
& rStm
, bool bExtendedInfo
);
154 bool ImpDetectWEBP( SvStream
& rStm
, bool bExtendedInfo
);
155 bool ImpDetectDXF( SvStream
& rStm
, bool bExtendedInfo
);
156 bool ImpDetectMET( SvStream
& rStm
, bool bExtendedInfo
);
157 bool ImpDetectPCT( SvStream
& rStm
, bool bExtendedInfo
);
158 bool ImpDetectSVM( SvStream
& rStm
, bool bExtendedInfo
);
159 bool ImpDetectWMF( SvStream
& rStm
, bool bExtendedInfo
);
160 bool ImpDetectEMF( SvStream
& rStm
, bool bExtendedInfo
);
161 bool ImpDetectSVG( SvStream
& rStm
, bool bExtendedInfo
);
162 GraphicDescriptor( const GraphicDescriptor
& ) = delete;
163 GraphicDescriptor
& operator=( const GraphicDescriptor
& ) = delete;
167 /** Ctor to set a filename
169 Detect() must be called to identify the file
170 If the file has no unique header (Mtf's), the format
171 is determined from the extension */
172 GraphicDescriptor( const INetURLObject
& rPath
);
174 /** Ctor using a stream
176 Detect() must be called to identify the file
177 As some formats (Mtf's) do not have a unique header, it makes sense
178 to supply the file name (incl. ext.), so that the format can be
179 derived from the extension */
180 GraphicDescriptor( SvStream
& rInStream
, const OUString
* pPath
);
182 ~GraphicDescriptor();
184 /** starts the detection
186 if bExtendedInfo == true the file header is used to derive
187 as many properties as possible (size, color, etc.) */
188 bool Detect( bool bExtendedInfo
= false );
190 /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
191 GraphicFileFormat
GetFileFormat() const { return aMetadata
.mnFormat
; }
193 /** @return graphic size in pixels or 0 size */
194 const Size
& GetSizePixel() const { return aMetadata
.maPixSize
; }
196 /** @return the logical graphic size in 1/100mm or 0 size */
197 const Size
& GetSize_100TH_MM() const { return aMetadata
.maLogSize
; }
200 * Returns the logic size, according to the map mode available via GetPreferredMapMode(). Prefer
201 * this size over GetSize_100TH_MM().
203 const std::optional
<Size
>& GetPreferredLogSize() const { return aMetadata
.maPreferredLogSize
; }
206 * If available, this returns the map mode the graphic prefers, which may be other than pixel or
207 * 100th mm. Prefer this map mode over just assuming MapUnit::Map100thMM.
209 const std::optional
<MapMode
>& GetPreferredMapMode() const { return aMetadata
.maPreferredMapMode
; }
211 /** @return bits/pixel or 0 **/
212 sal_uInt16
GetBitsPerPixel() const { return aMetadata
.mnBitsPerPixel
; }
214 /** @return number of color channels */
215 sal_uInt8
GetNumberOfImageComponents() const { return aMetadata
.mnNumberOfImageComponents
; }
217 /** @return whether image supports transparency */
218 bool IsTransparent() const { return aMetadata
.mbIsTransparent
; }
220 /** @return whether image supports alpha values for translucent colours */
221 bool IsAlpha() const { return aMetadata
.mbIsAlpha
; }
223 /** @return filter number that is needed by the GraphFilter to read this format */
224 static OUString
GetImportFormatShortName( GraphicFileFormat nFormat
);
227 /** Class to import and export graphic formats. */
228 class VCL_DLLPUBLIC GraphicFilter
231 GraphicFilter( bool bUseConfig
= true );
234 sal_uInt16
GetImportFormatCount() const;
235 sal_uInt16
GetImportFormatNumber( std::u16string_view rFormatName
);
236 sal_uInt16
GetImportFormatNumberForShortName( std::u16string_view rShortName
);
237 sal_uInt16
GetImportFormatNumberForTypeName( std::u16string_view rType
);
238 OUString
GetImportFormatName( sal_uInt16 nFormat
);
239 OUString
GetImportFormatTypeName( sal_uInt16 nFormat
);
241 OUString
GetImportFormatMediaType( sal_uInt16 nFormat
);
243 OUString
GetImportFormatShortName( sal_uInt16 nFormat
);
244 OUString
GetImportWildcard( sal_uInt16 nFormat
, sal_Int32 nEntry
);
246 sal_uInt16
GetExportFormatCount() const;
247 sal_uInt16
GetExportFormatNumber( std::u16string_view rFormatName
);
248 sal_uInt16
GetExportFormatNumberForMediaType( std::u16string_view rShortName
);
249 sal_uInt16
GetExportFormatNumberForShortName( std::u16string_view rShortName
);
250 OUString
GetExportInternalFilterName( sal_uInt16 nFormat
);
251 sal_uInt16
GetExportFormatNumberForTypeName( std::u16string_view rType
);
252 OUString
GetExportFormatName( sal_uInt16 nFormat
);
253 OUString
GetExportFormatMediaType( sal_uInt16 nFormat
);
254 OUString
GetExportFormatShortName( sal_uInt16 nFormat
);
255 OUString
GetExportWildcard( sal_uInt16 nFormat
);
256 bool IsExportPixelFormat( sal_uInt16 nFormat
);
258 ErrCode
ExportGraphic( const Graphic
& rGraphic
, const INetURLObject
& rPath
,
260 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
261 ErrCode
ExportGraphic( const Graphic
& rGraphic
, std::u16string_view rPath
,
262 SvStream
& rOStm
, sal_uInt16 nFormat
,
263 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
265 ErrCode
CanImportGraphic( const INetURLObject
& rPath
,
267 sal_uInt16
* pDeterminedFormat
);
269 ErrCode
ImportGraphic( Graphic
& rGraphic
, const INetURLObject
& rPath
,
270 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
271 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
);
273 ErrCode
CanImportGraphic( std::u16string_view rPath
, SvStream
& rStream
,
275 sal_uInt16
* pDeterminedFormat
);
277 ErrCode
ImportGraphic( Graphic
& rGraphic
, std::u16string_view rPath
,
279 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
280 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
);
282 /// Imports multiple graphics.
284 /// The resulting graphic is added to rGraphics on success, nullptr is added on failure.
285 void ImportGraphics(std::vector
< std::shared_ptr
<Graphic
> >& rGraphics
, std::vector
< std::unique_ptr
<SvStream
> > vStreams
);
288 Tries to ensure all Graphic objects are available (Graphic::isAvailable()). Only an optimization, may
289 not process all items.
291 void MakeGraphicsAvailableThreaded(std::vector
< Graphic
* >& rGraphics
);
293 // Setting sizeLimit limits how much will be read from the stream.
294 Graphic
ImportUnloadedGraphic(SvStream
& rIStream
, sal_uInt64 sizeLimit
= 0, const Size
* pSizeHint
= nullptr);
296 const ErrCode
& GetLastError() const { return *mxErrorEx
;}
297 void ResetLastError();
299 Link
<ConvertData
&,bool> GetFilterCallback() const;
300 static GraphicFilter
& GetGraphicFilter();
301 static ErrCode
LoadGraphic( const OUString
& rPath
, const OUString
& rFilter
,
303 GraphicFilter
* pFilter
= nullptr,
304 sal_uInt16
* pDeterminedFormat
= nullptr );
306 ErrCode
compressAsPNG(const Graphic
& rGraphic
, SvStream
& rOutputStream
);
308 static ErrCode
readGIF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
309 static ErrCode
readPNG(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
,
310 BinaryDataContainer
& rpGraphicContent
);
311 static ErrCode
readJPEG(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
,
312 GraphicFilterImportFlags nImportFlags
);
313 static ErrCode
readSVG(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
,
314 BinaryDataContainer
& rpGraphicContent
);
315 static ErrCode
readXBM(SvStream
& rStream
, Graphic
& rGraphic
);
316 static ErrCode
readXPM(SvStream
& rStream
, Graphic
& rGraphic
);
318 static ErrCode
readWMF_EMF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
, VectorGraphicDataType eType
);
319 static ErrCode
readWMF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
320 static ErrCode
readEMF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
322 static ErrCode
readPDF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
323 static ErrCode
readTIFF(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
324 static ErrCode
readWithTypeSerializer(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
, std::u16string_view aFilterName
);
325 static ErrCode
readBMP(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
326 static ErrCode
readTGA(SvStream
& rStream
, Graphic
& rGraphic
);
327 static ErrCode
readPICT(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
328 static ErrCode
readMET(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
329 static ErrCode
readRAS(SvStream
& rStream
, Graphic
& rGraphic
);
330 static ErrCode
readPCX(SvStream
& rStream
, Graphic
& rGraphic
);
331 static ErrCode
readEPS(SvStream
& rStream
, Graphic
& rGraphic
);
332 static ErrCode
readPSD(SvStream
& rStream
, Graphic
& rGraphic
);
333 static ErrCode
readPCD(SvStream
& rStream
, Graphic
& rGraphic
);
334 static ErrCode
readPBM(SvStream
& rStream
, Graphic
& rGraphic
);
335 static ErrCode
readDXF(SvStream
& rStream
, Graphic
& rGraphic
);
336 static ErrCode
readWEBP(SvStream
& rStream
, Graphic
& rGraphic
, GfxLinkType
& rLinkType
);
339 OUString aFilterPath
;
340 FilterConfigCache
* pConfig
;
343 ErrCode
ImplSetError( ErrCode nError
, const SvStream
* pStm
= nullptr );
344 ErrCode
ImpTestOrFindFormat( std::u16string_view rPath
, SvStream
& rStream
, sal_uInt16
& rFormat
);
346 DECL_DLLPRIVATE_LINK( FilterCallback
, ConvertData
&, bool );
348 /** Information about errors during the GraphicFilter operation. */
349 std::optional
<ErrCode
> mxErrorEx
;
353 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */