vcl: 'horizontically'
[LibreOffice.git] / include / vcl / graphicfilter.hxx
blob35728237dc7af3dba682b7d41e2d2a91c0e65c6c
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 <vcl/dllapi.h>
25 #include <vcl/graph.hxx>
26 #include <vcl/errcode.hxx>
27 #include <o3tl/typed_flags_set.hxx>
29 #include <memory>
31 namespace com::sun::star::beans { struct PropertyValue; }
32 namespace com::sun::star::uno { template <class E> class Sequence; }
34 class INetURLObject;
36 class FilterConfigCache;
37 class SvStream;
38 struct WmfExternal;
39 struct ConvertData;
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
55 NONE = 0x000,
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,
63 namespace o3tl
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
106 NOT = 0x0000,
107 BMP = 0x0001,
108 GIF = 0x0002,
109 JPG = 0x0003,
110 PCD = 0x0004,
111 PCX = 0x0005,
112 PNG = 0x0006,
113 TIF = 0x0007,
114 XBM = 0x0008,
115 XPM = 0x0009,
116 PBM = 0x000a,
117 PGM = 0x000b,
118 PPM = 0x000c,
119 RAS = 0x000d,
120 TGA = 0x000e,
121 PSD = 0x000f,
122 EPS = 0x0010,
123 DXF = 0x00f1,
124 MET = 0x00f2,
125 PCT = 0x00f3,
126 // retired SGF = 0x00f4,
127 SVM = 0x00f5,
128 WMF = 0x00f6,
129 // retired SGV = 0x00f7,
130 EMF = 0x00f8,
131 SVG = 0x00f9
135 class VCL_DLLPUBLIC GraphicDescriptor final
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 bOwnStream;
146 sal_uInt8 mnNumberOfImageComponents;
147 bool bIsTransparent;
148 bool bIsAlpha;
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 );
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;
178 public:
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. */
229 struct FilterErrorEx
231 ErrCode nStreamError;
233 FilterErrorEx() : nStreamError( ERRCODE_NONE ) {}
236 /** Class to import and export graphic formats. */
237 class VCL_DLLPUBLIC GraphicFilter
239 public:
240 GraphicFilter( bool bUseConfig = true );
241 ~GraphicFilter();
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 );
249 #ifdef _WIN32
250 OUString GetImportFormatMediaType( sal_uInt16 nFormat );
251 #endif
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,
268 sal_uInt16 nFormat,
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,
275 sal_uInt16 nFormat,
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,
283 sal_uInt16 nFormat,
284 sal_uInt16 * pDeterminedFormat);
286 ErrCode ImportGraphic( Graphic& rGraphic, const OUString& rPath,
287 SvStream& rStream,
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,
304 SvStream& rStream,
305 sal_uInt16 nFormat,
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,
319 Graphic& rGraphic,
320 GraphicFilter* pFilter = nullptr,
321 sal_uInt16* pDeterminedFormat = nullptr );
323 ErrCode compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream);
325 void preload();
327 private:
328 OUString aFilterPath;
329 FilterConfigCache* pConfig;
331 void ImplInit();
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;
338 bool bUseConfig;
341 #endif // INCLUDED_VCL_GRAPHICFILTER_HXX
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */