summaryrefslogtreecommitdiff
path: root/src/Image/Image_AveragePixelInterpolation.cxx
blob: b8249ec2b2e5537cbaf7f88f7945a43cbda8734b (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <Image_AveragePixelInterpolation.ixx>


Image_AveragePixelInterpolation::Image_AveragePixelInterpolation() {}

Standard_Boolean Image_AveragePixelInterpolation::Interpolate( 
	const Handle(Image_Image)& aImage,
	const Standard_Real FX, const Standard_Real FY,
	const Standard_Integer LowX,
	const Standard_Integer LowY,
	const Standard_Integer UpX,
	const Standard_Integer UpY,
	Aspect_Pixel& aPixel ) const

{ 

  if ( aImage->IsKind(STANDARD_TYPE(Image_DIndexedImage))) {

	return Interpolate( Handle(Image_DIndexedImage)::DownCast( aImage ),
			FX,FY,LowX,LowY,UpX,UpY,(Aspect_IndexPixel &)aPixel ) ;

  }
  else if ( aImage->IsKind(STANDARD_TYPE(Image_DColorImage))) {

	return Interpolate( Handle(Image_DColorImage)::DownCast( aImage ),
			FX,FY,LowX,LowY,UpX,UpY,(Aspect_ColorPixel &)aPixel ) ;
  }
  else {
	return Image_PixelInterpolation::Interpolate( aImage,
			FX,FY,LowX,LowY,UpX,UpY,aPixel ) ;
  }
}

Standard_Boolean Image_AveragePixelInterpolation::Interpolate( 
	const Handle(Image_DColorImage)& aImage,
	const Standard_Real FX, const Standard_Real FY,
	const Standard_Integer LowX,
	const Standard_Integer LowY,
	const Standard_Integer UpX,
	const Standard_Integer UpY,
	Aspect_ColorPixel& aPixel ) const

{ Standard_Integer NX[3], NY[3] ;
  Standard_Real    NZ[3] ;
  Standard_Real    R,G,B ;
  static Quantity_Color Col ;
  Standard_Boolean SamePixels = 1 ;

  if ( FX < 0. ) NX[0] = Standard_Integer(FX-0.5) ;
  else           NX[0] = Standard_Integer(FX+0.5) ;

  if ( FY < 0. ) NY[0] = Standard_Integer(FY-0.5) ;
  else           NY[0] = Standard_Integer(FY+0.5) ;

  if ( NX[0] < LowX || NX[0] > UpX ||
       NY[0] < LowY || NY[0] > UpY ) {
	return Standard_False ;
  }
  else if ( ( FX-NX[0] ) == 0. && ( FY-NY[0] ) == 0. ) {
	aImage->Pixel( NX[0], NY[0], aPixel );
	return Standard_True ;
  }
  else {

	if ( ( FX-NX[0] ) >= 0. )	{ NX[1] = NX[0]+1 ; NY[1] = NY[0] ; }
	else				{ NX[1] = NX[0]-1 ; NY[1] = NY[0] ; }
	if ( ( FY-NY[0] ) >= 0. )	{ NX[2] = NX[0]   ; NY[2] = NY[0]+1 ; }
	else				{ NX[2] = NX[0]   ; NY[2] = NY[0]-1 ; }

	if ( NX[1] < LowX || NX[1] > UpX || NY[1] < LowY || NY[1] > UpY ||
	     NX[2] < LowX || NX[2] > UpX || NY[2] < LowY || NY[2] > UpY ) {
	  	aImage->Pixel( NX[0], NY[0], aPixel );
	}
	else {
		NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Red() ;
		NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Red() ;
		NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Red() ;

		if ( NZ[0] == NZ[1] && NZ[0] == NZ[2] ) {
			R = NZ[0] ;
		}
		else {
			R = ( NZ[0] + NZ[1] + NZ[2] ) / 3. ;
			SamePixels = 0 ;
		}

		NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Green() ;
		NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Green() ;
		NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Green() ;

		if ( NZ[0] == NZ[1] && NZ[0] == NZ[2] ) {
			G = NZ[0] ;
		}
		else {
			G = ( NZ[0] + NZ[1] + NZ[2] ) / 3. ;
			SamePixels = 0 ;
		}

		NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Blue() ;
		NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Blue() ;
		NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Blue() ;

		if ( NZ[0] == NZ[1] && NZ[0] == NZ[2] ) {
			B = NZ[0] ;
		}
		else {
			B = ( NZ[0] + NZ[1] + NZ[2] ) / 3. ;
			SamePixels = 0 ;
		}

		if ( SamePixels ) {
			aPixel.SetValue( aImage->Pixel( NX[0],NY[0] ).Value() );
		}
		else {
		  	Col.SetValues(  R, G, B, Quantity_TOC_RGB ) ;

		  	aPixel.SetValue( Col ) ;
		}
	}

	return Standard_True ;
  }
}

Standard_Boolean Image_AveragePixelInterpolation::Interpolate( 
	const Handle(Image_DIndexedImage)& aImage,
	const Standard_Real FX, const Standard_Real FY,
	const Standard_Integer LowX,
	const Standard_Integer LowY,
	const Standard_Integer UpX,
	const Standard_Integer UpY,
	Aspect_IndexPixel& aPixel ) const

{ Standard_Integer NX[3], NY[3] ;
  Standard_Real    NZ[3] ;


  if ( FX < 0. ) NX[0] = Standard_Integer(FX-0.5) ;
  else           NX[0] = Standard_Integer(FX+0.5) ;

  if ( FY < 0. ) NY[0] = Standard_Integer(FY-0.5) ;
  else           NY[0] = Standard_Integer(FY+0.5) ;

  if ( NX[0] < LowX || NX[0] > UpX ||
       NY[0] < LowY || NY[0] > UpY ) {
	return Standard_False ;
  }
  else if ( ( FX-NX[0] ) == 0. && ( FY-NY[0] ) == 0. ) {
	aImage->Pixel( NX[0], NY[0], aPixel );
	return Standard_True ;
  }
  else {

	if ( ( FX-NX[0] ) >= 0. )	{ NX[1] = NX[0]+1 ; NY[1] = NY[0] ; }
	else				{ NX[1] = NX[0]-1 ; NY[1] = NY[0] ; }
	if ( ( FY-NY[0] ) >= 0. )	{ NX[2] = NX[0]   ; NY[2] = NY[0]+1 ; }
	else				{ NX[2] = NX[0]   ; NY[2] = NY[0]-1 ; }

	if ( NX[1] < LowX || NX[1] > UpX || NY[1] < LowY || NY[1] > UpY ||
	     NX[2] < LowX || NX[2] > UpX || NY[2] < LowY || NY[2] > UpY ) {
	  	aImage->Pixel( NX[0], NY[0], aPixel );
	}
	else {
		NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value() ;
		NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value() ;
		NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value() ;

		if ( NZ[0] == NZ[1] && NZ[0] == NZ[2] ) {
		  aPixel.SetValue( Standard_Integer( NZ[0] ) ) ;
		}
		else {
		  aPixel.SetValue( Standard_Integer((NZ[0]+NZ[1]+NZ[2])/3.) ) ;
		}

	}

	return Standard_True ;
  }
}

//##############################################################################

#ifdef OLD
Standard_Boolean Image_AveragePixelInterpolation::Interpolate( 
	const Handle(Image_DColorImage)& aImage,
	const Standard_Real FX, const Standard_Real FY,
	const Standard_Integer LowX,
	const Standard_Integer LowY,
	const Standard_Integer UpX,
	const Standard_Integer UpY,
	Aspect_ColorPixel& aPixel ) const

{ Standard_Integer NX = Standard_Integer(FX) ;
  Standard_Integer NY = Standard_Integer(FY) ;
  Standard_Integer X,Y ;
  Standard_Real SD, SR, SG, SB, R, G, B ;

  if ( NX < ( LowX-1 ) || NX > UpX || 
       NY < ( LowY-1 ) || NY > UpY ) {
	return Standard_False ;
  }
  else {

	if ( FX < 0. ) NX-- ;
	if ( FY < 0. ) NY-- ;

	SR = SG = SB = SD = 0. ;

	// (0,0)
	X = NX ; Y = NY ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  aImage->Pixel( X,Y ).Value().Values( R, G, B, Quantity_TOC_RGB ) ;

	  SR += R ; SG += G ; SB += B ; SD += 1. ;
	}

	// (1,0)
	X = NX+1 ; Y = NY ;

	if ( !( X < LowX || X > UpX || Y < LowY || Y > UpY ) ) {
	  aImage->Pixel( X,Y ).Value().Values( R, G, B, Quantity_TOC_RGB ) ;

	  SR += R ; SG += G ; SB += B ; SD += 1. ;
	}

	// (0,1)
	X = NX ; Y = NY+1 ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  aImage->Pixel( X,Y ).Value().Values( R, G, B, Quantity_TOC_RGB ) ;

	  SR += R ; SG += G ; SB += B ; SD += 1. ;
	}

	// (1,1)
	X = NX+1 ; Y = NY+1 ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  aImage->Pixel( X,Y ).Value().Values( R, G, B, Quantity_TOC_RGB ) ;

	  SR += R ; SG += G ; SB += B ; SD += 1. ;
	}

	// Result
	if ( SD != 0. ) {
	  SR /= SD ; SG /= SD ; SB /= SD ;

	  aPixel.SetValue( Quantity_Color( SR, SG, SB, Quantity_TOC_RGB ) ) ;

	  return Standard_True ;
	}
	else {
	  return Standard_False ;
	}
  }
}

Standard_Boolean Image_AveragePixelInterpolation::Interpolate( 
	const Handle(Image_DIndexedImage)& aImage,
	const Standard_Real FX, const Standard_Real FY,
	const Standard_Integer LowX,
	const Standard_Integer LowY,
	const Standard_Integer UpX,
	const Standard_Integer UpY,
	Aspect_IndexPixel& aPixel ) const

{ Standard_Integer NX = Standard_Integer(FX) ;
  Standard_Integer NY = Standard_Integer(FY) ;
  Standard_Integer X,Y ;
  Standard_Real SD, SP ;

  if ( NX < ( LowX-1 ) || NX > UpX || 
       NY < ( LowY-1 ) || NY > UpY ) {
	return Standard_False ;
  }
  else {

	if ( FX < 0. ) NX-- ;
	if ( FY < 0. ) NY-- ;

	SP = SD = 0. ;

	// (0,0)
	X = NX ; Y = NY ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  SP += aImage->Pixel( X,Y ).Value() ; SD += 1. ;
	}

	// (1,0)
	X = NX+1 ; Y = NY ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  SP += aImage->Pixel( X,Y ).Value() ; SD += 1. ;
	}

	// (0,1)
	X = NX ; Y = NY+1 ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  SP += aImage->Pixel( X,Y ).Value() ; SD += 1. ;
	}

	// (1,1)
	X = NX+1 ; Y = NY+1 ;

	if ( !( X < LowX || X > UpX || 
	        Y < LowY || Y > UpY ) ) {
	  SP += aImage->Pixel( X,Y ).Value() ; SD += 1. ;
	}

	// Result

	if ( SD != 0. ) {
	  SP /= SD ;

	  aPixel.SetValue( Standard_Integer(SP+0.5) ) ;

	  return Standard_True ;
	}
	else {
	  return Standard_False ;
	}
  }
}
#endif