Bsoft  2.1.0
Bernard's software package
rwTIFF.cpp
Go to the documentation of this file.
1 
9 #include "rwTIFF.h"
10 #include "utilities.h"
11 
12 #ifdef HAVE_TIFF
13 
14 #include "tiff.h"
15 #include "tiffio.h"
16 #include "tiffiop.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 extern char* month[];
22 
36 int readTIFF(Bimage* p, int readdata, int img_select)
37 {
38  if ( verbose & VERB_DEBUG )
39  cout << "DEBUG readTIFF: Reading image: " << p->file_name() << endl;
40 
41  TIFF* fimg;
42  if ( ( fimg = TIFFOpen(p->file_name().c_str(), "r") ) == NULL ) return -1;
43 
44  int flags(0);
45  if ( verbose & VERB_DEBUG ) {
46 // cout << "Magic number:" << fimg->tif_header.tiff_magic << endl;
47  flags = TIFFPRINT_STRIPS & TIFFPRINT_CURVES & TIFFPRINT_COLORMAP;
48  TIFFPrintDirectory(fimg, stdout, flags);
49  }
50 
51  // Map the parameters
52  long i, n, iz, iy, j, jz, jy, k;
53  uint32 x(1), y(1), z(1), px, py, pz;
54  uint16 compression(COMPRESSION_NONE);
55  uint16 samplesperpixel(1), bitspersample(0), lutsize(0);
56  uint16 sampleformat(SAMPLEFORMAT_UINT), photometric(0);
57  uint16 planarconfig(PLANARCONFIG_CONTIG);
58  uint16 orientation(ORIENTATION_BOTLEFT);
59  float xresolution(1), yresolution(1);
60  uint16* red = NULL;
61  uint16* green = NULL;
62  uint16* blue = NULL;
63 
64  // Get the number of images in the file
65  int notdone(1), nimg(0);
66 
67  if ( img_select > -1 ) {
68  while ( TIFFReadDirectory(fimg) ) nimg++;
69  if ( img_select > nimg ) img_select = nimg;
70  p->images(1);
71  TIFFSetDirectory(fimg, img_select);
72  TIFFGetField(fimg, TIFFTAG_IMAGEWIDTH, &x);
73  TIFFGetField(fimg, TIFFTAG_IMAGELENGTH, &y);
74  TIFFGetField(fimg, TIFFTAG_IMAGEDEPTH, &z);
75  p->size(x, y, z);
76  } else {
77  do {
78  TIFFGetField(fimg, TIFFTAG_IMAGEWIDTH, &x); // Test if image is the same size as the first image
79  TIFFGetField(fimg, TIFFTAG_IMAGELENGTH, &y);
80  TIFFGetField(fimg, TIFFTAG_IMAGEDEPTH, &z);
81  if ( nimg == 0 ) {
82  p->size(x, y, z);
83  nimg++;
84  } else {
85  if ( x != p->sizeX() || y != p->sizeY() || z != p->sizeZ() ) {
86  cerr << "Warning: TIFF image " << nimg << " is of a different size: " << x << "," << y << "," << z << endl;
87  cerr << " ---> Only " << nimg << " images read!" << endl;
88  notdone = 0;
89  } else nimg++;
90  }
91  } while ( notdone && TIFFReadDirectory(fimg) );
92  p->images(nimg);
93  TIFFSetDirectory(fimg, 0);
94  }
95 
96  tm* t = p->get_localtime();
97  char* timestring = NULL, mon[4];
98  TIFFGetField(fimg, TIFFTAG_DATETIME, &timestring);
99  if ( timestring ) {
100  if ( verbose & VERB_DEBUG )
101  cout << "TIFF timestring: " << timestring << endl;
102  if ( isdigit(timestring[0]) ) {
103  sscanf(timestring, "%4d:%2d:%2d %2d:%2d:%2d",
104  &t->tm_year, &t->tm_mon, &t->tm_mday,
105  &t->tm_hour, &t->tm_min, &t->tm_sec);
106  t->tm_mon -= 1;
107  } else {
108  sscanf(timestring, "%3s %3s %2d %2d:%2d:%2d %4d",
109  mon, mon, &t->tm_mday, &t->tm_hour, &t->tm_min, &t->tm_sec, &t->tm_year);
110  for ( i=0; strncmp(mon, month[i], 3) != 0; i++ ) ;
111  if ( i < 12 ) t->tm_mon = i;
112  }
113  t->tm_year -= 1900;
114  p->set_time(t);
115  }
116 
117  TIFFGetField(fimg, TIFFTAG_COMPRESSION, &compression);
118 
119  TIFFGetField(fimg, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
120 
121  // Allocate the sub-image array structure
122 // cout << "***** TIFF images read = " << p->n << endl;
123 
124  p->channels(samplesperpixel);
125  if ( p->sizeX() < 1 ) p->sizeX(1);
126  if ( p->sizeY() < 1 ) p->sizeY(1);
127  if ( p->sizeZ() < 1 ) p->sizeZ(1);
128  if ( p->channels() < 1 ) p->channels(1);
129  p->page_size(p->size());
130  if ( TIFFIsTiled(fimg) ) {
131  TIFFGetField(fimg, TIFFTAG_TILEWIDTH, &x);
132  TIFFGetField(fimg, TIFFTAG_TILELENGTH, &y);
133  TIFFGetField(fimg, TIFFTAG_TILEDEPTH, &z);
134  p->page_size(x, y, z);
135  }
136 
137  TIFFGetField(fimg, TIFFTAG_XRESOLUTION, &xresolution);
138  TIFFGetField(fimg, TIFFTAG_YRESOLUTION, &yresolution);
139  if ( verbose & VERB_DEBUG )
140  cout << "DEBUG readTIFF: xresolution=" << xresolution << " yresolution=" << yresolution << endl;
141  p->sampling(xresolution, yresolution, 1);
142  if ( p->sizeZ() > 1 && fabs(p->sampling(0)[0] - p->sampling(0)[1]) < 0.001 )
143  p->sampling(xresolution, yresolution, xresolution);
144 
145  TIFFGetField(fimg, TIFFTAG_BITSPERSAMPLE, &bitspersample);
146  TIFFGetField(fimg, TIFFTAG_PHOTOMETRIC, &photometric);
147  TIFFGetField(fimg, TIFFTAG_SAMPLEFORMAT, &sampleformat);
148  TIFFGetField(fimg, TIFFTAG_ORIENTATION, &orientation);
149 
150  if ( verbose & VERB_DEBUG )
151  cout << "DEBUG readTIFF: bitspersample=" << bitspersample << " tiff datatype=" << sampleformat << endl;
152 
153  switch ( photometric ) {
154  case PHOTOMETRIC_MINISWHITE:
155  case PHOTOMETRIC_MINISBLACK:
156  p->compound_type(TSimple); break;
157  case PHOTOMETRIC_RGB:
158  case PHOTOMETRIC_YCBCR:
159  p->compound_type(TRGB);
160  if ( p->channels() == 4 ) p->compound_type(TRGBA);
161  break;
162  case PHOTOMETRIC_SEPARATED:
163  p->compound_type(TCMYK);
164  break;
165  case PHOTOMETRIC_PALETTE:
166  p->compound_type(TRGB);
167  lutsize = 1;
168  for ( i=0; i<bitspersample; i++ ) lutsize *= 2;
169  if ( verbose & VERB_DEBUG )
170  cout << "DEBUG readTIFF: lutsize=" << lutsize << endl;
171  break;
172  default:
174  }
175 
176  switch ( bitspersample ) {
177  case 1:
178  p->data_type(Bit);
179  break;
180  case 8:
181  p->data_type(UCharacter);
182  if ( sampleformat == SAMPLEFORMAT_INT )
183  p->data_type(SCharacter);
184  break;
185  case 16:
186  p->data_type(UShort);
187  if ( sampleformat == SAMPLEFORMAT_INT )
188  p->data_type(Short);
189  break;
190  case 32:
191  p->data_type(UInteger);
192  if ( sampleformat == SAMPLEFORMAT_INT )
193  p->data_type(Integer);
194  else if ( sampleformat == SAMPLEFORMAT_IEEEFP )
195  p->data_type(Float);
196  break;
197  case 64:
198  p->data_type(ULong);
199  if ( sampleformat == SAMPLEFORMAT_INT )
200  p->data_type(Long);
201  else if ( sampleformat == SAMPLEFORMAT_IEEEFP )
202  p->data_type(Double);
203  break;
204  default:
205  p->data_type(UCharacter);
206  }
207 
208  if ( verbose & VERB_DEBUG )
209  cout << "DEBUG readTIFF: Datatype=" << p->data_type() << " Compoundtype=" << p->compound_type() << endl;
210 
211  uint16 min(0), max(0);
212  double dmin(0), dmax(0);
213  if ( p->data_type() <= Short ) {
214  TIFFGetField(fimg, TIFFTAG_MINSAMPLEVALUE, &min);
215  TIFFGetField(fimg, TIFFTAG_MAXSAMPLEVALUE, &max);
216  p->minimum(min);
217  p->maximum(max);
218  } else {
219  TIFFGetField(fimg, TIFFTAG_SMINSAMPLEVALUE, &dmin);
220  TIFFGetField(fimg, TIFFTAG_SMAXSAMPLEVALUE, &dmax);
221  p->minimum(dmin);
222  p->maximum(dmax);
223  }
224 
225  char* label = NULL;
226  TIFFGetField(fimg, TIFFTAG_IMAGEDESCRIPTION, &label);
227  if ( label ) p->label(label);
228 
229  if ( verbose & VERB_DEBUG )
230  cout << "DEBUG readTIFF: Minimum and maximum: " << p->minimum() << " " << p->maximum() << endl;
231 
232  int tilesize;
233  long datatypesize = p->data_type_size();
234  long elementsize = p->channels()*datatypesize;
235  long linesize = p->sizeX()*elementsize;
236  unsigned char* data = NULL;
237  char *page;
238 
239  if ( p->data_type() == Bit ) linesize = p->page_size()[0]/8;
240 
241  TIFFGetField(fimg, TIFFTAG_PLANARCONFIG, &planarconfig);
242  if ( samplesperpixel > 1 && planarconfig == PLANARCONFIG_SEPARATE ) {
243  cerr << "Error: Separate planes not supported!" << endl;
244 // bexit(-1);
245  }
246 
247  if ( verbose & VERB_DEBUG )
248  cout << "DEBUG readTIFF: readdata=" << readdata << " datasize=" <<
249  p->data_size() << " linesize=" << linesize << endl;
250 
251  if ( !readdata ) {
252  TIFFClose(fimg);
253  if ( lutsize ) {
254  p->data_type(UShort);
255  p->channels(3);
256  }
257  return 0;
258  }
259 
260  data = p->data_alloc();
261 
262  unsigned short* rgbdata = NULL;
263  unsigned short idx;
264  long datasize = p->images()*p->sizeX()*p->sizeY()*p->sizeZ();
265  if ( lutsize ) {
266  if ( verbose & VERB_DEBUG )
267  cout << "DEBUG readTIFF: lutsize=" << lutsize << endl;
268  rgbdata = new unsigned short[3*datasize];
269  }
270 
271  n = nimg = 0;
272  do {
273  if ( lutsize ) {
274  TIFFGetField(fimg, TIFFTAG_COLORMAP, &red, &green, &blue);
275  if ( verbose & VERB_DEBUG ) {
276  cout << "DEBUG readTIFF: lut:" << endl;
277  for ( i=0; i<lutsize; i++ )
278  cout << i << tab << red[i] << tab << green[i] << tab << blue[i] << endl;
279  }
280  }
281  TIFFGetField(fimg, TIFFTAG_PLANARCONFIG, &planarconfig);
282  if ( samplesperpixel > 1 && planarconfig == PLANARCONFIG_SEPARATE ) {
283  cerr << "Error: Separate planes not supported!" << endl;
284  bexit(-1);
285  }
286  if ( TIFFIsTiled(fimg) ) {
287  TIFFGetField(fimg, TIFFTAG_TILEWIDTH, &x);
288  TIFFGetField(fimg, TIFFTAG_TILELENGTH, &y);
289  TIFFGetField(fimg, TIFFTAG_TILEDEPTH, &z);
290  p->page_size(x, y, z);
291  tilesize = TIFFTileSize(fimg);
292 
293  if ( verbose & VERB_DEBUG )
294  cout << "DEBUG readTIFF: tile size: " << x << " x " << y
295  << " x " << z << " = " << tilesize << endl;
296 
297  page = new char[tilesize];
298  for ( pz=0; pz<p->sizeZ(); pz+=p->page_size()[2] ) {
299  for ( py=0; py<p->sizeY(); py+=p->page_size()[1] ) {
300  for ( px=0; px<p->sizeX(); px+=p->page_size()[0] ) {
301  TIFFReadTile(fimg, (char *) page, px, py, pz, 0);
302  for ( z=0; z<p->page_size()[2] && z<p->sizeZ()-pz; z++ ) {
303  iz = (nimg*p->sizeZ() + z + pz)*p->sizeY();
304  jz = z*p->page_size()[1];
305  for ( y=0; y<p->page_size()[1] && y<p->sizeY()-py; y++ ) {
306  iy = (iz + y + py)*p->sizeX();
307  jy = (jz + y)*p->page_size()[0];
308  if ( p->data_type() == Bit ) {
309  iy /= 8;
310  jy /= 8;
311  memcpy(data+iy, page+jy, linesize);
312  } else {
313  for ( x=0; x<p->page_size()[0] && x<p->sizeX()-px; x++ ) {
314  i = (iy + x + px)*elementsize;
315  j = (jy + x)*elementsize;
316  memcpy(data+i, page+j, elementsize);
317  }
318  }
319  }
320  }
321  }
322  }
323  }
324  delete[] page;
325  } else {
326 
327  linesize = TIFFScanlineSize(fimg);
328  if ( verbose & VERB_DEBUG )
329  cout << "DEBUG readTIFF: scanline size=" << linesize << endl;
330 
331  i = nimg*p->sizeZ()*p->sizeY()*linesize;
332  for ( z=0; z<p->sizeZ(); z++ ) {
333  for ( y=0; y<p->sizeY(); y++, i+=linesize ) {
334  // Note: Scanline rows are ambiguous for 3D images
335  TIFFReadScanline(fimg, data+i, y, 0);
336  if ( lutsize ) {
337  if ( bitspersample == 8 ) {
338  for ( x=0, j=i, k=3*j; x<p->sizeX(); x++, j++ ) {
339  idx = data[j];
340  rgbdata[k++] = red[idx];
341  rgbdata[k++] = green[idx];
342  rgbdata[k++] = blue[idx];
343  }
344  } else {
345  unsigned short* usdata = (unsigned short *) data;
346  for ( x=0, j=i/2, k=3*j; x<p->sizeX(); x++, j++ ) {
347  idx = usdata[j];
348  rgbdata[k++] = red[idx];
349  rgbdata[k++] = green[idx];
350  rgbdata[k++] = blue[idx];
351  }
352  }
353  }
354  }
355  }
356  }
357  p->image[nimg].view(0,0,1,0);
358  nimg++;
359  if ( verbose & VERB_DEBUG )
360  cout << "DEBUG readTIFF: image " << n << " read" << endl;
361 // }
362  n++;
363  } while ( n < p->images() && TIFFReadDirectory(fimg) );
364 
365  TIFFClose(fimg);
366 
367  if ( verbose & VERB_DEBUG )
368  cout << "DEBUG readTIFF: images read: " << p->images() << endl << endl;
369 
370  if ( lutsize ) {
371  p->data_type(UShort);
372  p->channels(3);
373  p->data_assign((unsigned char *) rgbdata);
374  }
375 
376  // Adherance to conventions:
377  // Density is positive-valued displayed as white on black background
378  // Display orientation is with the origin at the bottom left
379  Bstring order("xyz");
380  if ( p->data_pointer() ) {
381  if ( verbose & VERB_DEBUG )
382  cout << "DEBUG readTIFF: Tiff photometric order: " << photometric << endl;
383  if ( photometric == PHOTOMETRIC_MINISWHITE ) p->invert();
384  switch ( orientation ) {
385  case ORIENTATION_TOPLEFT: order = "x-yz"; break;
386  case ORIENTATION_TOPRIGHT: order = "-x-yz"; break;
387  case ORIENTATION_BOTRIGHT: order = "-xyz"; break;
388  case ORIENTATION_LEFTTOP: order = "-yxz"; break;
389  case ORIENTATION_RIGHTTOP: order = "-y-xz"; break;
390  case ORIENTATION_RIGHTBOT: order = "y-xz"; break;
391  case ORIENTATION_LEFTBOT: order = "yxz"; break;
392  default: order = "xyz";
393  }
394  if ( verbose & VERB_DEBUG )
395  cout << "DEBUG readTIFF: Tiff image order: " << order << endl;
396  if ( orientation != ORIENTATION_BOTLEFT ) p->reslice(order);
397  }
398 
399  return 0;
400 }
401 
413 int writeTIFF(Bimage* p, int flags)
414 {
415  TIFF *fimg;
416  if ( ( fimg = TIFFOpen(p->file_name().c_str(), "w") ) == NULL ) return -1;
417 
418  // Map the parameters
419  int istiled = flags & 1;
420 // uint16 compression = flags & 62;
421 // uint16 compression(COMPRESSION_LZW);
422  uint16 compression(flags & COMPRESSION_LZW);
423  long i, j, y, z, n;
424  uint16 min = (uint16) p->minimum();
425  uint16 max = (uint16) p->maximum();
426  uint16 resolution_unit(1);
427  float xresolution = p->sampling(0)[0];
428  float yresolution = p->sampling(0)[1];
429  uint32 rowsperstrip = (uint32) -1;
430 
431  long datatypesize = p->data_type_size();
432  long datasize = p->sizeX()*p->sizeY()*p->sizeZ()*p->channels()*datatypesize;
433  long linesize = p->sizeX()*p->channels()*datatypesize;
434  long unitsize = 64;
435  long pagesize, tilerowsize(0);
436  char* page;
437 // unsigned char* scanline;
438 
439  if ( p->data_type() == Bit ) {
440  linesize = p->page_size()[0]/8;
441  datasize = linesize*p->sizeY()*p->sizeZ();
442  }
443 
444  p->page_size((int) (unitsize*floor(p->sizeX()*1.0/unitsize + 0.99999)),
445  (int) (unitsize*floor(p->sizeY()*1.0/unitsize + 0.99999)), 1);
446 
447  long tilex = (int) (unitsize*floor(p->sizeX()*1.0/unitsize + 0.99999));
448  long tiley = (int) (unitsize*floor(p->sizeY()*1.0/unitsize + 0.99999));
449  long tilez(1);
450 
451  tm* t = p->get_localtime();
452  char timestring[20];
453  snprintf(timestring, 20, "%4d:%02d:%02d %02d:%02d:%02d",
454  t->tm_year+1900, t->tm_mon+1, t->tm_mday,
455  t->tm_hour, t->tm_min, t->tm_sec);
456 
457  for ( n=0; n<p->images(); n++ ) {
458  TIFFSetDirectory(fimg, n);
459  TIFFSetField(fimg, TIFFTAG_IMAGEWIDTH, p->sizeX());
460  TIFFSetField(fimg, TIFFTAG_IMAGELENGTH, p->sizeY());
461  TIFFSetField(fimg, TIFFTAG_IMAGEDEPTH, p->sizeZ());
462  TIFFSetField(fimg, TIFFTAG_SAMPLESPERPIXEL, p->channels());
463  if ( istiled ) {
464  TIFFSetField(fimg, TIFFTAG_TILEWIDTH, tilex);
465  TIFFSetField(fimg, TIFFTAG_TILELENGTH, tiley);
466  TIFFSetField(fimg, TIFFTAG_TILEDEPTH, tilez);
467  } else
468  TIFFSetField(fimg, TIFFTAG_ROWSPERSTRIP,
469  TIFFDefaultStripSize(fimg, rowsperstrip));
470  if ( compression )
471  TIFFSetField(fimg, TIFFTAG_COMPRESSION, compression);
472 
473  if ( verbose & VERB_DEBUG ) {
474  cout << "DEBUG writeTIFF: Image " << n << ": size " << p->sizeX() << " "
475  << p->sizeY() << " " << p->sizeZ() << " " << p->channels() << endl;
476  cout << "DEBUG writeTIFF: page dimensions: " <<
477  p->page_size()[0] << " " << p->page_size()[1] << " " << p->page_size()[2] << endl;
478  }
479 
480  TIFFSetField(fimg, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
481  TIFFSetField(fimg, TIFFTAG_XRESOLUTION, xresolution);
482  TIFFSetField(fimg, TIFFTAG_YRESOLUTION, yresolution);
483 
484  TIFFSetField(fimg, TIFFTAG_IMAGEDESCRIPTION, p->label().c_str());
485  TIFFSetField(fimg, TIFFTAG_DATETIME, timestring);
486 
487  if ( p->data_type() <= Short ) {
488  TIFFSetField(fimg, TIFFTAG_MINSAMPLEVALUE, min);
489  TIFFSetField(fimg, TIFFTAG_MAXSAMPLEVALUE, max);
490  } else {
491  TIFFSetField(fimg, TIFFTAG_SMINSAMPLEVALUE, p->minimum());
492  TIFFSetField(fimg, TIFFTAG_SMAXSAMPLEVALUE, p->maximum());
493  }
494 
495  if ( verbose & VERB_DEBUG )
496  cout << "DEBUG writeTIFF: min & max: " << min << " " << max << endl;
497 
498  switch ( p->data_type() ) {
499  case Bit:
500  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
501  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 1);
502  break;
503  case UCharacter:
504  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
505  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
506  break;
507  case SCharacter:
508  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
509  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
510  break;
511  case UShort:
512  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
513  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 16);
514  break;
515  case Short:
516  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
517  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 16);
518  break;
519  case UInteger:
520  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
521  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
522  break;
523  case Integer:
524  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
525  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
526  break;
527  case ULong:
528  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
529  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
530  break;
531  case Long:
532  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
533  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
534  break;
535  case Float:
536  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
537  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
538  break;
539  case Double:
540  TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
541  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
542  break;
543  default:
544  TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
545  }
546 
547  int extraTypes[1] = {EXTRASAMPLE_ASSOCALPHA};
548  switch ( p->compound_type() ) {
549  case TSimple:
550  TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
551  break;
552  case TVector3:
553  case TRGB:
554  TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
555  break;
556  case TRGBA:
557  TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
558  TIFFSetField(fimg, TIFFTAG_EXTRASAMPLES, 1, extraTypes);
559  break;
560  case TCMYK:
561  TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
562  TIFFSetField(fimg, TIFFTAG_INKSET, INKSET_CMYK);
563  break;
564  default:
565  TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
566  }
567 
568  TIFFSetField(fimg, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
569  TIFFSetField(fimg, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
570 
571  if ( verbose & VERB_DEBUG ) {
572  cout << "DEBUG writeTIFF: configuration set to planar" << endl;
573  cout << "DEBUG writeTIFF: Typesize=" << datatypesize << " Datasize=" << datasize << endl;
574  if ( istiled )
575  cout << "DEBUG writeTIFF: Tilesize=" << TIFFTileSize(fimg) << endl;
576  else
577  cout << "DEBUG writeTIFF: Scanlinesize=" << TIFFScanlineSize(fimg) << endl;
578  cout << "DEBUG writeTIFF: Pages: ";
579  }
580 
581  if ( istiled ) {
582  pagesize = TIFFTileSize(fimg);
583  page = new char[pagesize];
584  tilerowsize = TIFFTileRowSize(fimg);
585  for ( z=0; z<p->sizeZ(); z++ ) {
586  memset(page, 0, pagesize);
587  for ( y=0; y<p->sizeY(); y++ ) {
588  i = y*tilerowsize;
589  j = ((n*p->sizeZ() + z)*p->sizeY() + y)*linesize;
590  memcpy(page+i, p->data_pointer()+j, linesize);
591  }
592  if ( verbose & VERB_DEBUG ) cout << " " << z;
593  TIFFWriteTile(fimg, page, 0, 0, z, 0);
594  }
595  delete[] page;
596  } else {
597  if ( verbose & VERB_DEBUG )
598  cout << "DEBUG writeTIFF: linesize=" << linesize << endl;
599  for ( z=0; z<p->sizeZ(); z++ ) {
600  for ( y=0; y<p->sizeY(); y++ ) {
601  j = ((n*p->sizeZ() + z)*p->sizeY() + y)*linesize;
602  TIFFWriteScanline(fimg, p->data_pointer()+j, y, 0);
603  // Note: Scanline rows are ambiguous for 3D images
604  }
605  if ( verbose & VERB_DEBUG ) cout << " " << z;
606  }
607  }
608  if ( verbose & VERB_DEBUG ) cout << endl;
609 
610  TIFFWriteDirectory(fimg);
611  } // End of multi-image loop
612 
613  if ( verbose & VERB_DEBUG )
614  cout << "DEBUG writeTIFF: data written" << endl;
615 
616  TIFFClose(fimg);
617 
618  if ( verbose & VERB_DEBUG )
619  cout << "DEBUG writeTIFF: Memory freed" << endl;
620 
621  return 0;
622 }
623 
624 #else
625 
626 int readTIFF(Bimage* p, int readdata, int img_select)
627 {
628  cerr << "Error: TIFF files are not supported!" << endl;
629 
630  return -1;
631 }
632 
633 int writeTIFF(Bimage* p, int istiled)
634 {
635  cerr << "Error: TIFF files are not supported!" << endl;
636 
637  return -1;
638 }
639 
640 #endif
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bimage::get_localtime
tm * get_localtime()
Definition: Bimage.h:260
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
ULong
@ ULong
Definition: utilities.h:159
UCharacter
@ UCharacter
Definition: utilities.h:153
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bstring
Definition: Bstring.h:29
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:264
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::maximum
double maximum()
Definition: Bimage.h:542
TVector3
@ TVector3
Definition: Bimage.h:61
Bimage::images
long images()
Definition: Bimage.h:504
Short
@ Short
Definition: utilities.h:156
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
TRGBA
@ TRGBA
Definition: Bimage.h:64
Bimage::data_size
long data_size()
Definition: Bimage.h:270
UShort
@ UShort
Definition: utilities.h:155
TRGB
@ TRGB
Definition: Bimage.h:63
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
readTIFF
int readTIFF(Bimage *p, int readdata, int img_select)
Definition: rwTIFF.cpp:626
verbose
int verbose
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Integer
@ Integer
Definition: utilities.h:158
Long
@ Long
Definition: utilities.h:160
writeTIFF
int writeTIFF(Bimage *p, int istiled)
Definition: rwTIFF.cpp:633
TCMYK
@ TCMYK
Definition: Bimage.h:65
rwTIFF.h
Header file for reading and writing TIFF files.
SCharacter
@ SCharacter
Definition: utilities.h:154
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
month
const char * month[]
Definition: rwimg.cpp:56
Double
@ Double
Definition: utilities.h:162
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
UInteger
@ UInteger
Definition: utilities.h:157
Bimage::label
void label(string s)
Definition: Bimage.h:250