DETMON Pipeline Reference Manual  1.2.7
detmon_utils.c
1 /* $Id: detmon_utils.c,v 1.8 2013-07-15 12:03:32 amodigli Exp $
2  *
3  * This file is part of the IIINSTRUMENT Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2013-07-15 12:03:32 $
24  * $Revision: 1.8 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include "detmon_utils.h"
37 #include "irplib_utils.h"
38 
39 #include <cpl.h>
40 #include <assert.h>
41 
43 /*----------------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------------*/
48 
49 
50 /*----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------*/
59 const char * detmon_get_license(void)
60 {
61  const char * detmon_license =
62  "This file is part of the DETMON Instrument Pipeline\n"
63  "Copyright (C) 2002,2003 European Southern Observatory\n"
64  "\n"
65  "This program is free software; you can redistribute it and/or modify\n"
66  "it under the terms of the GNU General Public License as published by\n"
67  "the Free Software Foundation; either version 2 of the License, or\n"
68  "(at your option) any later version.\n"
69  "\n"
70  "This program is distributed in the hope that it will be useful,\n"
71  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
72  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
73  "GNU General Public License for more details.\n"
74  "\n"
75  "You should have received a copy of the GNU General Public License\n"
76  "along with this program; if not, write to the Free Software\n"
77  "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, \n"
78  "MA 02111-1307 USA" ;
79  return detmon_license ;
80 }
81 
82 
83 /* ---------------------------------------------------------------------------*/
89 /* ---------------------------------------------------------------------------*/
90 static void expand_region(const cpl_image * ref,
91  cpl_size * llx,
92  cpl_size * lly,
93  cpl_size * urx,
94  cpl_size * ury)
95 {
96  if (*llx < 1)
97  *llx = 1;
98  if (*lly < 1)
99  *lly = 1;
100  if (*urx < 1)
101  *urx = cpl_image_get_size_x(ref);
102  if (*ury < 1)
103  *ury = cpl_image_get_size_y(ref);
104 }
105 
106 
107 /* ---------------------------------------------------------------------------*/
127 /* ---------------------------------------------------------------------------*/
128 cpl_imagelist *
129 detmon_load_frameset_window(const cpl_frameset * fset, cpl_type type,
130  cpl_size pnum, cpl_size xtnum,
131  cpl_size llx, cpl_size lly,
132  cpl_size urx, cpl_size ury,
133  cpl_size nx, cpl_size ny)
134 {
135  cpl_imagelist * list = cpl_imagelist_new();
136  const size_t n = cpl_frameset_get_size(fset);
137  skip_if(pnum < 0);
138  if (nx >= 0 && ny >= 0) {
139  error_if(urx - llx + 1 > nx || ury - lly + 1 > ny ,
140  CPL_ERROR_ILLEGAL_INPUT,
141  "window size [%d:%d,%d:%d] larger than output size [%d, %d]",
142  (int)llx, (int)urx, (int)lly, (int)ury, (int)nx, (int)ny);
143  }
144 
145  for (size_t i = 0; i < n; i++) {
146  const cpl_frame * frm = cpl_frameset_get_position_const(fset, i);
147  const char * fn = cpl_frame_get_filename(frm);
148  if (xtnum < 0) {
149  cpl_size next = cpl_frame_get_nextensions(frm);
150  for (size_t e = 0; e < next; e++) {
151  cpl_image * image = cpl_image_load_window(fn, type, pnum, e,
152  llx, lly, urx, ury);
153  skip_if(image == NULL);
154  if(nx < 0 || ny < 0 ) {
155  cpl_imagelist_set(list, image, cpl_imagelist_get_size(list));
156  } else {
157  cpl_image * full = cpl_image_new(nx, ny, type);
158  cpl_image_copy(full, image, llx, lly);
159  cpl_image_delete(image);
160  cpl_imagelist_set(list, full, cpl_imagelist_get_size(list));
161  }
162 
163  }
164  }
165  else {
166  cpl_image * image = cpl_image_load_window(fn, type, pnum, xtnum,
167  llx, lly, urx, ury);
168  skip_if(image == NULL);
169  if(nx < 0 || ny < 0 ) {
170  cpl_imagelist_set(list, image, cpl_imagelist_get_size(list));
171  } else {
172  cpl_image * full = cpl_image_new(nx, ny, type);
173  cpl_image_copy(full, image, llx, lly);
174  cpl_image_delete(image);
175  cpl_imagelist_set(list, full, cpl_imagelist_get_size(list));
176  }
177  }
178  }
179 
180  end_skip;
181 
182  if (cpl_error_get_code()) {
183  cpl_imagelist_delete(list);
184  return NULL;
185  }
186 
187  return list;
188 }
189 
190 
191 /* ---------------------------------------------------------------------------*/
200 /* ---------------------------------------------------------------------------*/
201 cpl_image * detmon_subtract_create_window(const cpl_image * a,
202  const cpl_image * b,
203  cpl_size llx,
204  cpl_size lly,
205  cpl_size urx,
206  cpl_size ury)
207 {
208  cpl_image * na = cpl_image_extract(a, llx, lly, urx, ury);
209  cpl_image * nb = cpl_image_extract(b, llx, lly, urx, ury);
210  cpl_image_subtract(na, nb);
211  cpl_image_delete(nb);
212 
213  return na;
214 }
215 
216 
217 /* ---------------------------------------------------------------------------*/
229 /* ---------------------------------------------------------------------------*/
230 cpl_image * detmon_subtracted_avg(const cpl_image * on1,
231  const cpl_image * off1,
232  const cpl_image * on2,
233  const cpl_image * off2,
234  cpl_size llx, cpl_size lly,
235  cpl_size urx, cpl_size ury)
236 {
237  expand_region(on1, &llx, &lly, &urx, &ury);
238  cpl_image * dif1 = cpl_image_extract(on1, llx, lly, urx, ury);
239  cpl_image * dif2 = cpl_image_extract(on2, llx, lly, urx, ury);
240  cpl_image * b = cpl_image_extract(off1, llx, lly, urx, ury);
241  cpl_image * dif_avg;
242 
243  cpl_image_subtract(dif1, b);
244  if (off1 != off2) {
245  cpl_image_delete(b);
246  b = cpl_image_extract(off2, llx, lly, urx, ury);
247  cpl_image_subtract(dif1, b);
248  }
249  else {
250  cpl_image_subtract(dif2, b);
251  }
252  cpl_image_delete(b);
253 
254  dif_avg = cpl_image_average_create(dif1, dif2);
255  cpl_image_abs(dif_avg);
256 
257  cpl_image_delete(dif1);
258  cpl_image_delete(dif2);
259 
260  return dif_avg;
261 }
262