Salome HOME
Updated copyright comment
[modules/gui.git] / src / ImageComposer / ImageComposer_ColorMaskOperator.cxx
1 // Copyright (C) 2013-2024  CEA, EDF, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ImageComposer_ColorMaskOperator.h"
21 #include "ImageComposer_MetaTypes.h"
22 #include <QRectF>
23 #include <QRgb>
24 #include <QPixmap>
25 #include <QPainter>
26
27 /**
28   Constructor
29 */
30 ImageComposer_ColorMaskOperator::ImageComposer_ColorMaskOperator()
31 : ImageComposer_Operator(),
32   myRefColor( Qt::black ), myIsMakeTransparent( false ),
33   myRGBThreshold( 0 ), myAlphaThreshold( 0 )
34 {
35 }
36
37 /**
38 */
39 ImageComposer_ColorMaskOperator::~ImageComposer_ColorMaskOperator()
40 {
41 }
42
43 /**
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
50 */
51 void ImageComposer_ColorMaskOperator::setArgs( const QColor& theRefColor,
52                                                bool isMakeTransparent,
53                                                int theRGBThreshold,
54                                                int theAlphaThreshold )
55 {
56   myRefColor = theRefColor;
57   myIsMakeTransparent = isMakeTransparent;
58   myRGBThreshold = theRGBThreshold;
59   myAlphaThreshold = theAlphaThreshold;
60 }
61
62 /**
63 */
64 QString ImageComposer_ColorMaskOperator::name() const
65 {
66   return Type();
67 }
68
69 QStringList ImageComposer_ColorMaskOperator::dumpArgsToPython( QString& theArrayName ) const
70 {
71   QStringList aResList = ImageComposer_Operator::dumpArgsToPython( theArrayName );
72
73   QString aStreamName = theArrayName + "_stream";
74
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 );
81
82   aResList << QString( "" );
83   aResList << QString( "make_transparent = %1;" ).arg( myIsMakeTransparent );
84   aResList << QString( "%1 << make_transparent;" ).arg( aStreamName );
85
86   aResList << QString( "" );
87   aResList << QString( "rgb_threshold = %1;" ).arg( myRGBThreshold );
88   aResList << QString( "%1 << rgb_threshold;" ).arg( aStreamName );
89
90   aResList << QString( "" );
91   aResList << QString( "alpha_threshold = %1;" ).arg( myAlphaThreshold );
92   aResList << QString( "%1 << alpha_threshold;" ).arg( aStreamName );
93
94   return aResList;
95 }
96
97 /**
98 */
99 QRectF ImageComposer_ColorMaskOperator::calcResultBoundingRect( const QVariant& theObj1, 
100                                                                 const QVariant& ) const
101 {
102   QRectF aResRect;
103   if ( !theObj1.isNull() && theObj1.canConvert<ImageComposer_Image>() )
104   {
105     ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
106     aResRect = anImage1.boundingRect();
107   }
108   return aResRect;
109 }
110
111 /**
112 */
113 void ImageComposer_ColorMaskOperator::drawResult( QPainter&       thePainter,
114                                                   const QVariant& theObj1,
115                                                   const QVariant& ) const
116 {
117   if ( theObj1.isNull() || !theObj1.canConvert<ImageComposer_Image>() )
118     return;
119
120   ImageComposer_Image anImage1 = theObj1.value<ImageComposer_Image>();
121
122   QImage anImage = anImage1.convertToFormat( QImage::Format_ARGB32 );
123
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;
132
133   QRgb aTransparent = TRANSPARENT.rgba();
134
135   for( int y = 0, aMaxY = anImage.height(); y < aMaxY; y++ )
136     for( int x = 0, aMaxX = anImage.width(); x < aMaxX; x++ )
137     {
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;
149     }
150
151   ImageComposer_Image aResult;
152   aResult = anImage;
153   aResult.setTransform( anImage1.transform() );
154   aResult.draw( thePainter );
155 }
156
157 void ImageComposer_ColorMaskOperator::storeArgs( QDataStream& theStream ) const
158 {
159   ImageComposer_Operator::storeArgs( theStream );
160   theStream << myRefColor;
161   theStream << myIsMakeTransparent;
162   theStream << myRGBThreshold;
163   theStream << myAlphaThreshold;
164 }
165
166 void ImageComposer_ColorMaskOperator::restoreArgs( QDataStream& theStream )
167 {
168   ImageComposer_Operator::restoreArgs( theStream );
169   theStream >> myRefColor;
170   theStream >> myIsMakeTransparent;
171   theStream >> myRGBThreshold;
172   theStream >> myAlphaThreshold;
173 }