Move the sources to trunk
[opencv] / tests / cv / src / aimage.cpp
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
42 #include "cvtest.h"
43
44 #if 0
45
46 #define DEPTH_8U 0
47
48 /* Testing parameters */
49 static char test_desc[] = "Image Creation & access";
50 static char func_name[] = "cvCreateImage cvCreateImageHeader cvSetImageROI cvGetImageROI "
51                           "cvSetImageCOI cvCreateImageData cvReleaseImageData "
52                           "cvSetImageData cvCloneImage cvCopyImage cvInitImageHeader";
53
54 static int depths[] = { IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
55                         IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F, 0};
56 static int channels[] = {1, 2, 3, 4, 0};
57
58 static char* imageData = (char*)cvAlloc(10000);
59
60 const int align = 4;
61
62 static int foaImage( void )
63 {
64     CvSize size = cvSize(320, 200);
65     int i, j;
66     int Errors = 0;
67     //Creating new image with different channels & depths
68     for( i = 0; depths[i] != 0; i++ )  // cycle for depths
69         for(j = 0; channels[j] != 0; j++)  // cycle for channels
70         {
71             if( depths[i] == IPL_DEPTH_1U && channels[j] != 1 ) // skip for IPL_DEPTH_1U
72                 continue;                                       // all non 1 channels
73             IplImage* image = cvCreateImage( size, depths[i], channels[j] );
74             if( image->width != size.width || image->height != size.height )
75             {
76                 trsWrite( ATS_CON | ATS_LST,
77                           "cvCreateImage: Size mismatch: act %d x %d      exp %d x %d\n",
78                           image->width, image->height, size.width, size.height );
79                 Errors++;
80             }
81             if( size.width * (depths[i] & IPL_DEPTH_MASK) / 8 > image->widthStep ||
82                 (image->widthStep & 3) )
83             {
84                 trsWrite( ATS_CON | ATS_LST, "cvCreateImage: Wrong widthStep: act %d\n",
85                           image->widthStep );
86                 Errors++;
87             }
88
89             cvReleaseImage( &image );
90         }
91     trsWrite( ATS_CON, "cvCreateImage: ... done\n" );
92
93     //Creating new image header with different channels & depths
94     for( i = 0; depths[i] != 0; i++ )  // cycle for depths
95         for(j = 0; channels[j] != 0; j++)  // cycle for channels
96         {
97             if( depths[i] == IPL_DEPTH_1U && channels[j] != 1 ) // skip for IPL_DEPTH_1U
98                 continue;                                       // all non 1 channels
99             if( depths[i] == (int)IPL_DEPTH_8S )
100                 continue;
101             IplImage* image = cvCreateImageHeader( size, depths[i], channels[j] );
102             if( image->width != size.width || image->height != size.height )
103             {
104                 trsWrite( ATS_CON | ATS_LST,
105                           "cvCreateImageHeader: Size mismatch: act %d x %d      exp %d x %d\n",
106                           image->width, image->height, size.width, size.height );
107                 Errors++;
108             }
109             if( size.width * (depths[i] & IPL_DEPTH_MASK) / 8 > image->widthStep ||
110                 (image->widthStep & 3) )
111             {
112                 trsWrite( ATS_CON | ATS_LST, "cvCreateImageHeader: Wrong widthStep: act %d\n",
113                           image->widthStep );
114                 Errors++;
115             }
116             if( image->imageData )
117             {
118                 trsWrite( ATS_CON | ATS_LST, "cvCreateImageHeader: imageData created :(\n" );
119                 Errors++;
120             }
121
122             cvSetImageROI( image, cvRect(1, 1, size.width - 1, size.height - 1) );
123             if( image->roi->coi )
124             {
125                 trsWrite( ATS_CON | ATS_LST, "cvSetImageROI: coi non zero\n" );
126                 Errors++;
127             }
128
129             CvRect rect = cvGetImageROI( image );
130             if( rect.x != 1 || rect.y != 1 ||
131                 rect.width != size.width - 1 || rect.height != size.height - 1 )
132             {
133                 trsWrite( ATS_CON | ATS_LST,
134                           "cvGetImageROI: wrong rect: act %d x %d x %d x %d       "
135                           "exp %d x %d x %d x %d\n",
136                           rect.x, rect.y, rect.width, rect.height,
137                           1, 1, size.width - 1, size.height - 1 );
138                 Errors++;
139             }
140
141             cvSetImageCOI( image, 1 );
142             if( image->roi->coi != 1 )
143             {
144                 trsWrite( ATS_CON | ATS_LST, "cvSetImageCOI: soi non 1\n" );
145                 Errors++;
146             }
147             if( image->roi->xOffset != 1 || image->roi->yOffset != 1 ||
148                 image->roi->width != size.width  - 1 ||
149                 image->roi->height != size.height - 1)
150             {
151                 trsWrite( ATS_CON | ATS_LST,
152                           "cvCreateImageHeader: Size mismatch: act %d x %d x %d x %d"
153                           "exp %d x %d x %d x %d\n",
154                           image->roi->xOffset, image->roi->yOffset,
155                           image->roi->width, image->roi->height,
156                           1, 1, size.width - 1, size.height - 1 );
157                 Errors++;
158             }
159
160             cvCreateImageData( image );
161             if( !image->imageData )
162             {
163                 trsWrite( ATS_CON | ATS_LST,
164                           "cvCreateImageData: Wow :)... where is imageData ?....\n" );
165                 Errors++;
166             }
167
168             cvReleaseImageData( image );
169             if( image->imageData )
170             {
171                 trsWrite( ATS_CON | ATS_LST, "cvReleaseImageData: imageData non zero :(\n" );
172                 Errors++;
173             }
174
175             cvSetImageData( image, imageData, size.width * channels[j] * 8 ); // magic width step :)
176             if( image->imageData != imageData )
177             {
178                 trsWrite( ATS_CON | ATS_LST,
179                           "cvSetImageData: wrong pointer to imageData: act %x,  exp %x\n",
180                           image->imageData, imageData );
181                 Errors++;
182             }
183             if( image->widthStep != size.width * channels[j] * 8 )
184             {
185                 trsWrite( ATS_CON | ATS_LST, "cvSetImageData: wrong imageStep: act %d,   exp %d\n",
186                           image->widthStep, size.width * channels[j] * 8 );
187                 Errors++;
188             }
189
190             cvReleaseImageHeader( &image );
191         }
192     trsWrite( ATS_CON, "cvCreateImageHeader: ... done\n" );
193     trsWrite( ATS_CON, "cvSetImageROI: ... done\n" );
194     trsWrite( ATS_CON, "cvGetImageROI: ... done\n" );
195     trsWrite( ATS_CON, "cvSetImageCOI: ... done\n" );
196     trsWrite( ATS_CON, "cvCreateImageData: ... done\n" );
197     trsWrite( ATS_CON, "cvReleaseImageData: ... done\n" );
198     trsWrite( ATS_CON, "cvSetImageData: ... done\n" );
199
200     for( i = 0; depths[i] != 0; i++ )  // cycle for depths
201         for(j = 0; channels[j] != 0; j++)  // cycle for channels
202         {
203             if( depths[i] == IPL_DEPTH_1U && channels[j] != 1 ) // skip for IPL_DEPTH_1U
204                 continue;                                       // all non 1 channels
205             if( depths[i] == (int)IPL_DEPTH_8S )
206                 continue;
207             IplImage* src = cvCreateImage( size, depths[i], channels[j] );
208             //IplImage* dst = cvCreateImage( size, depths[i], channels[j] );
209             IplImage* dst = 0;
210             IplImage* clone = 0;
211
212             cvSetImageROI( src, cvRect(1, 1, size.width - 1, size.height - 1) );
213
214             for( int k = 0; k < src->widthStep * src->height; k++ )
215                 src->imageData[k] = (char)k;
216
217             //cvCopy/*Image*/( src, dst );
218             dst = cvCloneImage( src );
219             clone = dst;
220
221             if( clone->width != dst->width || clone->height != dst->height )
222             {
223                 trsWrite( ATS_CON | ATS_LST,
224                           "cvCopyImage/cvCloneImage: wrong destination size:"
225                           "%d x %d  <>  %d %d\n",
226                           clone->width, clone->height, dst->width, dst->height );
227                 Errors++;
228             }
229             if( clone->widthStep != src->widthStep )
230             {
231                 trsWrite( ATS_CON | ATS_LST,
232                           "cvCloneImage: wrong width step: act %d   exp %d\n",
233                           clone->widthStep, src->widthStep );
234                 Errors++;
235             }
236             if( !clone->roi )
237             {
238                 trsWrite( ATS_CON | ATS_LST, "cvCloneImage: roi was lost\n" );
239                 Errors++;
240             }
241             else
242             {
243                 if( clone->roi->xOffset != 1 || clone->roi->yOffset != 1 ||
244                     clone->roi->width != size.width  - 1 ||
245                     clone->roi->height != size.height - 1 )
246                 {
247                     trsWrite( ATS_CON | ATS_LST,
248                               "cvCloneImage: Size mismatch: act %d x %d x %d x %d"
249                               "exp %d x %d x %d x %d\n",
250                               clone->roi->xOffset, clone->roi->yOffset,
251                               clone->roi->width, clone->roi->height,
252                               1, 1, size.width - 1, size.height - 1 );
253                     Errors++;
254                 }
255
256             }
257             if( depths[i] == IPL_DEPTH_32F )
258             {
259                 src->depth = IPL_DEPTH_32S;
260                 dst->depth = IPL_DEPTH_32S;
261             }
262             else if( depths[i] == IPL_DEPTH_64F )
263             {
264                 src->depth = IPL_DEPTH_32S;
265                 dst->depth = IPL_DEPTH_32S;
266
267                 src->width *= 2;
268                 dst->width *= 2;
269             }
270
271             if( cvNorm( src, dst, CV_L1 ) )
272             {
273                 trsWrite( ATS_CON | ATS_LST, "cvCopyImage: wrong destination image\n" );
274                 Errors++;
275             }
276             /*if( cvNorm( src, clone, CV_L1 ) )
277             {
278                 trsWrite( ATS_CON | ATS_LST, "cvCloneImage: wrong destination image\n" );
279                 Errors++;
280             }*/
281
282             cvReleaseImage( &src );
283             cvReleaseImage( &clone );
284         }
285     trsWrite( ATS_CON, "cvCloneImage: ... done\n" );
286     //trsWrite( ATS_CON, "cvCopyImage: ... done\n" );
287
288     //Init new image header with different channels & depths
289     for( i = 0; depths[i] != 0; i++ )  // cycle for depths
290         for(j = 0; channels[j] != 0; j++)  // cycle for channels
291         {
292             if( depths[i] == IPL_DEPTH_1U && channels[j] != 1 ) // skip for IPL_DEPTH_1U
293                 continue;                                       // all non 1 channels
294             IplImage image;
295             cvInitImageHeader( &image, size, depths[i], channels[j], IPL_ORIGIN_TL, align );
296             if( image.width != size.width || image.height != size.height )
297             {
298                 trsWrite( ATS_CON | ATS_LST,
299                           "cvInitImageHeader: Size mismatch: act %d x %d      exp %d x %d\n",
300                           image.width, image.height, size.width, size.height );
301                 Errors++;
302             }
303             if( ((size.width * channels[j] * (depths[i] & IPL_DEPTH_MASK) / 8 + align - 1) &
304                 ~(align - 1)) != image.widthStep )
305             {
306                 trsWrite( ATS_CON | ATS_LST,
307                           "cvCreateImageHeader: Wrong widthStep: act %d   exp: %d\n",
308                           image.widthStep,
309                           (size.width * (depths[i] & IPL_DEPTH_MASK) / 8 + align - 1) &
310                           ~(align - 1) );
311                 Errors++;
312             }
313             if( image.imageData )
314             {
315                 trsWrite( ATS_CON | ATS_LST, "cvCreateImageHeader: imageData created :(\n" );
316                 Errors++;
317             }
318         }
319     trsWrite( ATS_CON, "cvInitImageHeader: ... done\n" );
320
321     if( !Errors )
322         return trsResult( TRS_OK, "Ok" );
323     else
324         return trsResult( TRS_FAIL, "%d errors" );
325 }
326
327
328
329 void InitAImage()
330 {
331     /* Register test function */
332     trsReg( func_name, test_desc, atsAlgoClass, foaImage );
333 } /* InitACanny */
334
335 #endif