LibreOffice Module vcl (master)  1
UnoGraphicTransformer.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 
22 
23 #include <vcl/graph.hxx>
24 #include <vcl/BitmapColor.hxx>
26 
27 using namespace com::sun::star;
28 
29 namespace unographic {
30 
31 
32 GraphicTransformer::GraphicTransformer()
33 {
34 }
35 
36 
37 GraphicTransformer::~GraphicTransformer()
38 {
39 }
40 
41 
42 // XGraphicTransformer
43 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
44  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
45 {
46  ::Graphic aGraphic(rxGraphic);
47  ::Graphic aReturnGraphic;
48 
49  BitmapColor aBmpColorFrom(static_cast< sal_uInt8 >(nColorFrom), static_cast< sal_uInt8 >(nColorFrom >> 8), static_cast< sal_uInt8 >(nColorFrom >> 16));
50  BitmapColor aBmpColorTo( static_cast< sal_uInt8 >(nColorTo), static_cast< sal_uInt8 >(nColorTo >> 8), static_cast< sal_uInt8 >(nColorTo >> 16));
51 
52  Color aColorFrom(aBmpColorFrom);
53  Color aColorTo(aBmpColorTo);
54 
55  const sal_uInt8 cIndexFrom = aBmpColorFrom.GetIndex();
56 
57  if (aGraphic.GetType() == GraphicType::Bitmap ||
58  aGraphic.GetType() == GraphicType::GdiMetafile)
59  {
60  BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
61  Bitmap aBitmap(aBitmapEx.GetBitmap());
62 
63  if (aBitmapEx.IsAlpha())
64  {
65  aBitmapEx.setAlphaFrom( cIndexFrom, nAlphaTo );
66  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
67  aReturnGraphic = ::Graphic(aBitmapEx);
68  }
69  else if (aBitmapEx.IsTransparent())
70  {
71  if (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff))
72  {
73  Bitmap aMask(aBitmapEx.GetMask());
74  Bitmap aMask2(aBitmap.CreateMask(aColorFrom, nTolerance));
75  aMask.CombineSimple(aMask2, BmpCombine::Or);
76  aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
77  aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
78  }
79  else
80  {
81  aBitmapEx.setAlphaFrom(cIndexFrom, 0xff - nAlphaTo);
82  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
83  aReturnGraphic = ::Graphic(aBitmapEx);
84  }
85  }
86  else
87  {
88  if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)))
89  {
90  Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance));
91  aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
92  aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
93  }
94  else
95  {
96  aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo);
97  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
98  aReturnGraphic = ::Graphic(aBitmapEx);
99  }
100  }
101  }
102 
103  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
104  return aReturnGraphic.GetXGraphic();
105 }
106 
107 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone(
108  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
109 {
110  ::Graphic aGraphic(rxGraphic);
111  ::Graphic aReturnGraphic;
112 
113  BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
114  AlphaMask aMask( aBitmapEx.GetAlpha() );
115  Bitmap aBitmap( aBitmapEx.GetBitmap() );
116 
117  BitmapEx aTmpBmpEx(aBitmap);
118  BitmapFilter::Filter(aTmpBmpEx, BitmapDuoToneFilter(static_cast<sal_uLong>(nColorOne), static_cast<sal_uLong>(nColorTwo)));
119  aBitmap = aTmpBmpEx.GetBitmap();
120 
121  aReturnGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
122  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
123  return aReturnGraphic.GetXGraphic();
124 }
125 
126 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyBrightnessContrast(
127  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso )
128 {
129  ::Graphic aGraphic(rxGraphic);
130  ::Graphic aReturnGraphic;
131 
132  BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
133  aBitmapEx.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso);
134  aReturnGraphic = ::Graphic(aBitmapEx);
135  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
136  return aReturnGraphic.GetXGraphic();
137 }
138 
139 }
140 
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:61
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:608
signed char sal_Int8
bool CombineSimple(const Bitmap &rMask, BmpCombine eCombine)
Perform boolean operations with another bitmap.
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
unsigned char sal_Bool
unsigned char sal_uInt8
bool Replace(const Bitmap &rMask, const Color &rReplaceColor)
Replace all pixel where the given mask is on with the specified color.