0023912: TDataStd_ExtStringArray::Value() returns a copy of TCollection_ExtendedStrin...
[occt.git] / src / Image / Image_PseudoColorImage.cxx
CommitLineData
b311480e 1// Copyright (c) 1995-1999 Matra Datavision
2// Copyright (c) 1999-2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
7fd59977 19#include <Image_PseudoColorImage.ixx>
20
21#include <Image_LookupTable.hxx>
22#include <TColStd_SetIteratorOfSetOfInteger.hxx>
23#include <TColStd_SetOfInteger.hxx>
24#include <Aspect_GenericColorMap.hxx>
25#include <Aspect_ColorMapEntry.hxx>
26
27Image_PseudoColorImage::Image_PseudoColorImage (
28 const Standard_Integer x,
29 const Standard_Integer y,
30 const Standard_Integer dx,
31 const Standard_Integer dy,
32 const Handle(Aspect_ColorMap)& aColorMap)
33 : Image_DIndexedImage( x, y, dx, dy, Aspect_IndexPixel( 0 ) ) ,
34 myColorMap (aColorMap)
35
36{
37}
38
39Image_PseudoColorImage::Image_PseudoColorImage (
40 const Standard_Integer x,
41 const Standard_Integer y,
42 const Standard_Integer dx,
43 const Standard_Integer dy,
44 const Handle(Aspect_ColorMap)& aColorMap,
45 const Aspect_IndexPixel& Back )
46 : Image_DIndexedImage( x, y, dx, dy, Back ) , myColorMap (aColorMap)
47
48{
49}
50
51Handle(Image_Image) Image_PseudoColorImage::Dup() const {
52
53 Handle(Image_PseudoColorImage) aImage =
54 new Image_PseudoColorImage( LowerX(), LowerY(),
55 Width(), Height(),
56 myColorMap,
57 BackgroundPixel() ) ;
58
59 aImage->InternalDup( this ) ;
60
61 return aImage ;
62}
63
64Image_TypeOfImage Image_PseudoColorImage::Type () const {
65
66 return Image_TOI_PseudoColorImage ;
67
68}
69
70Handle(Aspect_ColorMap) Image_PseudoColorImage::ColorMap () const {
71
72 return myColorMap;
73
74}
75
76const Quantity_Color& Image_PseudoColorImage::PixelColor(
77 const Standard_Integer x,
78 const Standard_Integer y ) const
79
80{ return( myColorMap->FindEntry ( Pixel( x, y ).Value()
81 ).Color() ) ;
82}
83
84void Image_PseudoColorImage::RowColor( const Standard_Integer Y,
85 Quantity_Array1OfColor& PR) const {
86
87 Standard_Integer TheLength = Min (PR.Length(), Width() );
88 Standard_Integer L = PR.Lower() ;
89 Standard_Integer X = LowerX() ;
90 Standard_Integer NIndex ;
91 Standard_Integer PIndex = Pixel( X, Y ).Value() ;
92 Quantity_Color PColor = PixelColor( X, Y ) ;
93
94 for (Standard_Integer i=0; i< TheLength; i++) {
95 NIndex = Pixel(X+i,Y).Value() ;
96 if ( NIndex != PIndex ) {
97 PIndex = NIndex ;
98 PColor = myColorMap->FindEntry( PIndex ).Color() ;
99 }
100 PR(L+i) = PColor;
101 }
102
103}
104
105Handle(Quantity_HArray1OfColor) Image_PseudoColorImage::RowColor(
106 const Standard_Integer Y ) const {
107
108 Standard_Integer TheLength = Width() ;
109 Standard_Integer X = LowerX() ;
110 Standard_Integer NIndex ;
111 Standard_Integer PIndex = Pixel( X, Y ).Value() ;
112 Quantity_Color PColor = PixelColor( X, Y ) ;
113
114 Handle(Quantity_HArray1OfColor) PR =
115 new Quantity_HArray1OfColor( 0, TheLength-1) ;
116
117 for (Standard_Integer i=0; i< TheLength; i++) {
118 NIndex = Pixel(X+i,Y).Value() ;
119 if ( NIndex != PIndex ) {
120 PIndex = NIndex ;
121 PColor = myColorMap->FindEntry( PIndex ).Color() ;
122 }
123 PR->SetValue(i,PColor);
124 }
125
126 return PR ;
127}
128
129void Image_PseudoColorImage::SqueezedLookupTable(
130 const Aspect_IndexPixel& BasePixel,
131 Image_LookupTable& aLookup ) const
132
133{ TColStd_SetOfInteger PixelSet ;
134 TColStd_SetIteratorOfSetOfInteger It;
135 Standard_Integer x, y, i, UpX, UpY ;
136// Aspect_IndexPixel aPixel ;
137 //Image_LookupTable aLookup( 101 ) ;
138// Aspect_ColorMapEntry aEntry;
139
140 UpX = UpperX() ;
141 UpY = UpperY() ;
142
143 for ( y = LowerY() ; y <= UpY ; y++ ) {
144 for ( x = LowerX() ; x <= UpX ; x++ ) {
145 PixelSet.Add( Pixel( x, y ).Value() ) ;
146 }
147 }
148
149#ifdef TRACE
150 cout << "Squeeze() PixelSet Extent :" << PixelSet.Extent() << endl ;
151#endif
152
153 for (It.Initialize(PixelSet), i = BasePixel.Value() ;
154 It.More(); It.Next(), i++ ) {
155 aLookup.Bind( It.Value(), i ) ;
156 }
157
158 // Modif CAL 10/02/95: passage par argument car copie inaccessible.
159 // return( aLookup ) ;
160
161}
162
163Handle(Image_PseudoColorImage) Image_PseudoColorImage::Squeeze(
164 const Aspect_IndexPixel& BasePixel ) const
165
166{ Handle(Image_PseudoColorImage) ret_image = NULL ;
167 Handle(Aspect_GenericColorMap) newcmap = NULL ;
168 Handle(Aspect_ColorMap) cmap = ColorMap() ;
169 TColStd_SetOfInteger PixelSet ;
170 TColStd_SetIteratorOfSetOfInteger It;
171 Standard_Integer x, y, i, UpX, UpY ;
172// Aspect_IndexPixel aPixel ;
173 Image_LookupTable aLookup( 101 ) ;
174 Aspect_ColorMapEntry aEntry;
175
176 UpX = UpperX() ;
177 UpY = UpperY() ;
178
179 for ( y = LowerY() ; y <= UpY ; y++ ) {
180 for ( x = LowerX() ; x <= UpX ; x++ ) {
181 PixelSet.Add( Pixel( x, y ).Value() ) ;
182 }
183 }
184
185#ifdef TRACE
186 cout << "Squeeze() PixelSet Extent :" << PixelSet.Extent() << endl ;
187#endif
188
189 if ( PixelSet.Extent() != 0 ) {
190 newcmap = new Aspect_GenericColorMap() ;
191
192 for (It.Initialize(PixelSet), i = BasePixel.Value() ;
193 It.More(); It.Next(), i++ ) {
194 aLookup.Bind( It.Value(), i ) ;
195 aEntry.SetValue( i, cmap->FindEntry( It.Value() ).Color() ) ;
196 newcmap->AddEntry( aEntry ) ;
197 }
198
199 ret_image = new Image_PseudoColorImage( LowerX(), LowerY(),
200 Width() , Height(),
201 newcmap );
202
203 ret_image->Fill( this ) ;
204
205 ret_image->Lookup( aLookup ) ;
206
207 }
208
209 return( ret_image ) ;
210
211}
212
213void Image_PseudoColorImage::Lookup( const Image_LookupTable& aLookup )
214
215{ Standard_Integer x,y, UpX, UpY;
216 Aspect_IndexPixel val, lastval, newval;
217
218 UpX = UpperX() ;
219 UpY = UpperY() ;
220
221 val = Pixel( LowerX(), LowerY() ) ;
222 lastval = val ;
223 newval = aLookup.Find( lastval ) ;
224
225 for ( y = LowerY() ; y <= UpY ; y++ ) {
226 for ( x = LowerX() ; x <= UpX ; x++ ) {
227 val = Pixel( x, y ) ;
228 if ( !(val == lastval) ) {
229 lastval = val ;
230 newval = aLookup.Find( lastval ) ;
231 }
232 SetPixel( x, y , newval ) ;
233 }
234 }
235
236}
237
238void Image_PseudoColorImage::Rescale( const Standard_Real Scale,
239 const Standard_Real Offset )
240
241{ Standard_Real S = Scale ;
242 Standard_Real O = Offset ;
243 Standard_Integer x,y, UpX, UpY, val;
244
245 UpX = UpperX() ;
246 UpY = UpperY() ;
247
248 for ( y = LowerY() ; y <= UpY ; y++ ) {
249 for ( x = LowerX() ; x <= UpX ; x++ ) {
250 val = ( Standard_Integer ) ( Pixel(x, y).Value() * S + O ) ;
251 MutPixel( x, y ).SetValue( val ) ;
252 }
253 }
254
255}
256
257void Image_PseudoColorImage::Extrema( Aspect_IndexPixel& PMin,
258 Aspect_IndexPixel& PMax ) const
259
260{ Standard_Integer x,y, UpX, UpY, min, max, val, lastval;
261// Aspect_IndexPixel aPixel ;
262
263 UpX = UpperX() ;
264 UpY = UpperY() ;
265
266 max = IntegerFirst() ;
267 min = IntegerLast() ;
268
269 lastval = Pixel( LowerX(), LowerY() ).Value() ;
270
271 max = Max( max, lastval ) ;
272 min = Min( min, lastval ) ;
273
274 for ( y = LowerY() ; y <= UpY ; y++ ) {
275 for ( x = LowerX() ; x <= UpX ; x++ ) {
276 val = Pixel( x, y ).Value();
277 if ( val != lastval ) {
278 lastval = val ;
279 max = Max( max, lastval ) ;
280 min = Min( min, lastval ) ;
281 }
282 }
283 }
284
285 PMin.SetValue( min ) ;
286 PMax.SetValue( max ) ;
287
288}
289
290void Image_PseudoColorImage::Threshold( const Aspect_IndexPixel& PMin,
291 const Aspect_IndexPixel& PMax,
292 const Aspect_IndexPixel& PMap )
293
294{ Standard_Integer x,y, UpX, UpY, min, max, val, map ;
295 Aspect_IndexPixel ThePixel ;
296 UpX = UpperX() ;
297 UpY = UpperY() ;
298
299 max = PMax.Value() ;
300 min = PMin.Value() ;
301 map = PMap.Value() ;
302
303 for ( y = LowerY() ; y <= UpY ; y++ ) {
304 for ( x = LowerX() ; x <= UpX ; x++ ) {
305 ThePixel = Pixel( x, y ) ;
306 val = Pixel( x, y ).Value();
307 if ( val >= min && val <= max ) {
308 MutPixel( x, y ).SetValue( map ) ;
309 }
310 }
311 }
312}
313#ifdef OLD
314Handle(Standard_Transient) Image_PseudoColorImage::ShallowCopy() const {
315 return DeepCopy() ;
316}
317
318Handle(Standard_Transient) Image_PseudoColorImage::DeepCopy() const {
319
320 return Dup() ;
321}
322#endif