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 <primitive2d/graphicprimitivehelper2d.hxx>
28 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
32 namespace drawinglayer::primitive2d
34 void GraphicPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
,
35 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 isBitmap(GraphicType::Bitmap
== aTransformedGraphic
.GetType()
79 && !aTransformedGraphic
.getVectorGraphicData());
80 const bool isAdjusted(getGraphicAttr().IsAdjusted());
81 const bool isDrawMode(GraphicDrawMode::Standard
!= getGraphicAttr().GetDrawMode());
83 if (isBitmap
&& (isAdjusted
|| isDrawMode
))
85 // the pure primitive solution with the color modifiers works well, too, but when
86 // it is a bitmap graphic the old modification currently is faster; so use it here
87 // instead of creating all as in create2DColorModifierEmbeddingsAsNeeded (see below).
88 // Still, crop, rotation, mirroring and transparency is handled by primitives already
90 // This could even be done when vector graphic, but we explicitly want to have the
91 // pure primitive solution for this; this will allow vector graphics to stay vector
92 // graphics, independent from the color filtering stuff. This will enhance e.g.
93 // SVG and print quality while reducing data size at the same time.
94 // The other way around the old modifications when only used on already bitmap objects
95 // will not lose any quality.
96 aTransformedGraphic
= rGraphicObject
.GetTransformedGraphic(&aSuppressGraphicAttr
);
98 // reset GraphicAttr after use to not apply double
99 aSuppressGraphicAttr
= GraphicAttr();
102 // create sub-content; helper takes care of correct handling of
103 // bitmap, svg or metafile content
104 Primitive2DContainer aRetval
;
105 create2DDecompositionOfGraphic(aRetval
, aTransformedGraphic
, aTransform
);
109 // content is invisible, done
113 if (isAdjusted
|| isDrawMode
)
115 // embed to needed ModifiedColorPrimitive2D's if necessary. Do this for
116 // adjustments and draw mode specials
117 aRetval
= create2DColorModifierEmbeddingsAsNeeded(
118 std::move(aRetval
), aSuppressGraphicAttr
.GetDrawMode(),
119 std::clamp(aSuppressGraphicAttr
.GetLuminance() * 0.01, -1.0, 1.0),
120 std::clamp(aSuppressGraphicAttr
.GetContrast() * 0.01, -1.0, 1.0),
121 std::clamp(aSuppressGraphicAttr
.GetChannelR() * 0.01, -1.0, 1.0),
122 std::clamp(aSuppressGraphicAttr
.GetChannelG() * 0.01, -1.0, 1.0),
123 std::clamp(aSuppressGraphicAttr
.GetChannelB() * 0.01, -1.0, 1.0),
124 std::clamp(aSuppressGraphicAttr
.GetGamma(), 0.0, 10.0),
125 aSuppressGraphicAttr
.IsInvert());
129 // content is invisible, done
134 if (getGraphicAttr().IsTransparent())
136 // check for transparency
137 const double fTransparency(
138 std::clamp((255 - getGraphicAttr().GetAlpha()) * (1.0 / 255.0), 0.0, 1.0));
140 if (!basegfx::fTools::equalZero(fTransparency
))
142 Primitive2DReference
aUnifiedTransparence(
143 new UnifiedTransparencePrimitive2D(std::move(aRetval
), fTransparency
));
145 aRetval
= Primitive2DContainer
{ aUnifiedTransparence
};
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 Primitive2DReference
xPrimitive(
162 new CropPrimitive2D(std::move(aRetval
), aTransform
,
163 getGraphicAttr().GetLeftCrop() * aCropScaleFactor
.getX(),
164 getGraphicAttr().GetTopCrop() * aCropScaleFactor
.getY(),
165 getGraphicAttr().GetRightCrop() * aCropScaleFactor
.getX(),
166 getGraphicAttr().GetBottomCrop() * aCropScaleFactor
.getY()));
168 aRetval
= Primitive2DContainer
{ xPrimitive
};
171 rContainer
.append(std::move(aRetval
));
174 GraphicPrimitive2D::GraphicPrimitive2D(basegfx::B2DHomMatrix aTransform
,
175 const GraphicObject
& rGraphicObject
,
176 const GraphicAttr
& rGraphicAttr
)
177 : maTransform(std::move(aTransform
))
178 , maGraphicObject(rGraphicObject
)
179 , maGraphicAttr(rGraphicAttr
)
183 GraphicPrimitive2D::GraphicPrimitive2D(basegfx::B2DHomMatrix aTransform
,
184 const GraphicObject
& rGraphicObject
)
185 : maTransform(std::move(aTransform
))
186 , maGraphicObject(rGraphicObject
)
190 bool GraphicPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
192 if (BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
194 const GraphicPrimitive2D
& rCompare
= static_cast<const GraphicPrimitive2D
&>(rPrimitive
);
196 return (getTransform() == rCompare
.getTransform()
197 && getGraphicObject() == rCompare
.getGraphicObject()
198 && getGraphicAttr() == rCompare
.getGraphicAttr());
205 GraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation*/) const
207 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
208 aRetval
.transform(getTransform());
213 sal_uInt32
GraphicPrimitive2D::getPrimitive2DID() const
215 return PRIMITIVE2D_ID_GRAPHICPRIMITIVE2D
;
218 } // end of namespace
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */