1/*M///////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
5//  By downloading, copying, installing or using the software you agree to this license.
6//  If you do not agree to this license, do not download, install,
7//  copy or use the software.
8//
9//
10//                        Intel License Agreement
11//                For Open Source Computer Vision Library
12//
13// Copyright (C) 2000, Intel Corporation, all rights reserved.
14// Third party copyrights are property of their respective owners.
15//
16// Redistribution and use in source and binary forms, with or without modification,
17// are permitted provided that the following conditions are met:
18//
19//   * Redistribution's of source code must retain the above copyright notice,
20//     this list of conditions and the following disclaimer.
21//
22//   * Redistribution's in binary form must reproduce the above copyright notice,
23//     this list of conditions and the following disclaimer in the documentation
24//     and/or other materials provided with the distribution.
25//
26//   * The name of Intel Corporation may not be used to endorse or promote products
27//     derived from this software without specific prior written permission.
28//
29// This software is provided by the copyright holders and contributors "as is" and
30// any express or implied warranties, including, but not limited to, the implied
31// warranties of merchantability and fitness for a particular purpose are disclaimed.
32// In no event shall the Intel Corporation or contributors be liable for any direct,
33// indirect, incidental, special, exemplary, or consequential damages
34// (including, but not limited to, procurement of substitute goods or services;
35// loss of use, data, or profits; or business interruption) however caused
36// and on any theory of liability, whether in contract, strict liability,
37// or tort (including negligence or otherwise) arising in any way out of
38// the use of this software, even if advised of the possibility of such damage.
39//
40//M*/
41#include "_cvaux.h"
42#include <assert.h>
43
44
45static CvStatus
46icvMorphEpilines8uC3( uchar * first_pix,        /* raster epiline from image 1      */
47                      uchar * second_pix,       /* raster epiline from image 2      */
48                      uchar * dst_pix,  /* raster epiline from dest image   */
49                      /* (it's an output parameter)       */
50                      float alpha,      /* relative position of camera      */
51                      int *first,       /* first sequence of runs           */
52                      int first_runs,   /* it's length                      */
53                      int *second,      /* second sequence of runs          */
54                      int second_runs, int *first_corr, /* corr data for the 1st seq        */
55                      int *second_corr, /* corr data for the 2nd seq        */
56                      int dst_len )
57{
58
59    float alpha1;               /* alpha - 1.0 */
60    int s, s1;                  /* integer variant of alpha and alpha1 ( 0 <= s,s1 <= 256 ) */
61    int curr;                   /* current index in run's array */
62
63    float begLine;              /* begin of current run */
64    float endLine;              /* end   of current run */
65
66    float begCorr;              /* begin of correspondence destination of run */
67    float endCorr;              /* end   of correspondence destination of run */
68
69    int begDestLine;            /* begin of current destanation of run */
70    int endDestLine;            /* end   of current destanation of run */
71    int begLineIndex;
72    int endLineIndex;
73    int indexImg1;
74    float step = 0;
75    int n;
76
77    memset( dst_pix, 0, dst_len );
78    alpha1 = (float) (1.0 - alpha);
79
80    s = (int) (alpha * 256);
81    s1 = 256 - s;
82
83    /* --------------Create first line------------- */
84
85    begLineIndex = first[0];
86    begLine = (float) begLineIndex;
87
88    curr = 0;
89
90    for( n = 0; n < first_runs; n++ )
91    {                           /* for each run */
92
93        begCorr = (float) first_corr[curr];
94        curr++;
95        endCorr = (float) first_corr[curr];
96        curr++;
97        endLineIndex = first[curr];
98        endLine = (float) endLineIndex;
99
100        begDestLine = (int) (alpha * begLine + alpha1 * begCorr);
101        endDestLine = (int) (alpha * endLine + alpha1 * endCorr);
102
103        indexImg1 = begDestLine * 3;
104
105        step = 0;
106        if( endDestLine != begDestLine )
107            step = (endLine - begLine) / ((float) (endDestLine - begDestLine));
108
109        if( begCorr != endCorr )
110        {
111
112            for( ; begDestLine < endDestLine; begDestLine++ )
113            {
114                /* for each pixel */
115
116                begLineIndex = (int) begLine;
117                begLineIndex *= 3;
118
119                /* Blend R */
120                dst_pix[indexImg1] = (uchar) (((int) (first_pix[begLineIndex]) * s) >> 8);
121
122                indexImg1++;
123
124                /* Blend G */
125                dst_pix[indexImg1] = (uchar) (((int) (first_pix[begLineIndex + 1]) * s) >> 8);
126
127                indexImg1++;
128
129                /* Blend B */
130                dst_pix[indexImg1] = (uchar) (((int) (first_pix[begLineIndex + 2]) * s) >> 8);
131
132                indexImg1++;
133
134                begLine += step;
135
136            }                   /* for */
137        }
138        else
139        {
140
141            for( ; begDestLine < endDestLine; begDestLine++ )
142            {
143                /* for each pixel */
144
145                begLineIndex = (int) begLine;
146                begLineIndex *= 3;
147
148                /* Blend R */
149                dst_pix[indexImg1] = first_pix[begLineIndex];
150                indexImg1++;
151
152                /* Blend G */
153                dst_pix[indexImg1] = first_pix[begLineIndex + 1];
154                indexImg1++;
155
156                /* Blend B */
157                dst_pix[indexImg1] = first_pix[begLineIndex + 2];
158
159                indexImg1++;
160
161                begLine += step;
162
163            }                   /* for */
164        }                       /* if */
165
166        begLineIndex = endLineIndex;
167        begLine = endLine;
168
169
170    }                           /* for each runs in first line */
171
172    begLineIndex = second[0];
173    begLine = (float) begLineIndex;
174
175    curr = 0;
176
177    /* --------------Create second line------------- */
178    curr = 0;;
179    for( n = 0; n < second_runs; n++ )
180    {                           /* for each run */
181
182        begCorr = (float) second_corr[curr];
183        curr++;
184        endCorr = (float) second_corr[curr];
185        curr++;
186        endLineIndex = second[curr];
187        endLine = (float) endLineIndex;
188
189        begDestLine = (int) (alpha1 * begLine + alpha * begCorr);
190        endDestLine = (int) (alpha1 * endLine + alpha * endCorr);
191
192        indexImg1 = begDestLine * 3;
193
194        step = 0;
195        if (endDestLine != begDestLine)
196            step = (endLine - begLine) / ((float) (endDestLine - begDestLine));
197
198        if( begCorr != endCorr )
199        {
200
201            for( ; begDestLine < endDestLine; begDestLine++ )
202            {
203                /* for each pixel */
204
205                begLineIndex = (int) begLine;
206                begLineIndex *= 3;
207
208                /* Blend R */
209                dst_pix[indexImg1] =
210                    (uchar) (dst_pix[indexImg1] +
211                             (uchar) (((unsigned int) (second_pix[begLineIndex]) * s1) >> 8));
212
213                indexImg1++;
214
215                /* Blend G */
216                dst_pix[indexImg1] =
217                    (uchar) (dst_pix[indexImg1] +
218                             (uchar) (((unsigned int) (second_pix[begLineIndex + 1]) * s1) >>
219                                      8));
220
221                indexImg1++;
222
223                /* Blend B */
224                dst_pix[indexImg1] =
225                    (uchar) (dst_pix[indexImg1] +
226                             (uchar) (((unsigned int) (second_pix[begLineIndex + 2]) * s1) >>
227                                      8));
228
229                indexImg1++;
230
231                begLine += step;
232
233            }                   /* for */
234        }
235        else
236        {
237
238            for( ; begDestLine < endDestLine; begDestLine++ )
239            {
240                /* for each pixel */
241
242                begLineIndex = (int) begLine;
243                begLineIndex *= 3;
244
245                /* Blend R */
246                dst_pix[indexImg1] = (uchar) (dst_pix[indexImg1] + second_pix[begLineIndex]);
247                indexImg1++;
248
249                /* Blend G */
250                dst_pix[indexImg1] =
251                    (uchar) (dst_pix[indexImg1] + second_pix[begLineIndex + 1]);
252                indexImg1++;
253
254                /* Blend B */
255                dst_pix[indexImg1] =
256                    (uchar) (dst_pix[indexImg1] + second_pix[begLineIndex + 2]);
257                /*assert(indexImg1 < dst_len); */
258
259                indexImg1++;
260
261                begLine += step;
262
263            }                   /* for */
264        }                       /* if */
265
266        begLineIndex = endLineIndex;
267        begLine = endLine;
268
269    }                           /* for each runs in second line */
270
271    return CV_NO_ERR;
272
273}                               /* icvMorphEpilines8uC3 */
274
275
276/*======================================================================================*/
277
278static CvStatus
279icvMorphEpilines8uC3Multi( int lines,   /* number of lines                              */
280                           uchar * first_pix,   /* raster epilines from the first image         */
281                           int *first_num,      /* numbers of pixel in first line               */
282                           uchar * second_pix,  /* raster epilines from the second image        */
283                           int *second_num,     /* numbers of pixel in second line              */
284                           uchar * dst_pix,     /* raster epiline from the destination image    */
285                           /* (it's an output parameter)                   */
286                           int *dst_num,        /* numbers of pixel in output line              */
287                           float alpha, /* relative position of camera                  */
288                           int *first,  /* first sequence of runs                       */
289                           int *first_runs,     /* it's length                                  */
290                           int *second, /* second sequence of runs                      */
291                           int *second_runs, int *first_corr,   /* correspond information for the 1st seq       */
292                           int *second_corr )   /* correspond information for the 2nd seq       */
293{
294    CvStatus error;
295    int currLine;
296    int currFirstPix = 0;
297    //int currFirstNum = 0;
298    int currSecondPix = 0;
299    //int currSecondNum = 0;
300    int currDstPix = 0;
301    int currFirst = 0;
302    //int currFirstRuns = 0;
303    int currSecond = 0;
304    //int currSecondRuns = 0;
305    int currFirstCorr = 0;
306    int currSecondCorr = 0;
307
308    if( lines < 1 ||
309        first_pix == 0 ||
310        first_num == 0 ||
311        second_pix == 0 ||
312        second_num == 0 ||
313        dst_pix == 0 ||
314        dst_num == 0 ||
315        alpha < 0 ||
316        alpha > 1 ||
317        first == 0 ||
318        first_runs == 0 ||
319        second == 0 || second_runs == 0 || first_corr == 0 || second_corr == 0 )
320        return CV_BADFACTOR_ERR;
321
322    for( currLine = 0; currLine < lines; currLine++ )
323    {
324
325        error = icvMorphEpilines8uC3( &(first_pix[currFirstPix]),
326                                      &(second_pix[currSecondPix]),
327                                      &(dst_pix[currDstPix]),
328                                      alpha,
329                                      &(first[currFirst]),
330                                      first_runs[currLine],
331                                      &(second[currSecond]),
332                                      second_runs[currLine],
333                                      &(first_corr[currFirstCorr]),
334                                      &(second_corr[currSecondCorr]), dst_num[currLine] * 3 );
335
336
337        if( error != CV_NO_ERR )
338            return CV_NO_ERR;
339
340        currFirstPix += first_num[currLine] * 3;
341        currSecondPix += second_num[currLine] * 3;
342        currDstPix += dst_num[currLine] * 3;
343        currFirst += (first_runs[currLine] * 2) + 1;
344        currSecond += (second_runs[currLine] * 2) + 1;
345        currFirstCorr += first_runs[currLine] * 2;
346        currSecondCorr += second_runs[currLine] * 2;
347
348    }                           /* for */
349
350    return CV_NO_ERR;
351
352}                               /* icvMorphEpilines8uC3Multi */
353
354
355
356
357/*======================================================================================*/
358
359CV_IMPL void
360cvMorphEpilinesMulti( int lines,        /* number of lines             */
361                      uchar * first_pix,        /* raster epilines from the first image      */
362                      int *first_num,   /* numbers of pixel in first line            */
363                      uchar * second_pix,       /* raster epilines from the second image     */
364                      int *second_num,  /* numbers of pixel in second line           */
365                      uchar * dst_pix,  /* raster epiline from the destination image */
366                      /* (it's an output parameter)                */
367                      int *dst_num,     /* numbers of pixel in output line           */
368                      float alpha,      /* relative position of camera               */
369                      int *first,       /* first sequence of runs                    */
370                      int *first_runs,  /* it's length                               */
371                      int *second,      /* second sequence of runs                   */
372                      int *second_runs, int *first_corr,        /* correspond information for the 1st seq    */
373                      int *second_corr  /* correspond information for the 2nd seq     */
374     )
375{
376    CV_FUNCNAME( "cvMorphEpilinesMulti" );
377    __BEGIN__;
378
379    IPPI_CALL( icvMorphEpilines8uC3Multi( lines,        /* number of lines                           */
380                                          first_pix,    /* raster epilines from the first image      */
381                                          first_num,    /* numbers of pixel in first line            */
382                                          second_pix,   /* raster epilines from the second image     */
383                                          second_num,   /* numbers of pixel in second line           */
384                                          dst_pix,      /* raster epiline from the destination image */
385                                          /* (it's an output parameter)                   */
386                                          dst_num,      /* numbers of pixel in output line           */
387                                          alpha,        /* relative position of camera               */
388                                          first,        /* first sequence of runs                    */
389                                          first_runs,   /* it's length                               */
390                                          second,       /* second sequence of runs                   */
391                                          second_runs, first_corr,      /* correspond information for the 1st seq    */
392                                          second_corr   /* correspond information for the 2nd seq     */
393                ));
394    __END__;
395}
396
397