Bsoft  2.1.0
Bernard's software package
Bimage.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "utilities.h"
11 
12 #include <iostream>
13 using namespace std;
14 
15 // Definition of the global variables
16 extern int verbose; // Level of output to the screen
17 
25 {
26  min = max = avg = std = 0;
27  bkg = 0;
28  ux = uy = uz = 1;
29  ox = oy = oz = 0;
30  vx = vy = vz = angle = 0;
31  mag = 1;
32  fom = 1;
33  sel = 1;
34 // cout << "sub-image initialized" << endl;
35 }
36 
44 {
45  if ( verbose & VERB_DEBUG )
46  cout << "DEBUG Bimage: Bimage size = " << sizeof(Bimage) << endl;
47 
48  initialize();
49 }
50 
58 {
59 // cout << "Copy image" << endl;
60  internal_copy(p);
61 }
62 
69 Bimage::Bimage(Bstring& fn, int readdata, int img_select)
70 {
71  if ( verbose & VERB_DEBUG )
72  cout << "DEBUG Bimage: Bimage size = " << sizeof(Bimage) << endl;
73 
74  initialize();
75 
76 }
77 
91 Bimage::Bimage(DataType type, CompoundType ctype, long nx, long ny,
92  long nz, long nn)
93 {
94  initialize(ctype, nx, ny, nz, nn);
95 
96  data_type(type);
97 
98  data_alloc_and_clear();
99 }
100 
113 {
114  initialize(ctype, size[0], size[1], size[2], nn);
115 
116  data_type(type);
117 
118  data_alloc_and_clear();
119 }
120 
132 Bimage::Bimage(DataType type, CompoundType ctype, vector<long> size, long nn)
133 {
134  initialize(ctype, size[0], size[1], size[2], nn);
135 
136  data_type(type);
137 
138  data_alloc_and_clear();
139 }
140 
154 Bimage::Bimage(DataType type, long nc, long nx, long ny,
155  long nz, long nn)
156 {
157  initialize(nc, nx, ny, nz, nn);
158 
159  data_type(type);
160 
161  data_alloc_and_clear();
162 }
163 
175 Bimage::Bimage(DataType type, long nc, Vector3<long> size, long nn)
176 {
177  initialize(nc, size[0], size[1], size[2], nn);
178 
179  data_type(type);
180 
181  data_alloc_and_clear();
182 }
183 
194 Bimage::Bimage(Matrix& mat, long scale)
195 {
196  if ( scale < 1 ) scale = 1;
197  if ( scale > 10 ) scale = 10;
198 
199  initialize(1, scale*mat.columns(), scale*mat.rows(), 1, 1);
200 
201  data_type(Float);
202 
203  data_alloc_and_clear();
204 
205  long i, j, k, xx, yy;
206 
207  for ( k=yy=0; yy<y; ++yy ) {
208  i = yy/scale;
209  for ( xx=0; xx<x; ++xx, ++k ) {
210  j = xx/scale;
211  set(k, mat[i][j]);
212  }
213  }
214 }
215 
223 {
224  if ( next ) {
225  try { delete next; }
226  catch (...) { cerr << "Failed to deallocate next image!" << endl; }
227  }
228  next = NULL;
229 
230  // Strings
231  id = 0;
232 
233 // metadata.erase();
234 
235  if ( image ) {
236  try { delete[] image; }
237  catch (...) { cerr << "Failed to deallocate sub-images!" << endl; }
238  }
239  image = NULL;
240 
241  if ( d.uc ) {
242  try { delete[] d.uc; }
243  catch (...) { cerr << "Failed to deallocate data block!" << endl; }
244  }
245  d.uc = NULL;
246 }
247 
248 /*
249 @brief Image initialization function.
250 
251  All properties are set to default values.
252 
253 **/
254 void Bimage::initialize()
255 {
256  // Set parameter defaults
257  x = y = z = c = n = 0;
258  px = py = pz = 0;
259  sn = sz = 0;
260  ss = 1;
261 
262  data_type(UCharacter);
263  compoundtype = TSimple;
264  fouriertype = NoTransform;
265 
266  offset = 0;
267 
268  min = max = avg = std = 0;
269  smin = smax = 0;
270 
271  ucell = UnitCell();
272 
273  // Strings
274  id = 0;
275 
276  // Metadata
277  metadata = JSvalue(JSobject);
278 // metadata["version"] = BVERSION; // Version number
279 // metadata["time"] = time(NULL);
280 // cout << "metadata type = " << metadata.type() << endl;
281 // cout << metadata << endl;
282  next = NULL;
283  image = NULL;
284  d.uc = NULL;
285 
286  data_size();
287 
288  if ( verbose & VERB_DEBUG )
289  cout << "DEBUG Bimage::initialize: Image setup done" << endl;
290 }
291 
292 void Bimage::initialize(long nc, long nx,
293  long ny, long nz, long nn)
294 {
295  initialize();
296 
297  c = nc;
298  x = nx;
299  y = ny;
300  z = nz;
301 
302  if ( c > 1 ) compoundtype = TMulti;
303 
304  images(nn);
305 }
306 
307 void Bimage::initialize(CompoundType ctype, long nx,
308  long ny, long nz, long nn)
309 {
310  long nc;
311 
312  switch ( ctype ) {
313  case TSimple: nc = 1; break;
314  case TComplex: nc = 2; break;
315  case TVector3: nc = 3; break;
316  case TView: nc = 4; break;
317  case TRGB: nc = 3; break;
318  case TRGBA: nc = 4; break;
319  case TCMYK: nc = 4; break;
320  default: nc = 1;
321  }
322 
323  initialize(nc, nx, ny, nz, nn);
324 
325  compoundtype = ctype;
326 }
327 
328 void Bimage::images(long nn) {
329  if ( nn <= 0 )
330  cerr << "Error in Bimage::images: The number of images must be greater than 1" << endl;
331  if ( nn != n && image ) { delete[] image; image = NULL; }
332  if ( !image ) image = new Bsub_image[nn];
333  n = nn;
334 }
335 
336 /*
337 void Bimage::images(long nn) {
338  if ( nn <= 0 )
339  cerr << "Error in Bimage::images: The number of images must be greater than 1" << endl;
340  if ( nn == n ) return;
341  JSvalue arr(JSarray);
342  JSvalue img(JSobject);
343  vector<double> sam{1,1,1}, ori{double(x/2),double(y/2),double(z/2)};
344  if ( metadata.exists("image") )
345  arr = metadata["image"].array();
346  if ( nn > n ) {
347  for ( long i=n+1; i<=nn; ++i ) {
348  img["number"] = i;
349  img["sampling"] = sam;
350  img["origin"] = ori;
351  img["magnification"] = 1.0;
352  img["background"] = 0.0;
353  img["fom"] = 0.0;
354  img["select"] = 1;
355  arr.push_back(img);
356  }
357  metadata["image"] = arr;
358  } else {
359  arr.erase(arr.begin()+nn, arr.end());
360  }
361  if ( nn != n && image ) { delete[] image; image = NULL; }
362  if ( !image ) image = new Bsub_image[nn];
363  n = nn;
364 }
365 */
366 void Bimage::internal_copy(const Bimage& p)
367 {
368  offset = p.offset;
369 
370  data_type(p.datatype);
371  compoundtype = p.compoundtype;
372  fouriertype = p.fouriertype;
373  c = p.c;
374  x = p.x;
375  y = p.y;
376  z = p.z;
377 // n = p.n;
378  px = p.px;
379  py = p.py;
380  pz = p.pz;
381 
382  min = p.min;
383  max = p.max;
384  avg = p.avg;
385  std = p.std;
386 
387  ucell = p.ucell;
388 
389  // Strings
390  id = p.id;
391 
392  next = NULL;
393 
394  images(p.n);
395 // cout << "image pointer:" << image << "." << endl;
396  if ( p.image )
397  for ( long j=0; j<n; j++ ) image[j] = p.image[j];
398 
399  // Metadata
400  metadata = p.metadata;
401 
402  data_size();
403  long allocsize = alloc_size();
404 
405  if ( d.uc ) delete[] d.uc;
406  d.uc = NULL;
407  if ( p.d.uc ) {
408  d.uc = new unsigned char[allocsize];
409  for ( long j=0; j<datasize; j++ ) set(j, p[j]);
410  }
411 // cout << "data pointer:" << &d.uc << "." << endl;
412 
413 }
414 
420 {
421  long j;
422 
423  check_sampling();
424 
425  Vector3<double> u(image->sampling());
426 
427  if ( fouriertype > Standard ) fouriertype = Standard;
428 
429  if ( c < 1 ) c = 1;
430  if ( px < 1 ) px = x;
431  if ( py < 1 ) py = y;
432  if ( pz < 1 ) pz = z;
433  if ( px > 2*x ) px = x;
434  if ( py > 2*y ) py = y;
435  if ( pz > 2*z ) pz = z;
436  if ( n < 1 ) n = 1;
437 // if ( ux <= 1e-6 ) ux = 1;
438 // if ( uy <= 1e-6 ) uy = 1;
439 // if ( uz <= 1e-6 ) uz = 1;
440 // if ( ux > 1e6 ) ux = 1;
441 // if ( uy > 1e6 ) uy = 1;
442 // if ( uz > 1e6 ) uz = 1;
443 // if ( fabs(ux - uy) > 100 ) ux = uy = uz = 1;
444  if ( !compoundtype ) compoundtype = TSimple;
445 
446  switch ( compoundtype ) {
447  case TSimple: j = 1;
448  case TComplex: j = 2;
449  case TVector3: j = 3;
450  case TView: j = 4;
451  case TRGB: j = 3;
452  case TRGBA: j = 4;
453  case TCMYK: j = 4;
454  default: j = c;
455  }
456 
457  if ( j != c ) {
458  cerr << "Error in Bimage::check: The compound type size (" << j << ") and number of channels (" << c << ") don't agree!" << endl;
459  bexit(-1);
460  }
461 
462  data_size();
463 
464  if ( verbose & VERB_DEBUG ) {
465  cout << "DEBUG Bimage::check: Data type=" << datatype << " Compound type=" << compoundtype << endl;
466  cout << "DEBUG Bimage::check: Images & channels: " << n << " " << c << endl;
467  cout << "DEBUG Bimage::check: Size: " << x << " " << y << " " << z << endl;
468  cout << "DEBUG Bimage::check: Page: " << px << " " << py << " " << pz << endl;
469  cout << "DEBUG Bimage::check: Units: " << u[0] << " " << u[1] << " " << u[2] << endl;
470  cout << "DEBUG Bimage::check: min=" << min << " max=" << max << " avg=" << avg << " std=" << std << endl;
471  }
472 
473  if ( !image ) {
474  cerr << "Error in Bimage::check: The sub-image list has not been allocated!" << endl;
475  bexit(-1);
476  }
477 
478  int check_stats = 0;
479 
480  if ( !isfinite(min) || !isfinite(max) || !isfinite(avg) || !isfinite(std) ) check_stats = 1;
481 
482  if ( (min>=max) || (std<=0) || (avg<min) || (avg>max) ) check_stats = 1;
483 
484  if ( min < dtmin ) check_stats = 1;
485  if ( max > dtmax ) check_stats = 1;
486 
487  if ( check_stats ) statistics();
488 
489  if ( smin == 0 && smax == 0 ) {
490  smin = min;
491  smax = max;
492  }
493 
494  if ( verbose & VERB_DEBUG ) {
495  cout << "DEBUG Bimage::check: Min, max, mean, stdev: " <<
496  min << " " << max << " " << avg << " " << std << endl;
497  cout << "DEBUG Bimage::check: Background:";
498 // for ( j=0; j<n; j++ ) cout << " " << image[j].background();
499 // cout << endl;
500  for ( j=0; j<n; j++ ) cout << " " << background(j);
501  cout << endl;
502  }
503 
504  View view;
505  for ( j=0; j<n; j++ ) {
506  if ( !isfinite(image[j].background()) ) image[j].background(0);
507  else if ( fabs((double)image[j].background()) < 1e-30 ) image[j].background(0);
508  if ( !isfinite(image[j].origin()[0]) || !isfinite(image[j].origin()[0])
509  | !isfinite(image[j].origin()[0]) ) image[j].origin(0,0,0);
510  view = image[j].view();
511  view.normalize();
512  image[j].view(view);
513  if ( image[j].magnification() <= 0 )
514  image[j].magnification(1);
515  }
516 
517  UnitCell uc = unit_cell();
518 
519  if ( verbose & VERB_DEBUG )
520  cout << "DEBUG Bimage::check: Unit cell: " << uc.a() << " " << uc.b() << " " << uc.c()
521  << " " << uc.alpha() << " " << uc.beta() << " " << uc.gamma() << endl;
522 
523 
524  if ( uc.a() < 1 ) uc.a(u[0]*x);
525  if ( uc.b() < 1 ) uc.b(u[1]*y);
526  if ( uc.c() < 1 ) uc.c(u[2]*z);
527  if ( uc.a() > 100*x*u[0] ) uc.a(u[0]*x);
528  if ( uc.b() > 100*y*u[1] ) uc.b(u[1]*y);
529  if ( uc.c() > 100*z*u[2] ) uc.c(u[2]*z);
530  if ( uc.alpha() > TWOPI || uc.beta() > TWOPI || uc.gamma() > TWOPI ) uc.degrees_to_radians();
531  if ( uc.alpha() < 0.001 || uc.alpha() > M_PI ) uc.alpha(M_PI_2);
532  if ( uc.beta() < 0.001 || uc.beta() > M_PI ) uc.beta(M_PI_2);
533  if ( uc.gamma() < 0.001 || uc.gamma() > M_PI ) uc.gamma(M_PI_2);
534 
535  unit_cell(uc);
536 
537  if ( verbose & VERB_DEBUG )
538  cout << "DEBUG Bimage::check: Unit cell: " << uc.a() << " " << uc.b() << " " << uc.c()
539  << " " << uc.alpha() << " " << uc.beta() << " " << uc.gamma() << endl;
540 /*
541  Bstring t;
542  if ( lbl.length() ) {
543  j = lbl.length() - 1;
544  if ( verbose & VERB_DEBUG )
545  cout << "DEBUG Bimage::check: lbl length=" << j << " last=" << lbl[j] << endl;
546 // if ( verbose & VERB_DEBUG )
547 // cout << "DEBUG Bimage::check: lbl=" << lbl << "/end/" << endl;
548  while ( j > 0 && isspace(lbl[j]) ) j--;
549  j++;
550  if ( j < lbl.length() ) lbl = lbl.left(j);
551  }
552 */
553  if ( verbose & VERB_DEBUG )
554  cout << "DEBUG Bimage::check: Finished checking parameters" << endl;
555 }
556 
567 {
568  if ( !p ) {
569  error_show("Both images must be defined", __FILE__, __LINE__);
570  return 0;
571  }
572 
573  if ( x != p->x || y != p->y || z != p->z || c != p->c || n != p->n ) {
574  cerr << "Error: Image dimensions not the same:" << endl;
575  cerr << "Image 1: c=" << c << " x=" << x << " y=" << y << " z=" << z << " n=" << n << endl;
576  cerr << "Image 2: c=" << p->c << " x=" << p->x << " y=" << p->y << " z=" << p->z << " n=" << p->n << endl;
577  return 0;
578  }
579 
580  return 1;
581 }
582 
593 {
594  if ( !p ) {
595  error_show("Both images must be defined", __FILE__, __LINE__);
596  return 0;
597  }
598 
599  if ( x != p->x || y != p->y || z != p->z || c != p->c ) {
600  cerr << "Error: Image dimensions not the same:" << endl;
601  cerr << "Image 1: c=" << c << " x=" << x << " y=" << y << " z=" << z << " n=" << n << endl;
602  cerr << "Image 2: c=" << p->c << " x=" << p->x << " y=" << p->y << " z=" << p->z << " n=" << p->n << endl;
603  return 0;
604  }
605 
606  return 1;
607 }
608 
617 {
618  Vector3<double> u;
619 
620  for ( long nn=0; nn<n; ++nn ) {
621  u = image[nn].sampling();
622  for ( long i=0; i<3; ++i )
623  if ( size()[i] < 2 ) u[i] = 1;
624  image[nn].sampling(u);
625  }
626 }
627 
635 void Bimage::check_resolution(double& resolution)
636 {
637  Vector3<double> u(image->sampling());
638  if ( x > 1 && resolution < 2*u[0] ) resolution = 2*u[0];
639  if ( y > 1 && resolution < 2*u[1] ) resolution = 2*u[1];
640  if ( z > 1 && resolution < 2*u[2] ) resolution = 2*u[2];
641  if ( resolution > real_size()[0]/2.0 ) resolution = real_size()[0]/2.0;
642 }
643 
654 {
655  int non(0);
656 
657  if ( c != p->c ) {
658 // cout << filename << " has an incompatible number of channels: " << c << endl;
659  cout << metadata["filename"] << " has an incompatible number of channels: " << c << endl;
660  non ++;
661  }
662 
663  if ( datatype != p->datatype ) {
664 // cout << filename << " has an incompatible data type: " << datatype << endl;
665  cout << metadata["filename"] << " has an incompatible data type: " << datatype << endl;
666  non ++;
667  }
668 
669  return (non==0);
670 }
671 
679 {
680  if ( n <= 0 ) {
681  cerr << "Error in Bimage::meta_data_update: The number of images must be greater than 0" << endl;
682  return;
683  }
684 
685  if ( verbose & VERB_DEBUG )
686  cout << "DEBUG Bimage::meta_data_update: Updating metadata" << endl;
687 
688  metadata["version"] = BVERSION; // Version number
689  metadata["time"] = time(NULL);
690  metadata["user"] = get_user_name();
691 
692  JSvalue arr(JSarray);
693 
694  if ( metadata.exists("image") )
695  arr = metadata["image"].array();
696 
697  for ( long i=0; i<n; ++i ) {
698  Bsub_image& si = image[i];
699  if ( i >= arr.size() )
700  arr.push_back(JSvalue(JSobject));
701  JSvalue& img = arr[i];
702  img["number"] = i+1;
703  img["sampling"] = si.sampling().array();
704  img["origin"] = si.origin().array();
705  img["view"] = si.view().array();
706 // img["magnification"] = si.magnification();
707  img["background"] = si.background();
708  img["average"] = si.average();
709  img["standard_deviation"] = si.standard_deviation();
710  img["minimum"] = si.minimum();
711  img["maximum"] = si.maximum();
712  img["fom"] = si.FOM();
713  img["select"] = si.select();
714  }
715 
716  metadata["image"] = arr;
717 }
718 
726 {
727  if ( verbose & VERB_DEBUG )
728  cout << "DEBUG update_from_meta_data: converting JSON header" << endl;
729 
730  vector<double> sam(3), ori(3), vw(4);
731  JSvalue arr(JSarray);
732 
733  if ( metadata.exists("image") )
734  arr = metadata["image"].array();
735 
736  for ( auto it = arr.begin(); it != arr.end(); ++it ) {
737  JSvalue& img = *it;
738  long i = img["number"].integer() - 1;
739  if ( i >= n ) break;
740  Bsub_image& si = image[i];
741  if ( img.exists("sampling") ) {
742  sam = img["sampling"].array_real();
743  si.sampling(sam);
744  }
745  if ( img.exists("origin") ) {
746  ori = img["origin"].array_real();
747  si.origin(ori);
748  }
749  if ( img.exists("view") ) {
750  vw = img["view"].array_real();
751  si.view(vw);
752  }
753  if ( img.exists("magnification") ) si.magnification(img["magnification"].real());
754  if ( img.exists("background") ) si.background(img["background"].real());
755  if ( img.exists("average") ) si.average(img["average"].real());
756  if ( img.exists("standard_deviation") ) si.standard_deviation(img["standard_deviation"].real());
757  if ( img.exists("minimum") ) si.minimum(img["minimum"].real());
758  if ( img.exists("maximum") ) si.maximum(img["maximum"].real());
759  if ( img.exists("fom") ) si.FOM(img["fom"].real());
760  if ( img.exists("select") ) si.select(img["select"].integer());
761  }
762 
763  if ( verbose & VERB_DEBUG )
764  cout << "DEBUG update_from_meta_data: converting done" << endl;
765 }
766 
772 {
773  JSvalue arr(JSarray);
774 
775  if ( metadata.exists("image") )
776  arr = metadata["image"].array();
777  else return;
778 
779  JSvalue img = arr[img_num];
780  JSvalue arrnu(JSarray);
781 
782  img["number"] = 1;
783 
784  arrnu.push_back(img);
785 
786  metadata["image"] = arrnu;
787 
788 // cout << "image number = " << img_num << tab << "new array = " << metadata["image"] << endl;
789 }
790 
799 unsigned char* Bimage::data_alloc(long nbytes)
800 {
801  if ( !data_size() ) {
802  cerr << "Error in Bimage::data_alloc: Data size is zero!" << endl;
803  cerr << "(c=" << c << " x=" << x << " y=" << y << " z=" << z << " n=" << n << ")" << endl;
804  exit(-1);
805  }
806 
807  long ds(datasize*data_type_size());
808  if ( datatype == Bit ) ds /= 8;
809 
810  if ( nbytes != ds ) {
811  cerr << "Error in Bimage::data_alloc: Data size is not equal to the allocation requested!" << endl;
812  cerr << "(" << ds << " != " << nbytes << ")" << endl;
813  exit(-1);
814  }
815 
816  if ( d.uc ) delete [] d.uc;
817 
818  d.uc = new unsigned char[nbytes];
819 
820  if ( !d.uc ) {
821  cerr << "Error: Image data not allocated! (" << nbytes << ")" << endl;
822  exit(-1);
823  }
824 
825 // for ( long j=0; j<nbytes; j++ ) d.uc[j] = 0;
826 
827  data_size();
828 
829  return d.uc;
830 }
831 
845 unsigned char* Bimage::data_alloc(DataType type, CompoundType ctype, long nx,
846  long ny, long nz, long nn)
847 {
848  if ( image ) {
849  if ( n != nn ) {
850  delete[] image;
851  image = NULL;
852  }
853  }
854 
855  data_type(type);
856 
857  c = compound_type_size();
858  x = nx; y = ny; z = nz;
859 
860  if ( datatype == Bit ) px = 8*((x-1)/8 + 1);
861 
862  images(nn);
863 
864  return data_alloc();
865 }
866 
875 unsigned char* Bimage::data_assign(unsigned char* nudata)
876 {
877  if ( !nudata ) {
878  cerr << "Error: Image data not assigned!" << endl;
879  exit(-1);
880  }
881 
882  if ( d.uc ) delete [] d.uc;
883 
884  d.uc = nudata;
885 
886  data_size();
887 
888  if ( verbose & VERB_DEBUG )
889  cout << "DEBUG Bimage::data_assign: datasize=" << datasize << endl;
890 
891  return d.uc;
892 }
893 
898 {
899  if ( d.uc ) delete [] d.uc;
900 
901  d.uc = NULL;
902 }
903 
904 
912 {
913 // cout << "Assign image" << endl;
914  internal_copy(p);
915 
916 // cout << "Copy image done" << endl;
917  return *this;
918 }
919 
928 double Bimage::operator[](long j) const
929 {
930  if ( j >= datasize ) return 0;
931 
932  switch ( datatype ) {
933  case Bit: return ((d.uc[j/8] << (j%8)) & 0x80) == 0x80;
934  case UCharacter: return d.uc[j];
935  case SCharacter: return d.sc[j];
936  case UShort: return d.us[j];
937  case Short: return d.ss[j];
938  case UInteger: return d.ui[j];
939  case Integer: return d.si[j];
940  case ULong: return d.ul[j];
941  case Long: return d.sl[j];
942  case Float: return d.f[j];
943  case Double: return d.d[j];
944  default: return 0;
945  }
946 }
947 
960 double Bimage::get(long nn, long xx, long yy, long zz, long cc)
961 {
962  if ( nn >= n ) return 0;
963  if ( xx >= x ) return 0;
964  if ( yy >= y ) return 0;
965  if ( zz >= z ) return 0;
966 
967  long j = (((nn*z + zz)*y + yy)*x + xx)*c + cc;
968 
969  return (*this)[j];
970 }
971 
982 double Bimage::get(long nn, Vector3<double> vox, long cc)
983 {
984  if ( nn >= n ) return 0;
985  if ( vox[0] < 0 || vox[0] > x-1 ) return 0;
986  if ( vox[1] < 0 || vox[1] > y-1 ) return 0;
987  if ( vox[2] < 0 || vox[2] > z-1 ) return 0;
988 
989  long j = (((nn*z + (long)(vox[2]+0.5))*y + (long)(vox[1]+0.5))*x + (long)(vox[0]+0.5))*c + cc;
990 
991  return (*this)[j];
992 }
993 
1003 vector<double> Bimage::values(long nn, Vector3<double> vox)
1004 {
1005  long i;
1006  vector<double> values(c,0);
1007 
1008  if ( nn >= n ) return values;
1009  if ( vox[0] < 0 || vox[0] > x-1 ) return values;
1010  if ( vox[1] < 0 || vox[1] > y-1 ) return values;
1011  if ( vox[2] < 0 || vox[2] > z-1 ) return values;
1012 
1013  long j = (((nn*z + (long)(vox[2]+0.5))*y + (long)(vox[1]+0.5))*x + (long)(vox[0]+0.5))*c;
1014  for ( i=0; i<c; i++, j++ ) values[i] = (*this)[j];
1015 
1016  return values;
1017 }
1018 
1028 {
1029  long i2 = 2*j;
1030  return Complex<double>((*this)[i2],(*this)[i2+1]);
1031 }
1032 
1042 {
1043  long i3 = 3*j;
1044  return Vector3<double>((*this)[i3],(*this)[i3+1],(*this)[i3+2]);
1045 }
1046 
1056 {
1057  long i3 = 3*j;
1058  return RGB<double>((*this)[i3],(*this)[i3+1],(*this)[i3+2]);
1059 }
1060 
1070 {
1071  long i4 = 4*j;
1072  return RGBA<double>((*this)[i4],(*this)[i4+1],(*this)[i4+2],(*this)[i4+3]);
1073 }
1074 
1084 {
1085  long i4 = 4*j;
1086  return CMYK<double>((*this)[i4],(*this)[i4+1],(*this)[i4+2],(*this)[i4+3]);
1087 }
1088 
1090 {
1091  TypePointer fp;
1092  fp.uc = new unsigned char[data_type_size()];
1093 
1094  switch ( datatype ) {
1095  case Bit: fp.uc[0] = (v)? 1: 0; break;
1096  case UCharacter: fp.uc[0] = (unsigned char)v; break;
1097  case SCharacter: fp.sc[0] = (signed char)v; break;
1098  case UShort: fp.us[0] = (unsigned short)v; break;
1099  case Short: fp.ss[0] = (short)v; break;
1100  case UInteger: fp.ui[0] = (unsigned int)v; break;
1101  case Integer: fp.si[0] = (int)v; break;
1102  case ULong: fp.ul[0] = (unsigned long)v; break;
1103  case Long: fp.sl[0] = (long)v; break;
1104  case Float: fp.f[0] = v; break;
1105  case Double: fp.d[0] = v; break;
1106  default: ;
1107  }
1108 
1109  return fp;
1110 }
1111 
1118 void Bimage::set(long j, double v)
1119 {
1120  if ( j >= datasize ) return;
1121 
1122  long k;
1123 
1124  if ( v < dtmin ) v = dtmin;
1125  else if ( v > dtmax ) v = dtmax;
1126 
1127  switch ( datatype ) {
1128  case Bit: k = (j/x)*px + j%x; if ( 2*v > min+max ) d.uc[k/8] |= 0x80 >> k%8; break;
1129  case UCharacter: d.uc[j] = (unsigned char)v; break;
1130  case SCharacter: d.sc[j] = (signed char)v; break;
1131  case UShort: d.us[j] = (unsigned short)v; break;
1132  case Short: d.ss[j] = (short)v; break;
1133  case UInteger: d.ui[j] = (unsigned int)v; break;
1134  case Integer: d.si[j] = (int)v; break;
1135  case ULong: d.ul[j] = (unsigned long)v; break;
1136  case Long: d.sl[j] = (long)v; break;
1137  case Float: d.f[j] = v; break;
1138  case Double: d.d[j] = v; break;
1139  default: ;
1140  }
1141 }
1142 
1152 {
1153  long i2 = 2*j;
1154 
1155  set(i2++, cv.real());
1156  set(i2, cv.imag());
1157 }
1158 
1167 void Bimage::set(long j, RGB<double> color)
1168 {
1169  long i3 = 3*j;
1170 
1171  if ( i3 >= datasize ) return;
1172 
1173  set(i3++, color.r());
1174  set(i3++, color.g());
1175  set(i3, color.b());
1176 }
1177 
1186 void Bimage::set(long j, RGBA<double> color)
1187 {
1188  long i4 = 4*j;
1189 
1190  if ( i4 >= datasize ) return;
1191 
1192  set(i4++, color.r());
1193  set(i4++, color.g());
1194  set(i4++, color.b());
1195  set(i4, color.a());
1196 }
1197 
1206 void Bimage::set(long j, CMYK<double> color)
1207 {
1208  long i4 = 4*j;
1209 
1210  if ( i4 >= datasize ) return;
1211 
1212  set(i4++, color.c());
1213  set(i4++, color.m());
1214  set(i4++, color[1]);
1215  set(i4, color.k());
1216 }
1217 
1226 void Bimage::set(long j, Vector3<double> vec)
1227 {
1228  long i3 = 3*j;
1229 
1230  if ( i3 >= datasize ) return;
1231 
1232  set(i3++, vec[0]);
1233  set(i3++, vec[1]);
1234  set(i3, vec[2]);
1235 }
1236 
1244 void Bimage::set(long j, View view)
1245 {
1246  long i4 = 4*j;
1247 
1248  if ( i4 >= datasize ) return;
1249 
1250  set(i4++, view[0]);
1251  set(i4++, view[1]);
1252  set(i4++, view[2]);
1253  set(i4, view.angle());
1254 }
1255 
1266 void Bimage::add(double xx, double yy, double zz, long nn, double v)
1267 {
1268  long slicesize = x*y;
1269  long imgsize = slicesize*z;
1270  long ix = (long) xx;
1271  long iy = (long) yy;
1272  long iz = (long) zz;
1273  long jx, jy, jz, xk, yk, zk;
1274  double fx = xx - ix;
1275  double fy = yy - iy;
1276  double fz = zz - iz;
1277  double w;
1278 
1279  for ( zk=0; zk<2; zk++, iz++ ) {
1280  fz = 1 - fz;
1281  jz = nn*imgsize + iz*slicesize + iy*x;
1282  for ( yk=0, jy=jz; yk<2; yk++, jy+=x ) {
1283  fy = 1 - fy;
1284  for ( xk=0, jx=jy+ix; xk<2; xk++, jx++ ) {
1285  fx = 1 - fx;
1286  w = fx*fy*fz;
1287  add(jx, v * w);
1288  }
1289  }
1290  }
1291 }
1292 
1303 void Bimage::set_max(double xx, double yy, double zz, long nn, double v)
1304 {
1305  long slicesize = x*y;
1306  long imgsize = slicesize*z;
1307  long ix = (long) xx;
1308  long iy = (long) yy;
1309  long iz = (long) zz;
1310  long jx, jy, jz, xk, yk, zk;
1311 
1312  for ( zk=0; zk<2; zk++, iz++ ) {
1313  jz = nn*imgsize + iz*slicesize + iy*x;
1314  for ( yk=0, jy=jz; yk<2; yk++, jy+=x ) {
1315  for ( xk=0, jx=jy+ix; xk<2; xk++, jx++ ) {
1316  if ( (*this)[jx] < v ) set(jx, v);
1317  }
1318  }
1319  }
1320 }
1321 
1334 double Bimage::average2D(long cc, double xf, double yf,
1335  double zf, long nn, double iscale)
1336 {
1337  long zz = (long) zf;
1338  long j, ix, iy, num(0), ij = (nn*z + zz)*y*x;
1339  double xx, yy, avg(0);
1340  double xmax = xf + iscale - 0.5, ymax = yf + iscale - 0.5;
1341 
1342  for ( yy=yf; yy < ymax; yy += 1 ) {
1343  iy = (long) yy;
1344  if ( iy < y ) for ( xx=xf; xx < xmax; xx += 1 ) {
1345  ix = (long) xx;
1346  if ( ix < x ) {
1347  j = (ij + iy*x + ix)*c + cc;
1348  avg += (*this)[j];
1349  num++;
1350  }
1351  }
1352  }
1353 
1354  if ( num ) avg /= num;
1355 
1356  return avg;
1357 }
1358 
1371 double Bimage::average(long cc, double xf, double yf,
1372  double zf, long nn, double iscale)
1373 {
1374  long j, ix, iy, iz, ij, num(0);
1375  double xx, yy, zz, avg(0);
1376  double xmax = xf + iscale - 0.5;
1377  double ymax = yf + iscale - 0.5;
1378  double zmax = zf + iscale - 0.5;
1379  if ( xmax > x ) xmax = x;
1380  if ( ymax > y ) ymax = y;
1381  if ( zmax > z ) zmax = z;
1382 
1383  for ( zz=zf; zz < zmax; zz += 1 ) {
1384  iz = (long) zz;
1385  ij = (nn*z + iz)*y*x;
1386  for ( yy=yf; yy < ymax; yy += 1 ) {
1387  iy = (long) yy;
1388  for ( xx=xf; xx < xmax; xx += 1 ) {
1389  ix = (long) xx;
1390  j = (ij + iy*x + ix)*c + cc;
1391  avg += (*this)[j];
1392  num++;
1393  }
1394  }
1395  }
1396 
1397  if ( num ) avg /= num;
1398 
1399  return avg;
1400 }
1401 
1414 double Bimage::interpolate(long cc, double xx,
1415  double yy, double zz, long nn, double fill) const
1416 {
1417 // cout << cc << tab << xx << tab << yy << tab << zz << tab << nn << endl;
1418  if ( cc >= c ) return fill;
1419  if ( xx < 0 || xx >= x ) return fill;
1420  if ( yy < 0 || yy >= y ) return fill;
1421  if ( zz < 0 || zz >= z ) return fill;
1422  if ( nn >= n ) return fill;
1423 
1424  long ix = (long) xx;
1425  long iy = (long) yy;
1426  long iz = (long) zz;
1427 
1428  long nx = (x < ix + 2)? 1: 2;
1429  long ny = (y < iy + 2)? 1: 2;
1430  long nz = (z < iz + 2)? 1: 2;
1431 
1432  long i, xk, yk, zk;
1433  double fx = xx - ix;
1434  double fy = yy - iy;
1435  double fz = zz - iz;
1436 
1437  double value(0), w(0), ws(0), wyz;
1438 
1439  iz = (nn*z + iz)*y;
1440 
1441  for ( zk=0; zk<nz; zk++, iz+=y ) {
1442  fz = 1.0L - fz;
1443  iy = (long) yy;
1444  iy = (iz + iy)*x;
1445  for ( yk=0; yk<ny; yk++, iy+=x ) {
1446  fy = 1.0L - fy;
1447  ix = (long) xx;
1448  i = (iy + ix)*c + cc;
1449  wyz = fy*fz;
1450  for ( xk=0; xk<nx; xk++, i+=c ) {
1451  fx = 1.0L - fx;
1452  w = fx*wyz;
1453  ws += w;
1454  value += (*this)[i] * w;
1455  }
1456  }
1457  }
1458 
1459  if ( ws ) value /= ws;
1460  else value = fill;
1461 
1462  return value;
1463 }
1464 /*
1465 double Bimage::interpolate(long cc, double xx,
1466  double yy, double zz, long nn, double fill) const
1467 {
1468 // cout << cc << tab << xx << tab << yy << tab << zz << tab << nn << endl;
1469  if ( cc >= c ) return fill;
1470  if ( xx < 0 || xx >= x ) return fill;
1471  if ( yy < 0 || yy >= y ) return fill;
1472  if ( zz < 0 || zz >= z ) return fill;
1473  if ( nn >= n ) return fill;
1474 
1475  long ix = (long) xx;
1476  long iy = (long) yy;
1477  long iz = (long) zz;
1478  long xc(x*c), xyc(xc*y), xyzc(xyc*z);
1479  double fx = xx - ix;
1480  double fy = yy - iy;
1481  double fz = zz - iz;
1482  double value(0), w(0), ws(0), wyz;
1483 
1484  nn *= xyzc;
1485  iz *= xyc;
1486  iy *= xc;
1487  ix *= c;
1488  long iz1 = (iz < xyzc - xyc)? iz+xyc: iz;
1489  long iy1 = (iy < xyc - xc)? iy+xc: iy;
1490  long ix1 = (ix < xc - c)? ix+c: ix;
1491 
1492  for ( ; iz<iz1; iz+=xyc ) {
1493  fz = 1 - fz;
1494  for ( iy = ((long) yy)*xc; iy<iy1; iy+=xc ) {
1495  fy = 1 - fy;
1496  wyz = fy*fz;
1497  for ( ix = ((long) xx)*c + cc; ix<ix1; ix+=c ) {
1498  fx = 1 - fx;
1499  w = fx*wyz;
1500  ws += w;
1501  value += (*this)[nn+iz+iy+ix] * w;
1502  }
1503  }
1504  }
1505 
1506  if ( ws ) value /= ws;
1507 
1508  return value;
1509 }
1510 */
1511 
1524 double Bimage::interpolate_wrap(long cc, double xx,
1525  double yy, double zz, long nn) const
1526 {
1527 // cout << cc << tab << xx << tab << yy << tab << zz << tab << nn << endl;
1528  if ( cc >= c ) return 0;
1529  if ( nn >= n ) return 0;
1530  while ( xx < 0 ) xx += x;
1531  while ( yy < 0 ) yy += y;
1532  while ( zz < 0 ) zz += z;
1533  while ( xx >= x ) xx -= x;
1534  while ( yy >= y ) yy -= y;
1535  while ( zz >= z ) zz -= z;
1536 
1537  long nx(2), ny(2), nz(2);
1538  if ( x < 2 ) nx = 1;
1539  if ( y < 2 ) ny = 1;
1540  if ( z < 2 ) nz = 1;
1541 
1542  long ix = (long) xx;
1543  long iy = (long) yy;
1544  long iz = (long) zz;
1545  long i, in(nn*z), xk, yk, zk, jy, jz;
1546  double fx = xx - ix;
1547  double fy = yy - iy;
1548  double fz = zz - iz;
1549 
1550  double value(0), wyz;
1551 
1552  for ( zk=0; zk<nz; zk++, iz++ ) {
1553  if ( iz >= z ) iz -= z;
1554  fz = 1 - fz;
1555  jz = (in + iz)*y;
1556  iy = (long) yy;
1557  for ( yk=0; yk<ny; yk++, iy++ ) {
1558  if ( iy >= y ) iy -= y;
1559  fy = 1 - fy;
1560  jy = (jz + iy)*x;
1561  ix = (long) xx;
1562  wyz = fy*fz;
1563  for ( xk=0; xk<nx; xk++, ix++ ) {
1564  if ( ix >= x ) ix -= x;
1565  i = (jy + ix)*c + cc;
1566  fx = 1 - fx;
1567  value += (*this)[i] * (fx*wyz);
1568  }
1569  }
1570  }
1571 
1572  return value;
1573 }
1574 
1583 {
1584  long bits(0);
1585 
1586  switch ( datatype ) {
1587  case Bit: bits = 1; break;
1588  case UCharacter: case SCharacter: bits = 8*sizeof(char); break;
1589  case UShort: case Short: bits = 8*sizeof(short); break;
1590  case UInteger: case Integer: bits = 8*sizeof(int); break;
1591  case ULong: case Long: bits = 8*sizeof(long); break;
1592  case Float: bits = 8*sizeof(float); break;
1593  case Double: bits = 8*sizeof(double); break;
1594  default: bits = 0;
1595  }
1596 
1597  return bits;
1598 }
1599 
1608 {
1609  long typesize(0);
1610 
1611  switch ( datatype ) {
1612  case Bit:
1613  case UCharacter: case SCharacter: typesize = sizeof(char); break;
1614  case UShort: case Short:typesize = sizeof(short); break;
1615  case UInteger: case Integer: typesize = sizeof(int); break;
1616  case ULong: case Long: typesize = sizeof(long); break;
1617  case Float: typesize = sizeof(float); break;
1618  case Double: typesize = sizeof(double); break;
1619  default: typesize = 0;
1620  }
1621 
1622  return typesize;
1623 }
1624 
1634 {
1635  double min(0);
1636 
1637  switch ( datatype ) {
1638  case Bit: min = 0; break;
1639  case UCharacter: min = 0; break;
1640  case SCharacter: min = -128; break;
1641  case UShort: min = 0; break;
1642  case Short: min = SHRT_MIN; break;
1643  case UInteger: min = 0; break;
1644  case Integer: min = INT_MIN; break;
1645  case ULong: min = 0; break;
1646  case Long: min = LONG_MIN; break;
1647  case Float: min = -FLT_MAX; break;
1648  case Double: min = -DBL_MAX; break;
1649  default: break;
1650  }
1651 
1652  return min;
1653 }
1654 
1664 {
1665  double max(0);
1666 
1667  switch ( datatype ) {
1668  case Bit: max = 1; break;
1669  case UCharacter: max = 255; break;
1670  case SCharacter: max = 127; break;
1671  case UShort: max = USHRT_MAX; break;
1672  case Short: max = SHRT_MAX; break;
1673  case UInteger: max = UINT_MAX; break;
1674  case Integer: max = INT_MAX; break;
1675  case ULong: max = ULONG_MAX; break;
1676  case Long: max = LONG_MAX; break;
1677  case Float: max = FLT_MAX; break;
1678  case Double: max = DBL_MAX; break;
1679  default: break;
1680  }
1681 
1682  return max;
1683 }
1684 
1690 {
1691  Bstring string;
1692 
1693  switch ( datatype ) {
1694  case Bit: string = "bit"; break;
1695  case UCharacter: string = "unsigned char"; break;
1696  case SCharacter: string = "signed char"; break;
1697  case UShort: string = "unsigned short"; break;
1698  case Short: string = "short"; break;
1699  case UInteger: string = "unsigned int"; break;
1700  case Integer: string = "int"; break;
1701  case ULong: string = "unsigned long"; break;
1702  case Long: string = "long"; break;
1703  case Float: string = "float"; break;
1704  case Double: string = "double"; break;
1705  default: string = "unknown";
1706  }
1707 
1708  return string;
1709 }
1710 
1716 {
1717  Bstring string;
1718 
1719  switch ( compoundtype ) {
1720  case TSimple: string = "Simple"; break;
1721  case TComplex: string = "Complex"; break;
1722  case TVector3: string = "Vector3"; break;
1723  case TView: string = "View"; break;
1724  case TRGB: string = "RGB"; break;
1725  case TRGBA: string = "RGBA"; break;
1726  case TCMYK: string = "CMYK"; break;
1727  default: string = "unknown"; break;
1728  }
1729 
1730  return string;
1731 }
1732 
1741 {
1742  switch ( datatype ) {
1743  case UCharacter: datatype = SCharacter; break;
1744  case SCharacter: datatype = UCharacter; break;
1745  case UShort: datatype = Short; break;
1746  case Short: datatype = UShort; break;
1747  case UInteger: datatype = Integer; break;
1748  case Integer: datatype = UInteger; break;
1749  case ULong: datatype = Long; break;
1750  case Long: datatype = ULong; break;
1751  default: break;
1752  }
1753 
1754  data_type(datatype);
1755 }
1756 
1765 void Bimage::change_type(char letter)
1766 {
1767  DataType nutype;
1768 
1769  switch ( letter ) {
1770  case '1': nutype = Bit; break;
1771  case 'b': nutype = UCharacter; break;
1772  case 'c': nutype = SCharacter; break;
1773  case 'u': nutype = UShort; break;
1774  case 's': nutype = Short; break;
1775  case 'j': nutype = UInteger; break;
1776  case 'i': nutype = Integer; break;
1777  case 'k': nutype = ULong; break;
1778  case 'l': nutype = Long; break;
1779  case 'f': nutype = Float; break;
1780  case 'd': nutype = Double; break;
1781  case 'S': nutype = Short; break;
1782  case 'I': nutype = Integer; break;
1783  case 'F': nutype = Float; break;
1784  case 'D': nutype = Double; break;
1785  default: nutype = Unknown_Type; break;
1786  }
1787 
1788  change_type(nutype);
1789 }
1790 
1795 void Bimage::change_type(char* string)
1796 {
1797  if ( strlen(string) == 1 ) {
1798  change_type(string[0]);
1799  return;
1800  }
1801 
1802  size_t j;
1803  DataType nutype = Unknown_Type;
1804 
1805  for ( j=0; j<strlen(string); j++ ) string[j] = tolower(string[j]);
1806 
1807  if ( strstr(string, "unsigned") ) {
1808  if ( strstr(string, "char") ) nutype = UCharacter;
1809  else if ( strstr(string, "short") ) nutype = UShort;
1810  else if ( strstr(string, "int") ) nutype = UInteger;
1811  else if ( strstr(string, "long") ) nutype = ULong;
1812  } else {
1813  if ( strstr(string, "bit") ) nutype = Bit;
1814  else if ( strstr(string, "byte") ) nutype = UCharacter;
1815  else if ( strstr(string, "char") ) nutype = SCharacter;
1816  else if ( strstr(string, "short") ) nutype = Short;
1817  else if ( strstr(string, "int") ) nutype = Integer;
1818  else if ( strstr(string, "long") ) nutype = Long;
1819  else if ( strstr(string, "float") ) nutype = Float;
1820  else if ( strstr(string, "double") ) nutype = Double;
1821  }
1822 
1823  change_type(nutype);
1824 }
1825 
1831 {
1832  if ( datatype == nutype || nutype == Unknown_Type ) return;
1833 
1834  if ( max - min < 1e-37 ) max = min + 1;
1835 
1836  long j, k, l, m;
1837  long oldtypesize = data_type_size();
1838  double v(0);
1839 // double omn = dtmin;
1840 
1841  TypePointer p = d;
1842 
1843  DataType oldtype = datatype;
1844 
1845  data_type(nutype);
1846  double mn = dtmin;
1847  double mx = dtmax;
1848 
1849  if ( datatype == Bit ) px = 8*((x-1)/8+1);
1850 
1851  long typesize = data_type_size();
1852 
1853  long allocsize = alloc_size();
1854 
1855  if ( verbose & VERB_DEBUG )
1856  cout << "DEBUG Bimage::change_type: datatype=" << datatype << " datasize=" << datasize << endl;
1857 
1858  double scale(1), shift(0);
1859 
1860  // Integer switches between signed and unsigned
1861  if ( datatype >= UCharacter && datatype <= Long ) {
1862  if ( oldtype >= Float ) { // floating point to integer
1863  if ( min >= 0 ) scale = mx/max;
1864  else {
1865  scale = (mx - mn)/(max - min);
1866  shift = mn - min*scale;
1867  }
1868  if ( compoundtype == TComplex && datatype == Short ) {
1869  scale = 0.5*mx/max;
1870  shift = 0;
1871  }
1872  } else {
1873  if ( min == 0 && max == 1 ) { // masks
1874  scale = 1;
1875  shift = 0;
1876  } else if ( oldtypesize == typesize ) {
1877  if ( min >= mn && max <= mx ) {
1878  shift = 0;
1879  } else if ( datatype == oldtype + 1 ) { // unsigned -> signed
1880  shift = mn;
1881  } else if ( datatype == oldtype - 1 ) { // signed -> unsigned
1882  shift = -min;
1883  }
1884  } else if ( oldtype > datatype && ( min < mn || max > mx ) ) { // larger to smaller integer
1885  scale = (mx - mn)/(max - min);
1886  shift = mn - min*scale;
1887  }
1888  }
1889  }
1890 
1891  if ( datatype == Float && oldtype == Double && ( min < mn || max > mx ) ) {
1892  scale = (mx - mn)/(max - min);
1893  }
1894 
1895  if ( verbose & VERB_DEBUG )
1896  cout << "DEBUG Bimage::change_type: scale=" << scale << " shift=" << shift << endl;
1897 
1898  if ( oldtypesize != typesize || oldtype == Bit || datatype == Bit ) {
1899  if ( verbose & VERB_DEBUG )
1900  cout << "DEBUG Bimage::change_type: allocsize=" << allocsize << endl;
1901  d.uc = new unsigned char[allocsize];
1902  for ( j=0; j<allocsize; j++ ) d.uc[j] = 0;
1903  }
1904 
1905  if ( oldtype == Bit ) {
1906  for ( j=l=0; l<n*y*z; l++ )
1907  for ( k=0, m=l*px; k<x; k++, j++, m++ ) set(j, ((p.uc[m/8] << (m%8)) & 0x80) == 0x80);
1908  } else for ( j=l=0; j<datasize; j++, l++ ) {
1909  switch ( oldtype ) {
1910  case UCharacter: v = p.uc[j]; break;
1911  case SCharacter: v = p.sc[j]; break;
1912  case UShort: v = p.us[j]; break;
1913  case Short: v = p.ss[j]; break;
1914  case UInteger: v = p.ui[j]; break;
1915  case Integer: v = p.si[j]; break;
1916  case ULong: v = p.ul[j]; break;
1917  case Long: v = p.sl[j]; break;
1918  case Float: v = p.f[j]; break;
1919  case Double: v = p.d[j]; break;
1920  default: ;
1921  }
1922  v = scale*v + shift;
1923  k = j;
1924  if ( datatype == Bit ) {
1925  if ( l >= x ) {
1926  for ( ; l<px; l++, k++ ) ;
1927  l = 0;
1928  }
1929  }
1930  set(k, v);
1931  }
1932 
1933  if ( oldtypesize != typesize || oldtype == Bit || datatype == Bit )
1934  delete [] p.uc;
1935 
1936  if ( statistics() )
1937  cerr << tab << "in Bimage::change_type" << endl;
1938 }
1939 
1947 {
1948  long nn, cc, j, m, k;
1949  long tsize = data_type_size();
1950  long imgsize = x*y*z;
1951  unsigned char* nudata = new unsigned char[alloc_size()];
1952 
1953  for ( nn=k=0; nn<n; nn++ ) {
1954  for ( j=0; j<imgsize; j++ ) {
1955  for ( cc=0, m=nn*imgsize+j; cc<c; cc++, k+=tsize, m+=imgsize ) {
1956  memcpy(nudata+m*tsize, d.uc+k, tsize);
1957  }
1958  }
1959  }
1960 
1961  data_assign(nudata);
1962 
1963  Bsub_image* nusub = new Bsub_image[n*c];
1964 
1965  if ( image ) {
1966  for ( nn=j=0; nn<n; nn++ )
1967  for ( cc=0; cc<c; cc++, j++ )
1968  nusub[j] = image[nn];
1969  delete[] image;
1970  }
1971 
1972  image = nusub;
1973 
1974  compoundtype = TSimple;
1975  n *= c;
1976  c = 1;
1977 
1978  statistics();
1979 }
1980 
1988 {
1989  if ( nc < 1 ) nc = n;
1990 
1991  long nun, nn, cc, j, m, k;
1992  long tsize = data_type_size();
1993  long imgsize = x*y*z;
1994  unsigned char* nudata = new unsigned char[alloc_size()];
1995 
1996  nun = n/nc;
1997 
1998  for ( nn=k=0; nn<nun; nn++ ) {
1999  for ( j=0; j<imgsize; j++ ) {
2000  for ( cc=0, m=nn*imgsize*nc+j; cc<nc; cc++, k+=tsize, m+=imgsize ) {
2001  memcpy(nudata+k*tsize, d.uc+m, tsize);
2002  }
2003  }
2004  }
2005 
2006  data_assign(nudata);
2007 
2008  Bsub_image* nusub = new Bsub_image[nun];
2009 
2010  if ( image ) {
2011  for ( nn=j=0; nn<nun; nn++, j+=nc )
2012  nusub[nn] = image[j];
2013  delete[] image;
2014  }
2015 
2016  image = nusub;
2017 
2018  if ( nc == 2 ) compoundtype = TComplex;
2019  else if ( nc == 3 ) compoundtype = TRGB;
2020  else if ( nc == 4 ) compoundtype = TRGBA;
2021  else compoundtype = TMulti;
2022  n = nun;
2023  c = nc;
2024 
2025  statistics();
2026 }
2027 
2028 
2034 {
2035  long j, nn;
2036  long imagesize = x*y*z;
2037  Complex<double> cv;
2038 
2039  for ( j=nn=0; nn<n; nn++, j+=imagesize ) set(j, cv);
2040 
2041 }
2042 
2043 
2059 {
2060  double max_radius;
2061  long min_dimension(x);
2062  if ( (y > 1) && (y < min_dimension) ) min_dimension = y;
2063  if ( (z > 1) && (z < min_dimension) ) min_dimension = z;
2064 
2065  max_radius = (min_dimension - 1)/2;
2066 
2067  return max_radius;
2068 }
2069 
2075 {
2076  if ( z == 1 ) return 0;
2077 
2078  if ( n > 1 ) {
2079  cerr << "Error: " << metadata["filename"] << " is already a multi-image file!" << endl;
2080  error_show("Bimage::slices_to_images", __FILE__, __LINE__);
2081  return -1;
2082  }
2083 
2084  if ( verbose & VERB_DEBUG )
2085  cout << "DEBUG Bimage::slices_to_images: " << z << " slices to images" << endl;
2086 
2087  long nn;
2088  Bsub_image subimg = image[0];
2089 
2090  if ( image ) delete [] image;
2091 
2092  n = z;
2093  z = 1;
2094 
2095  image = new Bsub_image[n];
2096 
2097  for ( nn=0; nn<n; nn++ )
2098  image[nn] = subimg;
2099 
2100 /* if ( verbose & VERB_DEBUG )
2101  cout << "DEBUG Bimage::slices_to_images: metadata:" << endl << metadata << endl;
2102 
2103  for ( nn=1; nn<n; nn++ )
2104  metadata["image"].array().push_back(metadata["image"][0]);
2105 */
2106  origin(default_origin());
2107 
2108  return 0;
2109 }
2110 
2116 {
2117  if ( z > 1 ) {
2118 // cerr << "Error: " << filename << " is already a 3D image file!" << endl;
2119  cerr << "Error: " << metadata["filename"] << " is already a 3D image file!" << endl;
2120  error_show("Bimage::images_to_slices", __FILE__, __LINE__);
2121  return -1;
2122  }
2123 
2124  if ( verbose & VERB_DEBUG )
2125  cout << "DEBUG Bimage::images_to_slices: " << n << " images to slices" << endl;
2126 
2127  Bsub_image subimg = image[0];
2128 
2129  if ( image ) delete [] image;
2130 
2131  z = n;
2132  n = 1;
2133 
2134  image = new Bsub_image[n];
2135 
2136  image[0] = subimg;
2137 
2138 // vector<JSvalue>& arr = metadata["image"].array();
2139 // arr.erase(arr.begin()+1, arr.end());
2140 
2141  origin(default_origin());
2142 
2143  return 0;
2144 }
2145 
2156 long Bimage::delete_images(Bstring list, int retain)
2157 {
2158  long ni, no, ns(0);
2159 
2160  vector<int> imgnum = select_numbers(list, n);
2161 
2162  for ( ni=0; ni<n; ni++ ) if ( imgnum[ni] ) ns++;
2163  if ( !retain ) ns = n - ns;
2164 
2165  long imgsize = x*y*z*c*data_type_size();
2166 
2167  Bsub_image* subimg = new Bsub_image[ns];
2168  unsigned char* nudata = new unsigned char[ns*imgsize];
2169 
2170  for ( ni=no=0; ni<n; ni++ ) if ( retain == imgnum[ni] ) {
2171  if ( verbose & VERB_DEBUG )
2172  cout << "DEBUG delete_images: retaining " << ni << endl;
2173  memcpy(nudata+no*imgsize, d.uc+ni*imgsize, imgsize);
2174  subimg[no] = image[ni];
2175  no++;
2176  }
2177 
2178  delete[] image;
2179  image = subimg;
2180  n = ns;
2181 
2182  data_assign(nudata);
2183 
2184  return ns;
2185 }
2186 
2188 {
2189  return delete_images(list, 1);
2190 }
2191 
2207 unsigned char* Bimage::read_data(ifstream* fimg, int img_select, int sb, int vax, long pad)
2208 {
2209  if ( page_size().volume() < 1 ) page_size(x, y, z);
2210 
2211  // If only half of a transform is stored, it need to be handled
2212  long xstore = x;
2213  long xpage = px;
2214  if ( fouriertype == Hermitian || fouriertype == CentHerm ) {
2215  xstore = x/2 + 1;
2216  if ( px > xstore ) xpage = xstore;
2217  }
2218 
2219  if ( datatype == Bit ) {
2220  xstore = xpage = (x - 1)/8 + 1;
2221  page_size(8*xstore, 1, 1);
2222  }
2223 
2224  // Reset select to get the correct offset
2225  if ( img_select < 0 ) img_select = 0;
2226 
2227  long i, iz, iy, j, jz, jy, nn, xx, yy, zz, pxx, pyy, pzz, npages, off;
2228  long datatypesize = data_type_size();
2229  long elementsize = datatypesize*c;
2230  long datasize = n*xstore*y*z*elementsize;
2231  long pagesize = xpage*py*pz*elementsize;
2232  long readsize, pagemax = 1073741824;
2233  unsigned char* data = data_alloc(datasize);
2234  unsigned char* page = NULL;
2235 
2236  if ( verbose & VERB_DEBUG ) {
2237  cout << "DEBUG Bimage::read_data: type size=" << datatypesize << endl;
2238  cout << "DEBUG Bimage::read_data: data size: " << size() << " c=" << c << " n=" << n << " (" << datasize << ")" << endl;
2239  cout << "DEBUG Bimage::read_data: page size: " << page_size() << " (" << pagesize << ")" << endl;
2240  cout << "DEBUG Bimage::read_data: sb=" << sb << " vax=" << vax << " offset=" << offset << endl;
2241  }
2242 
2243  if ( !data ) {
2244  error_show("No image data block allocated!", __FILE__, __LINE__);
2245  return NULL;
2246  }
2247 
2248  if ( x == px && y == py && z == pz ) { // 3D block
2249  off = offset + img_select*(pagesize + pad);
2250  if ( verbose & VERB_DEBUG )
2251  cout << "DEBUG Bimage::read_data: Reading 3D blocks: off=" << off << " pad=" << pad << endl;
2252  fimg->seekg(off);
2253  for ( nn=0; nn<n; nn++ ) {
2254  for ( j=0; j<pagesize; j+=pagemax ) {
2255  readsize = pagesize - j;
2256  if ( readsize > pagemax ) readsize = pagemax;
2257  fimg->read((char *) (data + nn*pagesize + j), readsize);
2258  }
2259  if ( pad ) fimg->seekg(pad, ios_base::cur);
2260  }
2261  } else if ( x == px && y == py && pz == 1 ) { // 2D page (MFF)
2262  off = offset + img_select*z*(pagesize + pad);
2263  if ( verbose & VERB_DEBUG )
2264  cout << "DEBUG Bimage::read_data: Reading 2D pages: off=" << off << " pad=" << pad << endl;
2265  fimg->seekg(off);
2266  for ( nn=0; nn<n; nn++ ) {
2267  for ( pzz=0; pzz<z; pzz+=pz ) {
2268  i = (nn*z + pzz)*pagesize;
2269  fimg->read((char *) (data + i), pagesize);
2270  if ( pad ) fimg->seekg(pad, ios_base::cur);
2271  }
2272  }
2273  } else { // More complicated paging (DSN6 and BRIX)
2274  off = offset;
2275  if ( verbose & VERB_DEBUG )
2276  cout << "DEBUG Bimage::read_data: Reading single values: pagesize=" <<
2277  pagesize << " off=" << off << " pad=" << pad << endl;
2278  npages = 1;
2279  npages *= (z - 1)/pz + 1;
2280  npages *= (y - 1)/py + 1;
2281  npages *= (xstore - 1)/xpage + 1;
2282  off = offset + img_select*npages*(pagesize + pad);
2283  fimg->seekg(off);
2284  page = new unsigned char[pagesize];
2285  for ( nn=0; nn<n; nn++ ) {
2286  for ( pzz=0; pzz<z; pzz+=pz ) {
2287  for ( pyy=0; pyy<y; pyy+=py ) {
2288  for ( pxx=0; pxx<xstore; pxx+=xpage ) { //cout << pxx << tab << pyy << tab << pzz << endl;
2289  fimg->read((char *)page, pagesize);
2290  if ( sb > 1 )
2291  for ( i=0; i<pagesize; i+=sb ) swapbytes(page+i, sb);
2292  for ( zz=0; zz<pz && zz<z-pzz; zz++ ) {
2293  iz = (nn*z + zz + pzz)*y;
2294  jz = zz*py;
2295  for ( yy=0; yy<py && yy<y-pyy; yy++ ) {
2296  iy = (iz + yy + pyy)*xstore;
2297  jy = (jz + yy)*xpage;
2298  for ( xx=0; xx<xpage && xx<xstore-pxx; xx++ ) { //cout << xx << tab << yy << tab << zz << endl;
2299  i = (iy + xx + pxx)*elementsize;
2300  j = (jy + xx)*elementsize;
2301  memcpy(data+i, page+j, elementsize);
2302  }
2303  }
2304  }
2305  if ( pad ) fimg->seekg(pad, ios_base::cur);
2306  }
2307  }
2308  }
2309  }
2310  if ( page ) delete [] page;
2311  }
2312 
2313  // Convert vax format and swap bytes if required
2314  if ( vax && sb < 2 && ( datatype == Float ) ) {
2315  if ( verbose & VERB_DEBUG )
2316  cout << "DEBUG Bimage::read_data: Converting vax image data with sb = " << sb << endl;
2317  for ( i=0; i<datasize; i+=4 )
2318  vax2ieee(data+i, 1-sb);
2319  } else if ( datatypesize > 1 ) {
2320  if ( sb == 1 ) {
2321  if ( verbose & VERB_DEBUG )
2322  cout << "DEBUG Bimage::read_data: Swapping image data with typesize = " << datatypesize << endl;
2323  for ( i=0; i<datasize; i+=datatypesize )
2324  swapbytes(data+i, datatypesize);
2325  } else if ( sb > 1 ) {
2326  if ( verbose & VERB_DEBUG )
2327  cout << "DEBUG Bimage::read_data: Swapping image data with typesize = " << sb << endl;
2328  for ( i=0; i<datasize; i+=sb )
2329  swapbytes(data+i, sb);
2330  }
2331  }
2332 
2333  if ( verbose & VERB_DEBUG )
2334  cout << "DEBUG Bimage::read_data: Finished reading and converting data" << endl;
2335 
2336  return data;
2337 }
2338 
2340 {
2341  if ( verbose )
2342  cout << "Write image to " << fn << endl;
2343 
2344  ofstream fimg(fn.c_str());
2345  if ( fimg.fail() ) return -1;
2346 
2347  fimg.write((char *)&datatype, 8);
2348  fimg.write((char *)&c, 5*8);
2349  fimg.write((char *)d.uc, alloc_size());
2350 
2351  fimg.close();
2352 
2353  return 0;
2354 }
2355 
2356 int Bimage::unpack_transform(unsigned char* data, FourierType tf)
2357 {
2358  return unpack_transform(-1, data, tf);
2359 }
2360 
2361 int Bimage::unpack_transform(int img_select, unsigned char* data, FourierType tf)
2362 {
2363  if ( tf <= Standard ) {
2364  cerr << "Error: The transform is not in the correct format to unpack!" << endl;
2365  exit(-1);
2366  }
2367 
2368  if ( verbose & VERB_PROCESS )
2369  cout << "Unpacking Fourier transform: " << tf << endl << endl;
2370 
2371  long i, h, xx, yy, zz, nn, hx, hy, hz;
2372  long xstore = x, ox(0), oy(0), oz(0);
2373  long elsize = c*data_type_size();
2374  Complex<double> cv;
2375 
2376  long nmin = (img_select >= 0)? img_select: 0;
2377  long nmax = (img_select >= 0)? img_select: n - 1;
2378 
2379  switch ( tf ) {
2380  case Centered:
2381  ox = x/2;
2382  oy = y/2;
2383  oz = z/2;
2384  break;
2385  case Hermitian:
2386  xstore = x/2 + 1;
2387  break;
2388  case CentHerm:
2389  oy = y/2;
2390  oz = z/2;
2391  xstore = x/2 + 1;
2392  break;
2393  default: break;
2394  }
2395 
2396  if ( verbose & VERB_DEBUG )
2397  cout << "DEBUG Bimage::unpack_transform: Element size = " << elsize << endl;
2398 
2399 // cout << "xstore = " << xstore << endl;
2400 
2401  // Transfer the data
2402  for ( nn=nmin; nn<=nmax; nn++ ) {
2403  for ( hz=0; hz<z; hz++ ) {
2404  zz = (hz>=oz)? hz - oz: z + hz - oz;
2405  for ( hy=0; hy<y; hy++ ) {
2406  yy = (hy>=oy)? hy - oy: y + hy - oy;
2407  for ( hx=0; hx<xstore; hx++, data+=elsize ) {
2408  xx = (hx>=ox)? hx - ox: x + hx - ox;
2409  i = index(xx, yy, zz, nn);
2410  memcpy(data_pointer() + i*elsize, data, elsize);
2411  }
2412  }
2413  }
2414  }
2415 
2416  // Fill in the hermitian/friedel partners
2417  if ( xstore < x ) {
2418  for ( nn=nmin; nn<=nmax; nn++ ) {
2419  for ( zz=0; zz<z; zz++ ) {
2420  hz = (zz)? z - zz: 0;
2421  for ( yy=0; yy<y; yy++ ) {
2422  hy = (yy)? y - yy: 0;
2423  for ( xx=xstore; xx<x; xx++ ) {
2424  hx = x - xx;
2425  i = index(xx, yy, zz, nn);
2426  h = index(hx, hy, hz, nn);
2427  cv = complex(h);
2428  cv = cv.conj();
2429  set(i, cv);
2430  }
2431  }
2432  }
2433  }
2434  }
2435 
2436  fourier_type(Standard);
2437 
2438  return 0;
2439 }
2440 
2441 int Bimage::pack_transform(unsigned char* data, FourierType tf)
2442 {
2443  return pack_transform(-1, data, tf);
2444 }
2445 
2446 int Bimage::pack_transform(int img_select, unsigned char* data, FourierType tf)
2447 {
2448  if ( tf <= Standard ) {
2449  cerr << "Error: The transform is not in the correct format to unpack!" << endl;
2450  exit(-1);
2451  }
2452 
2453  if ( verbose & VERB_PROCESS )
2454  cout << "Packing Fourier transform: " << tf << endl << endl;
2455 
2456  long i, xx, yy, zz, nn, hx, hy, hz;
2457  long xstore = x, ox(0), oy(0), oz(0);
2458  long elsize = c*data_type_size();
2459 
2460  long nmin = (img_select >= 0)? img_select: 0;
2461  long nmax = (img_select >= 0)? img_select: n - 1;
2462 
2463  switch ( tf ) {
2464  case Centered:
2465  ox = x/2;
2466  oy = y/2;
2467  oz = z/2;
2468  break;
2469  case Hermitian:
2470  xstore = x/2 + 1;
2471  break;
2472  case CentHerm:
2473  oy = y/2;
2474  oz = z/2;
2475  xstore = x/2 + 1;
2476  break;
2477  default: break;
2478  }
2479 
2480  if ( verbose & VERB_DEBUG )
2481  cout << "DEBUG Bimage::pack_transform: Element size = " << elsize << endl;
2482 
2483 // cout << "xstore = " << xstore << endl;
2484 
2485  // Transfer the data
2486  for ( nn=nmin; nn<=nmax; nn++ ) {
2487  for ( hz=0; hz<z; hz++ ) {
2488  zz = (hz>=oz)? hz - oz: z + hz - oz;
2489  for ( hy=0; hy<y; hy++ ) {
2490  yy = (hy>=oy)? hy - oy: y + hy - oy;
2491  for ( hx=0; hx<xstore; hx++, data+=elsize ) {
2492  xx = (hx>=ox)? hx - ox: x + hx - ox;
2493  i = index(xx, yy, zz, nn);
2494  memcpy(data, data_pointer() + i*elsize, elsize);
2495  }
2496  }
2497  }
2498  }
2499 
2500  return 0;
2501 }
2502 
2503 
2509 {
2510  tm* t = get_localtime();
2511  cout << "Time: " << t->tm_year+1900 << "-" << t->tm_mon+1
2512  << "-" << t->tm_mday << " " << t->tm_hour << ":" << t->tm_min << ":" << t->tm_sec << endl;
2513 // if ( usr.length() ) cout << "User: " << usr << endl;
2514  if ( metadata.exists("user") ) cout << "User: " << metadata["user"] << endl;
2515  cout << "Data offset: " << offset << endl;
2516  cout << "Number of images: " << n << endl;
2517  cout << "Dimensions: " << x << " " << y << " " << z << " voxels" << endl;
2518  cout << "Page dimensions: " << px << " " << py << " " << pz << " voxels" << endl;
2519  cout << "Channels: " << c << endl;
2520  Bstring datatype_string = data_type_string();
2521  cout << "Data type: " << datatype_string <<
2522  " (size = " << data_type_size() << ")" << endl;
2523  if ( fouriertype ) {
2524  cout << "Transform: ";
2525  switch ( fouriertype ) {
2526  case Standard: cout << "Standard" << endl; break;
2527  case Centered: cout << "Centered" << endl; break;
2528  case Hermitian: cout << "Hermitian" << endl; break;
2529  case CentHerm: cout << "Centered hermitian" << endl; break;
2530  default: break;
2531  }
2532  }
2533  Bstring compoundtype_string = compound_type_string();
2534  cout << "Compound type: " << compoundtype_string << endl;
2535  Vector3<double> u(image->sampling());
2536  cout << "Voxel units/sampling: " << u[0] << " " << u[1] << " " << u[2] << endl;
2537  cout << "Min, max, ave, std: " <<
2538  min << " " << max << " " << avg << " " << std << endl;
2539 
2540  cout << "Text label: (length = " << label().length() << ")" << endl;
2541  cout << label() << endl;
2542 
2543  UnitCell uc = unit_cell();
2544  Matrix3 frac_mat;
2545  if ( space_group() > 0 ) {
2546  frac_mat = uc.skew_matrix();
2547  cout.setf( ios::fixed, ios::floatfield );
2548  cout << "Unit cell dimensions: " << setprecision(2) <<
2549  uc.a() << " " << uc.b() << " " << uc.c() << endl;
2550  cout << "Unit cell angles: " <<
2551  uc.alpha()*180/M_PI << " " << uc.beta()*180/M_PI << " " << uc.gamma()*180/M_PI << endl;
2552  cout << "Fractionalization matrix: " << setprecision(5) <<
2553  endl << frac_mat;
2554 // cout << "Fractional translation: " <<
2555 // frac_mat[9] << " " << frac_mat[10] << " " << frac_mat[11] << endl;
2556  cout << "Space group: " << space_group() << endl;
2557  }
2558 
2559  if ( (symmetry()).length() )
2560  cout << "Symmetry label: " << symmetry() << endl;
2561 
2562  cout << endl;
2563 
2564  return 0;
2565 }
2566 
2572 {
2573  long nn;
2574 
2575  cout << "\nImage\tMin\tMax\tAvg\tStd\tBkg" << endl;
2576  for ( nn=0; nn<n; nn++ )
2577  cout << nn+1 << tab << setprecision(4) <<
2578  image[nn].minimum() << tab <<
2579  image[nn].maximum() << tab <<
2580  image[nn].average() << tab <<
2581  image[nn].standard_deviation() << tab <<
2582  image[nn].background() << endl;
2583 
2584  cout << endl;
2585 
2586  cout << "Image\tSampling\t\tOrigin\t\t\tView" << endl;
2587  for ( nn=0; nn<n; nn++ )
2588  cout << nn+1 << tab << setprecision(2) <<
2589  image[nn].sampling() << tab << setprecision(2) <<
2590  image[nn].origin() << tab << setprecision(4) <<
2591  image[nn].view().vector3() << tab <<
2592  image[nn].view().angle()*180.0/M_PI << endl;
2593 
2594  cout << endl;
2595 
2596 // cout << "metadata type = " << metadata.type() << endl;
2597 
2598 // cout << metadata << endl;
2599 
2600  return 0;
2601 }
2602 
2607 int Bimage::moments(long max_order)
2608 {
2609  long nn;
2610 
2611  for ( nn=0; nn<n; ++nn ) moments(max_order, nn);
2612 
2613  cout << endl;
2614 
2615  return 0;
2616 }
2617 
2624 int Bimage::moments(long max_order, long nn)
2625 {
2626  if ( max_order < 2 ) max_order = 2;
2627  if ( max_order > 20 ) max_order = 20;
2628 
2629  cout << "Moments for image " << nn << ":" << endl;
2630 
2631  long i, j, k, ds(image_size());
2632  double v, vc, cm[max_order], m[max_order];
2633 
2634  for ( k=0; k<max_order; ++k ) cm[k] = m[k] = 0;
2635 
2636  for ( i=0, j=nn*ds; i<ds; ++i, ++j ) {
2637  v = (*this)[j];
2638  vc = 1;
2639  for ( k=0; k<max_order; ++k ) {
2640  vc *= v;
2641  m[k] += vc;
2642  }
2643  }
2644 
2645  for ( k=0; k<max_order; ++k ) m[k] /= ds;
2646 
2647  for ( i=0, j=nn*ds; i<ds; ++i, ++j ) {
2648  v = (*this)[j] - m[0];
2649  vc = 1;
2650  for ( k=0; k<max_order; ++k ) {
2651  vc *= v;
2652  cm[k] += vc;
2653  }
2654  }
2655 
2656  for ( k=0; k<max_order; ++k ) cm[k] /= ds;
2657 
2658  v = sqrt(cm[1]); // StDev
2659  vc = 1;
2660 
2661  cout << "Order\tMoment\tCentral\tPearson" << endl;
2662  for ( k=0; k<max_order; ++k ) {
2663  vc *= v;
2664  cout << k+1 << tab << m[k] << tab << cm[k] << tab << cm[k]/vc << endl;
2665  }
2666 
2667  if ( max_order > 2) cout << "Skewness = " << cm[2]/(cm[1]*sqrt(cm[1])) << endl;
2668  if ( max_order > 3) cout << "Kurtosis = " << cm[3]/(cm[1]*cm[1]) - 3 << endl;
2669  cout << endl;
2670 
2671  return 0;
2672 }
2673 
2679 {
2680  if ( tag == "size" ) cout << size() << endl;
2681  else if ( tag == "channels" ) cout << c << endl;
2682  else if ( tag == "x" ) cout << x << endl;
2683  else if ( tag == "y" ) cout << y << endl;
2684  else if ( tag == "z" ) cout << z << endl;
2685  else if ( tag == "images" ) cout << n << endl;
2686  else if ( tag == "sampling" || tag == "pixelsize" ) cout << sampling(0) << endl;
2687  else if ( tag == "origin" ) cout << image->origin() << endl;
2688  else if ( tag == "view" ) cout << image->view() << endl;
2689  else if ( tag == "min" ) cout << min << endl;
2690  else if ( tag == "max" ) cout << max << endl;
2691  else if ( tag == "avg" ) cout << avg << endl;
2692  else if ( tag == "std" ) cout << std << endl;
2693  else if ( tag == "stat" ) cout << min << tab << max << tab << avg << tab << std << endl;
2694  else if ( tag == "text" ) cout << label() << endl;
2695  else if ( tag == "json" ) cout << metadata << endl;
2696 }
2697 
2703 {
2704  long j;
2705  Bimage* img = copy_header();
2706  if ( !img ) {
2707  cerr << "Error: Bimage:copy failed!" << endl;
2708  return img;
2709  }
2710 
2711 // if ( verbose & VERB_DEBUG )
2712 // cout << "DEBUG Bimage::copy: alloc_size=" << alloc_size() << " data_size=" << data_size() << endl;
2713 
2714  img->data_alloc();
2715  for ( j=0; j<img->alloc_size(); j++ ) img->d.uc[j] = d.uc[j];
2716 
2717  if ( verbose & VERB_DEBUG )
2718  cout << "DEBUG Bimage::copy: done" << endl;
2719 
2720  return img;
2721 }
2722 
2728 Bimage* Bimage::copy(long nu_nimg)
2729 {
2730  if ( nu_nimg < 1 ) return copy();
2731 
2732  long j;
2733  Bimage* img = copy_header(nu_nimg);
2734  if ( !img ) {
2735  cerr << "Error: Bimage:copy failed!" << endl;
2736  return img;
2737  }
2738 
2739 // if ( verbose & VERB_DEBUG )
2740 // cout << "DEBUG Bimage::copy: alloc_size=" << alloc_size() << " data_size=" << data_size() << endl;
2741 
2742  img->data_alloc();
2743  for ( j=0; j<alloc_size() && j<img->alloc_size(); j++ ) img->d.uc[j] = d.uc[j];
2744 
2745  if ( verbose & VERB_DEBUG )
2746  cout << "DEBUG Bimage::copy: done" << endl;
2747 
2748  return img;
2749 }
2750 
2751 
2763 {
2764  long j;
2765 
2766  // Allocate memory for the image parameter structure
2767  Bimage* img = new Bimage;
2768  if ( !img ) return img;
2769 
2770  img->offset = offset;
2771 
2772  img->size(x, y, z);
2773  img->channels(c);
2774  img->data_type(datatype);
2775  img->compound_type(compoundtype);
2776  img->page_size(px, py, pz);
2777  img->fourier_type(fouriertype);
2778 
2779  // Copy all strings
2780  img->id = id;
2781 
2782  // Statistics
2783  img->minimum(min);
2784  img->maximum(max);
2785  img->average(avg);
2786  img->standard_deviation(std);
2787  img->show_minimum(smin);
2788  img->show_maximum(smax);
2789  img->show_image(sn);
2790  img->show_slice(sz);
2791  img->show_scale(ss);
2792 
2793  if ( verbose & VERB_DEBUG )
2794  cout << "DEBUG Bimage::copy_header: New image header copied" << endl;
2795 
2796  // Copy metadata
2797  img->metadata = metadata;
2798  // cout << "copy_header:" << endl << img->metadata << endl;
2799 
2800  // Create and copy new sub-images
2801  if ( nu_nimg < 1 ) nu_nimg = n;
2802  img->images(nu_nimg);
2803 
2804  img->unit_cell(ucell);
2805 
2806  if ( verbose & VERB_DEBUG )
2807  cout << "DEBUG Bimage::copy_header: New number of images = " << img->n << endl;
2808 
2809  for ( j=0; j<n && j<img->n; j++ ) img->image[j] = image[j];
2810  for ( ; j<img->n; j++ ) img->image[j] = image[0];
2811 
2812  img->data_size();
2813 
2814  if ( verbose & VERB_DEBUG )
2815  cout << "DEBUG Bimage::copy_header: Units = " << img->sampling(0) << endl;
2816 
2817  return img;
2818 }
2819 
2820 
2821 
2831 {
2832  long j;
2833 
2834  for ( j=0; j<datasize; j++ ) set(j, (*img)[j]);
2835 
2836  return 0;
2837 }
2838 
2849 int Bimage::replace(long nn, Bimage* img, long nr)
2850 {
2851  long i, j, k, imgsize(x*y*z*c);
2852 
2853  image[nn] = img->image[nr];
2854 
2855  for ( i=nn*imgsize, j=nr*imgsize, k=0; k<imgsize; ++i, ++j, ++k ) set(i, (*img)[j]);
2856 
2857  return 0;
2858 }
2859 
2871 int Bimage::replace(long nn, Bimage* img, long nr, double fill)
2872 {
2873  long imgsize(x*y*z*c);
2874  long i, j, xx, yy, zz, cc;
2875 
2876  image[nn] = img->image[nr];
2877 
2878  for ( j=imgsize*nn, i=imgsize*(nn+1); j<i; j++ ) set(j, fill);
2879 
2880  for ( zz=0; zz<z && zz<img->z; zz++ ) {
2881  for ( yy=0; yy<y && yy<img->y; yy++ ) {
2882  for ( xx=0; xx<x && xx<img->x; xx++ ) {
2883  i = img->index(0, xx, yy, zz, nr);
2884  j = index(0, xx, yy, zz, nn);
2885  for ( cc=0; cc<c; cc++, i++, j++ ) set(j, (*img)[i]);
2886  }
2887  }
2888  }
2889 
2890  return 0;
2891 }
2892 
2899 long Bimage::kernel_max(long idx, long ksize)
2900 {
2901  long xx, yy, zz, nn(idx/(x*y*z));
2902  long i, ihi(idx);
2903  double max_val(-1e30);
2904 
2905  //calculate the bounds on the kernel
2906  Vector3<long> lo = kernel_low(idx, ksize);
2907  Vector3<long> hi = kernel_high(idx, ksize);
2908 
2909  // Loop through the kernel and check neighbors
2910  for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
2911  for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
2912  for ( xx=lo[0]; xx<=hi[0]; xx++ ) {
2913  i = index(0,xx,yy,zz,nn);
2914  if ( max_val < (*this)[i] ) {
2915  max_val = (*this)[i];
2916  ihi = i;
2917  }
2918  }
2919  }
2920  }
2921 
2922  return ihi;
2923 }
2924 
2931 long Bimage::kernel_max_neigbor(long idx, long ksize)
2932 {
2933  long xx, yy, zz, nn(idx/(x*y*z));
2934  long i, ihi(idx);
2935  double max_val(-1e30);
2936 
2937  //calculate the bounds on the kernel
2938  Vector3<long> lo = kernel_low(idx, ksize);
2939  Vector3<long> hi = kernel_high(idx, ksize);
2940 
2941  // Loop through the kernel and check neighbors
2942  for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
2943  for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
2944  for ( xx=lo[0]; xx<=hi[0]; xx++ ) {
2945  i = index(0,xx,yy,zz,nn);
2946  if ( max_val < (*this)[i] ) {
2947  if ( i != idx ) {
2948  max_val = (*this)[i];
2949  ihi = i;
2950  }
2951  }
2952  }
2953  }
2954  }
2955 
2956  return ihi;
2957 }
2958 
2965 long Bimage::kernel_max_wrap(long idx, long ksize)
2966 {
2967  long xx, yy, zz, ix, iy, iz, nn(idx/(x*y*z));
2968  long i, ihi(idx);
2969  double max_val(-1e30);
2970 
2971  //calculate the bounds on the kernel
2972  Vector3<long> lo(coordinates(idx)-ksize);
2973  Vector3<long> hi(coordinates(idx)+ksize);
2974 
2975  // Loop through the kernel and check neighbors
2976  for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
2977  iz = zz;
2978  while ( iz < 0 ) iz += z;
2979  while ( iz >= z ) iz -= z;
2980  for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
2981  iy = yy;
2982  while ( iy < 0 ) iy += y;
2983  while ( iy >= y ) iy -= y;
2984  for ( xx=lo[0]; xx<=hi[0]; xx++ ) {
2985  ix = xx;
2986  while ( ix < 0 ) ix += x;
2987  while ( ix >= x ) ix -= x;
2988  i = index(0,ix,iy,iz,nn);
2989  if ( max_val < (*this)[i] ) {
2990  max_val = (*this)[i];
2991  ihi = i;
2992  }
2993  }
2994  }
2995  }
2996 
2997  return ihi;
2998 }
2999 
3012 double Bimage::density(long nn, Vector3<double> coord, double radius, double& sigma)
3013 {
3014  long i, xx, yy, zz;
3015  double x2, y2, z2, rad2(radius*radius);
3016  double density(0), dist2, v, w, sum(0), sum2(0), sumw(0), fac(-9/rad2);
3017 
3018  Vector3<double> lo = coord - radius;
3019  Vector3<double> hi = coord + radius;
3020  lo = lo.max(0);
3021  if ( hi[0] >= x ) hi[0] = x - 1;
3022  if ( hi[1] >= y ) hi[1] = y - 1;
3023  if ( hi[2] >= z ) hi[2] = z - 1;
3024 
3025  for ( zz=(long)lo[2]; zz<=hi[2]; zz++ ) {
3026  z2 = zz - coord[2];
3027  z2 *= z2;
3028  for ( yy=(long)lo[1]; yy<=hi[1]; yy++ ) {
3029  y2 = yy - coord[1];
3030  y2 *= y2;
3031  for ( xx=(long)lo[0]; xx<=hi[0]; xx++ ) {
3032  x2 = xx - coord[0];
3033  x2 *= x2;
3034  dist2 = x2 + y2 + z2;
3035  if ( dist2 <= rad2 ) {
3036  i = index(0, xx, yy, zz, nn);
3037 // if ( (*this)[i] > average() ) {
3038  w = exp(fac*dist2);
3039  v = (*this)[i];
3040  sum += w*v;
3041  sum2 += w*v*v;
3042  sumw += w;
3043 // }
3044  }
3045  }
3046  }
3047  }
3048 
3049  if ( sumw ) {
3050  density = sum/sumw;
3051  sigma = sum2/sumw - density*density;
3052  if ( sigma > 0 ) sigma = sqrt(sigma);
3053  else sigma = 0;
3054  }
3055 
3056  return density;
3057 }
3058 
3072 {
3073  int t;
3074  long i, np(0), nr(0), nb(0);
3075  double v, dp(0), dr(0), db(0);
3076 
3077  for ( i=0; i<datasize; i++ ) {
3078  t = (int) ((*pmask)[i] + 0.5);
3079  v = (*this)[i];
3080  if ( t == 1 ) {
3081  dp += v; // Region of interest
3082  np++;
3083  } else if ( t == 2 ) {
3084  dr += v; // Reference region
3085  nr++;
3086  } else if ( t == 3 ) {
3087  db += v; // Background region
3088  nb++;
3089  }
3090  }
3091 
3092  if ( np ) dp /= np;
3093  if ( nr ) dr /= nr;
3094  if ( nb ) db /= nb;
3095 
3096  if ( dr - db ) dp = (dp - db)/(dr - db);
3097  else if ( dr ) dp /= dr;
3098 
3099  return dp;
3100 }
3101 
3102 
3112 {
3113  long j, ds;
3114 
3115  switch ( datatype ) {
3116  case Bit: ds = (px/8)*y*z*n; for ( j=0; j<ds; j++ ) d.uc[j] = ~d.uc[j]; break;
3117  case UCharacter: case UShort: case UInteger: case ULong:
3118  for ( j=0; j<datasize; j++ ) set(j, dtmax - (*this)[j]); break;
3119  default:
3120  for ( j=0; j<datasize; j++ ) set(j, -(*this)[j]); break;
3121  }
3122 
3123  statistics();
3124 }
3125 
3138 {
3139  if ( !d.uc ) return;
3140 
3141  long i, j;
3142  Vector3<long> nusize, coor, nucoor;
3143  Vector3<long> sign(1,1,1), ind(0,1,2);
3144  Vector3<double> u(image->sampling()), un;
3145 
3146  order.lower();
3147 
3148  if ( !order.contains("x") || !order.contains("y") || !order.contains("z") ) {
3149  cerr << "Error: Reslice order specification not correct: " << order << endl << endl;
3150  return;
3151  }
3152 
3153  if ( verbose & VERB_DEBUG ) {
3154  cout << "DEBUG Bimage::reslice: size" << tab << size() << endl;
3155  cout << "DEBUG Bimage::reslice:";
3156  }
3157 
3158  j = 0;
3159  for ( i=0; i<order.length(); i++ ) {
3160  if ( order[i] == 'x' ) {
3161  ind[j] = 0; nusize[j] = x; un[j] = u[0];
3162  j++;
3163  }
3164  if ( order[i] == 'y' ) {
3165  ind[j] = 1; nusize[j] = y; un[j] = u[1];
3166  j++;
3167  }
3168  if ( order[i] == 'z' ) {
3169  ind[j] = 2; nusize[j] = z; un[j] = u[2];
3170  j++;
3171  }
3172  if ( j < 3 ) {
3173  if ( order[i] == '-' ) sign[j] = -1;
3174  else sign[j] = 1;
3175  }
3176  if ( verbose & VERB_DEBUG )
3177  cout << tab << order[i] << " " << sign[j];
3178  }
3179 
3180  if ( verbose & VERB_DEBUG )
3181  cout << endl << "DEBUG Bimage::reslice: nusize" << tab << nusize << endl;
3182 
3183  long elementsize(c*data_type_size());
3184  long ds(x*y*z*n);
3185  unsigned char* nudata = new unsigned char[alloc_size()];
3186  for ( i=0; i<alloc_size(); i++ ) nudata[i] = 0;
3187 
3188  if ( verbose & VERB_DEBUG )
3189  cout << "DEBUG Bimage::reslice: elementsize=" << elementsize << " datasize=" << ds << endl;
3190 
3191  long nn, xx, yy, zz;
3192  Vector3<double> ori, nuori;
3193 
3194  for ( i=nn=0; nn<n; nn++ ) {
3195  ori = image[nn].origin();
3196  for ( j=0; j<3; j++ ) {
3197  nuori[j] = ori[ind[j]];
3198  if ( sign[j] < 0 ) nuori[j] = nusize[j] - 1 - nuori[j];
3199  }
3200  image[nn].origin(nuori);
3201  for ( zz=0; zz<z; zz++ ) {
3202  coor[2] = zz;
3203  for ( yy=0; yy<y; yy++ ) {
3204  coor[1] = yy;
3205  for ( xx=0; xx<x; xx++, i++ ) {
3206  coor[0] = xx;
3207  for ( j=0; j<3; j++ ) {
3208  nucoor[j] = coor[ind[j]];
3209  if ( sign[j] < 0 ) nucoor[j] = nusize[j] - 1 - nucoor[j];
3210  }
3211  j = ((nn*nusize[2] + nucoor[2])*nusize[1] + nucoor[1])*nusize[0] + nucoor[0];
3212  if ( datatype == Bit ) {
3213  if ( (*this)[i] ) nudata[j/8] |= 0x80 >> ((i/x)*px + i%x)%8;
3214  } else {
3215  memcpy(nudata+j*elementsize, d.uc+i*elementsize, elementsize);
3216 // for ( cc=0, ic=i*c, jc=j*c; cc<c; cc++, ic++, jc++ ) nudata[jc] = (*p)[ic];
3217  }
3218  }
3219  }
3220  }
3221  }
3222 
3223  size(nusize);
3224  sampling(un);
3225  data_assign(nudata);
3226 
3227  if ( verbose & VERB_DEBUG )
3228  cout << "DEBUG Bimage::reslice: alloc_size=" << alloc_size() << endl;
3229 
3230  return;
3231 }
3232 
3237 void Bimage::add(double v)
3238 {
3239  double v1;
3240 
3241  for ( long j=0; j<datasize; j++ ) {
3242  v1 = (*this)[j] + v;
3243  set(j, v1);
3244  }
3245 
3246  statistics();
3247 }
3248 
3253 void Bimage::multiply(double v)
3254 {
3255  long j, nn;
3256  double v1;
3257 
3258  for ( j=0; j<datasize; j++ ) {
3259  v1 = (*this)[j] * v;
3260  set(j, v1);
3261  }
3262 
3263  for ( nn=0; nn<n; nn++ )
3264  image[nn].background(background(nn) * v);
3265 
3266  statistics();
3267 }
3268 
3274 void Bimage::multiply(long nn, double v)
3275 {
3276  long i, j, imgsize(c*x*y*z);
3277  double v1;
3278 
3279  for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3280  v1 = (*this)[j] * v;
3281  set(j, v1);
3282  }
3283 
3284  statistics();
3285 }
3286 
3291 void Bimage::power(double v)
3292 {
3293  double v1;
3294 
3295  for ( long j=0; j<datasize; j++ ) {
3296  v1 = pow((*this)[j], v);
3297  set(j, v1);
3298  }
3299 
3300  statistics();
3301 }
3302 
3307 {
3308  if ( n < 2 ) return;
3309 
3310  long i, j, nn, img_size(c*x*y*z);
3311 
3312  float* nudata = new float[img_size];
3313 
3314  for ( j=0; j<img_size; j++ ) nudata[j] = 0;
3315 
3316  for ( nn=i=0; nn<n; nn++ )
3317  for ( j=0; j<img_size; j++, i++ ) nudata[j] += (*this)[i];
3318 
3319  n = 1;
3320  Bsub_image* nusub = new Bsub_image[1];
3321  nusub[0] = image[0];
3322  delete[] image;
3323  image = nusub;
3324 
3325  data_type(Float);
3326  data_assign((unsigned char *) nudata);
3327 
3328  statistics();
3329 }
3330 
3337 {
3338  if ( n < 2 ) return NULL;
3339 
3340  Bimage* pavg = new Bimage(Float, TSimple, size(), 1);
3341  pavg->clear();
3342 
3343  Bimage* pstd = NULL;
3344  if ( sd ) pstd = pavg->next = pavg->copy();
3345 
3346  long i, j, nn, img_size(c*x*y*z);
3347  double v, w(1.0/n);
3348 
3349  for ( nn=i=0; nn<n; nn++ )
3350  for ( j=0; j<img_size; j++, i++ ) {
3351  v = (*this)[i];
3352  pavg->add(j, v);
3353  if ( sd ) pstd->add(j, v*v);
3354  }
3355 
3356  pavg->multiply(w);
3357 
3358  if ( sd ) {
3359  pstd->multiply(w);
3360  for ( j=0; j<img_size; j++ ) {
3361  v = (*pstd)[j] - (*pavg)[j]*(*pavg)[j];
3362  if ( v > 0 ) v = sqrt(v);
3363  else v = 0;
3364  pstd->set(j, v);
3365  }
3366  }
3367 
3368  pavg->statistics();
3369  if ( sd ) pstd->statistics();
3370 
3371  return pavg;
3372 }
3373 
3382 {
3383  if ( !check_if_same_size(p) ) {
3384  error_show("Bimage::add", __FILE__, __LINE__);
3385  return;
3386  }
3387 
3388  long j;
3389  double v1;
3390 
3391  for ( j=0; j<datasize; j++ ) {
3392  v1 = (*this)[j] + (*p)[j];
3393  set(j, v1);
3394  }
3395 
3396  statistics();
3397 }
3398 
3407 {
3408  if ( !check_if_same_size(p) ) {
3409  error_show("Bimage::subtract", __FILE__, __LINE__);
3410  return;
3411  }
3412 
3413  double v1;
3414 
3415  for ( long j=0; j<datasize; j++ ) {
3416  v1 = (*this)[j] - (*p)[j];
3417  set(j, v1);
3418  }
3419 
3420  statistics();
3421 }
3422 
3434 void Bimage::add(long nn, Bimage* p)
3435 {
3436  long i, j, imgsize(c*image_size());
3437  double v1;
3438 
3439  for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3440  v1 = (*this)[j] + (*p)[i];
3441  set(j, v1);
3442  }
3443 
3444  statistics();
3445 }
3446 
3456 void Bimage::add(Bimage* p, double scale, double shift)
3457 {
3458  if ( !check_if_same_size(p) ) {
3459  error_show("Bimage::add", __FILE__, __LINE__);
3460  return;
3461  }
3462 
3463  double v1;
3464 
3465  for ( long j=0; j<datasize; j++ ) {
3466  v1 = (*this)[j] + (*p)[j] * scale + shift;
3467  set(j, v1);
3468  }
3469 
3470  statistics();
3471 }
3472 
3483 void Bimage::add(long nn, Bimage* p, double scale, double shift)
3484 {
3485  long i, j, imgsize(c*image_size());
3486  double v1;
3487 
3488  for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3489  v1 = (*this)[j] + (*p)[i] * scale + shift;
3490  set(j, v1);
3491  }
3492 
3493  statistics();
3494 }
3495 
3505 void Bimage::multiply(Bimage* p, double scale, double shift)
3506 {
3507  if ( !check_if_same_size(p) ) {
3508  error_show("Bimage::multiply", __FILE__, __LINE__);
3509  return;
3510  }
3511 
3512  double v1;
3513 
3514  for ( long j=0; j<datasize; j++ ) {
3515  v1 = (*this)[j] * ((*p)[j] * scale + shift);
3516  set(j, v1);
3517  }
3518 
3519  statistics();
3520 }
3521 
3530 void Bimage::multiply(long nn, Bimage* p)
3531 {
3532  if ( nn >= n ) return;
3533 
3534  long imgsize(x*y*z*c), i(nn*imgsize), j(0), k(0);
3535 
3536  if ( p->n == n ) j = i;
3537 
3538  for ( k=0; k<imgsize; ++i, ++j, ++k )
3539  set(i, (*this)[i] * (*p)[j]);
3540 }
3541 
3551 {
3552 // if ( verbose & VERB_PROCESS )
3553  cout << "Multiplying " << n << " sub-images with " << p->n << " sub-images" << endl << endl;
3554 
3555  if ( datatype >= Float || p->datatype >= Float )
3556  change_type(Float);
3557 
3558 #ifdef HAVE_GCD
3559  dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(size_t nn){
3560  multiply(nn, p);
3561  });
3562 #else
3563 #pragma omp parallel for
3564  for ( long nn=0; nn<n; ++nn )
3565  multiply(nn, p);
3566 #endif
3567 
3568  statistics();
3569 }
3570 
3582 void Bimage::divide(Bimage* p, double scale, double shift)
3583 {
3584  double minval = p->min*scale + shift;
3585 
3586  if ( !check_if_same_size(p) ) {
3587  error_show("Bimage::divide", __FILE__, __LINE__);
3588  return;
3589  }
3590 
3591  if ( minval < 0 ) {
3592  cerr << "Error: The scaled and shifted second image must be positive! (min = "
3593  << minval << ")" << endl << endl;
3594  return;
3595  }
3596 
3597  long j;
3598  double div;
3599  Complex<float> cv;
3600 // if ( minval <= 0 ) minval = p->standard_deviation()/1000;
3601  if ( minval < 1e-10 ) minval = scale;
3602  if ( minval < 1e-10 ) minval = shift;
3603  if ( minval < 1e-10 ) minval = 1e-10;
3604 
3605  if ( compound_type() != TComplex ) {
3606  for ( j=0; j<datasize; j++ ) {
3607  div = (*p)[j] * scale + shift;
3608  if ( div < minval ) div = minval;
3609  set(j, (*this)[j]/div);
3610  }
3611  } else {
3612  for ( j=0; j<datasize; j++ ) {
3613  cv = p->complex(j) * scale + shift;
3614  div = cv.power();
3615  if ( div < minval ) div = minval;
3616  cv = cv.conj();
3617  set(j, (p->complex(j)*cv)/div);
3618  }
3619  }
3620 
3621  statistics();
3622 }
3623 
3635 void Bimage::divide_one(Bimage* p, double scale, double shift)
3636 {
3637  double minval = p->image->minimum()*scale + shift;
3638 
3639  if ( minval < 0 ) {
3640  cerr << "Error: The scaled and shifted second image must be positive! (min = "
3641  << minval << ")" << endl << endl;
3642  return;
3643  }
3644 
3645  long i, j, nn, imgsize(x*y*z*c);
3646  double div;
3647  Complex<float> cv;
3648 // if ( minval <= 0 ) minval = p->standard_deviation()/1000;
3649  if ( minval < 1e-10 ) minval = scale;
3650  if ( minval < 1e-10 ) minval = shift;
3651  if ( minval < 1e-10 ) minval = 1e-10;
3652 
3653  if ( compound_type() != TComplex ) {
3654  for ( i=nn=0; nn<n; nn++ ) {
3655  for ( j=0; j<imgsize; j++, i++ ) {
3656  div = (*p)[j] * scale + shift;
3657  if ( div < minval ) div = minval;
3658  set(i, (*this)[i]/div);
3659  }
3660  }
3661  } else {
3662  imgsize /= 2;
3663  for ( i=nn=0; nn<n; nn++ ) {
3664  for ( j=0; j<imgsize; j++, i++ ) {
3665  cv = p->complex(j) * scale + shift;
3666  div = cv.power();
3667  if ( div < minval ) div = minval;
3668  cv = cv.conj();
3669  set(i, (p->complex(i)*cv)/div);
3670  }
3671  }
3672  }
3673 
3674  statistics();
3675 }
3676 
3685 void Bimage::inverse(double minval)
3686 {
3687  long j;
3688  double v;
3689  double invminval(0);
3690  if ( minval ) invminval = 1.0/minval;
3691 
3692  for ( j=0; j<datasize; j++ ) {
3693  v = (*this)[j];
3694  if ( fabs(v) > minval )
3695  set(j, 1/v);
3696  else if ( v > 0 )
3697  set (j, invminval);
3698  else
3699  set(j, -invminval);
3700  }
3701 
3702  statistics();
3703 }
3704 
3710 {
3711  if ( !check_if_same_size(p) ) {
3712  error_show("Bimage::largest", __FILE__, __LINE__);
3713  return;
3714  }
3715 
3716  if ( verbose & VERB_PROCESS )
3717  cout << "Setting to the largest" << endl;
3718 
3719  double v1;
3720  for ( long j=0; j<datasize; j++ ) {
3721  v1 = (*p)[j];
3722  if ( (*this)[j] < v1 ) set(j, v1);
3723  }
3724 
3725  statistics();
3726 }
3727 
3734 {
3735  if ( !check_if_same_size(p) ) {
3736  error_show("Bimage::smallest", __FILE__, __LINE__);
3737  return;
3738  }
3739 
3740  if ( verbose & VERB_PROCESS )
3741  cout << "Setting to the smallest" << endl;
3742 
3743  double v1;
3744  for ( long j=0; j<datasize; j++ ) {
3745  v1 = (*p)[j];
3746  if ( (*this)[j] > v1 ) set(j, v1);
3747  }
3748 
3749  statistics();
3750 }
3751 
3763 {
3764  if ( n != p.n ) {
3765  error_show("Error in Bimage::operator+", __FILE__, __LINE__);
3766  cerr << "Different numbers of sub-images: " << n << " vs " << p.n << endl << endl;
3767  return this;
3768  }
3769 
3770  if ( c != p.c ) {
3771  error_show("Error in Bimage::operator+", __FILE__, __LINE__);
3772  cerr << "Different numbers of channels: " << c << " vs " << p.c << endl << endl;
3773  return this;
3774  }
3775 
3776  Vector3<long> sz(size().max(p.size()));
3777 
3778  long i, j, xx, yy, zz, cc, nn;
3779  long i1, i2, x1, x2, y1, y2, z1, z2;
3780  double v;
3781  double fill((avg + p.avg)/2);
3782 
3783  Bimage* pnu = new Bimage(Float, compound_type(), sz, n);
3784  pnu->sampling(sampling(0));
3785 
3786  if ( verbose & VERB_PROCESS ) {
3787  cout << "Summing two images:" << endl;
3788  cout << "New size: " << sz << endl;
3789  }
3790 
3791  for ( i=nn=0; nn<n; nn++ ) {
3792  for ( zz=0; zz<pnu->z; zz++ ) {
3793  z1 = z2 = -1;
3794  if ( zz < z ) z1 = zz;
3795  if ( zz < p.z ) z2 = zz;
3796  for ( yy=0; yy<pnu->y; yy++ ) {
3797  y1 = y2 = -1;
3798  if ( z1 > -1 && yy < y ) y1 = yy;
3799  if ( z2 > -1 && yy < p.y ) y2 = yy;
3800  for ( xx=0; xx<pnu->x; xx++ ) {
3801  x1 = x2 = -1;
3802  if ( y1 > -1 && xx < x ) x1 = xx;
3803  if ( y2 > -1 && xx < p.x ) x2 = xx;
3804  for ( cc=0; cc<c; cc++, i++ ) {
3805  j = 0;
3806  v = (*pnu)[i];
3807  if ( x1 > -1 ) {
3808  i1 = index(cc,x1,y1,z1,nn);
3809  v += (*this)[i1];
3810  j++;
3811  }
3812  if ( x2 > -1 ) {
3813  i2 = p.index(cc,x2,y2,z2,nn);
3814  v += p[i2];
3815  j++;
3816  }
3817  if ( j ) v /= j;
3818  else v = fill;
3819  pnu->set(i, v);
3820  }
3821  }
3822  }
3823  }
3824  pnu->image[nn].background(background(nn) + p.background(nn));
3825  }
3826 
3827  pnu->statistics();
3828 
3829  return pnu;
3830 }
3831 
3840 {
3841  if ( y*z > 1 ) {
3842  cerr << "Error in plot_from_image: Only one-dimensional images can be converted!" << endl;
3843  bexit(-1);
3844  }
3845 
3846  change_type(Float);
3847 
3848  long i, j, nn, cc, xx;
3849  long npage(n);
3850  long nrow(x);
3851  long ncol(n*(1 + c));
3852  double dist, ux;
3853  Bstring title(file_name());
3854 
3855  Bplot* plot = new Bplot(npage, nrow, ncol);
3856 
3857  plot->title(title);
3858 
3859  for ( nn=0; nn<npage; nn++ ) {
3860  ux = sampling(nn)[0];
3861  title = file_name().c_str() + Bstring(nn+1, ": %d");
3862  plot->page(nn).title(title);
3863  plot->page(nn).columns(1+c);
3864  plot->page(nn).column(0).number(nn*(1+c));
3865  plot->page(nn).column(0).label("x");
3866  plot->page(nn).column(0).axis(1);
3867  for ( i=1, j=1+nn*(1+c); i<=c; i++, j++ ) {
3868  plot->page(nn).column(i).number(j);
3869  plot->page(nn).column(i).type(2);
3870  plot->page(nn).column(i).label("Value");
3871  plot->page(nn).column(i).axis(3);
3872  }
3873 /* if ( c == 2 ) {
3874  plot->page(nn).column(1).color(1,0,0);
3875  plot->page(nn).column(2).color(0,0,1);
3876  }
3877  if ( c > 2 ) {
3878  plot->page(nn).column(1).color(1,0,0);
3879  plot->page(nn).column(2).color(0,1,0);
3880  plot->page(nn).column(3).color(0,0,1);
3881  }*/
3882  for ( cc=0; cc<c; ++cc ) {
3883  dist = cc*2.0/(c-1);
3884  if ( dist < 1 ) {
3885  plot->page(nn).column(cc+1).color(1 - dist, dist, 0);
3886  } else {
3887  plot->page(nn).column(cc+1).color(0, 2 - dist, dist - 1);
3888  }
3889  }
3890  plot->page(nn).axis(1).min(-image[nn].origin()[0]*ux);
3891  plot->page(nn).axis(1).max((x-1-image[nn].origin()[0])*ux);
3892  plot->page(nn).axis(3).min(minimum());
3893  plot->page(nn).axis(3).max(maximum());
3894  for ( xx=0, i=nrow*nn*(1+c); xx<nrow; xx++, i++ )
3895  (*plot)[i] = (xx - image[nn].origin()[0])*ux;
3896  for ( cc=0; cc<c; cc++ )
3897  for ( xx=0, j=cc+nn*nrow*c; xx<nrow; xx++, i++, j+=c )
3898  (*plot)[i] = (*this)[j];
3899  }
3900 
3901  return plot;
3902 }
3903 
3904 
3905 
3906 
Bsub_image::average
double average()
Definition: Bimage.h:119
CentHerm
@ CentHerm
Definition: Bimage.h:45
vax2ieee
void vax2ieee(unsigned char *v, int sb)
Converts VAX floating point format to IEEE floating point format.
Definition: utilities.cpp:512
TView
@ TView
Definition: Bimage.h:62
JSarray
@ JSarray
Definition: json.h:27
Bimage::combine_channels
void combine_channels(long nc)
The channels are composed from individual images.
Definition: Bimage.cpp:1987
Bimage::operator+
Bimage * operator+(Bimage &p)
Adds two images together, adjusting for size difference.
Definition: Bimage.cpp:3762
Bimage::inverse
void inverse(double minval=0)
Calculates the inverse of the image.
Definition: Bimage.cpp:3685
Bit
@ Bit
Definition: utilities.h:152
FourierType
FourierType
Fourier transform format specifier.
Definition: Bimage.h:40
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
ULong
@ ULong
Definition: utilities.h:159
Bimage::get
double get(long nn, long xx, long yy, long zz, long cc=0)
Returns the data value at the given coordinates.
Definition: Bimage.cpp:960
UCharacter
@ UCharacter
Definition: utilities.h:153
Complex
Definition: Complex.h:23
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bimage::average
double average()
Definition: Bimage.h:543
Complex::real
Type real()
Definition: Complex.h:101
Bsub_image
General sub-image parameter class.
Definition: Bimage.h:89
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bsub_image::select
long select()
Definition: Bimage.h:167
Bimage::zero_fourier_origin
void zero_fourier_origin()
Zeroes the first voxel in each image.
Definition: Bimage.cpp:2033
Bimage::relative_density
double relative_density(Bimage *pmask)
Calculates the relative density in a region defined by a mask.
Definition: Bimage.cpp:3071
Bimage::show_image
long show_image()
Definition: Bimage.h:559
Bsub_image::standard_deviation
double standard_deviation()
Definition: Bimage.h:120
Bimage::compound_type_string
Bstring compound_type_string()
Get the string representation of a datatype.
Definition: Bimage.cpp:1715
RGBA::a
Type a()
Definition: Color.h:203
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::operator=
Bimage & operator=(const Bimage &p)
Assigns an image.
Definition: Bimage.cpp:911
TypePointer::sc
signed char * sc
Definition: Bimage.h:71
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Matrix::columns
long columns()
Definition: Matrix.h:154
Bcolumn::color
double * color()
Definition: Bplot.h:76
RGBA::b
Type b()
Definition: Color.h:202
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Vector3< long >
TypePointer::ui
unsigned int * ui
Definition: Bimage.h:74
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:70
Bimage::values
vector< double > values(long i)
Definition: Bimage.h:297
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bimage::information
int information()
Prints out header information for an image.
Definition: Bimage.cpp:2508
Bimage::data_type_bits
long data_type_bits() const
Returns the size of the datatype in bits.
Definition: Bimage.cpp:1582
TComplex
@ TComplex
Definition: Bimage.h:60
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
JSvalue::begin
vector< JSvalue >::iterator begin()
Definition: json.h:187
Bimage::meta_data_retain_one_image
void meta_data_retain_one_image(long img_num)
Erases all sub-image records from the meta data except one.
Definition: Bimage.cpp:771
sb
int sb(0)
Bplot
Definition: Bplot.h:146
Bimage::write
int write(Bstring &fn)
Definition: Bimage.cpp:2339
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
RGB
Definition: Color.h:24
Bimage::split_channels
void split_channels()
The channels are split into individual images.
Definition: Bimage.cpp:1946
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bcolumn::type
long type()
Definition: Bplot.h:66
TypePointer::f
float * f
Definition: Bimage.h:78
Bimage::data_type_min
double data_type_min()
Get the minimum of a datatype.
Definition: Bimage.cpp:1633
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
length
double length(vector< T > v)
Definition: Vector3.h:530
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bcolumn::axis
long axis()
Definition: Bplot.h:68
M_PI_2
#define M_PI_2
Definition: utilities.h:66
Hermitian
@ Hermitian
Definition: Bimage.h:44
TVector3
@ TVector3
Definition: Bimage.h:61
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bimage::rgba
RGBA< double > rgba(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1069
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Standard
@ Standard
Definition: Bimage.h:42
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bimage::kernel_max_wrap
long kernel_max_wrap(long idx, long ksize)
Finds the highest value in a kernel with wrapping.
Definition: Bimage.cpp:2965
Bimage::vector3
Vector3< double > vector3(long j)
Returns a 3-value vector at the given index.
Definition: Bimage.cpp:1041
Bimage::delete_images
long delete_images(Bstring list, int retain=0)
Retains or deletes sub-images from a multi-image structure.
Definition: Bimage.cpp:2156
Bsub_image::Bsub_image
Bsub_image()
Initializes a sub-image.
Definition: Bimage.cpp:24
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bimage::density
double density(long nn, Vector3< double > coord, double radius, double &sigma)
Calculates the density in a sphere around a coordinate in an image.
Definition: Bimage.cpp:3012
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
JSvalue::array
vector< JSvalue > & array()
Definition: json.h:157
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::background
double background()
Definition: Bimage.h:125
Bsub_image::maximum
double maximum()
Definition: Bimage.h:118
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Short
@ Short
Definition: utilities.h:156
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::kernel_max
long kernel_max(long idx, long ksize)
Finds the highest value in a kernel.
Definition: Bimage.cpp:2899
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
verbose
int verbose
Bimage::pack_transform
int pack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2441
BVERSION
#define BVERSION
Definition: utilities.h:9
Matrix::rows
long rows()
Definition: Matrix.h:153
JSvalue::end
vector< JSvalue >::iterator end()
Definition: json.h:188
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
JSvalue::exists
bool exists(string tag)
Definition: json.h:221
RGB::b
Type b()
Definition: Color.h:47
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
TRGBA
@ TRGBA
Definition: Bimage.h:64
Bimage::operator[]
JSvalue & operator[](string tag)
Definition: Bimage.h:235
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bimage::data_type_max
double data_type_max()
Get the maximum of a datatype.
Definition: Bimage.cpp:1663
Bimage::data_size
long data_size()
Definition: Bimage.h:270
UShort
@ UShort
Definition: utilities.h:155
TypePointer::ss
short * ss
Definition: Bimage.h:73
TRGB
@ TRGB
Definition: Bimage.h:63
CMYK
Definition: Color.h:264
Bimage::smallest
void smallest(Bimage *p)
Selects the smallest of each pixel from two images.
Definition: Bimage.cpp:3733
Bimage::divide_one
void divide_one(Bimage *p, double scale=1, double shift=0)
Divides the first image by the first sub-image of the second.
Definition: Bimage.cpp:3635
Bimage::divide
void divide(Bimage *p, double scale=1, double shift=0)
Divides the first image by the second.
Definition: Bimage.cpp:3582
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bimage::set_max
void set_max(double xx, double yy, double zz, long nn, double v)
Sets a value at a given location to neigboring data elements if it is larger.
Definition: Bimage.cpp:1303
Bimage::data_type_string
Bstring data_type_string()
Get the string representation of a datatype.
Definition: Bimage.cpp:1689
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
volume
T volume(vector< T > v)
Definition: Vector3.h:547
Bimage::interpolate_wrap
double interpolate_wrap(long cc, double xx, double yy=0, double zz=0, long nn=0) const
Interpolates using a given location with wrapping.
Definition: Bimage.cpp:1524
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3291
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
JSvalue
Definition: json.h:53
View::normalize
double normalize()
Definition: View.h:109
Bimage::kernel_max_neigbor
long kernel_max_neigbor(long idx, long ksize)
Finds the highest value in a kernel excluding the central voxel.
Definition: Bimage.cpp:2931
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bimage::fix_type
void fix_type()
Determines the replacement data type.
Definition: Bimage.cpp:1740
Bplot::title
Bstring & title()
Definition: Bplot.h:194
TMulti
@ TMulti
Definition: Bimage.h:66
Centered
@ Centered
Definition: Bimage.h:43
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
Complex::imag
Type imag()
Definition: Complex.h:102
Bimage::sum_images
void sum_images()
Adds all sub-images.
Definition: Bimage.cpp:3306
Bimage::clear
void clear()
Definition: Bimage.h:705
Baxis::min
double min()
Definition: Bplot.h:30
Bimage.h
Header file for image class.
Bimage::plot
Bplot * plot()
Converts a one-dimensional image into a plot.
Definition: Bimage.cpp:3839
JSvalue::push_back
void push_back(const JSvalue &val)
Definition: json.h:197
CompoundType
CompoundType
Compound data type specifier.
Definition: Bimage.h:58
UnitCell::skew_matrix
Matrix3 skew_matrix()
Calculates the skew matrix from the unit cell parameters.
Definition: UnitCell.cpp:26
Bimage::subtract
void subtract(Bimage *p)
Subtracts another image from an image.
Definition: Bimage.cpp:3406
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
RGBA
Definition: Color.h:162
CMYK::m
Type m()
Definition: Color.h:301
Bimage::data_delete
void data_delete()
Deallocates the image data.
Definition: Bimage.cpp:897
Complex::power
double power()
Definition: Complex.h:103
Bsub_image::magnification
double magnification()
Definition: Bimage.h:163
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
Integer
@ Integer
Definition: utilities.h:158
Bimage::read_data
unsigned char * read_data(ifstream *fimg, int img_select, int sb, int vax, long pad)
Read image data in a generalized style.
Definition: Bimage.cpp:2207
Long
@ Long
Definition: utilities.h:160
Bimage::interpolate
double interpolate(long cc, double xx, double yy=0, double zz=0, long nn=0, double fill=0) const
Interpolates using a given location.
Definition: Bimage.cpp:1414
TypePointer
Definition: Bimage.h:69
Bimage::moments
int moments(long max_order)
Prints out moments for all sub-images.
Definition: Bimage.cpp:2607
Bstring::length
long length() const
Definition: Bstring.cpp:263
JSvalue::array_real
vector< double > array_real()
Definition: json.h:172
isfinite
bool isfinite(complex< T > &value)
TCMYK
@ TCMYK
Definition: Bimage.h:65
TypePointer::sl
long * sl
Definition: Bimage.h:77
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
Complex::conj
Complex conj()
Definition: Complex.h:106
CMYK::c
Type c()
Definition: Color.h:300
Bimage::meta_data_update
void meta_data_update()
Update metadata from the sub-image information.
Definition: Bimage.cpp:678
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bimage::compatible
bool compatible(Bimage *p)
Check if this image has the same number of channels and data type as another.
Definition: Bimage.cpp:653
TypePointer::si
int * si
Definition: Bimage.h:75
JSobject
@ JSobject
Definition: json.h:26
TypePointer::d
double * d
Definition: Bimage.h:79
Bimage::check
void check()
Checks an image for reasonable properties.
Definition: Bimage.cpp:419
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
RGBA::r
Type r()
Definition: Color.h:200
Bimage::cmyk
CMYK< double > cmyk(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1083
Bimage::show_minimum
double show_minimum()
Definition: Bimage.h:565
Bimage::subimage_information
int subimage_information()
Prints out header information for all sub-images.
Definition: Bimage.cpp:2571
SCharacter
@ SCharacter
Definition: utilities.h:154
CMYK::k
Type k()
Definition: Color.h:303
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
TypePointer::us
unsigned short * us
Definition: Bimage.h:72
Bimage::check_if_same_image_size
bool check_if_same_image_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:592
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
Bimage::~Bimage
~Bimage()
Image destructor.
Definition: Bimage.cpp:222
Bimage::average_images
void average_images()
Definition: Bimage.h:724
JSvalue::integer
long & integer()
Definition: json.h:142
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::check_sampling
void check_sampling()
Checks that the sampling is properly specified.
Definition: Bimage.cpp:616
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
RGB::r
Type r()
Definition: Color.h:45
Bimage::update_from_meta_data
void update_from_meta_data()
Update sub-image information from the metadata.
Definition: Bimage.cpp:725
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
RGBA::g
Type g()
Definition: Color.h:201
Bimage::show_maximum
double show_maximum()
Definition: Bimage.h:566
Vector3::length
double length()
Definition: Vector3.h:222
Double
@ Double
Definition: utilities.h:162
Matrix
Definition: Matrix.h:22
Bimage::average2D
double average2D(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1334
Bcolumn::number
long number()
Definition: Bplot.h:61
JSvalue::size
size_t size()
Definition: json.h:114
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::rgb
RGB< double > rgb(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1055
Baxis::max
double max()
Definition: Bplot.h:32
Bimage::largest
void largest(Bimage *p)
Selects the largest of each pixel from two images.
Definition: Bimage.cpp:3709
Bimage::maximum_included_radius
double maximum_included_radius()
Returns the radius of the enclosed sphere or circle.
Definition: Bimage.cpp:2058
Bsub_image::minimum
double minimum()
Definition: Bimage.h:117
TypePointer::ul
unsigned long * ul
Definition: Bimage.h:76
View::angle
double angle()
Definition: View.h:98
Bimage::unpack_transform
int unpack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2356
Bimage::show_scale
double show_scale()
Definition: Bimage.h:563
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:335
UInteger
@ UInteger
Definition: utilities.h:157
NoTransform
@ NoTransform
Definition: Bimage.h:41
UnitCell::degrees_to_radians
void degrees_to_radians()
Definition: UnitCell.h:54
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::check_if_same_size
bool check_if_same_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:566
RGB::g
Type g()
Definition: Color.h:46
Bimage::select_images
long select_images(Bstring list)
Definition: Bimage.cpp:2187
Bimage::show_slice
long show_slice()
Definition: Bimage.h:561
TWOPI
#define TWOPI
Definition: utilities.h:72
get_user_name
string get_user_name()
Returns the user name.
Definition: utilities.cpp:92
Vector3::max
Type max()
Definition: Vector3.h:175