1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "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"
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
;
40 class BmpTransporter
:
41 public cppu::WeakImplHelper1
< com::sun::star::awt::XBitmap
>
43 Sequence
<sal_Int8
> m_aBM
;
44 com::sun::star::awt::Size m_aSize
;
46 BmpTransporter( const Bitmap
& rBM
);
47 virtual ~BmpTransporter();
49 virtual com::sun::star::awt::Size SAL_CALL
getSize() throw();
50 virtual Sequence
< sal_Int8
> SAL_CALL
getDIB() throw();
51 virtual Sequence
< sal_Int8
> SAL_CALL
getMaskDIB() throw();
55 public cppu::WeakImplHelper1
< com::sun::star::script::XInvocation
>
59 virtual ~BmpConverter();
61 virtual Reference
< XIntrospectionAccess
> SAL_CALL
getIntrospection() throw();
62 virtual void SAL_CALL
setValue( const OUString
& rProperty
, const Any
& rValue
)
63 throw( UnknownPropertyException
);
64 virtual Any SAL_CALL
getValue( const OUString
& rProperty
)
65 throw( UnknownPropertyException
);
66 virtual sal_Bool SAL_CALL
hasMethod( const OUString
& rName
) throw();
67 virtual sal_Bool SAL_CALL
hasProperty( const OUString
& rProp
) throw();
69 virtual Any SAL_CALL
invoke( const OUString
& rFunction
,
70 const Sequence
< Any
>& rParams
,
71 Sequence
< sal_Int16
>& rOutParamIndex
,
72 Sequence
< Any
>& rOutParam
74 throw( CannotConvertException
, InvocationTargetException
);
81 Reference
< XInvocation
> vcl::createBmpConverter()
83 return static_cast<XInvocation
*>(new BmpConverter());
86 BmpConverter::BmpConverter()
90 BmpConverter::~BmpConverter()
94 Reference
< XIntrospectionAccess
> SAL_CALL
BmpConverter::getIntrospection() throw()
96 return Reference
< XIntrospectionAccess
>();
99 void SAL_CALL
BmpConverter::setValue( const OUString
&, const Any
& ) throw( UnknownPropertyException
)
101 throw UnknownPropertyException();
104 Any SAL_CALL
BmpConverter::getValue( const OUString
& ) throw( UnknownPropertyException
)
106 throw UnknownPropertyException();
109 sal_Bool SAL_CALL
BmpConverter::hasMethod( const OUString
& rName
) throw()
111 return rName
.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") );
114 sal_Bool SAL_CALL
BmpConverter::hasProperty( const OUString
& ) throw()
119 Any SAL_CALL
BmpConverter::invoke(
120 const OUString
& rFunction
,
121 const Sequence
< Any
>& rParams
,
122 Sequence
< sal_Int16
>&,
124 throw( CannotConvertException
, InvocationTargetException
)
128 if( rFunction
.equalsIgnoreAsciiCase( OUString("convert-bitmap-depth") ) )
130 Reference
< XBitmap
> xBM
;
131 sal_uInt16 nTargetDepth
= 0;
132 if( rParams
.getLength() != 2 )
133 throw CannotConvertException();
135 if( ! (rParams
.getConstArray()[0] >>= xBM
) ||
136 ! ( rParams
.getConstArray()[1] >>= nTargetDepth
) )
137 throw CannotConvertException();
139 Sequence
< sal_Int8
> aDIB
= xBM
->getDIB();
141 // call into vcl not thread safe
142 SolarMutexGuard aGuard
;
144 SvMemoryStream
aStream( aDIB
.getArray(), aDIB
.getLength(), STREAM_READ
| STREAM_WRITE
);
146 aBM
.Read( aStream
, sal_True
);
147 if( nTargetDepth
< 4 )
149 else if( nTargetDepth
< 8 )
151 else if( nTargetDepth
>8 && nTargetDepth
< 24 )
154 if( aBM
.GetBitCount() == 24 && nTargetDepth
<= 8 )
155 aBM
.Dither( BMP_DITHER_FLOYD
);
157 if( aBM
.GetBitCount() != nTargetDepth
)
159 switch( nTargetDepth
)
161 case 1: aBM
.Convert( BMP_CONVERSION_1BIT_THRESHOLD
);break;
162 case 4: aBM
.ReduceColors( BMP_CONVERSION_4BIT_COLORS
);break;
163 case 8: aBM
.ReduceColors( BMP_CONVERSION_8BIT_COLORS
);break;
164 case 24: aBM
.Convert( BMP_CONVERSION_24BIT
);break;
167 xBM
= new BmpTransporter( aBM
);
171 throw InvocationTargetException();
176 BmpTransporter::BmpTransporter( const Bitmap
& rBM
)
178 m_aSize
.Width
= rBM
.GetSizePixel().Width();
179 m_aSize
.Height
= rBM
.GetSizePixel().Height();
180 SvMemoryStream aStream
;
181 rBM
.Write( aStream
, sal_False
, sal_True
);
182 m_aBM
= Sequence
<sal_Int8
>(static_cast<const sal_Int8
*>(aStream
.GetData()),
183 aStream
.GetEndOfData());
186 BmpTransporter::~BmpTransporter()
190 com::sun::star::awt::Size SAL_CALL
BmpTransporter::getSize() throw()
195 Sequence
< sal_Int8
> SAL_CALL
BmpTransporter::getDIB() throw()
200 Sequence
< sal_Int8
> SAL_CALL
BmpTransporter::getMaskDIB() throw()
202 return Sequence
< sal_Int8
>();
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */