26 min = max = avg = std = 0;
30 vx = vy = vz = angle = 0;
46 cout <<
"DEBUG Bimage: Bimage size = " <<
sizeof(
Bimage) << endl;
72 cout <<
"DEBUG Bimage: Bimage size = " <<
sizeof(
Bimage) << endl;
94 initialize(ctype, nx, ny, nz, nn);
98 data_alloc_and_clear();
114 initialize(ctype, size[0], size[1], size[2], nn);
118 data_alloc_and_clear();
134 initialize(ctype, size[0], size[1], size[2], nn);
138 data_alloc_and_clear();
157 initialize(nc, nx, ny, nz, nn);
161 data_alloc_and_clear();
177 initialize(nc, size[0], size[1], size[2], nn);
181 data_alloc_and_clear();
196 if ( scale < 1 ) scale = 1;
197 if ( scale > 10 ) scale = 10;
199 initialize(1, scale*mat.
columns(), scale*mat.
rows(), 1, 1);
203 data_alloc_and_clear();
205 long i, j, k, xx, yy;
207 for ( k=yy=0; yy<y; ++yy ) {
209 for ( xx=0; xx<x; ++xx, ++k ) {
226 catch (...) { cerr <<
"Failed to deallocate next image!" << endl; }
236 try {
delete[] image; }
237 catch (...) { cerr <<
"Failed to deallocate sub-images!" << endl; }
242 try {
delete[] d.uc; }
243 catch (...) { cerr <<
"Failed to deallocate data block!" << endl; }
254 void Bimage::initialize()
257 x = y = z = c = n = 0;
268 min = max = avg = std = 0;
289 cout <<
"DEBUG Bimage::initialize: Image setup done" << endl;
292 void Bimage::initialize(
long nc,
long nx,
293 long ny,
long nz,
long nn)
302 if ( c > 1 ) compoundtype =
TMulti;
308 long ny,
long nz,
long nn)
316 case TView: nc = 4;
break;
317 case TRGB: nc = 3;
break;
318 case TRGBA: nc = 4;
break;
319 case TCMYK: nc = 4;
break;
323 initialize(nc, nx, ny, nz, nn);
325 compoundtype = ctype;
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; }
366 void Bimage::internal_copy(
const Bimage& p)
370 data_type(p.datatype);
371 compoundtype = p.compoundtype;
372 fouriertype = p.fouriertype;
397 for (
long j=0; j<n; j++ ) image[j] = p.
image[j];
400 metadata = p.metadata;
403 long allocsize = alloc_size();
405 if ( d.uc )
delete[] d.uc;
408 d.uc =
new unsigned char[allocsize];
409 for (
long j=0; j<datasize; j++ ) set(j, p[j]);
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;
444 if ( !compoundtype ) compoundtype =
TSimple;
446 switch ( compoundtype ) {
458 cerr <<
"Error in Bimage::check: The compound type size (" << j <<
") and number of channels (" << c <<
") don't agree!" << endl;
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;
474 cerr <<
"Error in Bimage::check: The sub-image list has not been allocated!" << endl;
482 if ( (min>=max) || (std<=0) || (avg<min) || (avg>max) ) check_stats = 1;
484 if ( min < dtmin ) check_stats = 1;
485 if ( max > dtmax ) check_stats = 1;
487 if ( check_stats ) statistics();
489 if ( smin == 0 && smax == 0 ) {
495 cout <<
"DEBUG Bimage::check: Min, max, mean, stdev: " <<
496 min <<
" " << max <<
" " << avg <<
" " << std << endl;
497 cout <<
"DEBUG Bimage::check: Background:";
500 for ( j=0; j<n; j++ ) cout <<
" " << background(j);
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);
509 | !
isfinite(image[j].origin()[0]) ) image[j].origin(0,0,0);
510 view = image[j].view();
513 if ( image[j].magnification() <= 0 )
514 image[j].magnification(1);
520 cout <<
"DEBUG Bimage::check: Unit cell: " << uc.
a() <<
" " << uc.
b() <<
" " << uc.
c()
521 <<
" " << uc.
alpha() <<
" " << uc.
beta() <<
" " << uc.
gamma() << endl;
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);
538 cout <<
"DEBUG Bimage::check: Unit cell: " << uc.
a() <<
" " << uc.
b() <<
" " << uc.
c()
539 <<
" " << uc.
alpha() <<
" " << uc.
beta() <<
" " << uc.
gamma() << endl;
554 cout <<
"DEBUG Bimage::check: Finished checking parameters" << endl;
569 error_show(
"Both images must be defined", __FILE__, __LINE__);
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;
595 error_show(
"Both images must be defined", __FILE__, __LINE__);
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;
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);
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;
659 cout << metadata[
"filename"] <<
" has an incompatible number of channels: " << c << endl;
663 if ( datatype != p->datatype ) {
665 cout << metadata[
"filename"] <<
" has an incompatible data type: " << datatype << endl;
681 cerr <<
"Error in Bimage::meta_data_update: The number of images must be greater than 0" << endl;
686 cout <<
"DEBUG Bimage::meta_data_update: Updating metadata" << endl;
689 metadata[
"time"] = time(NULL);
694 if ( metadata.exists(
"image") )
695 arr = metadata[
"image"].
array();
697 for (
long i=0; i<n; ++i ) {
699 if ( i >= arr.
size() )
703 img[
"sampling"] = si.
sampling().array();
704 img[
"origin"] = si.
origin().array();
705 img[
"view"] = si.
view().array();
712 img[
"fom"] = si.
FOM();
713 img[
"select"] = si.
select();
716 metadata[
"image"] = arr;
728 cout <<
"DEBUG update_from_meta_data: converting JSON header" << endl;
730 vector<double> sam(3), ori(3), vw(4);
733 if ( metadata.exists(
"image") )
734 arr = metadata[
"image"].
array();
736 for (
auto it = arr.
begin(); it != arr.
end(); ++it ) {
738 long i = img[
"number"].
integer() - 1;
741 if ( img.
exists(
"sampling") ) {
745 if ( img.
exists(
"origin") ) {
749 if ( img.
exists(
"view") ) {
755 if ( img.
exists(
"average") ) si.
average(img[
"average"].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());
764 cout <<
"DEBUG update_from_meta_data: converting done" << endl;
775 if ( metadata.exists(
"image") )
776 arr = metadata[
"image"].
array();
786 metadata[
"image"] = arrnu;
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;
807 long ds(datasize*data_type_size());
808 if ( datatype ==
Bit ) ds /= 8;
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;
816 if ( d.uc )
delete [] d.uc;
818 d.uc =
new unsigned char[nbytes];
821 cerr <<
"Error: Image data not allocated! (" << nbytes <<
")" << endl;
846 long ny,
long nz,
long nn)
857 c = compound_type_size();
858 x = nx; y = ny; z = nz;
860 if ( datatype ==
Bit ) px = 8*((x-1)/8 + 1);
878 cerr <<
"Error: Image data not assigned!" << endl;
882 if ( d.uc )
delete [] d.uc;
889 cout <<
"DEBUG Bimage::data_assign: datasize=" << datasize << endl;
899 if ( d.uc )
delete [] d.uc;
930 if ( j >= datasize )
return 0;
932 switch ( datatype ) {
933 case Bit:
return ((d.uc[j/8] << (j%8)) & 0x80) == 0x80;
936 case UShort:
return d.us[j];
937 case Short:
return d.ss[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];
962 if ( nn >= n )
return 0;
963 if ( xx >= x )
return 0;
964 if ( yy >= y )
return 0;
965 if ( zz >= z )
return 0;
967 long j = (((nn*z + zz)*y + yy)*x + xx)*c + cc;
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;
989 long j = (((nn*z + (long)(vox[2]+0.5))*y + (
long)(vox[1]+0.5))*x + (
long)(vox[0]+0.5))*c + cc;
1006 vector<double> values(c,0);
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;
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];
1058 return RGB<double>((*
this)[i3],(*
this)[i3+1],(*
this)[i3+2]);
1072 return RGBA<double>((*
this)[i4],(*
this)[i4+1],(*
this)[i4+2],(*
this)[i4+3]);
1086 return CMYK<double>((*
this)[i4],(*
this)[i4+1],(*
this)[i4+2],(*
this)[i4+3]);
1092 fp.
uc =
new unsigned char[data_type_size()];
1094 switch ( datatype ) {
1095 case Bit: fp.
uc[0] = (v)? 1: 0;
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;
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;
1120 if ( j >= datasize )
return;
1124 if ( v < dtmin ) v = dtmin;
1125 else if ( v > dtmax ) v = dtmax;
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;
1155 set(i2++, cv.
real());
1171 if ( i3 >= datasize )
return;
1173 set(i3++, color.
r());
1174 set(i3++, color.
g());
1190 if ( i4 >= datasize )
return;
1192 set(i4++, color.
r());
1193 set(i4++, color.
g());
1194 set(i4++, color.
b());
1210 if ( i4 >= datasize )
return;
1212 set(i4++, color.
c());
1213 set(i4++, color.
m());
1214 set(i4++, color[1]);
1230 if ( i3 >= datasize )
return;
1248 if ( i4 >= datasize )
return;
1253 set(i4, view.
angle());
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;
1279 for ( zk=0; zk<2; zk++, iz++ ) {
1281 jz = nn*imgsize + iz*slicesize + iy*x;
1282 for ( yk=0, jy=jz; yk<2; yk++, jy+=x ) {
1284 for ( xk=0, jx=jy+ix; xk<2; xk++, jx++ ) {
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;
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);
1335 double zf,
long nn,
double iscale)
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;
1342 for ( yy=yf; yy < ymax; yy += 1 ) {
1344 if ( iy < y )
for ( xx=xf; xx < xmax; xx += 1 ) {
1347 j = (ij + iy*x + ix)*c + cc;
1354 if ( num ) avg /= num;
1372 double zf,
long nn,
double iscale)
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;
1383 for ( zz=zf; zz < zmax; zz += 1 ) {
1385 ij = (nn*z + iz)*y*x;
1386 for ( yy=yf; yy < ymax; yy += 1 ) {
1388 for ( xx=xf; xx < xmax; xx += 1 ) {
1390 j = (ij + iy*x + ix)*c + cc;
1397 if ( num ) avg /= num;
1415 double yy,
double zz,
long nn,
double fill)
const
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;
1424 long ix = (long) xx;
1425 long iy = (long) yy;
1426 long iz = (long) zz;
1428 long nx = (x < ix + 2)? 1: 2;
1429 long ny = (y < iy + 2)? 1: 2;
1430 long nz = (z < iz + 2)? 1: 2;
1433 double fx = xx - ix;
1434 double fy = yy - iy;
1435 double fz = zz - iz;
1437 double value(0), w(0), ws(0), wyz;
1441 for ( zk=0; zk<nz; zk++, iz+=y ) {
1445 for ( yk=0; yk<ny; yk++, iy+=x ) {
1448 i = (iy + ix)*c + cc;
1450 for ( xk=0; xk<nx; xk++, i+=c ) {
1454 value += (*this)[i] * w;
1459 if ( ws ) value /= ws;
1525 double yy,
double zz,
long nn)
const
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;
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;
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;
1550 double value(0), wyz;
1552 for ( zk=0; zk<nz; zk++, iz++ ) {
1553 if ( iz >= z ) iz -= z;
1557 for ( yk=0; yk<ny; yk++, iy++ ) {
1558 if ( iy >= y ) iy -= y;
1563 for ( xk=0; xk<nx; xk++, ix++ ) {
1564 if ( ix >= x ) ix -= x;
1565 i = (jy + ix)*c + cc;
1567 value += (*this)[i] * (fx*wyz);
1586 switch ( datatype ) {
1587 case Bit: bits = 1;
break;
1589 case UShort:
case Short: bits = 8*
sizeof(short);
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;
1611 switch ( datatype ) {
1614 case UShort:
case Short:typesize =
sizeof(short);
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;
1637 switch ( datatype ) {
1638 case Bit: min = 0;
break;
1641 case UShort: min = 0;
break;
1642 case Short: min = SHRT_MIN;
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;
1667 switch ( datatype ) {
1668 case Bit: max = 1;
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;
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";
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;
1742 switch ( datatype ) {
1754 data_type(datatype);
1770 case '1': nutype =
Bit;
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;
1788 change_type(nutype);
1797 if ( strlen(
string) == 1 ) {
1798 change_type(
string[0]);
1805 for ( j=0; j<strlen(
string); j++ )
string[j] = tolower(
string[j]);
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;
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;
1823 change_type(nutype);
1832 if ( datatype == nutype || nutype ==
Unknown_Type )
return;
1834 if ( max - min < 1e-37 ) max = min + 1;
1837 long oldtypesize = data_type_size();
1849 if ( datatype ==
Bit ) px = 8*((x-1)/8+1);
1851 long typesize = data_type_size();
1853 long allocsize = alloc_size();
1856 cout <<
"DEBUG Bimage::change_type: datatype=" << datatype <<
" datasize=" << datasize << endl;
1858 double scale(1), shift(0);
1862 if ( oldtype >=
Float ) {
1863 if ( min >= 0 ) scale = mx/max;
1865 scale = (mx - mn)/(max - min);
1866 shift = mn - min*scale;
1873 if ( min == 0 && max == 1 ) {
1876 }
else if ( oldtypesize == typesize ) {
1877 if ( min >= mn && max <= mx ) {
1879 }
else if ( datatype == oldtype + 1 ) {
1881 }
else if ( datatype == oldtype - 1 ) {
1884 }
else if ( oldtype > datatype && ( min < mn || max > mx ) ) {
1885 scale = (mx - mn)/(max - min);
1886 shift = mn - min*scale;
1891 if ( datatype ==
Float && oldtype ==
Double && ( min < mn || max > mx ) ) {
1892 scale = (mx - mn)/(max - min);
1896 cout <<
"DEBUG Bimage::change_type: scale=" << scale <<
" shift=" << shift << endl;
1898 if ( oldtypesize != typesize || oldtype ==
Bit || datatype ==
Bit ) {
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;
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 ) {
1913 case Short: v = p.
ss[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;
1922 v = scale*v + shift;
1924 if ( datatype ==
Bit ) {
1926 for ( ; l<px; l++, k++ ) ;
1933 if ( oldtypesize != typesize || oldtype ==
Bit || datatype ==
Bit )
1937 cerr <<
tab <<
"in Bimage::change_type" << endl;
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()];
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);
1961 data_assign(nudata);
1966 for ( nn=j=0; nn<n; nn++ )
1967 for ( cc=0; cc<c; cc++, j++ )
1968 nusub[j] = image[nn];
1989 if ( nc < 1 ) nc = n;
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()];
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);
2006 data_assign(nudata);
2011 for ( nn=j=0; nn<nun; nn++, j+=nc )
2012 nusub[nn] = image[j];
2018 if ( nc == 2 ) compoundtype =
TComplex;
2019 else if ( nc == 3 ) compoundtype =
TRGB;
2020 else if ( nc == 4 ) compoundtype =
TRGBA;
2021 else compoundtype =
TMulti;
2036 long imagesize = x*y*z;
2039 for ( j=nn=0; nn<n; nn++, j+=imagesize ) set(j, cv);
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;
2065 max_radius = (min_dimension - 1)/2;
2076 if ( z == 1 )
return 0;
2079 cerr <<
"Error: " << metadata[
"filename"] <<
" is already a multi-image file!" << endl;
2080 error_show(
"Bimage::slices_to_images", __FILE__, __LINE__);
2085 cout <<
"DEBUG Bimage::slices_to_images: " << z <<
" slices to images" << endl;
2090 if ( image )
delete [] image;
2097 for ( nn=0; nn<n; nn++ )
2106 origin(default_origin());
2119 cerr <<
"Error: " << metadata[
"filename"] <<
" is already a 3D image file!" << endl;
2120 error_show(
"Bimage::images_to_slices", __FILE__, __LINE__);
2125 cout <<
"DEBUG Bimage::images_to_slices: " << n <<
" images to slices" << endl;
2129 if ( image )
delete [] image;
2141 origin(default_origin());
2162 for ( ni=0; ni<n; ni++ )
if ( imgnum[ni] ) ns++;
2163 if ( !retain ) ns = n - ns;
2165 long imgsize = x*y*z*c*data_type_size();
2168 unsigned char* nudata =
new unsigned char[ns*imgsize];
2170 for ( ni=no=0; ni<n; ni++ )
if ( retain == imgnum[ni] ) {
2172 cout <<
"DEBUG delete_images: retaining " << ni << endl;
2173 memcpy(nudata+no*imgsize, d.uc+ni*imgsize, imgsize);
2174 subimg[no] = image[ni];
2182 data_assign(nudata);
2189 return delete_images(list, 1);
2209 if ( page_size().
volume() < 1 ) page_size(x, y, z);
2216 if ( px > xstore ) xpage = xstore;
2219 if ( datatype ==
Bit ) {
2220 xstore = xpage = (x - 1)/8 + 1;
2221 page_size(8*xstore, 1, 1);
2225 if ( img_select < 0 ) img_select = 0;
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;
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;
2244 error_show(
"No image data block allocated!", __FILE__, __LINE__);
2248 if ( x == px && y == py && z == pz ) {
2249 off = offset + img_select*(pagesize + pad);
2251 cout <<
"DEBUG Bimage::read_data: Reading 3D blocks: off=" << off <<
" pad=" << pad << endl;
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);
2259 if ( pad ) fimg->seekg(pad, ios_base::cur);
2261 }
else if ( x == px && y == py && pz == 1 ) {
2262 off = offset + img_select*z*(pagesize + pad);
2264 cout <<
"DEBUG Bimage::read_data: Reading 2D pages: off=" << off <<
" pad=" << pad << endl;
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);
2276 cout <<
"DEBUG Bimage::read_data: Reading single values: pagesize=" <<
2277 pagesize <<
" off=" << off <<
" pad=" << pad << endl;
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);
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 ) {
2289 fimg->read((
char *)page, pagesize);
2292 for ( zz=0; zz<pz && zz<z-pzz; zz++ ) {
2293 iz = (nn*z + zz + pzz)*y;
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++ ) {
2299 i = (iy + xx + pxx)*elementsize;
2300 j = (jy + xx)*elementsize;
2301 memcpy(data+i, page+j, elementsize);
2305 if ( pad ) fimg->seekg(pad, ios_base::cur);
2310 if ( page )
delete [] page;
2314 if ( vax &&
sb < 2 && ( datatype ==
Float ) ) {
2316 cout <<
"DEBUG Bimage::read_data: Converting vax image data with sb = " <<
sb << endl;
2317 for ( i=0; i<datasize; i+=4 )
2319 }
else if ( datatypesize > 1 ) {
2322 cout <<
"DEBUG Bimage::read_data: Swapping image data with typesize = " << datatypesize << endl;
2323 for ( i=0; i<datasize; i+=datatypesize )
2325 }
else if (
sb > 1 ) {
2327 cout <<
"DEBUG Bimage::read_data: Swapping image data with typesize = " <<
sb << endl;
2328 for ( i=0; i<datasize; i+=
sb )
2334 cout <<
"DEBUG Bimage::read_data: Finished reading and converting data" << endl;
2342 cout <<
"Write image to " << fn << endl;
2344 ofstream fimg(fn.
c_str());
2345 if ( fimg.fail() )
return -1;
2347 fimg.write((
char *)&datatype, 8);
2348 fimg.write((
char *)&c, 5*8);
2349 fimg.write((
char *)d.uc, alloc_size());
2358 return unpack_transform(-1, data, tf);
2364 cerr <<
"Error: The transform is not in the correct format to unpack!" << endl;
2369 cout <<
"Unpacking Fourier transform: " << tf << endl << endl;
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();
2376 long nmin = (img_select >= 0)? img_select: 0;
2377 long nmax = (img_select >= 0)? img_select: n - 1;
2397 cout <<
"DEBUG Bimage::unpack_transform: Element size = " << elsize << endl;
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);
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++ ) {
2425 i = index(xx, yy, zz, nn);
2426 h = index(hx, hy, hz, nn);
2443 return pack_transform(-1, data, tf);
2449 cerr <<
"Error: The transform is not in the correct format to unpack!" << endl;
2454 cout <<
"Packing Fourier transform: " << tf << endl << endl;
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();
2460 long nmin = (img_select >= 0)? img_select: 0;
2461 long nmax = (img_select >= 0)? img_select: n - 1;
2481 cout <<
"DEBUG Bimage::pack_transform: Element size = " << elsize << endl;
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);
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;
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;
2533 Bstring compoundtype_string = compound_type_string();
2534 cout <<
"Compound type: " << compoundtype_string << endl;
2536 cout <<
"Voxel units/sampling: " << u[0] <<
" " << u[1] <<
" " << u[2] << endl;
2537 cout <<
"Min, max, ave, std: " <<
2538 min <<
" " << max <<
" " << avg <<
" " << std << endl;
2540 cout <<
"Text label: (length = " << label().
length() <<
")" << endl;
2541 cout << label() << endl;
2545 if ( space_group() > 0 ) {
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: " <<
2552 cout <<
"Fractionalization matrix: " << setprecision(5) <<
2556 cout <<
"Space group: " << space_group() << endl;
2559 if ( (symmetry()).
length() )
2560 cout <<
"Symmetry label: " << symmetry() << endl;
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;
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;
2611 for ( nn=0; nn<n; ++nn ) moments(max_order, nn);
2626 if ( max_order < 2 ) max_order = 2;
2627 if ( max_order > 20 ) max_order = 20;
2629 cout <<
"Moments for image " << nn <<
":" << endl;
2631 long i, j, k, ds(image_size());
2632 double v, vc, cm[max_order], m[max_order];
2634 for ( k=0; k<max_order; ++k ) cm[k] = m[k] = 0;
2636 for ( i=0, j=nn*ds; i<ds; ++i, ++j ) {
2639 for ( k=0; k<max_order; ++k ) {
2645 for ( k=0; k<max_order; ++k ) m[k] /= ds;
2647 for ( i=0, j=nn*ds; i<ds; ++i, ++j ) {
2648 v = (*this)[j] - m[0];
2650 for ( k=0; k<max_order; ++k ) {
2656 for ( k=0; k<max_order; ++k ) cm[k] /= ds;
2661 cout <<
"Order\tMoment\tCentral\tPearson" << endl;
2662 for ( k=0; k<max_order; ++k ) {
2664 cout << k+1 <<
tab << m[k] <<
tab << cm[k] <<
tab << cm[k]/vc << endl;
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;
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;
2705 Bimage* img = copy_header();
2707 cerr <<
"Error: Bimage:copy failed!" << endl;
2715 for ( j=0; j<img->
alloc_size(); j++ ) img->d.
uc[j] = d.uc[j];
2718 cout <<
"DEBUG Bimage::copy: done" << endl;
2730 if ( nu_nimg < 1 )
return copy();
2733 Bimage* img = copy_header(nu_nimg);
2735 cerr <<
"Error: Bimage:copy failed!" << endl;
2743 for ( j=0; j<alloc_size() && j<img->
alloc_size(); j++ ) img->d.
uc[j] = d.uc[j];
2746 cout <<
"DEBUG Bimage::copy: done" << endl;
2768 if ( !img )
return img;
2770 img->offset = offset;
2794 cout <<
"DEBUG Bimage::copy_header: New image header copied" << endl;
2797 img->metadata = metadata;
2801 if ( nu_nimg < 1 ) nu_nimg = n;
2807 cout <<
"DEBUG Bimage::copy_header: New number of images = " << img->n << endl;
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];
2815 cout <<
"DEBUG Bimage::copy_header: Units = " << img->
sampling(0) << endl;
2834 for ( j=0; j<datasize; j++ ) set(j, (*img)[j]);
2851 long i, j, k, imgsize(x*y*z*c);
2853 image[nn] = img->
image[nr];
2855 for ( i=nn*imgsize, j=nr*imgsize, k=0; k<imgsize; ++i, ++j, ++k ) set(i, (*img)[j]);
2873 long imgsize(x*y*z*c);
2874 long i, j, xx, yy, zz, cc;
2876 image[nn] = img->
image[nr];
2878 for ( j=imgsize*nn, i=imgsize*(nn+1); j<i; j++ ) set(j, fill);
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]);
2901 long xx, yy, zz, nn(idx/(x*y*z));
2903 double max_val(-1e30);
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];
2933 long xx, yy, zz, nn(idx/(x*y*z));
2935 double max_val(-1e30);
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] ) {
2948 max_val = (*this)[i];
2967 long xx, yy, zz, ix, iy, iz, nn(idx/(x*y*z));
2969 double max_val(-1e30);
2976 for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
2978 while ( iz < 0 ) iz += z;
2979 while ( iz >= z ) iz -= z;
2980 for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
2982 while ( iy < 0 ) iy += y;
2983 while ( iy >= y ) iy -= y;
2984 for ( xx=lo[0]; xx<=hi[0]; 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];
3015 double x2, y2, z2, rad2(radius*radius);
3016 double density(0), dist2, v, w, sum(0), sum2(0), sumw(0), fac(-9/rad2);
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;
3025 for ( zz=(
long)lo[2]; zz<=hi[2]; zz++ ) {
3028 for ( yy=(
long)lo[1]; yy<=hi[1]; yy++ ) {
3031 for ( xx=(
long)lo[0]; xx<=hi[0]; xx++ ) {
3034 dist2 = x2 + y2 + z2;
3035 if ( dist2 <= rad2 ) {
3036 i = index(0, xx, yy, zz, nn);
3051 sigma = sum2/sumw - density*density;
3052 if ( sigma > 0 ) sigma = sqrt(sigma);
3074 long i, np(0), nr(0), nb(0);
3075 double v, dp(0), dr(0), db(0);
3077 for ( i=0; i<datasize; i++ ) {
3078 t = (int) ((*pmask)[i] + 0.5);
3083 }
else if ( t == 2 ) {
3086 }
else if ( t == 3 ) {
3096 if ( dr - db ) dp = (dp - db)/(dr - db);
3097 else if ( dr ) dp /= dr;
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;
3118 for ( j=0; j<datasize; j++ ) set(j, dtmax - (*
this)[j]);
break;
3120 for ( j=0; j<datasize; j++ ) set(j, -(*
this)[j]);
break;
3139 if ( !d.uc )
return;
3149 cerr <<
"Error: Reslice order specification not correct: " << order << endl << endl;
3154 cout <<
"DEBUG Bimage::reslice: size" <<
tab << size() << endl;
3155 cout <<
"DEBUG Bimage::reslice:";
3159 for ( i=0; i<order.
length(); i++ ) {
3160 if ( order[i] ==
'x' ) {
3161 ind[j] = 0; nusize[j] = x; un[j] = u[0];
3164 if ( order[i] ==
'y' ) {
3165 ind[j] = 1; nusize[j] = y; un[j] = u[1];
3168 if ( order[i] ==
'z' ) {
3169 ind[j] = 2; nusize[j] = z; un[j] = u[2];
3173 if ( order[i] ==
'-' ) sign[j] = -1;
3177 cout <<
tab << order[i] <<
" " << sign[j];
3181 cout << endl <<
"DEBUG Bimage::reslice: nusize" <<
tab << nusize << endl;
3183 long elementsize(c*data_type_size());
3185 unsigned char* nudata =
new unsigned char[alloc_size()];
3186 for ( i=0; i<alloc_size(); i++ ) nudata[i] = 0;
3189 cout <<
"DEBUG Bimage::reslice: elementsize=" << elementsize <<
" datasize=" << ds << endl;
3191 long nn, xx, yy, zz;
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];
3200 image[nn].origin(nuori);
3201 for ( zz=0; zz<z; zz++ ) {
3203 for ( yy=0; yy<y; yy++ ) {
3205 for ( xx=0; xx<x; xx++, i++ ) {
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];
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;
3215 memcpy(nudata+j*elementsize, d.uc+i*elementsize, elementsize);
3225 data_assign(nudata);
3228 cout <<
"DEBUG Bimage::reslice: alloc_size=" << alloc_size() << endl;
3241 for (
long j=0; j<datasize; j++ ) {
3242 v1 = (*this)[j] + v;
3258 for ( j=0; j<datasize; j++ ) {
3259 v1 = (*this)[j] * v;
3263 for ( nn=0; nn<n; nn++ )
3264 image[nn].background(background(nn) * v);
3276 long i, j, imgsize(c*x*y*z);
3279 for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3280 v1 = (*this)[j] * v;
3295 for (
long j=0; j<datasize; j++ ) {
3296 v1 = pow((*
this)[j], v);
3308 if ( n < 2 )
return;
3310 long i, j, nn, img_size(c*x*y*z);
3312 float* nudata =
new float[img_size];
3314 for ( j=0; j<img_size; j++ ) nudata[j] = 0;
3316 for ( nn=i=0; nn<n; nn++ )
3317 for ( j=0; j<img_size; j++, i++ ) nudata[j] += (*
this)[i];
3321 nusub[0] = image[0];
3326 data_assign((
unsigned char *) nudata);
3338 if ( n < 2 )
return NULL;
3344 if ( sd ) pstd = pavg->
next = pavg->
copy();
3346 long i, j, nn, img_size(c*x*y*z);
3349 for ( nn=i=0; nn<n; nn++ )
3350 for ( j=0; j<img_size; j++, i++ ) {
3353 if ( sd ) pstd->
add(j, v*v);
3360 for ( j=0; j<img_size; j++ ) {
3361 v = (*pstd)[j] - (*pavg)[j]*(*pavg)[j];
3362 if ( v > 0 ) v = sqrt(v);
3383 if ( !check_if_same_size(p) ) {
3384 error_show(
"Bimage::add", __FILE__, __LINE__);
3391 for ( j=0; j<datasize; j++ ) {
3392 v1 = (*this)[j] + (*p)[j];
3408 if ( !check_if_same_size(p) ) {
3409 error_show(
"Bimage::subtract", __FILE__, __LINE__);
3415 for (
long j=0; j<datasize; j++ ) {
3416 v1 = (*this)[j] - (*p)[j];
3436 long i, j, imgsize(c*image_size());
3439 for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3440 v1 = (*this)[j] + (*p)[i];
3458 if ( !check_if_same_size(p) ) {
3459 error_show(
"Bimage::add", __FILE__, __LINE__);
3465 for (
long j=0; j<datasize; j++ ) {
3466 v1 = (*this)[j] + (*p)[j] * scale + shift;
3485 long i, j, imgsize(c*image_size());
3488 for ( i=0, j=nn*imgsize; i<imgsize; i++, j++ ) {
3489 v1 = (*this)[j] + (*p)[i] * scale + shift;
3507 if ( !check_if_same_size(p) ) {
3508 error_show(
"Bimage::multiply", __FILE__, __LINE__);
3514 for (
long j=0; j<datasize; j++ ) {
3515 v1 = (*this)[j] * ((*p)[j] * scale + shift);
3532 if ( nn >= n )
return;
3534 long imgsize(x*y*z*c), i(nn*imgsize), j(0), k(0);
3536 if ( p->n == n ) j = i;
3538 for ( k=0; k<imgsize; ++i, ++j, ++k )
3539 set(i, (*
this)[i] * (*p)[j]);
3553 cout <<
"Multiplying " << n <<
" sub-images with " << p->n <<
" sub-images" << endl << endl;
3555 if ( datatype >=
Float || p->datatype >=
Float )
3559 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t nn){
3563 #pragma omp parallel for
3564 for (
long nn=0; nn<n; ++nn )
3584 double minval = p->min*scale + shift;
3586 if ( !check_if_same_size(p) ) {
3587 error_show(
"Bimage::divide", __FILE__, __LINE__);
3592 cerr <<
"Error: The scaled and shifted second image must be positive! (min = "
3593 << minval <<
")" << endl << endl;
3601 if ( minval < 1e-10 ) minval = scale;
3602 if ( minval < 1e-10 ) minval = shift;
3603 if ( minval < 1e-10 ) minval = 1e-10;
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);
3612 for ( j=0; j<datasize; j++ ) {
3613 cv = p->
complex(j) * scale + shift;
3615 if ( div < minval ) div = minval;
3617 set(j, (p->
complex(j)*cv)/div);
3640 cerr <<
"Error: The scaled and shifted second image must be positive! (min = "
3641 << minval <<
")" << endl << endl;
3645 long i, j, nn, imgsize(x*y*z*c);
3649 if ( minval < 1e-10 ) minval = scale;
3650 if ( minval < 1e-10 ) minval = shift;
3651 if ( minval < 1e-10 ) minval = 1e-10;
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);
3663 for ( i=nn=0; nn<n; nn++ ) {
3664 for ( j=0; j<imgsize; j++, i++ ) {
3665 cv = p->
complex(j) * scale + shift;
3667 if ( div < minval ) div = minval;
3669 set(i, (p->
complex(i)*cv)/div);
3689 double invminval(0);
3690 if ( minval ) invminval = 1.0/minval;
3692 for ( j=0; j<datasize; j++ ) {
3694 if ( fabs(v) > minval )
3711 if ( !check_if_same_size(p) ) {
3712 error_show(
"Bimage::largest", __FILE__, __LINE__);
3717 cout <<
"Setting to the largest" << endl;
3720 for (
long j=0; j<datasize; j++ ) {
3722 if ( (*
this)[j] < v1 ) set(j, v1);
3735 if ( !check_if_same_size(p) ) {
3736 error_show(
"Bimage::smallest", __FILE__, __LINE__);
3741 cout <<
"Setting to the smallest" << endl;
3744 for (
long j=0; j<datasize; j++ ) {
3746 if ( (*
this)[j] > v1 ) set(j, v1);
3765 error_show(
"Error in Bimage::operator+", __FILE__, __LINE__);
3766 cerr <<
"Different numbers of sub-images: " << n <<
" vs " << p.n << endl << endl;
3771 error_show(
"Error in Bimage::operator+", __FILE__, __LINE__);
3772 cerr <<
"Different numbers of channels: " << c <<
" vs " << p.c << endl << endl;
3778 long i, j, xx, yy, zz, cc, nn;
3779 long i1, i2, x1, x2, y1, y2, z1, z2;
3781 double fill((avg + p.avg)/2);
3787 cout <<
"Summing two images:" << endl;
3788 cout <<
"New size: " << sz << endl;
3791 for ( i=nn=0; nn<n; nn++ ) {
3792 for ( zz=0; zz<pnu->z; zz++ ) {
3794 if ( zz < z ) z1 = zz;
3795 if ( zz < p.z ) z2 = zz;
3796 for ( yy=0; yy<pnu->y; yy++ ) {
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++ ) {
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++ ) {
3808 i1 = index(cc,x1,y1,z1,nn);
3813 i2 = p.
index(cc,x2,y2,z2,nn);
3842 cerr <<
"Error in plot_from_image: Only one-dimensional images can be converted!" << endl;
3848 long i, j, nn, cc, xx;
3851 long ncol(n*(1 + c));
3859 for ( nn=0; nn<npage; nn++ ) {
3860 ux = sampling(nn)[0];
3867 for ( i=1, j=1+nn*(1+c); i<=c; i++, j++ ) {
3882 for ( cc=0; cc<c; ++cc ) {
3883 dist = cc*2.0/(c-1);
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);
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];