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 :
21 : #include "vcl/bitmap.hxx"
22 : #include "vcl/svapp.hxx"
23 : #include "vcl/salctype.hxx"
24 : #include <osl/mutex.hxx>
25 : #include "tools/stream.hxx"
26 : #include "com/sun/star/script/XInvocation.hpp"
27 : #include "com/sun/star/awt/XBitmap.hpp"
28 : #include "cppuhelper/compbase1.hxx"
29 :
30 :
31 : using namespace com::sun::star::uno;
32 : using namespace com::sun::star::script;
33 : using namespace com::sun::star::beans;
34 : using namespace com::sun::star::reflection;
35 : using namespace com::sun::star::awt;
36 :
37 : using ::rtl::OUString;
38 :
39 : namespace vcl {
40 :
41 : class BmpTransporter :
42 : public cppu::WeakImplHelper1< com::sun::star::awt::XBitmap >
43 : {
44 : Sequence<sal_Int8> m_aBM;
45 : com::sun::star::awt::Size m_aSize;
46 : public:
47 : BmpTransporter( const Bitmap& rBM );
48 : virtual ~BmpTransporter();
49 :
50 : virtual com::sun::star::awt::Size SAL_CALL getSize() throw();
51 : virtual Sequence< sal_Int8 > SAL_CALL getDIB() throw();
52 : virtual Sequence< sal_Int8 > SAL_CALL getMaskDIB() throw();
53 : };
54 :
55 : class BmpConverter :
56 : public cppu::WeakImplHelper1< com::sun::star::script::XInvocation >
57 : {
58 : public:
59 : BmpConverter();
60 : virtual ~BmpConverter();
61 :
62 : virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection() throw();
63 : virtual void SAL_CALL setValue( const OUString& rProperty, const Any& rValue )
64 : throw( UnknownPropertyException );
65 : virtual Any SAL_CALL getValue( const OUString& rProperty )
66 : throw( UnknownPropertyException );
67 : virtual sal_Bool SAL_CALL hasMethod( const OUString& rName ) throw();
68 : virtual sal_Bool SAL_CALL hasProperty( const OUString& rProp ) throw();
69 :
70 : virtual Any SAL_CALL invoke( const OUString& rFunction,
71 : const Sequence< Any >& rParams,
72 : Sequence< sal_Int16 >& rOutParamIndex,
73 : Sequence< Any >& rOutParam
74 : )
75 : throw( CannotConvertException, InvocationTargetException );
76 : };
77 :
78 : }
79 :
80 : using namespace vcl;
81 :
82 1476 : Reference< XInvocation > vcl::createBmpConverter()
83 : {
84 1476 : return static_cast<XInvocation*>(new BmpConverter());
85 : }
86 :
87 1476 : BmpConverter::BmpConverter()
88 : {
89 1476 : }
90 :
91 2952 : BmpConverter::~BmpConverter()
92 : {
93 2952 : }
94 :
95 0 : Reference< XIntrospectionAccess > SAL_CALL BmpConverter::getIntrospection() throw()
96 : {
97 0 : return Reference< XIntrospectionAccess >();
98 : }
99 :
100 0 : void SAL_CALL BmpConverter::setValue( const OUString&, const Any& ) throw( UnknownPropertyException )
101 : {
102 0 : throw UnknownPropertyException();
103 : }
104 :
105 0 : Any SAL_CALL BmpConverter::getValue( const OUString& ) throw( UnknownPropertyException )
106 : {
107 0 : throw UnknownPropertyException();
108 : }
109 :
110 0 : sal_Bool SAL_CALL BmpConverter::hasMethod( const OUString& rName ) throw()
111 : {
112 0 : return rName.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") );
113 : }
114 :
115 0 : sal_Bool SAL_CALL BmpConverter::hasProperty( const OUString& ) throw()
116 : {
117 0 : return sal_False;
118 : }
119 :
120 0 : Any SAL_CALL BmpConverter::invoke(
121 : const OUString& rFunction,
122 : const Sequence< Any >& rParams,
123 : Sequence< sal_Int16 >&,
124 : Sequence< Any >& )
125 : throw( CannotConvertException, InvocationTargetException )
126 : {
127 0 : Any aRet;
128 :
129 0 : if( rFunction.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") ) )
130 : {
131 0 : Reference< XBitmap > xBM;
132 0 : sal_uInt16 nTargetDepth = 0;
133 0 : if( rParams.getLength() != 2 )
134 0 : throw CannotConvertException();
135 :
136 0 : if( ! (rParams.getConstArray()[0] >>= xBM ) ||
137 0 : ! ( rParams.getConstArray()[1] >>= nTargetDepth ) )
138 0 : throw CannotConvertException();
139 :
140 0 : Sequence< sal_Int8 > aDIB = xBM->getDIB();
141 :
142 : // call into vcl not thread safe
143 0 : SolarMutexGuard aGuard;
144 :
145 0 : SvMemoryStream aStream( aDIB.getArray(), aDIB.getLength(), STREAM_READ | STREAM_WRITE );
146 0 : Bitmap aBM;
147 0 : aBM.Read( aStream, sal_True );
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 0 : SvMemoryStream aStream;
182 0 : rBM.Write( aStream, sal_False, sal_True );
183 0 : m_aBM = Sequence<sal_Int8>(static_cast<const sal_Int8*>(aStream.GetData()),
184 0 : aStream.GetEndOfData());
185 0 : }
186 :
187 0 : BmpTransporter::~BmpTransporter()
188 : {
189 0 : }
190 :
191 0 : com::sun::star::awt::Size SAL_CALL BmpTransporter::getSize() throw()
192 : {
193 0 : return m_aSize;
194 : }
195 :
196 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getDIB() throw()
197 : {
198 0 : return m_aBM;
199 : }
200 :
201 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getMaskDIB() throw()
202 : {
203 0 : return Sequence< sal_Int8 >();
204 : }
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|