1 // Copyright (C) 2013-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ImageComposer_ColorMaskOperator.h"
21 #include "ImageComposer_MetaTypes.h"
30 ImageComposer_ColorMaskOperator::ImageComposer_ColorMaskOperator()
31 : ImageComposer_Operator(),
32 myRefColor( Qt::black ), myIsMakeTransparent( false ),
33 myRGBThreshold( 0 ), myAlphaThreshold( 0 )
39 ImageComposer_ColorMaskOperator::~ImageComposer_ColorMaskOperator()
44 Set operator arguments
45 @param theRefColor the color to the searched (the color for mask)
46 @param isMakeTransparent the boolean flag controlling if the pixels with matching color
47 should be made transparent or one with non-matching color
48 @param theRGBThreshold the threshold for RGB components
49 @param theAlphaThreshold the threshold for Alpha component
51 void ImageComposer_ColorMaskOperator::setArgs( const QColor& theRefColor,
52 bool isMakeTransparent,
54 int theAlphaThreshold )
56 myRefColor = theRefColor;
57 myIsMakeTransparent = isMakeTransparent;
58 myRGBThreshold = theRGBThreshold;
59 myAlphaThreshold = theAlphaThreshold;
64 QString ImageComposer_ColorMaskOperator::name() const
69 QStringList ImageComposer_ColorMaskOperator::dumpArgsToPython( QString& theArrayName ) const
71 QStringList aResList = ImageComposer_Operator::dumpArgsToPython( theArrayName );
73 QString aStreamName = theArrayName + "_stream";
75 //Dump operator arguments
76 aResList << QString( "" );
77 aResList << QString( "mask_color = QColor( %1, %2, %3, %4 );" )
78 .arg( myRefColor.red() ).arg( myRefColor.green() )
79 .arg( myRefColor.blue() ).arg( myRefColor.alpha() );
80 aResList << QString( "%1 << mask_color;" ).arg( aStreamName );
82 aResList << QString( "" );
83 aResList << QString( "make_transparent = %1;" ).arg( myIsMakeTransparent );
84 aResList << QString( "%1 << make_transparent;" ).arg( aStreamName );
86 aResList << QString( "" );
87 aResList << QString( "rgb_threshold = %1;" ).arg( myRGBThreshold );
88 aResList << QString( "%1 << rgb_threshold;" ).arg( aStreamName );
90 aResList << QString( "" );
91 aResList << QString( "alpha_threshold = %1;" ).arg( myAlphaThreshold );
92 aResList << QString( "%1 << alpha_threshold;" ).arg( aStreamName );
99 QRectF ImageComposer_ColorMaskOperator::calcResultBoundingRect( const QVariant& theObj1,
100 const QVariant& ) const
103 if ( !theObj1.isNull() && theObj1.canConvert<ImageComposer_Image>() )
105 ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
106 aResRect = anImage1.boundingRect();
113 void ImageComposer_ColorMaskOperator::drawResult( QPainter& thePainter,
114 const QVariant& theObj1,
115 const QVariant& ) const
117 if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() )
120 ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
122 QImage anImage = anImage1.convertToFormat( QImage::Format_ARGB32 );
124 int aRMin = myRefColor.red() - myRGBThreshold;
125 int aRMax = myRefColor.red() + myRGBThreshold;
126 int aGMin = myRefColor.green() - myRGBThreshold;
127 int aGMax = myRefColor.green() + myRGBThreshold;
128 int aBMin = myRefColor.blue() - myRGBThreshold;
129 int aBMax = myRefColor.blue() + myRGBThreshold;
130 int anAMin = myRefColor.alpha() - myAlphaThreshold;
131 int anAMax = myRefColor.alpha() + myAlphaThreshold;
133 QRgb aTransparent = TRANSPARENT.rgba();
135 for( int y = 0, aMaxY = anImage.height(); y < aMaxY; y++ )
136 for( int x = 0, aMaxX = anImage.width(); x < aMaxX; x++ )
138 QRgb* aLine = ( QRgb* )anImage.scanLine( y );
139 int aRed = qRed( aLine[x] );
140 int aGreen = qGreen( aLine[x] );
141 int aBlue = qBlue( aLine[x] );
142 int anAlpha = qAlpha( aLine[x] );
143 bool isInRange = ( anAMin <= anAlpha && anAlpha <= anAMax )
144 && ( aRMin <= aRed && aRed <= aRMax )
145 && ( aGMin <= aGreen && aGreen <= aGMax )
146 && ( aBMin <= aBlue && aBlue <= aBMax );
147 if( myIsMakeTransparent == isInRange )
148 aLine[x] = aTransparent;
151 ImageComposer_Image aResult;
153 aResult.setTransform( anImage1.transform() );
154 aResult.draw( thePainter );
157 void ImageComposer_ColorMaskOperator::storeArgs( QDataStream& theStream ) const
159 ImageComposer_Operator::storeArgs( theStream );
160 theStream << myRefColor;
161 theStream << myIsMakeTransparent;
162 theStream << myRGBThreshold;
163 theStream << myAlphaThreshold;
166 void ImageComposer_ColorMaskOperator::restoreArgs( QDataStream& theStream )
168 ImageComposer_Operator::restoreArgs( theStream );
169 theStream >> myRefColor;
170 theStream >> myIsMakeTransparent;
171 theStream >> myRGBThreshold;
172 theStream >> myAlphaThreshold;