File: vmifutypes.c

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (500 lines) | stat: -rw-r--r-- 11,951 bytes parent folder | download | duplicates (4)
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
/* $Id: vmifutypes.c,v 1.2 2013-03-25 11:43:04 cgarcia Exp $
 *
 * This file is part of the VIMOS Pipeline
 * Copyright (C) 2002-2004 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

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

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

#include <pilmemory.h>

#include "vmimage.h"
#include "vmifutypes.h"
#include "cpl.h"


/* THESE FUNCTIONS & C. ARE STILL TO BE TESTED */

/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  These are functions for thresholding and detecting adjacent
  pixels on ifu images (Eclipse- like)

------------------------------------------------------------------------------
*/

/*
  Returns a pointer to a new Pixel Data
*/

VimosPixelData *newPixelData()
{
 VimosPixelData *newPData;
  /* allocate memory for Pixel Data */
  newPData = (VimosPixelData *) cpl_malloc(sizeof(VimosPixelData));

  /* error occured, exit */
  if (newPData == NULL)
    {
     abort();
    }

  /* fill up fields with default values */
  /* setting values to -1. to avoid always finding pixel (0,0) */
  /* IS IT OK? */
  newPData->x     = -1;
  newPData->y     = -1;
  newPData->value = -1.0;

  newPData->prev = newPData->next = NULL;
  /* return to caller */
  return(newPData);

}

/*
  Deletes all Pixel Data contained in the list
*/

void deletePixelData(VimosPixelData *aPixelData)
{
 VimosPixelData  *tmpPData;
 VimosPixelData  *nextPData;

  /* store start of the pixeldata list */
  tmpPData = aPixelData;

  /* traverse  list */
  while (tmpPData)
    {
     /* get address of next pixel data */
     nextPData = tmpPData->next;
     /* free current object */
     cpl_free(tmpPData);
     /* next one to process */
     tmpPData = nextPData;
    }
}


/*
  Returns a pointer to a new Pixel List
*/

VimosPixelList *newPixelList()
{
 VimosPixelList *newPList;
  /* allocate memory for Pixel List */
  newPList = (VimosPixelList *) cpl_malloc(sizeof(VimosPixelList));

  /* error occured, exit */
  if (newPList == NULL)
    {
     abort();
    }

  /* fill up fields with default values */
  newPList->numPixInRegion = 0;
  newPList->totIntensity   = 0.;
  newPList->pixelData      = NULL;

  newPList->prev = newPList->next = NULL;
  /* return to caller */
  return(newPList);

}

/*
  Deletes all Pixel List contained in the list
*/

void deletePixelList(VimosPixelList *aPixelList)
{
 VimosPixelList  *tmpPList;
 VimosPixelList  *nextPList;

  /* store start of the list */
  tmpPList = aPixelList;

  /* traverse  list */
  while (tmpPList)
    {
     /* delete the fibers in this slit */
     deletePixelData(tmpPList->pixelData);
     /* get address of next pixel list */
     nextPList = tmpPList->next;
     /* free current object */
     cpl_free(tmpPList);
     /* next one to process */
     tmpPList = nextPList;
    }
}



/*
  Returns a pointer to a new Pixel Region
*/

VimosPixelRegion *newPixelRegion()
{
  VimosPixelRegion *newPRegion;
  /* allocate memory for PixelRegion */
  newPRegion = (VimosPixelRegion *) cpl_malloc(sizeof(VimosPixelRegion));
  
  /* error occured, exit */
  if (newPRegion == NULL)
    {
     abort();
    }

  /* fill up fields with default values */
  newPRegion->numRegions = 0;
  newPRegion->pixelList = NULL;

  newPRegion->prev = newPRegion->next = NULL;
  /* return to caller */
  return(newPRegion);
  
}

/*
  Deletes all Pixel Regions contained in the list
*/

void deletePixelRegion(VimosPixelRegion *aPixelRegion)
{

  VimosPixelRegion *tmpPRegion, *nextPRegion;
  if (aPixelRegion == NULL)
    {
     return;
    }

  /* store start of the list */
  tmpPRegion = aPixelRegion;

  /* traverse  list */
  while (tmpPRegion)
    {

     deletePixelList(tmpPRegion->pixelList);
     /* get address of next pixel region */
     nextPRegion = tmpPRegion->next;
     /* free current object */
     cpl_free(tmpPRegion);
     /* next one to process */
     tmpPRegion = nextPRegion;
    }

}




/*
  Write a mask image with only pixels above threshold value.
*/

void  thresholdImage(VimosImage *ifuImage, float threshold, 
                     VimosImage *ifuMask, int *ngood)
{

#define ZERO  0.0
#define ONE  1.0

  int i;
  int imaSize;

  imaSize = ifuImage->xlen * ifuImage->ylen;
  *ngood = imaSize;

  /* loop on all input image pixels */
  for (i=0; i<imaSize; i++)
     {
      if (ifuImage->data[i] < threshold)
        {
         ifuMask->data[i] = ZERO;
         (*ngood)--;
        }
      else
        {
         ifuMask->data[i] = ONE;
        }
     }

#undef ZERO
#undef ONE

}

static int getPixelrankIfValid(VimosPixelAcc *pixAcc, int rank, int neighbor);

static int getPixelrankIfValid(VimosPixelAcc *pixAcc, int rank, int neighbor)
{
  /* Neighboring pixels are stored as: */
  /*      2                            */
  /*    1  3                           */
  /*      4                            */

  VimosUlong32 newpos;
  int i;
 
  /* This switch gets the neighbor position */
  switch (neighbor)
    {
     /* Left neighbor */
     case 1:
     newpos = pixAcc->pos[rank] - 1;
     break;

     /* Up neighbor */
     case 2:
     newpos = pixAcc->pos[rank] + pixAcc->imWidth;
     break;
 
     /* Right neighbor */
     case 3:
     newpos = pixAcc->pos[rank] + 1;
     break;
 
     /* Down neighbor */
     case 4:
     newpos = pixAcc->pos[rank] - pixAcc->imWidth;
     break;

     /* Invalid neighbor */
     default:
     puts("internal error: no such neighbor in getPixelrankIfValid");
     return 0;
    }

  /* Find out if the neighbor position is in a list, and if it is, */
  /* find if it has bot been tagged in another region yet          */

  for (i=pixAcc->CurrentRank ; i<pixAcc->Total1Pixels ; i++)
     {
      if ((pixAcc->pos[i] == newpos) && (pixAcc->ValidPixels[i] == TRUE))
          /* Found neighbor in the list: return its rank */
          return(i) ;
     }

  /* the neighbor is not a valid pixel: return 0 */
  return 0 ;

}


static void floodfillFromPixel(VimosPixelAcc *pixAcc, int rank,
                     VimosPixelList *pixList, VimosPixelData *lastPixData);


static void floodfillFromPixel(VimosPixelAcc *pixAcc, int rank,
                      VimosPixelList *pixList, VimosPixelData *lastPixData)
{
  int vrank;
  int posx, posy;
  VimosPixelData *pixData;

  /* At least the given pixel is white, accumulate its X and Y */
  /* coordinates and tag it as taken (FALSE)                   */
  posx = pixAcc->pos[rank] % pixAcc->imWidth;
  posy = pixAcc->pos[rank] / pixAcc->imWidth;

  pixAcc->accIntensity +=
                pixAcc->refImage->data[posx+posy*pixAcc->imWidth];
  pixAcc->ValidPixels[rank] = FALSE;
  pixAcc->RemainingPixels --;
  pixAcc->PixelsInCurrentBlock++;

  /* store coordinates and intensity of each pixel in this region in a */
  /* pixel list                                                        */
  pixData = newPixelData();
  pixData->x = posx;
  pixData->y = posy;
  pixData->value = pixAcc->refImage->data[posx+posy*pixAcc->imWidth];

  /* start linking the pixel to a list */
  /* link pixel to list */
  if (lastPixData == NULL) 
    {
     /* first pixel we do */
     pixList->pixelData = pixData;
    }
  else
    {
     /* not first, add to tail */
     lastPixData->next = pixData;
     pixData->prev = lastPixData;
    }
  /* store tail of linked list */
  lastPixData = pixData;

  /* Go recursive with 4 neighbors */
  if ((vrank = getPixelrankIfValid(pixAcc, rank, 1)) != 0)
      floodfillFromPixel(pixAcc, vrank, pixList, lastPixData);
 
  if ((vrank = getPixelrankIfValid(pixAcc, rank, 2)) != 0)
      floodfillFromPixel(pixAcc, vrank, pixList, lastPixData);
 
  if ((vrank = getPixelrankIfValid(pixAcc, rank, 3)) != 0)
      floodfillFromPixel(pixAcc, vrank, pixList, lastPixData);
 
  if ((vrank = getPixelrankIfValid(pixAcc, rank, 4)) != 0)
      floodfillFromPixel(pixAcc, vrank, pixList, lastPixData);

}

/*
  Find regions of "connected" pixels and store them in a PixelRegion structure.
*/

VimosPixelRegion *findRegionsOnPixelMap (VimosImage *refImage, 
                                         VimosImage *ifuMask, 
                                         VimosPixelRegion *pixRegion,
                                         int ngoodpix)
{

#define ZERO  0.0
#define ONE  1.0

  int i;
  int foundRegions;
  int numMapPix, nbpix;

  VimosPixelAcc pixAcc;
  VimosPixelList *lastPixList, *pixList;
  VimosPixelData *lastPixData;

  /* Initialize number of found white blobs to zero */
  foundRegions = 0;

  /* get total number of pixels in the ifu mask */
  numMapPix = ifuMask->xlen * ifuMask->ylen;

  /* if more than 60% of the input pixels are GOOD, do nothing ... WHY????*/
  if (ngoodpix > (int)(0.6 * (double)numMapPix))
    {
     puts("findRegionsOnPixelMap: more than 60% of good pixel. Exiting");
     abort();
    }

  /* Allocate memory: we will store a pixel position and a boolean flag */
  /* per valid pixel in the input binary map.                           */
 
  pixAcc.pos = (VimosUlong32*)cpl_calloc(ngoodpix, sizeof(VimosUlong32));
  pixAcc.ValidPixels = (VimosBool*)cpl_malloc(ngoodpix * sizeof(VimosBool));
 
  /* Set all flags to TRUE: no pixel has been placed in any region yet */
  for (i=0 ; i<ngoodpix ; i++) pixAcc.ValidPixels[i] = TRUE;

  /* Store all valid pixel positions in an array */
  nbpix = 0;

  for (i=0 ; i<numMapPix ; i++)
     {
      if (ifuMask->data[i] == ONE) pixAcc.pos[nbpix++] = i;
     }

  /* Initialize all before entering recursivity */
 
  pixAcc.CurrentRank = 0;
  pixAcc.imWidth = ifuMask->xlen;
  pixAcc.Total1Pixels = ngoodpix;
  pixAcc.RemainingPixels = ngoodpix;

  /*  pixAcc.accXpos = 0.0; */
  /*  pixAcc.accYpos = 0.0; */

  pixAcc.accIntensity = 0.;
  pixAcc.PixelsInCurrentBlock = 0;
  pixAcc.refImage = refImage ;

  i=0;
  lastPixList = NULL;

  while (pixAcc.RemainingPixels)
    {
     /* Find first valid pixel in the list */
     while ( (pixAcc.ValidPixels[i] == FALSE) && 
             (i<pixAcc.Total1Pixels))
       {
        i++;
       }
 
     /* If we reached the table size, no more pixels to compute */
     if (i == pixAcc.Total1Pixels) break ;

     pixAcc.CurrentRank = i;

     lastPixData = NULL;
     pixList = newPixelList();

     /* Go recursive on all white blobs */
     floodfillFromPixel(&pixAcc, i, pixList, lastPixData);

     /* One more found blob */
     foundRegions++;

     pixList->totIntensity = pixAcc.accIntensity;
     pixList->numPixInRegion = pixAcc.PixelsInCurrentBlock;

     /* link pixelList to list */
     if (lastPixList == NULL) 
       {
        /* first pixelList we do */
        pixRegion->pixelList = pixList;
       }
     else
       {
        /* not first, add to tail */
        lastPixList->next = pixList;
        pixList->prev = lastPixList;
       }
     /* store tail of linked list */
     lastPixList = pixList;
 
     /* Reset accumulators and loop */
     pixAcc.accIntensity = 0.;
     pixAcc.PixelsInCurrentBlock = 0;
    }

  pixRegion->numRegions = foundRegions;

  cpl_free(pixAcc.ValidPixels) ;
  cpl_free(pixAcc.pos) ;

#undef ZERO
#undef ONE

  return(pixRegion);
}