xref: /trunk/main/svtools/source/graphic/transformer.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
GraphicTransformer()53 GraphicTransformer::GraphicTransformer()
54 {
55 }
56 
57 // ------------------------------------------------------------------------------
58 
~GraphicTransformer()59 GraphicTransformer::~GraphicTransformer()
60 {
61 }
62 
63 // ------------------------------------------------------------------------------
64 
setAlpha(Bitmap & rBitmap,AlphaMask & rAlpha,sal_uInt8 cIndexFrom,sal_Int8 nAlphaTo)65 void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo )
66 {
67     BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
68     BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
69     if ( pReadAccess && pWriteAccess )
70     {
71         for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
72         {
73             for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
74             {
75                 const sal_uInt8 cIndex = pReadAccess->GetPixelIndex( nY, nX );
76                 if ( cIndex == cIndexFrom )
77                     pWriteAccess->SetPixelIndex( nY, nX, nAlphaTo );
78             }
79         }
80     }
81     rBitmap.ReleaseAccess( pReadAccess );
82     rAlpha.ReleaseAccess( pWriteAccess );
83 }
84 
85 // XGraphicTransformer
colorChange(const uno::Reference<graphic::XGraphic> & rxGraphic,sal_Int32 nColorFrom,sal_Int8 nTolerance,sal_Int32 nColorTo,sal_Int8 nAlphaTo)86 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
87     const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
88 {
89     const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
90     ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
91 
92     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
93     BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo  >> 16 ) );
94     const sal_uInt8 cIndexFrom = aColorFrom.GetBlueOrIndex();
95 
96     if ( aGraphic.GetType() == GRAPHIC_BITMAP )
97     {
98         BitmapEx    aBitmapEx( aGraphic.GetBitmapEx() );
99         Bitmap      aBitmap( aBitmapEx.GetBitmap() );
100 
101         if ( aBitmapEx.IsAlpha() )
102         {
103             AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
104             setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
105             aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
106             aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
107         }
108         else if ( aBitmapEx.IsTransparent() )
109         {
110             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
111             {
112                 Bitmap aMask( aBitmapEx.GetMask() );
113                 Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
114                 aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
115                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
116                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
117             }
118             else
119             {
120                 AlphaMask aAlphaMask( aBitmapEx.GetMask() );
121                 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
122                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
123                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
124             }
125         }
126         else
127         {
128             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
129             {
130                 Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
131                     aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
132                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
133             }
134             else
135             {
136                 AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
137                 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
138                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
139                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
140             }
141         }
142     }
143     ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
144     pUnoGraphic->init( aGraphic );
145     uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
146     return xRet;
147 }
148 
149 }
150