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_PlanarPixelInterpolation.ixx> |
20 | |
21 | |
22 | Image_PlanarPixelInterpolation::Image_PlanarPixelInterpolation() {} |
23 | |
24 | Standard_Boolean Image_PlanarPixelInterpolation::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 | |
52 | |
53 | static Standard_Real DoInterpolation( const Standard_Integer NX[3], |
54 | const Standard_Integer NY[3], |
55 | const Standard_Real NZ[3], |
56 | const Standard_Real FX, |
57 | const Standard_Real FY ) |
58 | |
59 | { Standard_Real VX[3], VY[3], VZ[3] ; |
60 | Standard_Real Result ; |
61 | |
62 | if ( NZ[0] == NZ[1] && NZ[0] == NZ[2] ) { |
63 | Result = NZ[0] ; |
64 | } |
65 | else { |
66 | |
67 | VX[1] = NX[1] - NX[0] ; |
68 | VY[1] = NY[1] - NY[0] ; |
69 | VZ[1] = NZ[1] - NZ[0] ; |
70 | |
71 | VX[2] = NX[2] - NX[0] ; |
72 | VY[2] = NY[2] - NY[0] ; |
73 | VZ[2] = NZ[2] - NZ[0] ; |
74 | |
75 | if ( VZ[1] == 0. && VZ[2] == 0. ) { |
76 | Result = NZ[0] ; |
77 | } |
78 | else { |
79 | VX[0] = VY[1]*VZ[2] - VY[2]*VZ[1] ; |
80 | VY[0] = VZ[1]*VX[2] - VZ[2]*VX[1] ; |
81 | VZ[0] = VX[1]*VY[2] - VX[2]*VY[1] ; |
82 | |
83 | if ( VZ[0] != 0. ) { |
84 | Result = NZ[0] - |
85 | ( (FX-NX[0])*VX[0] + (FY-NY[0])*VY[0] ) / VZ[0]; |
86 | } |
87 | else { |
88 | Result = NZ[0] ; |
89 | } |
90 | } |
91 | } |
92 | |
93 | return Result ; |
94 | |
95 | } |
96 | |
97 | Standard_Boolean Image_PlanarPixelInterpolation::Interpolate( |
98 | const Handle(Image_DColorImage)& aImage, |
99 | const Standard_Real FX, const Standard_Real FY, |
100 | const Standard_Integer LowX, |
101 | const Standard_Integer LowY, |
102 | const Standard_Integer UpX, |
103 | const Standard_Integer UpY, |
104 | Aspect_ColorPixel& aPixel ) const |
105 | |
106 | { Standard_Integer NX[3], NY[3] ; |
107 | Standard_Real NZ[3] ; |
108 | Standard_Real R,G,B ; |
109 | static Quantity_Color Col ; |
110 | |
111 | if ( FX < 0. ) NX[0] = Standard_Integer(FX-0.5) ; |
112 | else NX[0] = Standard_Integer(FX+0.5) ; |
113 | |
114 | if ( FY < 0. ) NY[0] = Standard_Integer(FY-0.5) ; |
115 | else NY[0] = Standard_Integer(FY+0.5) ; |
116 | |
117 | if ( NX[0] < LowX || NX[0] > UpX || |
118 | NY[0] < LowY || NY[0] > UpY ) { |
119 | return Standard_False ; |
120 | } |
121 | else if ( ( FX-NX[0] ) == 0. && ( FY-NY[0] ) == 0. ) { |
122 | aImage->Pixel( NX[0], NY[0], aPixel ); |
123 | return Standard_True ; |
124 | } |
125 | else { |
126 | |
127 | if ( ( FX-NX[0] ) >= 0. ) { NX[1] = NX[0]+1 ; NY[1] = NY[0] ; } |
128 | else { NX[1] = NX[0]-1 ; NY[1] = NY[0] ; } |
129 | if ( ( FY-NY[0] ) >= 0. ) { NX[2] = NX[0] ; NY[2] = NY[0]+1 ; } |
130 | else { NX[2] = NX[0] ; NY[2] = NY[0]-1 ; } |
131 | |
132 | if ( NX[1] < LowX || NX[1] > UpX || NY[1] < LowY || NY[1] > UpY || |
133 | NX[2] < LowX || NX[2] > UpX || NY[2] < LowY || NY[2] > UpY ) { |
134 | aImage->Pixel( NX[0], NY[0], aPixel ); |
135 | } |
136 | else { |
137 | NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Red() ; |
138 | NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Red() ; |
139 | NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Red() ; |
140 | |
141 | R = DoInterpolation( NX,NY,NZ, FX,FY ) ; |
142 | |
143 | NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Green() ; |
144 | NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Green() ; |
145 | NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Green() ; |
146 | |
147 | G = DoInterpolation( NX,NY,NZ, FX,FY ) ; |
148 | |
149 | NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value().Blue() ; |
150 | NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value().Blue() ; |
151 | NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value().Blue() ; |
152 | |
153 | B = DoInterpolation( NX,NY,NZ, FX,FY ) ; |
154 | |
155 | Col.SetValues( R, G, B, Quantity_TOC_RGB ) ; |
156 | |
157 | aPixel.SetValue( Col ) ; |
158 | } |
159 | |
160 | return Standard_True ; |
161 | } |
162 | } |
163 | |
164 | Standard_Boolean Image_PlanarPixelInterpolation::Interpolate( |
165 | const Handle(Image_DIndexedImage)& aImage, |
166 | const Standard_Real FX, const Standard_Real FY, |
167 | const Standard_Integer LowX, |
168 | const Standard_Integer LowY, |
169 | const Standard_Integer UpX, |
170 | const Standard_Integer UpY, |
171 | Aspect_IndexPixel& aPixel ) const |
172 | |
173 | { Standard_Integer NX[3], NY[3] ; |
174 | Standard_Real NZ[3] ; |
175 | |
176 | |
177 | if ( FX < 0. ) NX[0] = Standard_Integer(FX-0.5) ; |
178 | else NX[0] = Standard_Integer(FX+0.5) ; |
179 | |
180 | if ( FY < 0. ) NY[0] = Standard_Integer(FY-0.5) ; |
181 | else NY[0] = Standard_Integer(FY+0.5) ; |
182 | |
183 | if ( NX[0] < LowX || NX[0] > UpX || |
184 | NY[0] < LowY || NY[0] > UpY ) { |
185 | return Standard_False ; |
186 | } |
187 | else if ( ( FX-NX[0] ) == 0. && ( FY-NY[0] ) == 0. ) { |
188 | aImage->Pixel( NX[0], NY[0], aPixel ); |
189 | return Standard_True ; |
190 | } |
191 | else { |
192 | |
193 | if ( ( FX-NX[0] ) >= 0. ) { NX[1] = NX[0]+1 ; NY[1] = NY[0] ; } |
194 | else { NX[1] = NX[0]-1 ; NY[1] = NY[0] ; } |
195 | if ( ( FY-NY[0] ) >= 0. ) { NX[2] = NX[0] ; NY[2] = NY[0]+1 ; } |
196 | else { NX[2] = NX[0] ; NY[2] = NY[0]-1 ; } |
197 | |
198 | if ( NX[1] < LowX || NX[1] > UpX || NY[1] < LowY || NY[1] > UpY || |
199 | NX[2] < LowX || NX[2] > UpX || NY[2] < LowY || NY[2] > UpY ) { |
200 | aImage->Pixel( NX[0], NY[0], aPixel ); |
201 | } |
202 | else { |
203 | NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value() ; |
204 | NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value() ; |
205 | NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value() ; |
206 | |
207 | aPixel.SetValue( |
208 | Standard_Integer( DoInterpolation( NX,NY,NZ, FX,FY ) ) |
209 | ) ; |
210 | |
211 | } |
212 | |
213 | return Standard_True ; |
214 | } |
215 | } |
216 | |
217 | //############################################################################## |
218 | |
219 | #ifdef OLD |
220 | { Standard_Integer NX[3], NY[3], NZ[3] ; |
221 | Standard_Integer Result ; |
222 | Standard_Real VX[3], VY[3], VZ[3], PVALUE ; |
223 | |
224 | NX[0] = Standard_Integer(FX+0.5) ; |
225 | NY[0] = Standard_Integer(FY+0.5) ; |
226 | |
227 | if ( NX[0] < LowX || NX[0] > UpX || |
228 | NY[0] < LowY || NY[0] > UpY ) { |
229 | return Standard_False ; |
230 | } |
231 | else if ( ( FX-NX[0] ) == 0. && ( FY-NY[0] ) == 0. ) { |
232 | aImage->Pixel( NX[0], NY[0], aPixel ); |
233 | return Standard_True ; |
234 | } |
235 | else { |
236 | |
237 | if ( ( FX-NX[0] ) >= 0. ) { NX[1] = NX[0]+1 ; NY[1] = NY[0] ; } |
238 | else { NX[1] = NX[0]-1 ; NY[1] = NY[0] ; } |
239 | if ( ( FY-NY[0] ) >= 0. ) { NX[2] = NX[0] ; NY[2] = NY[0]+1 ; } |
240 | else { NX[2] = NX[0] ; NY[2] = NY[0]-1 ; } |
241 | |
242 | if ( NX[1] < LowX || NX[1] > UpX || NY[1] < LowY || NY[1] > UpY || |
243 | NX[2] < LowX || NX[2] > UpX || NY[2] < LowY || NY[2] > UpY ) { |
244 | aImage->Pixel( NX[0], NY[0], aPixel ); |
245 | } |
246 | else { |
247 | NZ[0] = aImage->Pixel( NX[0],NY[0] ).Value() ; |
248 | NZ[1] = aImage->Pixel( NX[1],NY[1] ).Value() ; |
249 | NZ[2] = aImage->Pixel( NX[2],NY[2] ).Value() ; |
250 | |
251 | VX[1] = NX[1] - NX[0] ; |
252 | VY[1] = NY[1] - NY[0] ; |
253 | VZ[1] = NZ[1] - NZ[0] ; |
254 | |
255 | VX[2] = NX[2] - NX[0] ; |
256 | VY[2] = NY[2] - NY[0] ; |
257 | VZ[2] = NZ[2] - NZ[0] ; |
258 | |
259 | if ( VZ[1] == 0. && VZ[2] == 0. ) { |
260 | Result = NZ[0] ; |
261 | } |
262 | else { |
263 | VX[0] = VY[1]*VZ[2] - VY[2]*VZ[1] ; |
264 | VY[0] = VZ[1]*VX[2] - VZ[2]*VX[1] ; |
265 | VZ[0] = VX[1]*VY[2] - VX[2]*VY[1] ; |
266 | |
267 | if ( VZ[0] != 0. ) { |
268 | PVALUE = NZ[0] - |
269 | ( (FX-NX[0])*VX[0] + (FY-NY[0])*VY[0] ) / VZ[0]; |
270 | Result = Standard_Integer( PVALUE ) ; |
271 | } |
272 | else { |
273 | Result = NZ[0] ; |
274 | } |
275 | } |
276 | |
277 | aPixel.SetValue( Result ) ; |
278 | } |
279 | |
280 | return Standard_True ; |
281 | } |
282 | } |
283 | #endif |