QXRD  0.11.16
qxrdfilesaver.cpp
Go to the documentation of this file.
1 #define _CRT_SECURE_NO_WARNINGS
2 
3 #include "qxrddebug.h"
4 #include "qxrdfilesaver.h"
5 #include "qxrdacquisition.h"
6 #include "qxrddataprocessor.h"
7 #include "qxrdapplication.h"
8 #include "qcepintegrateddata.h"
9 #include "qxrdexperiment.h"
10 #include "qxrdcenterfinder.h"
11 #include "qxrdintegrator.h"
12 
13 #include <QDir>
14 
15 #include "tiffio.h"
16 
18 (QcepAllocatorWPtr allocator)
19  : QObject(NULL),
20  m_Experiment(),
21  m_Processor(),
22  m_Allocator(allocator),
24 {
26  printf("QxrdFileSaver::QxrdFileSaver(%p)\n", this);
27  }
28 }
29 
31 {
33  printf("QxrdFileSaver::~QxrdFileSaver(%p)\n", this);
34  }
35 }
36 
38 {
39  m_Experiment = expt;
40 }
41 
43 {
44  if (g_Application && m_Experiment == NULL) {
45  g_Application->printMessage(("experiment == NULL in QxrdFileSaver::experiment\n"));
46  }
47 
48  return m_Experiment;
49 }
50 
52 {
53  m_Processor = proc;
54 }
55 
57 {
58  m_Acquisition = acq;
59 }
60 
62 {
63  if (g_Application && m_Acquisition == NULL) {
64  g_Application->printMessage("acquisition == NULL in QxrdFileSaver::acquisition\n");
65  }
66 
67  return m_Acquisition;
68 }
69 
71 {
73 
74  if (proc) {
75  proc -> prop_SaverQueueLength()->incValue(1);
76  }
77 }
78 
80 {
82 
83  if (proc) {
84  proc -> prop_SaverQueueLength()->incValue(-1);
85  }
86 }
87 
88 void QxrdFileSaver::mkPath(QString filePath)
89 {
90  QFileInfo f(filePath);
91  QDir dir = f.dir();
92 
93  if (!dir.exists()) {
94  dir.mkpath(dir.absolutePath());
95  }
96 }
97 
98 QString QxrdFileSaver::uniqueFileName(QString name)
99 {
100  THREAD_CHECK;
101 
102  QFileInfo f(name);
103 
104  if (f.exists()) {
105  QDir dir = f.dir();
106 // QString base = f.baseName();
107 // QString suff = f.completeSuffix();
108  QString base = f.completeBaseName();
109  QString suff = f.suffix();
110 
112 
113  int width = 5;
114 
115  if (acq) {
116  width = acq->get_FileOverflowWidth();
117  }
118 
119  for (int i=1; ; i++) {
120  QString newname = dir.filePath(base+QString().sprintf("-%0*d.",width,i)+suff);
121  QFileInfo f(newname);
122 
123  if (!f.exists()) {
124  return newname;
125  }
126  }
127  } else {
128  return name;
129  }
130 }
131 
132 #define TIFFCHECK(a) if (res && ((a)==0)) { res = 0; }
133 
134 void QxrdFileSaver::saveImageData(QString name, QcepImageDataBasePtr image, QcepMaskDataPtr overflow, int canOverwrite)
135 {
136  incBacklog();
137 
138  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveImageDataPrivate",
139  Qt::QueuedConnection,
140  Q_ARG(QString, name),
141  Q_ARG(QcepImageDataBasePtr, image),
142  Q_ARG(QcepMaskDataPtr, overflow),
143  Q_ARG(int, canOverwrite)));
144 }
145 
146 void QxrdFileSaver::saveImageDataPrivate(QString name, QcepImageDataBasePtr image, QcepMaskDataPtr overflow, int canOverwrite)
147 {
148  if (image == NULL) {
149  if (g_Application) {
150  g_Application->criticalMessage(tr("QxrdFileSaver::saveImageData: image == NULL"));
151  }
152  } else {
153  QcepDoubleImageDataPtr dimage = qSharedPointerDynamicCast<QcepDoubleImageData>(image);
154 
155  if (dimage) {
156  saveDoubleDataPrivate(name, dimage, overflow, canOverwrite);
157  } else {
158  QcepInt32ImageDataPtr i32image = qSharedPointerDynamicCast<QcepInt32ImageData>(image);
159 
160  if (i32image) {
161  saveRaw32DataPrivate(name, i32image, overflow, canOverwrite);
162  } else {
163  QcepInt16ImageDataPtr i16image = qSharedPointerDynamicCast<QcepInt16ImageData>(image);
164 
165  if (i16image) {
166  saveRaw16DataPrivate(name, i16image, overflow, canOverwrite);
167  } else {
168  if (g_Application) {
169  g_Application->criticalMessage(tr("QxrdFileSaver::saveImageData: unknown image type"));
170  }
171 
172  decBacklog();
173  }
174  }
175  }
176  }
177 }
178 
179 void QxrdFileSaver::saveDoubleData(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
180 {
181  incBacklog();
182 
183  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveDoubleDataPrivate",
184  Qt::QueuedConnection,
185  Q_ARG(QString,name),
186  Q_ARG(QcepDoubleImageDataPtr,image),
187  Q_ARG(QcepMaskDataPtr,overflow),
188  Q_ARG(int,canOverwrite)));
189 }
190 
191 void QxrdFileSaver::saveDoubleDataPrivate(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
192 {
193  if (image == NULL) {
194  if (g_Application) {
195  g_Application->criticalMessage(tr("QxrdFileSaver::saveDoubleData: image == NULL"));
196  }
197  } else {
198  QTime tic;
199  tic.start();
200 
201  int nrows = image -> get_Height();
202  int ncols = image -> get_Width();
203 
204  mkPath(name);
205 
206  if (canOverwrite == NoOverwrite) {
207  name = uniqueFileName(name);
208  }
209 
210  TIFF* tif = TIFFOpen(qPrintable(name),"w");
211  int res = 1;
212 
213  if (tif) {
214  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, ncols));
215  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGELENGTH, nrows));
216  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1));
217 
218  TIFFCHECK(TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG));
219  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32));
220  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP));
221 
222  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, qPrintable(image->get_FileName())));
223  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DATETIME, qPrintable(image->get_DateTime().toString("yyyy:MM:dd hh:mm:ss"))));
224 
225  QVector<float> buffvec(ncols);
226  float* buffer = buffvec.data();
227 
228  for (int y=0; y<nrows; y++) {
229  for (int x=0; x<ncols; x++) {
230  buffer[x] = image->value(x,y);
231  }
232 
233  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
234  }
235 
236  TIFFClose(tif);
237 
238  image -> set_FileBase(QFileInfo(name).fileName());
239  image -> set_FileName(name);
240  image -> set_Title(name);
241 
242  image -> set_ObjectSaved(true);
243 
244  image -> saveMetaData();
245 
247 
248  if (proc) {
249  if (proc->get_SaveOverflowFiles()) {
250  saveOverflowData(name, overflow);
251  }
252 
253  proc -> updateEstimatedTime(proc -> prop_SaveSubtractedTime(), tic.elapsed());
254  proc -> set_FileName(name);
255 
256  if (g_Application) {
257  g_Application->printMessage(tr("Saved subtracted data in file \"%1\" after %2 msec").
258  arg(name).arg(tic.restart()));
259  }
260  }
261  } else {
262  res = 0;
263  }
264 
265  if (g_Application && res == 0) {
266  g_Application->printMessage("Error saving file");
267  }
268  }
269 
270  decBacklog();
271 }
272 
273 void QxrdFileSaver::saveInt32Data(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
274 {
275  if (image == NULL) {
276  if (g_Application) {
277  g_Application->criticalMessage(tr("QxrdFileSaver::saveInt32Data: image == NULL"));
278  }
279  } else {
280  saveRaw32Data(name, image, overflow, canOverwrite);
281  }
282 }
283 
284 void QxrdFileSaver::saveInt16Data(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
285 {
286  if (image == NULL) {
287  if (g_Application) {
288  g_Application->criticalMessage(tr("QxrdFileSaver::saveInt16Data: image == NULL"));
289  }
290  } else {
291  saveRaw16Data(name, image, overflow, canOverwrite);
292  }
293 }
294 
295 void QxrdFileSaver::saveMaskData(QString name, QcepMaskDataPtr image, int canOverwrite)
296 {
297  incBacklog();
298 
299  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveMaskDataPrivate",
300  Qt::QueuedConnection,
301  Q_ARG(QString,name),
302  Q_ARG(QcepMaskDataPtr,image),
303  Q_ARG(int,canOverwrite)));
304 }
305 
306 void QxrdFileSaver::saveMaskDataPrivate(QString name, QcepMaskDataPtr image, int canOverwrite)
307 {
308  if (image == NULL) {
309  if (g_Application) {
310  g_Application->criticalMessage(tr("QxrdFileSaver::saveMaskData: image == NULL"));
311  }
312  } else {
313  int nrows = image -> get_Height();
314  int ncols = image -> get_Width();
315 
316  mkPath(name);
317 
318  if (canOverwrite == NoOverwrite) {
319  name = uniqueFileName(name);
320  }
321 
322  TIFF* tif = TIFFOpen(qPrintable(name),"w");
323  int res = 1;
324 
325  if (tif) {
326 
327  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, ncols));
328  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGELENGTH, nrows));
329  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1));
330  TIFFCHECK(TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE));
331  TIFFCHECK(TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG));
332  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8));
333  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT));
334 
335  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, qPrintable(image->get_FileName())));
336  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DATETIME, qPrintable(image->get_DateTime().toString("yyyy:MM:dd hh:mm:ss"))));
337 
338  QVector<quint8> buffvec(ncols);
339  quint8* buffer = buffvec.data();
340 
341  for (int y=0; y<nrows; y++) {
342  for (int x=0; x<ncols; x++) {
343  buffer[x] = image->value(x,y);
344  }
345 
346  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
347  }
348 
349  TIFFClose(tif);
350 
351  image -> set_FileBase(QFileInfo(name).fileName());
352  image -> set_FileName(name);
353  image -> set_Title(name);
354 
355  image -> set_ObjectSaved(true);
356 
357  image -> saveMetaData();
358  } else {
359  res = 0;
360  }
361 
362  if (g_Application && res == 0) {
363  g_Application->printMessage("Error saving file");
364  }
365  }
366 
367  decBacklog();
368 }
369 
370 void QxrdFileSaver::saveRaw32Data(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
371 {
372  incBacklog();
373 
374  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveRaw32DataPrivate",
375  Qt::QueuedConnection,
376  Q_ARG(QString,name),
377  Q_ARG(QcepInt32ImageDataPtr,image),
378  Q_ARG(QcepMaskDataPtr,overflow),
379  Q_ARG(int,canOverwrite)));
380 }
381 
382 void QxrdFileSaver::saveRaw32DataPrivate(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
383 {
384  if (image == NULL) {
385  if (g_Application) {
386  g_Application->criticalMessage(tr("QxrdFileSaver::saveRaw32Data: image == NULL"));
387  }
388  } else {
389  QTime tic;
390  tic.start();
391 
392  int nrows = image -> get_Height();
393  int ncols = image -> get_Width();
394 
395  mkPath(name);
396 
397  if (canOverwrite == NoOverwrite) {
398  name = uniqueFileName(name);
399  }
400 
401  if (g_Application) {
402  g_Application->printMessage(tr("Starting to save %1, fileIndex = %2").arg(name).arg(image->get_ImageNumber()));
403  }
404 
405  TIFF* tif = TIFFOpen(qPrintable(name),"w");
406  int res = 1;
407 
408  if (tif) {
409  int nsum = image->get_SummedExposures();
410 
411  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, ncols));
412  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGELENGTH, nrows));
413  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1));
414 
415  TIFFCHECK(TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG));
416 
417  if (nsum == 0) {
418  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8));
419  } else if (nsum == 1) {
420  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16));
421  } else {
422  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32));
423  }
424 
425  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT));
426 
427  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, qPrintable(image->get_FileName())));
428  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DATETIME, qPrintable(image->get_DateTime().toString("yyyy:MM:dd hh:mm:ss"))));
429 
430  if (nsum == 0) {
431  QVector<quint8> buffvec(ncols);
432  quint8* buffer = buffvec.data();
433 
434  for (int y=0; y<nrows; y++) {
435  for (int x=0; x<ncols; x++) {
436  buffer[x] = image->value(x,y);
437  }
438 
439  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
440  }
441  } else if (nsum == 1) {
442  QVector<quint16> buffvec(ncols);
443  quint16* buffer = buffvec.data();
444 
445  for (int y=0; y<nrows; y++) {
446  for (int x=0; x<ncols; x++) {
447  buffer[x] = image->value(x,y);
448  }
449 
450  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
451  }
452  } else {
453  QVector<quint32> buffvec(ncols);
454  quint32* buffer = buffvec.data();
455 
456  for (int y=0; y<nrows; y++) {
457  for (int x=0; x<ncols; x++) {
458  buffer[x] = image->value(x,y);
459  }
460 
461  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
462  }
463  }
464 
465  TIFFClose(tif);
466 
467  image -> set_FileBase(QFileInfo(name).fileName());
468  image -> set_FileName(name);
469  image -> set_Title(name);
470 
471  image -> set_ObjectSaved(true);
472 
473  image -> saveMetaData(name);
474 
477 
478  if (proc && acq) {
479  if (proc->get_SaveOverflowFiles()) {
480  saveOverflowData(name, overflow);
481  }
482 
483  proc -> updateEstimatedTime(acq -> prop_Raw32SaveTime(), tic.elapsed());
484  proc -> set_FileName(name);
485 
486  if (g_Application) {
487  g_Application->printMessage(tr("Saved 32 bit data in file \"%1\" after %2 msec").
488  arg(name).arg(tic.restart()));
489  }
490  }
491  } else {
492  res = 0;
493  }
494 
495  if (g_Application && res == 0) {
496  g_Application->printMessage("Error saving file");
497  }
498  }
499 
500  decBacklog();
501 }
502 
503 void QxrdFileSaver::saveRaw16Data(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
504 {
505  incBacklog();
506 
507  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveRaw16DataPrivate",
508  Qt::QueuedConnection,
509  Q_ARG(QString,name),
510  Q_ARG(QcepInt16ImageDataPtr,image),
511  Q_ARG(QcepMaskDataPtr,overflow),
512  Q_ARG(int,canOverwrite)))
513 }
514 
515 void QxrdFileSaver::saveRaw16DataPrivate(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
516 {
517  if (image == NULL) {
518  if (g_Application) {
519  g_Application->criticalMessage(tr("QxrdFileSaver::saveRaw16Data: image == NULL"));
520  }
521  } else {
522  QTime tic;
523  tic.start();
524 
525  int nrows = image -> get_Height();
526  int ncols = image -> get_Width();
527 
528  mkPath(name);
529 
530  if (canOverwrite == NoOverwrite) {
531  name = uniqueFileName(name);
532  }
533 
534  TIFF* tif = TIFFOpen(qPrintable(name),"w");
535  int res = 1;
536 
537  if (tif) {
538 
539  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, ncols));
540  TIFFCHECK(TIFFSetField(tif, TIFFTAG_IMAGELENGTH, nrows));
541  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1));
542 
543  TIFFCHECK(TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG));
544  TIFFCHECK(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16));
545  TIFFCHECK(TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT));
546 
547  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, qPrintable(image->get_FileName())));
548  TIFFCHECK(TIFFSetField(tif, TIFFTAG_DATETIME, qPrintable(image->get_DateTime().toString("yyyy:MM:dd hh:mm:ss"))));
549 
550  QVector<quint16> buffvec(ncols);
551  quint16* buffer = buffvec.data();
552 
553  for (int y=0; y<nrows; y++) {
554  for (int x=0; x<ncols; x++) {
555  buffer[x] = image->value(x,y);
556  }
557 
558  TIFFCHECK(TIFFWriteScanline(tif, buffer, y, 0));
559  }
560 
561  TIFFClose(tif);
562 
563  image -> set_FileBase(QFileInfo(name).fileName());
564  image -> set_FileName(name);
565  image -> set_Title(name);
566 
567  image -> set_ObjectSaved(true);
568 
569  image -> saveMetaData(name);
570 
573 
574  if (proc && acq) {
575  if (proc->get_SaveOverflowFiles()) {
576  saveOverflowData(name, overflow);
577  }
578 
579  proc -> updateEstimatedTime(acq -> prop_Raw16SaveTime(), tic.elapsed());
580  proc -> set_FileName(name);
581 
582  if (g_Application) {
583  g_Application->printMessage(tr("Saved 16 bit data in file \"%1\" after %2 msec").
584  arg(name).arg(tic.restart()));
585  }
586  }
587  } else {
588  res = 0;
589  }
590 
591  if (g_Application && res == 0) {
592  g_Application->printMessage("Error saving file");
593  }
594  }
595 
596  decBacklog();
597 }
598 
599 void QxrdFileSaver::saveTextData(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
600 {
601  incBacklog();
602 
603  INVOKE_CHECK(QMetaObject::invokeMethod(this, "saveTextDataPrivate",
604  Qt::QueuedConnection,
605  Q_ARG(QString,name),
606  Q_ARG(QcepDoubleImageDataPtr,image),
607  Q_ARG(QcepMaskDataPtr,overflow),
608  Q_ARG(int,canOverwrite)))
609 }
610 
611 void QxrdFileSaver::saveTextDataPrivate(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
612 {
613  if (image == NULL) {
614  if (g_Application) {
615  g_Application->criticalMessage(tr("QxrdFileSaver::saveTextData: image == NULL"));
616  }
617  } else {
618  int nrows = image -> get_Height();
619  int ncols = image -> get_Width();
620 
621  QFileInfo f(name);
622  QDir dir = f.dir();
623  QString base = f.completeBaseName();
624  QString suff = f.suffix();
625 
626  if (suff == "tif") {
627  name = dir.filePath(base+".txt");
628  }
629 
630  mkPath(name);
631 
632  if (canOverwrite == NoOverwrite) {
633  name = uniqueFileName(name);
634  }
635 
636  FILE* file = fopen(qPrintable(name),"a");
637 
639 
640  QString separator = "\t";
641 
642  if (proc) {
643  separator = proc -> get_SaveAsTextSeparator();
644  }
645 
646  char sep[10];
647  strncpy(sep,qPrintable(separator),10);
648  sep[9]=0;
649  int nperline = 8;
650 
651  if (proc) {
652  nperline = proc -> get_SaveAsTextPerLine();
653  }
654 
655  for (int y=0; y<nrows; y++) {
656  for (int x=0; x<ncols; x++) {
657  if (nperline && ((x % nperline)==0)) {
658  fputs("\n",file);
659  } else if (x!=0) {
660  fputs(sep,file);
661  }
662  fprintf(file,"%g",image->value(x,y));
663  }
664  }
665  fprintf(file,"\n");
666 
667  fclose(file);
668 
669  image -> set_FileBase(QFileInfo(name).fileName());
670  image -> set_FileName(name);
671  image -> set_Title(name);
672 
673  image -> set_ObjectSaved(true);
674 
675  image -> saveMetaData();
676 
677  if (proc) {
678  if (proc->get_SaveOverflowFiles()) {
679  saveOverflowData(name, overflow);
680  }
681 
682  proc -> set_FileName(name);
683  }
684  }
685 
686  decBacklog();
687 }
688 
689 void QxrdFileSaver::writeOutputScan(QString dir, QcepIntegratedDataPtr data, QString fileName)
690 {
691  incBacklog();
692 
693  INVOKE_CHECK(QMetaObject::invokeMethod(this, "writeOutputScanPrivate",
694  Qt::QueuedConnection,
695  Q_ARG(QString,dir),
696  Q_ARG(QcepIntegratedDataPtr,data),
697  Q_ARG(QString,fileName)));
698 }
699 
700 void QxrdFileSaver::writeOutputScanPrivate(QString dir, QcepIntegratedDataPtr data, QString fileName)
701 {
702  if (data == NULL) {
703  if (g_Application) {
704  g_Application->criticalMessage(tr("QxrdFileSaver::writeOutputScan: data == NULL"));
705  }
706  } else {
707  if (fileName.isNull()) {
708  QcepDoubleImageDataPtr image = data -> get_Image();
709  if (image == NULL) {
710  if (g_Application) {
711  g_Application->criticalMessage(tr("QxrdFileSaver::writeOutputScan: image == NULL"));
712  }
713  decBacklog();
714  return;
715  }
716 
717  fileName = image -> get_FileName();
718  }
719 
720  QFileInfo fi(fileName);
721 
722  QString fileBase = fi.completeBaseName();
723  QString extension = ".avg";
724 
726 
727  if (exp) {
728  extension = exp->get_ScanFileExtension();
729  }
730 
731  QString name = QDir(dir).filePath(fileBase+extension);
732 
733  mkPath(name);
734 
735  name = uniqueFileName(name);
736 
737  FILE *f = fopen(qPrintable(name),"a");
738 
739  if (f == NULL) {
740  if (g_Application) {
741  g_Application->printMessage(tr("Couldn't open file %1").arg(name));
742  }
743  } else {
744  writeOutputScanPrivate(f, data, fileName);
745 
746  fclose(f);
747 
748  if (g_Application) {
749  g_Application->printMessage(tr("Integrated data saved in %1").arg(name));
750  }
751 
752  return;
753  }
754  }
755 
756  decBacklog();
757 }
758 
759 void QxrdFileSaver::writeOutputScan(FILE* logFile, QcepIntegratedDataPtr data, QString fileName)
760 {
761  incBacklog();
762 
763  INVOKE_CHECK(QMetaObject::invokeMethod(this, "writeOutputScanPrivate",
764  Qt::QueuedConnection,
765  Q_ARG(FILE*, logFile),
766  Q_ARG(QcepIntegratedDataPtr,data),
767  Q_ARG(QString,fileName)))
768 }
769 
770 void QxrdFileSaver::writeOutputScanPrivate(FILE* logFile, QcepIntegratedDataPtr data, QString fileName)
771 {
772  if (data == NULL) {
773  if (g_Application) {
774  g_Application->criticalMessage(tr("QxrdFileSaver::writeOutputScan: data == NULL"));
775  }
776  } else if (logFile) {
777  QTime tic;
778  tic.start();
779  int imageNumber = 0;
780 
781  if (fileName.isNull()) {
782  QcepDoubleImageDataPtr image = data -> get_Image();
783  if (image == NULL) {
784  if (g_Application) {
785  g_Application->criticalMessage(tr("QxrdFileSaver::writeOutputScan: image == NULL"));
786  }
787  return;
788  }
789 
790  fileName = image -> get_FileName();
791  imageNumber = image -> get_ImageNumber();
792  }
793 
794  fprintf(logFile, "#S %d qxrd.integrate \"%s\" %g %g\n",
795  imageNumber,
796  qPrintable(fileName),
797  data -> cx(),
798  data -> cy());
799 
800  fprintf(logFile, "#D %s\n", qPrintable(QDateTime::currentDateTime().toString("ddd MMM d hh:mm:ss yyyy")));
801  fprintf(logFile, "#N 2\n");
802  fprintf(logFile, "#L x y\n");
803 
804  int n = data->size();
805  const double *x = data->x();
806  const double *y = data->y();
807 
808  int negh = 0;
810 
811  if (exp) {
812  negh = exp->get_ScanDataNegative();
813  }
814 
815  if (negh == 0) {
816  for (int i=0; i<n; i++) {
817  fprintf(logFile, "%E %E\n", x[i], y[i]);
818  }
819  } else if (negh == 1) {
820  for (int i=0; i<n; i++) {
821  if (y[i] >= 0) {
822  fprintf(logFile, "%E %E\n", x[i], y[i]);
823  } else {
824  fprintf(logFile, "%E %E\n", x[i], 0.0);
825  }
826  }
827  } else {
828  for (int i=0; i<n; i++) {
829  if (y[i] >= 0) {
830  fprintf(logFile, "%E %E\n", x[i], y[i]);
831  }
832  }
833  }
834 
835  fflush(logFile);
836 
838 
839  if (proc) {
840  proc -> updateEstimatedTime(proc -> prop_SaveIntegratedDataTime(), tic.restart());
841  }
842  }
843 
844  decBacklog();
845 }
846 
848 {
849  if (overflow) {
850  QString ovfname = name+".overflow";
851 
852  FILE *ovfile = fopen(qPrintable(ovfname),"w+");
853 
854  if (ovfile) {
855  int novf = overflow ->countOverflowPixels();
856 
857  fprintf(ovfile, "#S %d overflow pixels for file %s\n", novf, qPrintable(name));
858  fprintf(ovfile, "#N 3\n");
859  fprintf(ovfile, "#L x y d\n");
860 
861  int ncols = overflow -> get_Width();
862  int nrows = overflow -> get_Height();
863 
864  for (int row=0; row<nrows; row++) {
865  for (int col=0; col<ncols; col++) {
866  if (overflow->value(col,row)) {
867  fprintf(ovfile, "%d\t%d\t1\n", col, row);
868  }
869  }
870  }
871  }
872 
873  fclose(ovfile);
874  }
875 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
void writeOutputScanPrivate(FILE *logFile, QcepIntegratedDataPtr data, QString fileName=QString())
QxrdDataProcessorWPtr m_Processor
Definition: qxrdfilesaver.h:76
virtual void criticalMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())=0
QxrdExperimentWPtr experiment() const
void saveOverflowData(QString name, QcepMaskDataPtr overflow)
QWeakPointer< QxrdDataProcessor > QxrdDataProcessorWPtr
QxrdFileSaver(QcepAllocatorWPtr allocator)
void saveRaw16DataPrivate(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
QSharedPointer< QxrdAcquisition > QxrdAcquisitionPtr
QxrdAcquisitionWPtr acquisition() const
#define TIFFCHECK(a)
void saveInt32Data(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
QSharedPointer< QxrdDataProcessor > QxrdDataProcessorPtr
QxrdExperimentWPtr m_Experiment
Definition: qxrdfilesaver.h:75
void saveRaw32DataPrivate(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
void mkPath(QString filePath)
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
QSharedPointer< QcepIntegratedData > QcepIntegratedDataPtr
void saveDoubleData(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
QString uniqueFileName(QString name)
#define INVOKE_CHECK(res)
Definition: qcepmacros.h:13
QcepAllocatorWPtr m_Allocator
Definition: qxrdfilesaver.h:77
void saveTextDataPrivate(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
void saveTextData(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
QxrdAcquisitionWPtr m_Acquisition
Definition: qxrdfilesaver.h:78
void saveRaw16Data(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
QWeakPointer< QcepAllocator > QcepAllocatorWPtr
void saveRaw32Data(QString name, QcepInt32ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
void saveDoubleDataPrivate(QString name, QcepDoubleImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
void saveImageData(QString name, QcepImageDataBasePtr image, QcepMaskDataPtr overflow, int canOverwrite)
#define THREAD_CHECK
Definition: qcepmacros.h:11
void saveMaskData(QString name, QcepMaskDataPtr image, int canOverwrite)
virtual void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())=0
QWeakPointer< QxrdAcquisition > QxrdAcquisitionWPtr
QSharedPointer< QcepInt32ImageData > QcepInt32ImageDataPtr
QcepApplication * g_Application
void writeOutputScan(FILE *logFile, QcepIntegratedDataPtr data, QString fileName=QString())
void setProcessor(QxrdDataProcessorWPtr proc)
QSharedPointer< QcepImageDataBase > QcepImageDataBasePtr
QSharedPointer< QcepInt16ImageData > QcepInt16ImageDataPtr
QSharedPointer< QcepMaskData > QcepMaskDataPtr
void saveImageDataPrivate(QString name, QcepImageDataBasePtr image, QcepMaskDataPtr overflow, int canOverwrite)
struct tiff TIFF
Definition: qcepimagedata.h:21
void saveMaskDataPrivate(QString name, QcepMaskDataPtr image, int canOverwrite)
void setExperiment(QxrdExperimentWPtr expt)
void setAcquisition(QxrdAcquisitionWPtr acq)
void saveInt16Data(QString name, QcepInt16ImageDataPtr image, QcepMaskDataPtr overflow, int canOverwrite)
QSharedPointer< QcepDoubleImageData > QcepDoubleImageDataPtr