diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2018-03-12 13:04:41 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-03-12 12:56:02 +0100 |
commit | afea2bbcd5cb879d18cda6eae3dfe893f6044be4 (patch) | |
tree | b4e635b61f748a5397fd3e503496650b71dd721f | |
parent | ab9eff78f05e71d4d853fe72c03823287b49c93b (diff) |
svtools: cleanup GraphicAccess
Change-Id: I1eb2b5dd6859653594771d950992dfca38161446
Reviewed-on: https://gerrit.libreoffice.org/51104
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | include/svtools/imageresourceaccess.hxx | 68 | ||||
-rw-r--r-- | svtools/source/misc/imageresourceaccess.cxx | 241 |
2 files changed, 140 insertions, 169 deletions
diff --git a/include/svtools/imageresourceaccess.hxx b/include/svtools/imageresourceaccess.hxx index ee30ab0b75dc..0ad0eb7d35cb 100644 --- a/include/svtools/imageresourceaccess.hxx +++ b/include/svtools/imageresourceaccess.hxx @@ -29,45 +29,39 @@ class SvStream; namespace svt { +namespace GraphicAccess +{ - - //= GraphicAccess - - /** helper class for obtaining streams (which also can be used with the ImageProducer) - from a resource - */ - namespace GraphicAccess - { - /** determines whether the given URL denotes an image within a resource */ - SVT_DLLPUBLIC bool isSupportedURL( const OUString& _rURL ); - - /** for a given URL of an image within a resource, this method retrieves an SvStream for this image. - - This method works for arbitrary URLs denoting an image, since the - css::graphics::GraphicsProvider service is used - to resolve the URL. However, obtaining the stream is expensive (since - the image must be copied), so you are strongly encouraged to only use it - when you know that the image is small enough. - */ - SVT_DLLPUBLIC SvStream* getImageStream( - const css::uno::Reference< css::uno::XComponentContext >& _rxContext, - const OUString& _rImageResourceURL - ); - - /** for a given URL of an image within a resource, this method retrieves - an css::io::XInputStream for this image. - */ - SVT_DLLPUBLIC css::uno::Reference< css::io::XInputStream > - getImageXStream( - const css::uno::Reference< css::uno::XComponentContext >& _rxContext, - const OUString& _rImageResourceURL - ); - } - - +/** Helpers for obtaining streams (which also can be used with the ImageProducer) + from a resource. +*/ + +/** determines whether the given URL denotes an image within a resource */ +SVT_DLLPUBLIC bool isSupportedURL(OUString const & rURL); + +/** for a given URL of an image within a resource, this method retrieves an + SvStream for this image. + + This method works for arbitrary URLs denoting an image, since the + css::graphics::GraphicsProvider service is used + to resolve the URL. However, obtaining the stream is expensive (since + the image must be copied), so you are strongly encouraged to only use it + when you know that the image is small enough. +*/ +SVT_DLLPUBLIC SvStream* getImageStream( + css::uno::Reference<css::uno::XComponentContext> const & rxContext, + OUString const & rImageResourceURL); + +/** for a given URL of an image within a resource, this method retrieves + an css::io::XInputStream for this image. +*/ +SVT_DLLPUBLIC css::uno::Reference<css::io::XInputStream> getImageXStream( + css::uno::Reference<css::uno::XComponentContext> const & rxContext, + OUString const & rImageResourceURL); + +} // namespace GraphicAccess } // namespace svt - -#endif // DBA14_ INCLUDED_SVTOOLS_IMAGERESOURCEACCESS_HXX +#endif // INCLUDED_SVTOOLS_IMAGERESOURCEACCESS_HXX /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svtools/source/misc/imageresourceaccess.cxx b/svtools/source/misc/imageresourceaccess.cxx index 5c7ff0ec86ca..8cfabbc51c73 100644 --- a/svtools/source/misc/imageresourceaccess.cxx +++ b/svtools/source/misc/imageresourceaccess.cxx @@ -32,159 +32,136 @@ #include <cppuhelper/implbase.hxx> #include <comphelper/processfactory.hxx> - namespace svt { +namespace GraphicAccess +{ +using namespace ::utl; +using namespace css; - using namespace ::utl; - using namespace ::com::sun::star::io; - using namespace ::com::sun::star::uno; - using namespace ::com::sun::star::lang; - using namespace ::com::sun::star::beans; - using namespace ::com::sun::star::graphic; - - - //= StreamSupplier - - typedef ::cppu::WeakImplHelper < XStream - , XSeekable - > StreamSupplier_Base; - class StreamSupplier : public StreamSupplier_Base - { - private: - Reference< XInputStream > m_xInput; - Reference< XOutputStream > m_xOutput; - Reference< XSeekable > m_xSeekable; - - public: - StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput ); - - protected: - // XStream - virtual Reference< XInputStream > SAL_CALL getInputStream( ) override; - virtual Reference< XOutputStream > 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( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput ) - :m_xInput( _rxInput ) - ,m_xOutput( _rxOutput ) - { - m_xSeekable.set(m_xInput, css::uno::UNO_QUERY); - if ( !m_xSeekable.is() ) - m_xSeekable.set(m_xOutput, css::uno::UNO_QUERY); - OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" ); - } - - - Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) - { - return m_xInput; - } - - - Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) - { - return m_xOutput; - } - - - void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) - { - if ( !m_xSeekable.is() ) - throw NotConnectedException(); - - m_xSeekable->seek( location ); - } +typedef ::cppu::WeakImplHelper<io::XStream, io::XSeekable> StreamSupplier_Base; +class StreamSupplier : public StreamSupplier_Base +{ +private: + uno::Reference<io::XInputStream> m_xInput; + uno::Reference<io::XOutputStream> m_xOutput; + uno::Reference<io::XSeekable> m_xSeekable; + +public: + StreamSupplier(uno::Reference<io::XInputStream> const & rxInput, uno::Reference<io::XOutputStream> const & rxOutput); + +protected: + // XStream + virtual uno::Reference<io::XInputStream> SAL_CALL getInputStream() override; + virtual uno::Reference<io::XOutputStream> 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<io::XInputStream> const & rxInput, uno::Reference<io::XOutputStream> const & rxOutput) + : m_xInput(rxInput) + , m_xOutput(rxOutput) +{ + 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!"); +} - ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) - { - if ( !m_xSeekable.is() ) - throw NotConnectedException(); +uno::Reference<io::XInputStream> SAL_CALL StreamSupplier::getInputStream() +{ + return m_xInput; +} - return m_xSeekable->getPosition(); - } +uno::Reference<io::XOutputStream> 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::getLength( ) - { - if ( !m_xSeekable.is() ) - throw NotConnectedException(); +sal_Int64 SAL_CALL StreamSupplier::getPosition() +{ + if (!m_xSeekable.is()) + throw io::NotConnectedException(); + return m_xSeekable->getPosition(); +} - return m_xSeekable->getLength(); - } +sal_Int64 SAL_CALL StreamSupplier::getLength() +{ + if (!m_xSeekable.is()) + throw io::NotConnectedException(); + return m_xSeekable->getLength(); +} - //= GraphicAccess +bool isSupportedURL(OUString const & rURL) +{ + return rURL.startsWith("private:resource/") + || rURL.startsWith("private:graphicrepository/") + || rURL.startsWith("private:standardimage/") + || rURL.startsWith("vnd.sun.star.extension://"); +} +SvStream* getImageStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL) +{ + SvStream* pReturn = nullptr; - bool GraphicAccess::isSupportedURL( const OUString& _rURL ) + try { - return _rURL.startsWith( "private:resource/" ) - || _rURL.startsWith( "private:graphicrepository/" ) - || _rURL.startsWith( "private:standardimage/" ) - || _rURL.startsWith( "vnd.sun.star.extension://" ); + // get a GraphicProvider + uno::Reference<graphic::XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(rxContext); + + // let it create a graphic from the given URL + uno::Sequence<beans::PropertyValue> aMediaProperties(1); + aMediaProperties[0].Name = "URL"; + aMediaProperties[0].Value <<= rImageResourceURL; + uno::Reference<graphic::XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties)); + + OSL_ENSURE(xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!"); + if (!xGraphic.is()) + return pReturn; + + // copy the graphic to a in-memory buffer + SvMemoryStream* pMemBuffer = new SvMemoryStream; + uno::Reference<io::XStream> xBufferAccess = new StreamSupplier( + new OSeekableInputStreamWrapper(*pMemBuffer), + new OSeekableOutputStreamWrapper(*pMemBuffer)); + + aMediaProperties.realloc(2); + aMediaProperties[0].Name = "OutputStream"; + aMediaProperties[0].Value <<= xBufferAccess; + aMediaProperties[1].Name = "MimeType"; + aMediaProperties[1].Value <<= OUString("image/png"); + xProvider->storeGraphic(xGraphic, aMediaProperties); + + pMemBuffer->Seek(0); + pReturn = pMemBuffer; } - - - SvStream* GraphicAccess::getImageStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL ) + catch (const uno::Exception&) { - SvStream* pReturn = nullptr; - - try - { - // get a GraphicProvider - Reference< XGraphicProvider > xProvider = css::graphic::GraphicProvider::create(_rxContext); - - // let it create a graphic from the given URL - Sequence< PropertyValue > aMediaProperties( 1 ); - aMediaProperties[0].Name = "URL"; - aMediaProperties[0].Value <<= _rImageResourceURL; - Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) ); - OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" ); - if ( !xGraphic.is() ) - return pReturn; - - // copy the graphic to a in-memory buffer - SvMemoryStream* pMemBuffer = new SvMemoryStream; - Reference< XStream > xBufferAccess = new StreamSupplier( - new OSeekableInputStreamWrapper( *pMemBuffer ), - new OSeekableOutputStreamWrapper( *pMemBuffer ) - ); - - aMediaProperties.realloc( 2 ); - aMediaProperties[0].Name = "OutputStream"; - aMediaProperties[0].Value <<= xBufferAccess; - aMediaProperties[1].Name = "MimeType"; - aMediaProperties[1].Value <<= OUString( "image/png" ); - xProvider->storeGraphic( xGraphic, aMediaProperties ); - - pMemBuffer->Seek( 0 ); - pReturn = pMemBuffer; - } - catch( const Exception& ) - { - OSL_FAIL( "GraphicAccess::getImageStream: caught an exception!" ); - } - - return pReturn; + OSL_FAIL("GraphicAccess::getImageStream: caught an exception!"); } + return pReturn; +} - Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL ) - { - return new OSeekableInputStreamWrapper( getImageStream( _rxContext, _rImageResourceURL ), true ); // take ownership - } - +uno::Reference<io::XInputStream> getImageXStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL) +{ + return new OSeekableInputStreamWrapper(getImageStream(rxContext, rImageResourceURL), true); // take ownership +} +} // namespace GraphicAccess } // namespace svt - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |