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: transformer.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_goodies.hxx"
34 #include <vos/mutex.hxx>
35 #ifndef _SV_SVAPP_HXX_
36 #include <vcl/svapp.hxx>
38 #ifndef _SV_IMAGE_HXX_
39 #include <vcl/image.hxx>
41 #include <vcl/metaact.hxx>
42 #include <tools/rcid.h>
43 #include <tools/resid.hxx>
44 #include <tools/resmgr.hxx>
45 #include <unotools/ucbstreamhelper.hxx>
46 #include <svtools/solar.hrc>
47 #include <vcl/salbtype.hxx>
48 #include <vcl/virdev.hxx>
49 #include <vcl/bmpacc.hxx>
50 #include <com/sun/star/text/GraphicCrop.hpp>
52 #include "graphic.hxx"
53 #include "transformer.hxx"
55 using namespace com::sun::star
;
57 namespace unographic
{
59 // ----------------------
60 // - GraphicTransformer -
61 // ----------------------
63 GraphicTransformer::GraphicTransformer()
67 // ------------------------------------------------------------------------------
69 GraphicTransformer::~GraphicTransformer()
73 // ------------------------------------------------------------------------------
75 void setAlpha( Bitmap
& rBitmap
, AlphaMask
& rAlpha
, sal_Int32 nColorFrom
, sal_Int8 nAlphaTo
)
77 BitmapWriteAccess
* pWriteAccess
= rAlpha
.AcquireWriteAccess();
78 BitmapReadAccess
* pReadAccess
= rBitmap
.AcquireReadAccess();
79 BitmapColor
aColorFrom( static_cast< sal_uInt8
>( nColorFrom
>> 16 ),
80 static_cast< sal_uInt8
>( nColorFrom
>> 8 ),
81 static_cast< sal_uInt8
>( nColorFrom
) );
82 if ( pReadAccess
&& pWriteAccess
)
84 for ( sal_Int32 nY
= 0; nY
< pReadAccess
->Height(); nY
++ )
86 for ( sal_Int32 nX
= 0; nX
< pReadAccess
->Width(); nX
++ )
88 BitmapColor
aColor( pReadAccess
->GetPixel( nY
, nX
) );
89 if ( aColor
== aColorFrom
)
90 pWriteAccess
->SetPixel( nY
, nX
, nAlphaTo
);
94 rBitmap
.ReleaseAccess( pReadAccess
);
95 rAlpha
.ReleaseAccess( pWriteAccess
);
98 // XGraphicTransformer
99 uno::Reference
< graphic::XGraphic
> SAL_CALL
GraphicTransformer::colorChange(
100 const uno::Reference
< graphic::XGraphic
>& rxGraphic
, sal_Int32 nColorFrom
, sal_Int8 nTolerance
, sal_Int32 nColorTo
, sal_Int8 nAlphaTo
)
101 throw ( lang::IllegalArgumentException
, uno::RuntimeException
)
103 const uno::Reference
< uno::XInterface
> xIFace( rxGraphic
, uno::UNO_QUERY
);
104 ::Graphic
aGraphic( *::unographic::Graphic::getImplementation( xIFace
) );
106 BitmapColor
aColorFrom( static_cast< sal_uInt8
>( nColorFrom
), static_cast< sal_uInt8
>( nColorFrom
>> 8 ), static_cast< sal_uInt8
>( nColorFrom
>> 16 ) );
107 BitmapColor
aColorTo( static_cast< sal_uInt8
>( nColorTo
), static_cast< sal_uInt8
>( nColorTo
>> 8 ), static_cast< sal_uInt8
>( nColorTo
>> 16 ) );
109 if ( aGraphic
.GetType() == GRAPHIC_BITMAP
)
111 BitmapEx
aBitmapEx( aGraphic
.GetBitmapEx() );
112 Bitmap
aBitmap( aBitmapEx
.GetBitmap() );
114 if ( aBitmapEx
.IsAlpha() )
116 AlphaMask
aAlphaMask( aBitmapEx
.GetAlpha() );
117 setAlpha( aBitmap
, aAlphaMask
, aColorFrom
, nAlphaTo
);
118 aBitmap
.Replace( aColorFrom
, aColorTo
, nTolerance
);
119 aGraphic
= ::Graphic( BitmapEx( aBitmap
, aAlphaMask
) );
121 else if ( aBitmapEx
.IsTransparent() )
123 if ( ( nAlphaTo
== 0 ) || ( nAlphaTo
== sal::static_int_cast
<sal_Int8
>(0xff) ) )
125 Bitmap
aMask( aBitmapEx
.GetMask() );
126 Bitmap
aMask2( aBitmap
.CreateMask( aColorFrom
, nTolerance
) );
127 aMask
.CombineSimple( aMask2
, BMP_COMBINE_OR
);
128 aBitmap
.Replace( aColorFrom
, aColorTo
, nTolerance
);
129 aGraphic
= ::Graphic( BitmapEx( aBitmap
, aMask
) );
133 AlphaMask
aAlphaMask( aBitmapEx
.GetMask() );
134 setAlpha( aBitmap
, aAlphaMask
, aColorFrom
, nAlphaTo
);
135 aBitmap
.Replace( aColorFrom
, aColorTo
, nTolerance
);
136 aGraphic
= ::Graphic( BitmapEx( aBitmap
, aAlphaMask
) );
141 if ( ( nAlphaTo
== 0 ) || ( nAlphaTo
== sal::static_int_cast
<sal_Int8
>(0xff) ) )
143 Bitmap
aMask( aBitmap
.CreateMask( aColorFrom
, nTolerance
) );
144 aBitmap
.Replace( aColorFrom
, aColorTo
, nTolerance
);
145 aGraphic
= ::Graphic( BitmapEx( aBitmap
, aMask
) );
149 AlphaMask
aAlphaMask( aBitmapEx
.GetSizePixel() );
150 setAlpha( aBitmap
, aAlphaMask
, aColorFrom
, nAlphaTo
);
151 aBitmap
.Replace( aColorFrom
, aColorTo
, nTolerance
);
152 aGraphic
= ::Graphic( BitmapEx( aBitmap
, aAlphaMask
) );
156 ::unographic::Graphic
* pUnoGraphic
= new ::unographic::Graphic();
157 pUnoGraphic
->init( aGraphic
);
158 uno::Reference
< graphic::XGraphic
> xRet( pUnoGraphic
);