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 <graphic/UnoGraphic.hxx>
22 #include <tools/stream.hxx>
23 #include <vcl/svapp.hxx>
24 #include <com/sun/star/graphic/GraphicType.hpp>
25 #include <com/sun/star/graphic/XGraphicTransformer.hpp>
26 #include <vcl/dibtools.hxx>
27 #include <vcl/graph.hxx>
28 #include <vcl/BitmapColor.hxx>
29 #include <vcl/bitmap/BitmapDuoToneFilter.hxx>
30 #include <comphelper/servicehelper.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <cppuhelper/queryinterface.hxx>
33 #include <cppuhelper/typeprovider.hxx>
35 using namespace com::sun::star
;
37 namespace unographic
{
43 Graphic::~Graphic() noexcept
47 void Graphic::init(const ::Graphic
& rGraphic
)
50 unographic::GraphicDescriptor::init(maGraphic
);
53 uno::Any SAL_CALL
Graphic::queryInterface( const uno::Type
& rType
)
56 if( rType
== cppu::UnoType
<graphic::XGraphic
>::get())
57 aAny
<<= uno::Reference
< graphic::XGraphic
>( this );
58 else if( rType
== cppu::UnoType
<awt::XBitmap
>::get())
59 aAny
<<= uno::Reference
< awt::XBitmap
>( this );
60 else if( rType
== cppu::UnoType
<graphic::XGraphicTransformer
>::get())
61 aAny
<<= uno::Reference
< graphic::XGraphicTransformer
>(this);
63 aAny
= ::unographic::GraphicDescriptor::queryInterface( rType
);
68 void SAL_CALL
Graphic::acquire()
71 unographic::GraphicDescriptor::acquire();
74 void SAL_CALL
Graphic::release() noexcept
76 unographic::GraphicDescriptor::release();
79 OUString SAL_CALL
Graphic::getImplementationName()
81 return u
"com.sun.star.comp.graphic.Graphic"_ustr
;
84 sal_Bool SAL_CALL
Graphic::supportsService( const OUString
& rServiceName
)
86 return cppu::supportsService( this, rServiceName
);
89 uno::Sequence
< OUString
> SAL_CALL
Graphic::getSupportedServiceNames()
91 uno::Sequence
< OUString
> aRet( ::unographic::GraphicDescriptor::getSupportedServiceNames() );
92 const uno::Sequence
< OUString
> aNew
{ u
"com.sun.star.graphic.Graphic"_ustr
};
93 sal_Int32 nOldCount
= aRet
.getLength();
95 aRet
.realloc( nOldCount
+ aNew
.getLength() );
97 std::copy(aNew
.begin(), aNew
.end(), std::next(aRet
.getArray(), nOldCount
));
102 uno::Sequence
< uno::Type
> SAL_CALL
Graphic::getTypes()
104 return cppu::OTypeCollection(
105 cppu::UnoType
<graphic::XGraphic
>::get(),
106 cppu::UnoType
<awt::XBitmap
>::get(),
107 ::unographic::GraphicDescriptor::getTypes()
111 uno::Sequence
< sal_Int8
> SAL_CALL
Graphic::getImplementationId()
113 return css::uno::Sequence
<sal_Int8
>();
116 sal_Int8 SAL_CALL
Graphic::getType()
118 sal_Int8 cRet
= graphic::GraphicType::EMPTY
;
120 if (!maGraphic
.IsNone())
122 cRet
= (maGraphic
.GetType() == ::GraphicType::Bitmap
) ? graphic::GraphicType::PIXEL
123 : graphic::GraphicType::VECTOR
;
131 awt::Size SAL_CALL
Graphic::getSize()
133 SolarMutexGuard aGuard
;
136 if (!maGraphic
.IsNone())
138 aVclSize
= maGraphic
.GetSizePixel();
140 return awt::Size(aVclSize
.Width(), aVclSize
.Height());
143 uno::Sequence
<sal_Int8
> SAL_CALL
Graphic::getDIB()
145 SolarMutexGuard aGuard
;
147 if (!maGraphic
.IsNone())
149 SvMemoryStream aMemoryStream
;
151 WriteDIB(maGraphic
.GetBitmapEx().GetBitmap(), aMemoryStream
, false, true);
152 return css::uno::Sequence
<sal_Int8
>(static_cast<sal_Int8
const *>(aMemoryStream
.GetData()), aMemoryStream
.Tell());
156 return uno::Sequence
<sal_Int8
>();
160 uno::Sequence
<sal_Int8
> SAL_CALL
Graphic::getMaskDIB()
162 SolarMutexGuard aGuard
;
164 if (!maGraphic
.IsNone())
166 SvMemoryStream aMemoryStream
;
168 AlphaMask aMask
= maGraphic
.GetBitmapEx().GetAlphaMask();
169 // for backwards compatibility for extensions, we need to convert from alpha to transparency
171 WriteDIB(aMask
.GetBitmap(), aMemoryStream
, false, true);
172 return css::uno::Sequence
<sal_Int8
>( static_cast<sal_Int8
const *>(aMemoryStream
.GetData()), aMemoryStream
.Tell() );
176 return uno::Sequence
<sal_Int8
>();
180 // XGraphicTransformer
181 uno::Reference
< graphic::XGraphic
> SAL_CALL
Graphic::colorChange(
182 const uno::Reference
< graphic::XGraphic
>& rxGraphic
, sal_Int32 nColorFrom
, sal_Int8 nTolerance
, sal_Int32 nColorTo
, sal_Int8 nAlphaTo
)
184 ::Graphic
aGraphic(rxGraphic
);
185 ::Graphic aReturnGraphic
;
187 BitmapColor
aBmpColorFrom(Color(ColorTransparency
, static_cast<sal_uInt32
>(nColorFrom
)));
188 BitmapColor
aBmpColorTo(Color(ColorTransparency
, static_cast<sal_uInt32
>(nColorTo
)));
190 Color
aColorFrom(aBmpColorFrom
);
191 Color
aColorTo(aBmpColorTo
);
193 const sal_uInt8 cIndexFrom
= aBmpColorFrom
.GetIndex();
195 //TODO This code convert GdiMetafile(vector graphic) to Bitmap, which cause to information lost
196 if (aGraphic
.GetType() == GraphicType::Bitmap
||
197 aGraphic
.GetType() == GraphicType::GdiMetafile
)
199 BitmapEx
aBitmapEx(aGraphic
.GetBitmapEx());
201 if (aBitmapEx
.IsAlpha())
203 aBitmapEx
.ChangeColorAlpha( cIndexFrom
, nAlphaTo
);
204 aBitmapEx
.Replace(aColorFrom
, aColorTo
, nTolerance
);
205 aReturnGraphic
= ::Graphic(aBitmapEx
);
209 if ((nAlphaTo
== 0) || (nAlphaTo
== sal::static_int_cast
< sal_Int8
>(0xff)))
211 Bitmap
aBitmap(aBitmapEx
.GetBitmap());
212 AlphaMask
aMask(aBitmap
.CreateAlphaMask(aColorFrom
, nTolerance
));
213 aBitmap
.Replace(aColorFrom
, aColorTo
, nTolerance
);
214 aReturnGraphic
= ::Graphic(BitmapEx(aBitmap
, aMask
));
218 aBitmapEx
.Replace(aColorFrom
, aColorTo
, nTolerance
);
219 aReturnGraphic
= ::Graphic(aBitmapEx
);
224 aReturnGraphic
.setOriginURL(aGraphic
.getOriginURL());
225 return aReturnGraphic
.GetXGraphic();
228 uno::Reference
< graphic::XGraphic
> SAL_CALL
Graphic::applyDuotone(
229 const uno::Reference
< graphic::XGraphic
>& rxGraphic
, sal_Int32 nColorOne
, sal_Int32 nColorTwo
)
231 ::Graphic
aGraphic(rxGraphic
);
232 ::Graphic aReturnGraphic
;
234 BitmapEx
aBitmapEx( aGraphic
.GetBitmapEx() );
235 const AlphaMask
& aMask( aBitmapEx
.GetAlphaMask() );
237 BitmapEx
aTmpBmpEx(aBitmapEx
.GetBitmap());
238 BitmapFilter::Filter(aTmpBmpEx
,
240 Color(ColorTransparency
, nColorOne
),
241 Color(ColorTransparency
, nColorTwo
)));
243 aReturnGraphic
= ::Graphic( BitmapEx( aTmpBmpEx
.GetBitmap(), aMask
) );
244 aReturnGraphic
.setOriginURL(aGraphic
.getOriginURL());
245 return aReturnGraphic
.GetXGraphic();
248 uno::Reference
< graphic::XGraphic
> SAL_CALL
Graphic::applyBrightnessContrast(
249 const uno::Reference
< graphic::XGraphic
>& rxGraphic
, sal_Int32 nBrightness
, sal_Int32 nContrast
, sal_Bool mso
)
251 ::Graphic
aGraphic(rxGraphic
);
252 ::Graphic aReturnGraphic
;
254 BitmapEx
aBitmapEx(aGraphic
.GetBitmapEx());
255 aBitmapEx
.Adjust(nBrightness
, nContrast
, 0, 0, 0, 0, false, mso
);
256 aReturnGraphic
= ::Graphic(aBitmapEx
);
257 aReturnGraphic
.setOriginURL(aGraphic
.getOriginURL());
258 return aReturnGraphic
.GetXGraphic();
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */