xref: /trunk/main/svtools/source/graphic/transformer.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include <rtl/uuid.h>
32 #include <vos/mutex.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/image.hxx>
35 #include <vcl/metaact.hxx>
36 #include <tools/rcid.h>
37 #include <tools/resid.hxx>
38 #include <tools/resmgr.hxx>
39 #include <unotools/ucbstreamhelper.hxx>
40 #include <svl/solar.hrc>
41 #include <vcl/salbtype.hxx>
42 #include <vcl/virdev.hxx>
43 #include <vcl/bmpacc.hxx>
44 #include <com/sun/star/text/GraphicCrop.hpp>
45 
46 #include "graphic.hxx"
47 #include "transformer.hxx"
48 
49 using namespace com::sun::star;
50 
51 namespace unographic {
52 
53 // ----------------------
54 // - GraphicTransformer -
55 // ----------------------
56 
57 GraphicTransformer::GraphicTransformer()
58 {
59 }
60 
61 // ------------------------------------------------------------------------------
62 
63 GraphicTransformer::~GraphicTransformer()
64 {
65 }
66 
67 // ------------------------------------------------------------------------------
68 
69 void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_Int32 nColorFrom, sal_Int8 nAlphaTo )
70 {
71     BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
72     BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
73     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom >> 16 ),
74         static_cast< sal_uInt8 >( nColorFrom >> 8 ),
75         static_cast< sal_uInt8 >( nColorFrom ) );
76     if ( pReadAccess && pWriteAccess )
77     {
78         for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
79         {
80             for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
81             {
82                 BitmapColor aColor( pReadAccess->GetPixel( nY, nX ) );
83                 if ( aColor == aColorFrom )
84                     pWriteAccess->SetPixel( nY, nX, nAlphaTo );
85             }
86         }
87     }
88     rBitmap.ReleaseAccess( pReadAccess );
89     rAlpha.ReleaseAccess( pWriteAccess );
90 }
91 
92 // XGraphicTransformer
93 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
94     const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
95         throw ( lang::IllegalArgumentException, uno::RuntimeException)
96 {
97     const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
98     ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
99 
100     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
101     BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo  >> 16 ) );
102 
103     if ( aGraphic.GetType() == GRAPHIC_BITMAP )
104     {
105         BitmapEx    aBitmapEx( aGraphic.GetBitmapEx() );
106         Bitmap      aBitmap( aBitmapEx.GetBitmap() );
107 
108         if ( aBitmapEx.IsAlpha() )
109         {
110             AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
111             setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
112             aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
113             aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
114         }
115         else if ( aBitmapEx.IsTransparent() )
116         {
117             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
118             {
119                 Bitmap aMask( aBitmapEx.GetMask() );
120                 Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
121                 aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
122                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
123                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
124             }
125             else
126             {
127                 AlphaMask aAlphaMask( aBitmapEx.GetMask() );
128                 setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
129                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
130                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
131             }
132         }
133         else
134         {
135             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
136             {
137                 Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
138                     aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
139                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
140             }
141             else
142             {
143                 AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
144                 setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
145                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
146                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
147             }
148         }
149     }
150     ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
151     pUnoGraphic->init( aGraphic );
152     uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
153     return xRet;
154 }
155 
156 }
157