Bsoft  2.1.0
Bernard's software package
Bimage_resize.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "Matrix.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
29  int fill_type, double fill)
30 {
31  if ( verbose & VERB_DEBUG )
32  cout << "DEBUG Bimage::resize: " << nusize << endl;
33 
34  if ( nusize == size() ) return 0;
35 
36  if ( nusize[0] < 1 ) nusize[0] = x;
37  if ( nusize[1] < 1 ) nusize[1] = y;
38  if ( nusize[2] < 1 ) nusize[2] = z;
39 
40  if ( fill_type == FILL_AVERAGE ) fill = avg;
41  if ( fill_type == FILL_MIN ) fill = min;
42  if ( fill_type == FILL_MAX ) fill = max;
43 
44  long i, j, xx, yy, zz, nn;
45  long oldx, oldy, oldz;
46  long elementsize = c*data_type_size();
47  long nualloc = (long) nusize.volume()*n*elementsize;
48  long fomsize = (long) nusize.volume()*n*sizeof(float);
49  unsigned char* nudata = new unsigned char[nualloc];
50  float* fom = NULL;
51  float* nufom = NULL;
52  if ( next ) {
53  fom = (float *) next->data_pointer();
54  nufom = new float[fomsize];
55  for ( i=0; i<fomsize; i++ ) nufom[i] = 0;
56  }
57 
58  if ( verbose & VERB_FULL ) {
59  cout << "Resizing:" << endl;
60  cout << "Shift: " << translate << endl;
61  cout << "New size: " << nusize << endl;
62  if ( fill_type != FILL_BACKGROUND )
63  cout << "Fill value: " << fill << endl;
64  cout << endl;
65  }
66 
67  TypePointer fp;
68 
69  for ( i=nn=0; nn<n; nn++ ) {
70  image[nn].origin(image[nn].origin() + translate);
71 // if ( verbose & VERB_PROCESS )
72 // cout << "Image " << nn+1 << " new origin: " << image[nn].origin() << endl;
73  if ( fill_type == FILL_BACKGROUND ) {
74  fill = background(nn);
75 // if ( verbose & VERB_PROCESS )
76 // cout << "Image " << nn+1 << " fill value: " << fill << endl;
77  }
78  fp = fill_value(fill);
79  for ( zz=0; zz<nusize[2]; zz++ ) {
80  oldz = (long)zz - translate[2];
81  for ( yy=0; yy<nusize[1]; yy++ ) {
82  oldy = (long)yy - translate[1];
83  for ( xx=0; xx<nusize[0]; xx++, i++ ) {
84  oldx = (long)xx - translate[0];
85  if ( oldx < 0 || oldx >= x || oldy < 0 ||
86  oldy >= y || oldz < 0 || oldz >= z ) {
87  memcpy(nudata+i*elementsize, fp.uc, elementsize);
88  } else {
89  j = index(oldx, oldy, oldz, nn);
90  memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
91  if ( fom ) nufom[i] = fom[j];
92  }
93  }
94  }
95  }
96  delete[] fp.uc;
97  }
98 
99 // if ( verbose & VERB_PROCESS )
100 // cout << endl;
101 
102  size(nusize);
103  data_assign(nudata);
104 
105  if ( fom ) next->data_assign((unsigned char*) nufom);
106 
107  statistics();
108 
109  return 0;
110 }
111 
125  int fill_type, double fill)
126 {
127  if ( !data_pointer() ) return NULL;
128 
129  if ( nusize[0] < 1 ) nusize[0] = x;
130  if ( nusize[1] < 1 ) nusize[1] = y;
131  if ( nusize[2] < 1 ) nusize[2] = z;
132 
133  long i, j, cc, xx, yy, zz, nn;
134  long oldx, oldy, oldz;
135 
136  if ( fill_type == FILL_AVERAGE ) fill = avg;
137  if ( fill_type == FILL_MIN ) fill = min;
138  if ( fill_type == FILL_MAX ) fill = max;
139 
140  if ( verbose & VERB_PROCESS ) {
141  cout << "Resizing:" << endl;
142  cout << "Shift: " << translate << endl;
143  cout << "New size: " << nusize << endl;
144  if ( fill_type != FILL_BACKGROUND )
145  cout << "Fill value: " << fill << endl;
146  cout << endl;
147  } else if ( verbose & VERB_LABEL )
148  cout << "Resizing" << endl << endl;
149 
150  Bimage* pnu = copy_header();
151  pnu->size(nusize);
152  pnu->page_size(nusize);
153  pnu->data_alloc();
154 
155  for ( i=nn=0; nn<n; nn++ ) {
156  pnu->image[nn].origin(image[nn].origin() + translate);
157  pnu->image[nn].origin(image[nn].origin() + translate);
158  if ( verbose & VERB_PROCESS )
159  cout << "Image " << nn+1 << " new origin: " << pnu->image[nn].origin() << endl;
160  if ( fill_type == FILL_BACKGROUND ) {
161  fill = background(nn);
162  if ( verbose & VERB_PROCESS )
163  cout << "Image " << nn+1 << " fill value: " << fill << endl;
164  }
165  for ( zz=0; zz<pnu->sizeZ(); zz++ ) {
166  oldz = (long)zz - translate[2];
167  for ( yy=0; yy<pnu->sizeY(); yy++ ) {
168  oldy = (long)yy - translate[1];
169  for ( xx=0; xx<pnu->sizeX(); xx++ ) {
170  oldx = (long)xx - translate[0];
171  if ( oldx < 0 || oldx >= x || oldy < 0 ||
172  oldy >= y || oldz < 0 || oldz >= z ) {
173  for ( cc=0; cc<c; cc++, i++ ) pnu->set(i, fill);
174  } else {
175  j = index(0, oldx, oldy, oldz, nn);
176  for ( cc=0; cc<c; cc++, i++, j++ ) pnu->set(i, (*this)[j]);
177  }
178  }
179  }
180  }
181  }
182 
183  if ( verbose & VERB_PROCESS )
184  cout << endl;
185 
186  if ( pnu->statistics() )
187  cerr << tab << "in Bimage::resize" << endl;
188 
189  return pnu;
190 }
191 
203 {
204  if ( nusize[0] < 1 ) nusize[0] = x;
205  if ( nusize[1] < 1 ) nusize[1] = y;
206  if ( nusize[2] < 1 ) nusize[2] = z;
207 
208  long i, j, xx, yy, zz, nn;
209  long oldx, oldy, oldz;
210  long elementsize = c*data_type_size();
211  long nualloc = (long) nusize.volume()*n*elementsize;
212  long fomsize = (long) nusize.volume()*n*sizeof(float);
213  unsigned char* nudata = new unsigned char[nualloc];
214  float* fom = NULL;
215  float* nufom = NULL;
216  if ( next ) {
217  fom = (float *) next->data_pointer();
218  nufom = new float[fomsize];
219  for ( i=0; i<fomsize; i++ ) nufom[i] = 0;
220  }
221 
222  if ( verbose & VERB_FULL ) {
223  cout << "Resizing with wrapping:" << endl;
224  cout << "Shift: " << translate << endl;
225  cout << "New size: " << nusize << endl;
226  cout << endl;
227  }
228 
229  for ( i=nn=0; nn<n; nn++ ) {
230 // image[nn].origin(image[nn].origin() + translate);
231  image[nn].origin(image[nn].origin() + translate);
232 // if ( verbose & VERB_PROCESS )
233 // cout << "Image " << nn+1 << " new origin: " << image[nn].origin() << endl;
234  for ( zz=0; zz<nusize[2]; zz++ ) {
235  oldz = (long)zz - translate[2];
236  while ( oldz < 0 ) oldz += z;
237  while ( oldz >= z ) oldz -= z;
238  for ( yy=0; yy<nusize[1]; yy++ ) {
239  oldy = (long)yy - translate[1];
240  while ( oldy < 0 ) oldy += y;
241  while ( oldy >= y ) oldy -= y;
242  for ( xx=0; xx<nusize[0]; xx++, i++ ) {
243  oldx = (long)xx - translate[0];
244  while ( oldx < 0 ) oldx += x;
245  while ( oldx >= x ) oldx -= x;
246  j = index(oldx, oldy, oldz, nn);
247  memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
248  if ( fom ) nufom[i] = fom[j];
249  }
250  }
251  }
252  }
253 
254 // if ( verbose & VERB_PROCESS )
255 // cout << endl;
256 
257  size(nusize);
258  data_assign(nudata);
259 
260  if ( fom ) {
261  next->size(nusize);
262  next->data_assign((unsigned char*) nufom);
263  }
264 
265  statistics();
266 
267  return 0;
268 }
269 
281 {
282  if ( !data_pointer() ) return NULL;
283 
284  if ( nusize[0] < 1 ) nusize[0] = x;
285  if ( nusize[1] < 1 ) nusize[1] = y;
286  if ( nusize[2] < 1 ) nusize[2] = z;
287 
288  long i, j, cc, xx, yy, zz, nn;
289  long oldx, oldy, oldz;
290 
291  if ( verbose & VERB_PROCESS ) {
292  cout << "Resizing with wrapping:" << endl;
293  cout << "Shift: " << translate << endl;
294  cout << "New size: " << nusize << endl;
295  cout << endl;
296  } else if ( verbose & VERB_LABEL )
297  cout << "Resizing" << endl << endl;
298 
299  Bimage* pnu = copy_header();
300  pnu->size(nusize);
301  pnu->page_size(nusize);
302  pnu->data_alloc();
303 
304  for ( i=nn=0; nn<n; nn++ ) {
305  pnu->image[nn].origin(image[nn].origin() + translate);
306  if ( verbose & VERB_PROCESS )
307  cout << "Image " << nn+1 << " new origin: " << pnu->image[nn].origin() << endl;
308  for ( zz=0; zz<pnu->sizeZ(); zz++ ) {
309  oldz = (long)zz - translate[2];
310  while ( oldz < 0 ) oldz += z;
311  while ( oldz >= z ) oldz -= z;
312  for ( yy=0; yy<pnu->sizeY(); yy++ ) {
313  oldy = (long)yy - translate[1];
314  while ( oldy < 0 ) oldy += y;
315  while ( oldy >= y ) oldy -= y;
316  for ( xx=0; xx<pnu->sizeX(); xx++ ) {
317  oldx = (long)xx - translate[0];
318  while ( oldx < 0 ) oldx += x;
319  while ( oldx >= x ) oldx -= x;
320  j = index(0, oldx, oldy, oldz, nn);
321  for ( cc=0; cc<c; cc++, i++, j++ ) pnu->set(i, (*this)[j]);
322  }
323  }
324  }
325  }
326 
327  if ( verbose & VERB_PROCESS )
328  cout << endl;
329 
330  if ( pnu->statistics() )
331  cerr << tab << "in Bimage::resize" << endl;
332 
333  return pnu;
334 }
335 
347 int Bimage::pad(long sz, int fill_type, double fill)
348 {
349  Vector3<long> nusize(sz,sz,sz);
350  if ( x < 2 ) nusize[0] = 1;
351  if ( y < 2 ) nusize[1] = 1;
352  if ( z < 2 ) nusize[2] = 1;
353 
354  Vector3<long> translate;
355 
356  return resize(nusize, translate, fill_type, fill);
357 }
358 
370 int Bimage::pad(Vector3<long> sz, int fill_type, double fill)
371 {
372  Vector3<long> translate;
373 
374  return resize(sz, translate, fill_type, fill);
375 }
376 
388 Bimage* Bimage::pad_copy(long sz, int fill_type, double fill)
389 {
390  Vector3<long> nusize(sz,sz,sz);
391  if ( x < 2 ) nusize[0] = 1;
392  if ( y < 2 ) nusize[1] = 1;
393  if ( z < 2 ) nusize[2] = 1;
394 
395  Vector3<long> translate;
396 
397  return resize_copy(nusize, translate, fill_type, fill);
398 }
399 
411 Bimage* Bimage::pad_copy(Vector3<long> sz, int fill_type, double fill)
412 {
413  Vector3<long> translate;
414 
415  return resize_copy(sz, translate, fill_type, fill);
416 }
417 
433 {
434  if ( !data_pointer() ) return -1;
435 
436  if ( translate[0] == 0 && translate[1] == 0 && translate[2] == 0 &&
437  nusize[0] == x && nusize[1] == y && nusize[2] == z )
438  return 0;
439 
440  long i, j, cc, xx, yy, zz, nn;
441  long nux, nuy, nuz;
442 
443  if ( verbose & VERB_PROCESS ) {
444  cout << "Shrinking and wrapping:" << endl;
445  cout << "New size: " << nusize << endl;
446  cout << "New origin: " << translate << endl << endl;
447  } else if ( verbose & VERB_LABEL )
448  cout << "Shrinking and wrapping" << endl << endl;
449 
450  DataType olddatatype = data_type();
451 
452  long nudatasize = nusize[0]*nusize[1]*nusize[2]*n*c;
453  float* nudata = new float[nudatasize];
454  for ( i=0; i<nudatasize; i++ ) nudata[i] = 0;
455 
456  for ( i=nn=0; nn<n; nn++ ) {
457  for ( zz=0; zz<z; zz++ ) {
458  nuz = zz + translate[2];
459  if ( nuz < 0 ) nuz += nusize[2];
460  if ( nuz >= nusize[2] ) nuz -= nusize[2];
461  for ( yy=0; yy<y; yy++ ) {
462  nuy = yy + translate[1];
463  if ( nuy < 0 ) nuy += nusize[1];
464  if ( nuy >= nusize[1] ) nuy -= nusize[1];
465  for ( xx=0; xx<x; xx++ ) {
466  nux = xx + translate[0];
467  if ( nux < 0 ) nux += nusize[0];
468  if ( nux >= nusize[0] ) nux -= nusize[0];
469  j = (((nn*nusize[2] + nuz)*nusize[1] + nuy)*nusize[0] + nux)*c;
470  for ( cc=0; cc<c; cc++, i++, j++ )
471  nudata[j] += (*this)[i];
472  }
473  }
474  }
475  }
476 
477  size(nusize);
478  page_size(nusize);
479  data_type(Float);
480  data_assign((unsigned char *) nudata);
481 
482  if ( statistics() )
483  cerr << tab << "in Bimage::shrink_wrap" << endl;
484 
485  change_type(olddatatype);
486 
487  return 0;
488 }
489 
500 {
501  if ( !data_pointer() ) return -1;
502 
503  Vector3<long> nusize = size()*scale;
504 
505  long i, j, xx, yy, zz, nn;
506  long oldx, oldy, oldz;
507  long elementsize = c*data_type_size();
508 
509  if ( verbose ) {
510  cout << "Enlarging:" << endl;
511  cout << "Scale: " << scale << endl;
512  cout << "New size: " << nusize << endl << endl;
513  }
514 
515  long nudatasize = nusize[0]*nusize[1]*nusize[2]*n*elementsize;
516  char* nudata = new char[nudatasize];
517 
518  for ( i=nn=0; nn<n; nn++ ) {
519  image[nn].origin(image[nn].origin()*scale);
520  for ( zz=0; zz<nusize[2]; zz++ ) {
521  oldz = zz/scale[2];
522  for ( yy=0; yy<nusize[1]; yy++ ) {
523  oldy = yy/scale[1];
524  for ( xx=0; xx<nusize[0]; xx++, i++ ) {
525  oldx = xx/scale[0];
526  j = index(oldx, oldy, oldz, nn);
527  memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
528  }
529  }
530  }
531  }
532 
533  size(nusize);
534  page_size(nusize);
535  data_assign((unsigned char *) nudata);
536 
537  if ( statistics() )
538  cerr << tab << "in Bimage::enlarge" << endl;
539 
540  return 0;
541 }
542 
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bimage::resize_wrap_copy
Bimage * resize_wrap_copy(Vector3< long > nusize, Vector3< long > translate)
Resizes with wrapping without interpolation or rescaling.
Definition: Bimage_resize.cpp:280
Bimage::shrink_wrap
int shrink_wrap(Vector3< long > nusize, Vector3< long > translate)
Shrinks an image to a new size with wrapping of the excluded edges.
Definition: Bimage_resize.cpp:432
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::resize_copy
Bimage * resize_copy(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:124
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:70
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::enlarge
int enlarge(Vector3< long > scale)
Enlarges an image by an inetger scale.
Definition: Bimage_resize.cpp:499
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::resize_wrap
int resize_wrap(Vector3< long > nusize, Vector3< long > translate)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:202
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage.h
Header file for image class.
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::pad_copy
Bimage * pad_copy(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:388
TypePointer
Definition: Bimage.h:69
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::fill_value
TypePointer fill_value(double v)
Definition: Bimage.cpp:1089
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Matrix.h
Generalized matrix class.
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::pad
int pad(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:347
FILL_MIN
#define FILL_MIN
Definition: utilities.h:179
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
FILL_MAX
#define FILL_MAX
Definition: utilities.h:180
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
verbose
int verbose