Line data Source code
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 <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>
30 :
31 : #include <com/sun/star/uno/Sequence.h>
32 : #include <com/sun/star/beans/PropertyValue.hpp>
33 :
34 : class FilterConfigCache;
35 : struct WMF_EXTERNALHEADER;
36 : struct ConvertData;
37 :
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
46 :
47 : #define GRFILTER_OUTHINT_GREY 1
48 :
49 : #define GRFILTER_FORMAT_NOTFOUND ((sal_uInt16)0xFFFF)
50 : #define GRFILTER_FORMAT_DONTKNOW ((sal_uInt16)0xFFFF)
51 :
52 : enum class GraphicFilterImportFlags
53 : {
54 : NONE = 0x000,
55 : SetLogsizeForJpeg = 0x001,
56 : DontSetLogsizeForJpeg = 0x002,
57 : ForPreview = 0x004,
58 : AllowPartialStreamRead = 0x010,
59 : };
60 : namespace o3tl
61 : {
62 : template<> struct typed_flags<GraphicFilterImportFlags> : is_typed_flags<GraphicFilterImportFlags, 0x0017> {};
63 : }
64 :
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"
85 :
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"
97 :
98 : // Info class for all supported file formats
99 :
100 : enum class GraphicFileFormat
101 : {
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
130 : };
131 :
132 : // - GraphicDescriptor -
133 :
134 :
135 : class VCL_DLLPUBLIC GraphicDescriptor
136 : {
137 : SvStream* pFileStm;
138 :
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;
147 :
148 : void ImpConstruct();
149 :
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;
178 :
179 : public:
180 :
181 : /** Ctor to set a filename
182 :
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 );
187 :
188 : /** Ctor using a stream
189 :
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 );
195 :
196 : virtual ~GraphicDescriptor();
197 :
198 : /** starts the detection
199 :
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 );
203 :
204 : /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
205 9 : GraphicFileFormat GetFileFormat() const { return nFormat; }
206 :
207 : /** @return graphic size in pixels or 0 size */
208 0 : const Size& GetSizePixel() const { return (Size&) aPixSize; }
209 :
210 : /** @return the logical graphic size in 1/100mm or 0 size */
211 0 : const Size& GetSize_100TH_MM() const { return (Size&) aLogSize; }
212 :
213 : /** @return bits/pixel or 0 **/
214 0 : sal_uInt16 GetBitsPerPixel() const { return nBitsPerPixel; }
215 :
216 : /** return number of planes or 0 */
217 : sal_uInt16 GetPlanes() const { return nPlanes; }
218 :
219 : /** @return true if the graphic is compressed */
220 : bool IsCompressed() const { return bCompressed; }
221 :
222 : /** @return filter number that is needed by the GraphFilter to read this format */
223 : static OUString GetImportFormatShortName( GraphicFileFormat nFormat );
224 : };
225 :
226 : /** Information about errors during the GraphicFilter operation. */
227 : struct FilterErrorEx
228 : {
229 : sal_uLong nFilterError;
230 : sal_uLong nStreamError;
231 :
232 98 : FilterErrorEx() : nFilterError( 0UL ), nStreamError( 0UL ) {}
233 : };
234 :
235 : /** Class to import and export graphic formats. */
236 : class VCL_DLLPUBLIC GraphicFilter
237 : {
238 : private:
239 :
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 );
243 :
244 : DECL_LINK( FilterCallback, ConvertData* pData );
245 :
246 : protected:
247 :
248 : OUString aFilterPath;
249 : FilterConfigCache* pConfig;
250 : FilterErrorEx* pErrorEx;
251 : bool bAbort;
252 : bool bUseConfig;
253 : long nExpGraphHint;
254 :
255 : public:
256 :
257 : GraphicFilter( bool bUseConfig = true );
258 : ~GraphicFilter();
259 :
260 : void SetFilterPath( const OUString& rFilterPath ) { aFilterPath = rFilterPath; };
261 :
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 );
274 :
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 );
288 :
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; }
296 :
297 : sal_uInt16 CanImportGraphic( const INetURLObject& rPath,
298 : sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
299 : sal_uInt16 * pDeterminedFormat = NULL);
300 :
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 );
304 :
305 : sal_uInt16 CanImportGraphic( const OUString& rPath, SvStream& rStream,
306 : sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW,
307 : sal_uInt16 * pDeterminedFormat = NULL);
308 :
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 );
314 :
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 );
321 :
322 : void Abort() { bAbort = true; }
323 :
324 0 : const FilterErrorEx& GetLastError() const { return *pErrorEx;}
325 : void ResetLastError();
326 :
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 );
333 :
334 : sal_uInt16 compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream, sal_uInt32 nCompression = 5);
335 : };
336 :
337 : #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|