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  //TODO This code convert GdiMetafile(vector graphic) to Bitmap, which cause to information lost
58  if (aGraphic.GetType() == GraphicType::Bitmap ||
59  aGraphic.GetType() == GraphicType::GdiMetafile)
60  {
61  BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
62  Bitmap aBitmap(aBitmapEx.GetBitmap());
63 
64  if (aBitmapEx.IsAlpha())
65  {
66  aBitmapEx.setAlphaFrom( cIndexFrom, 0xff - nAlphaTo );
67  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
68  aReturnGraphic = ::Graphic(aBitmapEx);
69  }
70  else if (aBitmapEx.IsTransparent())
71  {
72  if (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff))
73  {
74  Bitmap aMask(aBitmapEx.GetMask());
75  Bitmap aMask2(aBitmap.CreateMask(aColorFrom, nTolerance));
76  aMask.CombineSimple(aMask2, BmpCombine::Or);
77  aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
78  aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
79  }
80  else
81  {
82  aBitmapEx.setAlphaFrom(cIndexFrom, 0xff - nAlphaTo);
83  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
84  aReturnGraphic = ::Graphic(aBitmapEx);
85  }
86  }
87  else
88  {
89  if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)))
90  {
91  Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance));
92  aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
93  aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
94  }
95  else
96  {
97  aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo);
98  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
99  aReturnGraphic = ::Graphic(aBitmapEx);
100  }
101  }
102  }
103 
104  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
105  return aReturnGraphic.GetXGraphic();
106 }
107 
108 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone(
109  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
110 {
111  ::Graphic aGraphic(rxGraphic);
112  ::Graphic aReturnGraphic;
113 
114  BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
115  AlphaMask aMask( aBitmapEx.GetAlpha() );
116 
117  BitmapEx aTmpBmpEx(aBitmapEx.GetBitmap());
118  BitmapFilter::Filter(aTmpBmpEx, BitmapDuoToneFilter(static_cast<sal_uLong>(nColorOne), static_cast<sal_uLong>(nColorTwo)));
119 
120  aReturnGraphic = ::Graphic( BitmapEx( aTmpBmpEx.GetBitmap(), aMask ) );
121  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
122  return aReturnGraphic.GetXGraphic();
123 }
124 
125 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyBrightnessContrast(
126  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso )
127 {
128  ::Graphic aGraphic(rxGraphic);
129  ::Graphic aReturnGraphic;
130 
131  BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
132  aBitmapEx.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso);
133  aReturnGraphic = ::Graphic(aBitmapEx);
134  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
135  return aReturnGraphic.GetXGraphic();
136 }
137 
138 }
139 
140 /* 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:603
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.