source: trunk/src/Cubes/trimImage.cc @ 258

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

Merging pixel-map-branch revisions 236:257 back into trunk.
The use of the PixelMap? functions is sorted now, so we put everything back into a uniform location.

File size: 7.2 KB
Line 
1#include <iostream>
2#include <param.hh>
3#include <Cubes/cubes.hh>
4#include <PixelMap/Object3D.hh>
5
6using namespace PixelInfo;
7
8void Cube::trimCube()
9{
10  /**
11   *  If the blankPix flag has been set, this routine trims excess blank
12   *    pixels from the edges of the spatial image.
13   *   It uses as its template the first channel, assuming that its non-BLANK
14   *    size is representative of the rest of the channels.
15   *   The edges are moved in until the first non-BLANK pixel is encountered.
16   *   All other arrays are similarly edited, and the amount of trimming is
17   *    recorded.
18   */
19
20  if(this->par.getFlagBlankPix()) {
21
22    long xdim = this->axisDim[0];
23    long ydim = this->axisDim[1];
24    long zdim = this->axisDim[2];
25    const long ZCHAN = 0;
26
27    int left,right,top,bottom;
28
29    for(int z = 0; z < zdim; z++){
30
31      for(int row=0;row<ydim;row++){
32        left=0;
33        right=0;
34        while((left<xdim)&&
35              (this->par.isBlank(this->array[row*xdim+left+ZCHAN*xdim*ydim])) ) {
36          left++;
37        }
38        while((right<xdim)&&
39              (this->par.isBlank(this->array[row*xdim+(xdim-1-right)+ZCHAN*xdim*ydim]))){
40          right++;
41        }
42
43        if( ((z==0)&&(row==0)) || (left < this->par.getBorderLeft()) )
44          this->par.setBorderLeft(left);
45        if( ((z==0)&&(row==0)) || (right < this->par.getBorderRight()) )
46          this->par.setBorderRight(right);
47
48      }
49   
50      for(int col=0;col<xdim;col++){
51        bottom=0;
52        top=0;
53        while((bottom<ydim)&&
54              (this->par.isBlank(this->array[col+bottom*xdim+ZCHAN*xdim*ydim])) ) bottom++;
55
56        while((top<ydim)&&
57              (this->par.isBlank(this->array[col+(ydim-1-top)*xdim+ZCHAN*xdim*ydim])) ) top++;
58
59        if( ((z==0)&&(col==0)) || (bottom < this->par.getBorderBottom()) )
60          this->par.setBorderBottom(bottom);
61        if( ((z==0)&&(col==0)) || (top < this->par.getBorderTop()) )
62          this->par.setBorderTop(top);
63
64      }
65
66    }
67   
68    left = this->par.getBorderLeft();
69    right = this->par.getBorderRight();
70    top = this->par.getBorderTop();
71    bottom = this->par.getBorderBottom();
72
73    this->axisDim[0] = xdim - left - right;
74    this->axisDim[1] = ydim - bottom - top;
75    this->axisDim[2] = zdim;
76    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
77
78    long oldpos,newpos;
79   
80    // Do the trimming, but only if we need to -- is there a border of Blanks?
81    if((left>0)||(right>0)||(bottom>0)||(top>0)) {
82
83      // Trim the array of pixel values
84      float *newarray  = new float[this->numPixels];
85      for(int x = 0; x < axisDim[0]; x++){
86        for(int y = 0; y < axisDim[1]; y++){
87          for(int z = 0; z < axisDim[2]; z++){
88            oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
89            newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1];
90            newarray[newpos]  = this->array[oldpos];
91          }
92        }
93      }
94      delete [] this->array;
95      this->array = newarray;
96
97      // Trim the array of baseline values
98      if(this->par.getFlagBaseline()){
99        float *newarray  = new float[this->numPixels];
100        for(int x = 0; x < axisDim[0]; x++){
101          for(int y = 0; y < axisDim[1]; y++){
102            for(int z = 0; z < axisDim[2]; z++){
103              oldpos = (x+this->par.getBorderLeft()) + (y+this->par.getBorderBottom())*xdim + z*xdim*ydim;
104              newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1];
105              newarray[newpos]  = this->baseline[oldpos];
106            }
107          }
108        }
109        delete [] this->baseline;
110        this->baseline = newarray;
111      }
112
113      // Trim the 2-D detection map
114      short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
115      for(int x = 0; x < axisDim[0]; x++){
116        for(int y = 0; y < axisDim[1]; y++){
117          oldpos = (x+left) + (y+bottom)*xdim;
118          newpos = x + y*axisDim[0];
119          newdetect[newpos] = this->detectMap[oldpos];
120        }
121      }
122      delete [] this->detectMap;
123      this->detectMap = newdetect;
124
125      if(this->par.getFlagATrous()){
126        // Trim the reconstructed array if we are going to do the
127        // reconstruction
128        float *newrecon  = 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*axisDim[0] + z*axisDim[0]*axisDim[1];
134              newrecon[newpos] = this->recon[oldpos];     
135            }
136          }
137        }
138        delete [] this->recon;
139        this->recon = newrecon;
140      }
141
142      // Set the flag indicating trimming has taken place only if it has.
143      this->par.setFlagCubeTrimmed(true);
144
145    }
146
147  }
148
149}
150
151
152void Cube::unTrimCube()
153{
154  /**
155   *  If the cube has been trimmed by trimCube(), this task adds back the
156   *   BLANK pixels on the edges, so that it returns to its original size.
157   *   All arrays are similarly edited.
158   */
159
160  if(this->par.getFlagCubeTrimmed()){
161
162    long left = this->par.getBorderLeft();
163    long right = this->par.getBorderRight();
164    long top = this->par.getBorderTop();
165    long bottom = this->par.getBorderBottom();
166
167    long smallXDim = this->axisDim[0];
168    long smallYDim = this->axisDim[1];
169    long smallZDim = this->axisDim[2];
170
171    // first correct the dimension sizes
172    this->axisDim[0] = smallXDim + left + right;
173    this->axisDim[1] = smallYDim + bottom + top;
174    this->axisDim[2] = smallZDim;
175    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
176
177    long pos,smlpos;
178    bool isDud;
179
180    // Correct the array of pixel values
181    float *newarray  = new float[this->numPixels];
182    for(int x = 0; x < this->axisDim[0]; x++){
183      for(int y = 0; y < this->axisDim[1]; y++){
184        isDud = (x<left) || (x>=smallXDim+left) ||
185          (y<bottom) || (y>=smallYDim+bottom);
186       
187        for(int z = 0; z < this->axisDim[2]; z++){
188          pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
189          smlpos = (x-left) + (y-bottom)*smallXDim + z * smallXDim * smallYDim;
190          if(isDud) newarray[pos] = this->par.getBlankPixVal();
191          else      newarray[pos] = this->array[smlpos];
192        }
193      }
194    }
195    delete [] this->array;
196    this->array = newarray;
197
198    if(this->reconExists){
199      // Correct the reconstructed array
200      float *newrecon   = new float[this->numPixels];
201      for(int x = 0; x < this->axisDim[0]; x++){
202        for(int y = 0; y < this->axisDim[1]; y++){
203          isDud = (x<left) || (x>=smallXDim+left) ||
204            (y<bottom) || (y>=smallYDim+bottom);
205         
206          for(int z = 0; z < this->axisDim[2]; z++){
207            pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
208            smlpos = (x-left) + (y-bottom)*smallXDim
209              + z * smallXDim * smallYDim;
210            if(isDud) newrecon[pos] = this->par.getBlankPixVal();
211            else      newrecon[pos] = this->recon[smlpos];
212          }
213        }
214      }
215      delete [] this->recon;
216      this->recon = newrecon;
217    }
218
219    // Correct the 2-D detection map
220    short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
221    for(int x = 0; x < this->axisDim[0]; x++){
222      for(int y = 0; y < this->axisDim[1]; y++){
223        pos = x + y*this->axisDim[0];
224        smlpos = (x-left) + (y-bottom)*smallXDim;
225        isDud = (x<left) || (x>=smallXDim+left) ||
226          (y<bottom) || (y>=smallYDim+bottom);
227        if(isDud) newdetect[pos]=0;
228        else newdetect[pos] = this->detectMap[smlpos];
229      }
230    }
231    delete [] this->detectMap;
232    this->detectMap = newdetect;   
233
234 
235    // Now update the positions for all the detections
236 
237    for(int i=0;i<this->objectList.size();i++){
238      this->objectList[i].pixels().addOffsets(left,bottom,0);
239      //      objectList[i].calcParams(this->array,this->axisDim);
240    }
241
242  }
243
244}
Note: See TracBrowser for help on using the repository browser.