Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / vcl / graphicfilter.hxx
blob8b8a7cef3008a13c9bbc16da397b0f3e99a4ce69
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 #ifndef INCLUDED_VCL_GRAPHICFILTER_HXX
21 #define INCLUDED_VCL_GRAPHICFILTER_HXX
23 #include <tools/gen.hxx>
24 #include <tools/urlobj.hxx>
25 #include <vcl/dllapi.h>
26 #include <vcl/field.hxx>
27 #include <vcl/graph.hxx>
28 #include <o3tl/typed_flags_set.hxx>
30 #include <com/sun/star/uno/Sequence.h>
31 #include <com/sun/star/beans/PropertyValue.hpp>
33 class FilterConfigCache;
34 class SvStream;
35 struct WMF_EXTERNALHEADER;
36 struct ConvertData;
38 #define GRFILTER_OK 0
39 #define GRFILTER_OPENERROR 1
40 #define GRFILTER_IOERROR 2
41 #define GRFILTER_FORMATERROR 3
42 #define GRFILTER_VERSIONERROR 4
43 #define GRFILTER_FILTERERROR 5
44 #define GRFILTER_ABORT 6
45 #define GRFILTER_TOOBIG 7
47 #define GRFILTER_OUTHINT_GREY 1
49 #define GRFILTER_FORMAT_NOTFOUND ((sal_uInt16)0xFFFF)
50 #define GRFILTER_FORMAT_DONTKNOW ((sal_uInt16)0xFFFF)
52 enum class GraphicFilterImportFlags
54 NONE = 0x000,
55 SetLogsizeForJpeg = 0x001,
56 DontSetLogsizeForJpeg = 0x002,
57 ForPreview = 0x004,
58 AllowPartialStreamRead = 0x010,
59 /// Only create a bitmap, do not read pixel data.
60 OnlyCreateBitmap = 0x020,
61 /// Read pixel data into an existing bitmap.
62 UseExistingBitmap = 0x040,
64 namespace o3tl
66 template<> struct typed_flags<GraphicFilterImportFlags> : is_typed_flags<GraphicFilterImportFlags, 0x0077> {};
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_SVSGF "SVSGF"
75 #define IMP_SVSGV "SVSGV"
76 #define IMP_GIF "SVIGIF"
77 #define IMP_PNG "SVIPNG"
78 #define IMP_JPEG "SVIJPEG"
79 #define IMP_XBM "SVIXBM"
80 #define IMP_XPM "SVIXPM"
81 #define IMP_SVG "SVISVG"
82 #define IMP_PDF "SVIPDF"
83 #define EXP_BMP "SVBMP"
84 #define EXP_SVMETAFILE "SVMETAFILE"
85 #define EXP_WMF "SVWMF"
86 #define EXP_EMF "SVEMF"
87 #define EXP_JPEG "SVEJPEG"
88 #define EXP_SVG "SVESVG"
89 #define EXP_PDF "SVEPDF"
90 #define EXP_PNG "SVEPNG"
92 #define BMP_SHORTNAME "BMP"
93 #define GIF_SHORTNAME "GIF"
94 #define JPG_SHORTNAME "JPG"
95 #define MET_SHORTNAME "MET"
96 #define PCT_SHORTNAME "PCT"
97 #define PNG_SHORTNAME "PNG"
98 #define SVM_SHORTNAME "SVM"
99 #define TIF_SHORTNAME "TIF"
100 #define WMF_SHORTNAME "WMF"
101 #define EMF_SHORTNAME "EMF"
102 #define SVG_SHORTNAME "SVG"
104 // Info class for all supported file formats
106 enum class GraphicFileFormat
108 NOT = 0x0000,
109 BMP = 0x0001,
110 GIF = 0x0002,
111 JPG = 0x0003,
112 PCD = 0x0004,
113 PCX = 0x0005,
114 PNG = 0x0006,
115 TIF = 0x0007,
116 XBM = 0x0008,
117 XPM = 0x0009,
118 PBM = 0x000a,
119 PGM = 0x000b,
120 PPM = 0x000c,
121 RAS = 0x000d,
122 TGA = 0x000e,
123 PSD = 0x000f,
124 EPS = 0x0010,
125 DXF = 0x00f1,
126 MET = 0x00f2,
127 PCT = 0x00f3,
128 SGF = 0x00f4,
129 SVM = 0x00f5,
130 WMF = 0x00f6,
131 SGV = 0x00f7,
132 EMF = 0x00f8,
133 SVG = 0x00f9
137 class VCL_DLLPUBLIC GraphicDescriptor final
139 SvStream* pFileStm;
141 OUString aPathExt;
142 Size aPixSize;
143 Size aLogSize;
144 sal_uInt16 nBitsPerPixel;
145 sal_uInt16 nPlanes;
146 GraphicFileFormat nFormat;
147 bool bCompressed;
148 bool bOwnStream;
150 void ImpConstruct();
152 bool ImpDetectBMP( SvStream& rStm, bool bExtendedInfo );
153 bool ImpDetectGIF( SvStream& rStm, bool bExtendedInfo );
154 bool ImpDetectJPG( SvStream& rStm, bool bExtendedInfo );
155 bool ImpDetectPCD( SvStream& rStm, bool bExtendedInfo );
156 bool ImpDetectPCX( SvStream& rStm, bool bExtendedInfo );
157 bool ImpDetectPNG( SvStream& rStm, bool bExtendedInfo );
158 bool ImpDetectTIF( SvStream& rStm, bool bExtendedInfo );
159 bool ImpDetectXBM( SvStream& rStm, bool bExtendedInfo );
160 bool ImpDetectXPM( SvStream& rStm, bool bExtendedInfo );
161 bool ImpDetectPBM( SvStream& rStm, bool bExtendedInfo );
162 bool ImpDetectPGM( SvStream& rStm, bool bExtendedInfo );
163 bool ImpDetectPPM( SvStream& rStm, bool bExtendedInfo );
164 bool ImpDetectRAS( SvStream& rStm, bool bExtendedInfo );
165 bool ImpDetectTGA( SvStream& rStm, bool bExtendedInfo );
166 bool ImpDetectPSD( SvStream& rStm, bool bExtendedInfo );
167 bool ImpDetectEPS( SvStream& rStm, bool bExtendedInfo );
168 bool ImpDetectDXF( SvStream& rStm, bool bExtendedInfo );
169 bool ImpDetectMET( SvStream& rStm, bool bExtendedInfo );
170 bool ImpDetectPCT( SvStream& rStm, bool bExtendedInfo );
171 bool ImpDetectSGF( SvStream& rStm, bool bExtendedInfo );
172 bool ImpDetectSVM( SvStream& rStm, bool bExtendedInfo );
173 bool ImpDetectWMF( SvStream& rStm, bool bExtendedInfo );
174 bool ImpDetectSGV( SvStream& rStm, bool bExtendedInfo );
175 bool ImpDetectEMF( SvStream& rStm, bool bExtendedInfo );
176 bool ImpDetectSVG( SvStream& rStm, bool bExtendedInfo );
177 GraphicDescriptor( const GraphicDescriptor& ) = delete;
178 GraphicDescriptor& operator=( const GraphicDescriptor& ) = delete;
180 public:
182 /** Ctor to set a filename
184 Detect() must be called to identify the file
185 If the file has no unique header (Mtf's), the format
186 is determined from the extension */
187 GraphicDescriptor( const INetURLObject& rPath );
189 /** Ctor using a stream
191 Detect() must be called to identify the file
192 As some formats (Mtf's) do not have a unique header, it makes sense
193 to supply the file name (incl. ext.), so that the format can be
194 derived from the extension */
195 GraphicDescriptor( SvStream& rInStream, const OUString* pPath );
197 ~GraphicDescriptor();
199 /** starts the detection
201 if bExtendedInfo == true the file header is used to derive
202 as many properties as possible (size, color, etc.) */
203 bool Detect( bool bExtendedInfo = false );
205 /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
206 GraphicFileFormat GetFileFormat() const { return nFormat; }
208 /** @return graphic size in pixels or 0 size */
209 const Size& GetSizePixel() const { return (Size&) aPixSize; }
211 /** @return the logical graphic size in 1/100mm or 0 size */
212 const Size& GetSize_100TH_MM() const { return (Size&) aLogSize; }
214 /** @return bits/pixel or 0 **/
215 sal_uInt16 GetBitsPerPixel() const { return nBitsPerPixel; }
217 /** @return filter number that is needed by the GraphFilter to read this format */
218 static OUString GetImportFormatShortName( GraphicFileFormat nFormat );
221 /** Information about errors during the GraphicFilter operation. */
222 struct FilterErrorEx
224 sal_uLong nFilterError;
225 sal_uLong nStreamError;
227 FilterErrorEx() : nFilterError( 0UL ), nStreamError( 0UL ) {}
230 /** Class to import and export graphic formats. */
231 class VCL_DLLPUBLIC GraphicFilter
233 public:
234 GraphicFilter( bool bUseConfig = true );
235 ~GraphicFilter();
237 sal_uInt16 GetImportFormatCount();
238 sal_uInt16 GetImportFormatNumber( const OUString& rFormatName );
239 sal_uInt16 GetImportFormatNumberForShortName( const OUString& rShortName );
240 sal_uInt16 GetImportFormatNumberForTypeName( const OUString& rType );
241 OUString GetImportFormatName( sal_uInt16 nFormat );
242 OUString GetImportFormatTypeName( sal_uInt16 nFormat );
243 #ifdef _WIN32
244 OUString GetImportFormatMediaType( sal_uInt16 nFormat );
245 #endif
246 OUString GetImportFormatShortName( sal_uInt16 nFormat );
247 OUString GetImportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry );
249 sal_uInt16 GetExportFormatCount();
250 sal_uInt16 GetExportFormatNumber( const OUString& rFormatName );
251 sal_uInt16 GetExportFormatNumberForMediaType( const OUString& rShortName );
252 sal_uInt16 GetExportFormatNumberForShortName( const OUString& rShortName );
253 OUString GetExportInternalFilterName( sal_uInt16 nFormat );
254 sal_uInt16 GetExportFormatNumberForTypeName( const OUString& rType );
255 OUString GetExportFormatName( sal_uInt16 nFormat );
256 OUString GetExportFormatMediaType( sal_uInt16 nFormat );
257 OUString GetExportFormatShortName( sal_uInt16 nFormat );
258 OUString GetExportWildcard( sal_uInt16 nFormat );
259 bool IsExportPixelFormat( sal_uInt16 nFormat );
261 sal_uInt16 ExportGraphic( const Graphic& rGraphic, const INetURLObject& rPath,
262 sal_uInt16 nFormat,
263 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = nullptr );
264 sal_uInt16 ExportGraphic( const Graphic& rGraphic, const OUString& rPath,
265 SvStream& rOStm, sal_uInt16 nFormat,
266 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = nullptr );
268 sal_uInt16 CanImportGraphic( const INetURLObject& rPath,
269 sal_uInt16 nFormat,
270 sal_uInt16 * pDeterminedFormat);
272 sal_uInt16 ImportGraphic( Graphic& rGraphic, const INetURLObject& rPath,
273 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
274 sal_uInt16 * pDeterminedFormat = nullptr, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE );
276 sal_uInt16 CanImportGraphic( const OUString& rPath, SvStream& rStream,
277 sal_uInt16 nFormat,
278 sal_uInt16 * pDeterminedFormat);
280 sal_uInt16 ImportGraphic( Graphic& rGraphic, const OUString& rPath,
281 SvStream& rStream,
282 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
283 sal_uInt16 * pDeterminedFormat = nullptr, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE,
284 WMF_EXTERNALHEADER *pExtHeader = nullptr );
286 sal_uInt16 ImportGraphic( Graphic& rGraphic, const OUString& rPath,
287 SvStream& rStream,
288 sal_uInt16 nFormat,
289 sal_uInt16 * pDeterminedFormat, GraphicFilterImportFlags nImportFlags,
290 css::uno::Sequence< css::beans::PropertyValue >* pFilterData,
291 WMF_EXTERNALHEADER *pExtHeader = nullptr );
293 const FilterErrorEx& GetLastError() const { return *pErrorEx;}
294 void ResetLastError();
296 const Link<ConvertData&,bool> GetFilterCallback() const;
297 static GraphicFilter& GetGraphicFilter();
298 static int LoadGraphic( const OUString& rPath, const OUString& rFilter,
299 Graphic& rGraphic,
300 GraphicFilter* pFilter = nullptr,
301 sal_uInt16* pDeterminedFormat = nullptr );
303 sal_uInt16 compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream);
305 protected:
306 OUString aFilterPath;
307 FilterConfigCache* pConfig;
309 private:
310 void ImplInit();
311 sal_uLong ImplSetError( sal_uLong nError, const SvStream* pStm = nullptr );
312 sal_uInt16 ImpTestOrFindFormat( const OUString& rPath, SvStream& rStream, sal_uInt16& rFormat );
314 DECL_LINK( FilterCallback, ConvertData&, bool );
316 FilterErrorEx* pErrorEx;
317 bool bUseConfig;
318 long nExpGraphHint;
321 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */