LibreOffice Module svtools (master) 1
imageresourceaccess.cxx
Go to the documentation of this file.
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
22
23#include <com/sun/star/io/NotConnectedException.hpp>
24#include <com/sun/star/io/XSeekable.hpp>
25#include <com/sun/star/graphic/GraphicProvider.hpp>
26#include <com/sun/star/graphic/XGraphicProvider.hpp>
27#include <com/sun/star/io/XStream.hpp>
28
30#include <o3tl/string_view.hxx>
31#include <osl/diagnose.h>
32#include <tools/stream.hxx>
36#include <utility>
37
39{
40
41using namespace ::utl;
42using namespace css;
43
44typedef ::cppu::WeakImplHelper<io::XStream, io::XSeekable> StreamSupplier_Base;
45
46namespace {
47
48class StreamSupplier : public StreamSupplier_Base
49{
50private:
51 uno::Reference<io::XInputStream> m_xInput;
52 uno::Reference<io::XOutputStream> m_xOutput;
53 uno::Reference<io::XSeekable> m_xSeekable;
54
55public:
56 StreamSupplier(uno::Reference<io::XInputStream> xInput, uno::Reference<io::XOutputStream> xOutput);
57
58protected:
59 // XStream
60 virtual uno::Reference<io::XInputStream> SAL_CALL getInputStream() override;
61 virtual uno::Reference<io::XOutputStream> SAL_CALL getOutputStream() override;
62
63 // XSeekable
64 virtual void SAL_CALL seek(sal_Int64 location) override;
65 virtual sal_Int64 SAL_CALL getPosition() override;
66 virtual sal_Int64 SAL_CALL getLength() override;
67};
68
69}
70
71StreamSupplier::StreamSupplier(uno::Reference<io::XInputStream> xInput, uno::Reference<io::XOutputStream> xOutput)
72 : m_xInput(std::move(xInput))
73 , m_xOutput(std::move(xOutput))
74{
75 m_xSeekable.set(m_xInput, uno::UNO_QUERY);
76 if (!m_xSeekable.is())
77 m_xSeekable.set(m_xOutput, uno::UNO_QUERY);
78 OSL_ENSURE(m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!");
79}
80
81uno::Reference<io::XInputStream> SAL_CALL StreamSupplier::getInputStream()
82{
83 return m_xInput;
84}
85
86uno::Reference<io::XOutputStream> SAL_CALL StreamSupplier::getOutputStream()
87{
88 return m_xOutput;
89}
90
91void SAL_CALL StreamSupplier::seek(sal_Int64 nLocation)
92{
93 if (!m_xSeekable.is())
94 throw io::NotConnectedException();
95 m_xSeekable->seek(nLocation);
96}
97
98sal_Int64 SAL_CALL StreamSupplier::getPosition()
99{
100 if (!m_xSeekable.is())
101 throw io::NotConnectedException();
102 return m_xSeekable->getPosition();
103}
104
105sal_Int64 SAL_CALL StreamSupplier::getLength()
106{
107 if (!m_xSeekable.is())
108 throw io::NotConnectedException();
109
110 return m_xSeekable->getLength();
111}
112
113bool isSupportedURL(std::u16string_view rURL)
114{
115 return o3tl::starts_with(rURL, u"private:resource/")
116 || o3tl::starts_with(rURL, u"private:graphicrepository/")
117 || o3tl::starts_with(rURL, u"private:standardimage/")
118 || o3tl::starts_with(rURL, u"vnd.sun.star.extension://");
119}
120
121std::unique_ptr<SvStream> getImageStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL)
122{
123 std::unique_ptr<SvMemoryStream> pMemBuffer;
124
125 try
126 {
127 // get a GraphicProvider
128 uno::Reference<graphic::XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(rxContext);
129
130 // let it create a graphic from the given URL
131 uno::Sequence<beans::PropertyValue> aMediaProperties{ comphelper::makePropertyValue(
132 "URL", rImageResourceURL) };
133 uno::Reference<graphic::XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
134
135 OSL_ENSURE(xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!");
136 if (!xGraphic.is())
137 return pMemBuffer;
138
139 // copy the graphic to an in-memory buffer
140 pMemBuffer.reset(new SvMemoryStream);
141 uno::Reference<io::XStream> xBufferAccess = new StreamSupplier(
142 new OSeekableInputStreamWrapper(*pMemBuffer),
143 new OSeekableOutputStreamWrapper(*pMemBuffer));
144
145 aMediaProperties = { comphelper::makePropertyValue("OutputStream", xBufferAccess),
146 comphelper::makePropertyValue("MimeType", OUString("image/png")) };
147 xProvider->storeGraphic(xGraphic, aMediaProperties);
148
149 pMemBuffer->Seek(0);
150 }
151 catch (const uno::Exception&)
152 {
153 TOOLS_WARN_EXCEPTION("svtools", "GraphicAccess::getImageStream");
154 pMemBuffer.reset();
155 }
156
157 return pMemBuffer;
158}
159
160uno::Reference<io::XInputStream> getImageXStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL)
161{
162 return new OSeekableInputStreamWrapper(getImageStream(rxContext, rImageResourceURL).release(), true); // take ownership
163}
164
165} // namespace svt::GraphicAccess
166
167/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
uno::Reference< io::XSeekable > m_xSeekable
uno::Reference< io::XOutputStream > m_xOutput
uno::Reference< io::XInputStream > m_xInput
double getLength(const B2DPolygon &rCandidate)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
bool getOutputStream(ProgramOptions const &options, OString const &extension, std::ostream **ppOutputStream, OString &targetSourceFileName, OString &tmpSourceFileName)
std::unique_ptr< SvStream > getImageStream(uno::Reference< uno::XComponentContext > const &rxContext, OUString const &rImageResourceURL)
bool isSupportedURL(std::u16string_view rURL)
::cppu::WeakImplHelper< io::XStream, io::XSeekable > StreamSupplier_Base
uno::Reference< io::XInputStream > getImageXStream(uno::Reference< uno::XComponentContext > const &rxContext, OUString const &rImageResourceURL)