Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/graphic/UnoGraphicDescriptor.cxx
Line
Count
Source
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
#include <graphic/UnoGraphicDescriptor.hxx>
21
22
#include <cppuhelper/weakagg.hxx>
23
#include <tools/mapunit.hxx>
24
#include <tools/stream.hxx>
25
#include <unotools/ucbstreamhelper.hxx>
26
#include <vcl/gfxlink.hxx>
27
#include <vcl/graphicfilter.hxx>
28
#include <cppuhelper/supportsservice.hxx>
29
30
#include <com/sun/star/beans/PropertyAttribute.hpp>
31
#include <com/sun/star/awt/Size.hpp>
32
#include <com/sun/star/graphic/GraphicType.hpp>
33
#include <com/sun/star/io/XInputStream.hpp>
34
35
#include <vcl/outdev.hxx>
36
#include <vcl/graph.hxx>
37
#include <vcl/svapp.hxx>
38
#include <memory>
39
40
namespace {
41
42
enum class UnoGraphicProperty
43
{
44
      GraphicType = 1
45
    , MimeType = 2
46
    , SizePixel = 3
47
    , Size100thMM = 4
48
    , BitsPerPixel = 5
49
    , Transparent = 6
50
    , Alpha = 7
51
    , Animated = 8
52
    , Linked = 9
53
    , OriginURL = 10
54
};
55
56
}
57
58
using namespace ::com::sun::star;
59
60
namespace unographic {
61
62
63
GraphicDescriptor::GraphicDescriptor() :
64
46.6k
    ::comphelper::PropertySetHelper( createPropertySetInfo() ),
65
46.6k
    mpGraphic( nullptr ),
66
46.6k
    meType( GraphicType::NONE ),
67
46.6k
    mnBitsPerPixel ( 0 ),
68
46.6k
    mbTransparent ( false )
69
46.6k
{
70
46.6k
}
71
72
GraphicDescriptor::~GraphicDescriptor()
73
    noexcept
74
46.6k
{
75
46.6k
}
76
77
void GraphicDescriptor::init( const ::Graphic& rGraphic )
78
46.6k
{
79
46.6k
    mpGraphic = &rGraphic;
80
46.6k
}
81
82
void GraphicDescriptor::init( const OUString& rURL )
83
0
{
84
0
    std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
85
86
0
    if( pIStm )
87
0
        implCreate( *pIStm, rURL );
88
0
}
89
90
void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
91
0
{
92
0
    std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
93
94
0
    if( pIStm )
95
0
        implCreate( *pIStm, rURL );
96
0
}
97
98
void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString& rURL )
99
0
{
100
0
    ::GraphicDescriptor aDescriptor( rIStm, &rURL );
101
102
0
    mpGraphic = nullptr;
103
0
    maMimeType.clear();
104
0
    meType = GraphicType::NONE;
105
0
    mnBitsPerPixel = 0;
106
0
    mbTransparent = false;
107
108
0
    if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) )
109
0
        return;
110
111
0
    OUString aMimeType;
112
0
    sal_uInt8               cType = graphic::GraphicType::EMPTY;
113
114
0
    switch( aDescriptor.GetFileFormat() )
115
0
    {
116
0
        case GraphicFileFormat::BMP: aMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
117
0
        case GraphicFileFormat::GIF: aMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
118
0
        case GraphicFileFormat::JPG: aMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
119
0
        case GraphicFileFormat::PCD: aMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
120
0
        case GraphicFileFormat::PCX: aMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
121
0
        case GraphicFileFormat::PNG: aMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
122
0
        case GraphicFileFormat::TIF: aMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
123
0
        case GraphicFileFormat::XBM: aMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
124
0
        case GraphicFileFormat::XPM: aMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
125
0
        case GraphicFileFormat::PBM: aMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
126
0
        case GraphicFileFormat::PGM: aMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
127
0
        case GraphicFileFormat::PPM: aMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
128
0
        case GraphicFileFormat::RAS: aMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
129
0
        case GraphicFileFormat::TGA: aMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
130
0
        case GraphicFileFormat::PSD: aMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
131
0
        case GraphicFileFormat::WEBP: aMimeType = MIMETYPE_WEBP; cType = graphic::GraphicType::PIXEL; break;
132
133
0
        case GraphicFileFormat::EPS: aMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
134
0
        case GraphicFileFormat::DXF: aMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
135
0
        case GraphicFileFormat::MET: aMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
136
0
        case GraphicFileFormat::PCT: aMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
137
0
        case GraphicFileFormat::SVM: aMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
138
0
        case GraphicFileFormat::WMF: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
139
0
        case GraphicFileFormat::WMZ: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
140
0
        case GraphicFileFormat::EMF: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
141
0
        case GraphicFileFormat::EMZ: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
142
0
        case GraphicFileFormat::SVG: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
143
0
        case GraphicFileFormat::SVGZ: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
144
145
0
        default:
146
0
        break;
147
0
    }
148
149
0
    if( graphic::GraphicType::EMPTY != cType )
150
0
    {
151
0
        meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
152
0
        maMimeType = aMimeType;
153
0
        maSizePixel = aDescriptor.GetSizePixel();
154
0
        maSize100thMM = aDescriptor.GetSize_100TH_MM();
155
0
        mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
156
0
        mbTransparent = ( graphic::GraphicType::VECTOR == cType );
157
0
    }
158
0
}
159
160
161
uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
162
2.24k
{
163
2.24k
    uno::Any aAny;
164
165
2.24k
    if( rType == cppu::UnoType<lang::XServiceInfo>::get())
166
0
        aAny <<= uno::Reference< lang::XServiceInfo >(this);
167
2.24k
    else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
168
0
        aAny <<= uno::Reference< lang::XTypeProvider >(this);
169
2.24k
    else if( rType == cppu::UnoType<beans::XPropertySet>::get())
170
1.49k
        aAny <<= uno::Reference< beans::XPropertySet >(this);
171
750
    else if( rType == cppu::UnoType<beans::XPropertyState>::get())
172
0
        aAny <<= uno::Reference< beans::XPropertyState >(this);
173
750
    else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
174
750
        aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
175
0
    else
176
0
        aAny = OWeakObject::queryInterface( rType );
177
178
2.24k
    return aAny;
179
2.24k
}
180
181
182
void SAL_CALL GraphicDescriptor::acquire()
183
    noexcept
184
246k
{
185
246k
    OWeakObject::acquire();
186
246k
}
187
188
189
void SAL_CALL GraphicDescriptor::release()
190
    noexcept
191
246k
{
192
246k
    OWeakObject::release();
193
246k
}
194
195
196
OUString SAL_CALL GraphicDescriptor::getImplementationName()
197
0
{
198
0
    return u"com.sun.star.comp.graphic.GraphicDescriptor"_ustr;
199
0
}
200
201
sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
202
0
{
203
0
    return cppu::supportsService(this, ServiceName);
204
0
}
205
206
207
uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
208
0
{
209
0
    return { u"com.sun.star.graphic.GraphicDescriptor"_ustr };
210
0
}
211
212
213
uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
214
0
{
215
0
    static const uno::Sequence< uno::Type > aTypes {
216
0
        cppu::UnoType<uno::XAggregation>::get(),
217
0
        cppu::UnoType<lang::XServiceInfo>::get(),
218
0
        cppu::UnoType<lang::XTypeProvider>::get(),
219
0
        cppu::UnoType<beans::XPropertySet>::get(),
220
0
        cppu::UnoType<beans::XPropertyState>::get(),
221
0
        cppu::UnoType<beans::XMultiPropertySet>::get() };
222
0
    return aTypes;
223
0
}
224
225
uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
226
0
{
227
0
    return css::uno::Sequence<sal_Int8>();
228
0
}
229
230
231
rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo()
232
46.6k
{
233
46.6k
    static ::comphelper::PropertyMapEntry const aEntries[] =
234
46.6k
    {
235
46.6k
        { u"GraphicType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
236
46.6k
        { u"MimeType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
237
46.6k
        { u"SizePixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
238
46.6k
        { u"Size100thMM"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
239
46.6k
        { u"BitsPerPixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
240
46.6k
        { u"Transparent"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
241
46.6k
        { u"Alpha"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
242
46.6k
        { u"Animated"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
243
46.6k
        { u"Linked"_ustr, sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
244
46.6k
        { u"OriginURL"_ustr, sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
245
46.6k
    };
246
247
46.6k
    return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
248
46.6k
}
249
250
251
void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
252
0
{
253
    // we only have readonly attributes
254
0
}
255
256
257
void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
258
1.66k
{
259
1.66k
    SolarMutexGuard aGuard;
260
261
3.33k
    while( *ppEntries )
262
1.66k
    {
263
1.66k
        UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
264
1.66k
        switch( theProperty )
265
1.66k
        {
266
0
            case UnoGraphicProperty::GraphicType:
267
0
            {
268
0
                const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
269
270
0
                *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
271
0
                                ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
272
0
                                graphic::GraphicType::EMPTY ) );
273
0
            }
274
0
            break;
275
276
201
            case UnoGraphicProperty::MimeType:
277
201
            {
278
201
                OUString aMimeType;
279
280
201
                if( mpGraphic )
281
201
                {
282
201
                    if( mpGraphic->IsGfxLink() )
283
27
                    {
284
27
                        GfxLink aLink = mpGraphic->GetGfxLink();
285
27
                        switch (aLink.GetType())
286
27
                        {
287
0
                            case GfxLinkType::NativeGif: aMimeType = MIMETYPE_GIF; break;
288
289
                            // #i15508# added BMP type for better exports (checked, works)
290
0
                            case GfxLinkType::NativeBmp: aMimeType = MIMETYPE_BMP; break;
291
292
0
                            case GfxLinkType::NativeJpg: aMimeType = MIMETYPE_JPG; break;
293
0
                            case GfxLinkType::NativePng: aMimeType = MIMETYPE_PNG; break;
294
0
                            case GfxLinkType::NativeTif: aMimeType = MIMETYPE_TIF; break;
295
0
                            case GfxLinkType::NativeWmf: aMimeType = aLink.IsEMF() ? MIMETYPE_EMF : MIMETYPE_WMF; break;
296
0
                            case GfxLinkType::NativeMet: aMimeType = MIMETYPE_MET; break;
297
0
                            case GfxLinkType::NativePct: aMimeType = MIMETYPE_PCT; break;
298
0
                            case GfxLinkType::NativeWebp: aMimeType = MIMETYPE_WEBP; break;
299
300
                            // added Svg mimetype support
301
0
                            case GfxLinkType::NativeSvg: aMimeType = MIMETYPE_SVG; break;
302
27
                            case GfxLinkType::NativePdf: aMimeType = MIMETYPE_PDF; break;
303
304
0
                            default:
305
0
                            break;
306
27
                        }
307
27
                    }
308
309
201
                    if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
310
174
                        aMimeType = MIMETYPE_VCLGRAPHIC;
311
201
                }
312
0
                else
313
0
                    aMimeType = maMimeType;
314
315
201
                *pValues <<= aMimeType;
316
201
            }
317
0
            break;
318
319
698
            case UnoGraphicProperty::SizePixel:
320
698
            {
321
698
                awt::Size aAWTSize( 0, 0 );
322
323
698
                if( mpGraphic )
324
698
                {
325
698
                    if( mpGraphic->GetType() == GraphicType::Bitmap )
326
656
                    {
327
656
                        const Size aSizePix( mpGraphic->GetSizePixel() );
328
656
                        aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
329
656
                    }
330
698
                }
331
0
                else
332
0
                    aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
333
334
698
                *pValues <<= aAWTSize;
335
698
            }
336
698
            break;
337
338
769
            case UnoGraphicProperty::Size100thMM:
339
769
            {
340
769
                awt::Size aAWTSize( 0, 0 );
341
342
769
                if( mpGraphic )
343
769
                {
344
769
                    if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
345
71
                    {
346
71
                        const Size aSizeLog( OutputDevice::LogicToLogic(
347
71
                            mpGraphic->GetPrefSize(),
348
71
                            mpGraphic->GetPrefMapMode(),
349
71
                            MapMode(MapUnit::Map100thMM)) );
350
71
                        aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
351
71
                    }
352
769
                }
353
0
                else
354
0
                    aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
355
356
769
                *pValues <<= aAWTSize;
357
769
            }
358
769
            break;
359
360
0
            case UnoGraphicProperty::BitsPerPixel:
361
0
            {
362
0
                sal_uInt16 nBitsPerPixel = 0;
363
364
0
                if( mpGraphic )
365
0
                {
366
0
                    if( mpGraphic->GetType() == GraphicType::Bitmap )
367
0
                    {
368
0
                        auto ePixelFormat = mpGraphic->GetBitmap().getPixelFormat();
369
0
                        nBitsPerPixel = vcl::pixelFormatBitCount(ePixelFormat);
370
0
                    }
371
0
                }
372
0
                else
373
0
                    nBitsPerPixel = mnBitsPerPixel;
374
375
0
                *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
376
0
            }
377
0
            break;
378
379
0
            case UnoGraphicProperty::Transparent:
380
0
            {
381
0
                *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
382
0
            }
383
0
            break;
384
385
0
            case UnoGraphicProperty::Alpha:
386
0
            {
387
0
                *pValues <<= mpGraphic && mpGraphic->IsAlpha();
388
0
            }
389
0
            break;
390
391
0
            case UnoGraphicProperty::Animated:
392
0
            {
393
0
                *pValues <<= mpGraphic && mpGraphic->IsAnimated();
394
0
            }
395
0
            break;
396
397
0
            case UnoGraphicProperty::Linked:
398
0
            {
399
0
                *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
400
0
            }
401
0
            break;
402
403
0
            case UnoGraphicProperty::OriginURL:
404
0
            {
405
0
                OUString aOriginURL;
406
0
                if (mpGraphic)
407
0
                    aOriginURL = mpGraphic->getOriginURL();
408
409
0
                *pValues <<= aOriginURL;
410
0
            }
411
0
            break;
412
1.66k
        }
413
414
1.66k
        ++ppEntries;
415
1.66k
        ++pValues;
416
1.66k
    }
417
1.66k
}
418
419
}
420
421
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */