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 "_cv.h"
42
43#if 0
44
45IPCVAPI(CvStatus, icvCalcContrastHist8uC1R, ( uchar** img, int step, CvSize size,
46                                              CvHistogram* hist, int dont_clear ))
47
48IPCVAPI(CvStatus, icvCalcContrastHistMask8uC1R, ( uchar** img, int step,
49                                                  uchar*  mask, int mask_step, CvSize size,
50                                                  CvHistogram* hist, int dont_clear ))
51
52/*F///////////////////////////////////////////////////////////////////////////////////////
53//    Name:       icvCalcContrastHist8uC1R
54//    Purpose:    Calculating the histogram of contrast from one-channel images
55//    Context:
56//    Parameters:
57//    Returns:
58//    Notes:      if dont_clear parameter is NULL then histogram clearing before
59//                calculating (all values sets to NULL)
60//F*/
61static CvStatus CV_STDCALL
62icvCalcContrastHist8uC1R( uchar** img, int step, CvSize size,
63                          CvHistogram* hist, int dont_clear )
64{
65    int i, j, t, x = 0, y = 0;
66    int dims;
67
68    if( !hist || !img )
69        return CV_NULLPTR_ERR;
70
71    dims = hist->c_dims;
72    if( dims != 1 )
73        return CV_BADSIZE_ERR;
74
75    if( hist->type != CV_HIST_ARRAY )
76        return CV_BADFLAG_ERR;
77
78    for( i = 0; i < dims; i++ )
79        if( !img[i] )
80            return CV_NULLPTR_ERR;
81
82    for( i = 0; i < hist->c_dims; i++ )
83    {
84        if( !hist->thresh[i] )
85            return CV_NULLPTR_ERR;
86        assert( hist->chdims[i] );
87    }
88
89    j = hist->dims[0] * hist->mdims[0];
90
91    int *n = (int *)cvAlloc( (size_t)hist->dims[0] * sizeof( int ));
92
93    if( hist->type == CV_HIST_ARRAY )
94    {
95        if( !dont_clear )
96            for( i = 0; i < j; i++ )
97            {
98                hist->array[i] = 0;
99                n[i] = 0;
100            }
101
102        switch (hist->c_dims)
103        {
104        case 1:
105            {
106                uchar *data0 = img[0];
107                int *array = (int *) hist->array;
108                int *chdims = hist->chdims[0];
109
110                for( i = 0; i < j; i++ )
111                    array[i] = cvRound( hist->array[i] );
112
113                for( y = 0; y < size.height; y++, data0 += step )
114                {
115                    for( x = 0; x <= size.width - 1; x += 2 )
116                    {
117                        int v1_r = MIN( data0[x], data0[x + 1] );
118                        int v2_r = MAX( data0[x], data0[x + 1] );
119
120//    calculate contrast for the right-left pair
121                        for( t = v1_r; t < v2_r; t++ )
122                        {
123                            int val0 = chdims[t + 128];
124
125                            array[val0] += MIN( t - v1_r, v2_r - t );
126                            n[val0]++;
127                        }
128
129                        if( y < size.height - 1 )
130                        {
131                            int v1_d = MIN( data0[x], data0[x + step] );
132                            int v2_d = MAX( data0[x], data0[x + step] );
133
134//    calculate contrast for the top-down pair
135                            for( t = v1_d; t < v2_d; t++ )
136                            {
137                                int val0 = chdims[t + 128];
138
139                                array[val0] += MIN( t - v1_d, v2_d - t );
140                                n[val0]++;
141                            }
142                        }
143                    }
144                }
145
146//  convert int to float
147                for( i = 0; i < j; i++ )
148                {
149                    if( n[i] != 0 )
150                        hist->array[i] = (float) array[i] / n[i];
151                    else
152                        hist->array[i] = 0;
153                }
154            }
155            break;
156        default:
157            return CV_BADSIZE_ERR;
158        }
159    }
160
161    cvFree( &n );
162    return CV_NO_ERR;
163}
164
165/*F///////////////////////////////////////////////////////////////////////////////////////
166//    Name:       icvCalcContrastHistMask8uC1R
167//    Purpose:    Calculating the mask histogram of contrast from one-channel images
168//    Context:
169//    Parameters:
170//    Returns:
171//    Notes:      if dont_clear parameter is NULL then histogram clearing before
172//                calculating (all values sets to NULL)
173//F*/
174static CvStatus CV_STDCALL
175icvCalcContrastHistMask8uC1R( uchar** img, int step, uchar* mask, int mask_step,
176                              CvSize size, CvHistogram * hist, int dont_clear )
177{
178    int i, j, t, x = 0, y = 0;
179    int dims;
180
181
182    if( !hist || !img || !mask )
183        return CV_NULLPTR_ERR;
184
185    dims = hist->c_dims;
186    if( dims != 1 )
187        return CV_BADSIZE_ERR;
188
189    if( hist->type != CV_HIST_ARRAY )
190        return CV_BADFLAG_ERR;
191
192    for( i = 0; i < dims; i++ )
193        if( !img[i] )
194            return CV_NULLPTR_ERR;
195
196    for( i = 0; i < hist->c_dims; i++ )
197    {
198        if( !hist->thresh[i] )
199            return CV_NULLPTR_ERR;
200        assert( hist->chdims[i] );
201    }
202
203    j = hist->dims[0] * hist->mdims[0];
204
205    int *n = (int *)cvAlloc( (size_t) hist->dims[0] * sizeof( int ));
206
207    if( hist->type == CV_HIST_ARRAY )
208    {
209        if( !dont_clear )
210            for( i = 0; i < j; i++ )
211            {
212                hist->array[i] = 0;
213                n[i] = 0;
214            }
215
216        switch (hist->c_dims)
217        {
218        case 1:
219            {
220                uchar *data0 = img[0];
221                uchar *maskp = mask;
222                int *array = (int *) hist->array;
223                int *chdims = hist->chdims[0];
224
225                for( i = 0; i < j; i++ )
226                    array[i] = cvRound( hist->array[i] );
227
228                for( y = 0; y < size.height; y++, data0 += step, maskp += mask_step )
229                {
230                    for( x = 0; x <= size.width - 2; x++ )
231                    {
232                        if( maskp[x] )
233                        {
234                            if( maskp[x + 1] )
235                            {
236                                int v1_r = MIN( data0[x], data0[x + 1] );
237                                int v2_r = MAX( data0[x], data0[x + 1] );
238
239
240                                //    calculate contrast for the right-left pair
241                                for( t = v1_r; t < v2_r; t++ )
242                                {
243                                    int val0 = chdims[t + 128];
244
245                                    array[val0] += MIN( t - v1_r, v2_r - t );
246                                    n[val0]++;
247
248                                }
249                            }
250
251                            if( y < size.height - 1 )
252                            {
253                                if( maskp[x + mask_step] )
254                                {
255                                    int v1_d = MIN( data0[x], data0[x + step] );
256                                    int v2_d = MAX( data0[x], data0[x + step] );
257
258                                    //    calculate contrast for the top-down pair
259                                    for( t = v1_d; t < v2_d; t++ )
260                                    {
261                                        int val0 = chdims[t + 128];
262
263                                        array[val0] += MIN( t - v1_d, v2_d - t );
264                                        n[val0]++;
265
266                                    }
267                                }
268                            }
269                        }
270                    }
271                }
272
273//  convert int to float
274                for( i = 0; i < j; i++ )
275                {
276                    if( n[i] != 0 )
277                        hist->array[i] = (float) array[i] / n[i];
278                    else
279                        hist->array[i] = 0;
280                }
281            }
282            break;
283        default:
284            return CV_BADSIZE_ERR;
285        }
286    }
287
288    cvFree( &n );
289    return CV_NO_ERR;
290}
291
292/*
293CV_IMPL void cvCalcContrastHist( IplImage** img, CvHistogram* hist, int dont_clear )
294{
295    CV_FUNCNAME( "cvCalcContrastHist" );
296    uchar*   data[CV_HIST_MAX_DIM];
297    int      step = 0;
298    CvSize roi = {0,0};
299
300    __BEGIN__;
301
302    {for( int i = 0; i < hist->c_dims; i++ )
303        CV_CALL( CV_CHECK_IMAGE( img[i] ) );}
304
305    {for( int i = 0; i < hist->c_dims; i++ )
306        cvGetImageRawData( img[i], &data[i], &step, &roi );}
307
308    if(img[0]->nChannels != 1)
309        CV_ERROR( IPL_BadNumChannels, "bad channels numbers" );
310
311    if(img[0]->depth != IPL_DEPTH_8U)
312        CV_ERROR( IPL_BadDepth, "bad image depth" );
313
314    switch(img[0]->depth)
315    {
316    case IPL_DEPTH_8U:
317        IPPI_CALL( icvCalcContrastHist8uC1R( data, step, roi, hist, dont_clear ) );
318        break;
319    default:  CV_ERROR( IPL_BadDepth, "bad image depth" );
320    }
321
322    __CLEANUP__;
323    __END__;
324}
325*/
326
327CV_IMPL void
328cvCalcContrastHist( IplImage ** img, CvHistogram * hist, int dont_clear, IplImage * mask )
329{
330    CV_FUNCNAME( "cvCalcContrastHist" );
331    uchar *data[CV_HIST_MAX_DIM];
332    uchar *mask_data = 0;
333    int step = 0;
334    int mask_step = 0;
335    CvSize roi = { 0, 0 };
336
337    __BEGIN__;
338
339    {
340        for( int i = 0; i < hist->c_dims; i++ )
341            CV_CALL( CV_CHECK_IMAGE( img[i] ));
342    }
343    if( mask )
344    {
345        CV_CALL( CV_CHECK_IMAGE( mask ));
346        if( mask->depth != IPL_DEPTH_8U )
347            CV_ERROR( CV_BadDepth, "bad mask depth" );
348        cvGetImageRawData( mask, &mask_data, &mask_step, 0 );
349    }
350
351
352    {
353        for( int i = 0; i < hist->c_dims; i++ )
354            cvGetImageRawData( img[i], &data[i], &step, &roi );
355    }
356
357    if( img[0]->nChannels != 1 )
358        CV_ERROR( CV_BadNumChannels, "bad channels numbers" );
359
360    if( img[0]->depth != IPL_DEPTH_8U )
361        CV_ERROR( CV_BadDepth, "bad image depth" );
362
363
364    switch (img[0]->depth)
365    {
366    case IPL_DEPTH_8U:
367        if( !mask )
368        {
369            IPPI_CALL( icvCalcContrastHist8uC1R( data, step, roi, hist, dont_clear ));
370        }
371        else
372        {
373            IPPI_CALL( icvCalcContrastHistMask8uC1R( data, step, mask_data,
374                                                     mask_step, roi, hist, dont_clear ));
375        }
376        break;
377    default:
378        CV_ERROR( CV_BadDepth, "bad image depth" );
379    }
380
381    __CLEANUP__;
382    __END__;
383}
384
385#endif
386