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::sun::star::beans
{ struct PropertyValue
; }
32 namespace com::sun::star::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,
58 /// Only create a bitmap, do not read pixel data.
59 OnlyCreateBitmap
= 0x020,
60 /// Read pixel data into an existing bitmap.
61 UseExistingBitmap
= 0x040,
65 template<> struct typed_flags
<GraphicFilterImportFlags
> : is_typed_flags
<GraphicFilterImportFlags
, 0x0063> {};
68 #define IMP_BMP "SVBMP"
69 #define IMP_MOV "SVMOV"
70 #define IMP_SVMETAFILE "SVMETAFILE"
71 #define IMP_WMF "SVWMF"
72 #define IMP_EMF "SVEMF"
73 #define IMP_GIF "SVIGIF"
74 #define IMP_PNG "SVIPNG"
75 #define IMP_JPEG "SVIJPEG"
76 #define IMP_XBM "SVIXBM"
77 #define IMP_XPM "SVIXPM"
78 #define IMP_SVG "SVISVG"
79 #define IMP_PDF "SVIPDF"
80 #define EXP_BMP "SVBMP"
81 #define EXP_SVMETAFILE "SVMETAFILE"
82 #define EXP_WMF "SVWMF"
83 #define EXP_EMF "SVEMF"
84 #define EXP_JPEG "SVEJPEG"
85 #define EXP_SVG "SVESVG"
86 #define EXP_PDF "SVEPDF"
87 #define EXP_PNG "SVEPNG"
89 #define BMP_SHORTNAME u"BMP"
90 #define GIF_SHORTNAME u"GIF"
91 #define JPG_SHORTNAME u"JPG"
92 #define MET_SHORTNAME u"MET"
93 #define PCT_SHORTNAME u"PCT"
94 #define PNG_SHORTNAME u"PNG"
95 #define SVM_SHORTNAME u"SVM"
96 #define TIF_SHORTNAME u"TIF"
97 #define WMF_SHORTNAME u"WMF"
98 #define EMF_SHORTNAME u"EMF"
99 #define SVG_SHORTNAME u"SVG"
100 #define PDF_SHORTNAME u"PDF"
102 // Info class for all supported file formats
104 enum class GraphicFileFormat
126 // retired SGF = 0x00f4,
129 // retired SGV = 0x00f7,
135 class VCL_DLLPUBLIC GraphicDescriptor final
142 sal_uInt16 nBitsPerPixel
;
144 GraphicFileFormat nFormat
;
146 sal_uInt8 mnNumberOfImageComponents
;
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
);
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 ImpDetectSVM( SvStream
& rStm
, bool bExtendedInfo
);
172 bool ImpDetectWMF( SvStream
& rStm
, bool bExtendedInfo
);
173 bool ImpDetectEMF( SvStream
& rStm
, bool bExtendedInfo
);
174 bool ImpDetectSVG( SvStream
& rStm
, bool bExtendedInfo
);
175 GraphicDescriptor( const GraphicDescriptor
& ) = delete;
176 GraphicDescriptor
& operator=( const GraphicDescriptor
& ) = delete;
180 /** Ctor to set a filename
182 Detect() must be called to identify the file
183 If the file has no unique header (Mtf's), the format
184 is determined from the extension */
185 GraphicDescriptor( const INetURLObject
& rPath
);
187 /** Ctor using a stream
189 Detect() must be called to identify the file
190 As some formats (Mtf's) do not have a unique header, it makes sense
191 to supply the file name (incl. ext.), so that the format can be
192 derived from the extension */
193 GraphicDescriptor( SvStream
& rInStream
, const OUString
* pPath
);
195 ~GraphicDescriptor();
197 /** starts the detection
199 if bExtendedInfo == true the file header is used to derive
200 as many properties as possible (size, color, etc.) */
201 bool Detect( bool bExtendedInfo
= false );
203 /** @return the file format, GraphicFileFormat::NOT if no format was recognized */
204 GraphicFileFormat
GetFileFormat() const { return nFormat
; }
206 /** @return graphic size in pixels or 0 size */
207 const Size
& GetSizePixel() const { return aPixSize
; }
209 /** @return the logical graphic size in 1/100mm or 0 size */
210 const Size
& GetSize_100TH_MM() const { return aLogSize
; }
212 /** @return bits/pixel or 0 **/
213 sal_uInt16
GetBitsPerPixel() const { return nBitsPerPixel
; }
215 /** @return number of color channels */
216 sal_uInt8
GetNumberOfImageComponents() const { return mnNumberOfImageComponents
; }
218 /** @return whether image supports transparency */
219 bool IsTransparent() const { return bIsTransparent
; }
221 /** @return whether image supports alpha values for translucent colours */
222 bool IsAlpha() const { return bIsAlpha
; }
224 /** @return filter number that is needed by the GraphFilter to read this format */
225 static OUString
GetImportFormatShortName( GraphicFileFormat nFormat
);
228 /** Information about errors during the GraphicFilter operation. */
231 ErrCode nStreamError
;
233 FilterErrorEx() : nStreamError( ERRCODE_NONE
) {}
236 /** Class to import and export graphic formats. */
237 class VCL_DLLPUBLIC GraphicFilter
240 GraphicFilter( bool bUseConfig
= true );
243 sal_uInt16
GetImportFormatCount() const;
244 sal_uInt16
GetImportFormatNumber( std::u16string_view rFormatName
);
245 sal_uInt16
GetImportFormatNumberForShortName( std::u16string_view rShortName
);
246 sal_uInt16
GetImportFormatNumberForTypeName( std::u16string_view rType
);
247 OUString
GetImportFormatName( sal_uInt16 nFormat
);
248 OUString
GetImportFormatTypeName( sal_uInt16 nFormat
);
250 OUString
GetImportFormatMediaType( sal_uInt16 nFormat
);
252 OUString
GetImportFormatShortName( sal_uInt16 nFormat
);
253 OUString
GetImportWildcard( sal_uInt16 nFormat
, sal_Int32 nEntry
);
255 sal_uInt16
GetExportFormatCount() const;
256 sal_uInt16
GetExportFormatNumber( std::u16string_view rFormatName
);
257 sal_uInt16
GetExportFormatNumberForMediaType( std::u16string_view rShortName
);
258 sal_uInt16
GetExportFormatNumberForShortName( std::u16string_view rShortName
);
259 OUString
GetExportInternalFilterName( sal_uInt16 nFormat
);
260 sal_uInt16
GetExportFormatNumberForTypeName( std::u16string_view rType
);
261 OUString
GetExportFormatName( sal_uInt16 nFormat
);
262 OUString
GetExportFormatMediaType( sal_uInt16 nFormat
);
263 OUString
GetExportFormatShortName( sal_uInt16 nFormat
);
264 OUString
GetExportWildcard( sal_uInt16 nFormat
);
265 bool IsExportPixelFormat( sal_uInt16 nFormat
);
267 ErrCode
ExportGraphic( const Graphic
& rGraphic
, const INetURLObject
& rPath
,
269 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
270 ErrCode
ExportGraphic( const Graphic
& rGraphic
, const OUString
& rPath
,
271 SvStream
& rOStm
, sal_uInt16 nFormat
,
272 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
= nullptr );
274 ErrCode
CanImportGraphic( const INetURLObject
& rPath
,
276 sal_uInt16
* pDeterminedFormat
);
278 ErrCode
ImportGraphic( Graphic
& rGraphic
, const INetURLObject
& rPath
,
279 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
280 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
);
282 ErrCode
CanImportGraphic( const OUString
& rPath
, SvStream
& rStream
,
284 sal_uInt16
* pDeterminedFormat
);
286 ErrCode
ImportGraphic( Graphic
& rGraphic
, const OUString
& rPath
,
288 sal_uInt16 nFormat
= GRFILTER_FORMAT_DONTKNOW
,
289 sal_uInt16
* pDeterminedFormat
= nullptr, GraphicFilterImportFlags nImportFlags
= GraphicFilterImportFlags::NONE
,
290 WmfExternal
const *pExtHeader
= nullptr );
292 /// Imports multiple graphics.
294 /// The resulting graphic is added to rGraphics on success, nullptr is added on failure.
295 void ImportGraphics(std::vector
< std::shared_ptr
<Graphic
> >& rGraphics
, std::vector
< std::unique_ptr
<SvStream
> > vStreams
);
298 Tries to ensure all Graphic objects are available (Graphic::isAvailable()). Only an optimization, may
299 not process all items.
301 void MakeGraphicsAvailableThreaded(std::vector
< Graphic
* >& rGraphics
);
303 ErrCode
ImportGraphic( Graphic
& rGraphic
, const OUString
& rPath
,
306 sal_uInt16
* pDeterminedFormat
, GraphicFilterImportFlags nImportFlags
,
307 const css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
,
308 WmfExternal
const *pExtHeader
= nullptr );
310 // Setting sizeLimit limits how much will be read from the stream.
311 Graphic
ImportUnloadedGraphic(SvStream
& rIStream
, sal_uInt64 sizeLimit
= 0, const Size
* pSizeHint
= nullptr);
313 const FilterErrorEx
& GetLastError() const { return *pErrorEx
;}
314 void ResetLastError();
316 Link
<ConvertData
&,bool> GetFilterCallback() const;
317 static GraphicFilter
& GetGraphicFilter();
318 static ErrCode
LoadGraphic( const OUString
& rPath
, const OUString
& rFilter
,
320 GraphicFilter
* pFilter
= nullptr,
321 sal_uInt16
* pDeterminedFormat
= nullptr );
323 ErrCode
compressAsPNG(const Graphic
& rGraphic
, SvStream
& rOutputStream
);
328 OUString aFilterPath
;
329 FilterConfigCache
* pConfig
;
332 ErrCode
ImplSetError( ErrCode nError
, const SvStream
* pStm
= nullptr );
333 ErrCode
ImpTestOrFindFormat( const OUString
& rPath
, SvStream
& rStream
, sal_uInt16
& rFormat
);
335 DECL_LINK( FilterCallback
, ConvertData
&, bool );
337 std::unique_ptr
<FilterErrorEx
> pErrorEx
;
341 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */