File: cpl_image_stats.c

package info (click to toggle)
cpl 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,764 kB
  • sloc: ansic: 111,368; sh: 14,549; makefile: 626
file content (782 lines) | stat: -rw-r--r-- 32,000 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/* $Id: cpl_image_stats.c,v 1.90 2011/08/17 16:39:00 cgarcia Exp $
 *
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2008 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2011/08/17 16:39:00 $
 * $Revision: 1.90 $
 * $Name: cpl-6_1_1 $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <float.h>

#include "cpl_memory.h"
#include "cpl_stats.h"
#include "cpl_image_bpm.h"
#include "cpl_image_stats.h"
#include "cpl_mask.h"
#include "cpl_tools.h"

#include "cpl_image_defs.h"

/*----------------------------------------------------------------------------*/
/**
  @internal
  @ingroup cpl_image
  @brief    Define the body of a stats window function
  @param    op   Operation in lower case
  @param    OP   Operation in upper case
 */ 
/*----------------------------------------------------------------------------*/
#define CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(op, OP)                  \
                                                                        \
     cpl_stats * stats =                                                \
        cpl_stats_new_from_image_window(image,                          \
                                        CPL_CONCAT2X(CPL_STATS, OP),    \
                                        llx, lly,  urx, ury);           \
    double res;                                                         \
                                                                        \
    cpl_ensure(stats != NULL, cpl_error_get_code(), 0.0);               \
                                                                        \
    /* Should not be able to fail now */                                \
    res = CPL_CONCAT2X(cpl_stats_get, op)((const cpl_stats *)stats);    \
                                                                        \
    cpl_stats_delete(stats);                                            \
                                                                        \
    return res                                                     

/*----------------------------------------------------------------------------*/
/**
  @internal
  @ingroup cpl_image
  @brief    Define the body of a stats function
  @param    op   Operation in lower case
  @param    OP   Operation in upper case
*/ 
/*----------------------------------------------------------------------------*/
#define CPL_IMAGE_GET_STATS_DOUBLE_BODY(op, OP)                         \
                                                                        \
    cpl_stats * stats =                                                 \
        cpl_stats_new_from_image(image, CPL_CONCAT2X(CPL_STATS, OP));   \
    double res;                                                         \
                                                                        \
    cpl_ensure(stats != NULL, cpl_error_get_code(), 0.0);               \
                                                                        \
    /* Should not be able to fail now */                                \
    res = CPL_CONCAT2X(cpl_stats_get, op)((const cpl_stats *)stats);    \
                                                                        \
    cpl_stats_delete(stats);                                            \
                                                                        \
    return res                                                     

/*-----------------------------------------------------------------------------
                            Function codes
 -----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes minimum pixel value over an image sub-window.
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the minimum value, or undefined on error
  @see      cpl_stats_new_from_window()
  @note In case of error, the #_cpl_error_code_ code is set.

  The specified bounds are included in the specified region.
  
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_min_window(const cpl_image * image,
                                cpl_size          llx,
                                cpl_size          lly,
                                cpl_size          urx,
                                cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(min, MIN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes minimum pixel value over an image.
  @param    image       input image.
  @return   the minimum value
  @see      cpl_image_get_min_window()

  In case of error, the #_cpl_error_code_ code is set, and the returned double
  is undefined.
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_min(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(min, MIN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes maximum pixel value over an image sub-window.
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the maximum value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_max_window(const cpl_image * image,
                                cpl_size          llx,
                                cpl_size          lly,
                                cpl_size          urx,
                                cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(max, MAX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes maximum pixel value over an image.
  @param    image       input image.
  @return   the maximum value
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_max(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(max, MAX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes mean pixel value over an image sub-window.
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the mean value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_mean_window(
        const cpl_image   *   image,
        cpl_size        llx,
        cpl_size        lly,
        cpl_size        urx,
        cpl_size        ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(mean, MEAN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes mean pixel value over an image.
  @param    image       input image.
  @return   the mean value
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_mean(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(mean, MEAN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes median pixel value over an image sub-window.
  @param    image       Input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   The median value, or undefined on error

  The specified bounds are included in the specified region.

  In case of error, the #_cpl_error_code_ code is set, and the returned value
  is undefined.
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  For a finite population or sample, the median is the middle value of an odd
  number of values (arranged in ascending order) or any value between the two
  middle values of an even number of values.
  For an even number of elements in the array, the mean of the two central
  values is returned. Note that in this case, the median might not belong
  to the input array.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if an input pointer is NULL
  - CPL_ERROR_ILLEGAL_INPUT if the window is outside the image or if
    there are only bad pixels in the window
  - CPL_ERROR_INVALID_TYPE if the passed image type is not supported
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_median_window(const cpl_image * image,
                                   cpl_size          llx,
                                   cpl_size          lly,
                                   cpl_size          urx,
                                   cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(median, MEDIAN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes median pixel value over an image.
  @param    image   Input image.
  @return   the median value
  @see      cpl_image_get_median_window()

  In case of error, the #_cpl_error_code_ code is set, and the returned double
  is undefined.
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_median(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(median, MEDIAN);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes pixel standard deviation over an image sub-window.
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the standard deviation value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_stdev_window(const cpl_image * image,
                                  cpl_size          llx,
                                  cpl_size          lly,
                                  cpl_size          urx,
                                  cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(stdev, STDEV);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    computes pixel standard deviation over an image.
  @param    image       input image.
  @return   the standard deviation value
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_stdev(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(stdev, STDEV);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of pixel values over an image sub-window
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the flux value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_flux_window(const cpl_image * image,
                                 cpl_size          llx,
                                 cpl_size          lly,
                                 cpl_size          urx,
                                 cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(flux, FLUX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of pixel values over an image.
  @param    image       input image.
  @return   the flux value
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_flux(const cpl_image * image)
{
    cpl_ensure(image, CPL_ERROR_NULL_INPUT, 0.0);
    return cpl_image_get_flux_window(image, 1, 1, image->nx, image->ny);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of absolute values over an image sub-window
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the absolute flux (sum of |pixels|) value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_absflux_window(const cpl_image * image,
                                    cpl_size          llx,
                                    cpl_size          lly,
                                    cpl_size          urx,
                                    cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(absflux, ABSFLUX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of squared values over an image sub-window
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the square flux
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_sqflux_window(
        const cpl_image   *   image,
        cpl_size        llx,
        cpl_size        lly,
        cpl_size        urx,
        cpl_size        ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(sqflux, SQFLUX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of absolute values over an image
  @param    image       input image.
  @return   the absolute flux (sum of |pixels|) value
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_absflux(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(absflux, ABSFLUX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the sum of squared values over an image
  @param    image       input image.
  @return   the sqaure flux
  @see      cpl_image_get_min()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_sqflux(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(sqflux, SQFLUX);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the x centroid value over the whole image
  @param    image       input image.
  @return   the x centroid value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_centroid_x(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(centroid_x, CENTROID);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the x centroid value over an image sub-window
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the x centroid value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_centroid_x_window(const cpl_image * image,
                                       cpl_size          llx,
                                       cpl_size          lly,
                                       cpl_size          urx,
                                       cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(centroid_x, CENTROID);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the y centroid value over the whole image
  @param    image       input image.
  @return   the y centroid value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_centroid_y(const cpl_image * image)
{
    CPL_IMAGE_GET_STATS_DOUBLE_BODY(centroid_y, CENTROID);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes the y centroid value over an image sub-window
  @param    image       input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @return   the y centroid value
  @see      cpl_image_get_min_window()
 */ 
/*----------------------------------------------------------------------------*/
double cpl_image_get_centroid_y_window(const cpl_image * image,
                                       cpl_size          llx,
                                       cpl_size          lly,
                                       cpl_size          urx,
                                       cpl_size          ury)
{
    CPL_IMAGE_GET_STATS_WINDOW_DOUBLE_BODY(centroid_y, CENTROID);
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes minimum pixel value and position over an image sub window.
  @param    image       Input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @param    px          ptr to the x coordinate of the minimum position
  @param    py          ptr to the y coordinate of the minimum position
  @return   CPL_ERROR_NONE or the #_cpl_error_code_ on error
  @see      cpl_image_get_min_window()

  The specified bounds are included in the specified region.

  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_get_minpos_window(const cpl_image * image,
                                           cpl_size          llx,
                                           cpl_size          lly,
                                           cpl_size          urx,
                                           cpl_size          ury,
                                           cpl_size        * px,
                                           cpl_size        * py)
{
    cpl_stats * stats;

    cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image_window(image, CPL_STATS_MINPOS,
                                            llx, lly,  urx, ury);

    cpl_ensure_code(stats != NULL, cpl_error_get_code());
                                                                        
    /* Should not be able to fail now */

    *px = cpl_stats_get_min_x(stats);
    *py = cpl_stats_get_min_y(stats);

    cpl_stats_delete(stats);

    return CPL_ERROR_NONE;
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes minimum pixel value and position over an image.
  @param    image       Input image.
  @param    px          ptr to the x coordinate of the minimum position
  @param    py          ptr to the y coordinate of the minimum position
  @return   CPL_ERROR_NONE or the #_cpl_error_code_ on error
  @see      cpl_image_get_minpos_window()

  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
 */
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_get_minpos(const cpl_image * image,
                                    cpl_size        * px,
                                    cpl_size        * py)
{

    cpl_stats * stats;

    cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image(image, CPL_STATS_MINPOS);

    cpl_ensure_code(stats != NULL, cpl_error_get_code());
                                                                        
    /* Should not be able to fail now */

    *px = cpl_stats_get_min_x(stats);
    *py = cpl_stats_get_min_y(stats);

    cpl_stats_delete(stats);

    return CPL_ERROR_NONE;

}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes maximum pixel value and position over an image sub window.
  @param    image       Input image.
  @param    llx         Lower left x position (FITS convention, 1 for leftmost)
  @param    lly         Lower left y position (FITS convention, 1 for lowest)
  @param    urx         Upper right x position (FITS convention)
  @param    ury         Upper right y position (FITS convention)
  @param    px          ptr to the x coordinate of the maximum position
  @param    py          ptr to the y coordinate of the maximum position
  @return   CPL_ERROR_NONE or the #_cpl_error_code_ on error
  @see      cpl_image_get_minpos_window()
 */
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_get_maxpos_window(const cpl_image * image,
                                           cpl_size          llx,
                                           cpl_size          lly,
                                           cpl_size          urx,
                                           cpl_size          ury,
                                           cpl_size        * px,
                                           cpl_size        * py)
{
    cpl_stats * stats;

    cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image_window(image, CPL_STATS_MAXPOS,
                                            llx, lly,  urx, ury);

    cpl_ensure_code(stats != NULL, cpl_error_get_code());
                                                                        
    /* Should not be able to fail now */

    *px = cpl_stats_get_max_x(stats);
    *py = cpl_stats_get_max_y(stats);

    cpl_stats_delete(stats);

    return CPL_ERROR_NONE;
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes maximum pixel value and position over an image.
  @param    image       Input image.
  @param    px          ptr to the x coordinate of the maximum position
  @param    py          ptr to the y coordinate of the maximum position
  @return   CPL_ERROR_NONE or the #_cpl_error_code_ on error
  @see      cpl_image_get_minpos()
 */
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_get_maxpos(const cpl_image * image,
                                    cpl_size        * px,
                                    cpl_size        * py)
{
    cpl_stats * stats;

    cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
    cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image(image, CPL_STATS_MAXPOS);

    cpl_ensure_code(stats != NULL, cpl_error_get_code());
                                                                        
    /* Should not be able to fail now */

    *px = cpl_stats_get_max_x(stats);
    *py = cpl_stats_get_max_y(stats);

    cpl_stats_delete(stats);

    return CPL_ERROR_NONE;
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes first and second order image statistics using median.
  @param    image   Input image.
  @param    llx     Lower left x position (FITS convention, 1 for leftmost)
  @param    lly     Lower left y position (FITS convention, 1 for lowest)
  @param    urx     Upper right x position (FITS convention)
  @param    ury     Upper right y position (FITS convention)
  @param    sigma   Output computed sigma value.
  @return   the median
  @see      cpl_image_get_median_window()

  This function takes an image in input. It tries to estimate the average
  and standard deviation of the image by approximating them by resp. the
  median and the average absolute distance to the median.
  The median is the returned double. The average absolute distance to the
  median is written into sigma.
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.
  In case of error, the #_cpl_error_code_ code is set, and the returned double
  is undefined.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
  - CPL_ERROR_ILLEGAL_INPUT if the image bad pixels map cannot be
    retrieved
  - CPL_ERROR_INVALID_TYPE if the passed image type is not supported
  - CPL_ERROR_DATA_NOT_FOUND if all the pixels are bad in image
 */
/*----------------------------------------------------------------------------*/
double cpl_image_get_median_dev_window(const cpl_image * image,
                                       cpl_size          llx,
                                       cpl_size          lly,
                                       cpl_size          urx,
                                       cpl_size          ury,
                                       double          * sigma)
{
    cpl_stats * stats;
    double median;


    cpl_ensure_code(sigma != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image_window(image, CPL_STATS_MEDIAN_DEV,
                                            llx, lly,  urx, ury);

    cpl_ensure(stats != NULL, cpl_error_get_code(), 0.0);               
                                                                        
    /* Should not be able to fail now */

    median = cpl_stats_get_median(stats);
    *sigma = cpl_stats_get_median_dev(stats);

    cpl_stats_delete(stats);

    return median;
}

/*----------------------------------------------------------------------------*/
/**
  @ingroup cpl_image
  @brief    Computes first and second order image statistics using median.
  @param    image   Input image.
  @param    sigma   Output computed sigma value.
  @return   the median
  @see      cpl_image_get_median_window()

  This function takes an image in input. It tries to estimate the average
  and standard deviation of the image by approximating them by resp. the
  median and the average absolute distance to the median.
  The median is the returned double. The average absolute distance to the
  median is written into sigma.
  Images can be CPL_TYPE_FLOAT, CPL_TYPE_INT or CPL_TYPE_DOUBLE.
  In case of error, the #_cpl_error_code_ code is set, and the returned double
  is undefined.

  Possible #_cpl_error_code_ set in this function:
  - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
  - CPL_ERROR_ILLEGAL_INPUT if the image bad pixels map cannot be
    retrieved
  - CPL_ERROR_INVALID_TYPE if the passed image type is not supported
  - CPL_ERROR_DATA_NOT_FOUND if all the pixels are bad in image
 */
/*----------------------------------------------------------------------------*/
double cpl_image_get_median_dev(const cpl_image * image,
                                double          * sigma)
{
    cpl_stats * stats;
    double median;


    cpl_ensure_code(sigma != NULL, CPL_ERROR_NULL_INPUT);

    stats = cpl_stats_new_from_image(image, CPL_STATS_MEDIAN_DEV);

    cpl_ensure(stats != NULL, cpl_error_get_code(), 0.0);               
                                                                        
    /* Should not be able to fail now */

    median = cpl_stats_get_median(stats);
    *sigma = cpl_stats_get_median_dev(stats);

    cpl_stats_delete(stats);

    return median;
}