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 137458 : GraphicTransformer::GraphicTransformer()
46 : {
47 137458 : }
48 :
49 :
50 :
51 137456 : GraphicTransformer::~GraphicTransformer()
52 : {
53 137456 : }
54 :
55 :
56 :
57 0 : void setAlpha( Bitmap& rBitmap, AlphaMask& rAlpha, sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo )
58 : {
59 0 : BitmapWriteAccess* pWriteAccess = rAlpha.AcquireWriteAccess();
60 0 : BitmapReadAccess* pReadAccess = rBitmap.AcquireReadAccess();
61 0 : if ( pReadAccess && pWriteAccess )
62 : {
63 0 : for ( sal_Int32 nY = 0; nY < pReadAccess->Height(); nY++ )
64 : {
65 0 : for ( sal_Int32 nX = 0; nX < pReadAccess->Width(); nX++ )
66 : {
67 0 : const sal_uInt8 cIndex = pReadAccess->GetPixelIndex( nY, nX );
68 0 : if ( cIndex == cIndexFrom )
69 0 : pWriteAccess->SetPixelIndex( nY, nX, nAlphaTo );
70 : }
71 : }
72 : }
73 0 : rBitmap.ReleaseAccess( pReadAccess );
74 0 : rAlpha.ReleaseAccess( pWriteAccess );
75 0 : }
76 :
77 : // XGraphicTransformer
78 0 : 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, std::exception)
81 : {
82 0 : const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
83 0 : ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
84 :
85 0 : BitmapColor aColorFrom( static_cast< sal_uInt8 >( nColorFrom ), static_cast< sal_uInt8 >( nColorFrom >> 8 ), static_cast< sal_uInt8 >( nColorFrom >> 16 ) );
86 0 : BitmapColor aColorTo( static_cast< sal_uInt8 >( nColorTo ), static_cast< sal_uInt8 >( nColorTo >> 8 ), static_cast< sal_uInt8 >( nColorTo >> 16 ) );
87 0 : const sal_uInt8 cIndexFrom = aColorFrom.GetBlueOrIndex();
88 :
89 0 : if ( aGraphic.GetType() == GRAPHIC_BITMAP || aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
90 : {
91 0 : BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
92 0 : Bitmap aBitmap( aBitmapEx.GetBitmap() );
93 :
94 0 : if ( aBitmapEx.IsAlpha() )
95 : {
96 0 : AlphaMask aAlphaMask( aBitmapEx.GetAlpha() );
97 0 : setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
98 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
99 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
100 : }
101 0 : else if ( aBitmapEx.IsTransparent() )
102 : {
103 0 : if ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) )
104 : {
105 0 : Bitmap aMask( aBitmapEx.GetMask() );
106 0 : Bitmap aMask2( aBitmap.CreateMask( aColorFrom, nTolerance ) );
107 0 : aMask.CombineSimple( aMask2, BMP_COMBINE_OR );
108 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
109 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
110 : }
111 : else
112 : {
113 0 : AlphaMask aAlphaMask( aBitmapEx.GetMask() );
114 0 : setAlpha( aBitmap, aAlphaMask, cIndexFrom, 0xff - nAlphaTo );
115 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
116 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
117 : }
118 : }
119 : else
120 : {
121 0 : if ( ( nAlphaTo == 0 ) || ( nAlphaTo == sal::static_int_cast<sal_Int8>(0xff) ) )
122 : {
123 0 : Bitmap aMask( aBitmap.CreateMask( aColorFrom, nTolerance ) );
124 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
125 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
126 : }
127 : else
128 : {
129 0 : AlphaMask aAlphaMask( aBitmapEx.GetSizePixel() );
130 0 : setAlpha( aBitmap, aAlphaMask, cIndexFrom, nAlphaTo );
131 0 : aBitmap.Replace( aColorFrom, aColorTo, nTolerance );
132 0 : aGraphic = ::Graphic( BitmapEx( aBitmap, aAlphaMask ) );
133 : }
134 0 : }
135 : }
136 0 : ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
137 0 : pUnoGraphic->init( aGraphic );
138 0 : uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
139 0 : return xRet;
140 : }
141 :
142 2 : 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, std::exception)
145 : {
146 2 : const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
147 4 : ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
148 :
149 4 : BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
150 4 : AlphaMask aMask( aBitmapEx.GetAlpha() );
151 4 : Bitmap aBitmap( aBitmapEx.GetBitmap() );
152 2 : BmpFilterParam aFilter( (sal_uLong) nColorOne, (sal_uLong) nColorTwo );
153 2 : aBitmap.Filter( BMP_FILTER_DUOTONE, &aFilter );
154 2 : aGraphic = ::Graphic( BitmapEx( aBitmap, aMask ) );
155 :
156 2 : ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
157 2 : pUnoGraphic->init( aGraphic );
158 2 : uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
159 4 : return xRet;
160 : }
161 :
162 4 : uno::Reference< graphic::XGraphic > SAL_CALL GraphicTransformer::applyBrightnessContrast(
163 : const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nBrightness, sal_Int32 nContrast, sal_Bool mso )
164 : throw ( lang::IllegalArgumentException, uno::RuntimeException, std::exception)
165 : {
166 4 : const uno::Reference< uno::XInterface > xIFace( rxGraphic, uno::UNO_QUERY );
167 8 : ::Graphic aGraphic( *::unographic::Graphic::getImplementation( xIFace ) );
168 :
169 8 : BitmapEx aBitmapEx( aGraphic.GetBitmapEx() );
170 4 : aBitmapEx.Adjust( nBrightness, nContrast, 0, 0, 0, 0, false, mso );
171 4 : aGraphic = ::Graphic( aBitmapEx );
172 :
173 4 : ::unographic::Graphic* pUnoGraphic = new ::unographic::Graphic();
174 4 : pUnoGraphic->init( aGraphic );
175 4 : uno::Reference< graphic::XGraphic > xRet( pUnoGraphic );
176 8 : return xRet;
177 : }
178 :
179 1227 : }
180 :
181 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|