EMAN2
emutil.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifdef WIN32
33#include <windows.h>
34 #define MAXPATHLEN (MAX_PATH*4)
35#else
36#include <sys/param.h>
37#endif // WIN32
38
39#include <utility>
40
41#include "io/all_imageio.h"
42#include "portable_fileio.h"
43#include "emcache.h"
44#include "emdata.h"
45#include "ctf.h"
46#include "emassert.h"
47#include "exception.h"
48
49
50//#ifdef EMAN2_USING_CUDA_MALLOC
51//#include "cuda/cuda_util.h"
52//#endif
53
54using namespace EMAN;
55
56static const int ATTR_NAME_LEN = 128;
57
58EMUtil::ImageType EMUtil::get_image_ext_type(const string & file_ext)
59{
61
62 static bool initialized = false;
63 static map < string, ImageType > imagetypes;
64
65 if (!initialized) {
66 imagetypes["rec"] = IMAGE_MRC;
67 imagetypes["mrc"] = IMAGE_MRC;
68 imagetypes["MRC"] = IMAGE_MRC;
69 imagetypes["ali"] = IMAGE_MRC;
70 imagetypes["st"] = IMAGE_MRC; // IMOD stack file
71
72 imagetypes["mrcs"] = IMAGE_MRC;
73 imagetypes["MRCS"] = IMAGE_MRC;
74
75 imagetypes["raw"] = IMAGE_MRC;
76 imagetypes["RAW"] = IMAGE_MRC;
77
78 imagetypes["tnf"] = IMAGE_MRC;
79 imagetypes["TNF"] = IMAGE_MRC;
80
81 imagetypes["ccp4"] = IMAGE_MRC;
82 imagetypes["map"] = IMAGE_MRC;
83
84 imagetypes["dm3"] = IMAGE_DM3;
85 imagetypes["DM3"] = IMAGE_DM3;
86
87 imagetypes["dm4"] = IMAGE_DM4;
88 imagetypes["DM4"] = IMAGE_DM4;
89
90 imagetypes["dat"] = IMAGE_SPIDER;
91 imagetypes["spi"] = IMAGE_SPIDER;
92 imagetypes["SPI"] = IMAGE_SPIDER;
93 imagetypes["dat"] = IMAGE_SPIDER;
94 imagetypes["DAT"] = IMAGE_SPIDER;
95
96 imagetypes["spider"] = IMAGE_SPIDER;
97 imagetypes["SPIDER"] = IMAGE_SPIDER;
98
99 imagetypes["spidersingle"] = IMAGE_SINGLE_SPIDER;
100 imagetypes["SPIDERSINGLE"] = IMAGE_SINGLE_SPIDER;
101
102 imagetypes["singlespider"] = IMAGE_SINGLE_SPIDER;
103 imagetypes["SINGLESPIDER"] = IMAGE_SINGLE_SPIDER;
104
105 imagetypes["img"] = IMAGE_IMAGIC;
106 imagetypes["IMG"] = IMAGE_IMAGIC;
107
108 imagetypes["hed"] = IMAGE_IMAGIC;
109 imagetypes["HED"] = IMAGE_IMAGIC;
110
111 imagetypes["imagic"] = IMAGE_IMAGIC;
112 imagetypes["IMAGIC"] = IMAGE_IMAGIC;
113
114 imagetypes["pgm"] = IMAGE_PGM;
115 imagetypes["PGM"] = IMAGE_PGM;
116
117 imagetypes["lst"] = IMAGE_LST;
118 imagetypes["LST"] = IMAGE_LST;
119
120 imagetypes["lsx"] = IMAGE_LSTFAST; // but .lst or another extension would also be ok
121 imagetypes["LSX"] = IMAGE_LSTFAST;
122
123 imagetypes["pif"] = IMAGE_PIF;
124 imagetypes["PIF"] = IMAGE_PIF;
125
126 imagetypes["png"] = IMAGE_PNG;
127 imagetypes["PNG"] = IMAGE_PNG;
128
129 imagetypes["h5"] = IMAGE_HDF;
130 imagetypes["H5"] = IMAGE_HDF;
131
132 imagetypes["hd5"] = IMAGE_HDF;
133 imagetypes["HD5"] = IMAGE_HDF;
134
135 imagetypes["hdf"] = IMAGE_HDF;
136 imagetypes["HDF"] = IMAGE_HDF;
137
138 imagetypes["tif"] = IMAGE_TIFF;
139 imagetypes["TIF"] = IMAGE_TIFF;
140
141 imagetypes["tiff"] = IMAGE_TIFF;
142 imagetypes["TIFF"] = IMAGE_TIFF;
143
144 imagetypes["fts"] = IMAGE_FITS;
145 imagetypes["FTS"] = IMAGE_FITS;
146
147 imagetypes["vtk"] = IMAGE_VTK;
148 imagetypes["VTK"] = IMAGE_VTK;
149
150 imagetypes["hdr"] = IMAGE_SAL;
151 imagetypes["HDR"] = IMAGE_SAL;
152
153 imagetypes["sal"] = IMAGE_SAL;
154 imagetypes["SAL"] = IMAGE_SAL;
155
156 imagetypes["map"] = IMAGE_ICOS;
157 imagetypes["MAP"] = IMAGE_ICOS;
158
159 imagetypes["icos"] = IMAGE_ICOS;
160 imagetypes["ICOS"] = IMAGE_ICOS;
161
162 imagetypes["am"] = IMAGE_AMIRA;
163 imagetypes["AM"] = IMAGE_AMIRA;
164
165 imagetypes["amira"] = IMAGE_AMIRA;
166 imagetypes["AMIRA"] = IMAGE_AMIRA;
167
168 imagetypes["emim"] = IMAGE_EMIM;
169 imagetypes["EMIM"] = IMAGE_EMIM;
170
171 imagetypes["xplor"] = IMAGE_XPLOR;
172 imagetypes["XPLOR"] = IMAGE_XPLOR;
173
174 imagetypes["em"] = IMAGE_EM;
175 imagetypes["EM"] = IMAGE_EM;
176
177 imagetypes["dm2"] = IMAGE_GATAN2;
178 imagetypes["DM2"] = IMAGE_GATAN2;
179
180 imagetypes["v4l"] = IMAGE_V4L;
181 imagetypes["V4L"] = IMAGE_V4L;
182
183 imagetypes["jpg"] = IMAGE_JPEG;
184 imagetypes["JPG"] = IMAGE_JPEG;
185 imagetypes["jpeg"] = IMAGE_JPEG;
186 imagetypes["JPEG"] = IMAGE_JPEG;
187
188 imagetypes["df3"] = IMAGE_DF3;
189 imagetypes["DF3"] = IMAGE_DF3;
190
191 imagetypes["Omap"] = IMAGE_OMAP;
192 imagetypes["omap"] = IMAGE_OMAP;
193 imagetypes["OMAP"] = IMAGE_OMAP;
194 imagetypes["BRIX"] = IMAGE_OMAP;
195 imagetypes["brix"] = IMAGE_OMAP;
196 imagetypes["DSN6"] = IMAGE_OMAP;
197
198 imagetypes["situs"] = IMAGE_SITUS;
199 imagetypes["SITUS"] = IMAGE_SITUS;
200
201 imagetypes["ser"] = IMAGE_SER;
202 imagetypes["SER"] = IMAGE_SER;
203
204 imagetypes["eer"] = IMAGE_EER;
205// imagetypes["eer"] = IMAGE_EER2X;
206// imagetypes["eer"] = IMAGE_EER4X;
207
208 initialized = true;
209 }
210
211 ImageType result = IMAGE_UNKNOWN;
212
213 if (imagetypes.find(file_ext) != imagetypes.end()) {
214 result = imagetypes[file_ext];
215 }
216
217 EXITFUNC;
218
219 return result;
220}
221
222bool EMUtil::is_valid_filename(const string & filename)
223{
225
226 return (type != IMAGE_UNKNOWN);
227}
228
230 const void *first_block,
231 off_t file_size)
232{
233 ENTERFUNC;
234
235 Assert(filename != "");
236 Assert(first_block != 0);
237 Assert(file_size > 0);
238
239#ifdef ENABLE_V4L2
240 if (filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
241#endif
242
243 string ext = Util::get_filename_ext(filename);
244
245 if (ext == "") {
246 return IMAGE_UNKNOWN;
247 }
248
249 ImageType image_type = get_image_ext_type(ext);
250
251 switch (image_type) {
252 case IMAGE_MRC:
253 if (MrcIO::is_valid(first_block, file_size)) {
254 return IMAGE_MRC;
255 }
256 break;
257 case IMAGE_EER:
258 return IMAGE_EER;
259 break;
260 case IMAGE_EER2X:
261 return IMAGE_EER2X;
262 break;
263 case IMAGE_EER4X:
264 return IMAGE_EER4X;
265 break;
266 case IMAGE_DM3:
267 if (DM3IO::is_valid(first_block)) {
268 return IMAGE_DM3;
269 }
270 break;
271 case IMAGE_DM4:
272 if (DM4IO::is_valid(first_block)) {
273 return IMAGE_DM4;
274 }
275 break;
276#ifdef USE_HDF5
277 case IMAGE_HDF:
278 if (HdfIO2::is_valid(first_block)) {
279 return IMAGE_HDF;
280 }
281 break;
282#endif
283 case IMAGE_LST:
284 if (LstIO::is_valid(first_block)) {
285 return IMAGE_LST;
286 }
287 break;
288 case IMAGE_LSTFAST:
289 if (LstFastIO::is_valid(first_block)) {
290 return IMAGE_LSTFAST;
291 }
292 break;
293#ifdef USE_TIFF
294 case IMAGE_TIFF:
295 if (TiffIO::is_valid(first_block)) {
296 return IMAGE_TIFF;
297 }
298 break;
299#endif
300 case IMAGE_SPIDER:
301 if (SpiderIO::is_valid(first_block)) {
302 return IMAGE_SPIDER;
303 }
304 break;
306 if (SingleSpiderIO::is_valid(first_block)) {
307 return IMAGE_SINGLE_SPIDER;
308 }
309 break;
310 case IMAGE_PIF:
311 if (PifIO::is_valid(first_block)) {
312 return IMAGE_PIF;
313 }
314 break;
315#ifdef USE_PNG
316 case IMAGE_PNG:
317 if (PngIO::is_valid(first_block)) {
318 return IMAGE_PNG;
319 }
320 break;
321#endif
322 case IMAGE_VTK:
323 if (VtkIO::is_valid(first_block)) {
324 return IMAGE_VTK;
325 }
326 break;
327 case IMAGE_PGM:
328 if (PgmIO::is_valid(first_block)) {
329 return IMAGE_PGM;
330 }
331 break;
332 case IMAGE_ICOS:
333 if (IcosIO::is_valid(first_block)) {
334 return IMAGE_ICOS;
335 }
336 break;
337 case IMAGE_SAL:
338 if (SalIO::is_valid(first_block)) {
339 return IMAGE_SAL;
340 }
341 break;
342 case IMAGE_AMIRA:
343 if (AmiraIO::is_valid(first_block)) {
344 return IMAGE_AMIRA;
345 }
346 break;
347 case IMAGE_XPLOR:
348 if (XplorIO::is_valid(first_block)) {
349 return IMAGE_XPLOR;
350 }
351 break;
352 case IMAGE_GATAN2:
353 if (Gatan2IO::is_valid(first_block)) {
354 return IMAGE_GATAN2;
355 }
356 break;
357 case IMAGE_EM:
358 if (EmIO::is_valid(first_block, file_size)) {
359 return IMAGE_EM;
360 }
361 break;
362 case IMAGE_DF3:
363 if (EmIO::is_valid(first_block, file_size)) {
364 return IMAGE_DF3;
365 }
366 break;
367 case IMAGE_OMAP:
368 if (OmapIO::is_valid(first_block, file_size)) {
369 return IMAGE_OMAP;
370 }
371 break;
372 case IMAGE_SITUS:
373 if (SitusIO::is_valid(first_block)) {
374 return IMAGE_SITUS;
375 }
376 break;
377 case IMAGE_SER:
378 if (SerIO::is_valid(first_block)) {
379 return IMAGE_SER;
380 }
381 break;
382 case IMAGE_IMAGIC:
383 if (ImagicIO::is_valid(first_block)) {
384 return IMAGE_IMAGIC;
385 }
386 break;
387 default:
388 return IMAGE_UNKNOWN;
389 }
390
391 EXITFUNC;
392
393 return IMAGE_UNKNOWN;
394}
395
396EMUtil::ImageType EMUtil::get_image_type(const string & in_filename)
397{
398 ENTERFUNC;
399
400 Assert(in_filename != "");
401
402#ifdef ENABLE_V4L2
403 if (in_filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
404#endif
405
406 string filename = in_filename;
407
408 string old_ext = Util::get_filename_ext(filename);
409
410 if (old_ext == ImagicIO::IMG_EXT) {
411 filename = Util::change_filename_ext(filename, ImagicIO::HED_EXT);
412 }
413 else if (old_ext == "hdf") {
414 return IMAGE_HDF;
415 }
416
417 FILE *in = fopen(filename.c_str(), "rb");
418
419 if (! in) {
420 throw FileAccessException(filename);
421 }
422
423 char first_block[1024];
424 size_t n = fread(first_block, 1, 1024, in);
425 portable_fseek(in, 0, SEEK_END);
426 off_t file_size = portable_ftell(in);
427
428 if (n == 0) {
429// This produces annoying console messages
430// LOGERR("file '%s' is an empty file", filename.c_str());
431 fclose(in);
432 return IMAGE_UNKNOWN;
433 }
434
435 fclose(in);
436
437 ImageType image_type = fast_get_image_type(filename, first_block, file_size);
438
439 if (image_type != IMAGE_UNKNOWN) {
440 return image_type;
441 }
442
443 if (SpiderIO::is_valid(first_block)) {
444 image_type = IMAGE_SPIDER;
445 }
446 else if (SingleSpiderIO::is_valid(first_block)) {
447 image_type = IMAGE_SINGLE_SPIDER;
448 }
449 else if (MrcIO::is_valid(first_block, file_size)) {
450 image_type = IMAGE_MRC;
451 }
452 else if (DM3IO::is_valid(first_block)) {
453 image_type = IMAGE_DM3;
454 }
455 else if (DM4IO::is_valid(first_block)) {
456 image_type = IMAGE_DM4;
457 }
458#ifdef USE_HDF5
459 else if (HdfIO2::is_valid(first_block)) {
460 image_type = IMAGE_HDF;
461 }
462#endif
463 else if (LstIO::is_valid(first_block)) {
464 image_type = IMAGE_LST;
465 }
466 else if (LstFastIO::is_valid(first_block)) {
467 image_type = IMAGE_LSTFAST;
468 }
469#ifdef USE_TIFF
470 else if (TiffIO::is_valid(first_block)) {
471 image_type = IMAGE_TIFF;
472 }
473#endif
474 else if (PifIO::is_valid(first_block)) {
475 image_type = IMAGE_PIF;
476 }
477#ifdef USE_PNG
478 else if (PngIO::is_valid(first_block)) {
479 image_type = IMAGE_PNG;
480 }
481#endif
482 else if (VtkIO::is_valid(first_block)) {
483 image_type = IMAGE_VTK;
484 }
485 else if (PgmIO::is_valid(first_block)) {
486 image_type = IMAGE_PGM;
487 }
488 else if (IcosIO::is_valid(first_block)) {
489 image_type = IMAGE_ICOS;
490 }
491 else if (SalIO::is_valid(first_block)) {
492 image_type = IMAGE_SAL;
493 }
494 else if (AmiraIO::is_valid(first_block)) {
495 image_type = IMAGE_AMIRA;
496 }
497 else if (XplorIO::is_valid(first_block)) {
498 image_type = IMAGE_XPLOR;
499 }
500 else if (Gatan2IO::is_valid(first_block)) {
501 image_type = IMAGE_GATAN2;
502 }
503 else if (FitsIO::is_valid(first_block)) {
504 image_type = IMAGE_FITS;
505 }
506 else if (EmIO::is_valid(first_block, file_size)) {
507 image_type = IMAGE_EM;
508 }
509 else if(OmapIO::is_valid(first_block, file_size)) {
510 image_type = IMAGE_OMAP;
511 }
512 else if(SitusIO::is_valid(first_block)) {
513 image_type = IMAGE_SITUS;
514 }
515 else if(SerIO::is_valid(first_block)) {
516 image_type = IMAGE_SER;
517 }
518 else if (ImagicIO::is_valid(first_block)) {
519 image_type = IMAGE_IMAGIC;
520 }
521 else if (Df3IO::is_valid(first_block)) {
522 image_type = IMAGE_DF3;
523 }
524 else {
525 // LOGERR("I don't know this image's type: '%s'", filename.c_str());
526 throw ImageFormatException("invalid image type");
527 }
528
529 EXITFUNC;
530
531 return image_type;
532}
533
534int EMUtil::get_image_count(const string & filename)
535{
536 ENTERFUNC;
537
538 Assert(filename != "");
539
540 int nimg = 0;
541
542 ImageIO *imageio = get_imageio(filename, ImageIO::READ_ONLY);
543 //printf("%p\n",imageio);
544
545 if (imageio) {
546 nimg = imageio->get_nimg();
547 }
548
549 EMUtil::close_imageio(filename, imageio);
550
551 imageio = NULL;
552
553 EXITFUNC;
554
555 return nimg;
556}
557
558ImageIO *EMUtil::get_imageio(const string & filename, int rw,
559 ImageType image_type)
560{
561 ENTERFUNC;
562
563 // printf("EMUtil::get_imageio\n");
564
565 Assert(filename != "");
567 rw == ImageIO::READ_WRITE ||
568 rw == ImageIO::WRITE_ONLY);
569
570 ImageIO *imageio = 0;
571 int persist = 0;
572
573 #ifdef IMAGEIO_CACHE
574 imageio = GlobalCache::instance()->get_imageio(filename, rw);
575 if (imageio) {
576 return imageio;
577 }
578 #endif
579
580 ImageIO::IOMode rw_mode = static_cast < ImageIO::IOMode > (rw);
581
582 if (image_type == IMAGE_UNKNOWN) {
583 image_type = get_image_type(filename);
584 }
585
586 if (image_type == IMAGE_UNKNOWN) {
587 if(rw == ImageIO::WRITE_ONLY || rw == ImageIO::READ_WRITE) {
588 throw ImageFormatException("writing to this image format not supported.");
589 }
590 }
591
592 switch (image_type) {
593#ifdef ENABLE_V4L2
594 case IMAGE_V4L:
595 imageio = new V4L2IO(filename, rw_mode);
596 break;
597#endif
598 case IMAGE_MRC:
599 imageio = new MrcIO(filename, rw_mode);
600 break;
601 case IMAGE_EER:
602 imageio = new EerIO(filename, rw_mode, decoder0x);
603 break;
604 case IMAGE_EER2X:
605 imageio = new EerIO(filename, rw_mode, decoder1x);
606 break;
607 case IMAGE_EER4X:
608 imageio = new EerIO(filename, rw_mode, decoder2x);
609 break;
610 case IMAGE_IMAGIC:
611 imageio = new ImagicIO2(filename, rw_mode);
612 if (rw_mode==ImageIO::READ_ONLY && ((ImagicIO2 *)imageio)->init_test()==-1 ) {
613 delete imageio;
614 imageio = new ImagicIO(filename, rw_mode);
615 }
616 break;
617 case IMAGE_DM3:
618 imageio = new DM3IO(filename, rw_mode);
619 break;
620 case IMAGE_DM4:
621 imageio = new DM4IO(filename, rw_mode);
622 break;
623#ifdef USE_TIFF
624 case IMAGE_TIFF:
625 imageio = new TiffIO(filename, rw_mode);
626 break;
627#endif
628#ifdef USE_HDF5
629 case IMAGE_HDF:
630 persist = 30;
631 if (rw_mode != ImageIO::READ_ONLY) {
632 persist = 3;
633 }
634 imageio = new HdfIO2(filename, rw_mode);
635 if (((HdfIO2 *)imageio)->init_test()==-1) {
636 delete imageio;
637 imageio = new HdfIO(filename, rw_mode);
638 }
639 break;
640#endif //USE_HDF5
641 case IMAGE_LST:
642 imageio = new LstIO(filename, rw_mode);
643 break;
644 case IMAGE_LSTFAST:
645 imageio = new LstFastIO(filename, rw_mode);
646 break;
647 case IMAGE_PIF:
648 imageio = new PifIO(filename, rw_mode);
649 break;
650 case IMAGE_VTK:
651 imageio = new VtkIO(filename, rw_mode);
652 break;
653 case IMAGE_SPIDER:
654 imageio = new SpiderIO(filename, rw_mode);
655 break;
657 imageio = new SingleSpiderIO(filename, rw_mode);
658 break;
659 case IMAGE_PGM:
660 imageio = new PgmIO(filename, rw_mode);
661 break;
662#ifdef USE_JPEG
663 case IMAGE_JPEG:
664 imageio = new JpegIO(filename,rw_mode);
665 break;
666#endif
667 case IMAGE_ICOS:
668 imageio = new IcosIO(filename, rw_mode);
669 break;
670#ifdef USE_PNG
671 case IMAGE_PNG:
672 imageio = new PngIO(filename, rw_mode);
673 break;
674#endif
675 case IMAGE_SAL:
676 imageio = new SalIO(filename, rw_mode);
677 break;
678 case IMAGE_AMIRA:
679 imageio = new AmiraIO(filename, rw_mode);
680 break;
681 case IMAGE_GATAN2:
682 imageio = new Gatan2IO(filename, rw_mode);
683 break;
684 case IMAGE_EM:
685 imageio = new EmIO(filename, rw_mode);
686 break;
687 case IMAGE_XPLOR:
688 imageio = new XplorIO(filename, rw_mode);
689 break;
690 case IMAGE_FITS:
691 imageio = new FitsIO(filename, rw_mode);
692 break;
693 case IMAGE_DF3:
694 imageio = new Df3IO(filename, rw_mode);
695 break;
696 case IMAGE_OMAP:
697 imageio = new OmapIO(filename, rw_mode);
698 break;
699 case IMAGE_SITUS:
700 imageio = new SitusIO(filename, rw_mode);
701 break;
702 case IMAGE_SER:
703 imageio = new SerIO(filename, rw_mode);
704 break;
705 default:
706 break;
707 }
708
709 #ifdef IMAGEIO_CACHE
710 if (persist > 0) {
711 GlobalCache::instance()->add_imageio(filename, rw, persist, imageio);
712 }
713 #endif
714
715 EXITFUNC;
716
717 return imageio;
718}
719
720void EMUtil::close_imageio(const string & filename, const ImageIO * io)
721{
722 //printf("EMUtil::close_imageio\n");
723 #ifdef IMAGEIO_CACHE
724 if (GlobalCache::instance()->contains(filename)) {
725 GlobalCache::instance()->close_imageio(filename);
726 } else {
727 delete io;
728 }
729 #else
730 delete io;
731 #endif
732}
733
735{
736 switch (t) {
737 case IMAGE_V4L:
738 return "V4L2";
739 break;
740 case IMAGE_MRC:
741 return "MRC";
742 break;
743 case IMAGE_SPIDER:
744 return "SPIDER";
745 break;
747 return "Single-SPIDER";
748 break;
749 case IMAGE_IMAGIC:
750 return "IMAGIC";
751 break;
752 case IMAGE_PGM:
753 return "PGM";
754 break;
755 case IMAGE_LST:
756 return "LST";
757 break;
758 case IMAGE_LSTFAST:
759 return "Fast LST";
760 break;
761 case IMAGE_PIF:
762 return "PIF";
763 break;
764 case IMAGE_PNG:
765 return "PNG";
766 break;
767 case IMAGE_HDF:
768 return "HDF5";
769 break;
770 case IMAGE_DM3:
771 return "GatanDM3";
772 break;
773 case IMAGE_DM4:
774 return "GatanDM4";
775 break;
776 case IMAGE_TIFF:
777 return "TIFF";
778 break;
779 case IMAGE_VTK:
780 return "VTK";
781 break;
782 case IMAGE_SAL:
783 return "HDR";
784 break;
785 case IMAGE_ICOS:
786 return "ICOS_MAP";
787 break;
788 case IMAGE_EMIM:
789 return "EMIM";
790 break;
791 case IMAGE_GATAN2:
792 return "GatanDM2";
793 break;
794 case IMAGE_JPEG:
795 return "JPEG";
796 break;
797 case IMAGE_AMIRA:
798 return "AmiraMesh";
799 break;
800 case IMAGE_XPLOR:
801 return "XPLOR";
802 break;
803 case IMAGE_EM:
804 return "EM";
805 break;
806 case IMAGE_FITS:
807 return "FITS";
808 break;
809 case IMAGE_DF3:
810 return "DF3";
811 break;
812 case IMAGE_OMAP:
813 return "OMAP";
814 break;
815 case IMAGE_SITUS:
816 return "SITUS";
817 break;
818 case IMAGE_SER:
819 return "SER";
820 break;
821 case IMAGE_UNKNOWN:
822 return "unknown";
823 }
824
825 return "unknown";
826}
827
829{
830 switch (type) {
831 case EM_CHAR:
832 return "CHAR";
833 case EM_UCHAR:
834 return "UNSIGNED CHAR";
835 case EM_SHORT:
836 return "SHORT";
837 case EM_USHORT:
838 return "UNSIGNED SHORT";
839 case EM_INT:
840 return "INT";
841 case EM_UINT:
842 return "UNSIGNED INT";
843 case EM_FLOAT:
844 return "FLOAT";
845 case EM_DOUBLE:
846 return "DOUBLE";
847 case EM_SHORT_COMPLEX:
848 return "SHORT_COMPLEX";
850 return "USHORT_COMPLEX";
851 case EM_FLOAT_COMPLEX:
852 return "FLOAT_COMPLEX";
853 case EM_UNKNOWN:
854 return "UNKNOWN";
855 }
856
857 return "UNKNOWN";
858}
859
860void EMUtil::get_region_dims(const Region * area, int nx, int *area_x,
861 int ny, int *area_y, int nz, int *area_z)
862{
863 Assert(area_x);
864 Assert(area_y);
865
866 if (! area) {
867 *area_x = nx;
868 *area_y = ny;
869
870 if (area_z) {
871 *area_z = nz;
872 }
873 }
874 else {
875 Vec3i size = area->get_size();
876 *area_x = size[0];
877 *area_y = size[1];
878
879 if (area_z) {
880 if (area->get_ndim() > 2 && nz > 1) {
881 *area_z = size[2];
882 }
883 else {
884 *area_z = 1;
885 }
886 }
887
888 }
889}
890
891void EMUtil::get_region_origins(const Region * area, int *p_x0, int *p_y0, int *p_z0,
892 int nz, int image_index)
893{
894 Assert(p_x0);
895 Assert(p_y0);
896
897 if (area) {
898 *p_x0 = static_cast < int >(area->origin[0]);
899 *p_y0 = static_cast < int >(area->origin[1]);
900
901 if (p_z0 && nz > 1 && area->get_ndim() > 2) {
902 *p_z0 = static_cast < int >(area->origin[2]);
903 }
904 }
905 else {
906 *p_x0 = 0;
907 *p_y0 = 0;
908
909 if (p_z0) {
910 *p_z0 = nz > 1 ? 0 : image_index;
911 }
912 }
913}
914
915double EMUtil::mode_size_product(size_t factor, size_t mode_size)
916{
917 const size_t mode_size_half = 11111111;
918
919 double product;
920
921 if (mode_size == mode_size_half) {
922 product = factor * 0.5;
923 }
924 else {
925 product = factor * mode_size;
926 }
927
928 return product;
929}
930
931void EMUtil::process_region_io(void *vdata, FILE * file,
932 int rw_mode, int image_index,
933 size_t mode_size, int nx, int ny, int nz,
934 const Region * area, bool need_flip,
935 ImageType imgtype, int pre_row, int post_row)
936{
937 Assert(vdata != 0);
938 Assert(file != 0);
939 Assert(rw_mode == ImageIO::READ_ONLY ||
940 rw_mode == ImageIO::READ_WRITE ||
941 rw_mode == ImageIO::WRITE_ONLY);
942
943 if (mode_size == 0) throw UnexpectedBehaviorException("The mode size was 0?");
944
945 const size_t mode_size_half = 11111111;
946
947 unsigned char * cdata = (unsigned char *)vdata;
948
949 int dx0 = 0; // data x0
950 int dy0 = 0; // data y0
951 int dz0 = 0; // data z0
952
953 int fx0 = 0; // file x0
954 int fy0 = 0; // file y0
955 int fz0 = nz > 1 ? 0 : image_index; // file z0
956
957 int xlen = 0;
958 int ylen = 0;
959 int zlen = 0;
960
961 get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);
962
963 bool debug = (getenv("DEBUG_IO") != NULL);
964
965 if (debug) {
966 printf ("-------------- process_region_io --------------\n");
967 printf ("rw, indx, modsiz, nx, ny, nz = %d %d %ld %d %d %d\n",
968 rw_mode, image_index, mode_size, nx, ny, nz);
969 printf ("flip, imtyp, prerow, postrow = %d %d %d %d\n",
970 (int) need_flip, (int) imgtype, pre_row, post_row);
971 printf ("xlen, ylen, zlen = %d %d %d\n", xlen, ylen, zlen);
972
973 if (area != NULL) {
974 printf ("x0, y0, z0, mx, my, mz, ndim = %g %g %g %g %g %g %d\n",
975 area->x_origin(),
976 area->y_origin(),
977 area->z_origin(),
978 area->get_width(),
979 area->get_height(),
980 area->get_depth(),
981 area->get_ndim());
982 }
983 }
984
985 if (area) { // Accommodate for all boundary overlaps of the region
986 Vec3i origin = area->get_origin();
987
988 fx0 = origin[0]; dx0 = origin[0];
989 fy0 = origin[1]; dy0 = origin[1];
990
991 if (nz > 1 && area->get_ndim() > 2) {
992 fz0 = origin[2]; dz0 = origin[2];
993 }
994
995 if (need_flip) {
996 Vec3i size = area->get_size();
997 fy0 = ny-(origin[1]+size[1]);
998 }
999
1000 if (fx0 < 0) {
1001 dx0 *= -1;
1002 xlen = xlen + fx0; // because there are less reads
1003 fx0 = 0;
1004 } else {
1005 dx0 = 0;
1006 //fx0 *= -1;
1007 }
1008
1009 if (fy0 < 0) {
1010 dy0 *= -1;
1011 ylen = ylen + fy0; // because there are less reads
1012 fy0 = 0;
1013 } else {
1014 if (need_flip){
1015 dy0*=-1;
1016 }
1017 else dy0 = 0;
1018 //fy0 *= -1;
1019 }
1020
1021 if (fz0 < 0) {
1022 dz0 *= -1;
1023 zlen = zlen + fz0; // because there are less reads
1024 fz0 = 0;
1025 } else {
1026 dz0 = 0;
1027 //fz0 *= -1;
1028 }
1029
1030 if ((fx0 + xlen)> nx) xlen = nx-fx0;
1031 if ((fy0 + ylen)> ny) ylen = ny-fy0;
1032 if ((fz0 + zlen)> nz && nz > 1) zlen = nz-fz0;
1033
1034 // This is fine - the region was entirely outside the image
1035 if ( xlen <= 0 || ylen <= 0 || zlen <= 0 ) return;
1036
1037 if (mode_size == mode_size_half) {
1038 // Have an area with 8 bit packed MRC format
1039 // (2 4-bit valus per 8 bit byte),
1040 // with an effective mode size of half a byte,
1041 // where most x-pixel parameters need to be even
1042 // for everything to work well.
1043 bool error = false;
1044
1045 if (fx0 % 2 != 0 && false) { // Don't check right now.
1046 cout << "First region x-pixel location "
1047 "must be even for packed 8 bit MRC." << endl;
1048 error = true;
1049 }
1050
1051 if ((nx - fx0 - xlen) % 2 != 0 && false) { // Don't check right now.
1052 cout << "No. of x-pixel locations after region "
1053 "must be even for packed 8 bit MRC." << endl;
1054 error = true;
1055 }
1056
1057 if (xlen % 2 != 0) {
1058 cout << "No. of region x-pixels "
1059 "must be even for packed 8 bit MRC." << endl;
1060 error = true;
1061 }
1062
1063 if (error) {
1064 return;
1065 }
1066 }
1067 }
1068
1069 if (xlen <= 0) {
1070 cout << "Xlen was too small " << xlen << endl;
1071
1072 return;
1073 }
1074
1075 Vec3i size;
1076
1077 if (area != 0) size = area->get_size();
1078 else size = Vec3d(nx,ny,nz);
1079
1080 //size_t area_sec_size = ylen * mode_size_product(xlen, mode_size);
1081 size_t memory_sec_size = size[1] * mode_size_product(size[0], mode_size);
1082 size_t img_row_size = mode_size_product(nx, mode_size) +
1083 pre_row + post_row;
1084 size_t area_row_size = mode_size_product(xlen, mode_size);
1085 size_t memory_row_size = mode_size_product(size[0], mode_size);
1086
1087 if ( area_row_size <= 0 ) {
1088 cout << "Xlen was too small " << xlen << " mode_size " << mode_size << endl;
1089
1090 return;
1091 }
1092
1093 size_t x_pre_gap = mode_size_product(fx0, mode_size);
1094 size_t x_post_gap = mode_size_product(nx - fx0 - xlen, mode_size);
1095
1096 size_t y_pre_gap = fy0 * img_row_size;
1097 size_t y_post_gap = (ny - fy0 - ylen) * img_row_size;
1098
1099 if (x_pre_gap < 0) x_pre_gap = 0;
1100 if (x_post_gap < 0) x_post_gap = 0;
1101
1102 size_t extra = img_row_size - (x_pre_gap + area_row_size + x_post_gap);
1103
1104 if (extra > 0) x_post_gap += extra;
1105
1106 portable_fseek(file, img_row_size * ny * fz0, SEEK_CUR);
1107
1108 float nxlendata[1];
1109 int floatsize = (int) sizeof(float);
1110 nxlendata[0] = (float)(nx * floatsize);
1111
1112 if (debug) {
1113 printf ("fz0, dz0, xlen, ylen, zlen = %d %d %d %d %d\n",
1114 fz0, dz0, xlen, ylen, zlen);
1115 printf ("x_pre_gap, x_post_gap, y_pre_gap, y_post_gap = %d %d %d %d\n",
1116 x_pre_gap, x_post_gap, y_pre_gap, y_post_gap);
1117 printf ("mem_sec_siz, img_row_siz, are_row_siz, mem_row_siz = %ld %ld %ld %ld\n",
1118 memory_sec_size, img_row_size, area_row_size, memory_row_size);
1119 printf ("-----------------------------------------------\n");
1120 }
1121
1122 for (int k = dz0; k < (dz0+zlen); k++) {
1123 // k is image/slice number, starting from 0
1124 if (y_pre_gap > 0) {
1125 portable_fseek(file, y_pre_gap, SEEK_CUR);
1126 }
1127
1128 //long k2 = k * area_sec_size;
1129 long k2 = k*memory_sec_size;
1130
1131 for (int j = dy0; j < (dy0+ylen); j++) {
1132 if (pre_row > 0) {
1133 if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
1134 fwrite(nxlendata, floatsize, 1, file);
1135 }
1136 else {
1137 portable_fseek(file, pre_row, SEEK_CUR);
1138 }
1139 }
1140
1141 if (x_pre_gap > 0) {
1142 portable_fseek(file, x_pre_gap, SEEK_CUR);
1143 }
1144
1145 int jj = j;
1146
1147 if (need_flip) {
1148 jj = (dy0+ylen) - 1 - j;
1149
1150 // region considerations add complications
1151 // in the flipping scenario (imagic format)
1152 if (dy0 > 0) {
1153 jj += dy0;
1154 }
1155 }
1156
1157 if (rw_mode == ImageIO::READ_ONLY) {
1158 if (fread(&cdata[k2 + jj * memory_row_size +
1159 (size_t) mode_size_product(dx0, mode_size)],
1160 area_row_size, 1, file) != 1) {
1161
1162// cout << jj << " " << k2 << " " << memory_row_size
1163// << " " << dx0 << " "
1164// << mode_size << " " << area_row_size << " "
1165// << cdata << ftell(file) << "done" << endl;
1166
1167 cout << "Reached premature end-of-file reading "
1168 << "region from image/slice number " << k
1169 << " of file with " << ftell(file)
1170 << " bytes." << endl;
1171
1172 throw ImageReadException("Unknownfilename",
1173 "incomplete data read");
1174 }
1175 }
1176 else {
1177 if (fwrite(&cdata[k2 + jj * memory_row_size +
1178 (size_t) mode_size_product(dx0, mode_size)],
1179 area_row_size, 1, file) != 1) {
1180 throw ImageWriteException("", "incomplete data write");
1181 }
1182 }
1183
1184 if (x_post_gap > 0) {
1185 portable_fseek(file, x_post_gap, SEEK_CUR);
1186 }
1187
1188 if (post_row > 0) {
1189 if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
1190 fwrite(nxlendata, floatsize, 1, file);
1191 }
1192 else {
1193 portable_fseek(file, post_row, SEEK_CUR);
1194 }
1195 }
1196 }
1197
1198 if (y_post_gap > 0) {
1199 portable_fseek(file, y_post_gap, SEEK_CUR);
1200 }
1201 }
1202}
1203
1204void EMUtil::dump_dict(const Dict & dict)
1205{
1206 vector < string > keys = dict.keys();
1207 vector < EMObject > values = dict.values();
1208
1209 for (unsigned int i = 0; i < keys.size(); i++) {
1210 EMObject obj = values[i];
1211
1212 if (! obj.is_null()) {
1213 string val = obj.to_str();
1214
1215 if (keys[i] == "datatype") {
1216 val = get_datatype_string((EMDataType) (int) obj);
1217 }
1218
1219 fprintf(stdout, "%25s\t%s\n", keys[i].c_str(), val.c_str());
1220 }
1221 }
1222}
1223
1224bool EMUtil::is_same_size(const EMData * const em1, const EMData * const em2)
1225{
1226 return em1->get_xsize() == em2->get_xsize() &&
1227 em1->get_ysize() == em2->get_ysize() &&
1228 em1->get_zsize() == em2->get_zsize();
1229}
1230
1232{
1233 return datatype == EM_SHORT_COMPLEX ||
1234 datatype == EM_USHORT_COMPLEX ||
1235 datatype == EM_FLOAT_COMPLEX;
1236}
1237
1238EMData *EMUtil::vertical_acf(const EMData * image, int maxdy)
1239{
1240 if (!image) {
1241 throw NullPointerException("NULL Image");
1242 }
1243
1244 EMData *ret = new EMData();
1245
1246 int nx = image->get_xsize();
1247 int ny = image->get_ysize();
1248
1249 if (maxdy <= 1) {
1250 maxdy = ny / 8;
1251 }
1252
1253 ret->set_size(nx, maxdy, 1);
1254
1255 float *data = image->get_data();
1256 float *ret_data = ret->get_data();
1257
1258 for (int x = 0; x < nx; x++) {
1259 for (int y = 0; y < maxdy; y++) {
1260 float dot = 0;
1261
1262 for (int yy = maxdy; yy < ny - maxdy; yy++) {
1263 dot += data[x + (yy + y) * nx] * data[x + (yy - y) * nx];
1264 }
1265
1266 ret_data[x + y * nx] = dot;
1267 }
1268 }
1269
1270 ret->update();
1271
1272 return ret;
1273}
1274
1275bool EMUtil::is_same_ctf(const EMData * image1, const EMData * image2)
1276{
1277 if (!image1) {
1278 throw NullPointerException("image1 is NULL");
1279 }
1280
1281 if (!image2) {
1282 throw NullPointerException("image2 is NULL");
1283 }
1284
1285 Ctf *ctf1 = image1->get_ctf();
1286 Ctf *ctf2 = image2->get_ctf();
1287
1288 if ((!ctf1 && !ctf2) && (image1->has_ctff() == false && image2->has_ctff() == false)) {
1289 return true;
1290 }
1291
1292 if (ctf1 && ctf2) {
1293 bool result = ctf1->equal(ctf2);
1294 delete ctf1;
1295 ctf1 = NULL;
1296 delete ctf2;
1297 ctf2 = NULL;
1298
1299 return result;
1300 }
1301
1302 return false;
1303}
1304
1305static int imgscore_cmp(const void *imgscore1, const void *imgscore2)
1306{
1307 Assert(imgscore1 != 0);
1308 Assert(imgscore2 != 0);
1309
1310 float c = ((ImageScore *)imgscore1)->score - ((ImageScore *)imgscore2)->score;
1311
1312 if (c<0) {
1313 return 1;
1314 }
1315 else if (c>0) {
1316 return -1;
1317 }
1318
1319 return 0;
1320}
1321
1323{
1324 Assert(nn > 0);
1325 n = nn;
1326 image_scores = new ImageScore[n];
1327}
1328
1330{
1331 if (image_scores) {
1332 delete [] image_scores;
1333 image_scores = NULL;
1334 }
1335}
1336
1338{
1339 qsort(image_scores, n, sizeof(ImageScore), imgscore_cmp);
1340}
1341
1342void ImageSort::set(int i, float score)
1343{
1344 Assert(i >= 0);
1345 image_scores[i] = ImageScore(i, score);
1346}
1347
1348int ImageSort::get_index(int i) const
1349{
1350 Assert(i >= 0);
1351 return image_scores[i].index;
1352}
1353
1354float ImageSort::get_score(int i) const
1355{
1356 Assert(i >= 0);
1357 return image_scores[i].score;
1358}
1359
1361{
1362 return n;
1363}
1364
1365void EMUtil::process_ascii_region_io(float *data, FILE * file, int rw_mode,
1366 int , size_t mode_size, int nx, int ny, int nz,
1367 const Region * area, bool has_index_line,
1368 int nitems_per_line, const char *outformat)
1369{
1370 Assert(data != 0);
1371 Assert(file != 0);
1372 Assert(rw_mode == ImageIO::READ_ONLY ||
1373 rw_mode == ImageIO::READ_WRITE ||
1374 rw_mode == ImageIO::WRITE_ONLY);
1375
1376 int xlen = 0, ylen = 0, zlen = 0;
1377 get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);
1378
1379 int x0 = 0;
1380 int y0 = 0;
1381 int z0 = 0;
1382
1383 if (area) {
1384 x0 = (int)area->origin[0];
1385 y0 = (int)area->origin[1];
1386 z0 = (int)area->origin[2];
1387 }
1388
1389 int nlines_per_sec = (nx *ny) / nitems_per_line;
1390 int nitems_last_line = (nx * ny) % nitems_per_line;
1391
1392 if (nitems_last_line != 0) {
1393 nlines_per_sec++;
1394 }
1395
1396 if (has_index_line) {
1397 nlines_per_sec++;
1398 }
1399
1400 if (z0 > 0) {
1401 jump_lines(file, z0 * nlines_per_sec);
1402 }
1403
1404 int nlines_pre_sec = (y0 * nx + x0) / nitems_per_line;
1405 int gap_nitems = nx - xlen;
1406 int ti = 0;
1407 int rlines = 0;
1408
1409 for (int k = 0; k < zlen; k++) {
1410 EMUtil::jump_lines(file, nlines_pre_sec+1);
1411
1412 int head_nitems = (y0 * nx + x0) % nitems_per_line;
1413 int tail_nitems = 0;
1414 bool is_head_read = false;
1415
1416 for (int j = 0; j < ylen; j++) {
1417 if (head_nitems > 0 && !is_head_read) {
1418 EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
1419 nitems_per_line-head_nitems,
1420 nitems_per_line-1, data, &ti, outformat);
1421 rlines++;
1422 }
1423
1424 EMUtil::process_lines_io(file, rw_mode, nitems_per_line,
1425 mode_size, (xlen - head_nitems),
1426 data, &ti, outformat);
1427
1428 rlines += ((xlen - head_nitems) / nitems_per_line);
1429
1430 tail_nitems = (xlen - head_nitems) % nitems_per_line;
1431
1432 if ((gap_nitems + tail_nitems) > 0) {
1433 head_nitems = nitems_per_line -
1434 (gap_nitems + tail_nitems) % nitems_per_line;
1435 }
1436 else {
1437 head_nitems = 0;
1438 }
1439
1440 is_head_read = false;
1441
1442 if (tail_nitems > 0) {
1443 if ((gap_nitems < (nitems_per_line-tail_nitems)) &&
1444 (j != (ylen-1))) {
1445 EMUtil::exclude_numbers_io(file, rw_mode, nitems_per_line,
1446 mode_size, tail_nitems,
1447 tail_nitems+gap_nitems-1, data, &ti, outformat);
1448 is_head_read = true;
1449 rlines++;
1450 }
1451 else {
1452 EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
1453 0, tail_nitems-1, data, &ti, outformat);
1454 rlines++;
1455 }
1456 }
1457
1458 if (gap_nitems > (nitems_per_line-tail_nitems)) {
1459 int gap_nlines = (gap_nitems - (nitems_per_line-tail_nitems)) /
1460 nitems_per_line;
1461
1462 if (gap_nlines > 0 && j != (ylen-1)) {
1463 EMUtil::jump_lines(file, gap_nlines);
1464 }
1465 }
1466 }
1467
1468 int ytail_nitems = (ny-ylen-y0) * nx + (nx-xlen-x0) - (nitems_per_line-tail_nitems);
1469 EMUtil::jump_lines_by_items(file, ytail_nitems, nitems_per_line);
1470 }
1471}
1472
1473void EMUtil::jump_lines_by_items(FILE * file, int nitems, int nitems_per_line)
1474{
1475 Assert(file);
1476 Assert(nitems_per_line > 0);
1477
1478 if (nitems <= 0) {
1479 return;
1480 }
1481
1482 int nlines = nitems / nitems_per_line;
1483
1484 if ((nitems % nitems_per_line) != 0) {
1485 nlines++;
1486 }
1487
1488 if (nlines > 0) {
1489 jump_lines(file, nlines);
1490 }
1491}
1492
1493void EMUtil::jump_lines(FILE * file, int nlines)
1494{
1495 Assert(file);
1496
1497 if (nlines > 0) {
1498 char line[MAXPATHLEN];
1499
1500 for (int l = 0; l < nlines; l++) {
1501 if (!fgets(line, sizeof(line), file)) {
1502 Assert("read xplor file failed");
1503 }
1504 }
1505 }
1506}
1507
1508void EMUtil::process_numbers_io(FILE * file, int rw_mode,
1509 int nitems_per_line, size_t mode_size, int start,
1510 int end, float *data, int *p_i, const char * outformat)
1511{
1512 Assert(file);
1513 Assert(start >= 0);
1514 Assert(start <= end);
1515 Assert(end <= nitems_per_line);
1516 Assert(data);
1517 Assert(p_i);
1518 Assert(outformat);
1519
1520 char line[MAXPATHLEN];
1521
1522 if (rw_mode == ImageIO::READ_ONLY) {
1523 if (!fgets(line, sizeof(line), file)) {
1524 Assert("read xplor file failed");
1525 }
1526
1527 int nitems_in_line = (int) (strlen(line) / mode_size);
1528 Assert(end <= nitems_in_line);
1529 vector<float> d(nitems_in_line);
1530 char * pline = line;
1531
1532 for (int i = 0; i < nitems_in_line; i++) {
1533 sscanf(pline, "%f", &d[i]);
1534 pline += (int)mode_size;
1535 }
1536
1537 for (int i = start; i <= end; i++) {
1538 data[*p_i] = d[i];
1539 (*p_i)++;
1540 }
1541 }
1542 else {
1543 portable_fseek(file, mode_size * start, SEEK_CUR);
1544
1545 for (int i = start; i <= end; i++) {
1546 fprintf(file, outformat, data[*p_i]);
1547 (*p_i)++;
1548 }
1549
1550 portable_fseek(file, mode_size * (nitems_per_line - end-1)+1, SEEK_CUR);
1551 }
1552}
1553
1554void EMUtil::exclude_numbers_io(FILE * file, int rw_mode,
1555 int nitems_per_line, size_t mode_size, int start,
1556 int end, float * data, int *p_i, const char * outformat)
1557{
1558 Assert(file);
1559 Assert(mode_size > 0);
1560 Assert(start >= 0);
1561 Assert(end <= nitems_per_line);
1562 Assert(data);
1563 Assert(p_i);
1564 Assert(outformat);
1565
1566 char line[MAXPATHLEN];
1567
1568 if (rw_mode == ImageIO::READ_ONLY) {
1569
1570 if (!fgets(line, sizeof(line), file)) {
1571 Assert("read xplor file failed");
1572 }
1573
1574 int nitems_in_line = (int) (strlen(line) / mode_size);
1575 Assert(end <= nitems_in_line);
1576
1577 vector<float> d(nitems_in_line);
1578 char *pline = line;
1579
1580 for (int i = 0; i < nitems_in_line; i++) {
1581 sscanf(pline, "%f", &d[i]);
1582 pline = pline + (int)mode_size;
1583 }
1584
1585
1586 for (int i = 0; i < start; i++) {
1587 data[*p_i] = d[i];
1588 (*p_i)++;
1589 }
1590
1591 for (int i = end+1; i < nitems_in_line; i++) {
1592 data[*p_i] = d[i];
1593 (*p_i)++;
1594 }
1595 }
1596 else {
1597 for (int i = 0; i < start; i++) {
1598 fprintf(file, outformat, data[*p_i]);
1599 (*p_i)++;
1600 }
1601
1602 portable_fseek(file, (end-start+1) * mode_size, SEEK_CUR);
1603
1604 for (int i = end+1; i < nitems_per_line; i++) {
1605 fprintf(file, outformat, data[*p_i]);
1606 (*p_i)++;
1607 }
1608 portable_fseek(file, 1, SEEK_CUR);
1609 }
1610}
1611
1612void EMUtil::process_lines_io(FILE * file, int rw_mode,
1613 int nitems_per_line, size_t mode_size,
1614 int nitems, float *data, int *p_i,
1615 const char * outformat)
1616{
1617 Assert(file);
1618 Assert(data);
1619 Assert(p_i);
1620
1621 if (nitems > 0) {
1622 int nlines = nitems / nitems_per_line;
1623
1624 for (int i = 0; i < nlines; i++) {
1625 EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size, 0,
1626 nitems_per_line-1, data, p_i, outformat);
1627 }
1628 }
1629}
1630
1631vector<string> EMUtil::get_euler_names(const string & euler_type)
1632{
1633 vector<string> v;
1634 string b = "euler_";
1635
1636 if (euler_type == "EMAN") {
1637 v.push_back(b + "alt");
1638 v.push_back(b + "az");
1639 v.push_back(b + "phi");
1640 }
1641 else if (euler_type == "MRC") {
1642 v.push_back(b + "theta");
1643 v.push_back(b + "phi");
1644 v.push_back(b + "omega");
1645 }
1646 else if (euler_type == "IMAGIC") {
1647 v.push_back(b + "alpha");
1648 v.push_back(b + "beta");
1649 v.push_back(b + "gamma");
1650 }
1651 else if (euler_type == "SPIDER") {
1652 v.push_back(b + "phi");
1653 v.push_back(b + "theta");
1654 v.push_back(b + "gamma");
1655 }
1656 else if (euler_type == "SPIN" ||
1657 euler_type == "SGIROT") {
1658 v.push_back(b + "q");
1659 v.push_back(b + "n1");
1660 v.push_back(b + "n2");
1661 v.push_back(b + "n3");
1662 }
1663
1664 else if (euler_type == "QUATERNION") {
1665 v.push_back(b + "e0");
1666 v.push_back(b + "e1");
1667 v.push_back(b + "e2");
1668 v.push_back(b + "e3");
1669 }
1670
1671 return v;
1672}
1673
1674vector<EMObject> EMUtil::get_all_attributes(const string & file_name, const string & attr_name)
1675{
1676 vector<EMObject> v;
1677
1678 Assert(file_name != "");
1679 Assert(attr_name != "");
1680
1681 auto vpImg = EMData::read_images(file_name, vector<int>(), EMUtil::IMAGE_UNKNOWN, true);
1682
1683 for (auto iter = vpImg.begin(); iter!=vpImg.end(); ++iter)
1684 v.push_back((*iter)->get_attr_default(attr_name));
1685
1686 return v;
1687}
1688
1689void EMUtil::getRenderLimits(const Dict & dict, float & rendermin, float & rendermax, int & renderbits)
1690{
1691 // This routine used to have some complicated logic for specifying the limits in various ways
1692 // that is now gone (as far as I can tell, nobody had ever used it), and replaced by the later
1693 // logic in getRenderMinMax, which is triggered when render_max<=render_min
1694 // at present this routine just reads header values. It is still here in case we need to implement
1695 // more complicated logic in future.
1696 rendermin = 0.0; // when rendermax<=rendermin, automatic mode is invoked
1697 rendermax = 0.0;
1698
1699 if (dict.has_key("render_bits")) renderbits = (int)dict["render_bits"];
1700 else renderbits=0; // 0 bits means float mode, any compression will be truly lossless
1701
1702 if (dict.has_key("render_min")) rendermin = (float) dict["render_min"];
1703 if (dict.has_key("render_max")) rendermax = (float) dict["render_max"];
1704}
1705
1706void EMUtil::getRenderMinMax(float * data, const int nx, const int ny,
1707 float & rendermin, float & rendermax, int &renderbits, const int nz)
1708{
1709 const float flag_base = 1.0e30;
1710 const float use_data_min_or_max = -flag_base;
1711 const float use_num_std_devs = flag_base / 100.0;
1712
1713 float num_std_devs;
1714
1715 bool debug = 0;
1716
1717 if (debug) printf ("into RenderMinMax, rmin = %g, rmax = %g, rbits = %d\n", rendermin, rendermax, renderbits);
1718
1719 if (renderbits<=0) return; // this is for float mode where rendermin/max isn't used
1720 if (renderbits>16) renderbits=16;
1721
1722 if (rendermax <= rendermin ||
1723 Util::is_nan(rendermin) || Util::is_nan(rendermax) ||
1724 fabs(rendermin) > use_num_std_devs ||
1725 fabs(rendermax) > use_num_std_devs) {
1726
1727 double m = 0.0f, s = 0.0f;
1728 size_t nint=0,n0=0,n1=0; // count the number of integers, zeroes and ones
1729 size_t size = (size_t)nx*ny*nz;
1730 float min = data[0], max = data[0];
1731 int bitval = 1<<renderbits;
1732
1733 // we compute image statistics. If this were designed right, we'd have the actual image instead of just
1734 // the data pointer and wouldn't need to do this (other than maybe the integer counting)
1735 for (size_t i = 0; i < size; ++i) {
1736 m += data[i];
1737 s += data[i] * data[i];
1738 if (data[i]==0.0f) n0++;
1739 else if (data[i]==1.0f) n1++;
1740 if (data[i]==floor(data[i])) nint++;
1741
1742 min = data[i] < min ? data[i] : min;
1743 max = data[i] > max ? data[i] : max;
1744// if (!Util::goodf(&data[i])) printf("NAN in image at pixel %ld\n",i);
1745 }
1746
1747
1748 float mnz = m/(size-n0); // mean, excluding zeroes
1749 float snz = sqrt(s/(size-n0)-mnz*mnz); // sigma, excluding zeroes
1750 m /= (float)(size);
1751 s = sqrt(s/(float)(size)-m*m);
1752
1753// // experimental code for looking at various limit testing schemes
1754// double s0=0,s1=1,sk0=0,sk1=0,ku0=0,ku1=0;
1755// size_t nn0=0,nn1=0;
1756// for (size_t i = 0; i < size; ++i) {
1757// if (data[i]<m) {
1758// s0+=pow(data[i]-m,2.0);
1759// sk0+=pow(m-data[i],3.0);
1760// ku0+=pow(data[i]-m,4.0);
1761// nn0++;
1762// }
1763// else if (data[i]>m) {
1764// s1+=pow(data[i]-m,2.0);
1765// sk1+=pow(data[i]-m,3.0);
1766// ku1+=pow(data[i]-m,4.0);
1767// nn1++;
1768// }
1769// }
1770// s0=pow(s0/nn0,0.5);
1771// s1=pow(s1/nn1,0.5);
1772// sk0=pow(sk0/nn0,1.0/3.0);
1773// sk1=pow(sk1/nn1,1.0/3.0);
1774// ku0=pow(ku0/nn0,0.25);
1775// ku1=pow(ku1/nn1,0.25);
1776// printf("\n%f %f %f %f %f %f",s0,s1,sk0,sk1,ku0,ku1);
1777// printf("\n%f - %f %f - %f %f - %f",m-s0*4.0,m+s1*4.0,m-sk0*4.0,m+sk1*4.0,m-ku0*4.0,m+ku1*4.0);
1778// printf ("\nmin, mean, max, s.d., nint, nzer, none = %g %g %g %g %d %d %d\n", min, m, max, s, nint, n0, n1);
1779// // end of experimental code
1780
1781
1782 if (debug) printf ("min, mean, max, s.d., nint, nzer, none = %g %g %g %g %d %d %d\n", min, m, max, s, nint, n0, n1);
1783
1784 if (s <= 0 || Util::is_nan(s)) s = 1.0; // this means all data values are the same
1785
1786 // probably a mask, or another imaged normalized to a range of 1 , binary or smoothed, treat it the same
1787 if ((max-min)==1) {
1788 rendermin=min;
1789 rendermax=max;
1790 }
1791 // all integer values, make sure we get integers back when we read, even if reduced bits
1792 else if (nint==size) {
1793 // if true, then we can safely store all of the values in the requested bits without change
1794 if (max-min<bitval) {
1795 rendermin=min;
1796 rendermax=min+bitval-1;
1797 }
1798 // otherwise, we will need to keep the most significant bits, but still try to get integers out (except in very odd cases)
1799 else {
1800 int neededbits=ceil(log(max-min+1)/log(2.0f));
1801 int step = 1<<(neededbits-renderbits);
1802 if (min<0<max) {
1803 rendermin = round( min / step ) * step;
1804 }
1805 else {
1806 rendermin=min;
1807 }
1808 rendermax=rendermin+step*(bitval-1);
1809 }
1810 }
1811 // Now into the more general case, but we still wish to preserve zero if present in significant numbers
1812 // TODO: This raises the tricky point of what would happen if you had a masked volume then added 0.0001 to it?
1813 // statistics might produce poor results. May need to consider using kurtosis instead of zero detection
1814 else if (min<0<max) { // 10 is arbitrary, just looking for a profusion of exactly zero values implying a mask
1815 // The first two seem stupid since they result in rendermin=min, rendermax=max, but we retain the option
1816 // of a more involved calculation to avoid outliers compressing the histogram to an unreasonable level
1817 if (min==0) {
1818 rendermin=0;
1819 rendermax=max; // keep everything, will only have issues with very large outliers
1820// rendermax=(mnz+snz*6.0)>max?max:mnz+snz*6.0; //TODO <- how large a sigma multiplier?
1821 }
1822 else if (max==0) {
1823 rendermax=0;
1824 rendermin=min;
1825// rendermin=(mnz-snz*6.0)<min?min:mnz-snz*6.0;
1826 }
1827 else {
1828 float step = (max-min)/(bitval-1);
1829 if (min == floor( min/step ) * step) {
1830 rendermin=min;
1831 rendermax=max;
1832 }
1833 else {
1834 // rendermin=(mnz-snz*4.0)<min?min:mnz-snz*4.0; // 4 standard deviations from the mean seems good empirically, e2iminfo.py -asO
1835 // rendermax=(mnz+snz*4.0)>max?max:mnz+snz*4.0;
1836 // logically impossible
1837 // if (min>0) min=0;
1838 // if (max<0) max=0;
1839 // if (rendermin==min && rendermax!=max) rendermax=(min+snz*8.0)>max?max:min+snz*8.0;
1840 // if (rendermin!=min && rendermax==max) rendermin=(max-snz*8.0)<min?min:max-snz*8.0;
1841 // float step=(rendermax-rendermin)/(bitval-1);
1842 // rendermin=ceil(rendermin/step)*step; // adjust rendermin so integral number of steps to exactly zero, may be roundoff issues
1843 step=(max-min)/(bitval-2); // -2 instead of -1 to give enough range to accommodate exactly 0
1844 rendermin=(floor(min/step)*step); // rendermin will be an integral number of steps from zero
1845 rendermax=rendermin+step*(bitval-1);
1846 }
1847 }
1848 }
1849 // Most general case, no "special values" to preserve
1850 else {
1851// rendermin=(m-s*4.0)<min?min:m-s*4.0; // 4 standard deviations from the mean seems good empirically, e2iminfo.py -asO
1852// rendermax=(m+s*4.0)>max?max:m+s*4.0;
1853// if (rendermin==min && rendermax!=max) rendermax=(min+s*8.0)>max?max:min+s*8.0;
1854// if (rendermin!=min && rendermax==max) rendermin=(max-s*8.0)<min?min:max-s*8.0;
1855 // Intelligent outlier removal is just too risky. large outliers are still a risk, but we'll stay conservative for now
1856 rendermin=min;
1857 rendermax=max;
1858 }
1859 }
1860
1861 if (debug) printf ("out of RenderMinMax, rmin = %g, rmax = %g, rbits = %d\n", rendermin, rendermax, renderbits);
1862}
1863
1864#ifdef USE_HDF5
1865EMObject EMUtil::read_hdf_attribute(const string & filename, const string & key, int image_index)
1866{
1867 ImageType image_type = get_image_type(filename);
1868
1869 if (image_type != IMAGE_HDF) {
1870 throw ImageFormatException("This function only applies to HDF5 file.");
1871 }
1872
1873 HdfIO2* imageio = new HdfIO2(filename, ImageIO::READ_ONLY);
1874 imageio->init();
1875
1876 // Each image is in a group for later expansion. Open the group
1877 hid_t file = imageio->get_fileid();
1878
1879 char ipath[50];
1880 sprintf(ipath, "/MDF/images/%d", image_index);
1881
1882 hid_t igrp = H5Gopen(file, ipath);
1883
1884 if (igrp < 0) { // group not existed
1885 throw _NotExistingObjectException(string(ipath));
1886 }
1887
1888 string s("EMAN.");
1889 s += key;
1890 hid_t attr = H5Aopen_name(igrp, s.c_str());
1891 EMObject emobj = imageio->read_attr(attr);
1892
1893 H5Aclose(attr);
1894 H5Gclose(igrp);
1895 delete imageio;
1896
1897 return emobj;
1898}
1899
1900int EMUtil::write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index)
1901{
1902 ImageType image_type = get_image_type(filename);
1903
1904 if (image_type != IMAGE_HDF) {
1905 throw ImageFormatException("This function only applies to HDF5 file.");
1906 }
1907
1908 HdfIO2* imageio = new HdfIO2(filename, ImageIO::WRITE_ONLY);
1909 imageio->init();
1910
1911 // Each image is in a group for later expansion. Open the group
1912
1913 hid_t file = imageio->get_fileid();
1914
1915 char ipath[50];
1916 sprintf(ipath, "/MDF/images/%d", image_index);
1917
1918 hid_t igrp = H5Gopen(file, ipath);
1919
1920 if (igrp<0) { // group not existed
1921 throw _NotExistingObjectException(string(ipath));
1922 }
1923
1924 string s("EMAN.");
1925 s += key;
1926 int ret = imageio->write_attr(igrp, s.c_str(), value);
1927
1928 H5Gclose(igrp);
1929 delete imageio;
1930
1931 return ret;
1932}
1933
1934int EMUtil::delete_hdf_attribute(const string & filename, const string & key, int image_index)
1935{
1936 ImageType image_type = get_image_type(filename);
1937
1938 if (image_type != IMAGE_HDF) {
1939 throw ImageFormatException("This function only applies to HDF5 file.");
1940 }
1941
1942 HdfIO2* imageio = new HdfIO2(filename, ImageIO::READ_WRITE);
1943 imageio->init();
1944
1945 // Each image is in a group for later expansion. Open the group
1946
1947 hid_t file = imageio->get_fileid();
1948
1949 char ipath[50];
1950 sprintf(ipath, "/MDF/images/%d", image_index);
1951
1952 hid_t igrp = H5Gopen(file,ipath);
1953
1954 if (igrp < 0) { // group not existed
1955 throw _NotExistingObjectException(string(ipath));
1956 }
1957
1958 string s("EMAN.");
1959 s += key;
1960 herr_t ret = H5Adelete(igrp, s.c_str());
1961
1962 H5Gclose(igrp);
1963 delete imageio;
1964
1965 if (ret >= 0) return 0;
1966 else return -1;
1967}
1968#endif //USE_HDF5
Amira file = ASCII header + binary data.
Definition: amiraio.h:49
static bool is_valid(const void *first_block)
Definition: amiraio.cpp:100
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual bool equal(const Ctf *ctf1) const =0
Gatan DM3 file is a hierarchical binary image format.
Definition: dm3io.h:243
static bool is_valid(const void *first_block)
Definition: dm3io.cpp:694
Gatan DM$ was introduced with the GMS 2.0 release.
Definition: dm4io.h:250
static bool is_valid(const void *first_block)
Definition: dm4io.cpp:811
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: df3io.cpp:264
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
vector< EMObject > values() const
Get a vector containing copies of each of the EMObjects in this dictionary.
Definition: emobject.h:487
bool has_key(const string &key) const
Ask the Dictionary if it as a particular key.
Definition: emobject.h:511
vector< string > keys() const
Get a vector containing all of the (string) keys in this dictionary.
Definition: emobject.h:475
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
EMObject is a wrapper class for types including int, float, double, etc as defined in ObjectType.
Definition: emobject.h:123
string to_str() const
Calls to_str( this->type)
Definition: emobject.cpp:684
bool is_null() const
Checks to see if the EMObject is interpretable This basically equates to checking to see if the type ...
Definition: emobject.cpp:679
static void process_region_io(void *cdata, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz=1, const Region *area=0, bool need_flip=false, ImageType imgtype=IMAGE_UNKNOWN, int pre_row=0, int post_row=0)
Process image region IO.
Definition: emutil.cpp:931
static void dump_dict(const Dict &dict)
Dump a Dict object.
Definition: emutil.cpp:1204
static void close_imageio(const string &filename, const ImageIO *io)
Ian: Close ImageIO object.
Definition: emutil.cpp:720
static void jump_lines_by_items(FILE *file, int nitems, int nitems_per_line)
Definition: emutil.cpp:1473
static int get_image_count(const string &filename)
Get the number of images in an image file.
Definition: emutil.cpp:534
static vector< EMObject > get_all_attributes(const string &file_name, const string &attr_name)
Get an attribute from a stack of image, returned as a vector.
Definition: emutil.cpp:1674
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
@ EM_UCHAR
Definition: emutil.h:95
@ EM_USHORT_COMPLEX
Definition: emutil.h:103
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_USHORT
Definition: emutil.h:97
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104
@ EM_SHORT_COMPLEX
Definition: emutil.h:102
@ EM_SHORT
Definition: emutil.h:96
static bool is_same_size(const EMData *image1, const EMData *image2)
Check whether two EMData images are of the same size.
Definition: emutil.cpp:1224
static ImageIO * get_imageio(const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
Get an ImageIO object.
Definition: emutil.cpp:558
static void process_numbers_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int start, int end, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1508
static bool is_complex_type(EMDataType datatype)
Definition: emutil.cpp:1231
static EMData * vertical_acf(const EMData *image, int maxdy)
Definition: emutil.cpp:1238
static void getRenderMinMax(float *data, const int nx, const int ny, float &rendermin, float &rendermax, int &renderbits, const int nz=1)
Calculate the min and max pixel value accepted for image nomalization, if we did not get them from im...
Definition: emutil.cpp:1706
ImageType
Image format types.
Definition: emutil.h:112
@ IMAGE_SINGLE_SPIDER
Definition: emutil.h:119
@ IMAGE_XPLOR
Definition: emutil.h:135
@ IMAGE_EMIM
Definition: emutil.h:132
@ IMAGE_IMAGIC
Definition: emutil.h:120
@ IMAGE_LSTFAST
Definition: emutil.h:140
@ IMAGE_UNKNOWN
Definition: emutil.h:113
@ IMAGE_SITUS
Definition: emutil.h:143
@ IMAGE_OMAP
Definition: emutil.h:142
@ IMAGE_EER4X
Definition: emutil.h:117
@ IMAGE_SPIDER
Definition: emutil.h:118
@ IMAGE_EER2X
Definition: emutil.h:116
@ IMAGE_GATAN2
Definition: emutil.h:133
@ IMAGE_JPEG
Definition: emutil.h:138
@ IMAGE_AMIRA
Definition: emutil.h:134
@ IMAGE_FITS
Definition: emutil.h:139
@ IMAGE_TIFF
Definition: emutil.h:124
@ IMAGE_ICOS
Definition: emutil.h:131
static ImageType get_image_ext_type(const string &file_ext)
Get an image's format type from its filename extension.
Definition: emutil.cpp:58
static void getRenderLimits(const Dict &dict, float &rendermin, float &rendermax, int &renderbits)
Get the min and max pixel value accepted for image nomalization from image attribute dictionary,...
Definition: emutil.cpp:1689
static bool is_valid_filename(const string &filename)
Ask whether or not the given filename is a valid EM image filename This is the same thing as checking...
Definition: emutil.cpp:222
static ImageType fast_get_image_type(const string &filename, const void *first_block, off_t file_size)
Definition: emutil.cpp:229
static void get_region_origins(const Region *area, int *p_x0, int *p_y0, int *p_z0=0, int nz=1, int image_index=0)
Get a region's original locations.
Definition: emutil.cpp:891
static void exclude_numbers_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int start, int end, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1554
static void process_ascii_region_io(float *data, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz, const Region *area, bool has_index_line, int nitems_per_line, const char *outformat)
Works for regions that are outside the image data dimension area.
Definition: emutil.cpp:1365
static vector< string > get_euler_names(const string &euler_type)
Definition: emutil.cpp:1631
static bool is_same_ctf(const EMData *image1, const EMData *image2)
Check whether two EMData images have the same CTF parameters.
Definition: emutil.cpp:1275
static const char * get_imagetype_name(EMUtil::ImageType type)
Give each image type a meaningful name.
Definition: emutil.cpp:734
static ImageType get_image_type(const string &filename)
Get an image's format type by processing the first 1K of the image.
Definition: emutil.cpp:396
static void get_region_dims(const Region *area, int nx, int *area_x, int ny, int *area_y, int nz=1, int *area_z=0)
Get a region's dimensions.
Definition: emutil.cpp:860
static void jump_lines(FILE *file, int nlines)
Definition: emutil.cpp:1493
static double mode_size_product(size_t factor, size_t mode_size)
Return a factor times the mode size, which may be a special value (11111111) meaning one half,...
Definition: emutil.cpp:915
static void process_lines_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int nitems, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1612
static const char * get_datatype_string(EMDataType type)
Give each data type a meaningful name.
Definition: emutil.cpp:828
EmIO defines I/O operations on EM image format.
Definition: emio.h:45
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: emio.cpp:95
MRC file = header + data (nx x ny x nz).
Definition: fitsio.h:45
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: fitsio.cpp:77
Gatan2 Image file = header + data.
Definition: gatan2io.h:46
static bool is_valid(const void *first_block)
Definition: gatan2io.cpp:82
ICOS file = header + data.
Definition: icosio.h:51
static bool is_valid(const void *first_block)
Definition: icosio.cpp:80
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
virtual int get_nimg()
Return the number of images in this image file.
Definition: imageio.cpp:176
ImageSort(int n)
Definition: emutil.cpp:1322
ImageScore * image_scores
Definition: emutil.h:435
float get_score(int i) const
Definition: emutil.cpp:1354
int size() const
Definition: emutil.cpp:1360
int get_index(int i) const
Definition: emutil.cpp:1348
void set(int i, float score)
Definition: emutil.cpp:1342
IMAGIC-5 Header File Format.
Definition: imagicio2.h:70
IMAGIC-5 Header File Format.
Definition: imagicio.h:66
static bool is_valid(const void *first_block)
Definition: imagicio.cpp:121
static const char * HED_EXT
Definition: imagicio.h:68
static const char * IMG_EXT
Definition: imagicio.h:69
A LSX file is a high performance ASCII file that contains a list of image numbers and file names.
Definition: lstfastio.h:55
static bool is_valid(const void *first_block)
Definition: lstfastio.cpp:111
A LST file is an ASCII file that contains a list of image file names.
Definition: lstio.h:46
static bool is_valid(const void *first_block)
Definition: lstio.cpp:110
MRC file = header + data (nx x ny x nz).
Definition: mrcio.h:45
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: mrcio.cpp:342
DSN6 MAP is composed of a series of records which are all 512 bytes long.
Definition: omapio.h:51
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: omapio.cpp:236
A PGM file = header + data.
Definition: pgmio.h:47
static bool is_valid(const void *first_block)
Definition: pgmio.cpp:120
PIF(Portable Image Format for EM Data) is an image format from Purdue University.
Definition: pifio.h:53
static bool is_valid(const void *first_block)
Definition: pifio.cpp:164
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
float get_width() const
get the width
Definition: geometry.h:606
vector< float > get_size() const
get the size of each dimension as a vector
Definition: geometry.h:627
float y_origin() const
get the y element of the origin
Definition: geometry.h:622
float x_origin() const
get the x element of the origin
Definition: geometry.h:620
int get_ndim() const
Get the region's dimension.
Definition: geometry.h:644
vector< float > get_origin() const
get the origin as a vector
Definition: geometry.h:629
float get_depth() const
get the depth
Definition: geometry.h:610
FloatPoint origin
Definition: geometry.h:654
float z_origin() const
get the z element of the origin
Definition: geometry.h:624
float get_height() const
get the height
Definition: geometry.h:608
A SAL image is an image from Perkin Elmer PDS Microdensitometer.
Definition: salio.h:48
static bool is_valid(const void *first_block)
Definition: salio.cpp:141
SER (Series File Format) is a file format created by Dr.
Definition: serio.h:56
static bool is_valid(const void *first_block)
Definition: serio.cpp:94
Single Spider Image I/O class.
Definition: sspiderio.h:45
static bool is_valid(const void *first_block)
Definition: sspiderio.cpp:71
situs is a a Situs-specific format on a cubic lattice.
Definition: situsio.h:49
static bool is_valid(const void *first_block)
Definition: situsio.cpp:182
SPIDER: (System for Processing Image Data from Electron microscopy and Related fields) is an image pr...
Definition: spiderio.h:72
static bool is_valid(const void *first_block)
Definition: spiderio.cpp:111
static bool is_nan(const float number)
tell whether a float value is a NaN
Definition: util.h:105
static string get_filename_ext(const string &filename)
Get a filename's extension.
Definition: util.cpp:526
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
Definition: util.cpp:485
Read-only.
Definition: v4l2io.h:46
VtkIO reads/writes VTK image file.
Definition: vtkio.h:73
static bool is_valid(const void *first_block)
Definition: vtkio.cpp:160
XPLOR image format is in ASCII:
Definition: xplorio.h:61
static bool is_valid(const void *first_block)
Definition: xplorio.cpp:148
Used when an object type, like an EMObject type, doesn't exist.
Definition: exception.h:119
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
Definition: emassert.h:42
EMData * log() const
return natural logarithm image for a image
EMData * sqrt() const
return square root of current image
static vector< std::shared_ptr< EMData > > read_images(const string &filename, vector< int > img_indices=vector< int >(), EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false)
Read a set of images from file specified by 'filename'.
static const int ATTR_NAME_LEN
Definition: emutil.cpp:56
static int imgscore_cmp(const void *imgscore1, const void *imgscore2)
Definition: emutil.cpp:1305
#define ImageReadException(filename, desc)
Definition: exception.h:204
#define ImageFormatException(desc)
Definition: exception.h:147
#define UnexpectedBehaviorException(desc)
Definition: exception.h:400
#define FileAccessException(filename)
Definition: exception.h:187
#define ImageWriteException(imagename, desc)
Definition: exception.h:223
#define NullPointerException(desc)
Definition: exception.h:241
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
E2Exception class.
Definition: aligner.h:40
static DecoderIx< 2 > decoder2x
Definition: eerio.h:177
static DecoderIx< 1 > decoder1x
Definition: eerio.h:176
double dot(const Vector3 &w, const Vector3 &v)
Definition: vecmath.h:305
Vec3< double > Vec3d
Definition: vec3.h:695
static DecoderIx< 0 > decoder0x
Definition: eerio.h:175
off_t portable_ftell(FILE *fp)
int portable_fseek(FILE *fp, off_t offset, int whence)
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517