EMAN2
imagicio2.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#include <cstring>
33#include <climits>
34#include "imagicio2.h"
35#include "portable_fileio.h"
36#include "util.h"
37#include "geometry.h"
38#include "ctf.h"
39#include "emassert.h"
40#include "transform.h"
41
42#include <ctime>
43
44using namespace EMAN;
45
46const char *ImagicIO2::HED_EXT = "hed";
47const char *ImagicIO2::IMG_EXT = "img";
48const char *ImagicIO2::REAL_TYPE_MAGIC = "REAL";
49const char *ImagicIO2::CTF_MAGIC = "!-";
50
51ImagicIO2::ImagicIO2(const string & fname, IOMode rw)
52: ImageIO(fname, rw), hed_file(0), img_file(0)
53{
56
58 is_new_hed = false;
59 is_new_img = false;
60 memset(&imagich, 0, sizeof(Imagic4D));
61 imagich.count = -1;
63 nz = 0;
64}
65
67{
68 if (hed_file) {
69 fclose(hed_file);
70 hed_file = 0;
71 }
72
73 if (img_file) {
74 fclose(img_file);
75 img_file = 0;
76 }
77}
78
79void ImagicIO2::init()
80{
82
83 if (initialized) {
84 return;
85 }
86
87 initialized = true;
88
89 is_new_hed = false;
90 is_new_img = false;
91
94
95 if (is_new_hed != is_new_img) {
96 LOGWARN("IMAGIC header file and data file should both exist or both not exist");
97 }
98
99 if (!is_new_hed) {
100 if (fread(&imagich, sizeof(Imagic4D), 1, hed_file) != 1) {
101 throw ImageReadException(hed_filename, "IMAGIC4D header");
102 }
103
104// if (!is_valid(&imagich)) {
105// throw ImageReadException(hed_filename, "invalid IMAGIC file");
106// }
107
109
111 LOGERR("unsupported imagic data type: %s", imagich.type);
112 throw ImageReadException(hed_filename, "unsupported imagic data type");
113 }
114
117 rewind(hed_file);
118 }
119
120 EXITFUNC;
121}
122
124{
125 ENTERFUNC;
126
127 if (initialized) {
128 return 1;
129 }
130
131 FILE *in = fopen(hed_filename.c_str(), "rb");
132 if (!in) {
134 }
135
136 char first_block[1024];
137 size_t n = fread(first_block, sizeof(char), sizeof(first_block), in);
138
139 if (n == 0) {
140 LOGERR("file '%s' is an empty file", filename.c_str());
141 fclose(in);
142 return -1;
143 }
144 fclose(in);
145
146 const int *data = reinterpret_cast <const int *>(first_block);
147 int nx = data[13];
148 int ny = data[12];
149 int izold = data[11];
150
151 if(izold==nx*ny) {
152 EXITFUNC;
153 return -1; //old style IMAGIC file
154 }
155 else {
156 EXITFUNC;
157 return 0; //new IMAGIC4D file
158 }
159}
160
161bool ImagicIO2::is_valid(const void *first_block)
162{
163 ENTERFUNC;
164
165 if (!first_block) {
166 return false;
167 }
168
169 const int *data = static_cast < const int *>(first_block);
170 int count = data[1];
171 int headrec = data[3];
172 int hour = data[7];
173 int minute = data[8];
174 int second = data[9];
175 int rsize = data[10];
176 int nx = data[13];
177 int ny = data[12];
178 int nz = data[60];
179 int realtype = data[68];
180
181 bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);
182
183 if (data_big_endian != ByteOrder::is_host_big_endian()) {
184 ByteOrder::swap_bytes(&count);
185 ByteOrder::swap_bytes(&headrec);
187 ByteOrder::swap_bytes(&rsize);
191 ByteOrder::swap_bytes(&realtype);
192 }
193
194 const int max_dim = 1 << 20;
195 bool result = false;
196
197 // this field realtype is unique to new Imagic-5 format
198 if(realtype != VAX_VMS && realtype != LINUX_WINDOWS && realtype != SGI_IBM) {
199 EXITFUNC;
200 return result;
201 }
202
203 if (headrec == 1 &&
204 count >= 0 && count < max_dim &&
205 nx > 0 && nx < max_dim &&
206 ny > 0 && ny < max_dim &&
207 nz > 0 && nz < max_dim &&
208 hour >= 0 && hour < 24 &&
209 minute >=0 && minute <60 &&
210 second >=0 && second <60) {
211 result = true;
212 }
213
214 EXITFUNC;
215 return result;
216}
217
218int ImagicIO2::read_header(Dict & dict, int image_index, const Region * area, bool)
219{
220 ENTERFUNC;
221
222 check_read_access(image_index);
223
224 Imagic4D hed;
225 if (image_index == 0) {
226 hed = imagich;
227 }
228 else {
229 memset(&hed, 0, sizeof(Imagic4D));
230 portable_fseek(hed_file, sizeof(Imagic4D) * image_index, SEEK_SET);
231 fread(&hed, sizeof(Imagic4D), 1, hed_file);
233 }
234
235 int nz = hed.izlp ? hed.izlp : 1;
236 check_region(area, FloatSize(hed.nx, hed.ny, nz), is_new_hed, false);
237
239
240 int xlen = 0, ylen = 0, zlen = 0;
241 EMUtil::get_region_dims(area, hed.nx, &xlen, hed.ny, &ylen, nz, &zlen);
242
243 dict["nx"] = xlen;
244 dict["ny"] = ylen;
245 dict["nz"] = zlen;
246
247 dict["IMAGIC.imgnum"] = hed.imgnum;
248 dict["IMAGIC.count"] = hed.count;
249 dict["IMAGIC.error"] = hed.error;
250
251 dict["IMAGIC.month"] = hed.month;
252 dict["IMAGIC.day"] = hed.mday;
253 dict["IMAGIC.year"] = hed.year;
254 dict["IMAGIC.hour"] = hed.hour;
255 dict["IMAGIC.minute"] = hed.minute;
256 dict["IMAGIC.sec"] = hed.sec;
257
258 dict["IMAGIC.rsize"] = hed.rsize;
259 dict["IMAGIC.izold"] = hed.izold;
260
261 dict["datatype"] = to_em_datatype(datatype);
262 dict["IMAGIC.type"] = hed.type+'\0';
263
264 dict["IMAGIC.ixold"] = hed.ixold;
265 dict["IMAGIC.iyold"] = hed.iyold;
266
267 dict["mean"] = hed.avdens;
268 dict["sigma"] = hed.sigma;
269
270 dict["maximum"] = hed.densmax;
271 dict["minimum"] = hed.densmin;
272
273 dict["IMAGIC.complex"] = hed.complex;
274 dict["IMAGIC.defocus1"] = hed.defocus1;
275 dict["IMAGIC.defocus2"] = hed.defocus2;
276 dict["IMAGIC.defangle"] = hed.defangle;
277 dict["IMAGIC.sinostart"] = hed.sinostart;
278 dict["IMAGIC.sinoend"] = hed.sinoend;
279
280 dict["IMAGIC.label"] = hed.label+'\0';
281
282 dict["IMAGIC.ccc3d"] = hed.ccc3d;
283 dict["IMAGIC.ref3d"] = hed.ref3d;
284 dict["IMAGIC.mident"] = hed.mident;
285 dict["IMAGIC.ezshift"] = hed.ezshift;
286
287 dict["IMAGIC.ealpha"] = hed.ealpha;
288 dict["IMAGIC.ebeta"] = hed.ebeta;
289 dict["IMAGIC.egamma"] = hed.egamma;
290
291 dict["IMAGIC.nalisum"] = hed.nalisum;
292 dict["IMAGIC.pgroup"] = hed.pgroup;
293
294 dict["IMAGIC.i4lp"] = hed.i4lp; //number of objects (1D, 2D, or 3D) in 4D data
295
296 dict["IMAGIC.alpha"] = hed.alpha;
297 dict["IMAGIC.beta"] = hed.beta;
298 dict["IMAGIC.gamma"] = hed.gamma;
299
300 dict["IMAGIC.imavers"] = hed.imavers;
301 dict["IMAGIC.realtype"] = hed.realtype;
302
303 dict["IMAGIC.angle"] = hed.angle;
304 dict["IMAGIC.voltage"] = hed.voltage;
305 dict["IMAGIC.spaberr"] = hed.spaberr;
306 dict["IMAGIC.pcoher"] = hed.pcoher;
307 dict["IMAGIC.ccc"] = hed.ccc;
308 dict["IMAGIC.errar"] = hed.errar;
309 dict["IMAGIC.err3d"] = hed.err3d;
310 dict["IMAGIC.ref"] = hed.ref;
311 dict["IMAGIC.classno"] = hed.ref;
312 dict["IMAGIC.locold"] = hed.locold;
313 dict["IMAGIC.repqual"] = hed.repqual;
314 dict["IMAGIC.zshift"] = hed.zshift;
315 dict["IMAGIC.xshift"] = hed.xshift;
316 dict["IMAGIC.yshift"] = hed.yshift;
317 dict["IMAGIC.numcls"] = hed.numcls;
318 dict["IMAGIC.ovqual"] = hed.ovqual;
319 dict["IMAGIC.eangle"] = hed.eangle;
320 dict["IMAGIC.exshift"] = hed.exshift;
321 dict["IMAGIC.eyshift"] = hed.eyshift;
322 dict["IMAGIC.cmtotvar"] = hed.cmtotvar;
323 dict["IMAGIC.informat"] = hed.informat;
324 dict["IMAGIC.numeigen"] = hed.numeigen;
325 dict["IMAGIC.niactive"] = hed.niactive;
326 dict["apix_x"] = hed.resolx;
327 dict["apix_y"] = hed.resoly;
328 dict["apix_z"] = hed.resolz;
329 if(hed.errar==-1.0) {
330 dict["IMAGIC.fabosa1"] = hed.alpha2;
331 dict["IMAGIC.fabosa2"] = hed.beta2;
332 dict["IMAGIC.fabosa3"] = hed.gamma2;
333 }
334 else {
335 dict["IMAGIC.alpha2"] = hed.alpha2;
336 dict["IMAGIC.beta2"] = hed.beta2;
337 dict["IMAGIC.gamma2"] = hed.gamma2;
338 }
339 dict["IMAGIC.nmetric"] = hed.nmetric;
340 dict["IMAGIC.actmsa"] = hed.actmsa;
341
342 vector<float> v_coosmsa(hed.coosmsa, hed.coosmsa+69);
343 dict["IMAGIC.coosmsa"] = v_coosmsa;
344
345 dict["IMAGIC.eigval"] = hed.coosmsa[19];
346 dict["IMAGIC.history"] = hed.history+'\0';
347
348 dict["orientation_convention"] = "IMAGIC";
349 const float alpha = hed.alpha;
350 const float beta = hed.beta;
351 const float gamma = hed.gamma;
352 dict["euler_alpha"] = alpha;
353 dict["euler_beta"] = beta;
354 dict["euler_gamma"] = gamma;
355 Transform *trans = new Transform();
356 trans->set_rotation(Dict("type", "imagic", "alpha", alpha, "beta", beta, "gamma", gamma));
357 if( nz<=1 ) {
358 dict["xform.projection"] = trans;
359 }
360 else {
361 dict["xform.projection"] = trans;
362 dict["xform.align3d"] = trans;
363 }
364 Ctf * ctf_ = read_ctf(hed);
365 if( ctf_ != 0) {
366 dict["ctf"] = ctf_;
367 }
368 if(trans) {delete trans; trans=0;}
369 if(ctf_) {delete ctf_; ctf_=0;}
370
371 EXITFUNC;
372 return 0;
373}
374
376{
378
379 if (strncmp(name, "PACK",4) == 0) {
380 t = IMAGIC_CHAR;
381 }
382 else if (strncmp(name, "INTG",4) == 0) {
383 t = IMAGIC_SHORT;
384 }
385 else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
386 t = IMAGIC_FLOAT;
387 }
388 else if (strncmp(name, "COMP",4) == 0) {
390 }
391 else if (strncmp(name, "RECO",4) == 0) {
393 }
394 return t;
395}
396
398{
399 switch (t) {
400 case IMAGIC_CHAR:
401 return EMUtil::EM_CHAR;
402 case IMAGIC_SHORT:
403 return EMUtil::EM_SHORT;
404 case IMAGIC_FLOAT:
405 return EMUtil::EM_FLOAT;
409 default:
410 break;
411 }
412
413 return EMUtil::EM_UNKNOWN;
414}
415
417{
418 ENTERFUNC;
419
420 Ctf * ctf_ = 0;
421 size_t n = strlen(CTF_MAGIC);
422
423 if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
424 ctf_ = new EMAN1Ctf();
425 string header_label(hed.label);
426 // Note: this block was making things crash because it assumed the following if statement
427 // was true - I added the if statement (d.woolford)
428 if (header_label.size() > 2) {
429 string sctf = "O" + header_label.substr(2);
430 ctf_->from_string(sctf);
431 }
432 }
433
434 EXITFUNC;
435 return ctf_;
436}
437
438void ImagicIO2::write_ctf(const Ctf * const ctf, int image_index)
439{
440 ENTERFUNC;
441 init();
442
443 size_t n = strlen(CTF_MAGIC);
444 strcpy(imagich.label, CTF_MAGIC);
445 string ctf_ = ctf->to_string().substr(1);
446
447 //pad ctf_ to 80 char
448 if(ctf_.size()>80) {
449 ctf_ = ctf_.substr(0, 80);
450 }
451 else {
452 string padded(80 - ctf_.size(), ' ');
453 ctf_ = ctf_ + padded;
454 }
455
456 strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);
457
458 rewind(hed_file);
459 if (fwrite(&imagich, sizeof(Imagic4D), 1, hed_file) != 1) {
460 throw ImageWriteException(hed_filename, "Imagic Header");
461 }
462
463 EXITFUNC;
464}
465
467{
468 init();
470 return true;
471 }
472 return false;
473}
474
475void ImagicIO2::flush()
476{
477 fflush(img_file);
478 fflush(hed_file);
479}
480
481int ImagicIO2::write_header(EMAN::Dict const& dict, int image_index,
482 EMAN::Region const* area, EMUtil::EMDataType, bool use_host_endian)
483{
484 ENTERFUNC;
485
486 if(image_index<0) {
487 image_index = get_nimg();
488 }
489 check_write_access(rw_mode, image_index);
490
491 if (area) {
493 is_new_hed);
494 EXITFUNC;
495 return 0;
496 }
497
498 int nx = dict["nx"];
499 int ny = dict["ny"];
500 int nz = dict["nz"];
501 int nimg=0; //# images currently in file
502
503 if (!is_new_hed) {
505
506 if (imagich.nx != nx || imagich.ny != ny || imagich.izlp != nz) {
507 char desc[256];
508 sprintf(desc, "new IMAGIC size %dx%dx%d is not equal to existing size %dx%dx%d",
509 nx, ny, nz, imagich.nx, imagich.ny, imagich.izlp);
510 throw ImageWriteException(filename, desc);
511 }
512
513 if (datatype!=IMAGIC_FLOAT) {
514 throw ImageWriteException(filename, "Attempted write to non REAL Imagic file");
515 }
516
517 rewind(hed_file);
518 nimg=image_index+1;
519 }
520 else {
521 nimg = 1; //new file writing
522 }
523
524 Imagic4D new_hed;
525 memset(&new_hed, 0, sizeof(Imagic4D));
526
527 time_t cur_time = time(0);
528 struct tm *tm = localtime(&cur_time);
529
530 new_hed.error = 0;
531 new_hed.headrec = 1; //always 1 a the moment
532
533 new_hed.mday = tm->tm_mday;
534 new_hed.month = tm->tm_mon;
535 new_hed.year = tm->tm_year + 1900;
536 new_hed.hour = tm->tm_hour;
537 new_hed.minute = tm->tm_min;
538 new_hed.sec = tm->tm_sec;
539
540 size_t img_size = nx*ny*nz;
541 if(img_size > (size_t)INT_MAX) {
542 new_hed.rsize = -1;
543 }
544 else {
545 new_hed.rsize = (int)img_size;
546 }
547
548 new_hed.nx = nx;
549 new_hed.ny = ny;
550 new_hed.izlp = nz;
551
552 strncpy(new_hed.type, REAL_TYPE_MAGIC,4);
553 new_hed.avdens = (float)dict["mean"];
554 new_hed.sigma = (float)dict["sigma"];
555 new_hed.densmax = (float)dict["maximum"];
556 new_hed.densmin = (float)dict["minimum"];
557
558 new_hed.ixold = 0;
559 new_hed.iyold = 0;
560
561 string new_label = dict.has_key("IMAGIC.label") ? (string) dict["IMAGIC.label"] : string(79, ' ');
562 sprintf(new_hed.label, "%s", new_label.c_str() );
563
564 string new_history = dict.has_key("IMAGIC.history") ? (string) dict["IMAGIC.history"] : string(227, ' ');
565 new_history = new_history.substr(0, 228);
566 sprintf(new_hed.history, "%s", new_history.c_str());
567
568 new_hed.i4lp = nimg;
569
570 Transform * t = 0;
571 if(nz<=1 && dict.has_key("xform.projection")) {
572 t = dict["xform.projection"];
573 }
574 else if(nz>1 && dict.has_key("xform.align3d")) {
575 t = dict["xform.align3d"];
576 }
577
578 if(t) {
579 Dict d = t->get_rotation("imagic");
580 new_hed.alpha = d["alpha"];
581 new_hed.beta = d["beta"];
582 new_hed.gamma = d["gamma"];
583 delete t;
584 t=0;
585 }
586 else {
587 if(dict.has_key("euler_alpha")) new_hed.alpha = dict["euler_alpha"];
588 if(dict.has_key("euler_beta")) new_hed.beta = dict["euler_beta"];
589 if(dict.has_key("euler_gamma")) new_hed.gamma = dict["euler_gamma"];
590 }
591
592 new_hed.realtype = generate_machine_stamp();
593
594 new_hed.resolx = dict["apix_x"];
595 new_hed.resoly = dict["apix_y"];
596 new_hed.resolz = dict["apix_z"];
597
598 if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian) swap_header(new_hed);
599
600 // overwrite existing header if necessary
601 if (image_index>=0 && image_index<nimg) {
602 portable_fseek(hed_file, sizeof(Imagic4D)*image_index, SEEK_SET);
603 new_hed.imgnum=image_index+1;
605 ByteOrder::swap_bytes((int *) &new_hed.imgnum,1);
606 fwrite(&new_hed, sizeof(Imagic4D),1,hed_file);
607 }
608
609 // update the 1st header with total # images
610 int ifol = nimg-1;
612 ByteOrder::swap_bytes((int *) &nimg,1);
613 ByteOrder::swap_bytes((int *) &ifol,1);
614 }
615 portable_fseek(hed_file, sizeof(int), SEEK_SET);
616 fwrite(&ifol, sizeof(int), 1, hed_file);
617 portable_fseek(hed_file, 61*sizeof(int), SEEK_SET); //I4LP(62) is the number of "objects" in file
618 fwrite(&nimg, sizeof(int), 1, hed_file);
619
620 // header in machine order
621 if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian) swap_header(new_hed);
622 imagich=new_hed;
623 imagich.count=ifol;
624 is_new_hed = false;
625
626 if( dict.has_key("ctf") ) {
627 Ctf * ctf_ = dict["ctf"];
628 write_ctf(ctf_);
629 if(ctf_) {delete ctf_; ctf_=0;}
630 }
631
632 EXITFUNC;
633 return 0;
634}
635
637{
638 int machinestamp;
639
640#ifdef __sgi
641 machinestamp = SGI_IBM;
642#elif defined __OPENVMS__
643 machinestamp = VAX_VMS;
644#else
645 machinestamp = LINUX_WINDOWS;
646#endif
647
648 return machinestamp;
649}
650
652{
654 swap_header(hed);
655 }
656}
657
659{
663}
664
665int ImagicIO2::write_data(float* data, int image_index, const Region * area, EMAN::EMUtil::EMDataType, bool use_host_endian)
666{
667 ENTERFUNC;
668
669 check_write_access(rw_mode, image_index, 0, data);
671
672 if (image_index == -1) {
673 portable_fseek(img_file, 0, SEEK_END);
674 }
675 else {
676 size_t img_size = imagich.nx * imagich.ny * imagich.izlp * sizeof(float);
677 portable_fseek(img_file, img_size*image_index, SEEK_SET);
678 }
679
680 if(is_new_img) {
681 if(!use_host_endian) {
683 }
684 }
687 }
688
690 sizeof(float), imagich.nx, imagich.ny,
691 imagich.izlp, area, true);
692
693 EXITFUNC;
694 return 0;
695}
696
697int ImagicIO2::read_data(float* data, int image_index, EMAN::Region const* area, bool)
698{
699 ENTERFUNC;
700
701 check_read_access(image_index, data);
703
704 int nx = imagich.ny;
705 int ny = imagich.nx;
706 int nz = imagich.izlp ? imagich.izlp : 1;
707 size_t img_size = (size_t)nx*ny*nz;
708
709 check_region(area, FloatSize(nx, ny, nz), is_new_hed, false);
710
711 portable_fseek(img_file, img_size*image_index*sizeof(float), SEEK_SET);
712
713 short *sdata = (short *) data;
714 unsigned char *cdata = (unsigned char *) data;
715 size_t mode_size = get_datatype_size(datatype);
716
719 EMUtil::process_region_io(cdata, img_file, READ_ONLY, 0, mode_size, nx, ny, nz, area, true);
720
721 if (datatype == IMAGIC_FLOAT) {
722 become_host_endian(data, img_size);
723 }
724 else if (datatype == IMAGIC_SHORT) {
725 become_host_endian(sdata, img_size);
726
727 for (ptrdiff_t j = img_size - 1; j >= 0; --j) {
728 data[j] = static_cast < float >(sdata[j]);
729 }
730 }
731 else {
732 throw ImageReadException(filename, "unknown imagic data type");
733 }
734
735 EXITFUNC;
736 return 0;
737}
738
740{
741 init();
742 return imagich.count + 1;
743}
744
746{
747 init();
748 return is_big_endian;
749}
750
752{
753 size_t s = 0;
754 switch (t) {
755 case IMAGIC_CHAR:
756 s = sizeof(unsigned char);
757 break;
758 case IMAGIC_SHORT:
759 s = sizeof(unsigned short);
760 break;
761 case IMAGIC_FLOAT:
764 s = sizeof(float);
765 break;
766 default:
767 s = 0;
768 }
769
770 return s;
771}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
Definition: byteorder.h:131
static bool is_data_big_endian(const T *small_num_addr)
given a pointer to a reasonable small integer number, return whether the number is big endian or not.
Definition: byteorder.h:76
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual int from_string(const string &ctf)=0
virtual string to_string() const =0
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
bool has_key(const string &key) const
Ask the Dictionary if it as a particular key.
Definition: emobject.h:511
EMAN1Ctf is the CTF model used in EMAN1.
Definition: ctf.h:120
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
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104
@ EM_SHORT
Definition: emutil.h:96
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
FloatSize is used to describe a 1D, 2D or 3D rectangular size in floating numbers.
Definition: geometry.h:105
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
IOMode rw_mode
Definition: imageio.h:353
string filename
Definition: imageio.h:352
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
Definition: imageio.cpp:58
virtual void flush()=0
Flush the IO buffer.
virtual int write_header(const Dict &dict, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write a header to an image.
bool initialized
Definition: imageio.h:355
virtual int write_data(float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write data to an image.
virtual bool is_complex_mode()=0
Is this an complex image or not.
virtual int read_header(Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the header from an image.
FILE * sfopen(const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
Run fopen safely.
Definition: imageio.cpp:135
void check_read_access(int image_index)
Validate 'image_index' in file reading.
Definition: imageio.cpp:95
virtual int read_data(float *data, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the data from an image.
virtual void init()=0
Do some initialization before doing the read/write.
void check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
Definition: imageio.cpp:113
void become_host_endian(T *data, size_t n=1)
Convert data of this image into host endian format.
Definition: imageio.h:261
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
DataType datatype
Definition: imagicio2.h:259
static const char * IMG_EXT
Definition: imagicio2.h:73
int get_nimg()
Get number of images in this file.
Definition: imagicio2.cpp:739
FILE * img_file
Definition: imagicio2.h:252
static bool is_valid(const void *first_block)
Definition: imagicio2.cpp:161
static const char * REAL_TYPE_MAGIC
Definition: imagicio2.h:100
@ IMAGIC_FFT_FLOAT_COMPLEX
Definition: imagicio2.h:110
string hed_filename
Definition: imagicio2.h:248
void write_ctf(const Ctf *const ctf, int image_index=0)
Definition: imagicio2.cpp:438
bool is_big_endian
Definition: imagicio2.h:255
Ctf * read_ctf(const Imagic4D &hed) const
the Ctf object is a EMAN1Ctf object.
Definition: imagicio2.cpp:416
static const char * HED_EXT
Definition: imagicio2.h:72
Imagic4D imagich
Definition: imagicio2.h:254
void make_header_host_endian(Imagic4D &hed) const
Definition: imagicio2.cpp:651
string img_filename
Definition: imagicio2.h:249
int generate_machine_stamp() const
Definition: imagicio2.cpp:636
int to_em_datatype(DataType t) const
Definition: imagicio2.cpp:397
static const char * CTF_MAGIC
Definition: imagicio2.h:101
void swap_header(Imagic4D &hed) const
Definition: imagicio2.cpp:658
size_t get_datatype_size(DataType t) const
Definition: imagicio2.cpp:751
DataType get_datatype_from_name(const char *name) const
Definition: imagicio2.cpp:375
FILE * hed_file
Definition: imagicio2.h:251
@ NUM_4BYTES_AFTER_IXOLD
Definition: imagicio2.h:118
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
A Transform object is a somewhat specialized object designed specifically for EMAN2/Sparx storage of ...
Definition: transform.h:75
void set_rotation(const Dict &rotation)
Set a rotation using a specific Euler type and the dictionary interface Works for all Euler types.
Definition: transform.cpp:519
Dict get_rotation(const string &euler_type="eman") const
Get a rotation in any Euler format.
Definition: transform.cpp:829
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
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
Definition: emassert.h:42
#define ImageReadException(filename, desc)
Definition: exception.h:204
#define FileAccessException(filename)
Definition: exception.h:187
#define ImageWriteException(imagename, desc)
Definition: exception.h:223
#define LOGWARN
Definition: log.h:53
#define LOGERR
Definition: log.h:51
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
E2Exception class.
Definition: aligner.h:40
int portable_fseek(FILE *fp, off_t offset, int whence)
IMAGIC-4D file format http://www.imagescience.de/formats/formats.htm.
Definition: imagicio2.h:132