Coverage Report

Created: 2026-10-14 09:33

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
54.9k
    ::comphelper::PropertySetHelper( createPropertySetInfo() ),
65
54.9k
    mpGraphic( nullptr ),
66
54.9k
    meType( GraphicType::NONE ),
67
54.9k
    mnBitsPerPixel ( 0 ),
68
54.9k
    mbTransparent ( false )
69
54.9k
{
70
54.9k
}
71
72
GraphicDescriptor::~GraphicDescriptor()
73
    noexcept
74
54.9k
{
75
54.9k
}
76
77
void GraphicDescriptor::init( const ::Graphic& rGraphic )
78
54.9k
{
79
54.9k
    mpGraphic = &rGraphic;
80
54.9k
}
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::JXL: aMimeType = MIMETYPE_JXL; cType = graphic::GraphicType::PIXEL; break;
120
0
        case GraphicFileFormat::PCD: aMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
121
0
        case GraphicFileFormat::PCX: aMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
122
0
        case GraphicFileFormat::PNG: aMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
123
0
        case GraphicFileFormat::TIF: aMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
124
0
        case GraphicFileFormat::XBM: aMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
125
0
        case GraphicFileFormat::XPM: aMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
126
0
        case GraphicFileFormat::PBM: aMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
127
0
        case GraphicFileFormat::PGM: aMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
128
0
        case GraphicFileFormat::PPM: aMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
129
0
        case GraphicFileFormat::RAS: aMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
130
0
        case GraphicFileFormat::TGA: aMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
131
0
        case GraphicFileFormat::PSD: aMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
132
0
        case GraphicFileFormat::WEBP: aMimeType = MIMETYPE_WEBP; cType = graphic::GraphicType::PIXEL; break;
133
134
0
        case GraphicFileFormat::EPS: aMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
135
0
        case GraphicFileFormat::DXF: aMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
136
0
        case GraphicFileFormat::MET: aMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
137
0
        case GraphicFileFormat::PCT: aMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
138
0
        case GraphicFileFormat::SVM: aMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
139
0
        case GraphicFileFormat::WMF: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
140
0
        case GraphicFileFormat::WMZ: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
141
0
        case GraphicFileFormat::EMF: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
142
0
        case GraphicFileFormat::EMZ: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
143
0
        case GraphicFileFormat::SVG: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
144
0
        case GraphicFileFormat::SVGZ: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
145
146
0
        default:
147
0
        break;
148
0
    }
149
150
0
    if( graphic::GraphicType::EMPTY != cType )
151
0
    {
152
0
        meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
153
0
        maMimeType = aMimeType;
154
0
        maSizePixel = aDescriptor.GetSizePixel();
155
0
        maSize100thMM = aDescriptor.GetSize_100TH_MM();
156
0
        mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
157
0
        mbTransparent = ( graphic::GraphicType::VECTOR == cType );
158
0
    }
159
0
}
160
161
162
uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
163
3.79k
{
164
3.79k
    uno::Any aAny;
165
166
3.79k
    if( rType == cppu::UnoType<lang::XServiceInfo>::get())
167
0
        aAny <<= uno::Reference< lang::XServiceInfo >(this);
168
3.79k
    else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
169
0
        aAny <<= uno::Reference< lang::XTypeProvider >(this);
170
3.79k
    else if( rType == cppu::UnoType<beans::XPropertySet>::get())
171
2.43k
        aAny <<= uno::Reference< beans::XPropertySet >(this);
172
1.35k
    else if( rType == cppu::UnoType<beans::XPropertyState>::get())
173
0
        aAny <<= uno::Reference< beans::XPropertyState >(this);
174
1.35k
    else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
175
1.35k
        aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
176
0
    else
177
0
        aAny = OWeakObject::queryInterface( rType );
178
179
3.79k
    return aAny;
180
3.79k
}
181
182
183
void SAL_CALL GraphicDescriptor::acquire()
184
    noexcept
185
294k
{
186
294k
    OWeakObject::acquire();
187
294k
}
188
189
190
void SAL_CALL GraphicDescriptor::release()
191
    noexcept
192
294k
{
193
294k
    OWeakObject::release();
194
294k
}
195
196
197
OUString SAL_CALL GraphicDescriptor::getImplementationName()
198
0
{
199
0
    return u"com.sun.star.comp.graphic.GraphicDescriptor"_ustr;
200
0
}
201
202
sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
203
0
{
204
0
    return cppu::supportsService(this, ServiceName);
205
0
}
206
207
208
uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
209
0
{
210
0
    return { u"com.sun.star.graphic.GraphicDescriptor"_ustr };
211
0
}
212
213
214
uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
215
0
{
216
0
    static const uno::Sequence< uno::Type > aTypes {
217
0
        cppu::UnoType<uno::XAggregation>::get(),
218
0
        cppu::UnoType<lang::XServiceInfo>::get(),
219
0
        cppu::UnoType<lang::XTypeProvider>::get(),
220
0
        cppu::UnoType<beans::XPropertySet>::get(),
221
0
        cppu::UnoType<beans::XPropertyState>::get(),
222
0
        cppu::UnoType<beans::XMultiPropertySet>::get() };
223
0
    return aTypes;
224
0
}
225
226
uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
227
0
{
228
0
    return css::uno::Sequence<sal_Int8>();
229
0
}
230
231
232
rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo()
233
54.9k
{
234
54.9k
    static ::comphelper::PropertyMapEntry const aEntries[] =
235
54.9k
    {
236
54.9k
        { u"GraphicType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
237
54.9k
        { u"MimeType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
238
54.9k
        { u"SizePixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
239
54.9k
        { u"Size100thMM"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
240
54.9k
        { u"BitsPerPixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
241
54.9k
        { u"Transparent"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
242
54.9k
        { u"Alpha"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
243
54.9k
        { u"Animated"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
244
54.9k
        { u"Linked"_ustr, sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
245
54.9k
        { u"OriginURL"_ustr, sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
246
54.9k
    };
247
248
54.9k
    return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
249
54.9k
}
250
251
252
void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
253
0
{
254
    // we only have readonly attributes
255
0
}
256
257
258
void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
259
3.13k
{
260
3.13k
    SolarMutexGuard aGuard;
261
262
6.27k
    while( *ppEntries )
263
3.13k
    {
264
3.13k
        UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
265
3.13k
        switch( theProperty )
266
3.13k
        {
267
0
            case UnoGraphicProperty::GraphicType:
268
0
            {
269
0
                const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
270
271
0
                *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
272
0
                                ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
273
0
                                graphic::GraphicType::EMPTY ) );
274
0
            }
275
0
            break;
276
277
449
            case UnoGraphicProperty::MimeType:
278
449
            {
279
449
                OUString aMimeType;
280
281
449
                if( mpGraphic )
282
449
                {
283
449
                    if( mpGraphic->IsGfxLink() )
284
111
                    {
285
111
                        GfxLink aLink = mpGraphic->GetGfxLink();
286
111
                        switch (aLink.GetType())
287
111
                        {
288
0
                            case GfxLinkType::NativeGif: aMimeType = MIMETYPE_GIF; break;
289
290
                            // #i15508# added BMP type for better exports (checked, works)
291
0
                            case GfxLinkType::NativeBmp: aMimeType = MIMETYPE_BMP; break;
292
293
0
                            case GfxLinkType::NativeJpg: aMimeType = MIMETYPE_JPG; break;
294
0
                            case GfxLinkType::NativeJxl: aMimeType = MIMETYPE_JXL; break;
295
0
                            case GfxLinkType::NativePng: aMimeType = MIMETYPE_PNG; break;
296
0
                            case GfxLinkType::NativeTif: aMimeType = MIMETYPE_TIF; break;
297
0
                            case GfxLinkType::NativeWmf: aMimeType = aLink.IsEMF() ? MIMETYPE_EMF : MIMETYPE_WMF; break;
298
0
                            case GfxLinkType::NativeMet: aMimeType = MIMETYPE_MET; break;
299
0
                            case GfxLinkType::NativePct: aMimeType = MIMETYPE_PCT; break;
300
0
                            case GfxLinkType::NativeWebp: aMimeType = MIMETYPE_WEBP; break;
301
302
                            // added Svg mimetype support
303
0
                            case GfxLinkType::NativeSvg: aMimeType = MIMETYPE_SVG; break;
304
111
                            case GfxLinkType::NativePdf: aMimeType = MIMETYPE_PDF; break;
305
306
0
                            default:
307
0
                            break;
308
111
                        }
309
111
                    }
310
311
449
                    if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
312
338
                        aMimeType = MIMETYPE_VCLGRAPHIC;
313
449
                }
314
0
                else
315
0
                    aMimeType = maMimeType;
316
317
449
                *pValues <<= aMimeType;
318
449
            }
319
0
            break;
320
321
1.31k
            case UnoGraphicProperty::SizePixel:
322
1.31k
            {
323
1.31k
                awt::Size aAWTSize( 0, 0 );
324
325
1.31k
                if( mpGraphic )
326
1.31k
                {
327
1.31k
                    if( mpGraphic->GetType() == GraphicType::Bitmap )
328
1.25k
                    {
329
1.25k
                        const Size aSizePix( mpGraphic->GetSizePixel() );
330
1.25k
                        aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
331
1.25k
                    }
332
1.31k
                }
333
0
                else
334
0
                    aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
335
336
1.31k
                *pValues <<= aAWTSize;
337
1.31k
            }
338
1.31k
            break;
339
340
1.37k
            case UnoGraphicProperty::Size100thMM:
341
1.37k
            {
342
1.37k
                awt::Size aAWTSize( 0, 0 );
343
344
1.37k
                if( mpGraphic )
345
1.37k
                {
346
1.37k
                    if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
347
68
                    {
348
68
                        const Size aSizeLog( OutputDevice::LogicToLogic(
349
68
                            mpGraphic->GetPrefSize(),
350
68
                            mpGraphic->GetPrefMapMode(),
351
68
                            MapMode(MapUnit::Map100thMM)) );
352
68
                        aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
353
68
                    }
354
1.37k
                }
355
0
                else
356
0
                    aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
357
358
1.37k
                *pValues <<= aAWTSize;
359
1.37k
            }
360
1.37k
            break;
361
362
0
            case UnoGraphicProperty::BitsPerPixel:
363
0
            {
364
0
                sal_uInt16 nBitsPerPixel = 0;
365
366
0
                if( mpGraphic )
367
0
                {
368
0
                    if( mpGraphic->GetType() == GraphicType::Bitmap )
369
0
                    {
370
0
                        auto ePixelFormat = mpGraphic->GetBitmap().getPixelFormat();
371
0
                        nBitsPerPixel = vcl::pixelFormatBitCount(ePixelFormat);
372
0
                    }
373
0
                }
374
0
                else
375
0
                    nBitsPerPixel = mnBitsPerPixel;
376
377
0
                *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
378
0
            }
379
0
            break;
380
381
0
            case UnoGraphicProperty::Transparent:
382
0
            {
383
0
                *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
384
0
            }
385
0
            break;
386
387
0
            case UnoGraphicProperty::Alpha:
388
0
            {
389
0
                *pValues <<= mpGraphic && mpGraphic->IsAlpha();
390
0
            }
391
0
            break;
392
393
0
            case UnoGraphicProperty::Animated:
394
0
            {
395
0
                *pValues <<= mpGraphic && mpGraphic->IsAnimated();
396
0
            }
397
0
            break;
398
399
0
            case UnoGraphicProperty::Linked:
400
0
            {
401
0
                *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
402
0
            }
403
0
            break;
404
405
0
            case UnoGraphicProperty::OriginURL:
406
0
            {
407
0
                OUString aOriginURL;
408
0
                if (mpGraphic)
409
0
                    aOriginURL = mpGraphic->getOriginURL();
410
411
0
                *pValues <<= aOriginURL;
412
0
            }
413
0
            break;
414
3.13k
        }
415
416
3.13k
        ++ppEntries;
417
3.13k
        ++pValues;
418
3.13k
    }
419
3.13k
}
420
421
}
422
423
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */