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 {
41 }
42 
43 Graphic::~Graphic() noexcept
44 {
45 }
46 
47 void Graphic::init(const ::Graphic& rGraphic)
48 {
49  maGraphic = rGraphic;
51 }
52 
53 uno::Any SAL_CALL Graphic::queryAggregation( const uno::Type& rType )
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 
68 uno::Any SAL_CALL Graphic::queryInterface( const uno::Type & rType )
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 
76 void SAL_CALL Graphic::acquire()
77  noexcept
78 {
80 }
81 
82 void SAL_CALL Graphic::release() noexcept
83 {
85 }
86 
87 OUString SAL_CALL Graphic::getImplementationName()
88 {
89  return "com.sun.star.comp.graphic.Graphic";
90 }
91 
92 sal_Bool SAL_CALL Graphic::supportsService( const OUString& rServiceName )
93 {
94  return cppu::supportsService( this, rServiceName );
95 }
96 
97 uno::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 
110 uno::Sequence< uno::Type > SAL_CALL Graphic::getTypes()
111 {
112  return cppu::OTypeCollection(
116  ).getTypes();
117 }
118 
119 uno::Sequence< sal_Int8 > SAL_CALL Graphic::getImplementationId()
120 {
121  return css::uno::Sequence<sal_Int8>();
122 }
123 
124 sal_Int8 SAL_CALL Graphic::getType()
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 
139 awt::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 
151 uno::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 
168 uno::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 
185 sal_Int64 SAL_CALL Graphic::getSomething( const uno::Sequence< sal_Int8 >& rId )
186 {
188 }
189 
190 
191 // XGraphicTransformer
192 uno::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(static_cast< sal_uInt8 >(nColorFrom), static_cast< sal_uInt8 >(nColorFrom >> 8), static_cast< sal_uInt8 >(nColorFrom >> 16));
199  BitmapColor aBmpColorTo( static_cast< sal_uInt8 >(nColorTo), static_cast< sal_uInt8 >(nColorTo >> 8), static_cast< sal_uInt8 >(nColorTo >> 16));
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 
240 uno::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 
260 uno::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
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
bool IsNone() const
Definition: graph.cxx:282
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
Definition: graph.cxx:411
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
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
GraphicType GetType() const
Definition: graph.cxx:293
unsigned char sal_Bool
ColorTransparency
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:329
virtual void SAL_CALL acquire() noexcept override
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
Graphic()
Definition: graph.cxx:161
AlphaMask GetAlpha() const
Definition: BitmapEx.cxx:215
constexpr tools::Long Height() const
unsigned char sal_uInt8
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
void init(const ::Graphic &rGraphic)
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
bool WriteDIB(const Bitmap &rSource, SvStream &rOStm, bool bCompressed, bool bFileHeader)
Definition: dibtools.cxx:1818
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
const void * GetData()
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override