Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / graphic / UnoGraphicTransformer.cxx
blob30fd389b9485df390d7803497842577843243a8c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <graphic/UnoGraphicTransformer.hxx>
23 #include <vcl/graph.hxx>
24 #include <vcl/BitmapColor.hxx>
25 #include <vcl/BitmapDuoToneFilter.hxx>
27 using namespace com::sun::star;
29 namespace unographic {
32 GraphicTransformer::GraphicTransformer()
37 GraphicTransformer::~GraphicTransformer()
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 )
46 ::Graphic aGraphic(rxGraphic);
47 ::Graphic aReturnGraphic;
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));
52 Color aColorFrom(aBmpColorFrom);
53 Color aColorTo(aBmpColorTo);
55 const sal_uInt8 cIndexFrom = aBmpColorFrom.GetIndex();
57 if (aGraphic.GetType() == GraphicType::Bitmap ||
58 aGraphic.GetType() == GraphicType::GdiMetafile)
60 BitmapEx aBitmapEx(aGraphic.GetBitmapEx());
61 Bitmap aBitmap(aBitmapEx.GetBitmap());
63 if (aBitmapEx.IsAlpha())
65 aBitmapEx.setAlphaFrom( cIndexFrom, nAlphaTo );
66 aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
67 aReturnGraphic = ::Graphic(aBitmapEx);
69 else if (aBitmapEx.IsTransparent())
71 if (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff))
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));
79 else
81 aBitmapEx.setAlphaFrom(cIndexFrom, 0xff - nAlphaTo);
82 aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
83 aReturnGraphic = ::Graphic(aBitmapEx);
86 else
88 if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff)))
90 Bitmap aMask(aBitmap.CreateMask(aColorFrom, nTolerance));
91 aBitmap.Replace(aColorFrom, aColorTo, nTolerance);
92 aReturnGraphic = ::Graphic(BitmapEx(aBitmap, aMask));
94 else
96 aBitmapEx.setAlphaFrom(cIndexFrom, nAlphaTo);
97 aBitmapEx.Replace(aColorFrom, aColorTo, nTolerance);
98 aReturnGraphic = ::Graphic(aBitmapEx);
103 aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
104 return aReturnGraphic.GetXGraphic();
107 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone(
108 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
110 ::Graphic aGraphic(rxGraphic);
111 ::Graphic aReturnGraphic;
113 BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
114 AlphaMask aMask( aBitmapEx.GetAlpha() );
115 Bitmap aBitmap( aBitmapEx.GetBitmap() );
117 BitmapEx aTmpBmpEx(aBitmap);
118 BitmapFilter::Filter(aTmpBmpEx, BitmapDuoToneFilter(static_cast<sal_uLong>(nColorOne), static_cast<sal_uLong>(nColorTwo)));
119 aBitmap = aTmpBmpEx.GetBitmap();
121 aReturnGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
122 aReturnGraphic.setOriginURL(aGraphic.getOriginURL());
123 return aReturnGraphic.GetXGraphic();
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 )
129 ::Graphic aGraphic(rxGraphic);
130 ::Graphic aReturnGraphic;
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();
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */