0023912: TDataStd_ExtStringArray::Value() returns a copy of TCollection_ExtendedStrin...
[occt.git] / src / Image / Image_BalancedPixelInterpolation.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_BalancedPixelInterpolation.ixx>
20
21
22Image_BalancedPixelInterpolation::Image_BalancedPixelInterpolation() {}
23
24Standard_Boolean Image_BalancedPixelInterpolation::Interpolate(
25 const Handle(Image_Image)& aImage,
26 const Standard_Real FX, const Standard_Real FY,
27 const Standard_Integer LowX,
28 const Standard_Integer LowY,
29 const Standard_Integer UpX,
30 const Standard_Integer UpY,
31 Aspect_Pixel& aPixel ) const
32
33{
34
35 if ( aImage->IsKind(STANDARD_TYPE(Image_DIndexedImage))) {
36
37 return Interpolate( Handle(Image_DIndexedImage)::DownCast( aImage ),
38 FX,FY,LowX,LowY,UpX,UpY,(Aspect_IndexPixel &)aPixel ) ;
39
40 }
41 else if ( aImage->IsKind(STANDARD_TYPE(Image_DColorImage))) {
42
43 return Interpolate( Handle(Image_DColorImage)::DownCast( aImage ),
44 FX,FY,LowX,LowY,UpX,UpY,(Aspect_ColorPixel &)aPixel ) ;
45 }
46 else {
47 return Image_PixelInterpolation::Interpolate( aImage,
48 FX,FY,LowX,LowY,UpX,UpY,aPixel ) ;
49 }
50}
51
52Standard_Boolean Image_BalancedPixelInterpolation::Interpolate(
53 const Handle(Image_DColorImage)& aImage,
54 const Standard_Real FX, const Standard_Real FY,
55 const Standard_Integer LowX,
56 const Standard_Integer LowY,
57 const Standard_Integer UpX,
58 const Standard_Integer UpY,
59 Aspect_ColorPixel& aPixel ) const
60
61{ Standard_Integer NX = Standard_Integer(FX) ;
62 Standard_Integer NY = Standard_Integer(FY) ;
63 Standard_Integer X[4],Y[4], i ;
64 Standard_Real R[4], G[4], B[4];
65 Standard_Boolean DoV[4], SamePixels = 1 ;
66 Standard_Real SR, SG, SB;
67
68 if ( FX < 0. ) NX-- ;
69 if ( FY < 0. ) NY-- ;
70
71 if ( NX < ( LowX-1 ) || NX > UpX ||
72 NY < ( LowY-1 ) || NY > UpY ) {
73 return Standard_False ;
74 }
75 else {
76
77 for ( i = 0 ; i < 4 ; i++ ) DoV[i] = 0 ;
78
79 // (0,0)
80 i = 0; X[i] = NX ; Y[i] = NY ;
81
82 if ( !( X[i] < LowX || X[i] > UpX ||
83 Y[i] < LowY || Y[i] > UpY ) ) {
84 aImage->Pixel(X[i],Y[i]).Value().
85 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
86 DoV[i] = 1 ;
87 }
88 else {
89 return Standard_False ;
90 }
91
92 // (1,0)
93 i++ ; X[i] = NX+1 ; Y[i] = NY ;
94
95 if ( !( X[i] < LowX || X[i] > UpX ||
96 Y[i] < LowY || Y[i] > UpY ) ) {
97 aImage->Pixel(X[i],Y[i]).Value().
98 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
99 DoV[i] = 1 ;
100 }
101 else {
102 R[i] = R[0] ; G[i] = G[0] ; B[i] = B[0] ;
103 }
104
105 // (0,1)
106 i++ ; X[i] = NX ; Y[i] = NY+1 ;
107
108 if ( !( X[i] < LowX || X[i] > UpX ||
109 Y[i] < LowY || Y[i] > UpY ) ) {
110 aImage->Pixel(X[i],Y[i]).Value().
111 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
112 DoV[i] = 1 ;
113 }
114 else {
115 R[i] = R[0] ; G[i] = G[0] ; B[i] = B[0] ;
116 }
117
118
119 // (1,1)
120 i++ ; X[i] = NX+1 ; Y[i] = NY+1 ;
121
122 if ( !( X[i] < LowX || X[i] > UpX ||
123 Y[i] < LowY || Y[i] > UpY ) ) {
124 aImage->Pixel(X[i],Y[i]).Value().
125 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
126 DoV[i] = 1 ;
127 }
128 else {
129 R[i] = R[0] ; G[i] = G[0] ; B[i] = B[0] ;
130 }
131
132// Pixel A [0] Pixel B [1]
133// Pixel X
134// Pixel C [2] Pixel D [3]
135
136
137 Standard_Boolean First ;
138 Standard_Integer Ref ;
139 Standard_Real ColDelta,RowDelta, ContribFromAB, ContribFromCD ;
140
141 ColDelta = FX - X[0] ;
142 RowDelta = FY - Y[0] ;
143
144 // Red Componant
145
146 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
147 if ( DoV[i] ) {
148 if ( First ) { Ref = i ; First = 0 ; }
149 else if ( R[i] != R[Ref] ) { SamePixels = 0 ; break ;}
150 }
151 }
152
153 if ( Ref == -1 ) {
154 return Standard_False ;
155 }
156 else if ( SamePixels ) {
157 SR = R[Ref] ;
158 }
159 else {
160 ContribFromAB = ColDelta * ( R[1] - R[0] ) + R[0] ;
161 ContribFromCD = ColDelta * ( R[3] - R[2] ) + R[2] ;
162
163 SR = ContribFromAB +
164 ( ContribFromCD - ContribFromAB ) * RowDelta ;
165 }
166
167 // Green Componant
168
169 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
170 if ( DoV[i] ) {
171 if ( First ) { Ref = i ; First = 0 ; }
172 else if ( G[i] != G[Ref] ) { SamePixels = 0 ; break ;}
173 }
174 }
175
176 if ( Ref == -1 ) {
177 return Standard_False ;
178 }
179 else if ( SamePixels ) {
180 SG = G[Ref] ;
181 }
182 else {
183 ContribFromAB = ColDelta * ( G[1] - G[0] ) + G[0] ;
184 ContribFromCD = ColDelta * ( G[3] - G[2] ) + G[2] ;
185
186 SG = ContribFromAB +
187 ( ContribFromCD - ContribFromAB ) * RowDelta ;
188 }
189
190
191 // Blue Componant
192
193 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
194 if ( DoV[i] ) {
195 if ( First ) { Ref = i ; First = 0 ; }
196 else if ( B[i] != B[Ref] ) { SamePixels = 0 ; break ;}
197 }
198 }
199
200 if ( Ref == -1 ) {
201 return Standard_False ;
202 }
203 else if ( SamePixels ) {
204 SB = B[Ref] ;
205 }
206 else {
207 ContribFromAB = ColDelta * ( B[1] - B[0] ) + B[0] ;
208 ContribFromCD = ColDelta * ( B[3] - B[2] ) + B[2] ;
209
210 SB = ContribFromAB +
211 ( ContribFromCD - ContribFromAB ) * RowDelta ;
212 }
213
214 // Result
215 aPixel.SetValue( Quantity_Color( SR, SG, SB, Quantity_TOC_RGB ) ) ;
216
217 return Standard_True ;
218 }
219}
220
221Standard_Boolean Image_BalancedPixelInterpolation::Interpolate(
222 const Handle(Image_DIndexedImage)& aImage,
223 const Standard_Real FX, const Standard_Real FY,
224 const Standard_Integer LowX,
225 const Standard_Integer LowY,
226 const Standard_Integer UpX,
227 const Standard_Integer UpY,
228 Aspect_IndexPixel& aPixel ) const
229
230{ Standard_Integer NX = Standard_Integer(FX) ;
231 Standard_Integer NY = Standard_Integer(FY) ;
232 Standard_Integer X[4],Y[4] ;
233 Standard_Integer V[4], i ;
234 Standard_Boolean DoV[4], SamePixels = 1 ;
235 Standard_Real SP ;
236
237 if ( FX < 0. ) NX-- ;
238 if ( FY < 0. ) NY-- ;
239
240 if ( NX < ( LowX-1 ) || NX > UpX ||
241 NY < ( LowY-1 ) || NY > UpY ) {
242 return Standard_False ;
243 }
244 else {
245
246 for ( i = 0 ; i < 4 ; i++ ) DoV[i] = 0 ;
247
248 i = 0;
249
250 // (0,0)
251 i = 0; X[i] = NX ; Y[i] = NY ;
252
253 if ( !( X[i] < LowX || X[i] > UpX ||
254 Y[i] < LowY || Y[i] > UpY ) ) {
255 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
256 }
257 else {
258 return Standard_False ;
259 }
260
261 // (1,0)
262 i++ ; X[i] = NX+1 ; Y[i] = NY ;
263
264 if ( !( X[i] < LowX || X[i] > UpX ||
265 Y[i] < LowY || Y[i] > UpY ) ) {
266 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
267 }
268 else {
269 V[i] = V[0] ;
270 }
271
272 // (0,1)
273 i++ ; X[i] = NX ; Y[i] = NY+1 ;
274
275 if ( !( X[i] < LowX || X[i] > UpX ||
276 Y[i] < LowY || Y[i] > UpY ) ) {
277 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
278 }
279 else {
280 V[i] = V[0] ;
281 }
282
283 // (1,1)
284 i++ ; X[i] = NX+1 ; Y[i] = NY+1 ;
285
286 if ( !( X[i] < LowX || X[i] > UpX ||
287 Y[i] < LowY || Y[i] > UpY ) ) {
288 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
289 }
290 else {
291 V[i] = V[0] ;
292 }
293
294
295// Pixel A [0] Pixel B [1]
296// Pixel X
297// Pixel C [2] Pixel D [3]
298
299
300 Standard_Boolean First ;
301 Standard_Integer Ref ;
302 Standard_Real ColDelta,RowDelta, ContribFromAB, ContribFromCD ;
303
304 for ( i = 0 , First = 1, Ref = -1 ; i < 4 ; i++ ) {
305 if ( DoV[i] ) {
306 if ( First ) { Ref = i ; First = 0 ; }
307 else if ( V[i] != V[Ref] ) { SamePixels = 0 ; break ;}
308 }
309 }
310
311 if ( Ref == -1 ) {
312 return Standard_False ;
313 }
314 else if ( SamePixels ) {
315 aPixel.SetValue( V[Ref] ) ;
316 return Standard_True ;
317 }
318 else {
319 ColDelta = FX - X[0] ;
320 RowDelta = FY - Y[0] ;
321
322 ContribFromAB = ColDelta * ( V[1] - V[0] ) + V[0] ;
323 ContribFromCD = ColDelta * ( V[3] - V[2] ) + V[2] ;
324
325 SP = ContribFromAB +
326 ( ContribFromCD - ContribFromAB ) * RowDelta ;
327
328 aPixel.SetValue( Standard_Integer(SP+0.5) ) ;
329
330 return Standard_True ;
331 }
332 }
333}
334
335#ifdef OLD
336
337Standard_Boolean Image_BalancedPixelInterpolation::Interpolate(
338 const Handle(Image_DColorImage)& aImage,
339 const Standard_Real FX, const Standard_Real FY,
340 const Standard_Integer LowX,
341 const Standard_Integer LowY,
342 const Standard_Integer UpX,
343 const Standard_Integer UpY,
344 Aspect_ColorPixel& aPixel ) const
345
346{ Standard_Integer NX = Standard_Integer(FX) ;
347 Standard_Integer NY = Standard_Integer(FY) ;
348 Standard_Integer X[4],Y[4], i ;
349 Standard_Real R[4], G[4], B[4];
350 Standard_Boolean DoV[4], SamePixels = 1 ;
351 Standard_Real D[4], SD, SR, SG, SB;
352
353 if ( NX < ( LowX-1 ) || NX > UpX ||
354 NY < ( LowY-1 ) || NY > UpY ) {
355 return Standard_False ;
356 }
357 else {
358
359 if ( FX < 0. ) NX-- ;
360 if ( FY < 0. ) NY-- ;
361
362 for ( i = 0 ; i < 4 ; i++ ) DoV[i] = 0 ;
363
364 // (0,0)
365 i = 0; X[i] = NX ; Y[i] = NY ;
366
367 if ( !( X[i] < LowX || X[i] > UpX ||
368 Y[i] < LowY || Y[i] > UpY ) ) {
369 aImage->Pixel(X[i],Y[i]).Value().
370 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
371 DoV[i] = 1 ;
372 }
373
374 // (1,0)
375 i++ ; X[i] = NX+1 ; Y[i] = NY ;
376
377 if ( !( X[i] < LowX || X[i] > UpX ||
378 Y[i] < LowY || Y[i] > UpY ) ) {
379 aImage->Pixel(X[i],Y[i]).Value().
380 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
381 DoV[i] = 1 ;
382 }
383
384 // (0,1)
385 i++ ; X[i] = NX ; Y[i] = NY+1 ;
386
387 if ( !( X[i] < LowX || X[i] > UpX ||
388 Y[i] < LowY || Y[i] > UpY ) ) {
389 aImage->Pixel(X[i],Y[i]).Value().
390 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
391 DoV[i] = 1 ;
392 }
393
394 // (1,1)
395 i++ ; X[i] = NX+1 ; Y[i] = NY+1 ;
396
397 if ( !( X[i] < LowX || X[i] > UpX ||
398 Y[i] < LowY || Y[i] > UpY ) ) {
399 aImage->Pixel(X[i],Y[i]).Value().
400 Values( R[i],G[i],B[i],Quantity_TOC_RGB);
401 DoV[i] = 1 ;
402 }
403
404 Standard_Boolean First, DIsComputed ;
405 Standard_Integer Ref ;
406
407 SR = SG = SB = SD = 0. ;
408 DIsComputed = 0 ;
409
410 // Red Componant
411
412 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
413 if ( DoV[i] ) {
414 if ( First ) { Ref = i ; First = 0 ; }
415 else if ( R[i] != R[Ref] ) { SamePixels = 0 ; break ;}
416 }
417 }
418
419 if ( Ref == -1 ) {
420 return Standard_False ;
421 }
422 else if ( SamePixels ) {
423 SR = R[Ref] ;
424 }
425 else {
426 for ( i = 0 ; i < 4 ; i++ ) {
427 if ( DoV[i] ) {
428 D[i] = Sqrt( (X[i]-FX)*(X[i]-FX) + (Y[i]-FY)*(Y[i]-FY) );
429 SD += D[i] ;
430 SR += D[i] * R[i] ;
431 }
432 }
433
434 DIsComputed = 1 ;
435
436 if ( SD != 0. ) SR /= SD ;
437 else {
438 return Standard_False ;
439 }
440 }
441
442 // Green Componant
443
444 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
445 if ( DoV[i] ) {
446 if ( First ) { Ref = i ; First = 0 ; }
447 else if ( G[i] != G[Ref] ) { SamePixels = 0 ; break ;}
448 }
449 }
450
451 if ( Ref == -1 ) {
452 return Standard_False ;
453 }
454 else if ( SamePixels ) {
455 SG = G[Ref] ;
456 }
457 else {
458 for ( i = 0 ; i < 4 ; i++ ) {
459 if ( DoV[i] ) {
460 if ( !DIsComputed ) {
461 D[i] = Sqrt( (X[i]-FX)*(X[i]-FX) + (Y[i]-FY)*(Y[i]-FY) );
462 SD += D[i] ;
463 }
464 SG += D[i] * G[i] ;
465 }
466 }
467
468 DIsComputed = 1 ;
469
470 if ( SD != 0. ) SG /= SD ;
471 else {
472 return Standard_False ;
473 }
474 }
475
476 // Blue Componant
477
478 for ( i = 0 , First = 1, Ref = -1, SamePixels = 1 ; i < 4 ; i++ ) {
479 if ( DoV[i] ) {
480 if ( First ) { Ref = i ; First = 0 ; }
481 else if ( B[i] != B[Ref] ) { SamePixels = 0 ; break ;}
482 }
483 }
484
485 if ( Ref == -1 ) {
486 return Standard_False ;
487 }
488 else if ( SamePixels ) {
489 SB = B[Ref] ;
490 }
491 else {
492 for ( i = 0 ; i < 4 ; i++ ) {
493 if ( DoV[i] ) {
494 if ( !DIsComputed ) {
495 D[i] = Sqrt( (X[i]-FX)*(X[i]-FX) + (Y[i]-FY)*(Y[i]-FY) );
496 SD += D[i] ;
497 }
498 SB += D[i] * B[i] ;
499 }
500 }
501
502 DIsComputed = 1 ;
503
504 if ( SD != 0. ) SB /= SD ;
505 else {
506 return Standard_False ;
507 }
508 }
509
510
511 // Result
512 aPixel.SetValue( Quantity_Color( SR, SG, SB, Quantity_TOC_RGB ) ) ;
513
514 return Standard_True ;
515 }
516}
517
518Standard_Boolean Image_BalancedPixelInterpolation::Interpolate(
519 const Handle(Image_DIndexedImage)& aImage,
520 const Standard_Real FX, const Standard_Real FY,
521 const Standard_Integer LowX,
522 const Standard_Integer LowY,
523 const Standard_Integer UpX,
524 const Standard_Integer UpY,
525 Aspect_IndexPixel& aPixel ) const
526
527{ Standard_Integer NX = Standard_Integer(FX) ;
528 Standard_Integer NY = Standard_Integer(FY) ;
529 Standard_Integer X[4],Y[4] ;
530 Standard_Integer V[4], i ;
531 Standard_Boolean DoV[4], SamePixels = 1 ;
532 Standard_Real D, SD, SP ;
533
534 if ( NX < ( LowX-1 ) || NX > UpX ||
535 NY < ( LowY-1 ) || NY > UpY ) {
536 return Standard_False ;
537 }
538 else {
539
540 if ( FX < 0. ) NX-- ;
541 if ( FY < 0. ) NY-- ;
542
543 SP = SD = 0. ;
544
545 for ( i = 0 ; i < 4 ; i++ ) DoV[i] = 0 ;
546
547 i = 0;
548
549 // (0,0)
550 i = 0; X[i] = NX ; Y[i] = NY ;
551
552 if ( !( X[i] < LowX || X[i] > UpX ||
553 Y[i] < LowY || Y[i] > UpY ) ) {
554 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
555 }
556
557 // (1,0)
558 i++ ; X[i] = NX+1 ; Y[i] = NY ;
559
560 if ( !( X[i] < LowX || X[i] > UpX ||
561 Y[i] < LowY || Y[i] > UpY ) ) {
562 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
563 }
564
565 // (0,1)
566 i++ ; X[i] = NX ; Y[i] = NY+1 ;
567
568 if ( !( X[i] < LowX || X[i] > UpX ||
569 Y[i] < LowY || Y[i] > UpY ) ) {
570 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
571 }
572
573 // (1,1)
574 i++ ; X[i] = NX+1 ; Y[i] = NY+1 ;
575
576 if ( !( X[i] < LowX || X[i] > UpX ||
577 Y[i] < LowY || Y[i] > UpY ) ) {
578 V[i] = aImage->Pixel( X[i],Y[i] ).Value() ; DoV[i] = 1 ;
579 }
580
581 Standard_Boolean First ;
582 Standard_Integer Ref ;
583
584 for ( i = 0 , First = 1, Ref = -1 ; i < 4 ; i++ ) {
585 if ( DoV[i] ) {
586 if ( First ) { Ref = i ; First = 0 ; }
587 else if ( V[i] != V[Ref] ) { SamePixels = 0 ; break ;}
588 }
589 }
590
591 if ( Ref == -1 ) {
592 return Standard_False ;
593 }
594 else if ( SamePixels ) {
595 aPixel.SetValue( V[Ref] ) ;
596 return Standard_True ;
597 }
598 else {
599 for ( i = 0 ; i < 4 ; i++ ) {
600 if ( DoV[i] ) {
601 D = Sqrt( (X[i]-FX)*(X[i]-FX) + (Y[i]-FY)*(Y[i]-FY) );
602
603 SP += D * V[i] ; SD += D ;
604 }
605 }
606
607 // Result
608
609 if ( SD != 0. ) {
610 SP /= SD ;
611
612 aPixel.SetValue( Standard_Integer(SP+0.5) ) ;
613
614 return Standard_True ;
615 }
616 else {
617 return Standard_False ;
618 }
619 }
620 }
621}
622#endif