| Bsoft 2.0.5 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
28 int Bimage::resize(Vector3<long> nusize, Vector3<long> translate,
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 image[nn].origin(image[nn].origin() + translate);
72 // if ( verbose & VERB_PROCESS )
73 // cout << "Image " << nn+1 << " new origin: " << image[nn].origin() << endl;
74 if ( fill_type == FILL_BACKGROUND ) {
75 // fill = image[nn].background();
76 fill = background(nn);
77 // if ( verbose & VERB_PROCESS )
78 // cout << "Image " << nn+1 << " fill value: " << fill << endl;
79 }
80 fp = fill_value(fill);
81 for ( zz=0; zz<nusize[2]; zz++ ) {
82 oldz = (long)zz - translate[2];
83 for ( yy=0; yy<nusize[1]; yy++ ) {
84 oldy = (long)yy - translate[1];
85 for ( xx=0; xx<nusize[0]; xx++, i++ ) {
86 oldx = (long)xx - translate[0];
87 if ( oldx < 0 || oldx >= x || oldy < 0 ||
88 oldy >= y || oldz < 0 || oldz >= z ) {
89 memcpy(nudata+i*elementsize, fp.uc, elementsize);
90 } else {
91 j = index(oldx, oldy, oldz, nn);
92 memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
93 if ( fom ) nufom[i] = fom[j];
94 }
95 }
96 }
97 }
98 delete[] fp.uc;
99 }
100
101 // if ( verbose & VERB_PROCESS )
102 // cout << endl;
103
104 size(nusize);
105 data_assign(nudata);
106
107 if ( fom ) next->data_assign((unsigned char*) nufom);
108
109 statistics();
110
111 return 0;
112 }
113
126 Bimage* Bimage::resize_copy(Vector3<long> nusize, Vector3<long> translate,
127 int fill_type, double fill)
128 {
129 if ( !data_pointer() ) return NULL;
130
131 if ( nusize[0] < 1 ) nusize[0] = x;
132 if ( nusize[1] < 1 ) nusize[1] = y;
133 if ( nusize[2] < 1 ) nusize[2] = z;
134
135 long i, j, cc, xx, yy, zz, nn;
136 long oldx, oldy, oldz;
137
138 if ( fill_type == FILL_AVERAGE ) fill = avg;
139 if ( fill_type == FILL_MIN ) fill = min;
140 if ( fill_type == FILL_MAX ) fill = max;
141
142 if ( verbose & VERB_PROCESS ) {
143 cout << "Resizing:" << endl;
144 cout << "Shift: " << translate << endl;
145 cout << "New size: " << nusize << endl;
146 if ( fill_type != FILL_BACKGROUND )
147 cout << "Fill value: " << fill << endl;
148 cout << endl;
149 } else if ( verbose & VERB_LABEL )
150 cout << "Resizing" << endl << endl;
151
152 Bimage* pnu = copy_header();
153 pnu->size(nusize);
154 pnu->page_size(nusize);
155 pnu->data_alloc();
156
157 for ( i=nn=0; nn<n; nn++ ) {
158 pnu->image[nn].origin(image[nn].origin() + translate);
159 pnu->image[nn].origin(image[nn].origin() + translate);
160 if ( verbose & VERB_PROCESS )
161 cout << "Image " << nn+1 << " new origin: " << pnu->image[nn].origin() << endl;
162 if ( fill_type == FILL_BACKGROUND ) {
163 // fill = image[nn].background();
164 fill = background(nn);
165 if ( verbose & VERB_PROCESS )
166 cout << "Image " << nn+1 << " fill value: " << fill << endl;
167 }
168 for ( zz=0; zz<pnu->sizeZ(); zz++ ) {
169 oldz = (long)zz - translate[2];
170 for ( yy=0; yy<pnu->sizeY(); yy++ ) {
171 oldy = (long)yy - translate[1];
172 for ( xx=0; xx<pnu->sizeX(); xx++ ) {
173 oldx = (long)xx - translate[0];
174 if ( oldx < 0 || oldx >= x || oldy < 0 ||
175 oldy >= y || oldz < 0 || oldz >= z ) {
176 for ( cc=0; cc<c; cc++, i++ ) pnu->set(i, fill);
177 } else {
178 j = index(0, oldx, oldy, oldz, nn);
179 for ( cc=0; cc<c; cc++, i++, j++ ) pnu->set(i, (*this)[j]);
180 }
181 }
182 }
183 }
184 }
185
186 if ( verbose & VERB_PROCESS )
187 cout << endl;
188
189 if ( pnu->statistics() )
190 cerr << tab << "in Bimage::resize" << endl;
191
192 return pnu;
193 }
194
205 int Bimage::resize_wrap(Vector3<long> nusize, Vector3<long> translate)
206 {
207 if ( nusize[0] < 1 ) nusize[0] = x;
208 if ( nusize[1] < 1 ) nusize[1] = y;
209 if ( nusize[2] < 1 ) nusize[2] = z;
210
211 long i, j, xx, yy, zz, nn;
212 long oldx, oldy, oldz;
213 long elementsize = c*data_type_size();
214 long nualloc = (long) nusize.volume()*n*elementsize;
215 long fomsize = (long) nusize.volume()*n*sizeof(float);
216 unsigned char* nudata = new unsigned char[nualloc];
217 float* fom = NULL;
218 float* nufom = NULL;
219 if ( next ) {
220 fom = (float *) next->data_pointer();
221 nufom = new float[fomsize];
222 for ( i=0; i<fomsize; i++ ) nufom[i] = 0;
223 }
224
225 if ( verbose & VERB_FULL ) {
226 cout << "Resizing with wrapping:" << endl;
227 cout << "Shift: " << translate << endl;
228 cout << "New size: " << nusize << endl;
229 cout << endl;
230 }
231
232 for ( i=nn=0; nn<n; nn++ ) {
233 // image[nn].origin(image[nn].origin() + translate);
234 image[nn].origin(image[nn].origin() + translate);
235 // if ( verbose & VERB_PROCESS )
236 // cout << "Image " << nn+1 << " new origin: " << image[nn].origin() << endl;
237 for ( zz=0; zz<nusize[2]; zz++ ) {
238 oldz = (long)zz - translate[2];
239 while ( oldz < 0 ) oldz += z;
240 while ( oldz >= z ) oldz -= z;
241 for ( yy=0; yy<nusize[1]; yy++ ) {
242 oldy = (long)yy - translate[1];
243 while ( oldy < 0 ) oldy += y;
244 while ( oldy >= y ) oldy -= y;
245 for ( xx=0; xx<nusize[0]; xx++, i++ ) {
246 oldx = (long)xx - translate[0];
247 while ( oldx < 0 ) oldx += x;
248 while ( oldx >= x ) oldx -= x;
249 j = index(oldx, oldy, oldz, nn);
250 memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
251 if ( fom ) nufom[i] = fom[j];
252 }
253 }
254 }
255 }
256
257 // if ( verbose & VERB_PROCESS )
258 // cout << endl;
259
260 size(nusize);
261 data_assign(nudata);
262
263 if ( fom ) {
264 next->size(nusize);
265 next->data_assign((unsigned char*) nufom);
266 }
267
268 statistics();
269
270 return 0;
271 }
272
283 Bimage* Bimage::resize_wrap_copy(Vector3<long> nusize, Vector3<long> translate)
284 {
285 if ( !data_pointer() ) return NULL;
286
287 if ( nusize[0] < 1 ) nusize[0] = x;
288 if ( nusize[1] < 1 ) nusize[1] = y;
289 if ( nusize[2] < 1 ) nusize[2] = z;
290
291 long i, j, cc, xx, yy, zz, nn;
292 long oldx, oldy, oldz;
293
294 if ( verbose & VERB_PROCESS ) {
295 cout << "Resizing with wrapping:" << endl;
296 cout << "Shift: " << translate << endl;
297 cout << "New size: " << nusize << endl;
298 cout << endl;
299 } else if ( verbose & VERB_LABEL )
300 cout << "Resizing" << endl << endl;
301
302 Bimage* pnu = copy_header();
303 pnu->size(nusize);
304 pnu->page_size(nusize);
305 pnu->data_alloc();
306
307 for ( i=nn=0; nn<n; nn++ ) {
308 // pnu->image[nn].origin(image[nn].origin() + translate);
309 pnu->image[nn].origin(image[nn].origin() + translate);
310 if ( verbose & VERB_PROCESS )
311 cout << "Image " << nn+1 << " new origin: " << pnu->image[nn].origin() << endl;
312 for ( zz=0; zz<pnu->sizeZ(); zz++ ) {
313 oldz = (long)zz - translate[2];
314 while ( oldz < 0 ) oldz += z;
315 while ( oldz >= z ) oldz -= z;
316 for ( yy=0; yy<pnu->sizeY(); yy++ ) {
317 oldy = (long)yy - translate[1];
318 while ( oldy < 0 ) oldy += y;
319 while ( oldy >= y ) oldy -= y;
320 for ( xx=0; xx<pnu->sizeX(); xx++ ) {
321 oldx = (long)xx - translate[0];
322 while ( oldx < 0 ) oldx += x;
323 while ( oldx >= x ) oldx -= x;
324 j = index(0, oldx, oldy, oldz, nn);
325 for ( cc=0; cc<c; cc++, i++, j++ ) pnu->set(i, (*this)[j]);
326 }
327 }
328 }
329 }
330
331 if ( verbose & VERB_PROCESS )
332 cout << endl;
333
334 if ( pnu->statistics() )
335 cerr << tab << "in Bimage::resize" << endl;
336
337 return pnu;
338 }
339
351 int Bimage::pad(long sz, int fill_type, double fill)
352 {
353 Vector3<long> nusize(sz,sz,sz);
354 if ( x < 2 ) nusize[0] = 1;
355 if ( y < 2 ) nusize[1] = 1;
356 if ( z < 2 ) nusize[2] = 1;
357
358 Vector3<long> translate;
359
360 return resize(nusize, translate, fill_type, fill);
361 }
362
374 int Bimage::pad(Vector3<long> sz, int fill_type, double fill)
375 {
376 Vector3<long> translate;
377
378 return resize(sz, translate, fill_type, fill);
379 }
380
392 Bimage* Bimage::pad_copy(long sz, int fill_type, double fill)
393 {
394 Vector3<long> nusize(sz,sz,sz);
395 if ( x < 2 ) nusize[0] = 1;
396 if ( y < 2 ) nusize[1] = 1;
397 if ( z < 2 ) nusize[2] = 1;
398
399 Vector3<long> translate;
400
401 return resize_copy(nusize, translate, fill_type, fill);
402 }
403
415 Bimage* Bimage::pad_copy(Vector3<long> sz, int fill_type, double fill)
416 {
417 Vector3<long> translate;
418
419 return resize_copy(sz, translate, fill_type, fill);
420 }
421
436 int Bimage::shrink_wrap(Vector3<long> nusize, Vector3<long> translate)
437 {
438 if ( !data_pointer() ) return -1;
439
440 if ( translate[0] == 0 && translate[1] == 0 && translate[2] == 0 &&
441 nusize[0] == x && nusize[1] == y && nusize[2] == z )
442 return 0;
443
444 long i, j, cc, xx, yy, zz, nn;
445 long nux, nuy, nuz;
446
447 if ( verbose & VERB_PROCESS ) {
448 cout << "Shrinking and wrapping:" << endl;
449 cout << "New size: " << nusize << endl;
450 cout << "New origin: " << translate << endl << endl;
451 } else if ( verbose & VERB_LABEL )
452 cout << "Shrinking and wrapping" << endl << endl;
453
454 DataType olddatatype = data_type();
455
456 long nudatasize = nusize[0]*nusize[1]*nusize[2]*n*c;
457 float* nudata = new float[nudatasize];
458 for ( i=0; i<nudatasize; i++ ) nudata[i] = 0;
459
460 for ( i=nn=0; nn<n; nn++ ) {
461 for ( zz=0; zz<z; zz++ ) {
462 nuz = zz + translate[2];
463 if ( nuz < 0 ) nuz += nusize[2];
464 if ( nuz >= nusize[2] ) nuz -= nusize[2];
465 for ( yy=0; yy<y; yy++ ) {
466 nuy = yy + translate[1];
467 if ( nuy < 0 ) nuy += nusize[1];
468 if ( nuy >= nusize[1] ) nuy -= nusize[1];
469 for ( xx=0; xx<x; xx++ ) {
470 nux = xx + translate[0];
471 if ( nux < 0 ) nux += nusize[0];
472 if ( nux >= nusize[0] ) nux -= nusize[0];
473 j = (((nn*nusize[2] + nuz)*nusize[1] + nuy)*nusize[0] + nux)*c;
474 for ( cc=0; cc<c; cc++, i++, j++ )
475 nudata[j] += (*this)[i];
476 }
477 }
478 }
479 }
480
481 size(nusize);
482 page_size(nusize);
483 data_type(Float);
484 data_assign((unsigned char *) nudata);
485
486 if ( statistics() )
487 cerr << tab << "in Bimage::shrink_wrap" << endl;
488
489 change_type(olddatatype);
490
491 return 0;
492 }
493
503 int Bimage::enlarge(Vector3<long> scale)
504 {
505 if ( !data_pointer() ) return -1;
506
507 Vector3<long> nusize = size()*scale;
508
509 long i, j, xx, yy, zz, nn;
510 long oldx, oldy, oldz;
511 long elementsize = c*data_type_size();
512
513 if ( verbose ) {
514 cout << "Enlarging:" << endl;
515 cout << "Scale: " << scale << endl;
516 cout << "New size: " << nusize << endl << endl;
517 }
518
519 long nudatasize = nusize[0]*nusize[1]*nusize[2]*n*elementsize;
520 char* nudata = new char[nudatasize];
521
522 for ( i=nn=0; nn<n; nn++ ) {
523 image[nn].origin(image[nn].origin()*scale);
524 for ( zz=0; zz<nusize[2]; zz++ ) {
525 oldz = zz/scale[2];
526 for ( yy=0; yy<nusize[1]; yy++ ) {
527 oldy = yy/scale[1];
528 for ( xx=0; xx<nusize[0]; xx++, i++ ) {
529 oldx = xx/scale[0];
530 j = index(oldx, oldy, oldz, nn);
531 memcpy(nudata+i*elementsize, d.uc+j*elementsize, elementsize);
532 }
533 }
534 }
535 }
536
537 size(nusize);
538 page_size(nusize);
539 data_assign((unsigned char *) nudata);
540
541 if ( statistics() )
542 cerr << tab << "in Bimage::enlarge" << endl;
543
544 return 0;
545 }
546
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:650
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bimage.h
Header file for image class.
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1585
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:383
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:173
Matrix.h
Generalized matrix class.
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:69
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::resize_wrap
int resize_wrap(Vector3< long > nusize, Vector3< long > translate)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:205
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::fill_value
TypePointer fill_value(double v)
Definition: Bimage.cpp:1067
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:852
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
FILL_MAX
#define FILL_MAX
Definition: utilities.h:176
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:126
TypePointer
Definition: Bimage.h:68
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Bimage::next
Bimage * next
Definition: Bimage.h:165
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
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:351
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< long >
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:436
FILL_MIN
#define FILL_MIN
Definition: utilities.h:175
Bimage::enlarge
int enlarge(Vector3< long > scale)
Enlarges an image by an inetger scale.
Definition: Bimage_resize.cpp:503
verbose
int verbose
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
Bimage::fill
void fill(double v)
Definition: Bimage.h:698
Vector3::volume
double volume()
Definition: Vector3.h:235
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
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:283
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
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:392
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563