summaryrefslogtreecommitdiff
path: root/src/Image/Image_PseudoColorImage.cxx
blob: c9212e3d72ca44315d4a3fe2b361526c15712350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <Image_PseudoColorImage.ixx>

#include <Image_LookupTable.hxx>
#include <TColStd_SetIteratorOfSetOfInteger.hxx>
#include <TColStd_SetOfInteger.hxx>
#include <Aspect_GenericColorMap.hxx>
#include <Aspect_ColorMapEntry.hxx>

Image_PseudoColorImage::Image_PseudoColorImage (
			const Standard_Integer x,
			const Standard_Integer y,
			const Standard_Integer dx,
			const Standard_Integer dy,
			const Handle(Aspect_ColorMap)& aColorMap) 
     : Image_DIndexedImage( x, y, dx, dy, Aspect_IndexPixel( 0 ) ) , 
	myColorMap (aColorMap) 

{ 
}

Image_PseudoColorImage::Image_PseudoColorImage (
			const Standard_Integer x,
			const Standard_Integer y,
			const Standard_Integer dx,
			const Standard_Integer dy,
			const Handle(Aspect_ColorMap)& aColorMap,
			const Aspect_IndexPixel& Back ) 
     : Image_DIndexedImage( x, y, dx, dy, Back ) , myColorMap (aColorMap) 

{
}

Handle(Image_Image) Image_PseudoColorImage::Dup() const {

  Handle(Image_PseudoColorImage) aImage = 
		new Image_PseudoColorImage(  LowerX(), LowerY(), 
				             Width(), Height(),
				             myColorMap,
					     BackgroundPixel() ) ;

	aImage->InternalDup( this ) ;

  return aImage ;
}

Image_TypeOfImage Image_PseudoColorImage::Type () const {

  return Image_TOI_PseudoColorImage ;

}

Handle(Aspect_ColorMap) Image_PseudoColorImage::ColorMap () const {

  return myColorMap;

}

const Quantity_Color& Image_PseudoColorImage::PixelColor( 
			const Standard_Integer x,
			const Standard_Integer y ) const 

{ return( myColorMap->FindEntry	( Pixel( x, y ).Value() 
				).Color() ) ;
}

void Image_PseudoColorImage::RowColor( const Standard_Integer Y,
			    Quantity_Array1OfColor&  PR) const {

  Standard_Integer TheLength = Min (PR.Length(), Width() );
  Standard_Integer L         = PR.Lower() ;
  Standard_Integer X         = LowerX() ;
  Standard_Integer NIndex    ;
  Standard_Integer PIndex    = Pixel( X, Y ).Value() ;
  Quantity_Color   PColor    = PixelColor( X, Y ) ;

  for (Standard_Integer i=0; i< TheLength; i++) {
	NIndex = Pixel(X+i,Y).Value() ;
	if ( NIndex != PIndex ) {
		PIndex  = NIndex ;
		PColor  = myColorMap->FindEntry( PIndex ).Color() ;
	}
	PR(L+i) = PColor;
  }

}

Handle(Quantity_HArray1OfColor) Image_PseudoColorImage::RowColor( 
		const Standard_Integer Y ) const {

  Standard_Integer TheLength = Width() ;
  Standard_Integer X         = LowerX() ;
  Standard_Integer NIndex ;
  Standard_Integer PIndex    = Pixel( X, Y ).Value() ;
  Quantity_Color   PColor    = PixelColor( X, Y ) ;

  Handle(Quantity_HArray1OfColor) PR = 
		new Quantity_HArray1OfColor( 0, TheLength-1) ;

  for (Standard_Integer i=0; i< TheLength; i++) {
	NIndex = Pixel(X+i,Y).Value() ;
	if ( NIndex != PIndex ) {
		PIndex  = NIndex ;
		PColor  = myColorMap->FindEntry( PIndex ).Color() ;
	}
	PR->SetValue(i,PColor);
  }

  return PR ;
}

void Image_PseudoColorImage::SqueezedLookupTable(
	const Aspect_IndexPixel& BasePixel,
	Image_LookupTable& aLookup ) const

{ TColStd_SetOfInteger 			PixelSet ;
  TColStd_SetIteratorOfSetOfInteger 	It;
  Standard_Integer 			x, y, i, UpX, UpY ;
//  Aspect_IndexPixel			aPixel ;
  //Image_LookupTable			aLookup( 101 ) ;
//  Aspect_ColorMapEntry			aEntry;

  UpX = UpperX() ;
  UpY = UpperY() ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		PixelSet.Add( Pixel( x, y ).Value() ) ;
	}
  }

#ifdef TRACE
	cout << "Squeeze() PixelSet Extent :" << PixelSet.Extent() << endl ;
#endif

  for (It.Initialize(PixelSet), i = BasePixel.Value() ;
	 It.More(); It.Next(), i++ ) {
	aLookup.Bind( It.Value(), i ) ;
  }

  // Modif CAL 10/02/95: passage par argument car copie inaccessible.
  // return( aLookup ) ;

}

Handle(Image_PseudoColorImage) Image_PseudoColorImage::Squeeze( 
	const Aspect_IndexPixel& BasePixel ) const

{ Handle(Image_PseudoColorImage) 	ret_image = NULL ;
  Handle(Aspect_GenericColorMap) 	newcmap   = NULL ;
  Handle(Aspect_ColorMap) 	        cmap      = ColorMap() ;
  TColStd_SetOfInteger 			PixelSet ;
  TColStd_SetIteratorOfSetOfInteger 	It;
  Standard_Integer 			x, y, i, UpX, UpY ;
//  Aspect_IndexPixel			aPixel ;
  Image_LookupTable			aLookup( 101 ) ;
  Aspect_ColorMapEntry			aEntry;

  UpX = UpperX() ;
  UpY = UpperY() ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		PixelSet.Add( Pixel( x, y ).Value() ) ;
	}
  }

#ifdef TRACE
	cout << "Squeeze() PixelSet Extent :" << PixelSet.Extent() << endl ;
#endif

  if ( PixelSet.Extent() != 0 ) {
	newcmap = new Aspect_GenericColorMap() ;

    	for (It.Initialize(PixelSet), i = BasePixel.Value() ; 
		It.More(); It.Next(), i++ ) {
		aLookup.Bind( It.Value(), i ) ;
		aEntry.SetValue( i, cmap->FindEntry( It.Value() ).Color() ) ;
		newcmap->AddEntry( aEntry ) ;
	}

	ret_image = new Image_PseudoColorImage( LowerX(), LowerY(),
				          	Width() , Height(),
					  	newcmap );

	ret_image->Fill( this ) ;

	ret_image->Lookup( aLookup ) ;

  }

  return( ret_image ) ;

}

void Image_PseudoColorImage::Lookup( const Image_LookupTable& aLookup )

{ Standard_Integer x,y, UpX, UpY;
  Aspect_IndexPixel val, lastval, newval;

  UpX = UpperX() ;
  UpY = UpperY() ;

  val     = Pixel( LowerX(), LowerY() ) ;
  lastval = val ;
  newval  = aLookup.Find( lastval ) ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		val = Pixel( x, y ) ;
		if ( !(val == lastval) ) {
			lastval = val ;
			newval  = aLookup.Find( lastval ) ;
		}
		SetPixel( x, y , newval ) ;
	}
  }
  
}

void Image_PseudoColorImage::Rescale( const Standard_Real Scale,
				      const Standard_Real Offset )

{ Standard_Real S = Scale ;
  Standard_Real O = Offset ;
  Standard_Integer x,y, UpX, UpY, val;

  UpX = UpperX() ;
  UpY = UpperY() ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		val = ( Standard_Integer ) ( Pixel(x, y).Value() * S + O ) ;
		MutPixel( x, y ).SetValue( val ) ;
	}
  }
  
}

void Image_PseudoColorImage::Extrema( Aspect_IndexPixel& PMin,
				      Aspect_IndexPixel& PMax ) const

{ Standard_Integer x,y, UpX, UpY, min, max, val, lastval;
//  Aspect_IndexPixel aPixel ;

  UpX = UpperX() ;
  UpY = UpperY() ;

  max = IntegerFirst() ;
  min = IntegerLast() ;

  lastval = Pixel( LowerX(), LowerY() ).Value() ;

  max = Max( max, lastval ) ;
  min = Min( min, lastval ) ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		val = Pixel( x, y ).Value();
		if ( val != lastval ) {
			lastval = val ;
			max = Max( max, lastval ) ;
			min = Min( min, lastval ) ;
		}		
	}
  }

  PMin.SetValue( min ) ;
  PMax.SetValue( max ) ;
  
}

void Image_PseudoColorImage::Threshold( const Aspect_IndexPixel& PMin,
				        const Aspect_IndexPixel& PMax, 
				        const Aspect_IndexPixel& PMap ) 

{ Standard_Integer x,y, UpX, UpY, min, max, val, map ;
  Aspect_IndexPixel ThePixel ;
  UpX = UpperX() ;
  UpY = UpperY() ;

  max = PMax.Value() ;
  min = PMin.Value() ;
  map = PMap.Value() ;

  for ( y = LowerY() ; y <= UpY ; y++ ) {
	for ( x = LowerX() ; x <= UpX ; x++ ) {
		ThePixel = Pixel( x, y ) ;
		val = Pixel( x, y ).Value();
		if ( val >= min && val <= max ) {
			MutPixel( x, y ).SetValue( map ) ;
		}		
	}
  }
}
#ifdef OLD
Handle(Standard_Transient) Image_PseudoColorImage::ShallowCopy() const {
	return DeepCopy() ;
}

Handle(Standard_Transient) Image_PseudoColorImage::DeepCopy() const {

  return Dup() ;
}
#endif