xref: /AOO41X/main/svtools/source/graphic/transformer.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26 
27 #include <rtl/uuid.h>
28 #include <vos/mutex.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/image.hxx>
31 #include <vcl/metaact.hxx>
32 #include <tools/rcid.h>
33 #include <tools/resid.hxx>
34 #include <tools/resmgr.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 #include <svl/solar.hrc>
37 #include <vcl/salbtype.hxx>
38 #include <vcl/virdev.hxx>
39 #include <vcl/bmpacc.hxx>
40 #include <com/sun/star/text/GraphicCrop.hpp>
41 
42 #include "graphic.hxx"
43 #include "transformer.hxx"
44 
45 using namespace com::sun::star;
46 
47 namespace unographic {
48 
49 // ----------------------
50 // - GraphicTransformer -
51 // ----------------------
52 
53 GraphicTransformer::GraphicTransformer()
54 {
55 }
56 
57 // ------------------------------------------------------------------------------
58 
59 GraphicTransformer::~GraphicTransformer()
60 {
61 }
62 
63 // ------------------------------------------------------------------------------
64 
65 void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_Int32 nColorFrom, sal_Int8 nAlphaTo )
66 {
67     BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
68     BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
69     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom >> 16 ),
70         static_cast< sal_uInt8 >( nColorFrom >> 8 ),
71         static_cast< sal_uInt8 >( nColorFrom ) );
72     if ( pReadAccess && pWriteAccess )
73     {
74         for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
75         {
76             for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
77             {
78                 BitmapColor aColor( pReadAccess->GetPixel( nY, nX ) );
79                 if ( aColor == aColorFrom )
80                     pWriteAccess->SetPixel( nY, nX, nAlphaTo );
81             }
82         }
83     }
84     rBitmap.ReleaseAccess( pReadAccess );
85     rAlpha.ReleaseAccess( pWriteAccess );
86 }
87 
88 // XGraphicTransformer
89 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
90     const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
91         throw ( lang::IllegalArgumentException, uno::RuntimeException)
92 {
93     const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
94     ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
95 
96     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
97     BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo  >> 16 ) );
98 
99     if ( aGraphic.GetType() == GRAPHIC_BITMAP )
100     {
101         BitmapEx    aBitmapEx( aGraphic.GetBitmapEx() );
102         Bitmap      aBitmap( aBitmapEx.GetBitmap() );
103 
104         if ( aBitmapEx.IsAlpha() )
105         {
106             AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
107             setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
108             aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
109             aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
110         }
111         else if ( aBitmapEx.IsTransparent() )
112         {
113             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
114             {
115                 Bitmap aMask( aBitmapEx.GetMask() );
116                 Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
117                 aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
118                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
119                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
120             }
121             else
122             {
123                 AlphaMask aAlphaMask( aBitmapEx.GetMask() );
124                 setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
125                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
126                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
127             }
128         }
129         else
130         {
131             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
132             {
133                 Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
134                     aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
135                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
136             }
137             else
138             {
139                 AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
140                 setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
141                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
142                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
143             }
144         }
145     }
146     ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
147     pUnoGraphic->init( aGraphic );
148     uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
149     return xRet;
150 }
151 
152 }
153