Branch libreoffice-5-0-4
[LibreOffice.git] / include / vcl / graphicfilter.hxx
blob5d34d0fade3ffe0136b0ff0f5e68f6f36a98bbe2
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/stream.hxx>
25 #include <tools/urlobj.hxx>
26 #include <vcl/dllapi.h>
27 #include <vcl/field.hxx>
28 #include <vcl/graph.hxx>
29 #include <o3tl/typed_flags_set.hxx>
31 #include <com/sun/star/uno/Sequence.h>
32 #include <com/sun/star/beans/PropertyValue.hpp>
34 class FilterConfigCache;
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,
60 namespace o3tl
62 template<> struct typed_flags<GraphicFilterImportFlags> : is_typed_flags<GraphicFilterImportFlags, 0x0017> {};
65 #define IMP_BMP "SVBMP"
66 #define IMP_MOV "SVMOV"
67 #define IMP_SVMETAFILE "SVMETAFILE"
68 #define IMP_WMF "SVWMF"
69 #define IMP_EMF "SVEMF"
70 #define IMP_SVSGF "SVSGF"
71 #define IMP_SVSGV "SVSGV"
72 #define IMP_GIF "SVIGIF"
73 #define IMP_PNG "SVIPNG"
74 #define IMP_JPEG "SVIJPEG"
75 #define IMP_XBM "SVIXBM"
76 #define IMP_XPM "SVIXPM"
77 #define IMP_SVG "SVISVG"
78 #define EXP_BMP "SVBMP"
79 #define EXP_SVMETAFILE "SVMETAFILE"
80 #define EXP_WMF "SVWMF"
81 #define EXP_EMF "SVEMF"
82 #define EXP_JPEG "SVEJPEG"
83 #define EXP_SVG "SVESVG"
84 #define EXP_PNG "SVEPNG"
86 #define BMP_SHORTNAME "BMP"
87 #define GIF_SHORTNAME "GIF"
88 #define JPG_SHORTNAME "JPG"
89 #define MET_SHORTNAME "MET"
90 #define PCT_SHORTNAME "PCT"
91 #define PNG_SHORTNAME "PNG"
92 #define SVM_SHORTNAME "SVM"
93 #define TIF_SHORTNAME "TIF"
94 #define WMF_SHORTNAME "WMF"
95 #define EMF_SHORTNAME "EMF"
96 #define SVG_SHORTNAME "SVG"
98 // Info class for all supported file formats
100 enum class GraphicFileFormat
102 NOT = 0x0000,
103 BMP = 0x0001,
104 GIF = 0x0002,
105 JPG = 0x0003,
106 PCD = 0x0004,
107 PCX = 0x0005,
108 PNG = 0x0006,
109 TIF = 0x0007,
110 XBM = 0x0008,
111 XPM = 0x0009,
112 PBM = 0x000a,
113 PGM = 0x000b,
114 PPM = 0x000c,
115 RAS = 0x000d,
116 TGA = 0x000e,
117 PSD = 0x000f,
118 EPS = 0x0010,
119 DXF = 0x00f1,
120 MET = 0x00f2,
121 PCT = 0x00f3,
122 SGF = 0x00f4,
123 SVM = 0x00f5,
124 WMF = 0x00f6,
125 SGV = 0x00f7,
126 EMF = 0x00f8,
127 SVG = 0x00f9,
128 MOV = 0x00fa,
129 XXX = 0xffff
132 // - GraphicDescriptor -
135 class VCL_DLLPUBLIC GraphicDescriptor
137 SvStream* pFileStm;
139 OUString aPathExt;
140 Size aPixSize;
141 Size aLogSize;
142 sal_uInt16 nBitsPerPixel;
143 sal_uInt16 nPlanes;
144 GraphicFileFormat nFormat;
145 bool bCompressed;
146 bool bOwnStream;
148 void ImpConstruct();
150 bool ImpDetectBMP( SvStream& rStm, bool bExtendedInfo );
151 bool ImpDetectGIF( SvStream& rStm, bool bExtendedInfo );
152 bool ImpDetectJPG( SvStream& rStm, bool bExtendedInfo );
153 bool ImpDetectPCD( SvStream& rStm, bool bExtendedInfo );
154 bool ImpDetectPCX( SvStream& rStm, bool bExtendedInfo );
155 bool ImpDetectPNG( SvStream& rStm, bool bExtendedInfo );
156 bool ImpDetectTIF( SvStream& rStm, bool bExtendedInfo );
157 bool ImpDetectXBM( SvStream& rStm, bool bExtendedInfo );
158 bool ImpDetectXPM( SvStream& rStm, bool bExtendedInfo );
159 bool ImpDetectPBM( SvStream& rStm, bool bExtendedInfo );
160 bool ImpDetectPGM( SvStream& rStm, bool bExtendedInfo );
161 bool ImpDetectPPM( SvStream& rStm, bool bExtendedInfo );
162 bool ImpDetectRAS( SvStream& rStm, bool bExtendedInfo );
163 bool ImpDetectTGA( SvStream& rStm, bool bExtendedInfo );
164 bool ImpDetectPSD( SvStream& rStm, bool bExtendedInfo );
165 bool ImpDetectEPS( SvStream& rStm, bool bExtendedInfo );
166 bool ImpDetectDXF( SvStream& rStm, bool bExtendedInfo );
167 bool ImpDetectMET( SvStream& rStm, bool bExtendedInfo );
168 bool ImpDetectPCT( SvStream& rStm, bool bExtendedInfo );
169 bool ImpDetectSGF( SvStream& rStm, bool bExtendedInfo );
170 bool ImpDetectSVM( SvStream& rStm, bool bExtendedInfo );
171 bool ImpDetectWMF( SvStream& rStm, bool bExtendedInfo );
172 bool ImpDetectSGV( SvStream& rStm, bool bExtendedInfo );
173 bool ImpDetectEMF( SvStream& rStm, bool bExtendedInfo );
174 bool ImpDetectSVG( SvStream& rStm, bool bExtendedInfo );
175 bool ImpDetectMOV( SvStream& rStm, bool bExtendedInfo );
176 GraphicDescriptor( const GraphicDescriptor& ) SAL_DELETED_FUNCTION;
177 GraphicDescriptor& operator=( const GraphicDescriptor& ) SAL_DELETED_FUNCTION;
179 public:
181 /** Ctor to set a filename
183 Detect() must be called to identify the file
184 If the file has no unique header (Mtf's), the format
185 is determined from the extension */
186 GraphicDescriptor( const INetURLObject& rPath );
188 /** Ctor using a stream
190 Detect() must be called to identify the file
191 As some formats (Mtf's) do not have a unique header, it makes sense
192 to supply the file name (incl. ext.), so that the format can be
193 derived from the extension */
194 GraphicDescriptor( SvStream& rInStream, const OUString* pPath = NULL );
196 virtual ~GraphicDescriptor();
198 /** starts the detection
200 if bExtendedInfo == true the file header is used to derive
201 as many properties as possible (size, color, etc.) */
202 bool Detect( bool bExtendedInfo = false );
204 /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
205 GraphicFileFormat GetFileFormat() const { return nFormat; }
207 /** @return graphic size in pixels or 0 size */
208 const Size& GetSizePixel() const { return (Size&) aPixSize; }
210 /** @return the logical graphic size in 1/100mm or 0 size */
211 const Size& GetSize_100TH_MM() const { return (Size&) aLogSize; }
213 /** @return bits/pixel or 0 **/
214 sal_uInt16 GetBitsPerPixel() const { return nBitsPerPixel; }
216 /** return number of planes or 0 */
217 sal_uInt16 GetPlanes() const { return nPlanes; }
219 /** @return true if the graphic is compressed */
220 bool IsCompressed() const { return bCompressed; }
222 /** @return filter number that is needed by the GraphFilter to read this format */
223 static OUString GetImportFormatShortName( GraphicFileFormat nFormat );
226 /** Information about errors during the GraphicFilter operation. */
227 struct FilterErrorEx
229 sal_uLong nFilterError;
230 sal_uLong nStreamError;
232 FilterErrorEx() : nFilterError( 0UL ), nStreamError( 0UL ) {}
235 /** Class to import and export graphic formats. */
236 class VCL_DLLPUBLIC GraphicFilter
238 private:
240 void ImplInit();
241 sal_uLong ImplSetError( sal_uLong nError, const SvStream* pStm = NULL );
242 sal_uInt16 ImpTestOrFindFormat( const OUString& rPath, SvStream& rStream, sal_uInt16& rFormat );
244 DECL_LINK( FilterCallback, ConvertData* pData );
246 protected:
248 OUString aFilterPath;
249 FilterConfigCache* pConfig;
250 FilterErrorEx* pErrorEx;
251 bool bAbort;
252 bool bUseConfig;
253 long nExpGraphHint;
255 public:
257 GraphicFilter( bool bUseConfig = true );
258 ~GraphicFilter();
260 void SetFilterPath( const OUString& rFilterPath ) { aFilterPath = rFilterPath; };
262 sal_uInt16 GetImportFormatCount();
263 sal_uInt16 GetImportFormatNumber( const OUString& rFormatName );
264 sal_uInt16 GetImportFormatNumberForMediaType( const OUString& rMediaType );
265 sal_uInt16 GetImportFormatNumberForShortName( const OUString& rShortName );
266 sal_uInt16 GetImportFormatNumberForTypeName( const OUString& rType );
267 OUString GetImportFormatName( sal_uInt16 nFormat );
268 OUString GetImportFormatTypeName( sal_uInt16 nFormat );
269 OUString GetImportFormatMediaType( sal_uInt16 nFormat );
270 OUString GetImportFormatShortName( sal_uInt16 nFormat );
271 static OUString GetImportOSFileType( sal_uInt16 nFormat );
272 OUString GetImportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry = 0 );
273 bool IsImportPixelFormat( sal_uInt16 nFormat );
275 sal_uInt16 GetExportFormatCount();
276 sal_uInt16 GetExportFormatNumber( const OUString& rFormatName );
277 sal_uInt16 GetExportFormatNumberForMediaType( const OUString& rShortName );
278 sal_uInt16 GetExportFormatNumberForShortName( const OUString& rShortName );
279 OUString GetExportInternalFilterName( sal_uInt16 nFormat );
280 sal_uInt16 GetExportFormatNumberForTypeName( const OUString& rType );
281 OUString GetExportFormatName( sal_uInt16 nFormat );
282 OUString GetExportFormatTypeName( sal_uInt16 nFormat );
283 OUString GetExportFormatMediaType( sal_uInt16 nFormat );
284 OUString GetExportFormatShortName( sal_uInt16 nFormat );
285 static OUString GetExportOSFileType( sal_uInt16 nFormat );
286 OUString GetExportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry = 0 );
287 bool IsExportPixelFormat( sal_uInt16 nFormat );
289 sal_uInt16 ExportGraphic( const Graphic& rGraphic, const INetURLObject& rPath,
290 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
291 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = NULL );
292 sal_uInt16 ExportGraphic( const Graphic& rGraphic, const OUString& rPath,
293 SvStream& rOStm, sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
294 const css::uno::Sequence< css::beans::PropertyValue >* pFilterData = NULL );
295 long GetExportGraphicHint() const { return nExpGraphHint; }
297 sal_uInt16 CanImportGraphic( const INetURLObject& rPath,
298 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
299 sal_uInt16 * pDeterminedFormat = NULL);
301 sal_uInt16 ImportGraphic( Graphic& rGraphic, const INetURLObject& rPath,
302 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
303 sal_uInt16 * pDeterminedFormat = NULL, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE );
305 sal_uInt16 CanImportGraphic( const OUString& rPath, SvStream& rStream,
306 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
307 sal_uInt16 * pDeterminedFormat = NULL);
309 sal_uInt16 ImportGraphic( Graphic& rGraphic, const OUString& rPath,
310 SvStream& rStream,
311 sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
312 sal_uInt16 * pDeterminedFormat = NULL, GraphicFilterImportFlags nImportFlags = GraphicFilterImportFlags::NONE,
313 WMF_EXTERNALHEADER *pExtHeader = NULL );
315 sal_uInt16 ImportGraphic( Graphic& rGraphic, const OUString& rPath,
316 SvStream& rStream,
317 sal_uInt16 nFormat,
318 sal_uInt16 * pDeterminedFormat, GraphicFilterImportFlags nImportFlags,
319 com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >* pFilterData,
320 WMF_EXTERNALHEADER *pExtHeader = NULL );
322 void Abort() { bAbort = true; }
324 const FilterErrorEx& GetLastError() const { return *pErrorEx;}
325 void ResetLastError();
327 const Link<> GetFilterCallback() const;
328 static GraphicFilter& GetGraphicFilter();
329 static int LoadGraphic( const OUString& rPath, const OUString& rFilter,
330 Graphic& rGraphic,
331 GraphicFilter* pFilter = NULL,
332 sal_uInt16* pDeterminedFormat = NULL );
334 sal_uInt16 compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream, sal_uInt32 nCompression = 5);
337 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */