xref: /AOO41X/main/svtools/source/graphic/transformer.cxx (revision ff0525f24f03981d56b7579b645949f111420994)
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_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
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         throw ( lang::IllegalArgumentException, uno::RuntimeException)
89 {
90     const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
91     ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
92 
93     BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
94     BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo  >> 16 ) );
95     const sal_uInt8 cIndexFrom = aColorFrom.GetBlueOrIndex();
96 
97     if ( aGraphic.GetType() == GRAPHIC_BITMAP )
98     {
99         BitmapEx    aBitmapEx( aGraphic.GetBitmapEx() );
100         Bitmap      aBitmap( aBitmapEx.GetBitmap() );
101 
102         if ( aBitmapEx.IsAlpha() )
103         {
104             AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
105             setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
106             aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
107             aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
108         }
109         else if ( aBitmapEx.IsTransparent() )
110         {
111             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
112             {
113                 Bitmap aMask( aBitmapEx.GetMask() );
114                 Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
115                 aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
116                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
117                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
118             }
119             else
120             {
121                 AlphaMask aAlphaMask( aBitmapEx.GetMask() );
122                 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
123                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
124                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
125             }
126         }
127         else
128         {
129             if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
130             {
131                 Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
132                     aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
133                 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
134             }
135             else
136             {
137                 AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
138                 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
139                 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
140                 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
141             }
142         }
143     }
144     ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
145     pUnoGraphic->init( aGraphic );
146     uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
147     return xRet;
148 }
149 
150 }
151