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 <vcl/errcode.hxx>
27 #include <o3tl/typed_flags_set.hxx>
31 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{ struct PropertyValue
; } } } }
32 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{ template <class E
> class Sequence
; } } } }
36 class FilterConfigCache
;
41 #define ERRCODE_GRFILTER_OPENERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 1)
42 #define ERRCODE_GRFILTER_IOERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 2)
43 #define ERRCODE_GRFILTER_FORMATERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 3)
44 #define ERRCODE_GRFILTER_VERSIONERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 4)
45 #define ERRCODE_GRFILTER_FILTERERROR ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 5)
46 #define ERRCODE_GRFILTER_TOOBIG ErrCode(ErrCodeArea::Vcl, ErrCodeClass::General, 7)
48 #define GRFILTER_OUTHINT_GREY 1
50 #define GRFILTER_FORMAT_NOTFOUND (sal_uInt16(0xFFFF))
51 #define GRFILTER_FORMAT_DONTKNOW (sal_uInt16(0xFFFF))
53 enum class GraphicFilterImportFlags
56 SetLogsizeForJpeg
= 0x001,
57 DontSetLogsizeForJpeg
= 0x002,
59 /// Only create a bitmap, do not read pixel data.
60 OnlyCreateBitmap
= 0x020,
61 /// Read pixel data into an existing bitmap.
62 UseExistingBitmap
= 0x040,
66 template<> struct typed_flags
<GraphicFilterImportFlags
> : is_typed_flags
<GraphicFilterImportFlags
, 0x0067> {};
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 EXP_BMP "SVBMP"
82 #define EXP_SVMETAFILE "SVMETAFILE"
83 #define EXP_WMF "SVWMF"
84 #define EXP_EMF "SVEMF"
85 #define EXP_JPEG "SVEJPEG"
86 #define EXP_SVG "SVESVG"
87 #define EXP_PDF "SVEPDF"
88 #define EXP_PNG "SVEPNG"
90 #define BMP_SHORTNAME "BMP"
91 #define GIF_SHORTNAME "GIF"
92 #define JPG_SHORTNAME "JPG"
93 #define MET_SHORTNAME "MET"
94 #define PCT_SHORTNAME "PCT"
95 #define PNG_SHORTNAME "PNG"
96 #define SVM_SHORTNAME "SVM"
97 #define TIF_SHORTNAME "TIF"
98 #define WMF_SHORTNAME "WMF"
99 #define EMF_SHORTNAME "EMF"
100 #define SVG_SHORTNAME "SVG"
101 #define PDF_SHORTNAME "PDF"
103 // Info class for all supported file formats
105 enum class GraphicFileFormat
127 // retired SGF = 0x00f4,
130 // retired SGV = 0x00f7,
136 class VCL_DLLPUBLIC GraphicDescriptor final
143 sal_uInt16 nBitsPerPixel
;
145 GraphicFileFormat nFormat
;
146 bool const bOwnStream
;
147 sal_uInt8 mnNumberOfImageComponents
;
151 bool ImpDetectBMP( SvStream
& rStm
, bool bExtendedInfo
);
152 bool ImpDetectGIF( SvStream
& rStm
, bool bExtendedInfo
);
153 bool ImpDetectJPG( SvStream
& rStm
, bool bExtendedInfo
);
154 bool ImpDetectPCD( SvStream
& rStm
, bool bExtendedInfo
);
155 bool ImpDetectPCX( SvStream
& rStm
);
156 bool ImpDetectPNG( SvStream
& rStm
, bool bExtendedInfo
);
157 bool ImpDetectTIF( SvStream
& rStm
, bool bExtendedInfo
);
158 bool ImpDetectXBM( SvStream
& rStm
, bool bExtendedInfo
);
159 bool ImpDetectXPM( SvStream
& rStm
, bool bExtendedInfo
);
160 bool ImpDetectPBM( SvStream
& rStm
, bool bExtendedInfo
);
161 bool ImpDetectPGM( SvStream
& rStm
, bool bExtendedInfo
);
162 bool ImpDetectPPM( SvStream
& rStm
, bool bExtendedInfo
);
163 bool ImpDetectRAS( SvStream
& rStm
, bool bExtendedInfo
);
164 bool ImpDetectTGA( SvStream
& rStm
, bool bExtendedInfo
);
165 bool ImpDetectPSD( SvStream
& rStm
, bool bExtendedInfo
);
166 bool ImpDetectEPS( SvStream
& rStm
, bool bExtendedInfo
);
167 bool ImpDetectDXF( SvStream
& rStm
, bool bExtendedInfo
);
168 bool ImpDetectMET( SvStream
& rStm
, bool bExtendedInfo
);
169 bool ImpDetectPCT( SvStream
& rStm
, bool bExtendedInfo
);
170 bool ImpDetectSVM( SvStream
& rStm
, bool bExtendedInfo
);
171 bool ImpDetectWMF( SvStream
& rStm
, bool bExtendedInfo
);
172 bool ImpDetectEMF( SvStream
& rStm
, bool bExtendedInfo
);
173 bool ImpDetectSVG( SvStream
& rStm
, bool bExtendedInfo
);
174 GraphicDescriptor( const GraphicDescriptor
& ) = delete;
175 GraphicDescriptor
& operator=( const GraphicDescriptor
& ) = delete;
179 /** Ctor to set a filename
181 Detect() must be called to identify the file
182 If the file has no unique header (Mtf's), the format
183 is determined from the extension */
184 GraphicDescriptor( const INetURLObject
& rPath
);
186 /** Ctor using a stream
188 Detect() must be called to identify the file
189 As some formats (Mtf's) do not have a unique header, it makes sense
190 to supply the file name (incl. ext.), so that the format can be
191 derived from the extension */
192 GraphicDescriptor( SvStream
& rInStream
, const OUString
* pPath
);
194 ~GraphicDescriptor();
196 /** starts the detection
198 if bExtendedInfo == true the file header is used to derive
199 as many properties as possible (size, color, etc.) */
200 bool Detect( bool bExtendedInfo
= false );
202 /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
203 GraphicFileFormat
GetFileFormat() const { return nFormat
; }
205 /** @return graphic size in pixels or 0 size */
206 const Size
& GetSizePixel() const { return aPixSize
; }
208 /** @return the logical graphic size in 1/100mm or 0 size */
209 const Size
& GetSize_100TH_MM() const { return aLogSize
; }
211 /** @return bits/pixel or 0 **/
212 sal_uInt16
GetBitsPerPixel() const { return nBitsPerPixel
; }
214 /** @return number of color channels */
215 sal_uInt8
GetNumberOfImageComponents() const { return mnNumberOfImageComponents
; }
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. */
224 ErrCode nStreamError
;
226 FilterErrorEx() : nStreamError( ERRCODE_NONE
) {}
229 /** Class to import and export graphic formats. */
230 class VCL_DLLPUBLIC GraphicFilter
233 GraphicFilter( bool bUseConfig
= true );
236 sal_uInt16
GetImportFormatCount();
237 sal_uInt16
GetImportFormatNumber( const OUString
& rFormatName
);
238 sal_uInt16
GetImportFormatNumberForShortName( const OUString
& rShortName
);
239 sal_uInt16
GetImportFormatNumberForTypeName( const OUString
& rType
);
240 OUString
GetImportFormatName( sal_uInt16 nFormat
);
241 OUString
GetImportFormatTypeName( sal_uInt16 nFormat
);
243 OUString
GetImportFormatMediaType( sal_uInt16 nFormat
);
245 OUString
GetImportFormatShortName( sal_uInt16 nFormat
);
246 OUString
GetImportWildcard( sal_uInt16 nFormat
, sal_Int32 nEntry
);
248 sal_uInt16
GetExportFormatCount();
249 sal_uInt16
GetExportFormatNumber( const OUString
& rFormatName
);
250 sal_uInt16
GetExportFormatNumberForMediaType( const OUString
& rShortName
);
251 sal_uInt16
GetExportFormatNumberForShortName( const OUString
& rShortName
);
252 OUString
GetExportInternalFilterName( sal_uInt16 nFormat
);
253 sal_uInt16
GetExportFormatNumberForTypeName( const OUString
& rType
);
254 OUString
GetExportFormatName( sal_uInt16 nFormat
);
255 OUString
GetExportFormatMediaType( sal_uInt16 nFormat
);
256 OUString
GetExportFormatShortName( sal_uInt16 nFormat
);
257 OUString
GetExportWildcard( sal_uInt16 nFormat
);
258 bool IsExportPixelFormat( sal_uInt16 nFormat
);
260 ErrCode
ExportGraphic( const Graphic
& rGraphic
, const INetURLObject
& rPath
,
262 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
263 ErrCode
ExportGraphic( const Graphic
& rGraphic
, const OUString
& rPath
,
264 SvStream
& rOStm
, sal_uInt16 nFormat
,
265 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
267 ErrCode
CanImportGraphic( const INetURLObject
& rPath
,
269 sal_uInt16
* pDeterminedFormat
);
271 ErrCode
ImportGraphic( Graphic
& rGraphic
, const INetURLObject
& rPath
,
272 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
273 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
);
275 ErrCode
CanImportGraphic( const OUString
& rPath
, SvStream
& rStream
,
277 sal_uInt16
* pDeterminedFormat
);
279 ErrCode
ImportGraphic( Graphic
& rGraphic
, const OUString
& rPath
,
281 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
282 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
,
283 WmfExternal
const *pExtHeader
= nullptr );
285 /// Imports multiple graphics.
287 /// The resulting graphic is added to rGraphics on success, nullptr is added on failure.
288 void ImportGraphics(std::vector
< std::shared_ptr
<Graphic
> >& rGraphics
, std::vector
< std::unique_ptr
<SvStream
> > vStreams
);
290 ErrCode
ImportGraphic( Graphic
& rGraphic
, const OUString
& rPath
,
293 sal_uInt16
* pDeterminedFormat
, GraphicFilterImportFlags nImportFlags
,
294 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
,
295 WmfExternal
const *pExtHeader
= nullptr );
297 // Setting sizeLimit limits how much will be read from the stream.
298 Graphic
ImportUnloadedGraphic(SvStream
& rIStream
, sal_uInt64 sizeLimit
= 0, Size
* pSizeHint
= nullptr);
300 const FilterErrorEx
& GetLastError() const { return *pErrorEx
;}
301 void ResetLastError();
303 const Link
<ConvertData
&,bool> GetFilterCallback() const;
304 static GraphicFilter
& GetGraphicFilter();
305 static ErrCode
LoadGraphic( const OUString
& rPath
, const OUString
& rFilter
,
307 GraphicFilter
* pFilter
= nullptr,
308 sal_uInt16
* pDeterminedFormat
= nullptr );
310 ErrCode
compressAsPNG(const Graphic
& rGraphic
, SvStream
& rOutputStream
);
315 OUString aFilterPath
;
316 FilterConfigCache
* pConfig
;
319 ErrCode
ImplSetError( ErrCode nError
, const SvStream
* pStm
= nullptr );
320 ErrCode
ImpTestOrFindFormat( const OUString
& rPath
, SvStream
& rStream
, sal_uInt16
& rFormat
);
322 DECL_LINK( FilterCallback
, ConvertData
&, bool );
324 std::unique_ptr
<FilterErrorEx
> pErrorEx
;
325 bool const bUseConfig
;
328 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */