From 267c6f2ac71f92999e969232431ba04678e7437e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 07:54:39 +0200 Subject: Adding upstream version 4:24.2.0. Signed-off-by: Daniel Baumann --- svtools/source/misc/imageresourceaccess.cxx | 167 ++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 svtools/source/misc/imageresourceaccess.cxx (limited to 'svtools/source/misc/imageresourceaccess.cxx') diff --git a/svtools/source/misc/imageresourceaccess.cxx b/svtools/source/misc/imageresourceaccess.cxx new file mode 100644 index 0000000000..7a44a4d90d --- /dev/null +++ b/svtools/source/misc/imageresourceaccess.cxx @@ -0,0 +1,167 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace svt::GraphicAccess +{ + +using namespace ::utl; +using namespace css; + +typedef ::cppu::WeakImplHelper StreamSupplier_Base; + +namespace { + +class StreamSupplier : public StreamSupplier_Base +{ +private: + uno::Reference m_xInput; + uno::Reference m_xOutput; + uno::Reference m_xSeekable; + +public: + StreamSupplier(uno::Reference xInput, uno::Reference xOutput); + +protected: + // XStream + virtual uno::Reference SAL_CALL getInputStream() override; + virtual uno::Reference SAL_CALL getOutputStream() override; + + // XSeekable + virtual void SAL_CALL seek(sal_Int64 location) override; + virtual sal_Int64 SAL_CALL getPosition() override; + virtual sal_Int64 SAL_CALL getLength() override; +}; + +} + +StreamSupplier::StreamSupplier(uno::Reference xInput, uno::Reference xOutput) + : m_xInput(std::move(xInput)) + , m_xOutput(std::move(xOutput)) +{ + m_xSeekable.set(m_xInput, uno::UNO_QUERY); + if (!m_xSeekable.is()) + m_xSeekable.set(m_xOutput, uno::UNO_QUERY); + OSL_ENSURE(m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!"); +} + +uno::Reference SAL_CALL StreamSupplier::getInputStream() +{ + return m_xInput; +} + +uno::Reference SAL_CALL StreamSupplier::getOutputStream() +{ + return m_xOutput; +} + +void SAL_CALL StreamSupplier::seek(sal_Int64 nLocation) +{ + if (!m_xSeekable.is()) + throw io::NotConnectedException(); + m_xSeekable->seek(nLocation); +} + +sal_Int64 SAL_CALL StreamSupplier::getPosition() +{ + if (!m_xSeekable.is()) + throw io::NotConnectedException(); + return m_xSeekable->getPosition(); +} + +sal_Int64 SAL_CALL StreamSupplier::getLength() +{ + if (!m_xSeekable.is()) + throw io::NotConnectedException(); + + return m_xSeekable->getLength(); +} + +bool isSupportedURL(std::u16string_view rURL) +{ + return o3tl::starts_with(rURL, u"private:resource/") + || o3tl::starts_with(rURL, u"private:graphicrepository/") + || o3tl::starts_with(rURL, u"private:standardimage/") + || o3tl::starts_with(rURL, u"vnd.sun.star.extension://"); +} + +std::unique_ptr getImageStream(uno::Reference const & rxContext, OUString const & rImageResourceURL) +{ + std::unique_ptr pMemBuffer; + + try + { + // get a GraphicProvider + uno::Reference xProvider = css::graphic::GraphicProvider::create(rxContext); + + // let it create a graphic from the given URL + uno::Sequence aMediaProperties{ comphelper::makePropertyValue( + "URL", rImageResourceURL) }; + uno::Reference xGraphic(xProvider->queryGraphic(aMediaProperties)); + + OSL_ENSURE(xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!"); + if (!xGraphic.is()) + return pMemBuffer; + + // copy the graphic to an in-memory buffer + pMemBuffer.reset(new SvMemoryStream); + uno::Reference xBufferAccess = new StreamSupplier( + new OSeekableInputStreamWrapper(*pMemBuffer), + new OSeekableOutputStreamWrapper(*pMemBuffer)); + + aMediaProperties = { comphelper::makePropertyValue("OutputStream", xBufferAccess), + comphelper::makePropertyValue("MimeType", OUString("image/png")) }; + xProvider->storeGraphic(xGraphic, aMediaProperties); + + pMemBuffer->Seek(0); + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION("svtools", "GraphicAccess::getImageStream"); + pMemBuffer.reset(); + } + + return pMemBuffer; +} + +uno::Reference getImageXStream(uno::Reference const & rxContext, OUString const & rImageResourceURL) +{ + return new OSeekableInputStreamWrapper(getImageStream(rxContext, rImageResourceURL).release(), true); // take ownership +} + +} // namespace svt::GraphicAccess + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3