1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
41 #include "precomp.hpp"
45 icvFetchLine8uC3R( uchar * src, int src_step,
46 uchar * dst, int *dst_num, CvSize src_size, CvPoint start, CvPoint end )
49 int dx = end.x - start.x, dy = end.y - start.y;
52 if( !src || !dst || (src_size.width | src_size.height) < 0 ||
53 src_step < src_size.width * 3 ||
54 (unsigned) start.x >= (unsigned) src_size.width ||
55 (unsigned) start.y >= (unsigned) src_size.height ||
56 (unsigned) end.x >= (unsigned) src_size.width ||
57 (unsigned) end.y >= (unsigned) src_size.height )
58 return CV_BADFACTOR_ERR;
68 src += start.y * src_step + start.x * 3;
72 src_step = (src_step ^ i) - i;
79 return CV_BADSIZE_ERR;
85 for( i = dx; i >= 0; i -= 2, dst += 3 )
87 int mask = (err -= dy) < 0 ? -1 : 0;
94 src += (src_step & mask) + 3;
102 return CV_BADSIZE_ERR;
108 for( i = dy; i >= 0; i -= 2, dst += 3 )
110 int mask = (err -= dx) < 0 ? -1 : 0;
117 src += src_step + (mask & 3);
124 icvDrawLine8uC3R( uchar * src, int src_num,
125 uchar * dst, int dst_step, CvSize dst_size, CvPoint start, CvPoint end )
128 int dx = end.x - start.x, dy = end.y - start.y;
131 if( !src || !dst || (dst_size.width | dst_size.height) < 0 ||
132 dst_step < dst_size.width * 3 ||
133 (unsigned) start.x >= (unsigned) dst_size.width ||
134 (unsigned) start.y >= (unsigned) dst_size.height ||
135 (unsigned) end.x >= (unsigned) dst_size.width ||
136 (unsigned) end.y >= (unsigned) dst_size.height )
137 return CV_BADFACTOR_ERR;
147 dst += start.y * dst_step + start.x * 3;
151 dst_step = (dst_step ^ i) - i;
155 if( (unsigned) (src_num - 1) < (unsigned) dx )
156 return CV_BADSIZE_ERR;
160 for( i = dx; i >= 0; i -= 2, src += 3 )
162 int mask = (err -= dy) < 0 ? -1 : 0;
168 dst += (dst_step & mask) + 3;
173 if( (unsigned) (src_num - 1) < (unsigned) dy )
174 return CV_BADSIZE_ERR;
178 for( i = dy; i >= 0; i -= 2, src += 3 )
180 int mask = (err -= dx) < 0 ? -1 : 0;
186 dst += dst_step + (mask & 3);
193 /*======================================================================================*/
196 icvPreWarpImage8uC3R( int numLines, /* number of scanlines */
197 uchar * src, /* source image */
198 int src_step, /* line step */
199 uchar * dst, /* dest buffers */
200 int *dst_nums, /* lens of buffer */
201 CvSize src_size, /* image size in pixels */
202 int *scanlines ) /* scanlines array */
214 cvInitMatHeader( &mat, src_size.height, src_size.width, CV_8UC3, src, src_step );
216 for( k = 0; k < numLines; k++ )
218 start.x = scanlines[curr++];
219 start.y = scanlines[curr++];
221 end.x = scanlines[curr++];
222 end.y = scanlines[curr++];
226 CvLineIterator iterator;
227 assert( cvInitLineIterator( &mat, start, end, &iterator, 8 ) == dst_nums[k] );
230 cvSampleLine( &mat, start, end, dst + curr_dst, 8 );
231 curr_dst += dst_nums[k] * 3;
239 /*======================================================================================*/
242 icvPostWarpImage8uC3R( int numLines, /* number of scanlines */
243 uchar * src, /* source buffers */
244 int *src_nums, /* lens of buffers */
245 uchar * dst, /* dest image */
246 int dst_step, /* dest image step */
247 CvSize dst_size, /* dest image size */
248 int *scanlines ) /* scanline */
257 CvLineIterator iterator;
262 cvInitMatHeader( &mat, dst_size.height, dst_size.width, CV_8UC3, dst, dst_step );
264 for( k = 0; k < numLines; k++ )
266 start.x = scanlines[curr++];
267 start.y = scanlines[curr++];
269 end.x = scanlines[curr++];
270 end.y = scanlines[curr++];
272 src_num = src_nums[k];
274 if( cvInitLineIterator( &mat, start, end, &iterator, 8 ) != src_num )
277 return CV_NOTDEFINED_ERR;
280 for( i = 0; i < src_num; i++ )
282 memcpy( iterator.ptr, src + curr_src, 3 );
283 CV_NEXT_LINE_POINT( iterator );
288 err = icvDrawLine8uC3R( src + curr_src, /* sourse buffer */
289 src_num, /* len of buffer */
290 dst, /* dest image */
291 dst_step, /* dest image step */
292 dst_size, /* dest image size */
293 start, /* start point */
294 end ); /* end point */
295 curr_src += src_num * 3;
304 /*======================================================================================*/
306 /*F///////////////////////////////////////////////////////////////////////////////////////
307 // Name: icvDeleteMoire8uC3R
309 // Function deletes moire - replaces black uncovered pixels with their neighboors.
313 // img_step - distance between lines in bytes
314 // img_size - width and height of the image in pixels
316 // CV_NO_ERR if all Ok or error code
320 icvDeleteMoire8u( uchar * img, int img_step, CvSize img_size, int cn )
323 uchar *src = img, *dst = img + img_step;
325 if( !img || img_size.width <= 0 || img_size.height <= 0 || img_step < img_size.width * 3 )
326 return CV_BADFACTOR_ERR;
328 img_size.width *= cn;
330 for( y = 1; y < img_size.height; y++, src = dst, dst += img_step )
335 for( x = 0; x < img_size.width; x++ )
342 for( x = 0; x < img_size.width; x += 3 )
344 if( dst[x] == 0 && dst[x + 1] == 0 && dst[x + 2] == 0 )
347 dst[x + 1] = src[x + 1];
348 dst[x + 2] = src[x + 2];
362 /*F///////////////////////////////////////////////////////////////////////////////////////
363 // Name: cvDeleteMoire
364 // Purpose: The functions delete moire on the image after ViewMorphing
366 // Parameters: img - image on which will delete moire
371 cvDeleteMoire( IplImage * img )
377 CV_FUNCNAME( "cvDeleteMoire" );
381 cvGetImageRawData( img, &img_data, &img_step, &img_size );
383 if( img->nChannels != 1 && img->nChannels != 3 )
384 CV_ERROR( CV_BadNumChannels, "Source image must have 3 channel." );
385 if( img->depth != IPL_DEPTH_8U )
386 CV_ERROR( CV_BadDepth, "Channel depth of source image must be 8." );
388 CV_CALL( icvDeleteMoire8u( img_data, img_step, img_size, img->nChannels ));
395 /*F///////////////////////////////////////////////////////////////////////////////////////
396 // Name: cvPreWarpImage
397 // Purpose: The functions warp image for next stage of ViewMorphing
399 // Parameters: img - initial image (in the beginning)
404 cvPreWarpImage( int numLines, /* number of scanlines */
405 IplImage * img, /* Source Image */
406 uchar * dst, /* dest buffers */
407 int *dst_nums, /* lens of buffer */
408 int *scanlines /* scanlines array */ )
414 CV_FUNCNAME( "cvPreWarpImage" );
418 cvGetImageRawData( img, &img_data, &img_step, &img_size );
420 if( img->nChannels != 3 )
421 CV_ERROR( CV_BadNumChannels, "Source image must have 3 channel." );
422 if( img->depth != IPL_DEPTH_8U )
423 CV_ERROR( CV_BadDepth, "Channel depth of image must be 8." );
425 CV_CALL( icvPreWarpImage8uC3R( numLines, /* number of scanlines */
426 img_data, /* source image */
427 img_step, /* line step */
428 dst, /* dest buffers */
429 dst_nums, /* lens of buffer */
430 img_size, /* image size in pixels */
431 scanlines /* scanlines array */ ));
438 /*F///////////////////////////////////////////////////////////////////////////////////////
439 // Name: cvPostWarpImage
440 // Purpose: The functions postwarp the image after morphing
442 // Parameters: img - initial image (in the beginning)
447 cvPostWarpImage( int numLines, /* number of scanlines */
448 uchar * src, /* source buffers */
449 int *src_nums, /* lens of buffers */
450 IplImage * img, /* dest image */
451 int *scanlines /* scanline */ )
457 CV_FUNCNAME( "cvPostWarpImage" );
461 cvGetImageRawData( img, &img_data, &img_step, &img_size );
463 if( img->nChannels != 3 )
464 CV_ERROR( CV_BadNumChannels, "Source image must have 3 channel." );
465 if( img->depth != IPL_DEPTH_8U )
466 CV_ERROR( CV_BadDepth, "Channel depth of image must be 8." );
468 CV_CALL( icvPostWarpImage8uC3R( numLines, /* number of scanlines */
469 src, /* source buffers */
470 src_nums, /* lens of buffers */
471 img_data, /* dest image */
472 img_step, /* dest image step */
473 img_size, /* dest image size */
474 scanlines /* scanline */ ));