1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
24 #include <drawinglayer/primitive2d/graphicprimitive2d.hxx>
25 #include <primitive2d/cropprimitive2d.hxx>
26 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
27 #include <drawinglayer/primitive2d/graphicprimitivehelper2d.hxx>
28 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
32 namespace drawinglayer::primitive2d
35 GraphicPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D
&) const
37 if (0 == getGraphicAttr().GetAlpha())
39 // content is invisible, done
43 // do not apply mirroring from GraphicAttr to the Metafile by calling
44 // GetTransformedGraphic, this will try to mirror the Metafile using Scale()
45 // at the Metafile. This again calls Scale at the single MetaFile actions,
46 // but this implementation never worked. I reworked that implementations,
47 // but for security reasons i will try not to use it.
48 basegfx::B2DHomMatrix
aTransform(getTransform());
50 if (getGraphicAttr().IsMirrored())
52 // content needs mirroring
53 const bool bHMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Horizontal
);
54 const bool bVMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Vertical
);
56 // mirror by applying negative scale to the unit primitive and
57 // applying the object transformation on it.
59 = basegfx::utils::createScaleB2DHomMatrix(bHMirr
? -1.0 : 1.0, bVMirr
? -1.0 : 1.0);
60 aTransform
.translate(bHMirr
? 1.0 : 0.0, bVMirr
? 1.0 : 0.0);
61 aTransform
= getTransform() * aTransform
;
64 // Get transformed graphic. Suppress rotation and cropping, only filtering is needed
65 // here (and may be replaced later on). Cropping is handled below as mask primitive (if set).
66 // Also need to suppress mirroring, it is part of the transformation now (see above).
67 // Also move transparency handling to embedding to a UnifiedTransparencePrimitive2D; do
68 // that by remembering original transparency and applying that later if needed
69 GraphicAttr
aSuppressGraphicAttr(getGraphicAttr());
71 aSuppressGraphicAttr
.SetCrop(0, 0, 0, 0);
72 aSuppressGraphicAttr
.SetRotation(0_deg10
);
73 aSuppressGraphicAttr
.SetMirrorFlags(BmpMirrorFlags::NONE
);
74 aSuppressGraphicAttr
.SetAlpha(255);
76 const GraphicObject
& rGraphicObject
= getGraphicObject();
77 Graphic
aTransformedGraphic(rGraphicObject
.GetGraphic());
78 const bool isAdjusted(getGraphicAttr().IsAdjusted());
79 const bool isDrawMode(GraphicDrawMode::Standard
!= getGraphicAttr().GetDrawMode());
81 // I have now added buffering BColorModifierStack-adapted Bitmaps,
82 // see BitmapEx::ModifyBitmapEx, thus the primitive case is fast now.
83 // It buffers the adapted bitmap and at that the SDPRs can then buffer
84 // the system-dependent representation.
85 // I keep the code below (adding a static switch). It modifies the
86 // Graphic and is a reliable fallback - just in case. Remember that
87 // it does *not* buffer and has to modify again at each re-use...
88 static bool bUseOldModification(false);
90 if (bUseOldModification
)
92 const bool isBitmap(GraphicType::Bitmap
== aTransformedGraphic
.GetType()
93 && !aTransformedGraphic
.getVectorGraphicData());
95 if (isBitmap
&& (isAdjusted
|| isDrawMode
))
97 // the pure primitive solution with the color modifiers works well, too, but when
98 // it is a bitmap graphic the old modification currently is faster; so use it here
99 // instead of creating all as in create2DColorModifierEmbeddingsAsNeeded (see below).
100 // Still, crop, rotation, mirroring and transparency is handled by primitives already
102 // This could even be done when vector graphic, but we explicitly want to have the
103 // pure primitive solution for this; this will allow vector graphics to stay vector
104 // graphics, independent from the color filtering stuff. This will enhance e.g.
105 // SVG and print quality while reducing data size at the same time.
106 // The other way around the old modifications when only used on already bitmap objects
107 // will not lose any quality.
108 aTransformedGraphic
= rGraphicObject
.GetTransformedGraphic(&aSuppressGraphicAttr
);
110 // reset GraphicAttr after use to not apply double
111 aSuppressGraphicAttr
= GraphicAttr();
115 // create sub-content; helper takes care of correct handling of
116 // bitmap, svg or metafile content. also handle alpha there directly
117 Primitive2DContainer aRetval
;
118 const double fTransparency(
119 std::clamp((255 - getGraphicAttr().GetAlpha()) * (1.0 / 255.0), 0.0, 1.0));
120 create2DDecompositionOfGraphic(aRetval
, aTransformedGraphic
, aTransform
, fTransparency
);
124 // content is invisible, done
128 if (isAdjusted
|| isDrawMode
)
130 // embed to needed ModifiedColorPrimitive2D's if necessary. Do this for
131 // adjustments and draw mode specials
132 aRetval
= create2DColorModifierEmbeddingsAsNeeded(
133 std::move(aRetval
), aSuppressGraphicAttr
.GetDrawMode(),
134 std::clamp(aSuppressGraphicAttr
.GetLuminance() * 0.01, -1.0, 1.0),
135 std::clamp(aSuppressGraphicAttr
.GetContrast() * 0.01, -1.0, 1.0),
136 std::clamp(aSuppressGraphicAttr
.GetChannelR() * 0.01, -1.0, 1.0),
137 std::clamp(aSuppressGraphicAttr
.GetChannelG() * 0.01, -1.0, 1.0),
138 std::clamp(aSuppressGraphicAttr
.GetChannelB() * 0.01, -1.0, 1.0),
139 std::clamp(aSuppressGraphicAttr
.GetGamma(), 0.0, 10.0),
140 aSuppressGraphicAttr
.IsInvert());
144 // content is invisible, done
149 if (getGraphicAttr().IsCropped())
151 // check for cropping
152 // calculate scalings between real image size and logic object size. This
153 // is necessary since the crop values are relative to original bitmap size
154 const basegfx::B2DVector
aObjectScale(aTransform
* basegfx::B2DVector(1.0, 1.0));
155 const basegfx::B2DVector
aCropScaleFactor(rGraphicObject
.calculateCropScaling(
156 aObjectScale
.getX(), aObjectScale
.getY(), getGraphicAttr().GetLeftCrop(),
157 getGraphicAttr().GetTopCrop(), getGraphicAttr().GetRightCrop(),
158 getGraphicAttr().GetBottomCrop()));
160 // embed content in cropPrimitive
161 aRetval
= Primitive2DContainer
{ new CropPrimitive2D(
162 std::move(aRetval
), aTransform
,
163 getGraphicAttr().GetLeftCrop() * aCropScaleFactor
.getX(),
164 getGraphicAttr().GetTopCrop() * aCropScaleFactor
.getY(),
165 getGraphicAttr().GetRightCrop() * aCropScaleFactor
.getX(),
166 getGraphicAttr().GetBottomCrop() * aCropScaleFactor
.getY()) };
169 return new GroupPrimitive2D(std::move(aRetval
));
172 GraphicPrimitive2D::GraphicPrimitive2D(basegfx::B2DHomMatrix aTransform
,
173 const GraphicObject
& rGraphicObject
,
174 const GraphicAttr
& rGraphicAttr
)
175 : maTransform(std::move(aTransform
))
176 , maGraphicObject(rGraphicObject
)
177 , maGraphicAttr(rGraphicAttr
)
179 // activate callback to flush buffered decomposition content
180 setCallbackSeconds(20);
183 GraphicPrimitive2D::GraphicPrimitive2D(basegfx::B2DHomMatrix aTransform
,
184 const GraphicObject
& rGraphicObject
)
185 : maTransform(std::move(aTransform
))
186 , maGraphicObject(rGraphicObject
)
188 // activate callback to flush buffered decomposition content
189 setCallbackSeconds(20);
192 bool GraphicPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
194 if (BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
196 const GraphicPrimitive2D
& rCompare
= static_cast<const GraphicPrimitive2D
&>(rPrimitive
);
198 return (getTransform() == rCompare
.getTransform()
199 && getGraphicObject() == rCompare
.getGraphicObject()
200 && getGraphicAttr() == rCompare
.getGraphicAttr());
207 GraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation*/) const
209 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
210 aRetval
.transform(getTransform());
215 sal_uInt32
GraphicPrimitive2D::getPrimitive2DID() const
217 return PRIMITIVE2D_ID_GRAPHICPRIMITIVE2D
;
220 } // end of namespace
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */