/src/libreoffice/vcl/source/graphic/UnoGraphic.cxx
Line | Count | Source |
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/alpha.hxx> |
27 | | #include <vcl/dibtools.hxx> |
28 | | #include <vcl/graph.hxx> |
29 | | #include <vcl/BitmapColor.hxx> |
30 | | #include <vcl/BitmapTools.hxx> |
31 | | #include <comphelper/servicehelper.hxx> |
32 | | #include <cppuhelper/supportsservice.hxx> |
33 | | #include <cppuhelper/queryinterface.hxx> |
34 | | #include <cppuhelper/typeprovider.hxx> |
35 | | |
36 | | #include <bitmap/BitmapDuoToneFilter.hxx> |
37 | | |
38 | | using namespace com::sun::star; |
39 | | |
40 | | namespace unographic { |
41 | | |
42 | | Graphic::Graphic(const ::Graphic& rGraphic) |
43 | 49.4k | : maGraphic(rGraphic) |
44 | 49.4k | { |
45 | 49.4k | unographic::GraphicDescriptor::init(maGraphic); |
46 | 49.4k | } |
47 | | |
48 | | Graphic::~Graphic() noexcept |
49 | 49.4k | { |
50 | 49.4k | } |
51 | | |
52 | | uno::Any SAL_CALL Graphic::queryInterface( const uno::Type & rType ) |
53 | 4.93k | { |
54 | 4.93k | uno::Any aAny; |
55 | 4.93k | if( rType == cppu::UnoType<graphic::XGraphic>::get()) |
56 | 2.44k | aAny <<= uno::Reference< graphic::XGraphic >( this ); |
57 | 2.49k | else if( rType == cppu::UnoType<awt::XBitmap>::get()) |
58 | 254 | aAny <<= uno::Reference< awt::XBitmap >( this ); |
59 | 2.23k | else if( rType == cppu::UnoType<graphic::XGraphicTransformer>::get()) |
60 | 10 | aAny <<= uno::Reference< graphic::XGraphicTransformer >(this); |
61 | 2.22k | else |
62 | 2.22k | aAny = ::unographic::GraphicDescriptor::queryInterface( rType ); |
63 | | |
64 | 4.93k | return aAny; |
65 | 4.93k | } |
66 | | |
67 | | void SAL_CALL Graphic::acquire() |
68 | | noexcept |
69 | 258k | { |
70 | 258k | unographic::GraphicDescriptor::acquire(); |
71 | 258k | } |
72 | | |
73 | | void SAL_CALL Graphic::release() noexcept |
74 | 258k | { |
75 | 258k | unographic::GraphicDescriptor::release(); |
76 | 258k | } |
77 | | |
78 | | OUString SAL_CALL Graphic::getImplementationName() |
79 | 0 | { |
80 | 0 | return u"com.sun.star.comp.graphic.Graphic"_ustr; |
81 | 0 | } |
82 | | |
83 | | sal_Bool SAL_CALL Graphic::supportsService( const OUString& rServiceName ) |
84 | 0 | { |
85 | 0 | return cppu::supportsService( this, rServiceName ); |
86 | 0 | } |
87 | | |
88 | | uno::Sequence< OUString > SAL_CALL Graphic::getSupportedServiceNames() |
89 | 0 | { |
90 | 0 | uno::Sequence< OUString > aRet( ::unographic::GraphicDescriptor::getSupportedServiceNames() ); |
91 | 0 | const uno::Sequence< OUString > aNew { u"com.sun.star.graphic.Graphic"_ustr }; |
92 | 0 | sal_Int32 nOldCount = aRet.getLength(); |
93 | |
|
94 | 0 | aRet.realloc( nOldCount + aNew.getLength() ); |
95 | |
|
96 | 0 | std::copy(aNew.begin(), aNew.end(), std::next(aRet.getArray(), nOldCount)); |
97 | |
|
98 | 0 | return aRet; |
99 | 0 | } |
100 | | |
101 | | uno::Sequence< uno::Type > SAL_CALL Graphic::getTypes() |
102 | 0 | { |
103 | 0 | return cppu::OTypeCollection( |
104 | 0 | cppu::UnoType<graphic::XGraphic>::get(), |
105 | 0 | cppu::UnoType<awt::XBitmap>::get(), |
106 | 0 | ::unographic::GraphicDescriptor::getTypes() |
107 | 0 | ).getTypes(); |
108 | 0 | } |
109 | | |
110 | | sal_Int8 SAL_CALL Graphic::getType() |
111 | 0 | { |
112 | 0 | sal_Int8 cRet = graphic::GraphicType::EMPTY; |
113 | |
|
114 | 0 | if (!maGraphic.IsNone()) |
115 | 0 | { |
116 | 0 | cRet = (maGraphic.GetType() == ::GraphicType::Bitmap) ? graphic::GraphicType::PIXEL |
117 | 0 | : graphic::GraphicType::VECTOR; |
118 | 0 | } |
119 | |
|
120 | 0 | return cRet; |
121 | 0 | } |
122 | | |
123 | | // XBitmap |
124 | | |
125 | | awt::Size SAL_CALL Graphic::getSize() |
126 | 0 | { |
127 | 0 | SolarMutexGuard aGuard; |
128 | |
|
129 | 0 | Size aVclSize; |
130 | 0 | if (!maGraphic.IsNone()) |
131 | 0 | { |
132 | 0 | aVclSize = maGraphic.GetSizePixel(); |
133 | 0 | } |
134 | 0 | return awt::Size(aVclSize.Width(), aVclSize.Height()); |
135 | 0 | } |
136 | | |
137 | | uno::Sequence<sal_Int8> SAL_CALL Graphic::getDIB() |
138 | 0 | { |
139 | 0 | SolarMutexGuard aGuard; |
140 | |
|
141 | 0 | if (!maGraphic.IsNone()) |
142 | 0 | { |
143 | 0 | SvMemoryStream aMemoryStream; |
144 | |
|
145 | 0 | WriteDIB(maGraphic.GetBitmap().CreateColorBitmap(), aMemoryStream, false, true); |
146 | 0 | return css::uno::Sequence<sal_Int8>(static_cast<sal_Int8 const *>(aMemoryStream.GetData()), aMemoryStream.Tell()); |
147 | 0 | } |
148 | 0 | else |
149 | 0 | { |
150 | 0 | return uno::Sequence<sal_Int8>(); |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | uno::Sequence<sal_Int8> SAL_CALL Graphic::getMaskDIB() |
155 | 0 | { |
156 | 0 | SolarMutexGuard aGuard; |
157 | |
|
158 | 0 | if (!maGraphic.IsNone()) |
159 | 0 | { |
160 | 0 | return vcl::bitmap::GetMaskDIB(maGraphic.GetBitmap()); |
161 | 0 | } |
162 | 0 | else |
163 | 0 | { |
164 | 0 | return uno::Sequence<sal_Int8>(); |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | // XGraphicTransformer |
169 | | uno::Reference< graphic::XGraphic > SAL_CALL Graphic::colorChange( |
170 | | const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo ) |
171 | 0 | { |
172 | 0 | ::Graphic aGraphic(rxGraphic); |
173 | 0 | ::Graphic aReturnGraphic; |
174 | |
|
175 | 0 | BitmapColor aBmpColorFrom(Color(ColorTransparency, static_cast<sal_uInt32>(nColorFrom))); |
176 | 0 | BitmapColor aBmpColorTo(Color(ColorTransparency, static_cast<sal_uInt32>(nColorTo))); |
177 | |
|
178 | 0 | Color aColorFrom(aBmpColorFrom); |
179 | 0 | Color aColorTo(aBmpColorTo); |
180 | |
|
181 | 0 | const sal_uInt8 cIndexFrom = aBmpColorFrom.GetIndex(); |
182 | | |
183 | | //TODO This code convert GdiMetafile(vector graphic) to Bitmap, which cause to information lost |
184 | 0 | if (aGraphic.GetType() == GraphicType::Bitmap || |
185 | 0 | aGraphic.GetType() == GraphicType::GdiMetafile) |
186 | 0 | { |
187 | 0 | Bitmap aBitmap(aGraphic.GetBitmap()); |
188 | |
|
189 | 0 | if (aBitmap.HasAlpha()) |
190 | 0 | { |
191 | 0 | aBitmap.ChangeColorAlpha( cIndexFrom, nAlphaTo ); |
192 | 0 | aBitmap.Replace(aColorFrom, aColorTo, nTolerance); |
193 | 0 | aReturnGraphic = ::Graphic(aBitmap); |
194 | 0 | } |
195 | 0 | else |
196 | 0 | { |
197 | 0 | if ((nAlphaTo == 0) || (nAlphaTo == sal::static_int_cast< sal_Int8 >(0xff))) |
198 | 0 | { |
199 | 0 | Bitmap aBitmap2(aBitmap); |
200 | 0 | AlphaMask aMask(aBitmap2.CreateAlphaMask(aColorFrom, nTolerance)); |
201 | 0 | aBitmap2.Replace(aColorFrom, aColorTo, nTolerance); |
202 | 0 | aReturnGraphic = ::Graphic(Bitmap(aBitmap2, aMask)); |
203 | 0 | } |
204 | 0 | else |
205 | 0 | { |
206 | 0 | aBitmap.Replace(aColorFrom, aColorTo, nTolerance); |
207 | 0 | aReturnGraphic = ::Graphic(aBitmap); |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | |
|
212 | 0 | aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); |
213 | 0 | return aReturnGraphic.GetXGraphic(); |
214 | 0 | } |
215 | | |
216 | | uno::Reference< graphic::XGraphic > SAL_CALL Graphic::applyDuotone( |
217 | | const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo ) |
218 | 10 | { |
219 | 10 | ::Graphic aGraphic(rxGraphic); |
220 | 10 | Bitmap aBitmap( aGraphic.GetBitmap() ); |
221 | | |
222 | 10 | BitmapFilter::Filter(aBitmap, |
223 | 10 | BitmapDuoToneFilter( |
224 | 10 | Color(ColorTransparency, nColorOne), |
225 | 10 | Color(ColorTransparency, nColorTwo))); |
226 | | |
227 | 10 | ::Graphic aReturnGraphic(aBitmap); |
228 | 10 | aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); |
229 | 10 | return aReturnGraphic.GetXGraphic(); |
230 | 10 | } |
231 | | |
232 | | uno::Reference< graphic::XGraphic > SAL_CALL Graphic::applyBrightnessContrast( |
233 | | const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso ) |
234 | 0 | { |
235 | 0 | ::Graphic aGraphic(rxGraphic); |
236 | 0 | ::Graphic aReturnGraphic; |
237 | |
|
238 | 0 | Bitmap aBitmap(aGraphic.GetBitmap()); |
239 | 0 | aBitmap.Adjust(nBrightness, nContrast, 0, 0, 0, 0, false, mso); |
240 | 0 | aReturnGraphic = ::Graphic(aBitmap); |
241 | 0 | aReturnGraphic.setOriginURL(aGraphic.getOriginURL()); |
242 | 0 | return aReturnGraphic.GetXGraphic(); |
243 | 0 | } |
244 | | |
245 | | |
246 | | } |
247 | | |
248 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |