LibreOffice Module vcl (master) 1
UnoGraphic.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
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>
34
35using namespace com::sun::star;
36
37namespace unographic {
38
40{
41}
42
44{
45}
46
47void Graphic::init(const ::Graphic& rGraphic)
48{
49 maGraphic = rGraphic;
51}
52
54{
55 uno::Any aAny;
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<lang::XUnoTunnel>::get())
61 aAny <<= uno::Reference< lang::XUnoTunnel >(this);
62 else
64
65 return aAny;
66}
67
69{
70 css::uno::Any aReturn = ::unographic::GraphicDescriptor::queryInterface( rType );
71 if ( !aReturn.hasValue() )
72 aReturn = ::cppu::queryInterface ( rType, static_cast< graphic::XGraphicTransformer*>( this ) );
73 return aReturn;
74}
75
76void SAL_CALL Graphic::acquire()
77 noexcept
78{
80}
81
82void SAL_CALL Graphic::release() noexcept
83{
85}
86
88{
89 return "com.sun.star.comp.graphic.Graphic";
90}
91
92sal_Bool SAL_CALL Graphic::supportsService( const OUString& rServiceName )
93{
94 return cppu::supportsService( this, rServiceName );
95}
96
97uno::Sequence< OUString > SAL_CALL Graphic::getSupportedServiceNames()
98{
99 uno::Sequence< OUString > aRet( ::unographic::GraphicDescriptor::getSupportedServiceNames() );
100 const uno::Sequence< OUString > aNew { "com.sun.star.graphic.Graphic" };
101 sal_Int32 nOldCount = aRet.getLength();
102
103 aRet.realloc( nOldCount + aNew.getLength() );
104
105 std::copy(aNew.begin(), aNew.end(), std::next(aRet.getArray(), nOldCount));
106
107 return aRet;
108}
109
110uno::Sequence< uno::Type > SAL_CALL Graphic::getTypes()
111{
116 ).getTypes();
117}
118
119uno::Sequence< sal_Int8 > SAL_CALL Graphic::getImplementationId()
120{
121 return css::uno::Sequence<sal_Int8>();
122}
123
125{
126 sal_Int8 cRet = graphic::GraphicType::EMPTY;
127
128 if (!maGraphic.IsNone())
129 {
130 cRet = (maGraphic.GetType() == ::GraphicType::Bitmap) ? graphic::GraphicType::PIXEL
131 : graphic::GraphicType::VECTOR;
132 }
133
134 return cRet;
135}
136
137// XBitmap
138
139awt::Size SAL_CALL Graphic::getSize()
140{
141 SolarMutexGuard aGuard;
142
143 Size aVclSize;
144 if (!maGraphic.IsNone())
145 {
146 aVclSize = maGraphic.GetSizePixel();
147 }
148 return awt::Size(aVclSize.Width(), aVclSize.Height());
149}
150
151uno::Sequence<sal_Int8> SAL_CALL Graphic::getDIB()
152{
153 SolarMutexGuard aGuard;
154
155 if (!maGraphic.IsNone())
156 {
157 SvMemoryStream aMemoryStream;
158
159 WriteDIB(maGraphic.GetBitmapEx().GetBitmap(), aMemoryStream, false, true);
160 return css::uno::Sequence<sal_Int8>(static_cast<sal_Int8 const *>(aMemoryStream.GetData()), aMemoryStream.Tell());
161 }
162 else
163 {
164 return uno::Sequence<sal_Int8>();
165 }
166}
167
168uno::Sequence<sal_Int8> SAL_CALL Graphic::getMaskDIB()
169{
170 SolarMutexGuard aGuard;
171
172 if (!maGraphic.IsNone())
173 {
174 SvMemoryStream aMemoryStream;
175
176 WriteDIB(maGraphic.GetBitmapEx().GetAlpha(), aMemoryStream, false, true);
177 return css::uno::Sequence<sal_Int8>( static_cast<sal_Int8 const *>(aMemoryStream.GetData()), aMemoryStream.Tell() );
178 }
179 else
180 {
181 return uno::Sequence<sal_Int8>();
182 }
183}
184
185sal_Int64 SAL_CALL Graphic::getSomething( const uno::Sequence< sal_Int8 >& rId )
186{
188}
189
190
191// XGraphicTransformer
192uno::Reference< graphic::XGraphic > SAL_CALL Graphic::colorChange(
193 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
194{
195 ::Graphic aGraphic(rxGraphic);
196 ::Graphic aReturnGraphic;
197
198 BitmapColor aBmpColorFrom(Color(ColorTransparency, static_cast<sal_uInt32>(nColorFrom)));
199 BitmapColor aBmpColorTo(Color(ColorTransparency, static_cast<sal_uInt32>(nColorTo)));
200
201 Color aColorFrom(aBmpColorFrom);
202 Color aColorTo(aBmpColorTo);
203
204 const sal_uInt8 cIndexFrom = aBmpColorFrom.GetIndex();
205
206 //TODO This code convert GdiMetafile(vector graphic) to Bitmap, which cause to information lost
207 if (aGraphic.GetType() == GraphicType::Bitmap ||
208 aGraphic.GetType() == GraphicType::GdiMetafile)
209 {
210 BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
211
212 if (aBitmapEx.IsAlpha())
213 {
214 aBitmapEx.setAlphaFrom( cIndexFrom, 0xff - nAlphaTo );
215 aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
216 aReturnGraphic = ::Graphic(aBitmapEx);
217 }
218 else
219 {
220 if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)))
221 {
222 Bitmap aBitmap(aBitmapEx.GetBitmap());
223 Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance));
224 aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
225 aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
226 }
227 else
228 {
229 aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo);
230 aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
231 aReturnGraphic = ::Graphic(aBitmapEx);
232 }
233 }
234 }
235
236 aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
237 return aReturnGraphic.GetXGraphic();
238}
239
240uno::Reference< graphic::XGraphic > SAL_CALL Graphic::applyDuotone(
241 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
242{
243 ::Graphic aGraphic(rxGraphic);
244 ::Graphic aReturnGraphic;
245
246 BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
247 AlphaMask aMask( aBitmapEx.GetAlpha() );
248
249 BitmapEx aTmpBmpEx(aBitmapEx.GetBitmap());
250 BitmapFilter::Filter(aTmpBmpEx,
252 Color(ColorTransparency, nColorOne),
253 Color(ColorTransparency, nColorTwo)));
254
255 aReturnGraphic = ::Graphic( BitmapEx( aTmpBmpEx.GetBitmap(), aMask ) );
256 aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
257 return aReturnGraphic.GetXGraphic();
258}
259
260uno::Reference< graphic::XGraphic > SAL_CALL Graphic::applyBrightnessContrast(
261 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso )
262{
263 ::Graphic aGraphic(rxGraphic);
264 ::Graphic aReturnGraphic;
265
266 BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
267 aBitmapEx.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso);
268 aReturnGraphic = ::Graphic(aBitmapEx);
269 aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
270 return aReturnGraphic.GetXGraphic();
271}
272
273
274}
275
276/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:70
AlphaMask GetAlpha() const
Definition: BitmapEx.cxx:215
bool IsAlpha() const
Definition: BitmapEx.cxx:193
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
void Replace(const Color &rSearchColor, const Color &rReplaceColor)
Replace all pixel having the search color with the specified color.
Definition: BitmapEx.cxx:479
bool Adjust(short nLuminancePercent, short nContrastPercent, short nChannelRPercent, short nChannelGPercent, short nChannelBPercent, double fGamma=1.0, bool bInvert=false, bool msoBrightness=false)
Change various global color characteristics.
Definition: BitmapEx.cxx:491
void setAlphaFrom(sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo)
Definition: BitmapEx.cxx:1367
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
Bitmap CreateMask(const Color &rTransColor, sal_uInt8 nTol=0) const
Create on-off mask from bitmap.
bool Replace(const Bitmap &rMask, const Color &rReplaceColor)
Replace all pixel where the given mask is on with the specified color.
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
Definition: graph.cxx:349
OUString getOriginURL() const
Definition: graph.cxx:534
GraphicType GetType() const
Definition: graph.cxx:293
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:329
void setOriginURL(OUString const &rOriginURL)
Definition: graph.cxx:543
constexpr tools::Long Height() const
constexpr tools::Long Width() const
const void * GetData()
sal_uInt64 Tell() const
css::uno::Sequence< css::uno::Type > SAL_CALL getTypes()
virtual void SAL_CALL release() noexcept override
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
void init(const ::Graphic &rGraphic)
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual void SAL_CALL acquire() noexcept override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
Definition: UnoGraphic.cxx:87
virtual css::awt::Size SAL_CALL getSize() override
Definition: UnoGraphic.cxx:139
virtual void SAL_CALL acquire() noexcept override
Definition: UnoGraphic.cxx:76
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) override
Definition: UnoGraphic.cxx:53
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: UnoGraphic.cxx:97
virtual ::sal_Int8 SAL_CALL getType() override
Definition: UnoGraphic.cxx:124
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: UnoGraphic.cxx:68
virtual ~Graphic() noexcept override
Definition: UnoGraphic.cxx:43
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &rId) override
Definition: UnoGraphic.cxx:185
void init(const ::Graphic &rGraphic)
Definition: UnoGraphic.cxx:47
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
Definition: UnoGraphic.cxx:119
virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL getMaskDIB() override
Definition: UnoGraphic.cxx:168
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: UnoGraphic.cxx:110
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL applyBrightnessContrast(const css::uno::Reference< css::graphic::XGraphic > &rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso) override
Definition: UnoGraphic.cxx:260
virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL getDIB() override
Definition: UnoGraphic.cxx:151
virtual void SAL_CALL release() noexcept override
Definition: UnoGraphic.cxx:82
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL colorChange(const css::uno::Reference< css::graphic::XGraphic > &rGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo) override
Definition: UnoGraphic.cxx:192
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: UnoGraphic.cxx:92
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL applyDuotone(const css::uno::Reference< css::graphic::XGraphic > &rGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo) override
Definition: UnoGraphic.cxx:240
ColorTransparency
bool WriteDIB(const Bitmap &rSource, SvStream &rOStm, bool bCompressed, bool bFileHeader)
Definition: dibtools.cxx:1832
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_uInt8
unsigned char sal_Bool
signed char sal_Int8