LibreOffice Module vcl (master)  1
graphicfilter.hxx
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 #ifndef INCLUDED_VCL_GRAPHICFILTER_HXX
21 #define INCLUDED_VCL_GRAPHICFILTER_HXX
22 
23 #include <tools/gen.hxx>
24 #include <vcl/dllapi.h>
25 #include <vcl/graph.hxx>
26 #include <vcl/errcode.hxx>
27 #include <o3tl/typed_flags_set.hxx>
28 
29 #include <memory>
30 #include <optional>
31 
32 namespace com::sun::star::beans { struct PropertyValue; }
33 namespace com::sun::star::uno { template <class E> class Sequence; }
34 
35 class INetURLObject;
36 
37 class FilterConfigCache;
38 class SvStream;
39 struct WmfExternal;
40 struct ConvertData;
41 
42 #define ERRCODE_GRFILTER_OPENERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 1)
43 #define ERRCODE_GRFILTER_IOERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 2)
44 #define ERRCODE_GRFILTER_FORMATERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 3)
45 #define ERRCODE_GRFILTER_VERSIONERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 4)
46 #define ERRCODE_GRFILTER_FILTERERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 5)
47 #define ERRCODE_GRFILTER_TOOBIG ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 7)
48 
49 #define GRFILTER_OUTHINT_GREY 1
50 
51 #define GRFILTER_FORMAT_NOTFOUND (sal_uInt16(0xFFFF))
52 #define GRFILTER_FORMAT_DONTKNOW (sal_uInt16(0xFFFF))
53 
55 {
56  NONE = 0x000,
57  SetLogsizeForJpeg = 0x001,
58  DontSetLogsizeForJpeg = 0x002,
60  OnlyCreateBitmap = 0x020,
62  UseExistingBitmap = 0x040,
63 };
64 namespace o3tl
65 {
66  template<> struct typed_flags<GraphicFilterImportFlags> : is_typed_flags<GraphicFilterImportFlags, 0x0063> {};
67 }
68 
69 #define IMP_BMP "SVBMP"
70 #define IMP_MOV "SVMOV"
71 #define IMP_SVMETAFILE "SVMETAFILE"
72 #define IMP_WMF "SVWMF"
73 #define IMP_EMF "SVEMF"
74 #define IMP_GIF "SVIGIF"
75 #define IMP_PNG "SVIPNG"
76 #define IMP_JPEG "SVIJPEG"
77 #define IMP_XBM "SVIXBM"
78 #define IMP_XPM "SVIXPM"
79 #define IMP_SVG "SVISVG"
80 #define IMP_PDF "SVIPDF"
81 #define IMP_TIFF "SVTIFF"
82 #define IMP_TGA "SVTGA"
83 #define IMP_PICT "SVPICT"
84 #define IMP_MET "SVMET"
85 #define IMP_RAS "SVRAS"
86 #define IMP_PCX "SVPCX"
87 #define IMP_EPS "SVIEPS"
88 #define IMP_PSD "SVPSD"
89 #define IMP_PCD "SVPCD"
90 #define IMP_PBM "SVPBM"
91 #define IMP_DXF "SVDXF"
92 #define IMP_WEBP "SVIWEBP"
93 
94 #define EXP_BMP "SVBMP"
95 #define EXP_SVMETAFILE "SVMETAFILE"
96 #define EXP_WMF "SVWMF"
97 #define EXP_EMF "SVEMF"
98 #define EXP_JPEG "SVEJPEG"
99 #define EXP_SVG "SVESVG"
100 #define EXP_PDF "SVEPDF"
101 #define EXP_PNG "SVEPNG"
102 #define EXP_TIFF "SVTIFF"
103 #define EXP_EPS "SVEEPS"
104 #define EXP_GIF "SVEGIF"
105 #define EXP_WEBP "SVEWEBP"
106 
107 
108 inline constexpr OUStringLiteral BMP_SHORTNAME = u"BMP";
109 inline constexpr OUStringLiteral GIF_SHORTNAME = u"GIF";
110 inline constexpr OUStringLiteral JPG_SHORTNAME = u"JPG";
111 inline constexpr OUStringLiteral MET_SHORTNAME = u"MET";
112 inline constexpr OUStringLiteral PCT_SHORTNAME = u"PCT";
113 inline constexpr OUStringLiteral PNG_SHORTNAME = u"PNG";
114 inline constexpr OUStringLiteral SVM_SHORTNAME = u"SVM";
115 inline constexpr OUStringLiteral TIF_SHORTNAME = u"TIF";
116 inline constexpr OUStringLiteral WMF_SHORTNAME = u"WMF";
117 inline constexpr OUStringLiteral EMF_SHORTNAME = u"EMF";
118 inline constexpr OUStringLiteral SVG_SHORTNAME = u"SVG";
119 inline constexpr OUStringLiteral PDF_SHORTNAME = u"PDF";
120 inline constexpr OUStringLiteral WEBP_SHORTNAME = u"WEBP";
121 
122 // Info class for all supported file formats
123 
125 {
126  NOT = 0x0000,
127  BMP = 0x0001,
128  GIF = 0x0002,
129  JPG = 0x0003,
130  PCD = 0x0004,
131  PCX = 0x0005,
132  PNG = 0x0006,
133  TIF = 0x0007,
134  XBM = 0x0008,
135  XPM = 0x0009,
136  PBM = 0x000a,
137  PGM = 0x000b,
138  PPM = 0x000c,
139  RAS = 0x000d,
140  TGA = 0x000e,
141  PSD = 0x000f,
142  EPS = 0x0010,
143  WEBP = 0x0011,
144  DXF = 0x00f1,
145  MET = 0x00f2,
146  PCT = 0x00f3,
147  // retired SGF = 0x00f4,
148  SVM = 0x00f5,
149  WMF = 0x00f6,
150  // retired SGV = 0x00f7,
151  EMF = 0x00f8,
152  SVG = 0x00f9
153 };
154 
155 
157 {
159 
160  OUString aPathExt;
163  std::optional<Size> maPreferredLogSize;
164  std::optional<MapMode> maPreferredMapMode;
165  sal_uInt16 nBitsPerPixel;
166  sal_uInt16 nPlanes;
171  bool bIsAlpha;
172 
173  void ImpConstruct();
174 
175  bool ImpDetectBMP( SvStream& rStm, bool bExtendedInfo );
176  bool ImpDetectGIF( SvStream& rStm, bool bExtendedInfo );
177  bool ImpDetectJPG( SvStream& rStm, bool bExtendedInfo );
178  bool ImpDetectPCD( SvStream& rStm, bool bExtendedInfo );
179  bool ImpDetectPCX( SvStream& rStm );
180  bool ImpDetectPNG( SvStream& rStm, bool bExtendedInfo );
181  bool ImpDetectTIF( SvStream& rStm, bool bExtendedInfo );
182  bool ImpDetectXBM( SvStream& rStm, bool bExtendedInfo );
183  bool ImpDetectXPM( SvStream& rStm, bool bExtendedInfo );
184  bool ImpDetectPBM( SvStream& rStm, bool bExtendedInfo );
185  bool ImpDetectPGM( SvStream& rStm, bool bExtendedInfo );
186  bool ImpDetectPPM( SvStream& rStm, bool bExtendedInfo );
187  bool ImpDetectRAS( SvStream& rStm, bool bExtendedInfo );
188  bool ImpDetectTGA( SvStream& rStm, bool bExtendedInfo );
189  bool ImpDetectPSD( SvStream& rStm, bool bExtendedInfo );
190  bool ImpDetectEPS( SvStream& rStm, bool bExtendedInfo );
191  bool ImpDetectWEBP( SvStream& rStm, bool bExtendedInfo );
192  bool ImpDetectDXF( SvStream& rStm, bool bExtendedInfo );
193  bool ImpDetectMET( SvStream& rStm, bool bExtendedInfo );
194  bool ImpDetectPCT( SvStream& rStm, bool bExtendedInfo );
195  bool ImpDetectSVM( SvStream& rStm, bool bExtendedInfo );
196  bool ImpDetectWMF( SvStream& rStm, bool bExtendedInfo );
197  bool ImpDetectEMF( SvStream& rStm, bool bExtendedInfo );
198  bool ImpDetectSVG( SvStream& rStm, bool bExtendedInfo );
199  GraphicDescriptor( const GraphicDescriptor& ) = delete;
200  GraphicDescriptor& operator=( const GraphicDescriptor& ) = delete;
201 
202 public:
203 
209  GraphicDescriptor( const INetURLObject& rPath );
210 
217  GraphicDescriptor( SvStream& rInStream, const OUString* pPath );
218 
220 
225  bool Detect( bool bExtendedInfo = false );
226 
228  GraphicFileFormat GetFileFormat() const { return nFormat; }
229 
231  const Size& GetSizePixel() const { return aPixSize; }
232 
234  const Size& GetSize_100TH_MM() const { return aLogSize; }
235 
240  const std::optional<Size>& GetPreferredLogSize() const { return maPreferredLogSize; }
241 
246  const std::optional<MapMode>& GetPreferredMapMode() const { return maPreferredMapMode; }
247 
249  sal_uInt16 GetBitsPerPixel() const { return nBitsPerPixel; }
250 
252  sal_uInt8 GetNumberOfImageComponents() const { return mnNumberOfImageComponents; }
253 
255  bool IsTransparent() const { return bIsTransparent; }
256 
258  bool IsAlpha() const { return bIsAlpha; }
259 
261  static OUString GetImportFormatShortName( GraphicFileFormat nFormat );
262 };
263 
266 {
267 public:
268  GraphicFilter( bool bUseConfig = true );
269  ~GraphicFilter();
270 
271  sal_uInt16 GetImportFormatCount() const;
272  sal_uInt16 GetImportFormatNumber( std::u16string_view rFormatName );
273  sal_uInt16 GetImportFormatNumberForShortName( std::u16string_view rShortName );
274  sal_uInt16 GetImportFormatNumberForTypeName( std::u16string_view rType );
275  OUString GetImportFormatName( sal_uInt16 nFormat );
276  OUString GetImportFormatTypeName( sal_uInt16 nFormat );
277 #ifdef _WIN32
278  OUString GetImportFormatMediaType( sal_uInt16 nFormat );
279 #endif
280  OUString GetImportFormatShortName( sal_uInt16 nFormat );
281  OUString GetImportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry );
282 
283  sal_uInt16 GetExportFormatCount() const;
284  sal_uInt16 GetExportFormatNumber( std::u16string_view rFormatName );
285  sal_uInt16 GetExportFormatNumberForMediaType( std::u16string_view rShortName );
286  sal_uInt16 GetExportFormatNumberForShortName( std::u16string_view rShortName );
287  OUString GetExportInternalFilterName( sal_uInt16 nFormat );
288  sal_uInt16 GetExportFormatNumberForTypeName( std::u16string_view rType );
289  OUString GetExportFormatName( sal_uInt16 nFormat );
290  OUString GetExportFormatMediaType( sal_uInt16 nFormat );
291  OUString GetExportFormatShortName( sal_uInt16 nFormat );
292  OUString GetExportWildcard( sal_uInt16 nFormat );
293  bool IsExportPixelFormat( sal_uInt16 nFormat );
294 
295  ErrCode ExportGraphic( const Graphic& rGraphic, const INetURLObject& rPath,
296  sal_uInt16 nFormat,
297  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = nullptr );
298  ErrCode ExportGraphic( const Graphic& rGraphic, std::u16string_view rPath,
299  SvStream& rOStm, sal_uInt16 nFormat,
300  const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = nullptr );
301 
302  ErrCode CanImportGraphic( const INetURLObject& rPath,
303  sal_uInt16 nFormat,
304  sal_uInt16 * pDeterminedFormat);
305 
306  ErrCode ImportGraphic( Graphic& rGraphic, const INetURLObject& rPath,
307  sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
308  sal_uInt16 * pDeterminedFormat = nullptr, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE );
309 
310  ErrCode CanImportGraphic( std::u16string_view rPath, SvStream& rStream,
311  sal_uInt16 nFormat,
312  sal_uInt16 * pDeterminedFormat);
313 
314  ErrCode ImportGraphic( Graphic& rGraphic, std::u16string_view rPath,
315  SvStream& rStream,
316  sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
317  sal_uInt16 * pDeterminedFormat = nullptr, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE );
318 
322  void ImportGraphics(std::vector< std::shared_ptr<Graphic> >& rGraphics, std::vector< std::unique_ptr<SvStream> > vStreams);
323 
328  void MakeGraphicsAvailableThreaded(std::vector< Graphic* >& rGraphics);
329 
330  // Setting sizeLimit limits how much will be read from the stream.
331  Graphic ImportUnloadedGraphic(SvStream& rIStream, sal_uInt64 sizeLimit = 0, const Size* pSizeHint = nullptr);
332 
333  const ErrCode& GetLastError() const { return *mxErrorEx;}
334  void ResetLastError();
335 
336  Link<ConvertData&,bool> GetFilterCallback() const;
337  static GraphicFilter& GetGraphicFilter();
338  static ErrCode LoadGraphic( const OUString& rPath, const OUString& rFilter,
339  Graphic& rGraphic,
340  GraphicFilter* pFilter = nullptr,
341  sal_uInt16* pDeterminedFormat = nullptr );
342 
343  ErrCode compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream);
344 
345  static ErrCode readGIF(SvStream& rStream, Graphic& rGraphic, GfxLinkType& rLinkType);
346  static ErrCode readPNG(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType,
347  std::unique_ptr<sal_uInt8[]> & rpGraphicContent, sal_Int32& rGraphicContentSize);
348  static ErrCode readJPEG(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType,
349  GraphicFilterImportFlags nImportFlags);
350  static ErrCode readSVG(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType,
351  std::unique_ptr<sal_uInt8[]> & rpGraphicContent, sal_Int32& rGraphicContentSize);
352  static ErrCode readXBM(SvStream & rStream, Graphic & rGraphic);
353  static ErrCode readXPM(SvStream & rStream, Graphic & rGraphic);
354 
355  static ErrCode readWMF_EMF(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType, VectorGraphicDataType eType);
356  static ErrCode readWMF(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
357  static ErrCode readEMF(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
358 
359  static ErrCode readPDF(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
360  static ErrCode readTIFF(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
361  static ErrCode readWithTypeSerializer(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType, std::u16string_view aFilterName);
362  static ErrCode readBMP(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
363  static ErrCode readTGA(SvStream & rStream, Graphic & rGraphic);
364  static ErrCode readPICT(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
365  static ErrCode readMET(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
366  static ErrCode readRAS(SvStream & rStream, Graphic & rGraphic);
367  static ErrCode readPCX(SvStream & rStream, Graphic & rGraphic);
368  static ErrCode readEPS(SvStream & rStream, Graphic & rGraphic);
369  static ErrCode readPSD(SvStream & rStream, Graphic & rGraphic);
370  static ErrCode readPCD(SvStream & rStream, Graphic & rGraphic);
371  static ErrCode readPBM(SvStream & rStream, Graphic & rGraphic);
372  static ErrCode readDXF(SvStream & rStream, Graphic & rGraphic);
373  static ErrCode readWEBP(SvStream & rStream, Graphic & rGraphic, GfxLinkType & rLinkType);
374 
375 private:
376  OUString aFilterPath;
378 
379  void ImplInit();
380  ErrCode ImplSetError( ErrCode nError, const SvStream* pStm = nullptr );
381  ErrCode ImpTestOrFindFormat( std::u16string_view rPath, SvStream& rStream, sal_uInt16& rFormat );
382 
383  DECL_DLLPRIVATE_LINK( FilterCallback, ConvertData&, bool );
384 
386  std::optional<ErrCode> mxErrorEx;
388 };
389 
390 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
391 
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
VectorGraphicDataType
Only create a bitmap, do not read pixel data.
Read pixel data into an existing bitmap.
constexpr OUStringLiteral MET_SHORTNAME
constexpr OUStringLiteral PCT_SHORTNAME
constexpr OUStringLiteral SVG_SHORTNAME
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
sal_uInt16 GetBitsPerPixel() const
const ErrCode & GetLastError() const
OUString aFilterPath
Class to import and export graphic formats.
std::optional< Size > maPreferredLogSize
bool IsAlpha() const
sal_uInt8 mnNumberOfImageComponents
const Size & GetSize_100TH_MM() const
constexpr OUStringLiteral WEBP_SHORTNAME
Cache to keep list of graphic filters + the filters themselves.
const Size & GetSizePixel() const
sal_uInt8 GetNumberOfImageComponents() const
constexpr OUStringLiteral SVM_SHORTNAME
FilterConfigCache * pConfig
const std::optional< MapMode > & GetPreferredMapMode() const
If available, this returns the map mode the graphic prefers, which may be other than pixel or 100th m...
const std::optional< Size > & GetPreferredLogSize() const
Returns the logic size, according to the map mode available via GetPreferredMapMode().
float u
#define GRFILTER_FORMAT_DONTKNOW
constexpr OUStringLiteral PDF_SHORTNAME
GraphicFileFormat
WMF
std::optional< ErrCode > mxErrorEx
Information about errors during the GraphicFilter operation.
unsigned char sal_uInt8
GraphicFileFormat nFormat
bool IsTransparent() const
constexpr OUStringLiteral JPG_SHORTNAME
constexpr OUStringLiteral EMF_SHORTNAME
constexpr OUStringLiteral GIF_SHORTNAME
PNG
GraphicFileFormat GetFileFormat() const
constexpr OUStringLiteral PNG_SHORTNAME
std::optional< MapMode > maPreferredMapMode
GraphicFilterImportFlags
EMF
sal_uInt16 nBitsPerPixel
constexpr OUStringLiteral TIF_SHORTNAME
constexpr OUStringLiteral BMP_SHORTNAME
constexpr OUStringLiteral WMF_SHORTNAME