SINFONI Pipeline Reference Manual  2.6.0
sinfo_pixel_handling.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 /*---------------------------------------------------------------------------
20 
21  File name : pixel_handling.c
22  Author : Nicolas Devillard
23  Created on : March 04, 1997
24  Description : Functions processing arrays of pixels.
25 
26  ---------------------------------------------------------------------------*/
27 /*
28 
29  $Id: sinfo_pixel_handling.c,v 1.6 2012-03-03 09:50:08 amodigli Exp $
30  $Author: amodigli $
31  $Date: 2012-03-03 09:50:08 $
32  $Revision: 1.6 $
33 
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
39 /*---------------------------------------------------------------------------
40  Includes
41  ---------------------------------------------------------------------------*/
42 #include <cpl.h>
43 #include "sinfo_pixel_handling.h"
51 /*---------------------------------------------------------------------------
52  Function codes
53  ---------------------------------------------------------------------------*/
65 #define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
66 #define PIX_STACK_SIZE 50
67 
68 void
69 sinfo_pixel_qsort(pixelvalue *pix_arr, int npix)
70 {
71  int i, ir, j, k, l;
72  int i_stack[PIX_STACK_SIZE * sizeof(pixelvalue)];
73  int j_stack;
74  pixelvalue a;
75 
76  ir = npix;
77  l = 1;
78  j_stack = 0;
79  for (;;) {
80  if (ir - l < 7) {
81  for (j = l + 1; j <= ir; j++) {
82  a = pix_arr[j - 1];
83  for (i = j - 1; i >= 1; i--) {
84  if (pix_arr[i - 1] <= a)
85  break;
86  pix_arr[i] = pix_arr[i - 1];
87  }
88  pix_arr[i] = a;
89  }
90  if (j_stack == 0)
91  break;
92  ir = i_stack[j_stack-- - 1];
93  l = i_stack[j_stack-- - 1];
94  }
95  else {
96  k = (l + ir) >> 1;
97  PIX_SWAP(pix_arr[k - 1], pix_arr[l])
98  if (pix_arr[l] > pix_arr[ir - 1]) {
99  PIX_SWAP(pix_arr[l], pix_arr[ir - 1])
100  }
101  if (pix_arr[l - 1] > pix_arr[ir - 1]) {
102  PIX_SWAP(pix_arr[l - 1], pix_arr[ir - 1])
103  }
104  if (pix_arr[l] > pix_arr[l - 1]) {
105  PIX_SWAP(pix_arr[l], pix_arr[l - 1])
106  }
107  i = l + 1;
108  j = ir;
109  a = pix_arr[l - 1];
110  for (;;) {
111  do
112  i++;
113  while (pix_arr[i - 1] < a);
114  do
115  j--;
116  while (pix_arr[j - 1] > a);
117  if (j < i)
118  break;
119  PIX_SWAP(pix_arr[i - 1], pix_arr[j - 1]);
120  }
121  pix_arr[l - 1] = pix_arr[j - 1];
122  pix_arr[j - 1] = a;
123  j_stack += 2;
124  if (j_stack > PIX_STACK_SIZE) {
125  sinfo_msg_error("stack too small : aborting");
126  exit(-2001);
127  }
128  if (ir - i + 1 >= j - l) {
129  i_stack[j_stack - 1] = ir;
130  i_stack[j_stack - 2] = i;
131  ir = j - 1;
132  }
133  else {
134  i_stack[j_stack - 1] = j - 1;
135  i_stack[j_stack - 2] = l;
136  l = i;
137  }
138  }
139  }
140 }
141 #undef PIX_STACK_SIZE
142 #undef PIX_SWAP
143 
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69