diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /vcl/source/graphic | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vcl/source/graphic')
-rw-r--r-- | vcl/source/graphic/GraphicID.cxx | 103 | ||||
-rw-r--r-- | vcl/source/graphic/GraphicLoader.cxx | 44 | ||||
-rw-r--r-- | vcl/source/graphic/GraphicObject.cxx | 942 | ||||
-rw-r--r-- | vcl/source/graphic/GraphicObject2.cxx | 506 | ||||
-rw-r--r-- | vcl/source/graphic/GraphicReader.cxx | 29 | ||||
-rw-r--r-- | vcl/source/graphic/Manager.cxx | 247 | ||||
-rw-r--r-- | vcl/source/graphic/UnoGraphic.cxx | 192 | ||||
-rw-r--r-- | vcl/source/graphic/UnoGraphicDescriptor.cxx | 424 | ||||
-rw-r--r-- | vcl/source/graphic/UnoGraphicObject.cxx | 100 | ||||
-rw-r--r-- | vcl/source/graphic/UnoGraphicProvider.cxx | 837 | ||||
-rw-r--r-- | vcl/source/graphic/UnoGraphicTransformer.cxx | 140 | ||||
-rw-r--r-- | vcl/source/graphic/grfattr.cxx | 62 |
12 files changed, 3626 insertions, 0 deletions
diff --git a/vcl/source/graphic/GraphicID.cxx b/vcl/source/graphic/GraphicID.cxx new file mode 100644 index 000000000..1cad21724 --- /dev/null +++ b/vcl/source/graphic/GraphicID.cxx @@ -0,0 +1,103 @@ +/* -*- 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 <graphic/GraphicID.hxx> + +#include <impgraph.hxx> +#include <rtl/strbuf.hxx> + +GraphicID::GraphicID(ImpGraphic const& rGraphic) +{ + rGraphic.ensureAvailable(); + + mnID1 = static_cast<sal_uLong>(rGraphic.ImplGetType()) << 28; + mnID2 = mnID3 = mnID4 = 0; + + if (rGraphic.ImplGetType() == GraphicType::Bitmap) + { + auto const& rVectorGraphicDataPtr = rGraphic.getVectorGraphicData(); + if (rVectorGraphicDataPtr) + { + const basegfx::B2DRange& rRange = rVectorGraphicDataPtr->getRange(); + + mnID1 |= rVectorGraphicDataPtr->getVectorGraphicDataArrayLength(); + mnID2 = basegfx::fround(rRange.getWidth()); + mnID3 = basegfx::fround(rRange.getHeight()); + mnID4 = vcl_get_checksum( + 0, rVectorGraphicDataPtr->getVectorGraphicDataArray().getConstArray(), + rVectorGraphicDataPtr->getVectorGraphicDataArrayLength()); + } + else if (rGraphic.ImplIsAnimated()) + { + const Animation aAnimation(rGraphic.ImplGetAnimation()); + + mnID1 |= (aAnimation.Count() & 0x0fffffff); + mnID2 = aAnimation.GetDisplaySizePixel().Width(); + mnID3 = aAnimation.GetDisplaySizePixel().Height(); + mnID4 = rGraphic.ImplGetChecksum(); + } + else + { + const BitmapEx aBmpEx(rGraphic.ImplGetBitmapEx(GraphicConversionParameters())); + + mnID1 |= (((static_cast<sal_uLong>(aBmpEx.GetTransparentType()) << 8) + | (aBmpEx.IsAlpha() ? 1 : 0)) + & 0x0fffffff); + mnID2 = aBmpEx.GetSizePixel().Width(); + mnID3 = aBmpEx.GetSizePixel().Height(); + mnID4 = rGraphic.ImplGetChecksum(); + } + } + else if (rGraphic.ImplGetType() == GraphicType::GdiMetafile) + { + const GDIMetaFile& rMtf = rGraphic.ImplGetGDIMetaFile(); + + mnID1 |= (rMtf.GetActionSize() & 0x0fffffff); + mnID2 = rMtf.GetPrefSize().Width(); + mnID3 = rMtf.GetPrefSize().Height(); + mnID4 = rGraphic.ImplGetChecksum(); + } +} + +OString GraphicID::getIDString() const +{ + static const char aHexData[] + = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + sal_Int32 nShift, nIndex = 0; + sal_Int32 nLen = 24 + (2 * BITMAP_CHECKSUM_SIZE); + OStringBuffer aHexStr(nLen); + aHexStr.setLength(nLen); + + for (nShift = 28; nShift >= 0; nShift -= 4) + aHexStr[nIndex++] = aHexData[(mnID1 >> static_cast<sal_uInt32>(nShift)) & 0xf]; + + for (nShift = 28; nShift >= 0; nShift -= 4) + aHexStr[nIndex++] = aHexData[(mnID2 >> static_cast<sal_uInt32>(nShift)) & 0xf]; + + for (nShift = 28; nShift >= 0; nShift -= 4) + aHexStr[nIndex++] = aHexData[(mnID3 >> static_cast<sal_uInt32>(nShift)) & 0xf]; + + for (nShift = (8 * BITMAP_CHECKSUM_SIZE) - 4; nShift >= 0; nShift -= 4) + aHexStr[nIndex++] = aHexData[(mnID4 >> static_cast<sal_uInt32>(nShift)) & 0xf]; + + return aHexStr.makeStringAndClear(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/GraphicLoader.cxx b/vcl/source/graphic/GraphicLoader.cxx new file mode 100644 index 000000000..c26b2b9d0 --- /dev/null +++ b/vcl/source/graphic/GraphicLoader.cxx @@ -0,0 +1,44 @@ +/* -*- 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/. + * + */ + +#include <vcl/GraphicLoader.hxx> + +#include <unotools/ucbstreamhelper.hxx> +#include <vcl/graphicfilter.hxx> +#include <vcl/weld.hxx> + +using namespace css; + +namespace vcl::graphic +{ +Graphic loadFromURL(OUString const& rURL, weld::Window* pParentWin) +{ + Graphic aGraphic; + + std::unique_ptr<SvStream> pInputStream = utl::UcbStreamHelper::CreateStream( + rURL, StreamMode::READ, pParentWin ? pParentWin->GetXWindow() : nullptr); + + if (pInputStream) + { + GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); + + ErrCode nError + = rFilter.ImportGraphic(aGraphic, rURL, *pInputStream, GRFILTER_FORMAT_DONTKNOW, + nullptr, GraphicFilterImportFlags::NONE, + /*pExtHeader*/ static_cast<WmfExternal const*>(nullptr)); + if (nError != ERRCODE_NONE || aGraphic.GetType() == GraphicType::NONE) + return Graphic(); + } + + return aGraphic; +} +} // end vcl::graphic + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/GraphicObject.cxx b/vcl/source/graphic/GraphicObject.cxx new file mode 100644 index 000000000..377945ef2 --- /dev/null +++ b/vcl/source/graphic/GraphicObject.cxx @@ -0,0 +1,942 @@ +/* -*- 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 <sal/config.h> + +#include <algorithm> + +#include <osl/diagnose.h> +#include <tools/fract.hxx> +#include <tools/helpers.hxx> +#include <vcl/svapp.hxx> +#include <vcl/metaact.hxx> +#include <vcl/GraphicObject.hxx> +#include <vcl/GraphicLoader.hxx> +#include <vcl/outdev.hxx> + +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/graphic/XGraphic.hpp> +#include <memory> + + +using namespace css; +using com::sun::star::uno::Reference; +using com::sun::star::uno::XInterface; +using com::sun::star::uno::UNO_QUERY; +using com::sun::star::uno::Sequence; +using com::sun::star::container::XNameContainer; +using com::sun::star::beans::XPropertySet; + +#define WATERMARK_LUM_OFFSET 50 +#define WATERMARK_CON_OFFSET -70 + +namespace vcl::graphic +{ + +void SearchForGraphics(uno::Reference<uno::XInterface> const & xInterface, + std::vector<uno::Reference<css::graphic::XGraphic>> & raGraphicList) +{ + uno::Reference<beans::XPropertySet> xPropertySet(xInterface, UNO_QUERY); + if (xPropertySet.is()) + { + if (xPropertySet->getPropertySetInfo()->hasPropertyByName("ImageURL")) + { + OUString sURL; + xPropertySet->getPropertyValue("ImageURL") >>= sURL; + if (!sURL.isEmpty() && !GraphicObject::isGraphicObjectUniqueIdURL(sURL)) + { + Graphic aGraphic = vcl::graphic::loadFromURL(sURL); + if (!aGraphic.IsNone()) + { + raGraphicList.push_back(aGraphic.GetXGraphic()); + } + } + } else if (xPropertySet->getPropertySetInfo()->hasPropertyByName("Graphic")) + { + uno::Reference<css::graphic::XGraphic> xGraphic; + xPropertySet->getPropertyValue("Graphic") >>= xGraphic; + if (xGraphic.is()) + { + raGraphicList.push_back(xGraphic); + } + } + } + Reference<XNameContainer> xContainer(xInterface, UNO_QUERY); + if (xContainer.is()) + { + const css::uno::Sequence<OUString> aElementNames = xContainer->getElementNames(); + for (OUString const & rName : aElementNames) + { + uno::Reference<XInterface> xInnerInterface; + xContainer->getByName(rName) >>= xInnerInterface; + SearchForGraphics(xInnerInterface, raGraphicList); + } + } +} + +} // end namespace vcl::graphic + +namespace +{ + +bool lclDrawObj( OutputDevice* pOut, const Point& rPt, const Size& rSz, + GraphicObject const & rObj, const GraphicAttr& rAttr) +{ + Point aPt( rPt ); + Size aSz( rSz ); + bool bRet = false; + + if( ( rObj.GetType() == GraphicType::Bitmap ) || ( rObj.GetType() == GraphicType::GdiMetafile ) ) + { + // simple output of transformed graphic + const Graphic aGraphic( rObj.GetTransformedGraphic( &rAttr ) ); + + if( aGraphic.IsSupportedGraphic() ) + { + const sal_uInt16 nRot10 = rAttr.GetRotation() % 3600; + + if( nRot10 ) + { + tools::Polygon aPoly( tools::Rectangle( aPt, aSz ) ); + + aPoly.Rotate( aPt, nRot10 ); + const tools::Rectangle aRotBoundRect( aPoly.GetBoundRect() ); + aPt = aRotBoundRect.TopLeft(); + aSz = aRotBoundRect.GetSize(); + } + + aGraphic.Draw( pOut, aPt, aSz ); + } + + bRet = true; + } + + return bRet; +} + +void lclImplAdjust( BitmapEx& rBmpEx, const GraphicAttr& rAttr, GraphicAdjustmentFlags nAdjustmentFlags ) +{ + GraphicAttr aAttr( rAttr ); + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::DRAWMODE ) && aAttr.IsSpecialDrawMode() ) + { + switch( aAttr.GetDrawMode() ) + { + case GraphicDrawMode::Mono: + rBmpEx.Convert( BmpConversion::N1BitThreshold ); + break; + + case GraphicDrawMode::Greys: + rBmpEx.Convert( BmpConversion::N8BitGreys ); + break; + + case GraphicDrawMode::Watermark: + { + aAttr.SetLuminance( aAttr.GetLuminance() + WATERMARK_LUM_OFFSET ); + aAttr.SetContrast( aAttr.GetContrast() + WATERMARK_CON_OFFSET ); + } + break; + + default: + break; + } + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::COLORS ) && aAttr.IsAdjusted() ) + { + rBmpEx.Adjust( aAttr.GetLuminance(), aAttr.GetContrast(), + aAttr.GetChannelR(), aAttr.GetChannelG(), aAttr.GetChannelB(), + aAttr.GetGamma(), aAttr.IsInvert() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::MIRROR ) && aAttr.IsMirrored() ) + { + rBmpEx.Mirror( aAttr.GetMirrorFlags() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::ROTATE ) && aAttr.IsRotated() ) + { + rBmpEx.Rotate( aAttr.GetRotation(), COL_TRANSPARENT ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::TRANSPARENCY ) && aAttr.IsTransparent() ) + { + rBmpEx.AdjustTransparency(aAttr.GetTransparency()); + } +} + +void lclImplAdjust( GDIMetaFile& rMtf, const GraphicAttr& rAttr, GraphicAdjustmentFlags nAdjustmentFlags ) +{ + GraphicAttr aAttr( rAttr ); + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::DRAWMODE ) && aAttr.IsSpecialDrawMode() ) + { + switch( aAttr.GetDrawMode() ) + { + case GraphicDrawMode::Mono: + rMtf.Convert( MtfConversion::N1BitThreshold ); + break; + + case GraphicDrawMode::Greys: + rMtf.Convert( MtfConversion::N8BitGreys ); + break; + + case GraphicDrawMode::Watermark: + { + aAttr.SetLuminance( aAttr.GetLuminance() + WATERMARK_LUM_OFFSET ); + aAttr.SetContrast( aAttr.GetContrast() + WATERMARK_CON_OFFSET ); + } + break; + + default: + break; + } + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::COLORS ) && aAttr.IsAdjusted() ) + { + rMtf.Adjust( aAttr.GetLuminance(), aAttr.GetContrast(), + aAttr.GetChannelR(), aAttr.GetChannelG(), aAttr.GetChannelB(), + aAttr.GetGamma(), aAttr.IsInvert() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::MIRROR ) && aAttr.IsMirrored() ) + { + rMtf.Mirror( aAttr.GetMirrorFlags() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::ROTATE ) && aAttr.IsRotated() ) + { + rMtf.Rotate( aAttr.GetRotation() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::TRANSPARENCY ) && aAttr.IsTransparent() ) + { + OSL_FAIL( "Missing implementation: Mtf-Transparency" ); + } +} + +void lclImplAdjust( Animation& rAnimation, const GraphicAttr& rAttr, GraphicAdjustmentFlags nAdjustmentFlags ) +{ + GraphicAttr aAttr( rAttr ); + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::DRAWMODE ) && aAttr.IsSpecialDrawMode() ) + { + switch( aAttr.GetDrawMode() ) + { + case GraphicDrawMode::Mono: + rAnimation.Convert( BmpConversion::N1BitThreshold ); + break; + + case GraphicDrawMode::Greys: + rAnimation.Convert( BmpConversion::N8BitGreys ); + break; + + case GraphicDrawMode::Watermark: + { + aAttr.SetLuminance( aAttr.GetLuminance() + WATERMARK_LUM_OFFSET ); + aAttr.SetContrast( aAttr.GetContrast() + WATERMARK_CON_OFFSET ); + } + break; + + default: + break; + } + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::COLORS ) && aAttr.IsAdjusted() ) + { + rAnimation.Adjust( aAttr.GetLuminance(), aAttr.GetContrast(), + aAttr.GetChannelR(), aAttr.GetChannelG(), aAttr.GetChannelB(), + aAttr.GetGamma(), aAttr.IsInvert() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::MIRROR ) && aAttr.IsMirrored() ) + { + rAnimation.Mirror( aAttr.GetMirrorFlags() ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::ROTATE ) && aAttr.IsRotated() ) + { + OSL_FAIL( "Missing implementation: Animation-Rotation" ); + } + + if( ( nAdjustmentFlags & GraphicAdjustmentFlags::TRANSPARENCY ) && aAttr.IsTransparent() ) + { + OSL_FAIL( "Missing implementation: Animation-Transparency" ); + } +} + +} // end anonymous namespace + +struct GrfSimpleCacheObj +{ + Graphic maGraphic; + GraphicAttr maAttr; + + GrfSimpleCacheObj( const Graphic& rGraphic, const GraphicAttr& rAttr ) : + maGraphic( rGraphic ), maAttr( rAttr ) {} +}; + +GraphicObject::GraphicObject() +{ +} + +GraphicObject::GraphicObject(const Graphic& rGraphic) + : maGraphic(rGraphic) +{ +} + +GraphicObject::GraphicObject(const GraphicObject& rGraphicObj) + : maGraphic(rGraphicObj.GetGraphic()) + , maAttr(rGraphicObj.maAttr) + , maUserData(rGraphicObj.maUserData) +{ +} + +GraphicObject::~GraphicObject() +{ +} + +GraphicType GraphicObject::GetType() const +{ + return maGraphic.GetType(); +} + +Size GraphicObject::GetPrefSize() const +{ + return maGraphic.GetPrefSize(); +} + +MapMode GraphicObject::GetPrefMapMode() const +{ + return maGraphic.GetPrefMapMode(); +} + +bool GraphicObject::IsTransparent() const +{ + return maGraphic.IsTransparent(); +} + +bool GraphicObject::IsAnimated() const +{ + return maGraphic.IsAnimated(); +} + +bool GraphicObject::IsEPS() const +{ + return maGraphic.IsEPS(); +} + +bool GraphicObject::ImplGetCropParams( OutputDevice const * pOut, Point& rPt, Size& rSz, const GraphicAttr* pAttr, + tools::PolyPolygon& rClipPolyPoly, bool& bRectClipRegion ) const +{ + bool bRet = false; + + if( GetType() != GraphicType::NONE ) + { + tools::Polygon aClipPoly( tools::Rectangle( rPt, rSz ) ); + const sal_uInt16 nRot10 = pAttr->GetRotation() % 3600; + const Point aOldOrigin( rPt ); + const MapMode aMap100( MapUnit::Map100thMM ); + Size aSize100; + long nTotalWidth, nTotalHeight; + + if( nRot10 ) + { + aClipPoly.Rotate( rPt, nRot10 ); + bRectClipRegion = false; + } + else + bRectClipRegion = true; + + rClipPolyPoly = aClipPoly; + + if (maGraphic.GetPrefMapMode().GetMapUnit() == MapUnit::MapPixel) + aSize100 = Application::GetDefaultDevice()->PixelToLogic( maGraphic.GetPrefSize(), aMap100 ); + else + { + MapMode m(maGraphic.GetPrefMapMode()); + aSize100 = pOut->LogicToLogic( maGraphic.GetPrefSize(), &m, &aMap100 ); + } + + nTotalWidth = aSize100.Width() - pAttr->GetLeftCrop() - pAttr->GetRightCrop(); + nTotalHeight = aSize100.Height() - pAttr->GetTopCrop() - pAttr->GetBottomCrop(); + + if( !aSize100.IsEmpty() && nTotalWidth > 0 && nTotalHeight > 0 ) + { + double fScale = static_cast<double>(aSize100.Width()) / nTotalWidth; + const long nNewLeft = -FRound( ( ( pAttr->GetMirrorFlags() & BmpMirrorFlags::Horizontal ) ? pAttr->GetRightCrop() : pAttr->GetLeftCrop() ) * fScale ); + const long nNewRight = nNewLeft + FRound( aSize100.Width() * fScale ) - 1; + + fScale = static_cast<double>(rSz.Width()) / aSize100.Width(); + rPt.AdjustX(FRound( nNewLeft * fScale ) ); + rSz.setWidth( FRound( ( nNewRight - nNewLeft + 1 ) * fScale ) ); + + fScale = static_cast<double>(aSize100.Height()) / nTotalHeight; + const long nNewTop = -FRound( ( ( pAttr->GetMirrorFlags() & BmpMirrorFlags::Vertical ) ? pAttr->GetBottomCrop() : pAttr->GetTopCrop() ) * fScale ); + const long nNewBottom = nNewTop + FRound( aSize100.Height() * fScale ) - 1; + + fScale = static_cast<double>(rSz.Height()) / aSize100.Height(); + rPt.AdjustY(FRound( nNewTop * fScale ) ); + rSz.setHeight( FRound( ( nNewBottom - nNewTop + 1 ) * fScale ) ); + + if( nRot10 ) + { + tools::Polygon aOriginPoly( 1 ); + + aOriginPoly[ 0 ] = rPt; + aOriginPoly.Rotate( aOldOrigin, nRot10 ); + rPt = aOriginPoly[ 0 ]; + } + + bRet = true; + } + } + + return bRet; +} + +GraphicObject& GraphicObject::operator=( const GraphicObject& rGraphicObj ) +{ + if( &rGraphicObj != this ) + { + mxSimpleCache.reset(); + maGraphic = rGraphicObj.GetGraphic(); + maAttr = rGraphicObj.maAttr; + maUserData = rGraphicObj.maUserData; + } + + return *this; +} + +bool GraphicObject::operator==( const GraphicObject& rGraphicObj ) const +{ + return rGraphicObj.maGraphic == maGraphic + && rGraphicObj.maAttr == maAttr; +} + +OString GraphicObject::GetUniqueID() const +{ + return GetGraphic().getUniqueID(); +} + +void GraphicObject::SetAttr( const GraphicAttr& rAttr ) +{ + maAttr = rAttr; + + if (mxSimpleCache && (mxSimpleCache->maAttr != rAttr)) + mxSimpleCache.reset(); +} + +void GraphicObject::SetUserData() +{ + maUserData.clear(); +} + +void GraphicObject::SetUserData( const OUString& rUserData ) +{ + maUserData = rUserData; +} + +bool GraphicObject::Draw( OutputDevice* pOut, const Point& rPt, const Size& rSz, + const GraphicAttr* pAttr ) +{ + GraphicAttr aAttr( pAttr ? *pAttr : GetAttr() ); + Point aPt( rPt ); + Size aSz( rSz ); + const DrawModeFlags nOldDrawMode = pOut->GetDrawMode(); + bool bCropped = aAttr.IsCropped(); + bool bRet; + + // #i29534# Provide output rects for PDF writer + tools::Rectangle aCropRect; + + pOut->SetDrawMode( nOldDrawMode & ~DrawModeFlags( DrawModeFlags::SettingsLine | DrawModeFlags::SettingsFill | DrawModeFlags::SettingsText | DrawModeFlags::SettingsGradient ) ); + + // mirrored horizontically + if( aSz.Width() < 0 ) + { + aPt.AdjustX(aSz.Width() + 1 ); + aSz.setWidth( -aSz.Width() ); + aAttr.SetMirrorFlags( aAttr.GetMirrorFlags() ^ BmpMirrorFlags::Horizontal ); + } + + // mirrored vertically + if( aSz.Height() < 0 ) + { + aPt.AdjustY(aSz.Height() + 1 ); + aSz.setHeight( -aSz.Height() ); + aAttr.SetMirrorFlags( aAttr.GetMirrorFlags() ^ BmpMirrorFlags::Vertical ); + } + + if( bCropped ) + { + tools::PolyPolygon aClipPolyPoly; + bool bRectClip; + const bool bCrop = ImplGetCropParams( pOut, aPt, aSz, &aAttr, aClipPolyPoly, bRectClip ); + + pOut->Push( PushFlags::CLIPREGION ); + + if( bCrop ) + { + if( bRectClip ) + { + // #i29534# Store crop rect for later forwarding to + // PDF writer + aCropRect = aClipPolyPoly.GetBoundRect(); + pOut->IntersectClipRegion( aCropRect ); + } + else + { + pOut->IntersectClipRegion(vcl::Region(aClipPolyPoly)); + } + } + } + + bRet = lclDrawObj(pOut, aPt, aSz, *this, aAttr); + + if( bCropped ) + pOut->Pop(); + + pOut->SetDrawMode( nOldDrawMode ); + + return bRet; +} + +void GraphicObject::DrawTiled( OutputDevice* pOut, const tools::Rectangle& rArea, const Size& rSize, + const Size& rOffset, int nTileCacheSize1D ) +{ + if( pOut == nullptr || rSize.IsEmpty() ) + return; + + const MapMode aOutMapMode( pOut->GetMapMode() ); + // #106258# Clamp size to 1 for zero values. This is okay, since + // logical size of zero is handled above already + const Size aOutTileSize( ::std::max( 1L, pOut->LogicToPixel( rSize, aOutMapMode ).Width() ), + ::std::max( 1L, pOut->LogicToPixel( rSize, aOutMapMode ).Height() ) ); + + //#i69780 clip final tile size to a sane max size + while ((static_cast<sal_Int64>(rSize.Width()) * nTileCacheSize1D) > SAL_MAX_UINT16) + nTileCacheSize1D /= 2; + while ((static_cast<sal_Int64>(rSize.Height()) * nTileCacheSize1D) > SAL_MAX_UINT16) + nTileCacheSize1D /= 2; + + ImplDrawTiled( pOut, rArea, aOutTileSize, rOffset, nullptr, nTileCacheSize1D ); +} + +bool GraphicObject::StartAnimation( OutputDevice* pOut, const Point& rPt, const Size& rSz, + long nExtraData, + OutputDevice* pFirstFrameOutDev ) +{ + bool bRet = false; + + GetGraphic(); + + const GraphicAttr aAttr( GetAttr() ); + + if (IsAnimated()) + { + Point aPt( rPt ); + Size aSz( rSz ); + bool bCropped = aAttr.IsCropped(); + + if( bCropped ) + { + tools::PolyPolygon aClipPolyPoly; + bool bRectClip; + const bool bCrop = ImplGetCropParams( pOut, aPt, aSz, &aAttr, aClipPolyPoly, bRectClip ); + + pOut->Push( PushFlags::CLIPREGION ); + + if( bCrop ) + { + if( bRectClip ) + pOut->IntersectClipRegion( aClipPolyPoly.GetBoundRect() ); + else + pOut->IntersectClipRegion(vcl::Region(aClipPolyPoly)); + } + } + + if (!mxSimpleCache || (mxSimpleCache->maAttr != aAttr) || pFirstFrameOutDev) + { + mxSimpleCache.reset(new GrfSimpleCacheObj(GetTransformedGraphic(&aAttr), aAttr)); + mxSimpleCache->maGraphic.SetAnimationNotifyHdl(GetGraphic().GetAnimationNotifyHdl()); + } + + mxSimpleCache->maGraphic.StartAnimation(pOut, aPt, aSz, nExtraData, pFirstFrameOutDev); + + if( bCropped ) + pOut->Pop(); + + bRet = true; + } + else + bRet = Draw( pOut, rPt, rSz, &aAttr ); + + return bRet; +} + +void GraphicObject::StopAnimation( OutputDevice* pOut, long nExtraData ) +{ + if (mxSimpleCache) + mxSimpleCache->maGraphic.StopAnimation(pOut, nExtraData); +} + +const Graphic& GraphicObject::GetGraphic() const +{ + return maGraphic; +} + +void GraphicObject::SetGraphic( const Graphic& rGraphic, const GraphicObject* /*pCopyObj*/) +{ + maGraphic = rGraphic; +} + +void GraphicObject::SetGraphic( const Graphic& rGraphic, const OUString& /*rLink*/ ) +{ + SetGraphic( rGraphic ); +} + +Graphic GraphicObject::GetTransformedGraphic( const Size& rDestSize, const MapMode& rDestMap, const GraphicAttr& rAttr ) const +{ + // #104550# Extracted from svx/source/svdraw/svdograf.cxx + Graphic aTransGraphic( GetGraphic() ); + const GraphicType eType = GetType(); + const Size aSrcSize( aTransGraphic.GetPrefSize() ); + + // #104115# Convert the crop margins to graphic object mapmode + const MapMode aMapGraph( aTransGraphic.GetPrefMapMode() ); + const MapMode aMap100( MapUnit::Map100thMM ); + + Size aCropLeftTop; + Size aCropRightBottom; + + if( GraphicType::GdiMetafile == eType ) + { + GDIMetaFile aMtf( aTransGraphic.GetGDIMetaFile() ); + + if (aMapGraph.GetMapUnit() == MapUnit::MapPixel) + { + // crops are in 1/100th mm -> to aMapGraph -> to MapUnit::MapPixel + aCropLeftTop = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetLeftCrop(), rAttr.GetTopCrop()), + aMap100); + aCropRightBottom = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetRightCrop(), rAttr.GetBottomCrop()), + aMap100); + } + else + { + // crops are in GraphicObject units -> to aMapGraph + aCropLeftTop = OutputDevice::LogicToLogic( + Size(rAttr.GetLeftCrop(), rAttr.GetTopCrop()), + aMap100, + aMapGraph); + aCropRightBottom = OutputDevice::LogicToLogic( + Size(rAttr.GetRightCrop(), rAttr.GetBottomCrop()), + aMap100, + aMapGraph); + } + + // #104115# If the metafile is cropped, give it a special + // treatment: clip against the remaining area, scale up such + // that this area later fills the desired size, and move the + // origin to the upper left edge of that area. + if( rAttr.IsCropped() ) + { + const MapMode aMtfMapMode( aMtf.GetPrefMapMode() ); + + tools::Rectangle aClipRect( aMtfMapMode.GetOrigin().X() + aCropLeftTop.Width(), + aMtfMapMode.GetOrigin().Y() + aCropLeftTop.Height(), + aMtfMapMode.GetOrigin().X() + aSrcSize.Width() - aCropRightBottom.Width(), + aMtfMapMode.GetOrigin().Y() + aSrcSize.Height() - aCropRightBottom.Height() ); + + // #104115# To correctly crop rotated metafiles, clip by view rectangle + aMtf.AddAction( new MetaISectRectClipRegionAction( aClipRect ), 0 ); + + // #104115# To crop the metafile, scale larger than the output rectangle + aMtf.Scale( static_cast<double>(rDestSize.Width()) / (aSrcSize.Width() - aCropLeftTop.Width() - aCropRightBottom.Width()), + static_cast<double>(rDestSize.Height()) / (aSrcSize.Height() - aCropLeftTop.Height() - aCropRightBottom.Height()) ); + + // #104115# Adapt the pref size by hand (scale changes it + // proportionally, but we want it to be smaller than the + // former size, to crop the excess out) + aMtf.SetPrefSize( Size( static_cast<long>(static_cast<double>(rDestSize.Width()) * (1.0 + (aCropLeftTop.Width() + aCropRightBottom.Width()) / aSrcSize.Width()) + .5), + static_cast<long>(static_cast<double>(rDestSize.Height()) * (1.0 + (aCropLeftTop.Height() + aCropRightBottom.Height()) / aSrcSize.Height()) + .5) ) ); + + // #104115# Adapt the origin of the new mapmode, such that it + // is shifted to the place where the cropped output starts + Point aNewOrigin( static_cast<long>(static_cast<double>(aMtfMapMode.GetOrigin().X()) + rDestSize.Width() * aCropLeftTop.Width() / (aSrcSize.Width() - aCropLeftTop.Width() - aCropRightBottom.Width()) + .5), + static_cast<long>(static_cast<double>(aMtfMapMode.GetOrigin().Y()) + rDestSize.Height() * aCropLeftTop.Height() / (aSrcSize.Height() - aCropLeftTop.Height() - aCropRightBottom.Height()) + .5) ); + MapMode aNewMap( rDestMap ); + aNewMap.SetOrigin( OutputDevice::LogicToLogic(aNewOrigin, aMtfMapMode, rDestMap) ); + aMtf.SetPrefMapMode( aNewMap ); + } + else + { + aMtf.Scale( Fraction( rDestSize.Width(), aSrcSize.Width() ), Fraction( rDestSize.Height(), aSrcSize.Height() ) ); + aMtf.SetPrefMapMode( rDestMap ); + } + + aTransGraphic = aMtf; + } + else if( GraphicType::Bitmap == eType ) + { + BitmapEx aBitmapEx( aTransGraphic.GetBitmapEx() ); + tools::Rectangle aCropRect; + + // convert crops to pixel + if(rAttr.IsCropped()) + { + if (aMapGraph.GetMapUnit() == MapUnit::MapPixel) + { + // crops are in 1/100th mm -> to MapUnit::MapPixel + aCropLeftTop = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetLeftCrop(), rAttr.GetTopCrop()), + aMap100); + aCropRightBottom = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetRightCrop(), rAttr.GetBottomCrop()), + aMap100); + } + else + { + // crops are in GraphicObject units -> to MapUnit::MapPixel + aCropLeftTop = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetLeftCrop(), rAttr.GetTopCrop()), + aMapGraph); + aCropRightBottom = Application::GetDefaultDevice()->LogicToPixel( + Size(rAttr.GetRightCrop(), rAttr.GetBottomCrop()), + aMapGraph); + } + + // convert from prefmapmode to pixel + Size aSrcSizePixel( + Application::GetDefaultDevice()->LogicToPixel( + aSrcSize, + aMapGraph)); + + if(rAttr.IsCropped() + && (aSrcSizePixel.Width() != aBitmapEx.GetSizePixel().Width() || aSrcSizePixel.Height() != aBitmapEx.GetSizePixel().Height()) + && aSrcSizePixel.Width()) + { + // the size in pixels calculated from Graphic's internal MapMode (aTransGraphic.GetPrefMapMode()) + // and its internal size (aTransGraphic.GetPrefSize()) is different from its real pixel size. + // This can be interpreted as this values to be set wrong, but needs to be corrected since e.g. + // existing cropping is calculated based on this logic values already. + // aBitmapEx.Scale(aSrcSizePixel); + + // another possibility is to adapt the values created so far with a factor; this + // will keep the original Bitmap untouched and thus quality will not change + // caution: convert to double first, else pretty big errors may occur + const double fFactorX(static_cast<double>(aBitmapEx.GetSizePixel().Width()) / aSrcSizePixel.Width()); + const double fFactorY(static_cast<double>(aBitmapEx.GetSizePixel().Height()) / aSrcSizePixel.Height()); + + aCropLeftTop.setWidth( basegfx::fround(aCropLeftTop.Width() * fFactorX) ); + aCropLeftTop.setHeight( basegfx::fround(aCropLeftTop.Height() * fFactorY) ); + aCropRightBottom.setWidth( basegfx::fround(aCropRightBottom.Width() * fFactorX) ); + aCropRightBottom.setHeight( basegfx::fround(aCropRightBottom.Height() * fFactorY) ); + + aSrcSizePixel = aBitmapEx.GetSizePixel(); + } + + // setup crop rectangle in pixel + aCropRect = tools::Rectangle( aCropLeftTop.Width(), aCropLeftTop.Height(), + aSrcSizePixel.Width() - aCropRightBottom.Width(), + aSrcSizePixel.Height() - aCropRightBottom.Height() ); + } + + // #105641# Also crop animations + if( aTransGraphic.IsAnimated() ) + { + Animation aAnim( aTransGraphic.GetAnimation() ); + + for( size_t nFrame=0; nFrame<aAnim.Count(); ++nFrame ) + { + AnimationBitmap aAnimationBitmap( aAnim.Get( nFrame ) ); + + if( !aCropRect.IsInside( tools::Rectangle(aAnimationBitmap.maPositionPixel, aAnimationBitmap.maSizePixel) ) ) + { + // setup actual cropping (relative to frame position) + tools::Rectangle aCropRectRel( aCropRect ); + aCropRectRel.Move( -aAnimationBitmap.maPositionPixel.X(), + -aAnimationBitmap.maPositionPixel.Y() ); + + // cropping affects this frame, apply it then + // do _not_ apply enlargement, this is done below + ImplTransformBitmap( aAnimationBitmap.maBitmapEx, rAttr, Size(), Size(), + aCropRectRel, rDestSize, false ); + + aAnim.Replace( aAnimationBitmap, nFrame ); + } + // else: bitmap completely within crop area, + // i.e. nothing is cropped away + } + + // now, apply enlargement (if any) through global animation size + if( aCropLeftTop.Width() < 0 || + aCropLeftTop.Height() < 0 || + aCropRightBottom.Width() < 0 || + aCropRightBottom.Height() < 0 ) + { + Size aNewSize( aAnim.GetDisplaySizePixel() ); + aNewSize.AdjustWidth(aCropRightBottom.Width() < 0 ? -aCropRightBottom.Width() : 0 ); + aNewSize.AdjustWidth(aCropLeftTop.Width() < 0 ? -aCropLeftTop.Width() : 0 ); + aNewSize.AdjustHeight(aCropRightBottom.Height() < 0 ? -aCropRightBottom.Height() : 0 ); + aNewSize.AdjustHeight(aCropLeftTop.Height() < 0 ? -aCropLeftTop.Height() : 0 ); + aAnim.SetDisplaySizePixel( aNewSize ); + } + + // if topleft has changed, we must move all frames to the + // right and bottom, resp. + if( aCropLeftTop.Width() < 0 || + aCropLeftTop.Height() < 0 ) + { + Point aPosOffset( aCropLeftTop.Width() < 0 ? -aCropLeftTop.Width() : 0, + aCropLeftTop.Height() < 0 ? -aCropLeftTop.Height() : 0 ); + + for( size_t nFrame=0; nFrame<aAnim.Count(); ++nFrame ) + { + AnimationBitmap aAnimationBitmap( aAnim.Get( nFrame ) ); + + aAnimationBitmap.maPositionPixel += aPosOffset; + + aAnim.Replace( aAnimationBitmap, nFrame ); + } + } + + aTransGraphic = aAnim; + } + else + { + ImplTransformBitmap( aBitmapEx, rAttr, aCropLeftTop, aCropRightBottom, + aCropRect, rDestSize, true ); + + aTransGraphic = aBitmapEx; + } + + aTransGraphic.SetPrefSize( rDestSize ); + aTransGraphic.SetPrefMapMode( rDestMap ); + } + + GraphicObject aGrfObj( aTransGraphic ); + aTransGraphic = aGrfObj.GetTransformedGraphic( &rAttr ); + + return aTransGraphic; +} + +Graphic GraphicObject::GetTransformedGraphic( const GraphicAttr* pAttr ) const +{ + GetGraphic(); + + Graphic aGraphic; + GraphicAttr aAttr( pAttr ? *pAttr : GetAttr() ); + + if (maGraphic.IsSupportedGraphic()) + { + if( aAttr.IsSpecialDrawMode() || aAttr.IsAdjusted() || aAttr.IsMirrored() || aAttr.IsRotated() || aAttr.IsTransparent() ) + { + if( GetType() == GraphicType::Bitmap ) + { + if( IsAnimated() ) + { + Animation aAnimation( maGraphic.GetAnimation() ); + lclImplAdjust( aAnimation, aAttr, GraphicAdjustmentFlags::ALL ); + aAnimation.SetLoopCount(maGraphic.GetAnimationLoopCount()); + aGraphic = aAnimation; + } + else + { + BitmapEx aBmpEx( maGraphic.GetBitmapEx() ); + lclImplAdjust( aBmpEx, aAttr, GraphicAdjustmentFlags::ALL ); + aGraphic = aBmpEx; + } + } + else + { + GDIMetaFile aMtf( maGraphic.GetGDIMetaFile() ); + lclImplAdjust( aMtf, aAttr, GraphicAdjustmentFlags::ALL ); + aGraphic = aMtf; + } + } + else + { + if( ( GetType() == GraphicType::Bitmap ) && IsAnimated() ) + { + Animation aAnimation( maGraphic.GetAnimation() ); + aAnimation.SetLoopCount(maGraphic.GetAnimationLoopCount()); + aGraphic = aAnimation; + } + else + aGraphic = maGraphic; + } + } + + return aGraphic; +} + +bool GraphicObject::isGraphicObjectUniqueIdURL(OUString const & rURL) +{ + const OUString aPrefix("vnd.sun.star.GraphicObject:"); + return rURL.startsWith(aPrefix); +} + +// calculate scalings between real image size and logic object size. This +// is necessary since the crop values are relative to original bitmap size +basegfx::B2DVector GraphicObject::calculateCropScaling( + double fWidth, + double fHeight, + double fLeftCrop, + double fTopCrop, + double fRightCrop, + double fBottomCrop) const +{ + const MapMode aMapMode100thmm(MapUnit::Map100thMM); + Size aBitmapSize(GetPrefSize()); + double fFactorX(1.0); + double fFactorY(1.0); + + if(MapUnit::MapPixel == GetPrefMapMode().GetMapUnit()) + { + aBitmapSize = Application::GetDefaultDevice()->PixelToLogic(aBitmapSize, aMapMode100thmm); + } + else + { + aBitmapSize = OutputDevice::LogicToLogic(aBitmapSize, GetPrefMapMode(), aMapMode100thmm); + } + + const double fDivX(aBitmapSize.Width() - fLeftCrop - fRightCrop); + const double fDivY(aBitmapSize.Height() - fTopCrop - fBottomCrop); + + if(!basegfx::fTools::equalZero(fDivX)) + { + fFactorX = fabs(fWidth) / fDivX; + } + + if(!basegfx::fTools::equalZero(fDivY)) + { + fFactorY = fabs(fHeight) / fDivY; + } + + return basegfx::B2DVector(fFactorX,fFactorY); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/GraphicObject2.cxx b/vcl/source/graphic/GraphicObject2.cxx new file mode 100644 index 000000000..dc60db55d --- /dev/null +++ b/vcl/source/graphic/GraphicObject2.cxx @@ -0,0 +1,506 @@ +/* -*- 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 <sal/config.h> + +#include <tools/gen.hxx> +#include <vcl/outdev.hxx> +#include <vcl/alpha.hxx> +#include <vcl/virdev.hxx> +#include <vcl/GraphicObject.hxx> +#include <memory> + +struct ImplTileInfo +{ + ImplTileInfo() : aTileTopLeft(), aNextTileTopLeft(), aTileSizePixel(), nTilesEmptyX(0), nTilesEmptyY(0) {} + + Point aTileTopLeft; // top, left position of the rendered tile + Point aNextTileTopLeft; // top, left position for next recursion + // level's tile + Size aTileSizePixel; // size of the generated tile (might + // differ from + // aNextTileTopLeft-aTileTopLeft, because + // this is nExponent*prevTileSize. The + // generated tile is always nExponent + // times the previous tile, such that it + // can be used in the next stage. The + // required area coverage is often + // less. The extraneous area covered is + // later overwritten by the next stage) + int nTilesEmptyX; // number of original tiles empty right of + // this tile. This counts from + // aNextTileTopLeft, i.e. the additional + // area covered by aTileSizePixel is not + // considered here. This is for + // unification purposes, as the iterative + // calculation of the next level's empty + // tiles has to be based on this value. + int nTilesEmptyY; // as above, for Y +}; + + +bool GraphicObject::ImplRenderTempTile( VirtualDevice& rVDev, + int nNumTilesX, int nNumTilesY, + const Size& rTileSizePixel, + const GraphicAttr* pAttr ) +{ + // how many tiles to generate per recursion step + const int nExponent = 2; + + // determine MSB factor + int nMSBFactor( 1 ); + while( nNumTilesX / nMSBFactor != 0 || + nNumTilesY / nMSBFactor != 0 ) + { + nMSBFactor *= nExponent; + } + + // one less + if(nMSBFactor > 1) + { + nMSBFactor /= nExponent; + } + ImplTileInfo aTileInfo; + + // #105229# Switch off mapping (converting to logic and back to + // pixel might cause roundoff errors) + bool bOldMap( rVDev.IsMapModeEnabled() ); + rVDev.EnableMapMode( false ); + + bool bRet( ImplRenderTileRecursive( rVDev, nExponent, nMSBFactor, nNumTilesX, nNumTilesY, + nNumTilesX, nNumTilesY, rTileSizePixel, pAttr, aTileInfo ) ); + + rVDev.EnableMapMode( bOldMap ); + + return bRet; +} + +// define for debug drawings +//#define DBG_TEST + +// see header comment. this works similar to base conversion of a +// number, i.e. if the exponent is 10, then the number for every tile +// size is given by the decimal place of the corresponding decimal +// representation. +bool GraphicObject::ImplRenderTileRecursive( VirtualDevice& rVDev, int nExponent, int nMSBFactor, + int nNumOrigTilesX, int nNumOrigTilesY, + int nRemainderTilesX, int nRemainderTilesY, + const Size& rTileSizePixel, const GraphicAttr* pAttr, + ImplTileInfo& rTileInfo ) +{ + // gets loaded with our tile bitmap + std::unique_ptr<GraphicObject> xTmpGraphic; + GraphicObject* pTileGraphic; + + // stores a flag that renders the zero'th tile position + // (i.e. (0,0)+rCurrPos) only if we're at the bottom of the + // recursion stack. All other position already have that tile + // rendered, because the lower levels painted their generated tile + // there. + bool bNoFirstTileDraw( false ); + + // what's left when we're done with our tile size + const int nNewRemainderX( nRemainderTilesX % nMSBFactor ); + const int nNewRemainderY( nRemainderTilesY % nMSBFactor ); + + // gets filled out from the recursive call with info of what's + // been generated + ImplTileInfo aTileInfo; + + // check for recursion's end condition: LSB place reached? + if( nMSBFactor == 1 ) + { + pTileGraphic = this; + + // set initial tile size -> orig size + aTileInfo.aTileSizePixel = rTileSizePixel; + aTileInfo.nTilesEmptyX = nNumOrigTilesX; + aTileInfo.nTilesEmptyY = nNumOrigTilesY; + } + else if( ImplRenderTileRecursive( rVDev, nExponent, nMSBFactor/nExponent, + nNumOrigTilesX, nNumOrigTilesY, + nNewRemainderX, nNewRemainderY, + rTileSizePixel, pAttr, aTileInfo ) ) + { + // extract generated tile -> see comment on the first loop below + BitmapEx aTileBitmap( rVDev.GetBitmap( aTileInfo.aTileTopLeft, aTileInfo.aTileSizePixel ) ); + + xTmpGraphic.reset(new GraphicObject(aTileBitmap)); + pTileGraphic = xTmpGraphic.get(); + + // fill stripes left over from upstream levels: + + // x0000 + // 0 + // 0 + // 0 + // 0 + + // where x denotes the place filled by our recursive predecessors + + // check whether we have to fill stripes here. Although not + // obvious, there is one case where we can skip this step: if + // the previous recursion level (the one who filled our + // aTileInfo) had zero area to fill, then there are no white + // stripes left, naturally. This happens if the digit + // associated to that level has a zero, and can be checked via + // aTileTopLeft==aNextTileTopLeft. + if( aTileInfo.aTileTopLeft != aTileInfo.aNextTileTopLeft ) + { + // now fill one row from aTileInfo.aNextTileTopLeft.X() all + // the way to the right + // current output position while drawing + Point aCurrPos(aTileInfo.aNextTileTopLeft.X(), aTileInfo.aTileTopLeft.Y()); + for (int nX=0; nX < aTileInfo.nTilesEmptyX; nX += nMSBFactor) + { + if (!pTileGraphic->Draw(&rVDev, aCurrPos, aTileInfo.aTileSizePixel, pAttr)) + return false; + + aCurrPos.AdjustX(aTileInfo.aTileSizePixel.Width() ); + } + +#ifdef DBG_TEST +// rVDev.SetFillCOL_WHITE ); + rVDev.SetFillColor(); + rVDev.SetLineColor( Color( 255 * nExponent / nMSBFactor, 255 - 255 * nExponent / nMSBFactor, 128 - 255 * nExponent / nMSBFactor ) ); + rVDev.DrawEllipse( tools::Rectangle(aTileInfo.aNextTileTopLeft.X(), aTileInfo.aTileTopLeft.Y(), + aTileInfo.aNextTileTopLeft.X() - 1 + (aTileInfo.nTilesEmptyX/nMSBFactor)*aTileInfo.aTileSizePixel.Width(), + aTileInfo.aTileTopLeft.Y() + aTileInfo.aTileSizePixel.Height() - 1) ); +#endif + + // now fill one column from aTileInfo.aNextTileTopLeft.Y() all + // the way to the bottom + aCurrPos.setX( aTileInfo.aTileTopLeft.X() ); + aCurrPos.setY( aTileInfo.aNextTileTopLeft.Y() ); + for (int nY=0; nY < aTileInfo.nTilesEmptyY; nY += nMSBFactor) + { + if (!pTileGraphic->Draw(&rVDev, aCurrPos, aTileInfo.aTileSizePixel, pAttr)) + return false; + + aCurrPos.AdjustY(aTileInfo.aTileSizePixel.Height() ); + } + +#ifdef DBG_TEST + rVDev.DrawEllipse( tools::Rectangle(aTileInfo.aTileTopLeft.X(), aTileInfo.aNextTileTopLeft.Y(), + aTileInfo.aTileTopLeft.X() + aTileInfo.aTileSizePixel.Width() - 1, + aTileInfo.aNextTileTopLeft.Y() - 1 + (aTileInfo.nTilesEmptyY/nMSBFactor)*aTileInfo.aTileSizePixel.Height()) ); +#endif + } + else + { + // Thought that aTileInfo.aNextTileTopLeft tile has always + // been drawn already, but that's wrong: typically, + // _parts_ of that tile have been drawn, since the + // previous level generated the tile there. But when + // aTileInfo.aNextTileTopLeft!=aTileInfo.aTileTopLeft, the + // difference between these two values is missing in the + // lower right corner of this first tile. So, can do that + // only here. + bNoFirstTileDraw = true; + } + } + else + { + return false; + } + + // calc number of original tiles in our drawing area without + // remainder + nRemainderTilesX -= nNewRemainderX; + nRemainderTilesY -= nNewRemainderY; + + // fill tile info for calling method + rTileInfo.aTileTopLeft = aTileInfo.aNextTileTopLeft; + rTileInfo.aNextTileTopLeft = Point( rTileInfo.aTileTopLeft.X() + rTileSizePixel.Width()*nRemainderTilesX, + rTileInfo.aTileTopLeft.Y() + rTileSizePixel.Height()*nRemainderTilesY ); + rTileInfo.aTileSizePixel = Size( rTileSizePixel.Width()*nMSBFactor*nExponent, + rTileSizePixel.Height()*nMSBFactor*nExponent ); + rTileInfo.nTilesEmptyX = aTileInfo.nTilesEmptyX - nRemainderTilesX; + rTileInfo.nTilesEmptyY = aTileInfo.nTilesEmptyY - nRemainderTilesY; + + // init output position + Point aCurrPos = aTileInfo.aNextTileTopLeft; + + // fill our drawing area. Fill possibly more, to create the next + // bigger tile size -> see bitmap extraction above. This does no + // harm, since everything right or below our actual area is + // overdrawn by our caller. Just in case we're in the last level, + // we don't draw beyond the right or bottom border. + for (int nY=0; nY < aTileInfo.nTilesEmptyY && nY < nExponent*nMSBFactor; nY += nMSBFactor) + { + aCurrPos.setX( aTileInfo.aNextTileTopLeft.X() ); + + for (int nX=0; nX < aTileInfo.nTilesEmptyX && nX < nExponent*nMSBFactor; nX += nMSBFactor) + { + if( bNoFirstTileDraw ) + bNoFirstTileDraw = false; // don't draw first tile position + else if (!pTileGraphic->Draw(&rVDev, aCurrPos, aTileInfo.aTileSizePixel, pAttr)) + return false; + + aCurrPos.AdjustX(aTileInfo.aTileSizePixel.Width() ); + } + + aCurrPos.AdjustY(aTileInfo.aTileSizePixel.Height() ); + } + +#ifdef DBG_TEST +// rVDev.SetFillCOL_WHITE ); + rVDev.SetFillColor(); + rVDev.SetLineColor( Color( 255 * nExponent / nMSBFactor, 255 - 255 * nExponent / nMSBFactor, 128 - 255 * nExponent / nMSBFactor ) ); + rVDev.DrawRect( tools::Rectangle((rTileInfo.aTileTopLeft.X())*rTileSizePixel.Width(), + (rTileInfo.aTileTopLeft.Y())*rTileSizePixel.Height(), + (rTileInfo.aNextTileTopLeft.X())*rTileSizePixel.Width()-1, + (rTileInfo.aNextTileTopLeft.Y())*rTileSizePixel.Height()-1) ); +#endif + + return true; +} + +bool GraphicObject::ImplDrawTiled( OutputDevice* pOut, const tools::Rectangle& rArea, const Size& rSizePixel, + const Size& rOffset, const GraphicAttr* pAttr, int nTileCacheSize1D ) +{ + const MapMode aOutMapMode( pOut->GetMapMode() ); + const MapMode aMapMode( aOutMapMode.GetMapUnit(), Point(), aOutMapMode.GetScaleX(), aOutMapMode.GetScaleY() ); + bool bRet( false ); + + // #i42643# Casting to Int64, to avoid integer overflow for + // huge-DPI output devices + if( GetGraphic().GetType() == GraphicType::Bitmap && + static_cast<sal_Int64>(rSizePixel.Width()) * rSizePixel.Height() < + static_cast<sal_Int64>(nTileCacheSize1D)*nTileCacheSize1D ) + { + // First combine very small bitmaps into a larger tile + + + ScopedVclPtrInstance< VirtualDevice > aVDev; + const int nNumTilesInCacheX( (nTileCacheSize1D + rSizePixel.Width()-1) / rSizePixel.Width() ); + const int nNumTilesInCacheY( (nTileCacheSize1D + rSizePixel.Height()-1) / rSizePixel.Height() ); + + aVDev->SetOutputSizePixel( Size( nNumTilesInCacheX*rSizePixel.Width(), + nNumTilesInCacheY*rSizePixel.Height() ) ); + aVDev->SetMapMode( aMapMode ); + + // draw bitmap content + if( ImplRenderTempTile( *aVDev, nNumTilesInCacheX, + nNumTilesInCacheY, rSizePixel, pAttr ) ) + { + BitmapEx aTileBitmap( aVDev->GetBitmap( Point(0,0), aVDev->GetOutputSize() ) ); + + // draw alpha content, if any + if( IsTransparent() ) + { + GraphicObject aAlphaGraphic; + + if( GetGraphic().IsAlpha() ) + aAlphaGraphic.SetGraphic( GetGraphic().GetBitmapEx().GetAlpha().GetBitmap() ); + else + aAlphaGraphic.SetGraphic( GetGraphic().GetBitmapEx().GetMask() ); + + if( aAlphaGraphic.ImplRenderTempTile( *aVDev, nNumTilesInCacheX, + nNumTilesInCacheY, rSizePixel, pAttr ) ) + { + // Combine bitmap and alpha/mask + if( GetGraphic().IsAlpha() ) + aTileBitmap = BitmapEx( aTileBitmap.GetBitmap(), + AlphaMask( aVDev->GetBitmap( Point(0,0), aVDev->GetOutputSize() ) ) ); + else + aTileBitmap = BitmapEx( aTileBitmap.GetBitmap(), + aVDev->GetBitmap( Point(0,0), aVDev->GetOutputSize() ).CreateMask( COL_WHITE ) ); + } + } + + // paint generated tile + GraphicObject aTmpGraphic( aTileBitmap ); + bRet = aTmpGraphic.ImplDrawTiled( pOut, rArea, + aTileBitmap.GetSizePixel(), + rOffset, pAttr, nTileCacheSize1D ); + } + } + else + { + const Size aOutOffset( pOut->LogicToPixel( rOffset, aOutMapMode ) ); + const tools::Rectangle aOutArea( pOut->LogicToPixel( rArea, aOutMapMode ) ); + + // number of invisible (because out-of-area) tiles + int nInvisibleTilesX; + int nInvisibleTilesY; + + // round towards -infty for negative offset + if( aOutOffset.Width() < 0 ) + nInvisibleTilesX = (aOutOffset.Width() - rSizePixel.Width() + 1) / rSizePixel.Width(); + else + nInvisibleTilesX = aOutOffset.Width() / rSizePixel.Width(); + + // round towards -infty for negative offset + if( aOutOffset.Height() < 0 ) + nInvisibleTilesY = (aOutOffset.Height() - rSizePixel.Height() + 1) / rSizePixel.Height(); + else + nInvisibleTilesY = aOutOffset.Height() / rSizePixel.Height(); + + // origin from where to 'virtually' start drawing in pixel + const Point aOutOrigin( pOut->LogicToPixel( Point( rArea.Left() - rOffset.Width(), + rArea.Top() - rOffset.Height() ) ) ); + // position in pixel from where to really start output + const Point aOutStart( aOutOrigin.X() + nInvisibleTilesX*rSizePixel.Width(), + aOutOrigin.Y() + nInvisibleTilesY*rSizePixel.Height() ); + + pOut->Push( PushFlags::CLIPREGION ); + pOut->IntersectClipRegion( rArea ); + + // Paint all tiles + + + bRet = ImplDrawTiled( *pOut, aOutStart, + (aOutArea.GetWidth() + aOutArea.Left() - aOutStart.X() + rSizePixel.Width() - 1) / rSizePixel.Width(), + (aOutArea.GetHeight() + aOutArea.Top() - aOutStart.Y() + rSizePixel.Height() - 1) / rSizePixel.Height(), + rSizePixel, pAttr ); + + pOut->Pop(); + } + + return bRet; +} + +bool GraphicObject::ImplDrawTiled( OutputDevice& rOut, const Point& rPosPixel, + int nNumTilesX, int nNumTilesY, + const Size& rTileSizePixel, const GraphicAttr* pAttr ) +{ + Point aCurrPos( rPosPixel ); + Size aTileSizeLogic( rOut.PixelToLogic( rTileSizePixel ) ); + int nX, nY; + + // #107607# Use logical coordinates for metafile playing, too + bool bDrawInPixel( rOut.GetConnectMetaFile() == nullptr && GraphicType::Bitmap == GetType() ); + bool bRet = false; + + // #105229# Switch off mapping (converting to logic and back to + // pixel might cause roundoff errors) + bool bOldMap( rOut.IsMapModeEnabled() ); + + if( bDrawInPixel ) + rOut.EnableMapMode( false ); + + for( nY=0; nY < nNumTilesY; ++nY ) + { + aCurrPos.setX( rPosPixel.X() ); + + for( nX=0; nX < nNumTilesX; ++nX ) + { + // #105229# work with pixel coordinates here, mapping is disabled! + // #104004# don't disable mapping for metafile recordings + // #108412# don't quit the loop if one draw fails + + // update return value. This method should return true, if + // at least one of the looped Draws succeeded. + bRet |= Draw( &rOut, + bDrawInPixel ? aCurrPos : rOut.PixelToLogic( aCurrPos ), + bDrawInPixel ? rTileSizePixel : aTileSizeLogic, + pAttr ); + + aCurrPos.AdjustX(rTileSizePixel.Width() ); + } + + aCurrPos.AdjustY(rTileSizePixel.Height() ); + } + + if( bDrawInPixel ) + rOut.EnableMapMode( bOldMap ); + + return bRet; +} + +void GraphicObject::ImplTransformBitmap( BitmapEx& rBmpEx, + const GraphicAttr& rAttr, + const Size& rCropLeftTop, + const Size& rCropRightBottom, + const tools::Rectangle& rCropRect, + const Size& rDstSize, + bool bEnlarge ) const +{ + // #107947# Extracted from svdograf.cxx + + // #104115# Crop the bitmap + if( rAttr.IsCropped() ) + { + rBmpEx.Crop( rCropRect ); + + // #104115# Negative crop sizes mean: enlarge bitmap and pad + if( bEnlarge && ( + rCropLeftTop.Width() < 0 || + rCropLeftTop.Height() < 0 || + rCropRightBottom.Width() < 0 || + rCropRightBottom.Height() < 0 ) ) + { + Size aBmpSize( rBmpEx.GetSizePixel() ); + sal_Int32 nPadLeft( rCropLeftTop.Width() < 0 ? -rCropLeftTop.Width() : 0 ); + sal_Int32 nPadTop( rCropLeftTop.Height() < 0 ? -rCropLeftTop.Height() : 0 ); + sal_Int32 nPadTotalWidth( aBmpSize.Width() + nPadLeft + (rCropRightBottom.Width() < 0 ? -rCropRightBottom.Width() : 0) ); + sal_Int32 nPadTotalHeight( aBmpSize.Height() + nPadTop + (rCropRightBottom.Height() < 0 ? -rCropRightBottom.Height() : 0) ); + + BitmapEx aBmpEx2; + + if( rBmpEx.IsTransparent() ) + { + if( rBmpEx.IsAlpha() ) + aBmpEx2 = BitmapEx( rBmpEx.GetBitmap(), rBmpEx.GetAlpha() ); + else + aBmpEx2 = BitmapEx( rBmpEx.GetBitmap(), rBmpEx.GetMask() ); + } + else + { + // #104115# Generate mask bitmap and init to zero + Bitmap aMask( aBmpSize, 1 ); + aMask.Erase( Color(0,0,0) ); + + // #104115# Always generate transparent bitmap, we need the border transparent + aBmpEx2 = BitmapEx( rBmpEx.GetBitmap(), aMask ); + + // #104115# Add opaque mask to source bitmap, otherwise the destination remains transparent + rBmpEx = aBmpEx2; + } + + aBmpEx2.Scale(Size(nPadTotalWidth, nPadTotalHeight)); + aBmpEx2.Erase( Color(0xFF,0,0,0) ); + aBmpEx2.CopyPixel( tools::Rectangle( Point(nPadLeft, nPadTop), aBmpSize ), tools::Rectangle( Point(0, 0), aBmpSize ), &rBmpEx ); + rBmpEx = aBmpEx2; + } + } + + const Size aSizePixel( rBmpEx.GetSizePixel() ); + + if( rAttr.GetRotation() == 0 || IsAnimated() ) + return; + + if( !(aSizePixel.Width() && aSizePixel.Height() && rDstSize.Width() && rDstSize.Height()) ) + return; + + double fSrcWH = static_cast<double>(aSizePixel.Width()) / aSizePixel.Height(); + double fDstWH = static_cast<double>(rDstSize.Width()) / rDstSize.Height(); + double fScaleX = 1.0, fScaleY = 1.0; + + // always choose scaling to shrink bitmap + if( fSrcWH < fDstWH ) + fScaleY = aSizePixel.Width() / ( fDstWH * aSizePixel.Height() ); + else + fScaleX = fDstWH * aSizePixel.Height() / aSizePixel.Width(); + + rBmpEx.Scale( fScaleX, fScaleY ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/GraphicReader.cxx b/vcl/source/graphic/GraphicReader.cxx new file mode 100644 index 000000000..9137ebd8a --- /dev/null +++ b/vcl/source/graphic/GraphicReader.cxx @@ -0,0 +1,29 @@ +/* -*- 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 <graphic/GraphicReader.hxx> + +#include <sal/config.h> +#include <sal/log.hxx> + +GraphicReader::GraphicReader() {} + +GraphicReader::~GraphicReader() {} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/Manager.cxx b/vcl/source/graphic/Manager.cxx new file mode 100644 index 000000000..65e81fc1e --- /dev/null +++ b/vcl/source/graphic/Manager.cxx @@ -0,0 +1,247 @@ +/* -*- 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 <graphic/Manager.hxx> +#include <impgraph.hxx> +#include <sal/log.hxx> + +#include <officecfg/Office/Common.hxx> +#include <unotools/configmgr.hxx> + +using namespace css; + +namespace vcl::graphic +{ +namespace +{ +void setupConfigurationValuesIfPossible(sal_Int64& rMemoryLimit, + std::chrono::seconds& rAllowedIdleTime, bool& bSwapEnabled) +{ + if (utl::ConfigManager::IsFuzzing()) + return; + + try + { + using officecfg::Office::Common::Cache; + + rMemoryLimit = Cache::GraphicManager::GraphicMemoryLimit::get(); + rAllowedIdleTime + = std::chrono::seconds(Cache::GraphicManager::GraphicAllowedIdleTime::get()); + bSwapEnabled = Cache::GraphicManager::GraphicSwappingEnabled::get(); + } + catch (...) + { + } +} +} + +Manager& Manager::get() +{ + static Manager gStaticManager; + return gStaticManager; +} + +Manager::Manager() + : mnAllowedIdleTime(10) + , mbSwapEnabled(true) + , mnMemoryLimit(300000000) + , mnUsedSize(0) + , maSwapOutTimer("graphic::Manager maSwapOutTimer") +{ + setupConfigurationValuesIfPossible(mnMemoryLimit, mnAllowedIdleTime, mbSwapEnabled); + + if (mbSwapEnabled) + { + maSwapOutTimer.SetInvokeHandler(LINK(this, Manager, SwapOutTimerHandler)); + maSwapOutTimer.SetTimeout(10000); + maSwapOutTimer.SetDebugName("graphic::Manager maSwapOutTimer"); + maSwapOutTimer.Start(); + } +} + +void Manager::reduceGraphicMemory() +{ + if (!mbSwapEnabled) + return; + + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + // make a copy of m_pImpGraphicList because if we swap out a svg, the svg + // filter may create more temp Graphics which are auto-added to + // m_pImpGraphicList invalidating a loop over m_pImpGraphicList, e.g. + // reexport of tdf118346-1.odg + o3tl::sorted_vector<ImpGraphic*> aImpGraphicList = m_pImpGraphicList; + for (ImpGraphic* pEachImpGraphic : aImpGraphicList) + { + if (mnUsedSize < mnMemoryLimit * 0.7) + return; + + sal_Int64 nCurrentGraphicSize = getGraphicSizeBytes(pEachImpGraphic); + if (!pEachImpGraphic->isSwappedOut() && nCurrentGraphicSize > 1000000) + { + if (!pEachImpGraphic->mpContext) + { + auto aCurrent = std::chrono::high_resolution_clock::now(); + auto aDeltaTime = aCurrent - pEachImpGraphic->maLastUsed; + auto aSeconds = std::chrono::duration_cast<std::chrono::seconds>(aDeltaTime); + + if (aSeconds > mnAllowedIdleTime) + pEachImpGraphic->swapOut(); + } + } + } +} + +sal_Int64 Manager::getGraphicSizeBytes(const ImpGraphic* pImpGraphic) +{ + if (!pImpGraphic->isAvailable()) + return 0; + return pImpGraphic->ImplGetSizeBytes(); +} + +IMPL_LINK(Manager, SwapOutTimerHandler, Timer*, pTimer, void) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + pTimer->Stop(); + reduceGraphicMemory(); + pTimer->Start(); +} + +void Manager::registerGraphic(const std::shared_ptr<ImpGraphic>& pImpGraphic, + OUString const& /*rsContext*/) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + // make some space first + if (mnUsedSize > mnMemoryLimit) + reduceGraphicMemory(); + + // Insert and update the used size (bytes) + mnUsedSize += getGraphicSizeBytes(pImpGraphic.get()); + m_pImpGraphicList.insert(pImpGraphic.get()); + + // calculate size of the graphic set + sal_Int64 calculatedSize = 0; + for (ImpGraphic* pEachImpGraphic : m_pImpGraphicList) + { + if (!pEachImpGraphic->isSwappedOut()) + { + calculatedSize += getGraphicSizeBytes(pEachImpGraphic); + } + } + + if (calculatedSize != mnUsedSize) + { + SAL_INFO_IF(calculatedSize != mnUsedSize, "vcl.gdi", + "Calculated size mismatch. Variable size is '" + << mnUsedSize << "' but calculated size is '" << calculatedSize << "'"); + mnUsedSize = calculatedSize; + } +} + +void Manager::unregisterGraphic(ImpGraphic* pImpGraphic) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + mnUsedSize -= getGraphicSizeBytes(pImpGraphic); + m_pImpGraphicList.erase(pImpGraphic); +} + +std::shared_ptr<ImpGraphic> Manager::copy(std::shared_ptr<ImpGraphic> const& rImpGraphicPtr) +{ + auto pReturn = std::make_shared<ImpGraphic>(*rImpGraphicPtr); + registerGraphic(pReturn, "Copy"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance() +{ + auto pReturn = std::make_shared<ImpGraphic>(); + registerGraphic(pReturn, "Empty"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const Bitmap& rBitmap) +{ + auto pReturn = std::make_shared<ImpGraphic>(rBitmap); + registerGraphic(pReturn, "Bitmap"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const BitmapEx& rBitmapEx) +{ + auto pReturn = std::make_shared<ImpGraphic>(rBitmapEx); + registerGraphic(pReturn, "BitmapEx"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const Animation& rAnimation) +{ + auto pReturn = std::make_shared<ImpGraphic>(rAnimation); + registerGraphic(pReturn, "Animation"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> +Manager::newInstance(const std::shared_ptr<VectorGraphicData>& rVectorGraphicDataPtr) +{ + auto pReturn = std::make_shared<ImpGraphic>(rVectorGraphicDataPtr); + registerGraphic(pReturn, "VectorGraphic"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const GDIMetaFile& rMetaFile) +{ + auto pReturn = std::make_shared<ImpGraphic>(rMetaFile); + registerGraphic(pReturn, "Metafile"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const GraphicExternalLink& rGraphicLink) +{ + auto pReturn = std::make_shared<ImpGraphic>(rGraphicLink); + registerGraphic(pReturn, "GraphicExternalLink"); + return pReturn; +} + +void Manager::swappedIn(const ImpGraphic* pImpGraphic) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + mnUsedSize += getGraphicSizeBytes(pImpGraphic); +} + +void Manager::swappedOut(const ImpGraphic* pImpGraphic) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + mnUsedSize -= getGraphicSizeBytes(pImpGraphic); +} + +void Manager::changeExisting(const ImpGraphic* pImpGraphic, sal_Int64 nOldSizeBytes) +{ + std::scoped_lock<std::recursive_mutex> aGuard(maMutex); + + mnUsedSize -= nOldSizeBytes; + mnUsedSize += getGraphicSizeBytes(pImpGraphic); +} +} // end vcl::graphic + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/UnoGraphic.cxx b/vcl/source/graphic/UnoGraphic.cxx new file mode 100644 index 000000000..4fddaebd6 --- /dev/null +++ b/vcl/source/graphic/UnoGraphic.cxx @@ -0,0 +1,192 @@ +/* -*- 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 <graphic/UnoGraphic.hxx> + +#include <tools/stream.hxx> +#include <vcl/svapp.hxx> +#include <com/sun/star/graphic/GraphicType.hpp> +#include <com/sun/star/graphic/XGraphicTransformer.hpp> +#include <vcl/dibtools.hxx> +#include <comphelper/servicehelper.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <cppuhelper/queryinterface.hxx> +#include <cppuhelper/typeprovider.hxx> + +using namespace com::sun::star; + +namespace unographic { + +Graphic::Graphic() : + maGraphic() +{ +} + +Graphic::~Graphic() throw() +{ +} + +void Graphic::init( const ::Graphic& rGraphic ) + throw() +{ + maGraphic = rGraphic; + unographic::GraphicDescriptor::init(maGraphic); +} + +uno::Any SAL_CALL Graphic::queryAggregation( const uno::Type& rType ) +{ + uno::Any aAny; + if( rType == cppu::UnoType<graphic::XGraphic>::get()) + aAny <<= uno::Reference< graphic::XGraphic >( this ); + else if( rType == cppu::UnoType<awt::XBitmap>::get()) + aAny <<= uno::Reference< awt::XBitmap >( this ); + else if( rType == cppu::UnoType<lang::XUnoTunnel>::get()) + aAny <<= uno::Reference< lang::XUnoTunnel >(this); + else + aAny = ::unographic::GraphicDescriptor::queryAggregation( rType ); + + return aAny; +} + +uno::Any SAL_CALL Graphic::queryInterface( const uno::Type & rType ) +{ + css::uno::Any aReturn = ::unographic::GraphicDescriptor::queryInterface( rType ); + if ( !aReturn.hasValue() ) + aReturn = ::cppu::queryInterface ( rType, static_cast< graphic::XGraphicTransformer*>( this ) ); + return aReturn; +} + +void SAL_CALL Graphic::acquire() + throw() +{ + unographic::GraphicDescriptor::acquire(); +} + +void SAL_CALL Graphic::release() throw() +{ + unographic::GraphicDescriptor::release(); +} + +OUString SAL_CALL Graphic::getImplementationName() +{ + return "com.sun.star.comp.graphic.Graphic"; +} + +sal_Bool SAL_CALL Graphic::supportsService( const OUString& rServiceName ) +{ + return cppu::supportsService( this, rServiceName ); +} + +uno::Sequence< OUString > SAL_CALL Graphic::getSupportedServiceNames() +{ + uno::Sequence< OUString > aRet( ::unographic::GraphicDescriptor::getSupportedServiceNames() ); + uno::Sequence< OUString > aNew { "com.sun.star.graphic.Graphic" }; + sal_Int32 nOldCount = aRet.getLength(); + + aRet.realloc( nOldCount + aNew.getLength() ); + + std::copy(aNew.begin(), aNew.end(), std::next(aRet.begin(), nOldCount)); + + return aRet; +} + +uno::Sequence< uno::Type > SAL_CALL Graphic::getTypes() +{ + return cppu::OTypeCollection( + cppu::UnoType<graphic::XGraphic>::get(), + cppu::UnoType<awt::XBitmap>::get(), + ::unographic::GraphicDescriptor::getTypes() + ).getTypes(); +} + +uno::Sequence< sal_Int8 > SAL_CALL Graphic::getImplementationId() +{ + return css::uno::Sequence<sal_Int8>(); +} + +sal_Int8 SAL_CALL Graphic::getType() +{ + sal_Int8 cRet = graphic::GraphicType::EMPTY; + + if (!maGraphic.IsNone()) + { + cRet = (maGraphic.GetType() == ::GraphicType::Bitmap) ? graphic::GraphicType::PIXEL + : graphic::GraphicType::VECTOR; + } + + return cRet; +} + +// XBitmap + +awt::Size SAL_CALL Graphic::getSize() +{ + SolarMutexGuard aGuard; + + Size aVclSize; + if (!maGraphic.IsNone()) + { + aVclSize = maGraphic.GetSizePixel(); + } + return awt::Size(aVclSize.Width(), aVclSize.Height()); +} + +uno::Sequence<sal_Int8> SAL_CALL Graphic::getDIB() +{ + SolarMutexGuard aGuard; + + if (!maGraphic.IsNone()) + { + SvMemoryStream aMemoryStream; + + WriteDIB(maGraphic.GetBitmapEx().GetBitmap(), aMemoryStream, false, true); + return css::uno::Sequence<sal_Int8>(static_cast<sal_Int8 const *>(aMemoryStream.GetData()), aMemoryStream.Tell()); + } + else + { + return uno::Sequence<sal_Int8>(); + } +} + +uno::Sequence<sal_Int8> SAL_CALL Graphic::getMaskDIB() +{ + SolarMutexGuard aGuard; + + if (!maGraphic.IsNone()) + { + SvMemoryStream aMemoryStream; + + WriteDIB(maGraphic.GetBitmapEx().GetMask(), aMemoryStream, false, true); + return css::uno::Sequence<sal_Int8>( static_cast<sal_Int8 const *>(aMemoryStream.GetData()), aMemoryStream.Tell() ); + } + else + { + return uno::Sequence<sal_Int8>(); + } +} + +sal_Int64 SAL_CALL Graphic::getSomething( const uno::Sequence< sal_Int8 >& rId ) +{ + return( ( isUnoTunnelId<::Graphic>(rId) ) ? + reinterpret_cast<sal_Int64>(&maGraphic) : 0 ); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/UnoGraphicDescriptor.cxx b/vcl/source/graphic/UnoGraphicDescriptor.cxx new file mode 100644 index 000000000..6f65828fe --- /dev/null +++ b/vcl/source/graphic/UnoGraphicDescriptor.cxx @@ -0,0 +1,424 @@ +/* -*- 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 <graphic/UnoGraphicDescriptor.hxx> + +#include <unotools/ucbstreamhelper.hxx> +#include <vcl/graphicfilter.hxx> +#include <cppuhelper/supportsservice.hxx> + +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <com/sun/star/awt/Size.hpp> +#include <com/sun/star/graphic/GraphicType.hpp> +#include <com/sun/star/io/XInputStream.hpp> + +#include <vcl/outdev.hxx> +#include <vcl/graph.hxx> +#include <vcl/svapp.hxx> +#include <memory> + +namespace { + +enum class UnoGraphicProperty +{ + GraphicType = 1 + , MimeType = 2 + , SizePixel = 3 + , Size100thMM = 4 + , BitsPerPixel = 5 + , Transparent = 6 + , Alpha = 7 + , Animated = 8 + , Linked = 9 + , OriginURL = 10 +}; + +} + +using namespace ::com::sun::star; + +namespace unographic { + + +GraphicDescriptor::GraphicDescriptor() : + ::comphelper::PropertySetHelper( createPropertySetInfo() ), + mpGraphic( nullptr ), + meType( GraphicType::NONE ), + mnBitsPerPixel ( 0 ), + mbTransparent ( false ) +{ +} + +GraphicDescriptor::~GraphicDescriptor() + throw() +{ +} + +void GraphicDescriptor::init( const ::Graphic& rGraphic ) +{ + mpGraphic = &rGraphic; +} + +void GraphicDescriptor::init( const OUString& rURL ) +{ + std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ )); + + if( pIStm ) + implCreate( *pIStm, &rURL ); +} + +void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL ) +{ + std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm )); + + if( pIStm ) + implCreate( *pIStm, &rURL ); +} + +void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL ) +{ + OUString aURL; + if( pURL ) + aURL = *pURL; + ::GraphicDescriptor aDescriptor( rIStm, &aURL ); + + mpGraphic = nullptr; + maMimeType.clear(); + meType = GraphicType::NONE; + mnBitsPerPixel = 0; + mbTransparent = false; + + if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) ) + return; + + const char* pMimeType = nullptr; + sal_uInt8 cType = graphic::GraphicType::EMPTY; + + switch( aDescriptor.GetFileFormat() ) + { + case GraphicFileFormat::BMP: pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::GIF: pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::JPG: pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PCD: pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PCX: pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PNG: pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::TIF: pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::XBM: pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::XPM: pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PBM: pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PGM: pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PPM: pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::RAS: pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::TGA: pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PSD: pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break; + + case GraphicFileFormat::EPS: pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::DXF: pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::MET: pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::PCT: pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::SVM: pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::WMF: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::EMF: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::SVG: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break; + + default: + break; + } + + if( graphic::GraphicType::EMPTY != cType ) + { + meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile ); + maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US ); + maSizePixel = aDescriptor.GetSizePixel(); + maSize100thMM = aDescriptor.GetSize_100TH_MM(); + mnBitsPerPixel = aDescriptor.GetBitsPerPixel(); + mbTransparent = ( graphic::GraphicType::VECTOR == cType ); + } +} + + +uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType ) +{ + uno::Any aAny; + + if( rType == cppu::UnoType<lang::XServiceInfo>::get()) + aAny <<= uno::Reference< lang::XServiceInfo >(this); + else if( rType == cppu::UnoType<lang::XTypeProvider>::get()) + aAny <<= uno::Reference< lang::XTypeProvider >(this); + else if( rType == cppu::UnoType<beans::XPropertySet>::get()) + aAny <<= uno::Reference< beans::XPropertySet >(this); + else if( rType == cppu::UnoType<beans::XPropertyState>::get()) + aAny <<= uno::Reference< beans::XPropertyState >(this); + else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get()) + aAny <<= uno::Reference< beans::XMultiPropertySet >(this); + else + aAny = OWeakAggObject::queryAggregation( rType ); + + return aAny; +} + + +uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType ) +{ + return OWeakAggObject::queryInterface( rType ); +} + + +void SAL_CALL GraphicDescriptor::acquire() + throw() +{ + OWeakAggObject::acquire(); +} + + +void SAL_CALL GraphicDescriptor::release() + throw() +{ + OWeakAggObject::release(); +} + + +OUString SAL_CALL GraphicDescriptor::getImplementationName() +{ + return "com.sun.star.comp.graphic.GraphicDescriptor"; +} + +sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + + +uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames() +{ + return { "com.sun.star.graphic.GraphicDescriptor" }; +} + + +uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes() +{ + static const uno::Sequence< uno::Type > aTypes { + cppu::UnoType<uno::XAggregation>::get(), + cppu::UnoType<lang::XServiceInfo>::get(), + cppu::UnoType<lang::XTypeProvider>::get(), + cppu::UnoType<beans::XPropertySet>::get(), + cppu::UnoType<beans::XPropertyState>::get(), + cppu::UnoType<beans::XMultiPropertySet>::get() }; + return aTypes; +} + +uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId() +{ + return css::uno::Sequence<sal_Int8>(); +} + + +rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo() +{ + static ::comphelper::PropertyMapEntry const aEntries[] = + { + { OUString( "GraphicType" ), static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "MimeType" ), static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "SizePixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Size100thMM" ), static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "BitsPerPixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_uInt8 >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Transparent" ), static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Alpha" ), static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Animated" ), static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString("Linked"), sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString("OriginURL"), sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 }, + + { OUString(), 0, css::uno::Type(), 0, 0 } + }; + + return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) ); +} + + +void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ ) +{ + // we only have readonly attributes +} + + +void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues ) +{ + SolarMutexGuard aGuard; + + while( *ppEntries ) + { + UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle ); + switch( theProperty ) + { + case UnoGraphicProperty::GraphicType: + { + const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType ); + + *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL : + ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR : + graphic::GraphicType::EMPTY ) ); + } + break; + + case UnoGraphicProperty::MimeType: + { + OUString aMimeType; + + if( mpGraphic ) + { + if( mpGraphic->IsGfxLink() ) + { + const char* pMimeType; + + switch( mpGraphic->GetGfxLink().GetType() ) + { + case GfxLinkType::NativeGif: pMimeType = MIMETYPE_GIF; break; + + // #i15508# added BMP type for better exports (checked, works) + case GfxLinkType::NativeBmp: pMimeType = MIMETYPE_BMP; break; + + case GfxLinkType::NativeJpg: pMimeType = MIMETYPE_JPG; break; + case GfxLinkType::NativePng: pMimeType = MIMETYPE_PNG; break; + case GfxLinkType::NativeWmf: pMimeType = MIMETYPE_WMF; break; + case GfxLinkType::NativeMet: pMimeType = MIMETYPE_MET; break; + case GfxLinkType::NativePct: pMimeType = MIMETYPE_PCT; break; + + // added Svg mimetype support + case GfxLinkType::NativeSvg: pMimeType = MIMETYPE_SVG; break; + case GfxLinkType::NativePdf: pMimeType = MIMETYPE_PDF; break; + + default: + pMimeType = nullptr; + break; + } + + if( pMimeType ) + aMimeType = OUString::createFromAscii( pMimeType ); + } + + if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) ) + aMimeType = MIMETYPE_VCLGRAPHIC; + } + else + aMimeType = maMimeType; + + *pValues <<= aMimeType; + } + break; + + case UnoGraphicProperty::SizePixel: + { + awt::Size aAWTSize( 0, 0 ); + + if( mpGraphic ) + { + if( mpGraphic->GetType() == GraphicType::Bitmap ) + { + const Size aSizePix( mpGraphic->GetSizePixel() ); + aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() ); + } + } + else + aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() ); + + *pValues <<= aAWTSize; + } + break; + + case UnoGraphicProperty::Size100thMM: + { + awt::Size aAWTSize( 0, 0 ); + + if( mpGraphic ) + { + if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel ) + { + const Size aSizeLog( OutputDevice::LogicToLogic( + mpGraphic->GetPrefSize(), + mpGraphic->GetPrefMapMode(), + MapMode(MapUnit::Map100thMM)) ); + aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() ); + } + } + else + aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() ); + + *pValues <<= aAWTSize; + } + break; + + case UnoGraphicProperty::BitsPerPixel: + { + sal_uInt16 nBitsPerPixel = 0; + + if( mpGraphic ) + { + if( mpGraphic->GetType() == GraphicType::Bitmap ) + nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount(); + } + else + nBitsPerPixel = mnBitsPerPixel; + + *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel); + } + break; + + case UnoGraphicProperty::Transparent: + { + *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent; + } + break; + + case UnoGraphicProperty::Alpha: + { + *pValues <<= mpGraphic && mpGraphic->IsAlpha(); + } + break; + + case UnoGraphicProperty::Animated: + { + *pValues <<= mpGraphic && mpGraphic->IsAnimated(); + } + break; + + case UnoGraphicProperty::Linked: + { + *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty(); + } + break; + + case UnoGraphicProperty::OriginURL: + { + OUString aOriginURL; + if (mpGraphic) + aOriginURL = mpGraphic->getOriginURL(); + + *pValues <<= aOriginURL; + } + break; + } + + ++ppEntries; + ++pValues; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/UnoGraphicObject.cxx b/vcl/source/graphic/UnoGraphicObject.cxx new file mode 100644 index 000000000..bab6bffe5 --- /dev/null +++ b/vcl/source/graphic/UnoGraphicObject.cxx @@ -0,0 +1,100 @@ +/* -*- 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 <memory> +#include <cppuhelper/implbase.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <com/sun/star/graphic/XGraphicObject.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <vcl/GraphicObject.hxx> + +using namespace css; + +namespace { + +typedef ::cppu::WeakImplHelper<graphic::XGraphicObject, css::lang::XServiceInfo> GraphicObject_BASE; + + // Simple uno wrapper around the GraphicObject class to allow basic + // access. ( and solves a horrible cyclic link problem between + // goodies/toolkit/extensions ) +class GraphicObjectImpl : public GraphicObject_BASE +{ + osl::Mutex m_aMutex; + std::unique_ptr<GraphicObject> mpGraphicObject; + +public: + /// @throws uno::RuntimeException + explicit GraphicObjectImpl(uno::Sequence<uno::Any> const & rArgs); + + // XGraphicObject + virtual uno::Reference<graphic::XGraphic> SAL_CALL getGraphic() override; + virtual void SAL_CALL setGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic) override; + + virtual OUString SAL_CALL getImplementationName() override + { + return "com.sun.star.graphic.GraphicObject"; + } + + virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override + { + return cppu::supportsService(this, ServiceName); + } + + virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override + { + return uno::Sequence<OUString> { "com.sun.star.graphic.GraphicObject" }; + } +}; + +GraphicObjectImpl::GraphicObjectImpl(const uno::Sequence<uno::Any>& /*rArgs*/) +{ + mpGraphicObject.reset(new GraphicObject()); +} + +uno::Reference<graphic::XGraphic> SAL_CALL GraphicObjectImpl::getGraphic() +{ + osl::MutexGuard aGuard(m_aMutex); + + if (!mpGraphicObject) + throw uno::RuntimeException(); + return mpGraphicObject->GetGraphic().GetXGraphic(); +} + +void SAL_CALL GraphicObjectImpl::setGraphic(uno::Reference<graphic::XGraphic> const & rxGraphic) +{ + osl::MutexGuard aGuard(m_aMutex); + + if (!mpGraphicObject) + throw uno::RuntimeException(); + Graphic aGraphic(rxGraphic); + mpGraphicObject->SetGraphic(aGraphic); +} + +} // end anonymous namespace + +extern "C" SAL_DLLPUBLIC_EXPORT +css::uno::XInterface* com_sun_star_graphic_GraphicObject_get_implementation( + SAL_UNUSED_PARAMETER uno::XComponentContext*, + uno::Sequence<uno::Any> const & rArguments) +{ + return cppu::acquire(new GraphicObjectImpl(rArguments)); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/UnoGraphicProvider.cxx b/vcl/source/graphic/UnoGraphicProvider.cxx new file mode 100644 index 000000000..d8c6e774d --- /dev/null +++ b/vcl/source/graphic/UnoGraphicProvider.cxx @@ -0,0 +1,837 @@ +/* -*- 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 <vcl/svapp.hxx> +#include <vcl/image.hxx> +#include <vcl/metaact.hxx> +#include <imagerepository.hxx> +#include <tools/fract.hxx> +#include <unotools/ucbstreamhelper.hxx> +#include <vcl/graphicfilter.hxx> +#include <vcl/stdtext.hxx> +#include <vcl/wmfexternal.hxx> +#include <vcl/virdev.hxx> +#include <com/sun/star/awt/XBitmap.hpp> +#include <com/sun/star/graphic/XGraphicProvider2.hpp> +#include <com/sun/star/io/XStream.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/text/GraphicCrop.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <comphelper/fileformat.h> +#include <comphelper/servicehelper.hxx> +#include <cppuhelper/implbase.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <sal/log.hxx> + +#include <graphic/UnoGraphicDescriptor.hxx> +#include <graphic/UnoGraphic.hxx> +#include <rtl/ref.hxx> +#include <vcl/dibtools.hxx> +#include <comphelper/sequence.hxx> +#include <memory> + +using namespace com::sun::star; + +namespace { + +class GraphicProvider : public ::cppu::WeakImplHelper< css::graphic::XGraphicProvider2, + css::lang::XServiceInfo > +{ +public: + + GraphicProvider(); + +protected: + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName() override; + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override; + + // XTypeProvider + virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( ) override; + virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override; + + // XGraphicProvider + virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL queryGraphicDescriptor( const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties ) override; + virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL queryGraphic( const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties ) override; + virtual void SAL_CALL storeGraphic( const css::uno::Reference< css::graphic::XGraphic >& Graphic, const css::uno::Sequence< css::beans::PropertyValue >& MediaProperties ) override; + + // XGraphicProvider2 + uno::Sequence< uno::Reference<graphic::XGraphic> > SAL_CALL queryGraphics(const uno::Sequence< uno::Sequence<beans::PropertyValue> >& MediaPropertiesSeq ) override; + +private: + + static css::uno::Reference< css::graphic::XGraphic > implLoadMemory( const OUString& rResourceURL ); + static css::uno::Reference< css::graphic::XGraphic > implLoadRepositoryImage( const OUString& rResourceURL ); + static css::uno::Reference< css::graphic::XGraphic > implLoadBitmap( const css::uno::Reference< css::awt::XBitmap >& rBitmap ); + static css::uno::Reference< css::graphic::XGraphic > implLoadStandardImage( const OUString& rResourceURL ); +}; + +GraphicProvider::GraphicProvider() +{ +} + +OUString SAL_CALL GraphicProvider::getImplementationName() +{ + return "com.sun.star.comp.graphic.GraphicProvider"; +} + +sal_Bool SAL_CALL GraphicProvider::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService( this, ServiceName ); +} + +uno::Sequence< OUString > SAL_CALL GraphicProvider::getSupportedServiceNames() +{ + uno::Sequence<OUString> aSeq { "com.sun.star.graphic.GraphicProvider" }; + return aSeq; +} + +uno::Sequence< uno::Type > SAL_CALL GraphicProvider::getTypes() +{ + static const uno::Sequence< uno::Type > aTypes { + cppu::UnoType<lang::XServiceInfo>::get(), + cppu::UnoType<lang::XTypeProvider>::get(), + cppu::UnoType<graphic::XGraphicProvider>::get() + }; + return aTypes; +} + +uno::Sequence< sal_Int8 > SAL_CALL GraphicProvider::getImplementationId() +{ + return css::uno::Sequence<sal_Int8>(); +} + +uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadMemory( const OUString& rResourceURL ) +{ + uno::Reference< ::graphic::XGraphic > xRet; + sal_Int32 nIndex = 0; + + if( rResourceURL.getToken( 0, '/', nIndex ) == "private:memorygraphic" ) + { + sal_Int64 nGraphicAddress = rResourceURL.getToken( 0, '/', nIndex ).toInt64(); + + if( nGraphicAddress ) + { + ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic; + + pUnoGraphic->init( *reinterpret_cast< ::Graphic* >( nGraphicAddress ) ); + xRet = pUnoGraphic; + } + } + + return xRet; +} + + +uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadRepositoryImage( const OUString& rResourceURL ) +{ + uno::Reference< ::graphic::XGraphic > xRet; + + OUString sPathName; + if( rResourceURL.startsWith("private:graphicrepository/", &sPathName) ) + { + BitmapEx aBitmap; + if ( vcl::ImageRepository::loadImage( sPathName, aBitmap ) ) + { + xRet = Graphic(aBitmap).GetXGraphic(); + } + } + return xRet; +} + + +uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadStandardImage( const OUString& rResourceURL ) +{ + uno::Reference< ::graphic::XGraphic > xRet; + + OUString sImageName; + if( rResourceURL.startsWith("private:standardimage/", &sImageName) ) + { + if ( sImageName == "info" ) + { + xRet = Graphic(GetStandardInfoBoxImage().GetBitmapEx()).GetXGraphic(); + } + else if ( sImageName == "warning" ) + { + xRet = Graphic(GetStandardWarningBoxImage().GetBitmapEx()).GetXGraphic(); + } + else if ( sImageName == "error" ) + { + xRet = Graphic(GetStandardErrorBoxImage().GetBitmapEx()).GetXGraphic(); + } + else if ( sImageName == "query" ) + { + xRet = Graphic(GetStandardQueryBoxImage().GetBitmapEx()).GetXGraphic(); + } + } + return xRet; +} + + +uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadBitmap( const uno::Reference< awt::XBitmap >& xBtm ) +{ + uno::Reference< ::graphic::XGraphic > xRet; + uno::Sequence< sal_Int8 > aBmpSeq( xBtm->getDIB() ); + uno::Sequence< sal_Int8 > aMaskSeq( xBtm->getMaskDIB() ); + SvMemoryStream aBmpStream( aBmpSeq.getArray(), aBmpSeq.getLength(), StreamMode::READ ); + Bitmap aBmp; + BitmapEx aBmpEx; + + ReadDIB(aBmp, aBmpStream, true); + + if( aMaskSeq.hasElements() ) + { + SvMemoryStream aMaskStream( aMaskSeq.getArray(), aMaskSeq.getLength(), StreamMode::READ ); + Bitmap aMask; + + ReadDIB(aMask, aMaskStream, true); + aBmpEx = BitmapEx( aBmp, aMask ); + } + else + aBmpEx = BitmapEx( aBmp ); + + if( !aBmpEx.IsEmpty() ) + { + ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic; + + pUnoGraphic->init( aBmpEx ); + xRet = pUnoGraphic; + } + return xRet; +} + +uno::Reference< beans::XPropertySet > SAL_CALL GraphicProvider::queryGraphicDescriptor( const uno::Sequence< beans::PropertyValue >& rMediaProperties ) +{ + uno::Reference< beans::XPropertySet > xRet; + + OUString aURL; + uno::Reference< io::XInputStream > xIStm; + uno::Reference< awt::XBitmap >xBtm; + + for( const auto& rMediaProperty : rMediaProperties ) + { + if (xRet.is()) + break; + + const OUString aName( rMediaProperty.Name ); + const uno::Any aValue( rMediaProperty.Value ); + + if (aName == "URL") + { + aValue >>= aURL; + } + else if (aName == "InputStream") + { + aValue >>= xIStm; + } + else if (aName == "Bitmap") + { + aValue >>= xBtm; + } + } + + SolarMutexGuard g; + + if( xIStm.is() ) + { + unographic::GraphicDescriptor* pDescriptor = new unographic::GraphicDescriptor; + pDescriptor->init( xIStm, aURL ); + xRet = pDescriptor; + } + else if( !aURL.isEmpty() ) + { + uno::Reference< ::graphic::XGraphic > xGraphic( implLoadMemory( aURL ) ); + + if ( !xGraphic.is() ) + xGraphic = implLoadRepositoryImage( aURL ); + + if ( !xGraphic.is() ) + xGraphic = implLoadStandardImage( aURL ); + + if( xGraphic.is() ) + { + xRet.set( xGraphic, uno::UNO_QUERY ); + } + else + { + unographic::GraphicDescriptor* pDescriptor = new unographic::GraphicDescriptor; + pDescriptor->init( aURL ); + xRet = pDescriptor; + } + } + else if( xBtm.is() ) + { + uno::Reference< ::graphic::XGraphic > xGraphic( implLoadBitmap( xBtm ) ); + if( xGraphic.is() ) + xRet.set( xGraphic, uno::UNO_QUERY ); + } + + return xRet; +} + + +uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( const uno::Sequence< ::beans::PropertyValue >& rMediaProperties ) +{ + uno::Reference< ::graphic::XGraphic > xRet; + OUString aPath; + + uno::Reference< io::XInputStream > xIStm; + uno::Reference< awt::XBitmap >xBtm; + + uno::Sequence< ::beans::PropertyValue > aFilterData; + + bool bLazyRead = false; + bool bLoadAsLink = false; + + for (const auto& rMediaProperty : rMediaProperties) + { + if (xRet.is()) + break; + + const OUString aName( rMediaProperty.Name ); + const uno::Any aValue( rMediaProperty.Value ); + + if (aName == "URL") + { + OUString aURL; + aValue >>= aURL; + aPath = aURL; + } + else if (aName == "InputStream") + { + aValue >>= xIStm; + } + else if (aName == "Bitmap") + { + aValue >>= xBtm; + } + else if (aName == "FilterData") + { + aValue >>= aFilterData; + } + else if (aName == "LazyRead") + { + aValue >>= bLazyRead; + } + else if (aName == "LoadAsLink") + { + aValue >>= bLoadAsLink; + } + } + + // Check for the goal width and height if they are defined + sal_uInt16 nExtWidth = 0; + sal_uInt16 nExtHeight = 0; + sal_uInt16 nExtMapMode = 0; + for( const auto& rProp : std::as_const(aFilterData) ) + { + const OUString aName( rProp.Name ); + const uno::Any aValue( rProp.Value ); + + if (aName == "ExternalWidth") + { + aValue >>= nExtWidth; + } + else if (aName == "ExternalHeight") + { + aValue >>= nExtHeight; + } + else if (aName == "ExternalMapMode") + { + aValue >>= nExtMapMode; + } + } + + SolarMutexGuard g; + + std::unique_ptr<SvStream> pIStm; + + if( xIStm.is() ) + { + pIStm = ::utl::UcbStreamHelper::CreateStream( xIStm ); + } + else if( !aPath.isEmpty() ) + { + xRet = implLoadMemory( aPath ); + + if ( !xRet.is() ) + xRet = implLoadRepositoryImage( aPath ); + + if ( !xRet.is() ) + xRet = implLoadStandardImage( aPath ); + + if( !xRet.is() ) + pIStm = ::utl::UcbStreamHelper::CreateStream( aPath, StreamMode::READ ); + } + else if( xBtm.is() ) + { + xRet = implLoadBitmap( xBtm ); + } + + if( pIStm ) + { + ::GraphicFilter& rFilter = ::GraphicFilter::GetGraphicFilter(); + + { + Graphic aVCLGraphic; + + // Define APM Header if goal height and width are defined + WmfExternal aExtHeader; + aExtHeader.xExt = nExtWidth; + aExtHeader.yExt = nExtHeight; + aExtHeader.mapMode = nExtMapMode; + WmfExternal *pExtHeader = nullptr; + if ( nExtMapMode > 0 ) + { + pExtHeader = &aExtHeader; + bLazyRead = false; + } + + ErrCode error = ERRCODE_NONE; + if (bLazyRead) + { + Graphic aGraphic = rFilter.ImportUnloadedGraphic(*pIStm); + if (!aGraphic.IsNone()) + aVCLGraphic = aGraphic; + } + if (aVCLGraphic.IsNone()) + error = rFilter.ImportGraphic(aVCLGraphic, aPath, *pIStm, GRFILTER_FORMAT_DONTKNOW, + nullptr, GraphicFilterImportFlags::NONE, pExtHeader); + + if( (error == ERRCODE_NONE ) && + ( aVCLGraphic.GetType() != GraphicType::NONE ) ) + { + if (!aPath.isEmpty() && bLoadAsLink) + aVCLGraphic.setOriginURL(aPath); + + ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic; + + pUnoGraphic->init( aVCLGraphic ); + xRet = pUnoGraphic; + } + else{ + SAL_WARN("svtools", "Could not create graphic: " << error); + } + } + } + + return xRet; +} + +uno::Sequence< uno::Reference<graphic::XGraphic> > SAL_CALL GraphicProvider::queryGraphics(const uno::Sequence< uno::Sequence<beans::PropertyValue> >& rMediaPropertiesSeq) +{ + SolarMutexGuard aGuard; + + // Turn properties into streams. + std::vector< std::unique_ptr<SvStream> > aStreams; + for (const auto& rMediaProperties : rMediaPropertiesSeq) + { + std::unique_ptr<SvStream> pStream; + uno::Reference<io::XInputStream> xStream; + + auto pProp = std::find_if(rMediaProperties.begin(), rMediaProperties.end(), + [](const beans::PropertyValue& rProp) { return rProp.Name == "InputStream"; }); + if (pProp != rMediaProperties.end()) + { + pProp->Value >>= xStream; + if (xStream.is()) + pStream = utl::UcbStreamHelper::CreateStream(xStream); + } + + aStreams.push_back(std::move(pStream)); + } + + // Import: streams to graphics. + std::vector< std::shared_ptr<Graphic> > aGraphics; + GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); + rFilter.ImportGraphics(aGraphics, std::move(aStreams)); + + // Returning: graphics to UNO objects. + std::vector< uno::Reference<graphic::XGraphic> > aRet; + for (const auto& pGraphic : aGraphics) + { + uno::Reference<graphic::XGraphic> xGraphic; + + if (pGraphic) + { + auto pUnoGraphic = new unographic::Graphic(); + pUnoGraphic->init(*pGraphic); + xGraphic = pUnoGraphic; + } + + aRet.push_back(xGraphic); + } + + return comphelper::containerToSequence(aRet); +} + +void ImplCalculateCropRect( ::Graphic const & rGraphic, const text::GraphicCrop& rGraphicCropLogic, tools::Rectangle& rGraphicCropPixel ) +{ + if ( !(rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom) ) + return; + + Size aSourceSizePixel( rGraphic.GetSizePixel() ); + if ( !(aSourceSizePixel.Width() && aSourceSizePixel.Height()) ) + return; + + if ( !(rGraphicCropLogic.Left || rGraphicCropLogic.Top || rGraphicCropLogic.Right || rGraphicCropLogic.Bottom) ) + return; + + Size aSize100thMM( 0, 0 ); + if( rGraphic.GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel ) + { + aSize100thMM = OutputDevice::LogicToLogic(rGraphic.GetPrefSize(), rGraphic.GetPrefMapMode(), MapMode(MapUnit::Map100thMM)); + } + else + { + aSize100thMM = Application::GetDefaultDevice()->PixelToLogic(rGraphic.GetPrefSize(), MapMode(MapUnit::Map100thMM)); + } + if ( aSize100thMM.Width() && aSize100thMM.Height() ) + { + double fSourceSizePixelWidth = static_cast<double>(aSourceSizePixel.Width()); + double fSourceSizePixelHeight= static_cast<double>(aSourceSizePixel.Height()); + rGraphicCropPixel.SetLeft( static_cast< sal_Int32 >((fSourceSizePixelWidth * rGraphicCropLogic.Left ) / aSize100thMM.Width()) ); + rGraphicCropPixel.SetTop( static_cast< sal_Int32 >((fSourceSizePixelHeight * rGraphicCropLogic.Top ) / aSize100thMM.Height()) ); + rGraphicCropPixel.SetRight( static_cast< sal_Int32 >(( fSourceSizePixelWidth * ( aSize100thMM.Width() - rGraphicCropLogic.Right ) ) / aSize100thMM.Width() ) ); + rGraphicCropPixel.SetBottom( static_cast< sal_Int32 >(( fSourceSizePixelHeight * ( aSize100thMM.Height() - rGraphicCropLogic.Bottom ) ) / aSize100thMM.Height() ) ); + } +} + +void ImplApplyBitmapScaling( ::Graphic& rGraphic, sal_Int32 nPixelWidth, sal_Int32 nPixelHeight ) +{ + if ( nPixelWidth && nPixelHeight ) + { + BitmapEx aBmpEx( rGraphic.GetBitmapEx() ); + MapMode aPrefMapMode( aBmpEx.GetPrefMapMode() ); + Size aPrefSize( aBmpEx.GetPrefSize() ); + aBmpEx.Scale( Size( nPixelWidth, nPixelHeight ) ); + aBmpEx.SetPrefMapMode( aPrefMapMode ); + aBmpEx.SetPrefSize( aPrefSize ); + rGraphic = aBmpEx; + } +} + +void ImplApplyBitmapResolution( ::Graphic& rGraphic, sal_Int32 nImageResolution, const Size& rVisiblePixelSize, const awt::Size& rLogicalSize ) +{ + if ( !(nImageResolution && rLogicalSize.Width && rLogicalSize.Height) ) + return; + + const double fImageResolution = static_cast<double>( nImageResolution ); + const double fSourceDPIX = ( static_cast<double>(rVisiblePixelSize.Width()) * 2540.0 ) / static_cast<double>(rLogicalSize.Width); + const double fSourceDPIY = ( static_cast<double>(rVisiblePixelSize.Height()) * 2540.0 ) / static_cast<double>(rLogicalSize.Height); + const sal_Int32 nSourcePixelWidth( rGraphic.GetSizePixel().Width() ); + const sal_Int32 nSourcePixelHeight( rGraphic.GetSizePixel().Height() ); + const double fSourcePixelWidth = static_cast<double>( nSourcePixelWidth ); + const double fSourcePixelHeight= static_cast<double>( nSourcePixelHeight ); + + sal_Int32 nDestPixelWidth = nSourcePixelWidth; + sal_Int32 nDestPixelHeight = nSourcePixelHeight; + + // check, if the bitmap DPI exceeds the maximum DPI + if( fSourceDPIX > fImageResolution ) + { + nDestPixelWidth = static_cast<sal_Int32>(( fSourcePixelWidth * fImageResolution ) / fSourceDPIX); + if ( !nDestPixelWidth || ( nDestPixelWidth > nSourcePixelWidth ) ) + nDestPixelWidth = nSourcePixelWidth; + } + if ( fSourceDPIY > fImageResolution ) + { + nDestPixelHeight= static_cast<sal_Int32>(( fSourcePixelHeight* fImageResolution ) / fSourceDPIY); + if ( !nDestPixelHeight || ( nDestPixelHeight > nSourcePixelHeight ) ) + nDestPixelHeight = nSourcePixelHeight; + } + if ( ( nDestPixelWidth != nSourcePixelWidth ) || ( nDestPixelHeight != nSourcePixelHeight ) ) + ImplApplyBitmapScaling( rGraphic, nDestPixelWidth, nDestPixelHeight ); +} + +void ImplApplyFilterData( ::Graphic& rGraphic, const uno::Sequence< beans::PropertyValue >& rFilterData ) +{ + /* this method applies following attributes to the graphic, in the first step the + cropping area (logical size in 100thmm) is applied, in the second step the resolution + is applied, in the third step the graphic is scaled to the corresponding pixelsize. + if a parameter value is zero or not available the corresponding step will be skipped */ + + sal_Int32 nPixelWidth = 0; + sal_Int32 nPixelHeight= 0; + sal_Int32 nImageResolution = 0; + awt::Size aLogicalSize( 0, 0 ); + text::GraphicCrop aCropLogic( 0, 0, 0, 0 ); + bool bRemoveCropArea = true; + + for( const auto& rProp : rFilterData ) + { + const OUString aName( rProp.Name ); + const uno::Any aValue( rProp.Value ); + + if (aName == "PixelWidth") + aValue >>= nPixelWidth; + else if (aName == "PixelHeight") + aValue >>= nPixelHeight; + else if (aName == "LogicalSize") + aValue >>= aLogicalSize; + else if (aName == "GraphicCropLogic") + aValue >>= aCropLogic; + else if (aName == "RemoveCropArea") + aValue >>= bRemoveCropArea; + else if (aName == "ImageResolution") + aValue >>= nImageResolution; + } + if ( rGraphic.GetType() == GraphicType::Bitmap ) + { + if(rGraphic.getVectorGraphicData()) + { + // embedded Vector Graphic Data, no need to scale. Also no method to apply crop data currently + } + else + { + tools::Rectangle aCropPixel( Point( 0, 0 ), rGraphic.GetSizePixel() ); + ImplCalculateCropRect( rGraphic, aCropLogic, aCropPixel ); + if ( bRemoveCropArea ) + { + BitmapEx aBmpEx( rGraphic.GetBitmapEx() ); + aBmpEx.Crop( aCropPixel ); + rGraphic = aBmpEx; + } + Size aVisiblePixelSize( bRemoveCropArea ? rGraphic.GetSizePixel() : aCropPixel.GetSize() ); + ImplApplyBitmapResolution( rGraphic, nImageResolution, aVisiblePixelSize, aLogicalSize ); + ImplApplyBitmapScaling( rGraphic, nPixelWidth, nPixelHeight ); + } + } + else if ( ( rGraphic.GetType() == GraphicType::GdiMetafile ) && nImageResolution ) + { + ScopedVclPtrInstance< VirtualDevice > aDummyVDev; + GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() ); + Size aMtfSize( OutputDevice::LogicToLogic(aMtf.GetPrefSize(), aMtf.GetPrefMapMode(), MapMode(MapUnit::Map100thMM)) ); + if ( aMtfSize.Width() && aMtfSize.Height() ) + { + MapMode aNewMapMode( MapUnit::Map100thMM ); + aNewMapMode.SetScaleX( Fraction( aLogicalSize.Width, aMtfSize.Width() ) ); + aNewMapMode.SetScaleY( Fraction( aLogicalSize.Height, aMtfSize.Height() ) ); + aDummyVDev->EnableOutput( false ); + aDummyVDev->SetMapMode( aNewMapMode ); + + for( size_t i = 0, nObjCount = aMtf.GetActionSize(); i < nObjCount; i++ ) + { + MetaAction* pAction = aMtf.GetAction( i ); + switch( pAction->GetType() ) + { + // only optimizing common bitmap actions: + case MetaActionType::MAPMODE: + { + pAction->Execute( aDummyVDev.get() ); + break; + } + case MetaActionType::PUSH: + { + const MetaPushAction* pA = static_cast<const MetaPushAction*>(pAction); + aDummyVDev->Push( pA->GetFlags() ); + break; + } + case MetaActionType::POP: + { + aDummyVDev->Pop(); + break; + } + case MetaActionType::BMPSCALE: + case MetaActionType::BMPEXSCALE: + { + BitmapEx aBmpEx; + Point aPos; + Size aSize; + if ( pAction->GetType() == MetaActionType::BMPSCALE ) + { + MetaBmpScaleAction* pScaleAction = dynamic_cast< MetaBmpScaleAction* >( pAction ); + assert(pScaleAction); + aBmpEx = pScaleAction->GetBitmap(); + aPos = pScaleAction->GetPoint(); + aSize = pScaleAction->GetSize(); + } + else + { + MetaBmpExScaleAction* pScaleAction = dynamic_cast< MetaBmpExScaleAction* >( pAction ); + assert(pScaleAction); + aBmpEx = pScaleAction->GetBitmapEx(); + aPos = pScaleAction->GetPoint(); + aSize = pScaleAction->GetSize(); + } + ::Graphic aGraphic( aBmpEx ); + const Size aSize100thmm( aDummyVDev->LogicToPixel( aSize ) ); + Size aSize100thmm2( aDummyVDev->PixelToLogic(aSize100thmm, MapMode(MapUnit::Map100thMM)) ); + + ImplApplyBitmapResolution( aGraphic, nImageResolution, + aGraphic.GetSizePixel(), awt::Size( aSize100thmm2.Width(), aSize100thmm2.Height() ) ); + + rtl::Reference<MetaAction> pNewAction = new MetaBmpExScaleAction( aPos, aSize, aGraphic.GetBitmapEx() ); + aMtf.ReplaceAction( pNewAction, i ); + break; + } + default: + case MetaActionType::BMP: + case MetaActionType::BMPSCALEPART: + case MetaActionType::BMPEX: + case MetaActionType::BMPEXSCALEPART: + case MetaActionType::MASK: + case MetaActionType::MASKSCALE: + break; + } + } + rGraphic = aMtf; + } + } +} + + +void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XGraphic >& rxGraphic, const uno::Sequence< beans::PropertyValue >& rMediaProperties ) +{ + SolarMutexGuard g; + + std::unique_ptr<SvStream> pOStm; + OUString aPath; + + for( const auto& rMediaProperty : rMediaProperties ) + { + const OUString aName( rMediaProperty.Name ); + const uno::Any aValue( rMediaProperty.Value ); + + if (aName == "URL") + { + OUString aURL; + + aValue >>= aURL; + pOStm = ::utl::UcbStreamHelper::CreateStream( aURL, StreamMode::WRITE | StreamMode::TRUNC ); + aPath = aURL; + } + else if (aName == "OutputStream") + { + uno::Reference< io::XStream > xOStm; + + aValue >>= xOStm; + + if( xOStm.is() ) + pOStm = ::utl::UcbStreamHelper::CreateStream( xOStm ); + } + + if( pOStm ) + break; + } + + if( !pOStm ) + return; + + uno::Sequence< beans::PropertyValue > aFilterDataSeq; + const char* pFilterShortName = nullptr; + + for( const auto& rMediaProperty : rMediaProperties ) + { + const OUString aName( rMediaProperty.Name ); + const uno::Any aValue( rMediaProperty.Value ); + + if (aName == "FilterData") + { + aValue >>= aFilterDataSeq; + } + else if (aName == "MimeType") + { + OUString aMimeType; + + aValue >>= aMimeType; + + if (aMimeType == MIMETYPE_BMP) + pFilterShortName = "bmp"; + else if (aMimeType == MIMETYPE_EPS) + pFilterShortName = "eps"; + else if (aMimeType == MIMETYPE_GIF) + pFilterShortName = "gif"; + else if (aMimeType == MIMETYPE_JPG) + pFilterShortName = "jpg"; + else if (aMimeType == MIMETYPE_MET) + pFilterShortName = "met"; + else if (aMimeType == MIMETYPE_PNG) + pFilterShortName = "png"; + else if (aMimeType == MIMETYPE_PCT) + pFilterShortName = "pct"; + else if (aMimeType == MIMETYPE_PBM) + pFilterShortName = "pbm"; + else if (aMimeType == MIMETYPE_PGM) + pFilterShortName = "pgm"; + else if (aMimeType == MIMETYPE_PPM) + pFilterShortName = "ppm"; + else if (aMimeType == MIMETYPE_RAS) + pFilterShortName = "ras"; + else if (aMimeType == MIMETYPE_SVM) + pFilterShortName = "svm"; + else if (aMimeType == MIMETYPE_TIF) + pFilterShortName = "tif"; + else if (aMimeType == MIMETYPE_EMF) + pFilterShortName = "emf"; + else if (aMimeType == MIMETYPE_WMF) + pFilterShortName = "wmf"; + else if (aMimeType == MIMETYPE_XPM) + pFilterShortName = "xpm"; + else if (aMimeType == MIMETYPE_SVG) + pFilterShortName = "svg"; + else if (aMimeType == MIMETYPE_VCLGRAPHIC) + pFilterShortName = MIMETYPE_VCLGRAPHIC; + } + } + + if( !pFilterShortName ) + return; + + ::GraphicFilter& rFilter = ::GraphicFilter::GetGraphicFilter(); + + { + const uno::Reference< XInterface > xIFace( rxGraphic, uno::UNO_QUERY ); + const ::Graphic* pGraphic = comphelper::getUnoTunnelImplementation<::Graphic>( xIFace ); + + if( pGraphic && ( pGraphic->GetType() != GraphicType::NONE ) ) + { + ::Graphic aGraphic( *pGraphic ); + ImplApplyFilterData( aGraphic, aFilterDataSeq ); + + /* sj: using a temporary memory stream, because some graphic filters are seeking behind + stream end (which leads to an invalid argument exception then). */ + SvMemoryStream aMemStrm; + aMemStrm.SetVersion( SOFFICE_FILEFORMAT_CURRENT ); + if( 0 == strcmp( pFilterShortName, MIMETYPE_VCLGRAPHIC ) ) + WriteGraphic( aMemStrm, aGraphic ); + else + { + rFilter.ExportGraphic( aGraphic, aPath, aMemStrm, + rFilter.GetExportFormatNumberForShortName( OUString::createFromAscii( pFilterShortName ) ), + ( aFilterDataSeq.hasElements() ? &aFilterDataSeq : nullptr ) ); + } + pOStm->WriteBytes( aMemStrm.GetData(), aMemStrm.TellEnd() ); + } + } +} + +} + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * +com_sun_star_comp_graphic_GraphicProvider_get_implementation( + css::uno::XComponentContext *, + css::uno::Sequence<css::uno::Any> const &) +{ + return cppu::acquire(new GraphicProvider); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/UnoGraphicTransformer.cxx b/vcl/source/graphic/UnoGraphicTransformer.cxx new file mode 100644 index 000000000..100e37b29 --- /dev/null +++ b/vcl/source/graphic/UnoGraphicTransformer.cxx @@ -0,0 +1,140 @@ +/* -*- 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 <graphic/UnoGraphicTransformer.hxx> + +#include <vcl/graph.hxx> +#include <vcl/BitmapColor.hxx> +#include <vcl/BitmapDuoToneFilter.hxx> + +using namespace com::sun::star; + +namespace unographic { + + +GraphicTransformer::GraphicTransformer() +{ +} + + +GraphicTransformer::~GraphicTransformer() +{ +} + + +// XGraphicTransformer +uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange( + const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo ) +{ + ::Graphic aGraphic(rxGraphic); + ::Graphic aReturnGraphic; + + BitmapColor aBmpColorFrom(static_cast< sal_uInt8 >(nColorFrom), static_cast< sal_uInt8 >(nColorFrom >> 8), static_cast< sal_uInt8 >(nColorFrom >> 16)); + BitmapColor aBmpColorTo( static_cast< sal_uInt8 >(nColorTo), static_cast< sal_uInt8 >(nColorTo >> 8), static_cast< sal_uInt8 >(nColorTo >> 16)); + + Color aColorFrom(aBmpColorFrom); + Color aColorTo(aBmpColorTo); + + const sal_uInt8 cIndexFrom = aBmpColorFrom.GetIndex(); + + //TODO This code convert GdiMetafile(vector graphic) to Bitmap, which cause to information lost + if (aGraphic.GetType() == GraphicType::Bitmap || + aGraphic.GetType() == GraphicType::GdiMetafile) + { + BitmapEx aBitmapEx(aGraphic.GetBitmapEx()); + Bitmap aBitmap(aBitmapEx.GetBitmap()); + + if (aBitmapEx.IsAlpha()) + { + aBitmapEx.setAlphaFrom( cIndexFrom, 0xff - nAlphaTo ); + aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance); + aReturnGraphic = ::Graphic(aBitmapEx); + } + else if (aBitmapEx.IsTransparent()) + { + if (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)) + { + Bitmap aMask(aBitmapEx.GetMask()); + Bitmap aMask2(aBitmap.CreateMask(aColorFrom, nTolerance)); + aMask.CombineSimple(aMask2, BmpCombine::Or); + aBitmap.Replace(aColorFrom, aColorTo, nTolerance); + aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask)); + } + else + { + aBitmapEx.setAlphaFrom(cIndexFrom, 0xff - nAlphaTo); + aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance); + aReturnGraphic = ::Graphic(aBitmapEx); + } + } + else + { + if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff))) + { + Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance)); + aBitmap.Replace(aColorFrom, aColorTo, nTolerance); + aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask)); + } + else + { + aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo); + aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance); + aReturnGraphic = ::Graphic(aBitmapEx); + } + } + } + + aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); + return aReturnGraphic.GetXGraphic(); +} + +uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone( + const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo ) +{ + ::Graphic aGraphic(rxGraphic); + ::Graphic aReturnGraphic; + + BitmapEx aBitmapEx( aGraphic.GetBitmapEx() ); + AlphaMask aMask( aBitmapEx.GetAlpha() ); + + BitmapEx aTmpBmpEx(aBitmapEx.GetBitmap()); + BitmapFilter::Filter(aTmpBmpEx, BitmapDuoToneFilter(static_cast<sal_uLong>(nColorOne), static_cast<sal_uLong>(nColorTwo))); + + aReturnGraphic = ::Graphic( BitmapEx( aTmpBmpEx.GetBitmap(), aMask ) ); + aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); + return aReturnGraphic.GetXGraphic(); +} + +uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyBrightnessContrast( + const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso ) +{ + ::Graphic aGraphic(rxGraphic); + ::Graphic aReturnGraphic; + + BitmapEx aBitmapEx(aGraphic.GetBitmapEx()); + aBitmapEx.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso); + aReturnGraphic = ::Graphic(aBitmapEx); + aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); + return aReturnGraphic.GetXGraphic(); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/graphic/grfattr.cxx b/vcl/source/graphic/grfattr.cxx new file mode 100644 index 000000000..88d0852af --- /dev/null +++ b/vcl/source/graphic/grfattr.cxx @@ -0,0 +1,62 @@ +/* -*- 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 <vcl/GraphicObject.hxx> + + +GraphicAttr::GraphicAttr() : + mfGamma ( 1.0 ), + mnMirrFlags ( BmpMirrorFlags::NONE ), + mnLeftCrop ( 0 ), + mnTopCrop ( 0 ), + mnRightCrop ( 0 ), + mnBottomCrop ( 0 ), + mnRotate10 ( 0 ), + mnContPercent ( 0 ), + mnLumPercent ( 0 ), + mnRPercent ( 0 ), + mnGPercent ( 0 ), + mnBPercent ( 0 ), + mbInvert ( false ), + mcTransparency ( 0 ), + meDrawMode ( GraphicDrawMode::Standard ) +{ +} + +bool GraphicAttr::operator==( const GraphicAttr& rAttr ) const +{ + return( ( mfGamma == rAttr.mfGamma ) && + ( mnMirrFlags == rAttr.mnMirrFlags ) && + ( mnLeftCrop == rAttr.mnLeftCrop ) && + ( mnTopCrop == rAttr.mnTopCrop ) && + ( mnRightCrop == rAttr.mnRightCrop ) && + ( mnBottomCrop == rAttr.mnBottomCrop ) && + ( mnRotate10 == rAttr.mnRotate10 ) && + ( mnContPercent == rAttr.mnContPercent ) && + ( mnLumPercent == rAttr.mnLumPercent ) && + ( mnRPercent == rAttr.mnRPercent ) && + ( mnGPercent == rAttr.mnGPercent ) && + ( mnBPercent == rAttr.mnBPercent ) && + ( mbInvert == rAttr.mbInvert ) && + ( mcTransparency == rAttr.mcTransparency ) && + ( meDrawMode == rAttr.meDrawMode ) ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |