sinfo_pixel_handling.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019  /*---------------------------------------------------------------------------
00020    
00021    File name     :    pixel_handling.c
00022    Author         :    Nicolas Devillard
00023    Created on    :    March 04, 1997
00024    Description    :    Functions processing arrays of pixels.
00025 
00026  ---------------------------------------------------------------------------*/
00027 /*
00028 
00029  $Id: sinfo_pixel_handling.c,v 1.6 2012/03/03 09:50:08 amodigli Exp $
00030  $Author: amodigli $
00031  $Date: 2012/03/03 09:50:08 $
00032  $Revision: 1.6 $
00033 
00034  */
00035 
00036 #ifdef HAVE_CONFIG_H
00037 #  include <config.h>
00038 #endif
00039 /*---------------------------------------------------------------------------
00040                                   Includes
00041  ---------------------------------------------------------------------------*/
00042 #include <cpl.h>
00043 #include "sinfo_pixel_handling.h"
00053 /*---------------------------------------------------------------------------
00054                                 Function codes
00055  ---------------------------------------------------------------------------*/
00067 #define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
00068 #define PIX_STACK_SIZE 50
00069 
00070 void 
00071 sinfo_pixel_qsort(pixelvalue *pix_arr, int npix)
00072 {
00073     int         i,
00074                 ir,
00075                 j,
00076                 k,
00077                 l;
00078     int        i_stack[PIX_STACK_SIZE*sizeof(pixelvalue)] ;
00079     int         j_stack ;
00080     pixelvalue  a ;
00081 
00082     ir = npix ;
00083     l = 1 ;
00084     j_stack = 0 ;
00085     for (;;) {
00086         if (ir-l < 7) {
00087             for (j=l+1 ; j<=ir ; j++) {
00088                 a = pix_arr[j-1];
00089                 for (i=j-1 ; i>=1 ; i--) {
00090                     if (pix_arr[i-1] <= a) break;
00091                     pix_arr[i] = pix_arr[i-1];
00092                 }
00093                 pix_arr[i] = a;
00094             }
00095             if (j_stack == 0) break;
00096             ir = i_stack[j_stack-- -1];
00097             l  = i_stack[j_stack-- -1];
00098         } else {
00099             k = (l+ir) >> 1;
00100             PIX_SWAP(pix_arr[k-1], pix_arr[l])
00101             if (pix_arr[l] > pix_arr[ir-1]) {
00102                 PIX_SWAP(pix_arr[l], pix_arr[ir-1])
00103             }
00104             if (pix_arr[l-1] > pix_arr[ir-1]) {
00105                 PIX_SWAP(pix_arr[l-1], pix_arr[ir-1])
00106             }
00107             if (pix_arr[l] > pix_arr[l-1]) {
00108                 PIX_SWAP(pix_arr[l], pix_arr[l-1])
00109             }
00110             i = l+1;
00111             j = ir;
00112             a = pix_arr[l-1];
00113             for (;;) {
00114                 do i++; while (pix_arr[i-1] < a);
00115                 do j--; while (pix_arr[j-1] > a);
00116                 if (j < i) break;
00117                 PIX_SWAP(pix_arr[i-1], pix_arr[j-1]);
00118             }
00119             pix_arr[l-1] = pix_arr[j-1];
00120             pix_arr[j-1] = a;
00121             j_stack += 2;
00122             if (j_stack > PIX_STACK_SIZE) {
00123                 sinfo_msg_error("stack too small : aborting");
00124                 exit(-2001) ;
00125             }
00126             if (ir-i+1 >= j-l) {
00127                 i_stack[j_stack-1] = ir;
00128                 i_stack[j_stack-2] = i;
00129                 ir = j-1;
00130             } else {
00131                 i_stack[j_stack-1] = j-1;
00132                 i_stack[j_stack-2] = l;
00133                 l = i;
00134             }
00135         }
00136     }
00137 }
00138 #undef PIX_STACK_SIZE
00139 #undef PIX_SWAP
00140 

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1