GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svtools / source / graphic / transformer.cxx
blob30f07c7af1971048c0d547935e854568b0be556b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
34 #include "graphic.hxx"
35 #include "transformer.hxx"
37 using namespace com::sun::star;
39 namespace unographic {
41 // ----------------------
42 // - GraphicTransformer -
43 // ----------------------
45 GraphicTransformer::GraphicTransformer()
49 // ------------------------------------------------------------------------------
51 GraphicTransformer::~GraphicTransformer()
55 // ------------------------------------------------------------------------------
57 void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo )
59 BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
60 BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
61 if ( pReadAccess && pWriteAccess )
63 for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
65 for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
67 const sal_uInt8 cIndex = pReadAccess->GetPixelIndex( nY, nX );
68 if ( cIndex == cIndexFrom )
69 pWriteAccess->SetPixelIndex( nY, nX, nAlphaTo );
73 rBitmap.ReleaseAccess( pReadAccess );
74 rAlpha.ReleaseAccess( pWriteAccess );
77 // XGraphicTransformer
78 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::colorChange(
79 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorFrom, sal_Int8 nTolerance, sal_Int32 nColorTo, sal_Int8 nAlphaTo )
80 throw ( lang::IllegalArgumentException, uno::RuntimeException)
82 const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
83 ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
85 BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
86 BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo >> 16 ) );
87 const sal_uInt8 cIndexFrom = aColorFrom.GetBlueOrIndex();
89 if ( aGraphic.GetType() == GRAPHIC_BITMAP || aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
91 BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
92 Bitmap aBitmap( aBitmapEx.GetBitmap() );
94 if ( aBitmapEx.IsAlpha() )
96 AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
97 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
98 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
99 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
101 else if ( aBitmapEx.IsTransparent() )
103 if ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) )
105 Bitmap aMask( aBitmapEx.GetMask() );
106 Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
107 aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
108 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
109 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
111 else
113 AlphaMask aAlphaMask( aBitmapEx.GetMask() );
114 setAlpha( aBitmap, aAlphaMask, cIndexFrom, 0xff - nAlphaTo );
115 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
116 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
119 else
121 if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
123 Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
124 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
125 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
127 else
129 AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
130 setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
131 aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
132 aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
136 ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
137 pUnoGraphic->init( aGraphic );
138 uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
139 return xRet;
142 uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyDuotone(
143 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nColorOne, sal_Int32 nColorTwo )
144 throw ( lang::IllegalArgumentException, uno::RuntimeException)
146 const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
147 ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
149 BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
150 Bitmap aMask( aBitmapEx.GetMask() );
151 Bitmap aBitmap( aBitmapEx.GetBitmap() );
152 BmpFilterParam aFilter( (sal_uLong) nColorOne, (sal_uLong) nColorTwo );
153 aBitmap.Filter( BMP_FILTER_DUOTONE, &aFilter );
154 aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
156 ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
157 pUnoGraphic->init( aGraphic );
158 uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
159 return xRet;
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */