1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bmpconv.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <vcl/bitmap.hxx>
35 #include <vcl/impbmpconv.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vos/mutex.hxx>
38 #include <tools/stream.hxx>
39 #include <com/sun/star/script/XInvocation.hpp>
40 #include <com/sun/star/awt/XBitmap.hpp>
41 #include <cppuhelper/compbase1.hxx>
44 using namespace com::sun::star::uno
;
45 using namespace com::sun::star::script
;
46 using namespace com::sun::star::beans
;
47 using namespace com::sun::star::reflection
;
48 using namespace com::sun::star::awt
;
53 class BmpTransporter
:
54 public cppu::WeakImplHelper1
< com::sun::star::awt::XBitmap
>
56 Sequence
<sal_Int8
> m_aBM
;
57 com::sun::star::awt::Size m_aSize
;
59 BmpTransporter( const Bitmap
& rBM
);
60 virtual ~BmpTransporter();
62 virtual com::sun::star::awt::Size SAL_CALL
getSize() throw();
63 virtual Sequence
< sal_Int8
> SAL_CALL
getDIB() throw();
64 virtual Sequence
< sal_Int8
> SAL_CALL
getMaskDIB() throw();
68 public cppu::WeakImplHelper1
< com::sun::star::script::XInvocation
>
72 virtual ~BmpConverter();
74 virtual Reference
< XIntrospectionAccess
> SAL_CALL
getIntrospection() throw();
75 virtual void SAL_CALL
setValue( const OUString
& rProperty
, const Any
& rValue
)
76 throw( UnknownPropertyException
);
77 virtual Any SAL_CALL
getValue( const OUString
& rProperty
)
78 throw( UnknownPropertyException
);
79 virtual sal_Bool SAL_CALL
hasMethod( const OUString
& rName
) throw();
80 virtual sal_Bool SAL_CALL
hasProperty( const OUString
& rProp
) throw();
82 virtual Any SAL_CALL
invoke( const OUString
& rFunction
,
83 const Sequence
< Any
>& rParams
,
84 Sequence
< sal_Int16
>& rOutParamIndex
,
85 Sequence
< Any
>& rOutParam
87 throw( CannotConvertException
, InvocationTargetException
);
94 Reference
< XInvocation
> vcl::createBmpConverter()
96 return static_cast<XInvocation
*>(new BmpConverter());
99 BmpConverter::BmpConverter()
103 BmpConverter::~BmpConverter()
107 Reference
< XIntrospectionAccess
> SAL_CALL
BmpConverter::getIntrospection() throw()
109 return Reference
< XIntrospectionAccess
>();
112 void SAL_CALL
BmpConverter::setValue( const OUString
&, const Any
& ) throw( UnknownPropertyException
)
114 throw UnknownPropertyException();
117 Any SAL_CALL
BmpConverter::getValue( const OUString
& ) throw( UnknownPropertyException
)
119 throw UnknownPropertyException();
122 sal_Bool SAL_CALL
BmpConverter::hasMethod( const OUString
& rName
) throw()
124 return rName
.equalsIgnoreAsciiCase( OUString::createFromAscii( "convert-bitmap-depth" ) );
127 sal_Bool SAL_CALL
BmpConverter::hasProperty( const OUString
& ) throw()
132 Any SAL_CALL
BmpConverter::invoke(
133 const OUString
& rFunction
,
134 const Sequence
< Any
>& rParams
,
135 Sequence
< sal_Int16
>&,
137 throw( CannotConvertException
, InvocationTargetException
)
141 if( rFunction
.equalsIgnoreAsciiCase( OUString::createFromAscii( "convert-bitmap-depth" ) ) )
143 Reference
< XBitmap
> xBM
;
144 sal_uInt16 nTargetDepth
= 0;
145 if( rParams
.getLength() != 2 )
146 throw CannotConvertException();
148 if( ! (rParams
.getConstArray()[0] >>= xBM
) ||
149 ! ( rParams
.getConstArray()[1] >>= nTargetDepth
) )
150 throw CannotConvertException();
152 Sequence
< sal_Int8
> aDIB
= xBM
->getDIB();
154 // call into vcl not thread safe
155 vos::OGuard
aGuard( Application::GetSolarMutex() );
157 SvMemoryStream
aStream( aDIB
.getArray(), aDIB
.getLength(), STREAM_READ
| STREAM_WRITE
);
159 aBM
.Read( aStream
, TRUE
);
160 if( nTargetDepth
< 4 )
162 else if( nTargetDepth
< 8 )
164 else if( nTargetDepth
>8 && nTargetDepth
< 24 )
167 if( aBM
.GetBitCount() == 24 && nTargetDepth
<= 8 )
168 aBM
.Dither( BMP_DITHER_FLOYD
);
170 if( aBM
.GetBitCount() != nTargetDepth
)
172 switch( nTargetDepth
)
174 case 1: aBM
.Convert( BMP_CONVERSION_1BIT_THRESHOLD
);break;
175 case 4: aBM
.ReduceColors( BMP_CONVERSION_4BIT_COLORS
);break;
176 case 8: aBM
.ReduceColors( BMP_CONVERSION_8BIT_COLORS
);break;
177 case 24: aBM
.Convert( BMP_CONVERSION_24BIT
);break;
180 xBM
= new BmpTransporter( aBM
);
184 throw InvocationTargetException();
189 BmpTransporter::BmpTransporter( const Bitmap
& rBM
)
191 m_aSize
.Width
= rBM
.GetSizePixel().Width();
192 m_aSize
.Height
= rBM
.GetSizePixel().Height();
193 SvMemoryStream aStream
;
194 rBM
.Write( aStream
, FALSE
, TRUE
);
195 m_aBM
= Sequence
<sal_Int8
>((const sal_Int8
*)aStream
.GetData(), aStream
.GetSize() );
198 BmpTransporter::~BmpTransporter()
202 com::sun::star::awt::Size SAL_CALL
BmpTransporter::getSize() throw()
207 Sequence
< sal_Int8
> SAL_CALL
BmpTransporter::getDIB() throw()
212 Sequence
< sal_Int8
> SAL_CALL
BmpTransporter::getMaskDIB() throw()
214 return Sequence
< sal_Int8
>();