Bsoft  2.1.0
Bernard's software package
Bimage_extract.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "spline.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
22 {
23  long j, k, imgsize(x*y*z*c);
24 
25  Bimage* img = copy_header(1);
26  img->image[0] = image[nn];
27  img->data_alloc();
28 
29  for ( j=nn*imgsize, k=0; k<imgsize; j++, k++ ) img->set(k, (*this)[j]);
30 
31  return img;
32 }
33 
40 Bimage* Bimage::extract(long n1, long n2)
41 {
42  if ( n1 > n2 ) swap(n1, n2);
43  if ( n1 >= n ) n1 = n-1;
44  if ( n2 >= n ) n2 = n-1;
45 
46  long i, j, k, nn, ne, imgsize(x*y*z*c);
47 
48  Bimage* img = copy_header(n2-n1+1);
49  img->data_alloc();
50 
51  for ( ne=0, nn=n1; nn<=n2; nn++, ne++ ) {
52  img->image[ne] = image[nn];
53  for ( i=0, j=nn*imgsize, k=ne*imgsize; i<imgsize; i++, j++, k++ )
54  img->set(k, (*this)[j]);
55  }
56 
57  return img;
58 }
59 
70  int fill_type, double fill)
71 {
72  long i, j, xx, yy, zz, cc;
73  long oldx, oldy, oldz;
74 
75  Bimage* img = copy_header(1);
76  img->size(size);
77  img->data_alloc();
78 
79  if ( fill_type == FILL_AVERAGE ) fill = avg;
80  if ( fill_type == FILL_BACKGROUND ) fill = background(nn);
81 
82  for ( zz=0, oldz=coords[2]; zz<img->z; zz++, oldz++ ) {
83  for ( yy=0, oldy=coords[1]; yy<img->y; yy++, oldy++ ) {
84  for ( xx=0, oldx=coords[0]; xx<img->x; xx++, oldx++ ) {
85  i = img->index(xx, yy, zz);
86  if ( within_boundaries(oldx, oldy, oldz) ) {
87  j = index(oldx, oldy, oldz, nn);
88  for ( cc=0; cc<c; cc++, i++, j++ ) img->set(i, (*this)[j]);
89  } else img->set(i, fill);
90  }
91  }
92  }
93 
94  img->origin(coords);
95 
96  return img;
97 }
98 
109  Vector3<double> ori, Matrix3 mat)
110 {
111  if ( ori[0] == 0 && ori[1] == 0 && ori[2] == 0 )
112  ori = size/2;
113 
114  long i, xx, yy, zz, cc;
115  Vector3<double> v, vold;
116 
117  Bimage* img = copy_header(1);
118  img->size(size);
119  img->image[0] = image[nn];
120  img->origin(ori);
121  img->data_alloc();
122 
123  if ( verbose & VERB_DEBUG )
124  cout << "DEBUG Bimage::extract: size=" << size << endl;
125 
126  for ( i=zz=0; zz<img->z; zz++ ) {
127  v[2] = zz - ori[2];
128  for ( yy=0; yy<img->y; yy++ ) {
129  v[1] = yy - ori[1];
130  for ( xx=0; xx<img->x; xx++ ) {
131  v[0] = xx - ori[0];
132  vold = (mat * v) + loc;
133  for ( cc=0; cc<c; cc++, i++ )
134  img->set(i, interpolate(cc, vold, nn, background(nn)));
135  }
136  }
137  }
138 
139  return img;
140 }
141 
152  Vector3<double> ori, Matrix3 mat)
153 {
154  long i, xx, yy, zz, cc;
155  Vector3<double> v, vold;
156 
157  Bimage* img = copy_header(1);
158  img->size(size);
159  img->image[0] = image[nn];
160  img->origin(ori);
161  img->data_alloc();
162 
163  for ( i=zz=0; zz<img->z; zz++ ) {
164  v[2] = zz - ori[2];
165  for ( yy=0; yy<img->y; yy++ ) {
166  v[1] = yy - ori[1];
167  for ( xx=0; xx<img->x; xx++ ) {
168  v[0] = xx - ori[0];
169  vold = (mat * v) + loc;
170  for ( cc=0; cc<c; cc++, i++ )
171  img->set(i, interpolate_wrap(cc, vold, nn));
172  }
173  }
174  }
175 
176  return img;
177 }
178 
193 Bimage* Bimage::extract_shell(long nn, double minrad, double maxrad)
194 {
195  long i, j, xx, yy, zz, cc;
196  double rx2, ry2, rz2, r2;
197 
198  if ( minrad > maxrad ) swap(minrad, maxrad);
199  if ( minrad < 0 ) minrad = 0;
200  if ( maxrad > x ) maxrad = x - 1;
201 
202  double minrad2 = minrad*minrad;
203  double maxrad2 = maxrad*maxrad;
204 
205  Bimage* pex = copy_header(1);
206  pex->image[0] = image[nn];
207  pex->data_alloc();
208  pex->fill(avg);
209 
210  if ( verbose & VERB_PROCESS ) {
211  cout << "Extracting subimage: " << nn << endl;
212  cout << "Shell: " << minrad << " " << maxrad << endl;
213  cout << endl;
214  }
215 
216  for ( zz=0; zz<z; zz++ ) {
217  if ( z > 1 ) {
218  rz2 = zz - image[nn].origin()[2];
219  rz2 *= rz2;
220  } else rz2 = 0;
221  for ( yy=0; yy<y; yy++ ) {
222  ry2 = yy - image[nn].origin()[1];
223  ry2 *= ry2;
224  for ( xx=0; xx<x; xx++ ) {
225  rx2 = xx - image[nn].origin()[0];
226  rx2 *= rx2;
227  r2 = rx2 + ry2 + rz2;
228  if ( r2 >= minrad2 && r2 <= maxrad2 ) {
229  i = pex->index(xx, yy, zz);
230  j = index(xx, yy, zz, nn);
231  for ( cc=0; cc<c; cc++, i++, j++ ) pex->set(i, (*this)[j]);
232  }
233  }
234  }
235  }
236 
237  pex->statistics();
238 
239  return pex;
240 }
241 
259 vector<Vector3<long>> Bimage::tile_coordinates(Vector3<long>& start,
260  Vector3<long>& region, Vector3<long>& tile_size,
261  Vector3<long>& step_size, int exceed)
262 {
263  vector<Vector3<long>> coords;
264 
265  if ( tile_size.length() < 1 ) {
266  error_show("Bimage::tile_coordinates", __FILE__, __LINE__);
267  return coords;
268  }
269 
270  long i, xx, yy, zz;
271 
272  if ( region.length() < 2 ) region = size();
273 
274  if ( start[0] > x ) start[0] = 0;
275  if ( start[1] > y ) start[1] = 0;
276  if ( start[2] > z ) start[2] = 0;
277 
278  if ( region[0] > x - start[0] ) region[0] = x - start[0];
279  if ( region[1] > y - start[1] ) region[1] = y - start[1];
280  if ( region[2] > z - start[2] ) region[2] = z - start[2];
281 
282  tile_size = tile_size.min(region);
283 
284  if ( step_size[0] <= 0 ) step_size[0] = tile_size[0];
285  if ( step_size[1] <= 0 ) step_size[1] = tile_size[1];
286  if ( step_size[2] <= 0 ) step_size[2] = tile_size[2];
287 
288  if ( !exceed ) region -= tile_size - 1;
289 
290 // calculate_background();
291 
292  // Count the number of tiles to be extracted
293  long ntiles(0);
294  for ( zz=0; zz<region[2]; zz+=step_size[2] )
295  for ( yy=0; yy<region[1]; yy+=step_size[1] )
296  for ( xx=0; xx<region[0]; xx+=step_size[0] )
297  ntiles++;
298 
299  coords.resize(ntiles);
300 
301  for ( i=zz=0; zz<region[2]; zz+=step_size[2] )
302  for ( yy=0; yy<region[1]; yy+=step_size[1] )
303  for ( xx=0; xx<region[0]; xx+=step_size[0], i++ )
304  coords[i] = Vector3<long>(xx + start[0], yy + start[1], zz + start[2]);
305 
306  if ( verbose & VERB_FULL ) {
307  cout << "Generating " << ntiles << " tile coordinates:" << endl;
308  cout << "Start: " << start << endl;
309  cout << "Tile size: " << tile_size << endl;
310  cout << "Step size: " << step_size << endl;
311  cout << endl;
312  }
313 
314  return coords;
315 }
316 
331 Bimage* Bimage::extract_tiles(long nn, vector<Vector3<long>>& coords, Vector3<long> tile_size)
332 {
333  if ( tile_size.length() < 1 ) {
334  error_show("Bimage::extract_tiles", __FILE__, __LINE__);
335  cerr << "Error: Size of subimage is less than one!" << endl;
336  return NULL;
337  }
338 
339  tile_size = tile_size.min(size());
340 
341  if ( nn >= n ) nn = n - 1;
342 
343  long ntiles(coords.size());
344  Bimage* pex = copy_header(ntiles);
345 
346  pex->size(tile_size);
347  pex->page_size(size());
348 
349  pex->data_alloc();
350  pex->fill(avg);
351 
352  if ( verbose & VERB_FULL ) {
353  cout << "Extracting " << pex->images() << " tiles using coordinates:" << endl;
354  cout << "Tile size: " << pex->size() << endl;
355  cout << endl;
356  }
357 
358 #ifdef HAVE_GCD
359  dispatch_apply(ntiles, dispatch_get_global_queue(0, 0), ^(size_t nex){
360  Bimage* pt = extract(nn, coords[nex], tile_size, FILL_BACKGROUND);
361  pex->replace(nex, pt);
362  delete pt;
363  });
364 #else
365 #pragma omp parallel for
366  for ( long nex=0; nex<ntiles; nex++ ) {
367  Bimage* pt = extract(nn, coords[nex], tile_size, FILL_BACKGROUND);
368  pex->replace(nex, pt);
369  delete pt;
370  }
371 #endif
372 
373  pex->statistics();
374 
375  return pex;
376 }
377 
378 
398  Vector3<long> region, Vector3<long> tile_size,
399  Vector3<long> step_size, int exceed)
400 {
401  vector<Vector3<long>> coords = tile_coordinates(start, region, tile_size, step_size, exceed);
402 
403  if ( verbose & VERB_FULL )
404  cout << "Extracting " << coords.size() << " tiles:" << endl << endl;
405 
406  Bimage* pex = extract_tiles(nn, coords, tile_size);
407 
408  return pex;
409 }
410 
411 long find_optimal_tiling(long target, long tile_size, double fraction=0.2)
412 {
413  if ( fraction > 0.5 ) fraction = 0.5;
414 
415  long overlap(fraction*tile_size), i, imin(overlap/2), imax(overlap*2);
416  long step(tile_size - overlap);
417  long num(target/step);
418  long d, dmin(target);
419 
420  for ( i = imin; i < imax; i++ ) {
421  step = tile_size - i;
422  num = target/step;
423  d = num*step + i - target;
424  if ( d >= 0 && dmin > d ) {
425  overlap = i;
426  dmin = d;
427  if ( verbose & VERB_FULL )
428  cout << overlap << tab << num << tab << step << tab << d << endl;
429  }
430  }
431 
432  return overlap;
433 }
434 
449 Bimage* Bimage::extract_tiles(long nn, Vector3<long> tile_size, double fraction)
450 {
451  tile_size = tile_size.min(size());
452 
453  Vector3<long> num, overlap, step, icoor;
454  Vector3<long> maxori(size() - tile_size);
455 
456  for ( long j=0; j<3; j++ )
457  if ( size()[j] > 1 ) overlap[j] = find_optimal_tiling(size()[j], tile_size[j], fraction);
458 
459  step = tile_size - overlap;
460 
461  num = size()/step;
462  num = num.max(1);
463 
464  if ( verbose & VERB_FULL ) {
465  cout << size() << endl;
466  cout << num * step + overlap << endl;
467  }
468 
469  long i, ntiles(num.volume());
470 
471  vector<Vector3<long>> coords(ntiles);
472 
473  for ( i=icoor[2]=0; icoor[2]<num[2]; icoor[2]++ )
474  for ( icoor[1]=0; icoor[1]<num[1]; icoor[1]++ )
475  for ( icoor[0]=0; icoor[0]<num[0]; icoor[0]++, i++ ) {
476  coords[i] = icoor * step;
477  coords[i] = coords[i].min(maxori);
478  }
479 
480 
481  if ( verbose & VERB_FULL ) {
482  cout << "Extracting " << ntiles << " tiles:" << endl;
483  cout << "Tile size: " << tile_size << endl;
484  cout << "Number of tiles: " << num << endl;
485  cout << "Overlap: " << overlap << endl << endl;
486  }
487 
488  Bimage* pex = extract_tiles(nn, coords, tile_size);
489 
490  return pex;
491 }
492 
506 {
507  if ( width < 1 ) width = 1;
508 
509  long i, j, wh(width/2);
510  Vector3<double> vec(end - start), vecp, coor;
511  long len(vec.length() + 1);
512 
513  vec.normalize();
514  vecp = Vector3<double>(vec[1], vec[0], vec[2]);
515 
516  Bimage* pline = new Bimage(Float, TSimple, len, 1, 1, 1);
517 
518  for ( i=0; i<len; i++ ) {
519  coor = start + vec*i;
520  for ( j=-wh; j<=wh; j++ )
521  pline->add(i, interpolate(coor + vecp*j, nn));
522  pline->set(i, (*pline)[i]/width);
523  }
524 
525  pline->statistics();
526 
527  return pline;
528 }
529 
542 Bimage* Bimage::extract_tetrahedron(Vector3<double>* tet, int fill_type, double fill)
543 {
544  if ( fill_type == FILL_AVERAGE ) fill = avg;
545 
546  long i, j, k, nn, xx, yy, zz, cc;
547 
549  Vector3<double> normal[4];
550  double origin[4];
551 
552  for ( i=0; i<4; i++ ) center = center + tet[i];
553  center /= 4.0;
554 
555  normal[0] = tet[0].normal(tet[1], tet[2]);
556  normal[1] = tet[1].normal(tet[2], tet[3]);
557  normal[2] = tet[2].normal(tet[3], tet[0]);
558  normal[3] = tet[3].normal(tet[0], tet[1]);
559 
560  // The distance of a point from a plane is given by the inner product sum of
561  // the normal to the plane and the vector from that point to any point on the plane
562  for ( i=0; i<4; i++ ) {
563  origin[i] = normal[i].scalar(tet[i]);
564  if ( normal[i].scalar(center) < origin[i] ) {
565  normal[i] = -normal[i];
566  origin[i] = -origin[i];
567  }
568  }
569 
570  Bimage* ptet = copy();
571 
572  if ( verbose & VERB_PROCESS ) {
573  cout << "Extracting a tetrahedron:" << endl;
574  cout << "Vertices: " << tet[0] << endl;
575  cout << " " << tet[1] << endl;
576  cout << " " << tet[2] << endl;
577  cout << " " << tet[3] << endl;
578  cout << "Center: " << center << endl;
579  cout << "Normals: " << normal[0] << " (" << origin[0] << ")" << endl;
580  cout << " " << normal[1] << " (" << origin[1] << ")" << endl;
581  cout << " " << normal[2] << " (" << origin[2] << ")" << endl;
582  cout << " " << normal[3] << " (" << origin[3] << ")" << endl;
583  cout << endl;
584  } else if ( verbose & VERB_LABEL )
585  cout << "Extracting a tetrahedron" << endl << endl;
586 
587  for ( i=nn=0; nn<n; nn++ ) {
588  if ( fill_type == FILL_BACKGROUND ) fill = background(nn);
589  for ( zz=0; zz<z; zz++ ) {
590  for ( yy=0; yy<y; yy++ ) {
591  for ( xx=0; xx<x; xx++, i++ ) {
592  for ( j=0; j<4; j++ ) {
593  if ( normal[j][0]*xx + normal[j][1]*yy + normal[j][2]*zz < origin[j] )
594  for ( cc=0, k=i*c; cc<c; cc++, k++ )
595  ptet->set(k, fill);
596  }
597  }
598  }
599  }
600  }
601 
602  return 0;
603 }
604 
617  Vector3<long> ext_size)
618 {
619  if ( voxel[0] < 0 || voxel[1] < 0 || voxel[2] < 0 ) return NULL;
620  if ( voxel[0] >= x || voxel[1] >= y || voxel[2] >= z ) return NULL;
621 
622  if ( ext_size.volume() < 1 ) ext_size = size();
623 
624  int xf, yf;
625  long i, j, iy, xx, yy, zz, cc;
626  long xo, yo, zo;
627  long h, nh(1);
628  if ( z > 1 ) nh = 3;
629  long nx = (long) (ext_size[0]);
630  long ny = (long) (ext_size[1]);
631 
632  Vector3<long> start(voxel - (ext_size * 0.5));
633  Vector3<long> end(start + ext_size);
634  Vector3<double> ori;
635 
636  if ( verbose & VERB_PROCESS ) {
637  cout << "Extracting orthogonal views:" << endl;
638  cout << "Start: " << start << endl;
639  cout << "End: " << end << endl;
640  cout << "Fill value: " << image[nn].background() << endl;
641  cout << endl;
642  }
643 
644  Bimage* porth = new Bimage(datatype, compoundtype, nx, ny, 1, nh);
645  porth->sampling(sampling(0));
646  porth->fill(image[nn].background());
647 
648  for ( h=0; h<nh; h++ ) {
649  porth->background(h, background(nn));
650  if ( h%2 == 0 ) ori[0] = image[nn].origin()[0] - start[0];
651  else ori[0] = image[nn].origin()[2] - start[2];
652  if ( h < 2 ) ori[1] = image[nn].origin()[1] - start[1];
653  else ori[1] = image[nn].origin()[2] - start[2];
654  porth->origin(h, ori);
655  if ( h == 0 ) porth->image[h].view(0,0,1,0);
656  else if ( h == 1 ) porth->image[h].view(1,0,0,0);
657  else porth->image[h].view(0,1,0,0);
658  xo = voxel[0];
659  yo = voxel[1];
660  zo = voxel[2];
661  for ( yy=0; yy<ny; yy++ ) {
662  yf = 1;
663  if ( h < 2 ) {
664  yo = (long) (yy + start[1]);
665  if ( yo < 0 ) yf = 0;
666  else if ( yo >= y ) yf = 0;
667  } else {
668  zz = yy;
669  zo = (long) (zz + start[2]);
670  if ( zo < 0 ) yf = 0;
671  else if ( zo >= z ) yf = 0;
672  }
673  iy = (h*ny + yy)*nx;
674  for ( xx=0; xx<nx; xx++ ) {
675  xf = 1;
676  if ( h%2 == 0 ) {
677  xo = (long) (xx + start[0]);
678  if ( xo < 0 ) xf = 0;
679  else if ( xo >= x ) xf = 0;
680  } else {
681  zz = xx;
682  zo = (long) (zz + start[2]);
683  if ( zo < 0 ) xf = 0;
684  else if ( zo >= z ) xf = 0;
685  }
686  i = (iy + xx)*porth->c;
687  if ( xf && yf ) {
688  j = index(xo, yo, zo, nn);
689  for ( cc=0; cc<porth->c; cc++, i++, j++ ) porth->set(i, (*this)[j]);
690  }
691  }
692  }
693  }
694 
695  porth->statistics();
696 
697  return porth;
698 }
699 
700 int Bimage::extract_show_chunk(Bimage* pshow, int aflag, long i, long len)
701 {
702  long nn(sn);
703  long zz(sz);
704  double scale(ss);
705 
706  if ( compoundtype == TComplex ) {
707  zz += z/2;
708  if ( zz >= z ) zz -= z;
709  }
710 
711  if ( scale >= 1 ) aflag = 0; // No averaging for enlargements
712 
713  int threshold(0);
714  if ( fabs(smax - smin) < 1e-37 ) threshold = 1;
715 
716  long ic, j, jz, xx, yy, cc, m, xm(x/2), ym(y/2);
717  long xo, yo;
718  double xf, yf;
719  double iscale(1.0/scale), shift = (scale < 1)? (iscale - 1)/2: 0;
720 
721  double* value = new double[c];
722  double dispval[4];
723  double dscale = 255.0/(smax - smin);
724  long ds(pshow->x*pshow->y);
727 
728  // Note that the y-axis is flipped
729  xx = i%pshow->x;
730  yy = i/pshow->x;
731  jz = (nn*z + zz)*y;
732 
733  m = i+len;
734  if ( m > ds ) m = ds;
735  for ( ; i<m; i++, xx++ ) {
736  if ( xx >= pshow->x ) {
737  xx = 0;
738  yy++;
739  }
740  if ( compoundtype == TComplex ) {
741  xf = xx*iscale - xm;
742  yf = (pshow->y - yy - 1)*iscale - ym;
743  if ( xf < 0 ) xf += x;
744  if ( yf < 0 ) yf += y;
745  } else {
746  xf = xx*iscale;
747  yf = (pshow->y - yy - 1)*iscale;
748  }
749  if ( xf >= 0 || xf <= x-1 || yf >=0 || yf <= y-1 ) {
750  yo = (long) (yf + shift);
751  xo = (long) (xf + shift);
752  if ( datatype == Bit ) {
753  j = (jz + yo)*page_size()[0] + xo;
754  pshow->set(i, 255 * (*this)[j]);
755  if ( dscale < 0 ) pshow->set(i, 255 - (*pshow)[i]);
756  } else {
757  if ( aflag ) {
758  for ( cc=0; cc<c; cc++ ) value[cc] = average2D(cc, xf, yf, zz, nn, iscale);
759  } else {
760  for ( j = ((jz + yo)*x + xo)*c, cc=0; cc<c; cc++, j++ )
761  value[cc] = (*this)[j];
762  }
763  if ( compoundtype == TComplex ) {
764  dispval[0] = value[0]*value[0] + value[1]*value[1];
765  } else if ( compoundtype == TCMYK ) {
766  if ( datatype == UCharacter ) for ( cc=0; cc<4; cc++ ) value[cc] /= 255;
767  cmyk = CMYK<double>(value);
768  rgb = RGB<double>(cmyk);
769  for ( cc=0; cc<3; cc++ ) dispval[cc] = 255*rgb[cc];
770  } else {
771  for ( cc=0; cc<pshow->c; cc++ ) dispval[cc] = value[cc];
772  }
773  if ( threshold ) {
774  for ( cc=0; cc<pshow->c && cc<3; cc++ ) {
775  if ( dispval[cc] > smin ) dispval[cc] = 255;
776  else dispval[cc] = 0;
777  }
778  } else {
779  for ( cc=0; cc<pshow->c; cc++ ) {
780  if ( cc < 3 ) dispval[cc] = floor(dscale*(dispval[cc]-smin)+0.5);
781  if ( dispval[cc] > 255 ) dispval[cc] = 255;
782  else if ( dispval[cc] < 0 ) dispval[cc] = 0;
783  }
784  }
785 // cout << dispval[3] << tab;
786  if ( pshow->c > 3 ) dispval[3] = 255;
787 // if ( dispval[3] > 100 ) dispval[3] = 255;
788  for ( cc=0, ic = i*pshow->c; cc<pshow->c; cc++, ic++ )
789  pshow->set(ic, dispval[cc]);
790  }
791  }
792  }
793 // cout << endl;
794 
795  delete [] value;
796 
797  return 0;
798 }
799 
817 {
818  CompoundType ctype;
819 
820  switch ( compoundtype ) {
821  case TSimple: ctype = TSimple; break;
822  case TComplex: ctype = TSimple; break;
823  case TVector3:
824  case TCMYK:
825  case TRGB: ctype = TRGB; break;
826  case TView:
827  case TRGBA: ctype = TRGBA; break;
828  default: ctype = compoundtype;
829  }
830 
831  Bimage* pshow = new Bimage(UCharacter, ctype,
832  (long) (ss*x), (long) (ss*y), 1, 1);
833  pshow->sampling(sampling(0)/ss);
834 
835  long ds = pshow->x*pshow->y;
836 // long chunk_size = get_chunk_size(ds, c);
837  long chunk_size = get_chunk_size(ds);
838 
839 #ifdef HAVE_GCD
840  dispatch_apply((ds - 1)/chunk_size + 1, dispatch_get_global_queue(0, 0), ^(size_t i){
841  extract_show_chunk(pshow, aflag, i*chunk_size, chunk_size);
842  });
843 #else
844 #pragma omp parallel for
845  for ( long i=0; i<ds; i+=chunk_size )
846  extract_show_chunk(pshow, aflag, i, chunk_size);
847 #endif
848 
849  return pshow;
850 }
851 
852 
873  Vector3<long> ext_size, double scale)
874 {
875  if ( compoundtype == TComplex ) {
876  center = vector3_set_PBC(center - size()/2, size());
877  } else {
878  if ( center[0] < 0 || center[1] < 0 || center[2] < 0 ) return NULL;
879  if ( center[0] >= x || center[1] >= y || center[2] >= z ) return NULL;
880  }
881 
882  int threshold(0), xf, yf;
883  if ( fabs(smax - smin) < 1e-37 ) threshold = 1;
884 
885  long i, j, iy, jz, xx, yy, zz, cc;
886  long xo, yo, zo;
887  long h, nh(1);
888  if ( z > 1 ) nh = 3;
889  long nx = (long) (scale*ext_size[0]);
890  long ny = (long) (scale*ext_size[1]);
891  double iscalex = 1.0/scale;
892  double iscaley = 1.0/scale;
893  double iscalez = 1.0/scale;
896 
897  Vector3<long> start = center - (ext_size * 0.5);
898  Vector3<long> end = start + ext_size;
899 
900  if ( verbose & VERB_DEBUG ) {
901  cout << "DEBUG Bimage::extract_magnify: start=" << start << endl;
902  cout << "DEBUG Bimage::extract_magnify: end=" << end << endl;
903  cout << "DEBUG Bimage::extract_magnify: size: nx=" << nx << " ny=" << ny << " c=" << c << endl;
904  }
905 
906  CompoundType ctype;
907 
908  switch ( compoundtype ) {
909  case TSimple: ctype = TSimple; break;
910  case TComplex: ctype = TSimple; break;
911  case TVector3:
912  case TCMYK:
913  case TRGB: ctype = TRGB; break;
914  case TView:
915  case TRGBA: ctype = TRGBA; break;
916  default: ctype = compoundtype;
917  }
918 
919  Bimage* pmag = new Bimage(UCharacter, ctype, nx, ny, 1, nh);
920  pmag->sampling(sampling(nn)/scale);
921 
922  double* value = new double[c];
923  double dispval[4];
924 
925  double dscale = 255.0/(smax - smin);
926 
927  // Note that the y-axis is flipped
928  for ( h=0; h<nh; h++ ) {
929  xo = center[0];
930  yo = center[1];
931  zo = center[2];
932  for ( yy=0; yy<ny; yy++ ) {
933  yf = 1;
934  if ( h < 2 ) {
935  yo = (long) (yy*iscaley + start[1]);
936  if ( compoundtype == TComplex ) {
937  if ( yo < 0 ) yo += y;
938  else if ( yo >= y ) yo -= y;
939  } else {
940  if ( yo < 0 ) yf = 0;
941  else if ( yo >= y ) yf = 0;
942  }
943  } else {
944  zz = yy;
945  zo = (long) (zz*iscalez + start[2]);
946  if ( compoundtype == TComplex ) {
947  if ( zo < 0 ) zo += z;
948  else if ( zo >= z ) yo -= z;
949  } else {
950  if ( zo < 0 ) yf = 0;
951  else if ( zo >= z ) yf = 0;
952  }
953  }
954  iy = (h*ny + ny - 1 - yy)*nx;
955  for ( xx=0; xx<nx; xx++ ) {
956  xf = 1;
957  if ( h%2 == 0 ) {
958  xo = (long) (xx*iscalex + start[0]);
959  if ( compoundtype == TComplex ) {
960  if ( xo < 0 ) xo += x;
961  else if ( xo >= x ) xo -= x;
962  } else {
963  if ( xo < 0 ) xf = 0;
964  else if ( xo >= x ) xf = 0;
965  }
966  } else {
967  zz = xx;
968  zo = (long) (zz*iscalez + start[2]);
969  if ( compoundtype == TComplex ) {
970  if ( zo < 0 ) zo += z;
971  else if ( zo >= z ) zo -= z;
972  } else {
973  if ( zo < 0 ) xf = 0;
974  else if ( zo >= z ) xf = 0;
975  }
976  }
977  if ( xf && yf ) {
978  i = (iy + xx)*pmag->c;
979  jz = (nn*z + zo)*y;
980  if ( datatype == Bit ) {
981  j = (jz + yo)*page_size()[0] + xo;
982  pmag->set(i, 255 * (*this)[j]);
983  if ( dscale < 0 ) pmag->set(i, 255 - (*pmag)[i]);
984  } else {
985  j = ((jz + yo)*x + xo)*c;
986  for ( cc=0; cc<c; cc++, j++ ) value[cc] = (*this)[j];
987  if ( compoundtype == TComplex ) {
988  dispval[0] = value[0]*value[0] + value[1]*value[1];
989  } else if ( compoundtype == TCMYK ) {
990  if ( datatype == UCharacter ) for ( cc=0; cc<4; cc++ ) value[cc] /= 255;
991  cmyk = CMYK<double>(value);
992  rgb = RGB<double>(cmyk);
993  for ( cc=0; cc<3; cc++ ) dispval[cc] = 255*rgb[cc];
994  } else {
995  for ( cc=0; cc<pmag->c; cc++ ) dispval[cc] = value[cc];
996  }
997  if ( threshold ) {
998  for ( cc=0; cc<pmag->c; cc++ ) {
999  if ( dispval[cc] > smin ) dispval[cc] = 255;
1000  else dispval[cc] = 0;
1001  }
1002  } else {
1003  for ( cc=0; cc<pmag->c; cc++ ) {
1004  dispval[cc] = floor(dscale*(dispval[cc]-smin)+0.5);
1005  if ( dispval[cc] > 255 ) dispval[cc] = 255;
1006  else if ( dispval[cc] < 0 ) dispval[cc] = 0;
1007  }
1008  }
1009  for ( cc=0; cc<pmag->c; cc++, i++ ) pmag->set(i, dispval[cc]);
1010  }
1011  }
1012  }
1013  }
1014  }
1015 
1016  delete [] value;
1017 
1018  return pmag;
1019 }
1020 
1037 {
1038  int threshold(0);
1039  if ( fabs(smax - smin) < 1e-37 ) threshold = 1;
1040 
1041  long i, j, xx, yy, cc, nn;
1042  RGB<double> rgb;
1044 
1045  if ( verbose & VERB_DEBUG )
1046  cout << "DEBUG Bimage::extract_slice: nz=" << nz << endl;
1047 
1048  CompoundType ctype;
1049 
1050  switch ( compoundtype ) {
1051  case TSimple: ctype = TSimple; break;
1052  case TComplex: ctype = TSimple; break;
1053  case TVector3:
1054  case TCMYK:
1055  case TRGB: ctype = TRGB; break;
1056  case TView:
1057  case TRGBA: ctype = TRGBA; break;
1058  default: ctype = compoundtype;
1059  }
1060 
1061  Bimage* pslice = new Bimage(UCharacter, ctype, x, y, 1, n);
1062  pslice->sampling(sampling(0));
1063 
1064  double* value = new double[c];
1065  double dispval[4];
1066 
1067  double dscale = 255.0/(smax - smin);
1068 
1069  if ( verbose & VERB_DEBUG )
1070  cout << "DEBUG Bimage::extract_slice: dscale=" << dscale << endl;
1071 
1072  // Note that the y-axis is flipped
1073  for ( i=nn=0; nn<n; nn++ ) {
1074  j = (nn*z + nz)*x*y*c;
1075  for ( yy=0; yy<y; yy++ ) {
1076  for ( xx=0; xx<x; xx++ ) {
1077  if ( datatype == Bit ) {
1078  j *= page_size()[0];
1079  pslice->set(i, 255 * (*this)[j]);
1080  if ( dscale < 0 ) pslice->set(i, 255 - (*pslice)[i]);
1081  } else {
1082  for ( cc=0; cc<c; cc++, j++ ) value[cc] = (*this)[j];
1083  if ( compoundtype == TComplex ) {
1084  dispval[0] = value[0]*value[0] + value[1]*value[1];
1085  } else if ( compoundtype == TCMYK ) {
1086  if ( datatype == UCharacter ) for ( cc=0; cc<4; cc++ ) value[cc] /= 255;
1087  cmyk = CMYK<double>(value);
1088  rgb = RGB<double>(cmyk);
1089  for ( cc=0; cc<3; cc++ ) dispval[cc] = 255*rgb[cc];
1090  } else {
1091  for ( cc=0; cc<pslice->c; cc++ ) dispval[cc] = value[cc];
1092  }
1093  if ( threshold ) {
1094  for ( cc=0; cc<pslice->c; cc++ ) {
1095  if ( dispval[cc] > smin ) dispval[cc] = 255;
1096  else dispval[cc] = 0;
1097  }
1098  } else {
1099  for ( cc=0; cc<pslice->c; cc++ ) {
1100  dispval[cc] = floor(dscale*(dispval[cc]-smin)+0.5);
1101  if ( dispval[cc] > 255 ) dispval[cc] = 255;
1102  else if ( dispval[cc] < 0 ) dispval[cc] = 0;
1103  }
1104  }
1105  for ( cc=0; cc<pslice->c; cc++, i++ ) pslice->set(i, dispval[cc]);
1106  }
1107  }
1108  }
1109  }
1110 
1111  delete [] value;
1112 
1113  return pslice;
1114 }
1115 
1128 Bimage* Bimage::extract_filament(long img_num, double width,
1129  int axis, long nspline, Vector3<double>* spline)
1130 {
1131  if ( !d.uc ) return NULL;
1132  if ( nspline < 1 ) return NULL;
1133 
1134  if ( axis < 1 || axis > 3 ) { // Default axis selection
1135  if ( z > 1 ) axis = 3;
1136  else axis = 1;
1137  }
1138 
1139  Vector3<long> filsize((long)width, (long)width, (long)width);
1140  filsize[axis-1] = nspline;
1141  filsize = filsize.min(size());
1142 
1143  Bimage* pfil = new Bimage(Float, compoundtype, filsize, 1);
1144  pfil->sampling(sampling(0));
1145  pfil->label(label());
1146 
1147  long i, xx, yy, zz, s(0);
1148  double w, halfwidth(width/2);
1149  Vector3<double> vref, v, vf;
1150  Matrix3 mat(1);
1151 
1152  vref[axis-1] = 1;
1153 
1154  if ( verbose & VERB_DEBUG ) {
1155  cout << "DEBUG Bimage:extract_filament: img_num=" << img_num << " width=" << width << " nspline=" << nspline << endl;
1156  cout << "DEBUG Bimage:extract_filament: axis=" << axis << " size=" << filsize << endl;
1157  }
1158 
1159  for ( i=zz=0; zz<pfil->z; zz++ ) {
1160  if ( axis == 3 ) s = zz;
1161  else if ( pfil->z > 1 ) vf[2] = zz - halfwidth;
1162  for ( yy=0; yy<pfil->y; yy++ ) {
1163  if ( axis == 2 ) s = yy;
1164  else if ( pfil->y > 1 ) vf[1] = yy - halfwidth;
1165  for ( xx=0; xx<pfil->x; xx++, i++ ) {
1166  if ( axis == 1 ) s = xx;
1167  else if ( pfil->x > 1 ) vf[0] = xx - halfwidth;
1168  if ( s < nspline ) {
1169  if ( s > 0 ) v = spline[s] - spline[s-1];
1170  else v = spline[1] - spline[0];
1171  v.normalize();
1172  mat = Matrix3(vref, v);
1173 // cout << mat << endl;
1174  v = mat * vf;
1175  v += spline[s];
1176  w = interpolate(v, img_num, background(img_num));
1177  pfil->add(i, w);
1178  }
1179  }
1180  }
1181  }
1182 
1183  return pfil;
1184 }
1185 
Bimage::extract_filament
Bimage * extract_filament(long img_num, double width, int axis, long nspline, Vector3< double > *spline)
Extracts a filament defined by a series of coordinates.
Definition: Bimage_extract.cpp:1128
TView
@ TView
Definition: Bimage.h:62
spline.h
Header file for spline interpolation.
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::label
string & label()
Definition: Bimage.h:251
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
get_chunk_size
size_t get_chunk_size(size_t datasize)
Returns the chunk size per thread.
Definition: utilities.cpp:609
UCharacter
@ UCharacter
Definition: utilities.h:153
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::extract_tetrahedron
Bimage * extract_tetrahedron(Vector3< double > *tet, int fill_type=0, double fill=0)
Extracts a tetrahedral part of the image.
Definition: Bimage_extract.cpp:542
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::extract_wrap
Bimage * extract_wrap(long nn, Vector3< double > loc, Vector3< long > size, Vector3< double > origin, Matrix3 mat)
Extracts a region of one sub-image into new image structure with wrapping.
Definition: Bimage_extract.cpp:151
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Bimage::center
int center(int fill_type=0, double fill=0)
Centers an image.
Definition: Bimage_transform.cpp:694
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Vector3< long >
find_optimal_tiling
long find_optimal_tiling(long target, long tile_size, double fraction=0.2)
Definition: Bimage_extract.cpp:411
TypePointer::uc
unsigned char * uc
Definition: Bimage.h:70
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
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
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
RGB
Definition: Color.h:24
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:249
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
TVector3
@ TVector3
Definition: Bimage.h:61
Bimage::extract_slice
Bimage * extract_slice(long nz)
Extracts a given slice or slices from an image.
Definition: Bimage_extract.cpp:1036
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::background
double background()
Definition: Bimage.h:125
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Vector3::normal
Vector3 normal(Vector3 &v1, Vector3 &v2)
Definition: Vector3.h:269
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Vector3::normalize
double normalize()
Definition: Vector3.h:238
TRGBA
@ TRGBA
Definition: Bimage.h:64
verbose
int verbose
TRGB
@ TRGB
Definition: Bimage.h:63
Bimage::extract_show
Bimage * extract_show(int aflag)
Converts a slice from an image to a 2D plane for display.
Definition: Bimage_extract.cpp:816
CMYK
Definition: Color.h:264
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::extract_tiles
Bimage * extract_tiles(long nn, vector< Vector3< long >> &coords, Vector3< long > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Definition: Bimage_extract.cpp:331
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::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bimage.h
Header file for image class.
vector3_set_PBC
Vector3< T1 > vector3_set_PBC(Vector3< T1 > coord, Vector3< T2 > box)
Definition: Vector3.h:684
Bimage::extract_magnify
Bimage * extract_magnify(long nn, Vector3< long > center, Vector3< long > ext_size, double scale)
Extracts a region from an image to magnify.
Definition: Bimage_extract.cpp:872
CompoundType
CompoundType
Compound data type specifier.
Definition: Bimage.h:58
utilities.h
Header file for general utilities.
Bimage::extract_show_chunk
int extract_show_chunk(Bimage *pshow, int aflag, long i, long len)
Definition: Bimage_extract.cpp:700
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
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
Bimage::tile_coordinates
vector< Vector3< long > > tile_coordinates(Vector3< long > &start, Vector3< long > &region, Vector3< long > &tile_size, Vector3< long > &step_size, int exceed)
Generates a set of tile coordinates for an image.
Definition: Bimage_extract.cpp:259
Bimage::extract_shell
Bimage * extract_shell(long nn, double minrad, double maxrad)
Extracts a shell from an image into a new image.
Definition: Bimage_extract.cpp:193
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
TCMYK
@ TCMYK
Definition: Bimage.h:65
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::extract_line
Bimage * extract_line(long nn, Vector3< double > start, Vector3< double > end, long width)
Extracts a line from an image into a new image.
Definition: Bimage_extract.cpp:505
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
Bimage::cmyk
CMYK< double > cmyk(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1083
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Vector3::min
Type min()
Definition: Vector3.h:169
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Vector3::length
double length()
Definition: Vector3.h:222
Bimage::within_boundaries
bool within_boundaries(Vector3< double > loc)
Definition: Bimage.h:461
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
Float
@ Float
Definition: utilities.h:161
Bimage::rgb
RGB< double > rgb(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1055
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::orthogonal_slices
Bimage * orthogonal_slices(long nn, Vector3< long > voxel, Vector3< long > ext_size)
Extracts orthogonal views around a voxel.
Definition: Bimage_extract.cpp:616
scalar
double scalar(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:613
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::label
void label(string s)
Definition: Bimage.h:250
Vector3::max
Type max()
Definition: Vector3.h:175