Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <osl/mutex.hxx>
22 : #include <vcl/svapp.hxx>
23 : #include <vcl/image.hxx>
24 : #include <vcl/metaact.hxx>
25 : #include <tools/rcid.h>
26 : #include <tools/resid.hxx>
27 : #include <unotools/ucbstreamhelper.hxx>
28 : #include <svl/solar.hrc>
29 : #include <vcl/salbtype.hxx>
30 : #include <vcl/virdev.hxx>
31 : #include <vcl/bmpacc.hxx>
32 : #include <com/sun/star/text/GraphicCrop.hpp>
33 :
34 : #include "graphic.hxx"
35 : #include "transformer.hxx"
36 :
37 : using namespace com::sun::star;
38 :
39 : namespace unographic {
40 :
41 : // ----------------------
42 : // - GraphicTransformer -
43 : // ----------------------
44 :
45 132 : GraphicTransformer::GraphicTransformer()
46 : {
47 132 : }
48 :
49 : // ------------------------------------------------------------------------------
50 :
51 132 : GraphicTransformer::~GraphicTransformer()
52 : {
53 132 : }
54 :
55 : // ------------------------------------------------------------------------------
56 :
57 0 : void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_Int32 nColorFrom, sal_Int8 nAlphaTo )
58 : {
59 0 : BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
60 0 : BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
61 : BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom >> 16 ),
62 : static_cast< sal_uInt8 >( nColorFrom >> 8 ),
63 0 : static_cast< sal_uInt8 >( nColorFrom ) );
64 0 : if ( pReadAccess && pWriteAccess )
65 : {
66 0 : for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
67 : {
68 0 : for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
69 : {
70 0 : BitmapColor aColor( pReadAccess->GetPixel( nY, nX ) );
71 0 : if ( aColor == aColorFrom )
72 0 : pWriteAccess->SetPixel( nY, nX, nAlphaTo );
73 0 : }
74 : }
75 : }
76 0 : rBitmap.ReleaseAccess( pReadAccess );
77 0 : rAlpha.ReleaseAccess( pWriteAccess );
78 0 : }
79 :
80 : // XGraphicTransformer
81 0 : uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
82 : const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
83 : throw ( lang::IllegalArgumentException, uno::RuntimeException)
84 : {
85 0 : const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
86 0 : ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
87 :
88 0 : BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
89 0 : BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo >> 16 ) );
90 :
91 0 : if ( aGraphic.GetType() == GRAPHIC_BITMAP )
92 : {
93 0 : BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
94 0 : Bitmap aBitmap( aBitmapEx.GetBitmap() );
95 :
96 0 : if ( aBitmapEx.IsAlpha() )
97 : {
98 0 : AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
99 0 : setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
100 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
101 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
102 : }
103 0 : else if ( aBitmapEx.IsTransparent() )
104 : {
105 0 : if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
106 : {
107 0 : Bitmap aMask( aBitmapEx.GetMask() );
108 0 : Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
109 0 : aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
110 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
111 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
112 : }
113 : else
114 : {
115 0 : AlphaMask aAlphaMask( aBitmapEx.GetMask() );
116 0 : setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
117 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
118 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
119 : }
120 : }
121 : else
122 : {
123 0 : if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
124 : {
125 0 : Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
126 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
127 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
128 : }
129 : else
130 : {
131 0 : AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
132 0 : setAlpha( aBitmap, aAlphaMask, aColorFrom, nAlphaTo );
133 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
134 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
135 : }
136 0 : }
137 : }
138 0 : ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
139 0 : pUnoGraphic->init( aGraphic );
140 0 : uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
141 0 : return xRet;
142 : }
143 :
144 : }
145 :
146 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|