31#include <com/sun/star/awt/XBitmap.hpp>
32#include <com/sun/star/graphic/XGraphicProvider2.hpp>
33#include <com/sun/star/io/XStream.hpp>
34#include <com/sun/star/lang/XServiceInfo.hpp>
35#include <com/sun/star/text/GraphicCrop.hpp>
36#include <com/sun/star/uno/XComponentContext.hpp>
57class GraphicProvider :
public ::cppu::WeakImplHelper< css::graphic::XGraphicProvider2,
58 css::lang::XServiceInfo >
72 virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( )
override;
73 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId( )
override;
76 virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL queryGraphicDescriptor(
const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties )
override;
77 virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL queryGraphic(
const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties )
override;
78 virtual void SAL_CALL storeGraphic(
const css::uno::Reference< css::graphic::XGraphic >&
Graphic,
const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties )
override;
81 uno::Sequence< uno::Reference<graphic::XGraphic> > SAL_CALL queryGraphics(
const uno::Sequence< uno::Sequence<beans::PropertyValue> >& MediaPropertiesSeq )
override;
85 static css::uno::Reference< css::graphic::XGraphic > implLoadMemory( std::u16string_view rResourceURL );
86 static css::uno::Reference< css::graphic::XGraphic > implLoadRepositoryImage( std::u16string_view rResourceURL );
87 static css::uno::Reference< css::graphic::XGraphic > implLoadBitmap(
const css::uno::Reference< css::awt::XBitmap >& rBitmap );
88 static css::uno::Reference< css::graphic::XGraphic > implLoadStandardImage( std::u16string_view rResourceURL );
91GraphicProvider::GraphicProvider()
95OUString SAL_CALL GraphicProvider::getImplementationName()
97 return "com.sun.star.comp.graphic.GraphicProvider";
100sal_Bool SAL_CALL GraphicProvider::supportsService(
const OUString& ServiceName )
105uno::Sequence< OUString > SAL_CALL GraphicProvider::getSupportedServiceNames()
107 return {
"com.sun.star.graphic.GraphicProvider" };
110uno::Sequence< uno::Type > SAL_CALL GraphicProvider::getTypes()
112 static const uno::Sequence< uno::Type >
aTypes {
120uno::Sequence< sal_Int8 > SAL_CALL GraphicProvider::getImplementationId()
122 return css::uno::Sequence<sal_Int8>();
125uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadMemory( std::u16string_view rResourceURL )
127 uno::Reference< ::graphic::XGraphic > xRet;
130 if(
o3tl::getToken(rResourceURL, 0,
'/', nIndex ) == u
"private:memorygraphic" )
134 if( nGraphicAddress )
138 pUnoGraphic->init( *
reinterpret_cast< ::
Graphic*
>( nGraphicAddress ) );
146uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadRepositoryImage( std::u16string_view rResourceURL )
148 uno::Reference< ::graphic::XGraphic > xRet;
150 std::u16string_view sPathName;
157 aGraphic.setOriginURL(OUString(rResourceURL));
158 xRet = aGraphic.GetXGraphic();
164uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadStandardImage( std::u16string_view rResourceURL )
166 uno::Reference< ::graphic::XGraphic > xRet;
168 std::u16string_view sImageName;
171 if ( sImageName == u
"info" )
175 else if ( sImageName == u
"warning" )
179 else if ( sImageName == u
"error" )
183 else if ( sImageName == u
"query" )
192uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadBitmap(
const uno::Reference< awt::XBitmap >& xBtm )
194 uno::Reference< ::graphic::XGraphic > xRet;
195 uno::Sequence< sal_Int8 > aBmpSeq( xBtm->getDIB() );
196 uno::Sequence< sal_Int8 > aMaskSeq( xBtm->getMaskDIB() );
197 SvMemoryStream aBmpStream( aBmpSeq.getArray(), aBmpSeq.getLength(), StreamMode::READ );
201 ReadDIB(aBmp, aBmpStream,
true);
203 if( aMaskSeq.hasElements() )
205 SvMemoryStream aMaskStream( aMaskSeq.getArray(), aMaskSeq.getLength(), StreamMode::READ );
208 ReadDIB(aMask, aMaskStream,
true);
218 pUnoGraphic->init( aBmpEx );
224uno::Reference< beans::XPropertySet > SAL_CALL GraphicProvider::queryGraphicDescriptor(
const uno::Sequence< beans::PropertyValue >& rMediaProperties )
226 uno::Reference< beans::XPropertySet > xRet;
229 uno::Reference< io::XInputStream > xIStm;
230 uno::Reference< awt::XBitmap >xBtm;
232 for(
const auto& rMediaProperty : rMediaProperties )
237 const OUString
aName( rMediaProperty.Name );
238 const uno::Any aValue( rMediaProperty.Value );
244 else if (aName ==
"InputStream")
248 else if (aName ==
"Bitmap")
259 pDescriptor->init( xIStm, aURL );
262 else if( !
aURL.isEmpty() )
264 uno::Reference< ::graphic::XGraphic > xGraphic( implLoadMemory( aURL ) );
266 if ( !xGraphic.is() )
267 xGraphic = implLoadRepositoryImage( aURL );
269 if ( !xGraphic.is() )
270 xGraphic = implLoadStandardImage( aURL );
274 xRet.set( xGraphic, uno::UNO_QUERY );
279 pDescriptor->init( aURL );
285 uno::Reference< ::graphic::XGraphic > xGraphic( implLoadBitmap( xBtm ) );
287 xRet.set( xGraphic, uno::UNO_QUERY );
294uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic(
const uno::Sequence< ::beans::PropertyValue >& rMediaProperties )
296 uno::Reference< ::graphic::XGraphic > xRet;
299 uno::Reference< io::XInputStream > xIStm;
300 uno::Reference< awt::XBitmap >xBtm;
302 uno::Sequence< ::beans::PropertyValue > aFilterData;
304 bool bLazyRead =
false;
305 bool bLoadAsLink =
false;
307 for (
const auto& rMediaProperty : rMediaProperties)
312 const OUString
aName( rMediaProperty.Name );
313 const uno::Any aValue( rMediaProperty.Value );
321 else if (aName ==
"InputStream")
325 else if (aName ==
"Bitmap")
329 else if (aName ==
"FilterData")
331 aValue >>= aFilterData;
333 else if (aName ==
"LazyRead")
335 aValue >>= bLazyRead;
337 else if (aName ==
"LoadAsLink")
339 aValue >>= bLoadAsLink;
344 sal_uInt16 nExtWidth = 0;
345 sal_uInt16 nExtHeight = 0;
346 sal_uInt16 nExtMapMode = 0;
347 for(
const auto& rProp : std::as_const(aFilterData) )
349 const OUString
aName( rProp.Name );
350 const uno::Any aValue( rProp.Value );
352 if (aName ==
"ExternalWidth")
354 aValue >>= nExtWidth;
356 else if (aName ==
"ExternalHeight")
358 aValue >>= nExtHeight;
360 else if (aName ==
"ExternalMapMode")
362 aValue >>= nExtMapMode;
368 std::unique_ptr<SvStream> pIStm;
374 else if( !aPath.isEmpty() )
376 xRet = implLoadMemory( aPath );
379 xRet = implLoadRepositoryImage( aPath );
382 xRet = implLoadStandardImage( aPath );
389 xRet = implLoadBitmap( xBtm );
401 aExtHeader.
xExt = nExtWidth;
402 aExtHeader.
yExt = nExtHeight;
403 aExtHeader.
mapMode = nExtMapMode;
404 if ( nExtMapMode > 0 )
414 aVCLGraphic = aGraphic;
419 if( (error == ERRCODE_NONE ) &&
422 if (!aPath.isEmpty() && bLoadAsLink)
427 pUnoGraphic->init( aVCLGraphic );
431 SAL_WARN(
"svtools",
"Could not create graphic for:" << aPath <<
" error: " << error);
439uno::Sequence< uno::Reference<graphic::XGraphic> > SAL_CALL GraphicProvider::queryGraphics(
const uno::Sequence< uno::Sequence<beans::PropertyValue> >& rMediaPropertiesSeq)
442 std::vector< std::unique_ptr<SvStream> > aStreams;
443 for (
const auto& rMediaProperties : rMediaPropertiesSeq)
445 std::unique_ptr<SvStream> pStream;
446 uno::Reference<io::XInputStream>
xStream;
448 auto pProp = std::find_if(rMediaProperties.begin(), rMediaProperties.end(),
449 [](
const beans::PropertyValue& rProp) { return rProp.Name ==
"InputStream"; });
450 if (pProp != rMediaProperties.end())
457 aStreams.push_back(std::move(pStream));
461 std::vector< std::shared_ptr<Graphic> > aGraphics;
466 std::vector< uno::Reference<graphic::XGraphic> > aRet;
467 for (
const auto& pGraphic : aGraphics)
469 uno::Reference<graphic::XGraphic> xGraphic;
474 pUnoGraphic->init(*pGraphic);
475 xGraphic = pUnoGraphic;
478 aRet.push_back(xGraphic);
484void ImplCalculateCropRect(
::Graphic const & rGraphic,
const text::GraphicCrop& rGraphicCropLogic,
tools::Rectangle& rGraphicCropPixel )
486 if ( !(rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom) )
490 if ( !(aSourceSizePixel.Width() && aSourceSizePixel.Height()) )
493 if ( !(rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom) )
496 Size aSize100thMM( 0, 0 );
505 if ( aSize100thMM.Width() && aSize100thMM.Height() )
507 double fSourceSizePixelWidth =
static_cast<double>(aSourceSizePixel.Width());
508 double fSourceSizePixelHeight=
static_cast<double>(aSourceSizePixel.Height());
509 rGraphicCropPixel.
SetLeft(
static_cast< sal_Int32
>((fSourceSizePixelWidth * rGraphicCropLogic.Left ) / aSize100thMM.Width()) );
510 rGraphicCropPixel.
SetTop(
static_cast< sal_Int32
>((fSourceSizePixelHeight * rGraphicCropLogic.Top ) / aSize100thMM.Height()) );
511 rGraphicCropPixel.
SetRight(
static_cast< sal_Int32
>(( fSourceSizePixelWidth * ( aSize100thMM.Width() - rGraphicCropLogic.Right ) ) / aSize100thMM.Width() ) );
512 rGraphicCropPixel.
SetBottom(
static_cast< sal_Int32
>(( fSourceSizePixelHeight * ( aSize100thMM.Height() - rGraphicCropLogic.Bottom ) ) / aSize100thMM.Height() ) );
516void ImplApplyBitmapScaling(
::Graphic& rGraphic, sal_Int32 nPixelWidth, sal_Int32 nPixelHeight )
518 if ( nPixelWidth && nPixelHeight )
523 aBmpEx.
Scale(
Size( nPixelWidth, nPixelHeight ) );
530void ImplApplyBitmapResolution(
::Graphic& rGraphic, sal_Int32 nImageResolution,
const Size& rVisiblePixelSize,
const awt::Size& rLogicalSize )
532 if ( !(nImageResolution && rLogicalSize.Width && rLogicalSize.Height) )
535 const double fImageResolution =
static_cast<double>( nImageResolution );
536 const double fSourceDPIX = (
static_cast<double>(rVisiblePixelSize.
Width()) * 2540.0 ) /
static_cast<double>(rLogicalSize.Width);
537 const double fSourceDPIY = (
static_cast<double>(rVisiblePixelSize.
Height()) * 2540.0 ) /
static_cast<double>(rLogicalSize.Height);
540 const double fSourcePixelWidth =
static_cast<double>( nSourcePixelWidth );
541 const double fSourcePixelHeight=
static_cast<double>( nSourcePixelHeight );
543 sal_Int32 nDestPixelWidth = nSourcePixelWidth;
544 sal_Int32 nDestPixelHeight = nSourcePixelHeight;
547 if( fSourceDPIX > fImageResolution )
549 nDestPixelWidth =
static_cast<sal_Int32
>(( fSourcePixelWidth * fImageResolution ) / fSourceDPIX);
550 if ( !nDestPixelWidth || ( nDestPixelWidth > nSourcePixelWidth ) )
551 nDestPixelWidth = nSourcePixelWidth;
553 if ( fSourceDPIY > fImageResolution )
555 nDestPixelHeight=
static_cast<sal_Int32
>(( fSourcePixelHeight* fImageResolution ) / fSourceDPIY);
556 if ( !nDestPixelHeight || ( nDestPixelHeight > nSourcePixelHeight ) )
557 nDestPixelHeight = nSourcePixelHeight;
559 if ( ( nDestPixelWidth != nSourcePixelWidth ) || ( nDestPixelHeight != nSourcePixelHeight ) )
560 ImplApplyBitmapScaling( rGraphic, nDestPixelWidth, nDestPixelHeight );
563void ImplApplyFilterData(
::Graphic& rGraphic,
const uno::Sequence< beans::PropertyValue >& rFilterData )
570 sal_Int32 nPixelWidth = 0;
571 sal_Int32 nPixelHeight= 0;
572 sal_Int32 nImageResolution = 0;
573 awt::Size aLogicalSize( 0, 0 );
574 text::GraphicCrop aCropLogic( 0, 0, 0, 0 );
575 bool bRemoveCropArea =
true;
577 for(
const auto& rProp : rFilterData )
579 const OUString
aName( rProp.Name );
580 const uno::Any aValue( rProp.Value );
582 if (aName ==
"PixelWidth")
583 aValue >>= nPixelWidth;
584 else if (aName ==
"PixelHeight")
585 aValue >>= nPixelHeight;
586 else if (aName ==
"LogicalSize")
587 aValue >>= aLogicalSize;
588 else if (aName ==
"GraphicCropLogic")
589 aValue >>= aCropLogic;
590 else if (aName ==
"RemoveCropArea")
591 aValue >>= bRemoveCropArea;
592 else if (aName ==
"ImageResolution")
593 aValue >>= nImageResolution;
604 ImplCalculateCropRect( rGraphic, aCropLogic, aCropPixel );
605 if ( bRemoveCropArea )
608 aBmpEx.
Crop( aCropPixel );
611 Size aVisiblePixelSize( bRemoveCropArea ? rGraphic.
GetSizePixel() : aCropPixel.GetSize() );
612 ImplApplyBitmapResolution( rGraphic, nImageResolution, aVisiblePixelSize, aLogicalSize );
613 ImplApplyBitmapScaling( rGraphic, nPixelWidth, nPixelHeight );
621 if ( aMtfSize.Width() && aMtfSize.Height() )
623 MapMode aNewMapMode( MapUnit::Map100thMM );
624 aNewMapMode.SetScaleX(
Fraction( aLogicalSize.Width, aMtfSize.Width() ) );
625 aNewMapMode.SetScaleY(
Fraction( aLogicalSize.Height, aMtfSize.Height() ) );
626 aDummyVDev->EnableOutput(
false );
627 aDummyVDev->SetMapMode( aNewMapMode );
629 for(
size_t i = 0, nObjCount = aMtf.GetActionSize(); i < nObjCount; i++ )
660 assert(pScaleAction);
663 aSize = pScaleAction->
GetSize();
668 assert(pScaleAction);
671 aSize = pScaleAction->
GetSize();
674 const Size aSize100thmm( aDummyVDev->LogicToPixel( aSize ) );
675 Size aSize100thmm2( aDummyVDev->PixelToLogic(aSize100thmm,
MapMode(MapUnit::Map100thMM)) );
677 ImplApplyBitmapResolution( aGraphic, nImageResolution,
678 aGraphic.
GetSizePixel(), awt::Size( aSize100thmm2.Width(), aSize100thmm2.Height() ) );
681 aMtf.ReplaceAction( pNewAction, i );
700void SAL_CALL GraphicProvider::storeGraphic(
const uno::Reference< ::graphic::XGraphic >& rxGraphic,
const uno::Sequence< beans::PropertyValue >& rMediaProperties )
702 std::unique_ptr<SvStream> pOStm;
705 for(
const auto& rMediaProperty : rMediaProperties )
707 const OUString
aName( rMediaProperty.Name );
708 const uno::Any aValue( rMediaProperty.Value );
718 else if (aName ==
"OutputStream")
720 uno::Reference< io::XStream > xOStm;
735 uno::Sequence< beans::PropertyValue > aFilterDataSeq;
736 OUString sFilterShortName;
738 for(
const auto& rMediaProperty : rMediaProperties )
740 const OUString
aName( rMediaProperty.Name );
741 const uno::Any aValue( rMediaProperty.Value );
743 if (aName ==
"FilterData")
745 aValue >>= aFilterDataSeq;
747 else if (aName ==
"MimeType")
754 sFilterShortName =
"bmp";
756 sFilterShortName =
"eps";
758 sFilterShortName =
"gif";
760 sFilterShortName =
"jpg";
762 sFilterShortName =
"met";
764 sFilterShortName =
"png";
766 sFilterShortName =
"pct";
768 sFilterShortName =
"pbm";
770 sFilterShortName =
"pgm";
772 sFilterShortName =
"ppm";
774 sFilterShortName =
"ras";
776 sFilterShortName =
"svm";
778 sFilterShortName =
"tif";
780 sFilterShortName =
"emf";
782 sFilterShortName =
"wmf";
784 sFilterShortName =
"xpm";
786 sFilterShortName =
"svg";
792 if( sFilterShortName.isEmpty() )
798 const uno::Reference< XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
799 const ::unographic::Graphic* pUnoGraphic =
dynamic_cast<::
unographic::Graphic*
>(xIFace.get());
800 const ::Graphic* pGraphic = pUnoGraphic ? &pUnoGraphic->
GetGraphic() :
nullptr;
805 ImplApplyFilterData( aGraphic, aFilterDataSeq );
810 aMemStrm.
SetVersion( SOFFICE_FILEFORMAT_CURRENT );
814 aSerializer.writeGraphic(aGraphic);
820 ( aFilterDataSeq.hasElements() ? &aFilterDataSeq :
nullptr ) );
829extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
831 css::uno::XComponentContext *,
832 css::uno::Sequence<css::uno::Any>
const &)
834 return cppu::acquire(
new GraphicProvider);
constexpr OUStringLiteral MIMETYPE_MET
constexpr OUStringLiteral MIMETYPE_TIF
constexpr OUStringLiteral MIMETYPE_EPS
constexpr OUStringLiteral MIMETYPE_JPG
constexpr OUStringLiteral MIMETYPE_XPM
constexpr OUStringLiteral MIMETYPE_SVG
constexpr OUStringLiteral MIMETYPE_WMF
constexpr OUStringLiteral MIMETYPE_RAS
constexpr OUStringLiteral MIMETYPE_PNG
constexpr OUStringLiteral MIMETYPE_VCLGRAPHIC
constexpr OUStringLiteral MIMETYPE_PBM
constexpr OUStringLiteral MIMETYPE_PGM
constexpr OUStringLiteral MIMETYPE_EMF
constexpr OUStringLiteral MIMETYPE_PPM
constexpr OUStringLiteral MIMETYPE_BMP
constexpr OUStringLiteral MIMETYPE_GIF
constexpr OUStringLiteral MIMETYPE_PCT
constexpr OUStringLiteral MIMETYPE_SVM
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_graphic_GraphicProvider_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
void SetPrefMapMode(const MapMode &rPrefMapMode)
void SetPrefSize(const Size &rPrefSize)
const MapMode & GetPrefMapMode() const
bool Crop(const tools::Rectangle &rRectPixel)
Crop the bitmap.
const Size & GetPrefSize() const
Class to import and export graphic formats.
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
Graphic ImportUnloadedGraphic(SvStream &rIStream, sal_uInt64 sizeLimit=0, const Size *pSizeHint=nullptr)
static GraphicFilter & GetGraphicFilter()
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
void ImportGraphics(std::vector< std::shared_ptr< Graphic > > &rGraphics, std::vector< std::unique_ptr< SvStream > > vStreams)
Imports multiple graphics.
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
const GDIMetaFile & GetGDIMetaFile() const
GraphicType GetType() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
MapMode GetPrefMapMode() const
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
const std::shared_ptr< VectorGraphicData > & getVectorGraphicData() const
void setOriginURL(OUString const &rOriginURL)
MapUnit GetMapUnit() const
SAL_WARN_UNUSED_RESULT Point PixelToLogic(const Point &rDevicePt) const
SAL_WARN_UNUSED_RESULT Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
virtual sal_uInt64 TellEnd() override
void SetVersion(sal_Int32 n)
reference_type * get() const
Get the body.
css::uno::Type const & get()
const ::Graphic & GetGraphic() const
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
static bool loadImage(const OUString &_rName, BitmapEx &_out_rImage)
loads an image from the application's image repository
#define GRFILTER_FORMAT_DONTKNOW
#define SAL_WARN(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
BitmapEx GetBitmapEx(BitmapEx const &rBitmapEx, DrawModeFlags nDrawMode)
const sal_Unicode *const aMimeType[]
Image const & GetStandardInfoBoxImage()
Image const & GetStandardWarningBoxImage()
Image const & GetStandardErrorBoxImage()
Image const & GetStandardQueryBoxImage()
sal_uInt16 mapMode
One of the following values: