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