Line data Source code
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 "vcl/bitmap.hxx"
21 : #include "vcl/svapp.hxx"
22 : #include "vcl/salctype.hxx"
23 : #include <osl/mutex.hxx>
24 : #include "tools/stream.hxx"
25 : #include "com/sun/star/script/XInvocation.hpp"
26 : #include "com/sun/star/awt/XBitmap.hpp"
27 : #include "cppuhelper/compbase1.hxx"
28 : #include <vcl/dibtools.hxx>
29 :
30 : using namespace com::sun::star::uno;
31 : using namespace com::sun::star::script;
32 : using namespace com::sun::star::beans;
33 : using namespace com::sun::star::reflection;
34 : using namespace com::sun::star::awt;
35 :
36 :
37 : namespace vcl {
38 :
39 : class BmpTransporter :
40 : public cppu::WeakImplHelper1< com::sun::star::awt::XBitmap >
41 : {
42 : Sequence<sal_Int8> m_aBM;
43 : com::sun::star::awt::Size m_aSize;
44 : public:
45 : BmpTransporter( const Bitmap& rBM );
46 : virtual ~BmpTransporter();
47 :
48 : virtual com::sun::star::awt::Size SAL_CALL getSize() throw();
49 : virtual Sequence< sal_Int8 > SAL_CALL getDIB() throw();
50 : virtual Sequence< sal_Int8 > SAL_CALL getMaskDIB() throw();
51 : };
52 :
53 : class BmpConverter :
54 : public cppu::WeakImplHelper1< com::sun::star::script::XInvocation >
55 : {
56 : public:
57 : BmpConverter();
58 : virtual ~BmpConverter();
59 :
60 : virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection() throw();
61 : virtual void SAL_CALL setValue( const OUString& rProperty, const Any& rValue )
62 : throw( UnknownPropertyException );
63 : virtual Any SAL_CALL getValue( const OUString& rProperty )
64 : throw( UnknownPropertyException );
65 : virtual sal_Bool SAL_CALL hasMethod( const OUString& rName ) throw();
66 : virtual sal_Bool SAL_CALL hasProperty( const OUString& rProp ) throw();
67 :
68 : virtual Any SAL_CALL invoke( const OUString& rFunction,
69 : const Sequence< Any >& rParams,
70 : Sequence< sal_Int16 >& rOutParamIndex,
71 : Sequence< Any >& rOutParam
72 : )
73 : throw( CannotConvertException, InvocationTargetException );
74 : };
75 :
76 : }
77 :
78 : using namespace vcl;
79 :
80 5833 : Reference< XInvocation > vcl::createBmpConverter()
81 : {
82 5833 : return static_cast<XInvocation*>(new BmpConverter());
83 : }
84 :
85 5833 : BmpConverter::BmpConverter()
86 : {
87 5833 : }
88 :
89 11666 : BmpConverter::~BmpConverter()
90 : {
91 11666 : }
92 :
93 0 : Reference< XIntrospectionAccess > SAL_CALL BmpConverter::getIntrospection() throw()
94 : {
95 0 : return Reference< XIntrospectionAccess >();
96 : }
97 :
98 0 : void SAL_CALL BmpConverter::setValue( const OUString&, const Any& ) throw( UnknownPropertyException )
99 : {
100 0 : throw UnknownPropertyException();
101 : }
102 :
103 0 : Any SAL_CALL BmpConverter::getValue( const OUString& ) throw( UnknownPropertyException )
104 : {
105 0 : throw UnknownPropertyException();
106 : }
107 :
108 0 : sal_Bool SAL_CALL BmpConverter::hasMethod( const OUString& rName ) throw()
109 : {
110 0 : return rName.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") );
111 : }
112 :
113 0 : sal_Bool SAL_CALL BmpConverter::hasProperty( const OUString& ) throw()
114 : {
115 0 : return sal_False;
116 : }
117 :
118 0 : Any SAL_CALL BmpConverter::invoke(
119 : const OUString& rFunction,
120 : const Sequence< Any >& rParams,
121 : Sequence< sal_Int16 >&,
122 : Sequence< Any >& )
123 : throw( CannotConvertException, InvocationTargetException )
124 : {
125 0 : Any aRet;
126 :
127 0 : if( rFunction.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") ) )
128 : {
129 0 : Reference< XBitmap > xBM;
130 0 : sal_uInt16 nTargetDepth = 0;
131 0 : if( rParams.getLength() != 2 )
132 0 : throw CannotConvertException();
133 :
134 0 : if( ! (rParams.getConstArray()[0] >>= xBM ) ||
135 0 : ! ( rParams.getConstArray()[1] >>= nTargetDepth ) )
136 0 : throw CannotConvertException();
137 :
138 0 : Sequence< sal_Int8 > aDIB = xBM->getDIB();
139 :
140 : // call into vcl not thread safe
141 0 : SolarMutexGuard aGuard;
142 :
143 0 : SvMemoryStream aStream( aDIB.getArray(), aDIB.getLength(), STREAM_READ | STREAM_WRITE );
144 0 : Bitmap aBM;
145 :
146 0 : ReadDIB(aBM, aStream, true);
147 :
148 0 : if( nTargetDepth < 4 )
149 0 : nTargetDepth = 1;
150 0 : else if( nTargetDepth < 8 )
151 0 : nTargetDepth = 4;
152 0 : else if( nTargetDepth >8 && nTargetDepth < 24 )
153 0 : nTargetDepth = 24;
154 :
155 0 : if( aBM.GetBitCount() == 24 && nTargetDepth <= 8 )
156 0 : aBM.Dither( BMP_DITHER_FLOYD );
157 :
158 0 : if( aBM.GetBitCount() != nTargetDepth )
159 : {
160 0 : switch( nTargetDepth )
161 : {
162 0 : case 1: aBM.Convert( BMP_CONVERSION_1BIT_THRESHOLD );break;
163 0 : case 4: aBM.ReduceColors( BMP_CONVERSION_4BIT_COLORS );break;
164 0 : case 8: aBM.ReduceColors( BMP_CONVERSION_8BIT_COLORS );break;
165 0 : case 24: aBM.Convert( BMP_CONVERSION_24BIT );break;
166 : }
167 : }
168 0 : xBM = new BmpTransporter( aBM );
169 0 : aRet <<= xBM;
170 : }
171 : else
172 0 : throw InvocationTargetException();
173 :
174 0 : return aRet;
175 : }
176 :
177 0 : BmpTransporter::BmpTransporter( const Bitmap& rBM )
178 : {
179 0 : m_aSize.Width = rBM.GetSizePixel().Width();
180 0 : m_aSize.Height = rBM.GetSizePixel().Height();
181 :
182 0 : SvMemoryStream aStream;
183 :
184 0 : WriteDIB(rBM, aStream, false, true);
185 :
186 0 : m_aBM = Sequence<sal_Int8>(static_cast<const sal_Int8*>(aStream.GetData()),
187 0 : aStream.GetEndOfData());
188 0 : }
189 :
190 0 : BmpTransporter::~BmpTransporter()
191 : {
192 0 : }
193 :
194 0 : com::sun::star::awt::Size SAL_CALL BmpTransporter::getSize() throw()
195 : {
196 0 : return m_aSize;
197 : }
198 :
199 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getDIB() throw()
200 : {
201 0 : return m_aBM;
202 : }
203 :
204 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getMaskDIB() throw()
205 : {
206 0 : return Sequence< sal_Int8 >();
207 465 : }
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|