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 
63  if (aBitmapEx.IsAlpha())
64  {
65  aBitmapEx.setAlphaFrom( cIndexFrom, 0xff - nAlphaTo );
66  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
67  aReturnGraphic = ::Graphic(aBitmapEx);
68  }
69  else
70  {
71  if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)))
72  {
73  Bitmap aBitmap(aBitmapEx.GetBitmap());
74  Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance));
75  aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
76  aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
77  }
78  else
79  {
80  aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo);
81  aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
82  aReturnGraphic = ::Graphic(aBitmapEx);
83  }
84  }
85  }
86 
87  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
88  return aReturnGraphic.GetXGraphic();
89 }
90 
91 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone(
92  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
93 {
94  ::Graphic aGraphic(rxGraphic);
95  ::Graphic aReturnGraphic;
96 
97  BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
98  AlphaMask aMask( aBitmapEx.GetAlpha() );
99 
100  BitmapEx aTmpBmpEx(aBitmapEx.GetBitmap());
101  BitmapFilter::Filter(aTmpBmpEx,
103  Color(ColorTransparency, nColorOne),
104  Color(ColorTransparency, nColorTwo)));
105 
106  aReturnGraphic = ::Graphic( BitmapEx( aTmpBmpEx.GetBitmap(), aMask ) );
107  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
108  return aReturnGraphic.GetXGraphic();
109 }
110 
111 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyBrightnessContrast(
112  const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso )
113 {
114  ::Graphic aGraphic(rxGraphic);
115  ::Graphic aReturnGraphic;
116 
117  BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
118  aBitmapEx.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso);
119  aReturnGraphic = ::Graphic(aBitmapEx);
120  aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
121  return aReturnGraphic.GetXGraphic();
122 }
123 
124 }
125 
126 /* 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
signed char sal_Int8
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
unsigned char sal_Bool
ColorTransparency
unsigned char sal_uInt8
void setAlphaFrom(sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo)
Definition: BitmapEx.cxx:1367
bool Replace(const Bitmap &rMask, const Color &rReplaceColor)
Replace all pixel where the given mask is on with the specified color.