source: tags/release-1.1/src/Cubes/trimImage.cc @ 1391

Last change on this file since 1391 was 299, checked in by Matthew Whiting, 17 years ago

Adding distribution text at the start of each file...

File size: 9.3 KB
Line 
1// -----------------------------------------------------------------------
2// trimImage.cc: Trim a Cube of BLANKs around the spatial edge.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <param.hh>
30#include <Cubes/cubes.hh>
31#include <PixelMap/Object3D.hh>
32
33using namespace PixelInfo;
34
35void Cube::trimCube()
36{
37  /**
38   *  If the blankPix flag has been set, this routine trims excess blank
39   *    pixels from the edges of the spatial image.
40   *   It uses as its template the first channel, assuming that its non-BLANK
41   *    size is representative of the rest of the channels.
42   *   The edges are moved in until the first non-BLANK pixel is encountered.
43   *   All other arrays are similarly edited, and the amount of trimming is
44   *    recorded.
45   */
46
47//   if(this->par.getFlagBlankPix()) {
48  if(this->par.getFlagTrim()) {
49
50    long xdim = this->axisDim[0];
51    long ydim = this->axisDim[1];
52    long zdim = this->axisDim[2];
53    const long ZCHAN = 0;
54
55    int left,right,top,bottom;
56
57    for(int z = 0; z < zdim; z++){
58
59      for(int row=0;row<ydim;row++){
60        left=0;
61        right=0;
62        while((left<xdim)&&
63              (this->par.isBlank(this->array[row*xdim+left+ZCHAN*xdim*ydim])) ) {
64          left++;
65        }
66        while((right<xdim)&&
67              (this->par.isBlank(this->array[row*xdim+(xdim-1-right)+ZCHAN*xdim*ydim]))){
68          right++;
69        }
70
71        if( ((z==0)&&(row==0)) || (left < this->par.getBorderLeft()) )
72          this->par.setBorderLeft(left);
73        if( ((z==0)&&(row==0)) || (right < this->par.getBorderRight()) )
74          this->par.setBorderRight(right);
75
76      }
77   
78      for(int col=0;col<xdim;col++){
79        bottom=0;
80        top=0;
81        while((bottom<ydim)&&
82              (this->par.isBlank(this->array[col+bottom*xdim+ZCHAN*xdim*ydim])) ) bottom++;
83
84        while((top<ydim)&&
85              (this->par.isBlank(this->array[col+(ydim-1-top)*xdim+ZCHAN*xdim*ydim])) ) top++;
86
87        if( ((z==0)&&(col==0)) || (bottom < this->par.getBorderBottom()) )
88          this->par.setBorderBottom(bottom);
89        if( ((z==0)&&(col==0)) || (top < this->par.getBorderTop()) )
90          this->par.setBorderTop(top);
91
92      }
93
94    }
95   
96    left = this->par.getBorderLeft();
97    right = this->par.getBorderRight();
98    top = this->par.getBorderTop();
99    bottom = this->par.getBorderBottom();
100
101    this->axisDim[0] = xdim - left - right;
102    this->axisDim[1] = ydim - bottom - top;
103    this->axisDim[2] = zdim;
104    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
105
106    long oldpos,newpos;
107   
108    // Do the trimming, but only if we need to -- is there a border of Blanks?
109    if((left>0)||(right>0)||(bottom>0)||(top>0)) {
110
111      // Trim the array of pixel values
112      float *newarray  = new float[this->numPixels];
113      for(int x = 0; x < axisDim[0]; x++){
114        for(int y = 0; y < axisDim[1]; y++){
115          for(int z = 0; z < axisDim[2]; z++){
116            oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
117            newpos = x + y*this->axisDim[0] +
118              z*this->axisDim[0]*this->axisDim[1];
119            newarray[newpos]  = this->array[oldpos];
120          }
121        }
122      }
123      delete [] this->array;
124      this->array = newarray;
125
126      // Trim the array of baseline values
127      if(this->par.getFlagBaseline()){
128        float *newarray  = new float[this->numPixels];
129        for(int x = 0; x < axisDim[0]; x++){
130          for(int y = 0; y < axisDim[1]; y++){
131            for(int z = 0; z < axisDim[2]; z++){
132              oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
133              newpos = x + y*this->axisDim[0] +
134                z*this->axisDim[0]*this->axisDim[1];
135              newarray[newpos]  = this->baseline[oldpos];
136            }
137          }
138        }
139        delete [] this->baseline;
140        this->baseline = newarray;
141      }
142
143      // Trim the 2-D detection map
144      short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
145      for(int x = 0; x < axisDim[0]; x++){
146        for(int y = 0; y < axisDim[1]; y++){
147          oldpos = (x+left) + (y+bottom)*xdim;
148          newpos = x + y*this->axisDim[0];
149          newdetect[newpos] = this->detectMap[oldpos];
150        }
151      }
152      delete [] this->detectMap;
153      this->detectMap = newdetect;
154
155      if(this->par.getFlagATrous() || this->par.getFlagSmooth()){
156        // Trim the reconstructed array if we are going to do the
157        // reconstruction or smooth the array
158        float *newrecon  = new float[this->numPixels];
159        for(int x = 0; x < axisDim[0]; x++){
160          for(int y = 0; y < axisDim[1]; y++){
161            for(int z = 0; z < axisDim[2]; z++){
162              oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
163              newpos = x + y*this->axisDim[0] +
164                z*this->axisDim[0]*this->axisDim[1];
165              newrecon[newpos] = this->recon[oldpos];     
166            }
167          }
168        }
169        delete [] this->recon;
170        this->recon = newrecon;
171      }
172
173      // Set the flag indicating trimming has taken place only if it has.
174      this->par.setFlagCubeTrimmed(true);
175
176    }
177
178  }
179
180}
181
182
183void Cube::unTrimCube()
184{
185  /**
186   *  If the cube has been trimmed by trimCube(), this task adds back the
187   *   BLANK pixels on the edges, so that it returns to its original size.
188   *   All arrays are similarly edited.
189   */
190
191  if(this->par.getFlagCubeTrimmed()){
192
193    long left = this->par.getBorderLeft();
194    long right = this->par.getBorderRight();
195    long top = this->par.getBorderTop();
196    long bottom = this->par.getBorderBottom();
197
198    long smallXDim = this->axisDim[0];
199    long smallYDim = this->axisDim[1];
200    long smallZDim = this->axisDim[2];
201
202    // first correct the dimension sizes
203    this->axisDim[0] = smallXDim + left + right;
204    this->axisDim[1] = smallYDim + bottom + top;
205    this->axisDim[2] = smallZDim;
206    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
207
208    long pos,smlpos;
209    bool isDud;
210
211    // Correct the array of pixel values
212    float *newarray  = new float[this->numPixels];
213    for(int x = 0; x < this->axisDim[0]; x++){
214      for(int y = 0; y < this->axisDim[1]; y++){
215        isDud = (x<left) || (x>=smallXDim+left) ||
216          (y<bottom) || (y>=smallYDim+bottom);
217       
218        for(int z = 0; z < this->axisDim[2]; z++){
219          pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
220          smlpos = (x-left) + (y-bottom)*smallXDim + z * smallXDim * smallYDim;
221          if(isDud) newarray[pos] = this->par.getBlankPixVal();
222          else      newarray[pos] = this->array[smlpos];
223        }
224      }
225    }
226    delete [] this->array;
227    this->array = newarray;
228
229    if(this->reconExists){
230      // Correct the reconstructed/smoothed array
231      float *newrecon   = new float[this->numPixels];
232      for(int x = 0; x < this->axisDim[0]; x++){
233        for(int y = 0; y < this->axisDim[1]; y++){
234          isDud = (x<left) || (x>=smallXDim+left) ||
235            (y<bottom) || (y>=smallYDim+bottom);
236         
237          for(int z = 0; z < this->axisDim[2]; z++){
238            pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
239            smlpos = (x-left) + (y-bottom)*smallXDim
240              + z * smallXDim * smallYDim;
241            if(isDud) newrecon[pos] = this->par.getBlankPixVal();
242            else      newrecon[pos] = this->recon[smlpos];
243          }
244        }
245      }
246      delete [] this->recon;
247      this->recon = newrecon;
248    }
249
250    // Correct the array of baseline values
251    if(this->par.getFlagBaseline()){
252      float *newbase  = new float[this->numPixels];
253      for(int x = 0; x < this->axisDim[0]; x++){
254        for(int y = 0; y < this->axisDim[1]; y++){
255          isDud = (x<left) || (x>=smallXDim+left) ||
256            (y<bottom) || (y>=smallYDim+bottom);
257       
258          for(int z = 0; z < this->axisDim[2]; z++){
259            pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
260            smlpos = (x-left) + (y-bottom)*smallXDim + z*smallXDim*smallYDim;
261            if(isDud) newbase[pos] = this->par.getBlankPixVal();
262            else      newbase[pos] = this->baseline[smlpos];
263          }
264        }
265      }
266      delete [] this->baseline;
267      this->baseline = newbase;
268    }
269
270    // Correct the 2-D detection map
271    short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
272    for(int x = 0; x < this->axisDim[0]; x++){
273      for(int y = 0; y < this->axisDim[1]; y++){
274        pos = x + y*this->axisDim[0];
275        smlpos = (x-left) + (y-bottom)*smallXDim;
276        isDud = (x<left) || (x>=smallXDim+left) ||
277          (y<bottom) || (y>=smallYDim+bottom);
278        if(isDud) newdetect[pos]=0;
279        else newdetect[pos] = this->detectMap[smlpos];
280      }
281    }
282    delete [] this->detectMap;
283    this->detectMap = newdetect;   
284
285 
286    // Now update the positions for all the detections
287 
288    for(int i=0;i<this->objectList->size();i++){
289      this->objectList->at(i).pixels().addOffsets(left,bottom,0);
290      //      objectList[i].calcParams(this->array,this->axisDim);
291    }
292
293  }
294
295}
Note: See TracBrowser for help on using the repository browser.