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